blob: 2e75e18f7b5c8d95a298f220fb1f7c178868d95e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Dov Levenglicka0296392015-06-24 19:51:58 +030044#include <linux/mmc/core.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020045#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046#include <linux/mmc/mmc.h>
47#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50
Pierre Ossman98ac2162006-12-23 20:03:02 +010051#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080052#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020066#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053069#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050070
Luca Porziod3df0462015-11-06 15:12:26 +000071#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072 (rq_data_dir(req) == WRITE))
73#define PACKED_CMD_VER 0x01
74#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030075#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090076
Maya Erezf93ca0a2014-12-09 23:34:41 +020077#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020078#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
79 do { \
80 if (stats->enabled) \
81 stats->pack_stop_reason[reason]++; \
82 } while (0)
83
Asutosh Das02e30862015-05-20 16:52:04 +053084#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030085#define PCKD_TRGR_INIT_MEAN_POTEN 17
86#define PCKD_TRGR_POTEN_LOWER_BOUND 5
87#define PCKD_TRGR_URGENT_PENALTY 2
88#define PCKD_TRGR_LOWER_BOUND 5
89#define PCKD_TRGR_PRECISION_MULTIPLIER 100
90
Sahitya Tummala9433a132015-06-09 09:38:36 +053091static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
92 struct mmc_queue_req *mqrq, struct mmc_queue *mq);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020093static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040094
95/*
96 * The defaults come from config options but can be overriden by module
97 * or bootarg options.
98 */
99static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
100
101/*
102 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000103 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105 */
106static int max_devices;
107
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000108#define MAX_DEVICES 256
109
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200110static DEFINE_IDA(mmc_blk_ida);
111static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * There is one mmc_blk_data per slot.
115 */
116struct mmc_blk_data {
117 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700118 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct gendisk *disk;
120 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500123 unsigned int flags;
124#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
125#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700127#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000130 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300132 unsigned int reset_done;
133#define MMC_BLK_READ BIT(0)
134#define MMC_BLK_WRITE BIT(1)
135#define MMC_BLK_DISCARD BIT(2)
136#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200137#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138
139 /*
140 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200141 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 * track of the current selected device partition.
143 */
144 unsigned int part_curr;
145 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200147 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200148 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100149 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Arjan van de Vena621aae2006-01-12 18:43:35 +0000152static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154enum {
155 MMC_PACKED_NR_IDX = -1,
156 MMC_PACKED_NR_ZERO,
157 MMC_PACKED_NR_SINGLE,
158};
159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
164 struct mmc_blk_data *md);
165static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700166static int mmc_blk_cmdq_switch(struct mmc_card *card,
167 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200168
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900169static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
170{
171 struct mmc_packed *packed = mqrq->packed;
172
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900173 mqrq->cmd_type = MMC_PACKED_NONE;
174 packed->nr_entries = MMC_PACKED_NR_ZERO;
175 packed->idx_failure = MMC_PACKED_NR_IDX;
176 packed->retries = 0;
177 packed->blocks = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
181{
182 struct mmc_blk_data *md;
183
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md = disk->private_data;
186 if (md && md->usage == 0)
187 md = NULL;
188 if (md)
189 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return md;
193}
194
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195static inline int mmc_get_devidx(struct gendisk *disk)
196{
Colin Cross382c55f2015-10-22 10:00:41 -0700197 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500198 return devidx;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void mmc_blk_put(struct mmc_blk_data *md)
202{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000203 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 md->usage--;
205 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500206 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800207 blk_cleanup_queue(md->queue.queue);
208
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200209 spin_lock(&mmc_blk_lock);
210 ida_remove(&mmc_blk_ida, devidx);
211 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kfree(md);
215 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000216 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219static ssize_t power_ro_lock_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 int ret;
223 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 int locked = 0;
226
Asutosh Das507d9a72014-12-09 10:15:53 +0200227 if (!md)
228 return -EINVAL;
229
230 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 locked = 2;
233 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
234 locked = 1;
235
236 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
237
Tomas Winkler9098f842015-07-16 15:50:45 +0200238 mmc_blk_put(md);
239
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240 return ret;
241}
242
243static ssize_t power_ro_lock_store(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
245{
246 int ret;
247 struct mmc_blk_data *md, *part_md;
248 struct mmc_card *card;
249 unsigned long set;
250
251 if (kstrtoul(buf, 0, &set))
252 return -EINVAL;
253
254 if (set != 1)
255 return count;
256
257 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200258 if (!md)
259 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260 card = md->queue.card;
261
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200262 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100263
264 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
265 card->ext_csd.boot_ro_lock |
266 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
267 card->ext_csd.part_time);
268 if (ret)
269 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
270 else
271 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
272
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200273 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100274
275 if (!ret) {
276 pr_info("%s: Locking boot partition ro until next power on\n",
277 md->disk->disk_name);
278 set_disk_ro(md->disk, 1);
279
280 list_for_each_entry(part_md, &md->part, part)
281 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
282 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
283 set_disk_ro(part_md->disk, 1);
284 }
285 }
286
287 mmc_blk_put(md);
288 return count;
289}
290
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
293{
294 int ret;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296
Asutosh Das507d9a72014-12-09 10:15:53 +0200297 if (!md)
298 return -EINVAL;
299
Baruch Siach0031a982014-09-22 10:12:51 +0300300 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500301 get_disk_ro(dev_to_disk(dev)) ^
302 md->read_only);
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int ret;
311 char *end;
312 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
313 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200314
315 if (!md)
316 return -EINVAL;
317
Andrei Warkentin371a6892011-04-11 18:10:25 -0500318 if (end == buf) {
319 ret = -EINVAL;
320 goto out;
321 }
322
323 set_disk_ro(dev_to_disk(dev), set || md->read_only);
324 ret = count;
325out:
326 mmc_blk_put(md);
327 return ret;
328}
329
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200330static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200331no_pack_for_random_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 int ret;
336
Asutosh Das507d9a72014-12-09 10:15:53 +0200337 if (!md)
338 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200339 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
340
341 mmc_blk_put(md);
342 return ret;
343}
344
345static ssize_t
346no_pack_for_random_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 int value;
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200353 int ret = count;
354
Asutosh Das507d9a72014-12-09 10:15:53 +0200355 if (!md)
356 return -EINVAL;
357
358 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200359 if (!card) {
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 sscanf(buf, "%d", &value);
365
366 if (value < 0) {
367 pr_err("%s: value %d is not valid. old value remains = %d",
368 mmc_hostname(card->host), value,
369 md->queue.no_pack_for_random);
370 ret = -EINVAL;
371 goto exit;
372 }
373
374 md->queue.no_pack_for_random = (value > 0) ? true : false;
375
376 pr_debug("%s: no_pack_for_random: new value = %d",
377 mmc_hostname(card->host),
378 md->queue.no_pack_for_random);
379
380exit:
381 mmc_blk_put(md);
382 return ret;
383}
384
385static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386num_wr_reqs_to_start_packing_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
390 int num_wr_reqs_to_start_packing;
391 int ret;
392
Asutosh Das507d9a72014-12-09 10:15:53 +0200393 if (!md)
394 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
396
397 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
398
399 mmc_blk_put(md);
400 return ret;
401}
402
403static ssize_t
404num_wr_reqs_to_start_packing_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 int value;
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200411 int ret = count;
412
Asutosh Das507d9a72014-12-09 10:15:53 +0200413 if (!md)
414 return -EINVAL;
415
416 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200417 if (!card) {
418 ret = -EINVAL;
419 goto exit;
420 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200421
422 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200423
Yaniv Gardi42399822014-12-04 00:26:23 +0200424 if (value >= 0) {
425 md->queue.num_wr_reqs_to_start_packing =
426 min_t(int, value, (int)card->ext_csd.max_packed_writes);
427
428 pr_debug("%s: trigger to pack: new value = %d",
429 mmc_hostname(card->host),
430 md->queue.num_wr_reqs_to_start_packing);
431 } else {
432 pr_err("%s: value %d is not valid. old value remains = %d",
433 mmc_hostname(card->host), value,
434 md->queue.num_wr_reqs_to_start_packing);
435 ret = -EINVAL;
436 }
437
438exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200439 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200440 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200441}
442
Mark Salyzyn6904e432016-01-28 11:12:25 -0800443#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
444
445static int max_read_speed, max_write_speed, cache_size = 4;
446
447module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
448MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
449module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
450MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
451module_param(cache_size, int, S_IRUSR | S_IRGRP);
452MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
453
454/*
455 * helper macros and expectations:
456 * size - unsigned long number of bytes
457 * jiffies - unsigned long HZ timestamp difference
458 * speed - unsigned KB/s transfer rate
459 */
460#define size_and_speed_to_jiffies(size, speed) \
461 ((size) * HZ / (speed) / 1024UL)
462#define jiffies_and_speed_to_size(jiffies, speed) \
463 (((speed) * (jiffies) * 1024UL) / HZ)
464#define jiffies_and_size_to_speed(jiffies, size) \
465 ((size) * HZ / (jiffies) / 1024UL)
466
467/* Limits to report warning */
468/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
469#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
470#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
471
472#define speed_valid(speed) ((speed) > 0)
473
474static const char off[] = "off\n";
475
476static int max_speed_show(int speed, char *buf)
477{
478 if (speed)
479 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
480 else
481 return scnprintf(buf, PAGE_SIZE, off);
482}
483
484static int max_speed_store(const char *buf, struct request_queue *q)
485{
486 unsigned int limit, set = 0;
487
488 if (!strncasecmp(off, buf, sizeof(off) - 2))
489 return set;
490 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
491 return -EINVAL;
492 if (set == 0)
493 return set;
494 limit = MAX_SPEED(q);
495 if (set > limit)
496 pr_warn("max speed %u ineffective above %u\n", set, limit);
497 limit = MIN_SPEED(q);
498 if (set < limit)
499 pr_warn("max speed %u painful below %u\n", set, limit);
500 return set;
501}
502
503static ssize_t max_write_speed_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
507 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
508
509 mmc_blk_put(md);
510 return ret;
511}
512
513static ssize_t max_write_speed_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
518 int set = max_speed_store(buf, md->queue.queue);
519
520 if (set < 0) {
521 mmc_blk_put(md);
522 return set;
523 }
524
525 atomic_set(&md->queue.max_write_speed, set);
526 mmc_blk_put(md);
527 return count;
528}
529
530static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
531 max_write_speed_show, max_write_speed_store);
532
533static ssize_t max_read_speed_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
537 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
538
539 mmc_blk_put(md);
540 return ret;
541}
542
543static ssize_t max_read_speed_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
548 int set = max_speed_store(buf, md->queue.queue);
549
550 if (set < 0) {
551 mmc_blk_put(md);
552 return set;
553 }
554
555 atomic_set(&md->queue.max_read_speed, set);
556 mmc_blk_put(md);
557 return count;
558}
559
560static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
561 max_read_speed_show, max_read_speed_store);
562
563static ssize_t cache_size_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
567 struct mmc_queue *mq = &md->queue;
568 int cache_size = atomic_read(&mq->cache_size);
569 int ret;
570
571 if (!cache_size)
572 ret = scnprintf(buf, PAGE_SIZE, off);
573 else {
574 int speed = atomic_read(&mq->max_write_speed);
575
576 if (!speed_valid(speed))
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
578 else { /* We accept race between cache_jiffies and cache_used */
579 unsigned long size = jiffies_and_speed_to_size(
580 jiffies - mq->cache_jiffies, speed);
581 long used = atomic_long_read(&mq->cache_used);
582
583 if (size >= used)
584 size = 0;
585 else
586 size = (used - size) * 100 / cache_size
587 / 1024UL / 1024UL;
588
589 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
590 cache_size, size);
591 }
592 }
593
594 mmc_blk_put(md);
595 return ret;
596}
597
598static ssize_t cache_size_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
601{
602 struct mmc_blk_data *md;
603 unsigned int set = 0;
604
605 if (strncasecmp(off, buf, sizeof(off) - 2)
606 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
607 return -EINVAL;
608
609 md = mmc_blk_get(dev_to_disk(dev));
610 atomic_set(&md->queue.cache_size, set);
611 mmc_blk_put(md);
612 return count;
613}
614
615static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
616 cache_size_show, cache_size_store);
617
618/* correct for write-back */
619static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
620{
621 long used = 0;
622 int speed = atomic_read(&mq->max_write_speed);
623
624 if (speed_valid(speed)) {
625 unsigned long size = jiffies_and_speed_to_size(
626 waitfor - mq->cache_jiffies, speed);
627 used = atomic_long_read(&mq->cache_used);
628
629 if (size >= used)
630 used = 0;
631 else
632 used -= size;
633 }
634
635 atomic_long_set(&mq->cache_used, used);
636 mq->cache_jiffies = waitfor;
637
638 return used;
639}
640
641static void mmc_blk_simulate_delay(
642 struct mmc_queue *mq,
643 struct request *req,
644 unsigned long waitfor)
645{
646 int max_speed;
647
648 if (!req)
649 return;
650
651 max_speed = (rq_data_dir(req) == READ)
652 ? atomic_read(&mq->max_read_speed)
653 : atomic_read(&mq->max_write_speed);
654 if (speed_valid(max_speed)) {
655 unsigned long bytes = blk_rq_bytes(req);
656
657 if (rq_data_dir(req) != READ) {
658 int cache_size = atomic_read(&mq->cache_size);
659
660 if (cache_size) {
661 unsigned long size = cache_size * 1024L * 1024L;
662 long used = mmc_blk_cache_used(mq, waitfor);
663
664 used += bytes;
665 atomic_long_set(&mq->cache_used, used);
666 bytes = 0;
667 if (used > size)
668 bytes = used - size;
669 }
670 }
671 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
672 if (time_is_after_jiffies(waitfor)) {
673 long msecs = jiffies_to_msecs(waitfor - jiffies);
674
675 if (likely(msecs > 0))
676 msleep(msecs);
677 }
678 }
679}
680
681#else
682
683#define mmc_blk_simulate_delay(mq, req, waitfor)
684
685#endif
686
Al Viroa5a15612008-03-02 10:33:30 -0500687static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viroa5a15612008-03-02 10:33:30 -0500689 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = -ENXIO;
691
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200692 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (md) {
694 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500695 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697
Al Viroa5a15612008-03-02 10:33:30 -0500698 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700699 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700700 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return ret;
706}
707
Al Virodb2a1442013-05-05 21:52:57 -0400708static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viroa5a15612008-03-02 10:33:30 -0500710 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200712 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200714 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800718mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
721 geo->heads = 4;
722 geo->sectors = 16;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
John Calixtocb87ea22011-04-26 18:56:29 -0400726struct mmc_blk_ioc_data {
727 struct mmc_ioc_cmd ic;
728 unsigned char *buf;
729 u64 buf_bytes;
730};
731
732static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
733 struct mmc_ioc_cmd __user *user)
734{
735 struct mmc_blk_ioc_data *idata;
736 int err;
737
yalin wang1ff89502015-11-12 19:27:11 +0800738 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400739 if (!idata) {
740 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400742 }
743
744 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
745 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400746 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400747 }
748
749 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
750 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
751 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400752 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400753 }
754
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 if (!idata->buf_bytes) {
756 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300758 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100759
yalin wang1ff89502015-11-12 19:27:11 +0800760 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400761 if (!idata->buf) {
762 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400763 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400764 }
765
766 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
767 idata->ic.data_ptr, idata->buf_bytes)) {
768 err = -EFAULT;
769 goto copy_err;
770 }
771
772 return idata;
773
774copy_err:
775 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400776idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400777 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400778out:
John Calixtocb87ea22011-04-26 18:56:29 -0400779 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
Jon Huntera5f57742015-09-22 10:27:53 +0100782static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
783 struct mmc_blk_ioc_data *idata)
784{
785 struct mmc_ioc_cmd *ic = &idata->ic;
786
787 if (copy_to_user(&(ic_ptr->response), ic->response,
788 sizeof(ic->response)))
789 return -EFAULT;
790
791 if (!idata->ic.write_flag) {
792 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
793 idata->buf, idata->buf_bytes))
794 return -EFAULT;
795 }
796
797 return 0;
798}
799
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200800static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
801 u32 retries_max)
802{
803 int err;
804 u32 retry_count = 0;
805
806 if (!status || !retries_max)
807 return -EINVAL;
808
809 do {
810 err = get_card_status(card, status, 5);
811 if (err)
812 break;
813
814 if (!R1_STATUS(*status) &&
815 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
816 break; /* RPMB programming operation complete */
817
818 /*
819 * Rechedule to give the MMC device a chance to continue
820 * processing the previous command without being polled too
821 * frequently.
822 */
823 usleep_range(1000, 5000);
824 } while (++retry_count < retries_max);
825
826 if (retry_count == retries_max)
827 err = -EPERM;
828
829 return err;
830}
831
Maya Erez775a9362013-04-18 15:41:55 +0300832static int ioctl_do_sanitize(struct mmc_card *card)
833{
834 int err;
835
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530836 if (!mmc_can_sanitize(card) &&
837 (card->host->caps2 & MMC_CAP2_SANITIZE)) {
838 pr_warn("%s: %s - SANITIZE is not supported\n",
Maya Erez775a9362013-04-18 15:41:55 +0300839 mmc_hostname(card->host), __func__);
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530840 err = -EOPNOTSUPP;
841 goto out;
Maya Erez775a9362013-04-18 15:41:55 +0300842 }
843
844 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
845 mmc_hostname(card->host), __func__);
846
847 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
848 EXT_CSD_SANITIZE_START, 1,
849 MMC_SANITIZE_REQ_TIMEOUT);
850
851 if (err)
852 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
853 mmc_hostname(card->host), __func__, err);
854
855 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
856 __func__);
857out:
858 return err;
859}
860
Jon Huntera5f57742015-09-22 10:27:53 +0100861static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
862 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400863{
John Calixtocb87ea22011-04-26 18:56:29 -0400864 struct mmc_command cmd = {0};
865 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530866 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400867 struct scatterlist sg;
868 int err;
869
Jon Huntera5f57742015-09-22 10:27:53 +0100870 if (!card || !md || !idata)
871 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400872
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100873 cmd.opcode = idata->ic.opcode;
874 cmd.arg = idata->ic.arg;
875 cmd.flags = idata->ic.flags;
876
877 if (idata->buf_bytes) {
878 data.sg = &sg;
879 data.sg_len = 1;
880 data.blksz = idata->ic.blksz;
881 data.blocks = idata->ic.blocks;
882
883 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
884
885 if (idata->ic.write_flag)
886 data.flags = MMC_DATA_WRITE;
887 else
888 data.flags = MMC_DATA_READ;
889
890 /* data.flags must already be set before doing this. */
891 mmc_set_data_timeout(&data, card);
892
893 /* Allow overriding the timeout_ns for empirical tuning. */
894 if (idata->ic.data_timeout_ns)
895 data.timeout_ns = idata->ic.data_timeout_ns;
896
897 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
898 /*
899 * Pretend this is a data transfer and rely on the
900 * host driver to compute timeout. When all host
901 * drivers support cmd.cmd_timeout for R1B, this
902 * can be changed to:
903 *
904 * mrq.data = NULL;
905 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
906 */
907 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
908 }
909
910 mrq.data = &data;
911 }
912
913 mrq.cmd = &cmd;
914
Maya Erez70afbaf2015-10-01 11:27:17 +0300915 if (mmc_card_doing_bkops(card)) {
916 err = mmc_stop_bkops(card);
917 if (err) {
918 dev_err(mmc_dev(card->host),
919 "%s: stop_bkops failed %d\n", __func__, err);
920 return err;
921 }
922 }
923
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200924 err = mmc_blk_part_switch(card, md);
925 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100926 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200927
John Calixtocb87ea22011-04-26 18:56:29 -0400928 if (idata->ic.is_acmd) {
929 err = mmc_app_cmd(card->host, card);
930 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100931 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400932 }
933
Yaniv Gardia82e4842013-06-05 14:13:08 +0300934 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
935 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300936 err = ioctl_do_sanitize(card);
937
938 if (err)
939 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
940 __func__, err);
941
Jon Huntera5f57742015-09-22 10:27:53 +0100942 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300943 }
944
John Calixtocb87ea22011-04-26 18:56:29 -0400945 mmc_wait_for_req(card->host, &mrq);
946
947 if (cmd.error) {
948 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
949 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100950 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400951 }
952 if (data.error) {
953 dev_err(mmc_dev(card->host), "%s: data error %d\n",
954 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100955 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400956 }
957
958 /*
959 * According to the SD specs, some commands require a delay after
960 * issuing the command.
961 */
962 if (idata->ic.postsleep_min_us)
963 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
964
Jon Huntera5f57742015-09-22 10:27:53 +0100965 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400966
Krishna Kondae6711632014-12-04 15:20:57 +0200967 return err;
968}
969
970struct mmc_blk_ioc_rpmb_data {
971 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
972};
973
974static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
975 struct mmc_ioc_rpmb __user *user)
976{
977 struct mmc_blk_ioc_rpmb_data *idata;
978 int err, i;
979
980 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
981 if (!idata) {
982 err = -ENOMEM;
983 goto out;
984 }
985
986 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
987 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
988 if (IS_ERR(idata->data[i])) {
989 err = PTR_ERR(idata->data[i]);
990 goto copy_err;
991 }
992 }
993
994 return idata;
995
996copy_err:
997 while (--i >= 0) {
998 kfree(idata->data[i]->buf);
999 kfree(idata->data[i]);
1000 }
1001 kfree(idata);
1002out:
1003 return ERR_PTR(err);
1004}
1005
1006static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
1007 struct mmc_ioc_rpmb __user *ic_ptr)
1008{
1009 struct mmc_blk_ioc_rpmb_data *idata;
1010 struct mmc_blk_data *md;
1011 struct mmc_card *card;
1012 struct mmc_command cmd = {0};
1013 struct mmc_data data = {0};
1014 struct mmc_request mrq = {NULL};
1015 struct scatterlist sg;
1016 int err = 0, i = 0;
1017 u32 status = 0;
1018
1019 /* The caller must have CAP_SYS_RAWIO */
1020 if (!capable(CAP_SYS_RAWIO))
1021 return -EPERM;
1022
1023 md = mmc_blk_get(bdev->bd_disk);
1024 /* make sure this is a rpmb partition */
1025 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1026 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001027 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001028 }
1029
1030 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1031 if (IS_ERR(idata)) {
1032 err = PTR_ERR(idata);
1033 goto cmd_done;
1034 }
1035
1036 card = md->queue.card;
1037 if (IS_ERR(card)) {
1038 err = PTR_ERR(card);
1039 goto idata_free;
1040 }
1041
Maya Erezdd669562015-02-12 20:37:31 +02001042 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001043
Maya Erez70afbaf2015-10-01 11:27:17 +03001044 if (mmc_card_doing_bkops(card)) {
1045 if (mmc_card_cmdq(card)) {
1046 err = mmc_cmdq_halt(card->host, true);
1047 if (err)
1048 goto cmd_rel_host;
1049 }
1050 err = mmc_stop_bkops(card);
1051 if (err) {
1052 dev_err(mmc_dev(card->host),
1053 "%s: stop_bkops failed %d\n", __func__, err);
1054 goto cmd_rel_host;
1055 }
1056 if (mmc_card_cmdq(card)) {
1057 err = mmc_cmdq_halt(card->host, false);
1058 if (err)
1059 goto cmd_rel_host;
1060 }
1061 }
1062
Krishna Kondae6711632014-12-04 15:20:57 +02001063 err = mmc_blk_part_switch(card, md);
1064 if (err)
1065 goto cmd_rel_host;
1066
1067 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1068 struct mmc_blk_ioc_data *curr_data;
1069 struct mmc_ioc_cmd *curr_cmd;
1070
1071 curr_data = idata->data[i];
1072 curr_cmd = &curr_data->ic;
1073 if (!curr_cmd->opcode)
1074 break;
1075
1076 cmd.opcode = curr_cmd->opcode;
1077 cmd.arg = curr_cmd->arg;
1078 cmd.flags = curr_cmd->flags;
1079
1080 if (curr_data->buf_bytes) {
1081 data.sg = &sg;
1082 data.sg_len = 1;
1083 data.blksz = curr_cmd->blksz;
1084 data.blocks = curr_cmd->blocks;
1085
1086 sg_init_one(data.sg, curr_data->buf,
1087 curr_data->buf_bytes);
1088
1089 if (curr_cmd->write_flag)
1090 data.flags = MMC_DATA_WRITE;
1091 else
1092 data.flags = MMC_DATA_READ;
1093
1094 /* data.flags must already be set before doing this. */
1095 mmc_set_data_timeout(&data, card);
1096
1097 /*
1098 * Allow overriding the timeout_ns for empirical tuning.
1099 */
1100 if (curr_cmd->data_timeout_ns)
1101 data.timeout_ns = curr_cmd->data_timeout_ns;
1102
1103 mrq.data = &data;
1104 }
1105
1106 mrq.cmd = &cmd;
1107
1108 err = mmc_set_blockcount(card, data.blocks,
1109 curr_cmd->write_flag & (1 << 31));
1110 if (err)
1111 goto cmd_rel_host;
1112
1113 mmc_wait_for_req(card->host, &mrq);
1114
1115 if (cmd.error) {
1116 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1117 __func__, cmd.error);
1118 err = cmd.error;
1119 goto cmd_rel_host;
1120 }
1121 if (data.error) {
1122 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1123 __func__, data.error);
1124 err = data.error;
1125 goto cmd_rel_host;
1126 }
1127
1128 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1129 sizeof(cmd.resp))) {
1130 err = -EFAULT;
1131 goto cmd_rel_host;
1132 }
1133
1134 if (!curr_cmd->write_flag) {
1135 if (copy_to_user((void __user *)(unsigned long)
1136 curr_cmd->data_ptr,
1137 curr_data->buf,
1138 curr_data->buf_bytes)) {
1139 err = -EFAULT;
1140 goto cmd_rel_host;
1141 }
1142 }
1143
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001144 /*
1145 * Ensure RPMB command has completed by polling CMD13
1146 * "Send Status".
1147 */
1148 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1149 if (err)
1150 dev_err(mmc_dev(card->host),
1151 "%s: Card Status=0x%08X, error %d\n",
1152 __func__, status, err);
1153 }
1154
Krishna Kondae6711632014-12-04 15:20:57 +02001155cmd_rel_host:
1156 mmc_put_card(card);
1157
1158idata_free:
1159 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1160 kfree(idata->data[i]->buf);
1161 kfree(idata->data[i]);
1162 }
1163 kfree(idata);
1164
1165cmd_done:
1166 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001167 return err;
1168}
1169
1170static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1171 struct mmc_ioc_cmd __user *ic_ptr)
1172{
1173 struct mmc_blk_ioc_data *idata;
1174 struct mmc_blk_data *md;
1175 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001176 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001177
Shawn Lin83c742c2016-03-16 18:15:47 +08001178 /*
1179 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1180 * whole block device, not on a partition. This prevents overspray
1181 * between sibling partitions.
1182 */
1183 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1184 return -EPERM;
1185
Jon Huntera5f57742015-09-22 10:27:53 +01001186 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301187 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001188 return PTR_ERR(idata);
1189
1190 md = mmc_blk_get(bdev->bd_disk);
1191 if (!md) {
1192 err = -EINVAL;
1193 goto cmd_err;
1194 }
1195
1196 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301197 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001198 err = PTR_ERR(card);
1199 goto cmd_done;
1200 }
1201
1202 mmc_get_card(card);
1203
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301204 if (mmc_card_cmdq(card)) {
1205 err = mmc_cmdq_halt_on_empty_queue(card->host);
1206 if (err) {
1207 pr_err("%s: halt failed while doing %s err (%d)\n",
1208 mmc_hostname(card->host),
1209 __func__, err);
1210 mmc_put_card(card);
1211 goto cmd_done;
1212 }
1213 }
1214
Grant Grundlerb0934102015-09-23 18:30:33 -07001215 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001216
Adrian Hunter3c866562016-05-04 14:38:12 +03001217 /* Always switch back to main area after RPMB access */
1218 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1219 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1220
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001221 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001222
Grant Grundlerb0934102015-09-23 18:30:33 -07001223 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001224
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301225 if (mmc_card_cmdq(card)) {
1226 if (mmc_cmdq_halt(card->host, false))
1227 pr_err("%s: %s: cmdq unhalt failed\n",
1228 mmc_hostname(card->host), __func__);
1229 }
1230
John Calixtocb87ea22011-04-26 18:56:29 -04001231cmd_done:
1232 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001233cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001234 kfree(idata->buf);
1235 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001236 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001237}
1238
Jon Huntera5f57742015-09-22 10:27:53 +01001239static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1240 struct mmc_ioc_multi_cmd __user *user)
1241{
1242 struct mmc_blk_ioc_data **idata = NULL;
1243 struct mmc_ioc_cmd __user *cmds = user->cmds;
1244 struct mmc_card *card;
1245 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001246 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001247 __u64 num_of_cmds;
1248
Shawn Lin83c742c2016-03-16 18:15:47 +08001249 /*
1250 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1251 * whole block device, not on a partition. This prevents overspray
1252 * between sibling partitions.
1253 */
1254 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1255 return -EPERM;
1256
Jon Huntera5f57742015-09-22 10:27:53 +01001257 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1258 sizeof(num_of_cmds)))
1259 return -EFAULT;
1260
1261 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1262 return -EINVAL;
1263
1264 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1265 if (!idata)
1266 return -ENOMEM;
1267
1268 for (i = 0; i < num_of_cmds; i++) {
1269 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1270 if (IS_ERR(idata[i])) {
1271 err = PTR_ERR(idata[i]);
1272 num_of_cmds = i;
1273 goto cmd_err;
1274 }
1275 }
1276
1277 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001278 if (!md) {
1279 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001280 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001281 }
Jon Huntera5f57742015-09-22 10:27:53 +01001282
1283 card = md->queue.card;
1284 if (IS_ERR(card)) {
1285 err = PTR_ERR(card);
1286 goto cmd_done;
1287 }
1288
1289 mmc_get_card(card);
1290
Grant Grundlerb0934102015-09-23 18:30:33 -07001291 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1292 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001293
Adrian Hunter3c866562016-05-04 14:38:12 +03001294 /* Always switch back to main area after RPMB access */
1295 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1296 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1297
Jon Huntera5f57742015-09-22 10:27:53 +01001298 mmc_put_card(card);
1299
1300 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001301 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001302 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001303
1304cmd_done:
1305 mmc_blk_put(md);
1306cmd_err:
1307 for (i = 0; i < num_of_cmds; i++) {
1308 kfree(idata[i]->buf);
1309 kfree(idata[i]);
1310 }
1311 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001312 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001313}
1314
John Calixtocb87ea22011-04-26 18:56:29 -04001315static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1316 unsigned int cmd, unsigned long arg)
1317{
Jon Huntera5f57742015-09-22 10:27:53 +01001318 switch (cmd) {
1319 case MMC_IOC_CMD:
1320 return mmc_blk_ioctl_cmd(bdev,
1321 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001322 case MMC_IOC_RPMB_CMD:
1323 return mmc_blk_ioctl_rpmb_cmd(bdev,
1324 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001325 case MMC_IOC_MULTI_CMD:
1326 return mmc_blk_ioctl_multi_cmd(bdev,
1327 (struct mmc_ioc_multi_cmd __user *)arg);
1328 default:
1329 return -EINVAL;
1330 }
John Calixtocb87ea22011-04-26 18:56:29 -04001331}
1332
1333#ifdef CONFIG_COMPAT
1334static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1335 unsigned int cmd, unsigned long arg)
1336{
1337 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1338}
1339#endif
1340
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001341static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001342 .open = mmc_blk_open,
1343 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001344 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001346 .ioctl = mmc_blk_ioctl,
1347#ifdef CONFIG_COMPAT
1348 .compat_ioctl = mmc_blk_compat_ioctl,
1349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350};
1351
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001352static int mmc_blk_cmdq_switch(struct mmc_card *card,
1353 struct mmc_blk_data *md, bool enable)
1354{
1355 int ret = 0;
1356 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301357 struct mmc_host *host = card->host;
1358 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001359
1360 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1361 !card->ext_csd.cmdq_support ||
1362 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1363 (cmdq_mode == enable))
1364 return 0;
1365
1366 if (enable) {
1367 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1368 if (ret) {
1369 pr_err("%s: failed (%d) to set block-size to %d\n",
1370 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1371 goto out;
1372 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301373
1374 } else {
1375 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1376 ret = mmc_cmdq_halt(host, true);
1377 if (ret) {
1378 pr_err("%s: halt: failed: %d\n",
1379 mmc_hostname(host), ret);
1380 goto out;
1381 }
1382 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001383 }
1384
1385 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1386 EXT_CSD_CMDQ, enable,
1387 card->ext_csd.generic_cmd6_time);
1388 if (ret) {
1389 pr_err("%s: cmdq mode %sable failed %d\n",
1390 md->disk->disk_name, enable ? "en" : "dis", ret);
1391 goto out;
1392 }
1393
1394 if (enable)
1395 mmc_card_set_cmdq(card);
1396 else
1397 mmc_card_clr_cmdq(card);
1398out:
1399 return ret;
1400}
1401
Andrei Warkentin371a6892011-04-11 18:10:25 -05001402static inline int mmc_blk_part_switch(struct mmc_card *card,
1403 struct mmc_blk_data *md)
1404{
1405 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001406 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001407
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001408 if ((main_md->part_curr == md->part_type) &&
1409 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001410 return 0;
1411
1412 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001413 u8 part_config = card->ext_csd.part_config;
1414
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001415 if (md->part_type) {
1416 /* disable CQ mode for non-user data partitions */
1417 ret = mmc_blk_cmdq_switch(card, md, false);
1418 if (ret)
1419 return ret;
1420 }
1421
Adrian Hunter57da0c02016-05-04 14:38:13 +03001422 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1423 mmc_retune_pause(card->host);
1424
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001425 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1426 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001427
1428 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001429 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001430 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001431 if (ret) {
1432 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1433 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001434 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001435 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001436
1437 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001438 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001439
1440 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1441 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001442 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001443
1444 main_md->part_curr = md->part_type;
1445 return 0;
1446}
1447
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001448static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1449{
1450 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001451 u32 result;
1452 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001453
Venkatraman Sad5fd972011-08-25 00:30:50 +05301454 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001455 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001456 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001457
1458 struct scatterlist sg;
1459
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001460 cmd.opcode = MMC_APP_CMD;
1461 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001462 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001463
1464 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001465 if (err)
1466 return (u32)-1;
1467 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001468 return (u32)-1;
1469
1470 memset(&cmd, 0, sizeof(struct mmc_command));
1471
1472 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1473 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001474 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001475
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001476 data.blksz = 4;
1477 data.blocks = 1;
1478 data.flags = MMC_DATA_READ;
1479 data.sg = &sg;
1480 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301481 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001482
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001483 mrq.cmd = &cmd;
1484 mrq.data = &data;
1485
Ben Dooks051913d2009-06-08 23:33:57 +01001486 blocks = kmalloc(4, GFP_KERNEL);
1487 if (!blocks)
1488 return (u32)-1;
1489
1490 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001491
1492 mmc_wait_for_req(card->host, &mrq);
1493
Ben Dooks051913d2009-06-08 23:33:57 +01001494 result = ntohl(*blocks);
1495 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001496
Ben Dooks051913d2009-06-08 23:33:57 +01001497 if (cmd.error || data.error)
1498 result = (u32)-1;
1499
1500 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001501}
1502
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001503static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001504{
Chris Ball1278dba2011-04-13 23:40:30 -04001505 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001506 int err;
1507
Adrian Hunter504f1912008-10-16 12:55:25 +03001508 cmd.opcode = MMC_SEND_STATUS;
1509 if (!mmc_host_is_spi(card->host))
1510 cmd.arg = card->rca << 16;
1511 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001512 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1513 if (err == 0)
1514 *status = cmd.resp[0];
1515 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001516}
1517
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001518static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001519 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001520{
1521 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1522 int err = 0;
1523 u32 status;
1524
1525 do {
1526 err = get_card_status(card, &status, 5);
1527 if (err) {
1528 pr_err("%s: error %d requesting status\n",
1529 req->rq_disk->disk_name, err);
1530 return err;
1531 }
1532
1533 if (status & R1_ERROR) {
1534 pr_err("%s: %s: error sending status cmd, status %#x\n",
1535 req->rq_disk->disk_name, __func__, status);
1536 *gen_err = 1;
1537 }
1538
Ulf Hansson95a91292014-01-29 13:11:27 +01001539 /* We may rely on the host hw to handle busy detection.*/
1540 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1541 hw_busy_detect)
1542 break;
1543
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001544 /*
1545 * Timeout if the device never becomes ready for data and never
1546 * leaves the program state.
1547 */
1548 if (time_after(jiffies, timeout)) {
1549 pr_err("%s: Card stuck in programming state! %s %s\n",
1550 mmc_hostname(card->host),
1551 req->rq_disk->disk_name, __func__);
1552 return -ETIMEDOUT;
1553 }
1554
1555 /*
1556 * Some cards mishandle the status bits,
1557 * so make sure to check both the busy
1558 * indication and the card state.
1559 */
1560 } while (!(status & R1_READY_FOR_DATA) ||
1561 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1562
1563 return err;
1564}
1565
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001566static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1567 struct request *req, int *gen_err, u32 *stop_status)
1568{
1569 struct mmc_host *host = card->host;
1570 struct mmc_command cmd = {0};
1571 int err;
1572 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1573
1574 /*
1575 * Normally we use R1B responses for WRITE, but in cases where the host
1576 * has specified a max_busy_timeout we need to validate it. A failure
1577 * means we need to prevent the host from doing hw busy detection, which
1578 * is done by converting to a R1 response instead.
1579 */
1580 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1581 use_r1b_resp = false;
1582
1583 cmd.opcode = MMC_STOP_TRANSMISSION;
1584 if (use_r1b_resp) {
1585 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1586 cmd.busy_timeout = timeout_ms;
1587 } else {
1588 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1589 }
1590
1591 err = mmc_wait_for_cmd(host, &cmd, 5);
1592 if (err)
1593 return err;
1594
1595 *stop_status = cmd.resp[0];
1596
1597 /* No need to check card status in case of READ. */
1598 if (rq_data_dir(req) == READ)
1599 return 0;
1600
1601 if (!mmc_host_is_spi(host) &&
1602 (*stop_status & R1_ERROR)) {
1603 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1604 req->rq_disk->disk_name, __func__, *stop_status);
1605 *gen_err = 1;
1606 }
1607
1608 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1609}
1610
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301611#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001612#define ERR_RETRY 2
1613#define ERR_ABORT 1
1614#define ERR_CONTINUE 0
1615
1616static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1617 bool status_valid, u32 status)
1618{
1619 switch (error) {
1620 case -EILSEQ:
1621 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001622 pr_err_ratelimited(
1623 "%s: response CRC error sending %s command, card status %#x\n",
1624 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001625 name, status);
1626 return ERR_RETRY;
1627
1628 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001629 pr_err_ratelimited(
1630 "%s: timed out sending %s command, card status %#x\n",
1631 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001632
1633 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301634 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001635 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301636 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001637 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301638 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001639
1640 /*
1641 * If it was a r/w cmd crc error, or illegal command
1642 * (eg, issued in wrong state) then retry - we should
1643 * have corrected the state problem above.
1644 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301645 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001646 pr_err_ratelimited(
1647 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301648 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001649 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301650 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001651
1652 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001653 pr_err_ratelimited(
1654 "%s: not retrying timeout\n",
1655 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001656 return ERR_ABORT;
1657
1658 default:
1659 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001660 pr_err_ratelimited(
1661 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 req->rq_disk->disk_name, error, status);
1663 return ERR_ABORT;
1664 }
1665}
1666
1667/*
1668 * Initial r/w and stop cmd error recovery.
1669 * We don't know whether the card received the r/w cmd or not, so try to
1670 * restore things back to a sane state. Essentially, we do this as follows:
1671 * - Obtain card status. If the first attempt to obtain card status fails,
1672 * the status word will reflect the failed status cmd, not the failed
1673 * r/w cmd. If we fail to obtain card status, it suggests we can no
1674 * longer communicate with the card.
1675 * - Check the card state. If the card received the cmd but there was a
1676 * transient problem with the response, it might still be in a data transfer
1677 * mode. Try to send it a stop command. If this fails, we can't recover.
1678 * - If the r/w cmd failed due to a response CRC error, it was probably
1679 * transient, so retry the cmd.
1680 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1681 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1682 * illegal cmd, retry.
1683 * Otherwise we don't understand what happened, so abort.
1684 */
1685static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001686 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001687{
1688 bool prev_cmd_status_valid = true;
1689 u32 status, stop_status = 0;
1690 int err, retry;
1691
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301692 if (mmc_card_removed(card))
1693 return ERR_NOMEDIUM;
1694
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001695 /*
1696 * Try to get card status which indicates both the card state
1697 * and why there was no response. If the first attempt fails,
1698 * we can't be sure the returned status is for the r/w command.
1699 */
1700 for (retry = 2; retry >= 0; retry--) {
1701 err = get_card_status(card, &status, 0);
1702 if (!err)
1703 break;
1704
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001705 /* Re-tune if needed */
1706 mmc_retune_recheck(card->host);
1707
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001708 prev_cmd_status_valid = false;
1709 pr_err("%s: error %d sending status command, %sing\n",
1710 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1711 }
1712
1713 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301714 if (err) {
1715 /* Check if the card is removed */
1716 if (mmc_detect_card_removed(card->host))
1717 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001718 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301719 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001720
Adrian Hunter67716322011-08-29 16:42:15 +03001721 /* Flag ECC errors */
1722 if ((status & R1_CARD_ECC_FAILED) ||
1723 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1724 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1725 *ecc_err = 1;
1726
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001727 /* Flag General errors */
1728 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1729 if ((status & R1_ERROR) ||
1730 (brq->stop.resp[0] & R1_ERROR)) {
1731 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1732 req->rq_disk->disk_name, __func__,
1733 brq->stop.resp[0], status);
1734 *gen_err = 1;
1735 }
1736
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001737 /*
1738 * Check the current card state. If it is in some data transfer
1739 * mode, tell it to stop (and hopefully transition back to TRAN.)
1740 */
1741 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1742 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001743 err = send_stop(card,
1744 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1745 req, gen_err, &stop_status);
1746 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001747 pr_err("%s: error %d sending stop command\n",
1748 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001749 /*
1750 * If the stop cmd also timed out, the card is probably
1751 * not present, so abort. Other errors are bad news too.
1752 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001753 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001754 }
1755
Adrian Hunter67716322011-08-29 16:42:15 +03001756 if (stop_status & R1_CARD_ECC_FAILED)
1757 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001758 }
1759
1760 /* Check for set block count errors */
1761 if (brq->sbc.error)
1762 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1763 prev_cmd_status_valid, status);
1764
1765 /* Check for r/w command errors */
1766 if (brq->cmd.error)
1767 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1768 prev_cmd_status_valid, status);
1769
Adrian Hunter67716322011-08-29 16:42:15 +03001770 /* Data errors */
1771 if (!brq->stop.error)
1772 return ERR_CONTINUE;
1773
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001774 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001775 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001776 req->rq_disk->disk_name, brq->stop.error,
1777 brq->cmd.resp[0], status);
1778
1779 /*
1780 * Subsitute in our own stop status as this will give the error
1781 * state which happened during the execution of the r/w command.
1782 */
1783 if (stop_status) {
1784 brq->stop.resp[0] = stop_status;
1785 brq->stop.error = 0;
1786 }
1787 return ERR_CONTINUE;
1788}
1789
Adrian Hunter67716322011-08-29 16:42:15 +03001790static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1791 int type)
1792{
1793 int err;
1794
1795 if (md->reset_done & type)
1796 return -EEXIST;
1797
1798 md->reset_done |= type;
1799 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301800 if (err && err != -EOPNOTSUPP) {
1801 /* We failed to reset so we need to abort the request */
1802 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1803 __func__, err);
1804 return -ENODEV;
1805 }
1806
Adrian Hunter67716322011-08-29 16:42:15 +03001807 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301808 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001809 struct mmc_blk_data *main_md =
1810 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001811 int part_err;
1812
1813 main_md->part_curr = main_md->part_type;
1814 part_err = mmc_blk_part_switch(host->card, md);
1815 if (part_err) {
1816 /*
1817 * We have failed to get back into the correct
1818 * partition, so we need to abort the whole request.
1819 */
1820 return -ENODEV;
1821 }
1822 }
1823 return err;
1824}
1825
1826static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1827{
1828 md->reset_done &= ~type;
1829}
1830
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001831int mmc_access_rpmb(struct mmc_queue *mq)
1832{
1833 struct mmc_blk_data *md = mq->data;
1834 /*
1835 * If this is a RPMB partition access, return ture
1836 */
1837 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1838 return true;
1839
1840 return false;
1841}
1842
Sahitya Tummala9433a132015-06-09 09:38:36 +05301843static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1844 struct request *req)
1845{
1846 struct mmc_blk_data *md = mq->data;
1847 struct mmc_card *card = md->queue.card;
1848 struct mmc_host *host = card->host;
1849 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1850 struct mmc_cmdq_req *cmdq_req;
1851 struct mmc_queue_req *active_mqrq;
1852
1853 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1854 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1855
1856 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1857
1858 active_mqrq = &mq->mqrq_cmdq[req->tag];
1859 active_mqrq->req = req;
1860
1861 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1862 cmdq_req->cmdq_req_flags |= QBR;
1863 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1864 cmdq_req->tag = req->tag;
1865 return cmdq_req;
1866}
1867
1868static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1869 struct request *req)
1870{
1871 struct mmc_blk_data *md = mq->data;
1872 struct mmc_card *card = md->queue.card;
1873 struct mmc_cmdq_req *cmdq_req = NULL;
1874 struct mmc_host *host = card->host;
1875 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1876 unsigned int from, nr, arg;
1877 int err = 0;
1878
1879 if (!mmc_can_erase(card)) {
1880 err = -EOPNOTSUPP;
1881 goto out;
1882 }
1883
1884 from = blk_rq_pos(req);
1885 nr = blk_rq_sectors(req);
1886
1887 if (mmc_can_discard(card))
1888 arg = MMC_DISCARD_ARG;
1889 else if (mmc_can_trim(card))
1890 arg = MMC_TRIM_ARG;
1891 else
1892 arg = MMC_ERASE_ARG;
1893
1894 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1895 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1896 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1897 EXT_CSD_CMD_SET_NORMAL,
1898 INAND_CMD38_ARG_EXT_CSD,
1899 arg == MMC_TRIM_ARG ?
1900 INAND_CMD38_ARG_TRIM :
1901 INAND_CMD38_ARG_ERASE,
1902 0, true, false);
1903 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1904 if (err)
1905 goto clear_dcmd;
1906 }
1907 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1908clear_dcmd:
1909 /* clear pending request */
1910 if (cmdq_req) {
1911 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1912 &ctx_info->active_reqs));
1913 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1914 }
1915out:
1916 blk_end_request(req, err, blk_rq_bytes(req));
Subhash Jadavani987144c2015-08-12 17:53:21 -07001917 wake_up(&ctx_info->wait);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001918 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301919 return err ? 1 : 0;
1920}
1921
Adrian Hunterbd788c92010-08-11 14:17:47 -07001922static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1923{
1924 struct mmc_blk_data *md = mq->data;
1925 struct mmc_card *card = md->queue.card;
1926 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001927 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001928
Adrian Hunterbd788c92010-08-11 14:17:47 -07001929 if (!mmc_can_erase(card)) {
1930 err = -EOPNOTSUPP;
1931 goto out;
1932 }
1933
1934 from = blk_rq_pos(req);
1935 nr = blk_rq_sectors(req);
1936
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001937 if (mmc_can_discard(card))
1938 arg = MMC_DISCARD_ARG;
1939 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001940 arg = MMC_TRIM_ARG;
1941 else
1942 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001943retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001944 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1945 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1946 INAND_CMD38_ARG_EXT_CSD,
1947 arg == MMC_TRIM_ARG ?
1948 INAND_CMD38_ARG_TRIM :
1949 INAND_CMD38_ARG_ERASE,
1950 0);
1951 if (err)
1952 goto out;
1953 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001954 err = mmc_erase(card, from, nr, arg);
1955out:
Adrian Hunter67716322011-08-29 16:42:15 +03001956 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1957 goto retry;
1958 if (!err)
1959 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301960 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001961
Adrian Hunterbd788c92010-08-11 14:17:47 -07001962 return err ? 0 : 1;
1963}
1964
Sahitya Tummala9433a132015-06-09 09:38:36 +05301965static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1966 struct request *req)
1967{
1968 struct mmc_blk_data *md = mq->data;
1969 struct mmc_card *card = md->queue.card;
1970 struct mmc_cmdq_req *cmdq_req = NULL;
1971 unsigned int from, nr, arg;
1972 struct mmc_host *host = card->host;
1973 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1974 int err = 0;
1975
1976 if (!(mmc_can_secure_erase_trim(card))) {
1977 err = -EOPNOTSUPP;
1978 goto out;
1979 }
1980
1981 from = blk_rq_pos(req);
1982 nr = blk_rq_sectors(req);
1983
1984 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1985 arg = MMC_SECURE_TRIM1_ARG;
1986 else
1987 arg = MMC_SECURE_ERASE_ARG;
1988
1989 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1990 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1991 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1992 EXT_CSD_CMD_SET_NORMAL,
1993 INAND_CMD38_ARG_EXT_CSD,
1994 arg == MMC_SECURE_TRIM1_ARG ?
1995 INAND_CMD38_ARG_SECTRIM1 :
1996 INAND_CMD38_ARG_SECERASE,
1997 0, true, false);
1998 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1999 if (err)
2000 goto clear_dcmd;
2001 }
2002
2003 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
2004 if (err)
2005 goto clear_dcmd;
2006
2007 if (arg == MMC_SECURE_TRIM1_ARG) {
2008 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2009 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
2010 EXT_CSD_CMD_SET_NORMAL,
2011 INAND_CMD38_ARG_EXT_CSD,
2012 INAND_CMD38_ARG_SECTRIM2,
2013 0, true, false);
2014 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2015 if (err)
2016 goto clear_dcmd;
2017 }
2018
2019 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
2020 MMC_SECURE_TRIM2_ARG);
2021 }
2022clear_dcmd:
2023 /* clear pending request */
2024 if (cmdq_req) {
2025 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
2026 &ctx_info->active_reqs));
2027 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
2028 }
2029out:
2030 blk_end_request(req, err, blk_rq_bytes(req));
Subhash Jadavani987144c2015-08-12 17:53:21 -07002031 wake_up(&ctx_info->wait);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03002032 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302033 return err ? 1 : 0;
2034}
2035
Adrian Hunter49804542010-08-11 14:17:50 -07002036static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2037 struct request *req)
2038{
2039 struct mmc_blk_data *md = mq->data;
2040 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002041 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002042 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002043
Maya Erez775a9362013-04-18 15:41:55 +03002044 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002045 err = -EOPNOTSUPP;
2046 goto out;
2047 }
2048
2049 from = blk_rq_pos(req);
2050 nr = blk_rq_sectors(req);
2051
Maya Erez775a9362013-04-18 15:41:55 +03002052 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2053 arg = MMC_SECURE_TRIM1_ARG;
2054 else
2055 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002056
Adrian Hunter67716322011-08-29 16:42:15 +03002057retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002058 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2059 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2060 INAND_CMD38_ARG_EXT_CSD,
2061 arg == MMC_SECURE_TRIM1_ARG ?
2062 INAND_CMD38_ARG_SECTRIM1 :
2063 INAND_CMD38_ARG_SECERASE,
2064 0);
2065 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002066 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002067 }
Adrian Hunter28302812012-04-05 14:45:48 +03002068
Adrian Hunter49804542010-08-11 14:17:50 -07002069 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002070 if (err == -EIO)
2071 goto out_retry;
2072 if (err)
2073 goto out;
2074
2075 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002076 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2077 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2078 INAND_CMD38_ARG_EXT_CSD,
2079 INAND_CMD38_ARG_SECTRIM2,
2080 0);
2081 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002082 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002083 }
Adrian Hunter28302812012-04-05 14:45:48 +03002084
Adrian Hunter49804542010-08-11 14:17:50 -07002085 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002086 if (err == -EIO)
2087 goto out_retry;
2088 if (err)
2089 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002090 }
Adrian Hunter28302812012-04-05 14:45:48 +03002091
Adrian Hunter28302812012-04-05 14:45:48 +03002092out_retry:
2093 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002094 goto retry;
2095 if (!err)
2096 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002097out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302098 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002099
Adrian Hunter49804542010-08-11 14:17:50 -07002100 return err ? 0 : 1;
2101}
2102
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002103static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2104{
2105 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002106 struct mmc_card *card = md->queue.card;
2107 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002108
Sahitya Tummala61868a42015-05-28 16:54:19 +05302109 if (!req)
2110 return 0;
2111
2112 if (req->cmd_flags & REQ_BARRIER) {
2113 /*
2114 * If eMMC cache flush policy is set to 1, then the device
2115 * shall flush the requests in First-In-First-Out (FIFO) order.
2116 * In this case, as per spec, the host must not send any cache
2117 * barrier requests as they are redundant and add unnecessary
2118 * overhead to both device and host.
2119 */
2120 if (card->ext_csd.cache_flush_policy & 1)
2121 goto end_req;
2122
2123 /*
2124 * In case barrier is not supported or enabled in the device,
2125 * use flush as a fallback option.
2126 */
2127 ret = mmc_cache_barrier(card);
2128 if (ret)
2129 ret = mmc_flush_cache(card);
2130 } else if (req_op(req) == REQ_OP_FLUSH) {
2131 ret = mmc_flush_cache(card);
2132 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002133 if (ret == -ENODEV) {
2134 pr_err("%s: %s: restart mmc card",
2135 req->rq_disk->disk_name, __func__);
2136 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2137 pr_err("%s: %s: fail to restart mmc",
2138 req->rq_disk->disk_name, __func__);
2139 else
2140 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2141 }
2142
2143 if (ret) {
2144 pr_err("%s: %s: notify flush error to upper layers",
2145 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002146 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002147 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002148
Mark Salyzyn6904e432016-01-28 11:12:25 -08002149#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2150 else if (atomic_read(&mq->cache_size)) {
2151 long used = mmc_blk_cache_used(mq, jiffies);
2152
2153 if (used) {
2154 int speed = atomic_read(&mq->max_write_speed);
2155
2156 if (speed_valid(speed)) {
2157 unsigned long msecs = jiffies_to_msecs(
2158 size_and_speed_to_jiffies(
2159 used, speed));
2160 if (msecs)
2161 msleep(msecs);
2162 }
2163 }
2164 }
2165#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302166end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302167 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002168
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002169 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002170}
2171
2172/*
2173 * Reformat current write as a reliable write, supporting
2174 * both legacy and the enhanced reliable write MMC cards.
2175 * In each transfer we'll handle only as much as a single
2176 * reliable write can handle, thus finish the request in
2177 * partial completions.
2178 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002179static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2180 struct mmc_card *card,
2181 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002182{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002183 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2184 /* Legacy mode imposes restrictions on transfers. */
2185 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2186 brq->data.blocks = 1;
2187
2188 if (brq->data.blocks > card->ext_csd.rel_sectors)
2189 brq->data.blocks = card->ext_csd.rel_sectors;
2190 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2191 brq->data.blocks = 1;
2192 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002193}
2194
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002195#define CMD_ERRORS \
2196 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2197 R1_ADDRESS_ERROR | /* Misaligned address */ \
2198 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2199 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2200 R1_CC_ERROR | /* Card controller error */ \
2201 R1_ERROR) /* General/unknown error */
2202
Per Forlinee8a43a2011-07-01 18:55:33 +02002203static int mmc_blk_err_check(struct mmc_card *card,
2204 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002205{
Per Forlinee8a43a2011-07-01 18:55:33 +02002206 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2207 mmc_active);
2208 struct mmc_blk_request *brq = &mq_mrq->brq;
2209 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002210 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002211 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002212
2213 /*
2214 * sbc.error indicates a problem with the set block count
2215 * command. No data will have been transferred.
2216 *
2217 * cmd.error indicates a problem with the r/w command. No
2218 * data will have been transferred.
2219 *
2220 * stop.error indicates a problem with the stop command. Data
2221 * may have been transferred, or may still be transferring.
2222 */
Adrian Hunter67716322011-08-29 16:42:15 +03002223 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2224 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002225 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002226 case ERR_RETRY:
2227 return MMC_BLK_RETRY;
2228 case ERR_ABORT:
2229 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302230 case ERR_NOMEDIUM:
2231 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002232 case ERR_CONTINUE:
2233 break;
2234 }
2235 }
2236
2237 /*
2238 * Check for errors relating to the execution of the
2239 * initial command - such as address errors. No data
2240 * has been transferred.
2241 */
2242 if (brq->cmd.resp[0] & CMD_ERRORS) {
2243 pr_err("%s: r/w command failed, status = %#x\n",
2244 req->rq_disk->disk_name, brq->cmd.resp[0]);
2245 return MMC_BLK_ABORT;
2246 }
2247
2248 /*
2249 * Everything else is either success, or a data error of some
2250 * kind. If it was a write, we may have transitioned to
2251 * program mode, which we have to wait for it to complete.
2252 */
2253 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002254 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002255
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002256 /* Check stop command response */
2257 if (brq->stop.resp[0] & R1_ERROR) {
2258 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2259 req->rq_disk->disk_name, __func__,
2260 brq->stop.resp[0]);
2261 gen_err = 1;
2262 }
2263
Ulf Hansson95a91292014-01-29 13:11:27 +01002264 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2265 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002266 if (err)
2267 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002268 }
2269
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002270 /* if general error occurs, retry the write operation. */
2271 if (gen_err) {
2272 pr_warn("%s: retrying write for general error\n",
2273 req->rq_disk->disk_name);
2274 return MMC_BLK_RETRY;
2275 }
2276
Per Forlind78d4a82011-07-01 18:55:30 +02002277 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002278 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002279 pr_debug("%s: retrying because a re-tune was needed\n",
2280 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002281 brq->retune_retry_done = 1;
2282 return MMC_BLK_RETRY;
2283 }
Per Forlind78d4a82011-07-01 18:55:30 +02002284 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2285 req->rq_disk->disk_name, brq->data.error,
2286 (unsigned)blk_rq_pos(req),
2287 (unsigned)blk_rq_sectors(req),
2288 brq->cmd.resp[0], brq->stop.resp[0]);
2289
2290 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002291 if (ecc_err)
2292 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002293 return MMC_BLK_DATA_ERR;
2294 } else {
2295 return MMC_BLK_CMD_ERR;
2296 }
2297 }
2298
Adrian Hunter67716322011-08-29 16:42:15 +03002299 if (!brq->data.bytes_xfered)
2300 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002301
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002302 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2303 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2304 return MMC_BLK_PARTIAL;
2305 else
2306 return MMC_BLK_SUCCESS;
2307 }
2308
Adrian Hunter67716322011-08-29 16:42:15 +03002309 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2310 return MMC_BLK_PARTIAL;
2311
2312 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002313}
2314
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002315static int mmc_blk_packed_err_check(struct mmc_card *card,
2316 struct mmc_async_req *areq)
2317{
2318 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2319 mmc_active);
2320 struct request *req = mq_rq->req;
2321 struct mmc_packed *packed = mq_rq->packed;
2322 int err, check, status;
2323 u8 *ext_csd;
2324
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002325 packed->retries--;
2326 check = mmc_blk_err_check(card, areq);
2327 err = get_card_status(card, &status, 0);
2328 if (err) {
2329 pr_err("%s: error %d sending status command\n",
2330 req->rq_disk->disk_name, err);
2331 return MMC_BLK_ABORT;
2332 }
2333
2334 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002335 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002336 if (err) {
2337 pr_err("%s: error %d sending ext_csd\n",
2338 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002339 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002340 }
2341
2342 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2343 EXT_CSD_PACKED_FAILURE) &&
2344 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2345 EXT_CSD_PACKED_GENERIC_ERROR)) {
2346 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2347 EXT_CSD_PACKED_INDEXED_ERROR) {
2348 packed->idx_failure =
2349 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2350 check = MMC_BLK_PARTIAL;
2351 }
2352 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2353 "failure index: %d\n",
2354 req->rq_disk->disk_name, packed->nr_entries,
2355 packed->blocks, packed->idx_failure);
2356 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002357 kfree(ext_csd);
2358 }
2359
2360 return check;
2361}
2362
Per Forlin54d49d72011-07-01 18:55:29 +02002363static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2364 struct mmc_card *card,
2365 int disable_multi,
2366 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Per Forlin54d49d72011-07-01 18:55:29 +02002368 u32 readcmd, writecmd;
2369 struct mmc_blk_request *brq = &mqrq->brq;
2370 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302372 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002374 /*
2375 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002376 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002377 */
Luca Porziod3df0462015-11-06 15:12:26 +00002378 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002379 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002380 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002381
Per Forlin54d49d72011-07-01 18:55:29 +02002382 memset(brq, 0, sizeof(struct mmc_blk_request));
2383 brq->mrq.cmd = &brq->cmd;
2384 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Per Forlin54d49d72011-07-01 18:55:29 +02002386 brq->cmd.arg = blk_rq_pos(req);
2387 if (!mmc_card_blockaddr(card))
2388 brq->cmd.arg <<= 9;
2389 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2390 brq->data.blksz = 512;
2391 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2392 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002393 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Asutosh Dasf0665412012-07-27 18:10:19 +05302395 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002396 /*
2397 * The block layer doesn't support all sector count
2398 * restrictions, so we need to be prepared for too big
2399 * requests.
2400 */
2401 if (brq->data.blocks > card->host->max_blk_count)
2402 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002404 if (brq->data.blocks > 1) {
2405 /*
2406 * After a read error, we redo the request one sector
2407 * at a time in order to accurately determine which
2408 * sectors can be read successfully.
2409 */
2410 if (disable_multi)
2411 brq->data.blocks = 1;
2412
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002413 /*
2414 * Some controllers have HW issues while operating
2415 * in multiple I/O mode
2416 */
2417 if (card->host->ops->multi_io_quirk)
2418 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2419 (rq_data_dir(req) == READ) ?
2420 MMC_DATA_READ : MMC_DATA_WRITE,
2421 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002422 }
Per Forlin54d49d72011-07-01 18:55:29 +02002423
2424 if (brq->data.blocks > 1 || do_rel_wr) {
2425 /* SPI multiblock writes terminate using a special
2426 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002427 */
Per Forlin54d49d72011-07-01 18:55:29 +02002428 if (!mmc_host_is_spi(card->host) ||
2429 rq_data_dir(req) == READ)
2430 brq->mrq.stop = &brq->stop;
2431 readcmd = MMC_READ_MULTIPLE_BLOCK;
2432 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2433 } else {
2434 brq->mrq.stop = NULL;
2435 readcmd = MMC_READ_SINGLE_BLOCK;
2436 writecmd = MMC_WRITE_BLOCK;
2437 }
2438 if (rq_data_dir(req) == READ) {
2439 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002440 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002441 if (brq->mrq.stop)
2442 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2443 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002444 } else {
2445 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002446 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002447 if (brq->mrq.stop)
2448 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2449 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002450 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002451
Per Forlin54d49d72011-07-01 18:55:29 +02002452 if (do_rel_wr)
2453 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002454
Per Forlin54d49d72011-07-01 18:55:29 +02002455 /*
Saugata Das42659002011-12-21 13:09:17 +05302456 * Data tag is used only during writing meta data to speed
2457 * up write and any subsequent read of this meta data
2458 */
2459 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2460 (req->cmd_flags & REQ_META) &&
2461 (rq_data_dir(req) == WRITE) &&
2462 ((brq->data.blocks * brq->data.blksz) >=
2463 card->ext_csd.data_tag_unit_size);
2464
2465 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002466 * Pre-defined multi-block transfers are preferable to
2467 * open ended-ones (and necessary for reliable writes).
2468 * However, it is not sufficient to just send CMD23,
2469 * and avoid the final CMD12, as on an error condition
2470 * CMD12 (stop) needs to be sent anyway. This, coupled
2471 * with Auto-CMD23 enhancements provided by some
2472 * hosts, means that the complexity of dealing
2473 * with this is best left to the host. If CMD23 is
2474 * supported by card and host, we'll fill sbc in and let
2475 * the host deal with handling it correctly. This means
2476 * that for hosts that don't expose MMC_CAP_CMD23, no
2477 * change of behavior will be observed.
2478 *
2479 * N.B: Some MMC cards experience perf degradation.
2480 * We'll avoid using CMD23-bounded multiblock writes for
2481 * these, while retaining features like reliable writes.
2482 */
Saugata Das42659002011-12-21 13:09:17 +05302483 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2484 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2485 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002486 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2487 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302488 (do_rel_wr ? (1 << 31) : 0) |
2489 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002490 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2491 brq->mrq.sbc = &brq->sbc;
2492 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002493
Per Forlin54d49d72011-07-01 18:55:29 +02002494 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002495
Per Forlin54d49d72011-07-01 18:55:29 +02002496 brq->data.sg = mqrq->sg;
2497 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002498
Per Forlin54d49d72011-07-01 18:55:29 +02002499 /*
2500 * Adjust the sg list so it is the same size as the
2501 * request.
2502 */
2503 if (brq->data.blocks != blk_rq_sectors(req)) {
2504 int i, data_size = brq->data.blocks << 9;
2505 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002506
Per Forlin54d49d72011-07-01 18:55:29 +02002507 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2508 data_size -= sg->length;
2509 if (data_size <= 0) {
2510 sg->length += data_size;
2511 i++;
2512 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002513 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002514 }
Per Forlin54d49d72011-07-01 18:55:29 +02002515 brq->data.sg_len = i;
2516 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002517
Per Forlinee8a43a2011-07-01 18:55:33 +02002518 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302519 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002520 mqrq->mmc_active.err_check = mmc_blk_err_check;
2521
Per Forlin54d49d72011-07-01 18:55:29 +02002522 mmc_queue_bounce_pre(mqrq);
2523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002525static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2526 struct mmc_card *card)
2527{
2528 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2529 unsigned int max_seg_sz = queue_max_segment_size(q);
2530 unsigned int len, nr_segs = 0;
2531
2532 do {
2533 len = min(hdr_sz, max_seg_sz);
2534 hdr_sz -= len;
2535 nr_segs++;
2536 } while (hdr_sz);
2537
2538 return nr_segs;
2539}
2540
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002541/**
2542 * mmc_blk_disable_wr_packing() - disables packing mode
2543 * @mq: MMC queue.
2544 *
2545 */
2546void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2547{
2548 if (mq) {
2549 mq->wr_packing_enabled = false;
2550 mq->num_of_potential_packed_wr_reqs = 0;
2551 }
2552}
2553EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2554
Lee Susman841fd132013-04-23 17:59:26 +03002555static int get_packed_trigger(int potential, struct mmc_card *card,
2556 struct request *req, int curr_trigger)
2557{
2558 static int num_mean_elements = 1;
2559 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2560 unsigned int trigger = curr_trigger;
2561 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2562
2563 /* scale down the upper bound to 75% */
2564 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2565
2566 /*
2567 * since the most common calls for this function are with small
2568 * potential write values and since we don't want these calls to affect
2569 * the packed trigger, set a lower bound and ignore calls with
2570 * potential lower than that bound
2571 */
2572 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2573 return trigger;
2574
2575 /*
2576 * this is to prevent integer overflow in the following calculation:
2577 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2578 */
2579 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2580 num_mean_elements = 1;
2581 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2582 }
2583
2584 /*
2585 * get next mean value based on previous mean value and current
2586 * potential packed writes. Calculation is as follows:
2587 * mean_pot[i+1] =
2588 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2589 */
2590 mean_potential *= num_mean_elements;
2591 /*
2592 * add num_mean_elements so that the division of two integers doesn't
2593 * lower mean_potential too much
2594 */
2595 if (potential > mean_potential)
2596 mean_potential += num_mean_elements;
2597 mean_potential += potential;
2598 /* this is for gaining more precision when dividing two integers */
2599 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2600 /* this completes the mean calculation */
2601 mean_potential /= ++num_mean_elements;
2602 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2603
2604 /*
2605 * if current potential packed writes is greater than the mean potential
2606 * then the heuristic is that the following workload will contain many
2607 * write requests, therefore we lower the packed trigger. In the
2608 * opposite case we want to increase the trigger in order to get less
2609 * packing events.
2610 */
2611 if (potential >= mean_potential)
2612 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2613 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2614 else
2615 trigger = (trigger >= pckd_trgr_upper_bound) ?
2616 pckd_trgr_upper_bound : trigger + 1;
2617
2618 /*
2619 * an urgent read request indicates a packed list being interrupted
2620 * by this read, therefore we aim for less packing, hence the trigger
2621 * gets increased
2622 */
2623 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2624 trigger += PCKD_TRGR_URGENT_PENALTY;
2625
2626 return trigger;
2627}
2628
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002629static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2630 struct request *req)
2631{
2632 struct mmc_host *host = mq->card->host;
2633 int data_dir;
2634
2635 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2636 return;
2637
Maya Erez8e2b3c32012-12-02 13:27:15 +02002638 /* Support for the write packing on eMMC 4.5 or later */
2639 if (mq->card->ext_csd.rev <= 5)
2640 return;
2641
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002642 /*
2643 * In case the packing control is not supported by the host, it should
2644 * not have an effect on the write packing. Therefore we have to enable
2645 * the write packing
2646 */
2647 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2648 mq->wr_packing_enabled = true;
2649 return;
2650 }
2651
2652 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2653 if (mq->num_of_potential_packed_wr_reqs >
2654 mq->num_wr_reqs_to_start_packing)
2655 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002656 mq->num_wr_reqs_to_start_packing =
2657 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2658 mq->card, req,
2659 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002660 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002661 return;
2662 }
2663
2664 data_dir = rq_data_dir(req);
2665
2666 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002667 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002668 mq->num_wr_reqs_to_start_packing =
2669 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2670 mq->card, req,
2671 mq->num_wr_reqs_to_start_packing);
2672 mq->num_of_potential_packed_wr_reqs = 0;
2673 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002674 return;
2675 } else if (data_dir == WRITE) {
2676 mq->num_of_potential_packed_wr_reqs++;
2677 }
2678
2679 if (mq->num_of_potential_packed_wr_reqs >
2680 mq->num_wr_reqs_to_start_packing)
2681 mq->wr_packing_enabled = true;
2682}
2683
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002684struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2685{
2686 if (!card)
2687 return NULL;
2688
2689 return &card->wr_pack_stats;
2690}
2691EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2692
2693void mmc_blk_init_packed_statistics(struct mmc_card *card)
2694{
2695 int max_num_of_packed_reqs = 0;
2696
2697 if (!card || !card->wr_pack_stats.packing_events)
2698 return;
2699
2700 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2701
2702 spin_lock(&card->wr_pack_stats.lock);
2703 memset(card->wr_pack_stats.packing_events, 0,
2704 (max_num_of_packed_reqs + 1) *
2705 sizeof(*card->wr_pack_stats.packing_events));
2706 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2707 sizeof(card->wr_pack_stats.pack_stop_reason));
2708 card->wr_pack_stats.enabled = true;
2709 spin_unlock(&card->wr_pack_stats.lock);
2710}
2711EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2712
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002713static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2714{
2715 struct request_queue *q = mq->queue;
2716 struct mmc_card *card = mq->card;
2717 struct request *cur = req, *next = NULL;
2718 struct mmc_blk_data *md = mq->data;
2719 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2720 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2721 unsigned int req_sectors = 0, phys_segments = 0;
2722 unsigned int max_blk_count, max_phys_segs;
2723 bool put_back = true;
2724 u8 max_packed_rw = 0;
2725 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002726 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002727
Shawn Lin96e52da2016-08-26 08:49:55 +08002728 /*
2729 * We don't need to check packed for any further
2730 * operation of packed stuff as we set MMC_PACKED_NONE
2731 * and return zero for reqs if geting null packed. Also
2732 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2733 * it again when removing blk req.
2734 */
2735 if (!mqrq->packed) {
2736 md->flags &= (~MMC_BLK_PACKED_CMD);
2737 goto no_packed;
2738 }
2739
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002740 if (!(md->flags & MMC_BLK_PACKED_CMD))
2741 goto no_packed;
2742
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002743 if (!mq->wr_packing_enabled)
2744 goto no_packed;
2745
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002746 if ((rq_data_dir(cur) == WRITE) &&
2747 mmc_host_packed_wr(card->host))
2748 max_packed_rw = card->ext_csd.max_packed_writes;
2749
2750 if (max_packed_rw == 0)
2751 goto no_packed;
2752
2753 if (mmc_req_rel_wr(cur) &&
2754 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2755 goto no_packed;
2756
2757 if (mmc_large_sector(card) &&
2758 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2759 goto no_packed;
2760
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002761 if (cur->cmd_flags & REQ_FUA)
2762 goto no_packed;
2763
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002764 mmc_blk_clear_packed(mqrq);
2765
2766 max_blk_count = min(card->host->max_blk_count,
2767 card->host->max_req_size >> 9);
2768 if (unlikely(max_blk_count > 0xffff))
2769 max_blk_count = 0xffff;
2770
2771 max_phys_segs = queue_max_segments(q);
2772 req_sectors += blk_rq_sectors(cur);
2773 phys_segments += cur->nr_phys_segments;
2774
2775 if (rq_data_dir(cur) == WRITE) {
2776 req_sectors += mmc_large_sector(card) ? 8 : 1;
2777 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2778 }
2779
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002780 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002781 do {
2782 if (reqs >= max_packed_rw - 1) {
2783 put_back = false;
2784 break;
2785 }
2786
2787 spin_lock_irq(q->queue_lock);
2788 next = blk_fetch_request(q);
2789 spin_unlock_irq(q->queue_lock);
2790 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002791 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002792 put_back = false;
2793 break;
2794 }
2795
2796 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002797 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2798 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
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
Mike Christie3a5e02c2016-06-05 14:32:23 -05002802 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002803 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002804 req_op(next) == REQ_OP_FLUSH) {
2805 if (req_op(next) != REQ_OP_SECURE_ERASE)
2806 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002807 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002808 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002809
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002810 if (next->cmd_flags & REQ_FUA) {
2811 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2812 break;
2813 }
2814
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002815 if (rq_data_dir(cur) != rq_data_dir(next)) {
2816 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002817 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002818 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002819
2820 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002821 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2822 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002823 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002824 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002825
2826 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002827 if (req_sectors > max_blk_count) {
2828 if (stats->enabled)
2829 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002830 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002831 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002832
2833 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002834 if (phys_segments > max_phys_segs) {
2835 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002836 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002837 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002838
Maya Erez5a8dae12014-12-04 15:13:59 +02002839 if (mq->no_pack_for_random) {
2840 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2841 blk_rq_pos(next)) {
2842 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2843 put_back = 1;
2844 break;
2845 }
2846 }
2847
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002848 if (rq_data_dir(next) == WRITE)
2849 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002850 list_add_tail(&next->queuelist, &mqrq->packed->list);
2851 cur = next;
2852 reqs++;
2853 } while (1);
2854
2855 if (put_back) {
2856 spin_lock_irq(q->queue_lock);
2857 blk_requeue_request(q, next);
2858 spin_unlock_irq(q->queue_lock);
2859 }
2860
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002861 if (stats->enabled) {
2862 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2863 stats->packing_events[reqs + 1]++;
2864 if (reqs + 1 == max_packed_rw)
2865 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2866 }
2867
2868 spin_unlock(&stats->lock);
2869
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002870 if (reqs > 0) {
2871 list_add(&req->queuelist, &mqrq->packed->list);
2872 mqrq->packed->nr_entries = ++reqs;
2873 mqrq->packed->retries = reqs;
2874 return reqs;
2875 }
2876
2877no_packed:
2878 mqrq->cmd_type = MMC_PACKED_NONE;
2879 return 0;
2880}
2881
2882static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2883 struct mmc_card *card,
2884 struct mmc_queue *mq)
2885{
2886 struct mmc_blk_request *brq = &mqrq->brq;
2887 struct request *req = mqrq->req;
2888 struct request *prq;
2889 struct mmc_blk_data *md = mq->data;
2890 struct mmc_packed *packed = mqrq->packed;
2891 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002892 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002893 u8 hdr_blocks;
2894 u8 i = 1;
2895
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002896 mqrq->cmd_type = MMC_PACKED_WRITE;
2897 packed->blocks = 0;
2898 packed->idx_failure = MMC_PACKED_NR_IDX;
2899
2900 packed_cmd_hdr = packed->cmd_hdr;
2901 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002902 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2903 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002904 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2905
2906 /*
2907 * Argument for each entry of packed group
2908 */
2909 list_for_each_entry(prq, &packed->list, queuelist) {
2910 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2911 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2912 (prq->cmd_flags & REQ_META) &&
2913 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002914 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002915 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002916 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002917 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2918 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002919 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002920 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002921 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002922 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002923 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002924 packed->blocks += blk_rq_sectors(prq);
2925 i++;
2926 }
2927
2928 memset(brq, 0, sizeof(struct mmc_blk_request));
2929 brq->mrq.cmd = &brq->cmd;
2930 brq->mrq.data = &brq->data;
2931 brq->mrq.sbc = &brq->sbc;
2932 brq->mrq.stop = &brq->stop;
2933
2934 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2935 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2936 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2937
2938 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2939 brq->cmd.arg = blk_rq_pos(req);
2940 if (!mmc_card_blockaddr(card))
2941 brq->cmd.arg <<= 9;
2942 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2943
2944 brq->data.blksz = 512;
2945 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002946 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302947 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002948
2949 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2950 brq->stop.arg = 0;
2951 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2952
2953 mmc_set_data_timeout(&brq->data, card);
2954
2955 brq->data.sg = mqrq->sg;
2956 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2957
2958 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002959
2960 /*
2961 * This is intended for packed commands tests usage - in case these
2962 * functions are not in use the respective pointers are NULL
2963 */
2964 if (mq->err_check_fn)
2965 mqrq->mmc_active.err_check = mq->err_check_fn;
2966 else
2967 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2968
2969 if (mq->packed_test_fn)
2970 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002971
2972 mmc_queue_bounce_pre(mqrq);
2973}
2974
Adrian Hunter67716322011-08-29 16:42:15 +03002975static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2976 struct mmc_blk_request *brq, struct request *req,
2977 int ret)
2978{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002979 struct mmc_queue_req *mq_rq;
2980 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2981
Adrian Hunter67716322011-08-29 16:42:15 +03002982 /*
2983 * If this is an SD card and we're writing, we can first
2984 * mark the known good sectors as ok.
2985 *
2986 * If the card is not SD, we can still ok written sectors
2987 * as reported by the controller (which might be less than
2988 * the real number of written sectors, but never more).
2989 */
2990 if (mmc_card_sd(card)) {
2991 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302992 if (!brq->data.fault_injected) {
2993 blocks = mmc_sd_num_wr_blocks(card);
2994 if (blocks != (u32)-1)
2995 ret = blk_end_request(req, 0, blocks << 9);
2996 } else
2997 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002998 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002999 if (!mmc_packed_cmd(mq_rq->cmd_type))
3000 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03003001 }
3002 return ret;
3003}
3004
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003005static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
3006{
3007 struct request *prq;
3008 struct mmc_packed *packed = mq_rq->packed;
3009 int idx = packed->idx_failure, i = 0;
3010 int ret = 0;
3011
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003012 while (!list_empty(&packed->list)) {
3013 prq = list_entry_rq(packed->list.next);
3014 if (idx == i) {
3015 /* retry from error index */
3016 packed->nr_entries -= idx;
3017 mq_rq->req = prq;
3018 ret = 1;
3019
3020 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
3021 list_del_init(&prq->queuelist);
3022 mmc_blk_clear_packed(mq_rq);
3023 }
3024 return ret;
3025 }
3026 list_del_init(&prq->queuelist);
3027 blk_end_request(prq, 0, blk_rq_bytes(prq));
3028 i++;
3029 }
3030
3031 mmc_blk_clear_packed(mq_rq);
3032 return ret;
3033}
3034
3035static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3036{
3037 struct request *prq;
3038 struct mmc_packed *packed = mq_rq->packed;
3039
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003040 while (!list_empty(&packed->list)) {
3041 prq = list_entry_rq(packed->list.next);
3042 list_del_init(&prq->queuelist);
3043 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3044 }
3045
3046 mmc_blk_clear_packed(mq_rq);
3047}
3048
3049static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3050 struct mmc_queue_req *mq_rq)
3051{
3052 struct request *prq;
3053 struct request_queue *q = mq->queue;
3054 struct mmc_packed *packed = mq_rq->packed;
3055
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003056 while (!list_empty(&packed->list)) {
3057 prq = list_entry_rq(packed->list.prev);
3058 if (prq->queuelist.prev != &packed->list) {
3059 list_del_init(&prq->queuelist);
3060 spin_lock_irq(q->queue_lock);
3061 blk_requeue_request(mq->queue, prq);
3062 spin_unlock_irq(q->queue_lock);
3063 } else {
3064 list_del_init(&prq->queuelist);
3065 }
3066 }
3067
3068 mmc_blk_clear_packed(mq_rq);
3069}
3070
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003071static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3072 struct mmc_cmdq_req *cmdq_req)
3073{
3074 struct mmc_request *mrq = &cmdq_req->mrq;
3075
3076 mrq->done = mmc_blk_cmdq_req_done;
3077 return mmc_cmdq_start_req(host, cmdq_req);
3078}
3079
Asutosh Das5238e022015-04-23 16:00:45 +05303080/* prepare for non-data commands */
3081static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3082 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3083{
3084 struct request *req = mqrq->req;
3085 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3086
3087 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3088
3089 cmdq_req->mrq.data = NULL;
3090 cmdq_req->cmd_flags = req->cmd_flags;
3091 cmdq_req->mrq.req = mqrq->req;
3092 req->special = mqrq;
3093 cmdq_req->cmdq_req_flags |= DCMD;
3094 cmdq_req->mrq.cmdq_req = cmdq_req;
3095
3096 return &mqrq->cmdq_req;
3097}
3098
3099
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003100#define IS_RT_CLASS_REQ(x) \
3101 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3102
3103static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3104 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3105{
3106 struct mmc_card *card = mq->card;
3107 struct request *req = mqrq->req;
3108 struct mmc_blk_data *md = mq->data;
3109 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3110 bool do_data_tag;
3111 bool read_dir = (rq_data_dir(req) == READ);
3112 bool prio = IS_RT_CLASS_REQ(req);
3113 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3114
3115 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3116
3117 cmdq_rq->tag = req->tag;
3118 if (read_dir) {
3119 cmdq_rq->cmdq_req_flags |= DIR;
3120 cmdq_rq->data.flags = MMC_DATA_READ;
3121 } else {
3122 cmdq_rq->data.flags = MMC_DATA_WRITE;
3123 }
3124 if (prio)
3125 cmdq_rq->cmdq_req_flags |= PRIO;
3126
3127 if (do_rel_wr)
3128 cmdq_rq->cmdq_req_flags |= REL_WR;
3129
3130 cmdq_rq->data.blocks = blk_rq_sectors(req);
3131 cmdq_rq->blk_addr = blk_rq_pos(req);
3132 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3133
3134 mmc_set_data_timeout(&cmdq_rq->data, card);
3135
3136 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3137 (req->cmd_flags & REQ_META) &&
3138 (rq_data_dir(req) == WRITE) &&
3139 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3140 card->ext_csd.data_tag_unit_size);
3141 if (do_data_tag)
3142 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3143 cmdq_rq->data.sg = mqrq->sg;
3144 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3145
3146 /*
3147 * Adjust the sg list so it is the same size as the
3148 * request.
3149 */
3150 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3151 cmdq_rq->data.blocks = card->host->max_blk_count;
3152
3153 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3154 int i, data_size = cmdq_rq->data.blocks << 9;
3155 struct scatterlist *sg;
3156
3157 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3158 data_size -= sg->length;
3159 if (data_size <= 0) {
3160 sg->length += data_size;
3161 i++;
3162 break;
3163 }
3164 }
3165 cmdq_rq->data.sg_len = i;
3166 }
3167
3168 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3169 mqrq->cmdq_req.mrq.req = mqrq->req;
3170 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3171 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3172 mqrq->req->special = mqrq;
3173
3174 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",
3175 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3176 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3177 cmdq_rq, cmdq_rq->blk_addr,
3178 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3179
3180 return &mqrq->cmdq_req;
3181}
3182
3183static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3184{
3185 struct mmc_queue_req *active_mqrq;
3186 struct mmc_card *card = mq->card;
3187 struct mmc_host *host = card->host;
3188 struct mmc_cmdq_req *mc_rq;
3189 int ret = 0;
3190
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003191 mmc_deferred_scaling(host);
3192 mmc_cmdq_clk_scaling_start_busy(host, true);
3193
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003194 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003195 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003196 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3197
3198 active_mqrq = &mq->mqrq_cmdq[req->tag];
3199 active_mqrq->req = req;
3200
3201 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3202
3203 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3204 return ret;
3205}
3206
Asutosh Das5238e022015-04-23 16:00:45 +05303207/*
3208 * Issues a flush (dcmd) request
3209 */
3210int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3211{
3212 int err;
3213 struct mmc_queue_req *active_mqrq;
3214 struct mmc_card *card = mq->card;
3215 struct mmc_host *host;
3216 struct mmc_cmdq_req *cmdq_req;
3217 struct mmc_cmdq_context_info *ctx_info;
3218
3219 BUG_ON(!card);
3220 host = card->host;
3221 BUG_ON(!host);
3222 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3223 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3224
3225 ctx_info = &host->cmdq_ctx;
3226
3227 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3228
3229 active_mqrq = &mq->mqrq_cmdq[req->tag];
3230 active_mqrq->req = req;
3231
3232 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3233 cmdq_req->cmdq_req_flags |= QBR;
3234 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3235 cmdq_req->tag = req->tag;
3236
3237 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3238 if (err) {
3239 pr_err("%s: failed (%d) preparing flush req\n",
3240 mmc_hostname(host), err);
3241 return err;
3242 }
3243 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3244 return err;
3245}
3246EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3247
Asutosh Das02e30862015-05-20 16:52:04 +05303248static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3249{
3250 if (!host->cmdq_ops->reset)
3251 return;
3252
3253 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3254 if (mmc_cmdq_halt(host, true)) {
3255 pr_err("%s: halt failed\n", mmc_hostname(host));
3256 goto reset;
3257 }
3258 }
3259
3260 if (clear_all)
3261 mmc_cmdq_discard_queue(host, 0);
3262reset:
3263 mmc_hw_reset(host);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303264 mmc_host_clk_hold(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303265 host->cmdq_ops->reset(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303266 mmc_host_clk_release(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303267 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3268}
3269
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303270static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3271{
3272 int err;
3273 struct mmc_card *card = mq->card;
3274 struct mmc_host *host = card->host;
3275
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003276 mmc_get_card(card);
3277 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303278 err = mmc_cmdq_halt(host, true);
3279 if (err) {
3280 pr_err("%s: halt: failed: %d\n", __func__, err);
3281 return;
3282 }
3283
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303284 /* disable CQ mode in card */
3285 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3286 EXT_CSD_CMDQ, 0,
3287 card->ext_csd.generic_cmd6_time);
3288 if (err) {
3289 pr_err("%s: failed to switch card to legacy mode: %d\n",
3290 __func__, err);
3291 goto out;
3292 } else {
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003293 mmc_card_clr_cmdq(card);
3294 host->cmdq_ops->disable(host, false);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303295 host->card->cmdq_init = false;
3296 }
3297out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003298 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003299 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303300}
3301
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303302static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3303{
3304 struct mmc_queue *mq = req->q->queuedata;
3305 struct mmc_host *host = mq->card->host;
3306 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003307 struct mmc_request *mrq;
3308 struct mmc_cmdq_req *cmdq_req;
3309
3310 BUG_ON(!host);
3311
3312 /*
3313 * The mmc_queue_req will be present only if the request
3314 * is issued to the LLD. The request could be fetched from
3315 * block layer queue but could be waiting to be issued
3316 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3317 * Reset the timer in such cases to give LLD more time
3318 */
3319 if (!mq_rq) {
3320 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3321 return BLK_EH_RESET_TIMER;
3322 }
3323
3324 mrq = &mq_rq->cmdq_req.mrq;
3325 cmdq_req = &mq_rq->cmdq_req;
3326
3327 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303328
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303329 if (cmdq_req->cmdq_req_flags & DCMD)
3330 mrq->cmd->error = -ETIMEDOUT;
3331 else
3332 mrq->data->error = -ETIMEDOUT;
3333
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003334 BUG_ON(host->err_mrq != NULL);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303335 host->err_mrq = mrq;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303336
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003337 mmc_host_clk_release(mrq->host);
3338 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303339}
3340
Asutosh Das02e30862015-05-20 16:52:04 +05303341static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3342{
3343 int err;
3344 int retry = 0;
3345 int gen_err;
3346 u32 status;
3347
3348 struct mmc_host *host = mq->card->host;
3349 struct mmc_request *mrq = host->err_mrq;
3350 struct mmc_card *card = mq->card;
3351 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3352
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003353 pm_runtime_get_sync(&card->dev);
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003354 mmc_host_clk_hold(host);
3355 host->cmdq_ops->dumpstate(host);
3356 mmc_host_clk_release(host);
3357
Asutosh Das02e30862015-05-20 16:52:04 +05303358 err = mmc_cmdq_halt(host, true);
3359 if (err) {
3360 pr_err("halt: failed: %d\n", err);
3361 goto reset;
3362 }
3363
3364 /* RED error - Fatal: requires reset */
3365 if (mrq->cmdq_req->resp_err) {
3366 pr_crit("%s: Response error detected: Device in bad state\n",
3367 mmc_hostname(host));
3368 blk_end_request_all(mrq->req, -EIO);
3369 goto reset;
3370 }
3371
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003372 if (mrq->data && mrq->data->error) {
Asutosh Das02e30862015-05-20 16:52:04 +05303373 blk_end_request_all(mrq->req, mrq->data->error);
3374 for (; retry < MAX_RETRIES; retry++) {
3375 err = get_card_status(card, &status, 0);
3376 if (!err)
3377 break;
3378 }
3379
3380 if (err) {
3381 pr_err("%s: No response from card !!!\n",
3382 mmc_hostname(host));
3383 goto reset;
3384 }
3385
3386 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3387 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3388 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3389 mrq->req, &gen_err, &status);
3390 if (err) {
3391 pr_err("%s: error %d sending stop (%d) command\n",
3392 mrq->req->rq_disk->disk_name,
3393 err, status);
3394 goto reset;
3395 }
3396 }
3397
3398 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3399 goto reset;
3400 else
3401 goto unhalt;
3402 }
3403
3404 /* DCMD commands */
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003405 if (mrq->cmd && mrq->cmd->error) {
3406 /*
3407 * Notify completion for non flush commands like discard
3408 * that wait for DCMD finish.
3409 */
3410 if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
3411 complete(&mrq->completion);
3412 goto reset;
3413 }
3414 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303415 blk_end_request_all(mrq->req, mrq->cmd->error);
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003416 }
Asutosh Das02e30862015-05-20 16:52:04 +05303417
3418reset:
3419 spin_lock_irq(mq->queue->queue_lock);
3420 blk_queue_invalidate_tags(mrq->req->q);
3421 spin_unlock_irq(mq->queue->queue_lock);
3422 mmc_blk_cmdq_reset(host, true);
3423 goto out;
3424
3425unhalt:
3426 mmc_cmdq_halt(host, false);
3427
3428out:
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003429 host->err_mrq = NULL;
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003430 pm_runtime_mark_last_busy(&card->dev);
Subhash Jadavani987144c2015-08-12 17:53:21 -07003431 clear_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3432 wake_up(&ctx_info->wait);
Dov Levenglick2b678302015-07-01 14:24:20 +03003433 __mmc_put_card(card);
Asutosh Das02e30862015-05-20 16:52:04 +05303434}
3435
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003436/* invoked by block layer in softirq context */
3437void mmc_blk_cmdq_complete_rq(struct request *rq)
3438{
3439 struct mmc_queue_req *mq_rq = rq->special;
3440 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3441 struct mmc_host *host = mrq->host;
3442 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3443 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3444 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3445 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003446 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003447
3448 if (mrq->cmd && mrq->cmd->error)
3449 err = mrq->cmd->error;
3450 else if (mrq->data && mrq->data->error)
3451 err = mrq->data->error;
3452
Asutosh Das02e30862015-05-20 16:52:04 +05303453 /* clear pending request */
3454 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3455 &ctx_info->active_reqs));
3456
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003457 if (cmdq_req->cmdq_req_flags & DCMD)
3458 is_dcmd = true;
3459 else
3460 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3461 &ctx_info->data_active_reqs));
Talel Shenhar339a4e12015-06-29 10:50:19 +03003462
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003463 mmc_cmdq_post_req(host, mrq, err);
3464 if (err) {
3465 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3466 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303467 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3468 pr_err("%s: CQ in error state, ending current req: %d\n",
3469 __func__, err);
3470 blk_end_request_all(rq, err);
3471 } else {
3472 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3473 schedule_work(&mq->cmdq_err_work);
3474 }
3475 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003476 }
3477
Asutosh Das5238e022015-04-23 16:00:45 +05303478 if (cmdq_req->cmdq_req_flags & DCMD) {
3479 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303480 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303481 goto out;
3482 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003483
3484 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3485
Asutosh Das5238e022015-04-23 16:00:45 +05303486out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003487
3488 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Subhash Jadavani987144c2015-08-12 17:53:21 -07003489 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state))
3490 wake_up(&ctx_info->wait);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303491
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003492 mmc_put_card(host->card);
Talel Shenhar339a4e12015-06-29 10:50:19 +03003493 if (!ctx_info->active_reqs)
3494 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3495
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303496 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3497 complete(&mq->cmdq_shutdown_complete);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003498 return;
3499}
3500
3501/*
3502 * Complete reqs from block layer softirq context
3503 * Invoked in irq context
3504 */
3505void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3506{
3507 struct request *req = mrq->req;
3508
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303509 mmc_host_clk_release(mrq->host);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003510 blk_complete_request(req);
3511}
3512EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3513
Per Forlinee8a43a2011-07-01 18:55:33 +02003514static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003515{
3516 struct mmc_blk_data *md = mq->data;
3517 struct mmc_card *card = md->queue.card;
3518 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003519 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003520 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003521 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303522 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003523 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003524 const u8 packed_nr = 2;
3525 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003526#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3527 unsigned long waitfor = jiffies;
3528#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003529
3530 if (!rqc && !mq->mqrq_prev->req)
3531 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003532
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003533 if (rqc)
3534 reqs = mmc_blk_prep_packed_list(mq, rqc);
3535
Per Forlin54d49d72011-07-01 18:55:29 +02003536 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003537 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303538 /*
3539 * When 4KB native sector is enabled, only 8 blocks
3540 * multiple read or write is allowed
3541 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003542 if (mmc_large_sector(card) &&
3543 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303544 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3545 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003546 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303547 goto cmd_abort;
3548 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003549
3550 if (reqs >= packed_nr)
3551 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3552 card, mq);
3553 else
3554 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003555 areq = &mq->mqrq_cur->mmc_active;
3556 } else
3557 areq = NULL;
3558 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003559 if (!areq) {
3560 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003561 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003562 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003563 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003564
Per Forlinee8a43a2011-07-01 18:55:33 +02003565 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3566 brq = &mq_rq->brq;
3567 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003568 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003569 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003570
Per Forlind78d4a82011-07-01 18:55:30 +02003571 switch (status) {
3572 case MMC_BLK_SUCCESS:
3573 case MMC_BLK_PARTIAL:
3574 /*
3575 * A block was successfully transferred.
3576 */
Adrian Hunter67716322011-08-29 16:42:15 +03003577 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003578
Mark Salyzyn6904e432016-01-28 11:12:25 -08003579 mmc_blk_simulate_delay(mq, rqc, waitfor);
3580
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003581 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3582 ret = mmc_blk_end_packed_req(mq_rq);
3583 break;
3584 } else {
3585 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003586 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003587 }
3588
Adrian Hunter67716322011-08-29 16:42:15 +03003589 /*
3590 * If the blk_end_request function returns non-zero even
3591 * though all data has been transferred and no errors
3592 * were returned by the host controller, it's a bug.
3593 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003594 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303595 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003596 __func__, blk_rq_bytes(req),
3597 brq->data.bytes_xfered);
3598 rqc = NULL;
3599 goto cmd_abort;
3600 }
Per Forlind78d4a82011-07-01 18:55:30 +02003601 break;
3602 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003603 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003604 if (mmc_blk_reset(md, card->host, type))
3605 goto cmd_abort;
3606 if (!ret)
3607 goto start_new_req;
3608 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003609 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003610 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003611 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003612 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003613 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003614 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003615 if (!mmc_blk_reset(md, card->host, type) &&
3616 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003617 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003618 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003619 case MMC_BLK_DATA_ERR: {
3620 int err;
3621
3622 err = mmc_blk_reset(md, card->host, type);
3623 if (!err)
3624 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303625 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003626 }
3627 case MMC_BLK_ECC_ERR:
3628 if (brq->data.blocks > 1) {
3629 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003630 pr_warn("%s: retrying using single block read\n",
3631 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003632 disable_multi = 1;
3633 break;
3634 }
Per Forlind78d4a82011-07-01 18:55:30 +02003635 /*
3636 * After an error, we redo I/O one sector at a
3637 * time, so we only reach here after trying to
3638 * read a single sector.
3639 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303640 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003641 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003642 if (!ret)
3643 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003644 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303645 case MMC_BLK_NOMEDIUM:
3646 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003647 default:
3648 pr_err("%s: Unhandled return value (%d)",
3649 req->rq_disk->disk_name, status);
3650 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003651 }
3652
Per Forlinee8a43a2011-07-01 18:55:33 +02003653 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003654 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3655 if (!mq_rq->packed->retries)
3656 goto cmd_abort;
3657 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3658 mmc_start_req(card->host,
3659 &mq_rq->mmc_active, NULL);
3660 } else {
3661
3662 /*
3663 * In case of a incomplete request
3664 * prepare it again and resend.
3665 */
3666 mmc_blk_rw_rq_prep(mq_rq, card,
3667 disable_multi, mq);
3668 mmc_start_req(card->host,
3669 &mq_rq->mmc_active, NULL);
3670 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003671 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 } while (ret);
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return 1;
3676
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003677 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003678 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3679 mmc_blk_abort_packed_req(mq_rq);
3680 } else {
3681 if (mmc_card_removed(card))
3682 req->cmd_flags |= REQ_QUIET;
3683 while (ret)
3684 ret = blk_end_request(req, -EIO,
3685 blk_rq_cur_bytes(req));
3686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Per Forlinee8a43a2011-07-01 18:55:33 +02003688 start_new_req:
3689 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003690 if (mmc_card_removed(card)) {
3691 rqc->cmd_flags |= REQ_QUIET;
3692 blk_end_request_all(rqc, -EIO);
3693 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003694 /*
3695 * If current request is packed, it needs to put back.
3696 */
3697 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3698 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3699
Seungwon Jeon7a819022013-01-22 19:48:07 +09003700 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3701 mmc_start_req(card->host,
3702 &mq->mqrq_cur->mmc_active, NULL);
3703 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003704 }
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return 0;
3707}
3708
Asutosh Das8b594832015-04-23 09:55:43 +05303709static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3710 struct mmc_blk_data *md)
3711{
3712 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3713 struct mmc_host *host = card->host;
3714 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3715 u8 part_config = card->ext_csd.part_config;
3716
3717 if ((main_md->part_curr == md->part_type) &&
3718 (card->part_curr == md->part_type))
3719 return 0;
3720
3721 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3722 card->ext_csd.cmdq_support &&
3723 (md->flags & MMC_BLK_CMD_QUEUE)));
3724
3725 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3726 WARN_ON(mmc_cmdq_halt(host, true));
3727
3728 /* disable CQ mode in card */
3729 if (mmc_card_cmdq(card)) {
3730 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3731 EXT_CSD_CMDQ, 0,
3732 card->ext_csd.generic_cmd6_time));
3733 mmc_card_clr_cmdq(card);
3734 }
3735
3736 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3737 part_config |= md->part_type;
3738
3739 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3740 EXT_CSD_PART_CONFIG, part_config,
3741 card->ext_csd.part_time));
3742
3743 card->ext_csd.part_config = part_config;
3744 card->part_curr = md->part_type;
3745
3746 main_md->part_curr = md->part_type;
3747
3748 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3749 WARN_ON(mmc_cmdq_halt(host, false));
3750
3751 return 0;
3752}
3753
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003754static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3755{
3756 int ret;
3757 struct mmc_blk_data *md = mq->data;
3758 struct mmc_card *card = md->queue.card;
3759
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003760 mmc_get_card(card);
Maya Ereza2152152015-09-21 14:43:38 +03003761
3762 if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
3763 ret = mmc_cmdq_halt(card->host, true);
3764 if (ret)
3765 goto out;
3766 ret = mmc_stop_bkops(card);
3767 if (ret) {
3768 pr_err("%s: %s: mmc_stop_bkops failed %d\n",
3769 md->disk->disk_name, __func__, ret);
3770 goto out;
3771 }
3772 ret = mmc_cmdq_halt(card->host, false);
3773 if (ret)
3774 goto out;
3775 }
3776
Asutosh Das8b594832015-04-23 09:55:43 +05303777 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003778 if (ret) {
3779 pr_err("%s: %s: partition switch failed %d\n",
3780 md->disk->disk_name, __func__, ret);
Maya Ereza2152152015-09-21 14:43:38 +03003781 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003782 }
3783
Asutosh Das5238e022015-04-23 16:00:45 +05303784 if (req) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05303785 if (req_op(req) == REQ_OP_DISCARD) {
3786 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3787 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3788 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3789 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3790 else
3791 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3792 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303793 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303794 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303795 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303796 }
Asutosh Das5238e022015-04-23 16:00:45 +05303797 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003798
Maya Ereza2152152015-09-21 14:43:38 +03003799 return ret;
3800
3801out:
3802 if (req)
3803 blk_end_request_all(req, ret);
3804 mmc_put_card(card);
3805
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003806 return ret;
3807}
3808
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003809int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003810{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003811 int ret;
3812 struct mmc_blk_data *md = mq->data;
3813 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003814 struct mmc_host *host = card->host;
3815 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303816 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003817 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003818
Dov Levenglicka0296392015-06-24 19:51:58 +03003819 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003820 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003821 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003822
Dov Levenglicka0296392015-06-24 19:51:58 +03003823 if (mmc_card_doing_bkops(host->card)) {
3824 ret = mmc_stop_bkops(host->card);
3825 if (ret)
3826 goto out;
3827 }
3828 }
3829
Andrei Warkentin371a6892011-04-11 18:10:25 -05003830 ret = mmc_blk_part_switch(card, md);
3831 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003832 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303833 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003834 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003835 ret = 0;
3836 goto out;
3837 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003838
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003839 mmc_blk_write_packing_control(mq, req);
3840
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003841 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003842 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003843 /* complete ongoing async transfer before issuing discard */
3844 if (card->host->areq)
3845 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003846 ret = mmc_blk_issue_discard_rq(mq, req);
3847 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3848 /* complete ongoing async transfer before issuing secure erase*/
3849 if (card->host->areq)
3850 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003851 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3852 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3853 else
3854 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303855 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3856 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003857 /* complete ongoing async transfer before issuing flush */
3858 if (card->host->areq)
3859 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003860 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003861 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003862 if (!req && host->areq) {
3863 spin_lock_irqsave(&host->context_info.lock, flags);
3864 host->context_info.is_waiting_last_req = true;
3865 spin_unlock_irqrestore(&host->context_info.lock, flags);
3866 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003867 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003868 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003869
Andrei Warkentin371a6892011-04-11 18:10:25 -05003870out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003871 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3872 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003873 /*
3874 * Release host when there are no more requests
3875 * and after special request(discard, flush) is done.
3876 * In case sepecial request, there is no reentry to
3877 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3878 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003879 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003880 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Russell Kinga6f6c962006-01-03 22:38:44 +00003883static inline int mmc_blk_readonly(struct mmc_card *card)
3884{
3885 return mmc_card_readonly(card) ||
3886 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3887}
3888
Andrei Warkentin371a6892011-04-11 18:10:25 -05003889static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3890 struct device *parent,
3891 sector_t size,
3892 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003893 const char *subname,
3894 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
3896 struct mmc_blk_data *md;
3897 int devidx, ret;
3898
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003899again:
3900 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3901 return ERR_PTR(-ENOMEM);
3902
3903 spin_lock(&mmc_blk_lock);
3904 ret = ida_get_new(&mmc_blk_ida, &devidx);
3905 spin_unlock(&mmc_blk_lock);
3906
3907 if (ret == -EAGAIN)
3908 goto again;
3909 else if (ret)
3910 return ERR_PTR(ret);
3911
3912 if (devidx >= max_devices) {
3913 ret = -ENOSPC;
3914 goto out;
3915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003917 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003918 if (!md) {
3919 ret = -ENOMEM;
3920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003922
Johan Rudholmadd710e2011-12-02 08:51:06 +01003923 md->area_type = area_type;
3924
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003925 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003926 * Set the read-only status based on the supported commands
3927 * and the write protect switch.
3928 */
3929 md->read_only = mmc_blk_readonly(card);
3930
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003931 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003932 if (md->disk == NULL) {
3933 ret = -ENOMEM;
3934 goto err_kfree;
3935 }
3936
3937 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003938 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003939 md->usage = 1;
3940
Asutosh Das963469b2015-05-21 13:29:51 +05303941 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003942 if (ret)
3943 goto err_putdisk;
3944
Russell Kinga6f6c962006-01-03 22:38:44 +00003945 md->queue.data = md;
3946
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003947 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003948 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003949 md->disk->fops = &mmc_bdops;
3950 md->disk->private_data = md;
3951 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003952 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003953 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003954 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003955 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003956 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003957
3958 /*
3959 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3960 *
3961 * - be set for removable media with permanent block devices
3962 * - be unset for removable block devices with permanent media
3963 *
3964 * Since MMC block devices clearly fall under the second
3965 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3966 * should use the block device creation/destruction hotplug
3967 * messages to tell when the card is present.
3968 */
3969
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003970 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003971 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003972
Saugata Dasa5075eb2012-05-17 16:32:21 +05303973 if (mmc_card_mmc(card))
3974 blk_queue_logical_block_size(md->queue.queue,
3975 card->ext_csd.data_sector_size);
3976 else
3977 blk_queue_logical_block_size(md->queue.queue, 512);
3978
Andrei Warkentin371a6892011-04-11 18:10:25 -05003979 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003980
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003981 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003982 if ((mmc_card_mmc(card) &&
3983 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003984 (mmc_card_sd(card) &&
3985 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3986 md->flags |= MMC_BLK_CMD23;
3987 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003988
3989 if (mmc_card_mmc(card) &&
3990 md->flags & MMC_BLK_CMD23 &&
3991 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303992 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003993 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003994 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003995 }
3996
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003997 if (card->cmdq_init) {
3998 md->flags |= MMC_BLK_CMD_QUEUE;
3999 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
4000 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05304001 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05304002 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05304003 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004004 }
4005
4006 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09004007 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
4008 (md->flags & MMC_BLK_CMD23) &&
4009 card->ext_csd.packed_event_en) {
4010 if (!mmc_packed_init(&md->queue, card))
4011 md->flags |= MMC_BLK_PACKED_CMD;
4012 }
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00004015
4016 err_putdisk:
4017 put_disk(md->disk);
4018 err_kfree:
4019 kfree(md);
4020 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004021 spin_lock(&mmc_blk_lock);
4022 ida_remove(&mmc_blk_ida, devidx);
4023 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00004024 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Andrei Warkentin371a6892011-04-11 18:10:25 -05004027static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
4028{
4029 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004030
4031 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
4032 /*
4033 * The EXT_CSD sector count is in number or 512 byte
4034 * sectors.
4035 */
4036 size = card->ext_csd.sectors;
4037 } else {
4038 /*
4039 * The CSD capacity field is in units of read_blkbits.
4040 * set_capacity takes units of 512 bytes.
4041 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00004042 size = (typeof(sector_t))card->csd.capacity
4043 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004044 }
4045
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01004046 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004047 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004048}
4049
4050static int mmc_blk_alloc_part(struct mmc_card *card,
4051 struct mmc_blk_data *md,
4052 unsigned int part_type,
4053 sector_t size,
4054 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004055 const char *subname,
4056 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004057{
4058 char cap_str[10];
4059 struct mmc_blk_data *part_md;
4060
4061 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004062 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004063 if (IS_ERR(part_md))
4064 return PTR_ERR(part_md);
4065 part_md->part_type = part_type;
4066 list_add(&part_md->part, &md->part);
4067
James Bottomleyb9f28d82015-03-05 18:47:01 -08004068 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004069 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304070 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004071 part_md->disk->disk_name, mmc_card_id(card),
4072 mmc_card_name(card), part_md->part_type, cap_str);
4073 return 0;
4074}
4075
Namjae Jeone0c368d2011-10-06 23:41:38 +09004076/* MMC Physical partitions consist of two boot partitions and
4077 * up to four general purpose partitions.
4078 * For each partition enabled in EXT_CSD a block device will be allocatedi
4079 * to provide access to the partition.
4080 */
4081
Andrei Warkentin371a6892011-04-11 18:10:25 -05004082static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4083{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004084 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004085
4086 if (!mmc_card_mmc(card))
4087 return 0;
4088
Namjae Jeone0c368d2011-10-06 23:41:38 +09004089 for (idx = 0; idx < card->nr_parts; idx++) {
4090 if (card->part[idx].size) {
4091 ret = mmc_blk_alloc_part(card, md,
4092 card->part[idx].part_cfg,
4093 card->part[idx].size >> 9,
4094 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004095 card->part[idx].name,
4096 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004097 if (ret)
4098 return ret;
4099 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004100 }
4101
4102 return ret;
4103}
4104
Andrei Warkentin371a6892011-04-11 18:10:25 -05004105static void mmc_blk_remove_req(struct mmc_blk_data *md)
4106{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004107 struct mmc_card *card;
4108
Andrei Warkentin371a6892011-04-11 18:10:25 -05004109 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004110 /*
4111 * Flush remaining requests and free queues. It
4112 * is freeing the queue that stops new requests
4113 * from being accepted.
4114 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004115 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004116 mmc_cleanup_queue(&md->queue);
4117 if (md->flags & MMC_BLK_PACKED_CMD)
4118 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004119 if (md->flags & MMC_BLK_CMD_QUEUE)
4120 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004121 device_remove_file(disk_to_dev(md->disk),
4122 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004123 if (md->disk->flags & GENHD_FL_UP) {
4124 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004125 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4126 card->ext_csd.boot_ro_lockable)
4127 device_remove_file(disk_to_dev(md->disk),
4128 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004129#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4130 device_remove_file(disk_to_dev(md->disk),
4131 &dev_attr_max_write_speed);
4132 device_remove_file(disk_to_dev(md->disk),
4133 &dev_attr_max_read_speed);
4134 device_remove_file(disk_to_dev(md->disk),
4135 &dev_attr_cache_size);
4136#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004137
Andrei Warkentin371a6892011-04-11 18:10:25 -05004138 del_gendisk(md->disk);
4139 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004140 mmc_blk_put(md);
4141 }
4142}
4143
4144static void mmc_blk_remove_parts(struct mmc_card *card,
4145 struct mmc_blk_data *md)
4146{
4147 struct list_head *pos, *q;
4148 struct mmc_blk_data *part_md;
4149
4150 list_for_each_safe(pos, q, &md->part) {
4151 part_md = list_entry(pos, struct mmc_blk_data, part);
4152 list_del(pos);
4153 mmc_blk_remove_req(part_md);
4154 }
4155}
4156
4157static int mmc_add_disk(struct mmc_blk_data *md)
4158{
4159 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004160 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004161
Dan Williams307d8e62016-06-20 10:40:44 -07004162 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004163 md->force_ro.show = force_ro_show;
4164 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304165 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004166 md->force_ro.attr.name = "force_ro";
4167 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4168 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4169 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004170 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004171#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4172 atomic_set(&md->queue.max_write_speed, max_write_speed);
4173 ret = device_create_file(disk_to_dev(md->disk),
4174 &dev_attr_max_write_speed);
4175 if (ret)
4176 goto max_write_speed_fail;
4177 atomic_set(&md->queue.max_read_speed, max_read_speed);
4178 ret = device_create_file(disk_to_dev(md->disk),
4179 &dev_attr_max_read_speed);
4180 if (ret)
4181 goto max_read_speed_fail;
4182 atomic_set(&md->queue.cache_size, cache_size);
4183 atomic_long_set(&md->queue.cache_used, 0);
4184 md->queue.cache_jiffies = jiffies;
4185 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4186 if (ret)
4187 goto cache_size_fail;
4188#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004189
4190 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4191 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004192 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004193
4194 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4195 mode = S_IRUGO;
4196 else
4197 mode = S_IRUGO | S_IWUSR;
4198
4199 md->power_ro_lock.show = power_ro_lock_show;
4200 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004201 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004202 md->power_ro_lock.attr.mode = mode;
4203 md->power_ro_lock.attr.name =
4204 "ro_lock_until_next_power_on";
4205 ret = device_create_file(disk_to_dev(md->disk),
4206 &md->power_ro_lock);
4207 if (ret)
4208 goto power_ro_lock_fail;
4209 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004210
4211 md->num_wr_reqs_to_start_packing.show =
4212 num_wr_reqs_to_start_packing_show;
4213 md->num_wr_reqs_to_start_packing.store =
4214 num_wr_reqs_to_start_packing_store;
4215 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4216 md->num_wr_reqs_to_start_packing.attr.name =
4217 "num_wr_reqs_to_start_packing";
4218 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4219 ret = device_create_file(disk_to_dev(md->disk),
4220 &md->num_wr_reqs_to_start_packing);
4221 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004222 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004223
Maya Erez5a8dae12014-12-04 15:13:59 +02004224 md->no_pack_for_random.show = no_pack_for_random_show;
4225 md->no_pack_for_random.store = no_pack_for_random_store;
4226 sysfs_attr_init(&md->no_pack_for_random.attr);
4227 md->no_pack_for_random.attr.name = "no_pack_for_random";
4228 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4229 ret = device_create_file(disk_to_dev(md->disk),
4230 &md->no_pack_for_random);
4231 if (ret)
4232 goto no_pack_for_random_fails;
4233
Johan Rudholmadd710e2011-12-02 08:51:06 +01004234 return ret;
4235
Maya Erez5a8dae12014-12-04 15:13:59 +02004236no_pack_for_random_fails:
4237 device_remove_file(disk_to_dev(md->disk),
4238 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004239num_wr_reqs_to_start_packing_fail:
4240 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004241power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004242#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4243 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4244cache_size_fail:
4245 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4246max_read_speed_fail:
4247 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4248max_write_speed_fail:
4249#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004250 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4251force_ro_fail:
4252 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004253
4254 return ret;
4255}
4256
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004257static const struct mmc_fixup blk_fixups[] =
4258{
Chris Ballc59d4472011-11-11 22:01:43 -05004259 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4260 MMC_QUIRK_INAND_CMD38),
4261 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4262 MMC_QUIRK_INAND_CMD38),
4263 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4264 MMC_QUIRK_INAND_CMD38),
4265 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4266 MMC_QUIRK_INAND_CMD38),
4267 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4268 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004269
4270 /*
4271 * Some MMC cards experience performance degradation with CMD23
4272 * instead of CMD12-bounded multiblock transfers. For now we'll
4273 * black list what's bad...
4274 * - Certain Toshiba cards.
4275 *
4276 * N.B. This doesn't affect SD cards.
4277 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004278 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4279 MMC_QUIRK_BLK_NO_CMD23),
4280 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4281 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004282 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004283 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004284 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004285 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004286 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004287 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004288
4289 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004290 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004291 */
Chris Ballc59d4472011-11-11 22:01:43 -05004292 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004293 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004294 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4295 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004296
Ian Chen3550ccd2012-08-29 15:05:36 +09004297 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004298 * Some Samsung MMC cards need longer data read timeout than
4299 * indicated in CSD.
4300 */
4301 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4302 MMC_QUIRK_LONG_READ_TIME),
4303
4304 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004305 * On these Samsung MoviNAND parts, performing secure erase or
4306 * secure trim can result in unrecoverable corruption due to a
4307 * firmware bug.
4308 */
4309 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4310 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4311 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4312 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4313 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4314 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4315 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4317 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4319 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4321 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4323 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4325
Shawn Linb5b4ff02015-08-12 13:08:32 +08004326 /*
4327 * On Some Kingston eMMCs, performing trim can result in
4328 * unrecoverable data conrruption occasionally due to a firmware bug.
4329 */
4330 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4331 MMC_QUIRK_TRIM_BROKEN),
4332 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4333 MMC_QUIRK_TRIM_BROKEN),
4334
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004335 /* Some INAND MCP devices advertise incorrect timeout values */
4336 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4337 MMC_QUIRK_INAND_DATA_TIMEOUT),
4338
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004339 END_FIXUP
4340};
4341
Ulf Hansson96541ba2015-04-14 13:06:12 +02004342static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004344 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004345 char cap_str[10];
4346
Pierre Ossman912490d2005-05-21 10:27:02 +01004347 /*
4348 * Check that the card supports the command class(es) we need.
4349 */
4350 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return -ENODEV;
4352
Lukas Czerner5204d002014-06-18 13:18:07 +02004353 mmc_fixup_device(card, blk_fixups);
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 md = mmc_blk_alloc(card);
4356 if (IS_ERR(md))
4357 return PTR_ERR(md);
4358
James Bottomleyb9f28d82015-03-05 18:47:01 -08004359 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004360 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304361 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004363 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Andrei Warkentin371a6892011-04-11 18:10:25 -05004365 if (mmc_blk_alloc_parts(card, md))
4366 goto out;
4367
Ulf Hansson96541ba2015-04-14 13:06:12 +02004368 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004369
Andrei Warkentin371a6892011-04-11 18:10:25 -05004370 if (mmc_add_disk(md))
4371 goto out;
4372
4373 list_for_each_entry(part_md, &md->part, part) {
4374 if (mmc_add_disk(part_md))
4375 goto out;
4376 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004377
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004378 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004379 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4380 /*
4381 * If there is a runtime_idle function, it should take care of
4382 * suspending the card
4383 */
4384 if (card->host->bus_ops->runtime_idle)
4385 pm_runtime_dont_use_autosuspend(&card->dev);
4386 else
4387 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004388
4389 /*
4390 * Don't enable runtime PM for SD-combo cards here. Leave that
4391 * decision to be taken during the SDIO init sequence instead.
4392 */
4393 if (card->type != MMC_TYPE_SD_COMBO) {
4394 pm_runtime_set_active(&card->dev);
4395 pm_runtime_enable(&card->dev);
4396 }
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return 0;
4399
4400 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004401 mmc_blk_remove_parts(card, md);
4402 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
Ulf Hansson96541ba2015-04-14 13:06:12 +02004406static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004408 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Andrei Warkentin371a6892011-04-11 18:10:25 -05004410 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004411 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004412 mmc_claim_host(card->host);
4413 mmc_blk_part_switch(card, md);
4414 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004415 if (card->type != MMC_TYPE_SD_COMBO)
4416 pm_runtime_disable(&card->dev);
4417 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004418 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004419 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004422static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004424 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004425 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304426 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004429 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304430 if (rc)
4431 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004432 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004433 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304434 if (rc)
4435 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304438 goto out;
4439
4440 out_resume:
4441 mmc_queue_resume(&md->queue);
4442 list_for_each_entry(part_md, &md->part, part) {
4443 mmc_queue_resume(&part_md->queue);
4444 }
4445 out:
4446 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Ulf Hansson96541ba2015-04-14 13:06:12 +02004449static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004450{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004451 _mmc_blk_suspend(card, 1);
Krishna Kondaa7ea2dc2015-06-29 19:20:05 -07004452
4453 /* send power off notification */
4454 if (mmc_card_mmc(card))
4455 mmc_send_pon(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004456}
4457
Ulf Hansson0967edc2014-10-06 11:29:42 +02004458#ifdef CONFIG_PM_SLEEP
4459static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004460{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004461 struct mmc_card *card = mmc_dev_to_card(dev);
4462
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004463 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004464}
4465
Ulf Hansson0967edc2014-10-06 11:29:42 +02004466static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004468 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004469 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004472 /*
4473 * Resume involves the card going into idle state,
4474 * so current partition is always the main one.
4475 */
4476 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004478 list_for_each_entry(part_md, &md->part, part) {
4479 mmc_queue_resume(&part_md->queue);
4480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
4482 return 0;
4483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484#endif
4485
Ulf Hansson0967edc2014-10-06 11:29:42 +02004486static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4487
Ulf Hansson96541ba2015-04-14 13:06:12 +02004488static struct mmc_driver mmc_driver = {
4489 .drv = {
4490 .name = "mmcblk",
4491 .pm = &mmc_blk_pm_ops,
4492 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 .probe = mmc_blk_probe,
4494 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004495 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496};
4497
4498static int __init mmc_blk_init(void)
4499{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004500 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004502 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4503 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4504
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004505 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004506
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004507 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4508 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004511 res = mmc_register_driver(&mmc_driver);
4512 if (res)
4513 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004515 return 0;
4516 out2:
4517 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 out:
4519 return res;
4520}
4521
4522static void __exit mmc_blk_exit(void)
4523{
4524 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004525 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
4528module_init(mmc_blk_init);
4529module_exit(mmc_blk_exit);
4530
4531MODULE_LICENSE("GPL");
4532MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4533