blob: 9d3dcd12d6045b106d7bb69fa2ca25ffa7431265 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Dov Levenglicka0296392015-06-24 19:51:58 +030044#include <linux/mmc/core.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020045#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046#include <linux/mmc/mmc.h>
47#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50
Pierre Ossman98ac2162006-12-23 20:03:02 +010051#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080052#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020066#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053069#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050070
Luca Porziod3df0462015-11-06 15:12:26 +000071#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072 (rq_data_dir(req) == WRITE))
73#define PACKED_CMD_VER 0x01
74#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030075#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090076
Maya Erezf93ca0a2014-12-09 23:34:41 +020077#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020078#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
79 do { \
80 if (stats->enabled) \
81 stats->pack_stop_reason[reason]++; \
82 } while (0)
83
Asutosh Das02e30862015-05-20 16:52:04 +053084#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030085#define PCKD_TRGR_INIT_MEAN_POTEN 17
86#define PCKD_TRGR_POTEN_LOWER_BOUND 5
87#define PCKD_TRGR_URGENT_PENALTY 2
88#define PCKD_TRGR_LOWER_BOUND 5
89#define PCKD_TRGR_PRECISION_MULTIPLIER 100
90
Sahitya Tummala9433a132015-06-09 09:38:36 +053091static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
92 struct mmc_queue_req *mqrq, struct mmc_queue *mq);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020093static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040094
95/*
96 * The defaults come from config options but can be overriden by module
97 * or bootarg options.
98 */
99static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
100
101/*
102 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000103 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105 */
106static int max_devices;
107
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000108#define MAX_DEVICES 256
109
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200110static DEFINE_IDA(mmc_blk_ida);
111static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * There is one mmc_blk_data per slot.
115 */
116struct mmc_blk_data {
117 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700118 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct gendisk *disk;
120 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500123 unsigned int flags;
124#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
125#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700127#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000130 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300132 unsigned int reset_done;
133#define MMC_BLK_READ BIT(0)
134#define MMC_BLK_WRITE BIT(1)
135#define MMC_BLK_DISCARD BIT(2)
136#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200137#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138
139 /*
140 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200141 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 * track of the current selected device partition.
143 */
144 unsigned int part_curr;
145 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200147 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200148 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100149 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Arjan van de Vena621aae2006-01-12 18:43:35 +0000152static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154enum {
155 MMC_PACKED_NR_IDX = -1,
156 MMC_PACKED_NR_ZERO,
157 MMC_PACKED_NR_SINGLE,
158};
159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
164 struct mmc_blk_data *md);
165static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700166static int mmc_blk_cmdq_switch(struct mmc_card *card,
167 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200168
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900169static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
170{
171 struct mmc_packed *packed = mqrq->packed;
172
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900173 mqrq->cmd_type = MMC_PACKED_NONE;
174 packed->nr_entries = MMC_PACKED_NR_ZERO;
175 packed->idx_failure = MMC_PACKED_NR_IDX;
176 packed->retries = 0;
177 packed->blocks = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
181{
182 struct mmc_blk_data *md;
183
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md = disk->private_data;
186 if (md && md->usage == 0)
187 md = NULL;
188 if (md)
189 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return md;
193}
194
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195static inline int mmc_get_devidx(struct gendisk *disk)
196{
Colin Cross382c55f2015-10-22 10:00:41 -0700197 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500198 return devidx;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void mmc_blk_put(struct mmc_blk_data *md)
202{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000203 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 md->usage--;
205 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500206 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800207 blk_cleanup_queue(md->queue.queue);
208
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200209 spin_lock(&mmc_blk_lock);
210 ida_remove(&mmc_blk_ida, devidx);
211 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kfree(md);
215 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000216 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219static ssize_t power_ro_lock_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 int ret;
223 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 int locked = 0;
226
Asutosh Das507d9a72014-12-09 10:15:53 +0200227 if (!md)
228 return -EINVAL;
229
230 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 locked = 2;
233 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
234 locked = 1;
235
236 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
237
Tomas Winkler9098f842015-07-16 15:50:45 +0200238 mmc_blk_put(md);
239
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240 return ret;
241}
242
243static ssize_t power_ro_lock_store(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
245{
246 int ret;
247 struct mmc_blk_data *md, *part_md;
248 struct mmc_card *card;
249 unsigned long set;
250
251 if (kstrtoul(buf, 0, &set))
252 return -EINVAL;
253
254 if (set != 1)
255 return count;
256
257 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200258 if (!md)
259 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260 card = md->queue.card;
261
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200262 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100263
264 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
265 card->ext_csd.boot_ro_lock |
266 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
267 card->ext_csd.part_time);
268 if (ret)
269 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
270 else
271 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
272
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200273 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100274
275 if (!ret) {
276 pr_info("%s: Locking boot partition ro until next power on\n",
277 md->disk->disk_name);
278 set_disk_ro(md->disk, 1);
279
280 list_for_each_entry(part_md, &md->part, part)
281 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
282 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
283 set_disk_ro(part_md->disk, 1);
284 }
285 }
286
287 mmc_blk_put(md);
288 return count;
289}
290
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
293{
294 int ret;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296
Asutosh Das507d9a72014-12-09 10:15:53 +0200297 if (!md)
298 return -EINVAL;
299
Baruch Siach0031a982014-09-22 10:12:51 +0300300 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500301 get_disk_ro(dev_to_disk(dev)) ^
302 md->read_only);
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int ret;
311 char *end;
312 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
313 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200314
315 if (!md)
316 return -EINVAL;
317
Andrei Warkentin371a6892011-04-11 18:10:25 -0500318 if (end == buf) {
319 ret = -EINVAL;
320 goto out;
321 }
322
323 set_disk_ro(dev_to_disk(dev), set || md->read_only);
324 ret = count;
325out:
326 mmc_blk_put(md);
327 return ret;
328}
329
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200330static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200331no_pack_for_random_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 int ret;
336
Asutosh Das507d9a72014-12-09 10:15:53 +0200337 if (!md)
338 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200339 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
340
341 mmc_blk_put(md);
342 return ret;
343}
344
345static ssize_t
346no_pack_for_random_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 int value;
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200353 int ret = count;
354
Asutosh Das507d9a72014-12-09 10:15:53 +0200355 if (!md)
356 return -EINVAL;
357
358 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200359 if (!card) {
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 sscanf(buf, "%d", &value);
365
366 if (value < 0) {
367 pr_err("%s: value %d is not valid. old value remains = %d",
368 mmc_hostname(card->host), value,
369 md->queue.no_pack_for_random);
370 ret = -EINVAL;
371 goto exit;
372 }
373
374 md->queue.no_pack_for_random = (value > 0) ? true : false;
375
376 pr_debug("%s: no_pack_for_random: new value = %d",
377 mmc_hostname(card->host),
378 md->queue.no_pack_for_random);
379
380exit:
381 mmc_blk_put(md);
382 return ret;
383}
384
385static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386num_wr_reqs_to_start_packing_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
390 int num_wr_reqs_to_start_packing;
391 int ret;
392
Asutosh Das507d9a72014-12-09 10:15:53 +0200393 if (!md)
394 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
396
397 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
398
399 mmc_blk_put(md);
400 return ret;
401}
402
403static ssize_t
404num_wr_reqs_to_start_packing_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 int value;
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200411 int ret = count;
412
Asutosh Das507d9a72014-12-09 10:15:53 +0200413 if (!md)
414 return -EINVAL;
415
416 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200417 if (!card) {
418 ret = -EINVAL;
419 goto exit;
420 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200421
422 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200423
Yaniv Gardi42399822014-12-04 00:26:23 +0200424 if (value >= 0) {
425 md->queue.num_wr_reqs_to_start_packing =
426 min_t(int, value, (int)card->ext_csd.max_packed_writes);
427
428 pr_debug("%s: trigger to pack: new value = %d",
429 mmc_hostname(card->host),
430 md->queue.num_wr_reqs_to_start_packing);
431 } else {
432 pr_err("%s: value %d is not valid. old value remains = %d",
433 mmc_hostname(card->host), value,
434 md->queue.num_wr_reqs_to_start_packing);
435 ret = -EINVAL;
436 }
437
438exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200439 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200440 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200441}
442
Mark Salyzyn6904e432016-01-28 11:12:25 -0800443#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
444
445static int max_read_speed, max_write_speed, cache_size = 4;
446
447module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
448MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
449module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
450MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
451module_param(cache_size, int, S_IRUSR | S_IRGRP);
452MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
453
454/*
455 * helper macros and expectations:
456 * size - unsigned long number of bytes
457 * jiffies - unsigned long HZ timestamp difference
458 * speed - unsigned KB/s transfer rate
459 */
460#define size_and_speed_to_jiffies(size, speed) \
461 ((size) * HZ / (speed) / 1024UL)
462#define jiffies_and_speed_to_size(jiffies, speed) \
463 (((speed) * (jiffies) * 1024UL) / HZ)
464#define jiffies_and_size_to_speed(jiffies, size) \
465 ((size) * HZ / (jiffies) / 1024UL)
466
467/* Limits to report warning */
468/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
469#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
470#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
471
472#define speed_valid(speed) ((speed) > 0)
473
474static const char off[] = "off\n";
475
476static int max_speed_show(int speed, char *buf)
477{
478 if (speed)
479 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
480 else
481 return scnprintf(buf, PAGE_SIZE, off);
482}
483
484static int max_speed_store(const char *buf, struct request_queue *q)
485{
486 unsigned int limit, set = 0;
487
488 if (!strncasecmp(off, buf, sizeof(off) - 2))
489 return set;
490 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
491 return -EINVAL;
492 if (set == 0)
493 return set;
494 limit = MAX_SPEED(q);
495 if (set > limit)
496 pr_warn("max speed %u ineffective above %u\n", set, limit);
497 limit = MIN_SPEED(q);
498 if (set < limit)
499 pr_warn("max speed %u painful below %u\n", set, limit);
500 return set;
501}
502
503static ssize_t max_write_speed_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
507 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
508
509 mmc_blk_put(md);
510 return ret;
511}
512
513static ssize_t max_write_speed_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
518 int set = max_speed_store(buf, md->queue.queue);
519
520 if (set < 0) {
521 mmc_blk_put(md);
522 return set;
523 }
524
525 atomic_set(&md->queue.max_write_speed, set);
526 mmc_blk_put(md);
527 return count;
528}
529
530static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
531 max_write_speed_show, max_write_speed_store);
532
533static ssize_t max_read_speed_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
537 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
538
539 mmc_blk_put(md);
540 return ret;
541}
542
543static ssize_t max_read_speed_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
548 int set = max_speed_store(buf, md->queue.queue);
549
550 if (set < 0) {
551 mmc_blk_put(md);
552 return set;
553 }
554
555 atomic_set(&md->queue.max_read_speed, set);
556 mmc_blk_put(md);
557 return count;
558}
559
560static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
561 max_read_speed_show, max_read_speed_store);
562
563static ssize_t cache_size_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
567 struct mmc_queue *mq = &md->queue;
568 int cache_size = atomic_read(&mq->cache_size);
569 int ret;
570
571 if (!cache_size)
572 ret = scnprintf(buf, PAGE_SIZE, off);
573 else {
574 int speed = atomic_read(&mq->max_write_speed);
575
576 if (!speed_valid(speed))
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
578 else { /* We accept race between cache_jiffies and cache_used */
579 unsigned long size = jiffies_and_speed_to_size(
580 jiffies - mq->cache_jiffies, speed);
581 long used = atomic_long_read(&mq->cache_used);
582
583 if (size >= used)
584 size = 0;
585 else
586 size = (used - size) * 100 / cache_size
587 / 1024UL / 1024UL;
588
589 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
590 cache_size, size);
591 }
592 }
593
594 mmc_blk_put(md);
595 return ret;
596}
597
598static ssize_t cache_size_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
601{
602 struct mmc_blk_data *md;
603 unsigned int set = 0;
604
605 if (strncasecmp(off, buf, sizeof(off) - 2)
606 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
607 return -EINVAL;
608
609 md = mmc_blk_get(dev_to_disk(dev));
610 atomic_set(&md->queue.cache_size, set);
611 mmc_blk_put(md);
612 return count;
613}
614
615static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
616 cache_size_show, cache_size_store);
617
618/* correct for write-back */
619static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
620{
621 long used = 0;
622 int speed = atomic_read(&mq->max_write_speed);
623
624 if (speed_valid(speed)) {
625 unsigned long size = jiffies_and_speed_to_size(
626 waitfor - mq->cache_jiffies, speed);
627 used = atomic_long_read(&mq->cache_used);
628
629 if (size >= used)
630 used = 0;
631 else
632 used -= size;
633 }
634
635 atomic_long_set(&mq->cache_used, used);
636 mq->cache_jiffies = waitfor;
637
638 return used;
639}
640
641static void mmc_blk_simulate_delay(
642 struct mmc_queue *mq,
643 struct request *req,
644 unsigned long waitfor)
645{
646 int max_speed;
647
648 if (!req)
649 return;
650
651 max_speed = (rq_data_dir(req) == READ)
652 ? atomic_read(&mq->max_read_speed)
653 : atomic_read(&mq->max_write_speed);
654 if (speed_valid(max_speed)) {
655 unsigned long bytes = blk_rq_bytes(req);
656
657 if (rq_data_dir(req) != READ) {
658 int cache_size = atomic_read(&mq->cache_size);
659
660 if (cache_size) {
661 unsigned long size = cache_size * 1024L * 1024L;
662 long used = mmc_blk_cache_used(mq, waitfor);
663
664 used += bytes;
665 atomic_long_set(&mq->cache_used, used);
666 bytes = 0;
667 if (used > size)
668 bytes = used - size;
669 }
670 }
671 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
672 if (time_is_after_jiffies(waitfor)) {
673 long msecs = jiffies_to_msecs(waitfor - jiffies);
674
675 if (likely(msecs > 0))
676 msleep(msecs);
677 }
678 }
679}
680
681#else
682
683#define mmc_blk_simulate_delay(mq, req, waitfor)
684
685#endif
686
Al Viroa5a15612008-03-02 10:33:30 -0500687static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viroa5a15612008-03-02 10:33:30 -0500689 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = -ENXIO;
691
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200692 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (md) {
694 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500695 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697
Al Viroa5a15612008-03-02 10:33:30 -0500698 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700699 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700700 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return ret;
706}
707
Al Virodb2a1442013-05-05 21:52:57 -0400708static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viroa5a15612008-03-02 10:33:30 -0500710 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200712 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200714 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800718mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
721 geo->heads = 4;
722 geo->sectors = 16;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
John Calixtocb87ea22011-04-26 18:56:29 -0400726struct mmc_blk_ioc_data {
727 struct mmc_ioc_cmd ic;
728 unsigned char *buf;
729 u64 buf_bytes;
730};
731
732static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
733 struct mmc_ioc_cmd __user *user)
734{
735 struct mmc_blk_ioc_data *idata;
736 int err;
737
yalin wang1ff89502015-11-12 19:27:11 +0800738 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400739 if (!idata) {
740 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400742 }
743
744 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
745 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400746 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400747 }
748
749 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
750 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
751 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400752 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400753 }
754
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 if (!idata->buf_bytes) {
756 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300758 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100759
yalin wang1ff89502015-11-12 19:27:11 +0800760 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400761 if (!idata->buf) {
762 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400763 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400764 }
765
766 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
767 idata->ic.data_ptr, idata->buf_bytes)) {
768 err = -EFAULT;
769 goto copy_err;
770 }
771
772 return idata;
773
774copy_err:
775 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400776idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400777 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400778out:
John Calixtocb87ea22011-04-26 18:56:29 -0400779 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
Jon Huntera5f57742015-09-22 10:27:53 +0100782static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
783 struct mmc_blk_ioc_data *idata)
784{
785 struct mmc_ioc_cmd *ic = &idata->ic;
786
787 if (copy_to_user(&(ic_ptr->response), ic->response,
788 sizeof(ic->response)))
789 return -EFAULT;
790
791 if (!idata->ic.write_flag) {
792 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
793 idata->buf, idata->buf_bytes))
794 return -EFAULT;
795 }
796
797 return 0;
798}
799
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200800static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
801 u32 retries_max)
802{
803 int err;
804 u32 retry_count = 0;
805
806 if (!status || !retries_max)
807 return -EINVAL;
808
809 do {
810 err = get_card_status(card, status, 5);
811 if (err)
812 break;
813
814 if (!R1_STATUS(*status) &&
815 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
816 break; /* RPMB programming operation complete */
817
818 /*
819 * Rechedule to give the MMC device a chance to continue
820 * processing the previous command without being polled too
821 * frequently.
822 */
823 usleep_range(1000, 5000);
824 } while (++retry_count < retries_max);
825
826 if (retry_count == retries_max)
827 err = -EPERM;
828
829 return err;
830}
831
Maya Erez775a9362013-04-18 15:41:55 +0300832static int ioctl_do_sanitize(struct mmc_card *card)
833{
834 int err;
835
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530836 if (!mmc_can_sanitize(card) &&
837 (card->host->caps2 & MMC_CAP2_SANITIZE)) {
838 pr_warn("%s: %s - SANITIZE is not supported\n",
Maya Erez775a9362013-04-18 15:41:55 +0300839 mmc_hostname(card->host), __func__);
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530840 err = -EOPNOTSUPP;
841 goto out;
Maya Erez775a9362013-04-18 15:41:55 +0300842 }
843
844 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
845 mmc_hostname(card->host), __func__);
846
847 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
848 EXT_CSD_SANITIZE_START, 1,
849 MMC_SANITIZE_REQ_TIMEOUT);
850
851 if (err)
852 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
853 mmc_hostname(card->host), __func__, err);
854
855 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
856 __func__);
857out:
858 return err;
859}
860
Jon Huntera5f57742015-09-22 10:27:53 +0100861static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
862 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400863{
John Calixtocb87ea22011-04-26 18:56:29 -0400864 struct mmc_command cmd = {0};
865 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530866 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400867 struct scatterlist sg;
868 int err;
869
Jon Huntera5f57742015-09-22 10:27:53 +0100870 if (!card || !md || !idata)
871 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400872
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100873 cmd.opcode = idata->ic.opcode;
874 cmd.arg = idata->ic.arg;
875 cmd.flags = idata->ic.flags;
876
877 if (idata->buf_bytes) {
878 data.sg = &sg;
879 data.sg_len = 1;
880 data.blksz = idata->ic.blksz;
881 data.blocks = idata->ic.blocks;
882
883 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
884
885 if (idata->ic.write_flag)
886 data.flags = MMC_DATA_WRITE;
887 else
888 data.flags = MMC_DATA_READ;
889
890 /* data.flags must already be set before doing this. */
891 mmc_set_data_timeout(&data, card);
892
893 /* Allow overriding the timeout_ns for empirical tuning. */
894 if (idata->ic.data_timeout_ns)
895 data.timeout_ns = idata->ic.data_timeout_ns;
896
897 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
898 /*
899 * Pretend this is a data transfer and rely on the
900 * host driver to compute timeout. When all host
901 * drivers support cmd.cmd_timeout for R1B, this
902 * can be changed to:
903 *
904 * mrq.data = NULL;
905 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
906 */
907 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
908 }
909
910 mrq.data = &data;
911 }
912
913 mrq.cmd = &cmd;
914
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200915 err = mmc_blk_part_switch(card, md);
916 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100917 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200918
John Calixtocb87ea22011-04-26 18:56:29 -0400919 if (idata->ic.is_acmd) {
920 err = mmc_app_cmd(card->host, card);
921 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100922 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400923 }
924
Yaniv Gardia82e4842013-06-05 14:13:08 +0300925 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
926 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300927 err = ioctl_do_sanitize(card);
928
929 if (err)
930 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
931 __func__, err);
932
Jon Huntera5f57742015-09-22 10:27:53 +0100933 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300934 }
935
John Calixtocb87ea22011-04-26 18:56:29 -0400936 mmc_wait_for_req(card->host, &mrq);
937
938 if (cmd.error) {
939 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
940 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100941 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400942 }
943 if (data.error) {
944 dev_err(mmc_dev(card->host), "%s: data error %d\n",
945 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100946 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400947 }
948
949 /*
950 * According to the SD specs, some commands require a delay after
951 * issuing the command.
952 */
953 if (idata->ic.postsleep_min_us)
954 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
955
Jon Huntera5f57742015-09-22 10:27:53 +0100956 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400957
Krishna Kondae6711632014-12-04 15:20:57 +0200958 return err;
959}
960
961struct mmc_blk_ioc_rpmb_data {
962 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
963};
964
965static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
966 struct mmc_ioc_rpmb __user *user)
967{
968 struct mmc_blk_ioc_rpmb_data *idata;
969 int err, i;
970
971 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
972 if (!idata) {
973 err = -ENOMEM;
974 goto out;
975 }
976
977 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
978 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
979 if (IS_ERR(idata->data[i])) {
980 err = PTR_ERR(idata->data[i]);
981 goto copy_err;
982 }
983 }
984
985 return idata;
986
987copy_err:
988 while (--i >= 0) {
989 kfree(idata->data[i]->buf);
990 kfree(idata->data[i]);
991 }
992 kfree(idata);
993out:
994 return ERR_PTR(err);
995}
996
997static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
998 struct mmc_ioc_rpmb __user *ic_ptr)
999{
1000 struct mmc_blk_ioc_rpmb_data *idata;
1001 struct mmc_blk_data *md;
1002 struct mmc_card *card;
1003 struct mmc_command cmd = {0};
1004 struct mmc_data data = {0};
1005 struct mmc_request mrq = {NULL};
1006 struct scatterlist sg;
1007 int err = 0, i = 0;
1008 u32 status = 0;
1009
1010 /* The caller must have CAP_SYS_RAWIO */
1011 if (!capable(CAP_SYS_RAWIO))
1012 return -EPERM;
1013
1014 md = mmc_blk_get(bdev->bd_disk);
1015 /* make sure this is a rpmb partition */
1016 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1017 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001018 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001019 }
1020
1021 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1022 if (IS_ERR(idata)) {
1023 err = PTR_ERR(idata);
1024 goto cmd_done;
1025 }
1026
1027 card = md->queue.card;
1028 if (IS_ERR(card)) {
1029 err = PTR_ERR(card);
1030 goto idata_free;
1031 }
1032
Maya Erezdd669562015-02-12 20:37:31 +02001033 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001034
1035 err = mmc_blk_part_switch(card, md);
1036 if (err)
1037 goto cmd_rel_host;
1038
1039 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1040 struct mmc_blk_ioc_data *curr_data;
1041 struct mmc_ioc_cmd *curr_cmd;
1042
1043 curr_data = idata->data[i];
1044 curr_cmd = &curr_data->ic;
1045 if (!curr_cmd->opcode)
1046 break;
1047
1048 cmd.opcode = curr_cmd->opcode;
1049 cmd.arg = curr_cmd->arg;
1050 cmd.flags = curr_cmd->flags;
1051
1052 if (curr_data->buf_bytes) {
1053 data.sg = &sg;
1054 data.sg_len = 1;
1055 data.blksz = curr_cmd->blksz;
1056 data.blocks = curr_cmd->blocks;
1057
1058 sg_init_one(data.sg, curr_data->buf,
1059 curr_data->buf_bytes);
1060
1061 if (curr_cmd->write_flag)
1062 data.flags = MMC_DATA_WRITE;
1063 else
1064 data.flags = MMC_DATA_READ;
1065
1066 /* data.flags must already be set before doing this. */
1067 mmc_set_data_timeout(&data, card);
1068
1069 /*
1070 * Allow overriding the timeout_ns for empirical tuning.
1071 */
1072 if (curr_cmd->data_timeout_ns)
1073 data.timeout_ns = curr_cmd->data_timeout_ns;
1074
1075 mrq.data = &data;
1076 }
1077
1078 mrq.cmd = &cmd;
1079
1080 err = mmc_set_blockcount(card, data.blocks,
1081 curr_cmd->write_flag & (1 << 31));
1082 if (err)
1083 goto cmd_rel_host;
1084
1085 mmc_wait_for_req(card->host, &mrq);
1086
1087 if (cmd.error) {
1088 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1089 __func__, cmd.error);
1090 err = cmd.error;
1091 goto cmd_rel_host;
1092 }
1093 if (data.error) {
1094 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1095 __func__, data.error);
1096 err = data.error;
1097 goto cmd_rel_host;
1098 }
1099
1100 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1101 sizeof(cmd.resp))) {
1102 err = -EFAULT;
1103 goto cmd_rel_host;
1104 }
1105
1106 if (!curr_cmd->write_flag) {
1107 if (copy_to_user((void __user *)(unsigned long)
1108 curr_cmd->data_ptr,
1109 curr_data->buf,
1110 curr_data->buf_bytes)) {
1111 err = -EFAULT;
1112 goto cmd_rel_host;
1113 }
1114 }
1115
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001116 /*
1117 * Ensure RPMB command has completed by polling CMD13
1118 * "Send Status".
1119 */
1120 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1121 if (err)
1122 dev_err(mmc_dev(card->host),
1123 "%s: Card Status=0x%08X, error %d\n",
1124 __func__, status, err);
1125 }
1126
Krishna Kondae6711632014-12-04 15:20:57 +02001127cmd_rel_host:
1128 mmc_put_card(card);
1129
1130idata_free:
1131 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1132 kfree(idata->data[i]->buf);
1133 kfree(idata->data[i]);
1134 }
1135 kfree(idata);
1136
1137cmd_done:
1138 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001139 return err;
1140}
1141
1142static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1143 struct mmc_ioc_cmd __user *ic_ptr)
1144{
1145 struct mmc_blk_ioc_data *idata;
1146 struct mmc_blk_data *md;
1147 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001148 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001149
Shawn Lin83c742c2016-03-16 18:15:47 +08001150 /*
1151 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1152 * whole block device, not on a partition. This prevents overspray
1153 * between sibling partitions.
1154 */
1155 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1156 return -EPERM;
1157
Jon Huntera5f57742015-09-22 10:27:53 +01001158 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301159 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001160 return PTR_ERR(idata);
1161
1162 md = mmc_blk_get(bdev->bd_disk);
1163 if (!md) {
1164 err = -EINVAL;
1165 goto cmd_err;
1166 }
1167
1168 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301169 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001170 err = PTR_ERR(card);
1171 goto cmd_done;
1172 }
1173
1174 mmc_get_card(card);
1175
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301176 if (mmc_card_cmdq(card)) {
1177 err = mmc_cmdq_halt_on_empty_queue(card->host);
1178 if (err) {
1179 pr_err("%s: halt failed while doing %s err (%d)\n",
1180 mmc_hostname(card->host),
1181 __func__, err);
1182 mmc_put_card(card);
1183 goto cmd_done;
1184 }
1185 }
1186
Grant Grundlerb0934102015-09-23 18:30:33 -07001187 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001188
Adrian Hunter3c866562016-05-04 14:38:12 +03001189 /* Always switch back to main area after RPMB access */
1190 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1191 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1192
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001193 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001194
Grant Grundlerb0934102015-09-23 18:30:33 -07001195 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001196
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301197 if (mmc_card_cmdq(card)) {
1198 if (mmc_cmdq_halt(card->host, false))
1199 pr_err("%s: %s: cmdq unhalt failed\n",
1200 mmc_hostname(card->host), __func__);
1201 }
1202
John Calixtocb87ea22011-04-26 18:56:29 -04001203cmd_done:
1204 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001205cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001206 kfree(idata->buf);
1207 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001208 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001209}
1210
Jon Huntera5f57742015-09-22 10:27:53 +01001211static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1212 struct mmc_ioc_multi_cmd __user *user)
1213{
1214 struct mmc_blk_ioc_data **idata = NULL;
1215 struct mmc_ioc_cmd __user *cmds = user->cmds;
1216 struct mmc_card *card;
1217 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001218 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001219 __u64 num_of_cmds;
1220
Shawn Lin83c742c2016-03-16 18:15:47 +08001221 /*
1222 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1223 * whole block device, not on a partition. This prevents overspray
1224 * between sibling partitions.
1225 */
1226 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1227 return -EPERM;
1228
Jon Huntera5f57742015-09-22 10:27:53 +01001229 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1230 sizeof(num_of_cmds)))
1231 return -EFAULT;
1232
1233 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1234 return -EINVAL;
1235
1236 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1237 if (!idata)
1238 return -ENOMEM;
1239
1240 for (i = 0; i < num_of_cmds; i++) {
1241 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1242 if (IS_ERR(idata[i])) {
1243 err = PTR_ERR(idata[i]);
1244 num_of_cmds = i;
1245 goto cmd_err;
1246 }
1247 }
1248
1249 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001250 if (!md) {
1251 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001252 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001253 }
Jon Huntera5f57742015-09-22 10:27:53 +01001254
1255 card = md->queue.card;
1256 if (IS_ERR(card)) {
1257 err = PTR_ERR(card);
1258 goto cmd_done;
1259 }
1260
1261 mmc_get_card(card);
1262
Grant Grundlerb0934102015-09-23 18:30:33 -07001263 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1264 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001265
Adrian Hunter3c866562016-05-04 14:38:12 +03001266 /* Always switch back to main area after RPMB access */
1267 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1268 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1269
Jon Huntera5f57742015-09-22 10:27:53 +01001270 mmc_put_card(card);
1271
1272 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001273 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001274 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001275
1276cmd_done:
1277 mmc_blk_put(md);
1278cmd_err:
1279 for (i = 0; i < num_of_cmds; i++) {
1280 kfree(idata[i]->buf);
1281 kfree(idata[i]);
1282 }
1283 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001284 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001285}
1286
John Calixtocb87ea22011-04-26 18:56:29 -04001287static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1288 unsigned int cmd, unsigned long arg)
1289{
Jon Huntera5f57742015-09-22 10:27:53 +01001290 switch (cmd) {
1291 case MMC_IOC_CMD:
1292 return mmc_blk_ioctl_cmd(bdev,
1293 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001294 case MMC_IOC_RPMB_CMD:
1295 return mmc_blk_ioctl_rpmb_cmd(bdev,
1296 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001297 case MMC_IOC_MULTI_CMD:
1298 return mmc_blk_ioctl_multi_cmd(bdev,
1299 (struct mmc_ioc_multi_cmd __user *)arg);
1300 default:
1301 return -EINVAL;
1302 }
John Calixtocb87ea22011-04-26 18:56:29 -04001303}
1304
1305#ifdef CONFIG_COMPAT
1306static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1307 unsigned int cmd, unsigned long arg)
1308{
1309 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1310}
1311#endif
1312
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001313static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001314 .open = mmc_blk_open,
1315 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001316 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001318 .ioctl = mmc_blk_ioctl,
1319#ifdef CONFIG_COMPAT
1320 .compat_ioctl = mmc_blk_compat_ioctl,
1321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322};
1323
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001324static int mmc_blk_cmdq_switch(struct mmc_card *card,
1325 struct mmc_blk_data *md, bool enable)
1326{
1327 int ret = 0;
1328 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301329 struct mmc_host *host = card->host;
1330 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001331
1332 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1333 !card->ext_csd.cmdq_support ||
1334 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1335 (cmdq_mode == enable))
1336 return 0;
1337
1338 if (enable) {
1339 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1340 if (ret) {
1341 pr_err("%s: failed (%d) to set block-size to %d\n",
1342 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1343 goto out;
1344 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301345
1346 } else {
1347 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1348 ret = mmc_cmdq_halt(host, true);
1349 if (ret) {
1350 pr_err("%s: halt: failed: %d\n",
1351 mmc_hostname(host), ret);
1352 goto out;
1353 }
1354 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001355 }
1356
1357 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1358 EXT_CSD_CMDQ, enable,
1359 card->ext_csd.generic_cmd6_time);
1360 if (ret) {
1361 pr_err("%s: cmdq mode %sable failed %d\n",
1362 md->disk->disk_name, enable ? "en" : "dis", ret);
1363 goto out;
1364 }
1365
1366 if (enable)
1367 mmc_card_set_cmdq(card);
1368 else
1369 mmc_card_clr_cmdq(card);
1370out:
1371 return ret;
1372}
1373
Andrei Warkentin371a6892011-04-11 18:10:25 -05001374static inline int mmc_blk_part_switch(struct mmc_card *card,
1375 struct mmc_blk_data *md)
1376{
1377 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001378 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001379
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001380 if ((main_md->part_curr == md->part_type) &&
1381 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001382 return 0;
1383
1384 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001385 u8 part_config = card->ext_csd.part_config;
1386
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001387 if (md->part_type) {
1388 /* disable CQ mode for non-user data partitions */
1389 ret = mmc_blk_cmdq_switch(card, md, false);
1390 if (ret)
1391 return ret;
1392 }
1393
Adrian Hunter57da0c02016-05-04 14:38:13 +03001394 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1395 mmc_retune_pause(card->host);
1396
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001397 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1398 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001399
1400 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001401 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001402 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001403 if (ret) {
1404 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1405 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001406 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001407 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001408
1409 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001410 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001411
1412 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1413 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001414 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001415
1416 main_md->part_curr = md->part_type;
1417 return 0;
1418}
1419
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001420static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1421{
1422 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001423 u32 result;
1424 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001425
Venkatraman Sad5fd972011-08-25 00:30:50 +05301426 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001427 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001428 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001429
1430 struct scatterlist sg;
1431
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001432 cmd.opcode = MMC_APP_CMD;
1433 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001434 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001435
1436 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001437 if (err)
1438 return (u32)-1;
1439 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001440 return (u32)-1;
1441
1442 memset(&cmd, 0, sizeof(struct mmc_command));
1443
1444 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1445 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001446 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001447
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001448 data.blksz = 4;
1449 data.blocks = 1;
1450 data.flags = MMC_DATA_READ;
1451 data.sg = &sg;
1452 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301453 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001454
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001455 mrq.cmd = &cmd;
1456 mrq.data = &data;
1457
Ben Dooks051913d2009-06-08 23:33:57 +01001458 blocks = kmalloc(4, GFP_KERNEL);
1459 if (!blocks)
1460 return (u32)-1;
1461
1462 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001463
1464 mmc_wait_for_req(card->host, &mrq);
1465
Ben Dooks051913d2009-06-08 23:33:57 +01001466 result = ntohl(*blocks);
1467 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001468
Ben Dooks051913d2009-06-08 23:33:57 +01001469 if (cmd.error || data.error)
1470 result = (u32)-1;
1471
1472 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001473}
1474
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001475static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001476{
Chris Ball1278dba2011-04-13 23:40:30 -04001477 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001478 int err;
1479
Adrian Hunter504f1912008-10-16 12:55:25 +03001480 cmd.opcode = MMC_SEND_STATUS;
1481 if (!mmc_host_is_spi(card->host))
1482 cmd.arg = card->rca << 16;
1483 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001484 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1485 if (err == 0)
1486 *status = cmd.resp[0];
1487 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001488}
1489
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001490static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001491 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001492{
1493 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1494 int err = 0;
1495 u32 status;
1496
1497 do {
1498 err = get_card_status(card, &status, 5);
1499 if (err) {
1500 pr_err("%s: error %d requesting status\n",
1501 req->rq_disk->disk_name, err);
1502 return err;
1503 }
1504
1505 if (status & R1_ERROR) {
1506 pr_err("%s: %s: error sending status cmd, status %#x\n",
1507 req->rq_disk->disk_name, __func__, status);
1508 *gen_err = 1;
1509 }
1510
Ulf Hansson95a91292014-01-29 13:11:27 +01001511 /* We may rely on the host hw to handle busy detection.*/
1512 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1513 hw_busy_detect)
1514 break;
1515
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001516 /*
1517 * Timeout if the device never becomes ready for data and never
1518 * leaves the program state.
1519 */
1520 if (time_after(jiffies, timeout)) {
1521 pr_err("%s: Card stuck in programming state! %s %s\n",
1522 mmc_hostname(card->host),
1523 req->rq_disk->disk_name, __func__);
1524 return -ETIMEDOUT;
1525 }
1526
1527 /*
1528 * Some cards mishandle the status bits,
1529 * so make sure to check both the busy
1530 * indication and the card state.
1531 */
1532 } while (!(status & R1_READY_FOR_DATA) ||
1533 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1534
1535 return err;
1536}
1537
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001538static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1539 struct request *req, int *gen_err, u32 *stop_status)
1540{
1541 struct mmc_host *host = card->host;
1542 struct mmc_command cmd = {0};
1543 int err;
1544 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1545
1546 /*
1547 * Normally we use R1B responses for WRITE, but in cases where the host
1548 * has specified a max_busy_timeout we need to validate it. A failure
1549 * means we need to prevent the host from doing hw busy detection, which
1550 * is done by converting to a R1 response instead.
1551 */
1552 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1553 use_r1b_resp = false;
1554
1555 cmd.opcode = MMC_STOP_TRANSMISSION;
1556 if (use_r1b_resp) {
1557 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1558 cmd.busy_timeout = timeout_ms;
1559 } else {
1560 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1561 }
1562
1563 err = mmc_wait_for_cmd(host, &cmd, 5);
1564 if (err)
1565 return err;
1566
1567 *stop_status = cmd.resp[0];
1568
1569 /* No need to check card status in case of READ. */
1570 if (rq_data_dir(req) == READ)
1571 return 0;
1572
1573 if (!mmc_host_is_spi(host) &&
1574 (*stop_status & R1_ERROR)) {
1575 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1576 req->rq_disk->disk_name, __func__, *stop_status);
1577 *gen_err = 1;
1578 }
1579
1580 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1581}
1582
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301583#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001584#define ERR_RETRY 2
1585#define ERR_ABORT 1
1586#define ERR_CONTINUE 0
1587
1588static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1589 bool status_valid, u32 status)
1590{
1591 switch (error) {
1592 case -EILSEQ:
1593 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001594 pr_err_ratelimited(
1595 "%s: response CRC error sending %s command, card status %#x\n",
1596 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001597 name, status);
1598 return ERR_RETRY;
1599
1600 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001601 pr_err_ratelimited(
1602 "%s: timed out sending %s command, card status %#x\n",
1603 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001604
1605 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301606 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001607 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301608 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001609 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301610 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001611
1612 /*
1613 * If it was a r/w cmd crc error, or illegal command
1614 * (eg, issued in wrong state) then retry - we should
1615 * have corrected the state problem above.
1616 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301617 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001618 pr_err_ratelimited(
1619 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301620 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001621 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301622 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001623
1624 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001625 pr_err_ratelimited(
1626 "%s: not retrying timeout\n",
1627 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001628 return ERR_ABORT;
1629
1630 default:
1631 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001632 pr_err_ratelimited(
1633 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001634 req->rq_disk->disk_name, error, status);
1635 return ERR_ABORT;
1636 }
1637}
1638
1639/*
1640 * Initial r/w and stop cmd error recovery.
1641 * We don't know whether the card received the r/w cmd or not, so try to
1642 * restore things back to a sane state. Essentially, we do this as follows:
1643 * - Obtain card status. If the first attempt to obtain card status fails,
1644 * the status word will reflect the failed status cmd, not the failed
1645 * r/w cmd. If we fail to obtain card status, it suggests we can no
1646 * longer communicate with the card.
1647 * - Check the card state. If the card received the cmd but there was a
1648 * transient problem with the response, it might still be in a data transfer
1649 * mode. Try to send it a stop command. If this fails, we can't recover.
1650 * - If the r/w cmd failed due to a response CRC error, it was probably
1651 * transient, so retry the cmd.
1652 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1653 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1654 * illegal cmd, retry.
1655 * Otherwise we don't understand what happened, so abort.
1656 */
1657static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001658 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001659{
1660 bool prev_cmd_status_valid = true;
1661 u32 status, stop_status = 0;
1662 int err, retry;
1663
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301664 if (mmc_card_removed(card))
1665 return ERR_NOMEDIUM;
1666
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001667 /*
1668 * Try to get card status which indicates both the card state
1669 * and why there was no response. If the first attempt fails,
1670 * we can't be sure the returned status is for the r/w command.
1671 */
1672 for (retry = 2; retry >= 0; retry--) {
1673 err = get_card_status(card, &status, 0);
1674 if (!err)
1675 break;
1676
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001677 /* Re-tune if needed */
1678 mmc_retune_recheck(card->host);
1679
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001680 prev_cmd_status_valid = false;
1681 pr_err("%s: error %d sending status command, %sing\n",
1682 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1683 }
1684
1685 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301686 if (err) {
1687 /* Check if the card is removed */
1688 if (mmc_detect_card_removed(card->host))
1689 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001690 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301691 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001692
Adrian Hunter67716322011-08-29 16:42:15 +03001693 /* Flag ECC errors */
1694 if ((status & R1_CARD_ECC_FAILED) ||
1695 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1696 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1697 *ecc_err = 1;
1698
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001699 /* Flag General errors */
1700 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1701 if ((status & R1_ERROR) ||
1702 (brq->stop.resp[0] & R1_ERROR)) {
1703 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1704 req->rq_disk->disk_name, __func__,
1705 brq->stop.resp[0], status);
1706 *gen_err = 1;
1707 }
1708
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001709 /*
1710 * Check the current card state. If it is in some data transfer
1711 * mode, tell it to stop (and hopefully transition back to TRAN.)
1712 */
1713 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1714 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001715 err = send_stop(card,
1716 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1717 req, gen_err, &stop_status);
1718 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001719 pr_err("%s: error %d sending stop command\n",
1720 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001721 /*
1722 * If the stop cmd also timed out, the card is probably
1723 * not present, so abort. Other errors are bad news too.
1724 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001725 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001726 }
1727
Adrian Hunter67716322011-08-29 16:42:15 +03001728 if (stop_status & R1_CARD_ECC_FAILED)
1729 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001730 }
1731
1732 /* Check for set block count errors */
1733 if (brq->sbc.error)
1734 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1735 prev_cmd_status_valid, status);
1736
1737 /* Check for r/w command errors */
1738 if (brq->cmd.error)
1739 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1740 prev_cmd_status_valid, status);
1741
Adrian Hunter67716322011-08-29 16:42:15 +03001742 /* Data errors */
1743 if (!brq->stop.error)
1744 return ERR_CONTINUE;
1745
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001746 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001747 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 +01001748 req->rq_disk->disk_name, brq->stop.error,
1749 brq->cmd.resp[0], status);
1750
1751 /*
1752 * Subsitute in our own stop status as this will give the error
1753 * state which happened during the execution of the r/w command.
1754 */
1755 if (stop_status) {
1756 brq->stop.resp[0] = stop_status;
1757 brq->stop.error = 0;
1758 }
1759 return ERR_CONTINUE;
1760}
1761
Adrian Hunter67716322011-08-29 16:42:15 +03001762static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1763 int type)
1764{
1765 int err;
1766
1767 if (md->reset_done & type)
1768 return -EEXIST;
1769
1770 md->reset_done |= type;
1771 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301772 if (err && err != -EOPNOTSUPP) {
1773 /* We failed to reset so we need to abort the request */
1774 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1775 __func__, err);
1776 return -ENODEV;
1777 }
1778
Adrian Hunter67716322011-08-29 16:42:15 +03001779 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301780 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001781 struct mmc_blk_data *main_md =
1782 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001783 int part_err;
1784
1785 main_md->part_curr = main_md->part_type;
1786 part_err = mmc_blk_part_switch(host->card, md);
1787 if (part_err) {
1788 /*
1789 * We have failed to get back into the correct
1790 * partition, so we need to abort the whole request.
1791 */
1792 return -ENODEV;
1793 }
1794 }
1795 return err;
1796}
1797
1798static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1799{
1800 md->reset_done &= ~type;
1801}
1802
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001803int mmc_access_rpmb(struct mmc_queue *mq)
1804{
1805 struct mmc_blk_data *md = mq->data;
1806 /*
1807 * If this is a RPMB partition access, return ture
1808 */
1809 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1810 return true;
1811
1812 return false;
1813}
1814
Sahitya Tummala9433a132015-06-09 09:38:36 +05301815static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1816 struct request *req)
1817{
1818 struct mmc_blk_data *md = mq->data;
1819 struct mmc_card *card = md->queue.card;
1820 struct mmc_host *host = card->host;
1821 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1822 struct mmc_cmdq_req *cmdq_req;
1823 struct mmc_queue_req *active_mqrq;
1824
1825 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1826 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1827
1828 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1829
1830 active_mqrq = &mq->mqrq_cmdq[req->tag];
1831 active_mqrq->req = req;
1832
1833 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1834 cmdq_req->cmdq_req_flags |= QBR;
1835 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1836 cmdq_req->tag = req->tag;
1837 return cmdq_req;
1838}
1839
1840static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1841 struct request *req)
1842{
1843 struct mmc_blk_data *md = mq->data;
1844 struct mmc_card *card = md->queue.card;
1845 struct mmc_cmdq_req *cmdq_req = NULL;
1846 struct mmc_host *host = card->host;
1847 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1848 unsigned int from, nr, arg;
1849 int err = 0;
1850
1851 if (!mmc_can_erase(card)) {
1852 err = -EOPNOTSUPP;
1853 goto out;
1854 }
1855
1856 from = blk_rq_pos(req);
1857 nr = blk_rq_sectors(req);
1858
1859 if (mmc_can_discard(card))
1860 arg = MMC_DISCARD_ARG;
1861 else if (mmc_can_trim(card))
1862 arg = MMC_TRIM_ARG;
1863 else
1864 arg = MMC_ERASE_ARG;
1865
1866 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1867 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1868 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1869 EXT_CSD_CMD_SET_NORMAL,
1870 INAND_CMD38_ARG_EXT_CSD,
1871 arg == MMC_TRIM_ARG ?
1872 INAND_CMD38_ARG_TRIM :
1873 INAND_CMD38_ARG_ERASE,
1874 0, true, false);
1875 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1876 if (err)
1877 goto clear_dcmd;
1878 }
1879 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1880clear_dcmd:
1881 /* clear pending request */
1882 if (cmdq_req) {
1883 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1884 &ctx_info->active_reqs));
1885 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1886 }
1887out:
1888 blk_end_request(req, err, blk_rq_bytes(req));
1889
1890 if (test_and_clear_bit(0, &ctx_info->req_starved))
1891 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001892 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301893 return err ? 1 : 0;
1894}
1895
Adrian Hunterbd788c92010-08-11 14:17:47 -07001896static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1897{
1898 struct mmc_blk_data *md = mq->data;
1899 struct mmc_card *card = md->queue.card;
1900 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001901 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001902
Adrian Hunterbd788c92010-08-11 14:17:47 -07001903 if (!mmc_can_erase(card)) {
1904 err = -EOPNOTSUPP;
1905 goto out;
1906 }
1907
1908 from = blk_rq_pos(req);
1909 nr = blk_rq_sectors(req);
1910
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001911 if (mmc_can_discard(card))
1912 arg = MMC_DISCARD_ARG;
1913 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001914 arg = MMC_TRIM_ARG;
1915 else
1916 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001917retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001918 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1919 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1920 INAND_CMD38_ARG_EXT_CSD,
1921 arg == MMC_TRIM_ARG ?
1922 INAND_CMD38_ARG_TRIM :
1923 INAND_CMD38_ARG_ERASE,
1924 0);
1925 if (err)
1926 goto out;
1927 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001928 err = mmc_erase(card, from, nr, arg);
1929out:
Adrian Hunter67716322011-08-29 16:42:15 +03001930 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1931 goto retry;
1932 if (!err)
1933 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301934 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001935
Adrian Hunterbd788c92010-08-11 14:17:47 -07001936 return err ? 0 : 1;
1937}
1938
Sahitya Tummala9433a132015-06-09 09:38:36 +05301939static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1940 struct request *req)
1941{
1942 struct mmc_blk_data *md = mq->data;
1943 struct mmc_card *card = md->queue.card;
1944 struct mmc_cmdq_req *cmdq_req = NULL;
1945 unsigned int from, nr, arg;
1946 struct mmc_host *host = card->host;
1947 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1948 int err = 0;
1949
1950 if (!(mmc_can_secure_erase_trim(card))) {
1951 err = -EOPNOTSUPP;
1952 goto out;
1953 }
1954
1955 from = blk_rq_pos(req);
1956 nr = blk_rq_sectors(req);
1957
1958 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1959 arg = MMC_SECURE_TRIM1_ARG;
1960 else
1961 arg = MMC_SECURE_ERASE_ARG;
1962
1963 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1964 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1965 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1966 EXT_CSD_CMD_SET_NORMAL,
1967 INAND_CMD38_ARG_EXT_CSD,
1968 arg == MMC_SECURE_TRIM1_ARG ?
1969 INAND_CMD38_ARG_SECTRIM1 :
1970 INAND_CMD38_ARG_SECERASE,
1971 0, true, false);
1972 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1973 if (err)
1974 goto clear_dcmd;
1975 }
1976
1977 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1978 if (err)
1979 goto clear_dcmd;
1980
1981 if (arg == MMC_SECURE_TRIM1_ARG) {
1982 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1983 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1984 EXT_CSD_CMD_SET_NORMAL,
1985 INAND_CMD38_ARG_EXT_CSD,
1986 INAND_CMD38_ARG_SECTRIM2,
1987 0, true, false);
1988 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1989 if (err)
1990 goto clear_dcmd;
1991 }
1992
1993 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
1994 MMC_SECURE_TRIM2_ARG);
1995 }
1996clear_dcmd:
1997 /* clear pending request */
1998 if (cmdq_req) {
1999 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
2000 &ctx_info->active_reqs));
2001 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
2002 }
2003out:
2004 blk_end_request(req, err, blk_rq_bytes(req));
2005
2006 if (test_and_clear_bit(0, &ctx_info->req_starved))
2007 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03002008 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302009 return err ? 1 : 0;
2010}
2011
Adrian Hunter49804542010-08-11 14:17:50 -07002012static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2013 struct request *req)
2014{
2015 struct mmc_blk_data *md = mq->data;
2016 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002017 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002018 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002019
Maya Erez775a9362013-04-18 15:41:55 +03002020 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002021 err = -EOPNOTSUPP;
2022 goto out;
2023 }
2024
2025 from = blk_rq_pos(req);
2026 nr = blk_rq_sectors(req);
2027
Maya Erez775a9362013-04-18 15:41:55 +03002028 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2029 arg = MMC_SECURE_TRIM1_ARG;
2030 else
2031 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002032
Adrian Hunter67716322011-08-29 16:42:15 +03002033retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002034 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2035 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2036 INAND_CMD38_ARG_EXT_CSD,
2037 arg == MMC_SECURE_TRIM1_ARG ?
2038 INAND_CMD38_ARG_SECTRIM1 :
2039 INAND_CMD38_ARG_SECERASE,
2040 0);
2041 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002042 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002043 }
Adrian Hunter28302812012-04-05 14:45:48 +03002044
Adrian Hunter49804542010-08-11 14:17:50 -07002045 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002046 if (err == -EIO)
2047 goto out_retry;
2048 if (err)
2049 goto out;
2050
2051 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002052 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2053 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2054 INAND_CMD38_ARG_EXT_CSD,
2055 INAND_CMD38_ARG_SECTRIM2,
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, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002062 if (err == -EIO)
2063 goto out_retry;
2064 if (err)
2065 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002066 }
Adrian Hunter28302812012-04-05 14:45:48 +03002067
Adrian Hunter28302812012-04-05 14:45:48 +03002068out_retry:
2069 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002070 goto retry;
2071 if (!err)
2072 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002073out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302074 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002075
Adrian Hunter49804542010-08-11 14:17:50 -07002076 return err ? 0 : 1;
2077}
2078
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002079static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2080{
2081 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002082 struct mmc_card *card = md->queue.card;
2083 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002084
Sahitya Tummala61868a42015-05-28 16:54:19 +05302085 if (!req)
2086 return 0;
2087
2088 if (req->cmd_flags & REQ_BARRIER) {
2089 /*
2090 * If eMMC cache flush policy is set to 1, then the device
2091 * shall flush the requests in First-In-First-Out (FIFO) order.
2092 * In this case, as per spec, the host must not send any cache
2093 * barrier requests as they are redundant and add unnecessary
2094 * overhead to both device and host.
2095 */
2096 if (card->ext_csd.cache_flush_policy & 1)
2097 goto end_req;
2098
2099 /*
2100 * In case barrier is not supported or enabled in the device,
2101 * use flush as a fallback option.
2102 */
2103 ret = mmc_cache_barrier(card);
2104 if (ret)
2105 ret = mmc_flush_cache(card);
2106 } else if (req_op(req) == REQ_OP_FLUSH) {
2107 ret = mmc_flush_cache(card);
2108 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002109 if (ret == -ENODEV) {
2110 pr_err("%s: %s: restart mmc card",
2111 req->rq_disk->disk_name, __func__);
2112 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2113 pr_err("%s: %s: fail to restart mmc",
2114 req->rq_disk->disk_name, __func__);
2115 else
2116 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2117 }
2118
2119 if (ret) {
2120 pr_err("%s: %s: notify flush error to upper layers",
2121 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002122 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002123 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002124
Mark Salyzyn6904e432016-01-28 11:12:25 -08002125#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2126 else if (atomic_read(&mq->cache_size)) {
2127 long used = mmc_blk_cache_used(mq, jiffies);
2128
2129 if (used) {
2130 int speed = atomic_read(&mq->max_write_speed);
2131
2132 if (speed_valid(speed)) {
2133 unsigned long msecs = jiffies_to_msecs(
2134 size_and_speed_to_jiffies(
2135 used, speed));
2136 if (msecs)
2137 msleep(msecs);
2138 }
2139 }
2140 }
2141#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302142end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302143 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002144
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002145 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002146}
2147
2148/*
2149 * Reformat current write as a reliable write, supporting
2150 * both legacy and the enhanced reliable write MMC cards.
2151 * In each transfer we'll handle only as much as a single
2152 * reliable write can handle, thus finish the request in
2153 * partial completions.
2154 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002155static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2156 struct mmc_card *card,
2157 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002158{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002159 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2160 /* Legacy mode imposes restrictions on transfers. */
2161 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2162 brq->data.blocks = 1;
2163
2164 if (brq->data.blocks > card->ext_csd.rel_sectors)
2165 brq->data.blocks = card->ext_csd.rel_sectors;
2166 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2167 brq->data.blocks = 1;
2168 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002169}
2170
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002171#define CMD_ERRORS \
2172 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2173 R1_ADDRESS_ERROR | /* Misaligned address */ \
2174 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2175 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2176 R1_CC_ERROR | /* Card controller error */ \
2177 R1_ERROR) /* General/unknown error */
2178
Per Forlinee8a43a2011-07-01 18:55:33 +02002179static int mmc_blk_err_check(struct mmc_card *card,
2180 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002181{
Per Forlinee8a43a2011-07-01 18:55:33 +02002182 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2183 mmc_active);
2184 struct mmc_blk_request *brq = &mq_mrq->brq;
2185 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002186 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002187 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002188
2189 /*
2190 * sbc.error indicates a problem with the set block count
2191 * command. No data will have been transferred.
2192 *
2193 * cmd.error indicates a problem with the r/w command. No
2194 * data will have been transferred.
2195 *
2196 * stop.error indicates a problem with the stop command. Data
2197 * may have been transferred, or may still be transferring.
2198 */
Adrian Hunter67716322011-08-29 16:42:15 +03002199 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2200 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002201 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002202 case ERR_RETRY:
2203 return MMC_BLK_RETRY;
2204 case ERR_ABORT:
2205 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302206 case ERR_NOMEDIUM:
2207 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002208 case ERR_CONTINUE:
2209 break;
2210 }
2211 }
2212
2213 /*
2214 * Check for errors relating to the execution of the
2215 * initial command - such as address errors. No data
2216 * has been transferred.
2217 */
2218 if (brq->cmd.resp[0] & CMD_ERRORS) {
2219 pr_err("%s: r/w command failed, status = %#x\n",
2220 req->rq_disk->disk_name, brq->cmd.resp[0]);
2221 return MMC_BLK_ABORT;
2222 }
2223
2224 /*
2225 * Everything else is either success, or a data error of some
2226 * kind. If it was a write, we may have transitioned to
2227 * program mode, which we have to wait for it to complete.
2228 */
2229 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002230 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002231
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002232 /* Check stop command response */
2233 if (brq->stop.resp[0] & R1_ERROR) {
2234 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2235 req->rq_disk->disk_name, __func__,
2236 brq->stop.resp[0]);
2237 gen_err = 1;
2238 }
2239
Ulf Hansson95a91292014-01-29 13:11:27 +01002240 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2241 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002242 if (err)
2243 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002244 }
2245
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002246 /* if general error occurs, retry the write operation. */
2247 if (gen_err) {
2248 pr_warn("%s: retrying write for general error\n",
2249 req->rq_disk->disk_name);
2250 return MMC_BLK_RETRY;
2251 }
2252
Per Forlind78d4a82011-07-01 18:55:30 +02002253 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002254 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002255 pr_debug("%s: retrying because a re-tune was needed\n",
2256 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002257 brq->retune_retry_done = 1;
2258 return MMC_BLK_RETRY;
2259 }
Per Forlind78d4a82011-07-01 18:55:30 +02002260 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2261 req->rq_disk->disk_name, brq->data.error,
2262 (unsigned)blk_rq_pos(req),
2263 (unsigned)blk_rq_sectors(req),
2264 brq->cmd.resp[0], brq->stop.resp[0]);
2265
2266 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002267 if (ecc_err)
2268 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002269 return MMC_BLK_DATA_ERR;
2270 } else {
2271 return MMC_BLK_CMD_ERR;
2272 }
2273 }
2274
Adrian Hunter67716322011-08-29 16:42:15 +03002275 if (!brq->data.bytes_xfered)
2276 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002277
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002278 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2279 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2280 return MMC_BLK_PARTIAL;
2281 else
2282 return MMC_BLK_SUCCESS;
2283 }
2284
Adrian Hunter67716322011-08-29 16:42:15 +03002285 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2286 return MMC_BLK_PARTIAL;
2287
2288 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002289}
2290
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002291static int mmc_blk_packed_err_check(struct mmc_card *card,
2292 struct mmc_async_req *areq)
2293{
2294 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2295 mmc_active);
2296 struct request *req = mq_rq->req;
2297 struct mmc_packed *packed = mq_rq->packed;
2298 int err, check, status;
2299 u8 *ext_csd;
2300
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002301 packed->retries--;
2302 check = mmc_blk_err_check(card, areq);
2303 err = get_card_status(card, &status, 0);
2304 if (err) {
2305 pr_err("%s: error %d sending status command\n",
2306 req->rq_disk->disk_name, err);
2307 return MMC_BLK_ABORT;
2308 }
2309
2310 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002311 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002312 if (err) {
2313 pr_err("%s: error %d sending ext_csd\n",
2314 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002315 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002316 }
2317
2318 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2319 EXT_CSD_PACKED_FAILURE) &&
2320 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2321 EXT_CSD_PACKED_GENERIC_ERROR)) {
2322 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2323 EXT_CSD_PACKED_INDEXED_ERROR) {
2324 packed->idx_failure =
2325 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2326 check = MMC_BLK_PARTIAL;
2327 }
2328 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2329 "failure index: %d\n",
2330 req->rq_disk->disk_name, packed->nr_entries,
2331 packed->blocks, packed->idx_failure);
2332 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002333 kfree(ext_csd);
2334 }
2335
2336 return check;
2337}
2338
Per Forlin54d49d72011-07-01 18:55:29 +02002339static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2340 struct mmc_card *card,
2341 int disable_multi,
2342 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Per Forlin54d49d72011-07-01 18:55:29 +02002344 u32 readcmd, writecmd;
2345 struct mmc_blk_request *brq = &mqrq->brq;
2346 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302348 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002350 /*
2351 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002352 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002353 */
Luca Porziod3df0462015-11-06 15:12:26 +00002354 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002355 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002356 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002357
Per Forlin54d49d72011-07-01 18:55:29 +02002358 memset(brq, 0, sizeof(struct mmc_blk_request));
2359 brq->mrq.cmd = &brq->cmd;
2360 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Per Forlin54d49d72011-07-01 18:55:29 +02002362 brq->cmd.arg = blk_rq_pos(req);
2363 if (!mmc_card_blockaddr(card))
2364 brq->cmd.arg <<= 9;
2365 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2366 brq->data.blksz = 512;
2367 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2368 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002369 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Asutosh Dasf0665412012-07-27 18:10:19 +05302371 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002372 /*
2373 * The block layer doesn't support all sector count
2374 * restrictions, so we need to be prepared for too big
2375 * requests.
2376 */
2377 if (brq->data.blocks > card->host->max_blk_count)
2378 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002380 if (brq->data.blocks > 1) {
2381 /*
2382 * After a read error, we redo the request one sector
2383 * at a time in order to accurately determine which
2384 * sectors can be read successfully.
2385 */
2386 if (disable_multi)
2387 brq->data.blocks = 1;
2388
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002389 /*
2390 * Some controllers have HW issues while operating
2391 * in multiple I/O mode
2392 */
2393 if (card->host->ops->multi_io_quirk)
2394 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2395 (rq_data_dir(req) == READ) ?
2396 MMC_DATA_READ : MMC_DATA_WRITE,
2397 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002398 }
Per Forlin54d49d72011-07-01 18:55:29 +02002399
2400 if (brq->data.blocks > 1 || do_rel_wr) {
2401 /* SPI multiblock writes terminate using a special
2402 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002403 */
Per Forlin54d49d72011-07-01 18:55:29 +02002404 if (!mmc_host_is_spi(card->host) ||
2405 rq_data_dir(req) == READ)
2406 brq->mrq.stop = &brq->stop;
2407 readcmd = MMC_READ_MULTIPLE_BLOCK;
2408 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2409 } else {
2410 brq->mrq.stop = NULL;
2411 readcmd = MMC_READ_SINGLE_BLOCK;
2412 writecmd = MMC_WRITE_BLOCK;
2413 }
2414 if (rq_data_dir(req) == READ) {
2415 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002416 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002417 if (brq->mrq.stop)
2418 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2419 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002420 } else {
2421 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002422 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002423 if (brq->mrq.stop)
2424 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2425 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002426 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002427
Per Forlin54d49d72011-07-01 18:55:29 +02002428 if (do_rel_wr)
2429 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002430
Per Forlin54d49d72011-07-01 18:55:29 +02002431 /*
Saugata Das42659002011-12-21 13:09:17 +05302432 * Data tag is used only during writing meta data to speed
2433 * up write and any subsequent read of this meta data
2434 */
2435 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2436 (req->cmd_flags & REQ_META) &&
2437 (rq_data_dir(req) == WRITE) &&
2438 ((brq->data.blocks * brq->data.blksz) >=
2439 card->ext_csd.data_tag_unit_size);
2440
2441 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002442 * Pre-defined multi-block transfers are preferable to
2443 * open ended-ones (and necessary for reliable writes).
2444 * However, it is not sufficient to just send CMD23,
2445 * and avoid the final CMD12, as on an error condition
2446 * CMD12 (stop) needs to be sent anyway. This, coupled
2447 * with Auto-CMD23 enhancements provided by some
2448 * hosts, means that the complexity of dealing
2449 * with this is best left to the host. If CMD23 is
2450 * supported by card and host, we'll fill sbc in and let
2451 * the host deal with handling it correctly. This means
2452 * that for hosts that don't expose MMC_CAP_CMD23, no
2453 * change of behavior will be observed.
2454 *
2455 * N.B: Some MMC cards experience perf degradation.
2456 * We'll avoid using CMD23-bounded multiblock writes for
2457 * these, while retaining features like reliable writes.
2458 */
Saugata Das42659002011-12-21 13:09:17 +05302459 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2460 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2461 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002462 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2463 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302464 (do_rel_wr ? (1 << 31) : 0) |
2465 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002466 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2467 brq->mrq.sbc = &brq->sbc;
2468 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002469
Per Forlin54d49d72011-07-01 18:55:29 +02002470 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002471
Per Forlin54d49d72011-07-01 18:55:29 +02002472 brq->data.sg = mqrq->sg;
2473 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002474
Per Forlin54d49d72011-07-01 18:55:29 +02002475 /*
2476 * Adjust the sg list so it is the same size as the
2477 * request.
2478 */
2479 if (brq->data.blocks != blk_rq_sectors(req)) {
2480 int i, data_size = brq->data.blocks << 9;
2481 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002482
Per Forlin54d49d72011-07-01 18:55:29 +02002483 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2484 data_size -= sg->length;
2485 if (data_size <= 0) {
2486 sg->length += data_size;
2487 i++;
2488 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002489 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002490 }
Per Forlin54d49d72011-07-01 18:55:29 +02002491 brq->data.sg_len = i;
2492 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002493
Per Forlinee8a43a2011-07-01 18:55:33 +02002494 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302495 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002496 mqrq->mmc_active.err_check = mmc_blk_err_check;
2497
Per Forlin54d49d72011-07-01 18:55:29 +02002498 mmc_queue_bounce_pre(mqrq);
2499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002501static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2502 struct mmc_card *card)
2503{
2504 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2505 unsigned int max_seg_sz = queue_max_segment_size(q);
2506 unsigned int len, nr_segs = 0;
2507
2508 do {
2509 len = min(hdr_sz, max_seg_sz);
2510 hdr_sz -= len;
2511 nr_segs++;
2512 } while (hdr_sz);
2513
2514 return nr_segs;
2515}
2516
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002517/**
2518 * mmc_blk_disable_wr_packing() - disables packing mode
2519 * @mq: MMC queue.
2520 *
2521 */
2522void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2523{
2524 if (mq) {
2525 mq->wr_packing_enabled = false;
2526 mq->num_of_potential_packed_wr_reqs = 0;
2527 }
2528}
2529EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2530
Lee Susman841fd132013-04-23 17:59:26 +03002531static int get_packed_trigger(int potential, struct mmc_card *card,
2532 struct request *req, int curr_trigger)
2533{
2534 static int num_mean_elements = 1;
2535 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2536 unsigned int trigger = curr_trigger;
2537 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2538
2539 /* scale down the upper bound to 75% */
2540 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2541
2542 /*
2543 * since the most common calls for this function are with small
2544 * potential write values and since we don't want these calls to affect
2545 * the packed trigger, set a lower bound and ignore calls with
2546 * potential lower than that bound
2547 */
2548 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2549 return trigger;
2550
2551 /*
2552 * this is to prevent integer overflow in the following calculation:
2553 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2554 */
2555 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2556 num_mean_elements = 1;
2557 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2558 }
2559
2560 /*
2561 * get next mean value based on previous mean value and current
2562 * potential packed writes. Calculation is as follows:
2563 * mean_pot[i+1] =
2564 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2565 */
2566 mean_potential *= num_mean_elements;
2567 /*
2568 * add num_mean_elements so that the division of two integers doesn't
2569 * lower mean_potential too much
2570 */
2571 if (potential > mean_potential)
2572 mean_potential += num_mean_elements;
2573 mean_potential += potential;
2574 /* this is for gaining more precision when dividing two integers */
2575 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2576 /* this completes the mean calculation */
2577 mean_potential /= ++num_mean_elements;
2578 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2579
2580 /*
2581 * if current potential packed writes is greater than the mean potential
2582 * then the heuristic is that the following workload will contain many
2583 * write requests, therefore we lower the packed trigger. In the
2584 * opposite case we want to increase the trigger in order to get less
2585 * packing events.
2586 */
2587 if (potential >= mean_potential)
2588 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2589 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2590 else
2591 trigger = (trigger >= pckd_trgr_upper_bound) ?
2592 pckd_trgr_upper_bound : trigger + 1;
2593
2594 /*
2595 * an urgent read request indicates a packed list being interrupted
2596 * by this read, therefore we aim for less packing, hence the trigger
2597 * gets increased
2598 */
2599 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2600 trigger += PCKD_TRGR_URGENT_PENALTY;
2601
2602 return trigger;
2603}
2604
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002605static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2606 struct request *req)
2607{
2608 struct mmc_host *host = mq->card->host;
2609 int data_dir;
2610
2611 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2612 return;
2613
Maya Erez8e2b3c32012-12-02 13:27:15 +02002614 /* Support for the write packing on eMMC 4.5 or later */
2615 if (mq->card->ext_csd.rev <= 5)
2616 return;
2617
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002618 /*
2619 * In case the packing control is not supported by the host, it should
2620 * not have an effect on the write packing. Therefore we have to enable
2621 * the write packing
2622 */
2623 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2624 mq->wr_packing_enabled = true;
2625 return;
2626 }
2627
2628 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2629 if (mq->num_of_potential_packed_wr_reqs >
2630 mq->num_wr_reqs_to_start_packing)
2631 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002632 mq->num_wr_reqs_to_start_packing =
2633 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2634 mq->card, req,
2635 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002636 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002637 return;
2638 }
2639
2640 data_dir = rq_data_dir(req);
2641
2642 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002643 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002644 mq->num_wr_reqs_to_start_packing =
2645 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2646 mq->card, req,
2647 mq->num_wr_reqs_to_start_packing);
2648 mq->num_of_potential_packed_wr_reqs = 0;
2649 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002650 return;
2651 } else if (data_dir == WRITE) {
2652 mq->num_of_potential_packed_wr_reqs++;
2653 }
2654
2655 if (mq->num_of_potential_packed_wr_reqs >
2656 mq->num_wr_reqs_to_start_packing)
2657 mq->wr_packing_enabled = true;
2658}
2659
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002660struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2661{
2662 if (!card)
2663 return NULL;
2664
2665 return &card->wr_pack_stats;
2666}
2667EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2668
2669void mmc_blk_init_packed_statistics(struct mmc_card *card)
2670{
2671 int max_num_of_packed_reqs = 0;
2672
2673 if (!card || !card->wr_pack_stats.packing_events)
2674 return;
2675
2676 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2677
2678 spin_lock(&card->wr_pack_stats.lock);
2679 memset(card->wr_pack_stats.packing_events, 0,
2680 (max_num_of_packed_reqs + 1) *
2681 sizeof(*card->wr_pack_stats.packing_events));
2682 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2683 sizeof(card->wr_pack_stats.pack_stop_reason));
2684 card->wr_pack_stats.enabled = true;
2685 spin_unlock(&card->wr_pack_stats.lock);
2686}
2687EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2688
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002689static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2690{
2691 struct request_queue *q = mq->queue;
2692 struct mmc_card *card = mq->card;
2693 struct request *cur = req, *next = NULL;
2694 struct mmc_blk_data *md = mq->data;
2695 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2696 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2697 unsigned int req_sectors = 0, phys_segments = 0;
2698 unsigned int max_blk_count, max_phys_segs;
2699 bool put_back = true;
2700 u8 max_packed_rw = 0;
2701 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002702 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002703
Shawn Lin96e52da2016-08-26 08:49:55 +08002704 /*
2705 * We don't need to check packed for any further
2706 * operation of packed stuff as we set MMC_PACKED_NONE
2707 * and return zero for reqs if geting null packed. Also
2708 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2709 * it again when removing blk req.
2710 */
2711 if (!mqrq->packed) {
2712 md->flags &= (~MMC_BLK_PACKED_CMD);
2713 goto no_packed;
2714 }
2715
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002716 if (!(md->flags & MMC_BLK_PACKED_CMD))
2717 goto no_packed;
2718
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002719 if (!mq->wr_packing_enabled)
2720 goto no_packed;
2721
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002722 if ((rq_data_dir(cur) == WRITE) &&
2723 mmc_host_packed_wr(card->host))
2724 max_packed_rw = card->ext_csd.max_packed_writes;
2725
2726 if (max_packed_rw == 0)
2727 goto no_packed;
2728
2729 if (mmc_req_rel_wr(cur) &&
2730 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2731 goto no_packed;
2732
2733 if (mmc_large_sector(card) &&
2734 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2735 goto no_packed;
2736
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002737 if (cur->cmd_flags & REQ_FUA)
2738 goto no_packed;
2739
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002740 mmc_blk_clear_packed(mqrq);
2741
2742 max_blk_count = min(card->host->max_blk_count,
2743 card->host->max_req_size >> 9);
2744 if (unlikely(max_blk_count > 0xffff))
2745 max_blk_count = 0xffff;
2746
2747 max_phys_segs = queue_max_segments(q);
2748 req_sectors += blk_rq_sectors(cur);
2749 phys_segments += cur->nr_phys_segments;
2750
2751 if (rq_data_dir(cur) == WRITE) {
2752 req_sectors += mmc_large_sector(card) ? 8 : 1;
2753 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2754 }
2755
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002756 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002757 do {
2758 if (reqs >= max_packed_rw - 1) {
2759 put_back = false;
2760 break;
2761 }
2762
2763 spin_lock_irq(q->queue_lock);
2764 next = blk_fetch_request(q);
2765 spin_unlock_irq(q->queue_lock);
2766 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002767 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002768 put_back = false;
2769 break;
2770 }
2771
2772 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002773 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2774 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002775 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002776 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002777
Mike Christie3a5e02c2016-06-05 14:32:23 -05002778 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002779 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002780 req_op(next) == REQ_OP_FLUSH) {
2781 if (req_op(next) != REQ_OP_SECURE_ERASE)
2782 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002783 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002784 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002785
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002786 if (next->cmd_flags & REQ_FUA) {
2787 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2788 break;
2789 }
2790
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002791 if (rq_data_dir(cur) != rq_data_dir(next)) {
2792 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002793 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002794 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002795
2796 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002797 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2798 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002799 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002800 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002801
2802 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002803 if (req_sectors > max_blk_count) {
2804 if (stats->enabled)
2805 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002806 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002807 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002808
2809 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002810 if (phys_segments > max_phys_segs) {
2811 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002812 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002813 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002814
Maya Erez5a8dae12014-12-04 15:13:59 +02002815 if (mq->no_pack_for_random) {
2816 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2817 blk_rq_pos(next)) {
2818 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2819 put_back = 1;
2820 break;
2821 }
2822 }
2823
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002824 if (rq_data_dir(next) == WRITE)
2825 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002826 list_add_tail(&next->queuelist, &mqrq->packed->list);
2827 cur = next;
2828 reqs++;
2829 } while (1);
2830
2831 if (put_back) {
2832 spin_lock_irq(q->queue_lock);
2833 blk_requeue_request(q, next);
2834 spin_unlock_irq(q->queue_lock);
2835 }
2836
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002837 if (stats->enabled) {
2838 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2839 stats->packing_events[reqs + 1]++;
2840 if (reqs + 1 == max_packed_rw)
2841 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2842 }
2843
2844 spin_unlock(&stats->lock);
2845
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002846 if (reqs > 0) {
2847 list_add(&req->queuelist, &mqrq->packed->list);
2848 mqrq->packed->nr_entries = ++reqs;
2849 mqrq->packed->retries = reqs;
2850 return reqs;
2851 }
2852
2853no_packed:
2854 mqrq->cmd_type = MMC_PACKED_NONE;
2855 return 0;
2856}
2857
2858static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2859 struct mmc_card *card,
2860 struct mmc_queue *mq)
2861{
2862 struct mmc_blk_request *brq = &mqrq->brq;
2863 struct request *req = mqrq->req;
2864 struct request *prq;
2865 struct mmc_blk_data *md = mq->data;
2866 struct mmc_packed *packed = mqrq->packed;
2867 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002868 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002869 u8 hdr_blocks;
2870 u8 i = 1;
2871
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002872 mqrq->cmd_type = MMC_PACKED_WRITE;
2873 packed->blocks = 0;
2874 packed->idx_failure = MMC_PACKED_NR_IDX;
2875
2876 packed_cmd_hdr = packed->cmd_hdr;
2877 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002878 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2879 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002880 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2881
2882 /*
2883 * Argument for each entry of packed group
2884 */
2885 list_for_each_entry(prq, &packed->list, queuelist) {
2886 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2887 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2888 (prq->cmd_flags & REQ_META) &&
2889 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002890 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002891 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002892 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002893 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2894 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002895 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002896 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002897 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002898 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002899 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002900 packed->blocks += blk_rq_sectors(prq);
2901 i++;
2902 }
2903
2904 memset(brq, 0, sizeof(struct mmc_blk_request));
2905 brq->mrq.cmd = &brq->cmd;
2906 brq->mrq.data = &brq->data;
2907 brq->mrq.sbc = &brq->sbc;
2908 brq->mrq.stop = &brq->stop;
2909
2910 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2911 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2912 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2913
2914 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2915 brq->cmd.arg = blk_rq_pos(req);
2916 if (!mmc_card_blockaddr(card))
2917 brq->cmd.arg <<= 9;
2918 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2919
2920 brq->data.blksz = 512;
2921 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002922 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302923 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002924
2925 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2926 brq->stop.arg = 0;
2927 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2928
2929 mmc_set_data_timeout(&brq->data, card);
2930
2931 brq->data.sg = mqrq->sg;
2932 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2933
2934 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002935
2936 /*
2937 * This is intended for packed commands tests usage - in case these
2938 * functions are not in use the respective pointers are NULL
2939 */
2940 if (mq->err_check_fn)
2941 mqrq->mmc_active.err_check = mq->err_check_fn;
2942 else
2943 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2944
2945 if (mq->packed_test_fn)
2946 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002947
2948 mmc_queue_bounce_pre(mqrq);
2949}
2950
Adrian Hunter67716322011-08-29 16:42:15 +03002951static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2952 struct mmc_blk_request *brq, struct request *req,
2953 int ret)
2954{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002955 struct mmc_queue_req *mq_rq;
2956 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2957
Adrian Hunter67716322011-08-29 16:42:15 +03002958 /*
2959 * If this is an SD card and we're writing, we can first
2960 * mark the known good sectors as ok.
2961 *
2962 * If the card is not SD, we can still ok written sectors
2963 * as reported by the controller (which might be less than
2964 * the real number of written sectors, but never more).
2965 */
2966 if (mmc_card_sd(card)) {
2967 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302968 if (!brq->data.fault_injected) {
2969 blocks = mmc_sd_num_wr_blocks(card);
2970 if (blocks != (u32)-1)
2971 ret = blk_end_request(req, 0, blocks << 9);
2972 } else
2973 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002974 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002975 if (!mmc_packed_cmd(mq_rq->cmd_type))
2976 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002977 }
2978 return ret;
2979}
2980
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002981static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2982{
2983 struct request *prq;
2984 struct mmc_packed *packed = mq_rq->packed;
2985 int idx = packed->idx_failure, i = 0;
2986 int ret = 0;
2987
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002988 while (!list_empty(&packed->list)) {
2989 prq = list_entry_rq(packed->list.next);
2990 if (idx == i) {
2991 /* retry from error index */
2992 packed->nr_entries -= idx;
2993 mq_rq->req = prq;
2994 ret = 1;
2995
2996 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2997 list_del_init(&prq->queuelist);
2998 mmc_blk_clear_packed(mq_rq);
2999 }
3000 return ret;
3001 }
3002 list_del_init(&prq->queuelist);
3003 blk_end_request(prq, 0, blk_rq_bytes(prq));
3004 i++;
3005 }
3006
3007 mmc_blk_clear_packed(mq_rq);
3008 return ret;
3009}
3010
3011static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3012{
3013 struct request *prq;
3014 struct mmc_packed *packed = mq_rq->packed;
3015
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003016 while (!list_empty(&packed->list)) {
3017 prq = list_entry_rq(packed->list.next);
3018 list_del_init(&prq->queuelist);
3019 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3020 }
3021
3022 mmc_blk_clear_packed(mq_rq);
3023}
3024
3025static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3026 struct mmc_queue_req *mq_rq)
3027{
3028 struct request *prq;
3029 struct request_queue *q = mq->queue;
3030 struct mmc_packed *packed = mq_rq->packed;
3031
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003032 while (!list_empty(&packed->list)) {
3033 prq = list_entry_rq(packed->list.prev);
3034 if (prq->queuelist.prev != &packed->list) {
3035 list_del_init(&prq->queuelist);
3036 spin_lock_irq(q->queue_lock);
3037 blk_requeue_request(mq->queue, prq);
3038 spin_unlock_irq(q->queue_lock);
3039 } else {
3040 list_del_init(&prq->queuelist);
3041 }
3042 }
3043
3044 mmc_blk_clear_packed(mq_rq);
3045}
3046
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003047static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3048 struct mmc_cmdq_req *cmdq_req)
3049{
3050 struct mmc_request *mrq = &cmdq_req->mrq;
3051
3052 mrq->done = mmc_blk_cmdq_req_done;
3053 return mmc_cmdq_start_req(host, cmdq_req);
3054}
3055
Asutosh Das5238e022015-04-23 16:00:45 +05303056/* prepare for non-data commands */
3057static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3058 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3059{
3060 struct request *req = mqrq->req;
3061 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3062
3063 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3064
3065 cmdq_req->mrq.data = NULL;
3066 cmdq_req->cmd_flags = req->cmd_flags;
3067 cmdq_req->mrq.req = mqrq->req;
3068 req->special = mqrq;
3069 cmdq_req->cmdq_req_flags |= DCMD;
3070 cmdq_req->mrq.cmdq_req = cmdq_req;
3071
3072 return &mqrq->cmdq_req;
3073}
3074
3075
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003076#define IS_RT_CLASS_REQ(x) \
3077 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3078
3079static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3080 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3081{
3082 struct mmc_card *card = mq->card;
3083 struct request *req = mqrq->req;
3084 struct mmc_blk_data *md = mq->data;
3085 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3086 bool do_data_tag;
3087 bool read_dir = (rq_data_dir(req) == READ);
3088 bool prio = IS_RT_CLASS_REQ(req);
3089 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3090
3091 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3092
3093 cmdq_rq->tag = req->tag;
3094 if (read_dir) {
3095 cmdq_rq->cmdq_req_flags |= DIR;
3096 cmdq_rq->data.flags = MMC_DATA_READ;
3097 } else {
3098 cmdq_rq->data.flags = MMC_DATA_WRITE;
3099 }
3100 if (prio)
3101 cmdq_rq->cmdq_req_flags |= PRIO;
3102
3103 if (do_rel_wr)
3104 cmdq_rq->cmdq_req_flags |= REL_WR;
3105
3106 cmdq_rq->data.blocks = blk_rq_sectors(req);
3107 cmdq_rq->blk_addr = blk_rq_pos(req);
3108 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3109
3110 mmc_set_data_timeout(&cmdq_rq->data, card);
3111
3112 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3113 (req->cmd_flags & REQ_META) &&
3114 (rq_data_dir(req) == WRITE) &&
3115 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3116 card->ext_csd.data_tag_unit_size);
3117 if (do_data_tag)
3118 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3119 cmdq_rq->data.sg = mqrq->sg;
3120 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3121
3122 /*
3123 * Adjust the sg list so it is the same size as the
3124 * request.
3125 */
3126 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3127 cmdq_rq->data.blocks = card->host->max_blk_count;
3128
3129 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3130 int i, data_size = cmdq_rq->data.blocks << 9;
3131 struct scatterlist *sg;
3132
3133 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3134 data_size -= sg->length;
3135 if (data_size <= 0) {
3136 sg->length += data_size;
3137 i++;
3138 break;
3139 }
3140 }
3141 cmdq_rq->data.sg_len = i;
3142 }
3143
3144 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3145 mqrq->cmdq_req.mrq.req = mqrq->req;
3146 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3147 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3148 mqrq->req->special = mqrq;
3149
3150 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",
3151 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3152 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3153 cmdq_rq, cmdq_rq->blk_addr,
3154 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3155
3156 return &mqrq->cmdq_req;
3157}
3158
3159static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3160{
3161 struct mmc_queue_req *active_mqrq;
3162 struct mmc_card *card = mq->card;
3163 struct mmc_host *host = card->host;
3164 struct mmc_cmdq_req *mc_rq;
3165 int ret = 0;
3166
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003167 mmc_deferred_scaling(host);
3168 mmc_cmdq_clk_scaling_start_busy(host, true);
3169
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003170 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003171 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003172 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3173
3174 active_mqrq = &mq->mqrq_cmdq[req->tag];
3175 active_mqrq->req = req;
3176
3177 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3178
3179 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3180 return ret;
3181}
3182
Asutosh Das5238e022015-04-23 16:00:45 +05303183/*
3184 * Issues a flush (dcmd) request
3185 */
3186int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3187{
3188 int err;
3189 struct mmc_queue_req *active_mqrq;
3190 struct mmc_card *card = mq->card;
3191 struct mmc_host *host;
3192 struct mmc_cmdq_req *cmdq_req;
3193 struct mmc_cmdq_context_info *ctx_info;
3194
3195 BUG_ON(!card);
3196 host = card->host;
3197 BUG_ON(!host);
3198 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3199 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3200
3201 ctx_info = &host->cmdq_ctx;
3202
3203 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3204
3205 active_mqrq = &mq->mqrq_cmdq[req->tag];
3206 active_mqrq->req = req;
3207
3208 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3209 cmdq_req->cmdq_req_flags |= QBR;
3210 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3211 cmdq_req->tag = req->tag;
3212
3213 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3214 if (err) {
3215 pr_err("%s: failed (%d) preparing flush req\n",
3216 mmc_hostname(host), err);
3217 return err;
3218 }
3219 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3220 return err;
3221}
3222EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3223
Asutosh Das02e30862015-05-20 16:52:04 +05303224static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3225{
3226 if (!host->cmdq_ops->reset)
3227 return;
3228
3229 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3230 if (mmc_cmdq_halt(host, true)) {
3231 pr_err("%s: halt failed\n", mmc_hostname(host));
3232 goto reset;
3233 }
3234 }
3235
3236 if (clear_all)
3237 mmc_cmdq_discard_queue(host, 0);
3238reset:
3239 mmc_hw_reset(host);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303240 mmc_host_clk_hold(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303241 host->cmdq_ops->reset(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303242 mmc_host_clk_release(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303243 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3244}
3245
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303246static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3247{
3248 int err;
3249 struct mmc_card *card = mq->card;
3250 struct mmc_host *host = card->host;
3251
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003252 mmc_get_card(card);
3253 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303254 err = mmc_cmdq_halt(host, true);
3255 if (err) {
3256 pr_err("%s: halt: failed: %d\n", __func__, err);
3257 return;
3258 }
3259
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303260 /* disable CQ mode in card */
3261 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3262 EXT_CSD_CMDQ, 0,
3263 card->ext_csd.generic_cmd6_time);
3264 if (err) {
3265 pr_err("%s: failed to switch card to legacy mode: %d\n",
3266 __func__, err);
3267 goto out;
3268 } else {
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003269 mmc_card_clr_cmdq(card);
3270 host->cmdq_ops->disable(host, false);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303271 host->card->cmdq_init = false;
3272 }
3273out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003274 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003275 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303276}
3277
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303278static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3279{
3280 struct mmc_queue *mq = req->q->queuedata;
3281 struct mmc_host *host = mq->card->host;
3282 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003283 struct mmc_request *mrq;
3284 struct mmc_cmdq_req *cmdq_req;
3285
3286 BUG_ON(!host);
3287
3288 /*
3289 * The mmc_queue_req will be present only if the request
3290 * is issued to the LLD. The request could be fetched from
3291 * block layer queue but could be waiting to be issued
3292 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3293 * Reset the timer in such cases to give LLD more time
3294 */
3295 if (!mq_rq) {
3296 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3297 return BLK_EH_RESET_TIMER;
3298 }
3299
3300 mrq = &mq_rq->cmdq_req.mrq;
3301 cmdq_req = &mq_rq->cmdq_req;
3302
3303 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303304
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303305 if (cmdq_req->cmdq_req_flags & DCMD)
3306 mrq->cmd->error = -ETIMEDOUT;
3307 else
3308 mrq->data->error = -ETIMEDOUT;
3309
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003310 BUG_ON(host->err_mrq != NULL);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303311 host->err_mrq = mrq;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303312
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003313 mmc_host_clk_release(mrq->host);
3314 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303315}
3316
Asutosh Das02e30862015-05-20 16:52:04 +05303317static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3318{
3319 int err;
3320 int retry = 0;
3321 int gen_err;
3322 u32 status;
3323
3324 struct mmc_host *host = mq->card->host;
3325 struct mmc_request *mrq = host->err_mrq;
3326 struct mmc_card *card = mq->card;
3327 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3328
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003329 pm_runtime_get_sync(&card->dev);
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003330 mmc_host_clk_hold(host);
3331 host->cmdq_ops->dumpstate(host);
3332 mmc_host_clk_release(host);
3333
Asutosh Das02e30862015-05-20 16:52:04 +05303334 err = mmc_cmdq_halt(host, true);
3335 if (err) {
3336 pr_err("halt: failed: %d\n", err);
3337 goto reset;
3338 }
3339
3340 /* RED error - Fatal: requires reset */
3341 if (mrq->cmdq_req->resp_err) {
3342 pr_crit("%s: Response error detected: Device in bad state\n",
3343 mmc_hostname(host));
3344 blk_end_request_all(mrq->req, -EIO);
3345 goto reset;
3346 }
3347
3348 if (mrq->data->error) {
3349 blk_end_request_all(mrq->req, mrq->data->error);
3350 for (; retry < MAX_RETRIES; retry++) {
3351 err = get_card_status(card, &status, 0);
3352 if (!err)
3353 break;
3354 }
3355
3356 if (err) {
3357 pr_err("%s: No response from card !!!\n",
3358 mmc_hostname(host));
3359 goto reset;
3360 }
3361
3362 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3363 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3364 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3365 mrq->req, &gen_err, &status);
3366 if (err) {
3367 pr_err("%s: error %d sending stop (%d) command\n",
3368 mrq->req->rq_disk->disk_name,
3369 err, status);
3370 goto reset;
3371 }
3372 }
3373
3374 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3375 goto reset;
3376 else
3377 goto unhalt;
3378 }
3379
3380 /* DCMD commands */
3381 if (mrq->cmd->error)
3382 blk_end_request_all(mrq->req, mrq->cmd->error);
3383
3384reset:
3385 spin_lock_irq(mq->queue->queue_lock);
3386 blk_queue_invalidate_tags(mrq->req->q);
3387 spin_unlock_irq(mq->queue->queue_lock);
3388 mmc_blk_cmdq_reset(host, true);
3389 goto out;
3390
3391unhalt:
3392 mmc_cmdq_halt(host, false);
3393
3394out:
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003395 host->err_mrq = NULL;
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003396 pm_runtime_mark_last_busy(&card->dev);
Dov Levenglick2b678302015-07-01 14:24:20 +03003397 __mmc_put_card(card);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003398
Asutosh Das02e30862015-05-20 16:52:04 +05303399 if (test_and_clear_bit(0, &ctx_info->req_starved))
3400 blk_run_queue(mrq->req->q);
3401}
3402
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003403/* invoked by block layer in softirq context */
3404void mmc_blk_cmdq_complete_rq(struct request *rq)
3405{
3406 struct mmc_queue_req *mq_rq = rq->special;
3407 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3408 struct mmc_host *host = mrq->host;
3409 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3410 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3411 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3412 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003413 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003414
3415 if (mrq->cmd && mrq->cmd->error)
3416 err = mrq->cmd->error;
3417 else if (mrq->data && mrq->data->error)
3418 err = mrq->data->error;
3419
Asutosh Das02e30862015-05-20 16:52:04 +05303420 /* clear pending request */
3421 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3422 &ctx_info->active_reqs));
3423
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003424 if (cmdq_req->cmdq_req_flags & DCMD)
3425 is_dcmd = true;
3426 else
3427 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3428 &ctx_info->data_active_reqs));
Talel Shenhar339a4e12015-06-29 10:50:19 +03003429
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003430 mmc_cmdq_post_req(host, mrq, err);
3431 if (err) {
3432 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3433 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303434 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3435 pr_err("%s: CQ in error state, ending current req: %d\n",
3436 __func__, err);
3437 blk_end_request_all(rq, err);
3438 } else {
3439 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3440 schedule_work(&mq->cmdq_err_work);
3441 }
3442 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003443 }
3444
Asutosh Das5238e022015-04-23 16:00:45 +05303445 if (cmdq_req->cmdq_req_flags & DCMD) {
3446 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303447 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303448 goto out;
3449 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003450
3451 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3452
Asutosh Das5238e022015-04-23 16:00:45 +05303453out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003454
3455 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Asutosh Das02e30862015-05-20 16:52:04 +05303456 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
3457 test_and_clear_bit(0, &ctx_info->req_starved))
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003458 blk_run_queue(mq->queue);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303459
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003460 mmc_put_card(host->card);
Talel Shenhar339a4e12015-06-29 10:50:19 +03003461 if (!ctx_info->active_reqs)
3462 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3463
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303464 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3465 complete(&mq->cmdq_shutdown_complete);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003466 return;
3467}
3468
3469/*
3470 * Complete reqs from block layer softirq context
3471 * Invoked in irq context
3472 */
3473void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3474{
3475 struct request *req = mrq->req;
3476
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303477 mmc_host_clk_release(mrq->host);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003478 blk_complete_request(req);
3479}
3480EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3481
Per Forlinee8a43a2011-07-01 18:55:33 +02003482static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003483{
3484 struct mmc_blk_data *md = mq->data;
3485 struct mmc_card *card = md->queue.card;
3486 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003487 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003488 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003489 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303490 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003491 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003492 const u8 packed_nr = 2;
3493 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003494#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3495 unsigned long waitfor = jiffies;
3496#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003497
3498 if (!rqc && !mq->mqrq_prev->req)
3499 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003500
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003501 if (rqc)
3502 reqs = mmc_blk_prep_packed_list(mq, rqc);
3503
Per Forlin54d49d72011-07-01 18:55:29 +02003504 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003505 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303506 /*
3507 * When 4KB native sector is enabled, only 8 blocks
3508 * multiple read or write is allowed
3509 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003510 if (mmc_large_sector(card) &&
3511 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303512 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3513 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003514 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303515 goto cmd_abort;
3516 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003517
3518 if (reqs >= packed_nr)
3519 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3520 card, mq);
3521 else
3522 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003523 areq = &mq->mqrq_cur->mmc_active;
3524 } else
3525 areq = NULL;
3526 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003527 if (!areq) {
3528 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003529 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003530 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003531 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003532
Per Forlinee8a43a2011-07-01 18:55:33 +02003533 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3534 brq = &mq_rq->brq;
3535 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003536 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003537 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003538
Per Forlind78d4a82011-07-01 18:55:30 +02003539 switch (status) {
3540 case MMC_BLK_SUCCESS:
3541 case MMC_BLK_PARTIAL:
3542 /*
3543 * A block was successfully transferred.
3544 */
Adrian Hunter67716322011-08-29 16:42:15 +03003545 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003546
Mark Salyzyn6904e432016-01-28 11:12:25 -08003547 mmc_blk_simulate_delay(mq, rqc, waitfor);
3548
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003549 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3550 ret = mmc_blk_end_packed_req(mq_rq);
3551 break;
3552 } else {
3553 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003554 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003555 }
3556
Adrian Hunter67716322011-08-29 16:42:15 +03003557 /*
3558 * If the blk_end_request function returns non-zero even
3559 * though all data has been transferred and no errors
3560 * were returned by the host controller, it's a bug.
3561 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003562 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303563 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003564 __func__, blk_rq_bytes(req),
3565 brq->data.bytes_xfered);
3566 rqc = NULL;
3567 goto cmd_abort;
3568 }
Per Forlind78d4a82011-07-01 18:55:30 +02003569 break;
3570 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003571 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003572 if (mmc_blk_reset(md, card->host, type))
3573 goto cmd_abort;
3574 if (!ret)
3575 goto start_new_req;
3576 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003577 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003578 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003579 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003580 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003581 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003582 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003583 if (!mmc_blk_reset(md, card->host, type) &&
3584 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003585 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003586 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003587 case MMC_BLK_DATA_ERR: {
3588 int err;
3589
3590 err = mmc_blk_reset(md, card->host, type);
3591 if (!err)
3592 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303593 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003594 }
3595 case MMC_BLK_ECC_ERR:
3596 if (brq->data.blocks > 1) {
3597 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003598 pr_warn("%s: retrying using single block read\n",
3599 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003600 disable_multi = 1;
3601 break;
3602 }
Per Forlind78d4a82011-07-01 18:55:30 +02003603 /*
3604 * After an error, we redo I/O one sector at a
3605 * time, so we only reach here after trying to
3606 * read a single sector.
3607 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303608 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003609 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003610 if (!ret)
3611 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003612 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303613 case MMC_BLK_NOMEDIUM:
3614 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003615 default:
3616 pr_err("%s: Unhandled return value (%d)",
3617 req->rq_disk->disk_name, status);
3618 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003619 }
3620
Per Forlinee8a43a2011-07-01 18:55:33 +02003621 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003622 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3623 if (!mq_rq->packed->retries)
3624 goto cmd_abort;
3625 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3626 mmc_start_req(card->host,
3627 &mq_rq->mmc_active, NULL);
3628 } else {
3629
3630 /*
3631 * In case of a incomplete request
3632 * prepare it again and resend.
3633 */
3634 mmc_blk_rw_rq_prep(mq_rq, card,
3635 disable_multi, mq);
3636 mmc_start_req(card->host,
3637 &mq_rq->mmc_active, NULL);
3638 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003639 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 } while (ret);
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return 1;
3644
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003645 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003646 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3647 mmc_blk_abort_packed_req(mq_rq);
3648 } else {
3649 if (mmc_card_removed(card))
3650 req->cmd_flags |= REQ_QUIET;
3651 while (ret)
3652 ret = blk_end_request(req, -EIO,
3653 blk_rq_cur_bytes(req));
3654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Per Forlinee8a43a2011-07-01 18:55:33 +02003656 start_new_req:
3657 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003658 if (mmc_card_removed(card)) {
3659 rqc->cmd_flags |= REQ_QUIET;
3660 blk_end_request_all(rqc, -EIO);
3661 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003662 /*
3663 * If current request is packed, it needs to put back.
3664 */
3665 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3666 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3667
Seungwon Jeon7a819022013-01-22 19:48:07 +09003668 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3669 mmc_start_req(card->host,
3670 &mq->mqrq_cur->mmc_active, NULL);
3671 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003672 }
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return 0;
3675}
3676
Asutosh Das8b594832015-04-23 09:55:43 +05303677static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3678 struct mmc_blk_data *md)
3679{
3680 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3681 struct mmc_host *host = card->host;
3682 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3683 u8 part_config = card->ext_csd.part_config;
3684
3685 if ((main_md->part_curr == md->part_type) &&
3686 (card->part_curr == md->part_type))
3687 return 0;
3688
3689 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3690 card->ext_csd.cmdq_support &&
3691 (md->flags & MMC_BLK_CMD_QUEUE)));
3692
3693 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3694 WARN_ON(mmc_cmdq_halt(host, true));
3695
3696 /* disable CQ mode in card */
3697 if (mmc_card_cmdq(card)) {
3698 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3699 EXT_CSD_CMDQ, 0,
3700 card->ext_csd.generic_cmd6_time));
3701 mmc_card_clr_cmdq(card);
3702 }
3703
3704 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3705 part_config |= md->part_type;
3706
3707 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3708 EXT_CSD_PART_CONFIG, part_config,
3709 card->ext_csd.part_time));
3710
3711 card->ext_csd.part_config = part_config;
3712 card->part_curr = md->part_type;
3713
3714 main_md->part_curr = md->part_type;
3715
3716 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3717 WARN_ON(mmc_cmdq_halt(host, false));
3718
3719 return 0;
3720}
3721
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003722static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3723{
3724 int ret;
3725 struct mmc_blk_data *md = mq->data;
3726 struct mmc_card *card = md->queue.card;
3727
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003728 mmc_get_card(card);
Asutosh Das8b594832015-04-23 09:55:43 +05303729 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003730 if (ret) {
3731 pr_err("%s: %s: partition switch failed %d\n",
3732 md->disk->disk_name, __func__, ret);
Asutosh Das5238e022015-04-23 16:00:45 +05303733 if (req)
3734 blk_end_request_all(req, ret);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003735 mmc_put_card(card);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003736 goto switch_failure;
3737 }
3738
Asutosh Das5238e022015-04-23 16:00:45 +05303739 if (req) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05303740 if (req_op(req) == REQ_OP_DISCARD) {
3741 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3742 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3743 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3744 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3745 else
3746 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3747 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303748 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303749 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303750 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303751 }
Asutosh Das5238e022015-04-23 16:00:45 +05303752 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003753
3754switch_failure:
3755 return ret;
3756}
3757
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003758int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003759{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003760 int ret;
3761 struct mmc_blk_data *md = mq->data;
3762 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003763 struct mmc_host *host = card->host;
3764 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303765 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003766 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003767
Dov Levenglicka0296392015-06-24 19:51:58 +03003768 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003769 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003770 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003771
Dov Levenglicka0296392015-06-24 19:51:58 +03003772 if (mmc_card_doing_bkops(host->card)) {
3773 ret = mmc_stop_bkops(host->card);
3774 if (ret)
3775 goto out;
3776 }
3777 }
3778
Andrei Warkentin371a6892011-04-11 18:10:25 -05003779 ret = mmc_blk_part_switch(card, md);
3780 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003781 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303782 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003783 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003784 ret = 0;
3785 goto out;
3786 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003787
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003788 mmc_blk_write_packing_control(mq, req);
3789
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003790 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003791 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003792 /* complete ongoing async transfer before issuing discard */
3793 if (card->host->areq)
3794 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003795 ret = mmc_blk_issue_discard_rq(mq, req);
3796 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3797 /* complete ongoing async transfer before issuing secure erase*/
3798 if (card->host->areq)
3799 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003800 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3801 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3802 else
3803 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303804 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3805 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003806 /* complete ongoing async transfer before issuing flush */
3807 if (card->host->areq)
3808 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003809 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003810 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003811 if (!req && host->areq) {
3812 spin_lock_irqsave(&host->context_info.lock, flags);
3813 host->context_info.is_waiting_last_req = true;
3814 spin_unlock_irqrestore(&host->context_info.lock, flags);
3815 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003816 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003817 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003818
Andrei Warkentin371a6892011-04-11 18:10:25 -05003819out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003820 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3821 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003822 /*
3823 * Release host when there are no more requests
3824 * and after special request(discard, flush) is done.
3825 * In case sepecial request, there is no reentry to
3826 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3827 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003828 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003829 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Russell Kinga6f6c962006-01-03 22:38:44 +00003832static inline int mmc_blk_readonly(struct mmc_card *card)
3833{
3834 return mmc_card_readonly(card) ||
3835 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3836}
3837
Andrei Warkentin371a6892011-04-11 18:10:25 -05003838static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3839 struct device *parent,
3840 sector_t size,
3841 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003842 const char *subname,
3843 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
3845 struct mmc_blk_data *md;
3846 int devidx, ret;
3847
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003848again:
3849 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3850 return ERR_PTR(-ENOMEM);
3851
3852 spin_lock(&mmc_blk_lock);
3853 ret = ida_get_new(&mmc_blk_ida, &devidx);
3854 spin_unlock(&mmc_blk_lock);
3855
3856 if (ret == -EAGAIN)
3857 goto again;
3858 else if (ret)
3859 return ERR_PTR(ret);
3860
3861 if (devidx >= max_devices) {
3862 ret = -ENOSPC;
3863 goto out;
3864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003866 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003867 if (!md) {
3868 ret = -ENOMEM;
3869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003871
Johan Rudholmadd710e2011-12-02 08:51:06 +01003872 md->area_type = area_type;
3873
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003874 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003875 * Set the read-only status based on the supported commands
3876 * and the write protect switch.
3877 */
3878 md->read_only = mmc_blk_readonly(card);
3879
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003880 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003881 if (md->disk == NULL) {
3882 ret = -ENOMEM;
3883 goto err_kfree;
3884 }
3885
3886 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003887 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003888 md->usage = 1;
3889
Asutosh Das963469b2015-05-21 13:29:51 +05303890 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003891 if (ret)
3892 goto err_putdisk;
3893
Russell Kinga6f6c962006-01-03 22:38:44 +00003894 md->queue.data = md;
3895
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003896 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003897 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003898 md->disk->fops = &mmc_bdops;
3899 md->disk->private_data = md;
3900 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003901 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003902 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003903 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003904 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003905 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003906
3907 /*
3908 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3909 *
3910 * - be set for removable media with permanent block devices
3911 * - be unset for removable block devices with permanent media
3912 *
3913 * Since MMC block devices clearly fall under the second
3914 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3915 * should use the block device creation/destruction hotplug
3916 * messages to tell when the card is present.
3917 */
3918
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003919 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003920 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003921
Saugata Dasa5075eb2012-05-17 16:32:21 +05303922 if (mmc_card_mmc(card))
3923 blk_queue_logical_block_size(md->queue.queue,
3924 card->ext_csd.data_sector_size);
3925 else
3926 blk_queue_logical_block_size(md->queue.queue, 512);
3927
Andrei Warkentin371a6892011-04-11 18:10:25 -05003928 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003929
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003930 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003931 if ((mmc_card_mmc(card) &&
3932 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003933 (mmc_card_sd(card) &&
3934 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3935 md->flags |= MMC_BLK_CMD23;
3936 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003937
3938 if (mmc_card_mmc(card) &&
3939 md->flags & MMC_BLK_CMD23 &&
3940 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303941 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003942 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003943 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003944 }
3945
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003946 if (card->cmdq_init) {
3947 md->flags |= MMC_BLK_CMD_QUEUE;
3948 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
3949 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05303950 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303951 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303952 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003953 }
3954
3955 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003956 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3957 (md->flags & MMC_BLK_CMD23) &&
3958 card->ext_csd.packed_event_en) {
3959 if (!mmc_packed_init(&md->queue, card))
3960 md->flags |= MMC_BLK_PACKED_CMD;
3961 }
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003964
3965 err_putdisk:
3966 put_disk(md->disk);
3967 err_kfree:
3968 kfree(md);
3969 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003970 spin_lock(&mmc_blk_lock);
3971 ida_remove(&mmc_blk_ida, devidx);
3972 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003973 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
Andrei Warkentin371a6892011-04-11 18:10:25 -05003976static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3977{
3978 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003979
3980 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3981 /*
3982 * The EXT_CSD sector count is in number or 512 byte
3983 * sectors.
3984 */
3985 size = card->ext_csd.sectors;
3986 } else {
3987 /*
3988 * The CSD capacity field is in units of read_blkbits.
3989 * set_capacity takes units of 512 bytes.
3990 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003991 size = (typeof(sector_t))card->csd.capacity
3992 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003993 }
3994
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003995 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003996 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003997}
3998
3999static int mmc_blk_alloc_part(struct mmc_card *card,
4000 struct mmc_blk_data *md,
4001 unsigned int part_type,
4002 sector_t size,
4003 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004004 const char *subname,
4005 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004006{
4007 char cap_str[10];
4008 struct mmc_blk_data *part_md;
4009
4010 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004011 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004012 if (IS_ERR(part_md))
4013 return PTR_ERR(part_md);
4014 part_md->part_type = part_type;
4015 list_add(&part_md->part, &md->part);
4016
James Bottomleyb9f28d82015-03-05 18:47:01 -08004017 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004018 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304019 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004020 part_md->disk->disk_name, mmc_card_id(card),
4021 mmc_card_name(card), part_md->part_type, cap_str);
4022 return 0;
4023}
4024
Namjae Jeone0c368d2011-10-06 23:41:38 +09004025/* MMC Physical partitions consist of two boot partitions and
4026 * up to four general purpose partitions.
4027 * For each partition enabled in EXT_CSD a block device will be allocatedi
4028 * to provide access to the partition.
4029 */
4030
Andrei Warkentin371a6892011-04-11 18:10:25 -05004031static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4032{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004033 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004034
4035 if (!mmc_card_mmc(card))
4036 return 0;
4037
Namjae Jeone0c368d2011-10-06 23:41:38 +09004038 for (idx = 0; idx < card->nr_parts; idx++) {
4039 if (card->part[idx].size) {
4040 ret = mmc_blk_alloc_part(card, md,
4041 card->part[idx].part_cfg,
4042 card->part[idx].size >> 9,
4043 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004044 card->part[idx].name,
4045 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004046 if (ret)
4047 return ret;
4048 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004049 }
4050
4051 return ret;
4052}
4053
Andrei Warkentin371a6892011-04-11 18:10:25 -05004054static void mmc_blk_remove_req(struct mmc_blk_data *md)
4055{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004056 struct mmc_card *card;
4057
Andrei Warkentin371a6892011-04-11 18:10:25 -05004058 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004059 /*
4060 * Flush remaining requests and free queues. It
4061 * is freeing the queue that stops new requests
4062 * from being accepted.
4063 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004064 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004065 mmc_cleanup_queue(&md->queue);
4066 if (md->flags & MMC_BLK_PACKED_CMD)
4067 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004068 if (md->flags & MMC_BLK_CMD_QUEUE)
4069 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004070 device_remove_file(disk_to_dev(md->disk),
4071 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004072 if (md->disk->flags & GENHD_FL_UP) {
4073 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004074 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4075 card->ext_csd.boot_ro_lockable)
4076 device_remove_file(disk_to_dev(md->disk),
4077 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004078#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4079 device_remove_file(disk_to_dev(md->disk),
4080 &dev_attr_max_write_speed);
4081 device_remove_file(disk_to_dev(md->disk),
4082 &dev_attr_max_read_speed);
4083 device_remove_file(disk_to_dev(md->disk),
4084 &dev_attr_cache_size);
4085#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004086
Andrei Warkentin371a6892011-04-11 18:10:25 -05004087 del_gendisk(md->disk);
4088 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004089 mmc_blk_put(md);
4090 }
4091}
4092
4093static void mmc_blk_remove_parts(struct mmc_card *card,
4094 struct mmc_blk_data *md)
4095{
4096 struct list_head *pos, *q;
4097 struct mmc_blk_data *part_md;
4098
4099 list_for_each_safe(pos, q, &md->part) {
4100 part_md = list_entry(pos, struct mmc_blk_data, part);
4101 list_del(pos);
4102 mmc_blk_remove_req(part_md);
4103 }
4104}
4105
4106static int mmc_add_disk(struct mmc_blk_data *md)
4107{
4108 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004109 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004110
Dan Williams307d8e62016-06-20 10:40:44 -07004111 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004112 md->force_ro.show = force_ro_show;
4113 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304114 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004115 md->force_ro.attr.name = "force_ro";
4116 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4117 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4118 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004119 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004120#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4121 atomic_set(&md->queue.max_write_speed, max_write_speed);
4122 ret = device_create_file(disk_to_dev(md->disk),
4123 &dev_attr_max_write_speed);
4124 if (ret)
4125 goto max_write_speed_fail;
4126 atomic_set(&md->queue.max_read_speed, max_read_speed);
4127 ret = device_create_file(disk_to_dev(md->disk),
4128 &dev_attr_max_read_speed);
4129 if (ret)
4130 goto max_read_speed_fail;
4131 atomic_set(&md->queue.cache_size, cache_size);
4132 atomic_long_set(&md->queue.cache_used, 0);
4133 md->queue.cache_jiffies = jiffies;
4134 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4135 if (ret)
4136 goto cache_size_fail;
4137#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004138
4139 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4140 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004141 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004142
4143 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4144 mode = S_IRUGO;
4145 else
4146 mode = S_IRUGO | S_IWUSR;
4147
4148 md->power_ro_lock.show = power_ro_lock_show;
4149 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004150 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004151 md->power_ro_lock.attr.mode = mode;
4152 md->power_ro_lock.attr.name =
4153 "ro_lock_until_next_power_on";
4154 ret = device_create_file(disk_to_dev(md->disk),
4155 &md->power_ro_lock);
4156 if (ret)
4157 goto power_ro_lock_fail;
4158 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004159
4160 md->num_wr_reqs_to_start_packing.show =
4161 num_wr_reqs_to_start_packing_show;
4162 md->num_wr_reqs_to_start_packing.store =
4163 num_wr_reqs_to_start_packing_store;
4164 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4165 md->num_wr_reqs_to_start_packing.attr.name =
4166 "num_wr_reqs_to_start_packing";
4167 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4168 ret = device_create_file(disk_to_dev(md->disk),
4169 &md->num_wr_reqs_to_start_packing);
4170 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004171 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004172
Maya Erez5a8dae12014-12-04 15:13:59 +02004173 md->no_pack_for_random.show = no_pack_for_random_show;
4174 md->no_pack_for_random.store = no_pack_for_random_store;
4175 sysfs_attr_init(&md->no_pack_for_random.attr);
4176 md->no_pack_for_random.attr.name = "no_pack_for_random";
4177 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4178 ret = device_create_file(disk_to_dev(md->disk),
4179 &md->no_pack_for_random);
4180 if (ret)
4181 goto no_pack_for_random_fails;
4182
Johan Rudholmadd710e2011-12-02 08:51:06 +01004183 return ret;
4184
Maya Erez5a8dae12014-12-04 15:13:59 +02004185no_pack_for_random_fails:
4186 device_remove_file(disk_to_dev(md->disk),
4187 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004188num_wr_reqs_to_start_packing_fail:
4189 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004190power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004191#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4192 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4193cache_size_fail:
4194 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4195max_read_speed_fail:
4196 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4197max_write_speed_fail:
4198#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004199 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4200force_ro_fail:
4201 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004202
4203 return ret;
4204}
4205
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004206static const struct mmc_fixup blk_fixups[] =
4207{
Chris Ballc59d4472011-11-11 22:01:43 -05004208 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4209 MMC_QUIRK_INAND_CMD38),
4210 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4211 MMC_QUIRK_INAND_CMD38),
4212 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4213 MMC_QUIRK_INAND_CMD38),
4214 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4215 MMC_QUIRK_INAND_CMD38),
4216 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4217 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004218
4219 /*
4220 * Some MMC cards experience performance degradation with CMD23
4221 * instead of CMD12-bounded multiblock transfers. For now we'll
4222 * black list what's bad...
4223 * - Certain Toshiba cards.
4224 *
4225 * N.B. This doesn't affect SD cards.
4226 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004227 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4228 MMC_QUIRK_BLK_NO_CMD23),
4229 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4230 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004231 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004232 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004233 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004234 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004235 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004236 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004237
4238 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004239 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004240 */
Chris Ballc59d4472011-11-11 22:01:43 -05004241 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004242 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004243 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4244 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004245
Ian Chen3550ccd2012-08-29 15:05:36 +09004246 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004247 * Some Samsung MMC cards need longer data read timeout than
4248 * indicated in CSD.
4249 */
4250 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4251 MMC_QUIRK_LONG_READ_TIME),
4252
4253 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004254 * On these Samsung MoviNAND parts, performing secure erase or
4255 * secure trim can result in unrecoverable corruption due to a
4256 * firmware bug.
4257 */
4258 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4259 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4260 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4261 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4262 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4263 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4264 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4265 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4266 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4267 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4268 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4269 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4270 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4271 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4272 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4273 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4274
Shawn Linb5b4ff02015-08-12 13:08:32 +08004275 /*
4276 * On Some Kingston eMMCs, performing trim can result in
4277 * unrecoverable data conrruption occasionally due to a firmware bug.
4278 */
4279 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4280 MMC_QUIRK_TRIM_BROKEN),
4281 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4282 MMC_QUIRK_TRIM_BROKEN),
4283
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004284 /* Some INAND MCP devices advertise incorrect timeout values */
4285 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4286 MMC_QUIRK_INAND_DATA_TIMEOUT),
4287
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004288 END_FIXUP
4289};
4290
Ulf Hansson96541ba2015-04-14 13:06:12 +02004291static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004293 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004294 char cap_str[10];
4295
Pierre Ossman912490d2005-05-21 10:27:02 +01004296 /*
4297 * Check that the card supports the command class(es) we need.
4298 */
4299 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return -ENODEV;
4301
Lukas Czerner5204d002014-06-18 13:18:07 +02004302 mmc_fixup_device(card, blk_fixups);
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 md = mmc_blk_alloc(card);
4305 if (IS_ERR(md))
4306 return PTR_ERR(md);
4307
James Bottomleyb9f28d82015-03-05 18:47:01 -08004308 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004309 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304310 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004312 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Andrei Warkentin371a6892011-04-11 18:10:25 -05004314 if (mmc_blk_alloc_parts(card, md))
4315 goto out;
4316
Ulf Hansson96541ba2015-04-14 13:06:12 +02004317 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004318
Andrei Warkentin371a6892011-04-11 18:10:25 -05004319 if (mmc_add_disk(md))
4320 goto out;
4321
4322 list_for_each_entry(part_md, &md->part, part) {
4323 if (mmc_add_disk(part_md))
4324 goto out;
4325 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004326
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004327 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004328 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4329 /*
4330 * If there is a runtime_idle function, it should take care of
4331 * suspending the card
4332 */
4333 if (card->host->bus_ops->runtime_idle)
4334 pm_runtime_dont_use_autosuspend(&card->dev);
4335 else
4336 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004337
4338 /*
4339 * Don't enable runtime PM for SD-combo cards here. Leave that
4340 * decision to be taken during the SDIO init sequence instead.
4341 */
4342 if (card->type != MMC_TYPE_SD_COMBO) {
4343 pm_runtime_set_active(&card->dev);
4344 pm_runtime_enable(&card->dev);
4345 }
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348
4349 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004350 mmc_blk_remove_parts(card, md);
4351 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353}
4354
Ulf Hansson96541ba2015-04-14 13:06:12 +02004355static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004357 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Andrei Warkentin371a6892011-04-11 18:10:25 -05004359 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004360 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004361 mmc_claim_host(card->host);
4362 mmc_blk_part_switch(card, md);
4363 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004364 if (card->type != MMC_TYPE_SD_COMBO)
4365 pm_runtime_disable(&card->dev);
4366 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004367 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004368 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004371static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004373 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004374 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304375 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004378 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304379 if (rc)
4380 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004381 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004382 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304383 if (rc)
4384 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304387 goto out;
4388
4389 out_resume:
4390 mmc_queue_resume(&md->queue);
4391 list_for_each_entry(part_md, &md->part, part) {
4392 mmc_queue_resume(&part_md->queue);
4393 }
4394 out:
4395 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
Ulf Hansson96541ba2015-04-14 13:06:12 +02004398static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004399{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004400 _mmc_blk_suspend(card, 1);
Krishna Kondaa7ea2dc2015-06-29 19:20:05 -07004401
4402 /* send power off notification */
4403 if (mmc_card_mmc(card))
4404 mmc_send_pon(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004405}
4406
Ulf Hansson0967edc2014-10-06 11:29:42 +02004407#ifdef CONFIG_PM_SLEEP
4408static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004409{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004410 struct mmc_card *card = mmc_dev_to_card(dev);
4411
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004412 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004413}
4414
Ulf Hansson0967edc2014-10-06 11:29:42 +02004415static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004417 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004418 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004421 /*
4422 * Resume involves the card going into idle state,
4423 * so current partition is always the main one.
4424 */
4425 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004427 list_for_each_entry(part_md, &md->part, part) {
4428 mmc_queue_resume(&part_md->queue);
4429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431 return 0;
4432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433#endif
4434
Ulf Hansson0967edc2014-10-06 11:29:42 +02004435static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4436
Ulf Hansson96541ba2015-04-14 13:06:12 +02004437static struct mmc_driver mmc_driver = {
4438 .drv = {
4439 .name = "mmcblk",
4440 .pm = &mmc_blk_pm_ops,
4441 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 .probe = mmc_blk_probe,
4443 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004444 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445};
4446
4447static int __init mmc_blk_init(void)
4448{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004449 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004451 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4452 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4453
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004454 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004455
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004456 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4457 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004460 res = mmc_register_driver(&mmc_driver);
4461 if (res)
4462 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004464 return 0;
4465 out2:
4466 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 out:
4468 return res;
4469}
4470
4471static void __exit mmc_blk_exit(void)
4472{
4473 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004474 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
4477module_init(mmc_blk_init);
4478module_exit(mmc_blk_exit);
4479
4480MODULE_LICENSE("GPL");
4481MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4482