blob: a030691da244e57680e87408bdc6422259966c1d [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
Ulf Hanssona2d10862013-12-16 14:37:26 +0100836 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300837 pr_warn("%s: %s - SANITIZE is not supported\n",
838 mmc_hostname(card->host), __func__);
839 err = -EOPNOTSUPP;
840 goto out;
841 }
842
843 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
844 mmc_hostname(card->host), __func__);
845
846 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
847 EXT_CSD_SANITIZE_START, 1,
848 MMC_SANITIZE_REQ_TIMEOUT);
849
850 if (err)
851 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
852 mmc_hostname(card->host), __func__, err);
853
854 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
855 __func__);
856out:
857 return err;
858}
859
Jon Huntera5f57742015-09-22 10:27:53 +0100860static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
861 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400862{
John Calixtocb87ea22011-04-26 18:56:29 -0400863 struct mmc_command cmd = {0};
864 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530865 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400866 struct scatterlist sg;
867 int err;
868
Jon Huntera5f57742015-09-22 10:27:53 +0100869 if (!card || !md || !idata)
870 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400871
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100872 cmd.opcode = idata->ic.opcode;
873 cmd.arg = idata->ic.arg;
874 cmd.flags = idata->ic.flags;
875
876 if (idata->buf_bytes) {
877 data.sg = &sg;
878 data.sg_len = 1;
879 data.blksz = idata->ic.blksz;
880 data.blocks = idata->ic.blocks;
881
882 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
883
884 if (idata->ic.write_flag)
885 data.flags = MMC_DATA_WRITE;
886 else
887 data.flags = MMC_DATA_READ;
888
889 /* data.flags must already be set before doing this. */
890 mmc_set_data_timeout(&data, card);
891
892 /* Allow overriding the timeout_ns for empirical tuning. */
893 if (idata->ic.data_timeout_ns)
894 data.timeout_ns = idata->ic.data_timeout_ns;
895
896 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
897 /*
898 * Pretend this is a data transfer and rely on the
899 * host driver to compute timeout. When all host
900 * drivers support cmd.cmd_timeout for R1B, this
901 * can be changed to:
902 *
903 * mrq.data = NULL;
904 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
905 */
906 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
907 }
908
909 mrq.data = &data;
910 }
911
912 mrq.cmd = &cmd;
913
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200914 err = mmc_blk_part_switch(card, md);
915 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100916 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200917
John Calixtocb87ea22011-04-26 18:56:29 -0400918 if (idata->ic.is_acmd) {
919 err = mmc_app_cmd(card->host, card);
920 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100921 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400922 }
923
Yaniv Gardia82e4842013-06-05 14:13:08 +0300924 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
925 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300926 err = ioctl_do_sanitize(card);
927
928 if (err)
929 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
930 __func__, err);
931
Jon Huntera5f57742015-09-22 10:27:53 +0100932 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300933 }
934
John Calixtocb87ea22011-04-26 18:56:29 -0400935 mmc_wait_for_req(card->host, &mrq);
936
937 if (cmd.error) {
938 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
939 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100940 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400941 }
942 if (data.error) {
943 dev_err(mmc_dev(card->host), "%s: data error %d\n",
944 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100945 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400946 }
947
948 /*
949 * According to the SD specs, some commands require a delay after
950 * issuing the command.
951 */
952 if (idata->ic.postsleep_min_us)
953 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
954
Jon Huntera5f57742015-09-22 10:27:53 +0100955 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400956
Krishna Kondae6711632014-12-04 15:20:57 +0200957 return err;
958}
959
960struct mmc_blk_ioc_rpmb_data {
961 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
962};
963
964static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
965 struct mmc_ioc_rpmb __user *user)
966{
967 struct mmc_blk_ioc_rpmb_data *idata;
968 int err, i;
969
970 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
971 if (!idata) {
972 err = -ENOMEM;
973 goto out;
974 }
975
976 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
977 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
978 if (IS_ERR(idata->data[i])) {
979 err = PTR_ERR(idata->data[i]);
980 goto copy_err;
981 }
982 }
983
984 return idata;
985
986copy_err:
987 while (--i >= 0) {
988 kfree(idata->data[i]->buf);
989 kfree(idata->data[i]);
990 }
991 kfree(idata);
992out:
993 return ERR_PTR(err);
994}
995
996static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
997 struct mmc_ioc_rpmb __user *ic_ptr)
998{
999 struct mmc_blk_ioc_rpmb_data *idata;
1000 struct mmc_blk_data *md;
1001 struct mmc_card *card;
1002 struct mmc_command cmd = {0};
1003 struct mmc_data data = {0};
1004 struct mmc_request mrq = {NULL};
1005 struct scatterlist sg;
1006 int err = 0, i = 0;
1007 u32 status = 0;
1008
1009 /* The caller must have CAP_SYS_RAWIO */
1010 if (!capable(CAP_SYS_RAWIO))
1011 return -EPERM;
1012
1013 md = mmc_blk_get(bdev->bd_disk);
1014 /* make sure this is a rpmb partition */
1015 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1016 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001017 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001018 }
1019
1020 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1021 if (IS_ERR(idata)) {
1022 err = PTR_ERR(idata);
1023 goto cmd_done;
1024 }
1025
1026 card = md->queue.card;
1027 if (IS_ERR(card)) {
1028 err = PTR_ERR(card);
1029 goto idata_free;
1030 }
1031
Maya Erezdd669562015-02-12 20:37:31 +02001032 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001033
1034 err = mmc_blk_part_switch(card, md);
1035 if (err)
1036 goto cmd_rel_host;
1037
1038 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1039 struct mmc_blk_ioc_data *curr_data;
1040 struct mmc_ioc_cmd *curr_cmd;
1041
1042 curr_data = idata->data[i];
1043 curr_cmd = &curr_data->ic;
1044 if (!curr_cmd->opcode)
1045 break;
1046
1047 cmd.opcode = curr_cmd->opcode;
1048 cmd.arg = curr_cmd->arg;
1049 cmd.flags = curr_cmd->flags;
1050
1051 if (curr_data->buf_bytes) {
1052 data.sg = &sg;
1053 data.sg_len = 1;
1054 data.blksz = curr_cmd->blksz;
1055 data.blocks = curr_cmd->blocks;
1056
1057 sg_init_one(data.sg, curr_data->buf,
1058 curr_data->buf_bytes);
1059
1060 if (curr_cmd->write_flag)
1061 data.flags = MMC_DATA_WRITE;
1062 else
1063 data.flags = MMC_DATA_READ;
1064
1065 /* data.flags must already be set before doing this. */
1066 mmc_set_data_timeout(&data, card);
1067
1068 /*
1069 * Allow overriding the timeout_ns for empirical tuning.
1070 */
1071 if (curr_cmd->data_timeout_ns)
1072 data.timeout_ns = curr_cmd->data_timeout_ns;
1073
1074 mrq.data = &data;
1075 }
1076
1077 mrq.cmd = &cmd;
1078
1079 err = mmc_set_blockcount(card, data.blocks,
1080 curr_cmd->write_flag & (1 << 31));
1081 if (err)
1082 goto cmd_rel_host;
1083
1084 mmc_wait_for_req(card->host, &mrq);
1085
1086 if (cmd.error) {
1087 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1088 __func__, cmd.error);
1089 err = cmd.error;
1090 goto cmd_rel_host;
1091 }
1092 if (data.error) {
1093 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1094 __func__, data.error);
1095 err = data.error;
1096 goto cmd_rel_host;
1097 }
1098
1099 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1100 sizeof(cmd.resp))) {
1101 err = -EFAULT;
1102 goto cmd_rel_host;
1103 }
1104
1105 if (!curr_cmd->write_flag) {
1106 if (copy_to_user((void __user *)(unsigned long)
1107 curr_cmd->data_ptr,
1108 curr_data->buf,
1109 curr_data->buf_bytes)) {
1110 err = -EFAULT;
1111 goto cmd_rel_host;
1112 }
1113 }
1114
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001115 /*
1116 * Ensure RPMB command has completed by polling CMD13
1117 * "Send Status".
1118 */
1119 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1120 if (err)
1121 dev_err(mmc_dev(card->host),
1122 "%s: Card Status=0x%08X, error %d\n",
1123 __func__, status, err);
1124 }
1125
Krishna Kondae6711632014-12-04 15:20:57 +02001126cmd_rel_host:
1127 mmc_put_card(card);
1128
1129idata_free:
1130 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1131 kfree(idata->data[i]->buf);
1132 kfree(idata->data[i]);
1133 }
1134 kfree(idata);
1135
1136cmd_done:
1137 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001138 return err;
1139}
1140
1141static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1142 struct mmc_ioc_cmd __user *ic_ptr)
1143{
1144 struct mmc_blk_ioc_data *idata;
1145 struct mmc_blk_data *md;
1146 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001147 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001148
Shawn Lin83c742c2016-03-16 18:15:47 +08001149 /*
1150 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1151 * whole block device, not on a partition. This prevents overspray
1152 * between sibling partitions.
1153 */
1154 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1155 return -EPERM;
1156
Jon Huntera5f57742015-09-22 10:27:53 +01001157 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301158 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001159 return PTR_ERR(idata);
1160
1161 md = mmc_blk_get(bdev->bd_disk);
1162 if (!md) {
1163 err = -EINVAL;
1164 goto cmd_err;
1165 }
1166
1167 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301168 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001169 err = PTR_ERR(card);
1170 goto cmd_done;
1171 }
1172
1173 mmc_get_card(card);
1174
Grant Grundlerb0934102015-09-23 18:30:33 -07001175 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001176
Adrian Hunter3c866562016-05-04 14:38:12 +03001177 /* Always switch back to main area after RPMB access */
1178 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1179 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1180
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001181 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001182
Grant Grundlerb0934102015-09-23 18:30:33 -07001183 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001184
John Calixtocb87ea22011-04-26 18:56:29 -04001185cmd_done:
1186 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001187cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001188 kfree(idata->buf);
1189 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001190 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001191}
1192
Jon Huntera5f57742015-09-22 10:27:53 +01001193static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1194 struct mmc_ioc_multi_cmd __user *user)
1195{
1196 struct mmc_blk_ioc_data **idata = NULL;
1197 struct mmc_ioc_cmd __user *cmds = user->cmds;
1198 struct mmc_card *card;
1199 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001200 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001201 __u64 num_of_cmds;
1202
Shawn Lin83c742c2016-03-16 18:15:47 +08001203 /*
1204 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1205 * whole block device, not on a partition. This prevents overspray
1206 * between sibling partitions.
1207 */
1208 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1209 return -EPERM;
1210
Jon Huntera5f57742015-09-22 10:27:53 +01001211 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1212 sizeof(num_of_cmds)))
1213 return -EFAULT;
1214
1215 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1216 return -EINVAL;
1217
1218 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1219 if (!idata)
1220 return -ENOMEM;
1221
1222 for (i = 0; i < num_of_cmds; i++) {
1223 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1224 if (IS_ERR(idata[i])) {
1225 err = PTR_ERR(idata[i]);
1226 num_of_cmds = i;
1227 goto cmd_err;
1228 }
1229 }
1230
1231 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001232 if (!md) {
1233 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001234 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001235 }
Jon Huntera5f57742015-09-22 10:27:53 +01001236
1237 card = md->queue.card;
1238 if (IS_ERR(card)) {
1239 err = PTR_ERR(card);
1240 goto cmd_done;
1241 }
1242
1243 mmc_get_card(card);
1244
Grant Grundlerb0934102015-09-23 18:30:33 -07001245 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1246 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001247
Adrian Hunter3c866562016-05-04 14:38:12 +03001248 /* Always switch back to main area after RPMB access */
1249 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1250 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1251
Jon Huntera5f57742015-09-22 10:27:53 +01001252 mmc_put_card(card);
1253
1254 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001255 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001256 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001257
1258cmd_done:
1259 mmc_blk_put(md);
1260cmd_err:
1261 for (i = 0; i < num_of_cmds; i++) {
1262 kfree(idata[i]->buf);
1263 kfree(idata[i]);
1264 }
1265 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001266 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001267}
1268
John Calixtocb87ea22011-04-26 18:56:29 -04001269static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1270 unsigned int cmd, unsigned long arg)
1271{
Jon Huntera5f57742015-09-22 10:27:53 +01001272 switch (cmd) {
1273 case MMC_IOC_CMD:
1274 return mmc_blk_ioctl_cmd(bdev,
1275 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001276 case MMC_IOC_RPMB_CMD:
1277 return mmc_blk_ioctl_rpmb_cmd(bdev,
1278 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001279 case MMC_IOC_MULTI_CMD:
1280 return mmc_blk_ioctl_multi_cmd(bdev,
1281 (struct mmc_ioc_multi_cmd __user *)arg);
1282 default:
1283 return -EINVAL;
1284 }
John Calixtocb87ea22011-04-26 18:56:29 -04001285}
1286
1287#ifdef CONFIG_COMPAT
1288static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1289 unsigned int cmd, unsigned long arg)
1290{
1291 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1292}
1293#endif
1294
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001295static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001296 .open = mmc_blk_open,
1297 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001298 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001300 .ioctl = mmc_blk_ioctl,
1301#ifdef CONFIG_COMPAT
1302 .compat_ioctl = mmc_blk_compat_ioctl,
1303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304};
1305
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001306static int mmc_blk_cmdq_switch(struct mmc_card *card,
1307 struct mmc_blk_data *md, bool enable)
1308{
1309 int ret = 0;
1310 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301311 struct mmc_host *host = card->host;
1312 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001313
1314 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1315 !card->ext_csd.cmdq_support ||
1316 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1317 (cmdq_mode == enable))
1318 return 0;
1319
1320 if (enable) {
1321 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1322 if (ret) {
1323 pr_err("%s: failed (%d) to set block-size to %d\n",
1324 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1325 goto out;
1326 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301327
1328 } else {
1329 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1330 ret = mmc_cmdq_halt(host, true);
1331 if (ret) {
1332 pr_err("%s: halt: failed: %d\n",
1333 mmc_hostname(host), ret);
1334 goto out;
1335 }
1336 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001337 }
1338
1339 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1340 EXT_CSD_CMDQ, enable,
1341 card->ext_csd.generic_cmd6_time);
1342 if (ret) {
1343 pr_err("%s: cmdq mode %sable failed %d\n",
1344 md->disk->disk_name, enable ? "en" : "dis", ret);
1345 goto out;
1346 }
1347
1348 if (enable)
1349 mmc_card_set_cmdq(card);
1350 else
1351 mmc_card_clr_cmdq(card);
1352out:
1353 return ret;
1354}
1355
Andrei Warkentin371a6892011-04-11 18:10:25 -05001356static inline int mmc_blk_part_switch(struct mmc_card *card,
1357 struct mmc_blk_data *md)
1358{
1359 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001360 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001361
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001362 if ((main_md->part_curr == md->part_type) &&
1363 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001364 return 0;
1365
1366 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001367 u8 part_config = card->ext_csd.part_config;
1368
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001369 if (md->part_type) {
1370 /* disable CQ mode for non-user data partitions */
1371 ret = mmc_blk_cmdq_switch(card, md, false);
1372 if (ret)
1373 return ret;
1374 }
1375
Adrian Hunter57da0c02016-05-04 14:38:13 +03001376 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1377 mmc_retune_pause(card->host);
1378
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001379 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1380 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001381
1382 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001383 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001384 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001385 if (ret) {
1386 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1387 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001388 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001389 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001390
1391 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001392 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001393
1394 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1395 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001396 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001397
1398 main_md->part_curr = md->part_type;
1399 return 0;
1400}
1401
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001402static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1403{
1404 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001405 u32 result;
1406 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001407
Venkatraman Sad5fd972011-08-25 00:30:50 +05301408 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001409 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001410 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001411
1412 struct scatterlist sg;
1413
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001414 cmd.opcode = MMC_APP_CMD;
1415 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001416 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001417
1418 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001419 if (err)
1420 return (u32)-1;
1421 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001422 return (u32)-1;
1423
1424 memset(&cmd, 0, sizeof(struct mmc_command));
1425
1426 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1427 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001428 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001429
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001430 data.blksz = 4;
1431 data.blocks = 1;
1432 data.flags = MMC_DATA_READ;
1433 data.sg = &sg;
1434 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301435 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001436
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001437 mrq.cmd = &cmd;
1438 mrq.data = &data;
1439
Ben Dooks051913d2009-06-08 23:33:57 +01001440 blocks = kmalloc(4, GFP_KERNEL);
1441 if (!blocks)
1442 return (u32)-1;
1443
1444 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001445
1446 mmc_wait_for_req(card->host, &mrq);
1447
Ben Dooks051913d2009-06-08 23:33:57 +01001448 result = ntohl(*blocks);
1449 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001450
Ben Dooks051913d2009-06-08 23:33:57 +01001451 if (cmd.error || data.error)
1452 result = (u32)-1;
1453
1454 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001455}
1456
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001457static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001458{
Chris Ball1278dba2011-04-13 23:40:30 -04001459 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001460 int err;
1461
Adrian Hunter504f1912008-10-16 12:55:25 +03001462 cmd.opcode = MMC_SEND_STATUS;
1463 if (!mmc_host_is_spi(card->host))
1464 cmd.arg = card->rca << 16;
1465 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001466 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1467 if (err == 0)
1468 *status = cmd.resp[0];
1469 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001470}
1471
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001472static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001473 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001474{
1475 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1476 int err = 0;
1477 u32 status;
1478
1479 do {
1480 err = get_card_status(card, &status, 5);
1481 if (err) {
1482 pr_err("%s: error %d requesting status\n",
1483 req->rq_disk->disk_name, err);
1484 return err;
1485 }
1486
1487 if (status & R1_ERROR) {
1488 pr_err("%s: %s: error sending status cmd, status %#x\n",
1489 req->rq_disk->disk_name, __func__, status);
1490 *gen_err = 1;
1491 }
1492
Ulf Hansson95a91292014-01-29 13:11:27 +01001493 /* We may rely on the host hw to handle busy detection.*/
1494 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1495 hw_busy_detect)
1496 break;
1497
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001498 /*
1499 * Timeout if the device never becomes ready for data and never
1500 * leaves the program state.
1501 */
1502 if (time_after(jiffies, timeout)) {
1503 pr_err("%s: Card stuck in programming state! %s %s\n",
1504 mmc_hostname(card->host),
1505 req->rq_disk->disk_name, __func__);
1506 return -ETIMEDOUT;
1507 }
1508
1509 /*
1510 * Some cards mishandle the status bits,
1511 * so make sure to check both the busy
1512 * indication and the card state.
1513 */
1514 } while (!(status & R1_READY_FOR_DATA) ||
1515 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1516
1517 return err;
1518}
1519
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001520static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1521 struct request *req, int *gen_err, u32 *stop_status)
1522{
1523 struct mmc_host *host = card->host;
1524 struct mmc_command cmd = {0};
1525 int err;
1526 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1527
1528 /*
1529 * Normally we use R1B responses for WRITE, but in cases where the host
1530 * has specified a max_busy_timeout we need to validate it. A failure
1531 * means we need to prevent the host from doing hw busy detection, which
1532 * is done by converting to a R1 response instead.
1533 */
1534 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1535 use_r1b_resp = false;
1536
1537 cmd.opcode = MMC_STOP_TRANSMISSION;
1538 if (use_r1b_resp) {
1539 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1540 cmd.busy_timeout = timeout_ms;
1541 } else {
1542 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1543 }
1544
1545 err = mmc_wait_for_cmd(host, &cmd, 5);
1546 if (err)
1547 return err;
1548
1549 *stop_status = cmd.resp[0];
1550
1551 /* No need to check card status in case of READ. */
1552 if (rq_data_dir(req) == READ)
1553 return 0;
1554
1555 if (!mmc_host_is_spi(host) &&
1556 (*stop_status & R1_ERROR)) {
1557 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1558 req->rq_disk->disk_name, __func__, *stop_status);
1559 *gen_err = 1;
1560 }
1561
1562 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1563}
1564
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301565#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001566#define ERR_RETRY 2
1567#define ERR_ABORT 1
1568#define ERR_CONTINUE 0
1569
1570static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1571 bool status_valid, u32 status)
1572{
1573 switch (error) {
1574 case -EILSEQ:
1575 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001576 pr_err_ratelimited(
1577 "%s: response CRC error sending %s command, card status %#x\n",
1578 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001579 name, status);
1580 return ERR_RETRY;
1581
1582 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001583 pr_err_ratelimited(
1584 "%s: timed out sending %s command, card status %#x\n",
1585 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001586
1587 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301588 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001589 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301590 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001591 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301592 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001593
1594 /*
1595 * If it was a r/w cmd crc error, or illegal command
1596 * (eg, issued in wrong state) then retry - we should
1597 * have corrected the state problem above.
1598 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301599 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001600 pr_err_ratelimited(
1601 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301602 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001603 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301604 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001605
1606 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001607 pr_err_ratelimited(
1608 "%s: not retrying timeout\n",
1609 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001610 return ERR_ABORT;
1611
1612 default:
1613 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001614 pr_err_ratelimited(
1615 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001616 req->rq_disk->disk_name, error, status);
1617 return ERR_ABORT;
1618 }
1619}
1620
1621/*
1622 * Initial r/w and stop cmd error recovery.
1623 * We don't know whether the card received the r/w cmd or not, so try to
1624 * restore things back to a sane state. Essentially, we do this as follows:
1625 * - Obtain card status. If the first attempt to obtain card status fails,
1626 * the status word will reflect the failed status cmd, not the failed
1627 * r/w cmd. If we fail to obtain card status, it suggests we can no
1628 * longer communicate with the card.
1629 * - Check the card state. If the card received the cmd but there was a
1630 * transient problem with the response, it might still be in a data transfer
1631 * mode. Try to send it a stop command. If this fails, we can't recover.
1632 * - If the r/w cmd failed due to a response CRC error, it was probably
1633 * transient, so retry the cmd.
1634 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1635 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1636 * illegal cmd, retry.
1637 * Otherwise we don't understand what happened, so abort.
1638 */
1639static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001640 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001641{
1642 bool prev_cmd_status_valid = true;
1643 u32 status, stop_status = 0;
1644 int err, retry;
1645
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301646 if (mmc_card_removed(card))
1647 return ERR_NOMEDIUM;
1648
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001649 /*
1650 * Try to get card status which indicates both the card state
1651 * and why there was no response. If the first attempt fails,
1652 * we can't be sure the returned status is for the r/w command.
1653 */
1654 for (retry = 2; retry >= 0; retry--) {
1655 err = get_card_status(card, &status, 0);
1656 if (!err)
1657 break;
1658
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001659 /* Re-tune if needed */
1660 mmc_retune_recheck(card->host);
1661
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 prev_cmd_status_valid = false;
1663 pr_err("%s: error %d sending status command, %sing\n",
1664 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1665 }
1666
1667 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301668 if (err) {
1669 /* Check if the card is removed */
1670 if (mmc_detect_card_removed(card->host))
1671 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001672 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301673 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001674
Adrian Hunter67716322011-08-29 16:42:15 +03001675 /* Flag ECC errors */
1676 if ((status & R1_CARD_ECC_FAILED) ||
1677 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1678 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1679 *ecc_err = 1;
1680
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001681 /* Flag General errors */
1682 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1683 if ((status & R1_ERROR) ||
1684 (brq->stop.resp[0] & R1_ERROR)) {
1685 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1686 req->rq_disk->disk_name, __func__,
1687 brq->stop.resp[0], status);
1688 *gen_err = 1;
1689 }
1690
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001691 /*
1692 * Check the current card state. If it is in some data transfer
1693 * mode, tell it to stop (and hopefully transition back to TRAN.)
1694 */
1695 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1696 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001697 err = send_stop(card,
1698 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1699 req, gen_err, &stop_status);
1700 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001701 pr_err("%s: error %d sending stop command\n",
1702 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001703 /*
1704 * If the stop cmd also timed out, the card is probably
1705 * not present, so abort. Other errors are bad news too.
1706 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001707 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001708 }
1709
Adrian Hunter67716322011-08-29 16:42:15 +03001710 if (stop_status & R1_CARD_ECC_FAILED)
1711 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001712 }
1713
1714 /* Check for set block count errors */
1715 if (brq->sbc.error)
1716 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1717 prev_cmd_status_valid, status);
1718
1719 /* Check for r/w command errors */
1720 if (brq->cmd.error)
1721 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1722 prev_cmd_status_valid, status);
1723
Adrian Hunter67716322011-08-29 16:42:15 +03001724 /* Data errors */
1725 if (!brq->stop.error)
1726 return ERR_CONTINUE;
1727
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001728 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001729 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 +01001730 req->rq_disk->disk_name, brq->stop.error,
1731 brq->cmd.resp[0], status);
1732
1733 /*
1734 * Subsitute in our own stop status as this will give the error
1735 * state which happened during the execution of the r/w command.
1736 */
1737 if (stop_status) {
1738 brq->stop.resp[0] = stop_status;
1739 brq->stop.error = 0;
1740 }
1741 return ERR_CONTINUE;
1742}
1743
Adrian Hunter67716322011-08-29 16:42:15 +03001744static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1745 int type)
1746{
1747 int err;
1748
1749 if (md->reset_done & type)
1750 return -EEXIST;
1751
1752 md->reset_done |= type;
1753 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301754 if (err && err != -EOPNOTSUPP) {
1755 /* We failed to reset so we need to abort the request */
1756 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1757 __func__, err);
1758 return -ENODEV;
1759 }
1760
Adrian Hunter67716322011-08-29 16:42:15 +03001761 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301762 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001763 struct mmc_blk_data *main_md =
1764 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001765 int part_err;
1766
1767 main_md->part_curr = main_md->part_type;
1768 part_err = mmc_blk_part_switch(host->card, md);
1769 if (part_err) {
1770 /*
1771 * We have failed to get back into the correct
1772 * partition, so we need to abort the whole request.
1773 */
1774 return -ENODEV;
1775 }
1776 }
1777 return err;
1778}
1779
1780static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1781{
1782 md->reset_done &= ~type;
1783}
1784
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001785int mmc_access_rpmb(struct mmc_queue *mq)
1786{
1787 struct mmc_blk_data *md = mq->data;
1788 /*
1789 * If this is a RPMB partition access, return ture
1790 */
1791 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1792 return true;
1793
1794 return false;
1795}
1796
Sahitya Tummala9433a132015-06-09 09:38:36 +05301797static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1798 struct request *req)
1799{
1800 struct mmc_blk_data *md = mq->data;
1801 struct mmc_card *card = md->queue.card;
1802 struct mmc_host *host = card->host;
1803 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1804 struct mmc_cmdq_req *cmdq_req;
1805 struct mmc_queue_req *active_mqrq;
1806
1807 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1808 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1809
1810 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1811
1812 active_mqrq = &mq->mqrq_cmdq[req->tag];
1813 active_mqrq->req = req;
1814
1815 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1816 cmdq_req->cmdq_req_flags |= QBR;
1817 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1818 cmdq_req->tag = req->tag;
1819 return cmdq_req;
1820}
1821
1822static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1823 struct request *req)
1824{
1825 struct mmc_blk_data *md = mq->data;
1826 struct mmc_card *card = md->queue.card;
1827 struct mmc_cmdq_req *cmdq_req = NULL;
1828 struct mmc_host *host = card->host;
1829 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1830 unsigned int from, nr, arg;
1831 int err = 0;
1832
1833 if (!mmc_can_erase(card)) {
1834 err = -EOPNOTSUPP;
1835 goto out;
1836 }
1837
1838 from = blk_rq_pos(req);
1839 nr = blk_rq_sectors(req);
1840
1841 if (mmc_can_discard(card))
1842 arg = MMC_DISCARD_ARG;
1843 else if (mmc_can_trim(card))
1844 arg = MMC_TRIM_ARG;
1845 else
1846 arg = MMC_ERASE_ARG;
1847
1848 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1849 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1850 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1851 EXT_CSD_CMD_SET_NORMAL,
1852 INAND_CMD38_ARG_EXT_CSD,
1853 arg == MMC_TRIM_ARG ?
1854 INAND_CMD38_ARG_TRIM :
1855 INAND_CMD38_ARG_ERASE,
1856 0, true, false);
1857 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1858 if (err)
1859 goto clear_dcmd;
1860 }
1861 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1862clear_dcmd:
1863 /* clear pending request */
1864 if (cmdq_req) {
1865 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1866 &ctx_info->active_reqs));
1867 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1868 }
1869out:
1870 blk_end_request(req, err, blk_rq_bytes(req));
1871
1872 if (test_and_clear_bit(0, &ctx_info->req_starved))
1873 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001874 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301875 return err ? 1 : 0;
1876}
1877
Adrian Hunterbd788c92010-08-11 14:17:47 -07001878static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1879{
1880 struct mmc_blk_data *md = mq->data;
1881 struct mmc_card *card = md->queue.card;
1882 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001883 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001884
Adrian Hunterbd788c92010-08-11 14:17:47 -07001885 if (!mmc_can_erase(card)) {
1886 err = -EOPNOTSUPP;
1887 goto out;
1888 }
1889
1890 from = blk_rq_pos(req);
1891 nr = blk_rq_sectors(req);
1892
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001893 if (mmc_can_discard(card))
1894 arg = MMC_DISCARD_ARG;
1895 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001896 arg = MMC_TRIM_ARG;
1897 else
1898 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001899retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001900 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1901 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1902 INAND_CMD38_ARG_EXT_CSD,
1903 arg == MMC_TRIM_ARG ?
1904 INAND_CMD38_ARG_TRIM :
1905 INAND_CMD38_ARG_ERASE,
1906 0);
1907 if (err)
1908 goto out;
1909 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001910 err = mmc_erase(card, from, nr, arg);
1911out:
Adrian Hunter67716322011-08-29 16:42:15 +03001912 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1913 goto retry;
1914 if (!err)
1915 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301916 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001917
Adrian Hunterbd788c92010-08-11 14:17:47 -07001918 return err ? 0 : 1;
1919}
1920
Sahitya Tummala9433a132015-06-09 09:38:36 +05301921static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1922 struct request *req)
1923{
1924 struct mmc_blk_data *md = mq->data;
1925 struct mmc_card *card = md->queue.card;
1926 struct mmc_cmdq_req *cmdq_req = NULL;
1927 unsigned int from, nr, arg;
1928 struct mmc_host *host = card->host;
1929 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1930 int err = 0;
1931
1932 if (!(mmc_can_secure_erase_trim(card))) {
1933 err = -EOPNOTSUPP;
1934 goto out;
1935 }
1936
1937 from = blk_rq_pos(req);
1938 nr = blk_rq_sectors(req);
1939
1940 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1941 arg = MMC_SECURE_TRIM1_ARG;
1942 else
1943 arg = MMC_SECURE_ERASE_ARG;
1944
1945 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1947 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1948 EXT_CSD_CMD_SET_NORMAL,
1949 INAND_CMD38_ARG_EXT_CSD,
1950 arg == MMC_SECURE_TRIM1_ARG ?
1951 INAND_CMD38_ARG_SECTRIM1 :
1952 INAND_CMD38_ARG_SECERASE,
1953 0, true, false);
1954 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1955 if (err)
1956 goto clear_dcmd;
1957 }
1958
1959 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1960 if (err)
1961 goto clear_dcmd;
1962
1963 if (arg == MMC_SECURE_TRIM1_ARG) {
1964 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1965 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1966 EXT_CSD_CMD_SET_NORMAL,
1967 INAND_CMD38_ARG_EXT_CSD,
1968 INAND_CMD38_ARG_SECTRIM2,
1969 0, true, false);
1970 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1971 if (err)
1972 goto clear_dcmd;
1973 }
1974
1975 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
1976 MMC_SECURE_TRIM2_ARG);
1977 }
1978clear_dcmd:
1979 /* clear pending request */
1980 if (cmdq_req) {
1981 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1982 &ctx_info->active_reqs));
1983 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1984 }
1985out:
1986 blk_end_request(req, err, blk_rq_bytes(req));
1987
1988 if (test_and_clear_bit(0, &ctx_info->req_starved))
1989 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001990 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301991 return err ? 1 : 0;
1992}
1993
Adrian Hunter49804542010-08-11 14:17:50 -07001994static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1995 struct request *req)
1996{
1997 struct mmc_blk_data *md = mq->data;
1998 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001999 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002000 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002001
Maya Erez775a9362013-04-18 15:41:55 +03002002 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002003 err = -EOPNOTSUPP;
2004 goto out;
2005 }
2006
2007 from = blk_rq_pos(req);
2008 nr = blk_rq_sectors(req);
2009
Maya Erez775a9362013-04-18 15:41:55 +03002010 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2011 arg = MMC_SECURE_TRIM1_ARG;
2012 else
2013 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002014
Adrian Hunter67716322011-08-29 16:42:15 +03002015retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002016 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2017 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2018 INAND_CMD38_ARG_EXT_CSD,
2019 arg == MMC_SECURE_TRIM1_ARG ?
2020 INAND_CMD38_ARG_SECTRIM1 :
2021 INAND_CMD38_ARG_SECERASE,
2022 0);
2023 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002024 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002025 }
Adrian Hunter28302812012-04-05 14:45:48 +03002026
Adrian Hunter49804542010-08-11 14:17:50 -07002027 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002028 if (err == -EIO)
2029 goto out_retry;
2030 if (err)
2031 goto out;
2032
2033 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002034 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2035 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2036 INAND_CMD38_ARG_EXT_CSD,
2037 INAND_CMD38_ARG_SECTRIM2,
2038 0);
2039 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002040 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002041 }
Adrian Hunter28302812012-04-05 14:45:48 +03002042
Adrian Hunter49804542010-08-11 14:17:50 -07002043 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002044 if (err == -EIO)
2045 goto out_retry;
2046 if (err)
2047 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002048 }
Adrian Hunter28302812012-04-05 14:45:48 +03002049
Adrian Hunter28302812012-04-05 14:45:48 +03002050out_retry:
2051 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002052 goto retry;
2053 if (!err)
2054 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002055out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302056 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002057
Adrian Hunter49804542010-08-11 14:17:50 -07002058 return err ? 0 : 1;
2059}
2060
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002061static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2062{
2063 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002064 struct mmc_card *card = md->queue.card;
2065 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002066
Sahitya Tummala61868a42015-05-28 16:54:19 +05302067 if (!req)
2068 return 0;
2069
2070 if (req->cmd_flags & REQ_BARRIER) {
2071 /*
2072 * If eMMC cache flush policy is set to 1, then the device
2073 * shall flush the requests in First-In-First-Out (FIFO) order.
2074 * In this case, as per spec, the host must not send any cache
2075 * barrier requests as they are redundant and add unnecessary
2076 * overhead to both device and host.
2077 */
2078 if (card->ext_csd.cache_flush_policy & 1)
2079 goto end_req;
2080
2081 /*
2082 * In case barrier is not supported or enabled in the device,
2083 * use flush as a fallback option.
2084 */
2085 ret = mmc_cache_barrier(card);
2086 if (ret)
2087 ret = mmc_flush_cache(card);
2088 } else if (req_op(req) == REQ_OP_FLUSH) {
2089 ret = mmc_flush_cache(card);
2090 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002091 if (ret == -ENODEV) {
2092 pr_err("%s: %s: restart mmc card",
2093 req->rq_disk->disk_name, __func__);
2094 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2095 pr_err("%s: %s: fail to restart mmc",
2096 req->rq_disk->disk_name, __func__);
2097 else
2098 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2099 }
2100
2101 if (ret) {
2102 pr_err("%s: %s: notify flush error to upper layers",
2103 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002104 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002105 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002106
Mark Salyzyn6904e432016-01-28 11:12:25 -08002107#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2108 else if (atomic_read(&mq->cache_size)) {
2109 long used = mmc_blk_cache_used(mq, jiffies);
2110
2111 if (used) {
2112 int speed = atomic_read(&mq->max_write_speed);
2113
2114 if (speed_valid(speed)) {
2115 unsigned long msecs = jiffies_to_msecs(
2116 size_and_speed_to_jiffies(
2117 used, speed));
2118 if (msecs)
2119 msleep(msecs);
2120 }
2121 }
2122 }
2123#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302124end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302125 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002126
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002127 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002128}
2129
2130/*
2131 * Reformat current write as a reliable write, supporting
2132 * both legacy and the enhanced reliable write MMC cards.
2133 * In each transfer we'll handle only as much as a single
2134 * reliable write can handle, thus finish the request in
2135 * partial completions.
2136 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002137static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2138 struct mmc_card *card,
2139 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002140{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002141 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2142 /* Legacy mode imposes restrictions on transfers. */
2143 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2144 brq->data.blocks = 1;
2145
2146 if (brq->data.blocks > card->ext_csd.rel_sectors)
2147 brq->data.blocks = card->ext_csd.rel_sectors;
2148 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2149 brq->data.blocks = 1;
2150 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002151}
2152
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002153#define CMD_ERRORS \
2154 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2155 R1_ADDRESS_ERROR | /* Misaligned address */ \
2156 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2157 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2158 R1_CC_ERROR | /* Card controller error */ \
2159 R1_ERROR) /* General/unknown error */
2160
Per Forlinee8a43a2011-07-01 18:55:33 +02002161static int mmc_blk_err_check(struct mmc_card *card,
2162 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002163{
Per Forlinee8a43a2011-07-01 18:55:33 +02002164 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2165 mmc_active);
2166 struct mmc_blk_request *brq = &mq_mrq->brq;
2167 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002168 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002169 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002170
2171 /*
2172 * sbc.error indicates a problem with the set block count
2173 * command. No data will have been transferred.
2174 *
2175 * cmd.error indicates a problem with the r/w command. No
2176 * data will have been transferred.
2177 *
2178 * stop.error indicates a problem with the stop command. Data
2179 * may have been transferred, or may still be transferring.
2180 */
Adrian Hunter67716322011-08-29 16:42:15 +03002181 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2182 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002183 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002184 case ERR_RETRY:
2185 return MMC_BLK_RETRY;
2186 case ERR_ABORT:
2187 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302188 case ERR_NOMEDIUM:
2189 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002190 case ERR_CONTINUE:
2191 break;
2192 }
2193 }
2194
2195 /*
2196 * Check for errors relating to the execution of the
2197 * initial command - such as address errors. No data
2198 * has been transferred.
2199 */
2200 if (brq->cmd.resp[0] & CMD_ERRORS) {
2201 pr_err("%s: r/w command failed, status = %#x\n",
2202 req->rq_disk->disk_name, brq->cmd.resp[0]);
2203 return MMC_BLK_ABORT;
2204 }
2205
2206 /*
2207 * Everything else is either success, or a data error of some
2208 * kind. If it was a write, we may have transitioned to
2209 * program mode, which we have to wait for it to complete.
2210 */
2211 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002212 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002213
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002214 /* Check stop command response */
2215 if (brq->stop.resp[0] & R1_ERROR) {
2216 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2217 req->rq_disk->disk_name, __func__,
2218 brq->stop.resp[0]);
2219 gen_err = 1;
2220 }
2221
Ulf Hansson95a91292014-01-29 13:11:27 +01002222 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2223 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002224 if (err)
2225 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002226 }
2227
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002228 /* if general error occurs, retry the write operation. */
2229 if (gen_err) {
2230 pr_warn("%s: retrying write for general error\n",
2231 req->rq_disk->disk_name);
2232 return MMC_BLK_RETRY;
2233 }
2234
Per Forlind78d4a82011-07-01 18:55:30 +02002235 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002236 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002237 pr_debug("%s: retrying because a re-tune was needed\n",
2238 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002239 brq->retune_retry_done = 1;
2240 return MMC_BLK_RETRY;
2241 }
Per Forlind78d4a82011-07-01 18:55:30 +02002242 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2243 req->rq_disk->disk_name, brq->data.error,
2244 (unsigned)blk_rq_pos(req),
2245 (unsigned)blk_rq_sectors(req),
2246 brq->cmd.resp[0], brq->stop.resp[0]);
2247
2248 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002249 if (ecc_err)
2250 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002251 return MMC_BLK_DATA_ERR;
2252 } else {
2253 return MMC_BLK_CMD_ERR;
2254 }
2255 }
2256
Adrian Hunter67716322011-08-29 16:42:15 +03002257 if (!brq->data.bytes_xfered)
2258 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002259
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002260 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2261 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2262 return MMC_BLK_PARTIAL;
2263 else
2264 return MMC_BLK_SUCCESS;
2265 }
2266
Adrian Hunter67716322011-08-29 16:42:15 +03002267 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2268 return MMC_BLK_PARTIAL;
2269
2270 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002271}
2272
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002273static int mmc_blk_packed_err_check(struct mmc_card *card,
2274 struct mmc_async_req *areq)
2275{
2276 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2277 mmc_active);
2278 struct request *req = mq_rq->req;
2279 struct mmc_packed *packed = mq_rq->packed;
2280 int err, check, status;
2281 u8 *ext_csd;
2282
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002283 packed->retries--;
2284 check = mmc_blk_err_check(card, areq);
2285 err = get_card_status(card, &status, 0);
2286 if (err) {
2287 pr_err("%s: error %d sending status command\n",
2288 req->rq_disk->disk_name, err);
2289 return MMC_BLK_ABORT;
2290 }
2291
2292 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002293 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002294 if (err) {
2295 pr_err("%s: error %d sending ext_csd\n",
2296 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002297 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002298 }
2299
2300 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2301 EXT_CSD_PACKED_FAILURE) &&
2302 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2303 EXT_CSD_PACKED_GENERIC_ERROR)) {
2304 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2305 EXT_CSD_PACKED_INDEXED_ERROR) {
2306 packed->idx_failure =
2307 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2308 check = MMC_BLK_PARTIAL;
2309 }
2310 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2311 "failure index: %d\n",
2312 req->rq_disk->disk_name, packed->nr_entries,
2313 packed->blocks, packed->idx_failure);
2314 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002315 kfree(ext_csd);
2316 }
2317
2318 return check;
2319}
2320
Per Forlin54d49d72011-07-01 18:55:29 +02002321static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2322 struct mmc_card *card,
2323 int disable_multi,
2324 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Per Forlin54d49d72011-07-01 18:55:29 +02002326 u32 readcmd, writecmd;
2327 struct mmc_blk_request *brq = &mqrq->brq;
2328 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302330 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002332 /*
2333 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002334 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002335 */
Luca Porziod3df0462015-11-06 15:12:26 +00002336 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002337 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002338 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002339
Per Forlin54d49d72011-07-01 18:55:29 +02002340 memset(brq, 0, sizeof(struct mmc_blk_request));
2341 brq->mrq.cmd = &brq->cmd;
2342 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Per Forlin54d49d72011-07-01 18:55:29 +02002344 brq->cmd.arg = blk_rq_pos(req);
2345 if (!mmc_card_blockaddr(card))
2346 brq->cmd.arg <<= 9;
2347 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2348 brq->data.blksz = 512;
2349 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2350 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002351 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Asutosh Dasf0665412012-07-27 18:10:19 +05302353 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002354 /*
2355 * The block layer doesn't support all sector count
2356 * restrictions, so we need to be prepared for too big
2357 * requests.
2358 */
2359 if (brq->data.blocks > card->host->max_blk_count)
2360 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002362 if (brq->data.blocks > 1) {
2363 /*
2364 * After a read error, we redo the request one sector
2365 * at a time in order to accurately determine which
2366 * sectors can be read successfully.
2367 */
2368 if (disable_multi)
2369 brq->data.blocks = 1;
2370
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002371 /*
2372 * Some controllers have HW issues while operating
2373 * in multiple I/O mode
2374 */
2375 if (card->host->ops->multi_io_quirk)
2376 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2377 (rq_data_dir(req) == READ) ?
2378 MMC_DATA_READ : MMC_DATA_WRITE,
2379 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002380 }
Per Forlin54d49d72011-07-01 18:55:29 +02002381
2382 if (brq->data.blocks > 1 || do_rel_wr) {
2383 /* SPI multiblock writes terminate using a special
2384 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002385 */
Per Forlin54d49d72011-07-01 18:55:29 +02002386 if (!mmc_host_is_spi(card->host) ||
2387 rq_data_dir(req) == READ)
2388 brq->mrq.stop = &brq->stop;
2389 readcmd = MMC_READ_MULTIPLE_BLOCK;
2390 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2391 } else {
2392 brq->mrq.stop = NULL;
2393 readcmd = MMC_READ_SINGLE_BLOCK;
2394 writecmd = MMC_WRITE_BLOCK;
2395 }
2396 if (rq_data_dir(req) == READ) {
2397 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002398 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002399 if (brq->mrq.stop)
2400 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2401 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002402 } else {
2403 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002404 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002405 if (brq->mrq.stop)
2406 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2407 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002408 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002409
Per Forlin54d49d72011-07-01 18:55:29 +02002410 if (do_rel_wr)
2411 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002412
Per Forlin54d49d72011-07-01 18:55:29 +02002413 /*
Saugata Das42659002011-12-21 13:09:17 +05302414 * Data tag is used only during writing meta data to speed
2415 * up write and any subsequent read of this meta data
2416 */
2417 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2418 (req->cmd_flags & REQ_META) &&
2419 (rq_data_dir(req) == WRITE) &&
2420 ((brq->data.blocks * brq->data.blksz) >=
2421 card->ext_csd.data_tag_unit_size);
2422
2423 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002424 * Pre-defined multi-block transfers are preferable to
2425 * open ended-ones (and necessary for reliable writes).
2426 * However, it is not sufficient to just send CMD23,
2427 * and avoid the final CMD12, as on an error condition
2428 * CMD12 (stop) needs to be sent anyway. This, coupled
2429 * with Auto-CMD23 enhancements provided by some
2430 * hosts, means that the complexity of dealing
2431 * with this is best left to the host. If CMD23 is
2432 * supported by card and host, we'll fill sbc in and let
2433 * the host deal with handling it correctly. This means
2434 * that for hosts that don't expose MMC_CAP_CMD23, no
2435 * change of behavior will be observed.
2436 *
2437 * N.B: Some MMC cards experience perf degradation.
2438 * We'll avoid using CMD23-bounded multiblock writes for
2439 * these, while retaining features like reliable writes.
2440 */
Saugata Das42659002011-12-21 13:09:17 +05302441 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2442 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2443 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002444 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2445 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302446 (do_rel_wr ? (1 << 31) : 0) |
2447 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002448 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2449 brq->mrq.sbc = &brq->sbc;
2450 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002451
Per Forlin54d49d72011-07-01 18:55:29 +02002452 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002453
Per Forlin54d49d72011-07-01 18:55:29 +02002454 brq->data.sg = mqrq->sg;
2455 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002456
Per Forlin54d49d72011-07-01 18:55:29 +02002457 /*
2458 * Adjust the sg list so it is the same size as the
2459 * request.
2460 */
2461 if (brq->data.blocks != blk_rq_sectors(req)) {
2462 int i, data_size = brq->data.blocks << 9;
2463 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002464
Per Forlin54d49d72011-07-01 18:55:29 +02002465 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2466 data_size -= sg->length;
2467 if (data_size <= 0) {
2468 sg->length += data_size;
2469 i++;
2470 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002471 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002472 }
Per Forlin54d49d72011-07-01 18:55:29 +02002473 brq->data.sg_len = i;
2474 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002475
Per Forlinee8a43a2011-07-01 18:55:33 +02002476 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302477 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002478 mqrq->mmc_active.err_check = mmc_blk_err_check;
2479
Per Forlin54d49d72011-07-01 18:55:29 +02002480 mmc_queue_bounce_pre(mqrq);
2481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002483static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2484 struct mmc_card *card)
2485{
2486 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2487 unsigned int max_seg_sz = queue_max_segment_size(q);
2488 unsigned int len, nr_segs = 0;
2489
2490 do {
2491 len = min(hdr_sz, max_seg_sz);
2492 hdr_sz -= len;
2493 nr_segs++;
2494 } while (hdr_sz);
2495
2496 return nr_segs;
2497}
2498
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002499/**
2500 * mmc_blk_disable_wr_packing() - disables packing mode
2501 * @mq: MMC queue.
2502 *
2503 */
2504void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2505{
2506 if (mq) {
2507 mq->wr_packing_enabled = false;
2508 mq->num_of_potential_packed_wr_reqs = 0;
2509 }
2510}
2511EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2512
Lee Susman841fd132013-04-23 17:59:26 +03002513static int get_packed_trigger(int potential, struct mmc_card *card,
2514 struct request *req, int curr_trigger)
2515{
2516 static int num_mean_elements = 1;
2517 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2518 unsigned int trigger = curr_trigger;
2519 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2520
2521 /* scale down the upper bound to 75% */
2522 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2523
2524 /*
2525 * since the most common calls for this function are with small
2526 * potential write values and since we don't want these calls to affect
2527 * the packed trigger, set a lower bound and ignore calls with
2528 * potential lower than that bound
2529 */
2530 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2531 return trigger;
2532
2533 /*
2534 * this is to prevent integer overflow in the following calculation:
2535 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2536 */
2537 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2538 num_mean_elements = 1;
2539 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2540 }
2541
2542 /*
2543 * get next mean value based on previous mean value and current
2544 * potential packed writes. Calculation is as follows:
2545 * mean_pot[i+1] =
2546 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2547 */
2548 mean_potential *= num_mean_elements;
2549 /*
2550 * add num_mean_elements so that the division of two integers doesn't
2551 * lower mean_potential too much
2552 */
2553 if (potential > mean_potential)
2554 mean_potential += num_mean_elements;
2555 mean_potential += potential;
2556 /* this is for gaining more precision when dividing two integers */
2557 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2558 /* this completes the mean calculation */
2559 mean_potential /= ++num_mean_elements;
2560 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2561
2562 /*
2563 * if current potential packed writes is greater than the mean potential
2564 * then the heuristic is that the following workload will contain many
2565 * write requests, therefore we lower the packed trigger. In the
2566 * opposite case we want to increase the trigger in order to get less
2567 * packing events.
2568 */
2569 if (potential >= mean_potential)
2570 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2571 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2572 else
2573 trigger = (trigger >= pckd_trgr_upper_bound) ?
2574 pckd_trgr_upper_bound : trigger + 1;
2575
2576 /*
2577 * an urgent read request indicates a packed list being interrupted
2578 * by this read, therefore we aim for less packing, hence the trigger
2579 * gets increased
2580 */
2581 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2582 trigger += PCKD_TRGR_URGENT_PENALTY;
2583
2584 return trigger;
2585}
2586
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002587static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2588 struct request *req)
2589{
2590 struct mmc_host *host = mq->card->host;
2591 int data_dir;
2592
2593 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2594 return;
2595
Maya Erez8e2b3c32012-12-02 13:27:15 +02002596 /* Support for the write packing on eMMC 4.5 or later */
2597 if (mq->card->ext_csd.rev <= 5)
2598 return;
2599
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002600 /*
2601 * In case the packing control is not supported by the host, it should
2602 * not have an effect on the write packing. Therefore we have to enable
2603 * the write packing
2604 */
2605 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2606 mq->wr_packing_enabled = true;
2607 return;
2608 }
2609
2610 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2611 if (mq->num_of_potential_packed_wr_reqs >
2612 mq->num_wr_reqs_to_start_packing)
2613 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002614 mq->num_wr_reqs_to_start_packing =
2615 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2616 mq->card, req,
2617 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002618 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002619 return;
2620 }
2621
2622 data_dir = rq_data_dir(req);
2623
2624 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002625 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002626 mq->num_wr_reqs_to_start_packing =
2627 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2628 mq->card, req,
2629 mq->num_wr_reqs_to_start_packing);
2630 mq->num_of_potential_packed_wr_reqs = 0;
2631 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002632 return;
2633 } else if (data_dir == WRITE) {
2634 mq->num_of_potential_packed_wr_reqs++;
2635 }
2636
2637 if (mq->num_of_potential_packed_wr_reqs >
2638 mq->num_wr_reqs_to_start_packing)
2639 mq->wr_packing_enabled = true;
2640}
2641
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002642struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2643{
2644 if (!card)
2645 return NULL;
2646
2647 return &card->wr_pack_stats;
2648}
2649EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2650
2651void mmc_blk_init_packed_statistics(struct mmc_card *card)
2652{
2653 int max_num_of_packed_reqs = 0;
2654
2655 if (!card || !card->wr_pack_stats.packing_events)
2656 return;
2657
2658 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2659
2660 spin_lock(&card->wr_pack_stats.lock);
2661 memset(card->wr_pack_stats.packing_events, 0,
2662 (max_num_of_packed_reqs + 1) *
2663 sizeof(*card->wr_pack_stats.packing_events));
2664 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2665 sizeof(card->wr_pack_stats.pack_stop_reason));
2666 card->wr_pack_stats.enabled = true;
2667 spin_unlock(&card->wr_pack_stats.lock);
2668}
2669EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2670
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002671static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2672{
2673 struct request_queue *q = mq->queue;
2674 struct mmc_card *card = mq->card;
2675 struct request *cur = req, *next = NULL;
2676 struct mmc_blk_data *md = mq->data;
2677 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2678 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2679 unsigned int req_sectors = 0, phys_segments = 0;
2680 unsigned int max_blk_count, max_phys_segs;
2681 bool put_back = true;
2682 u8 max_packed_rw = 0;
2683 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002684 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002685
Shawn Lin96e52da2016-08-26 08:49:55 +08002686 /*
2687 * We don't need to check packed for any further
2688 * operation of packed stuff as we set MMC_PACKED_NONE
2689 * and return zero for reqs if geting null packed. Also
2690 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2691 * it again when removing blk req.
2692 */
2693 if (!mqrq->packed) {
2694 md->flags &= (~MMC_BLK_PACKED_CMD);
2695 goto no_packed;
2696 }
2697
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002698 if (!(md->flags & MMC_BLK_PACKED_CMD))
2699 goto no_packed;
2700
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002701 if (!mq->wr_packing_enabled)
2702 goto no_packed;
2703
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002704 if ((rq_data_dir(cur) == WRITE) &&
2705 mmc_host_packed_wr(card->host))
2706 max_packed_rw = card->ext_csd.max_packed_writes;
2707
2708 if (max_packed_rw == 0)
2709 goto no_packed;
2710
2711 if (mmc_req_rel_wr(cur) &&
2712 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2713 goto no_packed;
2714
2715 if (mmc_large_sector(card) &&
2716 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2717 goto no_packed;
2718
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002719 if (cur->cmd_flags & REQ_FUA)
2720 goto no_packed;
2721
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002722 mmc_blk_clear_packed(mqrq);
2723
2724 max_blk_count = min(card->host->max_blk_count,
2725 card->host->max_req_size >> 9);
2726 if (unlikely(max_blk_count > 0xffff))
2727 max_blk_count = 0xffff;
2728
2729 max_phys_segs = queue_max_segments(q);
2730 req_sectors += blk_rq_sectors(cur);
2731 phys_segments += cur->nr_phys_segments;
2732
2733 if (rq_data_dir(cur) == WRITE) {
2734 req_sectors += mmc_large_sector(card) ? 8 : 1;
2735 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2736 }
2737
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002738 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002739 do {
2740 if (reqs >= max_packed_rw - 1) {
2741 put_back = false;
2742 break;
2743 }
2744
2745 spin_lock_irq(q->queue_lock);
2746 next = blk_fetch_request(q);
2747 spin_unlock_irq(q->queue_lock);
2748 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002749 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002750 put_back = false;
2751 break;
2752 }
2753
2754 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002755 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2756 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002757 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002758 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002759
Mike Christie3a5e02c2016-06-05 14:32:23 -05002760 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002761 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002762 req_op(next) == REQ_OP_FLUSH) {
2763 if (req_op(next) != REQ_OP_SECURE_ERASE)
2764 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002765 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002766 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002767
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002768 if (next->cmd_flags & REQ_FUA) {
2769 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2770 break;
2771 }
2772
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002773 if (rq_data_dir(cur) != rq_data_dir(next)) {
2774 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002775 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002776 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002777
2778 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002779 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2780 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002781 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002782 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002783
2784 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002785 if (req_sectors > max_blk_count) {
2786 if (stats->enabled)
2787 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002788 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002789 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002790
2791 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002792 if (phys_segments > max_phys_segs) {
2793 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002794 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002795 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796
Maya Erez5a8dae12014-12-04 15:13:59 +02002797 if (mq->no_pack_for_random) {
2798 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2799 blk_rq_pos(next)) {
2800 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2801 put_back = 1;
2802 break;
2803 }
2804 }
2805
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002806 if (rq_data_dir(next) == WRITE)
2807 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002808 list_add_tail(&next->queuelist, &mqrq->packed->list);
2809 cur = next;
2810 reqs++;
2811 } while (1);
2812
2813 if (put_back) {
2814 spin_lock_irq(q->queue_lock);
2815 blk_requeue_request(q, next);
2816 spin_unlock_irq(q->queue_lock);
2817 }
2818
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002819 if (stats->enabled) {
2820 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2821 stats->packing_events[reqs + 1]++;
2822 if (reqs + 1 == max_packed_rw)
2823 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2824 }
2825
2826 spin_unlock(&stats->lock);
2827
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002828 if (reqs > 0) {
2829 list_add(&req->queuelist, &mqrq->packed->list);
2830 mqrq->packed->nr_entries = ++reqs;
2831 mqrq->packed->retries = reqs;
2832 return reqs;
2833 }
2834
2835no_packed:
2836 mqrq->cmd_type = MMC_PACKED_NONE;
2837 return 0;
2838}
2839
2840static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2841 struct mmc_card *card,
2842 struct mmc_queue *mq)
2843{
2844 struct mmc_blk_request *brq = &mqrq->brq;
2845 struct request *req = mqrq->req;
2846 struct request *prq;
2847 struct mmc_blk_data *md = mq->data;
2848 struct mmc_packed *packed = mqrq->packed;
2849 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002850 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002851 u8 hdr_blocks;
2852 u8 i = 1;
2853
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002854 mqrq->cmd_type = MMC_PACKED_WRITE;
2855 packed->blocks = 0;
2856 packed->idx_failure = MMC_PACKED_NR_IDX;
2857
2858 packed_cmd_hdr = packed->cmd_hdr;
2859 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002860 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2861 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002862 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2863
2864 /*
2865 * Argument for each entry of packed group
2866 */
2867 list_for_each_entry(prq, &packed->list, queuelist) {
2868 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2869 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2870 (prq->cmd_flags & REQ_META) &&
2871 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002872 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002873 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002874 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002875 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2876 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002877 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002878 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002879 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002880 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002881 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002882 packed->blocks += blk_rq_sectors(prq);
2883 i++;
2884 }
2885
2886 memset(brq, 0, sizeof(struct mmc_blk_request));
2887 brq->mrq.cmd = &brq->cmd;
2888 brq->mrq.data = &brq->data;
2889 brq->mrq.sbc = &brq->sbc;
2890 brq->mrq.stop = &brq->stop;
2891
2892 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2893 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2894 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2895
2896 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2897 brq->cmd.arg = blk_rq_pos(req);
2898 if (!mmc_card_blockaddr(card))
2899 brq->cmd.arg <<= 9;
2900 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2901
2902 brq->data.blksz = 512;
2903 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002904 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302905 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002906
2907 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2908 brq->stop.arg = 0;
2909 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2910
2911 mmc_set_data_timeout(&brq->data, card);
2912
2913 brq->data.sg = mqrq->sg;
2914 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2915
2916 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002917
2918 /*
2919 * This is intended for packed commands tests usage - in case these
2920 * functions are not in use the respective pointers are NULL
2921 */
2922 if (mq->err_check_fn)
2923 mqrq->mmc_active.err_check = mq->err_check_fn;
2924 else
2925 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2926
2927 if (mq->packed_test_fn)
2928 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002929
2930 mmc_queue_bounce_pre(mqrq);
2931}
2932
Adrian Hunter67716322011-08-29 16:42:15 +03002933static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2934 struct mmc_blk_request *brq, struct request *req,
2935 int ret)
2936{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002937 struct mmc_queue_req *mq_rq;
2938 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2939
Adrian Hunter67716322011-08-29 16:42:15 +03002940 /*
2941 * If this is an SD card and we're writing, we can first
2942 * mark the known good sectors as ok.
2943 *
2944 * If the card is not SD, we can still ok written sectors
2945 * as reported by the controller (which might be less than
2946 * the real number of written sectors, but never more).
2947 */
2948 if (mmc_card_sd(card)) {
2949 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302950 if (!brq->data.fault_injected) {
2951 blocks = mmc_sd_num_wr_blocks(card);
2952 if (blocks != (u32)-1)
2953 ret = blk_end_request(req, 0, blocks << 9);
2954 } else
2955 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002956 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002957 if (!mmc_packed_cmd(mq_rq->cmd_type))
2958 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002959 }
2960 return ret;
2961}
2962
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002963static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2964{
2965 struct request *prq;
2966 struct mmc_packed *packed = mq_rq->packed;
2967 int idx = packed->idx_failure, i = 0;
2968 int ret = 0;
2969
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002970 while (!list_empty(&packed->list)) {
2971 prq = list_entry_rq(packed->list.next);
2972 if (idx == i) {
2973 /* retry from error index */
2974 packed->nr_entries -= idx;
2975 mq_rq->req = prq;
2976 ret = 1;
2977
2978 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2979 list_del_init(&prq->queuelist);
2980 mmc_blk_clear_packed(mq_rq);
2981 }
2982 return ret;
2983 }
2984 list_del_init(&prq->queuelist);
2985 blk_end_request(prq, 0, blk_rq_bytes(prq));
2986 i++;
2987 }
2988
2989 mmc_blk_clear_packed(mq_rq);
2990 return ret;
2991}
2992
2993static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2994{
2995 struct request *prq;
2996 struct mmc_packed *packed = mq_rq->packed;
2997
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002998 while (!list_empty(&packed->list)) {
2999 prq = list_entry_rq(packed->list.next);
3000 list_del_init(&prq->queuelist);
3001 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3002 }
3003
3004 mmc_blk_clear_packed(mq_rq);
3005}
3006
3007static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3008 struct mmc_queue_req *mq_rq)
3009{
3010 struct request *prq;
3011 struct request_queue *q = mq->queue;
3012 struct mmc_packed *packed = mq_rq->packed;
3013
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003014 while (!list_empty(&packed->list)) {
3015 prq = list_entry_rq(packed->list.prev);
3016 if (prq->queuelist.prev != &packed->list) {
3017 list_del_init(&prq->queuelist);
3018 spin_lock_irq(q->queue_lock);
3019 blk_requeue_request(mq->queue, prq);
3020 spin_unlock_irq(q->queue_lock);
3021 } else {
3022 list_del_init(&prq->queuelist);
3023 }
3024 }
3025
3026 mmc_blk_clear_packed(mq_rq);
3027}
3028
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003029static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3030 struct mmc_cmdq_req *cmdq_req)
3031{
3032 struct mmc_request *mrq = &cmdq_req->mrq;
3033
3034 mrq->done = mmc_blk_cmdq_req_done;
3035 return mmc_cmdq_start_req(host, cmdq_req);
3036}
3037
Asutosh Das5238e022015-04-23 16:00:45 +05303038/* prepare for non-data commands */
3039static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3040 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3041{
3042 struct request *req = mqrq->req;
3043 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3044
3045 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3046
3047 cmdq_req->mrq.data = NULL;
3048 cmdq_req->cmd_flags = req->cmd_flags;
3049 cmdq_req->mrq.req = mqrq->req;
3050 req->special = mqrq;
3051 cmdq_req->cmdq_req_flags |= DCMD;
3052 cmdq_req->mrq.cmdq_req = cmdq_req;
3053
3054 return &mqrq->cmdq_req;
3055}
3056
3057
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003058#define IS_RT_CLASS_REQ(x) \
3059 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3060
3061static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3062 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3063{
3064 struct mmc_card *card = mq->card;
3065 struct request *req = mqrq->req;
3066 struct mmc_blk_data *md = mq->data;
3067 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3068 bool do_data_tag;
3069 bool read_dir = (rq_data_dir(req) == READ);
3070 bool prio = IS_RT_CLASS_REQ(req);
3071 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3072
3073 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3074
3075 cmdq_rq->tag = req->tag;
3076 if (read_dir) {
3077 cmdq_rq->cmdq_req_flags |= DIR;
3078 cmdq_rq->data.flags = MMC_DATA_READ;
3079 } else {
3080 cmdq_rq->data.flags = MMC_DATA_WRITE;
3081 }
3082 if (prio)
3083 cmdq_rq->cmdq_req_flags |= PRIO;
3084
3085 if (do_rel_wr)
3086 cmdq_rq->cmdq_req_flags |= REL_WR;
3087
3088 cmdq_rq->data.blocks = blk_rq_sectors(req);
3089 cmdq_rq->blk_addr = blk_rq_pos(req);
3090 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3091
3092 mmc_set_data_timeout(&cmdq_rq->data, card);
3093
3094 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3095 (req->cmd_flags & REQ_META) &&
3096 (rq_data_dir(req) == WRITE) &&
3097 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3098 card->ext_csd.data_tag_unit_size);
3099 if (do_data_tag)
3100 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3101 cmdq_rq->data.sg = mqrq->sg;
3102 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3103
3104 /*
3105 * Adjust the sg list so it is the same size as the
3106 * request.
3107 */
3108 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3109 cmdq_rq->data.blocks = card->host->max_blk_count;
3110
3111 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3112 int i, data_size = cmdq_rq->data.blocks << 9;
3113 struct scatterlist *sg;
3114
3115 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3116 data_size -= sg->length;
3117 if (data_size <= 0) {
3118 sg->length += data_size;
3119 i++;
3120 break;
3121 }
3122 }
3123 cmdq_rq->data.sg_len = i;
3124 }
3125
3126 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3127 mqrq->cmdq_req.mrq.req = mqrq->req;
3128 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3129 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3130 mqrq->req->special = mqrq;
3131
3132 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",
3133 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3134 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3135 cmdq_rq, cmdq_rq->blk_addr,
3136 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3137
3138 return &mqrq->cmdq_req;
3139}
3140
3141static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3142{
3143 struct mmc_queue_req *active_mqrq;
3144 struct mmc_card *card = mq->card;
3145 struct mmc_host *host = card->host;
3146 struct mmc_cmdq_req *mc_rq;
3147 int ret = 0;
3148
Talel Shenhar339a4e12015-06-29 10:50:19 +03003149 if (host->clk_scaling.enable) {
3150 mmc_deferred_scaling(host);
3151 mmc_cmdq_clk_scaling_start_busy(host, true);
3152 BUG_ON(test_and_set_bit(req->tag,
3153 &host->cmdq_ctx.data_active_reqs));
3154 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003155 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
3156 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3157
3158 active_mqrq = &mq->mqrq_cmdq[req->tag];
3159 active_mqrq->req = req;
3160
3161 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3162
3163 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3164 return ret;
3165}
3166
Asutosh Das5238e022015-04-23 16:00:45 +05303167/*
3168 * Issues a flush (dcmd) request
3169 */
3170int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3171{
3172 int err;
3173 struct mmc_queue_req *active_mqrq;
3174 struct mmc_card *card = mq->card;
3175 struct mmc_host *host;
3176 struct mmc_cmdq_req *cmdq_req;
3177 struct mmc_cmdq_context_info *ctx_info;
3178
3179 BUG_ON(!card);
3180 host = card->host;
3181 BUG_ON(!host);
3182 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3183 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3184
3185 ctx_info = &host->cmdq_ctx;
3186
3187 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3188
3189 active_mqrq = &mq->mqrq_cmdq[req->tag];
3190 active_mqrq->req = req;
3191
3192 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3193 cmdq_req->cmdq_req_flags |= QBR;
3194 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3195 cmdq_req->tag = req->tag;
3196
3197 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3198 if (err) {
3199 pr_err("%s: failed (%d) preparing flush req\n",
3200 mmc_hostname(host), err);
3201 return err;
3202 }
3203 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3204 return err;
3205}
3206EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3207
Asutosh Das02e30862015-05-20 16:52:04 +05303208static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3209{
3210 if (!host->cmdq_ops->reset)
3211 return;
3212
3213 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3214 if (mmc_cmdq_halt(host, true)) {
3215 pr_err("%s: halt failed\n", mmc_hostname(host));
3216 goto reset;
3217 }
3218 }
3219
3220 if (clear_all)
3221 mmc_cmdq_discard_queue(host, 0);
3222reset:
3223 mmc_hw_reset(host);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303224 mmc_host_clk_hold(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303225 host->cmdq_ops->reset(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303226 mmc_host_clk_release(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303227 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3228}
3229
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303230static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3231{
3232 int err;
3233 struct mmc_card *card = mq->card;
3234 struct mmc_host *host = card->host;
3235
3236 err = mmc_cmdq_halt(host, true);
3237 if (err) {
3238 pr_err("%s: halt: failed: %d\n", __func__, err);
3239 return;
3240 }
3241
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003242 mmc_get_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303243 /* disable CQ mode in card */
3244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3245 EXT_CSD_CMDQ, 0,
3246 card->ext_csd.generic_cmd6_time);
3247 if (err) {
3248 pr_err("%s: failed to switch card to legacy mode: %d\n",
3249 __func__, err);
3250 goto out;
3251 } else {
3252 host->card->cmdq_init = false;
3253 }
3254out:
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003255 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303256}
3257
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303258static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3259{
3260 struct mmc_queue *mq = req->q->queuedata;
3261 struct mmc_host *host = mq->card->host;
3262 struct mmc_queue_req *mq_rq = req->special;
3263 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3264 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3265
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303266 if (cmdq_req->cmdq_req_flags & DCMD)
3267 mrq->cmd->error = -ETIMEDOUT;
3268 else
3269 mrq->data->error = -ETIMEDOUT;
3270
3271 host->err_mrq = mrq;
3272 mrq->done(mrq);
3273
3274 return BLK_EH_NOT_HANDLED;
3275}
3276
Asutosh Das02e30862015-05-20 16:52:04 +05303277static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3278{
3279 int err;
3280 int retry = 0;
3281 int gen_err;
3282 u32 status;
3283
3284 struct mmc_host *host = mq->card->host;
3285 struct mmc_request *mrq = host->err_mrq;
3286 struct mmc_card *card = mq->card;
3287 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3288
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003289 pm_runtime_get_sync(&card->dev);
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003290 mmc_host_clk_hold(host);
3291 host->cmdq_ops->dumpstate(host);
3292 mmc_host_clk_release(host);
3293
Asutosh Das02e30862015-05-20 16:52:04 +05303294 err = mmc_cmdq_halt(host, true);
3295 if (err) {
3296 pr_err("halt: failed: %d\n", err);
3297 goto reset;
3298 }
3299
3300 /* RED error - Fatal: requires reset */
3301 if (mrq->cmdq_req->resp_err) {
3302 pr_crit("%s: Response error detected: Device in bad state\n",
3303 mmc_hostname(host));
3304 blk_end_request_all(mrq->req, -EIO);
3305 goto reset;
3306 }
3307
3308 if (mrq->data->error) {
3309 blk_end_request_all(mrq->req, mrq->data->error);
3310 for (; retry < MAX_RETRIES; retry++) {
3311 err = get_card_status(card, &status, 0);
3312 if (!err)
3313 break;
3314 }
3315
3316 if (err) {
3317 pr_err("%s: No response from card !!!\n",
3318 mmc_hostname(host));
3319 goto reset;
3320 }
3321
3322 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3323 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3324 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3325 mrq->req, &gen_err, &status);
3326 if (err) {
3327 pr_err("%s: error %d sending stop (%d) command\n",
3328 mrq->req->rq_disk->disk_name,
3329 err, status);
3330 goto reset;
3331 }
3332 }
3333
3334 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3335 goto reset;
3336 else
3337 goto unhalt;
3338 }
3339
3340 /* DCMD commands */
3341 if (mrq->cmd->error)
3342 blk_end_request_all(mrq->req, mrq->cmd->error);
3343
3344reset:
3345 spin_lock_irq(mq->queue->queue_lock);
3346 blk_queue_invalidate_tags(mrq->req->q);
3347 spin_unlock_irq(mq->queue->queue_lock);
3348 mmc_blk_cmdq_reset(host, true);
3349 goto out;
3350
3351unhalt:
3352 mmc_cmdq_halt(host, false);
3353
3354out:
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003355 pm_runtime_mark_last_busy(&card->dev);
3356 pm_runtime_put_autosuspend(&card->dev);
3357
Asutosh Das02e30862015-05-20 16:52:04 +05303358 if (test_and_clear_bit(0, &ctx_info->req_starved))
3359 blk_run_queue(mrq->req->q);
3360}
3361
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003362/* invoked by block layer in softirq context */
3363void mmc_blk_cmdq_complete_rq(struct request *rq)
3364{
3365 struct mmc_queue_req *mq_rq = rq->special;
3366 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3367 struct mmc_host *host = mrq->host;
3368 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3369 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3370 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3371 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003372 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003373
3374 if (mrq->cmd && mrq->cmd->error)
3375 err = mrq->cmd->error;
3376 else if (mrq->data && mrq->data->error)
3377 err = mrq->data->error;
3378
Asutosh Das02e30862015-05-20 16:52:04 +05303379 /* clear pending request */
3380 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3381 &ctx_info->active_reqs));
3382
Talel Shenhar339a4e12015-06-29 10:50:19 +03003383 if (host->clk_scaling.enable) {
3384 if (cmdq_req->cmdq_req_flags & DCMD)
3385 is_dcmd = true;
3386 else
3387 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3388 &ctx_info->data_active_reqs));
3389 }
3390
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003391 mmc_cmdq_post_req(host, mrq, err);
3392 if (err) {
3393 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3394 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303395 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3396 pr_err("%s: CQ in error state, ending current req: %d\n",
3397 __func__, err);
3398 blk_end_request_all(rq, err);
3399 } else {
3400 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3401 schedule_work(&mq->cmdq_err_work);
3402 }
3403 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003404 }
3405
Asutosh Das5238e022015-04-23 16:00:45 +05303406 if (cmdq_req->cmdq_req_flags & DCMD) {
3407 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303408 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303409 goto out;
3410 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003411
3412 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3413
Asutosh Das5238e022015-04-23 16:00:45 +05303414out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003415
3416 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Asutosh Das02e30862015-05-20 16:52:04 +05303417 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
3418 test_and_clear_bit(0, &ctx_info->req_starved))
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003419 blk_run_queue(mq->queue);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303420
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003421 mmc_put_card(host->card);
Talel Shenhar339a4e12015-06-29 10:50:19 +03003422 if (!ctx_info->active_reqs)
3423 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3424
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303425 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3426 complete(&mq->cmdq_shutdown_complete);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003427 return;
3428}
3429
3430/*
3431 * Complete reqs from block layer softirq context
3432 * Invoked in irq context
3433 */
3434void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3435{
3436 struct request *req = mrq->req;
3437
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303438 mmc_host_clk_release(mrq->host);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003439 blk_complete_request(req);
3440}
3441EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3442
Per Forlinee8a43a2011-07-01 18:55:33 +02003443static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003444{
3445 struct mmc_blk_data *md = mq->data;
3446 struct mmc_card *card = md->queue.card;
3447 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003448 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003449 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003450 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303451 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003452 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003453 const u8 packed_nr = 2;
3454 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003455#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3456 unsigned long waitfor = jiffies;
3457#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003458
3459 if (!rqc && !mq->mqrq_prev->req)
3460 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003461
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003462 if (rqc)
3463 reqs = mmc_blk_prep_packed_list(mq, rqc);
3464
Per Forlin54d49d72011-07-01 18:55:29 +02003465 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003466 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303467 /*
3468 * When 4KB native sector is enabled, only 8 blocks
3469 * multiple read or write is allowed
3470 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003471 if (mmc_large_sector(card) &&
3472 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303473 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3474 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003475 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303476 goto cmd_abort;
3477 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003478
3479 if (reqs >= packed_nr)
3480 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3481 card, mq);
3482 else
3483 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003484 areq = &mq->mqrq_cur->mmc_active;
3485 } else
3486 areq = NULL;
3487 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003488 if (!areq) {
3489 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003490 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003491 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003492 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003493
Per Forlinee8a43a2011-07-01 18:55:33 +02003494 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3495 brq = &mq_rq->brq;
3496 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003497 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003498 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003499
Per Forlind78d4a82011-07-01 18:55:30 +02003500 switch (status) {
3501 case MMC_BLK_SUCCESS:
3502 case MMC_BLK_PARTIAL:
3503 /*
3504 * A block was successfully transferred.
3505 */
Adrian Hunter67716322011-08-29 16:42:15 +03003506 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003507
Mark Salyzyn6904e432016-01-28 11:12:25 -08003508 mmc_blk_simulate_delay(mq, rqc, waitfor);
3509
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003510 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3511 ret = mmc_blk_end_packed_req(mq_rq);
3512 break;
3513 } else {
3514 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003515 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003516 }
3517
Adrian Hunter67716322011-08-29 16:42:15 +03003518 /*
3519 * If the blk_end_request function returns non-zero even
3520 * though all data has been transferred and no errors
3521 * were returned by the host controller, it's a bug.
3522 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003523 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303524 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003525 __func__, blk_rq_bytes(req),
3526 brq->data.bytes_xfered);
3527 rqc = NULL;
3528 goto cmd_abort;
3529 }
Per Forlind78d4a82011-07-01 18:55:30 +02003530 break;
3531 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003532 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003533 if (mmc_blk_reset(md, card->host, type))
3534 goto cmd_abort;
3535 if (!ret)
3536 goto start_new_req;
3537 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003538 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003539 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003540 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003541 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003542 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003543 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003544 if (!mmc_blk_reset(md, card->host, type) &&
3545 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003546 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003547 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003548 case MMC_BLK_DATA_ERR: {
3549 int err;
3550
3551 err = mmc_blk_reset(md, card->host, type);
3552 if (!err)
3553 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303554 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003555 }
3556 case MMC_BLK_ECC_ERR:
3557 if (brq->data.blocks > 1) {
3558 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003559 pr_warn("%s: retrying using single block read\n",
3560 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003561 disable_multi = 1;
3562 break;
3563 }
Per Forlind78d4a82011-07-01 18:55:30 +02003564 /*
3565 * After an error, we redo I/O one sector at a
3566 * time, so we only reach here after trying to
3567 * read a single sector.
3568 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303569 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003570 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003571 if (!ret)
3572 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003573 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303574 case MMC_BLK_NOMEDIUM:
3575 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003576 default:
3577 pr_err("%s: Unhandled return value (%d)",
3578 req->rq_disk->disk_name, status);
3579 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003580 }
3581
Per Forlinee8a43a2011-07-01 18:55:33 +02003582 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003583 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3584 if (!mq_rq->packed->retries)
3585 goto cmd_abort;
3586 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3587 mmc_start_req(card->host,
3588 &mq_rq->mmc_active, NULL);
3589 } else {
3590
3591 /*
3592 * In case of a incomplete request
3593 * prepare it again and resend.
3594 */
3595 mmc_blk_rw_rq_prep(mq_rq, card,
3596 disable_multi, mq);
3597 mmc_start_req(card->host,
3598 &mq_rq->mmc_active, NULL);
3599 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003600 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 } while (ret);
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return 1;
3605
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003606 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003607 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3608 mmc_blk_abort_packed_req(mq_rq);
3609 } else {
3610 if (mmc_card_removed(card))
3611 req->cmd_flags |= REQ_QUIET;
3612 while (ret)
3613 ret = blk_end_request(req, -EIO,
3614 blk_rq_cur_bytes(req));
3615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Per Forlinee8a43a2011-07-01 18:55:33 +02003617 start_new_req:
3618 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003619 if (mmc_card_removed(card)) {
3620 rqc->cmd_flags |= REQ_QUIET;
3621 blk_end_request_all(rqc, -EIO);
3622 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003623 /*
3624 * If current request is packed, it needs to put back.
3625 */
3626 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3627 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3628
Seungwon Jeon7a819022013-01-22 19:48:07 +09003629 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3630 mmc_start_req(card->host,
3631 &mq->mqrq_cur->mmc_active, NULL);
3632 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003633 }
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return 0;
3636}
3637
Asutosh Das8b594832015-04-23 09:55:43 +05303638static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3639 struct mmc_blk_data *md)
3640{
3641 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3642 struct mmc_host *host = card->host;
3643 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3644 u8 part_config = card->ext_csd.part_config;
3645
3646 if ((main_md->part_curr == md->part_type) &&
3647 (card->part_curr == md->part_type))
3648 return 0;
3649
3650 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3651 card->ext_csd.cmdq_support &&
3652 (md->flags & MMC_BLK_CMD_QUEUE)));
3653
3654 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3655 WARN_ON(mmc_cmdq_halt(host, true));
3656
3657 /* disable CQ mode in card */
3658 if (mmc_card_cmdq(card)) {
3659 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3660 EXT_CSD_CMDQ, 0,
3661 card->ext_csd.generic_cmd6_time));
3662 mmc_card_clr_cmdq(card);
3663 }
3664
3665 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3666 part_config |= md->part_type;
3667
3668 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3669 EXT_CSD_PART_CONFIG, part_config,
3670 card->ext_csd.part_time));
3671
3672 card->ext_csd.part_config = part_config;
3673 card->part_curr = md->part_type;
3674
3675 main_md->part_curr = md->part_type;
3676
3677 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3678 WARN_ON(mmc_cmdq_halt(host, false));
3679
3680 return 0;
3681}
3682
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003683static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3684{
3685 int ret;
3686 struct mmc_blk_data *md = mq->data;
3687 struct mmc_card *card = md->queue.card;
3688
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003689 mmc_get_card(card);
Asutosh Das8b594832015-04-23 09:55:43 +05303690 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003691 if (ret) {
3692 pr_err("%s: %s: partition switch failed %d\n",
3693 md->disk->disk_name, __func__, ret);
Asutosh Das5238e022015-04-23 16:00:45 +05303694 if (req)
3695 blk_end_request_all(req, ret);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003696 mmc_put_card(card);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003697 goto switch_failure;
3698 }
3699
Asutosh Das5238e022015-04-23 16:00:45 +05303700 if (req) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05303701 if (req_op(req) == REQ_OP_DISCARD) {
3702 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3703 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3704 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3705 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3706 else
3707 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3708 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303709 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303710 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303711 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303712 }
Asutosh Das5238e022015-04-23 16:00:45 +05303713 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003714
3715switch_failure:
3716 return ret;
3717}
3718
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003719int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003720{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003721 int ret;
3722 struct mmc_blk_data *md = mq->data;
3723 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003724 struct mmc_host *host = card->host;
3725 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303726 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003727 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003728
Dov Levenglicka0296392015-06-24 19:51:58 +03003729 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003730 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003731 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003732
Dov Levenglicka0296392015-06-24 19:51:58 +03003733 if (mmc_card_doing_bkops(host->card)) {
3734 ret = mmc_stop_bkops(host->card);
3735 if (ret)
3736 goto out;
3737 }
3738 }
3739
Andrei Warkentin371a6892011-04-11 18:10:25 -05003740 ret = mmc_blk_part_switch(card, md);
3741 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003742 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303743 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003744 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003745 ret = 0;
3746 goto out;
3747 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003748
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003749 mmc_blk_write_packing_control(mq, req);
3750
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003751 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003752 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003753 /* complete ongoing async transfer before issuing discard */
3754 if (card->host->areq)
3755 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003756 ret = mmc_blk_issue_discard_rq(mq, req);
3757 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3758 /* complete ongoing async transfer before issuing secure erase*/
3759 if (card->host->areq)
3760 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003761 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3762 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3763 else
3764 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303765 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3766 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003767 /* complete ongoing async transfer before issuing flush */
3768 if (card->host->areq)
3769 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003770 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003771 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003772 if (!req && host->areq) {
3773 spin_lock_irqsave(&host->context_info.lock, flags);
3774 host->context_info.is_waiting_last_req = true;
3775 spin_unlock_irqrestore(&host->context_info.lock, flags);
3776 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003777 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003778 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003779
Andrei Warkentin371a6892011-04-11 18:10:25 -05003780out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003781 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3782 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003783 /*
3784 * Release host when there are no more requests
3785 * and after special request(discard, flush) is done.
3786 * In case sepecial request, there is no reentry to
3787 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3788 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003789 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003790 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Russell Kinga6f6c962006-01-03 22:38:44 +00003793static inline int mmc_blk_readonly(struct mmc_card *card)
3794{
3795 return mmc_card_readonly(card) ||
3796 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3797}
3798
Andrei Warkentin371a6892011-04-11 18:10:25 -05003799static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3800 struct device *parent,
3801 sector_t size,
3802 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003803 const char *subname,
3804 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
3806 struct mmc_blk_data *md;
3807 int devidx, ret;
3808
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003809again:
3810 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3811 return ERR_PTR(-ENOMEM);
3812
3813 spin_lock(&mmc_blk_lock);
3814 ret = ida_get_new(&mmc_blk_ida, &devidx);
3815 spin_unlock(&mmc_blk_lock);
3816
3817 if (ret == -EAGAIN)
3818 goto again;
3819 else if (ret)
3820 return ERR_PTR(ret);
3821
3822 if (devidx >= max_devices) {
3823 ret = -ENOSPC;
3824 goto out;
3825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003827 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003828 if (!md) {
3829 ret = -ENOMEM;
3830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003832
Johan Rudholmadd710e2011-12-02 08:51:06 +01003833 md->area_type = area_type;
3834
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003835 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003836 * Set the read-only status based on the supported commands
3837 * and the write protect switch.
3838 */
3839 md->read_only = mmc_blk_readonly(card);
3840
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003841 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003842 if (md->disk == NULL) {
3843 ret = -ENOMEM;
3844 goto err_kfree;
3845 }
3846
3847 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003848 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003849 md->usage = 1;
3850
Asutosh Das963469b2015-05-21 13:29:51 +05303851 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003852 if (ret)
3853 goto err_putdisk;
3854
Russell Kinga6f6c962006-01-03 22:38:44 +00003855 md->queue.data = md;
3856
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003857 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003858 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003859 md->disk->fops = &mmc_bdops;
3860 md->disk->private_data = md;
3861 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003862 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003863 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003864 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003865 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003866 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003867
3868 /*
3869 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3870 *
3871 * - be set for removable media with permanent block devices
3872 * - be unset for removable block devices with permanent media
3873 *
3874 * Since MMC block devices clearly fall under the second
3875 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3876 * should use the block device creation/destruction hotplug
3877 * messages to tell when the card is present.
3878 */
3879
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003880 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003881 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003882
Saugata Dasa5075eb2012-05-17 16:32:21 +05303883 if (mmc_card_mmc(card))
3884 blk_queue_logical_block_size(md->queue.queue,
3885 card->ext_csd.data_sector_size);
3886 else
3887 blk_queue_logical_block_size(md->queue.queue, 512);
3888
Andrei Warkentin371a6892011-04-11 18:10:25 -05003889 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003890
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003891 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003892 if ((mmc_card_mmc(card) &&
3893 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003894 (mmc_card_sd(card) &&
3895 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3896 md->flags |= MMC_BLK_CMD23;
3897 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003898
3899 if (mmc_card_mmc(card) &&
3900 md->flags & MMC_BLK_CMD23 &&
3901 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303902 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003903 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003904 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003905 }
3906
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003907 if (card->cmdq_init) {
3908 md->flags |= MMC_BLK_CMD_QUEUE;
3909 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
3910 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05303911 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303912 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303913 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003914 }
3915
3916 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003917 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3918 (md->flags & MMC_BLK_CMD23) &&
3919 card->ext_csd.packed_event_en) {
3920 if (!mmc_packed_init(&md->queue, card))
3921 md->flags |= MMC_BLK_PACKED_CMD;
3922 }
3923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003925
3926 err_putdisk:
3927 put_disk(md->disk);
3928 err_kfree:
3929 kfree(md);
3930 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003931 spin_lock(&mmc_blk_lock);
3932 ida_remove(&mmc_blk_ida, devidx);
3933 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003934 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Andrei Warkentin371a6892011-04-11 18:10:25 -05003937static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3938{
3939 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003940
3941 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3942 /*
3943 * The EXT_CSD sector count is in number or 512 byte
3944 * sectors.
3945 */
3946 size = card->ext_csd.sectors;
3947 } else {
3948 /*
3949 * The CSD capacity field is in units of read_blkbits.
3950 * set_capacity takes units of 512 bytes.
3951 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003952 size = (typeof(sector_t))card->csd.capacity
3953 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003954 }
3955
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003956 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003957 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003958}
3959
3960static int mmc_blk_alloc_part(struct mmc_card *card,
3961 struct mmc_blk_data *md,
3962 unsigned int part_type,
3963 sector_t size,
3964 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003965 const char *subname,
3966 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003967{
3968 char cap_str[10];
3969 struct mmc_blk_data *part_md;
3970
3971 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003972 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003973 if (IS_ERR(part_md))
3974 return PTR_ERR(part_md);
3975 part_md->part_type = part_type;
3976 list_add(&part_md->part, &md->part);
3977
James Bottomleyb9f28d82015-03-05 18:47:01 -08003978 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003979 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303980 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003981 part_md->disk->disk_name, mmc_card_id(card),
3982 mmc_card_name(card), part_md->part_type, cap_str);
3983 return 0;
3984}
3985
Namjae Jeone0c368d2011-10-06 23:41:38 +09003986/* MMC Physical partitions consist of two boot partitions and
3987 * up to four general purpose partitions.
3988 * For each partition enabled in EXT_CSD a block device will be allocatedi
3989 * to provide access to the partition.
3990 */
3991
Andrei Warkentin371a6892011-04-11 18:10:25 -05003992static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3993{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003994 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003995
3996 if (!mmc_card_mmc(card))
3997 return 0;
3998
Namjae Jeone0c368d2011-10-06 23:41:38 +09003999 for (idx = 0; idx < card->nr_parts; idx++) {
4000 if (card->part[idx].size) {
4001 ret = mmc_blk_alloc_part(card, md,
4002 card->part[idx].part_cfg,
4003 card->part[idx].size >> 9,
4004 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004005 card->part[idx].name,
4006 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004007 if (ret)
4008 return ret;
4009 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004010 }
4011
4012 return ret;
4013}
4014
Andrei Warkentin371a6892011-04-11 18:10:25 -05004015static void mmc_blk_remove_req(struct mmc_blk_data *md)
4016{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004017 struct mmc_card *card;
4018
Andrei Warkentin371a6892011-04-11 18:10:25 -05004019 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004020 /*
4021 * Flush remaining requests and free queues. It
4022 * is freeing the queue that stops new requests
4023 * from being accepted.
4024 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004025 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004026 mmc_cleanup_queue(&md->queue);
4027 if (md->flags & MMC_BLK_PACKED_CMD)
4028 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004029 if (md->flags & MMC_BLK_CMD_QUEUE)
4030 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004031 device_remove_file(disk_to_dev(md->disk),
4032 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004033 if (md->disk->flags & GENHD_FL_UP) {
4034 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004035 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4036 card->ext_csd.boot_ro_lockable)
4037 device_remove_file(disk_to_dev(md->disk),
4038 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004039#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4040 device_remove_file(disk_to_dev(md->disk),
4041 &dev_attr_max_write_speed);
4042 device_remove_file(disk_to_dev(md->disk),
4043 &dev_attr_max_read_speed);
4044 device_remove_file(disk_to_dev(md->disk),
4045 &dev_attr_cache_size);
4046#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004047
Andrei Warkentin371a6892011-04-11 18:10:25 -05004048 del_gendisk(md->disk);
4049 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004050 mmc_blk_put(md);
4051 }
4052}
4053
4054static void mmc_blk_remove_parts(struct mmc_card *card,
4055 struct mmc_blk_data *md)
4056{
4057 struct list_head *pos, *q;
4058 struct mmc_blk_data *part_md;
4059
4060 list_for_each_safe(pos, q, &md->part) {
4061 part_md = list_entry(pos, struct mmc_blk_data, part);
4062 list_del(pos);
4063 mmc_blk_remove_req(part_md);
4064 }
4065}
4066
4067static int mmc_add_disk(struct mmc_blk_data *md)
4068{
4069 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004070 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004071
Dan Williams307d8e62016-06-20 10:40:44 -07004072 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004073 md->force_ro.show = force_ro_show;
4074 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304075 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004076 md->force_ro.attr.name = "force_ro";
4077 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4078 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4079 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004080 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004081#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4082 atomic_set(&md->queue.max_write_speed, max_write_speed);
4083 ret = device_create_file(disk_to_dev(md->disk),
4084 &dev_attr_max_write_speed);
4085 if (ret)
4086 goto max_write_speed_fail;
4087 atomic_set(&md->queue.max_read_speed, max_read_speed);
4088 ret = device_create_file(disk_to_dev(md->disk),
4089 &dev_attr_max_read_speed);
4090 if (ret)
4091 goto max_read_speed_fail;
4092 atomic_set(&md->queue.cache_size, cache_size);
4093 atomic_long_set(&md->queue.cache_used, 0);
4094 md->queue.cache_jiffies = jiffies;
4095 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4096 if (ret)
4097 goto cache_size_fail;
4098#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004099
4100 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4101 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004102 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004103
4104 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4105 mode = S_IRUGO;
4106 else
4107 mode = S_IRUGO | S_IWUSR;
4108
4109 md->power_ro_lock.show = power_ro_lock_show;
4110 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004111 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004112 md->power_ro_lock.attr.mode = mode;
4113 md->power_ro_lock.attr.name =
4114 "ro_lock_until_next_power_on";
4115 ret = device_create_file(disk_to_dev(md->disk),
4116 &md->power_ro_lock);
4117 if (ret)
4118 goto power_ro_lock_fail;
4119 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004120
4121 md->num_wr_reqs_to_start_packing.show =
4122 num_wr_reqs_to_start_packing_show;
4123 md->num_wr_reqs_to_start_packing.store =
4124 num_wr_reqs_to_start_packing_store;
4125 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4126 md->num_wr_reqs_to_start_packing.attr.name =
4127 "num_wr_reqs_to_start_packing";
4128 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4129 ret = device_create_file(disk_to_dev(md->disk),
4130 &md->num_wr_reqs_to_start_packing);
4131 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004132 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004133
Maya Erez5a8dae12014-12-04 15:13:59 +02004134 md->no_pack_for_random.show = no_pack_for_random_show;
4135 md->no_pack_for_random.store = no_pack_for_random_store;
4136 sysfs_attr_init(&md->no_pack_for_random.attr);
4137 md->no_pack_for_random.attr.name = "no_pack_for_random";
4138 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4139 ret = device_create_file(disk_to_dev(md->disk),
4140 &md->no_pack_for_random);
4141 if (ret)
4142 goto no_pack_for_random_fails;
4143
Johan Rudholmadd710e2011-12-02 08:51:06 +01004144 return ret;
4145
Maya Erez5a8dae12014-12-04 15:13:59 +02004146no_pack_for_random_fails:
4147 device_remove_file(disk_to_dev(md->disk),
4148 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004149num_wr_reqs_to_start_packing_fail:
4150 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004151power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004152#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4153 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4154cache_size_fail:
4155 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4156max_read_speed_fail:
4157 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4158max_write_speed_fail:
4159#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004160 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4161force_ro_fail:
4162 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004163
4164 return ret;
4165}
4166
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004167static const struct mmc_fixup blk_fixups[] =
4168{
Chris Ballc59d4472011-11-11 22:01:43 -05004169 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4170 MMC_QUIRK_INAND_CMD38),
4171 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4172 MMC_QUIRK_INAND_CMD38),
4173 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4174 MMC_QUIRK_INAND_CMD38),
4175 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4176 MMC_QUIRK_INAND_CMD38),
4177 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4178 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004179
4180 /*
4181 * Some MMC cards experience performance degradation with CMD23
4182 * instead of CMD12-bounded multiblock transfers. For now we'll
4183 * black list what's bad...
4184 * - Certain Toshiba cards.
4185 *
4186 * N.B. This doesn't affect SD cards.
4187 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004188 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4189 MMC_QUIRK_BLK_NO_CMD23),
4190 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4191 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004192 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004193 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004194 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004195 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004196 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004197 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004198
4199 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004200 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004201 */
Chris Ballc59d4472011-11-11 22:01:43 -05004202 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004203 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004204 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4205 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004206
Ian Chen3550ccd2012-08-29 15:05:36 +09004207 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004208 * Some Samsung MMC cards need longer data read timeout than
4209 * indicated in CSD.
4210 */
4211 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4212 MMC_QUIRK_LONG_READ_TIME),
4213
4214 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004215 * On these Samsung MoviNAND parts, performing secure erase or
4216 * secure trim can result in unrecoverable corruption due to a
4217 * firmware bug.
4218 */
4219 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4220 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4221 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4222 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4223 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4224 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4225 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4226 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4227 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4228 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4229 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4230 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4231 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4232 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4233 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4234 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4235
Shawn Linb5b4ff02015-08-12 13:08:32 +08004236 /*
4237 * On Some Kingston eMMCs, performing trim can result in
4238 * unrecoverable data conrruption occasionally due to a firmware bug.
4239 */
4240 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4241 MMC_QUIRK_TRIM_BROKEN),
4242 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4243 MMC_QUIRK_TRIM_BROKEN),
4244
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004245 /* Some INAND MCP devices advertise incorrect timeout values */
4246 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4247 MMC_QUIRK_INAND_DATA_TIMEOUT),
4248
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004249 END_FIXUP
4250};
4251
Ulf Hansson96541ba2015-04-14 13:06:12 +02004252static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004254 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004255 char cap_str[10];
4256
Pierre Ossman912490d2005-05-21 10:27:02 +01004257 /*
4258 * Check that the card supports the command class(es) we need.
4259 */
4260 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 return -ENODEV;
4262
Lukas Czerner5204d002014-06-18 13:18:07 +02004263 mmc_fixup_device(card, blk_fixups);
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 md = mmc_blk_alloc(card);
4266 if (IS_ERR(md))
4267 return PTR_ERR(md);
4268
James Bottomleyb9f28d82015-03-05 18:47:01 -08004269 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004270 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304271 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004273 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Andrei Warkentin371a6892011-04-11 18:10:25 -05004275 if (mmc_blk_alloc_parts(card, md))
4276 goto out;
4277
Ulf Hansson96541ba2015-04-14 13:06:12 +02004278 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004279
Andrei Warkentin371a6892011-04-11 18:10:25 -05004280 if (mmc_add_disk(md))
4281 goto out;
4282
4283 list_for_each_entry(part_md, &md->part, part) {
4284 if (mmc_add_disk(part_md))
4285 goto out;
4286 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004287
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004288 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004289 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4290 /*
4291 * If there is a runtime_idle function, it should take care of
4292 * suspending the card
4293 */
4294 if (card->host->bus_ops->runtime_idle)
4295 pm_runtime_dont_use_autosuspend(&card->dev);
4296 else
4297 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004298
4299 /*
4300 * Don't enable runtime PM for SD-combo cards here. Leave that
4301 * decision to be taken during the SDIO init sequence instead.
4302 */
4303 if (card->type != MMC_TYPE_SD_COMBO) {
4304 pm_runtime_set_active(&card->dev);
4305 pm_runtime_enable(&card->dev);
4306 }
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 return 0;
4309
4310 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004311 mmc_blk_remove_parts(card, md);
4312 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
Ulf Hansson96541ba2015-04-14 13:06:12 +02004316static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004318 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Andrei Warkentin371a6892011-04-11 18:10:25 -05004320 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004321 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004322 mmc_claim_host(card->host);
4323 mmc_blk_part_switch(card, md);
4324 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004325 if (card->type != MMC_TYPE_SD_COMBO)
4326 pm_runtime_disable(&card->dev);
4327 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004328 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004329 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
Ulf Hansson96541ba2015-04-14 13:06:12 +02004332static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004334 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004335 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304336 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304339 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304340 if (rc)
4341 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004342 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304343 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304344 if (rc)
4345 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304348 goto out;
4349
4350 out_resume:
4351 mmc_queue_resume(&md->queue);
4352 list_for_each_entry(part_md, &md->part, part) {
4353 mmc_queue_resume(&part_md->queue);
4354 }
4355 out:
4356 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Ulf Hansson96541ba2015-04-14 13:06:12 +02004359static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004360{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004361 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004362}
4363
Ulf Hansson0967edc2014-10-06 11:29:42 +02004364#ifdef CONFIG_PM_SLEEP
4365static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004366{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004367 struct mmc_card *card = mmc_dev_to_card(dev);
4368
4369 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004370}
4371
Ulf Hansson0967edc2014-10-06 11:29:42 +02004372static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004374 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004375 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004378 /*
4379 * Resume involves the card going into idle state,
4380 * so current partition is always the main one.
4381 */
4382 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004384 list_for_each_entry(part_md, &md->part, part) {
4385 mmc_queue_resume(&part_md->queue);
4386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 }
4388 return 0;
4389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390#endif
4391
Ulf Hansson0967edc2014-10-06 11:29:42 +02004392static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4393
Ulf Hansson96541ba2015-04-14 13:06:12 +02004394static struct mmc_driver mmc_driver = {
4395 .drv = {
4396 .name = "mmcblk",
4397 .pm = &mmc_blk_pm_ops,
4398 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 .probe = mmc_blk_probe,
4400 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004401 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402};
4403
4404static int __init mmc_blk_init(void)
4405{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004406 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004408 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4409 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4410
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004411 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004412
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004413 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4414 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004417 res = mmc_register_driver(&mmc_driver);
4418 if (res)
4419 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004421 return 0;
4422 out2:
4423 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 out:
4425 return res;
4426}
4427
4428static void __exit mmc_blk_exit(void)
4429{
4430 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004431 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432}
4433
4434module_init(mmc_blk_init);
4435module_exit(mmc_blk_exit);
4436
4437MODULE_LICENSE("GPL");
4438MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4439