blob: e201ccb3fda4d74dab326e0f2eb983aa7bacc434 [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>
Linus Walleij97548572017-09-20 10:02:00 +020031#include <linux/cdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000032#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070033#include <linux/scatterlist.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>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020039#include <linux/idr.h>
Linus Walleij627c3cc2017-08-20 23:39:08 +020040#include <linux/debugfs.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>
Pierre Ossman385e32272006-06-18 14:34:37 +020044#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010045#include <linux/mmc/mmc.h>
46#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Pierre Ossman98ac2162006-12-23 20:03:02 +010050#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080051#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010052#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010054#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010055#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010056#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080057#include "quirks.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010058#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000060MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040061#ifdef MODULE_PARAM_PREFIX
62#undef MODULE_PARAM_PREFIX
63#endif
64#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010065
Adrian Hunter6b7a3632017-11-29 15:41:14 +020066/*
67 * Set a 10 second timeout for polling write request busy state. Note, mmc core
68 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
69 * second software timer to timeout the whole request, so 10 seconds should be
70 * ample.
71 */
72#define MMC_BLK_TIMEOUT_MS (10 * 1000)
Maya Erez775a9362013-04-18 15:41:55 +030073#define MMC_SANITIZE_REQ_TIMEOUT 240000
74#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Bastian Stendera0e95762018-03-08 15:08:11 +010075#define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050076
Luca Porziod3df0462015-11-06 15:12:26 +000077#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090078 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020079static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040080
81/*
82 * The defaults come from config options but can be overriden by module
83 * or bootarg options.
84 */
85static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
86
87/*
88 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000089 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020090 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040091 */
92static int max_devices;
93
Ben Hutchingsa26eba62014-11-06 03:35:09 +000094#define MAX_DEVICES 256
95
Ulf Hanssonb10fa992016-04-07 14:36:46 +020096static DEFINE_IDA(mmc_blk_ida);
Linus Walleij97548572017-09-20 10:02:00 +020097static DEFINE_IDA(mmc_rpmb_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * There is one mmc_blk_data per slot.
101 */
102struct mmc_blk_data {
103 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700104 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct gendisk *disk;
106 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 struct list_head part;
Linus Walleij97548572017-09-20 10:02:00 +0200108 struct list_head rpmbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500110 unsigned int flags;
111#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000115 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300117 unsigned int reset_done;
118#define MMC_BLK_READ BIT(0)
119#define MMC_BLK_WRITE BIT(1)
120#define MMC_BLK_DISCARD BIT(2)
121#define MMC_BLK_SECDISCARD BIT(3)
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200122#define MMC_BLK_CQE_RECOVERY BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500123
124 /*
125 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200126 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500127 * track of the current selected device partition.
128 */
129 unsigned int part_curr;
130 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100131 struct device_attribute power_ro_lock;
132 int area_type;
Adrian Hunterf9f0da92017-11-21 15:42:30 +0200133
134 /* debugfs files (only in main mmc_blk_data) */
135 struct dentry *status_dentry;
136 struct dentry *ext_csd_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Linus Walleij97548572017-09-20 10:02:00 +0200139/* Device type for RPMB character devices */
140static dev_t mmc_rpmb_devt;
141
142/* Bus type for RPMB character devices */
143static struct bus_type mmc_rpmb_bus_type = {
144 .name = "mmc_rpmb",
145};
146
147/**
148 * struct mmc_rpmb_data - special RPMB device type for these areas
149 * @dev: the device for the RPMB area
150 * @chrdev: character device for the RPMB area
151 * @id: unique device ID number
152 * @part_index: partition index (0 on first)
153 * @md: parent MMC block device
154 * @node: list item, so we can put this device on a list
155 */
156struct mmc_rpmb_data {
157 struct device dev;
158 struct cdev chrdev;
159 int id;
160 unsigned int part_index;
161 struct mmc_blk_data *md;
162 struct list_head node;
163};
164
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400167module_param(perdev_minors, int, 0444);
168MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
169
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200170static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200171 unsigned int part_type);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
174{
175 struct mmc_blk_data *md;
176
Arjan van de Vena621aae2006-01-12 18:43:35 +0000177 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 md = disk->private_data;
179 if (md && md->usage == 0)
180 md = NULL;
181 if (md)
182 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000183 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 return md;
186}
187
Andrei Warkentin371a6892011-04-11 18:10:25 -0500188static inline int mmc_get_devidx(struct gendisk *disk)
189{
Colin Cross382c55f2015-10-22 10:00:41 -0700190 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500191 return devidx;
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static void mmc_blk_put(struct mmc_blk_data *md)
195{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000196 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 md->usage--;
198 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500199 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter41e3efd2017-11-29 15:40:59 +0200200 blk_put_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100201 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 kfree(md);
204 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000205 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Johan Rudholmadd710e2011-12-02 08:51:06 +0100208static ssize_t power_ro_lock_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 int ret;
212 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
213 struct mmc_card *card = md->queue.card;
214 int locked = 0;
215
216 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
217 locked = 2;
218 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
219 locked = 1;
220
221 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
222
Tomas Winkler9098f842015-07-16 15:50:45 +0200223 mmc_blk_put(md);
224
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 return ret;
226}
227
228static ssize_t power_ro_lock_store(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t count)
230{
231 int ret;
232 struct mmc_blk_data *md, *part_md;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200233 struct mmc_queue *mq;
234 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100235 unsigned long set;
236
237 if (kstrtoul(buf, 0, &set))
238 return -EINVAL;
239
240 if (set != 1)
241 return count;
242
243 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200244 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100245
Linus Walleij0493f6f2017-05-19 15:37:30 +0200246 /* Dispatch locking to the block layer */
Christoph Hellwigff005a02018-05-09 09:54:05 +0200247 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200248 if (IS_ERR(req)) {
249 count = PTR_ERR(req);
250 goto out_put;
251 }
Linus Walleij0493f6f2017-05-19 15:37:30 +0200252 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
253 blk_execute_rq(mq->queue, NULL, req, 0);
254 ret = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +0200255 blk_put_request(req);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100256
257 if (!ret) {
258 pr_info("%s: Locking boot partition ro until next power on\n",
259 md->disk->disk_name);
260 set_disk_ro(md->disk, 1);
261
262 list_for_each_entry(part_md, &md->part, part)
263 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
264 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
265 set_disk_ro(part_md->disk, 1);
266 }
267 }
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200268out_put:
Johan Rudholmadd710e2011-12-02 08:51:06 +0100269 mmc_blk_put(md);
270 return count;
271}
272
Andrei Warkentin371a6892011-04-11 18:10:25 -0500273static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
274 char *buf)
275{
276 int ret;
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
278
Baruch Siach0031a982014-09-22 10:12:51 +0300279 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500280 get_disk_ro(dev_to_disk(dev)) ^
281 md->read_only);
282 mmc_blk_put(md);
283 return ret;
284}
285
286static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
288{
289 int ret;
290 char *end;
291 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
292 unsigned long set = simple_strtoul(buf, &end, 0);
293 if (end == buf) {
294 ret = -EINVAL;
295 goto out;
296 }
297
298 set_disk_ro(dev_to_disk(dev), set || md->read_only);
299 ret = count;
300out:
301 mmc_blk_put(md);
302 return ret;
303}
304
Al Viroa5a15612008-03-02 10:33:30 -0500305static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Al Viroa5a15612008-03-02 10:33:30 -0500307 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int ret = -ENXIO;
309
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200310 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (md) {
312 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500313 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700315
Al Viroa5a15612008-03-02 10:33:30 -0500316 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700317 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700318 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200321 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 return ret;
324}
325
Al Virodb2a1442013-05-05 21:52:57 -0400326static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Al Viroa5a15612008-03-02 10:33:30 -0500328 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200330 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200332 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800336mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800338 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
339 geo->heads = 4;
340 geo->sectors = 16;
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
John Calixtocb87ea22011-04-26 18:56:29 -0400344struct mmc_blk_ioc_data {
345 struct mmc_ioc_cmd ic;
346 unsigned char *buf;
347 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200348 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400349};
350
351static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
352 struct mmc_ioc_cmd __user *user)
353{
354 struct mmc_blk_ioc_data *idata;
355 int err;
356
yalin wang1ff89502015-11-12 19:27:11 +0800357 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400358 if (!idata) {
359 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400360 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400361 }
362
363 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
364 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400365 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400366 }
367
368 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
369 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
370 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400371 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400372 }
373
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300374 if (!idata->buf_bytes) {
375 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100376 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300377 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100378
Markus Elfring97a0c312018-03-05 11:33:21 +0100379 idata->buf = memdup_user((void __user *)(unsigned long)
380 idata->ic.data_ptr, idata->buf_bytes);
381 if (IS_ERR(idata->buf)) {
382 err = PTR_ERR(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400383 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400384 }
385
John Calixtocb87ea22011-04-26 18:56:29 -0400386 return idata;
387
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400388idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400389 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400390out:
John Calixtocb87ea22011-04-26 18:56:29 -0400391 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400392}
393
Jon Huntera5f57742015-09-22 10:27:53 +0100394static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
395 struct mmc_blk_ioc_data *idata)
396{
397 struct mmc_ioc_cmd *ic = &idata->ic;
398
399 if (copy_to_user(&(ic_ptr->response), ic->response,
400 sizeof(ic->response)))
401 return -EFAULT;
402
403 if (!idata->ic.write_flag) {
404 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
405 idata->buf, idata->buf_bytes))
406 return -EFAULT;
407 }
408
409 return 0;
410}
411
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200412static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
413 u32 retries_max)
414{
415 int err;
416 u32 retry_count = 0;
417
418 if (!status || !retries_max)
419 return -EINVAL;
420
421 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200422 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200423 if (err)
424 break;
425
426 if (!R1_STATUS(*status) &&
427 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
428 break; /* RPMB programming operation complete */
429
430 /*
431 * Rechedule to give the MMC device a chance to continue
432 * processing the previous command without being polled too
433 * frequently.
434 */
435 usleep_range(1000, 5000);
436 } while (++retry_count < retries_max);
437
438 if (retry_count == retries_max)
439 err = -EPERM;
440
441 return err;
442}
443
Maya Erez775a9362013-04-18 15:41:55 +0300444static int ioctl_do_sanitize(struct mmc_card *card)
445{
446 int err;
447
Ulf Hanssona2d10862013-12-16 14:37:26 +0100448 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300449 pr_warn("%s: %s - SANITIZE is not supported\n",
450 mmc_hostname(card->host), __func__);
451 err = -EOPNOTSUPP;
452 goto out;
453 }
454
455 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
456 mmc_hostname(card->host), __func__);
457
458 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
459 EXT_CSD_SANITIZE_START, 1,
460 MMC_SANITIZE_REQ_TIMEOUT);
461
462 if (err)
463 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
464 mmc_hostname(card->host), __func__, err);
465
466 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
467 __func__);
468out:
469 return err;
470}
471
Jon Huntera5f57742015-09-22 10:27:53 +0100472static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
473 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400474{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900475 struct mmc_command cmd = {};
476 struct mmc_data data = {};
477 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400478 struct scatterlist sg;
479 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200480 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200481 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400482
Jon Huntera5f57742015-09-22 10:27:53 +0100483 if (!card || !md || !idata)
484 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400485
Linus Walleij97548572017-09-20 10:02:00 +0200486 /*
487 * The RPMB accesses comes in from the character device, so we
488 * need to target these explicitly. Else we just target the
489 * partition type for the block device the ioctl() was issued
490 * on.
491 */
492 if (idata->rpmb) {
493 /* Support multiple RPMB partitions */
494 target_part = idata->rpmb->part_index;
495 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
496 } else {
497 target_part = md->part_type;
498 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200499
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100500 cmd.opcode = idata->ic.opcode;
501 cmd.arg = idata->ic.arg;
502 cmd.flags = idata->ic.flags;
503
504 if (idata->buf_bytes) {
505 data.sg = &sg;
506 data.sg_len = 1;
507 data.blksz = idata->ic.blksz;
508 data.blocks = idata->ic.blocks;
509
510 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
511
512 if (idata->ic.write_flag)
513 data.flags = MMC_DATA_WRITE;
514 else
515 data.flags = MMC_DATA_READ;
516
517 /* data.flags must already be set before doing this. */
518 mmc_set_data_timeout(&data, card);
519
520 /* Allow overriding the timeout_ns for empirical tuning. */
521 if (idata->ic.data_timeout_ns)
522 data.timeout_ns = idata->ic.data_timeout_ns;
523
524 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
525 /*
526 * Pretend this is a data transfer and rely on the
527 * host driver to compute timeout. When all host
528 * drivers support cmd.cmd_timeout for R1B, this
529 * can be changed to:
530 *
531 * mrq.data = NULL;
532 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
533 */
534 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
535 }
536
537 mrq.data = &data;
538 }
539
540 mrq.cmd = &cmd;
541
Linus Walleij97548572017-09-20 10:02:00 +0200542 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200543 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100544 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200545
John Calixtocb87ea22011-04-26 18:56:29 -0400546 if (idata->ic.is_acmd) {
547 err = mmc_app_cmd(card->host, card);
548 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100549 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400550 }
551
Linus Walleij97548572017-09-20 10:02:00 +0200552 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200553 err = mmc_set_blockcount(card, data.blocks,
554 idata->ic.write_flag & (1 << 31));
555 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100556 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200557 }
558
Yaniv Gardia82e4842013-06-05 14:13:08 +0300559 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
560 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300561 err = ioctl_do_sanitize(card);
562
563 if (err)
564 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
565 __func__, err);
566
Jon Huntera5f57742015-09-22 10:27:53 +0100567 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300568 }
569
John Calixtocb87ea22011-04-26 18:56:29 -0400570 mmc_wait_for_req(card->host, &mrq);
571
572 if (cmd.error) {
573 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
574 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100575 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400576 }
577 if (data.error) {
578 dev_err(mmc_dev(card->host), "%s: data error %d\n",
579 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100580 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400581 }
582
583 /*
Bastian Stendera0e95762018-03-08 15:08:11 +0100584 * Make sure the cache of the PARTITION_CONFIG register and
585 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
586 * changed it successfully.
587 */
588 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
589 (cmd.opcode == MMC_SWITCH)) {
590 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
591 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
592
593 /*
594 * Update cache so the next mmc_blk_part_switch call operates
595 * on up-to-date data.
596 */
597 card->ext_csd.part_config = value;
598 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
599 }
600
601 /*
John Calixtocb87ea22011-04-26 18:56:29 -0400602 * According to the SD specs, some commands require a delay after
603 * issuing the command.
604 */
605 if (idata->ic.postsleep_min_us)
606 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
607
Jon Huntera5f57742015-09-22 10:27:53 +0100608 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400609
Linus Walleij97548572017-09-20 10:02:00 +0200610 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200611 /*
612 * Ensure RPMB command has completed by polling CMD13
613 * "Send Status".
614 */
615 err = ioctl_rpmb_card_status_poll(card, &status, 5);
616 if (err)
617 dev_err(mmc_dev(card->host),
618 "%s: Card Status=0x%08X, error %d\n",
619 __func__, status, err);
620 }
621
Jon Huntera5f57742015-09-22 10:27:53 +0100622 return err;
623}
624
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200625static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200626 struct mmc_ioc_cmd __user *ic_ptr,
627 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100628{
629 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200630 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200631 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100632 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700633 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200634 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100635
636 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
637 if (IS_ERR(idata))
638 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200639 /* This will be NULL on non-RPMB ioctl():s */
640 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100641
Jon Huntera5f57742015-09-22 10:27:53 +0100642 card = md->queue.card;
643 if (IS_ERR(card)) {
644 err = PTR_ERR(card);
645 goto cmd_done;
646 }
647
Linus Walleij614f0382017-05-18 11:29:34 +0200648 /*
649 * Dispatch the ioctl() into the block request queue.
650 */
651 mq = &md->queue;
652 req = blk_get_request(mq->queue,
Christoph Hellwigff005a02018-05-09 09:54:05 +0200653 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200654 if (IS_ERR(req)) {
655 err = PTR_ERR(req);
656 goto cmd_done;
657 }
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200658 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200659 req_to_mmc_queue_req(req)->drv_op =
660 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200661 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200662 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200663 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200664 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700665 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200666 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100667
John Calixtocb87ea22011-04-26 18:56:29 -0400668cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400669 kfree(idata->buf);
670 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700671 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400672}
673
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200674static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200675 struct mmc_ioc_multi_cmd __user *user,
676 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100677{
678 struct mmc_blk_ioc_data **idata = NULL;
679 struct mmc_ioc_cmd __user *cmds = user->cmds;
680 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200681 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700682 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100683 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200684 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100685
686 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
687 sizeof(num_of_cmds)))
688 return -EFAULT;
689
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200690 if (!num_of_cmds)
691 return 0;
692
Jon Huntera5f57742015-09-22 10:27:53 +0100693 if (num_of_cmds > MMC_IOC_MAX_CMDS)
694 return -EINVAL;
695
696 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
697 if (!idata)
698 return -ENOMEM;
699
700 for (i = 0; i < num_of_cmds; i++) {
701 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
702 if (IS_ERR(idata[i])) {
703 err = PTR_ERR(idata[i]);
704 num_of_cmds = i;
705 goto cmd_err;
706 }
Linus Walleij97548572017-09-20 10:02:00 +0200707 /* This will be NULL on non-RPMB ioctl():s */
708 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100709 }
710
Jon Huntera5f57742015-09-22 10:27:53 +0100711 card = md->queue.card;
712 if (IS_ERR(card)) {
713 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200714 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100715 }
716
Jon Huntera5f57742015-09-22 10:27:53 +0100717
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200718 /*
719 * Dispatch the ioctl()s into the block request queue.
720 */
721 mq = &md->queue;
722 req = blk_get_request(mq->queue,
Christoph Hellwigff005a02018-05-09 09:54:05 +0200723 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200724 if (IS_ERR(req)) {
725 err = PTR_ERR(req);
726 goto cmd_err;
727 }
Linus Walleij97548572017-09-20 10:02:00 +0200728 req_to_mmc_queue_req(req)->drv_op =
729 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200730 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200731 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
732 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200733 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100734
735 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700736 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100737 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100738
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200739 blk_put_request(req);
740
Jon Huntera5f57742015-09-22 10:27:53 +0100741cmd_err:
742 for (i = 0; i < num_of_cmds; i++) {
743 kfree(idata[i]->buf);
744 kfree(idata[i]);
745 }
746 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700747 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100748}
749
Linus Walleij61fe0e22017-08-20 23:39:09 +0200750static int mmc_blk_check_blkdev(struct block_device *bdev)
751{
752 /*
753 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
754 * whole block device, not on a partition. This prevents overspray
755 * between sibling partitions.
756 */
757 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
758 return -EPERM;
759 return 0;
760}
761
John Calixtocb87ea22011-04-26 18:56:29 -0400762static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
763 unsigned int cmd, unsigned long arg)
764{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200765 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200766 int ret;
767
Jon Huntera5f57742015-09-22 10:27:53 +0100768 switch (cmd) {
769 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200770 ret = mmc_blk_check_blkdev(bdev);
771 if (ret)
772 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200773 md = mmc_blk_get(bdev->bd_disk);
774 if (!md)
775 return -EINVAL;
776 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200777 (struct mmc_ioc_cmd __user *)arg,
778 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200779 mmc_blk_put(md);
780 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100781 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200782 ret = mmc_blk_check_blkdev(bdev);
783 if (ret)
784 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200785 md = mmc_blk_get(bdev->bd_disk);
786 if (!md)
787 return -EINVAL;
788 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200789 (struct mmc_ioc_multi_cmd __user *)arg,
790 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200791 mmc_blk_put(md);
792 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100793 default:
794 return -EINVAL;
795 }
John Calixtocb87ea22011-04-26 18:56:29 -0400796}
797
798#ifdef CONFIG_COMPAT
799static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
800 unsigned int cmd, unsigned long arg)
801{
802 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
803}
804#endif
805
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700806static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500807 .open = mmc_blk_open,
808 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800809 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400811 .ioctl = mmc_blk_ioctl,
812#ifdef CONFIG_COMPAT
813 .compat_ioctl = mmc_blk_compat_ioctl,
814#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815};
816
Adrian Hunter025e3d52017-03-13 14:36:39 +0200817static int mmc_blk_part_switch_pre(struct mmc_card *card,
818 unsigned int part_type)
819{
820 int ret = 0;
821
822 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
823 if (card->ext_csd.cmdq_en) {
824 ret = mmc_cmdq_disable(card);
825 if (ret)
826 return ret;
827 }
828 mmc_retune_pause(card->host);
829 }
830
831 return ret;
832}
833
834static int mmc_blk_part_switch_post(struct mmc_card *card,
835 unsigned int part_type)
836{
837 int ret = 0;
838
839 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
840 mmc_retune_unpause(card->host);
841 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
842 ret = mmc_cmdq_enable(card);
843 }
844
845 return ret;
846}
847
Andrei Warkentin371a6892011-04-11 18:10:25 -0500848static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200849 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500850{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200851 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200852 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300853
Linus Walleij1f797ed2017-08-20 23:39:10 +0200854 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500855 return 0;
856
857 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300858 u8 part_config = card->ext_csd.part_config;
859
Linus Walleij1f797ed2017-08-20 23:39:10 +0200860 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200861 if (ret)
862 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300863
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300864 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200865 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500866
867 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300868 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500869 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300870 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200871 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500872 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300873 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300874
875 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300876
Adrian Hunter025e3d52017-03-13 14:36:39 +0200877 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300878 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500879
Linus Walleij1f797ed2017-08-20 23:39:10 +0200880 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200881 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500882}
883
Linus Walleij169f03a2017-02-01 13:47:57 +0100884static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700885{
886 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100887 u32 result;
888 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700889
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900890 struct mmc_request mrq = {};
891 struct mmc_command cmd = {};
892 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700893
894 struct scatterlist sg;
895
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700896 cmd.opcode = MMC_APP_CMD;
897 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700898 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700899
900 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700901 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100902 return err;
David Brownell7213d172007-08-08 09:10:23 -0700903 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100904 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700905
906 memset(&cmd, 0, sizeof(struct mmc_command));
907
908 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
909 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700910 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700911
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700912 data.blksz = 4;
913 data.blocks = 1;
914 data.flags = MMC_DATA_READ;
915 data.sg = &sg;
916 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530917 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700918
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700919 mrq.cmd = &cmd;
920 mrq.data = &data;
921
Ben Dooks051913d2009-06-08 23:33:57 +0100922 blocks = kmalloc(4, GFP_KERNEL);
923 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100924 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100925
926 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700927
928 mmc_wait_for_req(card->host, &mrq);
929
Ben Dooks051913d2009-06-08 23:33:57 +0100930 result = ntohl(*blocks);
931 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700932
Ben Dooks051913d2009-06-08 23:33:57 +0100933 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100934 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100935
Linus Walleij169f03a2017-02-01 13:47:57 +0100936 *written_blocks = result;
937
938 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700939}
940
Adrian Hunter92c0a0cc2017-11-29 15:41:13 +0200941static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
942{
943 if (host->actual_clock)
944 return host->actual_clock / 1000;
945
946 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
947 if (host->ios.clock)
948 return host->ios.clock / 2000;
949
950 /* How can there be no clock */
951 WARN_ON_ONCE(1);
952 return 100; /* 100 kHz is minimum possible value */
953}
954
955static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
956 struct mmc_data *data)
957{
958 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
959 unsigned int khz;
960
961 if (data->timeout_clks) {
962 khz = mmc_blk_clock_khz(host);
963 ms += DIV_ROUND_UP(data->timeout_clks, khz);
964 }
965
966 return ms;
967}
968
Adrian Hunter0987c6b2017-11-29 15:41:12 +0200969static inline bool mmc_blk_in_tran_state(u32 status)
970{
971 /*
972 * Some cards mishandle the status bits, so make sure to check both the
973 * busy indication and the card state.
974 */
975 return status & R1_READY_FOR_DATA &&
976 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
977}
978
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100979static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Adrian Hunter0fbfd122017-11-29 15:41:18 +0200980 struct request *req, u32 *resp_errs)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100981{
982 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
983 int err = 0;
984 u32 status;
985
986 do {
Adrian Hunter77018852017-11-29 15:41:11 +0200987 bool done = time_after(jiffies, timeout);
988
Ulf Hansson2185bc22017-05-22 10:23:58 +0200989 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100990 if (err) {
991 pr_err("%s: error %d requesting status\n",
992 req->rq_disk->disk_name, err);
993 return err;
994 }
995
Adrian Hunterc89b4852017-11-29 15:41:09 +0200996 /* Accumulate any response error bits seen */
997 if (resp_errs)
998 *resp_errs |= status;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100999
1000 /*
1001 * Timeout if the device never becomes ready for data and never
1002 * leaves the program state.
1003 */
Adrian Hunter77018852017-11-29 15:41:11 +02001004 if (done) {
Adrian Hunter0987c6b2017-11-29 15:41:12 +02001005 pr_err("%s: Card stuck in wrong state! %s %s status: %#x\n",
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001006 mmc_hostname(card->host),
Adrian Hunter0987c6b2017-11-29 15:41:12 +02001007 req->rq_disk->disk_name, __func__, status);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001008 return -ETIMEDOUT;
1009 }
1010
1011 /*
1012 * Some cards mishandle the status bits,
1013 * so make sure to check both the busy
1014 * indication and the card state.
1015 */
Adrian Hunter0987c6b2017-11-29 15:41:12 +02001016 } while (!mmc_blk_in_tran_state(status));
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001017
1018 return err;
1019}
1020
Adrian Hunter67716322011-08-29 16:42:15 +03001021static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1022 int type)
1023{
1024 int err;
1025
1026 if (md->reset_done & type)
1027 return -EEXIST;
1028
1029 md->reset_done |= type;
1030 err = mmc_hw_reset(host);
1031 /* Ensure we switch back to the correct partition */
1032 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001033 struct mmc_blk_data *main_md =
1034 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001035 int part_err;
1036
1037 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001038 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001039 if (part_err) {
1040 /*
1041 * We have failed to get back into the correct
1042 * partition, so we need to abort the whole request.
1043 */
1044 return -ENODEV;
1045 }
1046 }
1047 return err;
1048}
1049
1050static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1051{
1052 md->reset_done &= ~type;
1053}
1054
Linus Walleij5ec12392017-05-19 15:37:29 +02001055/*
1056 * The non-block commands come back from the block layer after it queued it and
1057 * processed it with all other requests and then they get issued in this
1058 * function.
1059 */
1060static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1061{
1062 struct mmc_queue_req *mq_rq;
1063 struct mmc_card *card = mq->card;
1064 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001065 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001066 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001067 u8 **ext_csd;
1068 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001069 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001070 int i;
1071
1072 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001073 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001074
1075 switch (mq_rq->drv_op) {
1076 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001077 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001078 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001079 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001080 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001081 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001082 break;
1083 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001084 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001085 if (rpmb_ioctl)
1086 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001087 break;
1088 case MMC_DRV_OP_BOOT_WP:
1089 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1090 card->ext_csd.boot_ro_lock |
1091 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1092 card->ext_csd.part_time);
1093 if (ret)
1094 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1095 md->disk->disk_name, ret);
1096 else
1097 card->ext_csd.boot_ro_lock |=
1098 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001099 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001100 case MMC_DRV_OP_GET_CARD_STATUS:
1101 ret = mmc_send_status(card, &status);
1102 if (!ret)
1103 ret = status;
1104 break;
1105 case MMC_DRV_OP_GET_EXT_CSD:
1106 ext_csd = mq_rq->drv_op_data;
1107 ret = mmc_get_ext_csd(card, ext_csd);
1108 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001109 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001110 pr_err("%s: unknown driver specific operation\n",
1111 md->disk->disk_name);
1112 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001113 break;
1114 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001115 mq_rq->drv_op_result = ret;
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001116 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001117}
1118
Linus Walleijdf061582017-01-24 11:17:57 +01001119static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001120{
Linus Walleij7db30282016-11-18 13:36:15 +01001121 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001122 struct mmc_card *card = md->queue.card;
1123 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001124 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001125 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001126
Adrian Hunterbd788c92010-08-11 14:17:47 -07001127 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001128 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001129 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001130 }
1131
1132 from = blk_rq_pos(req);
1133 nr = blk_rq_sectors(req);
1134
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001135 if (mmc_can_discard(card))
1136 arg = MMC_DISCARD_ARG;
1137 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001138 arg = MMC_TRIM_ARG;
1139 else
1140 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001141 do {
1142 err = 0;
1143 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1144 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1145 INAND_CMD38_ARG_EXT_CSD,
1146 arg == MMC_TRIM_ARG ?
1147 INAND_CMD38_ARG_TRIM :
1148 INAND_CMD38_ARG_ERASE,
1149 0);
1150 }
1151 if (!err)
1152 err = mmc_erase(card, from, nr, arg);
1153 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001154 if (err)
1155 status = BLK_STS_IOERR;
1156 else
Adrian Hunter67716322011-08-29 16:42:15 +03001157 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001158fail:
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001159 blk_mq_end_request(req, status);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001160}
1161
Linus Walleijdf061582017-01-24 11:17:57 +01001162static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001163 struct request *req)
1164{
Linus Walleij7db30282016-11-18 13:36:15 +01001165 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001166 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001167 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001168 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001169 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001170
Maya Erez775a9362013-04-18 15:41:55 +03001171 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001172 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001173 goto out;
1174 }
1175
1176 from = blk_rq_pos(req);
1177 nr = blk_rq_sectors(req);
1178
Maya Erez775a9362013-04-18 15:41:55 +03001179 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1180 arg = MMC_SECURE_TRIM1_ARG;
1181 else
1182 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001183
Adrian Hunter67716322011-08-29 16:42:15 +03001184retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001185 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1186 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1187 INAND_CMD38_ARG_EXT_CSD,
1188 arg == MMC_SECURE_TRIM1_ARG ?
1189 INAND_CMD38_ARG_SECTRIM1 :
1190 INAND_CMD38_ARG_SECERASE,
1191 0);
1192 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001193 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001194 }
Adrian Hunter28302812012-04-05 14:45:48 +03001195
Adrian Hunter49804542010-08-11 14:17:50 -07001196 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001197 if (err == -EIO)
1198 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001199 if (err) {
1200 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001201 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001202 }
Adrian Hunter28302812012-04-05 14:45:48 +03001203
1204 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001205 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1206 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1207 INAND_CMD38_ARG_EXT_CSD,
1208 INAND_CMD38_ARG_SECTRIM2,
1209 0);
1210 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001211 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001212 }
Adrian Hunter28302812012-04-05 14:45:48 +03001213
Adrian Hunter49804542010-08-11 14:17:50 -07001214 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001215 if (err == -EIO)
1216 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001217 if (err) {
1218 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001219 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001220 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001221 }
Adrian Hunter28302812012-04-05 14:45:48 +03001222
Adrian Hunter28302812012-04-05 14:45:48 +03001223out_retry:
1224 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001225 goto retry;
1226 if (!err)
1227 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001228out:
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001229 blk_mq_end_request(req, status);
Adrian Hunter49804542010-08-11 14:17:50 -07001230}
1231
Linus Walleijdf061582017-01-24 11:17:57 +01001232static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001233{
Linus Walleij7db30282016-11-18 13:36:15 +01001234 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001235 struct mmc_card *card = md->queue.card;
1236 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001237
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001238 ret = mmc_flush_cache(card);
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001239 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001240}
1241
1242/*
1243 * Reformat current write as a reliable write, supporting
1244 * both legacy and the enhanced reliable write MMC cards.
1245 * In each transfer we'll handle only as much as a single
1246 * reliable write can handle, thus finish the request in
1247 * partial completions.
1248 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001249static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1250 struct mmc_card *card,
1251 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001252{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001253 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1254 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001255 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001256 brq->data.blocks = 1;
1257
1258 if (brq->data.blocks > card->ext_csd.rel_sectors)
1259 brq->data.blocks = card->ext_csd.rel_sectors;
1260 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1261 brq->data.blocks = 1;
1262 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001263}
1264
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001265#define CMD_ERRORS_EXCL_OOR \
1266 (R1_ADDRESS_ERROR | /* Misaligned address */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001267 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1268 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001269 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001270 R1_CC_ERROR | /* Card controller error */ \
1271 R1_ERROR) /* General/unknown error */
1272
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001273#define CMD_ERRORS \
1274 (CMD_ERRORS_EXCL_OOR | \
1275 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1276
Shawn Lind83c2db2017-08-18 09:16:08 +08001277static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001278{
Shawn Lind83c2db2017-08-18 09:16:08 +08001279 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001280
Shawn Lind83c2db2017-08-18 09:16:08 +08001281 /*
1282 * Per the SD specification(physical layer version 4.10)[1],
1283 * section 4.3.3, it explicitly states that "When the last
1284 * block of user area is read using CMD18, the host should
1285 * ignore OUT_OF_RANGE error that may occur even the sequence
1286 * is correct". And JESD84-B51 for eMMC also has a similar
1287 * statement on section 6.8.3.
1288 *
1289 * Multiple block read/write could be done by either predefined
1290 * method, namely CMD23, or open-ending mode. For open-ending mode,
1291 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1292 *
1293 * However the spec[1] doesn't tell us whether we should also
1294 * ignore that for predefined method. But per the spec[1], section
1295 * 4.15 Set Block Count Command, it says"If illegal block count
1296 * is set, out of range error will be indicated during read/write
1297 * operation (For example, data transfer is stopped at user area
1298 * boundary)." In another word, we could expect a out of range error
1299 * in the response for the following CMD18/25. And if argument of
1300 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1301 * we could also expect to get a -ETIMEDOUT or any error number from
1302 * the host drivers due to missing data response(for write)/data(for
1303 * read), as the cards will stop the data transfer by itself per the
1304 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1305 */
1306
1307 if (!brq->stop.error) {
1308 bool oor_with_open_end;
1309 /* If there is no error yet, check R1 response */
1310
1311 val = brq->stop.resp[0] & CMD_ERRORS;
1312 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1313
1314 if (val && !oor_with_open_end)
1315 brq->stop.error = -EIO;
1316 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001317}
1318
Adrian Hunterca5717f2017-03-13 14:36:41 +02001319static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001320 int disable_multi, bool *do_rel_wr_p,
1321 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001323 struct mmc_blk_data *md = mq->blkdata;
1324 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001325 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001326 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001327 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001329 /*
1330 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001331 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001332 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001333 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1334 rq_data_dir(req) == WRITE &&
1335 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001336
Per Forlin54d49d72011-07-01 18:55:29 +02001337 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001338
Per Forlin54d49d72011-07-01 18:55:29 +02001339 brq->mrq.data = &brq->data;
Adrian Hunter93482b32017-09-22 15:36:56 +03001340 brq->mrq.tag = req->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Per Forlin54d49d72011-07-01 18:55:29 +02001342 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1343 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001344
1345 if (rq_data_dir(req) == READ) {
1346 brq->data.flags = MMC_DATA_READ;
1347 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1348 } else {
1349 brq->data.flags = MMC_DATA_WRITE;
1350 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1351 }
1352
1353 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001354 brq->data.blocks = blk_rq_sectors(req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001355 brq->data.blk_addr = blk_rq_pos(req);
1356
1357 /*
1358 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1359 * The eMMC will give "high" priority tasks priority over "simple"
1360 * priority tasks. Here we always set "simple" priority by not setting
1361 * MMC_DATA_PRIO.
1362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Per Forlin54d49d72011-07-01 18:55:29 +02001364 /*
1365 * The block layer doesn't support all sector count
1366 * restrictions, so we need to be prepared for too big
1367 * requests.
1368 */
1369 if (brq->data.blocks > card->host->max_blk_count)
1370 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001372 if (brq->data.blocks > 1) {
1373 /*
Chris Boot41591b32018-10-08 17:07:30 +02001374 * Some SD cards in SPI mode return a CRC error or even lock up
1375 * completely when trying to read the last block using a
1376 * multiblock read command.
1377 */
1378 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1379 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1380 get_capacity(md->disk)))
1381 brq->data.blocks--;
1382
1383 /*
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001384 * After a read error, we redo the request one sector
1385 * at a time in order to accurately determine which
1386 * sectors can be read successfully.
1387 */
1388 if (disable_multi)
1389 brq->data.blocks = 1;
1390
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001391 /*
1392 * Some controllers have HW issues while operating
1393 * in multiple I/O mode
1394 */
1395 if (card->host->ops->multi_io_quirk)
1396 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1397 (rq_data_dir(req) == READ) ?
1398 MMC_DATA_READ : MMC_DATA_WRITE,
1399 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001400 }
Per Forlin54d49d72011-07-01 18:55:29 +02001401
Adrian Hunter93482b32017-09-22 15:36:56 +03001402 if (do_rel_wr) {
Adrian Hunterca5717f2017-03-13 14:36:41 +02001403 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001404 brq->data.flags |= MMC_DATA_REL_WR;
1405 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001406
1407 /*
1408 * Data tag is used only during writing meta data to speed
1409 * up write and any subsequent read of this meta data
1410 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001411 do_data_tag = card->ext_csd.data_tag_unit_size &&
1412 (req->cmd_flags & REQ_META) &&
1413 (rq_data_dir(req) == WRITE) &&
1414 ((brq->data.blocks * brq->data.blksz) >=
1415 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001416
Adrian Hunter93482b32017-09-22 15:36:56 +03001417 if (do_data_tag)
1418 brq->data.flags |= MMC_DATA_DAT_TAG;
1419
Adrian Hunterca5717f2017-03-13 14:36:41 +02001420 mmc_set_data_timeout(&brq->data, card);
1421
1422 brq->data.sg = mqrq->sg;
1423 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1424
1425 /*
1426 * Adjust the sg list so it is the same size as the
1427 * request.
1428 */
1429 if (brq->data.blocks != blk_rq_sectors(req)) {
1430 int i, data_size = brq->data.blocks << 9;
1431 struct scatterlist *sg;
1432
1433 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1434 data_size -= sg->length;
1435 if (data_size <= 0) {
1436 sg->length += data_size;
1437 i++;
1438 break;
1439 }
1440 }
1441 brq->data.sg_len = i;
1442 }
1443
Adrian Hunterd3377c02017-09-22 15:36:55 +03001444 if (do_rel_wr_p)
1445 *do_rel_wr_p = do_rel_wr;
1446
1447 if (do_data_tag_p)
1448 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001449}
1450
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02001451#define MMC_CQE_RETRIES 2
1452
1453static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1454{
1455 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1456 struct mmc_request *mrq = &mqrq->brq.mrq;
1457 struct request_queue *q = req->q;
1458 struct mmc_host *host = mq->card->host;
1459 unsigned long flags;
1460 bool put_card;
1461 int err;
1462
1463 mmc_cqe_post_req(host, mrq);
1464
1465 if (mrq->cmd && mrq->cmd->error)
1466 err = mrq->cmd->error;
1467 else if (mrq->data && mrq->data->error)
1468 err = mrq->data->error;
1469 else
1470 err = 0;
1471
1472 if (err) {
1473 if (mqrq->retries++ < MMC_CQE_RETRIES)
1474 blk_mq_requeue_request(req, true);
1475 else
1476 blk_mq_end_request(req, BLK_STS_IOERR);
1477 } else if (mrq->data) {
1478 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1479 blk_mq_requeue_request(req, true);
1480 else
1481 __blk_mq_end_request(req, BLK_STS_OK);
1482 } else {
1483 blk_mq_end_request(req, BLK_STS_OK);
1484 }
1485
1486 spin_lock_irqsave(q->queue_lock, flags);
1487
1488 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1489
1490 put_card = (mmc_tot_in_flight(mq) == 0);
1491
1492 mmc_cqe_check_busy(mq);
1493
1494 spin_unlock_irqrestore(q->queue_lock, flags);
1495
1496 if (!mq->cqe_busy)
1497 blk_mq_run_hw_queues(q, true);
1498
1499 if (put_card)
1500 mmc_put_card(mq->card, &mq->ctx);
1501}
1502
1503void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1504{
1505 struct mmc_card *card = mq->card;
1506 struct mmc_host *host = card->host;
1507 int err;
1508
1509 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1510
1511 err = mmc_cqe_recovery(host);
1512 if (err)
1513 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1514 else
1515 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1516
1517 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1518}
1519
1520static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1521{
1522 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1523 brq.mrq);
1524 struct request *req = mmc_queue_req_to_req(mqrq);
1525 struct request_queue *q = req->q;
1526 struct mmc_queue *mq = q->queuedata;
1527
1528 /*
1529 * Block layer timeouts race with completions which means the normal
1530 * completion path cannot be used during recovery.
1531 */
1532 if (mq->in_recovery)
1533 mmc_blk_cqe_complete_rq(mq, req);
1534 else
1535 blk_mq_complete_request(req);
1536}
1537
1538static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1539{
1540 mrq->done = mmc_blk_cqe_req_done;
1541 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1542
1543 return mmc_cqe_start_req(host, mrq);
1544}
1545
1546static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1547 struct request *req)
1548{
1549 struct mmc_blk_request *brq = &mqrq->brq;
1550
1551 memset(brq, 0, sizeof(*brq));
1552
1553 brq->mrq.cmd = &brq->cmd;
1554 brq->mrq.tag = req->tag;
1555
1556 return &brq->mrq;
1557}
1558
1559static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1560{
1561 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1562 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1563
1564 mrq->cmd->opcode = MMC_SWITCH;
1565 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1566 (EXT_CSD_FLUSH_CACHE << 16) |
1567 (1 << 8) |
1568 EXT_CSD_CMD_SET_NORMAL;
1569 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1570
1571 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1572}
1573
1574static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1575{
1576 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1577
1578 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1579
1580 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1581}
1582
Adrian Hunterca5717f2017-03-13 14:36:41 +02001583static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1584 struct mmc_card *card,
1585 int disable_multi,
1586 struct mmc_queue *mq)
1587{
1588 u32 readcmd, writecmd;
1589 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001590 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001591 struct mmc_blk_data *md = mq->blkdata;
1592 bool do_rel_wr, do_data_tag;
1593
1594 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1595
1596 brq->mrq.cmd = &brq->cmd;
1597
1598 brq->cmd.arg = blk_rq_pos(req);
1599 if (!mmc_card_blockaddr(card))
1600 brq->cmd.arg <<= 9;
1601 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1602
Per Forlin54d49d72011-07-01 18:55:29 +02001603 if (brq->data.blocks > 1 || do_rel_wr) {
1604 /* SPI multiblock writes terminate using a special
1605 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001606 */
Per Forlin54d49d72011-07-01 18:55:29 +02001607 if (!mmc_host_is_spi(card->host) ||
1608 rq_data_dir(req) == READ)
1609 brq->mrq.stop = &brq->stop;
1610 readcmd = MMC_READ_MULTIPLE_BLOCK;
1611 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1612 } else {
1613 brq->mrq.stop = NULL;
1614 readcmd = MMC_READ_SINGLE_BLOCK;
1615 writecmd = MMC_WRITE_BLOCK;
1616 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001617 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301618
1619 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001620 * Pre-defined multi-block transfers are preferable to
1621 * open ended-ones (and necessary for reliable writes).
1622 * However, it is not sufficient to just send CMD23,
1623 * and avoid the final CMD12, as on an error condition
1624 * CMD12 (stop) needs to be sent anyway. This, coupled
1625 * with Auto-CMD23 enhancements provided by some
1626 * hosts, means that the complexity of dealing
1627 * with this is best left to the host. If CMD23 is
1628 * supported by card and host, we'll fill sbc in and let
1629 * the host deal with handling it correctly. This means
1630 * that for hosts that don't expose MMC_CAP_CMD23, no
1631 * change of behavior will be observed.
1632 *
1633 * N.B: Some MMC cards experience perf degradation.
1634 * We'll avoid using CMD23-bounded multiblock writes for
1635 * these, while retaining features like reliable writes.
1636 */
Saugata Das42659002011-12-21 13:09:17 +05301637 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1638 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1639 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001640 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1641 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301642 (do_rel_wr ? (1 << 31) : 0) |
1643 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001644 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1645 brq->mrq.sbc = &brq->sbc;
1646 }
Per Forlin54d49d72011-07-01 18:55:29 +02001647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Adrian Hunter81196972017-11-29 15:41:03 +02001649#define MMC_MAX_RETRIES 5
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001650#define MMC_DATA_RETRIES 2
Adrian Hunter81196972017-11-29 15:41:03 +02001651#define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1652
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001653static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1654{
1655 struct mmc_command cmd = {
1656 .opcode = MMC_STOP_TRANSMISSION,
1657 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1658 /* Some hosts wait for busy anyway, so provide a busy timeout */
1659 .busy_timeout = timeout,
1660 };
1661
1662 return mmc_wait_for_cmd(card->host, &cmd, 5);
1663}
1664
1665static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1666{
1667 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1668 struct mmc_blk_request *brq = &mqrq->brq;
1669 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1670 int err;
1671
1672 mmc_retune_hold_now(card->host);
1673
1674 mmc_blk_send_stop(card, timeout);
1675
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001676 err = card_busy_detect(card, timeout, req, NULL);
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001677
1678 mmc_retune_release(card->host);
1679
1680 return err;
1681}
1682
Adrian Hunter81196972017-11-29 15:41:03 +02001683#define MMC_READ_SINGLE_RETRIES 2
1684
1685/* Single sector read during recovery */
1686static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1687{
1688 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1689 struct mmc_request *mrq = &mqrq->brq.mrq;
1690 struct mmc_card *card = mq->card;
1691 struct mmc_host *host = card->host;
1692 blk_status_t error = BLK_STS_OK;
1693 int retries = 0;
1694
1695 do {
1696 u32 status;
1697 int err;
1698
1699 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1700
1701 mmc_wait_for_req(host, mrq);
1702
1703 err = mmc_send_status(card, &status);
1704 if (err)
1705 goto error_exit;
1706
1707 if (!mmc_host_is_spi(host) &&
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001708 !mmc_blk_in_tran_state(status)) {
1709 err = mmc_blk_fix_state(card, req);
Adrian Hunter81196972017-11-29 15:41:03 +02001710 if (err)
1711 goto error_exit;
1712 }
1713
1714 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1715 continue;
1716
1717 retries = 0;
1718
1719 if (mrq->cmd->error ||
1720 mrq->data->error ||
1721 (!mmc_host_is_spi(host) &&
1722 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1723 error = BLK_STS_IOERR;
1724 else
1725 error = BLK_STS_OK;
1726
1727 } while (blk_update_request(req, error, 512));
1728
1729 return;
1730
1731error_exit:
1732 mrq->data->bytes_xfered = 0;
1733 blk_update_request(req, BLK_STS_IOERR, 512);
1734 /* Let it try the remaining request again */
1735 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1736 mqrq->retries = MMC_MAX_RETRIES - 1;
1737}
1738
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001739static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1740{
1741 return !!brq->mrq.sbc;
1742}
1743
1744static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1745{
1746 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1747}
1748
1749/*
1750 * Check for errors the host controller driver might not have seen such as
1751 * response mode errors or invalid card state.
1752 */
1753static bool mmc_blk_status_error(struct request *req, u32 status)
1754{
1755 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1756 struct mmc_blk_request *brq = &mqrq->brq;
1757 struct mmc_queue *mq = req->q->queuedata;
1758 u32 stop_err_bits;
1759
1760 if (mmc_host_is_spi(mq->card->host))
Wu Fengguangaa950142017-11-30 05:54:24 +08001761 return false;
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001762
1763 stop_err_bits = mmc_blk_stop_err_bits(brq);
1764
1765 return brq->cmd.resp[0] & CMD_ERRORS ||
1766 brq->stop.resp[0] & stop_err_bits ||
1767 status & stop_err_bits ||
1768 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1769}
1770
1771static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1772{
1773 return !brq->sbc.error && !brq->cmd.error &&
1774 !(brq->cmd.resp[0] & CMD_ERRORS);
1775}
1776
1777/*
1778 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1779 * policy:
1780 * 1. A request that has transferred at least some data is considered
1781 * successful and will be requeued if there is remaining data to
1782 * transfer.
1783 * 2. Otherwise the number of retries is incremented and the request
1784 * will be requeued if there are remaining retries.
1785 * 3. Otherwise the request will be errored out.
1786 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1787 * mqrq->retries. So there are only 4 possible actions here:
1788 * 1. do not accept the bytes_xfered value i.e. set it to zero
1789 * 2. change mqrq->retries to determine the number of retries
1790 * 3. try to reset the card
1791 * 4. read one sector at a time
1792 */
Adrian Hunter81196972017-11-29 15:41:03 +02001793static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1794{
1795 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1796 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1797 struct mmc_blk_request *brq = &mqrq->brq;
1798 struct mmc_blk_data *md = mq->blkdata;
1799 struct mmc_card *card = mq->card;
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001800 u32 status;
1801 u32 blocks;
1802 int err;
Adrian Hunter81196972017-11-29 15:41:03 +02001803
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001804 /*
1805 * Some errors the host driver might not have seen. Set the number of
1806 * bytes transferred to zero in that case.
1807 */
1808 err = __mmc_send_status(card, &status, 0);
1809 if (err || mmc_blk_status_error(req, status))
1810 brq->data.bytes_xfered = 0;
Adrian Hunter81196972017-11-29 15:41:03 +02001811
1812 mmc_retune_release(card->host);
1813
1814 /*
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001815 * Try again to get the status. This also provides an opportunity for
1816 * re-tuning.
Adrian Hunter81196972017-11-29 15:41:03 +02001817 */
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001818 if (err)
1819 err = __mmc_send_status(card, &status, 0);
Adrian Hunter81196972017-11-29 15:41:03 +02001820
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001821 /*
1822 * Nothing more to do after the number of bytes transferred has been
1823 * updated and there is no card.
1824 */
1825 if (err && mmc_detect_card_removed(card->host))
1826 return;
Adrian Hunter81196972017-11-29 15:41:03 +02001827
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001828 /* Try to get back to "tran" state */
1829 if (!mmc_host_is_spi(mq->card->host) &&
1830 (err || !mmc_blk_in_tran_state(status)))
1831 err = mmc_blk_fix_state(mq->card, req);
1832
1833 /*
1834 * Special case for SD cards where the card might record the number of
1835 * blocks written.
1836 */
1837 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1838 rq_data_dir(req) == WRITE) {
1839 if (mmc_sd_num_wr_blocks(card, &blocks))
1840 brq->data.bytes_xfered = 0;
1841 else
1842 brq->data.bytes_xfered = blocks << 9;
Adrian Hunter81196972017-11-29 15:41:03 +02001843 }
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001844
1845 /* Reset if the card is in a bad state */
1846 if (!mmc_host_is_spi(mq->card->host) &&
1847 err && mmc_blk_reset(md, card->host, type)) {
1848 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
Adrian Hunter81196972017-11-29 15:41:03 +02001849 mqrq->retries = MMC_NO_RETRIES;
Adrian Hunter7eb43d52017-11-29 15:41:15 +02001850 return;
1851 }
1852
1853 /*
1854 * If anything was done, just return and if there is anything remaining
1855 * on the request it will get requeued.
1856 */
1857 if (brq->data.bytes_xfered)
1858 return;
1859
1860 /* Reset before last retry */
1861 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1862 mmc_blk_reset(md, card->host, type);
1863
1864 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1865 if (brq->sbc.error || brq->cmd.error)
1866 return;
1867
1868 /* Reduce the remaining retries for data errors */
1869 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1870 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1871 return;
1872 }
1873
1874 /* FIXME: Missing single sector read for large sector size */
1875 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1876 brq->data.blocks > 1) {
1877 /* Read one sector at a time */
1878 mmc_blk_read_single(mq, req);
1879 return;
Adrian Hunter81196972017-11-29 15:41:03 +02001880 }
1881}
1882
Adrian Hunter10f21df42017-11-29 15:41:07 +02001883static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1884{
1885 mmc_blk_eval_resp_error(brq);
1886
1887 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1888 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1889}
1890
Adrian Hunter88a51642017-11-29 15:41:08 +02001891static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1892{
1893 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001894 u32 status = 0;
Adrian Hunter88a51642017-11-29 15:41:08 +02001895 int err;
1896
1897 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1898 return 0;
1899
Adrian Hunter0fbfd122017-11-29 15:41:18 +02001900 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &status);
Adrian Hunter88a51642017-11-29 15:41:08 +02001901
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001902 /*
1903 * Do not assume data transferred correctly if there are any error bits
1904 * set.
1905 */
1906 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1907 mqrq->brq.data.bytes_xfered = 0;
Adrian Hunter88a51642017-11-29 15:41:08 +02001908 err = err ? err : -EIO;
1909 }
1910
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001911 /* Copy the exception bit so it will be seen later on */
1912 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1913 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1914
Adrian Hunter88a51642017-11-29 15:41:08 +02001915 return err;
1916}
1917
Adrian Hunter10f21df42017-11-29 15:41:07 +02001918static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1919 struct request *req)
1920{
1921 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1922
1923 mmc_blk_reset_success(mq->blkdata, type);
1924}
1925
Adrian Hunter81196972017-11-29 15:41:03 +02001926static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1927{
1928 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1929 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1930
1931 if (nr_bytes) {
1932 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1933 blk_mq_requeue_request(req, true);
1934 else
1935 __blk_mq_end_request(req, BLK_STS_OK);
1936 } else if (!blk_rq_bytes(req)) {
1937 __blk_mq_end_request(req, BLK_STS_IOERR);
1938 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1939 blk_mq_requeue_request(req, true);
1940 } else {
1941 if (mmc_card_removed(mq->card))
1942 req->rq_flags |= RQF_QUIET;
1943 blk_mq_end_request(req, BLK_STS_IOERR);
1944 }
1945}
1946
1947static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1948 struct mmc_queue_req *mqrq)
1949{
1950 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1951 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1952 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1953}
1954
1955static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1956 struct mmc_queue_req *mqrq)
1957{
1958 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1959 mmc_start_bkops(mq->card, true);
1960}
1961
1962void mmc_blk_mq_complete(struct request *req)
1963{
1964 struct mmc_queue *mq = req->q->queuedata;
1965
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02001966 if (mq->use_cqe)
1967 mmc_blk_cqe_complete_rq(mq, req);
1968 else
1969 mmc_blk_mq_complete_rq(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02001970}
1971
1972static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1973 struct request *req)
1974{
1975 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
Adrian Hunter88a51642017-11-29 15:41:08 +02001976 struct mmc_host *host = mq->card->host;
Adrian Hunter81196972017-11-29 15:41:03 +02001977
Adrian Hunter88a51642017-11-29 15:41:08 +02001978 if (mmc_blk_rq_error(&mqrq->brq) ||
1979 mmc_blk_card_busy(mq->card, req)) {
1980 mmc_blk_mq_rw_recovery(mq, req);
1981 } else {
1982 mmc_blk_rw_reset_success(mq, req);
1983 mmc_retune_release(host);
1984 }
Adrian Hunter81196972017-11-29 15:41:03 +02001985
1986 mmc_blk_urgent_bkops(mq, mqrq);
1987}
1988
1989static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1990{
1991 struct request_queue *q = req->q;
1992 unsigned long flags;
1993 bool put_card;
1994
1995 spin_lock_irqsave(q->queue_lock, flags);
1996
1997 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1998
1999 put_card = (mmc_tot_in_flight(mq) == 0);
2000
2001 spin_unlock_irqrestore(q->queue_lock, flags);
2002
2003 if (put_card)
2004 mmc_put_card(mq->card, &mq->ctx);
2005}
2006
2007static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2008{
2009 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2010 struct mmc_request *mrq = &mqrq->brq.mrq;
2011 struct mmc_host *host = mq->card->host;
2012
2013 mmc_post_req(host, mrq, 0);
2014
Adrian Hunter10f21df42017-11-29 15:41:07 +02002015 /*
2016 * Block layer timeouts race with completions which means the normal
2017 * completion path cannot be used during recovery.
2018 */
2019 if (mq->in_recovery)
2020 mmc_blk_mq_complete_rq(mq, req);
2021 else
2022 blk_mq_complete_request(req);
Adrian Hunter81196972017-11-29 15:41:03 +02002023
2024 mmc_blk_mq_dec_in_flight(mq, req);
2025}
2026
Adrian Hunter10f21df42017-11-29 15:41:07 +02002027void mmc_blk_mq_recovery(struct mmc_queue *mq)
2028{
2029 struct request *req = mq->recovery_req;
2030 struct mmc_host *host = mq->card->host;
2031 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2032
2033 mq->recovery_req = NULL;
2034 mq->rw_wait = false;
2035
2036 if (mmc_blk_rq_error(&mqrq->brq)) {
2037 mmc_retune_hold_now(host);
2038 mmc_blk_mq_rw_recovery(mq, req);
2039 }
2040
2041 mmc_blk_urgent_bkops(mq, mqrq);
2042
2043 mmc_blk_mq_post_req(mq, req);
2044}
2045
Adrian Hunter81196972017-11-29 15:41:03 +02002046static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2047 struct request **prev_req)
2048{
Adrian Hunter10f21df42017-11-29 15:41:07 +02002049 if (mmc_host_done_complete(mq->card->host))
2050 return;
2051
Adrian Hunter81196972017-11-29 15:41:03 +02002052 mutex_lock(&mq->complete_lock);
2053
2054 if (!mq->complete_req)
2055 goto out_unlock;
2056
2057 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2058
2059 if (prev_req)
2060 *prev_req = mq->complete_req;
2061 else
2062 mmc_blk_mq_post_req(mq, mq->complete_req);
2063
2064 mq->complete_req = NULL;
2065
2066out_unlock:
2067 mutex_unlock(&mq->complete_lock);
2068}
2069
2070void mmc_blk_mq_complete_work(struct work_struct *work)
2071{
2072 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2073 complete_work);
2074
2075 mmc_blk_mq_complete_prev_req(mq, NULL);
2076}
2077
2078static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2079{
2080 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2081 brq.mrq);
2082 struct request *req = mmc_queue_req_to_req(mqrq);
2083 struct request_queue *q = req->q;
2084 struct mmc_queue *mq = q->queuedata;
Adrian Hunter10f21df42017-11-29 15:41:07 +02002085 struct mmc_host *host = mq->card->host;
Adrian Hunter81196972017-11-29 15:41:03 +02002086 unsigned long flags;
Adrian Hunter81196972017-11-29 15:41:03 +02002087
Adrian Hunter10f21df42017-11-29 15:41:07 +02002088 if (!mmc_host_done_complete(host)) {
2089 bool waiting;
Adrian Hunter81196972017-11-29 15:41:03 +02002090
Adrian Hunter10f21df42017-11-29 15:41:07 +02002091 /*
2092 * We cannot complete the request in this context, so record
2093 * that there is a request to complete, and that a following
2094 * request does not need to wait (although it does need to
2095 * complete complete_req first).
2096 */
2097 spin_lock_irqsave(q->queue_lock, flags);
2098 mq->complete_req = req;
2099 mq->rw_wait = false;
2100 waiting = mq->waiting;
2101 spin_unlock_irqrestore(q->queue_lock, flags);
2102
2103 /*
2104 * If 'waiting' then the waiting task will complete this
2105 * request, otherwise queue a work to do it. Note that
2106 * complete_work may still race with the dispatch of a following
2107 * request.
2108 */
2109 if (waiting)
2110 wake_up(&mq->wait);
2111 else
2112 kblockd_schedule_work(&mq->complete_work);
2113
2114 return;
2115 }
2116
2117 /* Take the recovery path for errors or urgent background operations */
2118 if (mmc_blk_rq_error(&mqrq->brq) ||
2119 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2120 spin_lock_irqsave(q->queue_lock, flags);
2121 mq->recovery_needed = true;
2122 mq->recovery_req = req;
2123 spin_unlock_irqrestore(q->queue_lock, flags);
Adrian Hunter81196972017-11-29 15:41:03 +02002124 wake_up(&mq->wait);
Adrian Hunter10f21df42017-11-29 15:41:07 +02002125 schedule_work(&mq->recovery_work);
2126 return;
2127 }
2128
2129 mmc_blk_rw_reset_success(mq, req);
2130
2131 mq->rw_wait = false;
2132 wake_up(&mq->wait);
2133
2134 mmc_blk_mq_post_req(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02002135}
2136
2137static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2138{
2139 struct request_queue *q = mq->queue;
2140 unsigned long flags;
2141 bool done;
2142
2143 /*
Adrian Hunter10f21df42017-11-29 15:41:07 +02002144 * Wait while there is another request in progress, but not if recovery
2145 * is needed. Also indicate whether there is a request waiting to start.
Adrian Hunter81196972017-11-29 15:41:03 +02002146 */
2147 spin_lock_irqsave(q->queue_lock, flags);
Adrian Hunter10f21df42017-11-29 15:41:07 +02002148 if (mq->recovery_needed) {
2149 *err = -EBUSY;
2150 done = true;
2151 } else {
2152 done = !mq->rw_wait;
2153 }
Adrian Hunter81196972017-11-29 15:41:03 +02002154 mq->waiting = !done;
2155 spin_unlock_irqrestore(q->queue_lock, flags);
2156
2157 return done;
2158}
2159
2160static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2161{
2162 int err = 0;
2163
2164 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2165
2166 /* Always complete the previous request if there is one */
2167 mmc_blk_mq_complete_prev_req(mq, prev_req);
2168
2169 return err;
2170}
2171
2172static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2173 struct request *req)
2174{
2175 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2176 struct mmc_host *host = mq->card->host;
2177 struct request *prev_req = NULL;
2178 int err = 0;
2179
2180 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2181
2182 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2183
2184 mmc_pre_req(host, &mqrq->brq.mrq);
2185
2186 err = mmc_blk_rw_wait(mq, &prev_req);
2187 if (err)
2188 goto out_post_req;
2189
2190 mq->rw_wait = true;
2191
2192 err = mmc_start_request(host, &mqrq->brq.mrq);
2193
2194 if (prev_req)
2195 mmc_blk_mq_post_req(mq, prev_req);
2196
Adrian Hunter10f21df42017-11-29 15:41:07 +02002197 if (err)
Adrian Hunter81196972017-11-29 15:41:03 +02002198 mq->rw_wait = false;
Adrian Hunter10f21df42017-11-29 15:41:07 +02002199
2200 /* Release re-tuning here where there is no synchronization required */
2201 if (err || mmc_host_done_complete(host))
Adrian Hunter81196972017-11-29 15:41:03 +02002202 mmc_retune_release(host);
Adrian Hunter81196972017-11-29 15:41:03 +02002203
2204out_post_req:
2205 if (err)
2206 mmc_post_req(host, &mqrq->brq.mrq, err);
2207
2208 return err;
2209}
2210
2211static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2212{
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002213 if (mq->use_cqe)
2214 return host->cqe_ops->cqe_wait_for_idle(host);
2215
Adrian Hunter81196972017-11-29 15:41:03 +02002216 return mmc_blk_rw_wait(mq, NULL);
2217}
2218
2219enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2220{
2221 struct mmc_blk_data *md = mq->blkdata;
2222 struct mmc_card *card = md->queue.card;
2223 struct mmc_host *host = card->host;
2224 int ret;
2225
2226 ret = mmc_blk_part_switch(card, md->part_type);
2227 if (ret)
2228 return MMC_REQ_FAILED_TO_START;
2229
2230 switch (mmc_issue_type(mq, req)) {
2231 case MMC_ISSUE_SYNC:
2232 ret = mmc_blk_wait_for_idle(mq, host);
2233 if (ret)
2234 return MMC_REQ_BUSY;
2235 switch (req_op(req)) {
2236 case REQ_OP_DRV_IN:
2237 case REQ_OP_DRV_OUT:
2238 mmc_blk_issue_drv_op(mq, req);
2239 break;
2240 case REQ_OP_DISCARD:
2241 mmc_blk_issue_discard_rq(mq, req);
2242 break;
2243 case REQ_OP_SECURE_ERASE:
2244 mmc_blk_issue_secdiscard_rq(mq, req);
2245 break;
2246 case REQ_OP_FLUSH:
2247 mmc_blk_issue_flush(mq, req);
2248 break;
2249 default:
2250 WARN_ON_ONCE(1);
2251 return MMC_REQ_FAILED_TO_START;
2252 }
2253 return MMC_REQ_FINISHED;
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002254 case MMC_ISSUE_DCMD:
Adrian Hunter81196972017-11-29 15:41:03 +02002255 case MMC_ISSUE_ASYNC:
2256 switch (req_op(req)) {
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002257 case REQ_OP_FLUSH:
2258 ret = mmc_blk_cqe_issue_flush(mq, req);
2259 break;
Adrian Hunter81196972017-11-29 15:41:03 +02002260 case REQ_OP_READ:
2261 case REQ_OP_WRITE:
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002262 if (mq->use_cqe)
2263 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2264 else
2265 ret = mmc_blk_mq_issue_rw_rq(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02002266 break;
2267 default:
2268 WARN_ON_ONCE(1);
2269 ret = -EINVAL;
2270 }
2271 if (!ret)
2272 return MMC_REQ_STARTED;
2273 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2274 default:
2275 WARN_ON_ONCE(1);
2276 return MMC_REQ_FAILED_TO_START;
2277 }
2278}
2279
Russell Kinga6f6c962006-01-03 22:38:44 +00002280static inline int mmc_blk_readonly(struct mmc_card *card)
2281{
2282 return mmc_card_readonly(card) ||
2283 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2284}
2285
Andrei Warkentin371a6892011-04-11 18:10:25 -05002286static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2287 struct device *parent,
2288 sector_t size,
2289 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002290 const char *subname,
2291 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 struct mmc_blk_data *md;
2294 int devidx, ret;
2295
Heiner Kallweita04848c2017-02-01 19:44:22 +01002296 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002297 if (devidx < 0) {
2298 /*
2299 * We get -ENOSPC because there are no more any available
2300 * devidx. The reason may be that, either userspace haven't yet
2301 * unmounted the partitions, which postpones mmc_blk_release()
2302 * from being called, or the device has more partitions than
2303 * what we support.
2304 */
2305 if (devidx == -ENOSPC)
2306 dev_err(mmc_dev(card->host),
2307 "no more device IDs available\n");
2308
Heiner Kallweita04848c2017-02-01 19:44:22 +01002309 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002312 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002313 if (!md) {
2314 ret = -ENOMEM;
2315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002317
Johan Rudholmadd710e2011-12-02 08:51:06 +01002318 md->area_type = area_type;
2319
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002320 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002321 * Set the read-only status based on the supported commands
2322 * and the write protect switch.
2323 */
2324 md->read_only = mmc_blk_readonly(card);
2325
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002326 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002327 if (md->disk == NULL) {
2328 ret = -ENOMEM;
2329 goto err_kfree;
2330 }
2331
2332 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002333 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002334 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002335 md->usage = 1;
2336
Adrian Hunterd09408a2011-06-23 13:40:28 +03002337 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002338 if (ret)
2339 goto err_putdisk;
2340
Linus Walleij7db30282016-11-18 13:36:15 +01002341 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002342
Adrian Hunter41e3efd2017-11-29 15:40:59 +02002343 /*
2344 * Keep an extra reference to the queue so that we can shutdown the
2345 * queue (i.e. call blk_cleanup_queue()) while there are still
2346 * references to the 'md'. The corresponding blk_put_queue() is in
2347 * mmc_blk_put().
2348 */
2349 if (!blk_get_queue(md->queue.queue)) {
2350 mmc_cleanup_queue(&md->queue);
Dan Carpenter2361bfb2017-12-08 14:55:16 +03002351 ret = -ENODEV;
Adrian Hunter41e3efd2017-11-29 15:40:59 +02002352 goto err_putdisk;
2353 }
2354
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002355 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002356 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002357 md->disk->fops = &mmc_bdops;
2358 md->disk->private_data = md;
2359 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002360 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002361 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002362 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002363 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Andrew Gabbasov207b6522018-02-27 17:03:49 +05302364 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2365 | GENHD_FL_SUPPRESS_PARTITION_INFO;
Russell Kinga6f6c962006-01-03 22:38:44 +00002366
2367 /*
2368 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2369 *
2370 * - be set for removable media with permanent block devices
2371 * - be unset for removable block devices with permanent media
2372 *
2373 * Since MMC block devices clearly fall under the second
2374 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2375 * should use the block device creation/destruction hotplug
2376 * messages to tell when the card is present.
2377 */
2378
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002379 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002380 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002381
Saugata Dasa5075eb2012-05-17 16:32:21 +05302382 if (mmc_card_mmc(card))
2383 blk_queue_logical_block_size(md->queue.queue,
2384 card->ext_csd.data_sector_size);
2385 else
2386 blk_queue_logical_block_size(md->queue.queue, 512);
2387
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002389
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002390 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002391 if ((mmc_card_mmc(card) &&
2392 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002393 (mmc_card_sd(card) &&
2394 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2395 md->flags |= MMC_BLK_CMD23;
2396 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002397
2398 if (mmc_card_mmc(card) &&
2399 md->flags & MMC_BLK_CMD23 &&
2400 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2401 card->ext_csd.rel_sectors)) {
2402 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002403 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002404 }
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002407
2408 err_putdisk:
2409 put_disk(md->disk);
2410 err_kfree:
2411 kfree(md);
2412 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002413 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002414 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Andrei Warkentin371a6892011-04-11 18:10:25 -05002417static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2418{
2419 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002420
2421 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2422 /*
2423 * The EXT_CSD sector count is in number or 512 byte
2424 * sectors.
2425 */
2426 size = card->ext_csd.sectors;
2427 } else {
2428 /*
2429 * The CSD capacity field is in units of read_blkbits.
2430 * set_capacity takes units of 512 bytes.
2431 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002432 size = (typeof(sector_t))card->csd.capacity
2433 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002434 }
2435
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002436 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002437 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002438}
2439
2440static int mmc_blk_alloc_part(struct mmc_card *card,
2441 struct mmc_blk_data *md,
2442 unsigned int part_type,
2443 sector_t size,
2444 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002445 const char *subname,
2446 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002447{
2448 char cap_str[10];
2449 struct mmc_blk_data *part_md;
2450
2451 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002452 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002453 if (IS_ERR(part_md))
2454 return PTR_ERR(part_md);
2455 part_md->part_type = part_type;
2456 list_add(&part_md->part, &md->part);
2457
James Bottomleyb9f28d82015-03-05 18:47:01 -08002458 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002459 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302460 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002461 part_md->disk->disk_name, mmc_card_id(card),
2462 mmc_card_name(card), part_md->part_type, cap_str);
2463 return 0;
2464}
2465
Linus Walleij97548572017-09-20 10:02:00 +02002466/**
2467 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2468 * @filp: the character device file
2469 * @cmd: the ioctl() command
2470 * @arg: the argument from userspace
2471 *
2472 * This will essentially just redirect the ioctl()s coming in over to
2473 * the main block device spawning the RPMB character device.
2474 */
2475static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2476 unsigned long arg)
2477{
2478 struct mmc_rpmb_data *rpmb = filp->private_data;
2479 int ret;
2480
2481 switch (cmd) {
2482 case MMC_IOC_CMD:
2483 ret = mmc_blk_ioctl_cmd(rpmb->md,
2484 (struct mmc_ioc_cmd __user *)arg,
2485 rpmb);
2486 break;
2487 case MMC_IOC_MULTI_CMD:
2488 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2489 (struct mmc_ioc_multi_cmd __user *)arg,
2490 rpmb);
2491 break;
2492 default:
2493 ret = -EINVAL;
2494 break;
2495 }
2496
Mathieu Malaterreb25b7502018-05-16 21:20:20 +02002497 return ret;
Linus Walleij97548572017-09-20 10:02:00 +02002498}
2499
2500#ifdef CONFIG_COMPAT
2501static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2502 unsigned long arg)
2503{
2504 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2505}
2506#endif
2507
2508static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2509{
2510 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2511 struct mmc_rpmb_data, chrdev);
2512
2513 get_device(&rpmb->dev);
2514 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02002515 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02002516
2517 return nonseekable_open(inode, filp);
2518}
2519
2520static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2521{
2522 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2523 struct mmc_rpmb_data, chrdev);
2524
2525 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002526 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02002527
2528 return 0;
2529}
2530
2531static const struct file_operations mmc_rpmb_fileops = {
2532 .release = mmc_rpmb_chrdev_release,
2533 .open = mmc_rpmb_chrdev_open,
2534 .owner = THIS_MODULE,
2535 .llseek = no_llseek,
2536 .unlocked_ioctl = mmc_rpmb_ioctl,
2537#ifdef CONFIG_COMPAT
2538 .compat_ioctl = mmc_rpmb_ioctl_compat,
2539#endif
2540};
2541
Linus Walleij1c87f732017-10-04 11:10:07 +02002542static void mmc_blk_rpmb_device_release(struct device *dev)
2543{
2544 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2545
2546 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2547 kfree(rpmb);
2548}
Linus Walleij97548572017-09-20 10:02:00 +02002549
2550static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2551 struct mmc_blk_data *md,
2552 unsigned int part_index,
2553 sector_t size,
2554 const char *subname)
2555{
2556 int devidx, ret;
2557 char rpmb_name[DISK_NAME_LEN];
2558 char cap_str[10];
2559 struct mmc_rpmb_data *rpmb;
2560
2561 /* This creates the minor number for the RPMB char device */
2562 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2563 if (devidx < 0)
2564 return devidx;
2565
2566 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02002567 if (!rpmb) {
2568 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02002569 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02002570 }
Linus Walleij97548572017-09-20 10:02:00 +02002571
2572 snprintf(rpmb_name, sizeof(rpmb_name),
2573 "mmcblk%u%s", card->host->index, subname ? subname : "");
2574
2575 rpmb->id = devidx;
2576 rpmb->part_index = part_index;
2577 rpmb->dev.init_name = rpmb_name;
2578 rpmb->dev.bus = &mmc_rpmb_bus_type;
2579 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2580 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02002581 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02002582 device_initialize(&rpmb->dev);
2583 dev_set_drvdata(&rpmb->dev, rpmb);
2584 rpmb->md = md;
2585
2586 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2587 rpmb->chrdev.owner = THIS_MODULE;
2588 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2589 if (ret) {
2590 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02002591 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02002592 }
2593
2594 list_add(&rpmb->node, &md->rpmbs);
2595
2596 string_get_size((u64)size, 512, STRING_UNITS_2,
2597 cap_str, sizeof(cap_str));
2598
2599 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2600 rpmb_name, mmc_card_id(card),
2601 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2602 MAJOR(mmc_rpmb_devt), rpmb->id);
2603
2604 return 0;
2605
Linus Walleij1c87f732017-10-04 11:10:07 +02002606out_put_device:
2607 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002608 return ret;
2609}
2610
2611static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02002612
Linus Walleij97548572017-09-20 10:02:00 +02002613{
2614 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002615 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002616}
2617
Namjae Jeone0c368d2011-10-06 23:41:38 +09002618/* MMC Physical partitions consist of two boot partitions and
2619 * up to four general purpose partitions.
2620 * For each partition enabled in EXT_CSD a block device will be allocatedi
2621 * to provide access to the partition.
2622 */
2623
Andrei Warkentin371a6892011-04-11 18:10:25 -05002624static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2625{
Linus Walleij97548572017-09-20 10:02:00 +02002626 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002627
2628 if (!mmc_card_mmc(card))
2629 return 0;
2630
Namjae Jeone0c368d2011-10-06 23:41:38 +09002631 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02002632 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2633 /*
2634 * RPMB partitions does not provide block access, they
2635 * are only accessed using ioctl():s. Thus create
2636 * special RPMB block devices that do not have a
2637 * backing block queue for these.
2638 */
2639 ret = mmc_blk_alloc_rpmb_part(card, md,
2640 card->part[idx].part_cfg,
2641 card->part[idx].size >> 9,
2642 card->part[idx].name);
2643 if (ret)
2644 return ret;
2645 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09002646 ret = mmc_blk_alloc_part(card, md,
2647 card->part[idx].part_cfg,
2648 card->part[idx].size >> 9,
2649 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002650 card->part[idx].name,
2651 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002652 if (ret)
2653 return ret;
2654 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002655 }
2656
Linus Walleij97548572017-09-20 10:02:00 +02002657 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002658}
2659
Andrei Warkentin371a6892011-04-11 18:10:25 -05002660static void mmc_blk_remove_req(struct mmc_blk_data *md)
2661{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002662 struct mmc_card *card;
2663
Andrei Warkentin371a6892011-04-11 18:10:25 -05002664 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002665 /*
2666 * Flush remaining requests and free queues. It
2667 * is freeing the queue that stops new requests
2668 * from being accepted.
2669 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002670 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002671 if (md->disk->flags & GENHD_FL_UP) {
2672 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002673 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2674 card->ext_csd.boot_ro_lockable)
2675 device_remove_file(disk_to_dev(md->disk),
2676 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002677
Andrei Warkentin371a6892011-04-11 18:10:25 -05002678 del_gendisk(md->disk);
2679 }
Shawn Lin57678e52018-03-22 18:56:16 +08002680 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002681 mmc_blk_put(md);
2682 }
2683}
2684
2685static void mmc_blk_remove_parts(struct mmc_card *card,
2686 struct mmc_blk_data *md)
2687{
2688 struct list_head *pos, *q;
2689 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02002690 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002691
Linus Walleij97548572017-09-20 10:02:00 +02002692 /* Remove RPMB partitions */
2693 list_for_each_safe(pos, q, &md->rpmbs) {
2694 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2695 list_del(pos);
2696 mmc_blk_remove_rpmb_part(rpmb);
2697 }
2698 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05002699 list_for_each_safe(pos, q, &md->part) {
2700 part_md = list_entry(pos, struct mmc_blk_data, part);
2701 list_del(pos);
2702 mmc_blk_remove_req(part_md);
2703 }
2704}
2705
2706static int mmc_add_disk(struct mmc_blk_data *md)
2707{
2708 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002709 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002710
Dan Williams307d8e62016-06-20 10:40:44 -07002711 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002712 md->force_ro.show = force_ro_show;
2713 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302714 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002715 md->force_ro.attr.name = "force_ro";
2716 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2717 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2718 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002719 goto force_ro_fail;
2720
2721 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2722 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002723 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002724
2725 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2726 mode = S_IRUGO;
2727 else
2728 mode = S_IRUGO | S_IWUSR;
2729
2730 md->power_ro_lock.show = power_ro_lock_show;
2731 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002732 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002733 md->power_ro_lock.attr.mode = mode;
2734 md->power_ro_lock.attr.name =
2735 "ro_lock_until_next_power_on";
2736 ret = device_create_file(disk_to_dev(md->disk),
2737 &md->power_ro_lock);
2738 if (ret)
2739 goto power_ro_lock_fail;
2740 }
2741 return ret;
2742
2743power_ro_lock_fail:
2744 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2745force_ro_fail:
2746 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002747
2748 return ret;
2749}
2750
Linus Walleij627c3cc2017-08-20 23:39:08 +02002751#ifdef CONFIG_DEBUG_FS
2752
2753static int mmc_dbg_card_status_get(void *data, u64 *val)
2754{
2755 struct mmc_card *card = data;
2756 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2757 struct mmc_queue *mq = &md->queue;
2758 struct request *req;
2759 int ret;
2760
2761 /* Ask the block layer about the card status */
Christoph Hellwigff005a02018-05-09 09:54:05 +02002762 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002763 if (IS_ERR(req))
2764 return PTR_ERR(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002765 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2766 blk_execute_rq(mq->queue, NULL, req, 0);
2767 ret = req_to_mmc_queue_req(req)->drv_op_result;
2768 if (ret >= 0) {
2769 *val = ret;
2770 ret = 0;
2771 }
Adrian Hunter34c089e2017-11-21 15:42:27 +02002772 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002773
2774 return ret;
2775}
2776DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2777 NULL, "%08llx\n");
2778
2779/* That is two digits * 512 + 1 for newline */
2780#define EXT_CSD_STR_LEN 1025
2781
2782static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2783{
2784 struct mmc_card *card = inode->i_private;
2785 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2786 struct mmc_queue *mq = &md->queue;
2787 struct request *req;
2788 char *buf;
2789 ssize_t n = 0;
2790 u8 *ext_csd;
2791 int err, i;
2792
2793 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2794 if (!buf)
2795 return -ENOMEM;
2796
2797 /* Ask the block layer for the EXT CSD */
Christoph Hellwigff005a02018-05-09 09:54:05 +02002798 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002799 if (IS_ERR(req)) {
2800 err = PTR_ERR(req);
2801 goto out_free;
2802 }
Linus Walleij627c3cc2017-08-20 23:39:08 +02002803 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2804 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2805 blk_execute_rq(mq->queue, NULL, req, 0);
2806 err = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +02002807 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002808 if (err) {
2809 pr_err("FAILED %d\n", err);
2810 goto out_free;
2811 }
2812
2813 for (i = 0; i < 512; i++)
2814 n += sprintf(buf + n, "%02x", ext_csd[i]);
2815 n += sprintf(buf + n, "\n");
2816
2817 if (n != EXT_CSD_STR_LEN) {
2818 err = -EINVAL;
Liu, Changcheng0be55572017-12-16 23:15:45 +08002819 kfree(ext_csd);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002820 goto out_free;
2821 }
2822
2823 filp->private_data = buf;
2824 kfree(ext_csd);
2825 return 0;
2826
2827out_free:
2828 kfree(buf);
2829 return err;
2830}
2831
2832static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2833 size_t cnt, loff_t *ppos)
2834{
2835 char *buf = filp->private_data;
2836
2837 return simple_read_from_buffer(ubuf, cnt, ppos,
2838 buf, EXT_CSD_STR_LEN);
2839}
2840
2841static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2842{
2843 kfree(file->private_data);
2844 return 0;
2845}
2846
2847static const struct file_operations mmc_dbg_ext_csd_fops = {
2848 .open = mmc_ext_csd_open,
2849 .read = mmc_ext_csd_read,
2850 .release = mmc_ext_csd_release,
2851 .llseek = default_llseek,
2852};
2853
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002854static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002855{
2856 struct dentry *root;
2857
2858 if (!card->debugfs_root)
2859 return 0;
2860
2861 root = card->debugfs_root;
2862
2863 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002864 md->status_dentry =
2865 debugfs_create_file("status", S_IRUSR, root, card,
2866 &mmc_dbg_card_status_fops);
2867 if (!md->status_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002868 return -EIO;
2869 }
2870
2871 if (mmc_card_mmc(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002872 md->ext_csd_dentry =
2873 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2874 &mmc_dbg_ext_csd_fops);
2875 if (!md->ext_csd_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002876 return -EIO;
2877 }
2878
2879 return 0;
2880}
2881
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002882static void mmc_blk_remove_debugfs(struct mmc_card *card,
2883 struct mmc_blk_data *md)
2884{
2885 if (!card->debugfs_root)
2886 return;
2887
2888 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2889 debugfs_remove(md->status_dentry);
2890 md->status_dentry = NULL;
2891 }
2892
2893 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2894 debugfs_remove(md->ext_csd_dentry);
2895 md->ext_csd_dentry = NULL;
2896 }
2897}
Linus Walleij627c3cc2017-08-20 23:39:08 +02002898
2899#else
2900
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002901static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002902{
2903 return 0;
2904}
2905
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002906static void mmc_blk_remove_debugfs(struct mmc_card *card,
2907 struct mmc_blk_data *md)
2908{
2909}
2910
Linus Walleij627c3cc2017-08-20 23:39:08 +02002911#endif /* CONFIG_DEBUG_FS */
2912
Ulf Hansson96541ba2015-04-14 13:06:12 +02002913static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002915 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002916 char cap_str[10];
2917
Pierre Ossman912490d2005-05-21 10:27:02 +01002918 /*
2919 * Check that the card supports the command class(es) we need.
2920 */
2921 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return -ENODEV;
2923
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002924 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002927 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return PTR_ERR(md);
2929
James Bottomleyb9f28d82015-03-05 18:47:01 -08002930 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002931 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302932 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002934 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Andrei Warkentin371a6892011-04-11 18:10:25 -05002936 if (mmc_blk_alloc_parts(card, md))
2937 goto out;
2938
Ulf Hansson96541ba2015-04-14 13:06:12 +02002939 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002940
Andrei Warkentin371a6892011-04-11 18:10:25 -05002941 if (mmc_add_disk(md))
2942 goto out;
2943
2944 list_for_each_entry(part_md, &md->part, part) {
2945 if (mmc_add_disk(part_md))
2946 goto out;
2947 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002948
Linus Walleij627c3cc2017-08-20 23:39:08 +02002949 /* Add two debugfs entries */
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002950 mmc_blk_add_debugfs(card, md);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002951
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002952 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2953 pm_runtime_use_autosuspend(&card->dev);
2954
2955 /*
2956 * Don't enable runtime PM for SD-combo cards here. Leave that
2957 * decision to be taken during the SDIO init sequence instead.
2958 */
2959 if (card->type != MMC_TYPE_SD_COMBO) {
2960 pm_runtime_set_active(&card->dev);
2961 pm_runtime_enable(&card->dev);
2962 }
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return 0;
2965
2966 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002967 mmc_blk_remove_parts(card, md);
2968 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Ulf Hansson96541ba2015-04-14 13:06:12 +02002972static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002974 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002976 mmc_blk_remove_debugfs(card, md);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002977 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002978 pm_runtime_get_sync(&card->dev);
Shawn Lin65f9e202018-05-17 15:47:42 +08002979 if (md->part_curr != md->part_type) {
2980 mmc_claim_host(card->host);
2981 mmc_blk_part_switch(card, md->part_type);
2982 mmc_release_host(card->host);
2983 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002984 if (card->type != MMC_TYPE_SD_COMBO)
2985 pm_runtime_disable(&card->dev);
2986 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002987 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002988 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Ulf Hansson96541ba2015-04-14 13:06:12 +02002991static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002993 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002994 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 if (md) {
2997 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002998 list_for_each_entry(part_md, &md->part, part) {
2999 mmc_queue_suspend(&part_md->queue);
3000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002 return 0;
3003}
3004
Ulf Hansson96541ba2015-04-14 13:06:12 +02003005static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003006{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003007 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003008}
3009
Ulf Hansson0967edc2014-10-06 11:29:42 +02003010#ifdef CONFIG_PM_SLEEP
3011static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003012{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003013 struct mmc_card *card = mmc_dev_to_card(dev);
3014
3015 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003016}
3017
Ulf Hansson0967edc2014-10-06 11:29:42 +02003018static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003020 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003021 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003024 /*
3025 * Resume involves the card going into idle state,
3026 * so current partition is always the main one.
3027 */
3028 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003030 list_for_each_entry(part_md, &md->part, part) {
3031 mmc_queue_resume(&part_md->queue);
3032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034 return 0;
3035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036#endif
3037
Ulf Hansson0967edc2014-10-06 11:29:42 +02003038static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3039
Ulf Hansson96541ba2015-04-14 13:06:12 +02003040static struct mmc_driver mmc_driver = {
3041 .drv = {
3042 .name = "mmcblk",
3043 .pm = &mmc_blk_pm_ops,
3044 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 .probe = mmc_blk_probe,
3046 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003047 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048};
3049
3050static int __init mmc_blk_init(void)
3051{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003052 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Linus Walleij97548572017-09-20 10:02:00 +02003054 res = bus_register(&mmc_rpmb_bus_type);
3055 if (res < 0) {
3056 pr_err("mmcblk: could not register RPMB bus type\n");
3057 return res;
3058 }
3059 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3060 if (res < 0) {
3061 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3062 goto out_bus_unreg;
3063 }
3064
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003065 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3066 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3067
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003068 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003069
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003070 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3071 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02003072 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003074 res = mmc_register_driver(&mmc_driver);
3075 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02003076 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003078 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02003079
3080out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003081 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02003082out_chrdev_unreg:
3083 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3084out_bus_unreg:
3085 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return res;
3087}
3088
3089static void __exit mmc_blk_exit(void)
3090{
3091 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003092 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02003093 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Alexander Kappnerd0a08522018-03-28 15:18:31 -07003094 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097module_init(mmc_blk_init);
3098module_exit(mmc_blk_exit);
3099
3100MODULE_LICENSE("GPL");
3101MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3102