blob: 221d40332bb16ebb71fcf21b27faa5fcc7f20ddf [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)
Pavan Anamula973ba1c2015-11-20 15:23:47 +0530138#define MMC_BLK_PARTSWITCH BIT(5)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500139
140 /*
141 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200142 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500143 * track of the current selected device partition.
144 */
145 unsigned int part_curr;
146 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100147 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200148 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200149 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100150 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900155enum {
156 MMC_PACKED_NR_IDX = -1,
157 MMC_PACKED_NR_ZERO,
158 MMC_PACKED_NR_SINGLE,
159};
160
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400161module_param(perdev_minors, int, 0444);
162MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
163
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200164static inline int mmc_blk_part_switch(struct mmc_card *card,
165 struct mmc_blk_data *md);
166static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700167static int mmc_blk_cmdq_switch(struct mmc_card *card,
168 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200169
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900170static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
171{
172 struct mmc_packed *packed = mqrq->packed;
173
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900174 mqrq->cmd_type = MMC_PACKED_NONE;
175 packed->nr_entries = MMC_PACKED_NR_ZERO;
176 packed->idx_failure = MMC_PACKED_NR_IDX;
177 packed->retries = 0;
178 packed->blocks = 0;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
182{
183 struct mmc_blk_data *md;
184
Arjan van de Vena621aae2006-01-12 18:43:35 +0000185 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 md = disk->private_data;
187 if (md && md->usage == 0)
188 md = NULL;
189 if (md)
190 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000191 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return md;
194}
195
Andrei Warkentin371a6892011-04-11 18:10:25 -0500196static inline int mmc_get_devidx(struct gendisk *disk)
197{
Colin Cross382c55f2015-10-22 10:00:41 -0700198 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500199 return devidx;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static void mmc_blk_put(struct mmc_blk_data *md)
203{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000204 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 md->usage--;
206 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500207 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800208 blk_cleanup_queue(md->queue.queue);
209
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200210 spin_lock(&mmc_blk_lock);
211 ida_remove(&mmc_blk_ida, devidx);
212 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 kfree(md);
216 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000217 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Johan Rudholmadd710e2011-12-02 08:51:06 +0100220static ssize_t power_ro_lock_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
223 int ret;
224 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200225 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100226 int locked = 0;
227
Asutosh Das507d9a72014-12-09 10:15:53 +0200228 if (!md)
229 return -EINVAL;
230
231 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100232 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
233 locked = 2;
234 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
235 locked = 1;
236
237 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
238
Tomas Winkler9098f842015-07-16 15:50:45 +0200239 mmc_blk_put(md);
240
Johan Rudholmadd710e2011-12-02 08:51:06 +0100241 return ret;
242}
243
244static ssize_t power_ro_lock_store(struct device *dev,
245 struct device_attribute *attr, const char *buf, size_t count)
246{
247 int ret;
248 struct mmc_blk_data *md, *part_md;
249 struct mmc_card *card;
250 unsigned long set;
251
252 if (kstrtoul(buf, 0, &set))
253 return -EINVAL;
254
255 if (set != 1)
256 return count;
257
258 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200259 if (!md)
260 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100261 card = md->queue.card;
262
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200263 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100264
265 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
266 card->ext_csd.boot_ro_lock |
267 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
268 card->ext_csd.part_time);
269 if (ret)
270 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
271 else
272 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
273
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200274 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100275
276 if (!ret) {
277 pr_info("%s: Locking boot partition ro until next power on\n",
278 md->disk->disk_name);
279 set_disk_ro(md->disk, 1);
280
281 list_for_each_entry(part_md, &md->part, part)
282 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
283 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
284 set_disk_ro(part_md->disk, 1);
285 }
286 }
287
288 mmc_blk_put(md);
289 return count;
290}
291
Andrei Warkentin371a6892011-04-11 18:10:25 -0500292static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
293 char *buf)
294{
295 int ret;
296 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
297
Asutosh Das507d9a72014-12-09 10:15:53 +0200298 if (!md)
299 return -EINVAL;
300
Baruch Siach0031a982014-09-22 10:12:51 +0300301 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500302 get_disk_ro(dev_to_disk(dev)) ^
303 md->read_only);
304 mmc_blk_put(md);
305 return ret;
306}
307
308static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
309 const char *buf, size_t count)
310{
311 int ret;
312 char *end;
313 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
314 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200315
316 if (!md)
317 return -EINVAL;
318
Andrei Warkentin371a6892011-04-11 18:10:25 -0500319 if (end == buf) {
320 ret = -EINVAL;
321 goto out;
322 }
323
324 set_disk_ro(dev_to_disk(dev), set || md->read_only);
325 ret = count;
326out:
327 mmc_blk_put(md);
328 return ret;
329}
330
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200331static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200332no_pack_for_random_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
334{
335 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
336 int ret;
337
Asutosh Das507d9a72014-12-09 10:15:53 +0200338 if (!md)
339 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200340 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
341
342 mmc_blk_put(md);
343 return ret;
344}
345
346static ssize_t
347no_pack_for_random_store(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf, size_t count)
350{
351 int value;
352 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200353 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200354 int ret = count;
355
Asutosh Das507d9a72014-12-09 10:15:53 +0200356 if (!md)
357 return -EINVAL;
358
359 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200360 if (!card) {
361 ret = -EINVAL;
362 goto exit;
363 }
364
365 sscanf(buf, "%d", &value);
366
367 if (value < 0) {
368 pr_err("%s: value %d is not valid. old value remains = %d",
369 mmc_hostname(card->host), value,
370 md->queue.no_pack_for_random);
371 ret = -EINVAL;
372 goto exit;
373 }
374
375 md->queue.no_pack_for_random = (value > 0) ? true : false;
376
377 pr_debug("%s: no_pack_for_random: new value = %d",
378 mmc_hostname(card->host),
379 md->queue.no_pack_for_random);
380
381exit:
382 mmc_blk_put(md);
383 return ret;
384}
385
386static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200387num_wr_reqs_to_start_packing_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
389{
390 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
391 int num_wr_reqs_to_start_packing;
392 int ret;
393
Asutosh Das507d9a72014-12-09 10:15:53 +0200394 if (!md)
395 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200396 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
397
398 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
399
400 mmc_blk_put(md);
401 return ret;
402}
403
404static ssize_t
405num_wr_reqs_to_start_packing_store(struct device *dev,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
408{
409 int value;
410 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200411 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200412 int ret = count;
413
Asutosh Das507d9a72014-12-09 10:15:53 +0200414 if (!md)
415 return -EINVAL;
416
417 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200418 if (!card) {
419 ret = -EINVAL;
420 goto exit;
421 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200422
423 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200424
Yaniv Gardi42399822014-12-04 00:26:23 +0200425 if (value >= 0) {
426 md->queue.num_wr_reqs_to_start_packing =
427 min_t(int, value, (int)card->ext_csd.max_packed_writes);
428
429 pr_debug("%s: trigger to pack: new value = %d",
430 mmc_hostname(card->host),
431 md->queue.num_wr_reqs_to_start_packing);
432 } else {
433 pr_err("%s: value %d is not valid. old value remains = %d",
434 mmc_hostname(card->host), value,
435 md->queue.num_wr_reqs_to_start_packing);
436 ret = -EINVAL;
437 }
438
439exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200440 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200441 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200442}
443
Mark Salyzyn6904e432016-01-28 11:12:25 -0800444#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
445
446static int max_read_speed, max_write_speed, cache_size = 4;
447
448module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
449MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
450module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
451MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
452module_param(cache_size, int, S_IRUSR | S_IRGRP);
453MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
454
455/*
456 * helper macros and expectations:
457 * size - unsigned long number of bytes
458 * jiffies - unsigned long HZ timestamp difference
459 * speed - unsigned KB/s transfer rate
460 */
461#define size_and_speed_to_jiffies(size, speed) \
462 ((size) * HZ / (speed) / 1024UL)
463#define jiffies_and_speed_to_size(jiffies, speed) \
464 (((speed) * (jiffies) * 1024UL) / HZ)
465#define jiffies_and_size_to_speed(jiffies, size) \
466 ((size) * HZ / (jiffies) / 1024UL)
467
468/* Limits to report warning */
469/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
470#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
471#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
472
473#define speed_valid(speed) ((speed) > 0)
474
475static const char off[] = "off\n";
476
477static int max_speed_show(int speed, char *buf)
478{
479 if (speed)
480 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
481 else
482 return scnprintf(buf, PAGE_SIZE, off);
483}
484
485static int max_speed_store(const char *buf, struct request_queue *q)
486{
487 unsigned int limit, set = 0;
488
489 if (!strncasecmp(off, buf, sizeof(off) - 2))
490 return set;
491 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
492 return -EINVAL;
493 if (set == 0)
494 return set;
495 limit = MAX_SPEED(q);
496 if (set > limit)
497 pr_warn("max speed %u ineffective above %u\n", set, limit);
498 limit = MIN_SPEED(q);
499 if (set < limit)
500 pr_warn("max speed %u painful below %u\n", set, limit);
501 return set;
502}
503
504static ssize_t max_write_speed_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
508 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
509
510 mmc_blk_put(md);
511 return ret;
512}
513
514static ssize_t max_write_speed_store(struct device *dev,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
517{
518 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
519 int set = max_speed_store(buf, md->queue.queue);
520
521 if (set < 0) {
522 mmc_blk_put(md);
523 return set;
524 }
525
526 atomic_set(&md->queue.max_write_speed, set);
527 mmc_blk_put(md);
528 return count;
529}
530
531static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
532 max_write_speed_show, max_write_speed_store);
533
534static ssize_t max_read_speed_show(struct device *dev,
535 struct device_attribute *attr, char *buf)
536{
537 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
538 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
539
540 mmc_blk_put(md);
541 return ret;
542}
543
544static ssize_t max_read_speed_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
547{
548 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
549 int set = max_speed_store(buf, md->queue.queue);
550
551 if (set < 0) {
552 mmc_blk_put(md);
553 return set;
554 }
555
556 atomic_set(&md->queue.max_read_speed, set);
557 mmc_blk_put(md);
558 return count;
559}
560
561static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
562 max_read_speed_show, max_read_speed_store);
563
564static ssize_t cache_size_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
566{
567 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
568 struct mmc_queue *mq = &md->queue;
569 int cache_size = atomic_read(&mq->cache_size);
570 int ret;
571
572 if (!cache_size)
573 ret = scnprintf(buf, PAGE_SIZE, off);
574 else {
575 int speed = atomic_read(&mq->max_write_speed);
576
577 if (!speed_valid(speed))
578 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
579 else { /* We accept race between cache_jiffies and cache_used */
580 unsigned long size = jiffies_and_speed_to_size(
581 jiffies - mq->cache_jiffies, speed);
582 long used = atomic_long_read(&mq->cache_used);
583
584 if (size >= used)
585 size = 0;
586 else
587 size = (used - size) * 100 / cache_size
588 / 1024UL / 1024UL;
589
590 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
591 cache_size, size);
592 }
593 }
594
595 mmc_blk_put(md);
596 return ret;
597}
598
599static ssize_t cache_size_store(struct device *dev,
600 struct device_attribute *attr,
601 const char *buf, size_t count)
602{
603 struct mmc_blk_data *md;
604 unsigned int set = 0;
605
606 if (strncasecmp(off, buf, sizeof(off) - 2)
607 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
608 return -EINVAL;
609
610 md = mmc_blk_get(dev_to_disk(dev));
611 atomic_set(&md->queue.cache_size, set);
612 mmc_blk_put(md);
613 return count;
614}
615
616static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
617 cache_size_show, cache_size_store);
618
619/* correct for write-back */
620static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
621{
622 long used = 0;
623 int speed = atomic_read(&mq->max_write_speed);
624
625 if (speed_valid(speed)) {
626 unsigned long size = jiffies_and_speed_to_size(
627 waitfor - mq->cache_jiffies, speed);
628 used = atomic_long_read(&mq->cache_used);
629
630 if (size >= used)
631 used = 0;
632 else
633 used -= size;
634 }
635
636 atomic_long_set(&mq->cache_used, used);
637 mq->cache_jiffies = waitfor;
638
639 return used;
640}
641
642static void mmc_blk_simulate_delay(
643 struct mmc_queue *mq,
644 struct request *req,
645 unsigned long waitfor)
646{
647 int max_speed;
648
649 if (!req)
650 return;
651
652 max_speed = (rq_data_dir(req) == READ)
653 ? atomic_read(&mq->max_read_speed)
654 : atomic_read(&mq->max_write_speed);
655 if (speed_valid(max_speed)) {
656 unsigned long bytes = blk_rq_bytes(req);
657
658 if (rq_data_dir(req) != READ) {
659 int cache_size = atomic_read(&mq->cache_size);
660
661 if (cache_size) {
662 unsigned long size = cache_size * 1024L * 1024L;
663 long used = mmc_blk_cache_used(mq, waitfor);
664
665 used += bytes;
666 atomic_long_set(&mq->cache_used, used);
667 bytes = 0;
668 if (used > size)
669 bytes = used - size;
670 }
671 }
672 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
673 if (time_is_after_jiffies(waitfor)) {
674 long msecs = jiffies_to_msecs(waitfor - jiffies);
675
676 if (likely(msecs > 0))
677 msleep(msecs);
678 }
679 }
680}
681
682#else
683
684#define mmc_blk_simulate_delay(mq, req, waitfor)
685
686#endif
687
Al Viroa5a15612008-03-02 10:33:30 -0500688static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Al Viroa5a15612008-03-02 10:33:30 -0500690 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int ret = -ENXIO;
692
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200693 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (md) {
695 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500696 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700698
Al Viroa5a15612008-03-02 10:33:30 -0500699 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700700 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700701 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200704 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 return ret;
707}
708
Al Virodb2a1442013-05-05 21:52:57 -0400709static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Al Viroa5a15612008-03-02 10:33:30 -0500711 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200713 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200715 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800719mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800721 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
722 geo->heads = 4;
723 geo->sectors = 16;
724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
John Calixtocb87ea22011-04-26 18:56:29 -0400727struct mmc_blk_ioc_data {
728 struct mmc_ioc_cmd ic;
729 unsigned char *buf;
730 u64 buf_bytes;
731};
732
733static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
734 struct mmc_ioc_cmd __user *user)
735{
736 struct mmc_blk_ioc_data *idata;
737 int err;
738
yalin wang1ff89502015-11-12 19:27:11 +0800739 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400740 if (!idata) {
741 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400742 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400743 }
744
745 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
746 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400747 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400748 }
749
750 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
751 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
752 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400753 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400754 }
755
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300756 if (!idata->buf_bytes) {
757 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100758 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300759 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100760
yalin wang1ff89502015-11-12 19:27:11 +0800761 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400762 if (!idata->buf) {
763 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400764 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400765 }
766
767 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
768 idata->ic.data_ptr, idata->buf_bytes)) {
769 err = -EFAULT;
770 goto copy_err;
771 }
772
773 return idata;
774
775copy_err:
776 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400777idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400778 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400779out:
John Calixtocb87ea22011-04-26 18:56:29 -0400780 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400781}
782
Jon Huntera5f57742015-09-22 10:27:53 +0100783static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
784 struct mmc_blk_ioc_data *idata)
785{
786 struct mmc_ioc_cmd *ic = &idata->ic;
787
788 if (copy_to_user(&(ic_ptr->response), ic->response,
789 sizeof(ic->response)))
790 return -EFAULT;
791
792 if (!idata->ic.write_flag) {
793 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
794 idata->buf, idata->buf_bytes))
795 return -EFAULT;
796 }
797
798 return 0;
799}
800
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200801static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
802 u32 retries_max)
803{
804 int err;
805 u32 retry_count = 0;
806
807 if (!status || !retries_max)
808 return -EINVAL;
809
810 do {
811 err = get_card_status(card, status, 5);
812 if (err)
813 break;
814
815 if (!R1_STATUS(*status) &&
816 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
817 break; /* RPMB programming operation complete */
818
819 /*
820 * Rechedule to give the MMC device a chance to continue
821 * processing the previous command without being polled too
822 * frequently.
823 */
824 usleep_range(1000, 5000);
825 } while (++retry_count < retries_max);
826
827 if (retry_count == retries_max)
828 err = -EPERM;
829
830 return err;
831}
832
Maya Erez775a9362013-04-18 15:41:55 +0300833static int ioctl_do_sanitize(struct mmc_card *card)
834{
835 int err;
836
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530837 if (!mmc_can_sanitize(card) &&
838 (card->host->caps2 & MMC_CAP2_SANITIZE)) {
839 pr_warn("%s: %s - SANITIZE is not supported\n",
Maya Erez775a9362013-04-18 15:41:55 +0300840 mmc_hostname(card->host), __func__);
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530841 err = -EOPNOTSUPP;
842 goto out;
Maya Erez775a9362013-04-18 15:41:55 +0300843 }
844
845 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
846 mmc_hostname(card->host), __func__);
847
848 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
849 EXT_CSD_SANITIZE_START, 1,
850 MMC_SANITIZE_REQ_TIMEOUT);
851
852 if (err)
853 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
854 mmc_hostname(card->host), __func__, err);
855
856 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
857 __func__);
858out:
859 return err;
860}
861
Jon Huntera5f57742015-09-22 10:27:53 +0100862static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
863 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400864{
John Calixtocb87ea22011-04-26 18:56:29 -0400865 struct mmc_command cmd = {0};
866 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530867 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400868 struct scatterlist sg;
869 int err;
870
Jon Huntera5f57742015-09-22 10:27:53 +0100871 if (!card || !md || !idata)
872 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400873
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100874 cmd.opcode = idata->ic.opcode;
875 cmd.arg = idata->ic.arg;
876 cmd.flags = idata->ic.flags;
877
878 if (idata->buf_bytes) {
879 data.sg = &sg;
880 data.sg_len = 1;
881 data.blksz = idata->ic.blksz;
882 data.blocks = idata->ic.blocks;
883
884 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
885
886 if (idata->ic.write_flag)
887 data.flags = MMC_DATA_WRITE;
888 else
889 data.flags = MMC_DATA_READ;
890
891 /* data.flags must already be set before doing this. */
892 mmc_set_data_timeout(&data, card);
893
894 /* Allow overriding the timeout_ns for empirical tuning. */
895 if (idata->ic.data_timeout_ns)
896 data.timeout_ns = idata->ic.data_timeout_ns;
897
898 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
899 /*
900 * Pretend this is a data transfer and rely on the
901 * host driver to compute timeout. When all host
902 * drivers support cmd.cmd_timeout for R1B, this
903 * can be changed to:
904 *
905 * mrq.data = NULL;
906 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
907 */
908 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
909 }
910
911 mrq.data = &data;
912 }
913
914 mrq.cmd = &cmd;
915
Maya Erez70afbaf2015-10-01 11:27:17 +0300916 if (mmc_card_doing_bkops(card)) {
917 err = mmc_stop_bkops(card);
918 if (err) {
919 dev_err(mmc_dev(card->host),
920 "%s: stop_bkops failed %d\n", __func__, err);
921 return err;
922 }
923 }
924
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200925 err = mmc_blk_part_switch(card, md);
926 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100927 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200928
John Calixtocb87ea22011-04-26 18:56:29 -0400929 if (idata->ic.is_acmd) {
930 err = mmc_app_cmd(card->host, card);
931 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100932 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400933 }
934
Yaniv Gardia82e4842013-06-05 14:13:08 +0300935 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
936 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300937 err = ioctl_do_sanitize(card);
938
939 if (err)
940 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
941 __func__, err);
942
Jon Huntera5f57742015-09-22 10:27:53 +0100943 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300944 }
945
John Calixtocb87ea22011-04-26 18:56:29 -0400946 mmc_wait_for_req(card->host, &mrq);
947
948 if (cmd.error) {
949 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
950 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100951 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400952 }
953 if (data.error) {
954 dev_err(mmc_dev(card->host), "%s: data error %d\n",
955 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100956 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400957 }
958
959 /*
960 * According to the SD specs, some commands require a delay after
961 * issuing the command.
962 */
963 if (idata->ic.postsleep_min_us)
964 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
965
Jon Huntera5f57742015-09-22 10:27:53 +0100966 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400967
Krishna Kondae6711632014-12-04 15:20:57 +0200968 return err;
969}
970
971struct mmc_blk_ioc_rpmb_data {
972 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
973};
974
975static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
976 struct mmc_ioc_rpmb __user *user)
977{
978 struct mmc_blk_ioc_rpmb_data *idata;
979 int err, i;
980
981 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
982 if (!idata) {
983 err = -ENOMEM;
984 goto out;
985 }
986
987 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
988 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
989 if (IS_ERR(idata->data[i])) {
990 err = PTR_ERR(idata->data[i]);
991 goto copy_err;
992 }
993 }
994
995 return idata;
996
997copy_err:
998 while (--i >= 0) {
999 kfree(idata->data[i]->buf);
1000 kfree(idata->data[i]);
1001 }
1002 kfree(idata);
1003out:
1004 return ERR_PTR(err);
1005}
1006
1007static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
1008 struct mmc_ioc_rpmb __user *ic_ptr)
1009{
1010 struct mmc_blk_ioc_rpmb_data *idata;
1011 struct mmc_blk_data *md;
Subhash Jadavani692984f2015-10-19 12:04:46 -07001012 struct mmc_card *card = NULL;
Krishna Kondae6711632014-12-04 15:20:57 +02001013 struct mmc_command cmd = {0};
1014 struct mmc_data data = {0};
1015 struct mmc_request mrq = {NULL};
1016 struct scatterlist sg;
1017 int err = 0, i = 0;
1018 u32 status = 0;
1019
1020 /* The caller must have CAP_SYS_RAWIO */
1021 if (!capable(CAP_SYS_RAWIO))
1022 return -EPERM;
1023
1024 md = mmc_blk_get(bdev->bd_disk);
1025 /* make sure this is a rpmb partition */
1026 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1027 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001028 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001029 }
1030
1031 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1032 if (IS_ERR(idata)) {
1033 err = PTR_ERR(idata);
1034 goto cmd_done;
1035 }
1036
1037 card = md->queue.card;
1038 if (IS_ERR(card)) {
1039 err = PTR_ERR(card);
1040 goto idata_free;
1041 }
1042
Maya Erezdd669562015-02-12 20:37:31 +02001043 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001044
Maya Erez70afbaf2015-10-01 11:27:17 +03001045 if (mmc_card_doing_bkops(card)) {
1046 if (mmc_card_cmdq(card)) {
1047 err = mmc_cmdq_halt(card->host, true);
1048 if (err)
1049 goto cmd_rel_host;
1050 }
1051 err = mmc_stop_bkops(card);
1052 if (err) {
1053 dev_err(mmc_dev(card->host),
1054 "%s: stop_bkops failed %d\n", __func__, err);
1055 goto cmd_rel_host;
1056 }
1057 if (mmc_card_cmdq(card)) {
1058 err = mmc_cmdq_halt(card->host, false);
1059 if (err)
1060 goto cmd_rel_host;
1061 }
1062 }
1063
Krishna Kondae6711632014-12-04 15:20:57 +02001064 err = mmc_blk_part_switch(card, md);
1065 if (err)
1066 goto cmd_rel_host;
1067
1068 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1069 struct mmc_blk_ioc_data *curr_data;
1070 struct mmc_ioc_cmd *curr_cmd;
1071
1072 curr_data = idata->data[i];
1073 curr_cmd = &curr_data->ic;
1074 if (!curr_cmd->opcode)
1075 break;
1076
1077 cmd.opcode = curr_cmd->opcode;
1078 cmd.arg = curr_cmd->arg;
1079 cmd.flags = curr_cmd->flags;
1080
1081 if (curr_data->buf_bytes) {
1082 data.sg = &sg;
1083 data.sg_len = 1;
1084 data.blksz = curr_cmd->blksz;
1085 data.blocks = curr_cmd->blocks;
1086
1087 sg_init_one(data.sg, curr_data->buf,
1088 curr_data->buf_bytes);
1089
1090 if (curr_cmd->write_flag)
1091 data.flags = MMC_DATA_WRITE;
1092 else
1093 data.flags = MMC_DATA_READ;
1094
1095 /* data.flags must already be set before doing this. */
1096 mmc_set_data_timeout(&data, card);
1097
1098 /*
1099 * Allow overriding the timeout_ns for empirical tuning.
1100 */
1101 if (curr_cmd->data_timeout_ns)
1102 data.timeout_ns = curr_cmd->data_timeout_ns;
1103
1104 mrq.data = &data;
1105 }
1106
1107 mrq.cmd = &cmd;
1108
1109 err = mmc_set_blockcount(card, data.blocks,
1110 curr_cmd->write_flag & (1 << 31));
1111 if (err)
1112 goto cmd_rel_host;
1113
1114 mmc_wait_for_req(card->host, &mrq);
1115
1116 if (cmd.error) {
1117 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1118 __func__, cmd.error);
1119 err = cmd.error;
1120 goto cmd_rel_host;
1121 }
1122 if (data.error) {
1123 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1124 __func__, data.error);
1125 err = data.error;
1126 goto cmd_rel_host;
1127 }
1128
1129 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1130 sizeof(cmd.resp))) {
1131 err = -EFAULT;
1132 goto cmd_rel_host;
1133 }
1134
1135 if (!curr_cmd->write_flag) {
1136 if (copy_to_user((void __user *)(unsigned long)
1137 curr_cmd->data_ptr,
1138 curr_data->buf,
1139 curr_data->buf_bytes)) {
1140 err = -EFAULT;
1141 goto cmd_rel_host;
1142 }
1143 }
1144
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001145 /*
1146 * Ensure RPMB command has completed by polling CMD13
1147 * "Send Status".
1148 */
1149 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1150 if (err)
1151 dev_err(mmc_dev(card->host),
1152 "%s: Card Status=0x%08X, error %d\n",
1153 __func__, status, err);
1154 }
1155
Krishna Kondae6711632014-12-04 15:20:57 +02001156cmd_rel_host:
1157 mmc_put_card(card);
1158
1159idata_free:
1160 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1161 kfree(idata->data[i]->buf);
1162 kfree(idata->data[i]);
1163 }
1164 kfree(idata);
1165
1166cmd_done:
1167 mmc_blk_put(md);
Subhash Jadavani692984f2015-10-19 12:04:46 -07001168 if (card && card->cmdq_init)
1169 wake_up(&card->host->cmdq_ctx.wait);
Jon Huntera5f57742015-09-22 10:27:53 +01001170 return err;
1171}
1172
1173static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1174 struct mmc_ioc_cmd __user *ic_ptr)
1175{
1176 struct mmc_blk_ioc_data *idata;
1177 struct mmc_blk_data *md;
1178 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001179 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001180
Shawn Lin83c742c2016-03-16 18:15:47 +08001181 /*
1182 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1183 * whole block device, not on a partition. This prevents overspray
1184 * between sibling partitions.
1185 */
1186 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1187 return -EPERM;
1188
Jon Huntera5f57742015-09-22 10:27:53 +01001189 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301190 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001191 return PTR_ERR(idata);
1192
1193 md = mmc_blk_get(bdev->bd_disk);
1194 if (!md) {
1195 err = -EINVAL;
1196 goto cmd_err;
1197 }
1198
1199 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301200 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001201 err = PTR_ERR(card);
1202 goto cmd_done;
1203 }
1204
1205 mmc_get_card(card);
1206
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301207 if (mmc_card_cmdq(card)) {
1208 err = mmc_cmdq_halt_on_empty_queue(card->host);
1209 if (err) {
1210 pr_err("%s: halt failed while doing %s err (%d)\n",
1211 mmc_hostname(card->host),
1212 __func__, err);
1213 mmc_put_card(card);
1214 goto cmd_done;
1215 }
1216 }
1217
Grant Grundlerb0934102015-09-23 18:30:33 -07001218 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001219
Adrian Hunter3c866562016-05-04 14:38:12 +03001220 /* Always switch back to main area after RPMB access */
1221 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1222 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1223
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001224 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001225
Grant Grundlerb0934102015-09-23 18:30:33 -07001226 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001227
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301228 if (mmc_card_cmdq(card)) {
1229 if (mmc_cmdq_halt(card->host, false))
1230 pr_err("%s: %s: cmdq unhalt failed\n",
1231 mmc_hostname(card->host), __func__);
1232 }
1233
John Calixtocb87ea22011-04-26 18:56:29 -04001234cmd_done:
1235 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001236cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001237 kfree(idata->buf);
1238 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001239 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001240}
1241
Jon Huntera5f57742015-09-22 10:27:53 +01001242static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1243 struct mmc_ioc_multi_cmd __user *user)
1244{
1245 struct mmc_blk_ioc_data **idata = NULL;
1246 struct mmc_ioc_cmd __user *cmds = user->cmds;
1247 struct mmc_card *card;
1248 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001249 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001250 __u64 num_of_cmds;
1251
Shawn Lin83c742c2016-03-16 18:15:47 +08001252 /*
1253 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1254 * whole block device, not on a partition. This prevents overspray
1255 * between sibling partitions.
1256 */
1257 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1258 return -EPERM;
1259
Jon Huntera5f57742015-09-22 10:27:53 +01001260 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1261 sizeof(num_of_cmds)))
1262 return -EFAULT;
1263
1264 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1265 return -EINVAL;
1266
1267 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1268 if (!idata)
1269 return -ENOMEM;
1270
1271 for (i = 0; i < num_of_cmds; i++) {
1272 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1273 if (IS_ERR(idata[i])) {
1274 err = PTR_ERR(idata[i]);
1275 num_of_cmds = i;
1276 goto cmd_err;
1277 }
1278 }
1279
1280 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001281 if (!md) {
1282 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001283 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001284 }
Jon Huntera5f57742015-09-22 10:27:53 +01001285
1286 card = md->queue.card;
1287 if (IS_ERR(card)) {
1288 err = PTR_ERR(card);
1289 goto cmd_done;
1290 }
1291
1292 mmc_get_card(card);
1293
Grant Grundlerb0934102015-09-23 18:30:33 -07001294 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1295 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001296
Adrian Hunter3c866562016-05-04 14:38:12 +03001297 /* Always switch back to main area after RPMB access */
1298 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1299 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1300
Jon Huntera5f57742015-09-22 10:27:53 +01001301 mmc_put_card(card);
1302
1303 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001304 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001305 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001306
1307cmd_done:
1308 mmc_blk_put(md);
1309cmd_err:
1310 for (i = 0; i < num_of_cmds; i++) {
1311 kfree(idata[i]->buf);
1312 kfree(idata[i]);
1313 }
1314 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001315 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001316}
1317
John Calixtocb87ea22011-04-26 18:56:29 -04001318static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1319 unsigned int cmd, unsigned long arg)
1320{
Jon Huntera5f57742015-09-22 10:27:53 +01001321 switch (cmd) {
1322 case MMC_IOC_CMD:
1323 return mmc_blk_ioctl_cmd(bdev,
1324 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001325 case MMC_IOC_RPMB_CMD:
1326 return mmc_blk_ioctl_rpmb_cmd(bdev,
1327 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001328 case MMC_IOC_MULTI_CMD:
1329 return mmc_blk_ioctl_multi_cmd(bdev,
1330 (struct mmc_ioc_multi_cmd __user *)arg);
1331 default:
1332 return -EINVAL;
1333 }
John Calixtocb87ea22011-04-26 18:56:29 -04001334}
1335
1336#ifdef CONFIG_COMPAT
1337static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1338 unsigned int cmd, unsigned long arg)
1339{
1340 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1341}
1342#endif
1343
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001344static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001345 .open = mmc_blk_open,
1346 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001347 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001349 .ioctl = mmc_blk_ioctl,
1350#ifdef CONFIG_COMPAT
1351 .compat_ioctl = mmc_blk_compat_ioctl,
1352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353};
1354
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001355static int mmc_blk_cmdq_switch(struct mmc_card *card,
1356 struct mmc_blk_data *md, bool enable)
1357{
1358 int ret = 0;
1359 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301360 struct mmc_host *host = card->host;
1361 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001362
1363 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1364 !card->ext_csd.cmdq_support ||
1365 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1366 (cmdq_mode == enable))
1367 return 0;
1368
1369 if (enable) {
1370 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1371 if (ret) {
1372 pr_err("%s: failed (%d) to set block-size to %d\n",
1373 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1374 goto out;
1375 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301376
1377 } else {
1378 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1379 ret = mmc_cmdq_halt(host, true);
1380 if (ret) {
1381 pr_err("%s: halt: failed: %d\n",
1382 mmc_hostname(host), ret);
1383 goto out;
1384 }
1385 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001386 }
1387
1388 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1389 EXT_CSD_CMDQ, enable,
1390 card->ext_csd.generic_cmd6_time);
1391 if (ret) {
1392 pr_err("%s: cmdq mode %sable failed %d\n",
1393 md->disk->disk_name, enable ? "en" : "dis", ret);
1394 goto out;
1395 }
1396
1397 if (enable)
1398 mmc_card_set_cmdq(card);
1399 else
1400 mmc_card_clr_cmdq(card);
1401out:
1402 return ret;
1403}
1404
Andrei Warkentin371a6892011-04-11 18:10:25 -05001405static inline int mmc_blk_part_switch(struct mmc_card *card,
1406 struct mmc_blk_data *md)
1407{
1408 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001409 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001410
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001411 if ((main_md->part_curr == md->part_type) &&
1412 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001413 return 0;
1414
1415 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001416 u8 part_config = card->ext_csd.part_config;
1417
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001418 if (md->part_type) {
1419 /* disable CQ mode for non-user data partitions */
1420 ret = mmc_blk_cmdq_switch(card, md, false);
1421 if (ret)
1422 return ret;
1423 }
1424
Adrian Hunter57da0c02016-05-04 14:38:13 +03001425 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1426 mmc_retune_pause(card->host);
1427
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001428 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1429 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001430
1431 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001432 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001433 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001434 if (ret) {
Pavan Anamula973ba1c2015-11-20 15:23:47 +05301435 pr_err("%s: mmc_blk_part_switch failure, %d -> %d\n",
1436 mmc_hostname(card->host), main_md->part_curr,
1437 md->part_type);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001438 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1439 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001440 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001441 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001442
1443 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001444 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001445
1446 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1447 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001448 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001449
1450 main_md->part_curr = md->part_type;
1451 return 0;
1452}
1453
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001454static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1455{
1456 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001457 u32 result;
1458 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001459
Venkatraman Sad5fd972011-08-25 00:30:50 +05301460 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001461 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001462 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001463
1464 struct scatterlist sg;
1465
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001466 cmd.opcode = MMC_APP_CMD;
1467 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001468 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001469
1470 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001471 if (err)
1472 return (u32)-1;
1473 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001474 return (u32)-1;
1475
1476 memset(&cmd, 0, sizeof(struct mmc_command));
1477
1478 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1479 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001480 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001481
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001482 data.blksz = 4;
1483 data.blocks = 1;
1484 data.flags = MMC_DATA_READ;
1485 data.sg = &sg;
1486 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301487 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001488
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001489 mrq.cmd = &cmd;
1490 mrq.data = &data;
1491
Ben Dooks051913d2009-06-08 23:33:57 +01001492 blocks = kmalloc(4, GFP_KERNEL);
1493 if (!blocks)
1494 return (u32)-1;
1495
1496 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001497
1498 mmc_wait_for_req(card->host, &mrq);
1499
Ben Dooks051913d2009-06-08 23:33:57 +01001500 result = ntohl(*blocks);
1501 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001502
Ben Dooks051913d2009-06-08 23:33:57 +01001503 if (cmd.error || data.error)
1504 result = (u32)-1;
1505
1506 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001507}
1508
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001509static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001510{
Chris Ball1278dba2011-04-13 23:40:30 -04001511 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001512 int err;
1513
Adrian Hunter504f1912008-10-16 12:55:25 +03001514 cmd.opcode = MMC_SEND_STATUS;
1515 if (!mmc_host_is_spi(card->host))
1516 cmd.arg = card->rca << 16;
1517 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001518 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1519 if (err == 0)
1520 *status = cmd.resp[0];
1521 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001522}
1523
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001524static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001525 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001526{
1527 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1528 int err = 0;
1529 u32 status;
1530
1531 do {
1532 err = get_card_status(card, &status, 5);
1533 if (err) {
1534 pr_err("%s: error %d requesting status\n",
1535 req->rq_disk->disk_name, err);
1536 return err;
1537 }
1538
1539 if (status & R1_ERROR) {
1540 pr_err("%s: %s: error sending status cmd, status %#x\n",
1541 req->rq_disk->disk_name, __func__, status);
1542 *gen_err = 1;
1543 }
1544
Ulf Hansson95a91292014-01-29 13:11:27 +01001545 /* We may rely on the host hw to handle busy detection.*/
1546 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1547 hw_busy_detect)
1548 break;
1549
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001550 /*
1551 * Timeout if the device never becomes ready for data and never
1552 * leaves the program state.
1553 */
1554 if (time_after(jiffies, timeout)) {
1555 pr_err("%s: Card stuck in programming state! %s %s\n",
1556 mmc_hostname(card->host),
1557 req->rq_disk->disk_name, __func__);
1558 return -ETIMEDOUT;
1559 }
1560
1561 /*
1562 * Some cards mishandle the status bits,
1563 * so make sure to check both the busy
1564 * indication and the card state.
1565 */
1566 } while (!(status & R1_READY_FOR_DATA) ||
1567 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1568
1569 return err;
1570}
1571
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001572static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1573 struct request *req, int *gen_err, u32 *stop_status)
1574{
1575 struct mmc_host *host = card->host;
1576 struct mmc_command cmd = {0};
1577 int err;
1578 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1579
1580 /*
1581 * Normally we use R1B responses for WRITE, but in cases where the host
1582 * has specified a max_busy_timeout we need to validate it. A failure
1583 * means we need to prevent the host from doing hw busy detection, which
1584 * is done by converting to a R1 response instead.
1585 */
1586 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1587 use_r1b_resp = false;
1588
1589 cmd.opcode = MMC_STOP_TRANSMISSION;
1590 if (use_r1b_resp) {
1591 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1592 cmd.busy_timeout = timeout_ms;
1593 } else {
1594 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1595 }
1596
1597 err = mmc_wait_for_cmd(host, &cmd, 5);
1598 if (err)
1599 return err;
1600
1601 *stop_status = cmd.resp[0];
1602
1603 /* No need to check card status in case of READ. */
1604 if (rq_data_dir(req) == READ)
1605 return 0;
1606
1607 if (!mmc_host_is_spi(host) &&
1608 (*stop_status & R1_ERROR)) {
1609 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1610 req->rq_disk->disk_name, __func__, *stop_status);
1611 *gen_err = 1;
1612 }
1613
1614 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1615}
1616
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301617#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001618#define ERR_RETRY 2
1619#define ERR_ABORT 1
1620#define ERR_CONTINUE 0
1621
1622static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1623 bool status_valid, u32 status)
1624{
1625 switch (error) {
1626 case -EILSEQ:
1627 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001628 pr_err_ratelimited(
1629 "%s: response CRC error sending %s command, card status %#x\n",
1630 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001631 name, status);
1632 return ERR_RETRY;
1633
1634 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001635 pr_err_ratelimited(
1636 "%s: timed out sending %s command, card status %#x\n",
1637 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001638
1639 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301640 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001641 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301642 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001643 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301644 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001645
1646 /*
1647 * If it was a r/w cmd crc error, or illegal command
1648 * (eg, issued in wrong state) then retry - we should
1649 * have corrected the state problem above.
1650 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301651 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001652 pr_err_ratelimited(
1653 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301654 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001655 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301656 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001657
1658 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001659 pr_err_ratelimited(
1660 "%s: not retrying timeout\n",
1661 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 return ERR_ABORT;
1663
1664 default:
1665 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001666 pr_err_ratelimited(
1667 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001668 req->rq_disk->disk_name, error, status);
1669 return ERR_ABORT;
1670 }
1671}
1672
1673/*
1674 * Initial r/w and stop cmd error recovery.
1675 * We don't know whether the card received the r/w cmd or not, so try to
1676 * restore things back to a sane state. Essentially, we do this as follows:
1677 * - Obtain card status. If the first attempt to obtain card status fails,
1678 * the status word will reflect the failed status cmd, not the failed
1679 * r/w cmd. If we fail to obtain card status, it suggests we can no
1680 * longer communicate with the card.
1681 * - Check the card state. If the card received the cmd but there was a
1682 * transient problem with the response, it might still be in a data transfer
1683 * mode. Try to send it a stop command. If this fails, we can't recover.
1684 * - If the r/w cmd failed due to a response CRC error, it was probably
1685 * transient, so retry the cmd.
1686 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1687 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1688 * illegal cmd, retry.
1689 * Otherwise we don't understand what happened, so abort.
1690 */
1691static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001692 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001693{
1694 bool prev_cmd_status_valid = true;
1695 u32 status, stop_status = 0;
1696 int err, retry;
1697
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301698 if (mmc_card_removed(card))
1699 return ERR_NOMEDIUM;
1700
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001701 /*
1702 * Try to get card status which indicates both the card state
1703 * and why there was no response. If the first attempt fails,
1704 * we can't be sure the returned status is for the r/w command.
1705 */
1706 for (retry = 2; retry >= 0; retry--) {
1707 err = get_card_status(card, &status, 0);
1708 if (!err)
1709 break;
1710
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001711 /* Re-tune if needed */
1712 mmc_retune_recheck(card->host);
1713
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001714 prev_cmd_status_valid = false;
1715 pr_err("%s: error %d sending status command, %sing\n",
1716 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1717 }
1718
1719 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301720 if (err) {
1721 /* Check if the card is removed */
1722 if (mmc_detect_card_removed(card->host))
1723 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001724 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301725 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001726
Adrian Hunter67716322011-08-29 16:42:15 +03001727 /* Flag ECC errors */
1728 if ((status & R1_CARD_ECC_FAILED) ||
1729 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1730 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1731 *ecc_err = 1;
1732
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001733 /* Flag General errors */
1734 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1735 if ((status & R1_ERROR) ||
1736 (brq->stop.resp[0] & R1_ERROR)) {
1737 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1738 req->rq_disk->disk_name, __func__,
1739 brq->stop.resp[0], status);
1740 *gen_err = 1;
1741 }
1742
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001743 /*
1744 * Check the current card state. If it is in some data transfer
1745 * mode, tell it to stop (and hopefully transition back to TRAN.)
1746 */
1747 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1748 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001749 err = send_stop(card,
1750 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1751 req, gen_err, &stop_status);
1752 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001753 pr_err("%s: error %d sending stop command\n",
1754 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001755 /*
1756 * If the stop cmd also timed out, the card is probably
1757 * not present, so abort. Other errors are bad news too.
1758 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001759 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001760 }
1761
Adrian Hunter67716322011-08-29 16:42:15 +03001762 if (stop_status & R1_CARD_ECC_FAILED)
1763 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001764 }
1765
1766 /* Check for set block count errors */
1767 if (brq->sbc.error)
1768 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1769 prev_cmd_status_valid, status);
1770
1771 /* Check for r/w command errors */
1772 if (brq->cmd.error)
1773 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1774 prev_cmd_status_valid, status);
1775
Adrian Hunter67716322011-08-29 16:42:15 +03001776 /* Data errors */
1777 if (!brq->stop.error)
1778 return ERR_CONTINUE;
1779
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001780 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001781 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 +01001782 req->rq_disk->disk_name, brq->stop.error,
1783 brq->cmd.resp[0], status);
1784
1785 /*
1786 * Subsitute in our own stop status as this will give the error
1787 * state which happened during the execution of the r/w command.
1788 */
1789 if (stop_status) {
1790 brq->stop.resp[0] = stop_status;
1791 brq->stop.error = 0;
1792 }
1793 return ERR_CONTINUE;
1794}
1795
Adrian Hunter67716322011-08-29 16:42:15 +03001796static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1797 int type)
1798{
1799 int err;
1800
1801 if (md->reset_done & type)
1802 return -EEXIST;
1803
1804 md->reset_done |= type;
1805 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301806 if (err && err != -EOPNOTSUPP) {
1807 /* We failed to reset so we need to abort the request */
1808 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1809 __func__, err);
1810 return -ENODEV;
1811 }
1812
Adrian Hunter67716322011-08-29 16:42:15 +03001813 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301814 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001815 struct mmc_blk_data *main_md =
1816 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001817 int part_err;
1818
1819 main_md->part_curr = main_md->part_type;
1820 part_err = mmc_blk_part_switch(host->card, md);
1821 if (part_err) {
1822 /*
1823 * We have failed to get back into the correct
1824 * partition, so we need to abort the whole request.
1825 */
1826 return -ENODEV;
1827 }
1828 }
1829 return err;
1830}
1831
1832static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1833{
1834 md->reset_done &= ~type;
1835}
1836
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001837int mmc_access_rpmb(struct mmc_queue *mq)
1838{
1839 struct mmc_blk_data *md = mq->data;
1840 /*
1841 * If this is a RPMB partition access, return ture
1842 */
1843 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1844 return true;
1845
1846 return false;
1847}
1848
Sahitya Tummala9433a132015-06-09 09:38:36 +05301849static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1850 struct request *req)
1851{
1852 struct mmc_blk_data *md = mq->data;
1853 struct mmc_card *card = md->queue.card;
1854 struct mmc_host *host = card->host;
1855 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1856 struct mmc_cmdq_req *cmdq_req;
1857 struct mmc_queue_req *active_mqrq;
1858
1859 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1860 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1861
1862 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1863
1864 active_mqrq = &mq->mqrq_cmdq[req->tag];
1865 active_mqrq->req = req;
1866
1867 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1868 cmdq_req->cmdq_req_flags |= QBR;
1869 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1870 cmdq_req->tag = req->tag;
1871 return cmdq_req;
1872}
1873
1874static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1875 struct request *req)
1876{
1877 struct mmc_blk_data *md = mq->data;
1878 struct mmc_card *card = md->queue.card;
1879 struct mmc_cmdq_req *cmdq_req = NULL;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301880 unsigned int from, nr, arg;
1881 int err = 0;
1882
1883 if (!mmc_can_erase(card)) {
1884 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301885 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301886 goto out;
1887 }
1888
1889 from = blk_rq_pos(req);
1890 nr = blk_rq_sectors(req);
1891
1892 if (mmc_can_discard(card))
1893 arg = MMC_DISCARD_ARG;
1894 else if (mmc_can_trim(card))
1895 arg = MMC_TRIM_ARG;
1896 else
1897 arg = MMC_ERASE_ARG;
1898
1899 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1900 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1901 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1902 EXT_CSD_CMD_SET_NORMAL,
1903 INAND_CMD38_ARG_EXT_CSD,
1904 arg == MMC_TRIM_ARG ?
1905 INAND_CMD38_ARG_TRIM :
1906 INAND_CMD38_ARG_ERASE,
1907 0, true, false);
1908 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1909 if (err)
1910 goto clear_dcmd;
1911 }
1912 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1913clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301914 mmc_host_clk_hold(card->host);
1915 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301916out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05301917 return err ? 1 : 0;
1918}
1919
Adrian Hunterbd788c92010-08-11 14:17:47 -07001920static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1921{
1922 struct mmc_blk_data *md = mq->data;
1923 struct mmc_card *card = md->queue.card;
1924 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001925 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001926
Adrian Hunterbd788c92010-08-11 14:17:47 -07001927 if (!mmc_can_erase(card)) {
1928 err = -EOPNOTSUPP;
1929 goto out;
1930 }
1931
1932 from = blk_rq_pos(req);
1933 nr = blk_rq_sectors(req);
1934
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001935 if (mmc_can_discard(card))
1936 arg = MMC_DISCARD_ARG;
1937 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001938 arg = MMC_TRIM_ARG;
1939 else
1940 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001941retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001942 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1943 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1944 INAND_CMD38_ARG_EXT_CSD,
1945 arg == MMC_TRIM_ARG ?
1946 INAND_CMD38_ARG_TRIM :
1947 INAND_CMD38_ARG_ERASE,
1948 0);
1949 if (err)
1950 goto out;
1951 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001952 err = mmc_erase(card, from, nr, arg);
1953out:
Adrian Hunter67716322011-08-29 16:42:15 +03001954 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1955 goto retry;
1956 if (!err)
1957 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301958 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001959
Adrian Hunterbd788c92010-08-11 14:17:47 -07001960 return err ? 0 : 1;
1961}
1962
Sahitya Tummala9433a132015-06-09 09:38:36 +05301963static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1964 struct request *req)
1965{
1966 struct mmc_blk_data *md = mq->data;
1967 struct mmc_card *card = md->queue.card;
1968 struct mmc_cmdq_req *cmdq_req = NULL;
1969 unsigned int from, nr, arg;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301970 int err = 0;
1971
1972 if (!(mmc_can_secure_erase_trim(card))) {
1973 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301974 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301975 goto out;
1976 }
1977
1978 from = blk_rq_pos(req);
1979 nr = blk_rq_sectors(req);
1980
1981 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1982 arg = MMC_SECURE_TRIM1_ARG;
1983 else
1984 arg = MMC_SECURE_ERASE_ARG;
1985
1986 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1987 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1988 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1989 EXT_CSD_CMD_SET_NORMAL,
1990 INAND_CMD38_ARG_EXT_CSD,
1991 arg == MMC_SECURE_TRIM1_ARG ?
1992 INAND_CMD38_ARG_SECTRIM1 :
1993 INAND_CMD38_ARG_SECERASE,
1994 0, true, false);
1995 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1996 if (err)
1997 goto clear_dcmd;
1998 }
1999
2000 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
2001 if (err)
2002 goto clear_dcmd;
2003
2004 if (arg == MMC_SECURE_TRIM1_ARG) {
2005 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2006 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
2007 EXT_CSD_CMD_SET_NORMAL,
2008 INAND_CMD38_ARG_EXT_CSD,
2009 INAND_CMD38_ARG_SECTRIM2,
2010 0, true, false);
2011 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2012 if (err)
2013 goto clear_dcmd;
2014 }
2015
2016 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
2017 MMC_SECURE_TRIM2_ARG);
2018 }
2019clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05302020 mmc_host_clk_hold(card->host);
2021 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302022out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05302023 return err ? 1 : 0;
2024}
2025
Adrian Hunter49804542010-08-11 14:17:50 -07002026static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2027 struct request *req)
2028{
2029 struct mmc_blk_data *md = mq->data;
2030 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002031 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002032 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002033
Maya Erez775a9362013-04-18 15:41:55 +03002034 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002035 err = -EOPNOTSUPP;
2036 goto out;
2037 }
2038
2039 from = blk_rq_pos(req);
2040 nr = blk_rq_sectors(req);
2041
Maya Erez775a9362013-04-18 15:41:55 +03002042 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2043 arg = MMC_SECURE_TRIM1_ARG;
2044 else
2045 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002046
Adrian Hunter67716322011-08-29 16:42:15 +03002047retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002048 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2049 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2050 INAND_CMD38_ARG_EXT_CSD,
2051 arg == MMC_SECURE_TRIM1_ARG ?
2052 INAND_CMD38_ARG_SECTRIM1 :
2053 INAND_CMD38_ARG_SECERASE,
2054 0);
2055 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002056 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002057 }
Adrian Hunter28302812012-04-05 14:45:48 +03002058
Adrian Hunter49804542010-08-11 14:17:50 -07002059 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002060 if (err == -EIO)
2061 goto out_retry;
2062 if (err)
2063 goto out;
2064
2065 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002066 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2067 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2068 INAND_CMD38_ARG_EXT_CSD,
2069 INAND_CMD38_ARG_SECTRIM2,
2070 0);
2071 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002072 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002073 }
Adrian Hunter28302812012-04-05 14:45:48 +03002074
Adrian Hunter49804542010-08-11 14:17:50 -07002075 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002076 if (err == -EIO)
2077 goto out_retry;
2078 if (err)
2079 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002080 }
Adrian Hunter28302812012-04-05 14:45:48 +03002081
Adrian Hunter28302812012-04-05 14:45:48 +03002082out_retry:
2083 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002084 goto retry;
2085 if (!err)
2086 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002087out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302088 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002089
Adrian Hunter49804542010-08-11 14:17:50 -07002090 return err ? 0 : 1;
2091}
2092
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002093static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2094{
2095 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002096 struct mmc_card *card = md->queue.card;
2097 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002098
Sahitya Tummala61868a42015-05-28 16:54:19 +05302099 if (!req)
2100 return 0;
2101
2102 if (req->cmd_flags & REQ_BARRIER) {
2103 /*
2104 * If eMMC cache flush policy is set to 1, then the device
2105 * shall flush the requests in First-In-First-Out (FIFO) order.
2106 * In this case, as per spec, the host must not send any cache
2107 * barrier requests as they are redundant and add unnecessary
2108 * overhead to both device and host.
2109 */
2110 if (card->ext_csd.cache_flush_policy & 1)
2111 goto end_req;
2112
2113 /*
2114 * In case barrier is not supported or enabled in the device,
2115 * use flush as a fallback option.
2116 */
2117 ret = mmc_cache_barrier(card);
2118 if (ret)
2119 ret = mmc_flush_cache(card);
2120 } else if (req_op(req) == REQ_OP_FLUSH) {
2121 ret = mmc_flush_cache(card);
2122 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002123 if (ret == -ENODEV) {
2124 pr_err("%s: %s: restart mmc card",
2125 req->rq_disk->disk_name, __func__);
2126 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2127 pr_err("%s: %s: fail to restart mmc",
2128 req->rq_disk->disk_name, __func__);
2129 else
2130 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2131 }
2132
2133 if (ret) {
2134 pr_err("%s: %s: notify flush error to upper layers",
2135 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002136 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002137 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002138
Mark Salyzyn6904e432016-01-28 11:12:25 -08002139#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2140 else if (atomic_read(&mq->cache_size)) {
2141 long used = mmc_blk_cache_used(mq, jiffies);
2142
2143 if (used) {
2144 int speed = atomic_read(&mq->max_write_speed);
2145
2146 if (speed_valid(speed)) {
2147 unsigned long msecs = jiffies_to_msecs(
2148 size_and_speed_to_jiffies(
2149 used, speed));
2150 if (msecs)
2151 msleep(msecs);
2152 }
2153 }
2154 }
2155#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302156end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302157 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002158
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002159 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002160}
2161
2162/*
2163 * Reformat current write as a reliable write, supporting
2164 * both legacy and the enhanced reliable write MMC cards.
2165 * In each transfer we'll handle only as much as a single
2166 * reliable write can handle, thus finish the request in
2167 * partial completions.
2168 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002169static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2170 struct mmc_card *card,
2171 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002172{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002173 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2174 /* Legacy mode imposes restrictions on transfers. */
2175 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2176 brq->data.blocks = 1;
2177
2178 if (brq->data.blocks > card->ext_csd.rel_sectors)
2179 brq->data.blocks = card->ext_csd.rel_sectors;
2180 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2181 brq->data.blocks = 1;
2182 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002183}
2184
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002185#define CMD_ERRORS \
2186 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2187 R1_ADDRESS_ERROR | /* Misaligned address */ \
2188 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2189 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2190 R1_CC_ERROR | /* Card controller error */ \
2191 R1_ERROR) /* General/unknown error */
2192
Per Forlinee8a43a2011-07-01 18:55:33 +02002193static int mmc_blk_err_check(struct mmc_card *card,
2194 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002195{
Per Forlinee8a43a2011-07-01 18:55:33 +02002196 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2197 mmc_active);
2198 struct mmc_blk_request *brq = &mq_mrq->brq;
2199 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002200 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002201 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002202
2203 /*
2204 * sbc.error indicates a problem with the set block count
2205 * command. No data will have been transferred.
2206 *
2207 * cmd.error indicates a problem with the r/w command. No
2208 * data will have been transferred.
2209 *
2210 * stop.error indicates a problem with the stop command. Data
2211 * may have been transferred, or may still be transferring.
2212 */
Adrian Hunter67716322011-08-29 16:42:15 +03002213 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2214 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002215 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002216 case ERR_RETRY:
2217 return MMC_BLK_RETRY;
2218 case ERR_ABORT:
2219 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302220 case ERR_NOMEDIUM:
2221 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002222 case ERR_CONTINUE:
2223 break;
2224 }
2225 }
2226
2227 /*
2228 * Check for errors relating to the execution of the
2229 * initial command - such as address errors. No data
2230 * has been transferred.
2231 */
2232 if (brq->cmd.resp[0] & CMD_ERRORS) {
2233 pr_err("%s: r/w command failed, status = %#x\n",
2234 req->rq_disk->disk_name, brq->cmd.resp[0]);
2235 return MMC_BLK_ABORT;
2236 }
2237
2238 /*
2239 * Everything else is either success, or a data error of some
2240 * kind. If it was a write, we may have transitioned to
2241 * program mode, which we have to wait for it to complete.
2242 */
2243 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002244 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002245
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002246 /* Check stop command response */
2247 if (brq->stop.resp[0] & R1_ERROR) {
2248 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2249 req->rq_disk->disk_name, __func__,
2250 brq->stop.resp[0]);
2251 gen_err = 1;
2252 }
2253
Ulf Hansson95a91292014-01-29 13:11:27 +01002254 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2255 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002256 if (err)
2257 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002258 }
2259
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002260 /* if general error occurs, retry the write operation. */
2261 if (gen_err) {
2262 pr_warn("%s: retrying write for general error\n",
2263 req->rq_disk->disk_name);
2264 return MMC_BLK_RETRY;
2265 }
2266
Per Forlind78d4a82011-07-01 18:55:30 +02002267 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002268 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002269 pr_debug("%s: retrying because a re-tune was needed\n",
2270 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002271 brq->retune_retry_done = 1;
2272 return MMC_BLK_RETRY;
2273 }
Per Forlind78d4a82011-07-01 18:55:30 +02002274 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2275 req->rq_disk->disk_name, brq->data.error,
2276 (unsigned)blk_rq_pos(req),
2277 (unsigned)blk_rq_sectors(req),
2278 brq->cmd.resp[0], brq->stop.resp[0]);
2279
2280 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002281 if (ecc_err)
2282 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002283 return MMC_BLK_DATA_ERR;
2284 } else {
2285 return MMC_BLK_CMD_ERR;
2286 }
2287 }
2288
Adrian Hunter67716322011-08-29 16:42:15 +03002289 if (!brq->data.bytes_xfered)
2290 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002291
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002292 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2293 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2294 return MMC_BLK_PARTIAL;
2295 else
2296 return MMC_BLK_SUCCESS;
2297 }
2298
Adrian Hunter67716322011-08-29 16:42:15 +03002299 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2300 return MMC_BLK_PARTIAL;
2301
2302 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002303}
2304
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002305static int mmc_blk_packed_err_check(struct mmc_card *card,
2306 struct mmc_async_req *areq)
2307{
2308 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2309 mmc_active);
2310 struct request *req = mq_rq->req;
2311 struct mmc_packed *packed = mq_rq->packed;
2312 int err, check, status;
2313 u8 *ext_csd;
2314
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002315 packed->retries--;
2316 check = mmc_blk_err_check(card, areq);
2317 err = get_card_status(card, &status, 0);
2318 if (err) {
2319 pr_err("%s: error %d sending status command\n",
2320 req->rq_disk->disk_name, err);
2321 return MMC_BLK_ABORT;
2322 }
2323
2324 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002325 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002326 if (err) {
2327 pr_err("%s: error %d sending ext_csd\n",
2328 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002329 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002330 }
2331
2332 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2333 EXT_CSD_PACKED_FAILURE) &&
2334 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2335 EXT_CSD_PACKED_GENERIC_ERROR)) {
2336 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2337 EXT_CSD_PACKED_INDEXED_ERROR) {
2338 packed->idx_failure =
2339 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2340 check = MMC_BLK_PARTIAL;
2341 }
2342 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2343 "failure index: %d\n",
2344 req->rq_disk->disk_name, packed->nr_entries,
2345 packed->blocks, packed->idx_failure);
2346 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002347 kfree(ext_csd);
2348 }
2349
2350 return check;
2351}
2352
Per Forlin54d49d72011-07-01 18:55:29 +02002353static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2354 struct mmc_card *card,
2355 int disable_multi,
2356 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Per Forlin54d49d72011-07-01 18:55:29 +02002358 u32 readcmd, writecmd;
2359 struct mmc_blk_request *brq = &mqrq->brq;
2360 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302362 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002364 /*
2365 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002366 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002367 */
Luca Porziod3df0462015-11-06 15:12:26 +00002368 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002369 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002370 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002371
Per Forlin54d49d72011-07-01 18:55:29 +02002372 memset(brq, 0, sizeof(struct mmc_blk_request));
2373 brq->mrq.cmd = &brq->cmd;
2374 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Per Forlin54d49d72011-07-01 18:55:29 +02002376 brq->cmd.arg = blk_rq_pos(req);
2377 if (!mmc_card_blockaddr(card))
2378 brq->cmd.arg <<= 9;
2379 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2380 brq->data.blksz = 512;
2381 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2382 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002383 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Asutosh Dasf0665412012-07-27 18:10:19 +05302385 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002386 /*
2387 * The block layer doesn't support all sector count
2388 * restrictions, so we need to be prepared for too big
2389 * requests.
2390 */
2391 if (brq->data.blocks > card->host->max_blk_count)
2392 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002394 if (brq->data.blocks > 1) {
2395 /*
2396 * After a read error, we redo the request one sector
2397 * at a time in order to accurately determine which
2398 * sectors can be read successfully.
2399 */
2400 if (disable_multi)
2401 brq->data.blocks = 1;
2402
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002403 /*
2404 * Some controllers have HW issues while operating
2405 * in multiple I/O mode
2406 */
2407 if (card->host->ops->multi_io_quirk)
2408 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2409 (rq_data_dir(req) == READ) ?
2410 MMC_DATA_READ : MMC_DATA_WRITE,
2411 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002412 }
Per Forlin54d49d72011-07-01 18:55:29 +02002413
2414 if (brq->data.blocks > 1 || do_rel_wr) {
2415 /* SPI multiblock writes terminate using a special
2416 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002417 */
Per Forlin54d49d72011-07-01 18:55:29 +02002418 if (!mmc_host_is_spi(card->host) ||
2419 rq_data_dir(req) == READ)
2420 brq->mrq.stop = &brq->stop;
2421 readcmd = MMC_READ_MULTIPLE_BLOCK;
2422 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2423 } else {
2424 brq->mrq.stop = NULL;
2425 readcmd = MMC_READ_SINGLE_BLOCK;
2426 writecmd = MMC_WRITE_BLOCK;
2427 }
2428 if (rq_data_dir(req) == READ) {
2429 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002430 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002431 if (brq->mrq.stop)
2432 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2433 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002434 } else {
2435 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002436 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002437 if (brq->mrq.stop)
2438 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2439 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002440 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002441
Per Forlin54d49d72011-07-01 18:55:29 +02002442 if (do_rel_wr)
2443 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002444
Per Forlin54d49d72011-07-01 18:55:29 +02002445 /*
Saugata Das42659002011-12-21 13:09:17 +05302446 * Data tag is used only during writing meta data to speed
2447 * up write and any subsequent read of this meta data
2448 */
2449 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2450 (req->cmd_flags & REQ_META) &&
2451 (rq_data_dir(req) == WRITE) &&
2452 ((brq->data.blocks * brq->data.blksz) >=
2453 card->ext_csd.data_tag_unit_size);
2454
2455 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002456 * Pre-defined multi-block transfers are preferable to
2457 * open ended-ones (and necessary for reliable writes).
2458 * However, it is not sufficient to just send CMD23,
2459 * and avoid the final CMD12, as on an error condition
2460 * CMD12 (stop) needs to be sent anyway. This, coupled
2461 * with Auto-CMD23 enhancements provided by some
2462 * hosts, means that the complexity of dealing
2463 * with this is best left to the host. If CMD23 is
2464 * supported by card and host, we'll fill sbc in and let
2465 * the host deal with handling it correctly. This means
2466 * that for hosts that don't expose MMC_CAP_CMD23, no
2467 * change of behavior will be observed.
2468 *
2469 * N.B: Some MMC cards experience perf degradation.
2470 * We'll avoid using CMD23-bounded multiblock writes for
2471 * these, while retaining features like reliable writes.
2472 */
Saugata Das42659002011-12-21 13:09:17 +05302473 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2474 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2475 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002476 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2477 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302478 (do_rel_wr ? (1 << 31) : 0) |
2479 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002480 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2481 brq->mrq.sbc = &brq->sbc;
2482 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002483
Per Forlin54d49d72011-07-01 18:55:29 +02002484 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002485
Per Forlin54d49d72011-07-01 18:55:29 +02002486 brq->data.sg = mqrq->sg;
2487 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002488
Per Forlin54d49d72011-07-01 18:55:29 +02002489 /*
2490 * Adjust the sg list so it is the same size as the
2491 * request.
2492 */
2493 if (brq->data.blocks != blk_rq_sectors(req)) {
2494 int i, data_size = brq->data.blocks << 9;
2495 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002496
Per Forlin54d49d72011-07-01 18:55:29 +02002497 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2498 data_size -= sg->length;
2499 if (data_size <= 0) {
2500 sg->length += data_size;
2501 i++;
2502 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002503 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002504 }
Per Forlin54d49d72011-07-01 18:55:29 +02002505 brq->data.sg_len = i;
2506 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002507
Per Forlinee8a43a2011-07-01 18:55:33 +02002508 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302509 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002510 mqrq->mmc_active.err_check = mmc_blk_err_check;
2511
Per Forlin54d49d72011-07-01 18:55:29 +02002512 mmc_queue_bounce_pre(mqrq);
2513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002515static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2516 struct mmc_card *card)
2517{
2518 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2519 unsigned int max_seg_sz = queue_max_segment_size(q);
2520 unsigned int len, nr_segs = 0;
2521
2522 do {
2523 len = min(hdr_sz, max_seg_sz);
2524 hdr_sz -= len;
2525 nr_segs++;
2526 } while (hdr_sz);
2527
2528 return nr_segs;
2529}
2530
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002531/**
2532 * mmc_blk_disable_wr_packing() - disables packing mode
2533 * @mq: MMC queue.
2534 *
2535 */
2536void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2537{
2538 if (mq) {
2539 mq->wr_packing_enabled = false;
2540 mq->num_of_potential_packed_wr_reqs = 0;
2541 }
2542}
2543EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2544
Lee Susman841fd132013-04-23 17:59:26 +03002545static int get_packed_trigger(int potential, struct mmc_card *card,
2546 struct request *req, int curr_trigger)
2547{
2548 static int num_mean_elements = 1;
2549 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2550 unsigned int trigger = curr_trigger;
2551 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2552
2553 /* scale down the upper bound to 75% */
2554 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2555
2556 /*
2557 * since the most common calls for this function are with small
2558 * potential write values and since we don't want these calls to affect
2559 * the packed trigger, set a lower bound and ignore calls with
2560 * potential lower than that bound
2561 */
2562 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2563 return trigger;
2564
2565 /*
2566 * this is to prevent integer overflow in the following calculation:
2567 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2568 */
2569 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2570 num_mean_elements = 1;
2571 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2572 }
2573
2574 /*
2575 * get next mean value based on previous mean value and current
2576 * potential packed writes. Calculation is as follows:
2577 * mean_pot[i+1] =
2578 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2579 */
2580 mean_potential *= num_mean_elements;
2581 /*
2582 * add num_mean_elements so that the division of two integers doesn't
2583 * lower mean_potential too much
2584 */
2585 if (potential > mean_potential)
2586 mean_potential += num_mean_elements;
2587 mean_potential += potential;
2588 /* this is for gaining more precision when dividing two integers */
2589 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2590 /* this completes the mean calculation */
2591 mean_potential /= ++num_mean_elements;
2592 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2593
2594 /*
2595 * if current potential packed writes is greater than the mean potential
2596 * then the heuristic is that the following workload will contain many
2597 * write requests, therefore we lower the packed trigger. In the
2598 * opposite case we want to increase the trigger in order to get less
2599 * packing events.
2600 */
2601 if (potential >= mean_potential)
2602 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2603 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2604 else
2605 trigger = (trigger >= pckd_trgr_upper_bound) ?
2606 pckd_trgr_upper_bound : trigger + 1;
2607
2608 /*
2609 * an urgent read request indicates a packed list being interrupted
2610 * by this read, therefore we aim for less packing, hence the trigger
2611 * gets increased
2612 */
2613 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2614 trigger += PCKD_TRGR_URGENT_PENALTY;
2615
2616 return trigger;
2617}
2618
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002619static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2620 struct request *req)
2621{
2622 struct mmc_host *host = mq->card->host;
2623 int data_dir;
2624
2625 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2626 return;
2627
Maya Erez8e2b3c32012-12-02 13:27:15 +02002628 /* Support for the write packing on eMMC 4.5 or later */
2629 if (mq->card->ext_csd.rev <= 5)
2630 return;
2631
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002632 /*
2633 * In case the packing control is not supported by the host, it should
2634 * not have an effect on the write packing. Therefore we have to enable
2635 * the write packing
2636 */
2637 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2638 mq->wr_packing_enabled = true;
2639 return;
2640 }
2641
2642 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2643 if (mq->num_of_potential_packed_wr_reqs >
2644 mq->num_wr_reqs_to_start_packing)
2645 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002646 mq->num_wr_reqs_to_start_packing =
2647 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2648 mq->card, req,
2649 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002650 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002651 return;
2652 }
2653
2654 data_dir = rq_data_dir(req);
2655
2656 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002657 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002658 mq->num_wr_reqs_to_start_packing =
2659 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2660 mq->card, req,
2661 mq->num_wr_reqs_to_start_packing);
2662 mq->num_of_potential_packed_wr_reqs = 0;
2663 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002664 return;
2665 } else if (data_dir == WRITE) {
2666 mq->num_of_potential_packed_wr_reqs++;
2667 }
2668
2669 if (mq->num_of_potential_packed_wr_reqs >
2670 mq->num_wr_reqs_to_start_packing)
2671 mq->wr_packing_enabled = true;
2672}
2673
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002674struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2675{
2676 if (!card)
2677 return NULL;
2678
2679 return &card->wr_pack_stats;
2680}
2681EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2682
2683void mmc_blk_init_packed_statistics(struct mmc_card *card)
2684{
2685 int max_num_of_packed_reqs = 0;
2686
2687 if (!card || !card->wr_pack_stats.packing_events)
2688 return;
2689
2690 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2691
2692 spin_lock(&card->wr_pack_stats.lock);
2693 memset(card->wr_pack_stats.packing_events, 0,
2694 (max_num_of_packed_reqs + 1) *
2695 sizeof(*card->wr_pack_stats.packing_events));
2696 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2697 sizeof(card->wr_pack_stats.pack_stop_reason));
2698 card->wr_pack_stats.enabled = true;
2699 spin_unlock(&card->wr_pack_stats.lock);
2700}
2701EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2702
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002703static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2704{
2705 struct request_queue *q = mq->queue;
2706 struct mmc_card *card = mq->card;
2707 struct request *cur = req, *next = NULL;
2708 struct mmc_blk_data *md = mq->data;
2709 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2710 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2711 unsigned int req_sectors = 0, phys_segments = 0;
2712 unsigned int max_blk_count, max_phys_segs;
2713 bool put_back = true;
2714 u8 max_packed_rw = 0;
2715 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002716 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002717
Shawn Lin96e52da2016-08-26 08:49:55 +08002718 /*
2719 * We don't need to check packed for any further
2720 * operation of packed stuff as we set MMC_PACKED_NONE
2721 * and return zero for reqs if geting null packed. Also
2722 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2723 * it again when removing blk req.
2724 */
2725 if (!mqrq->packed) {
2726 md->flags &= (~MMC_BLK_PACKED_CMD);
2727 goto no_packed;
2728 }
2729
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002730 if (!(md->flags & MMC_BLK_PACKED_CMD))
2731 goto no_packed;
2732
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002733 if (!mq->wr_packing_enabled)
2734 goto no_packed;
2735
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002736 if ((rq_data_dir(cur) == WRITE) &&
2737 mmc_host_packed_wr(card->host))
2738 max_packed_rw = card->ext_csd.max_packed_writes;
2739
2740 if (max_packed_rw == 0)
2741 goto no_packed;
2742
2743 if (mmc_req_rel_wr(cur) &&
2744 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2745 goto no_packed;
2746
2747 if (mmc_large_sector(card) &&
2748 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2749 goto no_packed;
2750
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002751 if (cur->cmd_flags & REQ_FUA)
2752 goto no_packed;
2753
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002754 mmc_blk_clear_packed(mqrq);
2755
2756 max_blk_count = min(card->host->max_blk_count,
2757 card->host->max_req_size >> 9);
2758 if (unlikely(max_blk_count > 0xffff))
2759 max_blk_count = 0xffff;
2760
2761 max_phys_segs = queue_max_segments(q);
2762 req_sectors += blk_rq_sectors(cur);
2763 phys_segments += cur->nr_phys_segments;
2764
2765 if (rq_data_dir(cur) == WRITE) {
2766 req_sectors += mmc_large_sector(card) ? 8 : 1;
2767 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2768 }
2769
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002770 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002771 do {
2772 if (reqs >= max_packed_rw - 1) {
2773 put_back = false;
2774 break;
2775 }
2776
2777 spin_lock_irq(q->queue_lock);
2778 next = blk_fetch_request(q);
2779 spin_unlock_irq(q->queue_lock);
2780 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002781 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002782 put_back = false;
2783 break;
2784 }
2785
2786 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002787 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2788 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002789 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002790 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002791
Mike Christie3a5e02c2016-06-05 14:32:23 -05002792 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002793 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002794 req_op(next) == REQ_OP_FLUSH) {
2795 if (req_op(next) != REQ_OP_SECURE_ERASE)
2796 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002797 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002798 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002799
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002800 if (next->cmd_flags & REQ_FUA) {
2801 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2802 break;
2803 }
2804
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002805 if (rq_data_dir(cur) != rq_data_dir(next)) {
2806 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002807 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002808 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002809
2810 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002811 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2812 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002813 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002814 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002815
2816 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002817 if (req_sectors > max_blk_count) {
2818 if (stats->enabled)
2819 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002820 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002821 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002822
2823 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002824 if (phys_segments > max_phys_segs) {
2825 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002826 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002827 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002828
Maya Erez5a8dae12014-12-04 15:13:59 +02002829 if (mq->no_pack_for_random) {
2830 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2831 blk_rq_pos(next)) {
2832 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2833 put_back = 1;
2834 break;
2835 }
2836 }
2837
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002838 if (rq_data_dir(next) == WRITE)
2839 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002840 list_add_tail(&next->queuelist, &mqrq->packed->list);
2841 cur = next;
2842 reqs++;
2843 } while (1);
2844
2845 if (put_back) {
2846 spin_lock_irq(q->queue_lock);
2847 blk_requeue_request(q, next);
2848 spin_unlock_irq(q->queue_lock);
2849 }
2850
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002851 if (stats->enabled) {
2852 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2853 stats->packing_events[reqs + 1]++;
2854 if (reqs + 1 == max_packed_rw)
2855 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2856 }
2857
2858 spin_unlock(&stats->lock);
2859
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002860 if (reqs > 0) {
2861 list_add(&req->queuelist, &mqrq->packed->list);
2862 mqrq->packed->nr_entries = ++reqs;
2863 mqrq->packed->retries = reqs;
2864 return reqs;
2865 }
2866
2867no_packed:
2868 mqrq->cmd_type = MMC_PACKED_NONE;
2869 return 0;
2870}
2871
2872static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2873 struct mmc_card *card,
2874 struct mmc_queue *mq)
2875{
2876 struct mmc_blk_request *brq = &mqrq->brq;
2877 struct request *req = mqrq->req;
2878 struct request *prq;
2879 struct mmc_blk_data *md = mq->data;
2880 struct mmc_packed *packed = mqrq->packed;
2881 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002882 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002883 u8 hdr_blocks;
2884 u8 i = 1;
2885
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002886 mqrq->cmd_type = MMC_PACKED_WRITE;
2887 packed->blocks = 0;
2888 packed->idx_failure = MMC_PACKED_NR_IDX;
2889
2890 packed_cmd_hdr = packed->cmd_hdr;
2891 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002892 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2893 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002894 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2895
2896 /*
2897 * Argument for each entry of packed group
2898 */
2899 list_for_each_entry(prq, &packed->list, queuelist) {
2900 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2901 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2902 (prq->cmd_flags & REQ_META) &&
2903 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002904 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002905 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002906 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002907 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2908 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002909 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002910 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002911 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002912 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002913 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002914 packed->blocks += blk_rq_sectors(prq);
2915 i++;
2916 }
2917
2918 memset(brq, 0, sizeof(struct mmc_blk_request));
2919 brq->mrq.cmd = &brq->cmd;
2920 brq->mrq.data = &brq->data;
2921 brq->mrq.sbc = &brq->sbc;
2922 brq->mrq.stop = &brq->stop;
2923
2924 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2925 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2926 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2927
2928 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2929 brq->cmd.arg = blk_rq_pos(req);
2930 if (!mmc_card_blockaddr(card))
2931 brq->cmd.arg <<= 9;
2932 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2933
2934 brq->data.blksz = 512;
2935 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002936 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302937 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002938
2939 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2940 brq->stop.arg = 0;
2941 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2942
2943 mmc_set_data_timeout(&brq->data, card);
2944
2945 brq->data.sg = mqrq->sg;
2946 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2947
2948 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002949
2950 /*
2951 * This is intended for packed commands tests usage - in case these
2952 * functions are not in use the respective pointers are NULL
2953 */
2954 if (mq->err_check_fn)
2955 mqrq->mmc_active.err_check = mq->err_check_fn;
2956 else
2957 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2958
2959 if (mq->packed_test_fn)
2960 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002961
2962 mmc_queue_bounce_pre(mqrq);
2963}
2964
Adrian Hunter67716322011-08-29 16:42:15 +03002965static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2966 struct mmc_blk_request *brq, struct request *req,
2967 int ret)
2968{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002969 struct mmc_queue_req *mq_rq;
2970 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2971
Adrian Hunter67716322011-08-29 16:42:15 +03002972 /*
2973 * If this is an SD card and we're writing, we can first
2974 * mark the known good sectors as ok.
2975 *
2976 * If the card is not SD, we can still ok written sectors
2977 * as reported by the controller (which might be less than
2978 * the real number of written sectors, but never more).
2979 */
2980 if (mmc_card_sd(card)) {
2981 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302982 if (!brq->data.fault_injected) {
2983 blocks = mmc_sd_num_wr_blocks(card);
2984 if (blocks != (u32)-1)
2985 ret = blk_end_request(req, 0, blocks << 9);
2986 } else
2987 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002988 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002989 if (!mmc_packed_cmd(mq_rq->cmd_type))
2990 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002991 }
2992 return ret;
2993}
2994
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002995static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2996{
2997 struct request *prq;
2998 struct mmc_packed *packed = mq_rq->packed;
2999 int idx = packed->idx_failure, i = 0;
3000 int ret = 0;
3001
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003002 while (!list_empty(&packed->list)) {
3003 prq = list_entry_rq(packed->list.next);
3004 if (idx == i) {
3005 /* retry from error index */
3006 packed->nr_entries -= idx;
3007 mq_rq->req = prq;
3008 ret = 1;
3009
3010 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
3011 list_del_init(&prq->queuelist);
3012 mmc_blk_clear_packed(mq_rq);
3013 }
3014 return ret;
3015 }
3016 list_del_init(&prq->queuelist);
3017 blk_end_request(prq, 0, blk_rq_bytes(prq));
3018 i++;
3019 }
3020
3021 mmc_blk_clear_packed(mq_rq);
3022 return ret;
3023}
3024
3025static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3026{
3027 struct request *prq;
3028 struct mmc_packed *packed = mq_rq->packed;
3029
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003030 while (!list_empty(&packed->list)) {
3031 prq = list_entry_rq(packed->list.next);
3032 list_del_init(&prq->queuelist);
3033 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3034 }
3035
3036 mmc_blk_clear_packed(mq_rq);
3037}
3038
3039static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3040 struct mmc_queue_req *mq_rq)
3041{
3042 struct request *prq;
3043 struct request_queue *q = mq->queue;
3044 struct mmc_packed *packed = mq_rq->packed;
3045
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003046 while (!list_empty(&packed->list)) {
3047 prq = list_entry_rq(packed->list.prev);
3048 if (prq->queuelist.prev != &packed->list) {
3049 list_del_init(&prq->queuelist);
3050 spin_lock_irq(q->queue_lock);
3051 blk_requeue_request(mq->queue, prq);
3052 spin_unlock_irq(q->queue_lock);
3053 } else {
3054 list_del_init(&prq->queuelist);
3055 }
3056 }
3057
3058 mmc_blk_clear_packed(mq_rq);
3059}
3060
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003061static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3062 struct mmc_cmdq_req *cmdq_req)
3063{
3064 struct mmc_request *mrq = &cmdq_req->mrq;
3065
3066 mrq->done = mmc_blk_cmdq_req_done;
3067 return mmc_cmdq_start_req(host, cmdq_req);
3068}
3069
Asutosh Das5238e022015-04-23 16:00:45 +05303070/* prepare for non-data commands */
3071static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3072 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3073{
3074 struct request *req = mqrq->req;
3075 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3076
3077 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3078
3079 cmdq_req->mrq.data = NULL;
3080 cmdq_req->cmd_flags = req->cmd_flags;
3081 cmdq_req->mrq.req = mqrq->req;
3082 req->special = mqrq;
3083 cmdq_req->cmdq_req_flags |= DCMD;
3084 cmdq_req->mrq.cmdq_req = cmdq_req;
3085
3086 return &mqrq->cmdq_req;
3087}
3088
3089
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003090#define IS_RT_CLASS_REQ(x) \
3091 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3092
3093static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3094 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3095{
3096 struct mmc_card *card = mq->card;
3097 struct request *req = mqrq->req;
3098 struct mmc_blk_data *md = mq->data;
3099 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3100 bool do_data_tag;
3101 bool read_dir = (rq_data_dir(req) == READ);
3102 bool prio = IS_RT_CLASS_REQ(req);
3103 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3104
3105 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3106
3107 cmdq_rq->tag = req->tag;
3108 if (read_dir) {
3109 cmdq_rq->cmdq_req_flags |= DIR;
3110 cmdq_rq->data.flags = MMC_DATA_READ;
3111 } else {
3112 cmdq_rq->data.flags = MMC_DATA_WRITE;
3113 }
3114 if (prio)
3115 cmdq_rq->cmdq_req_flags |= PRIO;
3116
3117 if (do_rel_wr)
3118 cmdq_rq->cmdq_req_flags |= REL_WR;
3119
3120 cmdq_rq->data.blocks = blk_rq_sectors(req);
3121 cmdq_rq->blk_addr = blk_rq_pos(req);
3122 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3123
3124 mmc_set_data_timeout(&cmdq_rq->data, card);
3125
3126 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3127 (req->cmd_flags & REQ_META) &&
3128 (rq_data_dir(req) == WRITE) &&
3129 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3130 card->ext_csd.data_tag_unit_size);
3131 if (do_data_tag)
3132 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3133 cmdq_rq->data.sg = mqrq->sg;
3134 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3135
3136 /*
3137 * Adjust the sg list so it is the same size as the
3138 * request.
3139 */
3140 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3141 cmdq_rq->data.blocks = card->host->max_blk_count;
3142
3143 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3144 int i, data_size = cmdq_rq->data.blocks << 9;
3145 struct scatterlist *sg;
3146
3147 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3148 data_size -= sg->length;
3149 if (data_size <= 0) {
3150 sg->length += data_size;
3151 i++;
3152 break;
3153 }
3154 }
3155 cmdq_rq->data.sg_len = i;
3156 }
3157
3158 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3159 mqrq->cmdq_req.mrq.req = mqrq->req;
3160 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3161 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3162 mqrq->req->special = mqrq;
3163
3164 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",
3165 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3166 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3167 cmdq_rq, cmdq_rq->blk_addr,
3168 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3169
3170 return &mqrq->cmdq_req;
3171}
3172
3173static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3174{
3175 struct mmc_queue_req *active_mqrq;
3176 struct mmc_card *card = mq->card;
3177 struct mmc_host *host = card->host;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003178 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003179 struct mmc_cmdq_req *mc_rq;
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003180 u8 active_small_sector_read = 0;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003181 int ret = 0;
3182
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003183 mmc_deferred_scaling(host);
3184 mmc_cmdq_clk_scaling_start_busy(host, true);
3185
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003186 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003187 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003188 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3189
3190 active_mqrq = &mq->mqrq_cmdq[req->tag];
3191 active_mqrq->req = req;
3192
3193 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3194
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003195 if (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003196 unsigned int sectors = blk_rq_sectors(req);
3197
3198 if (((sectors > 0) && (sectors < 8))
3199 && (rq_data_dir(req) == READ))
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003200 active_small_sector_read = 1;
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003201 }
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003202 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3203 if (!ret && active_small_sector_read)
3204 host->cmdq_ctx.active_small_sector_read_reqs++;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003205 /*
3206 * When in SVS2 on low load scenario and there are lots of requests
3207 * queued for CMDQ we need to wait till the queue is empty to scale
3208 * back up to Nominal even if there is a sudden increase in load.
3209 * This impacts performance where lots of IO get executed in SVS2
3210 * frequency since the queue is full. As SVS2 is a low load use case
3211 * we can serialize the requests and not queue them in parallel
3212 * without impacting other use cases. This makes sure the queue gets
3213 * empty faster and we will be able to scale up to Nominal frequency
3214 * when needed.
3215 */
3216 if (!ret && (host->clk_scaling.state == MMC_LOAD_LOW))
3217 wait_event_interruptible(ctx->queue_empty_wq,
3218 (!ctx->active_reqs));
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003219
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003220 return ret;
3221}
3222
Asutosh Das5238e022015-04-23 16:00:45 +05303223/*
3224 * Issues a flush (dcmd) request
3225 */
3226int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3227{
3228 int err;
3229 struct mmc_queue_req *active_mqrq;
3230 struct mmc_card *card = mq->card;
3231 struct mmc_host *host;
3232 struct mmc_cmdq_req *cmdq_req;
3233 struct mmc_cmdq_context_info *ctx_info;
3234
3235 BUG_ON(!card);
3236 host = card->host;
3237 BUG_ON(!host);
3238 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3239 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3240
3241 ctx_info = &host->cmdq_ctx;
3242
3243 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3244
3245 active_mqrq = &mq->mqrq_cmdq[req->tag];
3246 active_mqrq->req = req;
3247
3248 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3249 cmdq_req->cmdq_req_flags |= QBR;
3250 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3251 cmdq_req->tag = req->tag;
3252
3253 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3254 if (err) {
3255 pr_err("%s: failed (%d) preparing flush req\n",
3256 mmc_hostname(host), err);
3257 return err;
3258 }
3259 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3260 return err;
3261}
3262EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3263
Asutosh Das02e30862015-05-20 16:52:04 +05303264static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3265{
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303266 int err = 0;
Asutosh Das02e30862015-05-20 16:52:04 +05303267
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303268 if (mmc_cmdq_halt(host, true)) {
3269 pr_err("%s: halt failed\n", mmc_hostname(host));
3270 goto reset;
Asutosh Das02e30862015-05-20 16:52:04 +05303271 }
3272
3273 if (clear_all)
3274 mmc_cmdq_discard_queue(host, 0);
3275reset:
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303276 mmc_host_clk_hold(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303277 host->cmdq_ops->disable(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303278 mmc_host_clk_release(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303279 err = mmc_cmdq_hw_reset(host);
3280 if (err && err != -EOPNOTSUPP) {
3281 pr_err("%s: failed to cmdq_hw_reset err = %d\n",
3282 mmc_hostname(host), err);
3283 mmc_host_clk_hold(host);
3284 host->cmdq_ops->enable(host);
3285 mmc_host_clk_release(host);
3286 mmc_cmdq_halt(host, false);
3287 goto out;
3288 }
3289 /*
3290 * CMDQ HW reset would have already made CQE
3291 * in unhalted state, but reflect the same
3292 * in software state of cmdq_ctx.
3293 */
3294 mmc_host_clr_halt(host);
3295out:
3296 return;
3297}
3298
3299/**
3300 * is_cmdq_dcmd_req - Checks if tag belongs to DCMD request.
3301 * @q: request_queue pointer.
3302 * @tag: tag number of request to check.
3303 *
3304 * This function checks if the request with tag number "tag"
3305 * is a DCMD request or not based on cmdq_req_flags set.
3306 *
3307 * returns true if DCMD req, otherwise false.
3308 */
3309static bool is_cmdq_dcmd_req(struct request_queue *q, int tag)
3310{
3311 struct request *req;
3312 struct mmc_queue_req *mq_rq;
3313 struct mmc_cmdq_req *cmdq_req;
3314
3315 req = blk_queue_find_tag(q, tag);
3316 if (WARN_ON(!req))
3317 goto out;
3318 mq_rq = req->special;
3319 if (WARN_ON(!mq_rq))
3320 goto out;
3321 cmdq_req = &(mq_rq->cmdq_req);
3322 return (cmdq_req->cmdq_req_flags & DCMD);
3323out:
3324 return -ENOENT;
3325}
3326
3327/**
3328 * mmc_blk_cmdq_reset_all - Reset everything for CMDQ block request.
3329 * @host: mmc_host pointer.
3330 * @err: error for which reset is performed.
3331 *
3332 * This function implements reset_all functionality for
3333 * cmdq. It resets the controller, power cycle the card,
3334 * and invalidate all busy tags(requeue all request back to
3335 * elevator).
3336 */
3337static void mmc_blk_cmdq_reset_all(struct mmc_host *host, int err)
3338{
3339 struct mmc_request *mrq = host->err_mrq;
3340 struct mmc_card *card = host->card;
3341 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3342 struct request_queue *q;
3343 int itag = 0;
3344 int ret = 0;
3345
3346 if (WARN_ON(!mrq))
3347 return;
3348
3349 q = mrq->req->q;
3350 WARN_ON(!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
3351
3352 #ifdef CONFIG_MMC_CLKGATE
3353 pr_debug("%s: %s: active_reqs = %lu, clk_requests = %d\n",
3354 mmc_hostname(host), __func__,
3355 ctx_info->active_reqs, host->clk_requests);
3356 #endif
3357
3358 mmc_blk_cmdq_reset(host, false);
3359
3360 for_each_set_bit(itag, &ctx_info->active_reqs,
3361 host->num_cq_slots) {
3362 ret = is_cmdq_dcmd_req(q, itag);
3363 if (WARN_ON(ret == -ENOENT))
3364 continue;
3365 if (!ret) {
3366 WARN_ON(!test_and_clear_bit(itag,
3367 &ctx_info->data_active_reqs));
Ritesh Harjanid99753e2015-11-23 11:46:45 +05303368 mmc_cmdq_post_req(host, itag, err);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303369 } else {
3370 clear_bit(CMDQ_STATE_DCMD_ACTIVE,
3371 &ctx_info->curr_state);
3372 }
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303373 WARN_ON(!test_and_clear_bit(itag,
3374 &ctx_info->active_reqs));
3375 mmc_host_clk_release(host);
3376 mmc_put_card(card);
3377 }
3378
3379 spin_lock_irq(q->queue_lock);
3380 blk_queue_invalidate_tags(q);
3381 spin_unlock_irq(q->queue_lock);
Asutosh Das02e30862015-05-20 16:52:04 +05303382}
3383
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303384static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3385{
3386 int err;
3387 struct mmc_card *card = mq->card;
3388 struct mmc_host *host = card->host;
3389
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003390 mmc_get_card(card);
3391 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303392 err = mmc_cmdq_halt(host, true);
3393 if (err) {
3394 pr_err("%s: halt: failed: %d\n", __func__, err);
Subhash Jadavanie88f5e22015-10-20 18:01:53 -07003395 goto out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303396 }
3397
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303398 /* disable CQ mode in card */
Sahitya Tummala92136382015-11-05 10:07:42 +05303399 if (mmc_card_cmdq(card)) {
3400 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3401 EXT_CSD_CMDQ, 0,
3402 card->ext_csd.generic_cmd6_time);
3403 if (err) {
3404 pr_err("%s: failed to switch card to legacy mode: %d\n",
3405 __func__, err);
3406 goto out;
3407 }
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003408 mmc_card_clr_cmdq(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303409 }
Sahitya Tummala92136382015-11-05 10:07:42 +05303410 host->cmdq_ops->disable(host, false);
3411 host->card->cmdq_init = false;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303412out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003413 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003414 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303415}
3416
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303417static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3418{
3419 struct mmc_queue *mq = req->q->queuedata;
3420 struct mmc_host *host = mq->card->host;
3421 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003422 struct mmc_request *mrq;
3423 struct mmc_cmdq_req *cmdq_req;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303424 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003425
3426 BUG_ON(!host);
3427
3428 /*
3429 * The mmc_queue_req will be present only if the request
3430 * is issued to the LLD. The request could be fetched from
3431 * block layer queue but could be waiting to be issued
3432 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3433 * Reset the timer in such cases to give LLD more time
3434 */
3435 if (!mq_rq) {
3436 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3437 return BLK_EH_RESET_TIMER;
3438 }
3439
3440 mrq = &mq_rq->cmdq_req.mrq;
3441 cmdq_req = &mq_rq->cmdq_req;
3442
3443 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303444
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303445 if (cmdq_req->cmdq_req_flags & DCMD)
3446 mrq->cmd->error = -ETIMEDOUT;
3447 else
3448 mrq->data->error = -ETIMEDOUT;
3449
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05303450 if (mrq->cmd && mrq->cmd->error) {
3451 if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
3452 /*
3453 * Notify completion for non flush commands like
3454 * discard that wait for DCMD finish.
3455 */
3456 set_bit(CMDQ_STATE_REQ_TIMED_OUT,
3457 &ctx_info->curr_state);
3458 complete(&mrq->completion);
3459 return BLK_EH_NOT_HANDLED;
3460 }
3461 }
3462
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303463 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state) ||
3464 test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state))
3465 return BLK_EH_NOT_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303466
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303467 set_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003468 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303469}
3470
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303471/*
3472 * mmc_blk_cmdq_err: error handling of cmdq error requests.
3473 * Function should be called in context of error out request
3474 * which has claim_host and rpm acquired.
3475 * This may be called with CQ engine halted. Make sure to
3476 * unhalt it after error recovery.
3477 *
3478 * TODO: Currently cmdq error handler does reset_all in case
3479 * of any erorr. Need to optimize error handling.
3480 */
Asutosh Das02e30862015-05-20 16:52:04 +05303481static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3482{
Asutosh Das02e30862015-05-20 16:52:04 +05303483 struct mmc_host *host = mq->card->host;
3484 struct mmc_request *mrq = host->err_mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303485 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303486 struct request_queue *q;
Sahitya Tummala1c73e1c2016-04-26 12:18:35 +05303487 int err, ret;
3488 u32 status = 0;
Asutosh Das02e30862015-05-20 16:52:04 +05303489
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003490 mmc_host_clk_hold(host);
3491 host->cmdq_ops->dumpstate(host);
3492 mmc_host_clk_release(host);
3493
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303494 if (WARN_ON(!mrq))
3495 return;
3496
3497 q = mrq->req->q;
Asutosh Das02e30862015-05-20 16:52:04 +05303498 err = mmc_cmdq_halt(host, true);
3499 if (err) {
3500 pr_err("halt: failed: %d\n", err);
3501 goto reset;
3502 }
3503
3504 /* RED error - Fatal: requires reset */
3505 if (mrq->cmdq_req->resp_err) {
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303506 err = mrq->cmdq_req->resp_err;
Sahitya Tummala1c73e1c2016-04-26 12:18:35 +05303507 if (mmc_host_halt(host) || mmc_host_cq_disable(host)) {
3508 ret = get_card_status(host->card, &status, 0);
3509 if (ret)
3510 pr_err("%s: CMD13 failed with err %d\n",
3511 mmc_hostname(host), ret);
3512 }
3513 pr_err("%s: Response error detected with device status 0x%08x\n",
3514 mmc_hostname(host), status);
Asutosh Das02e30862015-05-20 16:52:04 +05303515 goto reset;
3516 }
3517
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303518 /*
3519 * In case of software request time-out, we schedule err work only for
3520 * the first error out request and handles all other request in flight
3521 * here.
3522 */
3523 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state)) {
3524 err = -ETIMEDOUT;
3525 } else if (mrq->data && mrq->data->error) {
3526 err = mrq->data->error;
3527 } else if (mrq->cmd && mrq->cmd->error) {
3528 /* DCMD commands */
3529 err = mrq->cmd->error;
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003530 }
Asutosh Das02e30862015-05-20 16:52:04 +05303531
3532reset:
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303533 mmc_blk_cmdq_reset_all(host, err);
3534 if (mrq->cmdq_req->resp_err)
3535 mrq->cmdq_req->resp_err = false;
Asutosh Das02e30862015-05-20 16:52:04 +05303536 mmc_cmdq_halt(host, false);
3537
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003538 host->err_mrq = NULL;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303539 clear_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
3540 WARN_ON(!test_and_clear_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Subhash Jadavani987144c2015-08-12 17:53:21 -07003541 wake_up(&ctx_info->wait);
Asutosh Das02e30862015-05-20 16:52:04 +05303542}
3543
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003544/* invoked by block layer in softirq context */
3545void mmc_blk_cmdq_complete_rq(struct request *rq)
3546{
3547 struct mmc_queue_req *mq_rq = rq->special;
3548 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3549 struct mmc_host *host = mrq->host;
3550 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3551 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3552 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3553 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003554 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003555
3556 if (mrq->cmd && mrq->cmd->error)
3557 err = mrq->cmd->error;
3558 else if (mrq->data && mrq->data->error)
3559 err = mrq->data->error;
3560
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303561 if (err || cmdq_req->resp_err) {
3562 pr_err("%s: %s: txfr error(%d)/resp_err(%d)\n",
3563 mmc_hostname(mrq->host), __func__, err,
3564 cmdq_req->resp_err);
Asutosh Das02e30862015-05-20 16:52:04 +05303565 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3566 pr_err("%s: CQ in error state, ending current req: %d\n",
3567 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303568 } else {
3569 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303570 BUG_ON(host->err_mrq != NULL);
3571 host->err_mrq = mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303572 schedule_work(&mq->cmdq_err_work);
3573 }
3574 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003575 }
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303576 /*
3577 * In case of error CMDQ is expected to be either in halted
3578 * or disable state so cannot receive any completion of
3579 * other requests.
3580 */
3581 BUG_ON(test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003582
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303583 /* clear pending request */
3584 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3585 &ctx_info->active_reqs));
3586 if (cmdq_req->cmdq_req_flags & DCMD)
3587 is_dcmd = true;
3588 else
3589 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3590 &ctx_info->data_active_reqs));
Ritesh Harjanid99753e2015-11-23 11:46:45 +05303591 if (!is_dcmd)
3592 mmc_cmdq_post_req(host, cmdq_req->tag, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303593 if (cmdq_req->cmdq_req_flags & DCMD) {
3594 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303595 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303596 goto out;
3597 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003598
3599 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3600
Asutosh Das5238e022015-04-23 16:00:45 +05303601out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003602
3603 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303604 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
Sahitya Tummalaa5dd04f2016-03-03 15:07:24 +05303605 mmc_host_clk_release(host);
Subhash Jadavani987144c2015-08-12 17:53:21 -07003606 wake_up(&ctx_info->wait);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303607 mmc_put_card(host->card);
3608 }
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303609
Talel Shenhar339a4e12015-06-29 10:50:19 +03003610 if (!ctx_info->active_reqs)
3611 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3612
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003613 return;
3614}
3615
3616/*
3617 * Complete reqs from block layer softirq context
3618 * Invoked in irq context
3619 */
3620void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3621{
3622 struct request *req = mrq->req;
3623
3624 blk_complete_request(req);
3625}
3626EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3627
Per Forlinee8a43a2011-07-01 18:55:33 +02003628static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003629{
3630 struct mmc_blk_data *md = mq->data;
3631 struct mmc_card *card = md->queue.card;
3632 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003633 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003634 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003635 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303636 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003637 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003638 const u8 packed_nr = 2;
3639 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003640#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3641 unsigned long waitfor = jiffies;
3642#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003643
3644 if (!rqc && !mq->mqrq_prev->req)
3645 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003646
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003647 if (rqc)
3648 reqs = mmc_blk_prep_packed_list(mq, rqc);
3649
Per Forlin54d49d72011-07-01 18:55:29 +02003650 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003651 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303652 /*
3653 * When 4KB native sector is enabled, only 8 blocks
3654 * multiple read or write is allowed
3655 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003656 if (mmc_large_sector(card) &&
3657 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303658 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3659 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003660 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303661 goto cmd_abort;
3662 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003663
3664 if (reqs >= packed_nr)
3665 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3666 card, mq);
3667 else
3668 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003669 areq = &mq->mqrq_cur->mmc_active;
3670 } else
3671 areq = NULL;
3672 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003673 if (!areq) {
3674 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003675 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003676 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003677 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003678
Per Forlinee8a43a2011-07-01 18:55:33 +02003679 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3680 brq = &mq_rq->brq;
3681 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003682 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003683 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003684
Per Forlind78d4a82011-07-01 18:55:30 +02003685 switch (status) {
3686 case MMC_BLK_SUCCESS:
3687 case MMC_BLK_PARTIAL:
3688 /*
3689 * A block was successfully transferred.
3690 */
Adrian Hunter67716322011-08-29 16:42:15 +03003691 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003692
Mark Salyzyn6904e432016-01-28 11:12:25 -08003693 mmc_blk_simulate_delay(mq, rqc, waitfor);
3694
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003695 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3696 ret = mmc_blk_end_packed_req(mq_rq);
3697 break;
3698 } else {
3699 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003700 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003701 }
3702
Adrian Hunter67716322011-08-29 16:42:15 +03003703 /*
3704 * If the blk_end_request function returns non-zero even
3705 * though all data has been transferred and no errors
3706 * were returned by the host controller, it's a bug.
3707 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003708 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303709 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003710 __func__, blk_rq_bytes(req),
3711 brq->data.bytes_xfered);
3712 rqc = NULL;
3713 goto cmd_abort;
3714 }
Per Forlind78d4a82011-07-01 18:55:30 +02003715 break;
3716 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003717 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003718 if (mmc_blk_reset(md, card->host, type))
3719 goto cmd_abort;
3720 if (!ret)
3721 goto start_new_req;
3722 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003723 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003724 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003725 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003726 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003727 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003728 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003729 if (!mmc_blk_reset(md, card->host, type) &&
3730 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003731 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003732 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003733 case MMC_BLK_DATA_ERR: {
3734 int err;
3735
3736 err = mmc_blk_reset(md, card->host, type);
3737 if (!err)
3738 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303739 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003740 }
3741 case MMC_BLK_ECC_ERR:
3742 if (brq->data.blocks > 1) {
3743 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003744 pr_warn("%s: retrying using single block read\n",
3745 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003746 disable_multi = 1;
3747 break;
3748 }
Per Forlind78d4a82011-07-01 18:55:30 +02003749 /*
3750 * After an error, we redo I/O one sector at a
3751 * time, so we only reach here after trying to
3752 * read a single sector.
3753 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303754 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003755 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003756 if (!ret)
3757 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003758 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303759 case MMC_BLK_NOMEDIUM:
3760 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003761 default:
3762 pr_err("%s: Unhandled return value (%d)",
3763 req->rq_disk->disk_name, status);
3764 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003765 }
3766
Per Forlinee8a43a2011-07-01 18:55:33 +02003767 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003768 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3769 if (!mq_rq->packed->retries)
3770 goto cmd_abort;
3771 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3772 mmc_start_req(card->host,
3773 &mq_rq->mmc_active, NULL);
3774 } else {
3775
3776 /*
3777 * In case of a incomplete request
3778 * prepare it again and resend.
3779 */
3780 mmc_blk_rw_rq_prep(mq_rq, card,
3781 disable_multi, mq);
3782 mmc_start_req(card->host,
3783 &mq_rq->mmc_active, NULL);
3784 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003785 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 } while (ret);
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return 1;
3790
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003791 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003792 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3793 mmc_blk_abort_packed_req(mq_rq);
3794 } else {
3795 if (mmc_card_removed(card))
3796 req->cmd_flags |= REQ_QUIET;
3797 while (ret)
3798 ret = blk_end_request(req, -EIO,
3799 blk_rq_cur_bytes(req));
3800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Per Forlinee8a43a2011-07-01 18:55:33 +02003802 start_new_req:
3803 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003804 if (mmc_card_removed(card)) {
3805 rqc->cmd_flags |= REQ_QUIET;
3806 blk_end_request_all(rqc, -EIO);
3807 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003808 /*
3809 * If current request is packed, it needs to put back.
3810 */
3811 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3812 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3813
Seungwon Jeon7a819022013-01-22 19:48:07 +09003814 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3815 mmc_start_req(card->host,
3816 &mq->mqrq_cur->mmc_active, NULL);
3817 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003818 }
3819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return 0;
3821}
3822
Asutosh Das8b594832015-04-23 09:55:43 +05303823static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3824 struct mmc_blk_data *md)
3825{
3826 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3827 struct mmc_host *host = card->host;
3828 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3829 u8 part_config = card->ext_csd.part_config;
3830
3831 if ((main_md->part_curr == md->part_type) &&
3832 (card->part_curr == md->part_type))
3833 return 0;
3834
3835 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3836 card->ext_csd.cmdq_support &&
3837 (md->flags & MMC_BLK_CMD_QUEUE)));
3838
3839 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3840 WARN_ON(mmc_cmdq_halt(host, true));
3841
3842 /* disable CQ mode in card */
3843 if (mmc_card_cmdq(card)) {
3844 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3845 EXT_CSD_CMDQ, 0,
3846 card->ext_csd.generic_cmd6_time));
3847 mmc_card_clr_cmdq(card);
3848 }
3849
3850 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3851 part_config |= md->part_type;
3852
3853 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3854 EXT_CSD_PART_CONFIG, part_config,
3855 card->ext_csd.part_time));
3856
3857 card->ext_csd.part_config = part_config;
3858 card->part_curr = md->part_type;
3859
3860 main_md->part_curr = md->part_type;
3861
3862 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3863 WARN_ON(mmc_cmdq_halt(host, false));
3864
3865 return 0;
3866}
3867
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003868static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3869{
3870 int ret;
3871 struct mmc_blk_data *md = mq->data;
3872 struct mmc_card *card = md->queue.card;
3873
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003874 mmc_get_card(card);
Maya Ereza2152152015-09-21 14:43:38 +03003875
3876 if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
3877 ret = mmc_cmdq_halt(card->host, true);
3878 if (ret)
3879 goto out;
3880 ret = mmc_stop_bkops(card);
3881 if (ret) {
3882 pr_err("%s: %s: mmc_stop_bkops failed %d\n",
3883 md->disk->disk_name, __func__, ret);
3884 goto out;
3885 }
3886 ret = mmc_cmdq_halt(card->host, false);
3887 if (ret)
3888 goto out;
3889 }
3890
Asutosh Das8b594832015-04-23 09:55:43 +05303891 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003892 if (ret) {
3893 pr_err("%s: %s: partition switch failed %d\n",
3894 md->disk->disk_name, __func__, ret);
Maya Ereza2152152015-09-21 14:43:38 +03003895 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003896 }
3897
Asutosh Das5238e022015-04-23 16:00:45 +05303898 if (req) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003899 struct mmc_host *host = card->host;
3900 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3901
3902 if ((req_op(req) == REQ_OP_FLUSH || req_op(req) == REQ_OP_DISCARD) &&
3903 (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) &&
3904 ctx->active_small_sector_read_reqs) {
3905 ret = wait_event_interruptible(ctx->queue_empty_wq,
3906 !ctx->active_reqs);
3907 if (ret) {
3908 pr_err("%s: failed while waiting for the CMDQ to be empty %s err (%d)\n",
3909 mmc_hostname(host),
3910 __func__, ret);
3911 BUG_ON(1);
3912 }
3913 /* clear the counter now */
3914 ctx->active_small_sector_read_reqs = 0;
3915 /*
3916 * If there were small sector (less than 8 sectors) read
3917 * operations in progress then we have to wait for the
3918 * outstanding requests to finish and should also have
3919 * atleast 6 microseconds delay before queuing the DCMD
3920 * request.
3921 */
3922 udelay(MMC_QUIRK_CMDQ_DELAY_BEFORE_DCMD);
3923 }
3924
Sahitya Tummala9433a132015-06-09 09:38:36 +05303925 if (req_op(req) == REQ_OP_DISCARD) {
3926 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3927 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3928 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3929 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3930 else
3931 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3932 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303933 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303934 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303935 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303936 }
Asutosh Das5238e022015-04-23 16:00:45 +05303937 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003938
Maya Ereza2152152015-09-21 14:43:38 +03003939 return ret;
3940
3941out:
3942 if (req)
3943 blk_end_request_all(req, ret);
3944 mmc_put_card(card);
3945
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003946 return ret;
3947}
3948
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003949int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003950{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003951 int ret;
3952 struct mmc_blk_data *md = mq->data;
3953 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003954 struct mmc_host *host = card->host;
3955 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303956 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003957 bool req_is_special = mmc_req_is_special(req);
Pavan Anamula973ba1c2015-11-20 15:23:47 +05303958 int err;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003959
Dov Levenglicka0296392015-06-24 19:51:58 +03003960 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003961 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003962 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003963
Dov Levenglicka0296392015-06-24 19:51:58 +03003964 if (mmc_card_doing_bkops(host->card)) {
3965 ret = mmc_stop_bkops(host->card);
3966 if (ret)
3967 goto out;
3968 }
3969 }
3970
Andrei Warkentin371a6892011-04-11 18:10:25 -05003971 ret = mmc_blk_part_switch(card, md);
Pavan Anamula973ba1c2015-11-20 15:23:47 +05303972
Andrei Warkentin371a6892011-04-11 18:10:25 -05003973 if (ret) {
Pavan Anamula973ba1c2015-11-20 15:23:47 +05303974 err = mmc_blk_reset(md, card->host, MMC_BLK_PARTSWITCH);
3975 if (!err) {
3976 pr_err("%s: mmc_blk_reset(MMC_BLK_PARTSWITCH) succeeded.\n",
3977 mmc_hostname(host));
3978 mmc_blk_reset_success(md, MMC_BLK_PARTSWITCH);
3979 } else
3980 pr_err("%s: mmc_blk_reset(MMC_BLK_PARTSWITCH) failed.\n",
3981 mmc_hostname(host));
3982
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003983 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303984 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003985 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003986 ret = 0;
3987 goto out;
3988 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003989
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003990 mmc_blk_write_packing_control(mq, req);
3991
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003992 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003993 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003994 /* complete ongoing async transfer before issuing discard */
3995 if (card->host->areq)
3996 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003997 ret = mmc_blk_issue_discard_rq(mq, req);
3998 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3999 /* complete ongoing async transfer before issuing secure erase*/
4000 if (card->host->areq)
4001 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02004002 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
4003 ret = mmc_blk_issue_secdiscard_rq(mq, req);
4004 else
4005 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05304006 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
4007 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09004008 /* complete ongoing async transfer before issuing flush */
4009 if (card->host->areq)
4010 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004011 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07004012 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05004013 if (!req && host->areq) {
4014 spin_lock_irqsave(&host->context_info.lock, flags);
4015 host->context_info.is_waiting_last_req = true;
4016 spin_unlock_irqrestore(&host->context_info.lock, flags);
4017 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004018 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07004019 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004020
Andrei Warkentin371a6892011-04-11 18:10:25 -05004021out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02004022 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
4023 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09004024 /*
4025 * Release host when there are no more requests
4026 * and after special request(discard, flush) is done.
4027 * In case sepecial request, there is no reentry to
4028 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
4029 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004030 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004031 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07004032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Russell Kinga6f6c962006-01-03 22:38:44 +00004034static inline int mmc_blk_readonly(struct mmc_card *card)
4035{
4036 return mmc_card_readonly(card) ||
4037 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
4038}
4039
Andrei Warkentin371a6892011-04-11 18:10:25 -05004040static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
4041 struct device *parent,
4042 sector_t size,
4043 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004044 const char *subname,
4045 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
4047 struct mmc_blk_data *md;
4048 int devidx, ret;
4049
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004050again:
4051 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
4052 return ERR_PTR(-ENOMEM);
4053
4054 spin_lock(&mmc_blk_lock);
4055 ret = ida_get_new(&mmc_blk_ida, &devidx);
4056 spin_unlock(&mmc_blk_lock);
4057
4058 if (ret == -EAGAIN)
4059 goto again;
4060 else if (ret)
4061 return ERR_PTR(ret);
4062
4063 if (devidx >= max_devices) {
4064 ret = -ENOSPC;
4065 goto out;
4066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004068 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00004069 if (!md) {
4070 ret = -ENOMEM;
4071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Russell Kinga6f6c962006-01-03 22:38:44 +00004073
Johan Rudholmadd710e2011-12-02 08:51:06 +01004074 md->area_type = area_type;
4075
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004076 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00004077 * Set the read-only status based on the supported commands
4078 * and the write protect switch.
4079 */
4080 md->read_only = mmc_blk_readonly(card);
4081
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004082 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00004083 if (md->disk == NULL) {
4084 ret = -ENOMEM;
4085 goto err_kfree;
4086 }
4087
4088 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004089 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00004090 md->usage = 1;
4091
Asutosh Das963469b2015-05-21 13:29:51 +05304092 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00004093 if (ret)
4094 goto err_putdisk;
4095
Russell Kinga6f6c962006-01-03 22:38:44 +00004096 md->queue.data = md;
4097
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004098 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004099 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00004100 md->disk->fops = &mmc_bdops;
4101 md->disk->private_data = md;
4102 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07004103 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004104 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07004105 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02004106 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02004107 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00004108
4109 /*
4110 * As discussed on lkml, GENHD_FL_REMOVABLE should:
4111 *
4112 * - be set for removable media with permanent block devices
4113 * - be unset for removable block devices with permanent media
4114 *
4115 * Since MMC block devices clearly fall under the second
4116 * case, we do not set GENHD_FL_REMOVABLE. Userspace
4117 * should use the block device creation/destruction hotplug
4118 * messages to tell when the card is present.
4119 */
4120
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004121 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02004122 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00004123
Saugata Dasa5075eb2012-05-17 16:32:21 +05304124 if (mmc_card_mmc(card))
4125 blk_queue_logical_block_size(md->queue.queue,
4126 card->ext_csd.data_sector_size);
4127 else
4128 blk_queue_logical_block_size(md->queue.queue, 512);
4129
Andrei Warkentin371a6892011-04-11 18:10:25 -05004130 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004131
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004132 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02004133 if ((mmc_card_mmc(card) &&
4134 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004135 (mmc_card_sd(card) &&
4136 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
4137 md->flags |= MMC_BLK_CMD23;
4138 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004139
4140 if (mmc_card_mmc(card) &&
4141 md->flags & MMC_BLK_CMD23 &&
4142 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05304143 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004144 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06004145 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004146 }
4147
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004148 if (card->cmdq_init) {
4149 md->flags |= MMC_BLK_CMD_QUEUE;
4150 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
4151 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05304152 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05304153 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05304154 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004155 }
4156
4157 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09004158 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
4159 (md->flags & MMC_BLK_CMD23) &&
4160 card->ext_csd.packed_event_en) {
4161 if (!mmc_packed_init(&md->queue, card))
4162 md->flags |= MMC_BLK_PACKED_CMD;
4163 }
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00004166
4167 err_putdisk:
4168 put_disk(md->disk);
4169 err_kfree:
4170 kfree(md);
4171 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004172 spin_lock(&mmc_blk_lock);
4173 ida_remove(&mmc_blk_ida, devidx);
4174 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00004175 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
Andrei Warkentin371a6892011-04-11 18:10:25 -05004178static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
4179{
4180 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004181
4182 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
4183 /*
4184 * The EXT_CSD sector count is in number or 512 byte
4185 * sectors.
4186 */
4187 size = card->ext_csd.sectors;
4188 } else {
4189 /*
4190 * The CSD capacity field is in units of read_blkbits.
4191 * set_capacity takes units of 512 bytes.
4192 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00004193 size = (typeof(sector_t))card->csd.capacity
4194 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004195 }
4196
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01004197 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004198 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004199}
4200
4201static int mmc_blk_alloc_part(struct mmc_card *card,
4202 struct mmc_blk_data *md,
4203 unsigned int part_type,
4204 sector_t size,
4205 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004206 const char *subname,
4207 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004208{
4209 char cap_str[10];
4210 struct mmc_blk_data *part_md;
4211
4212 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004213 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004214 if (IS_ERR(part_md))
4215 return PTR_ERR(part_md);
4216 part_md->part_type = part_type;
4217 list_add(&part_md->part, &md->part);
4218
James Bottomleyb9f28d82015-03-05 18:47:01 -08004219 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004220 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304221 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004222 part_md->disk->disk_name, mmc_card_id(card),
4223 mmc_card_name(card), part_md->part_type, cap_str);
4224 return 0;
4225}
4226
Namjae Jeone0c368d2011-10-06 23:41:38 +09004227/* MMC Physical partitions consist of two boot partitions and
4228 * up to four general purpose partitions.
4229 * For each partition enabled in EXT_CSD a block device will be allocatedi
4230 * to provide access to the partition.
4231 */
4232
Andrei Warkentin371a6892011-04-11 18:10:25 -05004233static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4234{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004235 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004236
4237 if (!mmc_card_mmc(card))
4238 return 0;
4239
Namjae Jeone0c368d2011-10-06 23:41:38 +09004240 for (idx = 0; idx < card->nr_parts; idx++) {
4241 if (card->part[idx].size) {
4242 ret = mmc_blk_alloc_part(card, md,
4243 card->part[idx].part_cfg,
4244 card->part[idx].size >> 9,
4245 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004246 card->part[idx].name,
4247 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004248 if (ret)
4249 return ret;
4250 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004251 }
4252
4253 return ret;
4254}
4255
Andrei Warkentin371a6892011-04-11 18:10:25 -05004256static void mmc_blk_remove_req(struct mmc_blk_data *md)
4257{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004258 struct mmc_card *card;
4259
Andrei Warkentin371a6892011-04-11 18:10:25 -05004260 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004261 /*
4262 * Flush remaining requests and free queues. It
4263 * is freeing the queue that stops new requests
4264 * from being accepted.
4265 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004266 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004267 mmc_cleanup_queue(&md->queue);
4268 if (md->flags & MMC_BLK_PACKED_CMD)
4269 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004270 if (md->flags & MMC_BLK_CMD_QUEUE)
4271 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004272 device_remove_file(disk_to_dev(md->disk),
4273 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004274 if (md->disk->flags & GENHD_FL_UP) {
4275 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004276 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4277 card->ext_csd.boot_ro_lockable)
4278 device_remove_file(disk_to_dev(md->disk),
4279 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004280#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4281 device_remove_file(disk_to_dev(md->disk),
4282 &dev_attr_max_write_speed);
4283 device_remove_file(disk_to_dev(md->disk),
4284 &dev_attr_max_read_speed);
4285 device_remove_file(disk_to_dev(md->disk),
4286 &dev_attr_cache_size);
4287#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004288
Andrei Warkentin371a6892011-04-11 18:10:25 -05004289 del_gendisk(md->disk);
4290 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004291 mmc_blk_put(md);
4292 }
4293}
4294
4295static void mmc_blk_remove_parts(struct mmc_card *card,
4296 struct mmc_blk_data *md)
4297{
4298 struct list_head *pos, *q;
4299 struct mmc_blk_data *part_md;
4300
4301 list_for_each_safe(pos, q, &md->part) {
4302 part_md = list_entry(pos, struct mmc_blk_data, part);
4303 list_del(pos);
4304 mmc_blk_remove_req(part_md);
4305 }
4306}
4307
4308static int mmc_add_disk(struct mmc_blk_data *md)
4309{
4310 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004311 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004312
Dan Williams307d8e62016-06-20 10:40:44 -07004313 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004314 md->force_ro.show = force_ro_show;
4315 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304316 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004317 md->force_ro.attr.name = "force_ro";
4318 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4319 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4320 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004321 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004322#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4323 atomic_set(&md->queue.max_write_speed, max_write_speed);
4324 ret = device_create_file(disk_to_dev(md->disk),
4325 &dev_attr_max_write_speed);
4326 if (ret)
4327 goto max_write_speed_fail;
4328 atomic_set(&md->queue.max_read_speed, max_read_speed);
4329 ret = device_create_file(disk_to_dev(md->disk),
4330 &dev_attr_max_read_speed);
4331 if (ret)
4332 goto max_read_speed_fail;
4333 atomic_set(&md->queue.cache_size, cache_size);
4334 atomic_long_set(&md->queue.cache_used, 0);
4335 md->queue.cache_jiffies = jiffies;
4336 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4337 if (ret)
4338 goto cache_size_fail;
4339#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004340
4341 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4342 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004343 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004344
4345 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4346 mode = S_IRUGO;
4347 else
4348 mode = S_IRUGO | S_IWUSR;
4349
4350 md->power_ro_lock.show = power_ro_lock_show;
4351 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004352 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004353 md->power_ro_lock.attr.mode = mode;
4354 md->power_ro_lock.attr.name =
4355 "ro_lock_until_next_power_on";
4356 ret = device_create_file(disk_to_dev(md->disk),
4357 &md->power_ro_lock);
4358 if (ret)
4359 goto power_ro_lock_fail;
4360 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004361
4362 md->num_wr_reqs_to_start_packing.show =
4363 num_wr_reqs_to_start_packing_show;
4364 md->num_wr_reqs_to_start_packing.store =
4365 num_wr_reqs_to_start_packing_store;
4366 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4367 md->num_wr_reqs_to_start_packing.attr.name =
4368 "num_wr_reqs_to_start_packing";
4369 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4370 ret = device_create_file(disk_to_dev(md->disk),
4371 &md->num_wr_reqs_to_start_packing);
4372 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004373 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004374
Maya Erez5a8dae12014-12-04 15:13:59 +02004375 md->no_pack_for_random.show = no_pack_for_random_show;
4376 md->no_pack_for_random.store = no_pack_for_random_store;
4377 sysfs_attr_init(&md->no_pack_for_random.attr);
4378 md->no_pack_for_random.attr.name = "no_pack_for_random";
4379 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4380 ret = device_create_file(disk_to_dev(md->disk),
4381 &md->no_pack_for_random);
4382 if (ret)
4383 goto no_pack_for_random_fails;
4384
Johan Rudholmadd710e2011-12-02 08:51:06 +01004385 return ret;
4386
Maya Erez5a8dae12014-12-04 15:13:59 +02004387no_pack_for_random_fails:
4388 device_remove_file(disk_to_dev(md->disk),
4389 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004390num_wr_reqs_to_start_packing_fail:
4391 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004392power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004393#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4394 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4395cache_size_fail:
4396 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4397max_read_speed_fail:
4398 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4399max_write_speed_fail:
4400#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004401 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4402force_ro_fail:
4403 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004404
4405 return ret;
4406}
4407
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004408static const struct mmc_fixup blk_fixups[] =
4409{
Chris Ballc59d4472011-11-11 22:01:43 -05004410 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4411 MMC_QUIRK_INAND_CMD38),
4412 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4413 MMC_QUIRK_INAND_CMD38),
4414 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4415 MMC_QUIRK_INAND_CMD38),
4416 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4417 MMC_QUIRK_INAND_CMD38),
4418 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4419 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004420
4421 /*
4422 * Some MMC cards experience performance degradation with CMD23
4423 * instead of CMD12-bounded multiblock transfers. For now we'll
4424 * black list what's bad...
4425 * - Certain Toshiba cards.
4426 *
4427 * N.B. This doesn't affect SD cards.
4428 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004429 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4430 MMC_QUIRK_BLK_NO_CMD23),
4431 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4432 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004433 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004434 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004435 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004436 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004437 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004438 MMC_QUIRK_BLK_NO_CMD23),
Subhash Jadavanib3994402015-11-03 19:03:41 -08004439 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_TOSHIBA, CID_OEMID_ANY,
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08004440 add_quirk_mmc, MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004441
4442 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004443 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004444 */
Chris Ballc59d4472011-11-11 22:01:43 -05004445 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004446 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004447 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4448 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004449
Ian Chen3550ccd2012-08-29 15:05:36 +09004450 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004451 * Some Samsung MMC cards need longer data read timeout than
4452 * indicated in CSD.
4453 */
4454 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4455 MMC_QUIRK_LONG_READ_TIME),
4456
4457 /*
Ritesh Harjanie11a1b72016-02-25 17:54:58 +05304458 * Hynix eMMC cards need longer data read timeout than
4459 * indicated in CSD.
4460 */
4461 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
4462 MMC_QUIRK_LONG_READ_TIME),
4463
4464 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004465 * On these Samsung MoviNAND parts, performing secure erase or
4466 * secure trim can result in unrecoverable corruption due to a
4467 * firmware bug.
4468 */
4469 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4470 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4471 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4472 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4473 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4474 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4475 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4476 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4477 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4478 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4479 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4480 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4481 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4482 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4483 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4484 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4485
Shawn Linb5b4ff02015-08-12 13:08:32 +08004486 /*
4487 * On Some Kingston eMMCs, performing trim can result in
4488 * unrecoverable data conrruption occasionally due to a firmware bug.
4489 */
4490 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4491 MMC_QUIRK_TRIM_BROKEN),
4492 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4493 MMC_QUIRK_TRIM_BROKEN),
4494
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004495 /* Some INAND MCP devices advertise incorrect timeout values */
4496 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4497 MMC_QUIRK_INAND_DATA_TIMEOUT),
4498
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004499 END_FIXUP
4500};
4501
Ulf Hansson96541ba2015-04-14 13:06:12 +02004502static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004504 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004505 char cap_str[10];
4506
Pierre Ossman912490d2005-05-21 10:27:02 +01004507 /*
4508 * Check that the card supports the command class(es) we need.
4509 */
4510 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return -ENODEV;
4512
Lukas Czerner5204d002014-06-18 13:18:07 +02004513 mmc_fixup_device(card, blk_fixups);
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 md = mmc_blk_alloc(card);
4516 if (IS_ERR(md))
4517 return PTR_ERR(md);
4518
James Bottomleyb9f28d82015-03-05 18:47:01 -08004519 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004520 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304521 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004523 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Andrei Warkentin371a6892011-04-11 18:10:25 -05004525 if (mmc_blk_alloc_parts(card, md))
4526 goto out;
4527
Ulf Hansson96541ba2015-04-14 13:06:12 +02004528 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004529
Andrei Warkentin371a6892011-04-11 18:10:25 -05004530 if (mmc_add_disk(md))
4531 goto out;
4532
4533 list_for_each_entry(part_md, &md->part, part) {
4534 if (mmc_add_disk(part_md))
4535 goto out;
4536 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004537
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004538 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004539 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
Konstantin Dorfman5dd9f862015-12-31 13:25:40 +02004540 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004541
4542 /*
4543 * Don't enable runtime PM for SD-combo cards here. Leave that
4544 * decision to be taken during the SDIO init sequence instead.
4545 */
4546 if (card->type != MMC_TYPE_SD_COMBO) {
4547 pm_runtime_set_active(&card->dev);
4548 pm_runtime_enable(&card->dev);
4549 }
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return 0;
4552
4553 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004554 mmc_blk_remove_parts(card, md);
4555 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
Ulf Hansson96541ba2015-04-14 13:06:12 +02004559static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004561 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Andrei Warkentin371a6892011-04-11 18:10:25 -05004563 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004564 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004565 mmc_claim_host(card->host);
4566 mmc_blk_part_switch(card, md);
4567 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004568 if (card->type != MMC_TYPE_SD_COMBO)
4569 pm_runtime_disable(&card->dev);
4570 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004571 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004572 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004575static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004577 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004578 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304579 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004582 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304583 if (rc)
4584 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004585 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004586 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304587 if (rc)
4588 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304591 goto out;
4592
4593 out_resume:
4594 mmc_queue_resume(&md->queue);
4595 list_for_each_entry(part_md, &md->part, part) {
4596 mmc_queue_resume(&part_md->queue);
4597 }
4598 out:
4599 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Ulf Hansson96541ba2015-04-14 13:06:12 +02004602static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004603{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004604 _mmc_blk_suspend(card, 1);
Krishna Kondaa7ea2dc2015-06-29 19:20:05 -07004605
4606 /* send power off notification */
4607 if (mmc_card_mmc(card))
4608 mmc_send_pon(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004609}
4610
Ulf Hansson0967edc2014-10-06 11:29:42 +02004611#ifdef CONFIG_PM_SLEEP
4612static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004613{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004614 struct mmc_card *card = mmc_dev_to_card(dev);
4615
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004616 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004617}
4618
Ulf Hansson0967edc2014-10-06 11:29:42 +02004619static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004621 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004622 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004625 /*
4626 * Resume involves the card going into idle state,
4627 * so current partition is always the main one.
4628 */
4629 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004631 list_for_each_entry(part_md, &md->part, part) {
4632 mmc_queue_resume(&part_md->queue);
4633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
4635 return 0;
4636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637#endif
4638
Ulf Hansson0967edc2014-10-06 11:29:42 +02004639static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4640
Ulf Hansson96541ba2015-04-14 13:06:12 +02004641static struct mmc_driver mmc_driver = {
4642 .drv = {
4643 .name = "mmcblk",
4644 .pm = &mmc_blk_pm_ops,
4645 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 .probe = mmc_blk_probe,
4647 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004648 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649};
4650
4651static int __init mmc_blk_init(void)
4652{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004653 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004655 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4656 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4657
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004658 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004659
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004660 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4661 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004664 res = mmc_register_driver(&mmc_driver);
4665 if (res)
4666 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004668 return 0;
4669 out2:
4670 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 out:
4672 return res;
4673}
4674
4675static void __exit mmc_blk_exit(void)
4676{
4677 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004678 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
4681module_init(mmc_blk_init);
4682module_exit(mmc_blk_exit);
4683
4684MODULE_LICENSE("GPL");
4685MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4686