blob: 2b4d193d0ba7dbf686d4944b0b98ce83cf93b96b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Dov Levenglicka0296392015-06-24 19:51:58 +030044#include <linux/mmc/core.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020045#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046#include <linux/mmc/mmc.h>
47#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50
Pierre Ossman98ac2162006-12-23 20:03:02 +010051#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080052#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020066#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053069#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050070
Luca Porziod3df0462015-11-06 15:12:26 +000071#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072 (rq_data_dir(req) == WRITE))
73#define PACKED_CMD_VER 0x01
74#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030075#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090076
Maya Erezf93ca0a2014-12-09 23:34:41 +020077#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020078#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
79 do { \
80 if (stats->enabled) \
81 stats->pack_stop_reason[reason]++; \
82 } while (0)
83
Asutosh Das02e30862015-05-20 16:52:04 +053084#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030085#define PCKD_TRGR_INIT_MEAN_POTEN 17
86#define PCKD_TRGR_POTEN_LOWER_BOUND 5
87#define PCKD_TRGR_URGENT_PENALTY 2
88#define PCKD_TRGR_LOWER_BOUND 5
89#define PCKD_TRGR_PRECISION_MULTIPLIER 100
90
Sahitya Tummala9433a132015-06-09 09:38:36 +053091static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
92 struct mmc_queue_req *mqrq, struct mmc_queue *mq);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020093static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040094
95/*
96 * The defaults come from config options but can be overriden by module
97 * or bootarg options.
98 */
99static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
100
101/*
102 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000103 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105 */
106static int max_devices;
107
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000108#define MAX_DEVICES 256
109
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200110static DEFINE_IDA(mmc_blk_ida);
111static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * There is one mmc_blk_data per slot.
115 */
116struct mmc_blk_data {
117 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700118 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct gendisk *disk;
120 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500123 unsigned int flags;
124#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
125#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700127#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000130 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300132 unsigned int reset_done;
133#define MMC_BLK_READ BIT(0)
134#define MMC_BLK_WRITE BIT(1)
135#define MMC_BLK_DISCARD BIT(2)
136#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200137#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138
139 /*
140 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200141 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 * track of the current selected device partition.
143 */
144 unsigned int part_curr;
145 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200147 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200148 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100149 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Arjan van de Vena621aae2006-01-12 18:43:35 +0000152static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154enum {
155 MMC_PACKED_NR_IDX = -1,
156 MMC_PACKED_NR_ZERO,
157 MMC_PACKED_NR_SINGLE,
158};
159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
164 struct mmc_blk_data *md);
165static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700166static int mmc_blk_cmdq_switch(struct mmc_card *card,
167 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200168
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900169static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
170{
171 struct mmc_packed *packed = mqrq->packed;
172
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900173 mqrq->cmd_type = MMC_PACKED_NONE;
174 packed->nr_entries = MMC_PACKED_NR_ZERO;
175 packed->idx_failure = MMC_PACKED_NR_IDX;
176 packed->retries = 0;
177 packed->blocks = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
181{
182 struct mmc_blk_data *md;
183
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md = disk->private_data;
186 if (md && md->usage == 0)
187 md = NULL;
188 if (md)
189 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return md;
193}
194
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195static inline int mmc_get_devidx(struct gendisk *disk)
196{
Colin Cross382c55f2015-10-22 10:00:41 -0700197 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500198 return devidx;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void mmc_blk_put(struct mmc_blk_data *md)
202{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000203 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 md->usage--;
205 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500206 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800207 blk_cleanup_queue(md->queue.queue);
208
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200209 spin_lock(&mmc_blk_lock);
210 ida_remove(&mmc_blk_ida, devidx);
211 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kfree(md);
215 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000216 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219static ssize_t power_ro_lock_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 int ret;
223 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 int locked = 0;
226
Asutosh Das507d9a72014-12-09 10:15:53 +0200227 if (!md)
228 return -EINVAL;
229
230 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 locked = 2;
233 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
234 locked = 1;
235
236 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
237
Tomas Winkler9098f842015-07-16 15:50:45 +0200238 mmc_blk_put(md);
239
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240 return ret;
241}
242
243static ssize_t power_ro_lock_store(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
245{
246 int ret;
247 struct mmc_blk_data *md, *part_md;
248 struct mmc_card *card;
249 unsigned long set;
250
251 if (kstrtoul(buf, 0, &set))
252 return -EINVAL;
253
254 if (set != 1)
255 return count;
256
257 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200258 if (!md)
259 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260 card = md->queue.card;
261
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200262 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100263
264 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
265 card->ext_csd.boot_ro_lock |
266 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
267 card->ext_csd.part_time);
268 if (ret)
269 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
270 else
271 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
272
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200273 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100274
275 if (!ret) {
276 pr_info("%s: Locking boot partition ro until next power on\n",
277 md->disk->disk_name);
278 set_disk_ro(md->disk, 1);
279
280 list_for_each_entry(part_md, &md->part, part)
281 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
282 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
283 set_disk_ro(part_md->disk, 1);
284 }
285 }
286
287 mmc_blk_put(md);
288 return count;
289}
290
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
293{
294 int ret;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296
Asutosh Das507d9a72014-12-09 10:15:53 +0200297 if (!md)
298 return -EINVAL;
299
Baruch Siach0031a982014-09-22 10:12:51 +0300300 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500301 get_disk_ro(dev_to_disk(dev)) ^
302 md->read_only);
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int ret;
311 char *end;
312 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
313 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200314
315 if (!md)
316 return -EINVAL;
317
Andrei Warkentin371a6892011-04-11 18:10:25 -0500318 if (end == buf) {
319 ret = -EINVAL;
320 goto out;
321 }
322
323 set_disk_ro(dev_to_disk(dev), set || md->read_only);
324 ret = count;
325out:
326 mmc_blk_put(md);
327 return ret;
328}
329
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200330static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200331no_pack_for_random_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 int ret;
336
Asutosh Das507d9a72014-12-09 10:15:53 +0200337 if (!md)
338 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200339 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
340
341 mmc_blk_put(md);
342 return ret;
343}
344
345static ssize_t
346no_pack_for_random_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 int value;
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200353 int ret = count;
354
Asutosh Das507d9a72014-12-09 10:15:53 +0200355 if (!md)
356 return -EINVAL;
357
358 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200359 if (!card) {
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 sscanf(buf, "%d", &value);
365
366 if (value < 0) {
367 pr_err("%s: value %d is not valid. old value remains = %d",
368 mmc_hostname(card->host), value,
369 md->queue.no_pack_for_random);
370 ret = -EINVAL;
371 goto exit;
372 }
373
374 md->queue.no_pack_for_random = (value > 0) ? true : false;
375
376 pr_debug("%s: no_pack_for_random: new value = %d",
377 mmc_hostname(card->host),
378 md->queue.no_pack_for_random);
379
380exit:
381 mmc_blk_put(md);
382 return ret;
383}
384
385static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386num_wr_reqs_to_start_packing_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
390 int num_wr_reqs_to_start_packing;
391 int ret;
392
Asutosh Das507d9a72014-12-09 10:15:53 +0200393 if (!md)
394 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
396
397 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
398
399 mmc_blk_put(md);
400 return ret;
401}
402
403static ssize_t
404num_wr_reqs_to_start_packing_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 int value;
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200411 int ret = count;
412
Asutosh Das507d9a72014-12-09 10:15:53 +0200413 if (!md)
414 return -EINVAL;
415
416 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200417 if (!card) {
418 ret = -EINVAL;
419 goto exit;
420 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200421
422 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200423
Yaniv Gardi42399822014-12-04 00:26:23 +0200424 if (value >= 0) {
425 md->queue.num_wr_reqs_to_start_packing =
426 min_t(int, value, (int)card->ext_csd.max_packed_writes);
427
428 pr_debug("%s: trigger to pack: new value = %d",
429 mmc_hostname(card->host),
430 md->queue.num_wr_reqs_to_start_packing);
431 } else {
432 pr_err("%s: value %d is not valid. old value remains = %d",
433 mmc_hostname(card->host), value,
434 md->queue.num_wr_reqs_to_start_packing);
435 ret = -EINVAL;
436 }
437
438exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200439 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200440 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200441}
442
Mark Salyzyn6904e432016-01-28 11:12:25 -0800443#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
444
445static int max_read_speed, max_write_speed, cache_size = 4;
446
447module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
448MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
449module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
450MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
451module_param(cache_size, int, S_IRUSR | S_IRGRP);
452MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
453
454/*
455 * helper macros and expectations:
456 * size - unsigned long number of bytes
457 * jiffies - unsigned long HZ timestamp difference
458 * speed - unsigned KB/s transfer rate
459 */
460#define size_and_speed_to_jiffies(size, speed) \
461 ((size) * HZ / (speed) / 1024UL)
462#define jiffies_and_speed_to_size(jiffies, speed) \
463 (((speed) * (jiffies) * 1024UL) / HZ)
464#define jiffies_and_size_to_speed(jiffies, size) \
465 ((size) * HZ / (jiffies) / 1024UL)
466
467/* Limits to report warning */
468/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
469#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
470#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
471
472#define speed_valid(speed) ((speed) > 0)
473
474static const char off[] = "off\n";
475
476static int max_speed_show(int speed, char *buf)
477{
478 if (speed)
479 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
480 else
481 return scnprintf(buf, PAGE_SIZE, off);
482}
483
484static int max_speed_store(const char *buf, struct request_queue *q)
485{
486 unsigned int limit, set = 0;
487
488 if (!strncasecmp(off, buf, sizeof(off) - 2))
489 return set;
490 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
491 return -EINVAL;
492 if (set == 0)
493 return set;
494 limit = MAX_SPEED(q);
495 if (set > limit)
496 pr_warn("max speed %u ineffective above %u\n", set, limit);
497 limit = MIN_SPEED(q);
498 if (set < limit)
499 pr_warn("max speed %u painful below %u\n", set, limit);
500 return set;
501}
502
503static ssize_t max_write_speed_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
507 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
508
509 mmc_blk_put(md);
510 return ret;
511}
512
513static ssize_t max_write_speed_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
518 int set = max_speed_store(buf, md->queue.queue);
519
520 if (set < 0) {
521 mmc_blk_put(md);
522 return set;
523 }
524
525 atomic_set(&md->queue.max_write_speed, set);
526 mmc_blk_put(md);
527 return count;
528}
529
530static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
531 max_write_speed_show, max_write_speed_store);
532
533static ssize_t max_read_speed_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
537 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
538
539 mmc_blk_put(md);
540 return ret;
541}
542
543static ssize_t max_read_speed_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
548 int set = max_speed_store(buf, md->queue.queue);
549
550 if (set < 0) {
551 mmc_blk_put(md);
552 return set;
553 }
554
555 atomic_set(&md->queue.max_read_speed, set);
556 mmc_blk_put(md);
557 return count;
558}
559
560static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
561 max_read_speed_show, max_read_speed_store);
562
563static ssize_t cache_size_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
567 struct mmc_queue *mq = &md->queue;
568 int cache_size = atomic_read(&mq->cache_size);
569 int ret;
570
571 if (!cache_size)
572 ret = scnprintf(buf, PAGE_SIZE, off);
573 else {
574 int speed = atomic_read(&mq->max_write_speed);
575
576 if (!speed_valid(speed))
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
578 else { /* We accept race between cache_jiffies and cache_used */
579 unsigned long size = jiffies_and_speed_to_size(
580 jiffies - mq->cache_jiffies, speed);
581 long used = atomic_long_read(&mq->cache_used);
582
583 if (size >= used)
584 size = 0;
585 else
586 size = (used - size) * 100 / cache_size
587 / 1024UL / 1024UL;
588
589 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
590 cache_size, size);
591 }
592 }
593
594 mmc_blk_put(md);
595 return ret;
596}
597
598static ssize_t cache_size_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
601{
602 struct mmc_blk_data *md;
603 unsigned int set = 0;
604
605 if (strncasecmp(off, buf, sizeof(off) - 2)
606 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
607 return -EINVAL;
608
609 md = mmc_blk_get(dev_to_disk(dev));
610 atomic_set(&md->queue.cache_size, set);
611 mmc_blk_put(md);
612 return count;
613}
614
615static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
616 cache_size_show, cache_size_store);
617
618/* correct for write-back */
619static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
620{
621 long used = 0;
622 int speed = atomic_read(&mq->max_write_speed);
623
624 if (speed_valid(speed)) {
625 unsigned long size = jiffies_and_speed_to_size(
626 waitfor - mq->cache_jiffies, speed);
627 used = atomic_long_read(&mq->cache_used);
628
629 if (size >= used)
630 used = 0;
631 else
632 used -= size;
633 }
634
635 atomic_long_set(&mq->cache_used, used);
636 mq->cache_jiffies = waitfor;
637
638 return used;
639}
640
641static void mmc_blk_simulate_delay(
642 struct mmc_queue *mq,
643 struct request *req,
644 unsigned long waitfor)
645{
646 int max_speed;
647
648 if (!req)
649 return;
650
651 max_speed = (rq_data_dir(req) == READ)
652 ? atomic_read(&mq->max_read_speed)
653 : atomic_read(&mq->max_write_speed);
654 if (speed_valid(max_speed)) {
655 unsigned long bytes = blk_rq_bytes(req);
656
657 if (rq_data_dir(req) != READ) {
658 int cache_size = atomic_read(&mq->cache_size);
659
660 if (cache_size) {
661 unsigned long size = cache_size * 1024L * 1024L;
662 long used = mmc_blk_cache_used(mq, waitfor);
663
664 used += bytes;
665 atomic_long_set(&mq->cache_used, used);
666 bytes = 0;
667 if (used > size)
668 bytes = used - size;
669 }
670 }
671 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
672 if (time_is_after_jiffies(waitfor)) {
673 long msecs = jiffies_to_msecs(waitfor - jiffies);
674
675 if (likely(msecs > 0))
676 msleep(msecs);
677 }
678 }
679}
680
681#else
682
683#define mmc_blk_simulate_delay(mq, req, waitfor)
684
685#endif
686
Al Viroa5a15612008-03-02 10:33:30 -0500687static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viroa5a15612008-03-02 10:33:30 -0500689 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = -ENXIO;
691
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200692 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (md) {
694 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500695 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697
Al Viroa5a15612008-03-02 10:33:30 -0500698 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700699 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700700 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return ret;
706}
707
Al Virodb2a1442013-05-05 21:52:57 -0400708static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viroa5a15612008-03-02 10:33:30 -0500710 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200712 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200714 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800718mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
721 geo->heads = 4;
722 geo->sectors = 16;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
John Calixtocb87ea22011-04-26 18:56:29 -0400726struct mmc_blk_ioc_data {
727 struct mmc_ioc_cmd ic;
728 unsigned char *buf;
729 u64 buf_bytes;
730};
731
732static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
733 struct mmc_ioc_cmd __user *user)
734{
735 struct mmc_blk_ioc_data *idata;
736 int err;
737
yalin wang1ff89502015-11-12 19:27:11 +0800738 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400739 if (!idata) {
740 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400742 }
743
744 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
745 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400746 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400747 }
748
749 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
750 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
751 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400752 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400753 }
754
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 if (!idata->buf_bytes) {
756 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300758 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100759
yalin wang1ff89502015-11-12 19:27:11 +0800760 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400761 if (!idata->buf) {
762 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400763 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400764 }
765
766 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
767 idata->ic.data_ptr, idata->buf_bytes)) {
768 err = -EFAULT;
769 goto copy_err;
770 }
771
772 return idata;
773
774copy_err:
775 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400776idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400777 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400778out:
John Calixtocb87ea22011-04-26 18:56:29 -0400779 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
Jon Huntera5f57742015-09-22 10:27:53 +0100782static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
783 struct mmc_blk_ioc_data *idata)
784{
785 struct mmc_ioc_cmd *ic = &idata->ic;
786
787 if (copy_to_user(&(ic_ptr->response), ic->response,
788 sizeof(ic->response)))
789 return -EFAULT;
790
791 if (!idata->ic.write_flag) {
792 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
793 idata->buf, idata->buf_bytes))
794 return -EFAULT;
795 }
796
797 return 0;
798}
799
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200800static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
801 u32 retries_max)
802{
803 int err;
804 u32 retry_count = 0;
805
806 if (!status || !retries_max)
807 return -EINVAL;
808
809 do {
810 err = get_card_status(card, status, 5);
811 if (err)
812 break;
813
814 if (!R1_STATUS(*status) &&
815 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
816 break; /* RPMB programming operation complete */
817
818 /*
819 * Rechedule to give the MMC device a chance to continue
820 * processing the previous command without being polled too
821 * frequently.
822 */
823 usleep_range(1000, 5000);
824 } while (++retry_count < retries_max);
825
826 if (retry_count == retries_max)
827 err = -EPERM;
828
829 return err;
830}
831
Maya Erez775a9362013-04-18 15:41:55 +0300832static int ioctl_do_sanitize(struct mmc_card *card)
833{
834 int err;
835
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530836 if (!mmc_can_sanitize(card) &&
837 (card->host->caps2 & MMC_CAP2_SANITIZE)) {
838 pr_warn("%s: %s - SANITIZE is not supported\n",
Maya Erez775a9362013-04-18 15:41:55 +0300839 mmc_hostname(card->host), __func__);
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530840 err = -EOPNOTSUPP;
841 goto out;
Maya Erez775a9362013-04-18 15:41:55 +0300842 }
843
844 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
845 mmc_hostname(card->host), __func__);
846
847 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
848 EXT_CSD_SANITIZE_START, 1,
849 MMC_SANITIZE_REQ_TIMEOUT);
850
851 if (err)
852 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
853 mmc_hostname(card->host), __func__, err);
854
855 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
856 __func__);
857out:
858 return err;
859}
860
Jon Huntera5f57742015-09-22 10:27:53 +0100861static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
862 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400863{
John Calixtocb87ea22011-04-26 18:56:29 -0400864 struct mmc_command cmd = {0};
865 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530866 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400867 struct scatterlist sg;
868 int err;
869
Jon Huntera5f57742015-09-22 10:27:53 +0100870 if (!card || !md || !idata)
871 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400872
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100873 cmd.opcode = idata->ic.opcode;
874 cmd.arg = idata->ic.arg;
875 cmd.flags = idata->ic.flags;
876
877 if (idata->buf_bytes) {
878 data.sg = &sg;
879 data.sg_len = 1;
880 data.blksz = idata->ic.blksz;
881 data.blocks = idata->ic.blocks;
882
883 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
884
885 if (idata->ic.write_flag)
886 data.flags = MMC_DATA_WRITE;
887 else
888 data.flags = MMC_DATA_READ;
889
890 /* data.flags must already be set before doing this. */
891 mmc_set_data_timeout(&data, card);
892
893 /* Allow overriding the timeout_ns for empirical tuning. */
894 if (idata->ic.data_timeout_ns)
895 data.timeout_ns = idata->ic.data_timeout_ns;
896
897 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
898 /*
899 * Pretend this is a data transfer and rely on the
900 * host driver to compute timeout. When all host
901 * drivers support cmd.cmd_timeout for R1B, this
902 * can be changed to:
903 *
904 * mrq.data = NULL;
905 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
906 */
907 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
908 }
909
910 mrq.data = &data;
911 }
912
913 mrq.cmd = &cmd;
914
Maya Erez70afbaf2015-10-01 11:27:17 +0300915 if (mmc_card_doing_bkops(card)) {
916 err = mmc_stop_bkops(card);
917 if (err) {
918 dev_err(mmc_dev(card->host),
919 "%s: stop_bkops failed %d\n", __func__, err);
920 return err;
921 }
922 }
923
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200924 err = mmc_blk_part_switch(card, md);
925 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100926 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200927
John Calixtocb87ea22011-04-26 18:56:29 -0400928 if (idata->ic.is_acmd) {
929 err = mmc_app_cmd(card->host, card);
930 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100931 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400932 }
933
Yaniv Gardia82e4842013-06-05 14:13:08 +0300934 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
935 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300936 err = ioctl_do_sanitize(card);
937
938 if (err)
939 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
940 __func__, err);
941
Jon Huntera5f57742015-09-22 10:27:53 +0100942 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300943 }
944
John Calixtocb87ea22011-04-26 18:56:29 -0400945 mmc_wait_for_req(card->host, &mrq);
946
947 if (cmd.error) {
948 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
949 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100950 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400951 }
952 if (data.error) {
953 dev_err(mmc_dev(card->host), "%s: data error %d\n",
954 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100955 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400956 }
957
958 /*
959 * According to the SD specs, some commands require a delay after
960 * issuing the command.
961 */
962 if (idata->ic.postsleep_min_us)
963 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
964
Jon Huntera5f57742015-09-22 10:27:53 +0100965 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400966
Krishna Kondae6711632014-12-04 15:20:57 +0200967 return err;
968}
969
970struct mmc_blk_ioc_rpmb_data {
971 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
972};
973
974static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
975 struct mmc_ioc_rpmb __user *user)
976{
977 struct mmc_blk_ioc_rpmb_data *idata;
978 int err, i;
979
980 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
981 if (!idata) {
982 err = -ENOMEM;
983 goto out;
984 }
985
986 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
987 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
988 if (IS_ERR(idata->data[i])) {
989 err = PTR_ERR(idata->data[i]);
990 goto copy_err;
991 }
992 }
993
994 return idata;
995
996copy_err:
997 while (--i >= 0) {
998 kfree(idata->data[i]->buf);
999 kfree(idata->data[i]);
1000 }
1001 kfree(idata);
1002out:
1003 return ERR_PTR(err);
1004}
1005
1006static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
1007 struct mmc_ioc_rpmb __user *ic_ptr)
1008{
1009 struct mmc_blk_ioc_rpmb_data *idata;
1010 struct mmc_blk_data *md;
Subhash Jadavani692984f2015-10-19 12:04:46 -07001011 struct mmc_card *card = NULL;
Krishna Kondae6711632014-12-04 15:20:57 +02001012 struct mmc_command cmd = {0};
1013 struct mmc_data data = {0};
1014 struct mmc_request mrq = {NULL};
1015 struct scatterlist sg;
1016 int err = 0, i = 0;
1017 u32 status = 0;
1018
1019 /* The caller must have CAP_SYS_RAWIO */
1020 if (!capable(CAP_SYS_RAWIO))
1021 return -EPERM;
1022
1023 md = mmc_blk_get(bdev->bd_disk);
1024 /* make sure this is a rpmb partition */
1025 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1026 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001027 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001028 }
1029
1030 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1031 if (IS_ERR(idata)) {
1032 err = PTR_ERR(idata);
1033 goto cmd_done;
1034 }
1035
1036 card = md->queue.card;
1037 if (IS_ERR(card)) {
1038 err = PTR_ERR(card);
1039 goto idata_free;
1040 }
1041
Maya Erezdd669562015-02-12 20:37:31 +02001042 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001043
Maya Erez70afbaf2015-10-01 11:27:17 +03001044 if (mmc_card_doing_bkops(card)) {
1045 if (mmc_card_cmdq(card)) {
1046 err = mmc_cmdq_halt(card->host, true);
1047 if (err)
1048 goto cmd_rel_host;
1049 }
1050 err = mmc_stop_bkops(card);
1051 if (err) {
1052 dev_err(mmc_dev(card->host),
1053 "%s: stop_bkops failed %d\n", __func__, err);
1054 goto cmd_rel_host;
1055 }
1056 if (mmc_card_cmdq(card)) {
1057 err = mmc_cmdq_halt(card->host, false);
1058 if (err)
1059 goto cmd_rel_host;
1060 }
1061 }
1062
Krishna Kondae6711632014-12-04 15:20:57 +02001063 err = mmc_blk_part_switch(card, md);
1064 if (err)
1065 goto cmd_rel_host;
1066
1067 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1068 struct mmc_blk_ioc_data *curr_data;
1069 struct mmc_ioc_cmd *curr_cmd;
1070
1071 curr_data = idata->data[i];
1072 curr_cmd = &curr_data->ic;
1073 if (!curr_cmd->opcode)
1074 break;
1075
1076 cmd.opcode = curr_cmd->opcode;
1077 cmd.arg = curr_cmd->arg;
1078 cmd.flags = curr_cmd->flags;
1079
1080 if (curr_data->buf_bytes) {
1081 data.sg = &sg;
1082 data.sg_len = 1;
1083 data.blksz = curr_cmd->blksz;
1084 data.blocks = curr_cmd->blocks;
1085
1086 sg_init_one(data.sg, curr_data->buf,
1087 curr_data->buf_bytes);
1088
1089 if (curr_cmd->write_flag)
1090 data.flags = MMC_DATA_WRITE;
1091 else
1092 data.flags = MMC_DATA_READ;
1093
1094 /* data.flags must already be set before doing this. */
1095 mmc_set_data_timeout(&data, card);
1096
1097 /*
1098 * Allow overriding the timeout_ns for empirical tuning.
1099 */
1100 if (curr_cmd->data_timeout_ns)
1101 data.timeout_ns = curr_cmd->data_timeout_ns;
1102
1103 mrq.data = &data;
1104 }
1105
1106 mrq.cmd = &cmd;
1107
1108 err = mmc_set_blockcount(card, data.blocks,
1109 curr_cmd->write_flag & (1 << 31));
1110 if (err)
1111 goto cmd_rel_host;
1112
1113 mmc_wait_for_req(card->host, &mrq);
1114
1115 if (cmd.error) {
1116 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1117 __func__, cmd.error);
1118 err = cmd.error;
1119 goto cmd_rel_host;
1120 }
1121 if (data.error) {
1122 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1123 __func__, data.error);
1124 err = data.error;
1125 goto cmd_rel_host;
1126 }
1127
1128 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1129 sizeof(cmd.resp))) {
1130 err = -EFAULT;
1131 goto cmd_rel_host;
1132 }
1133
1134 if (!curr_cmd->write_flag) {
1135 if (copy_to_user((void __user *)(unsigned long)
1136 curr_cmd->data_ptr,
1137 curr_data->buf,
1138 curr_data->buf_bytes)) {
1139 err = -EFAULT;
1140 goto cmd_rel_host;
1141 }
1142 }
1143
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001144 /*
1145 * Ensure RPMB command has completed by polling CMD13
1146 * "Send Status".
1147 */
1148 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1149 if (err)
1150 dev_err(mmc_dev(card->host),
1151 "%s: Card Status=0x%08X, error %d\n",
1152 __func__, status, err);
1153 }
1154
Krishna Kondae6711632014-12-04 15:20:57 +02001155cmd_rel_host:
1156 mmc_put_card(card);
1157
1158idata_free:
1159 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1160 kfree(idata->data[i]->buf);
1161 kfree(idata->data[i]);
1162 }
1163 kfree(idata);
1164
1165cmd_done:
1166 mmc_blk_put(md);
Subhash Jadavani692984f2015-10-19 12:04:46 -07001167 if (card && card->cmdq_init)
1168 wake_up(&card->host->cmdq_ctx.wait);
Jon Huntera5f57742015-09-22 10:27:53 +01001169 return err;
1170}
1171
1172static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1173 struct mmc_ioc_cmd __user *ic_ptr)
1174{
1175 struct mmc_blk_ioc_data *idata;
1176 struct mmc_blk_data *md;
1177 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001178 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001179
Shawn Lin83c742c2016-03-16 18:15:47 +08001180 /*
1181 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1182 * whole block device, not on a partition. This prevents overspray
1183 * between sibling partitions.
1184 */
1185 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1186 return -EPERM;
1187
Jon Huntera5f57742015-09-22 10:27:53 +01001188 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301189 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001190 return PTR_ERR(idata);
1191
1192 md = mmc_blk_get(bdev->bd_disk);
1193 if (!md) {
1194 err = -EINVAL;
1195 goto cmd_err;
1196 }
1197
1198 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301199 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001200 err = PTR_ERR(card);
1201 goto cmd_done;
1202 }
1203
1204 mmc_get_card(card);
1205
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301206 if (mmc_card_cmdq(card)) {
1207 err = mmc_cmdq_halt_on_empty_queue(card->host);
1208 if (err) {
1209 pr_err("%s: halt failed while doing %s err (%d)\n",
1210 mmc_hostname(card->host),
1211 __func__, err);
1212 mmc_put_card(card);
1213 goto cmd_done;
1214 }
1215 }
1216
Grant Grundlerb0934102015-09-23 18:30:33 -07001217 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001218
Adrian Hunter3c866562016-05-04 14:38:12 +03001219 /* Always switch back to main area after RPMB access */
1220 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1221 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1222
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001223 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001224
Grant Grundlerb0934102015-09-23 18:30:33 -07001225 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001226
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301227 if (mmc_card_cmdq(card)) {
1228 if (mmc_cmdq_halt(card->host, false))
1229 pr_err("%s: %s: cmdq unhalt failed\n",
1230 mmc_hostname(card->host), __func__);
1231 }
1232
John Calixtocb87ea22011-04-26 18:56:29 -04001233cmd_done:
1234 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001235cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001236 kfree(idata->buf);
1237 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001238 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001239}
1240
Jon Huntera5f57742015-09-22 10:27:53 +01001241static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1242 struct mmc_ioc_multi_cmd __user *user)
1243{
1244 struct mmc_blk_ioc_data **idata = NULL;
1245 struct mmc_ioc_cmd __user *cmds = user->cmds;
1246 struct mmc_card *card;
1247 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001248 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001249 __u64 num_of_cmds;
1250
Shawn Lin83c742c2016-03-16 18:15:47 +08001251 /*
1252 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1253 * whole block device, not on a partition. This prevents overspray
1254 * between sibling partitions.
1255 */
1256 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1257 return -EPERM;
1258
Jon Huntera5f57742015-09-22 10:27:53 +01001259 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1260 sizeof(num_of_cmds)))
1261 return -EFAULT;
1262
1263 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1264 return -EINVAL;
1265
1266 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1267 if (!idata)
1268 return -ENOMEM;
1269
1270 for (i = 0; i < num_of_cmds; i++) {
1271 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1272 if (IS_ERR(idata[i])) {
1273 err = PTR_ERR(idata[i]);
1274 num_of_cmds = i;
1275 goto cmd_err;
1276 }
1277 }
1278
1279 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001280 if (!md) {
1281 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001282 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001283 }
Jon Huntera5f57742015-09-22 10:27:53 +01001284
1285 card = md->queue.card;
1286 if (IS_ERR(card)) {
1287 err = PTR_ERR(card);
1288 goto cmd_done;
1289 }
1290
1291 mmc_get_card(card);
1292
Grant Grundlerb0934102015-09-23 18:30:33 -07001293 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1294 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001295
Adrian Hunter3c866562016-05-04 14:38:12 +03001296 /* Always switch back to main area after RPMB access */
1297 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1298 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1299
Jon Huntera5f57742015-09-22 10:27:53 +01001300 mmc_put_card(card);
1301
1302 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001303 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001304 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001305
1306cmd_done:
1307 mmc_blk_put(md);
1308cmd_err:
1309 for (i = 0; i < num_of_cmds; i++) {
1310 kfree(idata[i]->buf);
1311 kfree(idata[i]);
1312 }
1313 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001314 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001315}
1316
John Calixtocb87ea22011-04-26 18:56:29 -04001317static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1318 unsigned int cmd, unsigned long arg)
1319{
Jon Huntera5f57742015-09-22 10:27:53 +01001320 switch (cmd) {
1321 case MMC_IOC_CMD:
1322 return mmc_blk_ioctl_cmd(bdev,
1323 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001324 case MMC_IOC_RPMB_CMD:
1325 return mmc_blk_ioctl_rpmb_cmd(bdev,
1326 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001327 case MMC_IOC_MULTI_CMD:
1328 return mmc_blk_ioctl_multi_cmd(bdev,
1329 (struct mmc_ioc_multi_cmd __user *)arg);
1330 default:
1331 return -EINVAL;
1332 }
John Calixtocb87ea22011-04-26 18:56:29 -04001333}
1334
1335#ifdef CONFIG_COMPAT
1336static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1337 unsigned int cmd, unsigned long arg)
1338{
1339 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1340}
1341#endif
1342
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001343static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001344 .open = mmc_blk_open,
1345 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001346 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001348 .ioctl = mmc_blk_ioctl,
1349#ifdef CONFIG_COMPAT
1350 .compat_ioctl = mmc_blk_compat_ioctl,
1351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352};
1353
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001354static int mmc_blk_cmdq_switch(struct mmc_card *card,
1355 struct mmc_blk_data *md, bool enable)
1356{
1357 int ret = 0;
1358 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301359 struct mmc_host *host = card->host;
1360 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001361
1362 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1363 !card->ext_csd.cmdq_support ||
1364 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1365 (cmdq_mode == enable))
1366 return 0;
1367
1368 if (enable) {
1369 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1370 if (ret) {
1371 pr_err("%s: failed (%d) to set block-size to %d\n",
1372 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1373 goto out;
1374 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301375
1376 } else {
1377 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1378 ret = mmc_cmdq_halt(host, true);
1379 if (ret) {
1380 pr_err("%s: halt: failed: %d\n",
1381 mmc_hostname(host), ret);
1382 goto out;
1383 }
1384 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001385 }
1386
1387 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1388 EXT_CSD_CMDQ, enable,
1389 card->ext_csd.generic_cmd6_time);
1390 if (ret) {
1391 pr_err("%s: cmdq mode %sable failed %d\n",
1392 md->disk->disk_name, enable ? "en" : "dis", ret);
1393 goto out;
1394 }
1395
1396 if (enable)
1397 mmc_card_set_cmdq(card);
1398 else
1399 mmc_card_clr_cmdq(card);
1400out:
1401 return ret;
1402}
1403
Andrei Warkentin371a6892011-04-11 18:10:25 -05001404static inline int mmc_blk_part_switch(struct mmc_card *card,
1405 struct mmc_blk_data *md)
1406{
1407 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001408 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001409
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001410 if ((main_md->part_curr == md->part_type) &&
1411 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001412 return 0;
1413
1414 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001415 u8 part_config = card->ext_csd.part_config;
1416
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001417 if (md->part_type) {
1418 /* disable CQ mode for non-user data partitions */
1419 ret = mmc_blk_cmdq_switch(card, md, false);
1420 if (ret)
1421 return ret;
1422 }
1423
Adrian Hunter57da0c02016-05-04 14:38:13 +03001424 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1425 mmc_retune_pause(card->host);
1426
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001427 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1428 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001429
1430 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001431 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001432 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001433 if (ret) {
1434 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1435 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001436 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001437 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001438
1439 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001440 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001441
1442 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1443 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001444 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001445
1446 main_md->part_curr = md->part_type;
1447 return 0;
1448}
1449
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001450static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1451{
1452 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001453 u32 result;
1454 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001455
Venkatraman Sad5fd972011-08-25 00:30:50 +05301456 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001457 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001458 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001459
1460 struct scatterlist sg;
1461
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001462 cmd.opcode = MMC_APP_CMD;
1463 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001464 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001465
1466 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001467 if (err)
1468 return (u32)-1;
1469 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001470 return (u32)-1;
1471
1472 memset(&cmd, 0, sizeof(struct mmc_command));
1473
1474 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1475 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001476 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001477
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001478 data.blksz = 4;
1479 data.blocks = 1;
1480 data.flags = MMC_DATA_READ;
1481 data.sg = &sg;
1482 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301483 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001484
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001485 mrq.cmd = &cmd;
1486 mrq.data = &data;
1487
Ben Dooks051913d2009-06-08 23:33:57 +01001488 blocks = kmalloc(4, GFP_KERNEL);
1489 if (!blocks)
1490 return (u32)-1;
1491
1492 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001493
1494 mmc_wait_for_req(card->host, &mrq);
1495
Ben Dooks051913d2009-06-08 23:33:57 +01001496 result = ntohl(*blocks);
1497 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001498
Ben Dooks051913d2009-06-08 23:33:57 +01001499 if (cmd.error || data.error)
1500 result = (u32)-1;
1501
1502 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001503}
1504
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001505static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001506{
Chris Ball1278dba2011-04-13 23:40:30 -04001507 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001508 int err;
1509
Adrian Hunter504f1912008-10-16 12:55:25 +03001510 cmd.opcode = MMC_SEND_STATUS;
1511 if (!mmc_host_is_spi(card->host))
1512 cmd.arg = card->rca << 16;
1513 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001514 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1515 if (err == 0)
1516 *status = cmd.resp[0];
1517 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001518}
1519
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001520static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001521 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001522{
1523 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1524 int err = 0;
1525 u32 status;
1526
1527 do {
1528 err = get_card_status(card, &status, 5);
1529 if (err) {
1530 pr_err("%s: error %d requesting status\n",
1531 req->rq_disk->disk_name, err);
1532 return err;
1533 }
1534
1535 if (status & R1_ERROR) {
1536 pr_err("%s: %s: error sending status cmd, status %#x\n",
1537 req->rq_disk->disk_name, __func__, status);
1538 *gen_err = 1;
1539 }
1540
Ulf Hansson95a91292014-01-29 13:11:27 +01001541 /* We may rely on the host hw to handle busy detection.*/
1542 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1543 hw_busy_detect)
1544 break;
1545
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001546 /*
1547 * Timeout if the device never becomes ready for data and never
1548 * leaves the program state.
1549 */
1550 if (time_after(jiffies, timeout)) {
1551 pr_err("%s: Card stuck in programming state! %s %s\n",
1552 mmc_hostname(card->host),
1553 req->rq_disk->disk_name, __func__);
1554 return -ETIMEDOUT;
1555 }
1556
1557 /*
1558 * Some cards mishandle the status bits,
1559 * so make sure to check both the busy
1560 * indication and the card state.
1561 */
1562 } while (!(status & R1_READY_FOR_DATA) ||
1563 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1564
1565 return err;
1566}
1567
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001568static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1569 struct request *req, int *gen_err, u32 *stop_status)
1570{
1571 struct mmc_host *host = card->host;
1572 struct mmc_command cmd = {0};
1573 int err;
1574 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1575
1576 /*
1577 * Normally we use R1B responses for WRITE, but in cases where the host
1578 * has specified a max_busy_timeout we need to validate it. A failure
1579 * means we need to prevent the host from doing hw busy detection, which
1580 * is done by converting to a R1 response instead.
1581 */
1582 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1583 use_r1b_resp = false;
1584
1585 cmd.opcode = MMC_STOP_TRANSMISSION;
1586 if (use_r1b_resp) {
1587 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1588 cmd.busy_timeout = timeout_ms;
1589 } else {
1590 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1591 }
1592
1593 err = mmc_wait_for_cmd(host, &cmd, 5);
1594 if (err)
1595 return err;
1596
1597 *stop_status = cmd.resp[0];
1598
1599 /* No need to check card status in case of READ. */
1600 if (rq_data_dir(req) == READ)
1601 return 0;
1602
1603 if (!mmc_host_is_spi(host) &&
1604 (*stop_status & R1_ERROR)) {
1605 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1606 req->rq_disk->disk_name, __func__, *stop_status);
1607 *gen_err = 1;
1608 }
1609
1610 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1611}
1612
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301613#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001614#define ERR_RETRY 2
1615#define ERR_ABORT 1
1616#define ERR_CONTINUE 0
1617
1618static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1619 bool status_valid, u32 status)
1620{
1621 switch (error) {
1622 case -EILSEQ:
1623 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001624 pr_err_ratelimited(
1625 "%s: response CRC error sending %s command, card status %#x\n",
1626 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001627 name, status);
1628 return ERR_RETRY;
1629
1630 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001631 pr_err_ratelimited(
1632 "%s: timed out sending %s command, card status %#x\n",
1633 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001634
1635 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301636 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001637 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301638 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001639 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301640 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001641
1642 /*
1643 * If it was a r/w cmd crc error, or illegal command
1644 * (eg, issued in wrong state) then retry - we should
1645 * have corrected the state problem above.
1646 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301647 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001648 pr_err_ratelimited(
1649 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301650 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001651 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301652 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001653
1654 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001655 pr_err_ratelimited(
1656 "%s: not retrying timeout\n",
1657 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001658 return ERR_ABORT;
1659
1660 default:
1661 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001662 pr_err_ratelimited(
1663 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001664 req->rq_disk->disk_name, error, status);
1665 return ERR_ABORT;
1666 }
1667}
1668
1669/*
1670 * Initial r/w and stop cmd error recovery.
1671 * We don't know whether the card received the r/w cmd or not, so try to
1672 * restore things back to a sane state. Essentially, we do this as follows:
1673 * - Obtain card status. If the first attempt to obtain card status fails,
1674 * the status word will reflect the failed status cmd, not the failed
1675 * r/w cmd. If we fail to obtain card status, it suggests we can no
1676 * longer communicate with the card.
1677 * - Check the card state. If the card received the cmd but there was a
1678 * transient problem with the response, it might still be in a data transfer
1679 * mode. Try to send it a stop command. If this fails, we can't recover.
1680 * - If the r/w cmd failed due to a response CRC error, it was probably
1681 * transient, so retry the cmd.
1682 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1683 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1684 * illegal cmd, retry.
1685 * Otherwise we don't understand what happened, so abort.
1686 */
1687static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001688 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001689{
1690 bool prev_cmd_status_valid = true;
1691 u32 status, stop_status = 0;
1692 int err, retry;
1693
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301694 if (mmc_card_removed(card))
1695 return ERR_NOMEDIUM;
1696
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001697 /*
1698 * Try to get card status which indicates both the card state
1699 * and why there was no response. If the first attempt fails,
1700 * we can't be sure the returned status is for the r/w command.
1701 */
1702 for (retry = 2; retry >= 0; retry--) {
1703 err = get_card_status(card, &status, 0);
1704 if (!err)
1705 break;
1706
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001707 /* Re-tune if needed */
1708 mmc_retune_recheck(card->host);
1709
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001710 prev_cmd_status_valid = false;
1711 pr_err("%s: error %d sending status command, %sing\n",
1712 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1713 }
1714
1715 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301716 if (err) {
1717 /* Check if the card is removed */
1718 if (mmc_detect_card_removed(card->host))
1719 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001720 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301721 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001722
Adrian Hunter67716322011-08-29 16:42:15 +03001723 /* Flag ECC errors */
1724 if ((status & R1_CARD_ECC_FAILED) ||
1725 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1726 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1727 *ecc_err = 1;
1728
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001729 /* Flag General errors */
1730 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1731 if ((status & R1_ERROR) ||
1732 (brq->stop.resp[0] & R1_ERROR)) {
1733 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1734 req->rq_disk->disk_name, __func__,
1735 brq->stop.resp[0], status);
1736 *gen_err = 1;
1737 }
1738
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001739 /*
1740 * Check the current card state. If it is in some data transfer
1741 * mode, tell it to stop (and hopefully transition back to TRAN.)
1742 */
1743 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1744 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001745 err = send_stop(card,
1746 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1747 req, gen_err, &stop_status);
1748 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001749 pr_err("%s: error %d sending stop command\n",
1750 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001751 /*
1752 * If the stop cmd also timed out, the card is probably
1753 * not present, so abort. Other errors are bad news too.
1754 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001755 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001756 }
1757
Adrian Hunter67716322011-08-29 16:42:15 +03001758 if (stop_status & R1_CARD_ECC_FAILED)
1759 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001760 }
1761
1762 /* Check for set block count errors */
1763 if (brq->sbc.error)
1764 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1765 prev_cmd_status_valid, status);
1766
1767 /* Check for r/w command errors */
1768 if (brq->cmd.error)
1769 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1770 prev_cmd_status_valid, status);
1771
Adrian Hunter67716322011-08-29 16:42:15 +03001772 /* Data errors */
1773 if (!brq->stop.error)
1774 return ERR_CONTINUE;
1775
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001776 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001777 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 +01001778 req->rq_disk->disk_name, brq->stop.error,
1779 brq->cmd.resp[0], status);
1780
1781 /*
1782 * Subsitute in our own stop status as this will give the error
1783 * state which happened during the execution of the r/w command.
1784 */
1785 if (stop_status) {
1786 brq->stop.resp[0] = stop_status;
1787 brq->stop.error = 0;
1788 }
1789 return ERR_CONTINUE;
1790}
1791
Adrian Hunter67716322011-08-29 16:42:15 +03001792static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1793 int type)
1794{
1795 int err;
1796
1797 if (md->reset_done & type)
1798 return -EEXIST;
1799
1800 md->reset_done |= type;
1801 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301802 if (err && err != -EOPNOTSUPP) {
1803 /* We failed to reset so we need to abort the request */
1804 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1805 __func__, err);
1806 return -ENODEV;
1807 }
1808
Adrian Hunter67716322011-08-29 16:42:15 +03001809 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301810 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001811 struct mmc_blk_data *main_md =
1812 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001813 int part_err;
1814
1815 main_md->part_curr = main_md->part_type;
1816 part_err = mmc_blk_part_switch(host->card, md);
1817 if (part_err) {
1818 /*
1819 * We have failed to get back into the correct
1820 * partition, so we need to abort the whole request.
1821 */
1822 return -ENODEV;
1823 }
1824 }
1825 return err;
1826}
1827
1828static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1829{
1830 md->reset_done &= ~type;
1831}
1832
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001833int mmc_access_rpmb(struct mmc_queue *mq)
1834{
1835 struct mmc_blk_data *md = mq->data;
1836 /*
1837 * If this is a RPMB partition access, return ture
1838 */
1839 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1840 return true;
1841
1842 return false;
1843}
1844
Sahitya Tummala9433a132015-06-09 09:38:36 +05301845static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1846 struct request *req)
1847{
1848 struct mmc_blk_data *md = mq->data;
1849 struct mmc_card *card = md->queue.card;
1850 struct mmc_host *host = card->host;
1851 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1852 struct mmc_cmdq_req *cmdq_req;
1853 struct mmc_queue_req *active_mqrq;
1854
1855 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1856 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1857
1858 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1859
1860 active_mqrq = &mq->mqrq_cmdq[req->tag];
1861 active_mqrq->req = req;
1862
1863 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1864 cmdq_req->cmdq_req_flags |= QBR;
1865 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1866 cmdq_req->tag = req->tag;
1867 return cmdq_req;
1868}
1869
1870static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1871 struct request *req)
1872{
1873 struct mmc_blk_data *md = mq->data;
1874 struct mmc_card *card = md->queue.card;
1875 struct mmc_cmdq_req *cmdq_req = NULL;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301876 unsigned int from, nr, arg;
1877 int err = 0;
1878
1879 if (!mmc_can_erase(card)) {
1880 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301881 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301882 goto out;
1883 }
1884
1885 from = blk_rq_pos(req);
1886 nr = blk_rq_sectors(req);
1887
1888 if (mmc_can_discard(card))
1889 arg = MMC_DISCARD_ARG;
1890 else if (mmc_can_trim(card))
1891 arg = MMC_TRIM_ARG;
1892 else
1893 arg = MMC_ERASE_ARG;
1894
1895 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1896 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1897 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1898 EXT_CSD_CMD_SET_NORMAL,
1899 INAND_CMD38_ARG_EXT_CSD,
1900 arg == MMC_TRIM_ARG ?
1901 INAND_CMD38_ARG_TRIM :
1902 INAND_CMD38_ARG_ERASE,
1903 0, true, false);
1904 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1905 if (err)
1906 goto clear_dcmd;
1907 }
1908 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1909clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301910 mmc_host_clk_hold(card->host);
1911 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301912out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05301913 return err ? 1 : 0;
1914}
1915
Adrian Hunterbd788c92010-08-11 14:17:47 -07001916static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1917{
1918 struct mmc_blk_data *md = mq->data;
1919 struct mmc_card *card = md->queue.card;
1920 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001921 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001922
Adrian Hunterbd788c92010-08-11 14:17:47 -07001923 if (!mmc_can_erase(card)) {
1924 err = -EOPNOTSUPP;
1925 goto out;
1926 }
1927
1928 from = blk_rq_pos(req);
1929 nr = blk_rq_sectors(req);
1930
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001931 if (mmc_can_discard(card))
1932 arg = MMC_DISCARD_ARG;
1933 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001934 arg = MMC_TRIM_ARG;
1935 else
1936 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001937retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001938 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1939 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1940 INAND_CMD38_ARG_EXT_CSD,
1941 arg == MMC_TRIM_ARG ?
1942 INAND_CMD38_ARG_TRIM :
1943 INAND_CMD38_ARG_ERASE,
1944 0);
1945 if (err)
1946 goto out;
1947 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001948 err = mmc_erase(card, from, nr, arg);
1949out:
Adrian Hunter67716322011-08-29 16:42:15 +03001950 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1951 goto retry;
1952 if (!err)
1953 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301954 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001955
Adrian Hunterbd788c92010-08-11 14:17:47 -07001956 return err ? 0 : 1;
1957}
1958
Sahitya Tummala9433a132015-06-09 09:38:36 +05301959static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1960 struct request *req)
1961{
1962 struct mmc_blk_data *md = mq->data;
1963 struct mmc_card *card = md->queue.card;
1964 struct mmc_cmdq_req *cmdq_req = NULL;
1965 unsigned int from, nr, arg;
Sahitya Tummala9433a132015-06-09 09:38:36 +05301966 int err = 0;
1967
1968 if (!(mmc_can_secure_erase_trim(card))) {
1969 err = -EOPNOTSUPP;
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05301970 blk_end_request(req, err, blk_rq_bytes(req));
Sahitya Tummala9433a132015-06-09 09:38:36 +05301971 goto out;
1972 }
1973
1974 from = blk_rq_pos(req);
1975 nr = blk_rq_sectors(req);
1976
1977 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1978 arg = MMC_SECURE_TRIM1_ARG;
1979 else
1980 arg = MMC_SECURE_ERASE_ARG;
1981
1982 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1983 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1984 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1985 EXT_CSD_CMD_SET_NORMAL,
1986 INAND_CMD38_ARG_EXT_CSD,
1987 arg == MMC_SECURE_TRIM1_ARG ?
1988 INAND_CMD38_ARG_SECTRIM1 :
1989 INAND_CMD38_ARG_SECERASE,
1990 0, true, false);
1991 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1992 if (err)
1993 goto clear_dcmd;
1994 }
1995
1996 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1997 if (err)
1998 goto clear_dcmd;
1999
2000 if (arg == MMC_SECURE_TRIM1_ARG) {
2001 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2002 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
2003 EXT_CSD_CMD_SET_NORMAL,
2004 INAND_CMD38_ARG_EXT_CSD,
2005 INAND_CMD38_ARG_SECTRIM2,
2006 0, true, false);
2007 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2008 if (err)
2009 goto clear_dcmd;
2010 }
2011
2012 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
2013 MMC_SECURE_TRIM2_ARG);
2014 }
2015clear_dcmd:
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05302016 mmc_host_clk_hold(card->host);
2017 blk_complete_request(req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302018out:
Sahitya Tummala9433a132015-06-09 09:38:36 +05302019 return err ? 1 : 0;
2020}
2021
Adrian Hunter49804542010-08-11 14:17:50 -07002022static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2023 struct request *req)
2024{
2025 struct mmc_blk_data *md = mq->data;
2026 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002027 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002028 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002029
Maya Erez775a9362013-04-18 15:41:55 +03002030 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002031 err = -EOPNOTSUPP;
2032 goto out;
2033 }
2034
2035 from = blk_rq_pos(req);
2036 nr = blk_rq_sectors(req);
2037
Maya Erez775a9362013-04-18 15:41:55 +03002038 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2039 arg = MMC_SECURE_TRIM1_ARG;
2040 else
2041 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002042
Adrian Hunter67716322011-08-29 16:42:15 +03002043retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002044 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2045 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2046 INAND_CMD38_ARG_EXT_CSD,
2047 arg == MMC_SECURE_TRIM1_ARG ?
2048 INAND_CMD38_ARG_SECTRIM1 :
2049 INAND_CMD38_ARG_SECERASE,
2050 0);
2051 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002052 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002053 }
Adrian Hunter28302812012-04-05 14:45:48 +03002054
Adrian Hunter49804542010-08-11 14:17:50 -07002055 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002056 if (err == -EIO)
2057 goto out_retry;
2058 if (err)
2059 goto out;
2060
2061 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002062 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2063 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2064 INAND_CMD38_ARG_EXT_CSD,
2065 INAND_CMD38_ARG_SECTRIM2,
2066 0);
2067 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002068 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002069 }
Adrian Hunter28302812012-04-05 14:45:48 +03002070
Adrian Hunter49804542010-08-11 14:17:50 -07002071 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002072 if (err == -EIO)
2073 goto out_retry;
2074 if (err)
2075 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002076 }
Adrian Hunter28302812012-04-05 14:45:48 +03002077
Adrian Hunter28302812012-04-05 14:45:48 +03002078out_retry:
2079 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002080 goto retry;
2081 if (!err)
2082 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002083out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302084 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002085
Adrian Hunter49804542010-08-11 14:17:50 -07002086 return err ? 0 : 1;
2087}
2088
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002089static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2090{
2091 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002092 struct mmc_card *card = md->queue.card;
2093 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002094
Sahitya Tummala61868a42015-05-28 16:54:19 +05302095 if (!req)
2096 return 0;
2097
2098 if (req->cmd_flags & REQ_BARRIER) {
2099 /*
2100 * If eMMC cache flush policy is set to 1, then the device
2101 * shall flush the requests in First-In-First-Out (FIFO) order.
2102 * In this case, as per spec, the host must not send any cache
2103 * barrier requests as they are redundant and add unnecessary
2104 * overhead to both device and host.
2105 */
2106 if (card->ext_csd.cache_flush_policy & 1)
2107 goto end_req;
2108
2109 /*
2110 * In case barrier is not supported or enabled in the device,
2111 * use flush as a fallback option.
2112 */
2113 ret = mmc_cache_barrier(card);
2114 if (ret)
2115 ret = mmc_flush_cache(card);
2116 } else if (req_op(req) == REQ_OP_FLUSH) {
2117 ret = mmc_flush_cache(card);
2118 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002119 if (ret == -ENODEV) {
2120 pr_err("%s: %s: restart mmc card",
2121 req->rq_disk->disk_name, __func__);
2122 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2123 pr_err("%s: %s: fail to restart mmc",
2124 req->rq_disk->disk_name, __func__);
2125 else
2126 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2127 }
2128
2129 if (ret) {
2130 pr_err("%s: %s: notify flush error to upper layers",
2131 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002132 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002133 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002134
Mark Salyzyn6904e432016-01-28 11:12:25 -08002135#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2136 else if (atomic_read(&mq->cache_size)) {
2137 long used = mmc_blk_cache_used(mq, jiffies);
2138
2139 if (used) {
2140 int speed = atomic_read(&mq->max_write_speed);
2141
2142 if (speed_valid(speed)) {
2143 unsigned long msecs = jiffies_to_msecs(
2144 size_and_speed_to_jiffies(
2145 used, speed));
2146 if (msecs)
2147 msleep(msecs);
2148 }
2149 }
2150 }
2151#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302152end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302153 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002154
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002155 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002156}
2157
2158/*
2159 * Reformat current write as a reliable write, supporting
2160 * both legacy and the enhanced reliable write MMC cards.
2161 * In each transfer we'll handle only as much as a single
2162 * reliable write can handle, thus finish the request in
2163 * partial completions.
2164 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002165static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2166 struct mmc_card *card,
2167 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002168{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002169 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2170 /* Legacy mode imposes restrictions on transfers. */
2171 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2172 brq->data.blocks = 1;
2173
2174 if (brq->data.blocks > card->ext_csd.rel_sectors)
2175 brq->data.blocks = card->ext_csd.rel_sectors;
2176 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2177 brq->data.blocks = 1;
2178 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002179}
2180
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002181#define CMD_ERRORS \
2182 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2183 R1_ADDRESS_ERROR | /* Misaligned address */ \
2184 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2185 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2186 R1_CC_ERROR | /* Card controller error */ \
2187 R1_ERROR) /* General/unknown error */
2188
Per Forlinee8a43a2011-07-01 18:55:33 +02002189static int mmc_blk_err_check(struct mmc_card *card,
2190 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002191{
Per Forlinee8a43a2011-07-01 18:55:33 +02002192 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2193 mmc_active);
2194 struct mmc_blk_request *brq = &mq_mrq->brq;
2195 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002196 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002197 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002198
2199 /*
2200 * sbc.error indicates a problem with the set block count
2201 * command. No data will have been transferred.
2202 *
2203 * cmd.error indicates a problem with the r/w command. No
2204 * data will have been transferred.
2205 *
2206 * stop.error indicates a problem with the stop command. Data
2207 * may have been transferred, or may still be transferring.
2208 */
Adrian Hunter67716322011-08-29 16:42:15 +03002209 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2210 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002211 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002212 case ERR_RETRY:
2213 return MMC_BLK_RETRY;
2214 case ERR_ABORT:
2215 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302216 case ERR_NOMEDIUM:
2217 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002218 case ERR_CONTINUE:
2219 break;
2220 }
2221 }
2222
2223 /*
2224 * Check for errors relating to the execution of the
2225 * initial command - such as address errors. No data
2226 * has been transferred.
2227 */
2228 if (brq->cmd.resp[0] & CMD_ERRORS) {
2229 pr_err("%s: r/w command failed, status = %#x\n",
2230 req->rq_disk->disk_name, brq->cmd.resp[0]);
2231 return MMC_BLK_ABORT;
2232 }
2233
2234 /*
2235 * Everything else is either success, or a data error of some
2236 * kind. If it was a write, we may have transitioned to
2237 * program mode, which we have to wait for it to complete.
2238 */
2239 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002240 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002241
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002242 /* Check stop command response */
2243 if (brq->stop.resp[0] & R1_ERROR) {
2244 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2245 req->rq_disk->disk_name, __func__,
2246 brq->stop.resp[0]);
2247 gen_err = 1;
2248 }
2249
Ulf Hansson95a91292014-01-29 13:11:27 +01002250 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2251 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002252 if (err)
2253 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002254 }
2255
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002256 /* if general error occurs, retry the write operation. */
2257 if (gen_err) {
2258 pr_warn("%s: retrying write for general error\n",
2259 req->rq_disk->disk_name);
2260 return MMC_BLK_RETRY;
2261 }
2262
Per Forlind78d4a82011-07-01 18:55:30 +02002263 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002264 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002265 pr_debug("%s: retrying because a re-tune was needed\n",
2266 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002267 brq->retune_retry_done = 1;
2268 return MMC_BLK_RETRY;
2269 }
Per Forlind78d4a82011-07-01 18:55:30 +02002270 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2271 req->rq_disk->disk_name, brq->data.error,
2272 (unsigned)blk_rq_pos(req),
2273 (unsigned)blk_rq_sectors(req),
2274 brq->cmd.resp[0], brq->stop.resp[0]);
2275
2276 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002277 if (ecc_err)
2278 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002279 return MMC_BLK_DATA_ERR;
2280 } else {
2281 return MMC_BLK_CMD_ERR;
2282 }
2283 }
2284
Adrian Hunter67716322011-08-29 16:42:15 +03002285 if (!brq->data.bytes_xfered)
2286 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002287
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002288 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2289 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2290 return MMC_BLK_PARTIAL;
2291 else
2292 return MMC_BLK_SUCCESS;
2293 }
2294
Adrian Hunter67716322011-08-29 16:42:15 +03002295 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2296 return MMC_BLK_PARTIAL;
2297
2298 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002299}
2300
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002301static int mmc_blk_packed_err_check(struct mmc_card *card,
2302 struct mmc_async_req *areq)
2303{
2304 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2305 mmc_active);
2306 struct request *req = mq_rq->req;
2307 struct mmc_packed *packed = mq_rq->packed;
2308 int err, check, status;
2309 u8 *ext_csd;
2310
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002311 packed->retries--;
2312 check = mmc_blk_err_check(card, areq);
2313 err = get_card_status(card, &status, 0);
2314 if (err) {
2315 pr_err("%s: error %d sending status command\n",
2316 req->rq_disk->disk_name, err);
2317 return MMC_BLK_ABORT;
2318 }
2319
2320 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002321 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002322 if (err) {
2323 pr_err("%s: error %d sending ext_csd\n",
2324 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002325 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002326 }
2327
2328 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2329 EXT_CSD_PACKED_FAILURE) &&
2330 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2331 EXT_CSD_PACKED_GENERIC_ERROR)) {
2332 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2333 EXT_CSD_PACKED_INDEXED_ERROR) {
2334 packed->idx_failure =
2335 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2336 check = MMC_BLK_PARTIAL;
2337 }
2338 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2339 "failure index: %d\n",
2340 req->rq_disk->disk_name, packed->nr_entries,
2341 packed->blocks, packed->idx_failure);
2342 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002343 kfree(ext_csd);
2344 }
2345
2346 return check;
2347}
2348
Per Forlin54d49d72011-07-01 18:55:29 +02002349static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2350 struct mmc_card *card,
2351 int disable_multi,
2352 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Per Forlin54d49d72011-07-01 18:55:29 +02002354 u32 readcmd, writecmd;
2355 struct mmc_blk_request *brq = &mqrq->brq;
2356 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302358 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002360 /*
2361 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002362 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002363 */
Luca Porziod3df0462015-11-06 15:12:26 +00002364 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002365 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002366 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002367
Per Forlin54d49d72011-07-01 18:55:29 +02002368 memset(brq, 0, sizeof(struct mmc_blk_request));
2369 brq->mrq.cmd = &brq->cmd;
2370 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Per Forlin54d49d72011-07-01 18:55:29 +02002372 brq->cmd.arg = blk_rq_pos(req);
2373 if (!mmc_card_blockaddr(card))
2374 brq->cmd.arg <<= 9;
2375 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2376 brq->data.blksz = 512;
2377 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2378 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002379 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Asutosh Dasf0665412012-07-27 18:10:19 +05302381 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002382 /*
2383 * The block layer doesn't support all sector count
2384 * restrictions, so we need to be prepared for too big
2385 * requests.
2386 */
2387 if (brq->data.blocks > card->host->max_blk_count)
2388 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002390 if (brq->data.blocks > 1) {
2391 /*
2392 * After a read error, we redo the request one sector
2393 * at a time in order to accurately determine which
2394 * sectors can be read successfully.
2395 */
2396 if (disable_multi)
2397 brq->data.blocks = 1;
2398
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002399 /*
2400 * Some controllers have HW issues while operating
2401 * in multiple I/O mode
2402 */
2403 if (card->host->ops->multi_io_quirk)
2404 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2405 (rq_data_dir(req) == READ) ?
2406 MMC_DATA_READ : MMC_DATA_WRITE,
2407 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002408 }
Per Forlin54d49d72011-07-01 18:55:29 +02002409
2410 if (brq->data.blocks > 1 || do_rel_wr) {
2411 /* SPI multiblock writes terminate using a special
2412 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002413 */
Per Forlin54d49d72011-07-01 18:55:29 +02002414 if (!mmc_host_is_spi(card->host) ||
2415 rq_data_dir(req) == READ)
2416 brq->mrq.stop = &brq->stop;
2417 readcmd = MMC_READ_MULTIPLE_BLOCK;
2418 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2419 } else {
2420 brq->mrq.stop = NULL;
2421 readcmd = MMC_READ_SINGLE_BLOCK;
2422 writecmd = MMC_WRITE_BLOCK;
2423 }
2424 if (rq_data_dir(req) == READ) {
2425 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002426 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002427 if (brq->mrq.stop)
2428 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2429 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002430 } else {
2431 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002432 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002433 if (brq->mrq.stop)
2434 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2435 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002436 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002437
Per Forlin54d49d72011-07-01 18:55:29 +02002438 if (do_rel_wr)
2439 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002440
Per Forlin54d49d72011-07-01 18:55:29 +02002441 /*
Saugata Das42659002011-12-21 13:09:17 +05302442 * Data tag is used only during writing meta data to speed
2443 * up write and any subsequent read of this meta data
2444 */
2445 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2446 (req->cmd_flags & REQ_META) &&
2447 (rq_data_dir(req) == WRITE) &&
2448 ((brq->data.blocks * brq->data.blksz) >=
2449 card->ext_csd.data_tag_unit_size);
2450
2451 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002452 * Pre-defined multi-block transfers are preferable to
2453 * open ended-ones (and necessary for reliable writes).
2454 * However, it is not sufficient to just send CMD23,
2455 * and avoid the final CMD12, as on an error condition
2456 * CMD12 (stop) needs to be sent anyway. This, coupled
2457 * with Auto-CMD23 enhancements provided by some
2458 * hosts, means that the complexity of dealing
2459 * with this is best left to the host. If CMD23 is
2460 * supported by card and host, we'll fill sbc in and let
2461 * the host deal with handling it correctly. This means
2462 * that for hosts that don't expose MMC_CAP_CMD23, no
2463 * change of behavior will be observed.
2464 *
2465 * N.B: Some MMC cards experience perf degradation.
2466 * We'll avoid using CMD23-bounded multiblock writes for
2467 * these, while retaining features like reliable writes.
2468 */
Saugata Das42659002011-12-21 13:09:17 +05302469 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2470 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2471 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002472 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2473 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302474 (do_rel_wr ? (1 << 31) : 0) |
2475 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002476 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2477 brq->mrq.sbc = &brq->sbc;
2478 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002479
Per Forlin54d49d72011-07-01 18:55:29 +02002480 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002481
Per Forlin54d49d72011-07-01 18:55:29 +02002482 brq->data.sg = mqrq->sg;
2483 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002484
Per Forlin54d49d72011-07-01 18:55:29 +02002485 /*
2486 * Adjust the sg list so it is the same size as the
2487 * request.
2488 */
2489 if (brq->data.blocks != blk_rq_sectors(req)) {
2490 int i, data_size = brq->data.blocks << 9;
2491 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002492
Per Forlin54d49d72011-07-01 18:55:29 +02002493 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2494 data_size -= sg->length;
2495 if (data_size <= 0) {
2496 sg->length += data_size;
2497 i++;
2498 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002499 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002500 }
Per Forlin54d49d72011-07-01 18:55:29 +02002501 brq->data.sg_len = i;
2502 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002503
Per Forlinee8a43a2011-07-01 18:55:33 +02002504 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302505 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002506 mqrq->mmc_active.err_check = mmc_blk_err_check;
2507
Per Forlin54d49d72011-07-01 18:55:29 +02002508 mmc_queue_bounce_pre(mqrq);
2509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002511static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2512 struct mmc_card *card)
2513{
2514 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2515 unsigned int max_seg_sz = queue_max_segment_size(q);
2516 unsigned int len, nr_segs = 0;
2517
2518 do {
2519 len = min(hdr_sz, max_seg_sz);
2520 hdr_sz -= len;
2521 nr_segs++;
2522 } while (hdr_sz);
2523
2524 return nr_segs;
2525}
2526
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002527/**
2528 * mmc_blk_disable_wr_packing() - disables packing mode
2529 * @mq: MMC queue.
2530 *
2531 */
2532void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2533{
2534 if (mq) {
2535 mq->wr_packing_enabled = false;
2536 mq->num_of_potential_packed_wr_reqs = 0;
2537 }
2538}
2539EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2540
Lee Susman841fd132013-04-23 17:59:26 +03002541static int get_packed_trigger(int potential, struct mmc_card *card,
2542 struct request *req, int curr_trigger)
2543{
2544 static int num_mean_elements = 1;
2545 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2546 unsigned int trigger = curr_trigger;
2547 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2548
2549 /* scale down the upper bound to 75% */
2550 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2551
2552 /*
2553 * since the most common calls for this function are with small
2554 * potential write values and since we don't want these calls to affect
2555 * the packed trigger, set a lower bound and ignore calls with
2556 * potential lower than that bound
2557 */
2558 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2559 return trigger;
2560
2561 /*
2562 * this is to prevent integer overflow in the following calculation:
2563 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2564 */
2565 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2566 num_mean_elements = 1;
2567 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2568 }
2569
2570 /*
2571 * get next mean value based on previous mean value and current
2572 * potential packed writes. Calculation is as follows:
2573 * mean_pot[i+1] =
2574 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2575 */
2576 mean_potential *= num_mean_elements;
2577 /*
2578 * add num_mean_elements so that the division of two integers doesn't
2579 * lower mean_potential too much
2580 */
2581 if (potential > mean_potential)
2582 mean_potential += num_mean_elements;
2583 mean_potential += potential;
2584 /* this is for gaining more precision when dividing two integers */
2585 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2586 /* this completes the mean calculation */
2587 mean_potential /= ++num_mean_elements;
2588 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2589
2590 /*
2591 * if current potential packed writes is greater than the mean potential
2592 * then the heuristic is that the following workload will contain many
2593 * write requests, therefore we lower the packed trigger. In the
2594 * opposite case we want to increase the trigger in order to get less
2595 * packing events.
2596 */
2597 if (potential >= mean_potential)
2598 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2599 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2600 else
2601 trigger = (trigger >= pckd_trgr_upper_bound) ?
2602 pckd_trgr_upper_bound : trigger + 1;
2603
2604 /*
2605 * an urgent read request indicates a packed list being interrupted
2606 * by this read, therefore we aim for less packing, hence the trigger
2607 * gets increased
2608 */
2609 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2610 trigger += PCKD_TRGR_URGENT_PENALTY;
2611
2612 return trigger;
2613}
2614
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002615static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2616 struct request *req)
2617{
2618 struct mmc_host *host = mq->card->host;
2619 int data_dir;
2620
2621 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2622 return;
2623
Maya Erez8e2b3c32012-12-02 13:27:15 +02002624 /* Support for the write packing on eMMC 4.5 or later */
2625 if (mq->card->ext_csd.rev <= 5)
2626 return;
2627
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002628 /*
2629 * In case the packing control is not supported by the host, it should
2630 * not have an effect on the write packing. Therefore we have to enable
2631 * the write packing
2632 */
2633 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2634 mq->wr_packing_enabled = true;
2635 return;
2636 }
2637
2638 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2639 if (mq->num_of_potential_packed_wr_reqs >
2640 mq->num_wr_reqs_to_start_packing)
2641 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002642 mq->num_wr_reqs_to_start_packing =
2643 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2644 mq->card, req,
2645 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002646 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002647 return;
2648 }
2649
2650 data_dir = rq_data_dir(req);
2651
2652 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002653 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002654 mq->num_wr_reqs_to_start_packing =
2655 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2656 mq->card, req,
2657 mq->num_wr_reqs_to_start_packing);
2658 mq->num_of_potential_packed_wr_reqs = 0;
2659 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002660 return;
2661 } else if (data_dir == WRITE) {
2662 mq->num_of_potential_packed_wr_reqs++;
2663 }
2664
2665 if (mq->num_of_potential_packed_wr_reqs >
2666 mq->num_wr_reqs_to_start_packing)
2667 mq->wr_packing_enabled = true;
2668}
2669
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002670struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2671{
2672 if (!card)
2673 return NULL;
2674
2675 return &card->wr_pack_stats;
2676}
2677EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2678
2679void mmc_blk_init_packed_statistics(struct mmc_card *card)
2680{
2681 int max_num_of_packed_reqs = 0;
2682
2683 if (!card || !card->wr_pack_stats.packing_events)
2684 return;
2685
2686 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2687
2688 spin_lock(&card->wr_pack_stats.lock);
2689 memset(card->wr_pack_stats.packing_events, 0,
2690 (max_num_of_packed_reqs + 1) *
2691 sizeof(*card->wr_pack_stats.packing_events));
2692 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2693 sizeof(card->wr_pack_stats.pack_stop_reason));
2694 card->wr_pack_stats.enabled = true;
2695 spin_unlock(&card->wr_pack_stats.lock);
2696}
2697EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2698
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002699static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2700{
2701 struct request_queue *q = mq->queue;
2702 struct mmc_card *card = mq->card;
2703 struct request *cur = req, *next = NULL;
2704 struct mmc_blk_data *md = mq->data;
2705 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2706 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2707 unsigned int req_sectors = 0, phys_segments = 0;
2708 unsigned int max_blk_count, max_phys_segs;
2709 bool put_back = true;
2710 u8 max_packed_rw = 0;
2711 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002712 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002713
Shawn Lin96e52da2016-08-26 08:49:55 +08002714 /*
2715 * We don't need to check packed for any further
2716 * operation of packed stuff as we set MMC_PACKED_NONE
2717 * and return zero for reqs if geting null packed. Also
2718 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2719 * it again when removing blk req.
2720 */
2721 if (!mqrq->packed) {
2722 md->flags &= (~MMC_BLK_PACKED_CMD);
2723 goto no_packed;
2724 }
2725
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002726 if (!(md->flags & MMC_BLK_PACKED_CMD))
2727 goto no_packed;
2728
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002729 if (!mq->wr_packing_enabled)
2730 goto no_packed;
2731
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002732 if ((rq_data_dir(cur) == WRITE) &&
2733 mmc_host_packed_wr(card->host))
2734 max_packed_rw = card->ext_csd.max_packed_writes;
2735
2736 if (max_packed_rw == 0)
2737 goto no_packed;
2738
2739 if (mmc_req_rel_wr(cur) &&
2740 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2741 goto no_packed;
2742
2743 if (mmc_large_sector(card) &&
2744 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2745 goto no_packed;
2746
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002747 if (cur->cmd_flags & REQ_FUA)
2748 goto no_packed;
2749
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002750 mmc_blk_clear_packed(mqrq);
2751
2752 max_blk_count = min(card->host->max_blk_count,
2753 card->host->max_req_size >> 9);
2754 if (unlikely(max_blk_count > 0xffff))
2755 max_blk_count = 0xffff;
2756
2757 max_phys_segs = queue_max_segments(q);
2758 req_sectors += blk_rq_sectors(cur);
2759 phys_segments += cur->nr_phys_segments;
2760
2761 if (rq_data_dir(cur) == WRITE) {
2762 req_sectors += mmc_large_sector(card) ? 8 : 1;
2763 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2764 }
2765
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002766 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002767 do {
2768 if (reqs >= max_packed_rw - 1) {
2769 put_back = false;
2770 break;
2771 }
2772
2773 spin_lock_irq(q->queue_lock);
2774 next = blk_fetch_request(q);
2775 spin_unlock_irq(q->queue_lock);
2776 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002777 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002778 put_back = false;
2779 break;
2780 }
2781
2782 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002783 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2784 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002785 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002786 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002787
Mike Christie3a5e02c2016-06-05 14:32:23 -05002788 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002789 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002790 req_op(next) == REQ_OP_FLUSH) {
2791 if (req_op(next) != REQ_OP_SECURE_ERASE)
2792 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002793 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002794 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002795
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002796 if (next->cmd_flags & REQ_FUA) {
2797 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2798 break;
2799 }
2800
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002801 if (rq_data_dir(cur) != rq_data_dir(next)) {
2802 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002803 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002804 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002805
2806 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002807 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2808 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002809 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002810 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002811
2812 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002813 if (req_sectors > max_blk_count) {
2814 if (stats->enabled)
2815 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002816 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002817 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002818
2819 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002820 if (phys_segments > max_phys_segs) {
2821 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002822 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002823 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002824
Maya Erez5a8dae12014-12-04 15:13:59 +02002825 if (mq->no_pack_for_random) {
2826 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2827 blk_rq_pos(next)) {
2828 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2829 put_back = 1;
2830 break;
2831 }
2832 }
2833
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002834 if (rq_data_dir(next) == WRITE)
2835 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002836 list_add_tail(&next->queuelist, &mqrq->packed->list);
2837 cur = next;
2838 reqs++;
2839 } while (1);
2840
2841 if (put_back) {
2842 spin_lock_irq(q->queue_lock);
2843 blk_requeue_request(q, next);
2844 spin_unlock_irq(q->queue_lock);
2845 }
2846
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002847 if (stats->enabled) {
2848 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2849 stats->packing_events[reqs + 1]++;
2850 if (reqs + 1 == max_packed_rw)
2851 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2852 }
2853
2854 spin_unlock(&stats->lock);
2855
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002856 if (reqs > 0) {
2857 list_add(&req->queuelist, &mqrq->packed->list);
2858 mqrq->packed->nr_entries = ++reqs;
2859 mqrq->packed->retries = reqs;
2860 return reqs;
2861 }
2862
2863no_packed:
2864 mqrq->cmd_type = MMC_PACKED_NONE;
2865 return 0;
2866}
2867
2868static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2869 struct mmc_card *card,
2870 struct mmc_queue *mq)
2871{
2872 struct mmc_blk_request *brq = &mqrq->brq;
2873 struct request *req = mqrq->req;
2874 struct request *prq;
2875 struct mmc_blk_data *md = mq->data;
2876 struct mmc_packed *packed = mqrq->packed;
2877 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002878 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002879 u8 hdr_blocks;
2880 u8 i = 1;
2881
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002882 mqrq->cmd_type = MMC_PACKED_WRITE;
2883 packed->blocks = 0;
2884 packed->idx_failure = MMC_PACKED_NR_IDX;
2885
2886 packed_cmd_hdr = packed->cmd_hdr;
2887 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002888 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2889 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002890 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2891
2892 /*
2893 * Argument for each entry of packed group
2894 */
2895 list_for_each_entry(prq, &packed->list, queuelist) {
2896 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2897 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2898 (prq->cmd_flags & REQ_META) &&
2899 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002900 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002901 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002902 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002903 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2904 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002905 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002906 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002907 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002908 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002909 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002910 packed->blocks += blk_rq_sectors(prq);
2911 i++;
2912 }
2913
2914 memset(brq, 0, sizeof(struct mmc_blk_request));
2915 brq->mrq.cmd = &brq->cmd;
2916 brq->mrq.data = &brq->data;
2917 brq->mrq.sbc = &brq->sbc;
2918 brq->mrq.stop = &brq->stop;
2919
2920 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2921 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2922 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2923
2924 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2925 brq->cmd.arg = blk_rq_pos(req);
2926 if (!mmc_card_blockaddr(card))
2927 brq->cmd.arg <<= 9;
2928 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2929
2930 brq->data.blksz = 512;
2931 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002932 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302933 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002934
2935 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2936 brq->stop.arg = 0;
2937 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2938
2939 mmc_set_data_timeout(&brq->data, card);
2940
2941 brq->data.sg = mqrq->sg;
2942 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2943
2944 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002945
2946 /*
2947 * This is intended for packed commands tests usage - in case these
2948 * functions are not in use the respective pointers are NULL
2949 */
2950 if (mq->err_check_fn)
2951 mqrq->mmc_active.err_check = mq->err_check_fn;
2952 else
2953 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2954
2955 if (mq->packed_test_fn)
2956 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002957
2958 mmc_queue_bounce_pre(mqrq);
2959}
2960
Adrian Hunter67716322011-08-29 16:42:15 +03002961static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2962 struct mmc_blk_request *brq, struct request *req,
2963 int ret)
2964{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002965 struct mmc_queue_req *mq_rq;
2966 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2967
Adrian Hunter67716322011-08-29 16:42:15 +03002968 /*
2969 * If this is an SD card and we're writing, we can first
2970 * mark the known good sectors as ok.
2971 *
2972 * If the card is not SD, we can still ok written sectors
2973 * as reported by the controller (which might be less than
2974 * the real number of written sectors, but never more).
2975 */
2976 if (mmc_card_sd(card)) {
2977 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302978 if (!brq->data.fault_injected) {
2979 blocks = mmc_sd_num_wr_blocks(card);
2980 if (blocks != (u32)-1)
2981 ret = blk_end_request(req, 0, blocks << 9);
2982 } else
2983 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002984 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002985 if (!mmc_packed_cmd(mq_rq->cmd_type))
2986 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002987 }
2988 return ret;
2989}
2990
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002991static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2992{
2993 struct request *prq;
2994 struct mmc_packed *packed = mq_rq->packed;
2995 int idx = packed->idx_failure, i = 0;
2996 int ret = 0;
2997
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002998 while (!list_empty(&packed->list)) {
2999 prq = list_entry_rq(packed->list.next);
3000 if (idx == i) {
3001 /* retry from error index */
3002 packed->nr_entries -= idx;
3003 mq_rq->req = prq;
3004 ret = 1;
3005
3006 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
3007 list_del_init(&prq->queuelist);
3008 mmc_blk_clear_packed(mq_rq);
3009 }
3010 return ret;
3011 }
3012 list_del_init(&prq->queuelist);
3013 blk_end_request(prq, 0, blk_rq_bytes(prq));
3014 i++;
3015 }
3016
3017 mmc_blk_clear_packed(mq_rq);
3018 return ret;
3019}
3020
3021static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3022{
3023 struct request *prq;
3024 struct mmc_packed *packed = mq_rq->packed;
3025
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003026 while (!list_empty(&packed->list)) {
3027 prq = list_entry_rq(packed->list.next);
3028 list_del_init(&prq->queuelist);
3029 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3030 }
3031
3032 mmc_blk_clear_packed(mq_rq);
3033}
3034
3035static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3036 struct mmc_queue_req *mq_rq)
3037{
3038 struct request *prq;
3039 struct request_queue *q = mq->queue;
3040 struct mmc_packed *packed = mq_rq->packed;
3041
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003042 while (!list_empty(&packed->list)) {
3043 prq = list_entry_rq(packed->list.prev);
3044 if (prq->queuelist.prev != &packed->list) {
3045 list_del_init(&prq->queuelist);
3046 spin_lock_irq(q->queue_lock);
3047 blk_requeue_request(mq->queue, prq);
3048 spin_unlock_irq(q->queue_lock);
3049 } else {
3050 list_del_init(&prq->queuelist);
3051 }
3052 }
3053
3054 mmc_blk_clear_packed(mq_rq);
3055}
3056
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003057static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3058 struct mmc_cmdq_req *cmdq_req)
3059{
3060 struct mmc_request *mrq = &cmdq_req->mrq;
3061
3062 mrq->done = mmc_blk_cmdq_req_done;
3063 return mmc_cmdq_start_req(host, cmdq_req);
3064}
3065
Asutosh Das5238e022015-04-23 16:00:45 +05303066/* prepare for non-data commands */
3067static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3068 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3069{
3070 struct request *req = mqrq->req;
3071 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3072
3073 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3074
3075 cmdq_req->mrq.data = NULL;
3076 cmdq_req->cmd_flags = req->cmd_flags;
3077 cmdq_req->mrq.req = mqrq->req;
3078 req->special = mqrq;
3079 cmdq_req->cmdq_req_flags |= DCMD;
3080 cmdq_req->mrq.cmdq_req = cmdq_req;
3081
3082 return &mqrq->cmdq_req;
3083}
3084
3085
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003086#define IS_RT_CLASS_REQ(x) \
3087 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3088
3089static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3090 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3091{
3092 struct mmc_card *card = mq->card;
3093 struct request *req = mqrq->req;
3094 struct mmc_blk_data *md = mq->data;
3095 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3096 bool do_data_tag;
3097 bool read_dir = (rq_data_dir(req) == READ);
3098 bool prio = IS_RT_CLASS_REQ(req);
3099 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3100
3101 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3102
3103 cmdq_rq->tag = req->tag;
3104 if (read_dir) {
3105 cmdq_rq->cmdq_req_flags |= DIR;
3106 cmdq_rq->data.flags = MMC_DATA_READ;
3107 } else {
3108 cmdq_rq->data.flags = MMC_DATA_WRITE;
3109 }
3110 if (prio)
3111 cmdq_rq->cmdq_req_flags |= PRIO;
3112
3113 if (do_rel_wr)
3114 cmdq_rq->cmdq_req_flags |= REL_WR;
3115
3116 cmdq_rq->data.blocks = blk_rq_sectors(req);
3117 cmdq_rq->blk_addr = blk_rq_pos(req);
3118 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3119
3120 mmc_set_data_timeout(&cmdq_rq->data, card);
3121
3122 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3123 (req->cmd_flags & REQ_META) &&
3124 (rq_data_dir(req) == WRITE) &&
3125 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3126 card->ext_csd.data_tag_unit_size);
3127 if (do_data_tag)
3128 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3129 cmdq_rq->data.sg = mqrq->sg;
3130 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3131
3132 /*
3133 * Adjust the sg list so it is the same size as the
3134 * request.
3135 */
3136 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3137 cmdq_rq->data.blocks = card->host->max_blk_count;
3138
3139 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3140 int i, data_size = cmdq_rq->data.blocks << 9;
3141 struct scatterlist *sg;
3142
3143 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3144 data_size -= sg->length;
3145 if (data_size <= 0) {
3146 sg->length += data_size;
3147 i++;
3148 break;
3149 }
3150 }
3151 cmdq_rq->data.sg_len = i;
3152 }
3153
3154 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3155 mqrq->cmdq_req.mrq.req = mqrq->req;
3156 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3157 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3158 mqrq->req->special = mqrq;
3159
3160 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",
3161 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3162 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3163 cmdq_rq, cmdq_rq->blk_addr,
3164 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3165
3166 return &mqrq->cmdq_req;
3167}
3168
3169static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3170{
3171 struct mmc_queue_req *active_mqrq;
3172 struct mmc_card *card = mq->card;
3173 struct mmc_host *host = card->host;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003174 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003175 struct mmc_cmdq_req *mc_rq;
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003176 u8 active_small_sector_read = 0;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003177 int ret = 0;
3178
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003179 mmc_deferred_scaling(host);
3180 mmc_cmdq_clk_scaling_start_busy(host, true);
3181
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003182 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003183 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003184 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3185
3186 active_mqrq = &mq->mqrq_cmdq[req->tag];
3187 active_mqrq->req = req;
3188
3189 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3190
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003191 if (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003192 unsigned int sectors = blk_rq_sectors(req);
3193
3194 if (((sectors > 0) && (sectors < 8))
3195 && (rq_data_dir(req) == READ))
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003196 active_small_sector_read = 1;
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003197 }
Venkat Gopalakrishnan869390a2015-12-18 17:52:54 -08003198 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3199 if (!ret && active_small_sector_read)
3200 host->cmdq_ctx.active_small_sector_read_reqs++;
Subhash Jadavani01deb372015-11-18 15:39:02 -08003201 /*
3202 * When in SVS2 on low load scenario and there are lots of requests
3203 * queued for CMDQ we need to wait till the queue is empty to scale
3204 * back up to Nominal even if there is a sudden increase in load.
3205 * This impacts performance where lots of IO get executed in SVS2
3206 * frequency since the queue is full. As SVS2 is a low load use case
3207 * we can serialize the requests and not queue them in parallel
3208 * without impacting other use cases. This makes sure the queue gets
3209 * empty faster and we will be able to scale up to Nominal frequency
3210 * when needed.
3211 */
3212 if (!ret && (host->clk_scaling.state == MMC_LOAD_LOW))
3213 wait_event_interruptible(ctx->queue_empty_wq,
3214 (!ctx->active_reqs));
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003215
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003216 return ret;
3217}
3218
Asutosh Das5238e022015-04-23 16:00:45 +05303219/*
3220 * Issues a flush (dcmd) request
3221 */
3222int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3223{
3224 int err;
3225 struct mmc_queue_req *active_mqrq;
3226 struct mmc_card *card = mq->card;
3227 struct mmc_host *host;
3228 struct mmc_cmdq_req *cmdq_req;
3229 struct mmc_cmdq_context_info *ctx_info;
3230
3231 BUG_ON(!card);
3232 host = card->host;
3233 BUG_ON(!host);
3234 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3235 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3236
3237 ctx_info = &host->cmdq_ctx;
3238
3239 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3240
3241 active_mqrq = &mq->mqrq_cmdq[req->tag];
3242 active_mqrq->req = req;
3243
3244 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3245 cmdq_req->cmdq_req_flags |= QBR;
3246 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3247 cmdq_req->tag = req->tag;
3248
3249 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3250 if (err) {
3251 pr_err("%s: failed (%d) preparing flush req\n",
3252 mmc_hostname(host), err);
3253 return err;
3254 }
3255 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3256 return err;
3257}
3258EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3259
Asutosh Das02e30862015-05-20 16:52:04 +05303260static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3261{
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303262 int err = 0;
Asutosh Das02e30862015-05-20 16:52:04 +05303263
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303264 if (mmc_cmdq_halt(host, true)) {
3265 pr_err("%s: halt failed\n", mmc_hostname(host));
3266 goto reset;
Asutosh Das02e30862015-05-20 16:52:04 +05303267 }
3268
3269 if (clear_all)
3270 mmc_cmdq_discard_queue(host, 0);
3271reset:
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303272 mmc_host_clk_hold(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303273 host->cmdq_ops->disable(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303274 mmc_host_clk_release(host);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303275 err = mmc_cmdq_hw_reset(host);
3276 if (err && err != -EOPNOTSUPP) {
3277 pr_err("%s: failed to cmdq_hw_reset err = %d\n",
3278 mmc_hostname(host), err);
3279 mmc_host_clk_hold(host);
3280 host->cmdq_ops->enable(host);
3281 mmc_host_clk_release(host);
3282 mmc_cmdq_halt(host, false);
3283 goto out;
3284 }
3285 /*
3286 * CMDQ HW reset would have already made CQE
3287 * in unhalted state, but reflect the same
3288 * in software state of cmdq_ctx.
3289 */
3290 mmc_host_clr_halt(host);
3291out:
3292 return;
3293}
3294
3295/**
3296 * is_cmdq_dcmd_req - Checks if tag belongs to DCMD request.
3297 * @q: request_queue pointer.
3298 * @tag: tag number of request to check.
3299 *
3300 * This function checks if the request with tag number "tag"
3301 * is a DCMD request or not based on cmdq_req_flags set.
3302 *
3303 * returns true if DCMD req, otherwise false.
3304 */
3305static bool is_cmdq_dcmd_req(struct request_queue *q, int tag)
3306{
3307 struct request *req;
3308 struct mmc_queue_req *mq_rq;
3309 struct mmc_cmdq_req *cmdq_req;
3310
3311 req = blk_queue_find_tag(q, tag);
3312 if (WARN_ON(!req))
3313 goto out;
3314 mq_rq = req->special;
3315 if (WARN_ON(!mq_rq))
3316 goto out;
3317 cmdq_req = &(mq_rq->cmdq_req);
3318 return (cmdq_req->cmdq_req_flags & DCMD);
3319out:
3320 return -ENOENT;
3321}
3322
3323/**
3324 * mmc_blk_cmdq_reset_all - Reset everything for CMDQ block request.
3325 * @host: mmc_host pointer.
3326 * @err: error for which reset is performed.
3327 *
3328 * This function implements reset_all functionality for
3329 * cmdq. It resets the controller, power cycle the card,
3330 * and invalidate all busy tags(requeue all request back to
3331 * elevator).
3332 */
3333static void mmc_blk_cmdq_reset_all(struct mmc_host *host, int err)
3334{
3335 struct mmc_request *mrq = host->err_mrq;
3336 struct mmc_card *card = host->card;
3337 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3338 struct request_queue *q;
3339 int itag = 0;
3340 int ret = 0;
3341
3342 if (WARN_ON(!mrq))
3343 return;
3344
3345 q = mrq->req->q;
3346 WARN_ON(!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
3347
3348 #ifdef CONFIG_MMC_CLKGATE
3349 pr_debug("%s: %s: active_reqs = %lu, clk_requests = %d\n",
3350 mmc_hostname(host), __func__,
3351 ctx_info->active_reqs, host->clk_requests);
3352 #endif
3353
3354 mmc_blk_cmdq_reset(host, false);
3355
3356 for_each_set_bit(itag, &ctx_info->active_reqs,
3357 host->num_cq_slots) {
3358 ret = is_cmdq_dcmd_req(q, itag);
3359 if (WARN_ON(ret == -ENOENT))
3360 continue;
3361 if (!ret) {
3362 WARN_ON(!test_and_clear_bit(itag,
3363 &ctx_info->data_active_reqs));
3364 } else {
3365 clear_bit(CMDQ_STATE_DCMD_ACTIVE,
3366 &ctx_info->curr_state);
3367 }
3368 mmc_cmdq_post_req(host, itag, err);
3369 WARN_ON(!test_and_clear_bit(itag,
3370 &ctx_info->active_reqs));
3371 mmc_host_clk_release(host);
3372 mmc_put_card(card);
3373 }
3374
3375 spin_lock_irq(q->queue_lock);
3376 blk_queue_invalidate_tags(q);
3377 spin_unlock_irq(q->queue_lock);
Asutosh Das02e30862015-05-20 16:52:04 +05303378}
3379
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303380static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3381{
3382 int err;
3383 struct mmc_card *card = mq->card;
3384 struct mmc_host *host = card->host;
3385
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003386 mmc_get_card(card);
3387 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303388 err = mmc_cmdq_halt(host, true);
3389 if (err) {
3390 pr_err("%s: halt: failed: %d\n", __func__, err);
Subhash Jadavanie88f5e22015-10-20 18:01:53 -07003391 goto out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303392 }
3393
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303394 /* disable CQ mode in card */
Sahitya Tummala92136382015-11-05 10:07:42 +05303395 if (mmc_card_cmdq(card)) {
3396 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3397 EXT_CSD_CMDQ, 0,
3398 card->ext_csd.generic_cmd6_time);
3399 if (err) {
3400 pr_err("%s: failed to switch card to legacy mode: %d\n",
3401 __func__, err);
3402 goto out;
3403 }
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003404 mmc_card_clr_cmdq(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303405 }
Sahitya Tummala92136382015-11-05 10:07:42 +05303406 host->cmdq_ops->disable(host, false);
3407 host->card->cmdq_init = false;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303408out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003409 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003410 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303411}
3412
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303413static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3414{
3415 struct mmc_queue *mq = req->q->queuedata;
3416 struct mmc_host *host = mq->card->host;
3417 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003418 struct mmc_request *mrq;
3419 struct mmc_cmdq_req *cmdq_req;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303420 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003421
3422 BUG_ON(!host);
3423
3424 /*
3425 * The mmc_queue_req will be present only if the request
3426 * is issued to the LLD. The request could be fetched from
3427 * block layer queue but could be waiting to be issued
3428 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3429 * Reset the timer in such cases to give LLD more time
3430 */
3431 if (!mq_rq) {
3432 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3433 return BLK_EH_RESET_TIMER;
3434 }
3435
3436 mrq = &mq_rq->cmdq_req.mrq;
3437 cmdq_req = &mq_rq->cmdq_req;
3438
3439 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303440
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303441 if (cmdq_req->cmdq_req_flags & DCMD)
3442 mrq->cmd->error = -ETIMEDOUT;
3443 else
3444 mrq->data->error = -ETIMEDOUT;
3445
Ritesh Harjani7b09b0e2015-10-08 17:54:01 +05303446 if (mrq->cmd && mrq->cmd->error) {
3447 if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
3448 /*
3449 * Notify completion for non flush commands like
3450 * discard that wait for DCMD finish.
3451 */
3452 set_bit(CMDQ_STATE_REQ_TIMED_OUT,
3453 &ctx_info->curr_state);
3454 complete(&mrq->completion);
3455 return BLK_EH_NOT_HANDLED;
3456 }
3457 }
3458
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303459 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state) ||
3460 test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state))
3461 return BLK_EH_NOT_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303462
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303463 set_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003464 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303465}
3466
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303467/*
3468 * mmc_blk_cmdq_err: error handling of cmdq error requests.
3469 * Function should be called in context of error out request
3470 * which has claim_host and rpm acquired.
3471 * This may be called with CQ engine halted. Make sure to
3472 * unhalt it after error recovery.
3473 *
3474 * TODO: Currently cmdq error handler does reset_all in case
3475 * of any erorr. Need to optimize error handling.
3476 */
Asutosh Das02e30862015-05-20 16:52:04 +05303477static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3478{
Asutosh Das02e30862015-05-20 16:52:04 +05303479 struct mmc_host *host = mq->card->host;
3480 struct mmc_request *mrq = host->err_mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303481 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303482 struct request_queue *q;
3483 int err;
Asutosh Das02e30862015-05-20 16:52:04 +05303484
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003485 mmc_host_clk_hold(host);
3486 host->cmdq_ops->dumpstate(host);
3487 mmc_host_clk_release(host);
3488
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303489 if (WARN_ON(!mrq))
3490 return;
3491
3492 q = mrq->req->q;
Asutosh Das02e30862015-05-20 16:52:04 +05303493 err = mmc_cmdq_halt(host, true);
3494 if (err) {
3495 pr_err("halt: failed: %d\n", err);
3496 goto reset;
3497 }
3498
3499 /* RED error - Fatal: requires reset */
3500 if (mrq->cmdq_req->resp_err) {
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303501 err = mrq->cmdq_req->resp_err;
Asutosh Das02e30862015-05-20 16:52:04 +05303502 pr_crit("%s: Response error detected: Device in bad state\n",
3503 mmc_hostname(host));
Asutosh Das02e30862015-05-20 16:52:04 +05303504 goto reset;
3505 }
3506
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303507 /*
3508 * In case of software request time-out, we schedule err work only for
3509 * the first error out request and handles all other request in flight
3510 * here.
3511 */
3512 if (test_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state)) {
3513 err = -ETIMEDOUT;
3514 } else if (mrq->data && mrq->data->error) {
3515 err = mrq->data->error;
3516 } else if (mrq->cmd && mrq->cmd->error) {
3517 /* DCMD commands */
3518 err = mrq->cmd->error;
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003519 }
Asutosh Das02e30862015-05-20 16:52:04 +05303520
3521reset:
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303522 mmc_blk_cmdq_reset_all(host, err);
3523 if (mrq->cmdq_req->resp_err)
3524 mrq->cmdq_req->resp_err = false;
Asutosh Das02e30862015-05-20 16:52:04 +05303525 mmc_cmdq_halt(host, false);
3526
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003527 host->err_mrq = NULL;
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303528 clear_bit(CMDQ_STATE_REQ_TIMED_OUT, &ctx_info->curr_state);
3529 WARN_ON(!test_and_clear_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Subhash Jadavani987144c2015-08-12 17:53:21 -07003530 wake_up(&ctx_info->wait);
Asutosh Das02e30862015-05-20 16:52:04 +05303531}
3532
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003533/* invoked by block layer in softirq context */
3534void mmc_blk_cmdq_complete_rq(struct request *rq)
3535{
3536 struct mmc_queue_req *mq_rq = rq->special;
3537 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3538 struct mmc_host *host = mrq->host;
3539 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3540 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3541 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3542 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003543 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003544
3545 if (mrq->cmd && mrq->cmd->error)
3546 err = mrq->cmd->error;
3547 else if (mrq->data && mrq->data->error)
3548 err = mrq->data->error;
3549
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303550 if (err || cmdq_req->resp_err) {
3551 pr_err("%s: %s: txfr error(%d)/resp_err(%d)\n",
3552 mmc_hostname(mrq->host), __func__, err,
3553 cmdq_req->resp_err);
Asutosh Das02e30862015-05-20 16:52:04 +05303554 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3555 pr_err("%s: CQ in error state, ending current req: %d\n",
3556 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303557 } else {
3558 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303559 BUG_ON(host->err_mrq != NULL);
3560 host->err_mrq = mrq;
Asutosh Das02e30862015-05-20 16:52:04 +05303561 schedule_work(&mq->cmdq_err_work);
3562 }
3563 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003564 }
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303565 /*
3566 * In case of error CMDQ is expected to be either in halted
3567 * or disable state so cannot receive any completion of
3568 * other requests.
3569 */
3570 BUG_ON(test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003571
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303572 /* clear pending request */
3573 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3574 &ctx_info->active_reqs));
3575 if (cmdq_req->cmdq_req_flags & DCMD)
3576 is_dcmd = true;
3577 else
3578 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3579 &ctx_info->data_active_reqs));
3580
3581 mmc_cmdq_post_req(host, cmdq_req->tag, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303582 if (cmdq_req->cmdq_req_flags & DCMD) {
3583 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303584 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303585 goto out;
3586 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003587
3588 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3589
Asutosh Das5238e022015-04-23 16:00:45 +05303590out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003591
3592 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303593 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
Subhash Jadavani987144c2015-08-12 17:53:21 -07003594 wake_up(&ctx_info->wait);
Ritesh Harjanid7e5be82015-10-07 16:42:47 +05303595 mmc_put_card(host->card);
3596 }
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303597
Talel Shenhar339a4e12015-06-29 10:50:19 +03003598 if (!ctx_info->active_reqs)
3599 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3600
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003601 return;
3602}
3603
3604/*
3605 * Complete reqs from block layer softirq context
3606 * Invoked in irq context
3607 */
3608void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3609{
3610 struct request *req = mrq->req;
3611
3612 blk_complete_request(req);
3613}
3614EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3615
Per Forlinee8a43a2011-07-01 18:55:33 +02003616static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003617{
3618 struct mmc_blk_data *md = mq->data;
3619 struct mmc_card *card = md->queue.card;
3620 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003621 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003622 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003623 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303624 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003625 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003626 const u8 packed_nr = 2;
3627 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003628#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3629 unsigned long waitfor = jiffies;
3630#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003631
3632 if (!rqc && !mq->mqrq_prev->req)
3633 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003634
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003635 if (rqc)
3636 reqs = mmc_blk_prep_packed_list(mq, rqc);
3637
Per Forlin54d49d72011-07-01 18:55:29 +02003638 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003639 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303640 /*
3641 * When 4KB native sector is enabled, only 8 blocks
3642 * multiple read or write is allowed
3643 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003644 if (mmc_large_sector(card) &&
3645 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303646 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3647 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003648 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303649 goto cmd_abort;
3650 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003651
3652 if (reqs >= packed_nr)
3653 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3654 card, mq);
3655 else
3656 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003657 areq = &mq->mqrq_cur->mmc_active;
3658 } else
3659 areq = NULL;
3660 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003661 if (!areq) {
3662 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003663 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003664 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003665 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003666
Per Forlinee8a43a2011-07-01 18:55:33 +02003667 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3668 brq = &mq_rq->brq;
3669 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003670 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003671 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003672
Per Forlind78d4a82011-07-01 18:55:30 +02003673 switch (status) {
3674 case MMC_BLK_SUCCESS:
3675 case MMC_BLK_PARTIAL:
3676 /*
3677 * A block was successfully transferred.
3678 */
Adrian Hunter67716322011-08-29 16:42:15 +03003679 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003680
Mark Salyzyn6904e432016-01-28 11:12:25 -08003681 mmc_blk_simulate_delay(mq, rqc, waitfor);
3682
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003683 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3684 ret = mmc_blk_end_packed_req(mq_rq);
3685 break;
3686 } else {
3687 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003688 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003689 }
3690
Adrian Hunter67716322011-08-29 16:42:15 +03003691 /*
3692 * If the blk_end_request function returns non-zero even
3693 * though all data has been transferred and no errors
3694 * were returned by the host controller, it's a bug.
3695 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003696 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303697 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003698 __func__, blk_rq_bytes(req),
3699 brq->data.bytes_xfered);
3700 rqc = NULL;
3701 goto cmd_abort;
3702 }
Per Forlind78d4a82011-07-01 18:55:30 +02003703 break;
3704 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003705 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003706 if (mmc_blk_reset(md, card->host, type))
3707 goto cmd_abort;
3708 if (!ret)
3709 goto start_new_req;
3710 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003711 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003712 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003713 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003714 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003715 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003716 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003717 if (!mmc_blk_reset(md, card->host, type) &&
3718 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003719 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003720 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003721 case MMC_BLK_DATA_ERR: {
3722 int err;
3723
3724 err = mmc_blk_reset(md, card->host, type);
3725 if (!err)
3726 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303727 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003728 }
3729 case MMC_BLK_ECC_ERR:
3730 if (brq->data.blocks > 1) {
3731 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003732 pr_warn("%s: retrying using single block read\n",
3733 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003734 disable_multi = 1;
3735 break;
3736 }
Per Forlind78d4a82011-07-01 18:55:30 +02003737 /*
3738 * After an error, we redo I/O one sector at a
3739 * time, so we only reach here after trying to
3740 * read a single sector.
3741 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303742 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003743 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003744 if (!ret)
3745 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003746 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303747 case MMC_BLK_NOMEDIUM:
3748 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003749 default:
3750 pr_err("%s: Unhandled return value (%d)",
3751 req->rq_disk->disk_name, status);
3752 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003753 }
3754
Per Forlinee8a43a2011-07-01 18:55:33 +02003755 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003756 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3757 if (!mq_rq->packed->retries)
3758 goto cmd_abort;
3759 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3760 mmc_start_req(card->host,
3761 &mq_rq->mmc_active, NULL);
3762 } else {
3763
3764 /*
3765 * In case of a incomplete request
3766 * prepare it again and resend.
3767 */
3768 mmc_blk_rw_rq_prep(mq_rq, card,
3769 disable_multi, mq);
3770 mmc_start_req(card->host,
3771 &mq_rq->mmc_active, NULL);
3772 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003773 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 } while (ret);
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return 1;
3778
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003779 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003780 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3781 mmc_blk_abort_packed_req(mq_rq);
3782 } else {
3783 if (mmc_card_removed(card))
3784 req->cmd_flags |= REQ_QUIET;
3785 while (ret)
3786 ret = blk_end_request(req, -EIO,
3787 blk_rq_cur_bytes(req));
3788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Per Forlinee8a43a2011-07-01 18:55:33 +02003790 start_new_req:
3791 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003792 if (mmc_card_removed(card)) {
3793 rqc->cmd_flags |= REQ_QUIET;
3794 blk_end_request_all(rqc, -EIO);
3795 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003796 /*
3797 * If current request is packed, it needs to put back.
3798 */
3799 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3800 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3801
Seungwon Jeon7a819022013-01-22 19:48:07 +09003802 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3803 mmc_start_req(card->host,
3804 &mq->mqrq_cur->mmc_active, NULL);
3805 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003806 }
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return 0;
3809}
3810
Asutosh Das8b594832015-04-23 09:55:43 +05303811static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3812 struct mmc_blk_data *md)
3813{
3814 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3815 struct mmc_host *host = card->host;
3816 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3817 u8 part_config = card->ext_csd.part_config;
3818
3819 if ((main_md->part_curr == md->part_type) &&
3820 (card->part_curr == md->part_type))
3821 return 0;
3822
3823 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3824 card->ext_csd.cmdq_support &&
3825 (md->flags & MMC_BLK_CMD_QUEUE)));
3826
3827 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3828 WARN_ON(mmc_cmdq_halt(host, true));
3829
3830 /* disable CQ mode in card */
3831 if (mmc_card_cmdq(card)) {
3832 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3833 EXT_CSD_CMDQ, 0,
3834 card->ext_csd.generic_cmd6_time));
3835 mmc_card_clr_cmdq(card);
3836 }
3837
3838 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3839 part_config |= md->part_type;
3840
3841 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3842 EXT_CSD_PART_CONFIG, part_config,
3843 card->ext_csd.part_time));
3844
3845 card->ext_csd.part_config = part_config;
3846 card->part_curr = md->part_type;
3847
3848 main_md->part_curr = md->part_type;
3849
3850 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3851 WARN_ON(mmc_cmdq_halt(host, false));
3852
3853 return 0;
3854}
3855
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003856static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3857{
3858 int ret;
3859 struct mmc_blk_data *md = mq->data;
3860 struct mmc_card *card = md->queue.card;
3861
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003862 mmc_get_card(card);
Maya Ereza2152152015-09-21 14:43:38 +03003863
3864 if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
3865 ret = mmc_cmdq_halt(card->host, true);
3866 if (ret)
3867 goto out;
3868 ret = mmc_stop_bkops(card);
3869 if (ret) {
3870 pr_err("%s: %s: mmc_stop_bkops failed %d\n",
3871 md->disk->disk_name, __func__, ret);
3872 goto out;
3873 }
3874 ret = mmc_cmdq_halt(card->host, false);
3875 if (ret)
3876 goto out;
3877 }
3878
Asutosh Das8b594832015-04-23 09:55:43 +05303879 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003880 if (ret) {
3881 pr_err("%s: %s: partition switch failed %d\n",
3882 md->disk->disk_name, __func__, ret);
Maya Ereza2152152015-09-21 14:43:38 +03003883 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003884 }
3885
Asutosh Das5238e022015-04-23 16:00:45 +05303886 if (req) {
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08003887 struct mmc_host *host = card->host;
3888 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3889
3890 if ((req_op(req) == REQ_OP_FLUSH || req_op(req) == REQ_OP_DISCARD) &&
3891 (card->quirks & MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD) &&
3892 ctx->active_small_sector_read_reqs) {
3893 ret = wait_event_interruptible(ctx->queue_empty_wq,
3894 !ctx->active_reqs);
3895 if (ret) {
3896 pr_err("%s: failed while waiting for the CMDQ to be empty %s err (%d)\n",
3897 mmc_hostname(host),
3898 __func__, ret);
3899 BUG_ON(1);
3900 }
3901 /* clear the counter now */
3902 ctx->active_small_sector_read_reqs = 0;
3903 /*
3904 * If there were small sector (less than 8 sectors) read
3905 * operations in progress then we have to wait for the
3906 * outstanding requests to finish and should also have
3907 * atleast 6 microseconds delay before queuing the DCMD
3908 * request.
3909 */
3910 udelay(MMC_QUIRK_CMDQ_DELAY_BEFORE_DCMD);
3911 }
3912
Sahitya Tummala9433a132015-06-09 09:38:36 +05303913 if (req_op(req) == REQ_OP_DISCARD) {
3914 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3915 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3916 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3917 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3918 else
3919 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3920 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303921 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303922 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303923 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303924 }
Asutosh Das5238e022015-04-23 16:00:45 +05303925 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003926
Maya Ereza2152152015-09-21 14:43:38 +03003927 return ret;
3928
3929out:
3930 if (req)
3931 blk_end_request_all(req, ret);
3932 mmc_put_card(card);
3933
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003934 return ret;
3935}
3936
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003937int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003938{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003939 int ret;
3940 struct mmc_blk_data *md = mq->data;
3941 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003942 struct mmc_host *host = card->host;
3943 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303944 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003945 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003946
Dov Levenglicka0296392015-06-24 19:51:58 +03003947 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003948 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003949 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003950
Dov Levenglicka0296392015-06-24 19:51:58 +03003951 if (mmc_card_doing_bkops(host->card)) {
3952 ret = mmc_stop_bkops(host->card);
3953 if (ret)
3954 goto out;
3955 }
3956 }
3957
Andrei Warkentin371a6892011-04-11 18:10:25 -05003958 ret = mmc_blk_part_switch(card, md);
3959 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003960 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303961 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003962 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003963 ret = 0;
3964 goto out;
3965 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003966
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003967 mmc_blk_write_packing_control(mq, req);
3968
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003969 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003970 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003971 /* complete ongoing async transfer before issuing discard */
3972 if (card->host->areq)
3973 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003974 ret = mmc_blk_issue_discard_rq(mq, req);
3975 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3976 /* complete ongoing async transfer before issuing secure erase*/
3977 if (card->host->areq)
3978 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003979 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3980 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3981 else
3982 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303983 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3984 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003985 /* complete ongoing async transfer before issuing flush */
3986 if (card->host->areq)
3987 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003988 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003989 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003990 if (!req && host->areq) {
3991 spin_lock_irqsave(&host->context_info.lock, flags);
3992 host->context_info.is_waiting_last_req = true;
3993 spin_unlock_irqrestore(&host->context_info.lock, flags);
3994 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003995 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003996 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003997
Andrei Warkentin371a6892011-04-11 18:10:25 -05003998out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003999 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
4000 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09004001 /*
4002 * Release host when there are no more requests
4003 * and after special request(discard, flush) is done.
4004 * In case sepecial request, there is no reentry to
4005 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
4006 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004007 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05004008 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07004009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Russell Kinga6f6c962006-01-03 22:38:44 +00004011static inline int mmc_blk_readonly(struct mmc_card *card)
4012{
4013 return mmc_card_readonly(card) ||
4014 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
4015}
4016
Andrei Warkentin371a6892011-04-11 18:10:25 -05004017static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
4018 struct device *parent,
4019 sector_t size,
4020 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004021 const char *subname,
4022 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
4024 struct mmc_blk_data *md;
4025 int devidx, ret;
4026
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004027again:
4028 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
4029 return ERR_PTR(-ENOMEM);
4030
4031 spin_lock(&mmc_blk_lock);
4032 ret = ida_get_new(&mmc_blk_ida, &devidx);
4033 spin_unlock(&mmc_blk_lock);
4034
4035 if (ret == -EAGAIN)
4036 goto again;
4037 else if (ret)
4038 return ERR_PTR(ret);
4039
4040 if (devidx >= max_devices) {
4041 ret = -ENOSPC;
4042 goto out;
4043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004045 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00004046 if (!md) {
4047 ret = -ENOMEM;
4048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Russell Kinga6f6c962006-01-03 22:38:44 +00004050
Johan Rudholmadd710e2011-12-02 08:51:06 +01004051 md->area_type = area_type;
4052
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004053 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00004054 * Set the read-only status based on the supported commands
4055 * and the write protect switch.
4056 */
4057 md->read_only = mmc_blk_readonly(card);
4058
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004059 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00004060 if (md->disk == NULL) {
4061 ret = -ENOMEM;
4062 goto err_kfree;
4063 }
4064
4065 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004066 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00004067 md->usage = 1;
4068
Asutosh Das963469b2015-05-21 13:29:51 +05304069 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00004070 if (ret)
4071 goto err_putdisk;
4072
Russell Kinga6f6c962006-01-03 22:38:44 +00004073 md->queue.data = md;
4074
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004075 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004076 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00004077 md->disk->fops = &mmc_bdops;
4078 md->disk->private_data = md;
4079 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07004080 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004081 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07004082 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02004083 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02004084 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00004085
4086 /*
4087 * As discussed on lkml, GENHD_FL_REMOVABLE should:
4088 *
4089 * - be set for removable media with permanent block devices
4090 * - be unset for removable block devices with permanent media
4091 *
4092 * Since MMC block devices clearly fall under the second
4093 * case, we do not set GENHD_FL_REMOVABLE. Userspace
4094 * should use the block device creation/destruction hotplug
4095 * messages to tell when the card is present.
4096 */
4097
Andrei Warkentinf06c9152011-04-21 22:46:13 -05004098 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02004099 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00004100
Saugata Dasa5075eb2012-05-17 16:32:21 +05304101 if (mmc_card_mmc(card))
4102 blk_queue_logical_block_size(md->queue.queue,
4103 card->ext_csd.data_sector_size);
4104 else
4105 blk_queue_logical_block_size(md->queue.queue, 512);
4106
Andrei Warkentin371a6892011-04-11 18:10:25 -05004107 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004108
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004109 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02004110 if ((mmc_card_mmc(card) &&
4111 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05004112 (mmc_card_sd(card) &&
4113 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
4114 md->flags |= MMC_BLK_CMD23;
4115 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004116
4117 if (mmc_card_mmc(card) &&
4118 md->flags & MMC_BLK_CMD23 &&
4119 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05304120 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004121 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06004122 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004123 }
4124
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004125 if (card->cmdq_init) {
4126 md->flags |= MMC_BLK_CMD_QUEUE;
4127 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
4128 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05304129 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05304130 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05304131 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004132 }
4133
4134 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09004135 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
4136 (md->flags & MMC_BLK_CMD23) &&
4137 card->ext_csd.packed_event_en) {
4138 if (!mmc_packed_init(&md->queue, card))
4139 md->flags |= MMC_BLK_PACKED_CMD;
4140 }
4141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00004143
4144 err_putdisk:
4145 put_disk(md->disk);
4146 err_kfree:
4147 kfree(md);
4148 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004149 spin_lock(&mmc_blk_lock);
4150 ida_remove(&mmc_blk_ida, devidx);
4151 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00004152 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
Andrei Warkentin371a6892011-04-11 18:10:25 -05004155static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
4156{
4157 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004158
4159 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
4160 /*
4161 * The EXT_CSD sector count is in number or 512 byte
4162 * sectors.
4163 */
4164 size = card->ext_csd.sectors;
4165 } else {
4166 /*
4167 * The CSD capacity field is in units of read_blkbits.
4168 * set_capacity takes units of 512 bytes.
4169 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00004170 size = (typeof(sector_t))card->csd.capacity
4171 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004172 }
4173
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01004174 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004175 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004176}
4177
4178static int mmc_blk_alloc_part(struct mmc_card *card,
4179 struct mmc_blk_data *md,
4180 unsigned int part_type,
4181 sector_t size,
4182 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004183 const char *subname,
4184 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004185{
4186 char cap_str[10];
4187 struct mmc_blk_data *part_md;
4188
4189 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004190 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004191 if (IS_ERR(part_md))
4192 return PTR_ERR(part_md);
4193 part_md->part_type = part_type;
4194 list_add(&part_md->part, &md->part);
4195
James Bottomleyb9f28d82015-03-05 18:47:01 -08004196 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004197 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304198 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004199 part_md->disk->disk_name, mmc_card_id(card),
4200 mmc_card_name(card), part_md->part_type, cap_str);
4201 return 0;
4202}
4203
Namjae Jeone0c368d2011-10-06 23:41:38 +09004204/* MMC Physical partitions consist of two boot partitions and
4205 * up to four general purpose partitions.
4206 * For each partition enabled in EXT_CSD a block device will be allocatedi
4207 * to provide access to the partition.
4208 */
4209
Andrei Warkentin371a6892011-04-11 18:10:25 -05004210static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4211{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004212 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004213
4214 if (!mmc_card_mmc(card))
4215 return 0;
4216
Namjae Jeone0c368d2011-10-06 23:41:38 +09004217 for (idx = 0; idx < card->nr_parts; idx++) {
4218 if (card->part[idx].size) {
4219 ret = mmc_blk_alloc_part(card, md,
4220 card->part[idx].part_cfg,
4221 card->part[idx].size >> 9,
4222 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004223 card->part[idx].name,
4224 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004225 if (ret)
4226 return ret;
4227 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004228 }
4229
4230 return ret;
4231}
4232
Andrei Warkentin371a6892011-04-11 18:10:25 -05004233static void mmc_blk_remove_req(struct mmc_blk_data *md)
4234{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004235 struct mmc_card *card;
4236
Andrei Warkentin371a6892011-04-11 18:10:25 -05004237 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004238 /*
4239 * Flush remaining requests and free queues. It
4240 * is freeing the queue that stops new requests
4241 * from being accepted.
4242 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004243 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004244 mmc_cleanup_queue(&md->queue);
4245 if (md->flags & MMC_BLK_PACKED_CMD)
4246 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004247 if (md->flags & MMC_BLK_CMD_QUEUE)
4248 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004249 device_remove_file(disk_to_dev(md->disk),
4250 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004251 if (md->disk->flags & GENHD_FL_UP) {
4252 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004253 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4254 card->ext_csd.boot_ro_lockable)
4255 device_remove_file(disk_to_dev(md->disk),
4256 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004257#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4258 device_remove_file(disk_to_dev(md->disk),
4259 &dev_attr_max_write_speed);
4260 device_remove_file(disk_to_dev(md->disk),
4261 &dev_attr_max_read_speed);
4262 device_remove_file(disk_to_dev(md->disk),
4263 &dev_attr_cache_size);
4264#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004265
Andrei Warkentin371a6892011-04-11 18:10:25 -05004266 del_gendisk(md->disk);
4267 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004268 mmc_blk_put(md);
4269 }
4270}
4271
4272static void mmc_blk_remove_parts(struct mmc_card *card,
4273 struct mmc_blk_data *md)
4274{
4275 struct list_head *pos, *q;
4276 struct mmc_blk_data *part_md;
4277
4278 list_for_each_safe(pos, q, &md->part) {
4279 part_md = list_entry(pos, struct mmc_blk_data, part);
4280 list_del(pos);
4281 mmc_blk_remove_req(part_md);
4282 }
4283}
4284
4285static int mmc_add_disk(struct mmc_blk_data *md)
4286{
4287 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004288 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004289
Dan Williams307d8e62016-06-20 10:40:44 -07004290 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004291 md->force_ro.show = force_ro_show;
4292 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304293 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004294 md->force_ro.attr.name = "force_ro";
4295 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4296 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4297 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004298 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004299#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4300 atomic_set(&md->queue.max_write_speed, max_write_speed);
4301 ret = device_create_file(disk_to_dev(md->disk),
4302 &dev_attr_max_write_speed);
4303 if (ret)
4304 goto max_write_speed_fail;
4305 atomic_set(&md->queue.max_read_speed, max_read_speed);
4306 ret = device_create_file(disk_to_dev(md->disk),
4307 &dev_attr_max_read_speed);
4308 if (ret)
4309 goto max_read_speed_fail;
4310 atomic_set(&md->queue.cache_size, cache_size);
4311 atomic_long_set(&md->queue.cache_used, 0);
4312 md->queue.cache_jiffies = jiffies;
4313 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4314 if (ret)
4315 goto cache_size_fail;
4316#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004317
4318 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4319 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004320 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004321
4322 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4323 mode = S_IRUGO;
4324 else
4325 mode = S_IRUGO | S_IWUSR;
4326
4327 md->power_ro_lock.show = power_ro_lock_show;
4328 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004329 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004330 md->power_ro_lock.attr.mode = mode;
4331 md->power_ro_lock.attr.name =
4332 "ro_lock_until_next_power_on";
4333 ret = device_create_file(disk_to_dev(md->disk),
4334 &md->power_ro_lock);
4335 if (ret)
4336 goto power_ro_lock_fail;
4337 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004338
4339 md->num_wr_reqs_to_start_packing.show =
4340 num_wr_reqs_to_start_packing_show;
4341 md->num_wr_reqs_to_start_packing.store =
4342 num_wr_reqs_to_start_packing_store;
4343 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4344 md->num_wr_reqs_to_start_packing.attr.name =
4345 "num_wr_reqs_to_start_packing";
4346 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4347 ret = device_create_file(disk_to_dev(md->disk),
4348 &md->num_wr_reqs_to_start_packing);
4349 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004350 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004351
Maya Erez5a8dae12014-12-04 15:13:59 +02004352 md->no_pack_for_random.show = no_pack_for_random_show;
4353 md->no_pack_for_random.store = no_pack_for_random_store;
4354 sysfs_attr_init(&md->no_pack_for_random.attr);
4355 md->no_pack_for_random.attr.name = "no_pack_for_random";
4356 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4357 ret = device_create_file(disk_to_dev(md->disk),
4358 &md->no_pack_for_random);
4359 if (ret)
4360 goto no_pack_for_random_fails;
4361
Johan Rudholmadd710e2011-12-02 08:51:06 +01004362 return ret;
4363
Maya Erez5a8dae12014-12-04 15:13:59 +02004364no_pack_for_random_fails:
4365 device_remove_file(disk_to_dev(md->disk),
4366 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004367num_wr_reqs_to_start_packing_fail:
4368 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004369power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004370#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4371 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4372cache_size_fail:
4373 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4374max_read_speed_fail:
4375 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4376max_write_speed_fail:
4377#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004378 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4379force_ro_fail:
4380 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004381
4382 return ret;
4383}
4384
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004385static const struct mmc_fixup blk_fixups[] =
4386{
Chris Ballc59d4472011-11-11 22:01:43 -05004387 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4388 MMC_QUIRK_INAND_CMD38),
4389 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4390 MMC_QUIRK_INAND_CMD38),
4391 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4392 MMC_QUIRK_INAND_CMD38),
4393 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4394 MMC_QUIRK_INAND_CMD38),
4395 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4396 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004397
4398 /*
4399 * Some MMC cards experience performance degradation with CMD23
4400 * instead of CMD12-bounded multiblock transfers. For now we'll
4401 * black list what's bad...
4402 * - Certain Toshiba cards.
4403 *
4404 * N.B. This doesn't affect SD cards.
4405 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004406 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4407 MMC_QUIRK_BLK_NO_CMD23),
4408 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4409 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004410 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004411 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004412 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004413 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004414 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004415 MMC_QUIRK_BLK_NO_CMD23),
Subhash Jadavanib3994402015-11-03 19:03:41 -08004416 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_TOSHIBA, CID_OEMID_ANY,
Subhash Jadavaniebbb9002015-11-13 12:15:53 -08004417 add_quirk_mmc, MMC_QUIRK_CMDQ_EMPTY_BEFORE_DCMD),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004418
4419 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004420 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004421 */
Chris Ballc59d4472011-11-11 22:01:43 -05004422 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004423 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004424 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4425 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004426
Ian Chen3550ccd2012-08-29 15:05:36 +09004427 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004428 * Some Samsung MMC cards need longer data read timeout than
4429 * indicated in CSD.
4430 */
4431 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4432 MMC_QUIRK_LONG_READ_TIME),
4433
4434 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004435 * On these Samsung MoviNAND parts, performing secure erase or
4436 * secure trim can result in unrecoverable corruption due to a
4437 * firmware bug.
4438 */
4439 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4440 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4441 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4442 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4443 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4444 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4445 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4446 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4447 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4448 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4449 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4450 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4451 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4452 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4453 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4454 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4455
Shawn Linb5b4ff02015-08-12 13:08:32 +08004456 /*
4457 * On Some Kingston eMMCs, performing trim can result in
4458 * unrecoverable data conrruption occasionally due to a firmware bug.
4459 */
4460 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4461 MMC_QUIRK_TRIM_BROKEN),
4462 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4463 MMC_QUIRK_TRIM_BROKEN),
4464
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004465 /* Some INAND MCP devices advertise incorrect timeout values */
4466 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4467 MMC_QUIRK_INAND_DATA_TIMEOUT),
4468
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004469 END_FIXUP
4470};
4471
Ulf Hansson96541ba2015-04-14 13:06:12 +02004472static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004474 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004475 char cap_str[10];
4476
Pierre Ossman912490d2005-05-21 10:27:02 +01004477 /*
4478 * Check that the card supports the command class(es) we need.
4479 */
4480 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 return -ENODEV;
4482
Lukas Czerner5204d002014-06-18 13:18:07 +02004483 mmc_fixup_device(card, blk_fixups);
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 md = mmc_blk_alloc(card);
4486 if (IS_ERR(md))
4487 return PTR_ERR(md);
4488
James Bottomleyb9f28d82015-03-05 18:47:01 -08004489 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004490 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304491 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004493 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Andrei Warkentin371a6892011-04-11 18:10:25 -05004495 if (mmc_blk_alloc_parts(card, md))
4496 goto out;
4497
Ulf Hansson96541ba2015-04-14 13:06:12 +02004498 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004499
Andrei Warkentin371a6892011-04-11 18:10:25 -05004500 if (mmc_add_disk(md))
4501 goto out;
4502
4503 list_for_each_entry(part_md, &md->part, part) {
4504 if (mmc_add_disk(part_md))
4505 goto out;
4506 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004507
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004508 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004509 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4510 /*
4511 * If there is a runtime_idle function, it should take care of
4512 * suspending the card
4513 */
4514 if (card->host->bus_ops->runtime_idle)
4515 pm_runtime_dont_use_autosuspend(&card->dev);
4516 else
4517 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004518
4519 /*
4520 * Don't enable runtime PM for SD-combo cards here. Leave that
4521 * decision to be taken during the SDIO init sequence instead.
4522 */
4523 if (card->type != MMC_TYPE_SD_COMBO) {
4524 pm_runtime_set_active(&card->dev);
4525 pm_runtime_enable(&card->dev);
4526 }
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 return 0;
4529
4530 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004531 mmc_blk_remove_parts(card, md);
4532 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534}
4535
Ulf Hansson96541ba2015-04-14 13:06:12 +02004536static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004538 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Andrei Warkentin371a6892011-04-11 18:10:25 -05004540 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004541 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004542 mmc_claim_host(card->host);
4543 mmc_blk_part_switch(card, md);
4544 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004545 if (card->type != MMC_TYPE_SD_COMBO)
4546 pm_runtime_disable(&card->dev);
4547 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004548 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004549 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550}
4551
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004552static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004554 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004555 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304556 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004559 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304560 if (rc)
4561 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004562 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004563 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304564 if (rc)
4565 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304568 goto out;
4569
4570 out_resume:
4571 mmc_queue_resume(&md->queue);
4572 list_for_each_entry(part_md, &md->part, part) {
4573 mmc_queue_resume(&part_md->queue);
4574 }
4575 out:
4576 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577}
4578
Ulf Hansson96541ba2015-04-14 13:06:12 +02004579static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004580{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004581 _mmc_blk_suspend(card, 1);
Krishna Kondaa7ea2dc2015-06-29 19:20:05 -07004582
4583 /* send power off notification */
4584 if (mmc_card_mmc(card))
4585 mmc_send_pon(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004586}
4587
Ulf Hansson0967edc2014-10-06 11:29:42 +02004588#ifdef CONFIG_PM_SLEEP
4589static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004590{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004591 struct mmc_card *card = mmc_dev_to_card(dev);
4592
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004593 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004594}
4595
Ulf Hansson0967edc2014-10-06 11:29:42 +02004596static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004598 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004599 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004602 /*
4603 * Resume involves the card going into idle state,
4604 * so current partition is always the main one.
4605 */
4606 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004608 list_for_each_entry(part_md, &md->part, part) {
4609 mmc_queue_resume(&part_md->queue);
4610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
4612 return 0;
4613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614#endif
4615
Ulf Hansson0967edc2014-10-06 11:29:42 +02004616static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4617
Ulf Hansson96541ba2015-04-14 13:06:12 +02004618static struct mmc_driver mmc_driver = {
4619 .drv = {
4620 .name = "mmcblk",
4621 .pm = &mmc_blk_pm_ops,
4622 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 .probe = mmc_blk_probe,
4624 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004625 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626};
4627
4628static int __init mmc_blk_init(void)
4629{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004630 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004632 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4633 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4634
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004635 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004636
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004637 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4638 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004641 res = mmc_register_driver(&mmc_driver);
4642 if (res)
4643 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004645 return 0;
4646 out2:
4647 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 out:
4649 return res;
4650}
4651
4652static void __exit mmc_blk_exit(void)
4653{
4654 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004655 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
4658module_init(mmc_blk_init);
4659module_exit(mmc_blk_exit);
4660
4661MODULE_LICENSE("GPL");
4662MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4663