blob: f609398585865f6b8ae22a842ac8ff49b57f9d08 [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
Trey Ramsay8fee4762012-11-16 09:31:41 -060066#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050069
Luca Porziod3df0462015-11-06 15:12:26 +000070#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090071 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
Linus Walleij97548572017-09-20 10:02:00 +020090static DEFINE_IDA(mmc_rpmb_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Walleij97548572017-09-20 10:02:00 +0200101 struct list_head rpmbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500103 unsigned int flags;
104#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
105#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000108 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200118 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Linus Walleij97548572017-09-20 10:02:00 +0200127/* Device type for RPMB character devices */
128static dev_t mmc_rpmb_devt;
129
130/* Bus type for RPMB character devices */
131static struct bus_type mmc_rpmb_bus_type = {
132 .name = "mmc_rpmb",
133};
134
135/**
136 * struct mmc_rpmb_data - special RPMB device type for these areas
137 * @dev: the device for the RPMB area
138 * @chrdev: character device for the RPMB area
139 * @id: unique device ID number
140 * @part_index: partition index (0 on first)
141 * @md: parent MMC block device
142 * @node: list item, so we can put this device on a list
143 */
144struct mmc_rpmb_data {
145 struct device dev;
146 struct cdev chrdev;
147 int id;
148 unsigned int part_index;
149 struct mmc_blk_data *md;
150 struct list_head node;
151};
152
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400155module_param(perdev_minors, int, 0444);
156MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
157
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200158static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200159 unsigned int part_type);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
162{
163 struct mmc_blk_data *md;
164
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 md = disk->private_data;
167 if (md && md->usage == 0)
168 md = NULL;
169 if (md)
170 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000171 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return md;
174}
175
Andrei Warkentin371a6892011-04-11 18:10:25 -0500176static inline int mmc_get_devidx(struct gendisk *disk)
177{
Colin Cross382c55f2015-10-22 10:00:41 -0700178 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 return devidx;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void mmc_blk_put(struct mmc_blk_data *md)
183{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md->usage--;
186 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500187 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800188 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100189 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(md);
192 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000193 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Johan Rudholmadd710e2011-12-02 08:51:06 +0100196static ssize_t power_ro_lock_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
199 int ret;
200 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
201 struct mmc_card *card = md->queue.card;
202 int locked = 0;
203
204 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
205 locked = 2;
206 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
207 locked = 1;
208
209 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
210
Tomas Winkler9098f842015-07-16 15:50:45 +0200211 mmc_blk_put(md);
212
Johan Rudholmadd710e2011-12-02 08:51:06 +0100213 return ret;
214}
215
216static ssize_t power_ro_lock_store(struct device *dev,
217 struct device_attribute *attr, const char *buf, size_t count)
218{
219 int ret;
220 struct mmc_blk_data *md, *part_md;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200221 struct mmc_queue *mq;
222 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100223 unsigned long set;
224
225 if (kstrtoul(buf, 0, &set))
226 return -EINVAL;
227
228 if (set != 1)
229 return count;
230
231 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200232 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100233
Linus Walleij0493f6f2017-05-19 15:37:30 +0200234 /* Dispatch locking to the block layer */
235 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
236 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
237 blk_execute_rq(mq->queue, NULL, req, 0);
238 ret = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +0200239 blk_put_request(req);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255}
256
Andrei Warkentin371a6892011-04-11 18:10:25 -0500257static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
Baruch Siach0031a982014-09-22 10:12:51 +0300263 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268}
269
270static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284out:
285 mmc_blk_put(md);
286 return ret;
287}
288
Al Viroa5a15612008-03-02 10:33:30 -0500289static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Al Viroa5a15612008-03-02 10:33:30 -0500291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int ret = -ENXIO;
293
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200294 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (md) {
296 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500297 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700299
Al Viroa5a15612008-03-02 10:33:30 -0500300 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700301 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700302 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200305 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return ret;
308}
309
Al Virodb2a1442013-05-05 21:52:57 -0400310static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Al Viroa5a15612008-03-02 10:33:30 -0500312 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200316 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800320mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
John Calixtocb87ea22011-04-26 18:56:29 -0400328struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200332 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400333};
334
335static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
336 struct mmc_ioc_cmd __user *user)
337{
338 struct mmc_blk_ioc_data *idata;
339 int err;
340
yalin wang1ff89502015-11-12 19:27:11 +0800341 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400342 if (!idata) {
343 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400344 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400345 }
346
347 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
348 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400349 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400350 }
351
352 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
353 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
354 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400355 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400356 }
357
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300358 if (!idata->buf_bytes) {
359 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100360 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300361 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100362
yalin wang1ff89502015-11-12 19:27:11 +0800363 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400364 if (!idata->buf) {
365 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400366 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400367 }
368
369 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
370 idata->ic.data_ptr, idata->buf_bytes)) {
371 err = -EFAULT;
372 goto copy_err;
373 }
374
375 return idata;
376
377copy_err:
378 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400379idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400380 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400381out:
John Calixtocb87ea22011-04-26 18:56:29 -0400382 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400383}
384
Jon Huntera5f57742015-09-22 10:27:53 +0100385static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
386 struct mmc_blk_ioc_data *idata)
387{
388 struct mmc_ioc_cmd *ic = &idata->ic;
389
390 if (copy_to_user(&(ic_ptr->response), ic->response,
391 sizeof(ic->response)))
392 return -EFAULT;
393
394 if (!idata->ic.write_flag) {
395 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
396 idata->buf, idata->buf_bytes))
397 return -EFAULT;
398 }
399
400 return 0;
401}
402
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200403static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
404 u32 retries_max)
405{
406 int err;
407 u32 retry_count = 0;
408
409 if (!status || !retries_max)
410 return -EINVAL;
411
412 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200413 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200414 if (err)
415 break;
416
417 if (!R1_STATUS(*status) &&
418 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
419 break; /* RPMB programming operation complete */
420
421 /*
422 * Rechedule to give the MMC device a chance to continue
423 * processing the previous command without being polled too
424 * frequently.
425 */
426 usleep_range(1000, 5000);
427 } while (++retry_count < retries_max);
428
429 if (retry_count == retries_max)
430 err = -EPERM;
431
432 return err;
433}
434
Maya Erez775a9362013-04-18 15:41:55 +0300435static int ioctl_do_sanitize(struct mmc_card *card)
436{
437 int err;
438
Ulf Hanssona2d10862013-12-16 14:37:26 +0100439 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300440 pr_warn("%s: %s - SANITIZE is not supported\n",
441 mmc_hostname(card->host), __func__);
442 err = -EOPNOTSUPP;
443 goto out;
444 }
445
446 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
447 mmc_hostname(card->host), __func__);
448
449 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450 EXT_CSD_SANITIZE_START, 1,
451 MMC_SANITIZE_REQ_TIMEOUT);
452
453 if (err)
454 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
455 mmc_hostname(card->host), __func__, err);
456
457 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
458 __func__);
459out:
460 return err;
461}
462
Jon Huntera5f57742015-09-22 10:27:53 +0100463static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
464 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400465{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900466 struct mmc_command cmd = {};
467 struct mmc_data data = {};
468 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400469 struct scatterlist sg;
470 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200471 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200472 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400473
Jon Huntera5f57742015-09-22 10:27:53 +0100474 if (!card || !md || !idata)
475 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400476
Linus Walleij97548572017-09-20 10:02:00 +0200477 /*
478 * The RPMB accesses comes in from the character device, so we
479 * need to target these explicitly. Else we just target the
480 * partition type for the block device the ioctl() was issued
481 * on.
482 */
483 if (idata->rpmb) {
484 /* Support multiple RPMB partitions */
485 target_part = idata->rpmb->part_index;
486 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
487 } else {
488 target_part = md->part_type;
489 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200490
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100491 cmd.opcode = idata->ic.opcode;
492 cmd.arg = idata->ic.arg;
493 cmd.flags = idata->ic.flags;
494
495 if (idata->buf_bytes) {
496 data.sg = &sg;
497 data.sg_len = 1;
498 data.blksz = idata->ic.blksz;
499 data.blocks = idata->ic.blocks;
500
501 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
502
503 if (idata->ic.write_flag)
504 data.flags = MMC_DATA_WRITE;
505 else
506 data.flags = MMC_DATA_READ;
507
508 /* data.flags must already be set before doing this. */
509 mmc_set_data_timeout(&data, card);
510
511 /* Allow overriding the timeout_ns for empirical tuning. */
512 if (idata->ic.data_timeout_ns)
513 data.timeout_ns = idata->ic.data_timeout_ns;
514
515 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
516 /*
517 * Pretend this is a data transfer and rely on the
518 * host driver to compute timeout. When all host
519 * drivers support cmd.cmd_timeout for R1B, this
520 * can be changed to:
521 *
522 * mrq.data = NULL;
523 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
524 */
525 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
526 }
527
528 mrq.data = &data;
529 }
530
531 mrq.cmd = &cmd;
532
Linus Walleij97548572017-09-20 10:02:00 +0200533 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200534 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100535 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200536
John Calixtocb87ea22011-04-26 18:56:29 -0400537 if (idata->ic.is_acmd) {
538 err = mmc_app_cmd(card->host, card);
539 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100540 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400541 }
542
Linus Walleij97548572017-09-20 10:02:00 +0200543 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200544 err = mmc_set_blockcount(card, data.blocks,
545 idata->ic.write_flag & (1 << 31));
546 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100547 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200548 }
549
Yaniv Gardia82e4842013-06-05 14:13:08 +0300550 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
551 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300552 err = ioctl_do_sanitize(card);
553
554 if (err)
555 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
556 __func__, err);
557
Jon Huntera5f57742015-09-22 10:27:53 +0100558 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300559 }
560
John Calixtocb87ea22011-04-26 18:56:29 -0400561 mmc_wait_for_req(card->host, &mrq);
562
563 if (cmd.error) {
564 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
565 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100566 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400567 }
568 if (data.error) {
569 dev_err(mmc_dev(card->host), "%s: data error %d\n",
570 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100571 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400572 }
573
574 /*
575 * According to the SD specs, some commands require a delay after
576 * issuing the command.
577 */
578 if (idata->ic.postsleep_min_us)
579 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
580
Jon Huntera5f57742015-09-22 10:27:53 +0100581 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400582
Linus Walleij97548572017-09-20 10:02:00 +0200583 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200584 /*
585 * Ensure RPMB command has completed by polling CMD13
586 * "Send Status".
587 */
588 err = ioctl_rpmb_card_status_poll(card, &status, 5);
589 if (err)
590 dev_err(mmc_dev(card->host),
591 "%s: Card Status=0x%08X, error %d\n",
592 __func__, status, err);
593 }
594
Jon Huntera5f57742015-09-22 10:27:53 +0100595 return err;
596}
597
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200598static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200599 struct mmc_ioc_cmd __user *ic_ptr,
600 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100601{
602 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200603 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200604 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100605 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700606 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200607 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100608
609 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
610 if (IS_ERR(idata))
611 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200612 /* This will be NULL on non-RPMB ioctl():s */
613 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100614
Jon Huntera5f57742015-09-22 10:27:53 +0100615 card = md->queue.card;
616 if (IS_ERR(card)) {
617 err = PTR_ERR(card);
618 goto cmd_done;
619 }
620
Linus Walleij614f0382017-05-18 11:29:34 +0200621 /*
622 * Dispatch the ioctl() into the block request queue.
623 */
624 mq = &md->queue;
625 req = blk_get_request(mq->queue,
626 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
627 __GFP_RECLAIM);
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200628 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200629 req_to_mmc_queue_req(req)->drv_op =
630 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200631 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200632 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200633 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200634 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700635 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200636 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100637
John Calixtocb87ea22011-04-26 18:56:29 -0400638cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400639 kfree(idata->buf);
640 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700641 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400642}
643
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200644static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200645 struct mmc_ioc_multi_cmd __user *user,
646 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100647{
648 struct mmc_blk_ioc_data **idata = NULL;
649 struct mmc_ioc_cmd __user *cmds = user->cmds;
650 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200651 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700652 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100653 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200654 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100655
656 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
657 sizeof(num_of_cmds)))
658 return -EFAULT;
659
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200660 if (!num_of_cmds)
661 return 0;
662
Jon Huntera5f57742015-09-22 10:27:53 +0100663 if (num_of_cmds > MMC_IOC_MAX_CMDS)
664 return -EINVAL;
665
666 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
667 if (!idata)
668 return -ENOMEM;
669
670 for (i = 0; i < num_of_cmds; i++) {
671 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
672 if (IS_ERR(idata[i])) {
673 err = PTR_ERR(idata[i]);
674 num_of_cmds = i;
675 goto cmd_err;
676 }
Linus Walleij97548572017-09-20 10:02:00 +0200677 /* This will be NULL on non-RPMB ioctl():s */
678 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100679 }
680
Jon Huntera5f57742015-09-22 10:27:53 +0100681 card = md->queue.card;
682 if (IS_ERR(card)) {
683 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200684 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100685 }
686
Jon Huntera5f57742015-09-22 10:27:53 +0100687
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200688 /*
689 * Dispatch the ioctl()s into the block request queue.
690 */
691 mq = &md->queue;
692 req = blk_get_request(mq->queue,
693 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
694 __GFP_RECLAIM);
Linus Walleij97548572017-09-20 10:02:00 +0200695 req_to_mmc_queue_req(req)->drv_op =
696 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200697 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200698 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
699 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200700 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100701
702 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700703 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100704 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100705
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200706 blk_put_request(req);
707
Jon Huntera5f57742015-09-22 10:27:53 +0100708cmd_err:
709 for (i = 0; i < num_of_cmds; i++) {
710 kfree(idata[i]->buf);
711 kfree(idata[i]);
712 }
713 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700714 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100715}
716
Linus Walleij61fe0e22017-08-20 23:39:09 +0200717static int mmc_blk_check_blkdev(struct block_device *bdev)
718{
719 /*
720 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
721 * whole block device, not on a partition. This prevents overspray
722 * between sibling partitions.
723 */
724 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
725 return -EPERM;
726 return 0;
727}
728
John Calixtocb87ea22011-04-26 18:56:29 -0400729static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
730 unsigned int cmd, unsigned long arg)
731{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200732 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200733 int ret;
734
Jon Huntera5f57742015-09-22 10:27:53 +0100735 switch (cmd) {
736 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200737 ret = mmc_blk_check_blkdev(bdev);
738 if (ret)
739 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200740 md = mmc_blk_get(bdev->bd_disk);
741 if (!md)
742 return -EINVAL;
743 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200744 (struct mmc_ioc_cmd __user *)arg,
745 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200746 mmc_blk_put(md);
747 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100748 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200749 ret = mmc_blk_check_blkdev(bdev);
750 if (ret)
751 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200752 md = mmc_blk_get(bdev->bd_disk);
753 if (!md)
754 return -EINVAL;
755 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200756 (struct mmc_ioc_multi_cmd __user *)arg,
757 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200758 mmc_blk_put(md);
759 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100760 default:
761 return -EINVAL;
762 }
John Calixtocb87ea22011-04-26 18:56:29 -0400763}
764
765#ifdef CONFIG_COMPAT
766static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
767 unsigned int cmd, unsigned long arg)
768{
769 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
770}
771#endif
772
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700773static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500774 .open = mmc_blk_open,
775 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800776 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400778 .ioctl = mmc_blk_ioctl,
779#ifdef CONFIG_COMPAT
780 .compat_ioctl = mmc_blk_compat_ioctl,
781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782};
783
Adrian Hunter025e3d52017-03-13 14:36:39 +0200784static int mmc_blk_part_switch_pre(struct mmc_card *card,
785 unsigned int part_type)
786{
787 int ret = 0;
788
789 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
790 if (card->ext_csd.cmdq_en) {
791 ret = mmc_cmdq_disable(card);
792 if (ret)
793 return ret;
794 }
795 mmc_retune_pause(card->host);
796 }
797
798 return ret;
799}
800
801static int mmc_blk_part_switch_post(struct mmc_card *card,
802 unsigned int part_type)
803{
804 int ret = 0;
805
806 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
807 mmc_retune_unpause(card->host);
808 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
809 ret = mmc_cmdq_enable(card);
810 }
811
812 return ret;
813}
814
Andrei Warkentin371a6892011-04-11 18:10:25 -0500815static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200816 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500817{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200818 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200819 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300820
Linus Walleij1f797ed2017-08-20 23:39:10 +0200821 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500822 return 0;
823
824 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300825 u8 part_config = card->ext_csd.part_config;
826
Linus Walleij1f797ed2017-08-20 23:39:10 +0200827 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200828 if (ret)
829 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300830
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300831 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200832 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500833
834 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300835 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500836 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300837 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200838 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500839 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300840 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300841
842 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300843
Adrian Hunter025e3d52017-03-13 14:36:39 +0200844 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300845 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500846
Linus Walleij1f797ed2017-08-20 23:39:10 +0200847 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200848 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500849}
850
Linus Walleij169f03a2017-02-01 13:47:57 +0100851static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700852{
853 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100854 u32 result;
855 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700856
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900857 struct mmc_request mrq = {};
858 struct mmc_command cmd = {};
859 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700860
861 struct scatterlist sg;
862
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700863 cmd.opcode = MMC_APP_CMD;
864 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700865 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700866
867 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700868 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100869 return err;
David Brownell7213d172007-08-08 09:10:23 -0700870 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100871 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700872
873 memset(&cmd, 0, sizeof(struct mmc_command));
874
875 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
876 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700877 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700878
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700879 data.blksz = 4;
880 data.blocks = 1;
881 data.flags = MMC_DATA_READ;
882 data.sg = &sg;
883 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530884 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700885
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700886 mrq.cmd = &cmd;
887 mrq.data = &data;
888
Ben Dooks051913d2009-06-08 23:33:57 +0100889 blocks = kmalloc(4, GFP_KERNEL);
890 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100891 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100892
893 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700894
895 mmc_wait_for_req(card->host, &mrq);
896
Ben Dooks051913d2009-06-08 23:33:57 +0100897 result = ntohl(*blocks);
898 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700899
Ben Dooks051913d2009-06-08 23:33:57 +0100900 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100901 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100902
Linus Walleij169f03a2017-02-01 13:47:57 +0100903 *written_blocks = result;
904
905 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700906}
907
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100908static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100909 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100910{
911 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
912 int err = 0;
913 u32 status;
914
915 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200916 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100917 if (err) {
918 pr_err("%s: error %d requesting status\n",
919 req->rq_disk->disk_name, err);
920 return err;
921 }
922
923 if (status & R1_ERROR) {
924 pr_err("%s: %s: error sending status cmd, status %#x\n",
925 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100926 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100927 }
928
Ulf Hansson95a91292014-01-29 13:11:27 +0100929 /* We may rely on the host hw to handle busy detection.*/
930 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
931 hw_busy_detect)
932 break;
933
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100934 /*
935 * Timeout if the device never becomes ready for data and never
936 * leaves the program state.
937 */
938 if (time_after(jiffies, timeout)) {
939 pr_err("%s: Card stuck in programming state! %s %s\n",
940 mmc_hostname(card->host),
941 req->rq_disk->disk_name, __func__);
942 return -ETIMEDOUT;
943 }
944
945 /*
946 * Some cards mishandle the status bits,
947 * so make sure to check both the busy
948 * indication and the card state.
949 */
950 } while (!(status & R1_READY_FOR_DATA) ||
951 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
952
953 return err;
954}
955
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100956static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100957 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100958{
959 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900960 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100961 int err;
962 bool use_r1b_resp = rq_data_dir(req) == WRITE;
963
964 /*
965 * Normally we use R1B responses for WRITE, but in cases where the host
966 * has specified a max_busy_timeout we need to validate it. A failure
967 * means we need to prevent the host from doing hw busy detection, which
968 * is done by converting to a R1 response instead.
969 */
970 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
971 use_r1b_resp = false;
972
973 cmd.opcode = MMC_STOP_TRANSMISSION;
974 if (use_r1b_resp) {
975 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
976 cmd.busy_timeout = timeout_ms;
977 } else {
978 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
979 }
980
981 err = mmc_wait_for_cmd(host, &cmd, 5);
982 if (err)
983 return err;
984
985 *stop_status = cmd.resp[0];
986
987 /* No need to check card status in case of READ. */
988 if (rq_data_dir(req) == READ)
989 return 0;
990
991 if (!mmc_host_is_spi(host) &&
992 (*stop_status & R1_ERROR)) {
993 pr_err("%s: %s: general error sending stop command, resp %#x\n",
994 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100995 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100996 }
997
998 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
999}
1000
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301001#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001002#define ERR_RETRY 2
1003#define ERR_ABORT 1
1004#define ERR_CONTINUE 0
1005
1006static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1007 bool status_valid, u32 status)
1008{
1009 switch (error) {
1010 case -EILSEQ:
1011 /* response crc error, retry the r/w cmd */
1012 pr_err("%s: %s sending %s command, card status %#x\n",
1013 req->rq_disk->disk_name, "response CRC error",
1014 name, status);
1015 return ERR_RETRY;
1016
1017 case -ETIMEDOUT:
1018 pr_err("%s: %s sending %s command, card status %#x\n",
1019 req->rq_disk->disk_name, "timed out", name, status);
1020
1021 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301022 if (!status_valid) {
1023 pr_err("%s: status not valid, retrying timeout\n",
1024 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001025 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301026 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001027
1028 /*
1029 * If it was a r/w cmd crc error, or illegal command
1030 * (eg, issued in wrong state) then retry - we should
1031 * have corrected the state problem above.
1032 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301033 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1034 pr_err("%s: command error, retrying timeout\n",
1035 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001036 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301037 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001038
1039 /* Otherwise abort the command */
1040 return ERR_ABORT;
1041
1042 default:
1043 /* We don't understand the error code the driver gave us */
1044 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1045 req->rq_disk->disk_name, error, status);
1046 return ERR_ABORT;
1047 }
1048}
1049
1050/*
1051 * Initial r/w and stop cmd error recovery.
1052 * We don't know whether the card received the r/w cmd or not, so try to
1053 * restore things back to a sane state. Essentially, we do this as follows:
1054 * - Obtain card status. If the first attempt to obtain card status fails,
1055 * the status word will reflect the failed status cmd, not the failed
1056 * r/w cmd. If we fail to obtain card status, it suggests we can no
1057 * longer communicate with the card.
1058 * - Check the card state. If the card received the cmd but there was a
1059 * transient problem with the response, it might still be in a data transfer
1060 * mode. Try to send it a stop command. If this fails, we can't recover.
1061 * - If the r/w cmd failed due to a response CRC error, it was probably
1062 * transient, so retry the cmd.
1063 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1064 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1065 * illegal cmd, retry.
1066 * Otherwise we don't understand what happened, so abort.
1067 */
1068static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001069 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001070{
1071 bool prev_cmd_status_valid = true;
1072 u32 status, stop_status = 0;
1073 int err, retry;
1074
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301075 if (mmc_card_removed(card))
1076 return ERR_NOMEDIUM;
1077
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001078 /*
1079 * Try to get card status which indicates both the card state
1080 * and why there was no response. If the first attempt fails,
1081 * we can't be sure the returned status is for the r/w command.
1082 */
1083 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001084 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001085 if (!err)
1086 break;
1087
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001088 /* Re-tune if needed */
1089 mmc_retune_recheck(card->host);
1090
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001091 prev_cmd_status_valid = false;
1092 pr_err("%s: error %d sending status command, %sing\n",
1093 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1094 }
1095
1096 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301097 if (err) {
1098 /* Check if the card is removed */
1099 if (mmc_detect_card_removed(card->host))
1100 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001101 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301102 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001103
Adrian Hunter67716322011-08-29 16:42:15 +03001104 /* Flag ECC errors */
1105 if ((status & R1_CARD_ECC_FAILED) ||
1106 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1107 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001108 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001109
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001110 /* Flag General errors */
1111 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1112 if ((status & R1_ERROR) ||
1113 (brq->stop.resp[0] & R1_ERROR)) {
1114 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1115 req->rq_disk->disk_name, __func__,
1116 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001117 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001118 }
1119
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001120 /*
1121 * Check the current card state. If it is in some data transfer
1122 * mode, tell it to stop (and hopefully transition back to TRAN.)
1123 */
1124 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1125 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001126 err = send_stop(card,
1127 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1128 req, gen_err, &stop_status);
1129 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001130 pr_err("%s: error %d sending stop command\n",
1131 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001132 /*
1133 * If the stop cmd also timed out, the card is probably
1134 * not present, so abort. Other errors are bad news too.
1135 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001136 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001137 }
1138
Adrian Hunter67716322011-08-29 16:42:15 +03001139 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001140 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001141 }
1142
1143 /* Check for set block count errors */
1144 if (brq->sbc.error)
1145 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1146 prev_cmd_status_valid, status);
1147
1148 /* Check for r/w command errors */
1149 if (brq->cmd.error)
1150 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1151 prev_cmd_status_valid, status);
1152
Adrian Hunter67716322011-08-29 16:42:15 +03001153 /* Data errors */
1154 if (!brq->stop.error)
1155 return ERR_CONTINUE;
1156
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001157 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001158 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001159 req->rq_disk->disk_name, brq->stop.error,
1160 brq->cmd.resp[0], status);
1161
1162 /*
1163 * Subsitute in our own stop status as this will give the error
1164 * state which happened during the execution of the r/w command.
1165 */
1166 if (stop_status) {
1167 brq->stop.resp[0] = stop_status;
1168 brq->stop.error = 0;
1169 }
1170 return ERR_CONTINUE;
1171}
1172
Adrian Hunter67716322011-08-29 16:42:15 +03001173static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1174 int type)
1175{
1176 int err;
1177
1178 if (md->reset_done & type)
1179 return -EEXIST;
1180
1181 md->reset_done |= type;
1182 err = mmc_hw_reset(host);
1183 /* Ensure we switch back to the correct partition */
1184 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001185 struct mmc_blk_data *main_md =
1186 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001187 int part_err;
1188
1189 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001190 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001191 if (part_err) {
1192 /*
1193 * We have failed to get back into the correct
1194 * partition, so we need to abort the whole request.
1195 */
1196 return -ENODEV;
1197 }
1198 }
1199 return err;
1200}
1201
1202static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1203{
1204 md->reset_done &= ~type;
1205}
1206
Linus Walleij5ec12392017-05-19 15:37:29 +02001207/*
1208 * The non-block commands come back from the block layer after it queued it and
1209 * processed it with all other requests and then they get issued in this
1210 * function.
1211 */
1212static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1213{
1214 struct mmc_queue_req *mq_rq;
1215 struct mmc_card *card = mq->card;
1216 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001217 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001218 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001219 u8 **ext_csd;
1220 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001221 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001222 int i;
1223
1224 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001225 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001226
1227 switch (mq_rq->drv_op) {
1228 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001229 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001230 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001231 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001232 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001233 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001234 break;
1235 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001236 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001237 if (rpmb_ioctl)
1238 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001239 break;
1240 case MMC_DRV_OP_BOOT_WP:
1241 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1242 card->ext_csd.boot_ro_lock |
1243 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1244 card->ext_csd.part_time);
1245 if (ret)
1246 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1247 md->disk->disk_name, ret);
1248 else
1249 card->ext_csd.boot_ro_lock |=
1250 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001251 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001252 case MMC_DRV_OP_GET_CARD_STATUS:
1253 ret = mmc_send_status(card, &status);
1254 if (!ret)
1255 ret = status;
1256 break;
1257 case MMC_DRV_OP_GET_EXT_CSD:
1258 ext_csd = mq_rq->drv_op_data;
1259 ret = mmc_get_ext_csd(card, ext_csd);
1260 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001261 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001262 pr_err("%s: unknown driver specific operation\n",
1263 md->disk->disk_name);
1264 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001265 break;
1266 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001267 mq_rq->drv_op_result = ret;
Adrian Huntera7c17d82017-08-25 15:43:45 +03001268 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001269}
1270
Linus Walleijdf061582017-01-24 11:17:57 +01001271static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001272{
Linus Walleij7db30282016-11-18 13:36:15 +01001273 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001274 struct mmc_card *card = md->queue.card;
1275 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001276 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001277 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001278
Adrian Hunterbd788c92010-08-11 14:17:47 -07001279 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001280 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001281 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001282 }
1283
1284 from = blk_rq_pos(req);
1285 nr = blk_rq_sectors(req);
1286
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001287 if (mmc_can_discard(card))
1288 arg = MMC_DISCARD_ARG;
1289 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001290 arg = MMC_TRIM_ARG;
1291 else
1292 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001293 do {
1294 err = 0;
1295 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1296 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1297 INAND_CMD38_ARG_EXT_CSD,
1298 arg == MMC_TRIM_ARG ?
1299 INAND_CMD38_ARG_TRIM :
1300 INAND_CMD38_ARG_ERASE,
1301 0);
1302 }
1303 if (!err)
1304 err = mmc_erase(card, from, nr, arg);
1305 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001306 if (err)
1307 status = BLK_STS_IOERR;
1308 else
Adrian Hunter67716322011-08-29 16:42:15 +03001309 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001310fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001311 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001312}
1313
Linus Walleijdf061582017-01-24 11:17:57 +01001314static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001315 struct request *req)
1316{
Linus Walleij7db30282016-11-18 13:36:15 +01001317 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001318 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001319 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001320 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001321 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001322
Maya Erez775a9362013-04-18 15:41:55 +03001323 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001324 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001325 goto out;
1326 }
1327
1328 from = blk_rq_pos(req);
1329 nr = blk_rq_sectors(req);
1330
Maya Erez775a9362013-04-18 15:41:55 +03001331 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1332 arg = MMC_SECURE_TRIM1_ARG;
1333 else
1334 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001335
Adrian Hunter67716322011-08-29 16:42:15 +03001336retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001337 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1338 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1339 INAND_CMD38_ARG_EXT_CSD,
1340 arg == MMC_SECURE_TRIM1_ARG ?
1341 INAND_CMD38_ARG_SECTRIM1 :
1342 INAND_CMD38_ARG_SECERASE,
1343 0);
1344 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001345 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001346 }
Adrian Hunter28302812012-04-05 14:45:48 +03001347
Adrian Hunter49804542010-08-11 14:17:50 -07001348 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001349 if (err == -EIO)
1350 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001351 if (err) {
1352 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001353 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001354 }
Adrian Hunter28302812012-04-05 14:45:48 +03001355
1356 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001357 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1358 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1359 INAND_CMD38_ARG_EXT_CSD,
1360 INAND_CMD38_ARG_SECTRIM2,
1361 0);
1362 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001363 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001364 }
Adrian Hunter28302812012-04-05 14:45:48 +03001365
Adrian Hunter49804542010-08-11 14:17:50 -07001366 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001367 if (err == -EIO)
1368 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001369 if (err) {
1370 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001371 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001372 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001373 }
Adrian Hunter28302812012-04-05 14:45:48 +03001374
Adrian Hunter28302812012-04-05 14:45:48 +03001375out_retry:
1376 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001377 goto retry;
1378 if (!err)
1379 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001380out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001381 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001382}
1383
Linus Walleijdf061582017-01-24 11:17:57 +01001384static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001385{
Linus Walleij7db30282016-11-18 13:36:15 +01001386 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001387 struct mmc_card *card = md->queue.card;
1388 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001389
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001390 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001391 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001392}
1393
1394/*
1395 * Reformat current write as a reliable write, supporting
1396 * both legacy and the enhanced reliable write MMC cards.
1397 * In each transfer we'll handle only as much as a single
1398 * reliable write can handle, thus finish the request in
1399 * partial completions.
1400 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001401static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1402 struct mmc_card *card,
1403 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001404{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001405 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1406 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001407 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001408 brq->data.blocks = 1;
1409
1410 if (brq->data.blocks > card->ext_csd.rel_sectors)
1411 brq->data.blocks = card->ext_csd.rel_sectors;
1412 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1413 brq->data.blocks = 1;
1414 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001415}
1416
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001417#define CMD_ERRORS \
1418 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1419 R1_ADDRESS_ERROR | /* Misaligned address */ \
1420 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1421 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001422 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001423 R1_CC_ERROR | /* Card controller error */ \
1424 R1_ERROR) /* General/unknown error */
1425
Shawn Lind83c2db2017-08-18 09:16:08 +08001426static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001427{
Shawn Lind83c2db2017-08-18 09:16:08 +08001428 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001429
Shawn Lind83c2db2017-08-18 09:16:08 +08001430 /*
1431 * Per the SD specification(physical layer version 4.10)[1],
1432 * section 4.3.3, it explicitly states that "When the last
1433 * block of user area is read using CMD18, the host should
1434 * ignore OUT_OF_RANGE error that may occur even the sequence
1435 * is correct". And JESD84-B51 for eMMC also has a similar
1436 * statement on section 6.8.3.
1437 *
1438 * Multiple block read/write could be done by either predefined
1439 * method, namely CMD23, or open-ending mode. For open-ending mode,
1440 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1441 *
1442 * However the spec[1] doesn't tell us whether we should also
1443 * ignore that for predefined method. But per the spec[1], section
1444 * 4.15 Set Block Count Command, it says"If illegal block count
1445 * is set, out of range error will be indicated during read/write
1446 * operation (For example, data transfer is stopped at user area
1447 * boundary)." In another word, we could expect a out of range error
1448 * in the response for the following CMD18/25. And if argument of
1449 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1450 * we could also expect to get a -ETIMEDOUT or any error number from
1451 * the host drivers due to missing data response(for write)/data(for
1452 * read), as the cards will stop the data transfer by itself per the
1453 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1454 */
1455
1456 if (!brq->stop.error) {
1457 bool oor_with_open_end;
1458 /* If there is no error yet, check R1 response */
1459
1460 val = brq->stop.resp[0] & CMD_ERRORS;
1461 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1462
1463 if (val && !oor_with_open_end)
1464 brq->stop.error = -EIO;
1465 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001466}
1467
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001468static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1469 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001470{
Per Forlinee8a43a2011-07-01 18:55:33 +02001471 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001472 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001473 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001474 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001475 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001476 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001477 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001478
1479 /*
1480 * sbc.error indicates a problem with the set block count
1481 * command. No data will have been transferred.
1482 *
1483 * cmd.error indicates a problem with the r/w command. No
1484 * data will have been transferred.
1485 *
1486 * stop.error indicates a problem with the stop command. Data
1487 * may have been transferred, or may still be transferring.
1488 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001489
1490 mmc_blk_eval_resp_error(brq);
1491
1492 if (brq->sbc.error || brq->cmd.error ||
1493 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001494 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001495 case ERR_RETRY:
1496 return MMC_BLK_RETRY;
1497 case ERR_ABORT:
1498 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301499 case ERR_NOMEDIUM:
1500 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001501 case ERR_CONTINUE:
1502 break;
1503 }
1504 }
1505
1506 /*
1507 * Check for errors relating to the execution of the
1508 * initial command - such as address errors. No data
1509 * has been transferred.
1510 */
1511 if (brq->cmd.resp[0] & CMD_ERRORS) {
1512 pr_err("%s: r/w command failed, status = %#x\n",
1513 req->rq_disk->disk_name, brq->cmd.resp[0]);
1514 return MMC_BLK_ABORT;
1515 }
1516
1517 /*
1518 * Everything else is either success, or a data error of some
1519 * kind. If it was a write, we may have transitioned to
1520 * program mode, which we have to wait for it to complete.
1521 */
1522 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001523 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001524
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001525 /* Check stop command response */
1526 if (brq->stop.resp[0] & R1_ERROR) {
1527 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1528 req->rq_disk->disk_name, __func__,
1529 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001530 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001531 }
1532
Ulf Hansson95a91292014-01-29 13:11:27 +01001533 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1534 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001535 if (err)
1536 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001537 }
1538
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001539 /* if general error occurs, retry the write operation. */
1540 if (gen_err) {
1541 pr_warn("%s: retrying write for general error\n",
1542 req->rq_disk->disk_name);
1543 return MMC_BLK_RETRY;
1544 }
1545
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001546 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1547 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001548 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001549 pr_debug("%s: retrying because a re-tune was needed\n",
1550 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001551 brq->retune_retry_done = 1;
1552 return MMC_BLK_RETRY;
1553 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001554 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001555 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001556 (unsigned)blk_rq_pos(req),
1557 (unsigned)blk_rq_sectors(req),
1558 brq->cmd.resp[0], brq->stop.resp[0]);
1559
1560 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001561 if (ecc_err)
1562 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001563 return MMC_BLK_DATA_ERR;
1564 } else {
1565 return MMC_BLK_CMD_ERR;
1566 }
1567 }
1568
Adrian Hunter67716322011-08-29 16:42:15 +03001569 if (!brq->data.bytes_xfered)
1570 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001571
Adrian Hunter67716322011-08-29 16:42:15 +03001572 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1573 return MMC_BLK_PARTIAL;
1574
1575 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001576}
1577
Adrian Hunterca5717f2017-03-13 14:36:41 +02001578static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001579 int disable_multi, bool *do_rel_wr_p,
1580 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001582 struct mmc_blk_data *md = mq->blkdata;
1583 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001584 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001585 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001586 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001588 /*
1589 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001590 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001591 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001592 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1593 rq_data_dir(req) == WRITE &&
1594 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001595
Per Forlin54d49d72011-07-01 18:55:29 +02001596 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001597
Per Forlin54d49d72011-07-01 18:55:29 +02001598 brq->mrq.data = &brq->data;
Adrian Hunter93482b32017-09-22 15:36:56 +03001599 brq->mrq.tag = req->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Per Forlin54d49d72011-07-01 18:55:29 +02001601 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1602 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001603
1604 if (rq_data_dir(req) == READ) {
1605 brq->data.flags = MMC_DATA_READ;
1606 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1607 } else {
1608 brq->data.flags = MMC_DATA_WRITE;
1609 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1610 }
1611
1612 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001613 brq->data.blocks = blk_rq_sectors(req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001614 brq->data.blk_addr = blk_rq_pos(req);
1615
1616 /*
1617 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1618 * The eMMC will give "high" priority tasks priority over "simple"
1619 * priority tasks. Here we always set "simple" priority by not setting
1620 * MMC_DATA_PRIO.
1621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Per Forlin54d49d72011-07-01 18:55:29 +02001623 /*
1624 * The block layer doesn't support all sector count
1625 * restrictions, so we need to be prepared for too big
1626 * requests.
1627 */
1628 if (brq->data.blocks > card->host->max_blk_count)
1629 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001631 if (brq->data.blocks > 1) {
1632 /*
1633 * After a read error, we redo the request one sector
1634 * at a time in order to accurately determine which
1635 * sectors can be read successfully.
1636 */
1637 if (disable_multi)
1638 brq->data.blocks = 1;
1639
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001640 /*
1641 * Some controllers have HW issues while operating
1642 * in multiple I/O mode
1643 */
1644 if (card->host->ops->multi_io_quirk)
1645 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1646 (rq_data_dir(req) == READ) ?
1647 MMC_DATA_READ : MMC_DATA_WRITE,
1648 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001649 }
Per Forlin54d49d72011-07-01 18:55:29 +02001650
Adrian Hunter93482b32017-09-22 15:36:56 +03001651 if (do_rel_wr) {
Adrian Hunterca5717f2017-03-13 14:36:41 +02001652 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001653 brq->data.flags |= MMC_DATA_REL_WR;
1654 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001655
1656 /*
1657 * Data tag is used only during writing meta data to speed
1658 * up write and any subsequent read of this meta data
1659 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001660 do_data_tag = card->ext_csd.data_tag_unit_size &&
1661 (req->cmd_flags & REQ_META) &&
1662 (rq_data_dir(req) == WRITE) &&
1663 ((brq->data.blocks * brq->data.blksz) >=
1664 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001665
Adrian Hunter93482b32017-09-22 15:36:56 +03001666 if (do_data_tag)
1667 brq->data.flags |= MMC_DATA_DAT_TAG;
1668
Adrian Hunterca5717f2017-03-13 14:36:41 +02001669 mmc_set_data_timeout(&brq->data, card);
1670
1671 brq->data.sg = mqrq->sg;
1672 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1673
1674 /*
1675 * Adjust the sg list so it is the same size as the
1676 * request.
1677 */
1678 if (brq->data.blocks != blk_rq_sectors(req)) {
1679 int i, data_size = brq->data.blocks << 9;
1680 struct scatterlist *sg;
1681
1682 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1683 data_size -= sg->length;
1684 if (data_size <= 0) {
1685 sg->length += data_size;
1686 i++;
1687 break;
1688 }
1689 }
1690 brq->data.sg_len = i;
1691 }
1692
1693 mqrq->areq.mrq = &brq->mrq;
Adrian Hunterd3377c02017-09-22 15:36:55 +03001694
1695 if (do_rel_wr_p)
1696 *do_rel_wr_p = do_rel_wr;
1697
1698 if (do_data_tag_p)
1699 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001700}
1701
1702static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1703 struct mmc_card *card,
1704 int disable_multi,
1705 struct mmc_queue *mq)
1706{
1707 u32 readcmd, writecmd;
1708 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001709 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001710 struct mmc_blk_data *md = mq->blkdata;
1711 bool do_rel_wr, do_data_tag;
1712
1713 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1714
1715 brq->mrq.cmd = &brq->cmd;
1716
1717 brq->cmd.arg = blk_rq_pos(req);
1718 if (!mmc_card_blockaddr(card))
1719 brq->cmd.arg <<= 9;
1720 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1721
Per Forlin54d49d72011-07-01 18:55:29 +02001722 if (brq->data.blocks > 1 || do_rel_wr) {
1723 /* SPI multiblock writes terminate using a special
1724 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001725 */
Per Forlin54d49d72011-07-01 18:55:29 +02001726 if (!mmc_host_is_spi(card->host) ||
1727 rq_data_dir(req) == READ)
1728 brq->mrq.stop = &brq->stop;
1729 readcmd = MMC_READ_MULTIPLE_BLOCK;
1730 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1731 } else {
1732 brq->mrq.stop = NULL;
1733 readcmd = MMC_READ_SINGLE_BLOCK;
1734 writecmd = MMC_WRITE_BLOCK;
1735 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001736 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301737
1738 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001739 * Pre-defined multi-block transfers are preferable to
1740 * open ended-ones (and necessary for reliable writes).
1741 * However, it is not sufficient to just send CMD23,
1742 * and avoid the final CMD12, as on an error condition
1743 * CMD12 (stop) needs to be sent anyway. This, coupled
1744 * with Auto-CMD23 enhancements provided by some
1745 * hosts, means that the complexity of dealing
1746 * with this is best left to the host. If CMD23 is
1747 * supported by card and host, we'll fill sbc in and let
1748 * the host deal with handling it correctly. This means
1749 * that for hosts that don't expose MMC_CAP_CMD23, no
1750 * change of behavior will be observed.
1751 *
1752 * N.B: Some MMC cards experience perf degradation.
1753 * We'll avoid using CMD23-bounded multiblock writes for
1754 * these, while retaining features like reliable writes.
1755 */
Saugata Das42659002011-12-21 13:09:17 +05301756 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1757 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1758 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001759 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1760 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301761 (do_rel_wr ? (1 << 31) : 0) |
1762 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001763 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1764 brq->mrq.sbc = &brq->sbc;
1765 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001766
Linus Walleij74f5ba32017-02-01 13:47:55 +01001767 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Linus Walleij0e65f102017-02-01 13:47:58 +01001770static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1771 struct mmc_blk_request *brq, struct request *req,
1772 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001773{
Linus Walleij0e65f102017-02-01 13:47:58 +01001774 bool req_pending;
1775
Adrian Hunter67716322011-08-29 16:42:15 +03001776 /*
1777 * If this is an SD card and we're writing, we can first
1778 * mark the known good sectors as ok.
1779 *
1780 * If the card is not SD, we can still ok written sectors
1781 * as reported by the controller (which might be less than
1782 * the real number of written sectors, but never more).
1783 */
1784 if (mmc_card_sd(card)) {
1785 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001786 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001787
Linus Walleij169f03a2017-02-01 13:47:57 +01001788 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001789 if (err)
1790 req_pending = old_req_pending;
1791 else
Adrian Huntera7c17d82017-08-25 15:43:45 +03001792 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001793 } else {
Adrian Huntera7c17d82017-08-25 15:43:45 +03001794 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001795 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001796 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001797}
1798
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001799static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1800 struct request *req,
1801 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001802{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001803 if (mmc_card_removed(card))
1804 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001805 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001806 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001807}
1808
Linus Walleijb2928e12017-02-01 13:47:54 +01001809/**
1810 * mmc_blk_rw_try_restart() - tries to restart the current async request
1811 * @mq: the queue with the card and host to restart
1812 * @req: a new request that want to be started after the current one
1813 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001814static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1815 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001816{
1817 if (!req)
1818 return;
1819
Linus Walleijb2928e12017-02-01 13:47:54 +01001820 /*
1821 * If the card was removed, just cancel everything and return.
1822 */
1823 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001824 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001825 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001826 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001827 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001828 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001829 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001830 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1831 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001832}
1833
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001834static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001835{
Linus Walleij7db30282016-11-18 13:36:15 +01001836 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001837 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001838 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001839 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001840 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001841 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001842 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001843 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001844 struct mmc_async_req *new_areq;
1845 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001846 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001847
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001848 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001849 mqrq_cur = req_to_mmc_queue_req(new_req);
1850 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001851 }
1852
1853 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001854 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001855
1856 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001857 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301858 /*
1859 * When 4KB native sector is enabled, only 8 blocks
1860 * multiple read or write is allowed
1861 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001862 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001863 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301864 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001865 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001866 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001867 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301868 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001869
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001870 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1871 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001872 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001873 new_areq = NULL;
1874
Linus Walleijc3399ef2017-02-01 13:47:53 +01001875 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001876 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001877 /*
1878 * We have just put the first request into the pipeline
1879 * and there is nothing more to do until it is
1880 * complete.
1881 */
Linus Walleijdf061582017-01-24 11:17:57 +01001882 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001883 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001884
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001885 /*
1886 * An asynchronous request has been completed and we proceed
1887 * to handle the result of it.
1888 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001889 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001890 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001891 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001892 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001893
Per Forlind78d4a8a2011-07-01 18:55:30 +02001894 switch (status) {
1895 case MMC_BLK_SUCCESS:
1896 case MMC_BLK_PARTIAL:
1897 /*
1898 * A block was successfully transferred.
1899 */
Adrian Hunter67716322011-08-29 16:42:15 +03001900 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001901
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001902 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001903 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001904 /*
1905 * If the blk_end_request function returns non-zero even
1906 * though all data has been transferred and no errors
1907 * were returned by the host controller, it's a bug.
1908 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001909 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301910 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001911 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001912 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001913 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001914 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001915 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001916 break;
1917 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001918 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001919 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001920 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001921 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1922 else
Linus Walleij304419d2017-05-18 11:29:32 +02001923 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001924 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001925 return;
1926 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001927 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001928 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001929 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001930 return;
1931 }
Ding Wang29535f72015-05-18 20:14:15 +08001932 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001933 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001934 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001935 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001936 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001937 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001938 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001939 if (!mmc_blk_reset(md, card->host, type))
1940 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001941 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001942 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001943 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001944 case MMC_BLK_DATA_ERR: {
1945 int err;
1946
1947 err = mmc_blk_reset(md, card->host, type);
1948 if (!err)
1949 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001950 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001951 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001952 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001953 return;
1954 }
Adrian Hunter67716322011-08-29 16:42:15 +03001955 /* Fall through */
1956 }
1957 case MMC_BLK_ECC_ERR:
1958 if (brq->data.blocks > 1) {
1959 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001960 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001961 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001962 disable_multi = 1;
1963 break;
1964 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001965 /*
1966 * After an error, we redo I/O one sector at a
1967 * time, so we only reach here after trying to
1968 * read a single sector.
1969 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001970 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001971 brq->data.blksz);
1972 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001973 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001974 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001975 return;
1976 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001977 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301978 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001979 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001980 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001981 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001982 default:
1983 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001984 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001985 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001986 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001987 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001988 }
1989
Linus Walleij0e65f102017-02-01 13:47:58 +01001990 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001991 /*
1992 * In case of a incomplete request
1993 * prepare it again and resend.
1994 */
1995 mmc_blk_rw_rq_prep(mq_rq, card,
1996 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001997 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001998 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001999 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002000 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002001 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002002
Linus Walleij304419d2017-05-18 11:29:32 +02002003 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Linus Walleijdf061582017-01-24 11:17:57 +01002006void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002007{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002008 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01002009 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002010 struct mmc_card *card = md->queue.card;
2011
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002012 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02002013 /* claim host only for the first request */
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002014 mmc_get_card(card, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02002015
Linus Walleij1f797ed2017-08-20 23:39:10 +02002016 ret = mmc_blk_part_switch(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002017 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002018 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002019 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002020 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021 goto out;
2022 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002023
Linus Walleij614f0382017-05-18 11:29:34 +02002024 if (req) {
2025 switch (req_op(req)) {
2026 case REQ_OP_DRV_IN:
2027 case REQ_OP_DRV_OUT:
2028 /*
2029 * Complete ongoing async transfer before issuing
2030 * ioctl()s
2031 */
2032 if (mq->qcnt)
2033 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02002034 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02002035 break;
2036 case REQ_OP_DISCARD:
2037 /*
2038 * Complete ongoing async transfer before issuing
2039 * discard.
2040 */
2041 if (mq->qcnt)
2042 mmc_blk_issue_rw_rq(mq, NULL);
2043 mmc_blk_issue_discard_rq(mq, req);
2044 break;
2045 case REQ_OP_SECURE_ERASE:
2046 /*
2047 * Complete ongoing async transfer before issuing
2048 * secure erase.
2049 */
2050 if (mq->qcnt)
2051 mmc_blk_issue_rw_rq(mq, NULL);
2052 mmc_blk_issue_secdiscard_rq(mq, req);
2053 break;
2054 case REQ_OP_FLUSH:
2055 /*
2056 * Complete ongoing async transfer before issuing
2057 * flush.
2058 */
2059 if (mq->qcnt)
2060 mmc_blk_issue_rw_rq(mq, NULL);
2061 mmc_blk_issue_flush(mq, req);
2062 break;
2063 default:
2064 /* Normal request, just issue it */
2065 mmc_blk_issue_rw_rq(mq, req);
2066 card->host->context_info.is_waiting_last_req = false;
2067 break;
Wu Fengguang73222382017-05-23 05:11:33 +08002068 }
Adrian Hunter49804542010-08-11 14:17:50 -07002069 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002070 /* No request, flushing the pipeline with NULL */
2071 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002072 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002073 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002074
Andrei Warkentin371a6892011-04-11 18:10:25 -05002075out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002076 if (!mq->qcnt)
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002077 mmc_put_card(card, NULL);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Russell Kinga6f6c962006-01-03 22:38:44 +00002080static inline int mmc_blk_readonly(struct mmc_card *card)
2081{
2082 return mmc_card_readonly(card) ||
2083 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2084}
2085
Andrei Warkentin371a6892011-04-11 18:10:25 -05002086static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2087 struct device *parent,
2088 sector_t size,
2089 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002090 const char *subname,
2091 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct mmc_blk_data *md;
2094 int devidx, ret;
2095
Heiner Kallweita04848c2017-02-01 19:44:22 +01002096 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002097 if (devidx < 0) {
2098 /*
2099 * We get -ENOSPC because there are no more any available
2100 * devidx. The reason may be that, either userspace haven't yet
2101 * unmounted the partitions, which postpones mmc_blk_release()
2102 * from being called, or the device has more partitions than
2103 * what we support.
2104 */
2105 if (devidx == -ENOSPC)
2106 dev_err(mmc_dev(card->host),
2107 "no more device IDs available\n");
2108
Heiner Kallweita04848c2017-02-01 19:44:22 +01002109 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002112 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002113 if (!md) {
2114 ret = -ENOMEM;
2115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002117
Johan Rudholmadd710e2011-12-02 08:51:06 +01002118 md->area_type = area_type;
2119
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002120 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002121 * Set the read-only status based on the supported commands
2122 * and the write protect switch.
2123 */
2124 md->read_only = mmc_blk_readonly(card);
2125
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002126 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002127 if (md->disk == NULL) {
2128 ret = -ENOMEM;
2129 goto err_kfree;
2130 }
2131
2132 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002133 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002134 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002135 md->usage = 1;
2136
Adrian Hunterd09408a2011-06-23 13:40:28 +03002137 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002138 if (ret)
2139 goto err_putdisk;
2140
Linus Walleij7db30282016-11-18 13:36:15 +01002141 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002142
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002143 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002144 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002145 md->disk->fops = &mmc_bdops;
2146 md->disk->private_data = md;
2147 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002148 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002149 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002150 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002151 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002152 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002153
2154 /*
2155 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2156 *
2157 * - be set for removable media with permanent block devices
2158 * - be unset for removable block devices with permanent media
2159 *
2160 * Since MMC block devices clearly fall under the second
2161 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2162 * should use the block device creation/destruction hotplug
2163 * messages to tell when the card is present.
2164 */
2165
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002166 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002167 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002168
Saugata Dasa5075eb2012-05-17 16:32:21 +05302169 if (mmc_card_mmc(card))
2170 blk_queue_logical_block_size(md->queue.queue,
2171 card->ext_csd.data_sector_size);
2172 else
2173 blk_queue_logical_block_size(md->queue.queue, 512);
2174
Andrei Warkentin371a6892011-04-11 18:10:25 -05002175 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002176
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002177 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002178 if ((mmc_card_mmc(card) &&
2179 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002180 (mmc_card_sd(card) &&
2181 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2182 md->flags |= MMC_BLK_CMD23;
2183 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002184
2185 if (mmc_card_mmc(card) &&
2186 md->flags & MMC_BLK_CMD23 &&
2187 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2188 card->ext_csd.rel_sectors)) {
2189 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002190 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002191 }
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002194
2195 err_putdisk:
2196 put_disk(md->disk);
2197 err_kfree:
2198 kfree(md);
2199 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002200 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002201 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Andrei Warkentin371a6892011-04-11 18:10:25 -05002204static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2205{
2206 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002207
2208 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2209 /*
2210 * The EXT_CSD sector count is in number or 512 byte
2211 * sectors.
2212 */
2213 size = card->ext_csd.sectors;
2214 } else {
2215 /*
2216 * The CSD capacity field is in units of read_blkbits.
2217 * set_capacity takes units of 512 bytes.
2218 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002219 size = (typeof(sector_t))card->csd.capacity
2220 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002221 }
2222
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002223 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002224 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225}
2226
2227static int mmc_blk_alloc_part(struct mmc_card *card,
2228 struct mmc_blk_data *md,
2229 unsigned int part_type,
2230 sector_t size,
2231 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002232 const char *subname,
2233 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002234{
2235 char cap_str[10];
2236 struct mmc_blk_data *part_md;
2237
2238 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002239 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240 if (IS_ERR(part_md))
2241 return PTR_ERR(part_md);
2242 part_md->part_type = part_type;
2243 list_add(&part_md->part, &md->part);
2244
James Bottomleyb9f28d82015-03-05 18:47:01 -08002245 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002246 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302247 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002248 part_md->disk->disk_name, mmc_card_id(card),
2249 mmc_card_name(card), part_md->part_type, cap_str);
2250 return 0;
2251}
2252
Linus Walleij97548572017-09-20 10:02:00 +02002253/**
2254 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2255 * @filp: the character device file
2256 * @cmd: the ioctl() command
2257 * @arg: the argument from userspace
2258 *
2259 * This will essentially just redirect the ioctl()s coming in over to
2260 * the main block device spawning the RPMB character device.
2261 */
2262static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2263 unsigned long arg)
2264{
2265 struct mmc_rpmb_data *rpmb = filp->private_data;
2266 int ret;
2267
2268 switch (cmd) {
2269 case MMC_IOC_CMD:
2270 ret = mmc_blk_ioctl_cmd(rpmb->md,
2271 (struct mmc_ioc_cmd __user *)arg,
2272 rpmb);
2273 break;
2274 case MMC_IOC_MULTI_CMD:
2275 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2276 (struct mmc_ioc_multi_cmd __user *)arg,
2277 rpmb);
2278 break;
2279 default:
2280 ret = -EINVAL;
2281 break;
2282 }
2283
2284 return 0;
2285}
2286
2287#ifdef CONFIG_COMPAT
2288static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2289 unsigned long arg)
2290{
2291 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2292}
2293#endif
2294
2295static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2296{
2297 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2298 struct mmc_rpmb_data, chrdev);
2299
2300 get_device(&rpmb->dev);
2301 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02002302 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02002303
2304 return nonseekable_open(inode, filp);
2305}
2306
2307static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2308{
2309 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2310 struct mmc_rpmb_data, chrdev);
2311
2312 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002313 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02002314
2315 return 0;
2316}
2317
2318static const struct file_operations mmc_rpmb_fileops = {
2319 .release = mmc_rpmb_chrdev_release,
2320 .open = mmc_rpmb_chrdev_open,
2321 .owner = THIS_MODULE,
2322 .llseek = no_llseek,
2323 .unlocked_ioctl = mmc_rpmb_ioctl,
2324#ifdef CONFIG_COMPAT
2325 .compat_ioctl = mmc_rpmb_ioctl_compat,
2326#endif
2327};
2328
Linus Walleij1c87f732017-10-04 11:10:07 +02002329static void mmc_blk_rpmb_device_release(struct device *dev)
2330{
2331 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2332
2333 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2334 kfree(rpmb);
2335}
Linus Walleij97548572017-09-20 10:02:00 +02002336
2337static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2338 struct mmc_blk_data *md,
2339 unsigned int part_index,
2340 sector_t size,
2341 const char *subname)
2342{
2343 int devidx, ret;
2344 char rpmb_name[DISK_NAME_LEN];
2345 char cap_str[10];
2346 struct mmc_rpmb_data *rpmb;
2347
2348 /* This creates the minor number for the RPMB char device */
2349 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2350 if (devidx < 0)
2351 return devidx;
2352
2353 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02002354 if (!rpmb) {
2355 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02002356 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02002357 }
Linus Walleij97548572017-09-20 10:02:00 +02002358
2359 snprintf(rpmb_name, sizeof(rpmb_name),
2360 "mmcblk%u%s", card->host->index, subname ? subname : "");
2361
2362 rpmb->id = devidx;
2363 rpmb->part_index = part_index;
2364 rpmb->dev.init_name = rpmb_name;
2365 rpmb->dev.bus = &mmc_rpmb_bus_type;
2366 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2367 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02002368 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02002369 device_initialize(&rpmb->dev);
2370 dev_set_drvdata(&rpmb->dev, rpmb);
2371 rpmb->md = md;
2372
2373 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2374 rpmb->chrdev.owner = THIS_MODULE;
2375 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2376 if (ret) {
2377 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02002378 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02002379 }
2380
2381 list_add(&rpmb->node, &md->rpmbs);
2382
2383 string_get_size((u64)size, 512, STRING_UNITS_2,
2384 cap_str, sizeof(cap_str));
2385
2386 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2387 rpmb_name, mmc_card_id(card),
2388 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2389 MAJOR(mmc_rpmb_devt), rpmb->id);
2390
2391 return 0;
2392
Linus Walleij1c87f732017-10-04 11:10:07 +02002393out_put_device:
2394 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002395 return ret;
2396}
2397
2398static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02002399
Linus Walleij97548572017-09-20 10:02:00 +02002400{
2401 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002402 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002403}
2404
Namjae Jeone0c368d2011-10-06 23:41:38 +09002405/* MMC Physical partitions consist of two boot partitions and
2406 * up to four general purpose partitions.
2407 * For each partition enabled in EXT_CSD a block device will be allocatedi
2408 * to provide access to the partition.
2409 */
2410
Andrei Warkentin371a6892011-04-11 18:10:25 -05002411static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2412{
Linus Walleij97548572017-09-20 10:02:00 +02002413 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002414
2415 if (!mmc_card_mmc(card))
2416 return 0;
2417
Namjae Jeone0c368d2011-10-06 23:41:38 +09002418 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02002419 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2420 /*
2421 * RPMB partitions does not provide block access, they
2422 * are only accessed using ioctl():s. Thus create
2423 * special RPMB block devices that do not have a
2424 * backing block queue for these.
2425 */
2426 ret = mmc_blk_alloc_rpmb_part(card, md,
2427 card->part[idx].part_cfg,
2428 card->part[idx].size >> 9,
2429 card->part[idx].name);
2430 if (ret)
2431 return ret;
2432 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09002433 ret = mmc_blk_alloc_part(card, md,
2434 card->part[idx].part_cfg,
2435 card->part[idx].size >> 9,
2436 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002437 card->part[idx].name,
2438 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002439 if (ret)
2440 return ret;
2441 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002442 }
2443
Linus Walleij97548572017-09-20 10:02:00 +02002444 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002445}
2446
Andrei Warkentin371a6892011-04-11 18:10:25 -05002447static void mmc_blk_remove_req(struct mmc_blk_data *md)
2448{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002449 struct mmc_card *card;
2450
Andrei Warkentin371a6892011-04-11 18:10:25 -05002451 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002452 /*
2453 * Flush remaining requests and free queues. It
2454 * is freeing the queue that stops new requests
2455 * from being accepted.
2456 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002457 card = md->queue.card;
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002458 spin_lock_irq(md->queue.queue->queue_lock);
Shawn Lin7c84b8b2017-07-25 09:11:28 +08002459 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002460 spin_unlock_irq(md->queue.queue->queue_lock);
Grzegorz Slujabbdc74d2017-07-13 11:17:58 +02002461 blk_set_queue_dying(md->queue.queue);
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002462 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002463 if (md->disk->flags & GENHD_FL_UP) {
2464 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002465 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2466 card->ext_csd.boot_ro_lockable)
2467 device_remove_file(disk_to_dev(md->disk),
2468 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002469
Andrei Warkentin371a6892011-04-11 18:10:25 -05002470 del_gendisk(md->disk);
2471 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002472 mmc_blk_put(md);
2473 }
2474}
2475
2476static void mmc_blk_remove_parts(struct mmc_card *card,
2477 struct mmc_blk_data *md)
2478{
2479 struct list_head *pos, *q;
2480 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02002481 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002482
Linus Walleij97548572017-09-20 10:02:00 +02002483 /* Remove RPMB partitions */
2484 list_for_each_safe(pos, q, &md->rpmbs) {
2485 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2486 list_del(pos);
2487 mmc_blk_remove_rpmb_part(rpmb);
2488 }
2489 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05002490 list_for_each_safe(pos, q, &md->part) {
2491 part_md = list_entry(pos, struct mmc_blk_data, part);
2492 list_del(pos);
2493 mmc_blk_remove_req(part_md);
2494 }
2495}
2496
2497static int mmc_add_disk(struct mmc_blk_data *md)
2498{
2499 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002500 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002501
Dan Williams307d8e62016-06-20 10:40:44 -07002502 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002503 md->force_ro.show = force_ro_show;
2504 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302505 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002506 md->force_ro.attr.name = "force_ro";
2507 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2508 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2509 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002510 goto force_ro_fail;
2511
2512 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2513 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002514 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002515
2516 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2517 mode = S_IRUGO;
2518 else
2519 mode = S_IRUGO | S_IWUSR;
2520
2521 md->power_ro_lock.show = power_ro_lock_show;
2522 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002523 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002524 md->power_ro_lock.attr.mode = mode;
2525 md->power_ro_lock.attr.name =
2526 "ro_lock_until_next_power_on";
2527 ret = device_create_file(disk_to_dev(md->disk),
2528 &md->power_ro_lock);
2529 if (ret)
2530 goto power_ro_lock_fail;
2531 }
2532 return ret;
2533
2534power_ro_lock_fail:
2535 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2536force_ro_fail:
2537 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002538
2539 return ret;
2540}
2541
Linus Walleij627c3cc2017-08-20 23:39:08 +02002542#ifdef CONFIG_DEBUG_FS
2543
2544static int mmc_dbg_card_status_get(void *data, u64 *val)
2545{
2546 struct mmc_card *card = data;
2547 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2548 struct mmc_queue *mq = &md->queue;
2549 struct request *req;
2550 int ret;
2551
2552 /* Ask the block layer about the card status */
2553 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2554 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2555 blk_execute_rq(mq->queue, NULL, req, 0);
2556 ret = req_to_mmc_queue_req(req)->drv_op_result;
2557 if (ret >= 0) {
2558 *val = ret;
2559 ret = 0;
2560 }
Adrian Hunter34c089e2017-11-21 15:42:27 +02002561 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002562
2563 return ret;
2564}
2565DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2566 NULL, "%08llx\n");
2567
2568/* That is two digits * 512 + 1 for newline */
2569#define EXT_CSD_STR_LEN 1025
2570
2571static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2572{
2573 struct mmc_card *card = inode->i_private;
2574 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2575 struct mmc_queue *mq = &md->queue;
2576 struct request *req;
2577 char *buf;
2578 ssize_t n = 0;
2579 u8 *ext_csd;
2580 int err, i;
2581
2582 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2583 if (!buf)
2584 return -ENOMEM;
2585
2586 /* Ask the block layer for the EXT CSD */
2587 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2588 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2589 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2590 blk_execute_rq(mq->queue, NULL, req, 0);
2591 err = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +02002592 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002593 if (err) {
2594 pr_err("FAILED %d\n", err);
2595 goto out_free;
2596 }
2597
2598 for (i = 0; i < 512; i++)
2599 n += sprintf(buf + n, "%02x", ext_csd[i]);
2600 n += sprintf(buf + n, "\n");
2601
2602 if (n != EXT_CSD_STR_LEN) {
2603 err = -EINVAL;
2604 goto out_free;
2605 }
2606
2607 filp->private_data = buf;
2608 kfree(ext_csd);
2609 return 0;
2610
2611out_free:
2612 kfree(buf);
2613 return err;
2614}
2615
2616static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2617 size_t cnt, loff_t *ppos)
2618{
2619 char *buf = filp->private_data;
2620
2621 return simple_read_from_buffer(ubuf, cnt, ppos,
2622 buf, EXT_CSD_STR_LEN);
2623}
2624
2625static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2626{
2627 kfree(file->private_data);
2628 return 0;
2629}
2630
2631static const struct file_operations mmc_dbg_ext_csd_fops = {
2632 .open = mmc_ext_csd_open,
2633 .read = mmc_ext_csd_read,
2634 .release = mmc_ext_csd_release,
2635 .llseek = default_llseek,
2636};
2637
2638static int mmc_blk_add_debugfs(struct mmc_card *card)
2639{
2640 struct dentry *root;
2641
2642 if (!card->debugfs_root)
2643 return 0;
2644
2645 root = card->debugfs_root;
2646
2647 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2648 if (!debugfs_create_file("status", S_IRUSR, root, card,
2649 &mmc_dbg_card_status_fops))
2650 return -EIO;
2651 }
2652
2653 if (mmc_card_mmc(card)) {
2654 if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
2655 &mmc_dbg_ext_csd_fops))
2656 return -EIO;
2657 }
2658
2659 return 0;
2660}
2661
2662
2663#else
2664
2665static int mmc_blk_add_debugfs(struct mmc_card *card)
2666{
2667 return 0;
2668}
2669
2670#endif /* CONFIG_DEBUG_FS */
2671
Ulf Hansson96541ba2015-04-14 13:06:12 +02002672static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002674 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002675 char cap_str[10];
2676
Pierre Ossman912490d2005-05-21 10:27:02 +01002677 /*
2678 * Check that the card supports the command class(es) we need.
2679 */
2680 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return -ENODEV;
2682
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002683 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002686 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return PTR_ERR(md);
2688
James Bottomleyb9f28d82015-03-05 18:47:01 -08002689 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002690 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302691 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002693 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Andrei Warkentin371a6892011-04-11 18:10:25 -05002695 if (mmc_blk_alloc_parts(card, md))
2696 goto out;
2697
Ulf Hansson96541ba2015-04-14 13:06:12 +02002698 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002699
Andrei Warkentin371a6892011-04-11 18:10:25 -05002700 if (mmc_add_disk(md))
2701 goto out;
2702
2703 list_for_each_entry(part_md, &md->part, part) {
2704 if (mmc_add_disk(part_md))
2705 goto out;
2706 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002707
Linus Walleij627c3cc2017-08-20 23:39:08 +02002708 /* Add two debugfs entries */
2709 mmc_blk_add_debugfs(card);
2710
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002711 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2712 pm_runtime_use_autosuspend(&card->dev);
2713
2714 /*
2715 * Don't enable runtime PM for SD-combo cards here. Leave that
2716 * decision to be taken during the SDIO init sequence instead.
2717 */
2718 if (card->type != MMC_TYPE_SD_COMBO) {
2719 pm_runtime_set_active(&card->dev);
2720 pm_runtime_enable(&card->dev);
2721 }
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
2724
2725 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002726 mmc_blk_remove_parts(card, md);
2727 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Ulf Hansson96541ba2015-04-14 13:06:12 +02002731static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002733 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Andrei Warkentin371a6892011-04-11 18:10:25 -05002735 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002736 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002737 mmc_claim_host(card->host);
Linus Walleij1f797ed2017-08-20 23:39:10 +02002738 mmc_blk_part_switch(card, md->part_type);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002739 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002740 if (card->type != MMC_TYPE_SD_COMBO)
2741 pm_runtime_disable(&card->dev);
2742 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002743 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002744 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Ulf Hansson96541ba2015-04-14 13:06:12 +02002747static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002749 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002750 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 if (md) {
2753 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002754 list_for_each_entry(part_md, &md->part, part) {
2755 mmc_queue_suspend(&part_md->queue);
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758 return 0;
2759}
2760
Ulf Hansson96541ba2015-04-14 13:06:12 +02002761static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002762{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002763 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002764}
2765
Ulf Hansson0967edc2014-10-06 11:29:42 +02002766#ifdef CONFIG_PM_SLEEP
2767static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002768{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002769 struct mmc_card *card = mmc_dev_to_card(dev);
2770
2771 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002772}
2773
Ulf Hansson0967edc2014-10-06 11:29:42 +02002774static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002776 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002777 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002780 /*
2781 * Resume involves the card going into idle state,
2782 * so current partition is always the main one.
2783 */
2784 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002786 list_for_each_entry(part_md, &md->part, part) {
2787 mmc_queue_resume(&part_md->queue);
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790 return 0;
2791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792#endif
2793
Ulf Hansson0967edc2014-10-06 11:29:42 +02002794static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2795
Ulf Hansson96541ba2015-04-14 13:06:12 +02002796static struct mmc_driver mmc_driver = {
2797 .drv = {
2798 .name = "mmcblk",
2799 .pm = &mmc_blk_pm_ops,
2800 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 .probe = mmc_blk_probe,
2802 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002803 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804};
2805
2806static int __init mmc_blk_init(void)
2807{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002808 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Linus Walleij97548572017-09-20 10:02:00 +02002810 res = bus_register(&mmc_rpmb_bus_type);
2811 if (res < 0) {
2812 pr_err("mmcblk: could not register RPMB bus type\n");
2813 return res;
2814 }
2815 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
2816 if (res < 0) {
2817 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
2818 goto out_bus_unreg;
2819 }
2820
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002821 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2822 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2823
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002824 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002825
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002826 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2827 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002828 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002830 res = mmc_register_driver(&mmc_driver);
2831 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002832 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002834 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02002835
2836out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002837 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002838out_chrdev_unreg:
2839 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2840out_bus_unreg:
2841 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return res;
2843}
2844
2845static void __exit mmc_blk_exit(void)
2846{
2847 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002848 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002849 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
2852module_init(mmc_blk_init);
2853module_exit(mmc_blk_exit);
2854
2855MODULE_LICENSE("GPL");
2856MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2857