blob: 098bf49ad53312f025fb118dec3dbead4a57feef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Dov Levenglicka0296392015-06-24 19:51:58 +030044#include <linux/mmc/core.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020045#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046#include <linux/mmc/mmc.h>
47#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50
Pierre Ossman98ac2162006-12-23 20:03:02 +010051#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080052#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020066#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053069#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050070
Luca Porziod3df0462015-11-06 15:12:26 +000071#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072 (rq_data_dir(req) == WRITE))
73#define PACKED_CMD_VER 0x01
74#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030075#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090076
Maya Erezf93ca0a2014-12-09 23:34:41 +020077#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020078#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
79 do { \
80 if (stats->enabled) \
81 stats->pack_stop_reason[reason]++; \
82 } while (0)
83
Asutosh Das02e30862015-05-20 16:52:04 +053084#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030085#define PCKD_TRGR_INIT_MEAN_POTEN 17
86#define PCKD_TRGR_POTEN_LOWER_BOUND 5
87#define PCKD_TRGR_URGENT_PENALTY 2
88#define PCKD_TRGR_LOWER_BOUND 5
89#define PCKD_TRGR_PRECISION_MULTIPLIER 100
90
Sahitya Tummala9433a132015-06-09 09:38:36 +053091static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
92 struct mmc_queue_req *mqrq, struct mmc_queue *mq);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020093static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040094
95/*
96 * The defaults come from config options but can be overriden by module
97 * or bootarg options.
98 */
99static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
100
101/*
102 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000103 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105 */
106static int max_devices;
107
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000108#define MAX_DEVICES 256
109
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200110static DEFINE_IDA(mmc_blk_ida);
111static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * There is one mmc_blk_data per slot.
115 */
116struct mmc_blk_data {
117 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700118 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct gendisk *disk;
120 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500123 unsigned int flags;
124#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
125#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700127#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000130 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300132 unsigned int reset_done;
133#define MMC_BLK_READ BIT(0)
134#define MMC_BLK_WRITE BIT(1)
135#define MMC_BLK_DISCARD BIT(2)
136#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200137#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138
139 /*
140 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200141 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 * track of the current selected device partition.
143 */
144 unsigned int part_curr;
145 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200147 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200148 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100149 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Arjan van de Vena621aae2006-01-12 18:43:35 +0000152static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154enum {
155 MMC_PACKED_NR_IDX = -1,
156 MMC_PACKED_NR_ZERO,
157 MMC_PACKED_NR_SINGLE,
158};
159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
164 struct mmc_blk_data *md);
165static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700166static int mmc_blk_cmdq_switch(struct mmc_card *card,
167 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200168
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900169static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
170{
171 struct mmc_packed *packed = mqrq->packed;
172
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900173 mqrq->cmd_type = MMC_PACKED_NONE;
174 packed->nr_entries = MMC_PACKED_NR_ZERO;
175 packed->idx_failure = MMC_PACKED_NR_IDX;
176 packed->retries = 0;
177 packed->blocks = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
181{
182 struct mmc_blk_data *md;
183
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md = disk->private_data;
186 if (md && md->usage == 0)
187 md = NULL;
188 if (md)
189 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return md;
193}
194
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195static inline int mmc_get_devidx(struct gendisk *disk)
196{
Colin Cross382c55f2015-10-22 10:00:41 -0700197 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500198 return devidx;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void mmc_blk_put(struct mmc_blk_data *md)
202{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000203 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 md->usage--;
205 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500206 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800207 blk_cleanup_queue(md->queue.queue);
208
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200209 spin_lock(&mmc_blk_lock);
210 ida_remove(&mmc_blk_ida, devidx);
211 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kfree(md);
215 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000216 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219static ssize_t power_ro_lock_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 int ret;
223 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 int locked = 0;
226
Asutosh Das507d9a72014-12-09 10:15:53 +0200227 if (!md)
228 return -EINVAL;
229
230 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 locked = 2;
233 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
234 locked = 1;
235
236 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
237
Tomas Winkler9098f842015-07-16 15:50:45 +0200238 mmc_blk_put(md);
239
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240 return ret;
241}
242
243static ssize_t power_ro_lock_store(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
245{
246 int ret;
247 struct mmc_blk_data *md, *part_md;
248 struct mmc_card *card;
249 unsigned long set;
250
251 if (kstrtoul(buf, 0, &set))
252 return -EINVAL;
253
254 if (set != 1)
255 return count;
256
257 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200258 if (!md)
259 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260 card = md->queue.card;
261
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200262 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100263
264 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
265 card->ext_csd.boot_ro_lock |
266 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
267 card->ext_csd.part_time);
268 if (ret)
269 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
270 else
271 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
272
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200273 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100274
275 if (!ret) {
276 pr_info("%s: Locking boot partition ro until next power on\n",
277 md->disk->disk_name);
278 set_disk_ro(md->disk, 1);
279
280 list_for_each_entry(part_md, &md->part, part)
281 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
282 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
283 set_disk_ro(part_md->disk, 1);
284 }
285 }
286
287 mmc_blk_put(md);
288 return count;
289}
290
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
293{
294 int ret;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296
Asutosh Das507d9a72014-12-09 10:15:53 +0200297 if (!md)
298 return -EINVAL;
299
Baruch Siach0031a982014-09-22 10:12:51 +0300300 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500301 get_disk_ro(dev_to_disk(dev)) ^
302 md->read_only);
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int ret;
311 char *end;
312 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
313 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200314
315 if (!md)
316 return -EINVAL;
317
Andrei Warkentin371a6892011-04-11 18:10:25 -0500318 if (end == buf) {
319 ret = -EINVAL;
320 goto out;
321 }
322
323 set_disk_ro(dev_to_disk(dev), set || md->read_only);
324 ret = count;
325out:
326 mmc_blk_put(md);
327 return ret;
328}
329
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200330static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200331no_pack_for_random_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 int ret;
336
Asutosh Das507d9a72014-12-09 10:15:53 +0200337 if (!md)
338 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200339 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
340
341 mmc_blk_put(md);
342 return ret;
343}
344
345static ssize_t
346no_pack_for_random_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 int value;
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200353 int ret = count;
354
Asutosh Das507d9a72014-12-09 10:15:53 +0200355 if (!md)
356 return -EINVAL;
357
358 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200359 if (!card) {
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 sscanf(buf, "%d", &value);
365
366 if (value < 0) {
367 pr_err("%s: value %d is not valid. old value remains = %d",
368 mmc_hostname(card->host), value,
369 md->queue.no_pack_for_random);
370 ret = -EINVAL;
371 goto exit;
372 }
373
374 md->queue.no_pack_for_random = (value > 0) ? true : false;
375
376 pr_debug("%s: no_pack_for_random: new value = %d",
377 mmc_hostname(card->host),
378 md->queue.no_pack_for_random);
379
380exit:
381 mmc_blk_put(md);
382 return ret;
383}
384
385static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386num_wr_reqs_to_start_packing_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
390 int num_wr_reqs_to_start_packing;
391 int ret;
392
Asutosh Das507d9a72014-12-09 10:15:53 +0200393 if (!md)
394 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
396
397 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
398
399 mmc_blk_put(md);
400 return ret;
401}
402
403static ssize_t
404num_wr_reqs_to_start_packing_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 int value;
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200411 int ret = count;
412
Asutosh Das507d9a72014-12-09 10:15:53 +0200413 if (!md)
414 return -EINVAL;
415
416 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200417 if (!card) {
418 ret = -EINVAL;
419 goto exit;
420 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200421
422 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200423
Yaniv Gardi42399822014-12-04 00:26:23 +0200424 if (value >= 0) {
425 md->queue.num_wr_reqs_to_start_packing =
426 min_t(int, value, (int)card->ext_csd.max_packed_writes);
427
428 pr_debug("%s: trigger to pack: new value = %d",
429 mmc_hostname(card->host),
430 md->queue.num_wr_reqs_to_start_packing);
431 } else {
432 pr_err("%s: value %d is not valid. old value remains = %d",
433 mmc_hostname(card->host), value,
434 md->queue.num_wr_reqs_to_start_packing);
435 ret = -EINVAL;
436 }
437
438exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200439 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200440 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200441}
442
Mark Salyzyn6904e432016-01-28 11:12:25 -0800443#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
444
445static int max_read_speed, max_write_speed, cache_size = 4;
446
447module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
448MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
449module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
450MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
451module_param(cache_size, int, S_IRUSR | S_IRGRP);
452MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
453
454/*
455 * helper macros and expectations:
456 * size - unsigned long number of bytes
457 * jiffies - unsigned long HZ timestamp difference
458 * speed - unsigned KB/s transfer rate
459 */
460#define size_and_speed_to_jiffies(size, speed) \
461 ((size) * HZ / (speed) / 1024UL)
462#define jiffies_and_speed_to_size(jiffies, speed) \
463 (((speed) * (jiffies) * 1024UL) / HZ)
464#define jiffies_and_size_to_speed(jiffies, size) \
465 ((size) * HZ / (jiffies) / 1024UL)
466
467/* Limits to report warning */
468/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
469#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
470#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
471
472#define speed_valid(speed) ((speed) > 0)
473
474static const char off[] = "off\n";
475
476static int max_speed_show(int speed, char *buf)
477{
478 if (speed)
479 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
480 else
481 return scnprintf(buf, PAGE_SIZE, off);
482}
483
484static int max_speed_store(const char *buf, struct request_queue *q)
485{
486 unsigned int limit, set = 0;
487
488 if (!strncasecmp(off, buf, sizeof(off) - 2))
489 return set;
490 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
491 return -EINVAL;
492 if (set == 0)
493 return set;
494 limit = MAX_SPEED(q);
495 if (set > limit)
496 pr_warn("max speed %u ineffective above %u\n", set, limit);
497 limit = MIN_SPEED(q);
498 if (set < limit)
499 pr_warn("max speed %u painful below %u\n", set, limit);
500 return set;
501}
502
503static ssize_t max_write_speed_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
507 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
508
509 mmc_blk_put(md);
510 return ret;
511}
512
513static ssize_t max_write_speed_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
518 int set = max_speed_store(buf, md->queue.queue);
519
520 if (set < 0) {
521 mmc_blk_put(md);
522 return set;
523 }
524
525 atomic_set(&md->queue.max_write_speed, set);
526 mmc_blk_put(md);
527 return count;
528}
529
530static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
531 max_write_speed_show, max_write_speed_store);
532
533static ssize_t max_read_speed_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
537 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
538
539 mmc_blk_put(md);
540 return ret;
541}
542
543static ssize_t max_read_speed_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
548 int set = max_speed_store(buf, md->queue.queue);
549
550 if (set < 0) {
551 mmc_blk_put(md);
552 return set;
553 }
554
555 atomic_set(&md->queue.max_read_speed, set);
556 mmc_blk_put(md);
557 return count;
558}
559
560static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
561 max_read_speed_show, max_read_speed_store);
562
563static ssize_t cache_size_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
567 struct mmc_queue *mq = &md->queue;
568 int cache_size = atomic_read(&mq->cache_size);
569 int ret;
570
571 if (!cache_size)
572 ret = scnprintf(buf, PAGE_SIZE, off);
573 else {
574 int speed = atomic_read(&mq->max_write_speed);
575
576 if (!speed_valid(speed))
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
578 else { /* We accept race between cache_jiffies and cache_used */
579 unsigned long size = jiffies_and_speed_to_size(
580 jiffies - mq->cache_jiffies, speed);
581 long used = atomic_long_read(&mq->cache_used);
582
583 if (size >= used)
584 size = 0;
585 else
586 size = (used - size) * 100 / cache_size
587 / 1024UL / 1024UL;
588
589 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
590 cache_size, size);
591 }
592 }
593
594 mmc_blk_put(md);
595 return ret;
596}
597
598static ssize_t cache_size_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
601{
602 struct mmc_blk_data *md;
603 unsigned int set = 0;
604
605 if (strncasecmp(off, buf, sizeof(off) - 2)
606 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
607 return -EINVAL;
608
609 md = mmc_blk_get(dev_to_disk(dev));
610 atomic_set(&md->queue.cache_size, set);
611 mmc_blk_put(md);
612 return count;
613}
614
615static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
616 cache_size_show, cache_size_store);
617
618/* correct for write-back */
619static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
620{
621 long used = 0;
622 int speed = atomic_read(&mq->max_write_speed);
623
624 if (speed_valid(speed)) {
625 unsigned long size = jiffies_and_speed_to_size(
626 waitfor - mq->cache_jiffies, speed);
627 used = atomic_long_read(&mq->cache_used);
628
629 if (size >= used)
630 used = 0;
631 else
632 used -= size;
633 }
634
635 atomic_long_set(&mq->cache_used, used);
636 mq->cache_jiffies = waitfor;
637
638 return used;
639}
640
641static void mmc_blk_simulate_delay(
642 struct mmc_queue *mq,
643 struct request *req,
644 unsigned long waitfor)
645{
646 int max_speed;
647
648 if (!req)
649 return;
650
651 max_speed = (rq_data_dir(req) == READ)
652 ? atomic_read(&mq->max_read_speed)
653 : atomic_read(&mq->max_write_speed);
654 if (speed_valid(max_speed)) {
655 unsigned long bytes = blk_rq_bytes(req);
656
657 if (rq_data_dir(req) != READ) {
658 int cache_size = atomic_read(&mq->cache_size);
659
660 if (cache_size) {
661 unsigned long size = cache_size * 1024L * 1024L;
662 long used = mmc_blk_cache_used(mq, waitfor);
663
664 used += bytes;
665 atomic_long_set(&mq->cache_used, used);
666 bytes = 0;
667 if (used > size)
668 bytes = used - size;
669 }
670 }
671 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
672 if (time_is_after_jiffies(waitfor)) {
673 long msecs = jiffies_to_msecs(waitfor - jiffies);
674
675 if (likely(msecs > 0))
676 msleep(msecs);
677 }
678 }
679}
680
681#else
682
683#define mmc_blk_simulate_delay(mq, req, waitfor)
684
685#endif
686
Al Viroa5a15612008-03-02 10:33:30 -0500687static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viroa5a15612008-03-02 10:33:30 -0500689 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = -ENXIO;
691
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200692 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (md) {
694 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500695 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697
Al Viroa5a15612008-03-02 10:33:30 -0500698 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700699 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700700 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return ret;
706}
707
Al Virodb2a1442013-05-05 21:52:57 -0400708static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viroa5a15612008-03-02 10:33:30 -0500710 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200712 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200714 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800718mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
721 geo->heads = 4;
722 geo->sectors = 16;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
John Calixtocb87ea22011-04-26 18:56:29 -0400726struct mmc_blk_ioc_data {
727 struct mmc_ioc_cmd ic;
728 unsigned char *buf;
729 u64 buf_bytes;
730};
731
732static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
733 struct mmc_ioc_cmd __user *user)
734{
735 struct mmc_blk_ioc_data *idata;
736 int err;
737
yalin wang1ff89502015-11-12 19:27:11 +0800738 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400739 if (!idata) {
740 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400742 }
743
744 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
745 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400746 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400747 }
748
749 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
750 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
751 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400752 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400753 }
754
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 if (!idata->buf_bytes) {
756 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300758 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100759
yalin wang1ff89502015-11-12 19:27:11 +0800760 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400761 if (!idata->buf) {
762 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400763 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400764 }
765
766 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
767 idata->ic.data_ptr, idata->buf_bytes)) {
768 err = -EFAULT;
769 goto copy_err;
770 }
771
772 return idata;
773
774copy_err:
775 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400776idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400777 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400778out:
John Calixtocb87ea22011-04-26 18:56:29 -0400779 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
Jon Huntera5f57742015-09-22 10:27:53 +0100782static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
783 struct mmc_blk_ioc_data *idata)
784{
785 struct mmc_ioc_cmd *ic = &idata->ic;
786
787 if (copy_to_user(&(ic_ptr->response), ic->response,
788 sizeof(ic->response)))
789 return -EFAULT;
790
791 if (!idata->ic.write_flag) {
792 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
793 idata->buf, idata->buf_bytes))
794 return -EFAULT;
795 }
796
797 return 0;
798}
799
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200800static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
801 u32 retries_max)
802{
803 int err;
804 u32 retry_count = 0;
805
806 if (!status || !retries_max)
807 return -EINVAL;
808
809 do {
810 err = get_card_status(card, status, 5);
811 if (err)
812 break;
813
814 if (!R1_STATUS(*status) &&
815 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
816 break; /* RPMB programming operation complete */
817
818 /*
819 * Rechedule to give the MMC device a chance to continue
820 * processing the previous command without being polled too
821 * frequently.
822 */
823 usleep_range(1000, 5000);
824 } while (++retry_count < retries_max);
825
826 if (retry_count == retries_max)
827 err = -EPERM;
828
829 return err;
830}
831
Maya Erez775a9362013-04-18 15:41:55 +0300832static int ioctl_do_sanitize(struct mmc_card *card)
833{
834 int err;
835
Ulf Hanssona2d10862013-12-16 14:37:26 +0100836 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300837 pr_warn("%s: %s - SANITIZE is not supported\n",
838 mmc_hostname(card->host), __func__);
839 err = -EOPNOTSUPP;
840 goto out;
841 }
842
843 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
844 mmc_hostname(card->host), __func__);
845
846 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
847 EXT_CSD_SANITIZE_START, 1,
848 MMC_SANITIZE_REQ_TIMEOUT);
849
850 if (err)
851 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
852 mmc_hostname(card->host), __func__, err);
853
854 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
855 __func__);
856out:
857 return err;
858}
859
Jon Huntera5f57742015-09-22 10:27:53 +0100860static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
861 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400862{
John Calixtocb87ea22011-04-26 18:56:29 -0400863 struct mmc_command cmd = {0};
864 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530865 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400866 struct scatterlist sg;
867 int err;
868
Jon Huntera5f57742015-09-22 10:27:53 +0100869 if (!card || !md || !idata)
870 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400871
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100872 cmd.opcode = idata->ic.opcode;
873 cmd.arg = idata->ic.arg;
874 cmd.flags = idata->ic.flags;
875
876 if (idata->buf_bytes) {
877 data.sg = &sg;
878 data.sg_len = 1;
879 data.blksz = idata->ic.blksz;
880 data.blocks = idata->ic.blocks;
881
882 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
883
884 if (idata->ic.write_flag)
885 data.flags = MMC_DATA_WRITE;
886 else
887 data.flags = MMC_DATA_READ;
888
889 /* data.flags must already be set before doing this. */
890 mmc_set_data_timeout(&data, card);
891
892 /* Allow overriding the timeout_ns for empirical tuning. */
893 if (idata->ic.data_timeout_ns)
894 data.timeout_ns = idata->ic.data_timeout_ns;
895
896 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
897 /*
898 * Pretend this is a data transfer and rely on the
899 * host driver to compute timeout. When all host
900 * drivers support cmd.cmd_timeout for R1B, this
901 * can be changed to:
902 *
903 * mrq.data = NULL;
904 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
905 */
906 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
907 }
908
909 mrq.data = &data;
910 }
911
912 mrq.cmd = &cmd;
913
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200914 err = mmc_blk_part_switch(card, md);
915 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100916 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200917
John Calixtocb87ea22011-04-26 18:56:29 -0400918 if (idata->ic.is_acmd) {
919 err = mmc_app_cmd(card->host, card);
920 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100921 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400922 }
923
Yaniv Gardia82e4842013-06-05 14:13:08 +0300924 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
925 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300926 err = ioctl_do_sanitize(card);
927
928 if (err)
929 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
930 __func__, err);
931
Jon Huntera5f57742015-09-22 10:27:53 +0100932 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300933 }
934
John Calixtocb87ea22011-04-26 18:56:29 -0400935 mmc_wait_for_req(card->host, &mrq);
936
937 if (cmd.error) {
938 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
939 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100940 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400941 }
942 if (data.error) {
943 dev_err(mmc_dev(card->host), "%s: data error %d\n",
944 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100945 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400946 }
947
948 /*
949 * According to the SD specs, some commands require a delay after
950 * issuing the command.
951 */
952 if (idata->ic.postsleep_min_us)
953 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
954
Jon Huntera5f57742015-09-22 10:27:53 +0100955 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400956
Krishna Kondae6711632014-12-04 15:20:57 +0200957 return err;
958}
959
960struct mmc_blk_ioc_rpmb_data {
961 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
962};
963
964static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
965 struct mmc_ioc_rpmb __user *user)
966{
967 struct mmc_blk_ioc_rpmb_data *idata;
968 int err, i;
969
970 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
971 if (!idata) {
972 err = -ENOMEM;
973 goto out;
974 }
975
976 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
977 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
978 if (IS_ERR(idata->data[i])) {
979 err = PTR_ERR(idata->data[i]);
980 goto copy_err;
981 }
982 }
983
984 return idata;
985
986copy_err:
987 while (--i >= 0) {
988 kfree(idata->data[i]->buf);
989 kfree(idata->data[i]);
990 }
991 kfree(idata);
992out:
993 return ERR_PTR(err);
994}
995
996static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
997 struct mmc_ioc_rpmb __user *ic_ptr)
998{
999 struct mmc_blk_ioc_rpmb_data *idata;
1000 struct mmc_blk_data *md;
1001 struct mmc_card *card;
1002 struct mmc_command cmd = {0};
1003 struct mmc_data data = {0};
1004 struct mmc_request mrq = {NULL};
1005 struct scatterlist sg;
1006 int err = 0, i = 0;
1007 u32 status = 0;
1008
1009 /* The caller must have CAP_SYS_RAWIO */
1010 if (!capable(CAP_SYS_RAWIO))
1011 return -EPERM;
1012
1013 md = mmc_blk_get(bdev->bd_disk);
1014 /* make sure this is a rpmb partition */
1015 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1016 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001017 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001018 }
1019
1020 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1021 if (IS_ERR(idata)) {
1022 err = PTR_ERR(idata);
1023 goto cmd_done;
1024 }
1025
1026 card = md->queue.card;
1027 if (IS_ERR(card)) {
1028 err = PTR_ERR(card);
1029 goto idata_free;
1030 }
1031
Maya Erezdd669562015-02-12 20:37:31 +02001032 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001033
1034 err = mmc_blk_part_switch(card, md);
1035 if (err)
1036 goto cmd_rel_host;
1037
1038 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1039 struct mmc_blk_ioc_data *curr_data;
1040 struct mmc_ioc_cmd *curr_cmd;
1041
1042 curr_data = idata->data[i];
1043 curr_cmd = &curr_data->ic;
1044 if (!curr_cmd->opcode)
1045 break;
1046
1047 cmd.opcode = curr_cmd->opcode;
1048 cmd.arg = curr_cmd->arg;
1049 cmd.flags = curr_cmd->flags;
1050
1051 if (curr_data->buf_bytes) {
1052 data.sg = &sg;
1053 data.sg_len = 1;
1054 data.blksz = curr_cmd->blksz;
1055 data.blocks = curr_cmd->blocks;
1056
1057 sg_init_one(data.sg, curr_data->buf,
1058 curr_data->buf_bytes);
1059
1060 if (curr_cmd->write_flag)
1061 data.flags = MMC_DATA_WRITE;
1062 else
1063 data.flags = MMC_DATA_READ;
1064
1065 /* data.flags must already be set before doing this. */
1066 mmc_set_data_timeout(&data, card);
1067
1068 /*
1069 * Allow overriding the timeout_ns for empirical tuning.
1070 */
1071 if (curr_cmd->data_timeout_ns)
1072 data.timeout_ns = curr_cmd->data_timeout_ns;
1073
1074 mrq.data = &data;
1075 }
1076
1077 mrq.cmd = &cmd;
1078
1079 err = mmc_set_blockcount(card, data.blocks,
1080 curr_cmd->write_flag & (1 << 31));
1081 if (err)
1082 goto cmd_rel_host;
1083
1084 mmc_wait_for_req(card->host, &mrq);
1085
1086 if (cmd.error) {
1087 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1088 __func__, cmd.error);
1089 err = cmd.error;
1090 goto cmd_rel_host;
1091 }
1092 if (data.error) {
1093 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1094 __func__, data.error);
1095 err = data.error;
1096 goto cmd_rel_host;
1097 }
1098
1099 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1100 sizeof(cmd.resp))) {
1101 err = -EFAULT;
1102 goto cmd_rel_host;
1103 }
1104
1105 if (!curr_cmd->write_flag) {
1106 if (copy_to_user((void __user *)(unsigned long)
1107 curr_cmd->data_ptr,
1108 curr_data->buf,
1109 curr_data->buf_bytes)) {
1110 err = -EFAULT;
1111 goto cmd_rel_host;
1112 }
1113 }
1114
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001115 /*
1116 * Ensure RPMB command has completed by polling CMD13
1117 * "Send Status".
1118 */
1119 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1120 if (err)
1121 dev_err(mmc_dev(card->host),
1122 "%s: Card Status=0x%08X, error %d\n",
1123 __func__, status, err);
1124 }
1125
Krishna Kondae6711632014-12-04 15:20:57 +02001126cmd_rel_host:
1127 mmc_put_card(card);
1128
1129idata_free:
1130 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1131 kfree(idata->data[i]->buf);
1132 kfree(idata->data[i]);
1133 }
1134 kfree(idata);
1135
1136cmd_done:
1137 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001138 return err;
1139}
1140
1141static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1142 struct mmc_ioc_cmd __user *ic_ptr)
1143{
1144 struct mmc_blk_ioc_data *idata;
1145 struct mmc_blk_data *md;
1146 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001147 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001148
Shawn Lin83c742c2016-03-16 18:15:47 +08001149 /*
1150 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1151 * whole block device, not on a partition. This prevents overspray
1152 * between sibling partitions.
1153 */
1154 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1155 return -EPERM;
1156
Jon Huntera5f57742015-09-22 10:27:53 +01001157 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301158 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001159 return PTR_ERR(idata);
1160
1161 md = mmc_blk_get(bdev->bd_disk);
1162 if (!md) {
1163 err = -EINVAL;
1164 goto cmd_err;
1165 }
1166
1167 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301168 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001169 err = PTR_ERR(card);
1170 goto cmd_done;
1171 }
1172
1173 mmc_get_card(card);
1174
Grant Grundlerb0934102015-09-23 18:30:33 -07001175 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001176
Adrian Hunter3c866562016-05-04 14:38:12 +03001177 /* Always switch back to main area after RPMB access */
1178 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1179 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1180
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001181 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001182
Grant Grundlerb0934102015-09-23 18:30:33 -07001183 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001184
John Calixtocb87ea22011-04-26 18:56:29 -04001185cmd_done:
1186 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001187cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001188 kfree(idata->buf);
1189 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001190 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001191}
1192
Jon Huntera5f57742015-09-22 10:27:53 +01001193static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1194 struct mmc_ioc_multi_cmd __user *user)
1195{
1196 struct mmc_blk_ioc_data **idata = NULL;
1197 struct mmc_ioc_cmd __user *cmds = user->cmds;
1198 struct mmc_card *card;
1199 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001200 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001201 __u64 num_of_cmds;
1202
Shawn Lin83c742c2016-03-16 18:15:47 +08001203 /*
1204 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1205 * whole block device, not on a partition. This prevents overspray
1206 * between sibling partitions.
1207 */
1208 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1209 return -EPERM;
1210
Jon Huntera5f57742015-09-22 10:27:53 +01001211 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1212 sizeof(num_of_cmds)))
1213 return -EFAULT;
1214
1215 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1216 return -EINVAL;
1217
1218 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1219 if (!idata)
1220 return -ENOMEM;
1221
1222 for (i = 0; i < num_of_cmds; i++) {
1223 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1224 if (IS_ERR(idata[i])) {
1225 err = PTR_ERR(idata[i]);
1226 num_of_cmds = i;
1227 goto cmd_err;
1228 }
1229 }
1230
1231 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001232 if (!md) {
1233 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001234 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001235 }
Jon Huntera5f57742015-09-22 10:27:53 +01001236
1237 card = md->queue.card;
1238 if (IS_ERR(card)) {
1239 err = PTR_ERR(card);
1240 goto cmd_done;
1241 }
1242
1243 mmc_get_card(card);
1244
Grant Grundlerb0934102015-09-23 18:30:33 -07001245 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1246 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001247
Adrian Hunter3c866562016-05-04 14:38:12 +03001248 /* Always switch back to main area after RPMB access */
1249 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1250 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1251
Jon Huntera5f57742015-09-22 10:27:53 +01001252 mmc_put_card(card);
1253
1254 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001255 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001256 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001257
1258cmd_done:
1259 mmc_blk_put(md);
1260cmd_err:
1261 for (i = 0; i < num_of_cmds; i++) {
1262 kfree(idata[i]->buf);
1263 kfree(idata[i]);
1264 }
1265 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001266 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001267}
1268
John Calixtocb87ea22011-04-26 18:56:29 -04001269static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1270 unsigned int cmd, unsigned long arg)
1271{
Jon Huntera5f57742015-09-22 10:27:53 +01001272 switch (cmd) {
1273 case MMC_IOC_CMD:
1274 return mmc_blk_ioctl_cmd(bdev,
1275 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001276 case MMC_IOC_RPMB_CMD:
1277 return mmc_blk_ioctl_rpmb_cmd(bdev,
1278 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001279 case MMC_IOC_MULTI_CMD:
1280 return mmc_blk_ioctl_multi_cmd(bdev,
1281 (struct mmc_ioc_multi_cmd __user *)arg);
1282 default:
1283 return -EINVAL;
1284 }
John Calixtocb87ea22011-04-26 18:56:29 -04001285}
1286
1287#ifdef CONFIG_COMPAT
1288static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1289 unsigned int cmd, unsigned long arg)
1290{
1291 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1292}
1293#endif
1294
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001295static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001296 .open = mmc_blk_open,
1297 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001298 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001300 .ioctl = mmc_blk_ioctl,
1301#ifdef CONFIG_COMPAT
1302 .compat_ioctl = mmc_blk_compat_ioctl,
1303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304};
1305
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001306static int mmc_blk_cmdq_switch(struct mmc_card *card,
1307 struct mmc_blk_data *md, bool enable)
1308{
1309 int ret = 0;
1310 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301311 struct mmc_host *host = card->host;
1312 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001313
1314 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1315 !card->ext_csd.cmdq_support ||
1316 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1317 (cmdq_mode == enable))
1318 return 0;
1319
1320 if (enable) {
1321 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1322 if (ret) {
1323 pr_err("%s: failed (%d) to set block-size to %d\n",
1324 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1325 goto out;
1326 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301327
1328 } else {
1329 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1330 ret = mmc_cmdq_halt(host, true);
1331 if (ret) {
1332 pr_err("%s: halt: failed: %d\n",
1333 mmc_hostname(host), ret);
1334 goto out;
1335 }
1336 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001337 }
1338
1339 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1340 EXT_CSD_CMDQ, enable,
1341 card->ext_csd.generic_cmd6_time);
1342 if (ret) {
1343 pr_err("%s: cmdq mode %sable failed %d\n",
1344 md->disk->disk_name, enable ? "en" : "dis", ret);
1345 goto out;
1346 }
1347
1348 if (enable)
1349 mmc_card_set_cmdq(card);
1350 else
1351 mmc_card_clr_cmdq(card);
1352out:
1353 return ret;
1354}
1355
Andrei Warkentin371a6892011-04-11 18:10:25 -05001356static inline int mmc_blk_part_switch(struct mmc_card *card,
1357 struct mmc_blk_data *md)
1358{
1359 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001360 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001361
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001362 if ((main_md->part_curr == md->part_type) &&
1363 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001364 return 0;
1365
1366 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001367 u8 part_config = card->ext_csd.part_config;
1368
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001369 if (md->part_type) {
1370 /* disable CQ mode for non-user data partitions */
1371 ret = mmc_blk_cmdq_switch(card, md, false);
1372 if (ret)
1373 return ret;
1374 }
1375
Adrian Hunter57da0c02016-05-04 14:38:13 +03001376 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1377 mmc_retune_pause(card->host);
1378
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001379 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1380 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001381
1382 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001383 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001384 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001385 if (ret) {
1386 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1387 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001388 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001389 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001390
1391 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001392 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001393
1394 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1395 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001396 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001397
1398 main_md->part_curr = md->part_type;
1399 return 0;
1400}
1401
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001402static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1403{
1404 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001405 u32 result;
1406 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001407
Venkatraman Sad5fd972011-08-25 00:30:50 +05301408 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001409 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001410 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001411
1412 struct scatterlist sg;
1413
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001414 cmd.opcode = MMC_APP_CMD;
1415 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001416 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001417
1418 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001419 if (err)
1420 return (u32)-1;
1421 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001422 return (u32)-1;
1423
1424 memset(&cmd, 0, sizeof(struct mmc_command));
1425
1426 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1427 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001428 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001429
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001430 data.blksz = 4;
1431 data.blocks = 1;
1432 data.flags = MMC_DATA_READ;
1433 data.sg = &sg;
1434 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301435 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001436
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001437 mrq.cmd = &cmd;
1438 mrq.data = &data;
1439
Ben Dooks051913d2009-06-08 23:33:57 +01001440 blocks = kmalloc(4, GFP_KERNEL);
1441 if (!blocks)
1442 return (u32)-1;
1443
1444 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001445
1446 mmc_wait_for_req(card->host, &mrq);
1447
Ben Dooks051913d2009-06-08 23:33:57 +01001448 result = ntohl(*blocks);
1449 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001450
Ben Dooks051913d2009-06-08 23:33:57 +01001451 if (cmd.error || data.error)
1452 result = (u32)-1;
1453
1454 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001455}
1456
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001457static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001458{
Chris Ball1278dba2011-04-13 23:40:30 -04001459 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001460 int err;
1461
Adrian Hunter504f1912008-10-16 12:55:25 +03001462 cmd.opcode = MMC_SEND_STATUS;
1463 if (!mmc_host_is_spi(card->host))
1464 cmd.arg = card->rca << 16;
1465 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001466 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1467 if (err == 0)
1468 *status = cmd.resp[0];
1469 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001470}
1471
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001472static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001473 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001474{
1475 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1476 int err = 0;
1477 u32 status;
1478
1479 do {
1480 err = get_card_status(card, &status, 5);
1481 if (err) {
1482 pr_err("%s: error %d requesting status\n",
1483 req->rq_disk->disk_name, err);
1484 return err;
1485 }
1486
1487 if (status & R1_ERROR) {
1488 pr_err("%s: %s: error sending status cmd, status %#x\n",
1489 req->rq_disk->disk_name, __func__, status);
1490 *gen_err = 1;
1491 }
1492
Ulf Hansson95a91292014-01-29 13:11:27 +01001493 /* We may rely on the host hw to handle busy detection.*/
1494 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1495 hw_busy_detect)
1496 break;
1497
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001498 /*
1499 * Timeout if the device never becomes ready for data and never
1500 * leaves the program state.
1501 */
1502 if (time_after(jiffies, timeout)) {
1503 pr_err("%s: Card stuck in programming state! %s %s\n",
1504 mmc_hostname(card->host),
1505 req->rq_disk->disk_name, __func__);
1506 return -ETIMEDOUT;
1507 }
1508
1509 /*
1510 * Some cards mishandle the status bits,
1511 * so make sure to check both the busy
1512 * indication and the card state.
1513 */
1514 } while (!(status & R1_READY_FOR_DATA) ||
1515 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1516
1517 return err;
1518}
1519
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001520static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1521 struct request *req, int *gen_err, u32 *stop_status)
1522{
1523 struct mmc_host *host = card->host;
1524 struct mmc_command cmd = {0};
1525 int err;
1526 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1527
1528 /*
1529 * Normally we use R1B responses for WRITE, but in cases where the host
1530 * has specified a max_busy_timeout we need to validate it. A failure
1531 * means we need to prevent the host from doing hw busy detection, which
1532 * is done by converting to a R1 response instead.
1533 */
1534 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1535 use_r1b_resp = false;
1536
1537 cmd.opcode = MMC_STOP_TRANSMISSION;
1538 if (use_r1b_resp) {
1539 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1540 cmd.busy_timeout = timeout_ms;
1541 } else {
1542 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1543 }
1544
1545 err = mmc_wait_for_cmd(host, &cmd, 5);
1546 if (err)
1547 return err;
1548
1549 *stop_status = cmd.resp[0];
1550
1551 /* No need to check card status in case of READ. */
1552 if (rq_data_dir(req) == READ)
1553 return 0;
1554
1555 if (!mmc_host_is_spi(host) &&
1556 (*stop_status & R1_ERROR)) {
1557 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1558 req->rq_disk->disk_name, __func__, *stop_status);
1559 *gen_err = 1;
1560 }
1561
1562 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1563}
1564
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301565#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001566#define ERR_RETRY 2
1567#define ERR_ABORT 1
1568#define ERR_CONTINUE 0
1569
1570static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1571 bool status_valid, u32 status)
1572{
1573 switch (error) {
1574 case -EILSEQ:
1575 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001576 pr_err_ratelimited(
1577 "%s: response CRC error sending %s command, card status %#x\n",
1578 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001579 name, status);
1580 return ERR_RETRY;
1581
1582 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001583 pr_err_ratelimited(
1584 "%s: timed out sending %s command, card status %#x\n",
1585 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001586
1587 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301588 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001589 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301590 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001591 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301592 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001593
1594 /*
1595 * If it was a r/w cmd crc error, or illegal command
1596 * (eg, issued in wrong state) then retry - we should
1597 * have corrected the state problem above.
1598 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301599 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001600 pr_err_ratelimited(
1601 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301602 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001603 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301604 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001605
1606 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001607 pr_err_ratelimited(
1608 "%s: not retrying timeout\n",
1609 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001610 return ERR_ABORT;
1611
1612 default:
1613 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001614 pr_err_ratelimited(
1615 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001616 req->rq_disk->disk_name, error, status);
1617 return ERR_ABORT;
1618 }
1619}
1620
1621/*
1622 * Initial r/w and stop cmd error recovery.
1623 * We don't know whether the card received the r/w cmd or not, so try to
1624 * restore things back to a sane state. Essentially, we do this as follows:
1625 * - Obtain card status. If the first attempt to obtain card status fails,
1626 * the status word will reflect the failed status cmd, not the failed
1627 * r/w cmd. If we fail to obtain card status, it suggests we can no
1628 * longer communicate with the card.
1629 * - Check the card state. If the card received the cmd but there was a
1630 * transient problem with the response, it might still be in a data transfer
1631 * mode. Try to send it a stop command. If this fails, we can't recover.
1632 * - If the r/w cmd failed due to a response CRC error, it was probably
1633 * transient, so retry the cmd.
1634 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1635 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1636 * illegal cmd, retry.
1637 * Otherwise we don't understand what happened, so abort.
1638 */
1639static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001640 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001641{
1642 bool prev_cmd_status_valid = true;
1643 u32 status, stop_status = 0;
1644 int err, retry;
1645
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301646 if (mmc_card_removed(card))
1647 return ERR_NOMEDIUM;
1648
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001649 /*
1650 * Try to get card status which indicates both the card state
1651 * and why there was no response. If the first attempt fails,
1652 * we can't be sure the returned status is for the r/w command.
1653 */
1654 for (retry = 2; retry >= 0; retry--) {
1655 err = get_card_status(card, &status, 0);
1656 if (!err)
1657 break;
1658
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001659 /* Re-tune if needed */
1660 mmc_retune_recheck(card->host);
1661
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 prev_cmd_status_valid = false;
1663 pr_err("%s: error %d sending status command, %sing\n",
1664 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1665 }
1666
1667 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301668 if (err) {
1669 /* Check if the card is removed */
1670 if (mmc_detect_card_removed(card->host))
1671 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001672 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301673 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001674
Adrian Hunter67716322011-08-29 16:42:15 +03001675 /* Flag ECC errors */
1676 if ((status & R1_CARD_ECC_FAILED) ||
1677 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1678 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1679 *ecc_err = 1;
1680
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001681 /* Flag General errors */
1682 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1683 if ((status & R1_ERROR) ||
1684 (brq->stop.resp[0] & R1_ERROR)) {
1685 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1686 req->rq_disk->disk_name, __func__,
1687 brq->stop.resp[0], status);
1688 *gen_err = 1;
1689 }
1690
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001691 /*
1692 * Check the current card state. If it is in some data transfer
1693 * mode, tell it to stop (and hopefully transition back to TRAN.)
1694 */
1695 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1696 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001697 err = send_stop(card,
1698 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1699 req, gen_err, &stop_status);
1700 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001701 pr_err("%s: error %d sending stop command\n",
1702 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001703 /*
1704 * If the stop cmd also timed out, the card is probably
1705 * not present, so abort. Other errors are bad news too.
1706 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001707 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001708 }
1709
Adrian Hunter67716322011-08-29 16:42:15 +03001710 if (stop_status & R1_CARD_ECC_FAILED)
1711 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001712 }
1713
1714 /* Check for set block count errors */
1715 if (brq->sbc.error)
1716 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1717 prev_cmd_status_valid, status);
1718
1719 /* Check for r/w command errors */
1720 if (brq->cmd.error)
1721 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1722 prev_cmd_status_valid, status);
1723
Adrian Hunter67716322011-08-29 16:42:15 +03001724 /* Data errors */
1725 if (!brq->stop.error)
1726 return ERR_CONTINUE;
1727
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001728 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001729 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001730 req->rq_disk->disk_name, brq->stop.error,
1731 brq->cmd.resp[0], status);
1732
1733 /*
1734 * Subsitute in our own stop status as this will give the error
1735 * state which happened during the execution of the r/w command.
1736 */
1737 if (stop_status) {
1738 brq->stop.resp[0] = stop_status;
1739 brq->stop.error = 0;
1740 }
1741 return ERR_CONTINUE;
1742}
1743
Adrian Hunter67716322011-08-29 16:42:15 +03001744static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1745 int type)
1746{
1747 int err;
1748
1749 if (md->reset_done & type)
1750 return -EEXIST;
1751
1752 md->reset_done |= type;
1753 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301754 if (err && err != -EOPNOTSUPP) {
1755 /* We failed to reset so we need to abort the request */
1756 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1757 __func__, err);
1758 return -ENODEV;
1759 }
1760
Adrian Hunter67716322011-08-29 16:42:15 +03001761 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301762 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001763 struct mmc_blk_data *main_md =
1764 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001765 int part_err;
1766
1767 main_md->part_curr = main_md->part_type;
1768 part_err = mmc_blk_part_switch(host->card, md);
1769 if (part_err) {
1770 /*
1771 * We have failed to get back into the correct
1772 * partition, so we need to abort the whole request.
1773 */
1774 return -ENODEV;
1775 }
1776 }
1777 return err;
1778}
1779
1780static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1781{
1782 md->reset_done &= ~type;
1783}
1784
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001785int mmc_access_rpmb(struct mmc_queue *mq)
1786{
1787 struct mmc_blk_data *md = mq->data;
1788 /*
1789 * If this is a RPMB partition access, return ture
1790 */
1791 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1792 return true;
1793
1794 return false;
1795}
1796
Sahitya Tummala9433a132015-06-09 09:38:36 +05301797static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1798 struct request *req)
1799{
1800 struct mmc_blk_data *md = mq->data;
1801 struct mmc_card *card = md->queue.card;
1802 struct mmc_host *host = card->host;
1803 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1804 struct mmc_cmdq_req *cmdq_req;
1805 struct mmc_queue_req *active_mqrq;
1806
1807 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1808 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1809
1810 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1811
1812 active_mqrq = &mq->mqrq_cmdq[req->tag];
1813 active_mqrq->req = req;
1814
1815 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1816 cmdq_req->cmdq_req_flags |= QBR;
1817 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1818 cmdq_req->tag = req->tag;
1819 return cmdq_req;
1820}
1821
1822static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1823 struct request *req)
1824{
1825 struct mmc_blk_data *md = mq->data;
1826 struct mmc_card *card = md->queue.card;
1827 struct mmc_cmdq_req *cmdq_req = NULL;
1828 struct mmc_host *host = card->host;
1829 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1830 unsigned int from, nr, arg;
1831 int err = 0;
1832
1833 if (!mmc_can_erase(card)) {
1834 err = -EOPNOTSUPP;
1835 goto out;
1836 }
1837
1838 from = blk_rq_pos(req);
1839 nr = blk_rq_sectors(req);
1840
1841 if (mmc_can_discard(card))
1842 arg = MMC_DISCARD_ARG;
1843 else if (mmc_can_trim(card))
1844 arg = MMC_TRIM_ARG;
1845 else
1846 arg = MMC_ERASE_ARG;
1847
1848 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1849 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1850 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1851 EXT_CSD_CMD_SET_NORMAL,
1852 INAND_CMD38_ARG_EXT_CSD,
1853 arg == MMC_TRIM_ARG ?
1854 INAND_CMD38_ARG_TRIM :
1855 INAND_CMD38_ARG_ERASE,
1856 0, true, false);
1857 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1858 if (err)
1859 goto clear_dcmd;
1860 }
1861 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1862clear_dcmd:
1863 /* clear pending request */
1864 if (cmdq_req) {
1865 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1866 &ctx_info->active_reqs));
1867 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1868 }
1869out:
1870 blk_end_request(req, err, blk_rq_bytes(req));
1871
1872 if (test_and_clear_bit(0, &ctx_info->req_starved))
1873 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001874 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301875 return err ? 1 : 0;
1876}
1877
Adrian Hunterbd788c92010-08-11 14:17:47 -07001878static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1879{
1880 struct mmc_blk_data *md = mq->data;
1881 struct mmc_card *card = md->queue.card;
1882 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001883 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001884
Adrian Hunterbd788c92010-08-11 14:17:47 -07001885 if (!mmc_can_erase(card)) {
1886 err = -EOPNOTSUPP;
1887 goto out;
1888 }
1889
1890 from = blk_rq_pos(req);
1891 nr = blk_rq_sectors(req);
1892
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001893 if (mmc_can_discard(card))
1894 arg = MMC_DISCARD_ARG;
1895 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001896 arg = MMC_TRIM_ARG;
1897 else
1898 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001899retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001900 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1901 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1902 INAND_CMD38_ARG_EXT_CSD,
1903 arg == MMC_TRIM_ARG ?
1904 INAND_CMD38_ARG_TRIM :
1905 INAND_CMD38_ARG_ERASE,
1906 0);
1907 if (err)
1908 goto out;
1909 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001910 err = mmc_erase(card, from, nr, arg);
1911out:
Adrian Hunter67716322011-08-29 16:42:15 +03001912 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1913 goto retry;
1914 if (!err)
1915 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301916 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001917
Adrian Hunterbd788c92010-08-11 14:17:47 -07001918 return err ? 0 : 1;
1919}
1920
Sahitya Tummala9433a132015-06-09 09:38:36 +05301921static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1922 struct request *req)
1923{
1924 struct mmc_blk_data *md = mq->data;
1925 struct mmc_card *card = md->queue.card;
1926 struct mmc_cmdq_req *cmdq_req = NULL;
1927 unsigned int from, nr, arg;
1928 struct mmc_host *host = card->host;
1929 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1930 int err = 0;
1931
1932 if (!(mmc_can_secure_erase_trim(card))) {
1933 err = -EOPNOTSUPP;
1934 goto out;
1935 }
1936
1937 from = blk_rq_pos(req);
1938 nr = blk_rq_sectors(req);
1939
1940 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1941 arg = MMC_SECURE_TRIM1_ARG;
1942 else
1943 arg = MMC_SECURE_ERASE_ARG;
1944
1945 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1947 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1948 EXT_CSD_CMD_SET_NORMAL,
1949 INAND_CMD38_ARG_EXT_CSD,
1950 arg == MMC_SECURE_TRIM1_ARG ?
1951 INAND_CMD38_ARG_SECTRIM1 :
1952 INAND_CMD38_ARG_SECERASE,
1953 0, true, false);
1954 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1955 if (err)
1956 goto clear_dcmd;
1957 }
1958
1959 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1960 if (err)
1961 goto clear_dcmd;
1962
1963 if (arg == MMC_SECURE_TRIM1_ARG) {
1964 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1965 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1966 EXT_CSD_CMD_SET_NORMAL,
1967 INAND_CMD38_ARG_EXT_CSD,
1968 INAND_CMD38_ARG_SECTRIM2,
1969 0, true, false);
1970 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1971 if (err)
1972 goto clear_dcmd;
1973 }
1974
1975 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
1976 MMC_SECURE_TRIM2_ARG);
1977 }
1978clear_dcmd:
1979 /* clear pending request */
1980 if (cmdq_req) {
1981 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1982 &ctx_info->active_reqs));
1983 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1984 }
1985out:
1986 blk_end_request(req, err, blk_rq_bytes(req));
1987
1988 if (test_and_clear_bit(0, &ctx_info->req_starved))
1989 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001990 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301991 return err ? 1 : 0;
1992}
1993
Adrian Hunter49804542010-08-11 14:17:50 -07001994static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1995 struct request *req)
1996{
1997 struct mmc_blk_data *md = mq->data;
1998 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001999 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002000 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002001
Maya Erez775a9362013-04-18 15:41:55 +03002002 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002003 err = -EOPNOTSUPP;
2004 goto out;
2005 }
2006
2007 from = blk_rq_pos(req);
2008 nr = blk_rq_sectors(req);
2009
Maya Erez775a9362013-04-18 15:41:55 +03002010 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2011 arg = MMC_SECURE_TRIM1_ARG;
2012 else
2013 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002014
Adrian Hunter67716322011-08-29 16:42:15 +03002015retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002016 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2017 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2018 INAND_CMD38_ARG_EXT_CSD,
2019 arg == MMC_SECURE_TRIM1_ARG ?
2020 INAND_CMD38_ARG_SECTRIM1 :
2021 INAND_CMD38_ARG_SECERASE,
2022 0);
2023 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002024 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002025 }
Adrian Hunter28302812012-04-05 14:45:48 +03002026
Adrian Hunter49804542010-08-11 14:17:50 -07002027 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002028 if (err == -EIO)
2029 goto out_retry;
2030 if (err)
2031 goto out;
2032
2033 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002034 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2035 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2036 INAND_CMD38_ARG_EXT_CSD,
2037 INAND_CMD38_ARG_SECTRIM2,
2038 0);
2039 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002040 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002041 }
Adrian Hunter28302812012-04-05 14:45:48 +03002042
Adrian Hunter49804542010-08-11 14:17:50 -07002043 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002044 if (err == -EIO)
2045 goto out_retry;
2046 if (err)
2047 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002048 }
Adrian Hunter28302812012-04-05 14:45:48 +03002049
Adrian Hunter28302812012-04-05 14:45:48 +03002050out_retry:
2051 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002052 goto retry;
2053 if (!err)
2054 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002055out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302056 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002057
Adrian Hunter49804542010-08-11 14:17:50 -07002058 return err ? 0 : 1;
2059}
2060
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002061static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2062{
2063 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002064 struct mmc_card *card = md->queue.card;
2065 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002066
Sahitya Tummala61868a42015-05-28 16:54:19 +05302067 if (!req)
2068 return 0;
2069
2070 if (req->cmd_flags & REQ_BARRIER) {
2071 /*
2072 * If eMMC cache flush policy is set to 1, then the device
2073 * shall flush the requests in First-In-First-Out (FIFO) order.
2074 * In this case, as per spec, the host must not send any cache
2075 * barrier requests as they are redundant and add unnecessary
2076 * overhead to both device and host.
2077 */
2078 if (card->ext_csd.cache_flush_policy & 1)
2079 goto end_req;
2080
2081 /*
2082 * In case barrier is not supported or enabled in the device,
2083 * use flush as a fallback option.
2084 */
2085 ret = mmc_cache_barrier(card);
2086 if (ret)
2087 ret = mmc_flush_cache(card);
2088 } else if (req_op(req) == REQ_OP_FLUSH) {
2089 ret = mmc_flush_cache(card);
2090 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002091 if (ret == -ENODEV) {
2092 pr_err("%s: %s: restart mmc card",
2093 req->rq_disk->disk_name, __func__);
2094 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2095 pr_err("%s: %s: fail to restart mmc",
2096 req->rq_disk->disk_name, __func__);
2097 else
2098 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2099 }
2100
2101 if (ret) {
2102 pr_err("%s: %s: notify flush error to upper layers",
2103 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002104 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002105 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002106
Mark Salyzyn6904e432016-01-28 11:12:25 -08002107#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2108 else if (atomic_read(&mq->cache_size)) {
2109 long used = mmc_blk_cache_used(mq, jiffies);
2110
2111 if (used) {
2112 int speed = atomic_read(&mq->max_write_speed);
2113
2114 if (speed_valid(speed)) {
2115 unsigned long msecs = jiffies_to_msecs(
2116 size_and_speed_to_jiffies(
2117 used, speed));
2118 if (msecs)
2119 msleep(msecs);
2120 }
2121 }
2122 }
2123#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302124end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302125 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002126
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002127 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002128}
2129
2130/*
2131 * Reformat current write as a reliable write, supporting
2132 * both legacy and the enhanced reliable write MMC cards.
2133 * In each transfer we'll handle only as much as a single
2134 * reliable write can handle, thus finish the request in
2135 * partial completions.
2136 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002137static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2138 struct mmc_card *card,
2139 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002140{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002141 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2142 /* Legacy mode imposes restrictions on transfers. */
2143 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2144 brq->data.blocks = 1;
2145
2146 if (brq->data.blocks > card->ext_csd.rel_sectors)
2147 brq->data.blocks = card->ext_csd.rel_sectors;
2148 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2149 brq->data.blocks = 1;
2150 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002151}
2152
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002153#define CMD_ERRORS \
2154 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2155 R1_ADDRESS_ERROR | /* Misaligned address */ \
2156 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2157 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2158 R1_CC_ERROR | /* Card controller error */ \
2159 R1_ERROR) /* General/unknown error */
2160
Per Forlinee8a43a2011-07-01 18:55:33 +02002161static int mmc_blk_err_check(struct mmc_card *card,
2162 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002163{
Per Forlinee8a43a2011-07-01 18:55:33 +02002164 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2165 mmc_active);
2166 struct mmc_blk_request *brq = &mq_mrq->brq;
2167 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002168 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002169 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002170
2171 /*
2172 * sbc.error indicates a problem with the set block count
2173 * command. No data will have been transferred.
2174 *
2175 * cmd.error indicates a problem with the r/w command. No
2176 * data will have been transferred.
2177 *
2178 * stop.error indicates a problem with the stop command. Data
2179 * may have been transferred, or may still be transferring.
2180 */
Adrian Hunter67716322011-08-29 16:42:15 +03002181 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2182 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002183 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002184 case ERR_RETRY:
2185 return MMC_BLK_RETRY;
2186 case ERR_ABORT:
2187 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302188 case ERR_NOMEDIUM:
2189 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002190 case ERR_CONTINUE:
2191 break;
2192 }
2193 }
2194
2195 /*
2196 * Check for errors relating to the execution of the
2197 * initial command - such as address errors. No data
2198 * has been transferred.
2199 */
2200 if (brq->cmd.resp[0] & CMD_ERRORS) {
2201 pr_err("%s: r/w command failed, status = %#x\n",
2202 req->rq_disk->disk_name, brq->cmd.resp[0]);
2203 return MMC_BLK_ABORT;
2204 }
2205
2206 /*
2207 * Everything else is either success, or a data error of some
2208 * kind. If it was a write, we may have transitioned to
2209 * program mode, which we have to wait for it to complete.
2210 */
2211 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002212 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002213
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002214 /* Check stop command response */
2215 if (brq->stop.resp[0] & R1_ERROR) {
2216 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2217 req->rq_disk->disk_name, __func__,
2218 brq->stop.resp[0]);
2219 gen_err = 1;
2220 }
2221
Ulf Hansson95a91292014-01-29 13:11:27 +01002222 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2223 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002224 if (err)
2225 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002226 }
2227
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002228 /* if general error occurs, retry the write operation. */
2229 if (gen_err) {
2230 pr_warn("%s: retrying write for general error\n",
2231 req->rq_disk->disk_name);
2232 return MMC_BLK_RETRY;
2233 }
2234
Per Forlind78d4a82011-07-01 18:55:30 +02002235 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002236 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002237 pr_debug("%s: retrying because a re-tune was needed\n",
2238 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002239 brq->retune_retry_done = 1;
2240 return MMC_BLK_RETRY;
2241 }
Per Forlind78d4a82011-07-01 18:55:30 +02002242 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2243 req->rq_disk->disk_name, brq->data.error,
2244 (unsigned)blk_rq_pos(req),
2245 (unsigned)blk_rq_sectors(req),
2246 brq->cmd.resp[0], brq->stop.resp[0]);
2247
2248 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002249 if (ecc_err)
2250 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002251 return MMC_BLK_DATA_ERR;
2252 } else {
2253 return MMC_BLK_CMD_ERR;
2254 }
2255 }
2256
Adrian Hunter67716322011-08-29 16:42:15 +03002257 if (!brq->data.bytes_xfered)
2258 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002259
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002260 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2261 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2262 return MMC_BLK_PARTIAL;
2263 else
2264 return MMC_BLK_SUCCESS;
2265 }
2266
Adrian Hunter67716322011-08-29 16:42:15 +03002267 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2268 return MMC_BLK_PARTIAL;
2269
2270 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002271}
2272
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002273static int mmc_blk_packed_err_check(struct mmc_card *card,
2274 struct mmc_async_req *areq)
2275{
2276 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2277 mmc_active);
2278 struct request *req = mq_rq->req;
2279 struct mmc_packed *packed = mq_rq->packed;
2280 int err, check, status;
2281 u8 *ext_csd;
2282
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002283 packed->retries--;
2284 check = mmc_blk_err_check(card, areq);
2285 err = get_card_status(card, &status, 0);
2286 if (err) {
2287 pr_err("%s: error %d sending status command\n",
2288 req->rq_disk->disk_name, err);
2289 return MMC_BLK_ABORT;
2290 }
2291
2292 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002293 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002294 if (err) {
2295 pr_err("%s: error %d sending ext_csd\n",
2296 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002297 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002298 }
2299
2300 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2301 EXT_CSD_PACKED_FAILURE) &&
2302 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2303 EXT_CSD_PACKED_GENERIC_ERROR)) {
2304 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2305 EXT_CSD_PACKED_INDEXED_ERROR) {
2306 packed->idx_failure =
2307 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2308 check = MMC_BLK_PARTIAL;
2309 }
2310 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2311 "failure index: %d\n",
2312 req->rq_disk->disk_name, packed->nr_entries,
2313 packed->blocks, packed->idx_failure);
2314 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002315 kfree(ext_csd);
2316 }
2317
2318 return check;
2319}
2320
Per Forlin54d49d72011-07-01 18:55:29 +02002321static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2322 struct mmc_card *card,
2323 int disable_multi,
2324 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Per Forlin54d49d72011-07-01 18:55:29 +02002326 u32 readcmd, writecmd;
2327 struct mmc_blk_request *brq = &mqrq->brq;
2328 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302330 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002332 /*
2333 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002334 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002335 */
Luca Porziod3df0462015-11-06 15:12:26 +00002336 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002337 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002338 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002339
Per Forlin54d49d72011-07-01 18:55:29 +02002340 memset(brq, 0, sizeof(struct mmc_blk_request));
2341 brq->mrq.cmd = &brq->cmd;
2342 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Per Forlin54d49d72011-07-01 18:55:29 +02002344 brq->cmd.arg = blk_rq_pos(req);
2345 if (!mmc_card_blockaddr(card))
2346 brq->cmd.arg <<= 9;
2347 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2348 brq->data.blksz = 512;
2349 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2350 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002351 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Asutosh Dasf0665412012-07-27 18:10:19 +05302353 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002354 /*
2355 * The block layer doesn't support all sector count
2356 * restrictions, so we need to be prepared for too big
2357 * requests.
2358 */
2359 if (brq->data.blocks > card->host->max_blk_count)
2360 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002362 if (brq->data.blocks > 1) {
2363 /*
2364 * After a read error, we redo the request one sector
2365 * at a time in order to accurately determine which
2366 * sectors can be read successfully.
2367 */
2368 if (disable_multi)
2369 brq->data.blocks = 1;
2370
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002371 /*
2372 * Some controllers have HW issues while operating
2373 * in multiple I/O mode
2374 */
2375 if (card->host->ops->multi_io_quirk)
2376 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2377 (rq_data_dir(req) == READ) ?
2378 MMC_DATA_READ : MMC_DATA_WRITE,
2379 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002380 }
Per Forlin54d49d72011-07-01 18:55:29 +02002381
2382 if (brq->data.blocks > 1 || do_rel_wr) {
2383 /* SPI multiblock writes terminate using a special
2384 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002385 */
Per Forlin54d49d72011-07-01 18:55:29 +02002386 if (!mmc_host_is_spi(card->host) ||
2387 rq_data_dir(req) == READ)
2388 brq->mrq.stop = &brq->stop;
2389 readcmd = MMC_READ_MULTIPLE_BLOCK;
2390 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2391 } else {
2392 brq->mrq.stop = NULL;
2393 readcmd = MMC_READ_SINGLE_BLOCK;
2394 writecmd = MMC_WRITE_BLOCK;
2395 }
2396 if (rq_data_dir(req) == READ) {
2397 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002398 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002399 if (brq->mrq.stop)
2400 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2401 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002402 } else {
2403 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002404 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002405 if (brq->mrq.stop)
2406 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2407 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002408 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002409
Per Forlin54d49d72011-07-01 18:55:29 +02002410 if (do_rel_wr)
2411 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002412
Per Forlin54d49d72011-07-01 18:55:29 +02002413 /*
Saugata Das42659002011-12-21 13:09:17 +05302414 * Data tag is used only during writing meta data to speed
2415 * up write and any subsequent read of this meta data
2416 */
2417 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2418 (req->cmd_flags & REQ_META) &&
2419 (rq_data_dir(req) == WRITE) &&
2420 ((brq->data.blocks * brq->data.blksz) >=
2421 card->ext_csd.data_tag_unit_size);
2422
2423 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002424 * Pre-defined multi-block transfers are preferable to
2425 * open ended-ones (and necessary for reliable writes).
2426 * However, it is not sufficient to just send CMD23,
2427 * and avoid the final CMD12, as on an error condition
2428 * CMD12 (stop) needs to be sent anyway. This, coupled
2429 * with Auto-CMD23 enhancements provided by some
2430 * hosts, means that the complexity of dealing
2431 * with this is best left to the host. If CMD23 is
2432 * supported by card and host, we'll fill sbc in and let
2433 * the host deal with handling it correctly. This means
2434 * that for hosts that don't expose MMC_CAP_CMD23, no
2435 * change of behavior will be observed.
2436 *
2437 * N.B: Some MMC cards experience perf degradation.
2438 * We'll avoid using CMD23-bounded multiblock writes for
2439 * these, while retaining features like reliable writes.
2440 */
Saugata Das42659002011-12-21 13:09:17 +05302441 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2442 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2443 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002444 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2445 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302446 (do_rel_wr ? (1 << 31) : 0) |
2447 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002448 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2449 brq->mrq.sbc = &brq->sbc;
2450 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002451
Per Forlin54d49d72011-07-01 18:55:29 +02002452 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002453
Per Forlin54d49d72011-07-01 18:55:29 +02002454 brq->data.sg = mqrq->sg;
2455 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002456
Per Forlin54d49d72011-07-01 18:55:29 +02002457 /*
2458 * Adjust the sg list so it is the same size as the
2459 * request.
2460 */
2461 if (brq->data.blocks != blk_rq_sectors(req)) {
2462 int i, data_size = brq->data.blocks << 9;
2463 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002464
Per Forlin54d49d72011-07-01 18:55:29 +02002465 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2466 data_size -= sg->length;
2467 if (data_size <= 0) {
2468 sg->length += data_size;
2469 i++;
2470 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002471 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002472 }
Per Forlin54d49d72011-07-01 18:55:29 +02002473 brq->data.sg_len = i;
2474 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002475
Per Forlinee8a43a2011-07-01 18:55:33 +02002476 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302477 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002478 mqrq->mmc_active.err_check = mmc_blk_err_check;
2479
Per Forlin54d49d72011-07-01 18:55:29 +02002480 mmc_queue_bounce_pre(mqrq);
2481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002483static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2484 struct mmc_card *card)
2485{
2486 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2487 unsigned int max_seg_sz = queue_max_segment_size(q);
2488 unsigned int len, nr_segs = 0;
2489
2490 do {
2491 len = min(hdr_sz, max_seg_sz);
2492 hdr_sz -= len;
2493 nr_segs++;
2494 } while (hdr_sz);
2495
2496 return nr_segs;
2497}
2498
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002499/**
2500 * mmc_blk_disable_wr_packing() - disables packing mode
2501 * @mq: MMC queue.
2502 *
2503 */
2504void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2505{
2506 if (mq) {
2507 mq->wr_packing_enabled = false;
2508 mq->num_of_potential_packed_wr_reqs = 0;
2509 }
2510}
2511EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2512
Lee Susman841fd132013-04-23 17:59:26 +03002513static int get_packed_trigger(int potential, struct mmc_card *card,
2514 struct request *req, int curr_trigger)
2515{
2516 static int num_mean_elements = 1;
2517 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2518 unsigned int trigger = curr_trigger;
2519 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2520
2521 /* scale down the upper bound to 75% */
2522 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2523
2524 /*
2525 * since the most common calls for this function are with small
2526 * potential write values and since we don't want these calls to affect
2527 * the packed trigger, set a lower bound and ignore calls with
2528 * potential lower than that bound
2529 */
2530 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2531 return trigger;
2532
2533 /*
2534 * this is to prevent integer overflow in the following calculation:
2535 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2536 */
2537 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2538 num_mean_elements = 1;
2539 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2540 }
2541
2542 /*
2543 * get next mean value based on previous mean value and current
2544 * potential packed writes. Calculation is as follows:
2545 * mean_pot[i+1] =
2546 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2547 */
2548 mean_potential *= num_mean_elements;
2549 /*
2550 * add num_mean_elements so that the division of two integers doesn't
2551 * lower mean_potential too much
2552 */
2553 if (potential > mean_potential)
2554 mean_potential += num_mean_elements;
2555 mean_potential += potential;
2556 /* this is for gaining more precision when dividing two integers */
2557 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2558 /* this completes the mean calculation */
2559 mean_potential /= ++num_mean_elements;
2560 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2561
2562 /*
2563 * if current potential packed writes is greater than the mean potential
2564 * then the heuristic is that the following workload will contain many
2565 * write requests, therefore we lower the packed trigger. In the
2566 * opposite case we want to increase the trigger in order to get less
2567 * packing events.
2568 */
2569 if (potential >= mean_potential)
2570 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2571 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2572 else
2573 trigger = (trigger >= pckd_trgr_upper_bound) ?
2574 pckd_trgr_upper_bound : trigger + 1;
2575
2576 /*
2577 * an urgent read request indicates a packed list being interrupted
2578 * by this read, therefore we aim for less packing, hence the trigger
2579 * gets increased
2580 */
2581 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2582 trigger += PCKD_TRGR_URGENT_PENALTY;
2583
2584 return trigger;
2585}
2586
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002587static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2588 struct request *req)
2589{
2590 struct mmc_host *host = mq->card->host;
2591 int data_dir;
2592
2593 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2594 return;
2595
Maya Erez8e2b3c32012-12-02 13:27:15 +02002596 /* Support for the write packing on eMMC 4.5 or later */
2597 if (mq->card->ext_csd.rev <= 5)
2598 return;
2599
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002600 /*
2601 * In case the packing control is not supported by the host, it should
2602 * not have an effect on the write packing. Therefore we have to enable
2603 * the write packing
2604 */
2605 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2606 mq->wr_packing_enabled = true;
2607 return;
2608 }
2609
2610 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2611 if (mq->num_of_potential_packed_wr_reqs >
2612 mq->num_wr_reqs_to_start_packing)
2613 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002614 mq->num_wr_reqs_to_start_packing =
2615 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2616 mq->card, req,
2617 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002618 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002619 return;
2620 }
2621
2622 data_dir = rq_data_dir(req);
2623
2624 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002625 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002626 mq->num_wr_reqs_to_start_packing =
2627 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2628 mq->card, req,
2629 mq->num_wr_reqs_to_start_packing);
2630 mq->num_of_potential_packed_wr_reqs = 0;
2631 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002632 return;
2633 } else if (data_dir == WRITE) {
2634 mq->num_of_potential_packed_wr_reqs++;
2635 }
2636
2637 if (mq->num_of_potential_packed_wr_reqs >
2638 mq->num_wr_reqs_to_start_packing)
2639 mq->wr_packing_enabled = true;
2640}
2641
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002642struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2643{
2644 if (!card)
2645 return NULL;
2646
2647 return &card->wr_pack_stats;
2648}
2649EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2650
2651void mmc_blk_init_packed_statistics(struct mmc_card *card)
2652{
2653 int max_num_of_packed_reqs = 0;
2654
2655 if (!card || !card->wr_pack_stats.packing_events)
2656 return;
2657
2658 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2659
2660 spin_lock(&card->wr_pack_stats.lock);
2661 memset(card->wr_pack_stats.packing_events, 0,
2662 (max_num_of_packed_reqs + 1) *
2663 sizeof(*card->wr_pack_stats.packing_events));
2664 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2665 sizeof(card->wr_pack_stats.pack_stop_reason));
2666 card->wr_pack_stats.enabled = true;
2667 spin_unlock(&card->wr_pack_stats.lock);
2668}
2669EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2670
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002671static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2672{
2673 struct request_queue *q = mq->queue;
2674 struct mmc_card *card = mq->card;
2675 struct request *cur = req, *next = NULL;
2676 struct mmc_blk_data *md = mq->data;
2677 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2678 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2679 unsigned int req_sectors = 0, phys_segments = 0;
2680 unsigned int max_blk_count, max_phys_segs;
2681 bool put_back = true;
2682 u8 max_packed_rw = 0;
2683 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002684 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002685
Shawn Lin96e52da2016-08-26 08:49:55 +08002686 /*
2687 * We don't need to check packed for any further
2688 * operation of packed stuff as we set MMC_PACKED_NONE
2689 * and return zero for reqs if geting null packed. Also
2690 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2691 * it again when removing blk req.
2692 */
2693 if (!mqrq->packed) {
2694 md->flags &= (~MMC_BLK_PACKED_CMD);
2695 goto no_packed;
2696 }
2697
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002698 if (!(md->flags & MMC_BLK_PACKED_CMD))
2699 goto no_packed;
2700
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002701 if (!mq->wr_packing_enabled)
2702 goto no_packed;
2703
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002704 if ((rq_data_dir(cur) == WRITE) &&
2705 mmc_host_packed_wr(card->host))
2706 max_packed_rw = card->ext_csd.max_packed_writes;
2707
2708 if (max_packed_rw == 0)
2709 goto no_packed;
2710
2711 if (mmc_req_rel_wr(cur) &&
2712 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2713 goto no_packed;
2714
2715 if (mmc_large_sector(card) &&
2716 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2717 goto no_packed;
2718
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002719 if (cur->cmd_flags & REQ_FUA)
2720 goto no_packed;
2721
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002722 mmc_blk_clear_packed(mqrq);
2723
2724 max_blk_count = min(card->host->max_blk_count,
2725 card->host->max_req_size >> 9);
2726 if (unlikely(max_blk_count > 0xffff))
2727 max_blk_count = 0xffff;
2728
2729 max_phys_segs = queue_max_segments(q);
2730 req_sectors += blk_rq_sectors(cur);
2731 phys_segments += cur->nr_phys_segments;
2732
2733 if (rq_data_dir(cur) == WRITE) {
2734 req_sectors += mmc_large_sector(card) ? 8 : 1;
2735 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2736 }
2737
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002738 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002739 do {
2740 if (reqs >= max_packed_rw - 1) {
2741 put_back = false;
2742 break;
2743 }
2744
2745 spin_lock_irq(q->queue_lock);
2746 next = blk_fetch_request(q);
2747 spin_unlock_irq(q->queue_lock);
2748 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002749 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002750 put_back = false;
2751 break;
2752 }
2753
2754 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002755 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2756 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002757 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002758 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002759
Mike Christie3a5e02c2016-06-05 14:32:23 -05002760 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002761 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002762 req_op(next) == REQ_OP_FLUSH) {
2763 if (req_op(next) != REQ_OP_SECURE_ERASE)
2764 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002765 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002766 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002767
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002768 if (next->cmd_flags & REQ_FUA) {
2769 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2770 break;
2771 }
2772
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002773 if (rq_data_dir(cur) != rq_data_dir(next)) {
2774 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002775 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002776 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002777
2778 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002779 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2780 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002781 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002782 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002783
2784 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002785 if (req_sectors > max_blk_count) {
2786 if (stats->enabled)
2787 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002788 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002789 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002790
2791 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002792 if (phys_segments > max_phys_segs) {
2793 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002794 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002795 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796
Maya Erez5a8dae12014-12-04 15:13:59 +02002797 if (mq->no_pack_for_random) {
2798 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2799 blk_rq_pos(next)) {
2800 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2801 put_back = 1;
2802 break;
2803 }
2804 }
2805
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002806 if (rq_data_dir(next) == WRITE)
2807 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002808 list_add_tail(&next->queuelist, &mqrq->packed->list);
2809 cur = next;
2810 reqs++;
2811 } while (1);
2812
2813 if (put_back) {
2814 spin_lock_irq(q->queue_lock);
2815 blk_requeue_request(q, next);
2816 spin_unlock_irq(q->queue_lock);
2817 }
2818
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002819 if (stats->enabled) {
2820 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2821 stats->packing_events[reqs + 1]++;
2822 if (reqs + 1 == max_packed_rw)
2823 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2824 }
2825
2826 spin_unlock(&stats->lock);
2827
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002828 if (reqs > 0) {
2829 list_add(&req->queuelist, &mqrq->packed->list);
2830 mqrq->packed->nr_entries = ++reqs;
2831 mqrq->packed->retries = reqs;
2832 return reqs;
2833 }
2834
2835no_packed:
2836 mqrq->cmd_type = MMC_PACKED_NONE;
2837 return 0;
2838}
2839
2840static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2841 struct mmc_card *card,
2842 struct mmc_queue *mq)
2843{
2844 struct mmc_blk_request *brq = &mqrq->brq;
2845 struct request *req = mqrq->req;
2846 struct request *prq;
2847 struct mmc_blk_data *md = mq->data;
2848 struct mmc_packed *packed = mqrq->packed;
2849 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002850 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002851 u8 hdr_blocks;
2852 u8 i = 1;
2853
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002854 mqrq->cmd_type = MMC_PACKED_WRITE;
2855 packed->blocks = 0;
2856 packed->idx_failure = MMC_PACKED_NR_IDX;
2857
2858 packed_cmd_hdr = packed->cmd_hdr;
2859 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002860 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2861 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002862 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2863
2864 /*
2865 * Argument for each entry of packed group
2866 */
2867 list_for_each_entry(prq, &packed->list, queuelist) {
2868 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2869 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2870 (prq->cmd_flags & REQ_META) &&
2871 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002872 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002873 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002874 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002875 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2876 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002877 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002878 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002879 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002880 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002881 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002882 packed->blocks += blk_rq_sectors(prq);
2883 i++;
2884 }
2885
2886 memset(brq, 0, sizeof(struct mmc_blk_request));
2887 brq->mrq.cmd = &brq->cmd;
2888 brq->mrq.data = &brq->data;
2889 brq->mrq.sbc = &brq->sbc;
2890 brq->mrq.stop = &brq->stop;
2891
2892 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2893 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2894 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2895
2896 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2897 brq->cmd.arg = blk_rq_pos(req);
2898 if (!mmc_card_blockaddr(card))
2899 brq->cmd.arg <<= 9;
2900 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2901
2902 brq->data.blksz = 512;
2903 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002904 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302905 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002906
2907 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2908 brq->stop.arg = 0;
2909 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2910
2911 mmc_set_data_timeout(&brq->data, card);
2912
2913 brq->data.sg = mqrq->sg;
2914 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2915
2916 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002917
2918 /*
2919 * This is intended for packed commands tests usage - in case these
2920 * functions are not in use the respective pointers are NULL
2921 */
2922 if (mq->err_check_fn)
2923 mqrq->mmc_active.err_check = mq->err_check_fn;
2924 else
2925 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2926
2927 if (mq->packed_test_fn)
2928 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002929
2930 mmc_queue_bounce_pre(mqrq);
2931}
2932
Adrian Hunter67716322011-08-29 16:42:15 +03002933static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2934 struct mmc_blk_request *brq, struct request *req,
2935 int ret)
2936{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002937 struct mmc_queue_req *mq_rq;
2938 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2939
Adrian Hunter67716322011-08-29 16:42:15 +03002940 /*
2941 * If this is an SD card and we're writing, we can first
2942 * mark the known good sectors as ok.
2943 *
2944 * If the card is not SD, we can still ok written sectors
2945 * as reported by the controller (which might be less than
2946 * the real number of written sectors, but never more).
2947 */
2948 if (mmc_card_sd(card)) {
2949 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302950 if (!brq->data.fault_injected) {
2951 blocks = mmc_sd_num_wr_blocks(card);
2952 if (blocks != (u32)-1)
2953 ret = blk_end_request(req, 0, blocks << 9);
2954 } else
2955 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002956 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002957 if (!mmc_packed_cmd(mq_rq->cmd_type))
2958 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002959 }
2960 return ret;
2961}
2962
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002963static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2964{
2965 struct request *prq;
2966 struct mmc_packed *packed = mq_rq->packed;
2967 int idx = packed->idx_failure, i = 0;
2968 int ret = 0;
2969
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002970 while (!list_empty(&packed->list)) {
2971 prq = list_entry_rq(packed->list.next);
2972 if (idx == i) {
2973 /* retry from error index */
2974 packed->nr_entries -= idx;
2975 mq_rq->req = prq;
2976 ret = 1;
2977
2978 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2979 list_del_init(&prq->queuelist);
2980 mmc_blk_clear_packed(mq_rq);
2981 }
2982 return ret;
2983 }
2984 list_del_init(&prq->queuelist);
2985 blk_end_request(prq, 0, blk_rq_bytes(prq));
2986 i++;
2987 }
2988
2989 mmc_blk_clear_packed(mq_rq);
2990 return ret;
2991}
2992
2993static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2994{
2995 struct request *prq;
2996 struct mmc_packed *packed = mq_rq->packed;
2997
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002998 while (!list_empty(&packed->list)) {
2999 prq = list_entry_rq(packed->list.next);
3000 list_del_init(&prq->queuelist);
3001 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3002 }
3003
3004 mmc_blk_clear_packed(mq_rq);
3005}
3006
3007static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3008 struct mmc_queue_req *mq_rq)
3009{
3010 struct request *prq;
3011 struct request_queue *q = mq->queue;
3012 struct mmc_packed *packed = mq_rq->packed;
3013
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003014 while (!list_empty(&packed->list)) {
3015 prq = list_entry_rq(packed->list.prev);
3016 if (prq->queuelist.prev != &packed->list) {
3017 list_del_init(&prq->queuelist);
3018 spin_lock_irq(q->queue_lock);
3019 blk_requeue_request(mq->queue, prq);
3020 spin_unlock_irq(q->queue_lock);
3021 } else {
3022 list_del_init(&prq->queuelist);
3023 }
3024 }
3025
3026 mmc_blk_clear_packed(mq_rq);
3027}
3028
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003029static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3030 struct mmc_cmdq_req *cmdq_req)
3031{
3032 struct mmc_request *mrq = &cmdq_req->mrq;
3033
3034 mrq->done = mmc_blk_cmdq_req_done;
3035 return mmc_cmdq_start_req(host, cmdq_req);
3036}
3037
Asutosh Das5238e022015-04-23 16:00:45 +05303038/* prepare for non-data commands */
3039static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3040 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3041{
3042 struct request *req = mqrq->req;
3043 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3044
3045 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3046
3047 cmdq_req->mrq.data = NULL;
3048 cmdq_req->cmd_flags = req->cmd_flags;
3049 cmdq_req->mrq.req = mqrq->req;
3050 req->special = mqrq;
3051 cmdq_req->cmdq_req_flags |= DCMD;
3052 cmdq_req->mrq.cmdq_req = cmdq_req;
3053
3054 return &mqrq->cmdq_req;
3055}
3056
3057
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003058#define IS_RT_CLASS_REQ(x) \
3059 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3060
3061static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3062 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3063{
3064 struct mmc_card *card = mq->card;
3065 struct request *req = mqrq->req;
3066 struct mmc_blk_data *md = mq->data;
3067 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3068 bool do_data_tag;
3069 bool read_dir = (rq_data_dir(req) == READ);
3070 bool prio = IS_RT_CLASS_REQ(req);
3071 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3072
3073 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3074
3075 cmdq_rq->tag = req->tag;
3076 if (read_dir) {
3077 cmdq_rq->cmdq_req_flags |= DIR;
3078 cmdq_rq->data.flags = MMC_DATA_READ;
3079 } else {
3080 cmdq_rq->data.flags = MMC_DATA_WRITE;
3081 }
3082 if (prio)
3083 cmdq_rq->cmdq_req_flags |= PRIO;
3084
3085 if (do_rel_wr)
3086 cmdq_rq->cmdq_req_flags |= REL_WR;
3087
3088 cmdq_rq->data.blocks = blk_rq_sectors(req);
3089 cmdq_rq->blk_addr = blk_rq_pos(req);
3090 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3091
3092 mmc_set_data_timeout(&cmdq_rq->data, card);
3093
3094 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3095 (req->cmd_flags & REQ_META) &&
3096 (rq_data_dir(req) == WRITE) &&
3097 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3098 card->ext_csd.data_tag_unit_size);
3099 if (do_data_tag)
3100 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3101 cmdq_rq->data.sg = mqrq->sg;
3102 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3103
3104 /*
3105 * Adjust the sg list so it is the same size as the
3106 * request.
3107 */
3108 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3109 cmdq_rq->data.blocks = card->host->max_blk_count;
3110
3111 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3112 int i, data_size = cmdq_rq->data.blocks << 9;
3113 struct scatterlist *sg;
3114
3115 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3116 data_size -= sg->length;
3117 if (data_size <= 0) {
3118 sg->length += data_size;
3119 i++;
3120 break;
3121 }
3122 }
3123 cmdq_rq->data.sg_len = i;
3124 }
3125
3126 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3127 mqrq->cmdq_req.mrq.req = mqrq->req;
3128 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3129 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3130 mqrq->req->special = mqrq;
3131
3132 pr_debug("%s: %s: mrq: 0x%p req: 0x%p mqrq: 0x%p bytes to xf: %d mmc_cmdq_req: 0x%p card-addr: 0x%08x dir(r-1/w-0): %d\n",
3133 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3134 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3135 cmdq_rq, cmdq_rq->blk_addr,
3136 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3137
3138 return &mqrq->cmdq_req;
3139}
3140
3141static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3142{
3143 struct mmc_queue_req *active_mqrq;
3144 struct mmc_card *card = mq->card;
3145 struct mmc_host *host = card->host;
3146 struct mmc_cmdq_req *mc_rq;
3147 int ret = 0;
3148
3149 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
3150 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3151
3152 active_mqrq = &mq->mqrq_cmdq[req->tag];
3153 active_mqrq->req = req;
3154
3155 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3156
3157 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3158 return ret;
3159}
3160
Asutosh Das5238e022015-04-23 16:00:45 +05303161/*
3162 * Issues a flush (dcmd) request
3163 */
3164int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3165{
3166 int err;
3167 struct mmc_queue_req *active_mqrq;
3168 struct mmc_card *card = mq->card;
3169 struct mmc_host *host;
3170 struct mmc_cmdq_req *cmdq_req;
3171 struct mmc_cmdq_context_info *ctx_info;
3172
3173 BUG_ON(!card);
3174 host = card->host;
3175 BUG_ON(!host);
3176 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3177 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3178
3179 ctx_info = &host->cmdq_ctx;
3180
3181 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3182
3183 active_mqrq = &mq->mqrq_cmdq[req->tag];
3184 active_mqrq->req = req;
3185
3186 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3187 cmdq_req->cmdq_req_flags |= QBR;
3188 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3189 cmdq_req->tag = req->tag;
3190
3191 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3192 if (err) {
3193 pr_err("%s: failed (%d) preparing flush req\n",
3194 mmc_hostname(host), err);
3195 return err;
3196 }
3197 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3198 return err;
3199}
3200EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3201
Asutosh Das02e30862015-05-20 16:52:04 +05303202static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3203{
3204 if (!host->cmdq_ops->reset)
3205 return;
3206
3207 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3208 if (mmc_cmdq_halt(host, true)) {
3209 pr_err("%s: halt failed\n", mmc_hostname(host));
3210 goto reset;
3211 }
3212 }
3213
3214 if (clear_all)
3215 mmc_cmdq_discard_queue(host, 0);
3216reset:
3217 mmc_hw_reset(host);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303218 mmc_host_clk_hold(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303219 host->cmdq_ops->reset(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303220 mmc_host_clk_release(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303221 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3222}
3223
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303224static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3225{
3226 int err;
3227 struct mmc_card *card = mq->card;
3228 struct mmc_host *host = card->host;
3229
3230 err = mmc_cmdq_halt(host, true);
3231 if (err) {
3232 pr_err("%s: halt: failed: %d\n", __func__, err);
3233 return;
3234 }
3235
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003236 mmc_get_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303237 /* disable CQ mode in card */
3238 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3239 EXT_CSD_CMDQ, 0,
3240 card->ext_csd.generic_cmd6_time);
3241 if (err) {
3242 pr_err("%s: failed to switch card to legacy mode: %d\n",
3243 __func__, err);
3244 goto out;
3245 } else {
3246 host->card->cmdq_init = false;
3247 }
3248out:
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003249 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303250}
3251
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303252static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3253{
3254 struct mmc_queue *mq = req->q->queuedata;
3255 struct mmc_host *host = mq->card->host;
3256 struct mmc_queue_req *mq_rq = req->special;
3257 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3258 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3259
3260 host->cmdq_ops->dumpstate(host);
3261 if (cmdq_req->cmdq_req_flags & DCMD)
3262 mrq->cmd->error = -ETIMEDOUT;
3263 else
3264 mrq->data->error = -ETIMEDOUT;
3265
3266 host->err_mrq = mrq;
3267 mrq->done(mrq);
3268
3269 return BLK_EH_NOT_HANDLED;
3270}
3271
Asutosh Das02e30862015-05-20 16:52:04 +05303272static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3273{
3274 int err;
3275 int retry = 0;
3276 int gen_err;
3277 u32 status;
3278
3279 struct mmc_host *host = mq->card->host;
3280 struct mmc_request *mrq = host->err_mrq;
3281 struct mmc_card *card = mq->card;
3282 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3283
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003284 pm_runtime_get_sync(&card->dev);
Asutosh Das02e30862015-05-20 16:52:04 +05303285 err = mmc_cmdq_halt(host, true);
3286 if (err) {
3287 pr_err("halt: failed: %d\n", err);
3288 goto reset;
3289 }
3290
3291 /* RED error - Fatal: requires reset */
3292 if (mrq->cmdq_req->resp_err) {
3293 pr_crit("%s: Response error detected: Device in bad state\n",
3294 mmc_hostname(host));
3295 blk_end_request_all(mrq->req, -EIO);
3296 goto reset;
3297 }
3298
3299 if (mrq->data->error) {
3300 blk_end_request_all(mrq->req, mrq->data->error);
3301 for (; retry < MAX_RETRIES; retry++) {
3302 err = get_card_status(card, &status, 0);
3303 if (!err)
3304 break;
3305 }
3306
3307 if (err) {
3308 pr_err("%s: No response from card !!!\n",
3309 mmc_hostname(host));
3310 goto reset;
3311 }
3312
3313 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3314 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3315 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3316 mrq->req, &gen_err, &status);
3317 if (err) {
3318 pr_err("%s: error %d sending stop (%d) command\n",
3319 mrq->req->rq_disk->disk_name,
3320 err, status);
3321 goto reset;
3322 }
3323 }
3324
3325 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3326 goto reset;
3327 else
3328 goto unhalt;
3329 }
3330
3331 /* DCMD commands */
3332 if (mrq->cmd->error)
3333 blk_end_request_all(mrq->req, mrq->cmd->error);
3334
3335reset:
3336 spin_lock_irq(mq->queue->queue_lock);
3337 blk_queue_invalidate_tags(mrq->req->q);
3338 spin_unlock_irq(mq->queue->queue_lock);
3339 mmc_blk_cmdq_reset(host, true);
3340 goto out;
3341
3342unhalt:
3343 mmc_cmdq_halt(host, false);
3344
3345out:
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003346 pm_runtime_mark_last_busy(&card->dev);
3347 pm_runtime_put_autosuspend(&card->dev);
3348
Asutosh Das02e30862015-05-20 16:52:04 +05303349 if (test_and_clear_bit(0, &ctx_info->req_starved))
3350 blk_run_queue(mrq->req->q);
3351}
3352
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003353/* invoked by block layer in softirq context */
3354void mmc_blk_cmdq_complete_rq(struct request *rq)
3355{
3356 struct mmc_queue_req *mq_rq = rq->special;
3357 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3358 struct mmc_host *host = mrq->host;
3359 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3360 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3361 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3362 int err = 0;
3363
3364 if (mrq->cmd && mrq->cmd->error)
3365 err = mrq->cmd->error;
3366 else if (mrq->data && mrq->data->error)
3367 err = mrq->data->error;
3368
Asutosh Das02e30862015-05-20 16:52:04 +05303369 /* clear pending request */
3370 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3371 &ctx_info->active_reqs));
3372
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003373 mmc_cmdq_post_req(host, mrq, err);
3374 if (err) {
3375 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3376 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303377 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3378 pr_err("%s: CQ in error state, ending current req: %d\n",
3379 __func__, err);
3380 blk_end_request_all(rq, err);
3381 } else {
3382 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3383 schedule_work(&mq->cmdq_err_work);
3384 }
3385 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003386 }
3387
Asutosh Das5238e022015-04-23 16:00:45 +05303388 if (cmdq_req->cmdq_req_flags & DCMD) {
3389 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303390 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303391 goto out;
3392 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003393
3394 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3395
Asutosh Das5238e022015-04-23 16:00:45 +05303396out:
Asutosh Das02e30862015-05-20 16:52:04 +05303397 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
3398 test_and_clear_bit(0, &ctx_info->req_starved))
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003399 blk_run_queue(mq->queue);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303400
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003401 mmc_put_card(host->card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303402 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3403 complete(&mq->cmdq_shutdown_complete);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003404 return;
3405}
3406
3407/*
3408 * Complete reqs from block layer softirq context
3409 * Invoked in irq context
3410 */
3411void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3412{
3413 struct request *req = mrq->req;
3414
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303415 mmc_host_clk_release(mrq->host);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003416 blk_complete_request(req);
3417}
3418EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3419
Per Forlinee8a43a2011-07-01 18:55:33 +02003420static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003421{
3422 struct mmc_blk_data *md = mq->data;
3423 struct mmc_card *card = md->queue.card;
3424 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003425 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003426 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003427 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303428 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003429 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003430 const u8 packed_nr = 2;
3431 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003432#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3433 unsigned long waitfor = jiffies;
3434#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003435
3436 if (!rqc && !mq->mqrq_prev->req)
3437 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003438
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003439 if (rqc)
3440 reqs = mmc_blk_prep_packed_list(mq, rqc);
3441
Per Forlin54d49d72011-07-01 18:55:29 +02003442 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003443 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303444 /*
3445 * When 4KB native sector is enabled, only 8 blocks
3446 * multiple read or write is allowed
3447 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003448 if (mmc_large_sector(card) &&
3449 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303450 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3451 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003452 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303453 goto cmd_abort;
3454 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003455
3456 if (reqs >= packed_nr)
3457 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3458 card, mq);
3459 else
3460 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003461 areq = &mq->mqrq_cur->mmc_active;
3462 } else
3463 areq = NULL;
3464 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003465 if (!areq) {
3466 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003467 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003468 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003469 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003470
Per Forlinee8a43a2011-07-01 18:55:33 +02003471 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3472 brq = &mq_rq->brq;
3473 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003474 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003475 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003476
Per Forlind78d4a82011-07-01 18:55:30 +02003477 switch (status) {
3478 case MMC_BLK_SUCCESS:
3479 case MMC_BLK_PARTIAL:
3480 /*
3481 * A block was successfully transferred.
3482 */
Adrian Hunter67716322011-08-29 16:42:15 +03003483 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003484
Mark Salyzyn6904e432016-01-28 11:12:25 -08003485 mmc_blk_simulate_delay(mq, rqc, waitfor);
3486
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003487 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3488 ret = mmc_blk_end_packed_req(mq_rq);
3489 break;
3490 } else {
3491 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003492 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003493 }
3494
Adrian Hunter67716322011-08-29 16:42:15 +03003495 /*
3496 * If the blk_end_request function returns non-zero even
3497 * though all data has been transferred and no errors
3498 * were returned by the host controller, it's a bug.
3499 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003500 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303501 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003502 __func__, blk_rq_bytes(req),
3503 brq->data.bytes_xfered);
3504 rqc = NULL;
3505 goto cmd_abort;
3506 }
Per Forlind78d4a82011-07-01 18:55:30 +02003507 break;
3508 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003509 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003510 if (mmc_blk_reset(md, card->host, type))
3511 goto cmd_abort;
3512 if (!ret)
3513 goto start_new_req;
3514 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003515 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003516 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003517 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003518 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003519 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003520 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003521 if (!mmc_blk_reset(md, card->host, type) &&
3522 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003523 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003524 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003525 case MMC_BLK_DATA_ERR: {
3526 int err;
3527
3528 err = mmc_blk_reset(md, card->host, type);
3529 if (!err)
3530 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303531 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003532 }
3533 case MMC_BLK_ECC_ERR:
3534 if (brq->data.blocks > 1) {
3535 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003536 pr_warn("%s: retrying using single block read\n",
3537 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003538 disable_multi = 1;
3539 break;
3540 }
Per Forlind78d4a82011-07-01 18:55:30 +02003541 /*
3542 * After an error, we redo I/O one sector at a
3543 * time, so we only reach here after trying to
3544 * read a single sector.
3545 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303546 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003547 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003548 if (!ret)
3549 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003550 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303551 case MMC_BLK_NOMEDIUM:
3552 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003553 default:
3554 pr_err("%s: Unhandled return value (%d)",
3555 req->rq_disk->disk_name, status);
3556 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003557 }
3558
Per Forlinee8a43a2011-07-01 18:55:33 +02003559 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003560 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3561 if (!mq_rq->packed->retries)
3562 goto cmd_abort;
3563 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3564 mmc_start_req(card->host,
3565 &mq_rq->mmc_active, NULL);
3566 } else {
3567
3568 /*
3569 * In case of a incomplete request
3570 * prepare it again and resend.
3571 */
3572 mmc_blk_rw_rq_prep(mq_rq, card,
3573 disable_multi, mq);
3574 mmc_start_req(card->host,
3575 &mq_rq->mmc_active, NULL);
3576 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003577 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 } while (ret);
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return 1;
3582
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003583 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003584 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3585 mmc_blk_abort_packed_req(mq_rq);
3586 } else {
3587 if (mmc_card_removed(card))
3588 req->cmd_flags |= REQ_QUIET;
3589 while (ret)
3590 ret = blk_end_request(req, -EIO,
3591 blk_rq_cur_bytes(req));
3592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Per Forlinee8a43a2011-07-01 18:55:33 +02003594 start_new_req:
3595 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003596 if (mmc_card_removed(card)) {
3597 rqc->cmd_flags |= REQ_QUIET;
3598 blk_end_request_all(rqc, -EIO);
3599 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003600 /*
3601 * If current request is packed, it needs to put back.
3602 */
3603 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3604 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3605
Seungwon Jeon7a819022013-01-22 19:48:07 +09003606 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3607 mmc_start_req(card->host,
3608 &mq->mqrq_cur->mmc_active, NULL);
3609 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003610 }
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return 0;
3613}
3614
Asutosh Das8b594832015-04-23 09:55:43 +05303615static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3616 struct mmc_blk_data *md)
3617{
3618 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3619 struct mmc_host *host = card->host;
3620 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3621 u8 part_config = card->ext_csd.part_config;
3622
3623 if ((main_md->part_curr == md->part_type) &&
3624 (card->part_curr == md->part_type))
3625 return 0;
3626
3627 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3628 card->ext_csd.cmdq_support &&
3629 (md->flags & MMC_BLK_CMD_QUEUE)));
3630
3631 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3632 WARN_ON(mmc_cmdq_halt(host, true));
3633
3634 /* disable CQ mode in card */
3635 if (mmc_card_cmdq(card)) {
3636 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3637 EXT_CSD_CMDQ, 0,
3638 card->ext_csd.generic_cmd6_time));
3639 mmc_card_clr_cmdq(card);
3640 }
3641
3642 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3643 part_config |= md->part_type;
3644
3645 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3646 EXT_CSD_PART_CONFIG, part_config,
3647 card->ext_csd.part_time));
3648
3649 card->ext_csd.part_config = part_config;
3650 card->part_curr = md->part_type;
3651
3652 main_md->part_curr = md->part_type;
3653
3654 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3655 WARN_ON(mmc_cmdq_halt(host, false));
3656
3657 return 0;
3658}
3659
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003660static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3661{
3662 int ret;
3663 struct mmc_blk_data *md = mq->data;
3664 struct mmc_card *card = md->queue.card;
3665
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003666 mmc_get_card(card);
Asutosh Das8b594832015-04-23 09:55:43 +05303667 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003668 if (ret) {
3669 pr_err("%s: %s: partition switch failed %d\n",
3670 md->disk->disk_name, __func__, ret);
Asutosh Das5238e022015-04-23 16:00:45 +05303671 if (req)
3672 blk_end_request_all(req, ret);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003673 mmc_put_card(card);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003674 goto switch_failure;
3675 }
3676
Asutosh Das5238e022015-04-23 16:00:45 +05303677 if (req) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05303678 if (req_op(req) == REQ_OP_DISCARD) {
3679 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3680 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3681 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3682 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3683 else
3684 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3685 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303686 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303687 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303688 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303689 }
Asutosh Das5238e022015-04-23 16:00:45 +05303690 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003691
3692switch_failure:
3693 return ret;
3694}
3695
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003696int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003697{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003698 int ret;
3699 struct mmc_blk_data *md = mq->data;
3700 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003701 struct mmc_host *host = card->host;
3702 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303703 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003704 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003705
Dov Levenglicka0296392015-06-24 19:51:58 +03003706 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003707 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003708 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003709
Dov Levenglicka0296392015-06-24 19:51:58 +03003710 if (mmc_card_doing_bkops(host->card)) {
3711 ret = mmc_stop_bkops(host->card);
3712 if (ret)
3713 goto out;
3714 }
3715 }
3716
Andrei Warkentin371a6892011-04-11 18:10:25 -05003717 ret = mmc_blk_part_switch(card, md);
3718 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003719 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303720 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003721 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003722 ret = 0;
3723 goto out;
3724 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003725
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003726 mmc_blk_write_packing_control(mq, req);
3727
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003728 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003729 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003730 /* complete ongoing async transfer before issuing discard */
3731 if (card->host->areq)
3732 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003733 ret = mmc_blk_issue_discard_rq(mq, req);
3734 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3735 /* complete ongoing async transfer before issuing secure erase*/
3736 if (card->host->areq)
3737 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003738 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3739 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3740 else
3741 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303742 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3743 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003744 /* complete ongoing async transfer before issuing flush */
3745 if (card->host->areq)
3746 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003747 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003748 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003749 if (!req && host->areq) {
3750 spin_lock_irqsave(&host->context_info.lock, flags);
3751 host->context_info.is_waiting_last_req = true;
3752 spin_unlock_irqrestore(&host->context_info.lock, flags);
3753 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003754 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003755 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003756
Andrei Warkentin371a6892011-04-11 18:10:25 -05003757out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003758 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3759 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003760 /*
3761 * Release host when there are no more requests
3762 * and after special request(discard, flush) is done.
3763 * In case sepecial request, there is no reentry to
3764 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3765 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003766 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003767 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Russell Kinga6f6c962006-01-03 22:38:44 +00003770static inline int mmc_blk_readonly(struct mmc_card *card)
3771{
3772 return mmc_card_readonly(card) ||
3773 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3774}
3775
Andrei Warkentin371a6892011-04-11 18:10:25 -05003776static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3777 struct device *parent,
3778 sector_t size,
3779 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003780 const char *subname,
3781 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 struct mmc_blk_data *md;
3784 int devidx, ret;
3785
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003786again:
3787 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3788 return ERR_PTR(-ENOMEM);
3789
3790 spin_lock(&mmc_blk_lock);
3791 ret = ida_get_new(&mmc_blk_ida, &devidx);
3792 spin_unlock(&mmc_blk_lock);
3793
3794 if (ret == -EAGAIN)
3795 goto again;
3796 else if (ret)
3797 return ERR_PTR(ret);
3798
3799 if (devidx >= max_devices) {
3800 ret = -ENOSPC;
3801 goto out;
3802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003804 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003805 if (!md) {
3806 ret = -ENOMEM;
3807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003809
Johan Rudholmadd710e2011-12-02 08:51:06 +01003810 md->area_type = area_type;
3811
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003812 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003813 * Set the read-only status based on the supported commands
3814 * and the write protect switch.
3815 */
3816 md->read_only = mmc_blk_readonly(card);
3817
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003818 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003819 if (md->disk == NULL) {
3820 ret = -ENOMEM;
3821 goto err_kfree;
3822 }
3823
3824 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003825 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003826 md->usage = 1;
3827
Asutosh Das963469b2015-05-21 13:29:51 +05303828 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003829 if (ret)
3830 goto err_putdisk;
3831
Russell Kinga6f6c962006-01-03 22:38:44 +00003832 md->queue.data = md;
3833
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003834 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003835 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003836 md->disk->fops = &mmc_bdops;
3837 md->disk->private_data = md;
3838 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003839 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003840 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003841 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003842 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003843 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003844
3845 /*
3846 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3847 *
3848 * - be set for removable media with permanent block devices
3849 * - be unset for removable block devices with permanent media
3850 *
3851 * Since MMC block devices clearly fall under the second
3852 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3853 * should use the block device creation/destruction hotplug
3854 * messages to tell when the card is present.
3855 */
3856
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003857 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003858 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003859
Saugata Dasa5075eb2012-05-17 16:32:21 +05303860 if (mmc_card_mmc(card))
3861 blk_queue_logical_block_size(md->queue.queue,
3862 card->ext_csd.data_sector_size);
3863 else
3864 blk_queue_logical_block_size(md->queue.queue, 512);
3865
Andrei Warkentin371a6892011-04-11 18:10:25 -05003866 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003867
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003868 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003869 if ((mmc_card_mmc(card) &&
3870 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003871 (mmc_card_sd(card) &&
3872 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3873 md->flags |= MMC_BLK_CMD23;
3874 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003875
3876 if (mmc_card_mmc(card) &&
3877 md->flags & MMC_BLK_CMD23 &&
3878 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303879 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003880 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003881 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003882 }
3883
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003884 if (card->cmdq_init) {
3885 md->flags |= MMC_BLK_CMD_QUEUE;
3886 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
3887 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05303888 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303889 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303890 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003891 }
3892
3893 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003894 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3895 (md->flags & MMC_BLK_CMD23) &&
3896 card->ext_csd.packed_event_en) {
3897 if (!mmc_packed_init(&md->queue, card))
3898 md->flags |= MMC_BLK_PACKED_CMD;
3899 }
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003902
3903 err_putdisk:
3904 put_disk(md->disk);
3905 err_kfree:
3906 kfree(md);
3907 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003908 spin_lock(&mmc_blk_lock);
3909 ida_remove(&mmc_blk_ida, devidx);
3910 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003911 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
Andrei Warkentin371a6892011-04-11 18:10:25 -05003914static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3915{
3916 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003917
3918 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3919 /*
3920 * The EXT_CSD sector count is in number or 512 byte
3921 * sectors.
3922 */
3923 size = card->ext_csd.sectors;
3924 } else {
3925 /*
3926 * The CSD capacity field is in units of read_blkbits.
3927 * set_capacity takes units of 512 bytes.
3928 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003929 size = (typeof(sector_t))card->csd.capacity
3930 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003931 }
3932
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003933 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003934 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003935}
3936
3937static int mmc_blk_alloc_part(struct mmc_card *card,
3938 struct mmc_blk_data *md,
3939 unsigned int part_type,
3940 sector_t size,
3941 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003942 const char *subname,
3943 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003944{
3945 char cap_str[10];
3946 struct mmc_blk_data *part_md;
3947
3948 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003949 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003950 if (IS_ERR(part_md))
3951 return PTR_ERR(part_md);
3952 part_md->part_type = part_type;
3953 list_add(&part_md->part, &md->part);
3954
James Bottomleyb9f28d82015-03-05 18:47:01 -08003955 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003956 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303957 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003958 part_md->disk->disk_name, mmc_card_id(card),
3959 mmc_card_name(card), part_md->part_type, cap_str);
3960 return 0;
3961}
3962
Namjae Jeone0c368d2011-10-06 23:41:38 +09003963/* MMC Physical partitions consist of two boot partitions and
3964 * up to four general purpose partitions.
3965 * For each partition enabled in EXT_CSD a block device will be allocatedi
3966 * to provide access to the partition.
3967 */
3968
Andrei Warkentin371a6892011-04-11 18:10:25 -05003969static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3970{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003971 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003972
3973 if (!mmc_card_mmc(card))
3974 return 0;
3975
Namjae Jeone0c368d2011-10-06 23:41:38 +09003976 for (idx = 0; idx < card->nr_parts; idx++) {
3977 if (card->part[idx].size) {
3978 ret = mmc_blk_alloc_part(card, md,
3979 card->part[idx].part_cfg,
3980 card->part[idx].size >> 9,
3981 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003982 card->part[idx].name,
3983 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003984 if (ret)
3985 return ret;
3986 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003987 }
3988
3989 return ret;
3990}
3991
Andrei Warkentin371a6892011-04-11 18:10:25 -05003992static void mmc_blk_remove_req(struct mmc_blk_data *md)
3993{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003994 struct mmc_card *card;
3995
Andrei Warkentin371a6892011-04-11 18:10:25 -05003996 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003997 /*
3998 * Flush remaining requests and free queues. It
3999 * is freeing the queue that stops new requests
4000 * from being accepted.
4001 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004002 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004003 mmc_cleanup_queue(&md->queue);
4004 if (md->flags & MMC_BLK_PACKED_CMD)
4005 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004006 if (md->flags & MMC_BLK_CMD_QUEUE)
4007 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004008 device_remove_file(disk_to_dev(md->disk),
4009 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004010 if (md->disk->flags & GENHD_FL_UP) {
4011 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004012 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4013 card->ext_csd.boot_ro_lockable)
4014 device_remove_file(disk_to_dev(md->disk),
4015 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004016#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4017 device_remove_file(disk_to_dev(md->disk),
4018 &dev_attr_max_write_speed);
4019 device_remove_file(disk_to_dev(md->disk),
4020 &dev_attr_max_read_speed);
4021 device_remove_file(disk_to_dev(md->disk),
4022 &dev_attr_cache_size);
4023#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004024
Andrei Warkentin371a6892011-04-11 18:10:25 -05004025 del_gendisk(md->disk);
4026 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004027 mmc_blk_put(md);
4028 }
4029}
4030
4031static void mmc_blk_remove_parts(struct mmc_card *card,
4032 struct mmc_blk_data *md)
4033{
4034 struct list_head *pos, *q;
4035 struct mmc_blk_data *part_md;
4036
4037 list_for_each_safe(pos, q, &md->part) {
4038 part_md = list_entry(pos, struct mmc_blk_data, part);
4039 list_del(pos);
4040 mmc_blk_remove_req(part_md);
4041 }
4042}
4043
4044static int mmc_add_disk(struct mmc_blk_data *md)
4045{
4046 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004047 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004048
Dan Williams307d8e62016-06-20 10:40:44 -07004049 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004050 md->force_ro.show = force_ro_show;
4051 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304052 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004053 md->force_ro.attr.name = "force_ro";
4054 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4055 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4056 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004057 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004058#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4059 atomic_set(&md->queue.max_write_speed, max_write_speed);
4060 ret = device_create_file(disk_to_dev(md->disk),
4061 &dev_attr_max_write_speed);
4062 if (ret)
4063 goto max_write_speed_fail;
4064 atomic_set(&md->queue.max_read_speed, max_read_speed);
4065 ret = device_create_file(disk_to_dev(md->disk),
4066 &dev_attr_max_read_speed);
4067 if (ret)
4068 goto max_read_speed_fail;
4069 atomic_set(&md->queue.cache_size, cache_size);
4070 atomic_long_set(&md->queue.cache_used, 0);
4071 md->queue.cache_jiffies = jiffies;
4072 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4073 if (ret)
4074 goto cache_size_fail;
4075#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004076
4077 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4078 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004079 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004080
4081 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4082 mode = S_IRUGO;
4083 else
4084 mode = S_IRUGO | S_IWUSR;
4085
4086 md->power_ro_lock.show = power_ro_lock_show;
4087 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004088 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004089 md->power_ro_lock.attr.mode = mode;
4090 md->power_ro_lock.attr.name =
4091 "ro_lock_until_next_power_on";
4092 ret = device_create_file(disk_to_dev(md->disk),
4093 &md->power_ro_lock);
4094 if (ret)
4095 goto power_ro_lock_fail;
4096 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004097
4098 md->num_wr_reqs_to_start_packing.show =
4099 num_wr_reqs_to_start_packing_show;
4100 md->num_wr_reqs_to_start_packing.store =
4101 num_wr_reqs_to_start_packing_store;
4102 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4103 md->num_wr_reqs_to_start_packing.attr.name =
4104 "num_wr_reqs_to_start_packing";
4105 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4106 ret = device_create_file(disk_to_dev(md->disk),
4107 &md->num_wr_reqs_to_start_packing);
4108 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004109 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004110
Maya Erez5a8dae12014-12-04 15:13:59 +02004111 md->no_pack_for_random.show = no_pack_for_random_show;
4112 md->no_pack_for_random.store = no_pack_for_random_store;
4113 sysfs_attr_init(&md->no_pack_for_random.attr);
4114 md->no_pack_for_random.attr.name = "no_pack_for_random";
4115 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4116 ret = device_create_file(disk_to_dev(md->disk),
4117 &md->no_pack_for_random);
4118 if (ret)
4119 goto no_pack_for_random_fails;
4120
Johan Rudholmadd710e2011-12-02 08:51:06 +01004121 return ret;
4122
Maya Erez5a8dae12014-12-04 15:13:59 +02004123no_pack_for_random_fails:
4124 device_remove_file(disk_to_dev(md->disk),
4125 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004126num_wr_reqs_to_start_packing_fail:
4127 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004128power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004129#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4130 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4131cache_size_fail:
4132 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4133max_read_speed_fail:
4134 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4135max_write_speed_fail:
4136#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004137 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4138force_ro_fail:
4139 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004140
4141 return ret;
4142}
4143
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004144static const struct mmc_fixup blk_fixups[] =
4145{
Chris Ballc59d4472011-11-11 22:01:43 -05004146 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4147 MMC_QUIRK_INAND_CMD38),
4148 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4149 MMC_QUIRK_INAND_CMD38),
4150 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4151 MMC_QUIRK_INAND_CMD38),
4152 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4153 MMC_QUIRK_INAND_CMD38),
4154 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4155 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004156
4157 /*
4158 * Some MMC cards experience performance degradation with CMD23
4159 * instead of CMD12-bounded multiblock transfers. For now we'll
4160 * black list what's bad...
4161 * - Certain Toshiba cards.
4162 *
4163 * N.B. This doesn't affect SD cards.
4164 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004165 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4166 MMC_QUIRK_BLK_NO_CMD23),
4167 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4168 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004169 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004170 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004171 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004172 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004173 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004174 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004175
4176 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004177 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004178 */
Chris Ballc59d4472011-11-11 22:01:43 -05004179 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004180 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004181 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4182 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004183
Ian Chen3550ccd2012-08-29 15:05:36 +09004184 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004185 * Some Samsung MMC cards need longer data read timeout than
4186 * indicated in CSD.
4187 */
4188 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4189 MMC_QUIRK_LONG_READ_TIME),
4190
4191 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004192 * On these Samsung MoviNAND parts, performing secure erase or
4193 * secure trim can result in unrecoverable corruption due to a
4194 * firmware bug.
4195 */
4196 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4197 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4198 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4199 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4200 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4201 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4202 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4203 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4204 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4205 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4206 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4207 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4208 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4209 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4210 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4211 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4212
Shawn Linb5b4ff02015-08-12 13:08:32 +08004213 /*
4214 * On Some Kingston eMMCs, performing trim can result in
4215 * unrecoverable data conrruption occasionally due to a firmware bug.
4216 */
4217 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4218 MMC_QUIRK_TRIM_BROKEN),
4219 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4220 MMC_QUIRK_TRIM_BROKEN),
4221
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004222 /* Some INAND MCP devices advertise incorrect timeout values */
4223 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4224 MMC_QUIRK_INAND_DATA_TIMEOUT),
4225
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004226 END_FIXUP
4227};
4228
Ulf Hansson96541ba2015-04-14 13:06:12 +02004229static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004231 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004232 char cap_str[10];
4233
Pierre Ossman912490d2005-05-21 10:27:02 +01004234 /*
4235 * Check that the card supports the command class(es) we need.
4236 */
4237 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return -ENODEV;
4239
Lukas Czerner5204d002014-06-18 13:18:07 +02004240 mmc_fixup_device(card, blk_fixups);
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 md = mmc_blk_alloc(card);
4243 if (IS_ERR(md))
4244 return PTR_ERR(md);
4245
James Bottomleyb9f28d82015-03-05 18:47:01 -08004246 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004247 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304248 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004250 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Andrei Warkentin371a6892011-04-11 18:10:25 -05004252 if (mmc_blk_alloc_parts(card, md))
4253 goto out;
4254
Ulf Hansson96541ba2015-04-14 13:06:12 +02004255 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004256
Andrei Warkentin371a6892011-04-11 18:10:25 -05004257 if (mmc_add_disk(md))
4258 goto out;
4259
4260 list_for_each_entry(part_md, &md->part, part) {
4261 if (mmc_add_disk(part_md))
4262 goto out;
4263 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004264
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004265 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004266 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4267 /*
4268 * If there is a runtime_idle function, it should take care of
4269 * suspending the card
4270 */
4271 if (card->host->bus_ops->runtime_idle)
4272 pm_runtime_dont_use_autosuspend(&card->dev);
4273 else
4274 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004275
4276 /*
4277 * Don't enable runtime PM for SD-combo cards here. Leave that
4278 * decision to be taken during the SDIO init sequence instead.
4279 */
4280 if (card->type != MMC_TYPE_SD_COMBO) {
4281 pm_runtime_set_active(&card->dev);
4282 pm_runtime_enable(&card->dev);
4283 }
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return 0;
4286
4287 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004288 mmc_blk_remove_parts(card, md);
4289 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
Ulf Hansson96541ba2015-04-14 13:06:12 +02004293static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004295 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Andrei Warkentin371a6892011-04-11 18:10:25 -05004297 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004298 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004299 mmc_claim_host(card->host);
4300 mmc_blk_part_switch(card, md);
4301 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004302 if (card->type != MMC_TYPE_SD_COMBO)
4303 pm_runtime_disable(&card->dev);
4304 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004305 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004306 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Ulf Hansson96541ba2015-04-14 13:06:12 +02004309static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004311 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004312 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304313 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
4315 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304316 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304317 if (rc)
4318 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004319 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304320 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304321 if (rc)
4322 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304325 goto out;
4326
4327 out_resume:
4328 mmc_queue_resume(&md->queue);
4329 list_for_each_entry(part_md, &md->part, part) {
4330 mmc_queue_resume(&part_md->queue);
4331 }
4332 out:
4333 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
Ulf Hansson96541ba2015-04-14 13:06:12 +02004336static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004337{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004338 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004339}
4340
Ulf Hansson0967edc2014-10-06 11:29:42 +02004341#ifdef CONFIG_PM_SLEEP
4342static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004343{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004344 struct mmc_card *card = mmc_dev_to_card(dev);
4345
4346 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004347}
4348
Ulf Hansson0967edc2014-10-06 11:29:42 +02004349static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004351 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004352 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004355 /*
4356 * Resume involves the card going into idle state,
4357 * so current partition is always the main one.
4358 */
4359 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004361 list_for_each_entry(part_md, &md->part, part) {
4362 mmc_queue_resume(&part_md->queue);
4363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 }
4365 return 0;
4366}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367#endif
4368
Ulf Hansson0967edc2014-10-06 11:29:42 +02004369static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4370
Ulf Hansson96541ba2015-04-14 13:06:12 +02004371static struct mmc_driver mmc_driver = {
4372 .drv = {
4373 .name = "mmcblk",
4374 .pm = &mmc_blk_pm_ops,
4375 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 .probe = mmc_blk_probe,
4377 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004378 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379};
4380
4381static int __init mmc_blk_init(void)
4382{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004383 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004385 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4386 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4387
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004388 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004389
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004390 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4391 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004394 res = mmc_register_driver(&mmc_driver);
4395 if (res)
4396 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004398 return 0;
4399 out2:
4400 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 out:
4402 return res;
4403}
4404
4405static void __exit mmc_blk_exit(void)
4406{
4407 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004408 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409}
4410
4411module_init(mmc_blk_init);
4412module_exit(mmc_blk_exit);
4413
4414MODULE_LICENSE("GPL");
4415MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4416