blob: eb14f0f5b587149d35742e13c712a2851a5fcacf [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) {
Can Guo6514fbe2017-07-04 12:36:09 +08001721 if (card->err_in_sdr104)
1722 return ERR_RETRY;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301723 /* Check if the card is removed */
1724 if (mmc_detect_card_removed(card->host))
1725 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001726 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301727 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001728
Adrian Hunter67716322011-08-29 16:42:15 +03001729 /* Flag ECC errors */
1730 if ((status & R1_CARD_ECC_FAILED) ||
1731 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1732 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1733 *ecc_err = 1;
1734
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001735 /* Flag General errors */
1736 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1737 if ((status & R1_ERROR) ||
1738 (brq->stop.resp[0] & R1_ERROR)) {
1739 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1740 req->rq_disk->disk_name, __func__,
1741 brq->stop.resp[0], status);
1742 *gen_err = 1;
1743 }
1744
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001745 /*
1746 * Check the current card state. If it is in some data transfer
1747 * mode, tell it to stop (and hopefully transition back to TRAN.)
1748 */
1749 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1750 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001751 err = send_stop(card,
1752 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1753 req, gen_err, &stop_status);
1754 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001755 pr_err("%s: error %d sending stop command\n",
1756 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001757 /*
1758 * If the stop cmd also timed out, the card is probably
1759 * not present, so abort. Other errors are bad news too.
1760 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001761 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001762 }
1763
Adrian Hunter67716322011-08-29 16:42:15 +03001764 if (stop_status & R1_CARD_ECC_FAILED)
1765 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001766 }
1767
1768 /* Check for set block count errors */
1769 if (brq->sbc.error)
1770 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1771 prev_cmd_status_valid, status);
1772
1773 /* Check for r/w command errors */
1774 if (brq->cmd.error)
1775 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1776 prev_cmd_status_valid, status);
1777
Adrian Hunter67716322011-08-29 16:42:15 +03001778 /* Data errors */
1779 if (!brq->stop.error)
1780 return ERR_CONTINUE;
1781
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001782 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001783 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 +01001784 req->rq_disk->disk_name, brq->stop.error,
1785 brq->cmd.resp[0], status);
1786
1787 /*
1788 * Subsitute in our own stop status as this will give the error
1789 * state which happened during the execution of the r/w command.
1790 */
1791 if (stop_status) {
1792 brq->stop.resp[0] = stop_status;
1793 brq->stop.error = 0;
1794 }
1795 return ERR_CONTINUE;
1796}
1797
Adrian Hunter67716322011-08-29 16:42:15 +03001798static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1799 int type)
1800{
1801 int err;
1802
1803 if (md->reset_done & type)
1804 return -EEXIST;
1805
1806 md->reset_done |= type;
1807 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301808 if (err && err != -EOPNOTSUPP) {
1809 /* We failed to reset so we need to abort the request */
1810 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1811 __func__, err);
1812 return -ENODEV;
1813 }
1814
Adrian Hunter67716322011-08-29 16:42:15 +03001815 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301816 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001817 struct mmc_blk_data *main_md =
1818 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001819 int part_err;
1820
1821 main_md->part_curr = main_md->part_type;
1822 part_err = mmc_blk_part_switch(host->card, md);
1823 if (part_err) {
1824 /*
1825 * We have failed to get back into the correct
1826 * partition, so we need to abort the whole request.
1827 */
1828 return -ENODEV;
1829 }
1830 }
1831 return err;
1832}
1833
1834static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1835{
1836 md->reset_done &= ~type;
1837}
1838
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001839int mmc_access_rpmb(struct mmc_queue *mq)
1840{
1841 struct mmc_blk_data *md = mq->data;
1842 /*
1843 * If this is a RPMB partition access, return ture
1844 */
1845 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1846 return true;
1847
1848 return false;
1849}
1850
Sahitya Tummala9433a132015-06-09 09:38:36 +05301851static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1852 struct request *req)
1853{
1854 struct mmc_blk_data *md = mq->data;
1855 struct mmc_card *card = md->queue.card;
1856 struct mmc_host *host = card->host;
1857 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1858 struct mmc_cmdq_req *cmdq_req;
1859 struct mmc_queue_req *active_mqrq;
1860
1861 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1862 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1863
1864 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1865
1866 active_mqrq = &mq->mqrq_cmdq[req->tag];
1867 active_mqrq->req = req;
1868
1869 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1870 cmdq_req->cmdq_req_flags |= QBR;
1871 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1872 cmdq_req->tag = req->tag;
1873 return cmdq_req;
1874}
1875
1876static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1877 struct request *req)
1878{
1879 struct mmc_blk_data *md = mq->data;
1880 struct mmc_card *card = md->queue.card;
1881 struct mmc_cmdq_req *cmdq_req = NULL;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301882 unsigned int from, nr, arg;
1883 int err = 0;
1884
1885 if (!mmc_can_erase(card)) {
1886 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301887 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301888 goto out;
1889 }
1890
1891 from = blk_rq_pos(req);
1892 nr = blk_rq_sectors(req);
1893
1894 if (mmc_can_discard(card))
1895 arg = MMC_DISCARD_ARG;
1896 else if (mmc_can_trim(card))
1897 arg = MMC_TRIM_ARG;
1898 else
1899 arg = MMC_ERASE_ARG;
1900
1901 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1902 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1903 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1904 EXT_CSD_CMD_SET_NORMAL,
1905 INAND_CMD38_ARG_EXT_CSD,
1906 arg == MMC_TRIM_ARG ?
1907 INAND_CMD38_ARG_TRIM :
1908 INAND_CMD38_ARG_ERASE,
1909 0, true, false);
1910 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1911 if (err)
1912 goto clear_dcmd;
1913 }
1914 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1915clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301916 mmc_host_clk_hold(card->host);
1917 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301918out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05301919 return err ? 1 : 0;
1920}
1921
Adrian Hunterbd788c92010-08-11 14:17:47 -07001922static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1923{
1924 struct mmc_blk_data *md = mq->data;
1925 struct mmc_card *card = md->queue.card;
1926 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001927 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001928
Adrian Hunterbd788c92010-08-11 14:17:47 -07001929 if (!mmc_can_erase(card)) {
1930 err = -EOPNOTSUPP;
1931 goto out;
1932 }
1933
1934 from = blk_rq_pos(req);
1935 nr = blk_rq_sectors(req);
1936
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001937 if (mmc_can_discard(card))
1938 arg = MMC_DISCARD_ARG;
1939 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001940 arg = MMC_TRIM_ARG;
1941 else
1942 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001943retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001944 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1945 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1946 INAND_CMD38_ARG_EXT_CSD,
1947 arg == MMC_TRIM_ARG ?
1948 INAND_CMD38_ARG_TRIM :
1949 INAND_CMD38_ARG_ERASE,
1950 0);
1951 if (err)
1952 goto out;
1953 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001954 err = mmc_erase(card, from, nr, arg);
1955out:
Adrian Hunter67716322011-08-29 16:42:15 +03001956 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1957 goto retry;
1958 if (!err)
1959 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301960 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001961
Adrian Hunterbd788c92010-08-11 14:17:47 -07001962 return err ? 0 : 1;
1963}
1964
Sahitya Tummala9433a132015-06-09 09:38:36 +05301965static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1966 struct request *req)
1967{
1968 struct mmc_blk_data *md = mq->data;
1969 struct mmc_card *card = md->queue.card;
1970 struct mmc_cmdq_req *cmdq_req = NULL;
1971 unsigned int from, nr, arg;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301972 int err = 0;
1973
1974 if (!(mmc_can_secure_erase_trim(card))) {
1975 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301976 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301977 goto out;
1978 }
1979
1980 from = blk_rq_pos(req);
1981 nr = blk_rq_sectors(req);
1982
1983 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1984 arg = MMC_SECURE_TRIM1_ARG;
1985 else
1986 arg = MMC_SECURE_ERASE_ARG;
1987
1988 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1989 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1990 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1991 EXT_CSD_CMD_SET_NORMAL,
1992 INAND_CMD38_ARG_EXT_CSD,
1993 arg == MMC_SECURE_TRIM1_ARG ?
1994 INAND_CMD38_ARG_SECTRIM1 :
1995 INAND_CMD38_ARG_SECERASE,
1996 0, true, false);
1997 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1998 if (err)
1999 goto clear_dcmd;
2000 }
2001
2002 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
2003 if (err)
2004 goto clear_dcmd;
2005
2006 if (arg == MMC_SECURE_TRIM1_ARG) {
2007 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2008 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
2009 EXT_CSD_CMD_SET_NORMAL,
2010 INAND_CMD38_ARG_EXT_CSD,
2011 INAND_CMD38_ARG_SECTRIM2,
2012 0, true, false);
2013 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2014 if (err)
2015 goto clear_dcmd;
2016 }
2017
2018 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
2019 MMC_SECURE_TRIM2_ARG);
2020 }
2021clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05302022 mmc_host_clk_hold(card->host);
2023 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302024out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05302025 return err ? 1 : 0;
2026}
2027
Adrian Hunter49804542010-08-11 14:17:50 -07002028static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2029 struct request *req)
2030{
2031 struct mmc_blk_data *md = mq->data;
2032 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002033 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002034 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002035
Maya Erez775a9362013-04-18 15:41:55 +03002036 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002037 err = -EOPNOTSUPP;
2038 goto out;
2039 }
2040
2041 from = blk_rq_pos(req);
2042 nr = blk_rq_sectors(req);
2043
Maya Erez775a9362013-04-18 15:41:55 +03002044 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2045 arg = MMC_SECURE_TRIM1_ARG;
2046 else
2047 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002048
Adrian Hunter67716322011-08-29 16:42:15 +03002049retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002050 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2051 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2052 INAND_CMD38_ARG_EXT_CSD,
2053 arg == MMC_SECURE_TRIM1_ARG ?
2054 INAND_CMD38_ARG_SECTRIM1 :
2055 INAND_CMD38_ARG_SECERASE,
2056 0);
2057 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002058 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002059 }
Adrian Hunter28302812012-04-05 14:45:48 +03002060
Adrian Hunter49804542010-08-11 14:17:50 -07002061 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002062 if (err == -EIO)
2063 goto out_retry;
2064 if (err)
2065 goto out;
2066
2067 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002068 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2069 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2070 INAND_CMD38_ARG_EXT_CSD,
2071 INAND_CMD38_ARG_SECTRIM2,
2072 0);
2073 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002074 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002075 }
Adrian Hunter28302812012-04-05 14:45:48 +03002076
Adrian Hunter49804542010-08-11 14:17:50 -07002077 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002078 if (err == -EIO)
2079 goto out_retry;
2080 if (err)
2081 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002082 }
Adrian Hunter28302812012-04-05 14:45:48 +03002083
Adrian Hunter28302812012-04-05 14:45:48 +03002084out_retry:
2085 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002086 goto retry;
2087 if (!err)
2088 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002089out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302090 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002091
Adrian Hunter49804542010-08-11 14:17:50 -07002092 return err ? 0 : 1;
2093}
2094
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002095static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2096{
2097 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002098 struct mmc_card *card = md->queue.card;
2099 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002100
Sahitya Tummala61868a42015-05-28 16:54:19 +05302101 if (!req)
2102 return 0;
2103
2104 if (req->cmd_flags & REQ_BARRIER) {
2105 /*
2106 * If eMMC cache flush policy is set to 1, then the device
2107 * shall flush the requests in First-In-First-Out (FIFO) order.
2108 * In this case, as per spec, the host must not send any cache
2109 * barrier requests as they are redundant and add unnecessary
2110 * overhead to both device and host.
2111 */
2112 if (card->ext_csd.cache_flush_policy & 1)
2113 goto end_req;
2114
2115 /*
2116 * In case barrier is not supported or enabled in the device,
2117 * use flush as a fallback option.
2118 */
2119 ret = mmc_cache_barrier(card);
2120 if (ret)
2121 ret = mmc_flush_cache(card);
2122 } else if (req_op(req) == REQ_OP_FLUSH) {
2123 ret = mmc_flush_cache(card);
2124 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002125 if (ret == -ENODEV) {
2126 pr_err("%s: %s: restart mmc card",
2127 req->rq_disk->disk_name, __func__);
2128 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2129 pr_err("%s: %s: fail to restart mmc",
2130 req->rq_disk->disk_name, __func__);
2131 else
2132 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2133 }
2134
2135 if (ret) {
2136 pr_err("%s: %s: notify flush error to upper layers",
2137 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002138 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002139 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002140
Mark Salyzyn6904e432016-01-28 11:12:25 -08002141#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2142 else if (atomic_read(&mq->cache_size)) {
2143 long used = mmc_blk_cache_used(mq, jiffies);
2144
2145 if (used) {
2146 int speed = atomic_read(&mq->max_write_speed);
2147
2148 if (speed_valid(speed)) {
2149 unsigned long msecs = jiffies_to_msecs(
2150 size_and_speed_to_jiffies(
2151 used, speed));
2152 if (msecs)
2153 msleep(msecs);
2154 }
2155 }
2156 }
2157#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302158end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302159 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002160
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002161 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002162}
2163
2164/*
2165 * Reformat current write as a reliable write, supporting
2166 * both legacy and the enhanced reliable write MMC cards.
2167 * In each transfer we'll handle only as much as a single
2168 * reliable write can handle, thus finish the request in
2169 * partial completions.
2170 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002171static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2172 struct mmc_card *card,
2173 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002174{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002175 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2176 /* Legacy mode imposes restrictions on transfers. */
2177 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2178 brq->data.blocks = 1;
2179
2180 if (brq->data.blocks > card->ext_csd.rel_sectors)
2181 brq->data.blocks = card->ext_csd.rel_sectors;
2182 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2183 brq->data.blocks = 1;
2184 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002185}
2186
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002187#define CMD_ERRORS \
2188 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2189 R1_ADDRESS_ERROR | /* Misaligned address */ \
2190 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2191 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2192 R1_CC_ERROR | /* Card controller error */ \
2193 R1_ERROR) /* General/unknown error */
2194
Per Forlinee8a43a2011-07-01 18:55:33 +02002195static int mmc_blk_err_check(struct mmc_card *card,
2196 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002197{
Per Forlinee8a43a2011-07-01 18:55:33 +02002198 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2199 mmc_active);
2200 struct mmc_blk_request *brq = &mq_mrq->brq;
2201 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002202 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002203 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002204
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07002205 if (card->host->sdr104_wa && mmc_card_sd(card) &&
2206 (card->host->ios.timing == MMC_TIMING_UHS_SDR104) &&
2207 !card->sdr104_blocked &&
2208 (brq->data.error == -EILSEQ ||
2209 brq->data.error == -EIO ||
2210 brq->data.error == -ETIMEDOUT ||
2211 brq->cmd.error == -EILSEQ ||
2212 brq->cmd.error == -EIO ||
Can Guo6514fbe2017-07-04 12:36:09 +08002213 brq->cmd.error == -ETIMEDOUT ||
2214 brq->sbc.error))
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07002215 card->err_in_sdr104 = true;
2216
Per Forlind78d4a82011-07-01 18:55:30 +02002217 /*
2218 * sbc.error indicates a problem with the set block count
2219 * command. No data will have been transferred.
2220 *
2221 * cmd.error indicates a problem with the r/w command. No
2222 * data will have been transferred.
2223 *
2224 * stop.error indicates a problem with the stop command. Data
2225 * may have been transferred, or may still be transferring.
2226 */
Adrian Hunter67716322011-08-29 16:42:15 +03002227 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2228 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002229 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002230 case ERR_RETRY:
2231 return MMC_BLK_RETRY;
2232 case ERR_ABORT:
2233 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302234 case ERR_NOMEDIUM:
2235 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002236 case ERR_CONTINUE:
2237 break;
2238 }
2239 }
2240
2241 /*
2242 * Check for errors relating to the execution of the
2243 * initial command - such as address errors. No data
2244 * has been transferred.
2245 */
2246 if (brq->cmd.resp[0] & CMD_ERRORS) {
2247 pr_err("%s: r/w command failed, status = %#x\n",
2248 req->rq_disk->disk_name, brq->cmd.resp[0]);
2249 return MMC_BLK_ABORT;
2250 }
2251
2252 /*
2253 * Everything else is either success, or a data error of some
2254 * kind. If it was a write, we may have transitioned to
2255 * program mode, which we have to wait for it to complete.
2256 */
2257 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002258 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002259
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002260 /* Check stop command response */
2261 if (brq->stop.resp[0] & R1_ERROR) {
2262 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2263 req->rq_disk->disk_name, __func__,
2264 brq->stop.resp[0]);
2265 gen_err = 1;
2266 }
2267
Ulf Hansson95a91292014-01-29 13:11:27 +01002268 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2269 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002270 if (err)
2271 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002272 }
2273
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002274 /* if general error occurs, retry the write operation. */
2275 if (gen_err) {
2276 pr_warn("%s: retrying write for general error\n",
2277 req->rq_disk->disk_name);
2278 return MMC_BLK_RETRY;
2279 }
2280
Per Forlind78d4a82011-07-01 18:55:30 +02002281 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002282 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002283 pr_debug("%s: retrying because a re-tune was needed\n",
2284 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002285 brq->retune_retry_done = 1;
2286 return MMC_BLK_RETRY;
2287 }
Per Forlind78d4a82011-07-01 18:55:30 +02002288 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2289 req->rq_disk->disk_name, brq->data.error,
2290 (unsigned)blk_rq_pos(req),
2291 (unsigned)blk_rq_sectors(req),
2292 brq->cmd.resp[0], brq->stop.resp[0]);
2293
2294 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002295 if (ecc_err)
2296 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002297 return MMC_BLK_DATA_ERR;
2298 } else {
2299 return MMC_BLK_CMD_ERR;
2300 }
2301 }
2302
Adrian Hunter67716322011-08-29 16:42:15 +03002303 if (!brq->data.bytes_xfered)
2304 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002305
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002306 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2307 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2308 return MMC_BLK_PARTIAL;
2309 else
2310 return MMC_BLK_SUCCESS;
2311 }
2312
Adrian Hunter67716322011-08-29 16:42:15 +03002313 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2314 return MMC_BLK_PARTIAL;
2315
2316 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002317}
2318
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002319static int mmc_blk_packed_err_check(struct mmc_card *card,
2320 struct mmc_async_req *areq)
2321{
2322 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2323 mmc_active);
2324 struct request *req = mq_rq->req;
2325 struct mmc_packed *packed = mq_rq->packed;
2326 int err, check, status;
2327 u8 *ext_csd;
2328
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002329 packed->retries--;
2330 check = mmc_blk_err_check(card, areq);
2331 err = get_card_status(card, &status, 0);
2332 if (err) {
2333 pr_err("%s: error %d sending status command\n",
2334 req->rq_disk->disk_name, err);
2335 return MMC_BLK_ABORT;
2336 }
2337
2338 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002339 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002340 if (err) {
2341 pr_err("%s: error %d sending ext_csd\n",
2342 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002343 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002344 }
2345
2346 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2347 EXT_CSD_PACKED_FAILURE) &&
2348 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2349 EXT_CSD_PACKED_GENERIC_ERROR)) {
2350 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2351 EXT_CSD_PACKED_INDEXED_ERROR) {
2352 packed->idx_failure =
2353 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2354 check = MMC_BLK_PARTIAL;
2355 }
2356 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2357 "failure index: %d\n",
2358 req->rq_disk->disk_name, packed->nr_entries,
2359 packed->blocks, packed->idx_failure);
2360 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002361 kfree(ext_csd);
2362 }
2363
2364 return check;
2365}
2366
Per Forlin54d49d72011-07-01 18:55:29 +02002367static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2368 struct mmc_card *card,
2369 int disable_multi,
2370 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Per Forlin54d49d72011-07-01 18:55:29 +02002372 u32 readcmd, writecmd;
2373 struct mmc_blk_request *brq = &mqrq->brq;
2374 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302376 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002378 /*
2379 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002380 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002381 */
Luca Porziod3df0462015-11-06 15:12:26 +00002382 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002383 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002384 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002385
Per Forlin54d49d72011-07-01 18:55:29 +02002386 memset(brq, 0, sizeof(struct mmc_blk_request));
2387 brq->mrq.cmd = &brq->cmd;
2388 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Per Forlin54d49d72011-07-01 18:55:29 +02002390 brq->cmd.arg = blk_rq_pos(req);
2391 if (!mmc_card_blockaddr(card))
2392 brq->cmd.arg <<= 9;
2393 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2394 brq->data.blksz = 512;
2395 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2396 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002397 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Asutosh Dasf0665412012-07-27 18:10:19 +05302399 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002400 /*
2401 * The block layer doesn't support all sector count
2402 * restrictions, so we need to be prepared for too big
2403 * requests.
2404 */
2405 if (brq->data.blocks > card->host->max_blk_count)
2406 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002408 if (brq->data.blocks > 1) {
2409 /*
2410 * After a read error, we redo the request one sector
2411 * at a time in order to accurately determine which
2412 * sectors can be read successfully.
2413 */
2414 if (disable_multi)
2415 brq->data.blocks = 1;
2416
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002417 /*
2418 * Some controllers have HW issues while operating
2419 * in multiple I/O mode
2420 */
2421 if (card->host->ops->multi_io_quirk)
2422 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2423 (rq_data_dir(req) == READ) ?
2424 MMC_DATA_READ : MMC_DATA_WRITE,
2425 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002426 }
Per Forlin54d49d72011-07-01 18:55:29 +02002427
2428 if (brq->data.blocks > 1 || do_rel_wr) {
2429 /* SPI multiblock writes terminate using a special
2430 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002431 */
Per Forlin54d49d72011-07-01 18:55:29 +02002432 if (!mmc_host_is_spi(card->host) ||
2433 rq_data_dir(req) == READ)
2434 brq->mrq.stop = &brq->stop;
2435 readcmd = MMC_READ_MULTIPLE_BLOCK;
2436 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2437 } else {
2438 brq->mrq.stop = NULL;
2439 readcmd = MMC_READ_SINGLE_BLOCK;
2440 writecmd = MMC_WRITE_BLOCK;
2441 }
2442 if (rq_data_dir(req) == READ) {
2443 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002444 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002445 if (brq->mrq.stop)
2446 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2447 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002448 } else {
2449 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002450 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002451 if (brq->mrq.stop)
2452 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2453 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002454 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002455
Per Forlin54d49d72011-07-01 18:55:29 +02002456 if (do_rel_wr)
2457 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002458
Per Forlin54d49d72011-07-01 18:55:29 +02002459 /*
Saugata Das42659002011-12-21 13:09:17 +05302460 * Data tag is used only during writing meta data to speed
2461 * up write and any subsequent read of this meta data
2462 */
2463 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2464 (req->cmd_flags & REQ_META) &&
2465 (rq_data_dir(req) == WRITE) &&
2466 ((brq->data.blocks * brq->data.blksz) >=
2467 card->ext_csd.data_tag_unit_size);
2468
2469 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002470 * Pre-defined multi-block transfers are preferable to
2471 * open ended-ones (and necessary for reliable writes).
2472 * However, it is not sufficient to just send CMD23,
2473 * and avoid the final CMD12, as on an error condition
2474 * CMD12 (stop) needs to be sent anyway. This, coupled
2475 * with Auto-CMD23 enhancements provided by some
2476 * hosts, means that the complexity of dealing
2477 * with this is best left to the host. If CMD23 is
2478 * supported by card and host, we'll fill sbc in and let
2479 * the host deal with handling it correctly. This means
2480 * that for hosts that don't expose MMC_CAP_CMD23, no
2481 * change of behavior will be observed.
2482 *
2483 * N.B: Some MMC cards experience perf degradation.
2484 * We'll avoid using CMD23-bounded multiblock writes for
2485 * these, while retaining features like reliable writes.
2486 */
Saugata Das42659002011-12-21 13:09:17 +05302487 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2488 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2489 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002490 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2491 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302492 (do_rel_wr ? (1 << 31) : 0) |
2493 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002494 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2495 brq->mrq.sbc = &brq->sbc;
2496 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002497
Per Forlin54d49d72011-07-01 18:55:29 +02002498 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002499
Per Forlin54d49d72011-07-01 18:55:29 +02002500 brq->data.sg = mqrq->sg;
2501 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002502
Per Forlin54d49d72011-07-01 18:55:29 +02002503 /*
2504 * Adjust the sg list so it is the same size as the
2505 * request.
2506 */
2507 if (brq->data.blocks != blk_rq_sectors(req)) {
2508 int i, data_size = brq->data.blocks << 9;
2509 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002510
Per Forlin54d49d72011-07-01 18:55:29 +02002511 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2512 data_size -= sg->length;
2513 if (data_size <= 0) {
2514 sg->length += data_size;
2515 i++;
2516 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002517 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002518 }
Per Forlin54d49d72011-07-01 18:55:29 +02002519 brq->data.sg_len = i;
2520 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002521
Per Forlinee8a43a2011-07-01 18:55:33 +02002522 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302523 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002524 mqrq->mmc_active.err_check = mmc_blk_err_check;
2525
Per Forlin54d49d72011-07-01 18:55:29 +02002526 mmc_queue_bounce_pre(mqrq);
2527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002529static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2530 struct mmc_card *card)
2531{
2532 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2533 unsigned int max_seg_sz = queue_max_segment_size(q);
2534 unsigned int len, nr_segs = 0;
2535
2536 do {
2537 len = min(hdr_sz, max_seg_sz);
2538 hdr_sz -= len;
2539 nr_segs++;
2540 } while (hdr_sz);
2541
2542 return nr_segs;
2543}
2544
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002545/**
2546 * mmc_blk_disable_wr_packing() - disables packing mode
2547 * @mq: MMC queue.
2548 *
2549 */
2550void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2551{
2552 if (mq) {
2553 mq->wr_packing_enabled = false;
2554 mq->num_of_potential_packed_wr_reqs = 0;
2555 }
2556}
2557EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2558
Lee Susman841fd132013-04-23 17:59:26 +03002559static int get_packed_trigger(int potential, struct mmc_card *card,
2560 struct request *req, int curr_trigger)
2561{
2562 static int num_mean_elements = 1;
2563 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2564 unsigned int trigger = curr_trigger;
2565 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2566
2567 /* scale down the upper bound to 75% */
2568 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2569
2570 /*
2571 * since the most common calls for this function are with small
2572 * potential write values and since we don't want these calls to affect
2573 * the packed trigger, set a lower bound and ignore calls with
2574 * potential lower than that bound
2575 */
2576 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2577 return trigger;
2578
2579 /*
2580 * this is to prevent integer overflow in the following calculation:
2581 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2582 */
2583 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2584 num_mean_elements = 1;
2585 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2586 }
2587
2588 /*
2589 * get next mean value based on previous mean value and current
2590 * potential packed writes. Calculation is as follows:
2591 * mean_pot[i+1] =
2592 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2593 */
2594 mean_potential *= num_mean_elements;
2595 /*
2596 * add num_mean_elements so that the division of two integers doesn't
2597 * lower mean_potential too much
2598 */
2599 if (potential > mean_potential)
2600 mean_potential += num_mean_elements;
2601 mean_potential += potential;
2602 /* this is for gaining more precision when dividing two integers */
2603 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2604 /* this completes the mean calculation */
2605 mean_potential /= ++num_mean_elements;
2606 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2607
2608 /*
2609 * if current potential packed writes is greater than the mean potential
2610 * then the heuristic is that the following workload will contain many
2611 * write requests, therefore we lower the packed trigger. In the
2612 * opposite case we want to increase the trigger in order to get less
2613 * packing events.
2614 */
2615 if (potential >= mean_potential)
2616 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2617 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2618 else
2619 trigger = (trigger >= pckd_trgr_upper_bound) ?
2620 pckd_trgr_upper_bound : trigger + 1;
2621
2622 /*
2623 * an urgent read request indicates a packed list being interrupted
2624 * by this read, therefore we aim for less packing, hence the trigger
2625 * gets increased
2626 */
2627 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2628 trigger += PCKD_TRGR_URGENT_PENALTY;
2629
2630 return trigger;
2631}
2632
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002633static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2634 struct request *req)
2635{
2636 struct mmc_host *host = mq->card->host;
2637 int data_dir;
2638
2639 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2640 return;
2641
Maya Erez8e2b3c32012-12-02 13:27:15 +02002642 /* Support for the write packing on eMMC 4.5 or later */
2643 if (mq->card->ext_csd.rev <= 5)
2644 return;
2645
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002646 /*
2647 * In case the packing control is not supported by the host, it should
2648 * not have an effect on the write packing. Therefore we have to enable
2649 * the write packing
2650 */
2651 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2652 mq->wr_packing_enabled = true;
2653 return;
2654 }
2655
2656 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2657 if (mq->num_of_potential_packed_wr_reqs >
2658 mq->num_wr_reqs_to_start_packing)
2659 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002660 mq->num_wr_reqs_to_start_packing =
2661 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2662 mq->card, req,
2663 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002664 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002665 return;
2666 }
2667
2668 data_dir = rq_data_dir(req);
2669
2670 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002671 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002672 mq->num_wr_reqs_to_start_packing =
2673 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2674 mq->card, req,
2675 mq->num_wr_reqs_to_start_packing);
2676 mq->num_of_potential_packed_wr_reqs = 0;
2677 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002678 return;
2679 } else if (data_dir == WRITE) {
2680 mq->num_of_potential_packed_wr_reqs++;
2681 }
2682
2683 if (mq->num_of_potential_packed_wr_reqs >
2684 mq->num_wr_reqs_to_start_packing)
2685 mq->wr_packing_enabled = true;
2686}
2687
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002688struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2689{
2690 if (!card)
2691 return NULL;
2692
2693 return &card->wr_pack_stats;
2694}
2695EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2696
2697void mmc_blk_init_packed_statistics(struct mmc_card *card)
2698{
2699 int max_num_of_packed_reqs = 0;
2700
2701 if (!card || !card->wr_pack_stats.packing_events)
2702 return;
2703
2704 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2705
2706 spin_lock(&card->wr_pack_stats.lock);
2707 memset(card->wr_pack_stats.packing_events, 0,
2708 (max_num_of_packed_reqs + 1) *
2709 sizeof(*card->wr_pack_stats.packing_events));
2710 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2711 sizeof(card->wr_pack_stats.pack_stop_reason));
2712 card->wr_pack_stats.enabled = true;
2713 spin_unlock(&card->wr_pack_stats.lock);
2714}
2715EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2716
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002717static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2718{
2719 struct request_queue *q = mq->queue;
2720 struct mmc_card *card = mq->card;
2721 struct request *cur = req, *next = NULL;
2722 struct mmc_blk_data *md = mq->data;
2723 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2724 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2725 unsigned int req_sectors = 0, phys_segments = 0;
2726 unsigned int max_blk_count, max_phys_segs;
2727 bool put_back = true;
2728 u8 max_packed_rw = 0;
2729 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002730 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002731
Shawn Lin96e52da2016-08-26 08:49:55 +08002732 /*
2733 * We don't need to check packed for any further
2734 * operation of packed stuff as we set MMC_PACKED_NONE
2735 * and return zero for reqs if geting null packed. Also
2736 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2737 * it again when removing blk req.
2738 */
2739 if (!mqrq->packed) {
2740 md->flags &= (~MMC_BLK_PACKED_CMD);
2741 goto no_packed;
2742 }
2743
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002744 if (!(md->flags & MMC_BLK_PACKED_CMD))
2745 goto no_packed;
2746
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002747 if (!mq->wr_packing_enabled)
2748 goto no_packed;
2749
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002750 if ((rq_data_dir(cur) == WRITE) &&
2751 mmc_host_packed_wr(card->host))
2752 max_packed_rw = card->ext_csd.max_packed_writes;
2753
2754 if (max_packed_rw == 0)
2755 goto no_packed;
2756
2757 if (mmc_req_rel_wr(cur) &&
2758 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2759 goto no_packed;
2760
2761 if (mmc_large_sector(card) &&
2762 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2763 goto no_packed;
2764
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002765 if (cur->cmd_flags & REQ_FUA)
2766 goto no_packed;
2767
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002768 mmc_blk_clear_packed(mqrq);
2769
2770 max_blk_count = min(card->host->max_blk_count,
2771 card->host->max_req_size >> 9);
2772 if (unlikely(max_blk_count > 0xffff))
2773 max_blk_count = 0xffff;
2774
2775 max_phys_segs = queue_max_segments(q);
2776 req_sectors += blk_rq_sectors(cur);
2777 phys_segments += cur->nr_phys_segments;
2778
2779 if (rq_data_dir(cur) == WRITE) {
2780 req_sectors += mmc_large_sector(card) ? 8 : 1;
2781 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2782 }
2783
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002784 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002785 do {
2786 if (reqs >= max_packed_rw - 1) {
2787 put_back = false;
2788 break;
2789 }
2790
2791 spin_lock_irq(q->queue_lock);
2792 next = blk_fetch_request(q);
2793 spin_unlock_irq(q->queue_lock);
2794 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002795 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796 put_back = false;
2797 break;
2798 }
2799
2800 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002801 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2802 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002803 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002804 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002805
Mike Christie3a5e02c2016-06-05 14:32:23 -05002806 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002807 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002808 req_op(next) == REQ_OP_FLUSH) {
2809 if (req_op(next) != REQ_OP_SECURE_ERASE)
2810 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002811 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002812 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002813
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002814 if (next->cmd_flags & REQ_FUA) {
2815 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2816 break;
2817 }
2818
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002819 if (rq_data_dir(cur) != rq_data_dir(next)) {
2820 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002821 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002822 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002823
2824 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002825 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2826 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002827 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002828 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002829
2830 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002831 if (req_sectors > max_blk_count) {
2832 if (stats->enabled)
2833 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002834 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002835 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002836
2837 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002838 if (phys_segments > max_phys_segs) {
2839 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002840 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002841 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002842
Maya Erez5a8dae12014-12-04 15:13:59 +02002843 if (mq->no_pack_for_random) {
2844 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2845 blk_rq_pos(next)) {
2846 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2847 put_back = 1;
2848 break;
2849 }
2850 }
2851
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002852 if (rq_data_dir(next) == WRITE)
2853 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002854 list_add_tail(&next->queuelist, &mqrq->packed->list);
2855 cur = next;
2856 reqs++;
2857 } while (1);
2858
2859 if (put_back) {
2860 spin_lock_irq(q->queue_lock);
2861 blk_requeue_request(q, next);
2862 spin_unlock_irq(q->queue_lock);
2863 }
2864
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002865 if (stats->enabled) {
2866 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2867 stats->packing_events[reqs + 1]++;
2868 if (reqs + 1 == max_packed_rw)
2869 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2870 }
2871
2872 spin_unlock(&stats->lock);
2873
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002874 if (reqs > 0) {
2875 list_add(&req->queuelist, &mqrq->packed->list);
2876 mqrq->packed->nr_entries = ++reqs;
2877 mqrq->packed->retries = reqs;
2878 return reqs;
2879 }
2880
2881no_packed:
2882 mqrq->cmd_type = MMC_PACKED_NONE;
2883 return 0;
2884}
2885
2886static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2887 struct mmc_card *card,
2888 struct mmc_queue *mq)
2889{
2890 struct mmc_blk_request *brq = &mqrq->brq;
2891 struct request *req = mqrq->req;
2892 struct request *prq;
2893 struct mmc_blk_data *md = mq->data;
2894 struct mmc_packed *packed = mqrq->packed;
2895 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002896 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002897 u8 hdr_blocks;
2898 u8 i = 1;
2899
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002900 mqrq->cmd_type = MMC_PACKED_WRITE;
2901 packed->blocks = 0;
2902 packed->idx_failure = MMC_PACKED_NR_IDX;
2903
2904 packed_cmd_hdr = packed->cmd_hdr;
2905 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002906 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2907 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002908 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2909
2910 /*
2911 * Argument for each entry of packed group
2912 */
2913 list_for_each_entry(prq, &packed->list, queuelist) {
2914 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2915 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2916 (prq->cmd_flags & REQ_META) &&
2917 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002918 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002919 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002920 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002921 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2922 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002923 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002924 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002925 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002926 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002927 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002928 packed->blocks += blk_rq_sectors(prq);
2929 i++;
2930 }
2931
2932 memset(brq, 0, sizeof(struct mmc_blk_request));
2933 brq->mrq.cmd = &brq->cmd;
2934 brq->mrq.data = &brq->data;
2935 brq->mrq.sbc = &brq->sbc;
2936 brq->mrq.stop = &brq->stop;
2937
2938 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2939 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2940 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2941
2942 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2943 brq->cmd.arg = blk_rq_pos(req);
2944 if (!mmc_card_blockaddr(card))
2945 brq->cmd.arg <<= 9;
2946 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2947
2948 brq->data.blksz = 512;
2949 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002950 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302951 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002952
2953 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2954 brq->stop.arg = 0;
2955 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2956
2957 mmc_set_data_timeout(&brq->data, card);
2958
2959 brq->data.sg = mqrq->sg;
2960 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2961
2962 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002963
2964 /*
2965 * This is intended for packed commands tests usage - in case these
2966 * functions are not in use the respective pointers are NULL
2967 */
2968 if (mq->err_check_fn)
2969 mqrq->mmc_active.err_check = mq->err_check_fn;
2970 else
2971 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2972
2973 if (mq->packed_test_fn)
2974 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002975
2976 mmc_queue_bounce_pre(mqrq);
2977}
2978
Adrian Hunter67716322011-08-29 16:42:15 +03002979static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2980 struct mmc_blk_request *brq, struct request *req,
2981 int ret)
2982{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002983 struct mmc_queue_req *mq_rq;
2984 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2985
Adrian Hunter67716322011-08-29 16:42:15 +03002986 /*
2987 * If this is an SD card and we're writing, we can first
2988 * mark the known good sectors as ok.
2989 *
2990 * If the card is not SD, we can still ok written sectors
2991 * as reported by the controller (which might be less than
2992 * the real number of written sectors, but never more).
2993 */
2994 if (mmc_card_sd(card)) {
2995 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302996 if (!brq->data.fault_injected) {
2997 blocks = mmc_sd_num_wr_blocks(card);
2998 if (blocks != (u32)-1)
2999 ret = blk_end_request(req, 0, blocks << 9);
3000 } else
3001 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03003002 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003003 if (!mmc_packed_cmd(mq_rq->cmd_type))
3004 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03003005 }
3006 return ret;
3007}
3008
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003009static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
3010{
3011 struct request *prq;
3012 struct mmc_packed *packed = mq_rq->packed;
3013 int idx = packed->idx_failure, i = 0;
3014 int ret = 0;
3015
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003016 while (!list_empty(&packed->list)) {
3017 prq = list_entry_rq(packed->list.next);
3018 if (idx == i) {
3019 /* retry from error index */
3020 packed->nr_entries -= idx;
3021 mq_rq->req = prq;
3022 ret = 1;
3023
3024 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
3025 list_del_init(&prq->queuelist);
3026 mmc_blk_clear_packed(mq_rq);
3027 }
3028 return ret;
3029 }
3030 list_del_init(&prq->queuelist);
3031 blk_end_request(prq, 0, blk_rq_bytes(prq));
3032 i++;
3033 }
3034
3035 mmc_blk_clear_packed(mq_rq);
3036 return ret;
3037}
3038
3039static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3040{
3041 struct request *prq;
3042 struct mmc_packed *packed = mq_rq->packed;
3043
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003044 while (!list_empty(&packed->list)) {
3045 prq = list_entry_rq(packed->list.next);
3046 list_del_init(&prq->queuelist);
3047 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3048 }
3049
3050 mmc_blk_clear_packed(mq_rq);
3051}
3052
3053static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3054 struct mmc_queue_req *mq_rq)
3055{
3056 struct request *prq;
3057 struct request_queue *q = mq->queue;
3058 struct mmc_packed *packed = mq_rq->packed;
3059
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003060 while (!list_empty(&packed->list)) {
3061 prq = list_entry_rq(packed->list.prev);
3062 if (prq->queuelist.prev != &packed->list) {
3063 list_del_init(&prq->queuelist);
3064 spin_lock_irq(q->queue_lock);
3065 blk_requeue_request(mq->queue, prq);
3066 spin_unlock_irq(q->queue_lock);
3067 } else {
3068 list_del_init(&prq->queuelist);
3069 }
3070 }
3071
3072 mmc_blk_clear_packed(mq_rq);
3073}
3074
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003075static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3076 struct mmc_cmdq_req *cmdq_req)
3077{
3078 struct mmc_request *mrq = &cmdq_req->mrq;
3079
3080 mrq->done = mmc_blk_cmdq_req_done;
3081 return mmc_cmdq_start_req(host, cmdq_req);
3082}
3083
Asutosh Das5238e022015-04-23 16:00:45 +05303084/* prepare for non-data commands */
3085static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3086 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3087{
3088 struct request *req = mqrq->req;
3089 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3090
3091 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3092
3093 cmdq_req->mrq.data = NULL;
3094 cmdq_req->cmd_flags = req->cmd_flags;
3095 cmdq_req->mrq.req = mqrq->req;
3096 req->special = mqrq;
3097 cmdq_req->cmdq_req_flags |= DCMD;
3098 cmdq_req->mrq.cmdq_req = cmdq_req;
3099
3100 return &mqrq->cmdq_req;
3101}
3102
3103
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003104#define IS_RT_CLASS_REQ(x) \
3105 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3106
3107static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3108 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3109{
3110 struct mmc_card *card = mq->card;
3111 struct request *req = mqrq->req;
3112 struct mmc_blk_data *md = mq->data;
3113 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3114 bool do_data_tag;
3115 bool read_dir = (rq_data_dir(req) == READ);
3116 bool prio = IS_RT_CLASS_REQ(req);
3117 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3118
3119 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3120
3121 cmdq_rq->tag = req->tag;
3122 if (read_dir) {
3123 cmdq_rq->cmdq_req_flags |= DIR;
3124 cmdq_rq->data.flags = MMC_DATA_READ;
3125 } else {
3126 cmdq_rq->data.flags = MMC_DATA_WRITE;
3127 }
3128 if (prio)
3129 cmdq_rq->cmdq_req_flags |= PRIO;
3130
3131 if (do_rel_wr)
3132 cmdq_rq->cmdq_req_flags |= REL_WR;
3133
3134 cmdq_rq->data.blocks = blk_rq_sectors(req);
3135 cmdq_rq->blk_addr = blk_rq_pos(req);
3136 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3137
3138 mmc_set_data_timeout(&cmdq_rq->data, card);
3139
3140 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3141 (req->cmd_flags & REQ_META) &&
3142 (rq_data_dir(req) == WRITE) &&
3143 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3144 card->ext_csd.data_tag_unit_size);
3145 if (do_data_tag)
3146 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3147 cmdq_rq->data.sg = mqrq->sg;
3148 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3149
3150 /*
3151 * Adjust the sg list so it is the same size as the
3152 * request.
3153 */
3154 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3155 cmdq_rq->data.blocks = card->host->max_blk_count;
3156
3157 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3158 int i, data_size = cmdq_rq->data.blocks << 9;
3159 struct scatterlist *sg;
3160
3161 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3162 data_size -= sg->length;
3163 if (data_size <= 0) {
3164 sg->length += data_size;
3165 i++;
3166 break;
3167 }
3168 }
3169 cmdq_rq->data.sg_len = i;
3170 }
3171
3172 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3173 mqrq->cmdq_req.mrq.req = mqrq->req;
3174 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3175 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3176 mqrq->req->special = mqrq;
3177
3178 pr_debug("%s: %s: mrq: 0x%p req: 0x%p mqrq: 0x%p bytes to xf: %d mmc_cmdq_req: 0x%p card-addr: 0x%08x dir(r-1/w-0): %d\n",
3179 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3180 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3181 cmdq_rq, cmdq_rq->blk_addr,
3182 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3183
3184 return &mqrq->cmdq_req;
3185}
3186
3187static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3188{
3189 struct mmc_queue_req *active_mqrq;
3190 struct mmc_card *card = mq->card;
3191 struct mmc_host *host = card->host;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003192 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003193 struct mmc_cmdq_req *mc_rq;
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003194 u8 active_small_sector_read = 0;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003195 int ret = 0;
3196
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003197 mmc_deferred_scaling(host);
3198 mmc_cmdq_clk_scaling_start_busy(host, true);
3199
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003200 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003201 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003202 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3203
3204 active_mqrq = &mq->mqrq_cmdq[req->tag];
3205 active_mqrq->req = req;
3206
3207 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3208
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003209 if (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003210 unsigned int sectors = blk_rq_sectors(req);
3211
3212 if (((sectors > 0) && (sectors < 8))
3213 && (rq_data_dir(req) == READ))
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003214 active_small_sector_read = 1;
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003215 }
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003216 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3217 if (!ret && active_small_sector_read)
3218 host->cmdq_ctx.active_small_sector_read_reqs++;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003219 /*
3220 * When in SVS2 on low load scenario and there are lots of requests
3221 * queued for CMDQ we need to wait till the queue is empty to scale
3222 * back up to Nominal even if there is a sudden increase in load.
3223 * This impacts performance where lots of IO get executed in SVS2
3224 * frequency since the queue is full. As SVS2 is a low load use case
3225 * we can serialize the requests and not queue them in parallel
3226 * without impacting other use cases. This makes sure the queue gets
3227 * empty faster and we will be able to scale up to Nominal frequency
3228 * when needed.
3229 */
3230 if (!ret && (host->clk_scaling.state == MMC_LOAD_LOW))
3231 wait_event_interruptible(ctx->queue_empty_wq,
3232 (!ctx->active_reqs));
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003233
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003234 return ret;
3235}
3236
Asutosh Das5238e022015-04-23 16:00:45 +05303237/*
3238 * Issues a flush (dcmd) request
3239 */
3240int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3241{
3242 int err;
3243 struct mmc_queue_req *active_mqrq;
3244 struct mmc_card *card = mq->card;
3245 struct mmc_host *host;
3246 struct mmc_cmdq_req *cmdq_req;
3247 struct mmc_cmdq_context_info *ctx_info;
3248
3249 BUG_ON(!card);
3250 host = card->host;
3251 BUG_ON(!host);
3252 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3253 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3254
3255 ctx_info = &host->cmdq_ctx;
3256
3257 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3258
3259 active_mqrq = &mq->mqrq_cmdq[req->tag];
3260 active_mqrq->req = req;
3261
3262 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3263 cmdq_req->cmdq_req_flags |= QBR;
3264 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3265 cmdq_req->tag = req->tag;
3266
3267 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3268 if (err) {
3269 pr_err("%s: failed (%d) preparing flush req\n",
3270 mmc_hostname(host), err);
3271 return err;
3272 }
3273 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3274 return err;
3275}
3276EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3277
Asutosh Das02e30862015-05-20 16:52:04 +05303278static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3279{
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303280 int err = 0;
Asutosh Das02e30862015-05-20 16:52:04 +05303281
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303282 if (mmc_cmdq_halt(host, true)) {
3283 pr_err("%s: halt failed\n", mmc_hostname(host));
3284 goto reset;
Asutosh Das02e30862015-05-20 16:52:04 +05303285 }
3286
3287 if (clear_all)
3288 mmc_cmdq_discard_queue(host, 0);
3289reset:
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303290 mmc_host_clk_hold(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303291 host->cmdq_ops->disable(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303292 mmc_host_clk_release(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303293 err = mmc_cmdq_hw_reset(host);
3294 if (err && err != -EOPNOTSUPP) {
3295 pr_err("%s: failed to cmdq_hw_reset err = %d\n",
3296 mmc_hostname(host), err);
3297 mmc_host_clk_hold(host);
3298 host->cmdq_ops->enable(host);
3299 mmc_host_clk_release(host);
3300 mmc_cmdq_halt(host, false);
3301 goto out;
3302 }
3303 /*
3304 * CMDQ HW reset would have already made CQE
3305 * in unhalted state, but reflect the same
3306 * in software state of cmdq_ctx.
3307 */
3308 mmc_host_clr_halt(host);
3309out:
3310 return;
3311}
3312
3313/**
3314 * is_cmdq_dcmd_req - Checks if tag belongs to DCMD request.
3315 * @q: request_queue pointer.
3316 * @tag: tag number of request to check.
3317 *
3318 * This function checks if the request with tag number "tag"
3319 * is a DCMD request or not based on cmdq_req_flags set.
3320 *
3321 * returns true if DCMD req, otherwise false.
3322 */
3323static bool is_cmdq_dcmd_req(struct request_queue *q, int tag)
3324{
3325 struct request *req;
3326 struct mmc_queue_req *mq_rq;
3327 struct mmc_cmdq_req *cmdq_req;
3328
3329 req = blk_queue_find_tag(q, tag);
3330 if (WARN_ON(!req))
3331 goto out;
3332 mq_rq = req->special;
3333 if (WARN_ON(!mq_rq))
3334 goto out;
3335 cmdq_req = &(mq_rq->cmdq_req);
3336 return (cmdq_req->cmdq_req_flags & DCMD);
3337out:
3338 return -ENOENT;
3339}
3340
3341/**
3342 * mmc_blk_cmdq_reset_all - Reset everything for CMDQ block request.
3343 * @host: mmc_host pointer.
3344 * @err: error for which reset is performed.
3345 *
3346 * This function implements reset_all functionality for
3347 * cmdq. It resets the controller, power cycle the card,
3348 * and invalidate all busy tags(requeue all request back to
3349 * elevator).
3350 */
3351static void mmc_blk_cmdq_reset_all(struct mmc_host *host, int err)
3352{
3353 struct mmc_request *mrq = host->err_mrq;
3354 struct mmc_card *card = host->card;
3355 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3356 struct request_queue *q;
3357 int itag = 0;
3358 int ret = 0;
3359
3360 if (WARN_ON(!mrq))
3361 return;
3362
3363 q = mrq->req->q;
3364 WARN_ON(!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
3365
3366 #ifdef CONFIG_MMC_CLKGATE
3367 pr_debug("%s: %s: active_reqs = %lu, clk_requests = %d\n",
3368 mmc_hostname(host), __func__,
3369 ctx_info->active_reqs, host->clk_requests);
3370 #endif
3371
3372 mmc_blk_cmdq_reset(host, false);
3373
3374 for_each_set_bit(itag, &ctx_info->active_reqs,
3375 host->num_cq_slots) {
3376 ret = is_cmdq_dcmd_req(q, itag);
3377 if (WARN_ON(ret == -ENOENT))
3378 continue;
3379 if (!ret) {
3380 WARN_ON(!test_and_clear_bit(itag,
3381 &ctx_info->data_active_reqs));
Ritesh Harjanid99753e2015-11-23 11:46:45 +05303382 mmc_cmdq_post_req(host, itag, err);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303383 } else {
3384 clear_bit(CMDQ_STATE_DCMD_ACTIVE,
3385 &ctx_info->curr_state);
3386 }
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303387 WARN_ON(!test_and_clear_bit(itag,
3388 &ctx_info->active_reqs));
3389 mmc_host_clk_release(host);
3390 mmc_put_card(card);
3391 }
3392
3393 spin_lock_irq(q->queue_lock);
3394 blk_queue_invalidate_tags(q);
3395 spin_unlock_irq(q->queue_lock);
Asutosh Das02e30862015-05-20 16:52:04 +05303396}
3397
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303398static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3399{
3400 int err;
3401 struct mmc_card *card = mq->card;
3402 struct mmc_host *host = card->host;
3403
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003404 mmc_get_card(card);
3405 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303406 err = mmc_cmdq_halt(host, true);
3407 if (err) {
3408 pr_err("%s: halt: failed: %d\n", __func__, err);
Subhash Jadavanie88f5e22015-10-20 18:01:53 -07003409 goto out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303410 }
3411
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303412 /* disable CQ mode in card */
Sahitya Tummala92136382015-11-05 10:07:42 +05303413 if (mmc_card_cmdq(card)) {
3414 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3415 EXT_CSD_CMDQ, 0,
3416 card->ext_csd.generic_cmd6_time);
3417 if (err) {
3418 pr_err("%s: failed to switch card to legacy mode: %d\n",
3419 __func__, err);
3420 goto out;
3421 }
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003422 mmc_card_clr_cmdq(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303423 }
Sahitya Tummala92136382015-11-05 10:07:42 +05303424 host->cmdq_ops->disable(host, false);
3425 host->card->cmdq_init = false;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303426out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003427 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003428 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303429}
3430
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303431static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3432{
3433 struct mmc_queue *mq = req->q->queuedata;
3434 struct mmc_host *host = mq->card->host;
3435 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003436 struct mmc_request *mrq;
3437 struct mmc_cmdq_req *cmdq_req;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303438 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003439
3440 BUG_ON(!host);
3441
3442 /*
3443 * The mmc_queue_req will be present only if the request
3444 * is issued to the LLD. The request could be fetched from
3445 * block layer queue but could be waiting to be issued
3446 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3447 * Reset the timer in such cases to give LLD more time
3448 */
3449 if (!mq_rq) {
3450 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3451 return BLK_EH_RESET_TIMER;
3452 }
3453
3454 mrq = &mq_rq->cmdq_req.mrq;
3455 cmdq_req = &mq_rq->cmdq_req;
3456
3457 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303458
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303459 if (cmdq_req->cmdq_req_flags & DCMD)
3460 mrq->cmd->error = -ETIMEDOUT;
3461 else
3462 mrq->data->error = -ETIMEDOUT;
3463
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05303464 if (mrq->cmd && mrq->cmd->error) {
3465 if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
3466 /*
3467 * Notify completion for non flush commands like
3468 * discard that wait for DCMD finish.
3469 */
3470 set_bit(CMDQ_STATE_REQ_TIMED_OUT,
3471 &ctx_info->curr_state);
3472 complete(&mrq->completion);
3473 return BLK_EH_NOT_HANDLED;
3474 }
3475 }
3476
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303477 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state) ||
3478 test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state))
3479 return BLK_EH_NOT_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303480
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303481 set_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003482 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303483}
3484
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303485/*
3486 * mmc_blk_cmdq_err: error handling of cmdq error requests.
3487 * Function should be called in context of error out request
3488 * which has claim_host and rpm acquired.
3489 * This may be called with CQ engine halted. Make sure to
3490 * unhalt it after error recovery.
3491 *
3492 * TODO: Currently cmdq error handler does reset_all in case
3493 * of any erorr. Need to optimize error handling.
3494 */
Asutosh Das02e30862015-05-20 16:52:04 +05303495static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3496{
Asutosh Das02e30862015-05-20 16:52:04 +05303497 struct mmc_host *host = mq->card->host;
3498 struct mmc_request *mrq = host->err_mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303499 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303500 struct request_queue *q;
Sahitya Tummala1c73e1c2016-04-26 12:18:35 +05303501 int err, ret;
3502 u32 status = 0;
Asutosh Das02e30862015-05-20 16:52:04 +05303503
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003504 mmc_host_clk_hold(host);
3505 host->cmdq_ops->dumpstate(host);
3506 mmc_host_clk_release(host);
3507
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303508 if (WARN_ON(!mrq))
3509 return;
3510
3511 q = mrq->req->q;
Asutosh Das02e30862015-05-20 16:52:04 +05303512 err = mmc_cmdq_halt(host, true);
3513 if (err) {
3514 pr_err("halt: failed: %d\n", err);
3515 goto reset;
3516 }
3517
3518 /* RED error - Fatal: requires reset */
3519 if (mrq->cmdq_req->resp_err) {
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303520 err = mrq->cmdq_req->resp_err;
Vijay Viswanatheed6d1c2016-11-02 16:17:30 +05303521 goto reset;
3522 }
3523
3524 /*
3525 * TIMEOUT errrors can happen because of execution error
3526 * in the last command. So send cmd 13 to get device status
3527 */
3528 if ((mrq->cmd && (mrq->cmd->error == -ETIMEDOUT)) ||
3529 (mrq->data && (mrq->data->error == -ETIMEDOUT))) {
Sahitya Tummala1c73e1c2016-04-26 12:18:35 +05303530 if (mmc_host_halt(host) || mmc_host_cq_disable(host)) {
3531 ret = get_card_status(host->card, &status, 0);
3532 if (ret)
3533 pr_err("%s: CMD13 failed with err %d\n",
3534 mmc_hostname(host), ret);
3535 }
Vijay Viswanatheed6d1c2016-11-02 16:17:30 +05303536 pr_err("%s: Timeout error detected with device status 0x%08x\n",
Sahitya Tummala1c73e1c2016-04-26 12:18:35 +05303537 mmc_hostname(host), status);
Asutosh Das02e30862015-05-20 16:52:04 +05303538 }
3539
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303540 /*
3541 * In case of software request time-out, we schedule err work only for
3542 * the first error out request and handles all other request in flight
3543 * here.
3544 */
3545 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state)) {
3546 err = -ETIMEDOUT;
3547 } else if (mrq->data && mrq->data->error) {
3548 err = mrq->data->error;
3549 } else if (mrq->cmd && mrq->cmd->error) {
3550 /* DCMD commands */
3551 err = mrq->cmd->error;
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003552 }
Asutosh Das02e30862015-05-20 16:52:04 +05303553
3554reset:
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303555 mmc_blk_cmdq_reset_all(host, err);
3556 if (mrq->cmdq_req->resp_err)
3557 mrq->cmdq_req->resp_err = false;
Asutosh Das02e30862015-05-20 16:52:04 +05303558 mmc_cmdq_halt(host, false);
3559
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003560 host->err_mrq = NULL;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303561 clear_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
3562 WARN_ON(!test_and_clear_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Subhash Jadavani987144c2015-08-12 17:53:21 -07003563 wake_up(&ctx_info->wait);
Asutosh Das02e30862015-05-20 16:52:04 +05303564}
3565
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003566/* invoked by block layer in softirq context */
3567void mmc_blk_cmdq_complete_rq(struct request *rq)
3568{
3569 struct mmc_queue_req *mq_rq = rq->special;
3570 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3571 struct mmc_host *host = mrq->host;
3572 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3573 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3574 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3575 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003576 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003577
3578 if (mrq->cmd && mrq->cmd->error)
3579 err = mrq->cmd->error;
3580 else if (mrq->data && mrq->data->error)
3581 err = mrq->data->error;
3582
Veerabhadrarao Badiganti459a9322017-02-21 15:06:23 +05303583 if ((err || cmdq_req->resp_err) && !cmdq_req->skip_err_handling) {
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303584 pr_err("%s: %s: txfr error(%d)/resp_err(%d)\n",
3585 mmc_hostname(mrq->host), __func__, err,
3586 cmdq_req->resp_err);
Asutosh Das02e30862015-05-20 16:52:04 +05303587 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3588 pr_err("%s: CQ in error state, ending current req: %d\n",
3589 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303590 } else {
3591 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303592 BUG_ON(host->err_mrq != NULL);
3593 host->err_mrq = mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303594 schedule_work(&mq->cmdq_err_work);
3595 }
3596 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003597 }
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303598 /*
3599 * In case of error CMDQ is expected to be either in halted
3600 * or disable state so cannot receive any completion of
3601 * other requests.
3602 */
3603 BUG_ON(test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003604
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303605 /* clear pending request */
3606 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3607 &ctx_info->active_reqs));
3608 if (cmdq_req->cmdq_req_flags & DCMD)
3609 is_dcmd = true;
3610 else
3611 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3612 &ctx_info->data_active_reqs));
Ritesh Harjanid99753e2015-11-23 11:46:45 +05303613 if (!is_dcmd)
3614 mmc_cmdq_post_req(host, cmdq_req->tag, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303615 if (cmdq_req->cmdq_req_flags & DCMD) {
3616 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303617 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303618 goto out;
3619 }
Veerabhadrarao Badiganti459a9322017-02-21 15:06:23 +05303620 /*
3621 * In case of error, cmdq_req->data.bytes_xfered is set to 0.
3622 * If we call blk_end_request() with nr_bytes as 0 then the request
3623 * never gets completed. So in case of error, to complete a request
3624 * with error we should use blk_end_request_all().
3625 */
3626 if (err && cmdq_req->skip_err_handling) {
3627 cmdq_req->skip_err_handling = false;
3628 blk_end_request_all(rq, err);
3629 goto out;
3630 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003631
3632 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3633
Asutosh Das5238e022015-04-23 16:00:45 +05303634out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003635
3636 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303637 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
Sahitya Tummalaa5dd04f2016-03-03 15:07:24 +05303638 mmc_host_clk_release(host);
Subhash Jadavani987144c2015-08-12 17:53:21 -07003639 wake_up(&ctx_info->wait);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303640 mmc_put_card(host->card);
3641 }
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303642
Talel Shenhar339a4e12015-06-29 10:50:19 +03003643 if (!ctx_info->active_reqs)
3644 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3645
Ritesh Harjani7a610872016-02-15 14:20:21 +05303646 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3647 complete(&mq->cmdq_shutdown_complete);
3648
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003649 return;
3650}
3651
3652/*
3653 * Complete reqs from block layer softirq context
3654 * Invoked in irq context
3655 */
3656void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3657{
3658 struct request *req = mrq->req;
3659
3660 blk_complete_request(req);
3661}
3662EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3663
Per Forlinee8a43a2011-07-01 18:55:33 +02003664static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003665{
3666 struct mmc_blk_data *md = mq->data;
3667 struct mmc_card *card = md->queue.card;
3668 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003669 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003670 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003671 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303672 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003673 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003674 const u8 packed_nr = 2;
3675 u8 reqs = 0;
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07003676 bool reset = false;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003677#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3678 unsigned long waitfor = jiffies;
3679#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003680
3681 if (!rqc && !mq->mqrq_prev->req)
3682 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003683
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003684 if (rqc)
3685 reqs = mmc_blk_prep_packed_list(mq, rqc);
3686
Per Forlin54d49d72011-07-01 18:55:29 +02003687 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003688 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303689 /*
3690 * When 4KB native sector is enabled, only 8 blocks
3691 * multiple read or write is allowed
3692 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003693 if (mmc_large_sector(card) &&
3694 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303695 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3696 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003697 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303698 goto cmd_abort;
3699 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003700
3701 if (reqs >= packed_nr)
3702 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3703 card, mq);
3704 else
3705 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003706 areq = &mq->mqrq_cur->mmc_active;
3707 } else
3708 areq = NULL;
3709 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003710 if (!areq) {
3711 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003712 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003713 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003714 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003715
Per Forlinee8a43a2011-07-01 18:55:33 +02003716 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3717 brq = &mq_rq->brq;
3718 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003719 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003720 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003721
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07003722 if (card->err_in_sdr104) {
3723 /*
3724 * Data CRC/timeout errors will manifest as CMD/DATA
3725 * ERR. But we'd like to retry these too.
3726 * Moreover, no harm done if this fails too for multiple
3727 * times, we anyway reduce the bus-speed and retry the
3728 * same request.
3729 * If that fails too, we don't override this status.
3730 */
3731 if (status == MMC_BLK_ABORT ||
3732 status == MMC_BLK_CMD_ERR ||
3733 status == MMC_BLK_DATA_ERR ||
3734 status == MMC_BLK_RETRY)
3735 /* reset on all of these errors and retry */
3736 reset = true;
3737
3738 status = MMC_BLK_RETRY;
3739 card->err_in_sdr104 = false;
3740 }
3741
Per Forlind78d4a82011-07-01 18:55:30 +02003742 switch (status) {
3743 case MMC_BLK_SUCCESS:
3744 case MMC_BLK_PARTIAL:
3745 /*
3746 * A block was successfully transferred.
3747 */
Adrian Hunter67716322011-08-29 16:42:15 +03003748 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003749
Mark Salyzyn6904e432016-01-28 11:12:25 -08003750 mmc_blk_simulate_delay(mq, rqc, waitfor);
3751
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003752 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3753 ret = mmc_blk_end_packed_req(mq_rq);
3754 break;
3755 } else {
3756 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003757 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003758 }
3759
Adrian Hunter67716322011-08-29 16:42:15 +03003760 /*
3761 * If the blk_end_request function returns non-zero even
3762 * though all data has been transferred and no errors
3763 * were returned by the host controller, it's a bug.
3764 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003765 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303766 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003767 __func__, blk_rq_bytes(req),
3768 brq->data.bytes_xfered);
3769 rqc = NULL;
3770 goto cmd_abort;
3771 }
Per Forlind78d4a82011-07-01 18:55:30 +02003772 break;
3773 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003774 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003775 if (mmc_blk_reset(md, card->host, type))
3776 goto cmd_abort;
3777 if (!ret)
3778 goto start_new_req;
3779 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003780 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003781 retune_retry_done = brq->retune_retry_done;
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07003782 if (retry++ < MMC_BLK_MAX_RETRIES) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003783 break;
Asutosh Dasaf6a03e2017-03-30 14:41:16 -07003784 } else if (reset) {
3785 reset = false;
3786 /*
3787 * If we exhaust all the retries due to
3788 * CRC/timeout errors in SDR140 mode with UHS SD
3789 * cards, re-configure the card in SDR50
3790 * bus-speed mode.
3791 * All subsequent re-init of this card will be
3792 * in SDR50 mode, unless it is removed and
3793 * re-inserted. When new UHS SD cards are
3794 * inserted, it may start at SDR104 mode if
3795 * supported by the card.
3796 */
3797 pr_err("%s: blocked SDR104, lower the bus-speed (SDR50 / DDR50)\n",
3798 req->rq_disk->disk_name);
3799 mmc_host_clear_sdr104(card->host);
3800 mmc_suspend_clk_scaling(card->host);
3801 mmc_blk_reset(md, card->host, type);
3802 /* SDR104 mode is blocked from now on */
3803 card->sdr104_blocked = true;
3804 /* retry 5 times again */
3805 retry = 0;
3806 break;
3807 }
Adrian Hunter67716322011-08-29 16:42:15 +03003808 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003809 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003810 if (!mmc_blk_reset(md, card->host, type) &&
3811 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003812 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003813 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003814 case MMC_BLK_DATA_ERR: {
3815 int err;
3816
3817 err = mmc_blk_reset(md, card->host, type);
3818 if (!err)
3819 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303820 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003821 }
3822 case MMC_BLK_ECC_ERR:
3823 if (brq->data.blocks > 1) {
3824 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003825 pr_warn("%s: retrying using single block read\n",
3826 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003827 disable_multi = 1;
3828 break;
3829 }
Per Forlind78d4a82011-07-01 18:55:30 +02003830 /*
3831 * After an error, we redo I/O one sector at a
3832 * time, so we only reach here after trying to
3833 * read a single sector.
3834 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303835 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003836 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003837 if (!ret)
3838 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003839 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303840 case MMC_BLK_NOMEDIUM:
3841 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003842 default:
3843 pr_err("%s: Unhandled return value (%d)",
3844 req->rq_disk->disk_name, status);
3845 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003846 }
3847
Per Forlinee8a43a2011-07-01 18:55:33 +02003848 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003849 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3850 if (!mq_rq->packed->retries)
3851 goto cmd_abort;
3852 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3853 mmc_start_req(card->host,
3854 &mq_rq->mmc_active, NULL);
3855 } else {
3856
3857 /*
3858 * In case of a incomplete request
3859 * prepare it again and resend.
3860 */
3861 mmc_blk_rw_rq_prep(mq_rq, card,
3862 disable_multi, mq);
3863 mmc_start_req(card->host,
3864 &mq_rq->mmc_active, NULL);
3865 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003866 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 } while (ret);
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return 1;
3871
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003872 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003873 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3874 mmc_blk_abort_packed_req(mq_rq);
3875 } else {
3876 if (mmc_card_removed(card))
3877 req->cmd_flags |= REQ_QUIET;
3878 while (ret)
3879 ret = blk_end_request(req, -EIO,
3880 blk_rq_cur_bytes(req));
3881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Per Forlinee8a43a2011-07-01 18:55:33 +02003883 start_new_req:
3884 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003885 if (mmc_card_removed(card)) {
3886 rqc->cmd_flags |= REQ_QUIET;
3887 blk_end_request_all(rqc, -EIO);
3888 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003889 /*
3890 * If current request is packed, it needs to put back.
3891 */
3892 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3893 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3894
Seungwon Jeon7a819022013-01-22 19:48:07 +09003895 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3896 mmc_start_req(card->host,
3897 &mq->mqrq_cur->mmc_active, NULL);
3898 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003899 }
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return 0;
3902}
3903
Asutosh Das8b594832015-04-23 09:55:43 +05303904static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3905 struct mmc_blk_data *md)
3906{
3907 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3908 struct mmc_host *host = card->host;
3909 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3910 u8 part_config = card->ext_csd.part_config;
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303911 int ret = 0, err = 0;
Asutosh Das8b594832015-04-23 09:55:43 +05303912
3913 if ((main_md->part_curr == md->part_type) &&
3914 (card->part_curr == md->part_type))
3915 return 0;
3916
3917 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3918 card->ext_csd.cmdq_support &&
3919 (md->flags & MMC_BLK_CMD_QUEUE)));
3920
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303921 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
3922 ret = mmc_cmdq_halt(host, true);
3923 if (ret) {
3924 pr_err("%s: %s: halt: failed: %d\n",
3925 mmc_hostname(host), __func__, ret);
3926 goto out;
3927 }
3928 }
Asutosh Das8b594832015-04-23 09:55:43 +05303929
3930 /* disable CQ mode in card */
3931 if (mmc_card_cmdq(card)) {
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303932 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Asutosh Das8b594832015-04-23 09:55:43 +05303933 EXT_CSD_CMDQ, 0,
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303934 card->ext_csd.generic_cmd6_time);
3935 if (ret) {
3936 pr_err("%s: %s: cmdq mode disable failed %d\n",
3937 mmc_hostname(host), __func__, ret);
3938 goto cmdq_unhalt;
3939 }
Asutosh Das8b594832015-04-23 09:55:43 +05303940 mmc_card_clr_cmdq(card);
3941 }
3942
3943 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3944 part_config |= md->part_type;
3945
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303946 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Asutosh Das8b594832015-04-23 09:55:43 +05303947 EXT_CSD_PART_CONFIG, part_config,
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303948 card->ext_csd.part_time);
3949 if (ret) {
3950 pr_err("%s: %s: mmc_switch failure, %d -> %d , err = %d\n",
3951 mmc_hostname(host), __func__, main_md->part_curr,
3952 md->part_type, ret);
3953 goto cmdq_switch;
3954 }
Asutosh Das8b594832015-04-23 09:55:43 +05303955
3956 card->ext_csd.part_config = part_config;
3957 card->part_curr = md->part_type;
3958
3959 main_md->part_curr = md->part_type;
3960
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303961cmdq_switch:
3962 err = mmc_blk_cmdq_switch(card, md, true);
3963 if (err) {
3964 pr_err("%s: %s: mmc_blk_cmdq_switch failed: %d\n",
3965 mmc_hostname(host), __func__, err);
3966 ret = err;
3967 }
3968cmdq_unhalt:
3969 err = mmc_cmdq_halt(host, false);
3970 if (err) {
3971 pr_err("%s: %s: unhalt: failed: %d\n",
3972 mmc_hostname(host), __func__, err);
3973 ret = err;
3974 }
3975out:
3976 return ret;
Asutosh Das8b594832015-04-23 09:55:43 +05303977}
3978
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003979static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3980{
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303981 int ret, err = 0;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003982 struct mmc_blk_data *md = mq->data;
3983 struct mmc_card *card = md->queue.card;
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05303984 struct mmc_host *host = card->host;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003985
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003986 mmc_get_card(card);
Maya Ereza2152152015-09-21 14:43:38 +03003987
3988 if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
3989 ret = mmc_cmdq_halt(card->host, true);
3990 if (ret)
3991 goto out;
3992 ret = mmc_stop_bkops(card);
3993 if (ret) {
3994 pr_err("%s: %s: mmc_stop_bkops failed %d\n",
3995 md->disk->disk_name, __func__, ret);
3996 goto out;
3997 }
3998 ret = mmc_cmdq_halt(card->host, false);
3999 if (ret)
4000 goto out;
4001 }
4002
Asutosh Das8b594832015-04-23 09:55:43 +05304003 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004004 if (ret) {
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05304005 pr_err("%s: %s: partition switch failed %d, resetting cmdq\n",
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004006 md->disk->disk_name, __func__, ret);
Sayali Lokhande3d2e6402017-08-09 15:38:28 +05304007
4008 mmc_blk_cmdq_reset(host, false);
4009 err = mmc_blk_cmdq_part_switch(card, md);
4010 if (!err) {
4011 pr_err("%s: %s: partition switch success err = %d\n",
4012 md->disk->disk_name, __func__, err);
4013 } else {
4014 pr_err("%s: %s: partition switch failed err = %d\n",
4015 md->disk->disk_name, __func__, err);
4016 ret = err;
4017 goto out;
4018 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004019 }
4020
Asutosh Das5238e022015-04-23 16:00:45 +05304021 if (req) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08004022 struct mmc_host *host = card->host;
4023 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
4024
4025 if ((req_op(req) == REQ_OP_FLUSH || req_op(req) == REQ_OP_DISCARD) &&
4026 (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) &&
4027 ctx->active_small_sector_read_reqs) {
4028 ret = wait_event_interruptible(ctx->queue_empty_wq,
4029 !ctx->active_reqs);
4030 if (ret) {
4031 pr_err("%s: failed while waiting for the CMDQ to be empty %s err (%d)\n",
4032 mmc_hostname(host),
4033 __func__, ret);
4034 BUG_ON(1);
4035 }
4036 /* clear the counter now */
4037 ctx->active_small_sector_read_reqs = 0;
4038 /*
4039 * If there were small sector (less than 8 sectors) read
4040 * operations in progress then we have to wait for the
4041 * outstanding requests to finish and should also have
4042 * atleast 6 microseconds delay before queuing the DCMD
4043 * request.
4044 */
4045 udelay(MMC_QUIRK_CMDQ_DELAY_BEFORE_DCMD);
4046 }
4047
Sahitya Tummala9433a132015-06-09 09:38:36 +05304048 if (req_op(req) == REQ_OP_DISCARD) {
4049 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
4050 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
4051 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
4052 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
4053 else
4054 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
4055 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05304056 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05304057 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05304058 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05304059 }
Asutosh Das5238e022015-04-23 16:00:45 +05304060 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004061
Maya Ereza2152152015-09-21 14:43:38 +03004062 return ret;
4063
4064out:
4065 if (req)
4066 blk_end_request_all(req, ret);
4067 mmc_put_card(card);
4068
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004069 return ret;
4070}
4071
Linus Walleij29eb7bd2016-09-20 11:34:38 +02004072int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07004073{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004074 int ret;
4075 struct mmc_blk_data *md = mq->data;
4076 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05004077 struct mmc_host *host = card->host;
4078 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05304079 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06004080 bool req_is_special = mmc_req_is_special(req);
Pavan Anamula973ba1c2015-11-20 15:23:47 +05304081 int err;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004082
Dov Levenglicka0296392015-06-24 19:51:58 +03004083 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02004084 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004085 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02004086
Dov Levenglicka0296392015-06-24 19:51:58 +03004087 if (mmc_card_doing_bkops(host->card)) {
4088 ret = mmc_stop_bkops(host->card);
4089 if (ret)
4090 goto out;
4091 }
4092 }
4093
Andrei Warkentin371a6892011-04-11 18:10:25 -05004094 ret = mmc_blk_part_switch(card, md);
Pavan Anamula973ba1c2015-11-20 15:23:47 +05304095
Andrei Warkentin371a6892011-04-11 18:10:25 -05004096 if (ret) {
Pavan Anamula973ba1c2015-11-20 15:23:47 +05304097 err = mmc_blk_reset(md, card->host, MMC_BLK_PARTSWITCH);
4098 if (!err) {
4099 pr_err("%s: mmc_blk_reset(MMC_BLK_PARTSWITCH) succeeded.\n",
4100 mmc_hostname(host));
4101 mmc_blk_reset_success(md, MMC_BLK_PARTSWITCH);
4102 } else
4103 pr_err("%s: mmc_blk_reset(MMC_BLK_PARTSWITCH) failed.\n",
4104 mmc_hostname(host));
4105
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03004106 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05304107 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03004108 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004109 ret = 0;
4110 goto out;
4111 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004112
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004113 mmc_blk_write_packing_control(mq, req);
4114
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02004115 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05004116 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02004117 /* complete ongoing async transfer before issuing discard */
4118 if (card->host->areq)
4119 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02004120 ret = mmc_blk_issue_discard_rq(mq, req);
4121 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
4122 /* complete ongoing async transfer before issuing secure erase*/
4123 if (card->host->areq)
4124 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02004125 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
4126 ret = mmc_blk_issue_secdiscard_rq(mq, req);
4127 else
4128 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05304129 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
4130 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09004131 /* complete ongoing async transfer before issuing flush */
4132 if (card->host->areq)
4133 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004134 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07004135 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05004136 if (!req && host->areq) {
4137 spin_lock_irqsave(&host->context_info.lock, flags);
4138 host->context_info.is_waiting_last_req = true;
4139 spin_unlock_irqrestore(&host->context_info.lock, flags);
4140 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004141 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07004142 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004143
Andrei Warkentin371a6892011-04-11 18:10:25 -05004144out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02004145 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
4146 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09004147 /*
4148 * Release host when there are no more requests
4149 * and after special request(discard, flush) is done.
4150 * In case sepecial request, there is no reentry to
4151 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
4152 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004153 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004154 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07004155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Russell Kinga6f6c962006-01-03 22:38:44 +00004157static inline int mmc_blk_readonly(struct mmc_card *card)
4158{
4159 return mmc_card_readonly(card) ||
4160 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
4161}
4162
Andrei Warkentin371a6892011-04-11 18:10:25 -05004163static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
4164 struct device *parent,
4165 sector_t size,
4166 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004167 const char *subname,
4168 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 struct mmc_blk_data *md;
4171 int devidx, ret;
4172
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004173again:
4174 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
4175 return ERR_PTR(-ENOMEM);
4176
4177 spin_lock(&mmc_blk_lock);
4178 ret = ida_get_new(&mmc_blk_ida, &devidx);
4179 spin_unlock(&mmc_blk_lock);
4180
4181 if (ret == -EAGAIN)
4182 goto again;
4183 else if (ret)
4184 return ERR_PTR(ret);
4185
4186 if (devidx >= max_devices) {
4187 ret = -ENOSPC;
4188 goto out;
4189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004191 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00004192 if (!md) {
4193 ret = -ENOMEM;
4194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
Russell Kinga6f6c962006-01-03 22:38:44 +00004196
Johan Rudholmadd710e2011-12-02 08:51:06 +01004197 md->area_type = area_type;
4198
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004199 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00004200 * Set the read-only status based on the supported commands
4201 * and the write protect switch.
4202 */
4203 md->read_only = mmc_blk_readonly(card);
4204
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004205 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00004206 if (md->disk == NULL) {
4207 ret = -ENOMEM;
4208 goto err_kfree;
4209 }
4210
4211 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004212 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00004213 md->usage = 1;
4214
Sahitya Tummala13805b32016-10-25 15:57:01 +05304215 ret = mmc_init_queue(&md->queue, card, NULL, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00004216 if (ret)
4217 goto err_putdisk;
4218
Russell Kinga6f6c962006-01-03 22:38:44 +00004219 md->queue.data = md;
4220
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004221 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004222 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00004223 md->disk->fops = &mmc_bdops;
4224 md->disk->private_data = md;
4225 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07004226 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004227 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07004228 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02004229 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02004230 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00004231
4232 /*
4233 * As discussed on lkml, GENHD_FL_REMOVABLE should:
4234 *
4235 * - be set for removable media with permanent block devices
4236 * - be unset for removable block devices with permanent media
4237 *
4238 * Since MMC block devices clearly fall under the second
4239 * case, we do not set GENHD_FL_REMOVABLE. Userspace
4240 * should use the block device creation/destruction hotplug
4241 * messages to tell when the card is present.
4242 */
4243
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004244 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02004245 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00004246
Saugata Dasa5075eb2012-05-17 16:32:21 +05304247 if (mmc_card_mmc(card))
4248 blk_queue_logical_block_size(md->queue.queue,
4249 card->ext_csd.data_sector_size);
4250 else
4251 blk_queue_logical_block_size(md->queue.queue, 512);
4252
Andrei Warkentin371a6892011-04-11 18:10:25 -05004253 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004254
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004255 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02004256 if ((mmc_card_mmc(card) &&
4257 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004258 (mmc_card_sd(card) &&
4259 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
4260 md->flags |= MMC_BLK_CMD23;
4261 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004262
4263 if (mmc_card_mmc(card) &&
4264 md->flags & MMC_BLK_CMD23 &&
4265 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05304266 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004267 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06004268 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004269 }
4270
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004271 if (card->cmdq_init) {
4272 md->flags |= MMC_BLK_CMD_QUEUE;
4273 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
4274 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05304275 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05304276 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05304277 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004278 }
4279
4280 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09004281 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
4282 (md->flags & MMC_BLK_CMD23) &&
4283 card->ext_csd.packed_event_en) {
4284 if (!mmc_packed_init(&md->queue, card))
4285 md->flags |= MMC_BLK_PACKED_CMD;
4286 }
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00004289
4290 err_putdisk:
4291 put_disk(md->disk);
4292 err_kfree:
4293 kfree(md);
4294 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004295 spin_lock(&mmc_blk_lock);
4296 ida_remove(&mmc_blk_ida, devidx);
4297 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00004298 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Andrei Warkentin371a6892011-04-11 18:10:25 -05004301static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
4302{
4303 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004304
4305 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
4306 /*
4307 * The EXT_CSD sector count is in number or 512 byte
4308 * sectors.
4309 */
4310 size = card->ext_csd.sectors;
4311 } else {
4312 /*
4313 * The CSD capacity field is in units of read_blkbits.
4314 * set_capacity takes units of 512 bytes.
4315 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00004316 size = (typeof(sector_t))card->csd.capacity
4317 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004318 }
4319
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01004320 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004321 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004322}
4323
4324static int mmc_blk_alloc_part(struct mmc_card *card,
4325 struct mmc_blk_data *md,
4326 unsigned int part_type,
4327 sector_t size,
4328 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004329 const char *subname,
4330 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004331{
4332 char cap_str[10];
4333 struct mmc_blk_data *part_md;
4334
4335 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004336 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004337 if (IS_ERR(part_md))
4338 return PTR_ERR(part_md);
4339 part_md->part_type = part_type;
4340 list_add(&part_md->part, &md->part);
4341
James Bottomleyb9f28d82015-03-05 18:47:01 -08004342 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004343 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304344 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004345 part_md->disk->disk_name, mmc_card_id(card),
4346 mmc_card_name(card), part_md->part_type, cap_str);
4347 return 0;
4348}
4349
Namjae Jeone0c368d2011-10-06 23:41:38 +09004350/* MMC Physical partitions consist of two boot partitions and
4351 * up to four general purpose partitions.
4352 * For each partition enabled in EXT_CSD a block device will be allocatedi
4353 * to provide access to the partition.
4354 */
4355
Andrei Warkentin371a6892011-04-11 18:10:25 -05004356static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4357{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004358 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004359
4360 if (!mmc_card_mmc(card))
4361 return 0;
4362
Namjae Jeone0c368d2011-10-06 23:41:38 +09004363 for (idx = 0; idx < card->nr_parts; idx++) {
4364 if (card->part[idx].size) {
4365 ret = mmc_blk_alloc_part(card, md,
4366 card->part[idx].part_cfg,
4367 card->part[idx].size >> 9,
4368 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004369 card->part[idx].name,
4370 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004371 if (ret)
4372 return ret;
4373 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004374 }
4375
4376 return ret;
4377}
4378
Andrei Warkentin371a6892011-04-11 18:10:25 -05004379static void mmc_blk_remove_req(struct mmc_blk_data *md)
4380{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004381 struct mmc_card *card;
4382
Andrei Warkentin371a6892011-04-11 18:10:25 -05004383 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004384 /*
4385 * Flush remaining requests and free queues. It
4386 * is freeing the queue that stops new requests
4387 * from being accepted.
4388 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004389 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004390 mmc_cleanup_queue(&md->queue);
4391 if (md->flags & MMC_BLK_PACKED_CMD)
4392 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004393 if (md->flags & MMC_BLK_CMD_QUEUE)
4394 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004395 device_remove_file(disk_to_dev(md->disk),
4396 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004397 if (md->disk->flags & GENHD_FL_UP) {
4398 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004399 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4400 card->ext_csd.boot_ro_lockable)
4401 device_remove_file(disk_to_dev(md->disk),
4402 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004403#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4404 device_remove_file(disk_to_dev(md->disk),
4405 &dev_attr_max_write_speed);
4406 device_remove_file(disk_to_dev(md->disk),
4407 &dev_attr_max_read_speed);
4408 device_remove_file(disk_to_dev(md->disk),
4409 &dev_attr_cache_size);
4410#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004411
Andrei Warkentin371a6892011-04-11 18:10:25 -05004412 del_gendisk(md->disk);
4413 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004414 mmc_blk_put(md);
4415 }
4416}
4417
4418static void mmc_blk_remove_parts(struct mmc_card *card,
4419 struct mmc_blk_data *md)
4420{
4421 struct list_head *pos, *q;
4422 struct mmc_blk_data *part_md;
4423
4424 list_for_each_safe(pos, q, &md->part) {
4425 part_md = list_entry(pos, struct mmc_blk_data, part);
4426 list_del(pos);
4427 mmc_blk_remove_req(part_md);
4428 }
4429}
4430
4431static int mmc_add_disk(struct mmc_blk_data *md)
4432{
4433 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004434 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004435
Dan Williams307d8e62016-06-20 10:40:44 -07004436 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004437 md->force_ro.show = force_ro_show;
4438 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304439 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004440 md->force_ro.attr.name = "force_ro";
4441 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4442 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4443 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004444 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004445#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4446 atomic_set(&md->queue.max_write_speed, max_write_speed);
4447 ret = device_create_file(disk_to_dev(md->disk),
4448 &dev_attr_max_write_speed);
4449 if (ret)
4450 goto max_write_speed_fail;
4451 atomic_set(&md->queue.max_read_speed, max_read_speed);
4452 ret = device_create_file(disk_to_dev(md->disk),
4453 &dev_attr_max_read_speed);
4454 if (ret)
4455 goto max_read_speed_fail;
4456 atomic_set(&md->queue.cache_size, cache_size);
4457 atomic_long_set(&md->queue.cache_used, 0);
4458 md->queue.cache_jiffies = jiffies;
4459 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4460 if (ret)
4461 goto cache_size_fail;
4462#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004463
4464 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4465 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004466 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004467
4468 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4469 mode = S_IRUGO;
4470 else
4471 mode = S_IRUGO | S_IWUSR;
4472
4473 md->power_ro_lock.show = power_ro_lock_show;
4474 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004475 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004476 md->power_ro_lock.attr.mode = mode;
4477 md->power_ro_lock.attr.name =
4478 "ro_lock_until_next_power_on";
4479 ret = device_create_file(disk_to_dev(md->disk),
4480 &md->power_ro_lock);
4481 if (ret)
4482 goto power_ro_lock_fail;
4483 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004484
4485 md->num_wr_reqs_to_start_packing.show =
4486 num_wr_reqs_to_start_packing_show;
4487 md->num_wr_reqs_to_start_packing.store =
4488 num_wr_reqs_to_start_packing_store;
4489 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4490 md->num_wr_reqs_to_start_packing.attr.name =
4491 "num_wr_reqs_to_start_packing";
4492 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4493 ret = device_create_file(disk_to_dev(md->disk),
4494 &md->num_wr_reqs_to_start_packing);
4495 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004496 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004497
Maya Erez5a8dae12014-12-04 15:13:59 +02004498 md->no_pack_for_random.show = no_pack_for_random_show;
4499 md->no_pack_for_random.store = no_pack_for_random_store;
4500 sysfs_attr_init(&md->no_pack_for_random.attr);
4501 md->no_pack_for_random.attr.name = "no_pack_for_random";
4502 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4503 ret = device_create_file(disk_to_dev(md->disk),
4504 &md->no_pack_for_random);
4505 if (ret)
4506 goto no_pack_for_random_fails;
4507
Johan Rudholmadd710e2011-12-02 08:51:06 +01004508 return ret;
4509
Maya Erez5a8dae12014-12-04 15:13:59 +02004510no_pack_for_random_fails:
4511 device_remove_file(disk_to_dev(md->disk),
4512 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004513num_wr_reqs_to_start_packing_fail:
4514 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004515power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004516#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4517 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4518cache_size_fail:
4519 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4520max_read_speed_fail:
4521 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4522max_write_speed_fail:
4523#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004524 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4525force_ro_fail:
4526 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004527
4528 return ret;
4529}
4530
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004531static const struct mmc_fixup blk_fixups[] =
4532{
Chris Ballc59d4472011-11-11 22:01:43 -05004533 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4534 MMC_QUIRK_INAND_CMD38),
4535 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4536 MMC_QUIRK_INAND_CMD38),
4537 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4538 MMC_QUIRK_INAND_CMD38),
4539 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4540 MMC_QUIRK_INAND_CMD38),
4541 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4542 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004543
4544 /*
4545 * Some MMC cards experience performance degradation with CMD23
4546 * instead of CMD12-bounded multiblock transfers. For now we'll
4547 * black list what's bad...
4548 * - Certain Toshiba cards.
4549 *
4550 * N.B. This doesn't affect SD cards.
4551 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004552 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4553 MMC_QUIRK_BLK_NO_CMD23),
4554 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4555 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004556 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004557 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004558 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004559 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004560 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004561 MMC_QUIRK_BLK_NO_CMD23),
Subhash Jadavanib3994402015-11-03 19:03:41 -08004562 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_TOSHIBA, CID_OEMID_ANY,
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08004563 add_quirk_mmc, MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004564
4565 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004566 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004567 */
Chris Ballc59d4472011-11-11 22:01:43 -05004568 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004569 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004570 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4571 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004572
Ian Chen3550ccd2012-08-29 15:05:36 +09004573 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004574 * Some Samsung MMC cards need longer data read timeout than
4575 * indicated in CSD.
4576 */
4577 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4578 MMC_QUIRK_LONG_READ_TIME),
4579
4580 /*
Ritesh Harjanie11a1b72016-02-25 17:54:58 +05304581 * Hynix eMMC cards need longer data read timeout than
4582 * indicated in CSD.
4583 */
4584 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
4585 MMC_QUIRK_LONG_READ_TIME),
4586
4587 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004588 * On these Samsung MoviNAND parts, performing secure erase or
4589 * secure trim can result in unrecoverable corruption due to a
4590 * firmware bug.
4591 */
4592 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4593 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4594 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4595 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4596 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4597 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4598 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4599 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4600 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4601 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4602 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4603 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4604 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4605 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4606 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4607 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4608
Shawn Linb5b4ff02015-08-12 13:08:32 +08004609 /*
4610 * On Some Kingston eMMCs, performing trim can result in
4611 * unrecoverable data conrruption occasionally due to a firmware bug.
4612 */
4613 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4614 MMC_QUIRK_TRIM_BROKEN),
4615 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4616 MMC_QUIRK_TRIM_BROKEN),
4617
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004618 /* Some INAND MCP devices advertise incorrect timeout values */
4619 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4620 MMC_QUIRK_INAND_DATA_TIMEOUT),
4621
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004622 END_FIXUP
4623};
4624
Ulf Hansson96541ba2015-04-14 13:06:12 +02004625static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004627 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004628 char cap_str[10];
4629
Pierre Ossman912490d2005-05-21 10:27:02 +01004630 /*
4631 * Check that the card supports the command class(es) we need.
4632 */
4633 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 return -ENODEV;
4635
Lukas Czerner5204d002014-06-18 13:18:07 +02004636 mmc_fixup_device(card, blk_fixups);
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 md = mmc_blk_alloc(card);
4639 if (IS_ERR(md))
4640 return PTR_ERR(md);
4641
James Bottomleyb9f28d82015-03-05 18:47:01 -08004642 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004643 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304644 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004646 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Andrei Warkentin371a6892011-04-11 18:10:25 -05004648 if (mmc_blk_alloc_parts(card, md))
4649 goto out;
4650
Ulf Hansson96541ba2015-04-14 13:06:12 +02004651 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004652
San Mehat8706e3a2009-07-30 08:21:19 -07004653#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
4654 mmc_set_bus_resume_policy(card->host, 1);
4655#endif
4656
Andrei Warkentin371a6892011-04-11 18:10:25 -05004657 if (mmc_add_disk(md))
4658 goto out;
4659
4660 list_for_each_entry(part_md, &md->part, part) {
4661 if (mmc_add_disk(part_md))
4662 goto out;
4663 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004664
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004665 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004666 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
Konstantin Dorfman5dd9f862015-12-31 13:25:40 +02004667 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004668
4669 /*
4670 * Don't enable runtime PM for SD-combo cards here. Leave that
4671 * decision to be taken during the SDIO init sequence instead.
4672 */
4673 if (card->type != MMC_TYPE_SD_COMBO) {
4674 pm_runtime_set_active(&card->dev);
4675 pm_runtime_enable(&card->dev);
4676 }
4677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return 0;
4679
4680 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004681 mmc_blk_remove_parts(card, md);
4682 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Ulf Hansson96541ba2015-04-14 13:06:12 +02004686static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004688 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Andrei Warkentin371a6892011-04-11 18:10:25 -05004690 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004691 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004692 mmc_claim_host(card->host);
4693 mmc_blk_part_switch(card, md);
4694 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004695 if (card->type != MMC_TYPE_SD_COMBO)
4696 pm_runtime_disable(&card->dev);
4697 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004698 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004699 dev_set_drvdata(&card->dev, NULL);
San Mehat8706e3a2009-07-30 08:21:19 -07004700#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
4701 mmc_set_bus_resume_policy(card->host, 0);
4702#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004705static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004707 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004708 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304709 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004712 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304713 if (rc)
4714 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004715 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004716 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304717 if (rc)
4718 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304721 goto out;
4722
4723 out_resume:
4724 mmc_queue_resume(&md->queue);
4725 list_for_each_entry(part_md, &md->part, part) {
4726 mmc_queue_resume(&part_md->queue);
4727 }
4728 out:
4729 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
4731
Ulf Hansson96541ba2015-04-14 13:06:12 +02004732static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004733{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004734 _mmc_blk_suspend(card, 1);
Ulf Hansson76287742013-06-10 17:03:40 +02004735}
4736
Ulf Hansson0967edc2014-10-06 11:29:42 +02004737#ifdef CONFIG_PM_SLEEP
4738static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004739{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004740 struct mmc_card *card = mmc_dev_to_card(dev);
4741
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004742 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004743}
4744
Ulf Hansson0967edc2014-10-06 11:29:42 +02004745static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004747 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004748 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004751 /*
4752 * Resume involves the card going into idle state,
4753 * so current partition is always the main one.
4754 */
4755 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004757 list_for_each_entry(part_md, &md->part, part) {
4758 mmc_queue_resume(&part_md->queue);
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761 return 0;
4762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763#endif
4764
Ulf Hansson0967edc2014-10-06 11:29:42 +02004765static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4766
Ulf Hansson96541ba2015-04-14 13:06:12 +02004767static struct mmc_driver mmc_driver = {
4768 .drv = {
4769 .name = "mmcblk",
4770 .pm = &mmc_blk_pm_ops,
4771 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 .probe = mmc_blk_probe,
4773 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004774 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775};
4776
4777static int __init mmc_blk_init(void)
4778{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004779 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004781 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4782 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4783
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004784 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004785
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004786 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4787 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004790 res = mmc_register_driver(&mmc_driver);
4791 if (res)
4792 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004794 return 0;
4795 out2:
4796 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 out:
4798 return res;
4799}
4800
4801static void __exit mmc_blk_exit(void)
4802{
4803 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004804 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
4807module_init(mmc_blk_init);
4808module_exit(mmc_blk_exit);
4809
4810MODULE_LICENSE("GPL");
4811MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4812