blob: 4a319ddbd956db6925652a4f7a55afa4ffb935d6 [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);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200236 if (IS_ERR(req)) {
237 count = PTR_ERR(req);
238 goto out_put;
239 }
Linus Walleij0493f6f2017-05-19 15:37:30 +0200240 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
241 blk_execute_rq(mq->queue, NULL, req, 0);
242 ret = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +0200243 blk_put_request(req);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100244
245 if (!ret) {
246 pr_info("%s: Locking boot partition ro until next power on\n",
247 md->disk->disk_name);
248 set_disk_ro(md->disk, 1);
249
250 list_for_each_entry(part_md, &md->part, part)
251 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
252 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
253 set_disk_ro(part_md->disk, 1);
254 }
255 }
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200256out_put:
Johan Rudholmadd710e2011-12-02 08:51:06 +0100257 mmc_blk_put(md);
258 return count;
259}
260
Andrei Warkentin371a6892011-04-11 18:10:25 -0500261static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
262 char *buf)
263{
264 int ret;
265 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
266
Baruch Siach0031a982014-09-22 10:12:51 +0300267 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500268 get_disk_ro(dev_to_disk(dev)) ^
269 md->read_only);
270 mmc_blk_put(md);
271 return ret;
272}
273
274static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
276{
277 int ret;
278 char *end;
279 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
280 unsigned long set = simple_strtoul(buf, &end, 0);
281 if (end == buf) {
282 ret = -EINVAL;
283 goto out;
284 }
285
286 set_disk_ro(dev_to_disk(dev), set || md->read_only);
287 ret = count;
288out:
289 mmc_blk_put(md);
290 return ret;
291}
292
Al Viroa5a15612008-03-02 10:33:30 -0500293static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Al Viroa5a15612008-03-02 10:33:30 -0500295 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int ret = -ENXIO;
297
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200298 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (md) {
300 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500301 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700303
Al Viroa5a15612008-03-02 10:33:30 -0500304 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700305 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700306 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200309 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 return ret;
312}
313
Al Virodb2a1442013-05-05 21:52:57 -0400314static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Al Viroa5a15612008-03-02 10:33:30 -0500316 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200318 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200320 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800324mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800326 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
327 geo->heads = 4;
328 geo->sectors = 16;
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
John Calixtocb87ea22011-04-26 18:56:29 -0400332struct mmc_blk_ioc_data {
333 struct mmc_ioc_cmd ic;
334 unsigned char *buf;
335 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200336 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400337};
338
339static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
340 struct mmc_ioc_cmd __user *user)
341{
342 struct mmc_blk_ioc_data *idata;
343 int err;
344
yalin wang1ff89502015-11-12 19:27:11 +0800345 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400346 if (!idata) {
347 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
352 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400353 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400354 }
355
356 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
357 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
358 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400359 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400360 }
361
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300362 if (!idata->buf_bytes) {
363 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100364 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300365 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100366
yalin wang1ff89502015-11-12 19:27:11 +0800367 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400368 if (!idata->buf) {
369 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400370 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400371 }
372
373 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
374 idata->ic.data_ptr, idata->buf_bytes)) {
375 err = -EFAULT;
376 goto copy_err;
377 }
378
379 return idata;
380
381copy_err:
382 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400383idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400384 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400385out:
John Calixtocb87ea22011-04-26 18:56:29 -0400386 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400387}
388
Jon Huntera5f57742015-09-22 10:27:53 +0100389static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
390 struct mmc_blk_ioc_data *idata)
391{
392 struct mmc_ioc_cmd *ic = &idata->ic;
393
394 if (copy_to_user(&(ic_ptr->response), ic->response,
395 sizeof(ic->response)))
396 return -EFAULT;
397
398 if (!idata->ic.write_flag) {
399 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
400 idata->buf, idata->buf_bytes))
401 return -EFAULT;
402 }
403
404 return 0;
405}
406
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200407static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
408 u32 retries_max)
409{
410 int err;
411 u32 retry_count = 0;
412
413 if (!status || !retries_max)
414 return -EINVAL;
415
416 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200417 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200418 if (err)
419 break;
420
421 if (!R1_STATUS(*status) &&
422 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
423 break; /* RPMB programming operation complete */
424
425 /*
426 * Rechedule to give the MMC device a chance to continue
427 * processing the previous command without being polled too
428 * frequently.
429 */
430 usleep_range(1000, 5000);
431 } while (++retry_count < retries_max);
432
433 if (retry_count == retries_max)
434 err = -EPERM;
435
436 return err;
437}
438
Maya Erez775a9362013-04-18 15:41:55 +0300439static int ioctl_do_sanitize(struct mmc_card *card)
440{
441 int err;
442
Ulf Hanssona2d10862013-12-16 14:37:26 +0100443 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300444 pr_warn("%s: %s - SANITIZE is not supported\n",
445 mmc_hostname(card->host), __func__);
446 err = -EOPNOTSUPP;
447 goto out;
448 }
449
450 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
451 mmc_hostname(card->host), __func__);
452
453 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
454 EXT_CSD_SANITIZE_START, 1,
455 MMC_SANITIZE_REQ_TIMEOUT);
456
457 if (err)
458 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
459 mmc_hostname(card->host), __func__, err);
460
461 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
462 __func__);
463out:
464 return err;
465}
466
Jon Huntera5f57742015-09-22 10:27:53 +0100467static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
468 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400469{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900470 struct mmc_command cmd = {};
471 struct mmc_data data = {};
472 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400473 struct scatterlist sg;
474 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200475 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200476 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400477
Jon Huntera5f57742015-09-22 10:27:53 +0100478 if (!card || !md || !idata)
479 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400480
Linus Walleij97548572017-09-20 10:02:00 +0200481 /*
482 * The RPMB accesses comes in from the character device, so we
483 * need to target these explicitly. Else we just target the
484 * partition type for the block device the ioctl() was issued
485 * on.
486 */
487 if (idata->rpmb) {
488 /* Support multiple RPMB partitions */
489 target_part = idata->rpmb->part_index;
490 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
491 } else {
492 target_part = md->part_type;
493 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200494
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100495 cmd.opcode = idata->ic.opcode;
496 cmd.arg = idata->ic.arg;
497 cmd.flags = idata->ic.flags;
498
499 if (idata->buf_bytes) {
500 data.sg = &sg;
501 data.sg_len = 1;
502 data.blksz = idata->ic.blksz;
503 data.blocks = idata->ic.blocks;
504
505 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
506
507 if (idata->ic.write_flag)
508 data.flags = MMC_DATA_WRITE;
509 else
510 data.flags = MMC_DATA_READ;
511
512 /* data.flags must already be set before doing this. */
513 mmc_set_data_timeout(&data, card);
514
515 /* Allow overriding the timeout_ns for empirical tuning. */
516 if (idata->ic.data_timeout_ns)
517 data.timeout_ns = idata->ic.data_timeout_ns;
518
519 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
520 /*
521 * Pretend this is a data transfer and rely on the
522 * host driver to compute timeout. When all host
523 * drivers support cmd.cmd_timeout for R1B, this
524 * can be changed to:
525 *
526 * mrq.data = NULL;
527 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
528 */
529 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
530 }
531
532 mrq.data = &data;
533 }
534
535 mrq.cmd = &cmd;
536
Linus Walleij97548572017-09-20 10:02:00 +0200537 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200538 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100539 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200540
John Calixtocb87ea22011-04-26 18:56:29 -0400541 if (idata->ic.is_acmd) {
542 err = mmc_app_cmd(card->host, card);
543 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100544 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400545 }
546
Linus Walleij97548572017-09-20 10:02:00 +0200547 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200548 err = mmc_set_blockcount(card, data.blocks,
549 idata->ic.write_flag & (1 << 31));
550 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100551 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200552 }
553
Yaniv Gardia82e4842013-06-05 14:13:08 +0300554 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
555 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300556 err = ioctl_do_sanitize(card);
557
558 if (err)
559 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
560 __func__, err);
561
Jon Huntera5f57742015-09-22 10:27:53 +0100562 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300563 }
564
John Calixtocb87ea22011-04-26 18:56:29 -0400565 mmc_wait_for_req(card->host, &mrq);
566
567 if (cmd.error) {
568 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
569 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100570 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400571 }
572 if (data.error) {
573 dev_err(mmc_dev(card->host), "%s: data error %d\n",
574 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100575 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400576 }
577
578 /*
579 * According to the SD specs, some commands require a delay after
580 * issuing the command.
581 */
582 if (idata->ic.postsleep_min_us)
583 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
584
Jon Huntera5f57742015-09-22 10:27:53 +0100585 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400586
Linus Walleij97548572017-09-20 10:02:00 +0200587 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200588 /*
589 * Ensure RPMB command has completed by polling CMD13
590 * "Send Status".
591 */
592 err = ioctl_rpmb_card_status_poll(card, &status, 5);
593 if (err)
594 dev_err(mmc_dev(card->host),
595 "%s: Card Status=0x%08X, error %d\n",
596 __func__, status, err);
597 }
598
Jon Huntera5f57742015-09-22 10:27:53 +0100599 return err;
600}
601
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200602static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200603 struct mmc_ioc_cmd __user *ic_ptr,
604 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100605{
606 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200607 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200608 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100609 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700610 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200611 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100612
613 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
614 if (IS_ERR(idata))
615 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200616 /* This will be NULL on non-RPMB ioctl():s */
617 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100618
Jon Huntera5f57742015-09-22 10:27:53 +0100619 card = md->queue.card;
620 if (IS_ERR(card)) {
621 err = PTR_ERR(card);
622 goto cmd_done;
623 }
624
Linus Walleij614f0382017-05-18 11:29:34 +0200625 /*
626 * Dispatch the ioctl() into the block request queue.
627 */
628 mq = &md->queue;
629 req = blk_get_request(mq->queue,
630 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
631 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200632 if (IS_ERR(req)) {
633 err = PTR_ERR(req);
634 goto cmd_done;
635 }
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200636 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200637 req_to_mmc_queue_req(req)->drv_op =
638 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200639 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200640 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200641 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200642 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700643 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200644 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100645
John Calixtocb87ea22011-04-26 18:56:29 -0400646cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400647 kfree(idata->buf);
648 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700649 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400650}
651
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200652static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200653 struct mmc_ioc_multi_cmd __user *user,
654 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100655{
656 struct mmc_blk_ioc_data **idata = NULL;
657 struct mmc_ioc_cmd __user *cmds = user->cmds;
658 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200659 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700660 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100661 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200662 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100663
664 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
665 sizeof(num_of_cmds)))
666 return -EFAULT;
667
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200668 if (!num_of_cmds)
669 return 0;
670
Jon Huntera5f57742015-09-22 10:27:53 +0100671 if (num_of_cmds > MMC_IOC_MAX_CMDS)
672 return -EINVAL;
673
674 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
675 if (!idata)
676 return -ENOMEM;
677
678 for (i = 0; i < num_of_cmds; i++) {
679 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
680 if (IS_ERR(idata[i])) {
681 err = PTR_ERR(idata[i]);
682 num_of_cmds = i;
683 goto cmd_err;
684 }
Linus Walleij97548572017-09-20 10:02:00 +0200685 /* This will be NULL on non-RPMB ioctl():s */
686 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100687 }
688
Jon Huntera5f57742015-09-22 10:27:53 +0100689 card = md->queue.card;
690 if (IS_ERR(card)) {
691 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200692 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100693 }
694
Jon Huntera5f57742015-09-22 10:27:53 +0100695
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200696 /*
697 * Dispatch the ioctl()s into the block request queue.
698 */
699 mq = &md->queue;
700 req = blk_get_request(mq->queue,
701 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
702 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200703 if (IS_ERR(req)) {
704 err = PTR_ERR(req);
705 goto cmd_err;
706 }
Linus Walleij97548572017-09-20 10:02:00 +0200707 req_to_mmc_queue_req(req)->drv_op =
708 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200709 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200710 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
711 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200712 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100713
714 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700715 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100716 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100717
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200718 blk_put_request(req);
719
Jon Huntera5f57742015-09-22 10:27:53 +0100720cmd_err:
721 for (i = 0; i < num_of_cmds; i++) {
722 kfree(idata[i]->buf);
723 kfree(idata[i]);
724 }
725 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700726 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100727}
728
Linus Walleij61fe0e22017-08-20 23:39:09 +0200729static int mmc_blk_check_blkdev(struct block_device *bdev)
730{
731 /*
732 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
733 * whole block device, not on a partition. This prevents overspray
734 * between sibling partitions.
735 */
736 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
737 return -EPERM;
738 return 0;
739}
740
John Calixtocb87ea22011-04-26 18:56:29 -0400741static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
742 unsigned int cmd, unsigned long arg)
743{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200744 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200745 int ret;
746
Jon Huntera5f57742015-09-22 10:27:53 +0100747 switch (cmd) {
748 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200749 ret = mmc_blk_check_blkdev(bdev);
750 if (ret)
751 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200752 md = mmc_blk_get(bdev->bd_disk);
753 if (!md)
754 return -EINVAL;
755 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200756 (struct mmc_ioc_cmd __user *)arg,
757 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200758 mmc_blk_put(md);
759 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100760 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200761 ret = mmc_blk_check_blkdev(bdev);
762 if (ret)
763 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200764 md = mmc_blk_get(bdev->bd_disk);
765 if (!md)
766 return -EINVAL;
767 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200768 (struct mmc_ioc_multi_cmd __user *)arg,
769 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200770 mmc_blk_put(md);
771 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100772 default:
773 return -EINVAL;
774 }
John Calixtocb87ea22011-04-26 18:56:29 -0400775}
776
777#ifdef CONFIG_COMPAT
778static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
779 unsigned int cmd, unsigned long arg)
780{
781 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
782}
783#endif
784
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700785static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500786 .open = mmc_blk_open,
787 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800788 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400790 .ioctl = mmc_blk_ioctl,
791#ifdef CONFIG_COMPAT
792 .compat_ioctl = mmc_blk_compat_ioctl,
793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794};
795
Adrian Hunter025e3d52017-03-13 14:36:39 +0200796static int mmc_blk_part_switch_pre(struct mmc_card *card,
797 unsigned int part_type)
798{
799 int ret = 0;
800
801 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
802 if (card->ext_csd.cmdq_en) {
803 ret = mmc_cmdq_disable(card);
804 if (ret)
805 return ret;
806 }
807 mmc_retune_pause(card->host);
808 }
809
810 return ret;
811}
812
813static int mmc_blk_part_switch_post(struct mmc_card *card,
814 unsigned int part_type)
815{
816 int ret = 0;
817
818 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
819 mmc_retune_unpause(card->host);
820 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
821 ret = mmc_cmdq_enable(card);
822 }
823
824 return ret;
825}
826
Andrei Warkentin371a6892011-04-11 18:10:25 -0500827static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200828 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500829{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200830 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200831 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300832
Linus Walleij1f797ed2017-08-20 23:39:10 +0200833 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500834 return 0;
835
836 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300837 u8 part_config = card->ext_csd.part_config;
838
Linus Walleij1f797ed2017-08-20 23:39:10 +0200839 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200840 if (ret)
841 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300842
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300843 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200844 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500845
846 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300847 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500848 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300849 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200850 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500851 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300852 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300853
854 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300855
Adrian Hunter025e3d52017-03-13 14:36:39 +0200856 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300857 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500858
Linus Walleij1f797ed2017-08-20 23:39:10 +0200859 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200860 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500861}
862
Linus Walleij169f03a2017-02-01 13:47:57 +0100863static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700864{
865 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100866 u32 result;
867 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700868
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900869 struct mmc_request mrq = {};
870 struct mmc_command cmd = {};
871 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700872
873 struct scatterlist sg;
874
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700875 cmd.opcode = MMC_APP_CMD;
876 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700877 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700878
879 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700880 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100881 return err;
David Brownell7213d172007-08-08 09:10:23 -0700882 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100883 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700884
885 memset(&cmd, 0, sizeof(struct mmc_command));
886
887 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
888 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700889 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700890
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700891 data.blksz = 4;
892 data.blocks = 1;
893 data.flags = MMC_DATA_READ;
894 data.sg = &sg;
895 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530896 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700897
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700898 mrq.cmd = &cmd;
899 mrq.data = &data;
900
Ben Dooks051913d2009-06-08 23:33:57 +0100901 blocks = kmalloc(4, GFP_KERNEL);
902 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100903 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100904
905 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700906
907 mmc_wait_for_req(card->host, &mrq);
908
Ben Dooks051913d2009-06-08 23:33:57 +0100909 result = ntohl(*blocks);
910 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700911
Ben Dooks051913d2009-06-08 23:33:57 +0100912 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100913 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100914
Linus Walleij169f03a2017-02-01 13:47:57 +0100915 *written_blocks = result;
916
917 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700918}
919
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100920static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100921 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100922{
923 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
924 int err = 0;
925 u32 status;
926
927 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200928 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100929 if (err) {
930 pr_err("%s: error %d requesting status\n",
931 req->rq_disk->disk_name, err);
932 return err;
933 }
934
935 if (status & R1_ERROR) {
936 pr_err("%s: %s: error sending status cmd, status %#x\n",
937 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100938 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100939 }
940
Ulf Hansson95a91292014-01-29 13:11:27 +0100941 /* We may rely on the host hw to handle busy detection.*/
942 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
943 hw_busy_detect)
944 break;
945
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100946 /*
947 * Timeout if the device never becomes ready for data and never
948 * leaves the program state.
949 */
950 if (time_after(jiffies, timeout)) {
951 pr_err("%s: Card stuck in programming state! %s %s\n",
952 mmc_hostname(card->host),
953 req->rq_disk->disk_name, __func__);
954 return -ETIMEDOUT;
955 }
956
957 /*
958 * Some cards mishandle the status bits,
959 * so make sure to check both the busy
960 * indication and the card state.
961 */
962 } while (!(status & R1_READY_FOR_DATA) ||
963 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
964
965 return err;
966}
967
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100968static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100969 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100970{
971 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900972 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100973 int err;
974 bool use_r1b_resp = rq_data_dir(req) == WRITE;
975
976 /*
977 * Normally we use R1B responses for WRITE, but in cases where the host
978 * has specified a max_busy_timeout we need to validate it. A failure
979 * means we need to prevent the host from doing hw busy detection, which
980 * is done by converting to a R1 response instead.
981 */
982 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
983 use_r1b_resp = false;
984
985 cmd.opcode = MMC_STOP_TRANSMISSION;
986 if (use_r1b_resp) {
987 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
988 cmd.busy_timeout = timeout_ms;
989 } else {
990 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
991 }
992
993 err = mmc_wait_for_cmd(host, &cmd, 5);
994 if (err)
995 return err;
996
997 *stop_status = cmd.resp[0];
998
999 /* No need to check card status in case of READ. */
1000 if (rq_data_dir(req) == READ)
1001 return 0;
1002
1003 if (!mmc_host_is_spi(host) &&
1004 (*stop_status & R1_ERROR)) {
1005 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1006 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001007 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001008 }
1009
1010 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1011}
1012
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301013#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001014#define ERR_RETRY 2
1015#define ERR_ABORT 1
1016#define ERR_CONTINUE 0
1017
1018static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1019 bool status_valid, u32 status)
1020{
1021 switch (error) {
1022 case -EILSEQ:
1023 /* response crc error, retry the r/w cmd */
1024 pr_err("%s: %s sending %s command, card status %#x\n",
1025 req->rq_disk->disk_name, "response CRC error",
1026 name, status);
1027 return ERR_RETRY;
1028
1029 case -ETIMEDOUT:
1030 pr_err("%s: %s sending %s command, card status %#x\n",
1031 req->rq_disk->disk_name, "timed out", name, status);
1032
1033 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301034 if (!status_valid) {
1035 pr_err("%s: status not valid, retrying timeout\n",
1036 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001037 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301038 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001039
1040 /*
1041 * If it was a r/w cmd crc error, or illegal command
1042 * (eg, issued in wrong state) then retry - we should
1043 * have corrected the state problem above.
1044 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301045 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1046 pr_err("%s: command error, retrying timeout\n",
1047 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001048 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301049 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001050
1051 /* Otherwise abort the command */
1052 return ERR_ABORT;
1053
1054 default:
1055 /* We don't understand the error code the driver gave us */
1056 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1057 req->rq_disk->disk_name, error, status);
1058 return ERR_ABORT;
1059 }
1060}
1061
1062/*
1063 * Initial r/w and stop cmd error recovery.
1064 * We don't know whether the card received the r/w cmd or not, so try to
1065 * restore things back to a sane state. Essentially, we do this as follows:
1066 * - Obtain card status. If the first attempt to obtain card status fails,
1067 * the status word will reflect the failed status cmd, not the failed
1068 * r/w cmd. If we fail to obtain card status, it suggests we can no
1069 * longer communicate with the card.
1070 * - Check the card state. If the card received the cmd but there was a
1071 * transient problem with the response, it might still be in a data transfer
1072 * mode. Try to send it a stop command. If this fails, we can't recover.
1073 * - If the r/w cmd failed due to a response CRC error, it was probably
1074 * transient, so retry the cmd.
1075 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1076 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1077 * illegal cmd, retry.
1078 * Otherwise we don't understand what happened, so abort.
1079 */
1080static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001081 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001082{
1083 bool prev_cmd_status_valid = true;
1084 u32 status, stop_status = 0;
1085 int err, retry;
1086
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301087 if (mmc_card_removed(card))
1088 return ERR_NOMEDIUM;
1089
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001090 /*
1091 * Try to get card status which indicates both the card state
1092 * and why there was no response. If the first attempt fails,
1093 * we can't be sure the returned status is for the r/w command.
1094 */
1095 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001096 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001097 if (!err)
1098 break;
1099
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001100 /* Re-tune if needed */
1101 mmc_retune_recheck(card->host);
1102
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001103 prev_cmd_status_valid = false;
1104 pr_err("%s: error %d sending status command, %sing\n",
1105 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1106 }
1107
1108 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301109 if (err) {
1110 /* Check if the card is removed */
1111 if (mmc_detect_card_removed(card->host))
1112 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001113 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301114 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001115
Adrian Hunter67716322011-08-29 16:42:15 +03001116 /* Flag ECC errors */
1117 if ((status & R1_CARD_ECC_FAILED) ||
1118 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1119 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001120 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001121
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001122 /* Flag General errors */
1123 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1124 if ((status & R1_ERROR) ||
1125 (brq->stop.resp[0] & R1_ERROR)) {
1126 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1127 req->rq_disk->disk_name, __func__,
1128 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001129 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001130 }
1131
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001132 /*
1133 * Check the current card state. If it is in some data transfer
1134 * mode, tell it to stop (and hopefully transition back to TRAN.)
1135 */
1136 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1137 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001138 err = send_stop(card,
1139 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1140 req, gen_err, &stop_status);
1141 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001142 pr_err("%s: error %d sending stop command\n",
1143 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001144 /*
1145 * If the stop cmd also timed out, the card is probably
1146 * not present, so abort. Other errors are bad news too.
1147 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001148 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001149 }
1150
Adrian Hunter67716322011-08-29 16:42:15 +03001151 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001152 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001153 }
1154
1155 /* Check for set block count errors */
1156 if (brq->sbc.error)
1157 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1158 prev_cmd_status_valid, status);
1159
1160 /* Check for r/w command errors */
1161 if (brq->cmd.error)
1162 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1163 prev_cmd_status_valid, status);
1164
Adrian Hunter67716322011-08-29 16:42:15 +03001165 /* Data errors */
1166 if (!brq->stop.error)
1167 return ERR_CONTINUE;
1168
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001169 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001170 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 +01001171 req->rq_disk->disk_name, brq->stop.error,
1172 brq->cmd.resp[0], status);
1173
1174 /*
1175 * Subsitute in our own stop status as this will give the error
1176 * state which happened during the execution of the r/w command.
1177 */
1178 if (stop_status) {
1179 brq->stop.resp[0] = stop_status;
1180 brq->stop.error = 0;
1181 }
1182 return ERR_CONTINUE;
1183}
1184
Adrian Hunter67716322011-08-29 16:42:15 +03001185static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1186 int type)
1187{
1188 int err;
1189
1190 if (md->reset_done & type)
1191 return -EEXIST;
1192
1193 md->reset_done |= type;
1194 err = mmc_hw_reset(host);
1195 /* Ensure we switch back to the correct partition */
1196 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001197 struct mmc_blk_data *main_md =
1198 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001199 int part_err;
1200
1201 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001202 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001203 if (part_err) {
1204 /*
1205 * We have failed to get back into the correct
1206 * partition, so we need to abort the whole request.
1207 */
1208 return -ENODEV;
1209 }
1210 }
1211 return err;
1212}
1213
1214static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1215{
1216 md->reset_done &= ~type;
1217}
1218
Linus Walleij5ec12392017-05-19 15:37:29 +02001219/*
1220 * The non-block commands come back from the block layer after it queued it and
1221 * processed it with all other requests and then they get issued in this
1222 * function.
1223 */
1224static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1225{
1226 struct mmc_queue_req *mq_rq;
1227 struct mmc_card *card = mq->card;
1228 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001229 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001230 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001231 u8 **ext_csd;
1232 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001233 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001234 int i;
1235
1236 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001237 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001238
1239 switch (mq_rq->drv_op) {
1240 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001241 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001242 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001243 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001244 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001245 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001246 break;
1247 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001248 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001249 if (rpmb_ioctl)
1250 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001251 break;
1252 case MMC_DRV_OP_BOOT_WP:
1253 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1254 card->ext_csd.boot_ro_lock |
1255 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1256 card->ext_csd.part_time);
1257 if (ret)
1258 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1259 md->disk->disk_name, ret);
1260 else
1261 card->ext_csd.boot_ro_lock |=
1262 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001263 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001264 case MMC_DRV_OP_GET_CARD_STATUS:
1265 ret = mmc_send_status(card, &status);
1266 if (!ret)
1267 ret = status;
1268 break;
1269 case MMC_DRV_OP_GET_EXT_CSD:
1270 ext_csd = mq_rq->drv_op_data;
1271 ret = mmc_get_ext_csd(card, ext_csd);
1272 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001273 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001274 pr_err("%s: unknown driver specific operation\n",
1275 md->disk->disk_name);
1276 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001277 break;
1278 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001279 mq_rq->drv_op_result = ret;
Adrian Huntera7c17d82017-08-25 15:43:45 +03001280 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001281}
1282
Linus Walleijdf061582017-01-24 11:17:57 +01001283static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001284{
Linus Walleij7db30282016-11-18 13:36:15 +01001285 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001286 struct mmc_card *card = md->queue.card;
1287 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001288 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001289 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001290
Adrian Hunterbd788c92010-08-11 14:17:47 -07001291 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001292 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001293 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001294 }
1295
1296 from = blk_rq_pos(req);
1297 nr = blk_rq_sectors(req);
1298
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001299 if (mmc_can_discard(card))
1300 arg = MMC_DISCARD_ARG;
1301 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001302 arg = MMC_TRIM_ARG;
1303 else
1304 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001305 do {
1306 err = 0;
1307 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1308 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1309 INAND_CMD38_ARG_EXT_CSD,
1310 arg == MMC_TRIM_ARG ?
1311 INAND_CMD38_ARG_TRIM :
1312 INAND_CMD38_ARG_ERASE,
1313 0);
1314 }
1315 if (!err)
1316 err = mmc_erase(card, from, nr, arg);
1317 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001318 if (err)
1319 status = BLK_STS_IOERR;
1320 else
Adrian Hunter67716322011-08-29 16:42:15 +03001321 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001322fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001323 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001324}
1325
Linus Walleijdf061582017-01-24 11:17:57 +01001326static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001327 struct request *req)
1328{
Linus Walleij7db30282016-11-18 13:36:15 +01001329 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001330 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001331 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001332 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001333 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001334
Maya Erez775a9362013-04-18 15:41:55 +03001335 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001336 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001337 goto out;
1338 }
1339
1340 from = blk_rq_pos(req);
1341 nr = blk_rq_sectors(req);
1342
Maya Erez775a9362013-04-18 15:41:55 +03001343 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1344 arg = MMC_SECURE_TRIM1_ARG;
1345 else
1346 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001347
Adrian Hunter67716322011-08-29 16:42:15 +03001348retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001349 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1350 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1351 INAND_CMD38_ARG_EXT_CSD,
1352 arg == MMC_SECURE_TRIM1_ARG ?
1353 INAND_CMD38_ARG_SECTRIM1 :
1354 INAND_CMD38_ARG_SECERASE,
1355 0);
1356 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001357 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001358 }
Adrian Hunter28302812012-04-05 14:45:48 +03001359
Adrian Hunter49804542010-08-11 14:17:50 -07001360 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001361 if (err == -EIO)
1362 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001363 if (err) {
1364 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001365 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001366 }
Adrian Hunter28302812012-04-05 14:45:48 +03001367
1368 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001369 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1370 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1371 INAND_CMD38_ARG_EXT_CSD,
1372 INAND_CMD38_ARG_SECTRIM2,
1373 0);
1374 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001375 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001376 }
Adrian Hunter28302812012-04-05 14:45:48 +03001377
Adrian Hunter49804542010-08-11 14:17:50 -07001378 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001379 if (err == -EIO)
1380 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001381 if (err) {
1382 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001383 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001384 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001385 }
Adrian Hunter28302812012-04-05 14:45:48 +03001386
Adrian Hunter28302812012-04-05 14:45:48 +03001387out_retry:
1388 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001389 goto retry;
1390 if (!err)
1391 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001392out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001393 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001394}
1395
Linus Walleijdf061582017-01-24 11:17:57 +01001396static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001397{
Linus Walleij7db30282016-11-18 13:36:15 +01001398 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001399 struct mmc_card *card = md->queue.card;
1400 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001401
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001402 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001403 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001404}
1405
1406/*
1407 * Reformat current write as a reliable write, supporting
1408 * both legacy and the enhanced reliable write MMC cards.
1409 * In each transfer we'll handle only as much as a single
1410 * reliable write can handle, thus finish the request in
1411 * partial completions.
1412 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001413static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1414 struct mmc_card *card,
1415 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001416{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001417 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1418 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001419 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420 brq->data.blocks = 1;
1421
1422 if (brq->data.blocks > card->ext_csd.rel_sectors)
1423 brq->data.blocks = card->ext_csd.rel_sectors;
1424 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1425 brq->data.blocks = 1;
1426 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001427}
1428
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001429#define CMD_ERRORS \
1430 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1431 R1_ADDRESS_ERROR | /* Misaligned address */ \
1432 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1433 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001434 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001435 R1_CC_ERROR | /* Card controller error */ \
1436 R1_ERROR) /* General/unknown error */
1437
Shawn Lind83c2db2017-08-18 09:16:08 +08001438static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001439{
Shawn Lind83c2db2017-08-18 09:16:08 +08001440 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001441
Shawn Lind83c2db2017-08-18 09:16:08 +08001442 /*
1443 * Per the SD specification(physical layer version 4.10)[1],
1444 * section 4.3.3, it explicitly states that "When the last
1445 * block of user area is read using CMD18, the host should
1446 * ignore OUT_OF_RANGE error that may occur even the sequence
1447 * is correct". And JESD84-B51 for eMMC also has a similar
1448 * statement on section 6.8.3.
1449 *
1450 * Multiple block read/write could be done by either predefined
1451 * method, namely CMD23, or open-ending mode. For open-ending mode,
1452 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1453 *
1454 * However the spec[1] doesn't tell us whether we should also
1455 * ignore that for predefined method. But per the spec[1], section
1456 * 4.15 Set Block Count Command, it says"If illegal block count
1457 * is set, out of range error will be indicated during read/write
1458 * operation (For example, data transfer is stopped at user area
1459 * boundary)." In another word, we could expect a out of range error
1460 * in the response for the following CMD18/25. And if argument of
1461 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1462 * we could also expect to get a -ETIMEDOUT or any error number from
1463 * the host drivers due to missing data response(for write)/data(for
1464 * read), as the cards will stop the data transfer by itself per the
1465 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1466 */
1467
1468 if (!brq->stop.error) {
1469 bool oor_with_open_end;
1470 /* If there is no error yet, check R1 response */
1471
1472 val = brq->stop.resp[0] & CMD_ERRORS;
1473 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1474
1475 if (val && !oor_with_open_end)
1476 brq->stop.error = -EIO;
1477 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001478}
1479
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001480static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1481 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001482{
Per Forlinee8a43a2011-07-01 18:55:33 +02001483 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001484 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001485 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001486 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001487 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001488 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001489 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001490
1491 /*
1492 * sbc.error indicates a problem with the set block count
1493 * command. No data will have been transferred.
1494 *
1495 * cmd.error indicates a problem with the r/w command. No
1496 * data will have been transferred.
1497 *
1498 * stop.error indicates a problem with the stop command. Data
1499 * may have been transferred, or may still be transferring.
1500 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001501
1502 mmc_blk_eval_resp_error(brq);
1503
1504 if (brq->sbc.error || brq->cmd.error ||
1505 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001506 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001507 case ERR_RETRY:
1508 return MMC_BLK_RETRY;
1509 case ERR_ABORT:
1510 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301511 case ERR_NOMEDIUM:
1512 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001513 case ERR_CONTINUE:
1514 break;
1515 }
1516 }
1517
1518 /*
1519 * Check for errors relating to the execution of the
1520 * initial command - such as address errors. No data
1521 * has been transferred.
1522 */
1523 if (brq->cmd.resp[0] & CMD_ERRORS) {
1524 pr_err("%s: r/w command failed, status = %#x\n",
1525 req->rq_disk->disk_name, brq->cmd.resp[0]);
1526 return MMC_BLK_ABORT;
1527 }
1528
1529 /*
1530 * Everything else is either success, or a data error of some
1531 * kind. If it was a write, we may have transitioned to
1532 * program mode, which we have to wait for it to complete.
1533 */
1534 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001535 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001536
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001537 /* Check stop command response */
1538 if (brq->stop.resp[0] & R1_ERROR) {
1539 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1540 req->rq_disk->disk_name, __func__,
1541 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001542 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001543 }
1544
Ulf Hansson95a91292014-01-29 13:11:27 +01001545 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1546 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001547 if (err)
1548 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001549 }
1550
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001551 /* if general error occurs, retry the write operation. */
1552 if (gen_err) {
1553 pr_warn("%s: retrying write for general error\n",
1554 req->rq_disk->disk_name);
1555 return MMC_BLK_RETRY;
1556 }
1557
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001558 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1559 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001560 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001561 pr_debug("%s: retrying because a re-tune was needed\n",
1562 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001563 brq->retune_retry_done = 1;
1564 return MMC_BLK_RETRY;
1565 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001566 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 +02001567 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001568 (unsigned)blk_rq_pos(req),
1569 (unsigned)blk_rq_sectors(req),
1570 brq->cmd.resp[0], brq->stop.resp[0]);
1571
1572 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001573 if (ecc_err)
1574 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001575 return MMC_BLK_DATA_ERR;
1576 } else {
1577 return MMC_BLK_CMD_ERR;
1578 }
1579 }
1580
Adrian Hunter67716322011-08-29 16:42:15 +03001581 if (!brq->data.bytes_xfered)
1582 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001583
Adrian Hunter67716322011-08-29 16:42:15 +03001584 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1585 return MMC_BLK_PARTIAL;
1586
1587 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001588}
1589
Adrian Hunterca5717f2017-03-13 14:36:41 +02001590static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001591 int disable_multi, bool *do_rel_wr_p,
1592 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001594 struct mmc_blk_data *md = mq->blkdata;
1595 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001596 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001597 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001598 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001600 /*
1601 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001602 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001603 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001604 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1605 rq_data_dir(req) == WRITE &&
1606 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001607
Per Forlin54d49d72011-07-01 18:55:29 +02001608 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001609
Per Forlin54d49d72011-07-01 18:55:29 +02001610 brq->mrq.data = &brq->data;
Adrian Hunter93482b32017-09-22 15:36:56 +03001611 brq->mrq.tag = req->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Per Forlin54d49d72011-07-01 18:55:29 +02001613 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1614 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001615
1616 if (rq_data_dir(req) == READ) {
1617 brq->data.flags = MMC_DATA_READ;
1618 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1619 } else {
1620 brq->data.flags = MMC_DATA_WRITE;
1621 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1622 }
1623
1624 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001625 brq->data.blocks = blk_rq_sectors(req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001626 brq->data.blk_addr = blk_rq_pos(req);
1627
1628 /*
1629 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1630 * The eMMC will give "high" priority tasks priority over "simple"
1631 * priority tasks. Here we always set "simple" priority by not setting
1632 * MMC_DATA_PRIO.
1633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Per Forlin54d49d72011-07-01 18:55:29 +02001635 /*
1636 * The block layer doesn't support all sector count
1637 * restrictions, so we need to be prepared for too big
1638 * requests.
1639 */
1640 if (brq->data.blocks > card->host->max_blk_count)
1641 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001643 if (brq->data.blocks > 1) {
1644 /*
1645 * After a read error, we redo the request one sector
1646 * at a time in order to accurately determine which
1647 * sectors can be read successfully.
1648 */
1649 if (disable_multi)
1650 brq->data.blocks = 1;
1651
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001652 /*
1653 * Some controllers have HW issues while operating
1654 * in multiple I/O mode
1655 */
1656 if (card->host->ops->multi_io_quirk)
1657 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1658 (rq_data_dir(req) == READ) ?
1659 MMC_DATA_READ : MMC_DATA_WRITE,
1660 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001661 }
Per Forlin54d49d72011-07-01 18:55:29 +02001662
Adrian Hunter93482b32017-09-22 15:36:56 +03001663 if (do_rel_wr) {
Adrian Hunterca5717f2017-03-13 14:36:41 +02001664 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001665 brq->data.flags |= MMC_DATA_REL_WR;
1666 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001667
1668 /*
1669 * Data tag is used only during writing meta data to speed
1670 * up write and any subsequent read of this meta data
1671 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001672 do_data_tag = card->ext_csd.data_tag_unit_size &&
1673 (req->cmd_flags & REQ_META) &&
1674 (rq_data_dir(req) == WRITE) &&
1675 ((brq->data.blocks * brq->data.blksz) >=
1676 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001677
Adrian Hunter93482b32017-09-22 15:36:56 +03001678 if (do_data_tag)
1679 brq->data.flags |= MMC_DATA_DAT_TAG;
1680
Adrian Hunterca5717f2017-03-13 14:36:41 +02001681 mmc_set_data_timeout(&brq->data, card);
1682
1683 brq->data.sg = mqrq->sg;
1684 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1685
1686 /*
1687 * Adjust the sg list so it is the same size as the
1688 * request.
1689 */
1690 if (brq->data.blocks != blk_rq_sectors(req)) {
1691 int i, data_size = brq->data.blocks << 9;
1692 struct scatterlist *sg;
1693
1694 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1695 data_size -= sg->length;
1696 if (data_size <= 0) {
1697 sg->length += data_size;
1698 i++;
1699 break;
1700 }
1701 }
1702 brq->data.sg_len = i;
1703 }
1704
1705 mqrq->areq.mrq = &brq->mrq;
Adrian Hunterd3377c02017-09-22 15:36:55 +03001706
1707 if (do_rel_wr_p)
1708 *do_rel_wr_p = do_rel_wr;
1709
1710 if (do_data_tag_p)
1711 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001712}
1713
1714static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1715 struct mmc_card *card,
1716 int disable_multi,
1717 struct mmc_queue *mq)
1718{
1719 u32 readcmd, writecmd;
1720 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001721 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001722 struct mmc_blk_data *md = mq->blkdata;
1723 bool do_rel_wr, do_data_tag;
1724
1725 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1726
1727 brq->mrq.cmd = &brq->cmd;
1728
1729 brq->cmd.arg = blk_rq_pos(req);
1730 if (!mmc_card_blockaddr(card))
1731 brq->cmd.arg <<= 9;
1732 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1733
Per Forlin54d49d72011-07-01 18:55:29 +02001734 if (brq->data.blocks > 1 || do_rel_wr) {
1735 /* SPI multiblock writes terminate using a special
1736 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001737 */
Per Forlin54d49d72011-07-01 18:55:29 +02001738 if (!mmc_host_is_spi(card->host) ||
1739 rq_data_dir(req) == READ)
1740 brq->mrq.stop = &brq->stop;
1741 readcmd = MMC_READ_MULTIPLE_BLOCK;
1742 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1743 } else {
1744 brq->mrq.stop = NULL;
1745 readcmd = MMC_READ_SINGLE_BLOCK;
1746 writecmd = MMC_WRITE_BLOCK;
1747 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001748 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301749
1750 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001751 * Pre-defined multi-block transfers are preferable to
1752 * open ended-ones (and necessary for reliable writes).
1753 * However, it is not sufficient to just send CMD23,
1754 * and avoid the final CMD12, as on an error condition
1755 * CMD12 (stop) needs to be sent anyway. This, coupled
1756 * with Auto-CMD23 enhancements provided by some
1757 * hosts, means that the complexity of dealing
1758 * with this is best left to the host. If CMD23 is
1759 * supported by card and host, we'll fill sbc in and let
1760 * the host deal with handling it correctly. This means
1761 * that for hosts that don't expose MMC_CAP_CMD23, no
1762 * change of behavior will be observed.
1763 *
1764 * N.B: Some MMC cards experience perf degradation.
1765 * We'll avoid using CMD23-bounded multiblock writes for
1766 * these, while retaining features like reliable writes.
1767 */
Saugata Das42659002011-12-21 13:09:17 +05301768 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1769 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1770 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001771 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1772 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301773 (do_rel_wr ? (1 << 31) : 0) |
1774 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001775 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1776 brq->mrq.sbc = &brq->sbc;
1777 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001778
Linus Walleij74f5ba32017-02-01 13:47:55 +01001779 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Walleij0e65f102017-02-01 13:47:58 +01001782static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1783 struct mmc_blk_request *brq, struct request *req,
1784 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001785{
Linus Walleij0e65f102017-02-01 13:47:58 +01001786 bool req_pending;
1787
Adrian Hunter67716322011-08-29 16:42:15 +03001788 /*
1789 * If this is an SD card and we're writing, we can first
1790 * mark the known good sectors as ok.
1791 *
1792 * If the card is not SD, we can still ok written sectors
1793 * as reported by the controller (which might be less than
1794 * the real number of written sectors, but never more).
1795 */
1796 if (mmc_card_sd(card)) {
1797 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001798 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001799
Linus Walleij169f03a2017-02-01 13:47:57 +01001800 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001801 if (err)
1802 req_pending = old_req_pending;
1803 else
Adrian Huntera7c17d82017-08-25 15:43:45 +03001804 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001805 } else {
Adrian Huntera7c17d82017-08-25 15:43:45 +03001806 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001807 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001808 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001809}
1810
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001811static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1812 struct request *req,
1813 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001814{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001815 if (mmc_card_removed(card))
1816 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001817 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001818 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001819}
1820
Linus Walleijb2928e12017-02-01 13:47:54 +01001821/**
1822 * mmc_blk_rw_try_restart() - tries to restart the current async request
1823 * @mq: the queue with the card and host to restart
1824 * @req: a new request that want to be started after the current one
1825 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001826static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1827 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001828{
1829 if (!req)
1830 return;
1831
Linus Walleijb2928e12017-02-01 13:47:54 +01001832 /*
1833 * If the card was removed, just cancel everything and return.
1834 */
1835 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001836 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001837 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001838 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001839 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001840 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001841 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001842 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1843 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001844}
1845
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001846static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001847{
Linus Walleij7db30282016-11-18 13:36:15 +01001848 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001849 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001850 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001851 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001852 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001853 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001854 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001855 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001856 struct mmc_async_req *new_areq;
1857 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001858 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001859
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001860 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001861 mqrq_cur = req_to_mmc_queue_req(new_req);
1862 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001863 }
1864
1865 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001866 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001867
1868 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001869 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301870 /*
1871 * When 4KB native sector is enabled, only 8 blocks
1872 * multiple read or write is allowed
1873 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001874 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001875 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301876 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001877 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001878 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001879 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301880 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001881
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001882 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1883 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001884 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001885 new_areq = NULL;
1886
Linus Walleijc3399ef2017-02-01 13:47:53 +01001887 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001888 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001889 /*
1890 * We have just put the first request into the pipeline
1891 * and there is nothing more to do until it is
1892 * complete.
1893 */
Linus Walleijdf061582017-01-24 11:17:57 +01001894 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001895 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001896
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001897 /*
1898 * An asynchronous request has been completed and we proceed
1899 * to handle the result of it.
1900 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001901 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001902 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001903 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001904 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001905
Per Forlind78d4a8a2011-07-01 18:55:30 +02001906 switch (status) {
1907 case MMC_BLK_SUCCESS:
1908 case MMC_BLK_PARTIAL:
1909 /*
1910 * A block was successfully transferred.
1911 */
Adrian Hunter67716322011-08-29 16:42:15 +03001912 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001913
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001914 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001915 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001916 /*
1917 * If the blk_end_request function returns non-zero even
1918 * though all data has been transferred and no errors
1919 * were returned by the host controller, it's a bug.
1920 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001921 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301922 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001923 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001924 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001925 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001926 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001927 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001928 break;
1929 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001930 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001931 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001932 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001933 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1934 else
Linus Walleij304419d2017-05-18 11:29:32 +02001935 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001936 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001937 return;
1938 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001939 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001940 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001941 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001942 return;
1943 }
Ding Wang29535f72015-05-18 20:14:15 +08001944 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001945 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001946 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001947 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001948 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001949 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001950 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001951 if (!mmc_blk_reset(md, card->host, type))
1952 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001953 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001954 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001955 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001956 case MMC_BLK_DATA_ERR: {
1957 int err;
1958
1959 err = mmc_blk_reset(md, card->host, type);
1960 if (!err)
1961 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001962 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001963 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001964 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001965 return;
1966 }
Adrian Hunter67716322011-08-29 16:42:15 +03001967 /* Fall through */
1968 }
1969 case MMC_BLK_ECC_ERR:
1970 if (brq->data.blocks > 1) {
1971 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001972 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001973 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001974 disable_multi = 1;
1975 break;
1976 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001977 /*
1978 * After an error, we redo I/O one sector at a
1979 * time, so we only reach here after trying to
1980 * read a single sector.
1981 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001982 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001983 brq->data.blksz);
1984 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001985 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001986 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001987 return;
1988 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001989 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301990 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001991 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001992 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001993 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001994 default:
1995 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001996 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001997 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001998 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001999 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002000 }
2001
Linus Walleij0e65f102017-02-01 13:47:58 +01002002 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01002003 /*
2004 * In case of a incomplete request
2005 * prepare it again and resend.
2006 */
2007 mmc_blk_rw_rq_prep(mq_rq, card,
2008 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01002009 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01002010 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002011 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002012 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002013 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002014
Linus Walleij304419d2017-05-18 11:29:32 +02002015 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Linus Walleijdf061582017-01-24 11:17:57 +01002018void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002019{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002020 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01002021 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002022 struct mmc_card *card = md->queue.card;
2023
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002024 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02002025 /* claim host only for the first request */
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002026 mmc_get_card(card, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02002027
Linus Walleij1f797ed2017-08-20 23:39:10 +02002028 ret = mmc_blk_part_switch(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002029 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002030 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002031 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002032 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002033 goto out;
2034 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002035
Linus Walleij614f0382017-05-18 11:29:34 +02002036 if (req) {
2037 switch (req_op(req)) {
2038 case REQ_OP_DRV_IN:
2039 case REQ_OP_DRV_OUT:
2040 /*
2041 * Complete ongoing async transfer before issuing
2042 * ioctl()s
2043 */
2044 if (mq->qcnt)
2045 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02002046 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02002047 break;
2048 case REQ_OP_DISCARD:
2049 /*
2050 * Complete ongoing async transfer before issuing
2051 * discard.
2052 */
2053 if (mq->qcnt)
2054 mmc_blk_issue_rw_rq(mq, NULL);
2055 mmc_blk_issue_discard_rq(mq, req);
2056 break;
2057 case REQ_OP_SECURE_ERASE:
2058 /*
2059 * Complete ongoing async transfer before issuing
2060 * secure erase.
2061 */
2062 if (mq->qcnt)
2063 mmc_blk_issue_rw_rq(mq, NULL);
2064 mmc_blk_issue_secdiscard_rq(mq, req);
2065 break;
2066 case REQ_OP_FLUSH:
2067 /*
2068 * Complete ongoing async transfer before issuing
2069 * flush.
2070 */
2071 if (mq->qcnt)
2072 mmc_blk_issue_rw_rq(mq, NULL);
2073 mmc_blk_issue_flush(mq, req);
2074 break;
2075 default:
2076 /* Normal request, just issue it */
2077 mmc_blk_issue_rw_rq(mq, req);
2078 card->host->context_info.is_waiting_last_req = false;
2079 break;
Wu Fengguang73222382017-05-23 05:11:33 +08002080 }
Adrian Hunter49804542010-08-11 14:17:50 -07002081 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002082 /* No request, flushing the pipeline with NULL */
2083 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002084 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002085 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002086
Andrei Warkentin371a6892011-04-11 18:10:25 -05002087out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002088 if (!mq->qcnt)
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002089 mmc_put_card(card, NULL);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Russell Kinga6f6c962006-01-03 22:38:44 +00002092static inline int mmc_blk_readonly(struct mmc_card *card)
2093{
2094 return mmc_card_readonly(card) ||
2095 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2096}
2097
Andrei Warkentin371a6892011-04-11 18:10:25 -05002098static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2099 struct device *parent,
2100 sector_t size,
2101 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002102 const char *subname,
2103 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 struct mmc_blk_data *md;
2106 int devidx, ret;
2107
Heiner Kallweita04848c2017-02-01 19:44:22 +01002108 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002109 if (devidx < 0) {
2110 /*
2111 * We get -ENOSPC because there are no more any available
2112 * devidx. The reason may be that, either userspace haven't yet
2113 * unmounted the partitions, which postpones mmc_blk_release()
2114 * from being called, or the device has more partitions than
2115 * what we support.
2116 */
2117 if (devidx == -ENOSPC)
2118 dev_err(mmc_dev(card->host),
2119 "no more device IDs available\n");
2120
Heiner Kallweita04848c2017-02-01 19:44:22 +01002121 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002124 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002125 if (!md) {
2126 ret = -ENOMEM;
2127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002129
Johan Rudholmadd710e2011-12-02 08:51:06 +01002130 md->area_type = area_type;
2131
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002132 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002133 * Set the read-only status based on the supported commands
2134 * and the write protect switch.
2135 */
2136 md->read_only = mmc_blk_readonly(card);
2137
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002138 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002139 if (md->disk == NULL) {
2140 ret = -ENOMEM;
2141 goto err_kfree;
2142 }
2143
2144 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002145 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002146 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002147 md->usage = 1;
2148
Adrian Hunterd09408a2011-06-23 13:40:28 +03002149 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002150 if (ret)
2151 goto err_putdisk;
2152
Linus Walleij7db30282016-11-18 13:36:15 +01002153 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002154
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002155 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002156 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002157 md->disk->fops = &mmc_bdops;
2158 md->disk->private_data = md;
2159 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002160 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002161 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002162 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002163 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002164 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002165
2166 /*
2167 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2168 *
2169 * - be set for removable media with permanent block devices
2170 * - be unset for removable block devices with permanent media
2171 *
2172 * Since MMC block devices clearly fall under the second
2173 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2174 * should use the block device creation/destruction hotplug
2175 * messages to tell when the card is present.
2176 */
2177
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002178 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002179 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002180
Saugata Dasa5075eb2012-05-17 16:32:21 +05302181 if (mmc_card_mmc(card))
2182 blk_queue_logical_block_size(md->queue.queue,
2183 card->ext_csd.data_sector_size);
2184 else
2185 blk_queue_logical_block_size(md->queue.queue, 512);
2186
Andrei Warkentin371a6892011-04-11 18:10:25 -05002187 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002188
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002189 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002190 if ((mmc_card_mmc(card) &&
2191 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002192 (mmc_card_sd(card) &&
2193 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2194 md->flags |= MMC_BLK_CMD23;
2195 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002196
2197 if (mmc_card_mmc(card) &&
2198 md->flags & MMC_BLK_CMD23 &&
2199 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2200 card->ext_csd.rel_sectors)) {
2201 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002202 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002203 }
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002206
2207 err_putdisk:
2208 put_disk(md->disk);
2209 err_kfree:
2210 kfree(md);
2211 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002212 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002213 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Andrei Warkentin371a6892011-04-11 18:10:25 -05002216static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2217{
2218 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002219
2220 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2221 /*
2222 * The EXT_CSD sector count is in number or 512 byte
2223 * sectors.
2224 */
2225 size = card->ext_csd.sectors;
2226 } else {
2227 /*
2228 * The CSD capacity field is in units of read_blkbits.
2229 * set_capacity takes units of 512 bytes.
2230 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002231 size = (typeof(sector_t))card->csd.capacity
2232 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002233 }
2234
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002235 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002236 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002237}
2238
2239static int mmc_blk_alloc_part(struct mmc_card *card,
2240 struct mmc_blk_data *md,
2241 unsigned int part_type,
2242 sector_t size,
2243 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002244 const char *subname,
2245 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002246{
2247 char cap_str[10];
2248 struct mmc_blk_data *part_md;
2249
2250 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002251 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002252 if (IS_ERR(part_md))
2253 return PTR_ERR(part_md);
2254 part_md->part_type = part_type;
2255 list_add(&part_md->part, &md->part);
2256
James Bottomleyb9f28d82015-03-05 18:47:01 -08002257 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002258 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302259 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002260 part_md->disk->disk_name, mmc_card_id(card),
2261 mmc_card_name(card), part_md->part_type, cap_str);
2262 return 0;
2263}
2264
Linus Walleij97548572017-09-20 10:02:00 +02002265/**
2266 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2267 * @filp: the character device file
2268 * @cmd: the ioctl() command
2269 * @arg: the argument from userspace
2270 *
2271 * This will essentially just redirect the ioctl()s coming in over to
2272 * the main block device spawning the RPMB character device.
2273 */
2274static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2275 unsigned long arg)
2276{
2277 struct mmc_rpmb_data *rpmb = filp->private_data;
2278 int ret;
2279
2280 switch (cmd) {
2281 case MMC_IOC_CMD:
2282 ret = mmc_blk_ioctl_cmd(rpmb->md,
2283 (struct mmc_ioc_cmd __user *)arg,
2284 rpmb);
2285 break;
2286 case MMC_IOC_MULTI_CMD:
2287 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2288 (struct mmc_ioc_multi_cmd __user *)arg,
2289 rpmb);
2290 break;
2291 default:
2292 ret = -EINVAL;
2293 break;
2294 }
2295
2296 return 0;
2297}
2298
2299#ifdef CONFIG_COMPAT
2300static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2301 unsigned long arg)
2302{
2303 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2304}
2305#endif
2306
2307static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2308{
2309 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2310 struct mmc_rpmb_data, chrdev);
2311
2312 get_device(&rpmb->dev);
2313 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02002314 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02002315
2316 return nonseekable_open(inode, filp);
2317}
2318
2319static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2320{
2321 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2322 struct mmc_rpmb_data, chrdev);
2323
2324 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002325 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02002326
2327 return 0;
2328}
2329
2330static const struct file_operations mmc_rpmb_fileops = {
2331 .release = mmc_rpmb_chrdev_release,
2332 .open = mmc_rpmb_chrdev_open,
2333 .owner = THIS_MODULE,
2334 .llseek = no_llseek,
2335 .unlocked_ioctl = mmc_rpmb_ioctl,
2336#ifdef CONFIG_COMPAT
2337 .compat_ioctl = mmc_rpmb_ioctl_compat,
2338#endif
2339};
2340
Linus Walleij1c87f732017-10-04 11:10:07 +02002341static void mmc_blk_rpmb_device_release(struct device *dev)
2342{
2343 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2344
2345 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2346 kfree(rpmb);
2347}
Linus Walleij97548572017-09-20 10:02:00 +02002348
2349static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2350 struct mmc_blk_data *md,
2351 unsigned int part_index,
2352 sector_t size,
2353 const char *subname)
2354{
2355 int devidx, ret;
2356 char rpmb_name[DISK_NAME_LEN];
2357 char cap_str[10];
2358 struct mmc_rpmb_data *rpmb;
2359
2360 /* This creates the minor number for the RPMB char device */
2361 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2362 if (devidx < 0)
2363 return devidx;
2364
2365 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02002366 if (!rpmb) {
2367 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02002368 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02002369 }
Linus Walleij97548572017-09-20 10:02:00 +02002370
2371 snprintf(rpmb_name, sizeof(rpmb_name),
2372 "mmcblk%u%s", card->host->index, subname ? subname : "");
2373
2374 rpmb->id = devidx;
2375 rpmb->part_index = part_index;
2376 rpmb->dev.init_name = rpmb_name;
2377 rpmb->dev.bus = &mmc_rpmb_bus_type;
2378 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2379 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02002380 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02002381 device_initialize(&rpmb->dev);
2382 dev_set_drvdata(&rpmb->dev, rpmb);
2383 rpmb->md = md;
2384
2385 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2386 rpmb->chrdev.owner = THIS_MODULE;
2387 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2388 if (ret) {
2389 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02002390 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02002391 }
2392
2393 list_add(&rpmb->node, &md->rpmbs);
2394
2395 string_get_size((u64)size, 512, STRING_UNITS_2,
2396 cap_str, sizeof(cap_str));
2397
2398 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2399 rpmb_name, mmc_card_id(card),
2400 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2401 MAJOR(mmc_rpmb_devt), rpmb->id);
2402
2403 return 0;
2404
Linus Walleij1c87f732017-10-04 11:10:07 +02002405out_put_device:
2406 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002407 return ret;
2408}
2409
2410static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02002411
Linus Walleij97548572017-09-20 10:02:00 +02002412{
2413 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002414 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002415}
2416
Namjae Jeone0c368d2011-10-06 23:41:38 +09002417/* MMC Physical partitions consist of two boot partitions and
2418 * up to four general purpose partitions.
2419 * For each partition enabled in EXT_CSD a block device will be allocatedi
2420 * to provide access to the partition.
2421 */
2422
Andrei Warkentin371a6892011-04-11 18:10:25 -05002423static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2424{
Linus Walleij97548572017-09-20 10:02:00 +02002425 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002426
2427 if (!mmc_card_mmc(card))
2428 return 0;
2429
Namjae Jeone0c368d2011-10-06 23:41:38 +09002430 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02002431 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2432 /*
2433 * RPMB partitions does not provide block access, they
2434 * are only accessed using ioctl():s. Thus create
2435 * special RPMB block devices that do not have a
2436 * backing block queue for these.
2437 */
2438 ret = mmc_blk_alloc_rpmb_part(card, md,
2439 card->part[idx].part_cfg,
2440 card->part[idx].size >> 9,
2441 card->part[idx].name);
2442 if (ret)
2443 return ret;
2444 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09002445 ret = mmc_blk_alloc_part(card, md,
2446 card->part[idx].part_cfg,
2447 card->part[idx].size >> 9,
2448 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002449 card->part[idx].name,
2450 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002451 if (ret)
2452 return ret;
2453 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002454 }
2455
Linus Walleij97548572017-09-20 10:02:00 +02002456 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002457}
2458
Andrei Warkentin371a6892011-04-11 18:10:25 -05002459static void mmc_blk_remove_req(struct mmc_blk_data *md)
2460{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002461 struct mmc_card *card;
2462
Andrei Warkentin371a6892011-04-11 18:10:25 -05002463 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002464 /*
2465 * Flush remaining requests and free queues. It
2466 * is freeing the queue that stops new requests
2467 * from being accepted.
2468 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002469 card = md->queue.card;
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002470 spin_lock_irq(md->queue.queue->queue_lock);
Shawn Lin7c84b8b2017-07-25 09:11:28 +08002471 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002472 spin_unlock_irq(md->queue.queue->queue_lock);
Grzegorz Slujabbdc74d2017-07-13 11:17:58 +02002473 blk_set_queue_dying(md->queue.queue);
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002474 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002475 if (md->disk->flags & GENHD_FL_UP) {
2476 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002477 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2478 card->ext_csd.boot_ro_lockable)
2479 device_remove_file(disk_to_dev(md->disk),
2480 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002481
Andrei Warkentin371a6892011-04-11 18:10:25 -05002482 del_gendisk(md->disk);
2483 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002484 mmc_blk_put(md);
2485 }
2486}
2487
2488static void mmc_blk_remove_parts(struct mmc_card *card,
2489 struct mmc_blk_data *md)
2490{
2491 struct list_head *pos, *q;
2492 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02002493 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002494
Linus Walleij97548572017-09-20 10:02:00 +02002495 /* Remove RPMB partitions */
2496 list_for_each_safe(pos, q, &md->rpmbs) {
2497 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2498 list_del(pos);
2499 mmc_blk_remove_rpmb_part(rpmb);
2500 }
2501 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05002502 list_for_each_safe(pos, q, &md->part) {
2503 part_md = list_entry(pos, struct mmc_blk_data, part);
2504 list_del(pos);
2505 mmc_blk_remove_req(part_md);
2506 }
2507}
2508
2509static int mmc_add_disk(struct mmc_blk_data *md)
2510{
2511 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002512 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002513
Dan Williams307d8e62016-06-20 10:40:44 -07002514 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002515 md->force_ro.show = force_ro_show;
2516 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302517 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002518 md->force_ro.attr.name = "force_ro";
2519 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2520 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2521 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002522 goto force_ro_fail;
2523
2524 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2525 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002526 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002527
2528 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2529 mode = S_IRUGO;
2530 else
2531 mode = S_IRUGO | S_IWUSR;
2532
2533 md->power_ro_lock.show = power_ro_lock_show;
2534 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002535 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002536 md->power_ro_lock.attr.mode = mode;
2537 md->power_ro_lock.attr.name =
2538 "ro_lock_until_next_power_on";
2539 ret = device_create_file(disk_to_dev(md->disk),
2540 &md->power_ro_lock);
2541 if (ret)
2542 goto power_ro_lock_fail;
2543 }
2544 return ret;
2545
2546power_ro_lock_fail:
2547 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2548force_ro_fail:
2549 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002550
2551 return ret;
2552}
2553
Linus Walleij627c3cc2017-08-20 23:39:08 +02002554#ifdef CONFIG_DEBUG_FS
2555
2556static int mmc_dbg_card_status_get(void *data, u64 *val)
2557{
2558 struct mmc_card *card = data;
2559 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2560 struct mmc_queue *mq = &md->queue;
2561 struct request *req;
2562 int ret;
2563
2564 /* Ask the block layer about the card status */
2565 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002566 if (IS_ERR(req))
2567 return PTR_ERR(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002568 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2569 blk_execute_rq(mq->queue, NULL, req, 0);
2570 ret = req_to_mmc_queue_req(req)->drv_op_result;
2571 if (ret >= 0) {
2572 *val = ret;
2573 ret = 0;
2574 }
Adrian Hunter34c089e2017-11-21 15:42:27 +02002575 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002576
2577 return ret;
2578}
2579DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2580 NULL, "%08llx\n");
2581
2582/* That is two digits * 512 + 1 for newline */
2583#define EXT_CSD_STR_LEN 1025
2584
2585static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2586{
2587 struct mmc_card *card = inode->i_private;
2588 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2589 struct mmc_queue *mq = &md->queue;
2590 struct request *req;
2591 char *buf;
2592 ssize_t n = 0;
2593 u8 *ext_csd;
2594 int err, i;
2595
2596 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2597 if (!buf)
2598 return -ENOMEM;
2599
2600 /* Ask the block layer for the EXT CSD */
2601 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002602 if (IS_ERR(req)) {
2603 err = PTR_ERR(req);
2604 goto out_free;
2605 }
Linus Walleij627c3cc2017-08-20 23:39:08 +02002606 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2607 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2608 blk_execute_rq(mq->queue, NULL, req, 0);
2609 err = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +02002610 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002611 if (err) {
2612 pr_err("FAILED %d\n", err);
2613 goto out_free;
2614 }
2615
2616 for (i = 0; i < 512; i++)
2617 n += sprintf(buf + n, "%02x", ext_csd[i]);
2618 n += sprintf(buf + n, "\n");
2619
2620 if (n != EXT_CSD_STR_LEN) {
2621 err = -EINVAL;
2622 goto out_free;
2623 }
2624
2625 filp->private_data = buf;
2626 kfree(ext_csd);
2627 return 0;
2628
2629out_free:
2630 kfree(buf);
2631 return err;
2632}
2633
2634static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2635 size_t cnt, loff_t *ppos)
2636{
2637 char *buf = filp->private_data;
2638
2639 return simple_read_from_buffer(ubuf, cnt, ppos,
2640 buf, EXT_CSD_STR_LEN);
2641}
2642
2643static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2644{
2645 kfree(file->private_data);
2646 return 0;
2647}
2648
2649static const struct file_operations mmc_dbg_ext_csd_fops = {
2650 .open = mmc_ext_csd_open,
2651 .read = mmc_ext_csd_read,
2652 .release = mmc_ext_csd_release,
2653 .llseek = default_llseek,
2654};
2655
2656static int mmc_blk_add_debugfs(struct mmc_card *card)
2657{
2658 struct dentry *root;
2659
2660 if (!card->debugfs_root)
2661 return 0;
2662
2663 root = card->debugfs_root;
2664
2665 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2666 if (!debugfs_create_file("status", S_IRUSR, root, card,
2667 &mmc_dbg_card_status_fops))
2668 return -EIO;
2669 }
2670
2671 if (mmc_card_mmc(card)) {
2672 if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
2673 &mmc_dbg_ext_csd_fops))
2674 return -EIO;
2675 }
2676
2677 return 0;
2678}
2679
2680
2681#else
2682
2683static int mmc_blk_add_debugfs(struct mmc_card *card)
2684{
2685 return 0;
2686}
2687
2688#endif /* CONFIG_DEBUG_FS */
2689
Ulf Hansson96541ba2015-04-14 13:06:12 +02002690static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002692 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002693 char cap_str[10];
2694
Pierre Ossman912490d2005-05-21 10:27:02 +01002695 /*
2696 * Check that the card supports the command class(es) we need.
2697 */
2698 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return -ENODEV;
2700
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002701 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002704 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return PTR_ERR(md);
2706
James Bottomleyb9f28d82015-03-05 18:47:01 -08002707 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002708 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302709 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002711 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Andrei Warkentin371a6892011-04-11 18:10:25 -05002713 if (mmc_blk_alloc_parts(card, md))
2714 goto out;
2715
Ulf Hansson96541ba2015-04-14 13:06:12 +02002716 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002717
Andrei Warkentin371a6892011-04-11 18:10:25 -05002718 if (mmc_add_disk(md))
2719 goto out;
2720
2721 list_for_each_entry(part_md, &md->part, part) {
2722 if (mmc_add_disk(part_md))
2723 goto out;
2724 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002725
Linus Walleij627c3cc2017-08-20 23:39:08 +02002726 /* Add two debugfs entries */
2727 mmc_blk_add_debugfs(card);
2728
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002729 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2730 pm_runtime_use_autosuspend(&card->dev);
2731
2732 /*
2733 * Don't enable runtime PM for SD-combo cards here. Leave that
2734 * decision to be taken during the SDIO init sequence instead.
2735 */
2736 if (card->type != MMC_TYPE_SD_COMBO) {
2737 pm_runtime_set_active(&card->dev);
2738 pm_runtime_enable(&card->dev);
2739 }
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return 0;
2742
2743 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002744 mmc_blk_remove_parts(card, md);
2745 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Ulf Hansson96541ba2015-04-14 13:06:12 +02002749static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002751 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Andrei Warkentin371a6892011-04-11 18:10:25 -05002753 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002754 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002755 mmc_claim_host(card->host);
Linus Walleij1f797ed2017-08-20 23:39:10 +02002756 mmc_blk_part_switch(card, md->part_type);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002757 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002758 if (card->type != MMC_TYPE_SD_COMBO)
2759 pm_runtime_disable(&card->dev);
2760 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002761 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002762 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Ulf Hansson96541ba2015-04-14 13:06:12 +02002765static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002767 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002768 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 if (md) {
2771 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002772 list_for_each_entry(part_md, &md->part, part) {
2773 mmc_queue_suspend(&part_md->queue);
2774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
2776 return 0;
2777}
2778
Ulf Hansson96541ba2015-04-14 13:06:12 +02002779static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002780{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002781 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002782}
2783
Ulf Hansson0967edc2014-10-06 11:29:42 +02002784#ifdef CONFIG_PM_SLEEP
2785static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002786{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002787 struct mmc_card *card = mmc_dev_to_card(dev);
2788
2789 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002790}
2791
Ulf Hansson0967edc2014-10-06 11:29:42 +02002792static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002794 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002795 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002798 /*
2799 * Resume involves the card going into idle state,
2800 * so current partition is always the main one.
2801 */
2802 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002804 list_for_each_entry(part_md, &md->part, part) {
2805 mmc_queue_resume(&part_md->queue);
2806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808 return 0;
2809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810#endif
2811
Ulf Hansson0967edc2014-10-06 11:29:42 +02002812static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2813
Ulf Hansson96541ba2015-04-14 13:06:12 +02002814static struct mmc_driver mmc_driver = {
2815 .drv = {
2816 .name = "mmcblk",
2817 .pm = &mmc_blk_pm_ops,
2818 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 .probe = mmc_blk_probe,
2820 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002821 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822};
2823
2824static int __init mmc_blk_init(void)
2825{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002826 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Linus Walleij97548572017-09-20 10:02:00 +02002828 res = bus_register(&mmc_rpmb_bus_type);
2829 if (res < 0) {
2830 pr_err("mmcblk: could not register RPMB bus type\n");
2831 return res;
2832 }
2833 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
2834 if (res < 0) {
2835 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
2836 goto out_bus_unreg;
2837 }
2838
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002839 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2840 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2841
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002842 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002843
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002844 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2845 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002846 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002848 res = mmc_register_driver(&mmc_driver);
2849 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002850 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002852 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02002853
2854out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002855 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002856out_chrdev_unreg:
2857 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2858out_bus_unreg:
2859 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return res;
2861}
2862
2863static void __exit mmc_blk_exit(void)
2864{
2865 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002866 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002867 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
2870module_init(mmc_blk_init);
2871module_exit(mmc_blk_exit);
2872
2873MODULE_LICENSE("GPL");
2874MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2875