blob: bb5f70ec80156ce84885567369588b70cb636056 [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;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100239
240 if (!ret) {
241 pr_info("%s: Locking boot partition ro until next power on\n",
242 md->disk->disk_name);
243 set_disk_ro(md->disk, 1);
244
245 list_for_each_entry(part_md, &md->part, part)
246 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
247 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
248 set_disk_ro(part_md->disk, 1);
249 }
250 }
251
252 mmc_blk_put(md);
253 return count;
254}
255
Andrei Warkentin371a6892011-04-11 18:10:25 -0500256static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
257 char *buf)
258{
259 int ret;
260 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
261
Baruch Siach0031a982014-09-22 10:12:51 +0300262 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500263 get_disk_ro(dev_to_disk(dev)) ^
264 md->read_only);
265 mmc_blk_put(md);
266 return ret;
267}
268
269static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
270 const char *buf, size_t count)
271{
272 int ret;
273 char *end;
274 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
275 unsigned long set = simple_strtoul(buf, &end, 0);
276 if (end == buf) {
277 ret = -EINVAL;
278 goto out;
279 }
280
281 set_disk_ro(dev_to_disk(dev), set || md->read_only);
282 ret = count;
283out:
284 mmc_blk_put(md);
285 return ret;
286}
287
Al Viroa5a15612008-03-02 10:33:30 -0500288static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Al Viroa5a15612008-03-02 10:33:30 -0500290 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int ret = -ENXIO;
292
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200293 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (md) {
295 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500296 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700298
Al Viroa5a15612008-03-02 10:33:30 -0500299 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700300 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700301 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200304 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 return ret;
307}
308
Al Virodb2a1442013-05-05 21:52:57 -0400309static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Al Viroa5a15612008-03-02 10:33:30 -0500311 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200313 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200315 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800319mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800321 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
322 geo->heads = 4;
323 geo->sectors = 16;
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
John Calixtocb87ea22011-04-26 18:56:29 -0400327struct mmc_blk_ioc_data {
328 struct mmc_ioc_cmd ic;
329 unsigned char *buf;
330 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200331 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400332};
333
334static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
336{
337 struct mmc_blk_ioc_data *idata;
338 int err;
339
yalin wang1ff89502015-11-12 19:27:11 +0800340 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400341 if (!idata) {
342 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400343 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400344 }
345
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400355 }
356
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300357 if (!idata->buf_bytes) {
358 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100359 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300360 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100361
yalin wang1ff89502015-11-12 19:27:11 +0800362 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400363 if (!idata->buf) {
364 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400365 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400366 }
367
368 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
369 idata->ic.data_ptr, idata->buf_bytes)) {
370 err = -EFAULT;
371 goto copy_err;
372 }
373
374 return idata;
375
376copy_err:
377 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400378idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400379 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400380out:
John Calixtocb87ea22011-04-26 18:56:29 -0400381 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400382}
383
Jon Huntera5f57742015-09-22 10:27:53 +0100384static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
385 struct mmc_blk_ioc_data *idata)
386{
387 struct mmc_ioc_cmd *ic = &idata->ic;
388
389 if (copy_to_user(&(ic_ptr->response), ic->response,
390 sizeof(ic->response)))
391 return -EFAULT;
392
393 if (!idata->ic.write_flag) {
394 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
395 idata->buf, idata->buf_bytes))
396 return -EFAULT;
397 }
398
399 return 0;
400}
401
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200402static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
403 u32 retries_max)
404{
405 int err;
406 u32 retry_count = 0;
407
408 if (!status || !retries_max)
409 return -EINVAL;
410
411 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200412 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200413 if (err)
414 break;
415
416 if (!R1_STATUS(*status) &&
417 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
418 break; /* RPMB programming operation complete */
419
420 /*
421 * Rechedule to give the MMC device a chance to continue
422 * processing the previous command without being polled too
423 * frequently.
424 */
425 usleep_range(1000, 5000);
426 } while (++retry_count < retries_max);
427
428 if (retry_count == retries_max)
429 err = -EPERM;
430
431 return err;
432}
433
Maya Erez775a9362013-04-18 15:41:55 +0300434static int ioctl_do_sanitize(struct mmc_card *card)
435{
436 int err;
437
Ulf Hanssona2d10862013-12-16 14:37:26 +0100438 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300439 pr_warn("%s: %s - SANITIZE is not supported\n",
440 mmc_hostname(card->host), __func__);
441 err = -EOPNOTSUPP;
442 goto out;
443 }
444
445 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
446 mmc_hostname(card->host), __func__);
447
448 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
449 EXT_CSD_SANITIZE_START, 1,
450 MMC_SANITIZE_REQ_TIMEOUT);
451
452 if (err)
453 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
454 mmc_hostname(card->host), __func__, err);
455
456 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
457 __func__);
458out:
459 return err;
460}
461
Jon Huntera5f57742015-09-22 10:27:53 +0100462static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
463 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400464{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900465 struct mmc_command cmd = {};
466 struct mmc_data data = {};
467 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400468 struct scatterlist sg;
469 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200470 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200471 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400472
Jon Huntera5f57742015-09-22 10:27:53 +0100473 if (!card || !md || !idata)
474 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400475
Linus Walleij97548572017-09-20 10:02:00 +0200476 /*
477 * The RPMB accesses comes in from the character device, so we
478 * need to target these explicitly. Else we just target the
479 * partition type for the block device the ioctl() was issued
480 * on.
481 */
482 if (idata->rpmb) {
483 /* Support multiple RPMB partitions */
484 target_part = idata->rpmb->part_index;
485 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
486 } else {
487 target_part = md->part_type;
488 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200489
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100490 cmd.opcode = idata->ic.opcode;
491 cmd.arg = idata->ic.arg;
492 cmd.flags = idata->ic.flags;
493
494 if (idata->buf_bytes) {
495 data.sg = &sg;
496 data.sg_len = 1;
497 data.blksz = idata->ic.blksz;
498 data.blocks = idata->ic.blocks;
499
500 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
501
502 if (idata->ic.write_flag)
503 data.flags = MMC_DATA_WRITE;
504 else
505 data.flags = MMC_DATA_READ;
506
507 /* data.flags must already be set before doing this. */
508 mmc_set_data_timeout(&data, card);
509
510 /* Allow overriding the timeout_ns for empirical tuning. */
511 if (idata->ic.data_timeout_ns)
512 data.timeout_ns = idata->ic.data_timeout_ns;
513
514 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
515 /*
516 * Pretend this is a data transfer and rely on the
517 * host driver to compute timeout. When all host
518 * drivers support cmd.cmd_timeout for R1B, this
519 * can be changed to:
520 *
521 * mrq.data = NULL;
522 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
523 */
524 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
525 }
526
527 mrq.data = &data;
528 }
529
530 mrq.cmd = &cmd;
531
Linus Walleij97548572017-09-20 10:02:00 +0200532 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200533 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100534 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200535
John Calixtocb87ea22011-04-26 18:56:29 -0400536 if (idata->ic.is_acmd) {
537 err = mmc_app_cmd(card->host, card);
538 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100539 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400540 }
541
Linus Walleij97548572017-09-20 10:02:00 +0200542 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200543 err = mmc_set_blockcount(card, data.blocks,
544 idata->ic.write_flag & (1 << 31));
545 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100546 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200547 }
548
Yaniv Gardia82e4842013-06-05 14:13:08 +0300549 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
550 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300551 err = ioctl_do_sanitize(card);
552
553 if (err)
554 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
555 __func__, err);
556
Jon Huntera5f57742015-09-22 10:27:53 +0100557 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300558 }
559
John Calixtocb87ea22011-04-26 18:56:29 -0400560 mmc_wait_for_req(card->host, &mrq);
561
562 if (cmd.error) {
563 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
564 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100565 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400566 }
567 if (data.error) {
568 dev_err(mmc_dev(card->host), "%s: data error %d\n",
569 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100570 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400571 }
572
573 /*
574 * According to the SD specs, some commands require a delay after
575 * issuing the command.
576 */
577 if (idata->ic.postsleep_min_us)
578 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
579
Jon Huntera5f57742015-09-22 10:27:53 +0100580 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400581
Linus Walleij97548572017-09-20 10:02:00 +0200582 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200583 /*
584 * Ensure RPMB command has completed by polling CMD13
585 * "Send Status".
586 */
587 err = ioctl_rpmb_card_status_poll(card, &status, 5);
588 if (err)
589 dev_err(mmc_dev(card->host),
590 "%s: Card Status=0x%08X, error %d\n",
591 __func__, status, err);
592 }
593
Jon Huntera5f57742015-09-22 10:27:53 +0100594 return err;
595}
596
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200597static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200598 struct mmc_ioc_cmd __user *ic_ptr,
599 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100600{
601 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200602 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200603 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100604 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700605 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200606 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100607
608 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
609 if (IS_ERR(idata))
610 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200611 /* This will be NULL on non-RPMB ioctl():s */
612 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100613
Jon Huntera5f57742015-09-22 10:27:53 +0100614 card = md->queue.card;
615 if (IS_ERR(card)) {
616 err = PTR_ERR(card);
617 goto cmd_done;
618 }
619
Linus Walleij614f0382017-05-18 11:29:34 +0200620 /*
621 * Dispatch the ioctl() into the block request queue.
622 */
623 mq = &md->queue;
624 req = blk_get_request(mq->queue,
625 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
626 __GFP_RECLAIM);
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200627 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200628 req_to_mmc_queue_req(req)->drv_op =
629 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200630 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200631 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200632 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200633 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700634 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200635 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100636
John Calixtocb87ea22011-04-26 18:56:29 -0400637cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400638 kfree(idata->buf);
639 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700640 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400641}
642
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200643static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200644 struct mmc_ioc_multi_cmd __user *user,
645 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100646{
647 struct mmc_blk_ioc_data **idata = NULL;
648 struct mmc_ioc_cmd __user *cmds = user->cmds;
649 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200650 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700651 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100652 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200653 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100654
655 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
656 sizeof(num_of_cmds)))
657 return -EFAULT;
658
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200659 if (!num_of_cmds)
660 return 0;
661
Jon Huntera5f57742015-09-22 10:27:53 +0100662 if (num_of_cmds > MMC_IOC_MAX_CMDS)
663 return -EINVAL;
664
665 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
666 if (!idata)
667 return -ENOMEM;
668
669 for (i = 0; i < num_of_cmds; i++) {
670 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
671 if (IS_ERR(idata[i])) {
672 err = PTR_ERR(idata[i]);
673 num_of_cmds = i;
674 goto cmd_err;
675 }
Linus Walleij97548572017-09-20 10:02:00 +0200676 /* This will be NULL on non-RPMB ioctl():s */
677 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100678 }
679
Jon Huntera5f57742015-09-22 10:27:53 +0100680 card = md->queue.card;
681 if (IS_ERR(card)) {
682 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200683 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100684 }
685
Jon Huntera5f57742015-09-22 10:27:53 +0100686
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200687 /*
688 * Dispatch the ioctl()s into the block request queue.
689 */
690 mq = &md->queue;
691 req = blk_get_request(mq->queue,
692 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
693 __GFP_RECLAIM);
Linus Walleij97548572017-09-20 10:02:00 +0200694 req_to_mmc_queue_req(req)->drv_op =
695 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200696 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200697 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
698 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200699 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100700
701 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700702 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100703 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100704
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200705 blk_put_request(req);
706
Jon Huntera5f57742015-09-22 10:27:53 +0100707cmd_err:
708 for (i = 0; i < num_of_cmds; i++) {
709 kfree(idata[i]->buf);
710 kfree(idata[i]);
711 }
712 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700713 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100714}
715
Linus Walleij61fe0e22017-08-20 23:39:09 +0200716static int mmc_blk_check_blkdev(struct block_device *bdev)
717{
718 /*
719 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
720 * whole block device, not on a partition. This prevents overspray
721 * between sibling partitions.
722 */
723 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
724 return -EPERM;
725 return 0;
726}
727
John Calixtocb87ea22011-04-26 18:56:29 -0400728static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
729 unsigned int cmd, unsigned long arg)
730{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200731 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200732 int ret;
733
Jon Huntera5f57742015-09-22 10:27:53 +0100734 switch (cmd) {
735 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200736 ret = mmc_blk_check_blkdev(bdev);
737 if (ret)
738 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200739 md = mmc_blk_get(bdev->bd_disk);
740 if (!md)
741 return -EINVAL;
742 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200743 (struct mmc_ioc_cmd __user *)arg,
744 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200745 mmc_blk_put(md);
746 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100747 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200748 ret = mmc_blk_check_blkdev(bdev);
749 if (ret)
750 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200751 md = mmc_blk_get(bdev->bd_disk);
752 if (!md)
753 return -EINVAL;
754 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200755 (struct mmc_ioc_multi_cmd __user *)arg,
756 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200757 mmc_blk_put(md);
758 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100759 default:
760 return -EINVAL;
761 }
John Calixtocb87ea22011-04-26 18:56:29 -0400762}
763
764#ifdef CONFIG_COMPAT
765static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
766 unsigned int cmd, unsigned long arg)
767{
768 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
769}
770#endif
771
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700772static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500773 .open = mmc_blk_open,
774 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800775 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400777 .ioctl = mmc_blk_ioctl,
778#ifdef CONFIG_COMPAT
779 .compat_ioctl = mmc_blk_compat_ioctl,
780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781};
782
Adrian Hunter025e3d52017-03-13 14:36:39 +0200783static int mmc_blk_part_switch_pre(struct mmc_card *card,
784 unsigned int part_type)
785{
786 int ret = 0;
787
788 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
789 if (card->ext_csd.cmdq_en) {
790 ret = mmc_cmdq_disable(card);
791 if (ret)
792 return ret;
793 }
794 mmc_retune_pause(card->host);
795 }
796
797 return ret;
798}
799
800static int mmc_blk_part_switch_post(struct mmc_card *card,
801 unsigned int part_type)
802{
803 int ret = 0;
804
805 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
806 mmc_retune_unpause(card->host);
807 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
808 ret = mmc_cmdq_enable(card);
809 }
810
811 return ret;
812}
813
Andrei Warkentin371a6892011-04-11 18:10:25 -0500814static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200815 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500816{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200817 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200818 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300819
Linus Walleij1f797ed2017-08-20 23:39:10 +0200820 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500821 return 0;
822
823 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300824 u8 part_config = card->ext_csd.part_config;
825
Linus Walleij1f797ed2017-08-20 23:39:10 +0200826 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200827 if (ret)
828 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300829
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300830 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200831 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500832
833 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300834 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500835 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300836 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200837 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500838 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300839 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300840
841 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300842
Adrian Hunter025e3d52017-03-13 14:36:39 +0200843 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300844 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500845
Linus Walleij1f797ed2017-08-20 23:39:10 +0200846 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200847 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500848}
849
Linus Walleij169f03a2017-02-01 13:47:57 +0100850static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700851{
852 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100853 u32 result;
854 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700855
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900856 struct mmc_request mrq = {};
857 struct mmc_command cmd = {};
858 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700859
860 struct scatterlist sg;
861
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700862 cmd.opcode = MMC_APP_CMD;
863 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700864 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700865
866 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700867 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100868 return err;
David Brownell7213d172007-08-08 09:10:23 -0700869 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100870 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700871
872 memset(&cmd, 0, sizeof(struct mmc_command));
873
874 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
875 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700876 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700877
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700878 data.blksz = 4;
879 data.blocks = 1;
880 data.flags = MMC_DATA_READ;
881 data.sg = &sg;
882 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530883 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700884
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700885 mrq.cmd = &cmd;
886 mrq.data = &data;
887
Ben Dooks051913d2009-06-08 23:33:57 +0100888 blocks = kmalloc(4, GFP_KERNEL);
889 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100890 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100891
892 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700893
894 mmc_wait_for_req(card->host, &mrq);
895
Ben Dooks051913d2009-06-08 23:33:57 +0100896 result = ntohl(*blocks);
897 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700898
Ben Dooks051913d2009-06-08 23:33:57 +0100899 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100900 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100901
Linus Walleij169f03a2017-02-01 13:47:57 +0100902 *written_blocks = result;
903
904 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700905}
906
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100907static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100908 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100909{
910 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
911 int err = 0;
912 u32 status;
913
914 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200915 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100916 if (err) {
917 pr_err("%s: error %d requesting status\n",
918 req->rq_disk->disk_name, err);
919 return err;
920 }
921
922 if (status & R1_ERROR) {
923 pr_err("%s: %s: error sending status cmd, status %#x\n",
924 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100925 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100926 }
927
Ulf Hansson95a91292014-01-29 13:11:27 +0100928 /* We may rely on the host hw to handle busy detection.*/
929 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
930 hw_busy_detect)
931 break;
932
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100933 /*
934 * Timeout if the device never becomes ready for data and never
935 * leaves the program state.
936 */
937 if (time_after(jiffies, timeout)) {
938 pr_err("%s: Card stuck in programming state! %s %s\n",
939 mmc_hostname(card->host),
940 req->rq_disk->disk_name, __func__);
941 return -ETIMEDOUT;
942 }
943
944 /*
945 * Some cards mishandle the status bits,
946 * so make sure to check both the busy
947 * indication and the card state.
948 */
949 } while (!(status & R1_READY_FOR_DATA) ||
950 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
951
952 return err;
953}
954
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100955static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100956 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100957{
958 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900959 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100960 int err;
961 bool use_r1b_resp = rq_data_dir(req) == WRITE;
962
963 /*
964 * Normally we use R1B responses for WRITE, but in cases where the host
965 * has specified a max_busy_timeout we need to validate it. A failure
966 * means we need to prevent the host from doing hw busy detection, which
967 * is done by converting to a R1 response instead.
968 */
969 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
970 use_r1b_resp = false;
971
972 cmd.opcode = MMC_STOP_TRANSMISSION;
973 if (use_r1b_resp) {
974 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
975 cmd.busy_timeout = timeout_ms;
976 } else {
977 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
978 }
979
980 err = mmc_wait_for_cmd(host, &cmd, 5);
981 if (err)
982 return err;
983
984 *stop_status = cmd.resp[0];
985
986 /* No need to check card status in case of READ. */
987 if (rq_data_dir(req) == READ)
988 return 0;
989
990 if (!mmc_host_is_spi(host) &&
991 (*stop_status & R1_ERROR)) {
992 pr_err("%s: %s: general error sending stop command, resp %#x\n",
993 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100994 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100995 }
996
997 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
998}
999
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301000#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001001#define ERR_RETRY 2
1002#define ERR_ABORT 1
1003#define ERR_CONTINUE 0
1004
1005static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1006 bool status_valid, u32 status)
1007{
1008 switch (error) {
1009 case -EILSEQ:
1010 /* response crc error, retry the r/w cmd */
1011 pr_err("%s: %s sending %s command, card status %#x\n",
1012 req->rq_disk->disk_name, "response CRC error",
1013 name, status);
1014 return ERR_RETRY;
1015
1016 case -ETIMEDOUT:
1017 pr_err("%s: %s sending %s command, card status %#x\n",
1018 req->rq_disk->disk_name, "timed out", name, status);
1019
1020 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301021 if (!status_valid) {
1022 pr_err("%s: status not valid, retrying timeout\n",
1023 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001024 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301025 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001026
1027 /*
1028 * If it was a r/w cmd crc error, or illegal command
1029 * (eg, issued in wrong state) then retry - we should
1030 * have corrected the state problem above.
1031 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301032 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1033 pr_err("%s: command error, retrying timeout\n",
1034 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001035 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301036 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001037
1038 /* Otherwise abort the command */
1039 return ERR_ABORT;
1040
1041 default:
1042 /* We don't understand the error code the driver gave us */
1043 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1044 req->rq_disk->disk_name, error, status);
1045 return ERR_ABORT;
1046 }
1047}
1048
1049/*
1050 * Initial r/w and stop cmd error recovery.
1051 * We don't know whether the card received the r/w cmd or not, so try to
1052 * restore things back to a sane state. Essentially, we do this as follows:
1053 * - Obtain card status. If the first attempt to obtain card status fails,
1054 * the status word will reflect the failed status cmd, not the failed
1055 * r/w cmd. If we fail to obtain card status, it suggests we can no
1056 * longer communicate with the card.
1057 * - Check the card state. If the card received the cmd but there was a
1058 * transient problem with the response, it might still be in a data transfer
1059 * mode. Try to send it a stop command. If this fails, we can't recover.
1060 * - If the r/w cmd failed due to a response CRC error, it was probably
1061 * transient, so retry the cmd.
1062 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1063 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1064 * illegal cmd, retry.
1065 * Otherwise we don't understand what happened, so abort.
1066 */
1067static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001068 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069{
1070 bool prev_cmd_status_valid = true;
1071 u32 status, stop_status = 0;
1072 int err, retry;
1073
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301074 if (mmc_card_removed(card))
1075 return ERR_NOMEDIUM;
1076
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001077 /*
1078 * Try to get card status which indicates both the card state
1079 * and why there was no response. If the first attempt fails,
1080 * we can't be sure the returned status is for the r/w command.
1081 */
1082 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001083 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001084 if (!err)
1085 break;
1086
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001087 /* Re-tune if needed */
1088 mmc_retune_recheck(card->host);
1089
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001090 prev_cmd_status_valid = false;
1091 pr_err("%s: error %d sending status command, %sing\n",
1092 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1093 }
1094
1095 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301096 if (err) {
1097 /* Check if the card is removed */
1098 if (mmc_detect_card_removed(card->host))
1099 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001100 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301101 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001102
Adrian Hunter67716322011-08-29 16:42:15 +03001103 /* Flag ECC errors */
1104 if ((status & R1_CARD_ECC_FAILED) ||
1105 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1106 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001107 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001108
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001109 /* Flag General errors */
1110 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1111 if ((status & R1_ERROR) ||
1112 (brq->stop.resp[0] & R1_ERROR)) {
1113 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1114 req->rq_disk->disk_name, __func__,
1115 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001116 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001117 }
1118
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001119 /*
1120 * Check the current card state. If it is in some data transfer
1121 * mode, tell it to stop (and hopefully transition back to TRAN.)
1122 */
1123 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1124 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001125 err = send_stop(card,
1126 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1127 req, gen_err, &stop_status);
1128 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001129 pr_err("%s: error %d sending stop command\n",
1130 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001131 /*
1132 * If the stop cmd also timed out, the card is probably
1133 * not present, so abort. Other errors are bad news too.
1134 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001135 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001136 }
1137
Adrian Hunter67716322011-08-29 16:42:15 +03001138 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001139 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001140 }
1141
1142 /* Check for set block count errors */
1143 if (brq->sbc.error)
1144 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1145 prev_cmd_status_valid, status);
1146
1147 /* Check for r/w command errors */
1148 if (brq->cmd.error)
1149 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1150 prev_cmd_status_valid, status);
1151
Adrian Hunter67716322011-08-29 16:42:15 +03001152 /* Data errors */
1153 if (!brq->stop.error)
1154 return ERR_CONTINUE;
1155
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001156 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001157 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 +01001158 req->rq_disk->disk_name, brq->stop.error,
1159 brq->cmd.resp[0], status);
1160
1161 /*
1162 * Subsitute in our own stop status as this will give the error
1163 * state which happened during the execution of the r/w command.
1164 */
1165 if (stop_status) {
1166 brq->stop.resp[0] = stop_status;
1167 brq->stop.error = 0;
1168 }
1169 return ERR_CONTINUE;
1170}
1171
Adrian Hunter67716322011-08-29 16:42:15 +03001172static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1173 int type)
1174{
1175 int err;
1176
1177 if (md->reset_done & type)
1178 return -EEXIST;
1179
1180 md->reset_done |= type;
1181 err = mmc_hw_reset(host);
1182 /* Ensure we switch back to the correct partition */
1183 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001184 struct mmc_blk_data *main_md =
1185 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001186 int part_err;
1187
1188 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001189 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001190 if (part_err) {
1191 /*
1192 * We have failed to get back into the correct
1193 * partition, so we need to abort the whole request.
1194 */
1195 return -ENODEV;
1196 }
1197 }
1198 return err;
1199}
1200
1201static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1202{
1203 md->reset_done &= ~type;
1204}
1205
Linus Walleij5ec12392017-05-19 15:37:29 +02001206/*
1207 * The non-block commands come back from the block layer after it queued it and
1208 * processed it with all other requests and then they get issued in this
1209 * function.
1210 */
1211static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1212{
1213 struct mmc_queue_req *mq_rq;
1214 struct mmc_card *card = mq->card;
1215 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001216 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001217 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001218 u8 **ext_csd;
1219 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001220 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001221 int i;
1222
1223 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001224 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001225
1226 switch (mq_rq->drv_op) {
1227 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001228 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001229 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001230 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001231 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001232 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001233 break;
1234 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001235 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001236 if (rpmb_ioctl)
1237 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001238 break;
1239 case MMC_DRV_OP_BOOT_WP:
1240 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1241 card->ext_csd.boot_ro_lock |
1242 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1243 card->ext_csd.part_time);
1244 if (ret)
1245 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1246 md->disk->disk_name, ret);
1247 else
1248 card->ext_csd.boot_ro_lock |=
1249 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001250 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001251 case MMC_DRV_OP_GET_CARD_STATUS:
1252 ret = mmc_send_status(card, &status);
1253 if (!ret)
1254 ret = status;
1255 break;
1256 case MMC_DRV_OP_GET_EXT_CSD:
1257 ext_csd = mq_rq->drv_op_data;
1258 ret = mmc_get_ext_csd(card, ext_csd);
1259 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001260 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001261 pr_err("%s: unknown driver specific operation\n",
1262 md->disk->disk_name);
1263 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001264 break;
1265 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001266 mq_rq->drv_op_result = ret;
Adrian Huntera7c17d82017-08-25 15:43:45 +03001267 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001268}
1269
Linus Walleijdf061582017-01-24 11:17:57 +01001270static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001271{
Linus Walleij7db30282016-11-18 13:36:15 +01001272 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001273 struct mmc_card *card = md->queue.card;
1274 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001275 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001276 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001277
Adrian Hunterbd788c92010-08-11 14:17:47 -07001278 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001279 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001280 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001281 }
1282
1283 from = blk_rq_pos(req);
1284 nr = blk_rq_sectors(req);
1285
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001286 if (mmc_can_discard(card))
1287 arg = MMC_DISCARD_ARG;
1288 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001289 arg = MMC_TRIM_ARG;
1290 else
1291 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001292 do {
1293 err = 0;
1294 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1295 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1296 INAND_CMD38_ARG_EXT_CSD,
1297 arg == MMC_TRIM_ARG ?
1298 INAND_CMD38_ARG_TRIM :
1299 INAND_CMD38_ARG_ERASE,
1300 0);
1301 }
1302 if (!err)
1303 err = mmc_erase(card, from, nr, arg);
1304 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001305 if (err)
1306 status = BLK_STS_IOERR;
1307 else
Adrian Hunter67716322011-08-29 16:42:15 +03001308 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001309fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001310 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001311}
1312
Linus Walleijdf061582017-01-24 11:17:57 +01001313static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001314 struct request *req)
1315{
Linus Walleij7db30282016-11-18 13:36:15 +01001316 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001317 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001318 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001319 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001320 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001321
Maya Erez775a9362013-04-18 15:41:55 +03001322 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001323 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001324 goto out;
1325 }
1326
1327 from = blk_rq_pos(req);
1328 nr = blk_rq_sectors(req);
1329
Maya Erez775a9362013-04-18 15:41:55 +03001330 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1331 arg = MMC_SECURE_TRIM1_ARG;
1332 else
1333 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001334
Adrian Hunter67716322011-08-29 16:42:15 +03001335retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001336 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1337 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1338 INAND_CMD38_ARG_EXT_CSD,
1339 arg == MMC_SECURE_TRIM1_ARG ?
1340 INAND_CMD38_ARG_SECTRIM1 :
1341 INAND_CMD38_ARG_SECERASE,
1342 0);
1343 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001344 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001345 }
Adrian Hunter28302812012-04-05 14:45:48 +03001346
Adrian Hunter49804542010-08-11 14:17:50 -07001347 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001348 if (err == -EIO)
1349 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001350 if (err) {
1351 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001352 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001353 }
Adrian Hunter28302812012-04-05 14:45:48 +03001354
1355 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001356 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1357 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1358 INAND_CMD38_ARG_EXT_CSD,
1359 INAND_CMD38_ARG_SECTRIM2,
1360 0);
1361 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001362 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001363 }
Adrian Hunter28302812012-04-05 14:45:48 +03001364
Adrian Hunter49804542010-08-11 14:17:50 -07001365 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001366 if (err == -EIO)
1367 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001368 if (err) {
1369 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001370 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001371 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001372 }
Adrian Hunter28302812012-04-05 14:45:48 +03001373
Adrian Hunter28302812012-04-05 14:45:48 +03001374out_retry:
1375 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001376 goto retry;
1377 if (!err)
1378 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001379out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001380 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001381}
1382
Linus Walleijdf061582017-01-24 11:17:57 +01001383static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001384{
Linus Walleij7db30282016-11-18 13:36:15 +01001385 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001386 struct mmc_card *card = md->queue.card;
1387 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001388
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001389 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001390 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001391}
1392
1393/*
1394 * Reformat current write as a reliable write, supporting
1395 * both legacy and the enhanced reliable write MMC cards.
1396 * In each transfer we'll handle only as much as a single
1397 * reliable write can handle, thus finish the request in
1398 * partial completions.
1399 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001400static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1401 struct mmc_card *card,
1402 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001403{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001404 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1405 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001406 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001407 brq->data.blocks = 1;
1408
1409 if (brq->data.blocks > card->ext_csd.rel_sectors)
1410 brq->data.blocks = card->ext_csd.rel_sectors;
1411 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1412 brq->data.blocks = 1;
1413 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001414}
1415
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001416#define CMD_ERRORS \
1417 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1418 R1_ADDRESS_ERROR | /* Misaligned address */ \
1419 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1420 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001421 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001422 R1_CC_ERROR | /* Card controller error */ \
1423 R1_ERROR) /* General/unknown error */
1424
Shawn Lind83c2db2017-08-18 09:16:08 +08001425static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001426{
Shawn Lind83c2db2017-08-18 09:16:08 +08001427 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001428
Shawn Lind83c2db2017-08-18 09:16:08 +08001429 /*
1430 * Per the SD specification(physical layer version 4.10)[1],
1431 * section 4.3.3, it explicitly states that "When the last
1432 * block of user area is read using CMD18, the host should
1433 * ignore OUT_OF_RANGE error that may occur even the sequence
1434 * is correct". And JESD84-B51 for eMMC also has a similar
1435 * statement on section 6.8.3.
1436 *
1437 * Multiple block read/write could be done by either predefined
1438 * method, namely CMD23, or open-ending mode. For open-ending mode,
1439 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1440 *
1441 * However the spec[1] doesn't tell us whether we should also
1442 * ignore that for predefined method. But per the spec[1], section
1443 * 4.15 Set Block Count Command, it says"If illegal block count
1444 * is set, out of range error will be indicated during read/write
1445 * operation (For example, data transfer is stopped at user area
1446 * boundary)." In another word, we could expect a out of range error
1447 * in the response for the following CMD18/25. And if argument of
1448 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1449 * we could also expect to get a -ETIMEDOUT or any error number from
1450 * the host drivers due to missing data response(for write)/data(for
1451 * read), as the cards will stop the data transfer by itself per the
1452 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1453 */
1454
1455 if (!brq->stop.error) {
1456 bool oor_with_open_end;
1457 /* If there is no error yet, check R1 response */
1458
1459 val = brq->stop.resp[0] & CMD_ERRORS;
1460 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1461
1462 if (val && !oor_with_open_end)
1463 brq->stop.error = -EIO;
1464 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001465}
1466
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001467static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1468 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001469{
Per Forlinee8a43a2011-07-01 18:55:33 +02001470 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001471 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001472 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001473 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001474 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001475 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001476 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001477
1478 /*
1479 * sbc.error indicates a problem with the set block count
1480 * command. No data will have been transferred.
1481 *
1482 * cmd.error indicates a problem with the r/w command. No
1483 * data will have been transferred.
1484 *
1485 * stop.error indicates a problem with the stop command. Data
1486 * may have been transferred, or may still be transferring.
1487 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001488
1489 mmc_blk_eval_resp_error(brq);
1490
1491 if (brq->sbc.error || brq->cmd.error ||
1492 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001493 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001494 case ERR_RETRY:
1495 return MMC_BLK_RETRY;
1496 case ERR_ABORT:
1497 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301498 case ERR_NOMEDIUM:
1499 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001500 case ERR_CONTINUE:
1501 break;
1502 }
1503 }
1504
1505 /*
1506 * Check for errors relating to the execution of the
1507 * initial command - such as address errors. No data
1508 * has been transferred.
1509 */
1510 if (brq->cmd.resp[0] & CMD_ERRORS) {
1511 pr_err("%s: r/w command failed, status = %#x\n",
1512 req->rq_disk->disk_name, brq->cmd.resp[0]);
1513 return MMC_BLK_ABORT;
1514 }
1515
1516 /*
1517 * Everything else is either success, or a data error of some
1518 * kind. If it was a write, we may have transitioned to
1519 * program mode, which we have to wait for it to complete.
1520 */
1521 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001522 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001523
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001524 /* Check stop command response */
1525 if (brq->stop.resp[0] & R1_ERROR) {
1526 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1527 req->rq_disk->disk_name, __func__,
1528 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001529 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001530 }
1531
Ulf Hansson95a91292014-01-29 13:11:27 +01001532 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1533 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001534 if (err)
1535 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001536 }
1537
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001538 /* if general error occurs, retry the write operation. */
1539 if (gen_err) {
1540 pr_warn("%s: retrying write for general error\n",
1541 req->rq_disk->disk_name);
1542 return MMC_BLK_RETRY;
1543 }
1544
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001545 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1546 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001547 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001548 pr_debug("%s: retrying because a re-tune was needed\n",
1549 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001550 brq->retune_retry_done = 1;
1551 return MMC_BLK_RETRY;
1552 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001553 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 +02001554 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001555 (unsigned)blk_rq_pos(req),
1556 (unsigned)blk_rq_sectors(req),
1557 brq->cmd.resp[0], brq->stop.resp[0]);
1558
1559 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001560 if (ecc_err)
1561 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001562 return MMC_BLK_DATA_ERR;
1563 } else {
1564 return MMC_BLK_CMD_ERR;
1565 }
1566 }
1567
Adrian Hunter67716322011-08-29 16:42:15 +03001568 if (!brq->data.bytes_xfered)
1569 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001570
Adrian Hunter67716322011-08-29 16:42:15 +03001571 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1572 return MMC_BLK_PARTIAL;
1573
1574 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001575}
1576
Adrian Hunterca5717f2017-03-13 14:36:41 +02001577static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001578 int disable_multi, bool *do_rel_wr_p,
1579 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001581 struct mmc_blk_data *md = mq->blkdata;
1582 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001583 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001584 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001585 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001587 /*
1588 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001589 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001590 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001591 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1592 rq_data_dir(req) == WRITE &&
1593 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001594
Per Forlin54d49d72011-07-01 18:55:29 +02001595 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001596
Per Forlin54d49d72011-07-01 18:55:29 +02001597 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Per Forlin54d49d72011-07-01 18:55:29 +02001599 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1600 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001601
1602 if (rq_data_dir(req) == READ) {
1603 brq->data.flags = MMC_DATA_READ;
1604 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1605 } else {
1606 brq->data.flags = MMC_DATA_WRITE;
1607 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1608 }
1609
1610 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001611 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Per Forlin54d49d72011-07-01 18:55:29 +02001613 /*
1614 * The block layer doesn't support all sector count
1615 * restrictions, so we need to be prepared for too big
1616 * requests.
1617 */
1618 if (brq->data.blocks > card->host->max_blk_count)
1619 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001621 if (brq->data.blocks > 1) {
1622 /*
1623 * After a read error, we redo the request one sector
1624 * at a time in order to accurately determine which
1625 * sectors can be read successfully.
1626 */
1627 if (disable_multi)
1628 brq->data.blocks = 1;
1629
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001630 /*
1631 * Some controllers have HW issues while operating
1632 * in multiple I/O mode
1633 */
1634 if (card->host->ops->multi_io_quirk)
1635 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1636 (rq_data_dir(req) == READ) ?
1637 MMC_DATA_READ : MMC_DATA_WRITE,
1638 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001639 }
Per Forlin54d49d72011-07-01 18:55:29 +02001640
Adrian Hunterd3377c02017-09-22 15:36:55 +03001641 if (do_rel_wr)
Adrian Hunterca5717f2017-03-13 14:36:41 +02001642 mmc_apply_rel_rw(brq, card, req);
1643
1644 /*
1645 * Data tag is used only during writing meta data to speed
1646 * up write and any subsequent read of this meta data
1647 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001648 do_data_tag = card->ext_csd.data_tag_unit_size &&
1649 (req->cmd_flags & REQ_META) &&
1650 (rq_data_dir(req) == WRITE) &&
1651 ((brq->data.blocks * brq->data.blksz) >=
1652 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001653
1654 mmc_set_data_timeout(&brq->data, card);
1655
1656 brq->data.sg = mqrq->sg;
1657 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1658
1659 /*
1660 * Adjust the sg list so it is the same size as the
1661 * request.
1662 */
1663 if (brq->data.blocks != blk_rq_sectors(req)) {
1664 int i, data_size = brq->data.blocks << 9;
1665 struct scatterlist *sg;
1666
1667 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1668 data_size -= sg->length;
1669 if (data_size <= 0) {
1670 sg->length += data_size;
1671 i++;
1672 break;
1673 }
1674 }
1675 brq->data.sg_len = i;
1676 }
1677
1678 mqrq->areq.mrq = &brq->mrq;
Adrian Hunterd3377c02017-09-22 15:36:55 +03001679
1680 if (do_rel_wr_p)
1681 *do_rel_wr_p = do_rel_wr;
1682
1683 if (do_data_tag_p)
1684 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001685}
1686
1687static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1688 struct mmc_card *card,
1689 int disable_multi,
1690 struct mmc_queue *mq)
1691{
1692 u32 readcmd, writecmd;
1693 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001694 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001695 struct mmc_blk_data *md = mq->blkdata;
1696 bool do_rel_wr, do_data_tag;
1697
1698 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1699
1700 brq->mrq.cmd = &brq->cmd;
1701
1702 brq->cmd.arg = blk_rq_pos(req);
1703 if (!mmc_card_blockaddr(card))
1704 brq->cmd.arg <<= 9;
1705 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1706
Per Forlin54d49d72011-07-01 18:55:29 +02001707 if (brq->data.blocks > 1 || do_rel_wr) {
1708 /* SPI multiblock writes terminate using a special
1709 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001710 */
Per Forlin54d49d72011-07-01 18:55:29 +02001711 if (!mmc_host_is_spi(card->host) ||
1712 rq_data_dir(req) == READ)
1713 brq->mrq.stop = &brq->stop;
1714 readcmd = MMC_READ_MULTIPLE_BLOCK;
1715 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1716 } else {
1717 brq->mrq.stop = NULL;
1718 readcmd = MMC_READ_SINGLE_BLOCK;
1719 writecmd = MMC_WRITE_BLOCK;
1720 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001721 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301722
1723 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001724 * Pre-defined multi-block transfers are preferable to
1725 * open ended-ones (and necessary for reliable writes).
1726 * However, it is not sufficient to just send CMD23,
1727 * and avoid the final CMD12, as on an error condition
1728 * CMD12 (stop) needs to be sent anyway. This, coupled
1729 * with Auto-CMD23 enhancements provided by some
1730 * hosts, means that the complexity of dealing
1731 * with this is best left to the host. If CMD23 is
1732 * supported by card and host, we'll fill sbc in and let
1733 * the host deal with handling it correctly. This means
1734 * that for hosts that don't expose MMC_CAP_CMD23, no
1735 * change of behavior will be observed.
1736 *
1737 * N.B: Some MMC cards experience perf degradation.
1738 * We'll avoid using CMD23-bounded multiblock writes for
1739 * these, while retaining features like reliable writes.
1740 */
Saugata Das42659002011-12-21 13:09:17 +05301741 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1742 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1743 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001744 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1745 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301746 (do_rel_wr ? (1 << 31) : 0) |
1747 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001748 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1749 brq->mrq.sbc = &brq->sbc;
1750 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001751
Linus Walleij74f5ba32017-02-01 13:47:55 +01001752 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001753}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Walleij0e65f102017-02-01 13:47:58 +01001755static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1756 struct mmc_blk_request *brq, struct request *req,
1757 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001758{
Linus Walleij0e65f102017-02-01 13:47:58 +01001759 bool req_pending;
1760
Adrian Hunter67716322011-08-29 16:42:15 +03001761 /*
1762 * If this is an SD card and we're writing, we can first
1763 * mark the known good sectors as ok.
1764 *
1765 * If the card is not SD, we can still ok written sectors
1766 * as reported by the controller (which might be less than
1767 * the real number of written sectors, but never more).
1768 */
1769 if (mmc_card_sd(card)) {
1770 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001771 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001772
Linus Walleij169f03a2017-02-01 13:47:57 +01001773 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001774 if (err)
1775 req_pending = old_req_pending;
1776 else
Adrian Huntera7c17d82017-08-25 15:43:45 +03001777 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001778 } else {
Adrian Huntera7c17d82017-08-25 15:43:45 +03001779 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001780 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001781 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001782}
1783
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001784static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1785 struct request *req,
1786 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001787{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001788 if (mmc_card_removed(card))
1789 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001790 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001791 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001792}
1793
Linus Walleijb2928e12017-02-01 13:47:54 +01001794/**
1795 * mmc_blk_rw_try_restart() - tries to restart the current async request
1796 * @mq: the queue with the card and host to restart
1797 * @req: a new request that want to be started after the current one
1798 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001799static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1800 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001801{
1802 if (!req)
1803 return;
1804
Linus Walleijb2928e12017-02-01 13:47:54 +01001805 /*
1806 * If the card was removed, just cancel everything and return.
1807 */
1808 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001809 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001810 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001811 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001812 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001813 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001814 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001815 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1816 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001817}
1818
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001819static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001820{
Linus Walleij7db30282016-11-18 13:36:15 +01001821 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001822 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001823 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001824 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001825 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001826 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001827 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001828 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001829 struct mmc_async_req *new_areq;
1830 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001831 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001832
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001833 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001834 mqrq_cur = req_to_mmc_queue_req(new_req);
1835 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001836 }
1837
1838 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001839 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001840
1841 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001842 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301843 /*
1844 * When 4KB native sector is enabled, only 8 blocks
1845 * multiple read or write is allowed
1846 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001847 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001848 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301849 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001850 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001851 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001852 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301853 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001854
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001855 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1856 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001857 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001858 new_areq = NULL;
1859
Linus Walleijc3399ef2017-02-01 13:47:53 +01001860 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001861 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001862 /*
1863 * We have just put the first request into the pipeline
1864 * and there is nothing more to do until it is
1865 * complete.
1866 */
Linus Walleijdf061582017-01-24 11:17:57 +01001867 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001868 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001869
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001870 /*
1871 * An asynchronous request has been completed and we proceed
1872 * to handle the result of it.
1873 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001874 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001875 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001876 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001877 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001878
Per Forlind78d4a8a2011-07-01 18:55:30 +02001879 switch (status) {
1880 case MMC_BLK_SUCCESS:
1881 case MMC_BLK_PARTIAL:
1882 /*
1883 * A block was successfully transferred.
1884 */
Adrian Hunter67716322011-08-29 16:42:15 +03001885 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001886
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001887 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001888 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001889 /*
1890 * If the blk_end_request function returns non-zero even
1891 * though all data has been transferred and no errors
1892 * were returned by the host controller, it's a bug.
1893 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001894 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301895 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001896 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001897 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001898 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001899 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001900 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001901 break;
1902 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001903 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001904 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001905 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001906 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1907 else
Linus Walleij304419d2017-05-18 11:29:32 +02001908 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001909 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001910 return;
1911 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001912 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001913 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001914 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001915 return;
1916 }
Ding Wang29535f72015-05-18 20:14:15 +08001917 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001918 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001919 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001920 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001921 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001922 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001923 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001924 if (!mmc_blk_reset(md, card->host, type))
1925 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001926 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001927 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001928 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001929 case MMC_BLK_DATA_ERR: {
1930 int err;
1931
1932 err = mmc_blk_reset(md, card->host, type);
1933 if (!err)
1934 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001935 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001936 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001937 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001938 return;
1939 }
Adrian Hunter67716322011-08-29 16:42:15 +03001940 /* Fall through */
1941 }
1942 case MMC_BLK_ECC_ERR:
1943 if (brq->data.blocks > 1) {
1944 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001945 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001946 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001947 disable_multi = 1;
1948 break;
1949 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001950 /*
1951 * After an error, we redo I/O one sector at a
1952 * time, so we only reach here after trying to
1953 * read a single sector.
1954 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001955 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001956 brq->data.blksz);
1957 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001958 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001959 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001960 return;
1961 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001962 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301963 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001964 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001965 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001966 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001967 default:
1968 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001969 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001970 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001971 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001972 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001973 }
1974
Linus Walleij0e65f102017-02-01 13:47:58 +01001975 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001976 /*
1977 * In case of a incomplete request
1978 * prepare it again and resend.
1979 */
1980 mmc_blk_rw_rq_prep(mq_rq, card,
1981 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001982 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001983 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001984 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001985 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001986 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001987
Linus Walleij304419d2017-05-18 11:29:32 +02001988 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Linus Walleijdf061582017-01-24 11:17:57 +01001991void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001992{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001993 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001994 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001995 struct mmc_card *card = md->queue.card;
1996
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001997 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001998 /* claim host only for the first request */
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03001999 mmc_get_card(card, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02002000
Linus Walleij1f797ed2017-08-20 23:39:10 +02002001 ret = mmc_blk_part_switch(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002002 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002003 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002004 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002005 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002006 goto out;
2007 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002008
Linus Walleij614f0382017-05-18 11:29:34 +02002009 if (req) {
2010 switch (req_op(req)) {
2011 case REQ_OP_DRV_IN:
2012 case REQ_OP_DRV_OUT:
2013 /*
2014 * Complete ongoing async transfer before issuing
2015 * ioctl()s
2016 */
2017 if (mq->qcnt)
2018 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02002019 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02002020 break;
2021 case REQ_OP_DISCARD:
2022 /*
2023 * Complete ongoing async transfer before issuing
2024 * discard.
2025 */
2026 if (mq->qcnt)
2027 mmc_blk_issue_rw_rq(mq, NULL);
2028 mmc_blk_issue_discard_rq(mq, req);
2029 break;
2030 case REQ_OP_SECURE_ERASE:
2031 /*
2032 * Complete ongoing async transfer before issuing
2033 * secure erase.
2034 */
2035 if (mq->qcnt)
2036 mmc_blk_issue_rw_rq(mq, NULL);
2037 mmc_blk_issue_secdiscard_rq(mq, req);
2038 break;
2039 case REQ_OP_FLUSH:
2040 /*
2041 * Complete ongoing async transfer before issuing
2042 * flush.
2043 */
2044 if (mq->qcnt)
2045 mmc_blk_issue_rw_rq(mq, NULL);
2046 mmc_blk_issue_flush(mq, req);
2047 break;
2048 default:
2049 /* Normal request, just issue it */
2050 mmc_blk_issue_rw_rq(mq, req);
2051 card->host->context_info.is_waiting_last_req = false;
2052 break;
Wu Fengguang73222382017-05-23 05:11:33 +08002053 }
Adrian Hunter49804542010-08-11 14:17:50 -07002054 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002055 /* No request, flushing the pipeline with NULL */
2056 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002057 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002058 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002059
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002061 if (!mq->qcnt)
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002062 mmc_put_card(card, NULL);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002063}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Russell Kinga6f6c962006-01-03 22:38:44 +00002065static inline int mmc_blk_readonly(struct mmc_card *card)
2066{
2067 return mmc_card_readonly(card) ||
2068 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2069}
2070
Andrei Warkentin371a6892011-04-11 18:10:25 -05002071static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2072 struct device *parent,
2073 sector_t size,
2074 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002075 const char *subname,
2076 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 struct mmc_blk_data *md;
2079 int devidx, ret;
2080
Heiner Kallweita04848c2017-02-01 19:44:22 +01002081 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002082 if (devidx < 0) {
2083 /*
2084 * We get -ENOSPC because there are no more any available
2085 * devidx. The reason may be that, either userspace haven't yet
2086 * unmounted the partitions, which postpones mmc_blk_release()
2087 * from being called, or the device has more partitions than
2088 * what we support.
2089 */
2090 if (devidx == -ENOSPC)
2091 dev_err(mmc_dev(card->host),
2092 "no more device IDs available\n");
2093
Heiner Kallweita04848c2017-02-01 19:44:22 +01002094 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002097 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002098 if (!md) {
2099 ret = -ENOMEM;
2100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002102
Johan Rudholmadd710e2011-12-02 08:51:06 +01002103 md->area_type = area_type;
2104
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002105 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002106 * Set the read-only status based on the supported commands
2107 * and the write protect switch.
2108 */
2109 md->read_only = mmc_blk_readonly(card);
2110
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002111 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002112 if (md->disk == NULL) {
2113 ret = -ENOMEM;
2114 goto err_kfree;
2115 }
2116
2117 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002118 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002119 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002120 md->usage = 1;
2121
Adrian Hunterd09408a2011-06-23 13:40:28 +03002122 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002123 if (ret)
2124 goto err_putdisk;
2125
Linus Walleij7db30282016-11-18 13:36:15 +01002126 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002127
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002128 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002129 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002130 md->disk->fops = &mmc_bdops;
2131 md->disk->private_data = md;
2132 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002133 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002134 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002135 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002136 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002137 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002138
2139 /*
2140 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2141 *
2142 * - be set for removable media with permanent block devices
2143 * - be unset for removable block devices with permanent media
2144 *
2145 * Since MMC block devices clearly fall under the second
2146 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2147 * should use the block device creation/destruction hotplug
2148 * messages to tell when the card is present.
2149 */
2150
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002151 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002152 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002153
Saugata Dasa5075eb2012-05-17 16:32:21 +05302154 if (mmc_card_mmc(card))
2155 blk_queue_logical_block_size(md->queue.queue,
2156 card->ext_csd.data_sector_size);
2157 else
2158 blk_queue_logical_block_size(md->queue.queue, 512);
2159
Andrei Warkentin371a6892011-04-11 18:10:25 -05002160 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002161
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002162 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002163 if ((mmc_card_mmc(card) &&
2164 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002165 (mmc_card_sd(card) &&
2166 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2167 md->flags |= MMC_BLK_CMD23;
2168 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002169
2170 if (mmc_card_mmc(card) &&
2171 md->flags & MMC_BLK_CMD23 &&
2172 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2173 card->ext_csd.rel_sectors)) {
2174 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002175 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002176 }
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002179
2180 err_putdisk:
2181 put_disk(md->disk);
2182 err_kfree:
2183 kfree(md);
2184 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002185 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002186 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Andrei Warkentin371a6892011-04-11 18:10:25 -05002189static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2190{
2191 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002192
2193 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2194 /*
2195 * The EXT_CSD sector count is in number or 512 byte
2196 * sectors.
2197 */
2198 size = card->ext_csd.sectors;
2199 } else {
2200 /*
2201 * The CSD capacity field is in units of read_blkbits.
2202 * set_capacity takes units of 512 bytes.
2203 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002204 size = (typeof(sector_t))card->csd.capacity
2205 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206 }
2207
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002208 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002209 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002210}
2211
2212static int mmc_blk_alloc_part(struct mmc_card *card,
2213 struct mmc_blk_data *md,
2214 unsigned int part_type,
2215 sector_t size,
2216 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002217 const char *subname,
2218 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002219{
2220 char cap_str[10];
2221 struct mmc_blk_data *part_md;
2222
2223 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002224 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225 if (IS_ERR(part_md))
2226 return PTR_ERR(part_md);
2227 part_md->part_type = part_type;
2228 list_add(&part_md->part, &md->part);
2229
James Bottomleyb9f28d82015-03-05 18:47:01 -08002230 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002231 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302232 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002233 part_md->disk->disk_name, mmc_card_id(card),
2234 mmc_card_name(card), part_md->part_type, cap_str);
2235 return 0;
2236}
2237
Linus Walleij97548572017-09-20 10:02:00 +02002238/**
2239 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2240 * @filp: the character device file
2241 * @cmd: the ioctl() command
2242 * @arg: the argument from userspace
2243 *
2244 * This will essentially just redirect the ioctl()s coming in over to
2245 * the main block device spawning the RPMB character device.
2246 */
2247static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2248 unsigned long arg)
2249{
2250 struct mmc_rpmb_data *rpmb = filp->private_data;
2251 int ret;
2252
2253 switch (cmd) {
2254 case MMC_IOC_CMD:
2255 ret = mmc_blk_ioctl_cmd(rpmb->md,
2256 (struct mmc_ioc_cmd __user *)arg,
2257 rpmb);
2258 break;
2259 case MMC_IOC_MULTI_CMD:
2260 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2261 (struct mmc_ioc_multi_cmd __user *)arg,
2262 rpmb);
2263 break;
2264 default:
2265 ret = -EINVAL;
2266 break;
2267 }
2268
2269 return 0;
2270}
2271
2272#ifdef CONFIG_COMPAT
2273static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2274 unsigned long arg)
2275{
2276 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2277}
2278#endif
2279
2280static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2281{
2282 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2283 struct mmc_rpmb_data, chrdev);
2284
2285 get_device(&rpmb->dev);
2286 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02002287 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02002288
2289 return nonseekable_open(inode, filp);
2290}
2291
2292static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2293{
2294 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2295 struct mmc_rpmb_data, chrdev);
2296
2297 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002298 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02002299
2300 return 0;
2301}
2302
2303static const struct file_operations mmc_rpmb_fileops = {
2304 .release = mmc_rpmb_chrdev_release,
2305 .open = mmc_rpmb_chrdev_open,
2306 .owner = THIS_MODULE,
2307 .llseek = no_llseek,
2308 .unlocked_ioctl = mmc_rpmb_ioctl,
2309#ifdef CONFIG_COMPAT
2310 .compat_ioctl = mmc_rpmb_ioctl_compat,
2311#endif
2312};
2313
Linus Walleij1c87f732017-10-04 11:10:07 +02002314static void mmc_blk_rpmb_device_release(struct device *dev)
2315{
2316 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2317
2318 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2319 kfree(rpmb);
2320}
Linus Walleij97548572017-09-20 10:02:00 +02002321
2322static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2323 struct mmc_blk_data *md,
2324 unsigned int part_index,
2325 sector_t size,
2326 const char *subname)
2327{
2328 int devidx, ret;
2329 char rpmb_name[DISK_NAME_LEN];
2330 char cap_str[10];
2331 struct mmc_rpmb_data *rpmb;
2332
2333 /* This creates the minor number for the RPMB char device */
2334 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2335 if (devidx < 0)
2336 return devidx;
2337
2338 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02002339 if (!rpmb) {
2340 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02002341 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02002342 }
Linus Walleij97548572017-09-20 10:02:00 +02002343
2344 snprintf(rpmb_name, sizeof(rpmb_name),
2345 "mmcblk%u%s", card->host->index, subname ? subname : "");
2346
2347 rpmb->id = devidx;
2348 rpmb->part_index = part_index;
2349 rpmb->dev.init_name = rpmb_name;
2350 rpmb->dev.bus = &mmc_rpmb_bus_type;
2351 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2352 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02002353 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02002354 device_initialize(&rpmb->dev);
2355 dev_set_drvdata(&rpmb->dev, rpmb);
2356 rpmb->md = md;
2357
2358 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2359 rpmb->chrdev.owner = THIS_MODULE;
2360 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2361 if (ret) {
2362 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02002363 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02002364 }
2365
2366 list_add(&rpmb->node, &md->rpmbs);
2367
2368 string_get_size((u64)size, 512, STRING_UNITS_2,
2369 cap_str, sizeof(cap_str));
2370
2371 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2372 rpmb_name, mmc_card_id(card),
2373 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2374 MAJOR(mmc_rpmb_devt), rpmb->id);
2375
2376 return 0;
2377
Linus Walleij1c87f732017-10-04 11:10:07 +02002378out_put_device:
2379 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002380 return ret;
2381}
2382
2383static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02002384
Linus Walleij97548572017-09-20 10:02:00 +02002385{
2386 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002387 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002388}
2389
Namjae Jeone0c368d2011-10-06 23:41:38 +09002390/* MMC Physical partitions consist of two boot partitions and
2391 * up to four general purpose partitions.
2392 * For each partition enabled in EXT_CSD a block device will be allocatedi
2393 * to provide access to the partition.
2394 */
2395
Andrei Warkentin371a6892011-04-11 18:10:25 -05002396static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2397{
Linus Walleij97548572017-09-20 10:02:00 +02002398 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002399
2400 if (!mmc_card_mmc(card))
2401 return 0;
2402
Namjae Jeone0c368d2011-10-06 23:41:38 +09002403 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02002404 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2405 /*
2406 * RPMB partitions does not provide block access, they
2407 * are only accessed using ioctl():s. Thus create
2408 * special RPMB block devices that do not have a
2409 * backing block queue for these.
2410 */
2411 ret = mmc_blk_alloc_rpmb_part(card, md,
2412 card->part[idx].part_cfg,
2413 card->part[idx].size >> 9,
2414 card->part[idx].name);
2415 if (ret)
2416 return ret;
2417 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09002418 ret = mmc_blk_alloc_part(card, md,
2419 card->part[idx].part_cfg,
2420 card->part[idx].size >> 9,
2421 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002422 card->part[idx].name,
2423 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002424 if (ret)
2425 return ret;
2426 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002427 }
2428
Linus Walleij97548572017-09-20 10:02:00 +02002429 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002430}
2431
Andrei Warkentin371a6892011-04-11 18:10:25 -05002432static void mmc_blk_remove_req(struct mmc_blk_data *md)
2433{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002434 struct mmc_card *card;
2435
Andrei Warkentin371a6892011-04-11 18:10:25 -05002436 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002437 /*
2438 * Flush remaining requests and free queues. It
2439 * is freeing the queue that stops new requests
2440 * from being accepted.
2441 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002442 card = md->queue.card;
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002443 spin_lock_irq(md->queue.queue->queue_lock);
Shawn Lin7c84b8b2017-07-25 09:11:28 +08002444 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002445 spin_unlock_irq(md->queue.queue->queue_lock);
Grzegorz Slujabbdc74d2017-07-13 11:17:58 +02002446 blk_set_queue_dying(md->queue.queue);
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002447 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002448 if (md->disk->flags & GENHD_FL_UP) {
2449 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002450 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2451 card->ext_csd.boot_ro_lockable)
2452 device_remove_file(disk_to_dev(md->disk),
2453 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002454
Andrei Warkentin371a6892011-04-11 18:10:25 -05002455 del_gendisk(md->disk);
2456 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002457 mmc_blk_put(md);
2458 }
2459}
2460
2461static void mmc_blk_remove_parts(struct mmc_card *card,
2462 struct mmc_blk_data *md)
2463{
2464 struct list_head *pos, *q;
2465 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02002466 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002467
Linus Walleij97548572017-09-20 10:02:00 +02002468 /* Remove RPMB partitions */
2469 list_for_each_safe(pos, q, &md->rpmbs) {
2470 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2471 list_del(pos);
2472 mmc_blk_remove_rpmb_part(rpmb);
2473 }
2474 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05002475 list_for_each_safe(pos, q, &md->part) {
2476 part_md = list_entry(pos, struct mmc_blk_data, part);
2477 list_del(pos);
2478 mmc_blk_remove_req(part_md);
2479 }
2480}
2481
2482static int mmc_add_disk(struct mmc_blk_data *md)
2483{
2484 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002485 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002486
Dan Williams307d8e62016-06-20 10:40:44 -07002487 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002488 md->force_ro.show = force_ro_show;
2489 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302490 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002491 md->force_ro.attr.name = "force_ro";
2492 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2493 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2494 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002495 goto force_ro_fail;
2496
2497 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2498 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002499 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002500
2501 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2502 mode = S_IRUGO;
2503 else
2504 mode = S_IRUGO | S_IWUSR;
2505
2506 md->power_ro_lock.show = power_ro_lock_show;
2507 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002508 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002509 md->power_ro_lock.attr.mode = mode;
2510 md->power_ro_lock.attr.name =
2511 "ro_lock_until_next_power_on";
2512 ret = device_create_file(disk_to_dev(md->disk),
2513 &md->power_ro_lock);
2514 if (ret)
2515 goto power_ro_lock_fail;
2516 }
2517 return ret;
2518
2519power_ro_lock_fail:
2520 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2521force_ro_fail:
2522 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002523
2524 return ret;
2525}
2526
Linus Walleij627c3cc2017-08-20 23:39:08 +02002527#ifdef CONFIG_DEBUG_FS
2528
2529static int mmc_dbg_card_status_get(void *data, u64 *val)
2530{
2531 struct mmc_card *card = data;
2532 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2533 struct mmc_queue *mq = &md->queue;
2534 struct request *req;
2535 int ret;
2536
2537 /* Ask the block layer about the card status */
2538 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2539 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2540 blk_execute_rq(mq->queue, NULL, req, 0);
2541 ret = req_to_mmc_queue_req(req)->drv_op_result;
2542 if (ret >= 0) {
2543 *val = ret;
2544 ret = 0;
2545 }
2546
2547 return ret;
2548}
2549DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2550 NULL, "%08llx\n");
2551
2552/* That is two digits * 512 + 1 for newline */
2553#define EXT_CSD_STR_LEN 1025
2554
2555static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2556{
2557 struct mmc_card *card = inode->i_private;
2558 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2559 struct mmc_queue *mq = &md->queue;
2560 struct request *req;
2561 char *buf;
2562 ssize_t n = 0;
2563 u8 *ext_csd;
2564 int err, i;
2565
2566 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2567 if (!buf)
2568 return -ENOMEM;
2569
2570 /* Ask the block layer for the EXT CSD */
2571 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2572 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2573 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2574 blk_execute_rq(mq->queue, NULL, req, 0);
2575 err = req_to_mmc_queue_req(req)->drv_op_result;
2576 if (err) {
2577 pr_err("FAILED %d\n", err);
2578 goto out_free;
2579 }
2580
2581 for (i = 0; i < 512; i++)
2582 n += sprintf(buf + n, "%02x", ext_csd[i]);
2583 n += sprintf(buf + n, "\n");
2584
2585 if (n != EXT_CSD_STR_LEN) {
2586 err = -EINVAL;
2587 goto out_free;
2588 }
2589
2590 filp->private_data = buf;
2591 kfree(ext_csd);
2592 return 0;
2593
2594out_free:
2595 kfree(buf);
2596 return err;
2597}
2598
2599static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2600 size_t cnt, loff_t *ppos)
2601{
2602 char *buf = filp->private_data;
2603
2604 return simple_read_from_buffer(ubuf, cnt, ppos,
2605 buf, EXT_CSD_STR_LEN);
2606}
2607
2608static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2609{
2610 kfree(file->private_data);
2611 return 0;
2612}
2613
2614static const struct file_operations mmc_dbg_ext_csd_fops = {
2615 .open = mmc_ext_csd_open,
2616 .read = mmc_ext_csd_read,
2617 .release = mmc_ext_csd_release,
2618 .llseek = default_llseek,
2619};
2620
2621static int mmc_blk_add_debugfs(struct mmc_card *card)
2622{
2623 struct dentry *root;
2624
2625 if (!card->debugfs_root)
2626 return 0;
2627
2628 root = card->debugfs_root;
2629
2630 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2631 if (!debugfs_create_file("status", S_IRUSR, root, card,
2632 &mmc_dbg_card_status_fops))
2633 return -EIO;
2634 }
2635
2636 if (mmc_card_mmc(card)) {
2637 if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
2638 &mmc_dbg_ext_csd_fops))
2639 return -EIO;
2640 }
2641
2642 return 0;
2643}
2644
2645
2646#else
2647
2648static int mmc_blk_add_debugfs(struct mmc_card *card)
2649{
2650 return 0;
2651}
2652
2653#endif /* CONFIG_DEBUG_FS */
2654
Ulf Hansson96541ba2015-04-14 13:06:12 +02002655static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002657 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002658 char cap_str[10];
2659
Pierre Ossman912490d2005-05-21 10:27:02 +01002660 /*
2661 * Check that the card supports the command class(es) we need.
2662 */
2663 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return -ENODEV;
2665
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002666 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002669 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return PTR_ERR(md);
2671
James Bottomleyb9f28d82015-03-05 18:47:01 -08002672 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002673 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302674 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002676 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Andrei Warkentin371a6892011-04-11 18:10:25 -05002678 if (mmc_blk_alloc_parts(card, md))
2679 goto out;
2680
Ulf Hansson96541ba2015-04-14 13:06:12 +02002681 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002682
Andrei Warkentin371a6892011-04-11 18:10:25 -05002683 if (mmc_add_disk(md))
2684 goto out;
2685
2686 list_for_each_entry(part_md, &md->part, part) {
2687 if (mmc_add_disk(part_md))
2688 goto out;
2689 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002690
Linus Walleij627c3cc2017-08-20 23:39:08 +02002691 /* Add two debugfs entries */
2692 mmc_blk_add_debugfs(card);
2693
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002694 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2695 pm_runtime_use_autosuspend(&card->dev);
2696
2697 /*
2698 * Don't enable runtime PM for SD-combo cards here. Leave that
2699 * decision to be taken during the SDIO init sequence instead.
2700 */
2701 if (card->type != MMC_TYPE_SD_COMBO) {
2702 pm_runtime_set_active(&card->dev);
2703 pm_runtime_enable(&card->dev);
2704 }
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return 0;
2707
2708 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002709 mmc_blk_remove_parts(card, md);
2710 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
Ulf Hansson96541ba2015-04-14 13:06:12 +02002714static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002716 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Andrei Warkentin371a6892011-04-11 18:10:25 -05002718 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002719 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002720 mmc_claim_host(card->host);
Linus Walleij1f797ed2017-08-20 23:39:10 +02002721 mmc_blk_part_switch(card, md->part_type);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002722 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002723 if (card->type != MMC_TYPE_SD_COMBO)
2724 pm_runtime_disable(&card->dev);
2725 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002726 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002727 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Ulf Hansson96541ba2015-04-14 13:06:12 +02002730static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002732 struct mmc_blk_data *part_md;
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
2735 if (md) {
2736 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002737 list_for_each_entry(part_md, &md->part, part) {
2738 mmc_queue_suspend(&part_md->queue);
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 return 0;
2742}
2743
Ulf Hansson96541ba2015-04-14 13:06:12 +02002744static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002745{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002746 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002747}
2748
Ulf Hansson0967edc2014-10-06 11:29:42 +02002749#ifdef CONFIG_PM_SLEEP
2750static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002751{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002752 struct mmc_card *card = mmc_dev_to_card(dev);
2753
2754 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002755}
2756
Ulf Hansson0967edc2014-10-06 11:29:42 +02002757static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002759 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002760 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002763 /*
2764 * Resume involves the card going into idle state,
2765 * so current partition is always the main one.
2766 */
2767 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002769 list_for_each_entry(part_md, &md->part, part) {
2770 mmc_queue_resume(&part_md->queue);
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773 return 0;
2774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775#endif
2776
Ulf Hansson0967edc2014-10-06 11:29:42 +02002777static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2778
Ulf Hansson96541ba2015-04-14 13:06:12 +02002779static struct mmc_driver mmc_driver = {
2780 .drv = {
2781 .name = "mmcblk",
2782 .pm = &mmc_blk_pm_ops,
2783 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 .probe = mmc_blk_probe,
2785 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002786 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787};
2788
2789static int __init mmc_blk_init(void)
2790{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002791 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Linus Walleij97548572017-09-20 10:02:00 +02002793 res = bus_register(&mmc_rpmb_bus_type);
2794 if (res < 0) {
2795 pr_err("mmcblk: could not register RPMB bus type\n");
2796 return res;
2797 }
2798 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
2799 if (res < 0) {
2800 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
2801 goto out_bus_unreg;
2802 }
2803
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002804 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2805 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2806
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002807 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002808
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002809 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2810 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002811 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002813 res = mmc_register_driver(&mmc_driver);
2814 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002815 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002817 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02002818
2819out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002820 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002821out_chrdev_unreg:
2822 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2823out_bus_unreg:
2824 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return res;
2826}
2827
2828static void __exit mmc_blk_exit(void)
2829{
2830 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002831 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002832 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
2835module_init(mmc_blk_init);
2836module_exit(mmc_blk_exit);
2837
2838MODULE_LICENSE("GPL");
2839MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2840