blob: f7c387c27ac0578c51b756d161d063c517916fe4 [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)
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200115#define MMC_BLK_CQE_RECOVERY BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116
117 /*
118 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200119 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500120 * track of the current selected device partition.
121 */
122 unsigned int part_curr;
123 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 struct device_attribute power_ro_lock;
125 int area_type;
Adrian Hunterf9f0da92017-11-21 15:42:30 +0200126
127 /* debugfs files (only in main mmc_blk_data) */
128 struct dentry *status_dentry;
129 struct dentry *ext_csd_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Linus Walleij97548572017-09-20 10:02:00 +0200132/* Device type for RPMB character devices */
133static dev_t mmc_rpmb_devt;
134
135/* Bus type for RPMB character devices */
136static struct bus_type mmc_rpmb_bus_type = {
137 .name = "mmc_rpmb",
138};
139
140/**
141 * struct mmc_rpmb_data - special RPMB device type for these areas
142 * @dev: the device for the RPMB area
143 * @chrdev: character device for the RPMB area
144 * @id: unique device ID number
145 * @part_index: partition index (0 on first)
146 * @md: parent MMC block device
147 * @node: list item, so we can put this device on a list
148 */
149struct mmc_rpmb_data {
150 struct device dev;
151 struct cdev chrdev;
152 int id;
153 unsigned int part_index;
154 struct mmc_blk_data *md;
155 struct list_head node;
156};
157
Arjan van de Vena621aae2006-01-12 18:43:35 +0000158static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200164 unsigned int part_type);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
167{
168 struct mmc_blk_data *md;
169
Arjan van de Vena621aae2006-01-12 18:43:35 +0000170 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 md = disk->private_data;
172 if (md && md->usage == 0)
173 md = NULL;
174 if (md)
175 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000176 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 return md;
179}
180
Andrei Warkentin371a6892011-04-11 18:10:25 -0500181static inline int mmc_get_devidx(struct gendisk *disk)
182{
Colin Cross382c55f2015-10-22 10:00:41 -0700183 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500184 return devidx;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void mmc_blk_put(struct mmc_blk_data *md)
188{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000189 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 md->usage--;
191 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500192 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter41e3efd2017-11-29 15:40:59 +0200193 blk_put_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100194 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kfree(md);
197 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000198 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Johan Rudholmadd710e2011-12-02 08:51:06 +0100201static ssize_t power_ro_lock_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
204 int ret;
205 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
206 struct mmc_card *card = md->queue.card;
207 int locked = 0;
208
209 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
210 locked = 2;
211 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
212 locked = 1;
213
214 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
215
Tomas Winkler9098f842015-07-16 15:50:45 +0200216 mmc_blk_put(md);
217
Johan Rudholmadd710e2011-12-02 08:51:06 +0100218 return ret;
219}
220
221static ssize_t power_ro_lock_store(struct device *dev,
222 struct device_attribute *attr, const char *buf, size_t count)
223{
224 int ret;
225 struct mmc_blk_data *md, *part_md;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200226 struct mmc_queue *mq;
227 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100228 unsigned long set;
229
230 if (kstrtoul(buf, 0, &set))
231 return -EINVAL;
232
233 if (set != 1)
234 return count;
235
236 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200237 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100238
Linus Walleij0493f6f2017-05-19 15:37:30 +0200239 /* Dispatch locking to the block layer */
240 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200241 if (IS_ERR(req)) {
242 count = PTR_ERR(req);
243 goto out_put;
244 }
Linus Walleij0493f6f2017-05-19 15:37:30 +0200245 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
246 blk_execute_rq(mq->queue, NULL, req, 0);
247 ret = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +0200248 blk_put_request(req);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100249
250 if (!ret) {
251 pr_info("%s: Locking boot partition ro until next power on\n",
252 md->disk->disk_name);
253 set_disk_ro(md->disk, 1);
254
255 list_for_each_entry(part_md, &md->part, part)
256 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
257 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
258 set_disk_ro(part_md->disk, 1);
259 }
260 }
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200261out_put:
Johan Rudholmadd710e2011-12-02 08:51:06 +0100262 mmc_blk_put(md);
263 return count;
264}
265
Andrei Warkentin371a6892011-04-11 18:10:25 -0500266static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
267 char *buf)
268{
269 int ret;
270 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
271
Baruch Siach0031a982014-09-22 10:12:51 +0300272 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500273 get_disk_ro(dev_to_disk(dev)) ^
274 md->read_only);
275 mmc_blk_put(md);
276 return ret;
277}
278
279static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
281{
282 int ret;
283 char *end;
284 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
285 unsigned long set = simple_strtoul(buf, &end, 0);
286 if (end == buf) {
287 ret = -EINVAL;
288 goto out;
289 }
290
291 set_disk_ro(dev_to_disk(dev), set || md->read_only);
292 ret = count;
293out:
294 mmc_blk_put(md);
295 return ret;
296}
297
Al Viroa5a15612008-03-02 10:33:30 -0500298static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Al Viroa5a15612008-03-02 10:33:30 -0500300 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int ret = -ENXIO;
302
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200303 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (md) {
305 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500306 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700308
Al Viroa5a15612008-03-02 10:33:30 -0500309 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700310 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700311 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return ret;
317}
318
Al Virodb2a1442013-05-05 21:52:57 -0400319static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Al Viroa5a15612008-03-02 10:33:30 -0500321 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200323 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200325 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800329mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800331 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
332 geo->heads = 4;
333 geo->sectors = 16;
334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
John Calixtocb87ea22011-04-26 18:56:29 -0400337struct mmc_blk_ioc_data {
338 struct mmc_ioc_cmd ic;
339 unsigned char *buf;
340 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200341 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400342};
343
344static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
345 struct mmc_ioc_cmd __user *user)
346{
347 struct mmc_blk_ioc_data *idata;
348 int err;
349
yalin wang1ff89502015-11-12 19:27:11 +0800350 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400351 if (!idata) {
352 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400353 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400354 }
355
356 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
357 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400358 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400359 }
360
361 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
362 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
363 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400364 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400365 }
366
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300367 if (!idata->buf_bytes) {
368 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100369 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300370 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100371
yalin wang1ff89502015-11-12 19:27:11 +0800372 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400373 if (!idata->buf) {
374 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400376 }
377
378 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
379 idata->ic.data_ptr, idata->buf_bytes)) {
380 err = -EFAULT;
381 goto copy_err;
382 }
383
384 return idata;
385
386copy_err:
387 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400388idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400389 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400390out:
John Calixtocb87ea22011-04-26 18:56:29 -0400391 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400392}
393
Jon Huntera5f57742015-09-22 10:27:53 +0100394static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
395 struct mmc_blk_ioc_data *idata)
396{
397 struct mmc_ioc_cmd *ic = &idata->ic;
398
399 if (copy_to_user(&(ic_ptr->response), ic->response,
400 sizeof(ic->response)))
401 return -EFAULT;
402
403 if (!idata->ic.write_flag) {
404 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
405 idata->buf, idata->buf_bytes))
406 return -EFAULT;
407 }
408
409 return 0;
410}
411
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200412static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
413 u32 retries_max)
414{
415 int err;
416 u32 retry_count = 0;
417
418 if (!status || !retries_max)
419 return -EINVAL;
420
421 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200422 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200423 if (err)
424 break;
425
426 if (!R1_STATUS(*status) &&
427 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
428 break; /* RPMB programming operation complete */
429
430 /*
431 * Rechedule to give the MMC device a chance to continue
432 * processing the previous command without being polled too
433 * frequently.
434 */
435 usleep_range(1000, 5000);
436 } while (++retry_count < retries_max);
437
438 if (retry_count == retries_max)
439 err = -EPERM;
440
441 return err;
442}
443
Maya Erez775a9362013-04-18 15:41:55 +0300444static int ioctl_do_sanitize(struct mmc_card *card)
445{
446 int err;
447
Ulf Hanssona2d10862013-12-16 14:37:26 +0100448 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300449 pr_warn("%s: %s - SANITIZE is not supported\n",
450 mmc_hostname(card->host), __func__);
451 err = -EOPNOTSUPP;
452 goto out;
453 }
454
455 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
456 mmc_hostname(card->host), __func__);
457
458 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
459 EXT_CSD_SANITIZE_START, 1,
460 MMC_SANITIZE_REQ_TIMEOUT);
461
462 if (err)
463 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
464 mmc_hostname(card->host), __func__, err);
465
466 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
467 __func__);
468out:
469 return err;
470}
471
Jon Huntera5f57742015-09-22 10:27:53 +0100472static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
473 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400474{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900475 struct mmc_command cmd = {};
476 struct mmc_data data = {};
477 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400478 struct scatterlist sg;
479 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200480 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200481 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400482
Jon Huntera5f57742015-09-22 10:27:53 +0100483 if (!card || !md || !idata)
484 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400485
Linus Walleij97548572017-09-20 10:02:00 +0200486 /*
487 * The RPMB accesses comes in from the character device, so we
488 * need to target these explicitly. Else we just target the
489 * partition type for the block device the ioctl() was issued
490 * on.
491 */
492 if (idata->rpmb) {
493 /* Support multiple RPMB partitions */
494 target_part = idata->rpmb->part_index;
495 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
496 } else {
497 target_part = md->part_type;
498 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200499
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100500 cmd.opcode = idata->ic.opcode;
501 cmd.arg = idata->ic.arg;
502 cmd.flags = idata->ic.flags;
503
504 if (idata->buf_bytes) {
505 data.sg = &sg;
506 data.sg_len = 1;
507 data.blksz = idata->ic.blksz;
508 data.blocks = idata->ic.blocks;
509
510 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
511
512 if (idata->ic.write_flag)
513 data.flags = MMC_DATA_WRITE;
514 else
515 data.flags = MMC_DATA_READ;
516
517 /* data.flags must already be set before doing this. */
518 mmc_set_data_timeout(&data, card);
519
520 /* Allow overriding the timeout_ns for empirical tuning. */
521 if (idata->ic.data_timeout_ns)
522 data.timeout_ns = idata->ic.data_timeout_ns;
523
524 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
525 /*
526 * Pretend this is a data transfer and rely on the
527 * host driver to compute timeout. When all host
528 * drivers support cmd.cmd_timeout for R1B, this
529 * can be changed to:
530 *
531 * mrq.data = NULL;
532 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
533 */
534 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
535 }
536
537 mrq.data = &data;
538 }
539
540 mrq.cmd = &cmd;
541
Linus Walleij97548572017-09-20 10:02:00 +0200542 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200543 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100544 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200545
John Calixtocb87ea22011-04-26 18:56:29 -0400546 if (idata->ic.is_acmd) {
547 err = mmc_app_cmd(card->host, card);
548 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100549 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400550 }
551
Linus Walleij97548572017-09-20 10:02:00 +0200552 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200553 err = mmc_set_blockcount(card, data.blocks,
554 idata->ic.write_flag & (1 << 31));
555 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100556 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200557 }
558
Yaniv Gardia82e4842013-06-05 14:13:08 +0300559 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
560 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300561 err = ioctl_do_sanitize(card);
562
563 if (err)
564 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
565 __func__, err);
566
Jon Huntera5f57742015-09-22 10:27:53 +0100567 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300568 }
569
John Calixtocb87ea22011-04-26 18:56:29 -0400570 mmc_wait_for_req(card->host, &mrq);
571
572 if (cmd.error) {
573 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
574 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100575 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400576 }
577 if (data.error) {
578 dev_err(mmc_dev(card->host), "%s: data error %d\n",
579 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100580 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400581 }
582
583 /*
584 * According to the SD specs, some commands require a delay after
585 * issuing the command.
586 */
587 if (idata->ic.postsleep_min_us)
588 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
589
Jon Huntera5f57742015-09-22 10:27:53 +0100590 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400591
Linus Walleij97548572017-09-20 10:02:00 +0200592 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200593 /*
594 * Ensure RPMB command has completed by polling CMD13
595 * "Send Status".
596 */
597 err = ioctl_rpmb_card_status_poll(card, &status, 5);
598 if (err)
599 dev_err(mmc_dev(card->host),
600 "%s: Card Status=0x%08X, error %d\n",
601 __func__, status, err);
602 }
603
Jon Huntera5f57742015-09-22 10:27:53 +0100604 return err;
605}
606
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200607static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200608 struct mmc_ioc_cmd __user *ic_ptr,
609 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100610{
611 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200612 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200613 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100614 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700615 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200616 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100617
618 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
619 if (IS_ERR(idata))
620 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200621 /* This will be NULL on non-RPMB ioctl():s */
622 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100623
Jon Huntera5f57742015-09-22 10:27:53 +0100624 card = md->queue.card;
625 if (IS_ERR(card)) {
626 err = PTR_ERR(card);
627 goto cmd_done;
628 }
629
Linus Walleij614f0382017-05-18 11:29:34 +0200630 /*
631 * Dispatch the ioctl() into the block request queue.
632 */
633 mq = &md->queue;
634 req = blk_get_request(mq->queue,
635 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
636 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200637 if (IS_ERR(req)) {
638 err = PTR_ERR(req);
639 goto cmd_done;
640 }
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200641 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200642 req_to_mmc_queue_req(req)->drv_op =
643 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200644 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200645 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200646 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200647 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700648 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200649 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100650
John Calixtocb87ea22011-04-26 18:56:29 -0400651cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400652 kfree(idata->buf);
653 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700654 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400655}
656
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200657static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200658 struct mmc_ioc_multi_cmd __user *user,
659 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100660{
661 struct mmc_blk_ioc_data **idata = NULL;
662 struct mmc_ioc_cmd __user *cmds = user->cmds;
663 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200664 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700665 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100666 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200667 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100668
669 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
670 sizeof(num_of_cmds)))
671 return -EFAULT;
672
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200673 if (!num_of_cmds)
674 return 0;
675
Jon Huntera5f57742015-09-22 10:27:53 +0100676 if (num_of_cmds > MMC_IOC_MAX_CMDS)
677 return -EINVAL;
678
679 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
680 if (!idata)
681 return -ENOMEM;
682
683 for (i = 0; i < num_of_cmds; i++) {
684 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
685 if (IS_ERR(idata[i])) {
686 err = PTR_ERR(idata[i]);
687 num_of_cmds = i;
688 goto cmd_err;
689 }
Linus Walleij97548572017-09-20 10:02:00 +0200690 /* This will be NULL on non-RPMB ioctl():s */
691 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100692 }
693
Jon Huntera5f57742015-09-22 10:27:53 +0100694 card = md->queue.card;
695 if (IS_ERR(card)) {
696 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200697 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100698 }
699
Jon Huntera5f57742015-09-22 10:27:53 +0100700
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200701 /*
702 * Dispatch the ioctl()s into the block request queue.
703 */
704 mq = &md->queue;
705 req = blk_get_request(mq->queue,
706 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
707 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200708 if (IS_ERR(req)) {
709 err = PTR_ERR(req);
710 goto cmd_err;
711 }
Linus Walleij97548572017-09-20 10:02:00 +0200712 req_to_mmc_queue_req(req)->drv_op =
713 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200714 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200715 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
716 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200717 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100718
719 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700720 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100721 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100722
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200723 blk_put_request(req);
724
Jon Huntera5f57742015-09-22 10:27:53 +0100725cmd_err:
726 for (i = 0; i < num_of_cmds; i++) {
727 kfree(idata[i]->buf);
728 kfree(idata[i]);
729 }
730 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700731 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100732}
733
Linus Walleij61fe0e22017-08-20 23:39:09 +0200734static int mmc_blk_check_blkdev(struct block_device *bdev)
735{
736 /*
737 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
738 * whole block device, not on a partition. This prevents overspray
739 * between sibling partitions.
740 */
741 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
742 return -EPERM;
743 return 0;
744}
745
John Calixtocb87ea22011-04-26 18:56:29 -0400746static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
747 unsigned int cmd, unsigned long arg)
748{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200749 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200750 int ret;
751
Jon Huntera5f57742015-09-22 10:27:53 +0100752 switch (cmd) {
753 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200754 ret = mmc_blk_check_blkdev(bdev);
755 if (ret)
756 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200757 md = mmc_blk_get(bdev->bd_disk);
758 if (!md)
759 return -EINVAL;
760 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200761 (struct mmc_ioc_cmd __user *)arg,
762 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200763 mmc_blk_put(md);
764 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100765 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200766 ret = mmc_blk_check_blkdev(bdev);
767 if (ret)
768 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200769 md = mmc_blk_get(bdev->bd_disk);
770 if (!md)
771 return -EINVAL;
772 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200773 (struct mmc_ioc_multi_cmd __user *)arg,
774 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200775 mmc_blk_put(md);
776 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100777 default:
778 return -EINVAL;
779 }
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
782#ifdef CONFIG_COMPAT
783static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
784 unsigned int cmd, unsigned long arg)
785{
786 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
787}
788#endif
789
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700790static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500791 .open = mmc_blk_open,
792 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800793 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400795 .ioctl = mmc_blk_ioctl,
796#ifdef CONFIG_COMPAT
797 .compat_ioctl = mmc_blk_compat_ioctl,
798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799};
800
Adrian Hunter025e3d52017-03-13 14:36:39 +0200801static int mmc_blk_part_switch_pre(struct mmc_card *card,
802 unsigned int part_type)
803{
804 int ret = 0;
805
806 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
807 if (card->ext_csd.cmdq_en) {
808 ret = mmc_cmdq_disable(card);
809 if (ret)
810 return ret;
811 }
812 mmc_retune_pause(card->host);
813 }
814
815 return ret;
816}
817
818static int mmc_blk_part_switch_post(struct mmc_card *card,
819 unsigned int part_type)
820{
821 int ret = 0;
822
823 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
824 mmc_retune_unpause(card->host);
825 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
826 ret = mmc_cmdq_enable(card);
827 }
828
829 return ret;
830}
831
Andrei Warkentin371a6892011-04-11 18:10:25 -0500832static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200833 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500834{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200835 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200836 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300837
Linus Walleij1f797ed2017-08-20 23:39:10 +0200838 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500839 return 0;
840
841 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300842 u8 part_config = card->ext_csd.part_config;
843
Linus Walleij1f797ed2017-08-20 23:39:10 +0200844 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200845 if (ret)
846 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300847
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300848 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200849 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500850
851 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300852 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500853 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300854 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200855 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500856 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300857 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300858
859 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300860
Adrian Hunter025e3d52017-03-13 14:36:39 +0200861 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300862 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500863
Linus Walleij1f797ed2017-08-20 23:39:10 +0200864 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200865 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500866}
867
Linus Walleij169f03a2017-02-01 13:47:57 +0100868static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700869{
870 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100871 u32 result;
872 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700873
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900874 struct mmc_request mrq = {};
875 struct mmc_command cmd = {};
876 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700877
878 struct scatterlist sg;
879
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700880 cmd.opcode = MMC_APP_CMD;
881 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700882 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700883
884 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700885 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100886 return err;
David Brownell7213d172007-08-08 09:10:23 -0700887 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100888 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700889
890 memset(&cmd, 0, sizeof(struct mmc_command));
891
892 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
893 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700894 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700895
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700896 data.blksz = 4;
897 data.blocks = 1;
898 data.flags = MMC_DATA_READ;
899 data.sg = &sg;
900 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530901 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700902
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700903 mrq.cmd = &cmd;
904 mrq.data = &data;
905
Ben Dooks051913d2009-06-08 23:33:57 +0100906 blocks = kmalloc(4, GFP_KERNEL);
907 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100908 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100909
910 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700911
912 mmc_wait_for_req(card->host, &mrq);
913
Ben Dooks051913d2009-06-08 23:33:57 +0100914 result = ntohl(*blocks);
915 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700916
Ben Dooks051913d2009-06-08 23:33:57 +0100917 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100918 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100919
Linus Walleij169f03a2017-02-01 13:47:57 +0100920 *written_blocks = result;
921
922 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700923}
924
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100925static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Adrian Hunterc89b4852017-11-29 15:41:09 +0200926 bool hw_busy_detect, struct request *req,
927 u32 *resp_errs)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100928{
929 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
930 int err = 0;
931 u32 status;
932
933 do {
Adrian Hunter77018852017-11-29 15:41:11 +0200934 bool done = time_after(jiffies, timeout);
935
Ulf Hansson2185bc22017-05-22 10:23:58 +0200936 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100937 if (err) {
938 pr_err("%s: error %d requesting status\n",
939 req->rq_disk->disk_name, err);
940 return err;
941 }
942
Adrian Hunterc89b4852017-11-29 15:41:09 +0200943 /* Accumulate any response error bits seen */
944 if (resp_errs)
945 *resp_errs |= status;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100946
Ulf Hansson95a91292014-01-29 13:11:27 +0100947 /* We may rely on the host hw to handle busy detection.*/
948 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
949 hw_busy_detect)
950 break;
951
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100952 /*
953 * Timeout if the device never becomes ready for data and never
954 * leaves the program state.
955 */
Adrian Hunter77018852017-11-29 15:41:11 +0200956 if (done) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100957 pr_err("%s: Card stuck in programming state! %s %s\n",
958 mmc_hostname(card->host),
959 req->rq_disk->disk_name, __func__);
960 return -ETIMEDOUT;
961 }
962
963 /*
964 * Some cards mishandle the status bits,
965 * so make sure to check both the busy
966 * indication and the card state.
967 */
968 } while (!(status & R1_READY_FOR_DATA) ||
969 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
970
971 return err;
972}
973
Adrian Hunterc89b4852017-11-29 15:41:09 +0200974static int card_busy_detect_err(struct mmc_card *card, unsigned int timeout_ms,
975 bool hw_busy_detect, struct request *req,
976 bool *gen_err)
977{
978 u32 resp_errs = 0;
979 int err;
980
981 err = card_busy_detect(card, timeout_ms, hw_busy_detect, req,
982 &resp_errs);
983 if (resp_errs & R1_ERROR) {
984 pr_err("%s: %s: error sending status cmd, status %#x\n",
985 req->rq_disk->disk_name, __func__, resp_errs);
986 *gen_err = true;
987 }
988
989 return err;
990}
991
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100992static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100993 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100994{
995 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900996 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100997 int err;
998 bool use_r1b_resp = rq_data_dir(req) == WRITE;
999
1000 /*
1001 * Normally we use R1B responses for WRITE, but in cases where the host
1002 * has specified a max_busy_timeout we need to validate it. A failure
1003 * means we need to prevent the host from doing hw busy detection, which
1004 * is done by converting to a R1 response instead.
1005 */
1006 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1007 use_r1b_resp = false;
1008
1009 cmd.opcode = MMC_STOP_TRANSMISSION;
1010 if (use_r1b_resp) {
1011 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1012 cmd.busy_timeout = timeout_ms;
1013 } else {
1014 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1015 }
1016
1017 err = mmc_wait_for_cmd(host, &cmd, 5);
1018 if (err)
1019 return err;
1020
1021 *stop_status = cmd.resp[0];
1022
1023 /* No need to check card status in case of READ. */
1024 if (rq_data_dir(req) == READ)
1025 return 0;
1026
1027 if (!mmc_host_is_spi(host) &&
1028 (*stop_status & R1_ERROR)) {
1029 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1030 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001031 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001032 }
1033
Adrian Hunterc89b4852017-11-29 15:41:09 +02001034 return card_busy_detect_err(card, timeout_ms, use_r1b_resp, req,
1035 gen_err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001036}
1037
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301038#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001039#define ERR_RETRY 2
1040#define ERR_ABORT 1
1041#define ERR_CONTINUE 0
1042
1043static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1044 bool status_valid, u32 status)
1045{
1046 switch (error) {
1047 case -EILSEQ:
1048 /* response crc error, retry the r/w cmd */
1049 pr_err("%s: %s sending %s command, card status %#x\n",
1050 req->rq_disk->disk_name, "response CRC error",
1051 name, status);
1052 return ERR_RETRY;
1053
1054 case -ETIMEDOUT:
1055 pr_err("%s: %s sending %s command, card status %#x\n",
1056 req->rq_disk->disk_name, "timed out", name, status);
1057
1058 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301059 if (!status_valid) {
1060 pr_err("%s: status not valid, retrying timeout\n",
1061 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001062 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301063 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001064
1065 /*
1066 * If it was a r/w cmd crc error, or illegal command
1067 * (eg, issued in wrong state) then retry - we should
1068 * have corrected the state problem above.
1069 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301070 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1071 pr_err("%s: command error, retrying timeout\n",
1072 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001073 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301074 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001075
1076 /* Otherwise abort the command */
1077 return ERR_ABORT;
1078
1079 default:
1080 /* We don't understand the error code the driver gave us */
1081 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1082 req->rq_disk->disk_name, error, status);
1083 return ERR_ABORT;
1084 }
1085}
1086
1087/*
1088 * Initial r/w and stop cmd error recovery.
1089 * We don't know whether the card received the r/w cmd or not, so try to
1090 * restore things back to a sane state. Essentially, we do this as follows:
1091 * - Obtain card status. If the first attempt to obtain card status fails,
1092 * the status word will reflect the failed status cmd, not the failed
1093 * r/w cmd. If we fail to obtain card status, it suggests we can no
1094 * longer communicate with the card.
1095 * - Check the card state. If the card received the cmd but there was a
1096 * transient problem with the response, it might still be in a data transfer
1097 * mode. Try to send it a stop command. If this fails, we can't recover.
1098 * - If the r/w cmd failed due to a response CRC error, it was probably
1099 * transient, so retry the cmd.
1100 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1101 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1102 * illegal cmd, retry.
1103 * Otherwise we don't understand what happened, so abort.
1104 */
1105static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001106 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001107{
1108 bool prev_cmd_status_valid = true;
1109 u32 status, stop_status = 0;
1110 int err, retry;
1111
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301112 if (mmc_card_removed(card))
1113 return ERR_NOMEDIUM;
1114
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001115 /*
1116 * Try to get card status which indicates both the card state
1117 * and why there was no response. If the first attempt fails,
1118 * we can't be sure the returned status is for the r/w command.
1119 */
1120 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001121 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001122 if (!err)
1123 break;
1124
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001125 /* Re-tune if needed */
1126 mmc_retune_recheck(card->host);
1127
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001128 prev_cmd_status_valid = false;
1129 pr_err("%s: error %d sending status command, %sing\n",
1130 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1131 }
1132
1133 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301134 if (err) {
1135 /* Check if the card is removed */
1136 if (mmc_detect_card_removed(card->host))
1137 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001138 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301139 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001140
Adrian Hunter67716322011-08-29 16:42:15 +03001141 /* Flag ECC errors */
1142 if ((status & R1_CARD_ECC_FAILED) ||
1143 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1144 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001145 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001146
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001147 /* Flag General errors */
1148 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1149 if ((status & R1_ERROR) ||
1150 (brq->stop.resp[0] & R1_ERROR)) {
1151 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1152 req->rq_disk->disk_name, __func__,
1153 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001154 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001155 }
1156
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001157 /*
1158 * Check the current card state. If it is in some data transfer
1159 * mode, tell it to stop (and hopefully transition back to TRAN.)
1160 */
1161 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1162 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001163 err = send_stop(card,
1164 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1165 req, gen_err, &stop_status);
1166 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001167 pr_err("%s: error %d sending stop command\n",
1168 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001169 /*
1170 * If the stop cmd also timed out, the card is probably
1171 * not present, so abort. Other errors are bad news too.
1172 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001173 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001174 }
1175
Adrian Hunter67716322011-08-29 16:42:15 +03001176 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001177 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001178 }
1179
1180 /* Check for set block count errors */
1181 if (brq->sbc.error)
1182 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1183 prev_cmd_status_valid, status);
1184
1185 /* Check for r/w command errors */
1186 if (brq->cmd.error)
1187 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1188 prev_cmd_status_valid, status);
1189
Adrian Hunter67716322011-08-29 16:42:15 +03001190 /* Data errors */
1191 if (!brq->stop.error)
1192 return ERR_CONTINUE;
1193
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001194 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001195 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 +01001196 req->rq_disk->disk_name, brq->stop.error,
1197 brq->cmd.resp[0], status);
1198
1199 /*
1200 * Subsitute in our own stop status as this will give the error
1201 * state which happened during the execution of the r/w command.
1202 */
1203 if (stop_status) {
1204 brq->stop.resp[0] = stop_status;
1205 brq->stop.error = 0;
1206 }
1207 return ERR_CONTINUE;
1208}
1209
Adrian Hunter67716322011-08-29 16:42:15 +03001210static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1211 int type)
1212{
1213 int err;
1214
1215 if (md->reset_done & type)
1216 return -EEXIST;
1217
1218 md->reset_done |= type;
1219 err = mmc_hw_reset(host);
1220 /* Ensure we switch back to the correct partition */
1221 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001222 struct mmc_blk_data *main_md =
1223 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001224 int part_err;
1225
1226 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001227 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001228 if (part_err) {
1229 /*
1230 * We have failed to get back into the correct
1231 * partition, so we need to abort the whole request.
1232 */
1233 return -ENODEV;
1234 }
1235 }
1236 return err;
1237}
1238
1239static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1240{
1241 md->reset_done &= ~type;
1242}
1243
Adrian Hunter81196972017-11-29 15:41:03 +02001244static void mmc_blk_end_request(struct request *req, blk_status_t error)
1245{
1246 if (req->mq_ctx)
1247 blk_mq_end_request(req, error);
1248 else
1249 blk_end_request_all(req, error);
1250}
1251
Linus Walleij5ec12392017-05-19 15:37:29 +02001252/*
1253 * The non-block commands come back from the block layer after it queued it and
1254 * processed it with all other requests and then they get issued in this
1255 * function.
1256 */
1257static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1258{
1259 struct mmc_queue_req *mq_rq;
1260 struct mmc_card *card = mq->card;
1261 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001262 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001263 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001264 u8 **ext_csd;
1265 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001266 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001267 int i;
1268
1269 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001270 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001271
1272 switch (mq_rq->drv_op) {
1273 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001274 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001275 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001276 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001277 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001278 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001279 break;
1280 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001281 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001282 if (rpmb_ioctl)
1283 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001284 break;
1285 case MMC_DRV_OP_BOOT_WP:
1286 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1287 card->ext_csd.boot_ro_lock |
1288 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1289 card->ext_csd.part_time);
1290 if (ret)
1291 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1292 md->disk->disk_name, ret);
1293 else
1294 card->ext_csd.boot_ro_lock |=
1295 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001296 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001297 case MMC_DRV_OP_GET_CARD_STATUS:
1298 ret = mmc_send_status(card, &status);
1299 if (!ret)
1300 ret = status;
1301 break;
1302 case MMC_DRV_OP_GET_EXT_CSD:
1303 ext_csd = mq_rq->drv_op_data;
1304 ret = mmc_get_ext_csd(card, ext_csd);
1305 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001306 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001307 pr_err("%s: unknown driver specific operation\n",
1308 md->disk->disk_name);
1309 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001310 break;
1311 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001312 mq_rq->drv_op_result = ret;
Adrian Hunter81196972017-11-29 15:41:03 +02001313 mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001314}
1315
Linus Walleijdf061582017-01-24 11:17:57 +01001316static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001317{
Linus Walleij7db30282016-11-18 13:36:15 +01001318 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001319 struct mmc_card *card = md->queue.card;
1320 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001321 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001322 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001323
Adrian Hunterbd788c92010-08-11 14:17:47 -07001324 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001325 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001326 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001327 }
1328
1329 from = blk_rq_pos(req);
1330 nr = blk_rq_sectors(req);
1331
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001332 if (mmc_can_discard(card))
1333 arg = MMC_DISCARD_ARG;
1334 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001335 arg = MMC_TRIM_ARG;
1336 else
1337 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001338 do {
1339 err = 0;
1340 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1341 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1342 INAND_CMD38_ARG_EXT_CSD,
1343 arg == MMC_TRIM_ARG ?
1344 INAND_CMD38_ARG_TRIM :
1345 INAND_CMD38_ARG_ERASE,
1346 0);
1347 }
1348 if (!err)
1349 err = mmc_erase(card, from, nr, arg);
1350 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001351 if (err)
1352 status = BLK_STS_IOERR;
1353 else
Adrian Hunter67716322011-08-29 16:42:15 +03001354 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001355fail:
Adrian Hunter81196972017-11-29 15:41:03 +02001356 mmc_blk_end_request(req, status);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001357}
1358
Linus Walleijdf061582017-01-24 11:17:57 +01001359static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001360 struct request *req)
1361{
Linus Walleij7db30282016-11-18 13:36:15 +01001362 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001363 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001364 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001365 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001366 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001367
Maya Erez775a9362013-04-18 15:41:55 +03001368 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001369 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001370 goto out;
1371 }
1372
1373 from = blk_rq_pos(req);
1374 nr = blk_rq_sectors(req);
1375
Maya Erez775a9362013-04-18 15:41:55 +03001376 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1377 arg = MMC_SECURE_TRIM1_ARG;
1378 else
1379 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001380
Adrian Hunter67716322011-08-29 16:42:15 +03001381retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001382 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1383 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1384 INAND_CMD38_ARG_EXT_CSD,
1385 arg == MMC_SECURE_TRIM1_ARG ?
1386 INAND_CMD38_ARG_SECTRIM1 :
1387 INAND_CMD38_ARG_SECERASE,
1388 0);
1389 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001390 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001391 }
Adrian Hunter28302812012-04-05 14:45:48 +03001392
Adrian Hunter49804542010-08-11 14:17:50 -07001393 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001394 if (err == -EIO)
1395 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001396 if (err) {
1397 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001398 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001399 }
Adrian Hunter28302812012-04-05 14:45:48 +03001400
1401 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001402 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1403 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1404 INAND_CMD38_ARG_EXT_CSD,
1405 INAND_CMD38_ARG_SECTRIM2,
1406 0);
1407 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001408 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001409 }
Adrian Hunter28302812012-04-05 14:45:48 +03001410
Adrian Hunter49804542010-08-11 14:17:50 -07001411 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001412 if (err == -EIO)
1413 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001414 if (err) {
1415 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001416 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001417 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001418 }
Adrian Hunter28302812012-04-05 14:45:48 +03001419
Adrian Hunter28302812012-04-05 14:45:48 +03001420out_retry:
1421 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001422 goto retry;
1423 if (!err)
1424 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001425out:
Adrian Hunter81196972017-11-29 15:41:03 +02001426 mmc_blk_end_request(req, status);
Adrian Hunter49804542010-08-11 14:17:50 -07001427}
1428
Linus Walleijdf061582017-01-24 11:17:57 +01001429static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001430{
Linus Walleij7db30282016-11-18 13:36:15 +01001431 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001432 struct mmc_card *card = md->queue.card;
1433 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001434
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001435 ret = mmc_flush_cache(card);
Adrian Hunter81196972017-11-29 15:41:03 +02001436 mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001437}
1438
1439/*
1440 * Reformat current write as a reliable write, supporting
1441 * both legacy and the enhanced reliable write MMC cards.
1442 * In each transfer we'll handle only as much as a single
1443 * reliable write can handle, thus finish the request in
1444 * partial completions.
1445 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001446static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1447 struct mmc_card *card,
1448 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001449{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001450 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1451 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001452 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001453 brq->data.blocks = 1;
1454
1455 if (brq->data.blocks > card->ext_csd.rel_sectors)
1456 brq->data.blocks = card->ext_csd.rel_sectors;
1457 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1458 brq->data.blocks = 1;
1459 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001460}
1461
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001462#define CMD_ERRORS_EXCL_OOR \
1463 (R1_ADDRESS_ERROR | /* Misaligned address */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001464 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1465 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001466 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001467 R1_CC_ERROR | /* Card controller error */ \
1468 R1_ERROR) /* General/unknown error */
1469
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02001470#define CMD_ERRORS \
1471 (CMD_ERRORS_EXCL_OOR | \
1472 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1473
Shawn Lind83c2db2017-08-18 09:16:08 +08001474static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001475{
Shawn Lind83c2db2017-08-18 09:16:08 +08001476 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001477
Shawn Lind83c2db2017-08-18 09:16:08 +08001478 /*
1479 * Per the SD specification(physical layer version 4.10)[1],
1480 * section 4.3.3, it explicitly states that "When the last
1481 * block of user area is read using CMD18, the host should
1482 * ignore OUT_OF_RANGE error that may occur even the sequence
1483 * is correct". And JESD84-B51 for eMMC also has a similar
1484 * statement on section 6.8.3.
1485 *
1486 * Multiple block read/write could be done by either predefined
1487 * method, namely CMD23, or open-ending mode. For open-ending mode,
1488 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1489 *
1490 * However the spec[1] doesn't tell us whether we should also
1491 * ignore that for predefined method. But per the spec[1], section
1492 * 4.15 Set Block Count Command, it says"If illegal block count
1493 * is set, out of range error will be indicated during read/write
1494 * operation (For example, data transfer is stopped at user area
1495 * boundary)." In another word, we could expect a out of range error
1496 * in the response for the following CMD18/25. And if argument of
1497 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1498 * we could also expect to get a -ETIMEDOUT or any error number from
1499 * the host drivers due to missing data response(for write)/data(for
1500 * read), as the cards will stop the data transfer by itself per the
1501 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1502 */
1503
1504 if (!brq->stop.error) {
1505 bool oor_with_open_end;
1506 /* If there is no error yet, check R1 response */
1507
1508 val = brq->stop.resp[0] & CMD_ERRORS;
1509 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1510
1511 if (val && !oor_with_open_end)
1512 brq->stop.error = -EIO;
1513 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001514}
1515
Adrian Hunter81196972017-11-29 15:41:03 +02001516static enum mmc_blk_status __mmc_blk_err_check(struct mmc_card *card,
1517 struct mmc_queue_req *mq_mrq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001518{
Per Forlinee8a43a2011-07-01 18:55:33 +02001519 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001520 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001521 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001522 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001523 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001524
1525 /*
1526 * sbc.error indicates a problem with the set block count
1527 * command. No data will have been transferred.
1528 *
1529 * cmd.error indicates a problem with the r/w command. No
1530 * data will have been transferred.
1531 *
1532 * stop.error indicates a problem with the stop command. Data
1533 * may have been transferred, or may still be transferring.
1534 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001535
1536 mmc_blk_eval_resp_error(brq);
1537
1538 if (brq->sbc.error || brq->cmd.error ||
1539 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001540 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001541 case ERR_RETRY:
1542 return MMC_BLK_RETRY;
1543 case ERR_ABORT:
1544 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301545 case ERR_NOMEDIUM:
1546 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001547 case ERR_CONTINUE:
1548 break;
1549 }
1550 }
1551
1552 /*
1553 * Check for errors relating to the execution of the
1554 * initial command - such as address errors. No data
1555 * has been transferred.
1556 */
1557 if (brq->cmd.resp[0] & CMD_ERRORS) {
1558 pr_err("%s: r/w command failed, status = %#x\n",
1559 req->rq_disk->disk_name, brq->cmd.resp[0]);
1560 return MMC_BLK_ABORT;
1561 }
1562
1563 /*
1564 * Everything else is either success, or a data error of some
1565 * kind. If it was a write, we may have transitioned to
1566 * program mode, which we have to wait for it to complete.
1567 */
1568 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001569 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001570
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001571 /* Check stop command response */
1572 if (brq->stop.resp[0] & R1_ERROR) {
1573 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1574 req->rq_disk->disk_name, __func__,
1575 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001576 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001577 }
1578
Adrian Hunterc89b4852017-11-29 15:41:09 +02001579 err = card_busy_detect_err(card, MMC_BLK_TIMEOUT_MS, false, req,
1580 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001581 if (err)
1582 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001583 }
1584
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001585 /* if general error occurs, retry the write operation. */
1586 if (gen_err) {
1587 pr_warn("%s: retrying write for general error\n",
1588 req->rq_disk->disk_name);
1589 return MMC_BLK_RETRY;
1590 }
1591
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001592 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1593 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001594 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001595 pr_debug("%s: retrying because a re-tune was needed\n",
1596 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001597 brq->retune_retry_done = 1;
1598 return MMC_BLK_RETRY;
1599 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001600 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 +02001601 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001602 (unsigned)blk_rq_pos(req),
1603 (unsigned)blk_rq_sectors(req),
1604 brq->cmd.resp[0], brq->stop.resp[0]);
1605
1606 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001607 if (ecc_err)
1608 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001609 return MMC_BLK_DATA_ERR;
1610 } else {
1611 return MMC_BLK_CMD_ERR;
1612 }
1613 }
1614
Adrian Hunter67716322011-08-29 16:42:15 +03001615 if (!brq->data.bytes_xfered)
1616 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001617
Adrian Hunter67716322011-08-29 16:42:15 +03001618 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1619 return MMC_BLK_PARTIAL;
1620
1621 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001622}
1623
Adrian Hunter81196972017-11-29 15:41:03 +02001624static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1625 struct mmc_async_req *areq)
1626{
1627 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1628 areq);
1629
1630 return __mmc_blk_err_check(card, mq_mrq);
1631}
1632
Adrian Hunterca5717f2017-03-13 14:36:41 +02001633static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001634 int disable_multi, bool *do_rel_wr_p,
1635 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001637 struct mmc_blk_data *md = mq->blkdata;
1638 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001639 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001640 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001641 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001643 /*
1644 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001645 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001646 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001647 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1648 rq_data_dir(req) == WRITE &&
1649 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001650
Per Forlin54d49d72011-07-01 18:55:29 +02001651 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001652
Per Forlin54d49d72011-07-01 18:55:29 +02001653 brq->mrq.data = &brq->data;
Adrian Hunter93482b32017-09-22 15:36:56 +03001654 brq->mrq.tag = req->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Per Forlin54d49d72011-07-01 18:55:29 +02001656 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1657 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001658
1659 if (rq_data_dir(req) == READ) {
1660 brq->data.flags = MMC_DATA_READ;
1661 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1662 } else {
1663 brq->data.flags = MMC_DATA_WRITE;
1664 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1665 }
1666
1667 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001668 brq->data.blocks = blk_rq_sectors(req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001669 brq->data.blk_addr = blk_rq_pos(req);
1670
1671 /*
1672 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1673 * The eMMC will give "high" priority tasks priority over "simple"
1674 * priority tasks. Here we always set "simple" priority by not setting
1675 * MMC_DATA_PRIO.
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Per Forlin54d49d72011-07-01 18:55:29 +02001678 /*
1679 * The block layer doesn't support all sector count
1680 * restrictions, so we need to be prepared for too big
1681 * requests.
1682 */
1683 if (brq->data.blocks > card->host->max_blk_count)
1684 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001686 if (brq->data.blocks > 1) {
1687 /*
1688 * After a read error, we redo the request one sector
1689 * at a time in order to accurately determine which
1690 * sectors can be read successfully.
1691 */
1692 if (disable_multi)
1693 brq->data.blocks = 1;
1694
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001695 /*
1696 * Some controllers have HW issues while operating
1697 * in multiple I/O mode
1698 */
1699 if (card->host->ops->multi_io_quirk)
1700 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1701 (rq_data_dir(req) == READ) ?
1702 MMC_DATA_READ : MMC_DATA_WRITE,
1703 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001704 }
Per Forlin54d49d72011-07-01 18:55:29 +02001705
Adrian Hunter93482b32017-09-22 15:36:56 +03001706 if (do_rel_wr) {
Adrian Hunterca5717f2017-03-13 14:36:41 +02001707 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001708 brq->data.flags |= MMC_DATA_REL_WR;
1709 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001710
1711 /*
1712 * Data tag is used only during writing meta data to speed
1713 * up write and any subsequent read of this meta data
1714 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001715 do_data_tag = card->ext_csd.data_tag_unit_size &&
1716 (req->cmd_flags & REQ_META) &&
1717 (rq_data_dir(req) == WRITE) &&
1718 ((brq->data.blocks * brq->data.blksz) >=
1719 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001720
Adrian Hunter93482b32017-09-22 15:36:56 +03001721 if (do_data_tag)
1722 brq->data.flags |= MMC_DATA_DAT_TAG;
1723
Adrian Hunterca5717f2017-03-13 14:36:41 +02001724 mmc_set_data_timeout(&brq->data, card);
1725
1726 brq->data.sg = mqrq->sg;
1727 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1728
1729 /*
1730 * Adjust the sg list so it is the same size as the
1731 * request.
1732 */
1733 if (brq->data.blocks != blk_rq_sectors(req)) {
1734 int i, data_size = brq->data.blocks << 9;
1735 struct scatterlist *sg;
1736
1737 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1738 data_size -= sg->length;
1739 if (data_size <= 0) {
1740 sg->length += data_size;
1741 i++;
1742 break;
1743 }
1744 }
1745 brq->data.sg_len = i;
1746 }
1747
1748 mqrq->areq.mrq = &brq->mrq;
Adrian Hunterd3377c02017-09-22 15:36:55 +03001749
1750 if (do_rel_wr_p)
1751 *do_rel_wr_p = do_rel_wr;
1752
1753 if (do_data_tag_p)
1754 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001755}
1756
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02001757#define MMC_CQE_RETRIES 2
1758
1759static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1760{
1761 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1762 struct mmc_request *mrq = &mqrq->brq.mrq;
1763 struct request_queue *q = req->q;
1764 struct mmc_host *host = mq->card->host;
1765 unsigned long flags;
1766 bool put_card;
1767 int err;
1768
1769 mmc_cqe_post_req(host, mrq);
1770
1771 if (mrq->cmd && mrq->cmd->error)
1772 err = mrq->cmd->error;
1773 else if (mrq->data && mrq->data->error)
1774 err = mrq->data->error;
1775 else
1776 err = 0;
1777
1778 if (err) {
1779 if (mqrq->retries++ < MMC_CQE_RETRIES)
1780 blk_mq_requeue_request(req, true);
1781 else
1782 blk_mq_end_request(req, BLK_STS_IOERR);
1783 } else if (mrq->data) {
1784 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1785 blk_mq_requeue_request(req, true);
1786 else
1787 __blk_mq_end_request(req, BLK_STS_OK);
1788 } else {
1789 blk_mq_end_request(req, BLK_STS_OK);
1790 }
1791
1792 spin_lock_irqsave(q->queue_lock, flags);
1793
1794 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1795
1796 put_card = (mmc_tot_in_flight(mq) == 0);
1797
1798 mmc_cqe_check_busy(mq);
1799
1800 spin_unlock_irqrestore(q->queue_lock, flags);
1801
1802 if (!mq->cqe_busy)
1803 blk_mq_run_hw_queues(q, true);
1804
1805 if (put_card)
1806 mmc_put_card(mq->card, &mq->ctx);
1807}
1808
1809void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1810{
1811 struct mmc_card *card = mq->card;
1812 struct mmc_host *host = card->host;
1813 int err;
1814
1815 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1816
1817 err = mmc_cqe_recovery(host);
1818 if (err)
1819 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1820 else
1821 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1822
1823 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1824}
1825
1826static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1827{
1828 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1829 brq.mrq);
1830 struct request *req = mmc_queue_req_to_req(mqrq);
1831 struct request_queue *q = req->q;
1832 struct mmc_queue *mq = q->queuedata;
1833
1834 /*
1835 * Block layer timeouts race with completions which means the normal
1836 * completion path cannot be used during recovery.
1837 */
1838 if (mq->in_recovery)
1839 mmc_blk_cqe_complete_rq(mq, req);
1840 else
1841 blk_mq_complete_request(req);
1842}
1843
1844static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1845{
1846 mrq->done = mmc_blk_cqe_req_done;
1847 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1848
1849 return mmc_cqe_start_req(host, mrq);
1850}
1851
1852static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1853 struct request *req)
1854{
1855 struct mmc_blk_request *brq = &mqrq->brq;
1856
1857 memset(brq, 0, sizeof(*brq));
1858
1859 brq->mrq.cmd = &brq->cmd;
1860 brq->mrq.tag = req->tag;
1861
1862 return &brq->mrq;
1863}
1864
1865static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1866{
1867 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1868 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1869
1870 mrq->cmd->opcode = MMC_SWITCH;
1871 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1872 (EXT_CSD_FLUSH_CACHE << 16) |
1873 (1 << 8) |
1874 EXT_CSD_CMD_SET_NORMAL;
1875 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1876
1877 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1878}
1879
1880static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1881{
1882 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1883
1884 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1885
1886 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1887}
1888
Adrian Hunterca5717f2017-03-13 14:36:41 +02001889static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1890 struct mmc_card *card,
1891 int disable_multi,
1892 struct mmc_queue *mq)
1893{
1894 u32 readcmd, writecmd;
1895 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001896 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001897 struct mmc_blk_data *md = mq->blkdata;
1898 bool do_rel_wr, do_data_tag;
1899
1900 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1901
1902 brq->mrq.cmd = &brq->cmd;
1903
1904 brq->cmd.arg = blk_rq_pos(req);
1905 if (!mmc_card_blockaddr(card))
1906 brq->cmd.arg <<= 9;
1907 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1908
Per Forlin54d49d72011-07-01 18:55:29 +02001909 if (brq->data.blocks > 1 || do_rel_wr) {
1910 /* SPI multiblock writes terminate using a special
1911 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001912 */
Per Forlin54d49d72011-07-01 18:55:29 +02001913 if (!mmc_host_is_spi(card->host) ||
1914 rq_data_dir(req) == READ)
1915 brq->mrq.stop = &brq->stop;
1916 readcmd = MMC_READ_MULTIPLE_BLOCK;
1917 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1918 } else {
1919 brq->mrq.stop = NULL;
1920 readcmd = MMC_READ_SINGLE_BLOCK;
1921 writecmd = MMC_WRITE_BLOCK;
1922 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001923 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301924
1925 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001926 * Pre-defined multi-block transfers are preferable to
1927 * open ended-ones (and necessary for reliable writes).
1928 * However, it is not sufficient to just send CMD23,
1929 * and avoid the final CMD12, as on an error condition
1930 * CMD12 (stop) needs to be sent anyway. This, coupled
1931 * with Auto-CMD23 enhancements provided by some
1932 * hosts, means that the complexity of dealing
1933 * with this is best left to the host. If CMD23 is
1934 * supported by card and host, we'll fill sbc in and let
1935 * the host deal with handling it correctly. This means
1936 * that for hosts that don't expose MMC_CAP_CMD23, no
1937 * change of behavior will be observed.
1938 *
1939 * N.B: Some MMC cards experience perf degradation.
1940 * We'll avoid using CMD23-bounded multiblock writes for
1941 * these, while retaining features like reliable writes.
1942 */
Saugata Das42659002011-12-21 13:09:17 +05301943 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1944 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1945 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001946 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1947 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301948 (do_rel_wr ? (1 << 31) : 0) |
1949 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001950 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1951 brq->mrq.sbc = &brq->sbc;
1952 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001953
Linus Walleij74f5ba32017-02-01 13:47:55 +01001954 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001955}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Adrian Hunter81196972017-11-29 15:41:03 +02001957#define MMC_MAX_RETRIES 5
1958#define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1959
1960#define MMC_READ_SINGLE_RETRIES 2
1961
1962/* Single sector read during recovery */
1963static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1964{
1965 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1966 struct mmc_request *mrq = &mqrq->brq.mrq;
1967 struct mmc_card *card = mq->card;
1968 struct mmc_host *host = card->host;
1969 blk_status_t error = BLK_STS_OK;
1970 int retries = 0;
1971
1972 do {
1973 u32 status;
1974 int err;
1975
1976 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1977
1978 mmc_wait_for_req(host, mrq);
1979
1980 err = mmc_send_status(card, &status);
1981 if (err)
1982 goto error_exit;
1983
1984 if (!mmc_host_is_spi(host) &&
1985 R1_CURRENT_STATE(status) != R1_STATE_TRAN) {
1986 u32 stop_status = 0;
1987 bool gen_err = false;
1988
1989 err = send_stop(card,
1990 DIV_ROUND_UP(mrq->data->timeout_ns,
1991 1000000),
1992 req, &gen_err, &stop_status);
1993 if (err)
1994 goto error_exit;
1995 }
1996
1997 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1998 continue;
1999
2000 retries = 0;
2001
2002 if (mrq->cmd->error ||
2003 mrq->data->error ||
2004 (!mmc_host_is_spi(host) &&
2005 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
2006 error = BLK_STS_IOERR;
2007 else
2008 error = BLK_STS_OK;
2009
2010 } while (blk_update_request(req, error, 512));
2011
2012 return;
2013
2014error_exit:
2015 mrq->data->bytes_xfered = 0;
2016 blk_update_request(req, BLK_STS_IOERR, 512);
2017 /* Let it try the remaining request again */
2018 if (mqrq->retries > MMC_MAX_RETRIES - 1)
2019 mqrq->retries = MMC_MAX_RETRIES - 1;
2020}
2021
2022static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
2023{
2024 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
2025 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2026 struct mmc_blk_request *brq = &mqrq->brq;
2027 struct mmc_blk_data *md = mq->blkdata;
2028 struct mmc_card *card = mq->card;
2029 static enum mmc_blk_status status;
2030
2031 brq->retune_retry_done = mqrq->retries;
2032
2033 status = __mmc_blk_err_check(card, mqrq);
2034
2035 mmc_retune_release(card->host);
2036
2037 /*
2038 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
2039 * policy:
2040 * 1. A request that has transferred at least some data is considered
2041 * successful and will be requeued if there is remaining data to
2042 * transfer.
2043 * 2. Otherwise the number of retries is incremented and the request
2044 * will be requeued if there are remaining retries.
2045 * 3. Otherwise the request will be errored out.
2046 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
2047 * mqrq->retries. So there are only 4 possible actions here:
2048 * 1. do not accept the bytes_xfered value i.e. set it to zero
2049 * 2. change mqrq->retries to determine the number of retries
2050 * 3. try to reset the card
2051 * 4. read one sector at a time
2052 */
2053 switch (status) {
2054 case MMC_BLK_SUCCESS:
2055 case MMC_BLK_PARTIAL:
2056 /* Reset success, and accept bytes_xfered */
2057 mmc_blk_reset_success(md, type);
2058 break;
2059 case MMC_BLK_CMD_ERR:
2060 /*
2061 * For SD cards, get bytes written, but do not accept
2062 * bytes_xfered if that fails. For MMC cards accept
2063 * bytes_xfered. Then try to reset. If reset fails then
2064 * error out the remaining request, otherwise retry
2065 * once (N.B mmc_blk_reset() will not succeed twice in a
2066 * row).
2067 */
2068 if (mmc_card_sd(card)) {
2069 u32 blocks;
2070 int err;
2071
2072 err = mmc_sd_num_wr_blocks(card, &blocks);
2073 if (err)
2074 brq->data.bytes_xfered = 0;
2075 else
2076 brq->data.bytes_xfered = blocks << 9;
2077 }
2078 if (mmc_blk_reset(md, card->host, type))
2079 mqrq->retries = MMC_NO_RETRIES;
2080 else
2081 mqrq->retries = MMC_MAX_RETRIES - 1;
2082 break;
2083 case MMC_BLK_RETRY:
2084 /*
2085 * Do not accept bytes_xfered, but retry up to 5 times,
2086 * otherwise same as abort.
2087 */
2088 brq->data.bytes_xfered = 0;
2089 if (mqrq->retries < MMC_MAX_RETRIES)
2090 break;
2091 /* Fall through */
2092 case MMC_BLK_ABORT:
2093 /*
2094 * Do not accept bytes_xfered, but try to reset. If
2095 * reset succeeds, try once more, otherwise error out
2096 * the request.
2097 */
2098 brq->data.bytes_xfered = 0;
2099 if (mmc_blk_reset(md, card->host, type))
2100 mqrq->retries = MMC_NO_RETRIES;
2101 else
2102 mqrq->retries = MMC_MAX_RETRIES - 1;
2103 break;
2104 case MMC_BLK_DATA_ERR: {
2105 int err;
2106
2107 /*
2108 * Do not accept bytes_xfered, but try to reset. If
2109 * reset succeeds, try once more. If reset fails with
2110 * ENODEV which means the partition is wrong, then error
2111 * out the request. Otherwise attempt to read one sector
2112 * at a time.
2113 */
2114 brq->data.bytes_xfered = 0;
2115 err = mmc_blk_reset(md, card->host, type);
2116 if (!err) {
2117 mqrq->retries = MMC_MAX_RETRIES - 1;
2118 break;
2119 }
2120 if (err == -ENODEV) {
2121 mqrq->retries = MMC_NO_RETRIES;
2122 break;
2123 }
2124 /* Fall through */
2125 }
2126 case MMC_BLK_ECC_ERR:
2127 /*
2128 * Do not accept bytes_xfered. If reading more than one
2129 * sector, try reading one sector at a time.
2130 */
2131 brq->data.bytes_xfered = 0;
2132 /* FIXME: Missing single sector read for large sector size */
2133 if (brq->data.blocks > 1 && !mmc_large_sector(card)) {
2134 /* Redo read one sector at a time */
2135 pr_warn("%s: retrying using single block read\n",
2136 req->rq_disk->disk_name);
2137 mmc_blk_read_single(mq, req);
2138 } else {
2139 mqrq->retries = MMC_NO_RETRIES;
2140 }
2141 break;
2142 case MMC_BLK_NOMEDIUM:
2143 /* Do not accept bytes_xfered. Error out the request */
2144 brq->data.bytes_xfered = 0;
2145 mqrq->retries = MMC_NO_RETRIES;
2146 break;
2147 default:
2148 /* Do not accept bytes_xfered. Error out the request */
2149 brq->data.bytes_xfered = 0;
2150 mqrq->retries = MMC_NO_RETRIES;
2151 pr_err("%s: Unhandled return value (%d)",
2152 req->rq_disk->disk_name, status);
2153 break;
2154 }
2155}
2156
Adrian Hunter10f21df42017-11-29 15:41:07 +02002157static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
2158{
2159 mmc_blk_eval_resp_error(brq);
2160
2161 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
2162 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
2163}
2164
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02002165static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
2166{
2167 return !!brq->mrq.sbc;
2168}
2169
2170static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
2171{
2172 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
2173}
2174
Adrian Hunter88a51642017-11-29 15:41:08 +02002175static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
2176{
2177 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02002178 u32 status = 0;
Adrian Hunter88a51642017-11-29 15:41:08 +02002179 int err;
2180
2181 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
2182 return 0;
2183
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02002184 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, &status);
Adrian Hunter88a51642017-11-29 15:41:08 +02002185
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02002186 /*
2187 * Do not assume data transferred correctly if there are any error bits
2188 * set.
2189 */
2190 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
2191 mqrq->brq.data.bytes_xfered = 0;
Adrian Hunter88a51642017-11-29 15:41:08 +02002192 err = err ? err : -EIO;
2193 }
2194
Adrian Hunterf47a1fe2017-11-29 15:41:10 +02002195 /* Copy the exception bit so it will be seen later on */
2196 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
2197 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
2198
Adrian Hunter88a51642017-11-29 15:41:08 +02002199 return err;
2200}
2201
Adrian Hunter10f21df42017-11-29 15:41:07 +02002202static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
2203 struct request *req)
2204{
2205 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
2206
2207 mmc_blk_reset_success(mq->blkdata, type);
2208}
2209
Adrian Hunter81196972017-11-29 15:41:03 +02002210static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
2211{
2212 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2213 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
2214
2215 if (nr_bytes) {
2216 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
2217 blk_mq_requeue_request(req, true);
2218 else
2219 __blk_mq_end_request(req, BLK_STS_OK);
2220 } else if (!blk_rq_bytes(req)) {
2221 __blk_mq_end_request(req, BLK_STS_IOERR);
2222 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
2223 blk_mq_requeue_request(req, true);
2224 } else {
2225 if (mmc_card_removed(mq->card))
2226 req->rq_flags |= RQF_QUIET;
2227 blk_mq_end_request(req, BLK_STS_IOERR);
2228 }
2229}
2230
2231static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
2232 struct mmc_queue_req *mqrq)
2233{
2234 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
2235 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
2236 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
2237}
2238
2239static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
2240 struct mmc_queue_req *mqrq)
2241{
2242 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
2243 mmc_start_bkops(mq->card, true);
2244}
2245
2246void mmc_blk_mq_complete(struct request *req)
2247{
2248 struct mmc_queue *mq = req->q->queuedata;
2249
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002250 if (mq->use_cqe)
2251 mmc_blk_cqe_complete_rq(mq, req);
2252 else
2253 mmc_blk_mq_complete_rq(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02002254}
2255
2256static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2257 struct request *req)
2258{
2259 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
Adrian Hunter88a51642017-11-29 15:41:08 +02002260 struct mmc_host *host = mq->card->host;
Adrian Hunter81196972017-11-29 15:41:03 +02002261
Adrian Hunter88a51642017-11-29 15:41:08 +02002262 if (mmc_blk_rq_error(&mqrq->brq) ||
2263 mmc_blk_card_busy(mq->card, req)) {
2264 mmc_blk_mq_rw_recovery(mq, req);
2265 } else {
2266 mmc_blk_rw_reset_success(mq, req);
2267 mmc_retune_release(host);
2268 }
Adrian Hunter81196972017-11-29 15:41:03 +02002269
2270 mmc_blk_urgent_bkops(mq, mqrq);
2271}
2272
2273static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
2274{
2275 struct request_queue *q = req->q;
2276 unsigned long flags;
2277 bool put_card;
2278
2279 spin_lock_irqsave(q->queue_lock, flags);
2280
2281 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2282
2283 put_card = (mmc_tot_in_flight(mq) == 0);
2284
2285 spin_unlock_irqrestore(q->queue_lock, flags);
2286
2287 if (put_card)
2288 mmc_put_card(mq->card, &mq->ctx);
2289}
2290
2291static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2292{
2293 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2294 struct mmc_request *mrq = &mqrq->brq.mrq;
2295 struct mmc_host *host = mq->card->host;
2296
2297 mmc_post_req(host, mrq, 0);
2298
Adrian Hunter10f21df42017-11-29 15:41:07 +02002299 /*
2300 * Block layer timeouts race with completions which means the normal
2301 * completion path cannot be used during recovery.
2302 */
2303 if (mq->in_recovery)
2304 mmc_blk_mq_complete_rq(mq, req);
2305 else
2306 blk_mq_complete_request(req);
Adrian Hunter81196972017-11-29 15:41:03 +02002307
2308 mmc_blk_mq_dec_in_flight(mq, req);
2309}
2310
Adrian Hunter10f21df42017-11-29 15:41:07 +02002311void mmc_blk_mq_recovery(struct mmc_queue *mq)
2312{
2313 struct request *req = mq->recovery_req;
2314 struct mmc_host *host = mq->card->host;
2315 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2316
2317 mq->recovery_req = NULL;
2318 mq->rw_wait = false;
2319
2320 if (mmc_blk_rq_error(&mqrq->brq)) {
2321 mmc_retune_hold_now(host);
2322 mmc_blk_mq_rw_recovery(mq, req);
2323 }
2324
2325 mmc_blk_urgent_bkops(mq, mqrq);
2326
2327 mmc_blk_mq_post_req(mq, req);
2328}
2329
Adrian Hunter81196972017-11-29 15:41:03 +02002330static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2331 struct request **prev_req)
2332{
Adrian Hunter10f21df42017-11-29 15:41:07 +02002333 if (mmc_host_done_complete(mq->card->host))
2334 return;
2335
Adrian Hunter81196972017-11-29 15:41:03 +02002336 mutex_lock(&mq->complete_lock);
2337
2338 if (!mq->complete_req)
2339 goto out_unlock;
2340
2341 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2342
2343 if (prev_req)
2344 *prev_req = mq->complete_req;
2345 else
2346 mmc_blk_mq_post_req(mq, mq->complete_req);
2347
2348 mq->complete_req = NULL;
2349
2350out_unlock:
2351 mutex_unlock(&mq->complete_lock);
2352}
2353
2354void mmc_blk_mq_complete_work(struct work_struct *work)
2355{
2356 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2357 complete_work);
2358
2359 mmc_blk_mq_complete_prev_req(mq, NULL);
2360}
2361
2362static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2363{
2364 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2365 brq.mrq);
2366 struct request *req = mmc_queue_req_to_req(mqrq);
2367 struct request_queue *q = req->q;
2368 struct mmc_queue *mq = q->queuedata;
Adrian Hunter10f21df42017-11-29 15:41:07 +02002369 struct mmc_host *host = mq->card->host;
Adrian Hunter81196972017-11-29 15:41:03 +02002370 unsigned long flags;
Adrian Hunter81196972017-11-29 15:41:03 +02002371
Adrian Hunter10f21df42017-11-29 15:41:07 +02002372 if (!mmc_host_done_complete(host)) {
2373 bool waiting;
Adrian Hunter81196972017-11-29 15:41:03 +02002374
Adrian Hunter10f21df42017-11-29 15:41:07 +02002375 /*
2376 * We cannot complete the request in this context, so record
2377 * that there is a request to complete, and that a following
2378 * request does not need to wait (although it does need to
2379 * complete complete_req first).
2380 */
2381 spin_lock_irqsave(q->queue_lock, flags);
2382 mq->complete_req = req;
2383 mq->rw_wait = false;
2384 waiting = mq->waiting;
2385 spin_unlock_irqrestore(q->queue_lock, flags);
2386
2387 /*
2388 * If 'waiting' then the waiting task will complete this
2389 * request, otherwise queue a work to do it. Note that
2390 * complete_work may still race with the dispatch of a following
2391 * request.
2392 */
2393 if (waiting)
2394 wake_up(&mq->wait);
2395 else
2396 kblockd_schedule_work(&mq->complete_work);
2397
2398 return;
2399 }
2400
2401 /* Take the recovery path for errors or urgent background operations */
2402 if (mmc_blk_rq_error(&mqrq->brq) ||
2403 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2404 spin_lock_irqsave(q->queue_lock, flags);
2405 mq->recovery_needed = true;
2406 mq->recovery_req = req;
2407 spin_unlock_irqrestore(q->queue_lock, flags);
Adrian Hunter81196972017-11-29 15:41:03 +02002408 wake_up(&mq->wait);
Adrian Hunter10f21df42017-11-29 15:41:07 +02002409 schedule_work(&mq->recovery_work);
2410 return;
2411 }
2412
2413 mmc_blk_rw_reset_success(mq, req);
2414
2415 mq->rw_wait = false;
2416 wake_up(&mq->wait);
2417
2418 mmc_blk_mq_post_req(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02002419}
2420
2421static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2422{
2423 struct request_queue *q = mq->queue;
2424 unsigned long flags;
2425 bool done;
2426
2427 /*
Adrian Hunter10f21df42017-11-29 15:41:07 +02002428 * Wait while there is another request in progress, but not if recovery
2429 * is needed. Also indicate whether there is a request waiting to start.
Adrian Hunter81196972017-11-29 15:41:03 +02002430 */
2431 spin_lock_irqsave(q->queue_lock, flags);
Adrian Hunter10f21df42017-11-29 15:41:07 +02002432 if (mq->recovery_needed) {
2433 *err = -EBUSY;
2434 done = true;
2435 } else {
2436 done = !mq->rw_wait;
2437 }
Adrian Hunter81196972017-11-29 15:41:03 +02002438 mq->waiting = !done;
2439 spin_unlock_irqrestore(q->queue_lock, flags);
2440
2441 return done;
2442}
2443
2444static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2445{
2446 int err = 0;
2447
2448 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2449
2450 /* Always complete the previous request if there is one */
2451 mmc_blk_mq_complete_prev_req(mq, prev_req);
2452
2453 return err;
2454}
2455
2456static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2457 struct request *req)
2458{
2459 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2460 struct mmc_host *host = mq->card->host;
2461 struct request *prev_req = NULL;
2462 int err = 0;
2463
2464 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2465
2466 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2467
2468 mmc_pre_req(host, &mqrq->brq.mrq);
2469
2470 err = mmc_blk_rw_wait(mq, &prev_req);
2471 if (err)
2472 goto out_post_req;
2473
2474 mq->rw_wait = true;
2475
2476 err = mmc_start_request(host, &mqrq->brq.mrq);
2477
2478 if (prev_req)
2479 mmc_blk_mq_post_req(mq, prev_req);
2480
Adrian Hunter10f21df42017-11-29 15:41:07 +02002481 if (err)
Adrian Hunter81196972017-11-29 15:41:03 +02002482 mq->rw_wait = false;
Adrian Hunter10f21df42017-11-29 15:41:07 +02002483
2484 /* Release re-tuning here where there is no synchronization required */
2485 if (err || mmc_host_done_complete(host))
Adrian Hunter81196972017-11-29 15:41:03 +02002486 mmc_retune_release(host);
Adrian Hunter81196972017-11-29 15:41:03 +02002487
2488out_post_req:
2489 if (err)
2490 mmc_post_req(host, &mqrq->brq.mrq, err);
2491
2492 return err;
2493}
2494
2495static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2496{
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002497 if (mq->use_cqe)
2498 return host->cqe_ops->cqe_wait_for_idle(host);
2499
Adrian Hunter81196972017-11-29 15:41:03 +02002500 return mmc_blk_rw_wait(mq, NULL);
2501}
2502
2503enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2504{
2505 struct mmc_blk_data *md = mq->blkdata;
2506 struct mmc_card *card = md->queue.card;
2507 struct mmc_host *host = card->host;
2508 int ret;
2509
2510 ret = mmc_blk_part_switch(card, md->part_type);
2511 if (ret)
2512 return MMC_REQ_FAILED_TO_START;
2513
2514 switch (mmc_issue_type(mq, req)) {
2515 case MMC_ISSUE_SYNC:
2516 ret = mmc_blk_wait_for_idle(mq, host);
2517 if (ret)
2518 return MMC_REQ_BUSY;
2519 switch (req_op(req)) {
2520 case REQ_OP_DRV_IN:
2521 case REQ_OP_DRV_OUT:
2522 mmc_blk_issue_drv_op(mq, req);
2523 break;
2524 case REQ_OP_DISCARD:
2525 mmc_blk_issue_discard_rq(mq, req);
2526 break;
2527 case REQ_OP_SECURE_ERASE:
2528 mmc_blk_issue_secdiscard_rq(mq, req);
2529 break;
2530 case REQ_OP_FLUSH:
2531 mmc_blk_issue_flush(mq, req);
2532 break;
2533 default:
2534 WARN_ON_ONCE(1);
2535 return MMC_REQ_FAILED_TO_START;
2536 }
2537 return MMC_REQ_FINISHED;
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002538 case MMC_ISSUE_DCMD:
Adrian Hunter81196972017-11-29 15:41:03 +02002539 case MMC_ISSUE_ASYNC:
2540 switch (req_op(req)) {
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002541 case REQ_OP_FLUSH:
2542 ret = mmc_blk_cqe_issue_flush(mq, req);
2543 break;
Adrian Hunter81196972017-11-29 15:41:03 +02002544 case REQ_OP_READ:
2545 case REQ_OP_WRITE:
Adrian Hunter1e8e55b2017-11-29 15:41:04 +02002546 if (mq->use_cqe)
2547 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2548 else
2549 ret = mmc_blk_mq_issue_rw_rq(mq, req);
Adrian Hunter81196972017-11-29 15:41:03 +02002550 break;
2551 default:
2552 WARN_ON_ONCE(1);
2553 ret = -EINVAL;
2554 }
2555 if (!ret)
2556 return MMC_REQ_STARTED;
2557 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2558 default:
2559 WARN_ON_ONCE(1);
2560 return MMC_REQ_FAILED_TO_START;
2561 }
2562}
2563
Linus Walleij0e65f102017-02-01 13:47:58 +01002564static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2565 struct mmc_blk_request *brq, struct request *req,
2566 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03002567{
Linus Walleij0e65f102017-02-01 13:47:58 +01002568 bool req_pending;
2569
Adrian Hunter67716322011-08-29 16:42:15 +03002570 /*
2571 * If this is an SD card and we're writing, we can first
2572 * mark the known good sectors as ok.
2573 *
2574 * If the card is not SD, we can still ok written sectors
2575 * as reported by the controller (which might be less than
2576 * the real number of written sectors, but never more).
2577 */
2578 if (mmc_card_sd(card)) {
2579 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01002580 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03002581
Linus Walleij169f03a2017-02-01 13:47:57 +01002582 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01002583 if (err)
2584 req_pending = old_req_pending;
2585 else
Adrian Huntera7c17d82017-08-25 15:43:45 +03002586 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02002587 } else {
Adrian Huntera7c17d82017-08-25 15:43:45 +03002588 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002589 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002590 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03002591}
2592
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002593static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
2594 struct request *req,
2595 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01002596{
Linus Walleij4e1f7802017-01-24 11:17:52 +01002597 if (mmc_card_removed(card))
2598 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002599 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02002600 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01002601}
2602
Linus Walleijb2928e12017-02-01 13:47:54 +01002603/**
2604 * mmc_blk_rw_try_restart() - tries to restart the current async request
2605 * @mq: the queue with the card and host to restart
2606 * @req: a new request that want to be started after the current one
2607 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002608static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
2609 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01002610{
2611 if (!req)
2612 return;
2613
Linus Walleijb2928e12017-02-01 13:47:54 +01002614 /*
2615 * If the card was removed, just cancel everything and return.
2616 */
2617 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01002618 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002619 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02002620 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01002621 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01002622 }
Linus Walleijb2928e12017-02-01 13:47:54 +01002623 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002624 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2625 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01002626}
2627
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002628static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02002629{
Linus Walleij7db30282016-11-18 13:36:15 +01002630 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02002631 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02002632 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01002633 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02002634 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002635 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02002636 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002637 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01002638 struct mmc_async_req *new_areq;
2639 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01002640 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02002641
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002642 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02002643 mqrq_cur = req_to_mmc_queue_req(new_req);
2644 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002645 }
2646
2647 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01002648 return;
Per Forlin54d49d72011-07-01 18:55:29 +02002649
2650 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002651 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302652 /*
2653 * When 4KB native sector is enabled, only 8 blocks
2654 * multiple read or write is allowed
2655 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002656 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002657 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302658 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002659 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002660 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01002661 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302662 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002663
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002664 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
2665 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02002666 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01002667 new_areq = NULL;
2668
Linus Walleijc3399ef2017-02-01 13:47:53 +01002669 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01002670 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01002671 /*
2672 * We have just put the first request into the pipeline
2673 * and there is nothing more to do until it is
2674 * complete.
2675 */
Linus Walleijdf061582017-01-24 11:17:57 +01002676 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002677 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002678
Linus Walleijda0dbaf2017-01-24 11:17:55 +01002679 /*
2680 * An asynchronous request has been completed and we proceed
2681 * to handle the result of it.
2682 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01002683 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002684 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02002685 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002686 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Pierre Ossman98ccf142007-05-12 00:26:16 +02002687
Per Forlind78d4a8a2011-07-01 18:55:30 +02002688 switch (status) {
2689 case MMC_BLK_SUCCESS:
2690 case MMC_BLK_PARTIAL:
2691 /*
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002692 * Reset success, and accept bytes_xfered. For
2693 * MMC_BLK_PARTIAL re-submit the remaining request. For
2694 * MMC_BLK_SUCCESS error out the remaining request (it
2695 * could not be re-submitted anyway if a next request
2696 * had already begun).
Per Forlind78d4a8a2011-07-01 18:55:30 +02002697 */
Adrian Hunter67716322011-08-29 16:42:15 +03002698 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002699
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002700 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01002701 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002702 /*
2703 * If the blk_end_request function returns non-zero even
2704 * though all data has been transferred and no errors
2705 * were returned by the host controller, it's a bug.
2706 */
Linus Walleij0e65f102017-02-01 13:47:58 +01002707 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302708 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002709 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02002710 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002711 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01002712 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02002713 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02002714 break;
2715 case MMC_BLK_CMD_ERR:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002716 /*
2717 * For SD cards, get bytes written, but do not accept
2718 * bytes_xfered if that fails. For MMC cards accept
2719 * bytes_xfered. Then try to reset. If reset fails then
2720 * error out the remaining request, otherwise retry
2721 * once (N.B mmc_blk_reset() will not succeed twice in a
2722 * row).
2723 */
Linus Walleij0e65f102017-02-01 13:47:58 +01002724 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01002725 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02002726 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002727 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
2728 else
Linus Walleij304419d2017-05-18 11:29:32 +02002729 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002730 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002731 return;
2732 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002733 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02002734 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002735 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002736 return;
2737 }
Ding Wang29535f72015-05-18 20:14:15 +08002738 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02002739 case MMC_BLK_RETRY:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002740 /*
2741 * Do not accept bytes_xfered, but retry up to 5 times,
2742 * otherwise same as abort.
2743 */
Adrian Hunterb8360a42015-05-07 13:10:24 +03002744 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02002745 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002746 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002747 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02002748 case MMC_BLK_ABORT:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002749 /*
2750 * Do not accept bytes_xfered, but try to reset. If
2751 * reset succeeds, try once more, otherwise error out
2752 * the request.
2753 */
Adrian Hunter67716322011-08-29 16:42:15 +03002754 if (!mmc_blk_reset(md, card->host, type))
2755 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002756 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002757 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002758 return;
Adrian Hunter67716322011-08-29 16:42:15 +03002759 case MMC_BLK_DATA_ERR: {
2760 int err;
2761
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002762 /*
2763 * Do not accept bytes_xfered, but try to reset. If
2764 * reset succeeds, try once more. If reset fails with
2765 * ENODEV which means the partition is wrong, then error
2766 * out the request. Otherwise attempt to read one sector
2767 * at a time.
2768 */
Adrian Hunter67716322011-08-29 16:42:15 +03002769 err = mmc_blk_reset(md, card->host, type);
2770 if (!err)
2771 break;
Linus Walleijdb435502017-02-01 13:47:51 +01002772 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002773 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002774 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002775 return;
2776 }
Adrian Hunter67716322011-08-29 16:42:15 +03002777 /* Fall through */
2778 }
2779 case MMC_BLK_ECC_ERR:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002780 /*
2781 * Do not accept bytes_xfered. If reading more than one
2782 * sector, try reading one sector at a time.
2783 */
Adrian Hunter67716322011-08-29 16:42:15 +03002784 if (brq->data.blocks > 1) {
2785 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002786 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002787 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002788 disable_multi = 1;
2789 break;
2790 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02002791 /*
2792 * After an error, we redo I/O one sector at a
2793 * time, so we only reach here after trying to
2794 * read a single sector.
2795 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002796 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01002797 brq->data.blksz);
2798 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02002799 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002800 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002801 return;
2802 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02002803 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302804 case MMC_BLK_NOMEDIUM:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002805 /* Do not accept bytes_xfered. Error out the request */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002806 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002807 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002808 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002809 default:
Adrian Hunter6d3898a62017-11-29 15:41:01 +02002810 /* Do not accept bytes_xfered. Error out the request */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002811 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002812 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002813 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002814 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002815 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002816 }
2817
Linus Walleij0e65f102017-02-01 13:47:58 +01002818 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01002819 /*
2820 * In case of a incomplete request
2821 * prepare it again and resend.
2822 */
2823 mmc_blk_rw_rq_prep(mq_rq, card,
2824 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01002825 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01002826 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002827 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002828 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002829 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002830
Linus Walleij304419d2017-05-18 11:29:32 +02002831 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Linus Walleijdf061582017-01-24 11:17:57 +01002834void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002835{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002836 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01002837 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002838 struct mmc_card *card = md->queue.card;
2839
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002840 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02002841 /* claim host only for the first request */
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002842 mmc_get_card(card, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02002843
Linus Walleij1f797ed2017-08-20 23:39:10 +02002844 ret = mmc_blk_part_switch(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002845 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002846 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002847 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002848 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002849 goto out;
2850 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002851
Linus Walleij614f0382017-05-18 11:29:34 +02002852 if (req) {
2853 switch (req_op(req)) {
2854 case REQ_OP_DRV_IN:
2855 case REQ_OP_DRV_OUT:
2856 /*
2857 * Complete ongoing async transfer before issuing
2858 * ioctl()s
2859 */
2860 if (mq->qcnt)
2861 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02002862 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02002863 break;
2864 case REQ_OP_DISCARD:
2865 /*
2866 * Complete ongoing async transfer before issuing
2867 * discard.
2868 */
2869 if (mq->qcnt)
2870 mmc_blk_issue_rw_rq(mq, NULL);
2871 mmc_blk_issue_discard_rq(mq, req);
2872 break;
2873 case REQ_OP_SECURE_ERASE:
2874 /*
2875 * Complete ongoing async transfer before issuing
2876 * secure erase.
2877 */
2878 if (mq->qcnt)
2879 mmc_blk_issue_rw_rq(mq, NULL);
2880 mmc_blk_issue_secdiscard_rq(mq, req);
2881 break;
2882 case REQ_OP_FLUSH:
2883 /*
2884 * Complete ongoing async transfer before issuing
2885 * flush.
2886 */
2887 if (mq->qcnt)
2888 mmc_blk_issue_rw_rq(mq, NULL);
2889 mmc_blk_issue_flush(mq, req);
2890 break;
2891 default:
2892 /* Normal request, just issue it */
2893 mmc_blk_issue_rw_rq(mq, req);
2894 card->host->context_info.is_waiting_last_req = false;
2895 break;
Wu Fengguang73222382017-05-23 05:11:33 +08002896 }
Adrian Hunter49804542010-08-11 14:17:50 -07002897 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002898 /* No request, flushing the pipeline with NULL */
2899 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002900 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002901 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002902
Andrei Warkentin371a6892011-04-11 18:10:25 -05002903out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002904 if (!mq->qcnt)
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002905 mmc_put_card(card, NULL);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Russell Kinga6f6c962006-01-03 22:38:44 +00002908static inline int mmc_blk_readonly(struct mmc_card *card)
2909{
2910 return mmc_card_readonly(card) ||
2911 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2912}
2913
Andrei Warkentin371a6892011-04-11 18:10:25 -05002914static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2915 struct device *parent,
2916 sector_t size,
2917 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002918 const char *subname,
2919 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct mmc_blk_data *md;
2922 int devidx, ret;
2923
Heiner Kallweita04848c2017-02-01 19:44:22 +01002924 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002925 if (devidx < 0) {
2926 /*
2927 * We get -ENOSPC because there are no more any available
2928 * devidx. The reason may be that, either userspace haven't yet
2929 * unmounted the partitions, which postpones mmc_blk_release()
2930 * from being called, or the device has more partitions than
2931 * what we support.
2932 */
2933 if (devidx == -ENOSPC)
2934 dev_err(mmc_dev(card->host),
2935 "no more device IDs available\n");
2936
Heiner Kallweita04848c2017-02-01 19:44:22 +01002937 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002940 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002941 if (!md) {
2942 ret = -ENOMEM;
2943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002945
Johan Rudholmadd710e2011-12-02 08:51:06 +01002946 md->area_type = area_type;
2947
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002948 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002949 * Set the read-only status based on the supported commands
2950 * and the write protect switch.
2951 */
2952 md->read_only = mmc_blk_readonly(card);
2953
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002954 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002955 if (md->disk == NULL) {
2956 ret = -ENOMEM;
2957 goto err_kfree;
2958 }
2959
2960 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002961 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002962 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002963 md->usage = 1;
2964
Adrian Hunterd09408a2011-06-23 13:40:28 +03002965 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002966 if (ret)
2967 goto err_putdisk;
2968
Linus Walleij7db30282016-11-18 13:36:15 +01002969 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002970
Adrian Hunter41e3efd2017-11-29 15:40:59 +02002971 /*
2972 * Keep an extra reference to the queue so that we can shutdown the
2973 * queue (i.e. call blk_cleanup_queue()) while there are still
2974 * references to the 'md'. The corresponding blk_put_queue() is in
2975 * mmc_blk_put().
2976 */
2977 if (!blk_get_queue(md->queue.queue)) {
2978 mmc_cleanup_queue(&md->queue);
2979 goto err_putdisk;
2980 }
2981
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002982 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002983 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002984 md->disk->fops = &mmc_bdops;
2985 md->disk->private_data = md;
2986 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002987 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002988 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002989 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002990 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002991 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002992
2993 /*
2994 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2995 *
2996 * - be set for removable media with permanent block devices
2997 * - be unset for removable block devices with permanent media
2998 *
2999 * Since MMC block devices clearly fall under the second
3000 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3001 * should use the block device creation/destruction hotplug
3002 * messages to tell when the card is present.
3003 */
3004
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003005 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003006 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003007
Saugata Dasa5075eb2012-05-17 16:32:21 +05303008 if (mmc_card_mmc(card))
3009 blk_queue_logical_block_size(md->queue.queue,
3010 card->ext_csd.data_sector_size);
3011 else
3012 blk_queue_logical_block_size(md->queue.queue, 512);
3013
Andrei Warkentin371a6892011-04-11 18:10:25 -05003014 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003015
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003016 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003017 if ((mmc_card_mmc(card) &&
3018 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003019 (mmc_card_sd(card) &&
3020 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3021 md->flags |= MMC_BLK_CMD23;
3022 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003023
3024 if (mmc_card_mmc(card) &&
3025 md->flags & MMC_BLK_CMD23 &&
3026 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3027 card->ext_csd.rel_sectors)) {
3028 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003029 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003030 }
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003033
3034 err_putdisk:
3035 put_disk(md->disk);
3036 err_kfree:
3037 kfree(md);
3038 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01003039 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00003040 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Andrei Warkentin371a6892011-04-11 18:10:25 -05003043static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3044{
3045 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003046
3047 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3048 /*
3049 * The EXT_CSD sector count is in number or 512 byte
3050 * sectors.
3051 */
3052 size = card->ext_csd.sectors;
3053 } else {
3054 /*
3055 * The CSD capacity field is in units of read_blkbits.
3056 * set_capacity takes units of 512 bytes.
3057 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003058 size = (typeof(sector_t))card->csd.capacity
3059 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003060 }
3061
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003062 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003063 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003064}
3065
3066static int mmc_blk_alloc_part(struct mmc_card *card,
3067 struct mmc_blk_data *md,
3068 unsigned int part_type,
3069 sector_t size,
3070 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003071 const char *subname,
3072 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003073{
3074 char cap_str[10];
3075 struct mmc_blk_data *part_md;
3076
3077 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003078 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003079 if (IS_ERR(part_md))
3080 return PTR_ERR(part_md);
3081 part_md->part_type = part_type;
3082 list_add(&part_md->part, &md->part);
3083
James Bottomleyb9f28d82015-03-05 18:47:01 -08003084 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003085 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303086 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003087 part_md->disk->disk_name, mmc_card_id(card),
3088 mmc_card_name(card), part_md->part_type, cap_str);
3089 return 0;
3090}
3091
Linus Walleij97548572017-09-20 10:02:00 +02003092/**
3093 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
3094 * @filp: the character device file
3095 * @cmd: the ioctl() command
3096 * @arg: the argument from userspace
3097 *
3098 * This will essentially just redirect the ioctl()s coming in over to
3099 * the main block device spawning the RPMB character device.
3100 */
3101static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
3102 unsigned long arg)
3103{
3104 struct mmc_rpmb_data *rpmb = filp->private_data;
3105 int ret;
3106
3107 switch (cmd) {
3108 case MMC_IOC_CMD:
3109 ret = mmc_blk_ioctl_cmd(rpmb->md,
3110 (struct mmc_ioc_cmd __user *)arg,
3111 rpmb);
3112 break;
3113 case MMC_IOC_MULTI_CMD:
3114 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
3115 (struct mmc_ioc_multi_cmd __user *)arg,
3116 rpmb);
3117 break;
3118 default:
3119 ret = -EINVAL;
3120 break;
3121 }
3122
3123 return 0;
3124}
3125
3126#ifdef CONFIG_COMPAT
3127static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
3128 unsigned long arg)
3129{
3130 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
3131}
3132#endif
3133
3134static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
3135{
3136 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
3137 struct mmc_rpmb_data, chrdev);
3138
3139 get_device(&rpmb->dev);
3140 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02003141 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02003142
3143 return nonseekable_open(inode, filp);
3144}
3145
3146static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
3147{
3148 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
3149 struct mmc_rpmb_data, chrdev);
3150
3151 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02003152 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02003153
3154 return 0;
3155}
3156
3157static const struct file_operations mmc_rpmb_fileops = {
3158 .release = mmc_rpmb_chrdev_release,
3159 .open = mmc_rpmb_chrdev_open,
3160 .owner = THIS_MODULE,
3161 .llseek = no_llseek,
3162 .unlocked_ioctl = mmc_rpmb_ioctl,
3163#ifdef CONFIG_COMPAT
3164 .compat_ioctl = mmc_rpmb_ioctl_compat,
3165#endif
3166};
3167
Linus Walleij1c87f732017-10-04 11:10:07 +02003168static void mmc_blk_rpmb_device_release(struct device *dev)
3169{
3170 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
3171
3172 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
3173 kfree(rpmb);
3174}
Linus Walleij97548572017-09-20 10:02:00 +02003175
3176static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
3177 struct mmc_blk_data *md,
3178 unsigned int part_index,
3179 sector_t size,
3180 const char *subname)
3181{
3182 int devidx, ret;
3183 char rpmb_name[DISK_NAME_LEN];
3184 char cap_str[10];
3185 struct mmc_rpmb_data *rpmb;
3186
3187 /* This creates the minor number for the RPMB char device */
3188 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
3189 if (devidx < 0)
3190 return devidx;
3191
3192 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02003193 if (!rpmb) {
3194 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02003195 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02003196 }
Linus Walleij97548572017-09-20 10:02:00 +02003197
3198 snprintf(rpmb_name, sizeof(rpmb_name),
3199 "mmcblk%u%s", card->host->index, subname ? subname : "");
3200
3201 rpmb->id = devidx;
3202 rpmb->part_index = part_index;
3203 rpmb->dev.init_name = rpmb_name;
3204 rpmb->dev.bus = &mmc_rpmb_bus_type;
3205 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
3206 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02003207 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02003208 device_initialize(&rpmb->dev);
3209 dev_set_drvdata(&rpmb->dev, rpmb);
3210 rpmb->md = md;
3211
3212 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
3213 rpmb->chrdev.owner = THIS_MODULE;
3214 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
3215 if (ret) {
3216 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02003217 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02003218 }
3219
3220 list_add(&rpmb->node, &md->rpmbs);
3221
3222 string_get_size((u64)size, 512, STRING_UNITS_2,
3223 cap_str, sizeof(cap_str));
3224
3225 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
3226 rpmb_name, mmc_card_id(card),
3227 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
3228 MAJOR(mmc_rpmb_devt), rpmb->id);
3229
3230 return 0;
3231
Linus Walleij1c87f732017-10-04 11:10:07 +02003232out_put_device:
3233 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02003234 return ret;
3235}
3236
3237static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02003238
Linus Walleij97548572017-09-20 10:02:00 +02003239{
3240 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02003241 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02003242}
3243
Namjae Jeone0c368d2011-10-06 23:41:38 +09003244/* MMC Physical partitions consist of two boot partitions and
3245 * up to four general purpose partitions.
3246 * For each partition enabled in EXT_CSD a block device will be allocatedi
3247 * to provide access to the partition.
3248 */
3249
Andrei Warkentin371a6892011-04-11 18:10:25 -05003250static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3251{
Linus Walleij97548572017-09-20 10:02:00 +02003252 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003253
3254 if (!mmc_card_mmc(card))
3255 return 0;
3256
Namjae Jeone0c368d2011-10-06 23:41:38 +09003257 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02003258 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
3259 /*
3260 * RPMB partitions does not provide block access, they
3261 * are only accessed using ioctl():s. Thus create
3262 * special RPMB block devices that do not have a
3263 * backing block queue for these.
3264 */
3265 ret = mmc_blk_alloc_rpmb_part(card, md,
3266 card->part[idx].part_cfg,
3267 card->part[idx].size >> 9,
3268 card->part[idx].name);
3269 if (ret)
3270 return ret;
3271 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09003272 ret = mmc_blk_alloc_part(card, md,
3273 card->part[idx].part_cfg,
3274 card->part[idx].size >> 9,
3275 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003276 card->part[idx].name,
3277 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003278 if (ret)
3279 return ret;
3280 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003281 }
3282
Linus Walleij97548572017-09-20 10:02:00 +02003283 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003284}
3285
Andrei Warkentin371a6892011-04-11 18:10:25 -05003286static void mmc_blk_remove_req(struct mmc_blk_data *md)
3287{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003288 struct mmc_card *card;
3289
Andrei Warkentin371a6892011-04-11 18:10:25 -05003290 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003291 /*
3292 * Flush remaining requests and free queues. It
3293 * is freeing the queue that stops new requests
3294 * from being accepted.
3295 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003296 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003297 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003298 if (md->disk->flags & GENHD_FL_UP) {
3299 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003300 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3301 card->ext_csd.boot_ro_lockable)
3302 device_remove_file(disk_to_dev(md->disk),
3303 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003304
Andrei Warkentin371a6892011-04-11 18:10:25 -05003305 del_gendisk(md->disk);
3306 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003307 mmc_blk_put(md);
3308 }
3309}
3310
3311static void mmc_blk_remove_parts(struct mmc_card *card,
3312 struct mmc_blk_data *md)
3313{
3314 struct list_head *pos, *q;
3315 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02003316 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003317
Linus Walleij97548572017-09-20 10:02:00 +02003318 /* Remove RPMB partitions */
3319 list_for_each_safe(pos, q, &md->rpmbs) {
3320 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
3321 list_del(pos);
3322 mmc_blk_remove_rpmb_part(rpmb);
3323 }
3324 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05003325 list_for_each_safe(pos, q, &md->part) {
3326 part_md = list_entry(pos, struct mmc_blk_data, part);
3327 list_del(pos);
3328 mmc_blk_remove_req(part_md);
3329 }
3330}
3331
3332static int mmc_add_disk(struct mmc_blk_data *md)
3333{
3334 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003335 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003336
Dan Williams307d8e62016-06-20 10:40:44 -07003337 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003338 md->force_ro.show = force_ro_show;
3339 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303340 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003341 md->force_ro.attr.name = "force_ro";
3342 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3343 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3344 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003345 goto force_ro_fail;
3346
3347 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3348 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003349 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003350
3351 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3352 mode = S_IRUGO;
3353 else
3354 mode = S_IRUGO | S_IWUSR;
3355
3356 md->power_ro_lock.show = power_ro_lock_show;
3357 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003358 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003359 md->power_ro_lock.attr.mode = mode;
3360 md->power_ro_lock.attr.name =
3361 "ro_lock_until_next_power_on";
3362 ret = device_create_file(disk_to_dev(md->disk),
3363 &md->power_ro_lock);
3364 if (ret)
3365 goto power_ro_lock_fail;
3366 }
3367 return ret;
3368
3369power_ro_lock_fail:
3370 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3371force_ro_fail:
3372 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003373
3374 return ret;
3375}
3376
Linus Walleij627c3cc2017-08-20 23:39:08 +02003377#ifdef CONFIG_DEBUG_FS
3378
3379static int mmc_dbg_card_status_get(void *data, u64 *val)
3380{
3381 struct mmc_card *card = data;
3382 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3383 struct mmc_queue *mq = &md->queue;
3384 struct request *req;
3385 int ret;
3386
3387 /* Ask the block layer about the card status */
3388 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02003389 if (IS_ERR(req))
3390 return PTR_ERR(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02003391 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
3392 blk_execute_rq(mq->queue, NULL, req, 0);
3393 ret = req_to_mmc_queue_req(req)->drv_op_result;
3394 if (ret >= 0) {
3395 *val = ret;
3396 ret = 0;
3397 }
Adrian Hunter34c089e2017-11-21 15:42:27 +02003398 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02003399
3400 return ret;
3401}
3402DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
3403 NULL, "%08llx\n");
3404
3405/* That is two digits * 512 + 1 for newline */
3406#define EXT_CSD_STR_LEN 1025
3407
3408static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
3409{
3410 struct mmc_card *card = inode->i_private;
3411 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3412 struct mmc_queue *mq = &md->queue;
3413 struct request *req;
3414 char *buf;
3415 ssize_t n = 0;
3416 u8 *ext_csd;
3417 int err, i;
3418
3419 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
3420 if (!buf)
3421 return -ENOMEM;
3422
3423 /* Ask the block layer for the EXT CSD */
3424 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02003425 if (IS_ERR(req)) {
3426 err = PTR_ERR(req);
3427 goto out_free;
3428 }
Linus Walleij627c3cc2017-08-20 23:39:08 +02003429 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
3430 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
3431 blk_execute_rq(mq->queue, NULL, req, 0);
3432 err = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +02003433 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02003434 if (err) {
3435 pr_err("FAILED %d\n", err);
3436 goto out_free;
3437 }
3438
3439 for (i = 0; i < 512; i++)
3440 n += sprintf(buf + n, "%02x", ext_csd[i]);
3441 n += sprintf(buf + n, "\n");
3442
3443 if (n != EXT_CSD_STR_LEN) {
3444 err = -EINVAL;
3445 goto out_free;
3446 }
3447
3448 filp->private_data = buf;
3449 kfree(ext_csd);
3450 return 0;
3451
3452out_free:
3453 kfree(buf);
3454 return err;
3455}
3456
3457static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
3458 size_t cnt, loff_t *ppos)
3459{
3460 char *buf = filp->private_data;
3461
3462 return simple_read_from_buffer(ubuf, cnt, ppos,
3463 buf, EXT_CSD_STR_LEN);
3464}
3465
3466static int mmc_ext_csd_release(struct inode *inode, struct file *file)
3467{
3468 kfree(file->private_data);
3469 return 0;
3470}
3471
3472static const struct file_operations mmc_dbg_ext_csd_fops = {
3473 .open = mmc_ext_csd_open,
3474 .read = mmc_ext_csd_read,
3475 .release = mmc_ext_csd_release,
3476 .llseek = default_llseek,
3477};
3478
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003479static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02003480{
3481 struct dentry *root;
3482
3483 if (!card->debugfs_root)
3484 return 0;
3485
3486 root = card->debugfs_root;
3487
3488 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003489 md->status_dentry =
3490 debugfs_create_file("status", S_IRUSR, root, card,
3491 &mmc_dbg_card_status_fops);
3492 if (!md->status_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02003493 return -EIO;
3494 }
3495
3496 if (mmc_card_mmc(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003497 md->ext_csd_dentry =
3498 debugfs_create_file("ext_csd", S_IRUSR, root, card,
3499 &mmc_dbg_ext_csd_fops);
3500 if (!md->ext_csd_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02003501 return -EIO;
3502 }
3503
3504 return 0;
3505}
3506
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003507static void mmc_blk_remove_debugfs(struct mmc_card *card,
3508 struct mmc_blk_data *md)
3509{
3510 if (!card->debugfs_root)
3511 return;
3512
3513 if (!IS_ERR_OR_NULL(md->status_dentry)) {
3514 debugfs_remove(md->status_dentry);
3515 md->status_dentry = NULL;
3516 }
3517
3518 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
3519 debugfs_remove(md->ext_csd_dentry);
3520 md->ext_csd_dentry = NULL;
3521 }
3522}
Linus Walleij627c3cc2017-08-20 23:39:08 +02003523
3524#else
3525
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003526static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02003527{
3528 return 0;
3529}
3530
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003531static void mmc_blk_remove_debugfs(struct mmc_card *card,
3532 struct mmc_blk_data *md)
3533{
3534}
3535
Linus Walleij627c3cc2017-08-20 23:39:08 +02003536#endif /* CONFIG_DEBUG_FS */
3537
Ulf Hansson96541ba2015-04-14 13:06:12 +02003538static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003540 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003541 char cap_str[10];
3542
Pierre Ossman912490d2005-05-21 10:27:02 +01003543 /*
3544 * Check that the card supports the command class(es) we need.
3545 */
3546 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 return -ENODEV;
3548
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08003549 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02003550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02003552 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return PTR_ERR(md);
3554
James Bottomleyb9f28d82015-03-05 18:47:01 -08003555 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003556 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303557 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003559 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Andrei Warkentin371a6892011-04-11 18:10:25 -05003561 if (mmc_blk_alloc_parts(card, md))
3562 goto out;
3563
Ulf Hansson96541ba2015-04-14 13:06:12 +02003564 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003565
Andrei Warkentin371a6892011-04-11 18:10:25 -05003566 if (mmc_add_disk(md))
3567 goto out;
3568
3569 list_for_each_entry(part_md, &md->part, part) {
3570 if (mmc_add_disk(part_md))
3571 goto out;
3572 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003573
Linus Walleij627c3cc2017-08-20 23:39:08 +02003574 /* Add two debugfs entries */
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003575 mmc_blk_add_debugfs(card, md);
Linus Walleij627c3cc2017-08-20 23:39:08 +02003576
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003577 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3578 pm_runtime_use_autosuspend(&card->dev);
3579
3580 /*
3581 * Don't enable runtime PM for SD-combo cards here. Leave that
3582 * decision to be taken during the SDIO init sequence instead.
3583 */
3584 if (card->type != MMC_TYPE_SD_COMBO) {
3585 pm_runtime_set_active(&card->dev);
3586 pm_runtime_enable(&card->dev);
3587 }
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return 0;
3590
3591 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003592 mmc_blk_remove_parts(card, md);
3593 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Ulf Hansson96541ba2015-04-14 13:06:12 +02003597static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003599 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Adrian Hunterf9f0da92017-11-21 15:42:30 +02003601 mmc_blk_remove_debugfs(card, md);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003602 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003603 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003604 mmc_claim_host(card->host);
Linus Walleij1f797ed2017-08-20 23:39:10 +02003605 mmc_blk_part_switch(card, md->part_type);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003606 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003607 if (card->type != MMC_TYPE_SD_COMBO)
3608 pm_runtime_disable(&card->dev);
3609 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003610 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003611 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
Ulf Hansson96541ba2015-04-14 13:06:12 +02003614static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003616 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003617 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 if (md) {
3620 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003621 list_for_each_entry(part_md, &md->part, part) {
3622 mmc_queue_suspend(&part_md->queue);
3623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625 return 0;
3626}
3627
Ulf Hansson96541ba2015-04-14 13:06:12 +02003628static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003629{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003630 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003631}
3632
Ulf Hansson0967edc2014-10-06 11:29:42 +02003633#ifdef CONFIG_PM_SLEEP
3634static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003635{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003636 struct mmc_card *card = mmc_dev_to_card(dev);
3637
3638 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003639}
3640
Ulf Hansson0967edc2014-10-06 11:29:42 +02003641static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003643 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003644 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003647 /*
3648 * Resume involves the card going into idle state,
3649 * so current partition is always the main one.
3650 */
3651 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003653 list_for_each_entry(part_md, &md->part, part) {
3654 mmc_queue_resume(&part_md->queue);
3655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657 return 0;
3658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659#endif
3660
Ulf Hansson0967edc2014-10-06 11:29:42 +02003661static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3662
Ulf Hansson96541ba2015-04-14 13:06:12 +02003663static struct mmc_driver mmc_driver = {
3664 .drv = {
3665 .name = "mmcblk",
3666 .pm = &mmc_blk_pm_ops,
3667 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 .probe = mmc_blk_probe,
3669 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003670 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671};
3672
3673static int __init mmc_blk_init(void)
3674{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003675 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Linus Walleij97548572017-09-20 10:02:00 +02003677 res = bus_register(&mmc_rpmb_bus_type);
3678 if (res < 0) {
3679 pr_err("mmcblk: could not register RPMB bus type\n");
3680 return res;
3681 }
3682 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3683 if (res < 0) {
3684 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3685 goto out_bus_unreg;
3686 }
3687
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003688 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3689 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3690
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003691 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003692
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003693 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3694 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02003695 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003697 res = mmc_register_driver(&mmc_driver);
3698 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02003699 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003701 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02003702
3703out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003704 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02003705out_chrdev_unreg:
3706 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3707out_bus_unreg:
3708 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return res;
3710}
3711
3712static void __exit mmc_blk_exit(void)
3713{
3714 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003715 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02003716 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
3719module_init(mmc_blk_init);
3720module_exit(mmc_blk_exit);
3721
3722MODULE_LICENSE("GPL");
3723MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3724