blob: ccfa98af1dd3fc93fdaa52b6ced6db3dc0f9db79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Linus Walleij97548572017-09-20 10:02:00 +020031#include <linux/cdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000032#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070033#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020039#include <linux/idr.h>
Linus Walleij627c3cc2017-08-20 23:39:08 +020040#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020044#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010045#include <linux/mmc/mmc.h>
46#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Pierre Ossman98ac2162006-12-23 20:03:02 +010050#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080051#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010052#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010054#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010055#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010056#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080057#include "quirks.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010058#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000060MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040061#ifdef MODULE_PARAM_PREFIX
62#undef MODULE_PARAM_PREFIX
63#endif
64#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010065
Trey Ramsay8fee4762012-11-16 09:31:41 -060066#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050069
Luca Porziod3df0462015-11-06 15:12:26 +000070#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090071 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
Linus Walleij97548572017-09-20 10:02:00 +020090static DEFINE_IDA(mmc_rpmb_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Walleij97548572017-09-20 10:02:00 +0200101 struct list_head rpmbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500103 unsigned int flags;
104#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
105#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000108 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200118 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
124 int area_type;
Adrian Hunterf9f0da92017-11-21 15:42:30 +0200125
126 /* debugfs files (only in main mmc_blk_data) */
127 struct dentry *status_dentry;
128 struct dentry *ext_csd_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Linus Walleij97548572017-09-20 10:02:00 +0200131/* Device type for RPMB character devices */
132static dev_t mmc_rpmb_devt;
133
134/* Bus type for RPMB character devices */
135static struct bus_type mmc_rpmb_bus_type = {
136 .name = "mmc_rpmb",
137};
138
139/**
140 * struct mmc_rpmb_data - special RPMB device type for these areas
141 * @dev: the device for the RPMB area
142 * @chrdev: character device for the RPMB area
143 * @id: unique device ID number
144 * @part_index: partition index (0 on first)
145 * @md: parent MMC block device
146 * @node: list item, so we can put this device on a list
147 */
148struct mmc_rpmb_data {
149 struct device dev;
150 struct cdev chrdev;
151 int id;
152 unsigned int part_index;
153 struct mmc_blk_data *md;
154 struct list_head node;
155};
156
Arjan van de Vena621aae2006-01-12 18:43:35 +0000157static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400159module_param(perdev_minors, int, 0444);
160MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
161
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200162static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200163 unsigned int part_type);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
166{
167 struct mmc_blk_data *md;
168
Arjan van de Vena621aae2006-01-12 18:43:35 +0000169 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 md = disk->private_data;
171 if (md && md->usage == 0)
172 md = NULL;
173 if (md)
174 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000175 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 return md;
178}
179
Andrei Warkentin371a6892011-04-11 18:10:25 -0500180static inline int mmc_get_devidx(struct gendisk *disk)
181{
Colin Cross382c55f2015-10-22 10:00:41 -0700182 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500183 return devidx;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static void mmc_blk_put(struct mmc_blk_data *md)
187{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000188 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 md->usage--;
190 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500191 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800192 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100193 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(md);
196 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000197 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Johan Rudholmadd710e2011-12-02 08:51:06 +0100200static ssize_t power_ro_lock_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203 int ret;
204 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
205 struct mmc_card *card = md->queue.card;
206 int locked = 0;
207
208 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
209 locked = 2;
210 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
211 locked = 1;
212
213 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
214
Tomas Winkler9098f842015-07-16 15:50:45 +0200215 mmc_blk_put(md);
216
Johan Rudholmadd710e2011-12-02 08:51:06 +0100217 return ret;
218}
219
220static ssize_t power_ro_lock_store(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t count)
222{
223 int ret;
224 struct mmc_blk_data *md, *part_md;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200225 struct mmc_queue *mq;
226 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100227 unsigned long set;
228
229 if (kstrtoul(buf, 0, &set))
230 return -EINVAL;
231
232 if (set != 1)
233 return count;
234
235 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200236 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100237
Linus Walleij0493f6f2017-05-19 15:37:30 +0200238 /* Dispatch locking to the block layer */
239 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200240 if (IS_ERR(req)) {
241 count = PTR_ERR(req);
242 goto out_put;
243 }
Linus Walleij0493f6f2017-05-19 15:37:30 +0200244 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
245 blk_execute_rq(mq->queue, NULL, req, 0);
246 ret = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +0200247 blk_put_request(req);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100248
249 if (!ret) {
250 pr_info("%s: Locking boot partition ro until next power on\n",
251 md->disk->disk_name);
252 set_disk_ro(md->disk, 1);
253
254 list_for_each_entry(part_md, &md->part, part)
255 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
256 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
257 set_disk_ro(part_md->disk, 1);
258 }
259 }
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200260out_put:
Johan Rudholmadd710e2011-12-02 08:51:06 +0100261 mmc_blk_put(md);
262 return count;
263}
264
Andrei Warkentin371a6892011-04-11 18:10:25 -0500265static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
266 char *buf)
267{
268 int ret;
269 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
270
Baruch Siach0031a982014-09-22 10:12:51 +0300271 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500272 get_disk_ro(dev_to_disk(dev)) ^
273 md->read_only);
274 mmc_blk_put(md);
275 return ret;
276}
277
278static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
280{
281 int ret;
282 char *end;
283 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284 unsigned long set = simple_strtoul(buf, &end, 0);
285 if (end == buf) {
286 ret = -EINVAL;
287 goto out;
288 }
289
290 set_disk_ro(dev_to_disk(dev), set || md->read_only);
291 ret = count;
292out:
293 mmc_blk_put(md);
294 return ret;
295}
296
Al Viroa5a15612008-03-02 10:33:30 -0500297static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Al Viroa5a15612008-03-02 10:33:30 -0500299 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int ret = -ENXIO;
301
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200302 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (md) {
304 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500305 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700307
Al Viroa5a15612008-03-02 10:33:30 -0500308 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700309 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700310 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200313 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return ret;
316}
317
Al Virodb2a1442013-05-05 21:52:57 -0400318static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Al Viroa5a15612008-03-02 10:33:30 -0500320 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200322 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200324 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800328mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800330 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
331 geo->heads = 4;
332 geo->sectors = 16;
333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
John Calixtocb87ea22011-04-26 18:56:29 -0400336struct mmc_blk_ioc_data {
337 struct mmc_ioc_cmd ic;
338 unsigned char *buf;
339 u64 buf_bytes;
Linus Walleij97548572017-09-20 10:02:00 +0200340 struct mmc_rpmb_data *rpmb;
John Calixtocb87ea22011-04-26 18:56:29 -0400341};
342
343static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
344 struct mmc_ioc_cmd __user *user)
345{
346 struct mmc_blk_ioc_data *idata;
347 int err;
348
yalin wang1ff89502015-11-12 19:27:11 +0800349 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400350 if (!idata) {
351 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400352 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400353 }
354
355 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
356 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400357 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400358 }
359
360 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
361 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
362 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400364 }
365
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300366 if (!idata->buf_bytes) {
367 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100368 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300369 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100370
yalin wang1ff89502015-11-12 19:27:11 +0800371 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400372 if (!idata->buf) {
373 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400374 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400375 }
376
377 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
378 idata->ic.data_ptr, idata->buf_bytes)) {
379 err = -EFAULT;
380 goto copy_err;
381 }
382
383 return idata;
384
385copy_err:
386 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400387idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400388 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400389out:
John Calixtocb87ea22011-04-26 18:56:29 -0400390 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400391}
392
Jon Huntera5f57742015-09-22 10:27:53 +0100393static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
394 struct mmc_blk_ioc_data *idata)
395{
396 struct mmc_ioc_cmd *ic = &idata->ic;
397
398 if (copy_to_user(&(ic_ptr->response), ic->response,
399 sizeof(ic->response)))
400 return -EFAULT;
401
402 if (!idata->ic.write_flag) {
403 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
404 idata->buf, idata->buf_bytes))
405 return -EFAULT;
406 }
407
408 return 0;
409}
410
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200411static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
412 u32 retries_max)
413{
414 int err;
415 u32 retry_count = 0;
416
417 if (!status || !retries_max)
418 return -EINVAL;
419
420 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200421 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200422 if (err)
423 break;
424
425 if (!R1_STATUS(*status) &&
426 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
427 break; /* RPMB programming operation complete */
428
429 /*
430 * Rechedule to give the MMC device a chance to continue
431 * processing the previous command without being polled too
432 * frequently.
433 */
434 usleep_range(1000, 5000);
435 } while (++retry_count < retries_max);
436
437 if (retry_count == retries_max)
438 err = -EPERM;
439
440 return err;
441}
442
Maya Erez775a9362013-04-18 15:41:55 +0300443static int ioctl_do_sanitize(struct mmc_card *card)
444{
445 int err;
446
Ulf Hanssona2d10862013-12-16 14:37:26 +0100447 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300448 pr_warn("%s: %s - SANITIZE is not supported\n",
449 mmc_hostname(card->host), __func__);
450 err = -EOPNOTSUPP;
451 goto out;
452 }
453
454 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
455 mmc_hostname(card->host), __func__);
456
457 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
458 EXT_CSD_SANITIZE_START, 1,
459 MMC_SANITIZE_REQ_TIMEOUT);
460
461 if (err)
462 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
463 mmc_hostname(card->host), __func__, err);
464
465 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
466 __func__);
467out:
468 return err;
469}
470
Jon Huntera5f57742015-09-22 10:27:53 +0100471static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
472 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400473{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900474 struct mmc_command cmd = {};
475 struct mmc_data data = {};
476 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400477 struct scatterlist sg;
478 int err;
Linus Walleij97548572017-09-20 10:02:00 +0200479 unsigned int target_part;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200480 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400481
Jon Huntera5f57742015-09-22 10:27:53 +0100482 if (!card || !md || !idata)
483 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400484
Linus Walleij97548572017-09-20 10:02:00 +0200485 /*
486 * The RPMB accesses comes in from the character device, so we
487 * need to target these explicitly. Else we just target the
488 * partition type for the block device the ioctl() was issued
489 * on.
490 */
491 if (idata->rpmb) {
492 /* Support multiple RPMB partitions */
493 target_part = idata->rpmb->part_index;
494 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
495 } else {
496 target_part = md->part_type;
497 }
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200498
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100499 cmd.opcode = idata->ic.opcode;
500 cmd.arg = idata->ic.arg;
501 cmd.flags = idata->ic.flags;
502
503 if (idata->buf_bytes) {
504 data.sg = &sg;
505 data.sg_len = 1;
506 data.blksz = idata->ic.blksz;
507 data.blocks = idata->ic.blocks;
508
509 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
510
511 if (idata->ic.write_flag)
512 data.flags = MMC_DATA_WRITE;
513 else
514 data.flags = MMC_DATA_READ;
515
516 /* data.flags must already be set before doing this. */
517 mmc_set_data_timeout(&data, card);
518
519 /* Allow overriding the timeout_ns for empirical tuning. */
520 if (idata->ic.data_timeout_ns)
521 data.timeout_ns = idata->ic.data_timeout_ns;
522
523 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
524 /*
525 * Pretend this is a data transfer and rely on the
526 * host driver to compute timeout. When all host
527 * drivers support cmd.cmd_timeout for R1B, this
528 * can be changed to:
529 *
530 * mrq.data = NULL;
531 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
532 */
533 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
534 }
535
536 mrq.data = &data;
537 }
538
539 mrq.cmd = &cmd;
540
Linus Walleij97548572017-09-20 10:02:00 +0200541 err = mmc_blk_part_switch(card, target_part);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200542 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100543 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200544
John Calixtocb87ea22011-04-26 18:56:29 -0400545 if (idata->ic.is_acmd) {
546 err = mmc_app_cmd(card->host, card);
547 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100548 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400549 }
550
Linus Walleij97548572017-09-20 10:02:00 +0200551 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200552 err = mmc_set_blockcount(card, data.blocks,
553 idata->ic.write_flag & (1 << 31));
554 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100555 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200556 }
557
Yaniv Gardia82e4842013-06-05 14:13:08 +0300558 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
559 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300560 err = ioctl_do_sanitize(card);
561
562 if (err)
563 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
564 __func__, err);
565
Jon Huntera5f57742015-09-22 10:27:53 +0100566 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300567 }
568
John Calixtocb87ea22011-04-26 18:56:29 -0400569 mmc_wait_for_req(card->host, &mrq);
570
571 if (cmd.error) {
572 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
573 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100574 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400575 }
576 if (data.error) {
577 dev_err(mmc_dev(card->host), "%s: data error %d\n",
578 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100579 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400580 }
581
582 /*
583 * According to the SD specs, some commands require a delay after
584 * issuing the command.
585 */
586 if (idata->ic.postsleep_min_us)
587 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
588
Jon Huntera5f57742015-09-22 10:27:53 +0100589 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400590
Linus Walleij97548572017-09-20 10:02:00 +0200591 if (idata->rpmb) {
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200592 /*
593 * Ensure RPMB command has completed by polling CMD13
594 * "Send Status".
595 */
596 err = ioctl_rpmb_card_status_poll(card, &status, 5);
597 if (err)
598 dev_err(mmc_dev(card->host),
599 "%s: Card Status=0x%08X, error %d\n",
600 __func__, status, err);
601 }
602
Jon Huntera5f57742015-09-22 10:27:53 +0100603 return err;
604}
605
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200606static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200607 struct mmc_ioc_cmd __user *ic_ptr,
608 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100609{
610 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200611 struct mmc_blk_ioc_data *idatas[1];
Linus Walleij614f0382017-05-18 11:29:34 +0200612 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100613 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700614 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200615 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100616
617 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
618 if (IS_ERR(idata))
619 return PTR_ERR(idata);
Linus Walleij97548572017-09-20 10:02:00 +0200620 /* This will be NULL on non-RPMB ioctl():s */
621 idata->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100622
Jon Huntera5f57742015-09-22 10:27:53 +0100623 card = md->queue.card;
624 if (IS_ERR(card)) {
625 err = PTR_ERR(card);
626 goto cmd_done;
627 }
628
Linus Walleij614f0382017-05-18 11:29:34 +0200629 /*
630 * Dispatch the ioctl() into the block request queue.
631 */
632 mq = &md->queue;
633 req = blk_get_request(mq->queue,
634 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
635 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200636 if (IS_ERR(req)) {
637 err = PTR_ERR(req);
638 goto cmd_done;
639 }
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200640 idatas[0] = idata;
Linus Walleij97548572017-09-20 10:02:00 +0200641 req_to_mmc_queue_req(req)->drv_op =
642 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200643 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200644 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200645 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200646 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700647 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200648 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100649
John Calixtocb87ea22011-04-26 18:56:29 -0400650cmd_done:
John Calixtocb87ea22011-04-26 18:56:29 -0400651 kfree(idata->buf);
652 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700653 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400654}
655
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200656static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
Linus Walleij97548572017-09-20 10:02:00 +0200657 struct mmc_ioc_multi_cmd __user *user,
658 struct mmc_rpmb_data *rpmb)
Jon Huntera5f57742015-09-22 10:27:53 +0100659{
660 struct mmc_blk_ioc_data **idata = NULL;
661 struct mmc_ioc_cmd __user *cmds = user->cmds;
662 struct mmc_card *card;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200663 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700664 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100665 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200666 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100667
668 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
669 sizeof(num_of_cmds)))
670 return -EFAULT;
671
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200672 if (!num_of_cmds)
673 return 0;
674
Jon Huntera5f57742015-09-22 10:27:53 +0100675 if (num_of_cmds > MMC_IOC_MAX_CMDS)
676 return -EINVAL;
677
678 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
679 if (!idata)
680 return -ENOMEM;
681
682 for (i = 0; i < num_of_cmds; i++) {
683 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
684 if (IS_ERR(idata[i])) {
685 err = PTR_ERR(idata[i]);
686 num_of_cmds = i;
687 goto cmd_err;
688 }
Linus Walleij97548572017-09-20 10:02:00 +0200689 /* This will be NULL on non-RPMB ioctl():s */
690 idata[i]->rpmb = rpmb;
Jon Huntera5f57742015-09-22 10:27:53 +0100691 }
692
Jon Huntera5f57742015-09-22 10:27:53 +0100693 card = md->queue.card;
694 if (IS_ERR(card)) {
695 err = PTR_ERR(card);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200696 goto cmd_err;
Jon Huntera5f57742015-09-22 10:27:53 +0100697 }
698
Jon Huntera5f57742015-09-22 10:27:53 +0100699
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200700 /*
701 * Dispatch the ioctl()s into the block request queue.
702 */
703 mq = &md->queue;
704 req = blk_get_request(mq->queue,
705 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
706 __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +0200707 if (IS_ERR(req)) {
708 err = PTR_ERR(req);
709 goto cmd_err;
710 }
Linus Walleij97548572017-09-20 10:02:00 +0200711 req_to_mmc_queue_req(req)->drv_op =
712 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200713 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200714 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
715 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200716 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100717
718 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700719 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100720 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100721
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200722 blk_put_request(req);
723
Jon Huntera5f57742015-09-22 10:27:53 +0100724cmd_err:
725 for (i = 0; i < num_of_cmds; i++) {
726 kfree(idata[i]->buf);
727 kfree(idata[i]);
728 }
729 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700730 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100731}
732
Linus Walleij61fe0e22017-08-20 23:39:09 +0200733static int mmc_blk_check_blkdev(struct block_device *bdev)
734{
735 /*
736 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
737 * whole block device, not on a partition. This prevents overspray
738 * between sibling partitions.
739 */
740 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
741 return -EPERM;
742 return 0;
743}
744
John Calixtocb87ea22011-04-26 18:56:29 -0400745static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
746 unsigned int cmd, unsigned long arg)
747{
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200748 struct mmc_blk_data *md;
Linus Walleij61fe0e22017-08-20 23:39:09 +0200749 int ret;
750
Jon Huntera5f57742015-09-22 10:27:53 +0100751 switch (cmd) {
752 case MMC_IOC_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200753 ret = mmc_blk_check_blkdev(bdev);
754 if (ret)
755 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200756 md = mmc_blk_get(bdev->bd_disk);
757 if (!md)
758 return -EINVAL;
759 ret = mmc_blk_ioctl_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200760 (struct mmc_ioc_cmd __user *)arg,
761 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200762 mmc_blk_put(md);
763 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100764 case MMC_IOC_MULTI_CMD:
Linus Walleij61fe0e22017-08-20 23:39:09 +0200765 ret = mmc_blk_check_blkdev(bdev);
766 if (ret)
767 return ret;
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200768 md = mmc_blk_get(bdev->bd_disk);
769 if (!md)
770 return -EINVAL;
771 ret = mmc_blk_ioctl_multi_cmd(md,
Linus Walleij97548572017-09-20 10:02:00 +0200772 (struct mmc_ioc_multi_cmd __user *)arg,
773 NULL);
Linus Walleij2fe20ba2017-08-20 23:39:11 +0200774 mmc_blk_put(md);
775 return ret;
Jon Huntera5f57742015-09-22 10:27:53 +0100776 default:
777 return -EINVAL;
778 }
John Calixtocb87ea22011-04-26 18:56:29 -0400779}
780
781#ifdef CONFIG_COMPAT
782static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
783 unsigned int cmd, unsigned long arg)
784{
785 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
786}
787#endif
788
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700789static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500790 .open = mmc_blk_open,
791 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800792 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400794 .ioctl = mmc_blk_ioctl,
795#ifdef CONFIG_COMPAT
796 .compat_ioctl = mmc_blk_compat_ioctl,
797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
Adrian Hunter025e3d52017-03-13 14:36:39 +0200800static int mmc_blk_part_switch_pre(struct mmc_card *card,
801 unsigned int part_type)
802{
803 int ret = 0;
804
805 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
806 if (card->ext_csd.cmdq_en) {
807 ret = mmc_cmdq_disable(card);
808 if (ret)
809 return ret;
810 }
811 mmc_retune_pause(card->host);
812 }
813
814 return ret;
815}
816
817static int mmc_blk_part_switch_post(struct mmc_card *card,
818 unsigned int part_type)
819{
820 int ret = 0;
821
822 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
823 mmc_retune_unpause(card->host);
824 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
825 ret = mmc_cmdq_enable(card);
826 }
827
828 return ret;
829}
830
Andrei Warkentin371a6892011-04-11 18:10:25 -0500831static inline int mmc_blk_part_switch(struct mmc_card *card,
Linus Walleij1f797ed2017-08-20 23:39:10 +0200832 unsigned int part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500833{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200834 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200835 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300836
Linus Walleij1f797ed2017-08-20 23:39:10 +0200837 if (main_md->part_curr == part_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500838 return 0;
839
840 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300841 u8 part_config = card->ext_csd.part_config;
842
Linus Walleij1f797ed2017-08-20 23:39:10 +0200843 ret = mmc_blk_part_switch_pre(card, part_type);
Adrian Hunter025e3d52017-03-13 14:36:39 +0200844 if (ret)
845 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300846
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300847 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
Linus Walleij1f797ed2017-08-20 23:39:10 +0200848 part_config |= part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500849
850 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300851 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500852 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300853 if (ret) {
Linus Walleij1f797ed2017-08-20 23:39:10 +0200854 mmc_blk_part_switch_post(card, part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500855 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300856 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300857
858 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300859
Adrian Hunter025e3d52017-03-13 14:36:39 +0200860 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300861 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500862
Linus Walleij1f797ed2017-08-20 23:39:10 +0200863 main_md->part_curr = part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200864 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500865}
866
Linus Walleij169f03a2017-02-01 13:47:57 +0100867static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700868{
869 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100870 u32 result;
871 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700872
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900873 struct mmc_request mrq = {};
874 struct mmc_command cmd = {};
875 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700876
877 struct scatterlist sg;
878
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700879 cmd.opcode = MMC_APP_CMD;
880 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700881 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700882
883 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700884 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100885 return err;
David Brownell7213d172007-08-08 09:10:23 -0700886 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100887 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700888
889 memset(&cmd, 0, sizeof(struct mmc_command));
890
891 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
892 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700893 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700894
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700895 data.blksz = 4;
896 data.blocks = 1;
897 data.flags = MMC_DATA_READ;
898 data.sg = &sg;
899 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530900 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700901
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700902 mrq.cmd = &cmd;
903 mrq.data = &data;
904
Ben Dooks051913d2009-06-08 23:33:57 +0100905 blocks = kmalloc(4, GFP_KERNEL);
906 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100907 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100908
909 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700910
911 mmc_wait_for_req(card->host, &mrq);
912
Ben Dooks051913d2009-06-08 23:33:57 +0100913 result = ntohl(*blocks);
914 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700915
Ben Dooks051913d2009-06-08 23:33:57 +0100916 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100917 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100918
Linus Walleij169f03a2017-02-01 13:47:57 +0100919 *written_blocks = result;
920
921 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700922}
923
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100924static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100925 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100926{
927 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
928 int err = 0;
929 u32 status;
930
931 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200932 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100933 if (err) {
934 pr_err("%s: error %d requesting status\n",
935 req->rq_disk->disk_name, err);
936 return err;
937 }
938
939 if (status & R1_ERROR) {
940 pr_err("%s: %s: error sending status cmd, status %#x\n",
941 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100942 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100943 }
944
Ulf Hansson95a91292014-01-29 13:11:27 +0100945 /* We may rely on the host hw to handle busy detection.*/
946 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
947 hw_busy_detect)
948 break;
949
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100950 /*
951 * Timeout if the device never becomes ready for data and never
952 * leaves the program state.
953 */
954 if (time_after(jiffies, timeout)) {
955 pr_err("%s: Card stuck in programming state! %s %s\n",
956 mmc_hostname(card->host),
957 req->rq_disk->disk_name, __func__);
958 return -ETIMEDOUT;
959 }
960
961 /*
962 * Some cards mishandle the status bits,
963 * so make sure to check both the busy
964 * indication and the card state.
965 */
966 } while (!(status & R1_READY_FOR_DATA) ||
967 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
968
969 return err;
970}
971
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100972static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100973 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100974{
975 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900976 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100977 int err;
978 bool use_r1b_resp = rq_data_dir(req) == WRITE;
979
980 /*
981 * Normally we use R1B responses for WRITE, but in cases where the host
982 * has specified a max_busy_timeout we need to validate it. A failure
983 * means we need to prevent the host from doing hw busy detection, which
984 * is done by converting to a R1 response instead.
985 */
986 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
987 use_r1b_resp = false;
988
989 cmd.opcode = MMC_STOP_TRANSMISSION;
990 if (use_r1b_resp) {
991 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
992 cmd.busy_timeout = timeout_ms;
993 } else {
994 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
995 }
996
997 err = mmc_wait_for_cmd(host, &cmd, 5);
998 if (err)
999 return err;
1000
1001 *stop_status = cmd.resp[0];
1002
1003 /* No need to check card status in case of READ. */
1004 if (rq_data_dir(req) == READ)
1005 return 0;
1006
1007 if (!mmc_host_is_spi(host) &&
1008 (*stop_status & R1_ERROR)) {
1009 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1010 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001011 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001012 }
1013
1014 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1015}
1016
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301017#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001018#define ERR_RETRY 2
1019#define ERR_ABORT 1
1020#define ERR_CONTINUE 0
1021
1022static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1023 bool status_valid, u32 status)
1024{
1025 switch (error) {
1026 case -EILSEQ:
1027 /* response crc error, retry the r/w cmd */
1028 pr_err("%s: %s sending %s command, card status %#x\n",
1029 req->rq_disk->disk_name, "response CRC error",
1030 name, status);
1031 return ERR_RETRY;
1032
1033 case -ETIMEDOUT:
1034 pr_err("%s: %s sending %s command, card status %#x\n",
1035 req->rq_disk->disk_name, "timed out", name, status);
1036
1037 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301038 if (!status_valid) {
1039 pr_err("%s: status not valid, retrying timeout\n",
1040 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001041 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301042 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001043
1044 /*
1045 * If it was a r/w cmd crc error, or illegal command
1046 * (eg, issued in wrong state) then retry - we should
1047 * have corrected the state problem above.
1048 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301049 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1050 pr_err("%s: command error, retrying timeout\n",
1051 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001052 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301053 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001054
1055 /* Otherwise abort the command */
1056 return ERR_ABORT;
1057
1058 default:
1059 /* We don't understand the error code the driver gave us */
1060 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1061 req->rq_disk->disk_name, error, status);
1062 return ERR_ABORT;
1063 }
1064}
1065
1066/*
1067 * Initial r/w and stop cmd error recovery.
1068 * We don't know whether the card received the r/w cmd or not, so try to
1069 * restore things back to a sane state. Essentially, we do this as follows:
1070 * - Obtain card status. If the first attempt to obtain card status fails,
1071 * the status word will reflect the failed status cmd, not the failed
1072 * r/w cmd. If we fail to obtain card status, it suggests we can no
1073 * longer communicate with the card.
1074 * - Check the card state. If the card received the cmd but there was a
1075 * transient problem with the response, it might still be in a data transfer
1076 * mode. Try to send it a stop command. If this fails, we can't recover.
1077 * - If the r/w cmd failed due to a response CRC error, it was probably
1078 * transient, so retry the cmd.
1079 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1080 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1081 * illegal cmd, retry.
1082 * Otherwise we don't understand what happened, so abort.
1083 */
1084static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001085 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001086{
1087 bool prev_cmd_status_valid = true;
1088 u32 status, stop_status = 0;
1089 int err, retry;
1090
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301091 if (mmc_card_removed(card))
1092 return ERR_NOMEDIUM;
1093
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001094 /*
1095 * Try to get card status which indicates both the card state
1096 * and why there was no response. If the first attempt fails,
1097 * we can't be sure the returned status is for the r/w command.
1098 */
1099 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001100 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001101 if (!err)
1102 break;
1103
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001104 /* Re-tune if needed */
1105 mmc_retune_recheck(card->host);
1106
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001107 prev_cmd_status_valid = false;
1108 pr_err("%s: error %d sending status command, %sing\n",
1109 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1110 }
1111
1112 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301113 if (err) {
1114 /* Check if the card is removed */
1115 if (mmc_detect_card_removed(card->host))
1116 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001117 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301118 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001119
Adrian Hunter67716322011-08-29 16:42:15 +03001120 /* Flag ECC errors */
1121 if ((status & R1_CARD_ECC_FAILED) ||
1122 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1123 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001124 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001125
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001126 /* Flag General errors */
1127 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1128 if ((status & R1_ERROR) ||
1129 (brq->stop.resp[0] & R1_ERROR)) {
1130 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1131 req->rq_disk->disk_name, __func__,
1132 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001133 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001134 }
1135
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001136 /*
1137 * Check the current card state. If it is in some data transfer
1138 * mode, tell it to stop (and hopefully transition back to TRAN.)
1139 */
1140 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1141 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001142 err = send_stop(card,
1143 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1144 req, gen_err, &stop_status);
1145 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001146 pr_err("%s: error %d sending stop command\n",
1147 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001148 /*
1149 * If the stop cmd also timed out, the card is probably
1150 * not present, so abort. Other errors are bad news too.
1151 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001152 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001153 }
1154
Adrian Hunter67716322011-08-29 16:42:15 +03001155 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001156 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001157 }
1158
1159 /* Check for set block count errors */
1160 if (brq->sbc.error)
1161 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1162 prev_cmd_status_valid, status);
1163
1164 /* Check for r/w command errors */
1165 if (brq->cmd.error)
1166 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1167 prev_cmd_status_valid, status);
1168
Adrian Hunter67716322011-08-29 16:42:15 +03001169 /* Data errors */
1170 if (!brq->stop.error)
1171 return ERR_CONTINUE;
1172
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001173 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001174 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 +01001175 req->rq_disk->disk_name, brq->stop.error,
1176 brq->cmd.resp[0], status);
1177
1178 /*
1179 * Subsitute in our own stop status as this will give the error
1180 * state which happened during the execution of the r/w command.
1181 */
1182 if (stop_status) {
1183 brq->stop.resp[0] = stop_status;
1184 brq->stop.error = 0;
1185 }
1186 return ERR_CONTINUE;
1187}
1188
Adrian Hunter67716322011-08-29 16:42:15 +03001189static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1190 int type)
1191{
1192 int err;
1193
1194 if (md->reset_done & type)
1195 return -EEXIST;
1196
1197 md->reset_done |= type;
1198 err = mmc_hw_reset(host);
1199 /* Ensure we switch back to the correct partition */
1200 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001201 struct mmc_blk_data *main_md =
1202 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001203 int part_err;
1204
1205 main_md->part_curr = main_md->part_type;
Linus Walleij1f797ed2017-08-20 23:39:10 +02001206 part_err = mmc_blk_part_switch(host->card, md->part_type);
Adrian Hunter67716322011-08-29 16:42:15 +03001207 if (part_err) {
1208 /*
1209 * We have failed to get back into the correct
1210 * partition, so we need to abort the whole request.
1211 */
1212 return -ENODEV;
1213 }
1214 }
1215 return err;
1216}
1217
1218static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1219{
1220 md->reset_done &= ~type;
1221}
1222
Linus Walleij5ec12392017-05-19 15:37:29 +02001223/*
1224 * The non-block commands come back from the block layer after it queued it and
1225 * processed it with all other requests and then they get issued in this
1226 * function.
1227 */
1228static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1229{
1230 struct mmc_queue_req *mq_rq;
1231 struct mmc_card *card = mq->card;
1232 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001233 struct mmc_blk_ioc_data **idata;
Linus Walleij97548572017-09-20 10:02:00 +02001234 bool rpmb_ioctl;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001235 u8 **ext_csd;
1236 u32 status;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001237 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001238 int i;
1239
1240 mq_rq = req_to_mmc_queue_req(req);
Linus Walleij97548572017-09-20 10:02:00 +02001241 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
Linus Walleij5ec12392017-05-19 15:37:29 +02001242
1243 switch (mq_rq->drv_op) {
1244 case MMC_DRV_OP_IOCTL:
Linus Walleij97548572017-09-20 10:02:00 +02001245 case MMC_DRV_OP_IOCTL_RPMB:
Linus Walleij69f75992017-08-20 23:39:06 +02001246 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001247 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001248 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001249 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001250 break;
1251 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001252 /* Always switch back to main area after RPMB access */
Linus Walleij97548572017-09-20 10:02:00 +02001253 if (rpmb_ioctl)
1254 mmc_blk_part_switch(card, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001255 break;
1256 case MMC_DRV_OP_BOOT_WP:
1257 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1258 card->ext_csd.boot_ro_lock |
1259 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1260 card->ext_csd.part_time);
1261 if (ret)
1262 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1263 md->disk->disk_name, ret);
1264 else
1265 card->ext_csd.boot_ro_lock |=
1266 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001267 break;
Linus Walleij627c3cc2017-08-20 23:39:08 +02001268 case MMC_DRV_OP_GET_CARD_STATUS:
1269 ret = mmc_send_status(card, &status);
1270 if (!ret)
1271 ret = status;
1272 break;
1273 case MMC_DRV_OP_GET_EXT_CSD:
1274 ext_csd = mq_rq->drv_op_data;
1275 ret = mmc_get_ext_csd(card, ext_csd);
1276 break;
Linus Walleij5ec12392017-05-19 15:37:29 +02001277 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001278 pr_err("%s: unknown driver specific operation\n",
1279 md->disk->disk_name);
1280 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001281 break;
1282 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001283 mq_rq->drv_op_result = ret;
Adrian Huntera7c17d82017-08-25 15:43:45 +03001284 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Linus Walleij5ec12392017-05-19 15:37:29 +02001285}
1286
Linus Walleijdf061582017-01-24 11:17:57 +01001287static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001288{
Linus Walleij7db30282016-11-18 13:36:15 +01001289 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001290 struct mmc_card *card = md->queue.card;
1291 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001292 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001293 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001294
Adrian Hunterbd788c92010-08-11 14:17:47 -07001295 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001296 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001297 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001298 }
1299
1300 from = blk_rq_pos(req);
1301 nr = blk_rq_sectors(req);
1302
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001303 if (mmc_can_discard(card))
1304 arg = MMC_DISCARD_ARG;
1305 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001306 arg = MMC_TRIM_ARG;
1307 else
1308 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001309 do {
1310 err = 0;
1311 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1312 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1313 INAND_CMD38_ARG_EXT_CSD,
1314 arg == MMC_TRIM_ARG ?
1315 INAND_CMD38_ARG_TRIM :
1316 INAND_CMD38_ARG_ERASE,
1317 0);
1318 }
1319 if (!err)
1320 err = mmc_erase(card, from, nr, arg);
1321 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001322 if (err)
1323 status = BLK_STS_IOERR;
1324 else
Adrian Hunter67716322011-08-29 16:42:15 +03001325 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001326fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001327 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001328}
1329
Linus Walleijdf061582017-01-24 11:17:57 +01001330static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001331 struct request *req)
1332{
Linus Walleij7db30282016-11-18 13:36:15 +01001333 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001334 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001335 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001336 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001337 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001338
Maya Erez775a9362013-04-18 15:41:55 +03001339 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001340 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001341 goto out;
1342 }
1343
1344 from = blk_rq_pos(req);
1345 nr = blk_rq_sectors(req);
1346
Maya Erez775a9362013-04-18 15:41:55 +03001347 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1348 arg = MMC_SECURE_TRIM1_ARG;
1349 else
1350 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001351
Adrian Hunter67716322011-08-29 16:42:15 +03001352retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001353 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1354 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1355 INAND_CMD38_ARG_EXT_CSD,
1356 arg == MMC_SECURE_TRIM1_ARG ?
1357 INAND_CMD38_ARG_SECTRIM1 :
1358 INAND_CMD38_ARG_SECERASE,
1359 0);
1360 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001361 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001362 }
Adrian Hunter28302812012-04-05 14:45:48 +03001363
Adrian Hunter49804542010-08-11 14:17:50 -07001364 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001365 if (err == -EIO)
1366 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001367 if (err) {
1368 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001369 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001370 }
Adrian Hunter28302812012-04-05 14:45:48 +03001371
1372 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001373 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1374 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1375 INAND_CMD38_ARG_EXT_CSD,
1376 INAND_CMD38_ARG_SECTRIM2,
1377 0);
1378 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001379 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001380 }
Adrian Hunter28302812012-04-05 14:45:48 +03001381
Adrian Hunter49804542010-08-11 14:17:50 -07001382 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001383 if (err == -EIO)
1384 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001385 if (err) {
1386 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001387 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001388 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001389 }
Adrian Hunter28302812012-04-05 14:45:48 +03001390
Adrian Hunter28302812012-04-05 14:45:48 +03001391out_retry:
1392 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001393 goto retry;
1394 if (!err)
1395 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001396out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001397 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001398}
1399
Linus Walleijdf061582017-01-24 11:17:57 +01001400static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001401{
Linus Walleij7db30282016-11-18 13:36:15 +01001402 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001403 struct mmc_card *card = md->queue.card;
1404 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001405
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001406 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001407 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001408}
1409
1410/*
1411 * Reformat current write as a reliable write, supporting
1412 * both legacy and the enhanced reliable write MMC cards.
1413 * In each transfer we'll handle only as much as a single
1414 * reliable write can handle, thus finish the request in
1415 * partial completions.
1416 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001417static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1418 struct mmc_card *card,
1419 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001421 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1422 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001423 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001424 brq->data.blocks = 1;
1425
1426 if (brq->data.blocks > card->ext_csd.rel_sectors)
1427 brq->data.blocks = card->ext_csd.rel_sectors;
1428 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1429 brq->data.blocks = 1;
1430 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001431}
1432
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001433#define CMD_ERRORS \
1434 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1435 R1_ADDRESS_ERROR | /* Misaligned address */ \
1436 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1437 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001438 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001439 R1_CC_ERROR | /* Card controller error */ \
1440 R1_ERROR) /* General/unknown error */
1441
Shawn Lind83c2db2017-08-18 09:16:08 +08001442static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001443{
Shawn Lind83c2db2017-08-18 09:16:08 +08001444 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001445
Shawn Lind83c2db2017-08-18 09:16:08 +08001446 /*
1447 * Per the SD specification(physical layer version 4.10)[1],
1448 * section 4.3.3, it explicitly states that "When the last
1449 * block of user area is read using CMD18, the host should
1450 * ignore OUT_OF_RANGE error that may occur even the sequence
1451 * is correct". And JESD84-B51 for eMMC also has a similar
1452 * statement on section 6.8.3.
1453 *
1454 * Multiple block read/write could be done by either predefined
1455 * method, namely CMD23, or open-ending mode. For open-ending mode,
1456 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1457 *
1458 * However the spec[1] doesn't tell us whether we should also
1459 * ignore that for predefined method. But per the spec[1], section
1460 * 4.15 Set Block Count Command, it says"If illegal block count
1461 * is set, out of range error will be indicated during read/write
1462 * operation (For example, data transfer is stopped at user area
1463 * boundary)." In another word, we could expect a out of range error
1464 * in the response for the following CMD18/25. And if argument of
1465 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1466 * we could also expect to get a -ETIMEDOUT or any error number from
1467 * the host drivers due to missing data response(for write)/data(for
1468 * read), as the cards will stop the data transfer by itself per the
1469 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1470 */
1471
1472 if (!brq->stop.error) {
1473 bool oor_with_open_end;
1474 /* If there is no error yet, check R1 response */
1475
1476 val = brq->stop.resp[0] & CMD_ERRORS;
1477 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1478
1479 if (val && !oor_with_open_end)
1480 brq->stop.error = -EIO;
1481 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001482}
1483
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001484static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1485 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001486{
Per Forlinee8a43a2011-07-01 18:55:33 +02001487 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001488 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001489 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001490 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001491 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001492 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001493 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001494
1495 /*
1496 * sbc.error indicates a problem with the set block count
1497 * command. No data will have been transferred.
1498 *
1499 * cmd.error indicates a problem with the r/w command. No
1500 * data will have been transferred.
1501 *
1502 * stop.error indicates a problem with the stop command. Data
1503 * may have been transferred, or may still be transferring.
1504 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001505
1506 mmc_blk_eval_resp_error(brq);
1507
1508 if (brq->sbc.error || brq->cmd.error ||
1509 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001510 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001511 case ERR_RETRY:
1512 return MMC_BLK_RETRY;
1513 case ERR_ABORT:
1514 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301515 case ERR_NOMEDIUM:
1516 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001517 case ERR_CONTINUE:
1518 break;
1519 }
1520 }
1521
1522 /*
1523 * Check for errors relating to the execution of the
1524 * initial command - such as address errors. No data
1525 * has been transferred.
1526 */
1527 if (brq->cmd.resp[0] & CMD_ERRORS) {
1528 pr_err("%s: r/w command failed, status = %#x\n",
1529 req->rq_disk->disk_name, brq->cmd.resp[0]);
1530 return MMC_BLK_ABORT;
1531 }
1532
1533 /*
1534 * Everything else is either success, or a data error of some
1535 * kind. If it was a write, we may have transitioned to
1536 * program mode, which we have to wait for it to complete.
1537 */
1538 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001539 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001540
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001541 /* Check stop command response */
1542 if (brq->stop.resp[0] & R1_ERROR) {
1543 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1544 req->rq_disk->disk_name, __func__,
1545 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001546 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001547 }
1548
Ulf Hansson95a91292014-01-29 13:11:27 +01001549 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1550 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001551 if (err)
1552 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001553 }
1554
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001555 /* if general error occurs, retry the write operation. */
1556 if (gen_err) {
1557 pr_warn("%s: retrying write for general error\n",
1558 req->rq_disk->disk_name);
1559 return MMC_BLK_RETRY;
1560 }
1561
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001562 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1563 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001564 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001565 pr_debug("%s: retrying because a re-tune was needed\n",
1566 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001567 brq->retune_retry_done = 1;
1568 return MMC_BLK_RETRY;
1569 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001570 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 +02001571 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001572 (unsigned)blk_rq_pos(req),
1573 (unsigned)blk_rq_sectors(req),
1574 brq->cmd.resp[0], brq->stop.resp[0]);
1575
1576 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001577 if (ecc_err)
1578 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001579 return MMC_BLK_DATA_ERR;
1580 } else {
1581 return MMC_BLK_CMD_ERR;
1582 }
1583 }
1584
Adrian Hunter67716322011-08-29 16:42:15 +03001585 if (!brq->data.bytes_xfered)
1586 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001587
Adrian Hunter67716322011-08-29 16:42:15 +03001588 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1589 return MMC_BLK_PARTIAL;
1590
1591 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001592}
1593
Adrian Hunterca5717f2017-03-13 14:36:41 +02001594static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
Adrian Hunterd3377c02017-09-22 15:36:55 +03001595 int disable_multi, bool *do_rel_wr_p,
1596 bool *do_data_tag_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001598 struct mmc_blk_data *md = mq->blkdata;
1599 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001600 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001601 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterd3377c02017-09-22 15:36:55 +03001602 bool do_rel_wr, do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001604 /*
1605 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001606 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001607 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001608 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1609 rq_data_dir(req) == WRITE &&
1610 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001611
Per Forlin54d49d72011-07-01 18:55:29 +02001612 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001613
Per Forlin54d49d72011-07-01 18:55:29 +02001614 brq->mrq.data = &brq->data;
Adrian Hunter93482b32017-09-22 15:36:56 +03001615 brq->mrq.tag = req->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Per Forlin54d49d72011-07-01 18:55:29 +02001617 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1618 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001619
1620 if (rq_data_dir(req) == READ) {
1621 brq->data.flags = MMC_DATA_READ;
1622 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1623 } else {
1624 brq->data.flags = MMC_DATA_WRITE;
1625 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1626 }
1627
1628 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001629 brq->data.blocks = blk_rq_sectors(req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001630 brq->data.blk_addr = blk_rq_pos(req);
1631
1632 /*
1633 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1634 * The eMMC will give "high" priority tasks priority over "simple"
1635 * priority tasks. Here we always set "simple" priority by not setting
1636 * MMC_DATA_PRIO.
1637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Per Forlin54d49d72011-07-01 18:55:29 +02001639 /*
1640 * The block layer doesn't support all sector count
1641 * restrictions, so we need to be prepared for too big
1642 * requests.
1643 */
1644 if (brq->data.blocks > card->host->max_blk_count)
1645 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001647 if (brq->data.blocks > 1) {
1648 /*
1649 * After a read error, we redo the request one sector
1650 * at a time in order to accurately determine which
1651 * sectors can be read successfully.
1652 */
1653 if (disable_multi)
1654 brq->data.blocks = 1;
1655
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001656 /*
1657 * Some controllers have HW issues while operating
1658 * in multiple I/O mode
1659 */
1660 if (card->host->ops->multi_io_quirk)
1661 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1662 (rq_data_dir(req) == READ) ?
1663 MMC_DATA_READ : MMC_DATA_WRITE,
1664 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001665 }
Per Forlin54d49d72011-07-01 18:55:29 +02001666
Adrian Hunter93482b32017-09-22 15:36:56 +03001667 if (do_rel_wr) {
Adrian Hunterca5717f2017-03-13 14:36:41 +02001668 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter93482b32017-09-22 15:36:56 +03001669 brq->data.flags |= MMC_DATA_REL_WR;
1670 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001671
1672 /*
1673 * Data tag is used only during writing meta data to speed
1674 * up write and any subsequent read of this meta data
1675 */
Adrian Hunterd3377c02017-09-22 15:36:55 +03001676 do_data_tag = card->ext_csd.data_tag_unit_size &&
1677 (req->cmd_flags & REQ_META) &&
1678 (rq_data_dir(req) == WRITE) &&
1679 ((brq->data.blocks * brq->data.blksz) >=
1680 card->ext_csd.data_tag_unit_size);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001681
Adrian Hunter93482b32017-09-22 15:36:56 +03001682 if (do_data_tag)
1683 brq->data.flags |= MMC_DATA_DAT_TAG;
1684
Adrian Hunterca5717f2017-03-13 14:36:41 +02001685 mmc_set_data_timeout(&brq->data, card);
1686
1687 brq->data.sg = mqrq->sg;
1688 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1689
1690 /*
1691 * Adjust the sg list so it is the same size as the
1692 * request.
1693 */
1694 if (brq->data.blocks != blk_rq_sectors(req)) {
1695 int i, data_size = brq->data.blocks << 9;
1696 struct scatterlist *sg;
1697
1698 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1699 data_size -= sg->length;
1700 if (data_size <= 0) {
1701 sg->length += data_size;
1702 i++;
1703 break;
1704 }
1705 }
1706 brq->data.sg_len = i;
1707 }
1708
1709 mqrq->areq.mrq = &brq->mrq;
Adrian Hunterd3377c02017-09-22 15:36:55 +03001710
1711 if (do_rel_wr_p)
1712 *do_rel_wr_p = do_rel_wr;
1713
1714 if (do_data_tag_p)
1715 *do_data_tag_p = do_data_tag;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001716}
1717
1718static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1719 struct mmc_card *card,
1720 int disable_multi,
1721 struct mmc_queue *mq)
1722{
1723 u32 readcmd, writecmd;
1724 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001725 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001726 struct mmc_blk_data *md = mq->blkdata;
1727 bool do_rel_wr, do_data_tag;
1728
1729 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1730
1731 brq->mrq.cmd = &brq->cmd;
1732
1733 brq->cmd.arg = blk_rq_pos(req);
1734 if (!mmc_card_blockaddr(card))
1735 brq->cmd.arg <<= 9;
1736 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1737
Per Forlin54d49d72011-07-01 18:55:29 +02001738 if (brq->data.blocks > 1 || do_rel_wr) {
1739 /* SPI multiblock writes terminate using a special
1740 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001741 */
Per Forlin54d49d72011-07-01 18:55:29 +02001742 if (!mmc_host_is_spi(card->host) ||
1743 rq_data_dir(req) == READ)
1744 brq->mrq.stop = &brq->stop;
1745 readcmd = MMC_READ_MULTIPLE_BLOCK;
1746 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1747 } else {
1748 brq->mrq.stop = NULL;
1749 readcmd = MMC_READ_SINGLE_BLOCK;
1750 writecmd = MMC_WRITE_BLOCK;
1751 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001752 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301753
1754 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001755 * Pre-defined multi-block transfers are preferable to
1756 * open ended-ones (and necessary for reliable writes).
1757 * However, it is not sufficient to just send CMD23,
1758 * and avoid the final CMD12, as on an error condition
1759 * CMD12 (stop) needs to be sent anyway. This, coupled
1760 * with Auto-CMD23 enhancements provided by some
1761 * hosts, means that the complexity of dealing
1762 * with this is best left to the host. If CMD23 is
1763 * supported by card and host, we'll fill sbc in and let
1764 * the host deal with handling it correctly. This means
1765 * that for hosts that don't expose MMC_CAP_CMD23, no
1766 * change of behavior will be observed.
1767 *
1768 * N.B: Some MMC cards experience perf degradation.
1769 * We'll avoid using CMD23-bounded multiblock writes for
1770 * these, while retaining features like reliable writes.
1771 */
Saugata Das42659002011-12-21 13:09:17 +05301772 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1773 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1774 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001775 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1776 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301777 (do_rel_wr ? (1 << 31) : 0) |
1778 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001779 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1780 brq->mrq.sbc = &brq->sbc;
1781 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001782
Linus Walleij74f5ba32017-02-01 13:47:55 +01001783 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001784}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Linus Walleij0e65f102017-02-01 13:47:58 +01001786static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1787 struct mmc_blk_request *brq, struct request *req,
1788 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001789{
Linus Walleij0e65f102017-02-01 13:47:58 +01001790 bool req_pending;
1791
Adrian Hunter67716322011-08-29 16:42:15 +03001792 /*
1793 * If this is an SD card and we're writing, we can first
1794 * mark the known good sectors as ok.
1795 *
1796 * If the card is not SD, we can still ok written sectors
1797 * as reported by the controller (which might be less than
1798 * the real number of written sectors, but never more).
1799 */
1800 if (mmc_card_sd(card)) {
1801 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001802 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001803
Linus Walleij169f03a2017-02-01 13:47:57 +01001804 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001805 if (err)
1806 req_pending = old_req_pending;
1807 else
Adrian Huntera7c17d82017-08-25 15:43:45 +03001808 req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001809 } else {
Adrian Huntera7c17d82017-08-25 15:43:45 +03001810 req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001811 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001812 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001813}
1814
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001815static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1816 struct request *req,
1817 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001818{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001819 if (mmc_card_removed(card))
1820 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001821 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001822 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001823}
1824
Linus Walleijb2928e12017-02-01 13:47:54 +01001825/**
1826 * mmc_blk_rw_try_restart() - tries to restart the current async request
1827 * @mq: the queue with the card and host to restart
1828 * @req: a new request that want to be started after the current one
1829 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001830static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1831 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001832{
1833 if (!req)
1834 return;
1835
Linus Walleijb2928e12017-02-01 13:47:54 +01001836 /*
1837 * If the card was removed, just cancel everything and return.
1838 */
1839 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001840 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001841 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001842 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001843 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001844 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001845 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001846 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1847 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001848}
1849
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001850static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001851{
Linus Walleij7db30282016-11-18 13:36:15 +01001852 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001853 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001854 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001855 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001856 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001857 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001858 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001859 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001860 struct mmc_async_req *new_areq;
1861 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001862 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001863
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001864 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001865 mqrq_cur = req_to_mmc_queue_req(new_req);
1866 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001867 }
1868
1869 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001870 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001871
1872 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001873 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301874 /*
1875 * When 4KB native sector is enabled, only 8 blocks
1876 * multiple read or write is allowed
1877 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001878 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001879 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301880 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001881 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001882 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001883 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301884 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001885
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001886 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1887 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001888 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001889 new_areq = NULL;
1890
Linus Walleijc3399ef2017-02-01 13:47:53 +01001891 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001892 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001893 /*
1894 * We have just put the first request into the pipeline
1895 * and there is nothing more to do until it is
1896 * complete.
1897 */
Linus Walleijdf061582017-01-24 11:17:57 +01001898 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001899 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001900
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001901 /*
1902 * An asynchronous request has been completed and we proceed
1903 * to handle the result of it.
1904 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001905 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001906 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001907 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001908 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001909
Per Forlind78d4a8a2011-07-01 18:55:30 +02001910 switch (status) {
1911 case MMC_BLK_SUCCESS:
1912 case MMC_BLK_PARTIAL:
1913 /*
1914 * A block was successfully transferred.
1915 */
Adrian Hunter67716322011-08-29 16:42:15 +03001916 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001917
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001918 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001919 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001920 /*
1921 * If the blk_end_request function returns non-zero even
1922 * though all data has been transferred and no errors
1923 * were returned by the host controller, it's a bug.
1924 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001925 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301926 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001927 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001928 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001929 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001930 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001931 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001932 break;
1933 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001934 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001935 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001936 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001937 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1938 else
Linus Walleij304419d2017-05-18 11:29:32 +02001939 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001940 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001941 return;
1942 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001943 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001944 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001945 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001946 return;
1947 }
Ding Wang29535f72015-05-18 20:14:15 +08001948 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001949 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001950 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001951 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001952 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001953 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001954 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001955 if (!mmc_blk_reset(md, card->host, type))
1956 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001957 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001958 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001959 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001960 case MMC_BLK_DATA_ERR: {
1961 int err;
1962
1963 err = mmc_blk_reset(md, card->host, type);
1964 if (!err)
1965 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001966 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001967 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001968 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001969 return;
1970 }
Adrian Hunter67716322011-08-29 16:42:15 +03001971 /* Fall through */
1972 }
1973 case MMC_BLK_ECC_ERR:
1974 if (brq->data.blocks > 1) {
1975 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001976 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001977 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001978 disable_multi = 1;
1979 break;
1980 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001981 /*
1982 * After an error, we redo I/O one sector at a
1983 * time, so we only reach here after trying to
1984 * read a single sector.
1985 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001986 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001987 brq->data.blksz);
1988 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001989 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001990 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001991 return;
1992 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001993 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301994 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001995 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001996 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001997 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001998 default:
1999 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01002000 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002001 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02002002 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01002003 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002004 }
2005
Linus Walleij0e65f102017-02-01 13:47:58 +01002006 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01002007 /*
2008 * In case of a incomplete request
2009 * prepare it again and resend.
2010 */
2011 mmc_blk_rw_rq_prep(mq_rq, card,
2012 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01002013 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01002014 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002015 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002016 }
Linus Walleij0e65f102017-02-01 13:47:58 +01002017 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002018
Linus Walleij304419d2017-05-18 11:29:32 +02002019 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Linus Walleijdf061582017-01-24 11:17:57 +01002022void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002023{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002024 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01002025 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002026 struct mmc_card *card = md->queue.card;
2027
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002028 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02002029 /* claim host only for the first request */
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002030 mmc_get_card(card, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02002031
Linus Walleij1f797ed2017-08-20 23:39:10 +02002032 ret = mmc_blk_part_switch(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002033 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002034 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002035 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002036 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002037 goto out;
2038 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002039
Linus Walleij614f0382017-05-18 11:29:34 +02002040 if (req) {
2041 switch (req_op(req)) {
2042 case REQ_OP_DRV_IN:
2043 case REQ_OP_DRV_OUT:
2044 /*
2045 * Complete ongoing async transfer before issuing
2046 * ioctl()s
2047 */
2048 if (mq->qcnt)
2049 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02002050 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02002051 break;
2052 case REQ_OP_DISCARD:
2053 /*
2054 * Complete ongoing async transfer before issuing
2055 * discard.
2056 */
2057 if (mq->qcnt)
2058 mmc_blk_issue_rw_rq(mq, NULL);
2059 mmc_blk_issue_discard_rq(mq, req);
2060 break;
2061 case REQ_OP_SECURE_ERASE:
2062 /*
2063 * Complete ongoing async transfer before issuing
2064 * secure erase.
2065 */
2066 if (mq->qcnt)
2067 mmc_blk_issue_rw_rq(mq, NULL);
2068 mmc_blk_issue_secdiscard_rq(mq, req);
2069 break;
2070 case REQ_OP_FLUSH:
2071 /*
2072 * Complete ongoing async transfer before issuing
2073 * flush.
2074 */
2075 if (mq->qcnt)
2076 mmc_blk_issue_rw_rq(mq, NULL);
2077 mmc_blk_issue_flush(mq, req);
2078 break;
2079 default:
2080 /* Normal request, just issue it */
2081 mmc_blk_issue_rw_rq(mq, req);
2082 card->host->context_info.is_waiting_last_req = false;
2083 break;
Wu Fengguang73222382017-05-23 05:11:33 +08002084 }
Adrian Hunter49804542010-08-11 14:17:50 -07002085 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002086 /* No request, flushing the pipeline with NULL */
2087 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002088 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002089 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002090
Andrei Warkentin371a6892011-04-11 18:10:25 -05002091out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002092 if (!mq->qcnt)
Adrian Hunter6c0cedd2017-09-22 15:36:51 +03002093 mmc_put_card(card, NULL);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Russell Kinga6f6c962006-01-03 22:38:44 +00002096static inline int mmc_blk_readonly(struct mmc_card *card)
2097{
2098 return mmc_card_readonly(card) ||
2099 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2100}
2101
Andrei Warkentin371a6892011-04-11 18:10:25 -05002102static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2103 struct device *parent,
2104 sector_t size,
2105 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002106 const char *subname,
2107 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 struct mmc_blk_data *md;
2110 int devidx, ret;
2111
Heiner Kallweita04848c2017-02-01 19:44:22 +01002112 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
Shawn Line7b42762017-08-23 15:38:31 +08002113 if (devidx < 0) {
2114 /*
2115 * We get -ENOSPC because there are no more any available
2116 * devidx. The reason may be that, either userspace haven't yet
2117 * unmounted the partitions, which postpones mmc_blk_release()
2118 * from being called, or the device has more partitions than
2119 * what we support.
2120 */
2121 if (devidx == -ENOSPC)
2122 dev_err(mmc_dev(card->host),
2123 "no more device IDs available\n");
2124
Heiner Kallweita04848c2017-02-01 19:44:22 +01002125 return ERR_PTR(devidx);
Shawn Line7b42762017-08-23 15:38:31 +08002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002128 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002129 if (!md) {
2130 ret = -ENOMEM;
2131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002133
Johan Rudholmadd710e2011-12-02 08:51:06 +01002134 md->area_type = area_type;
2135
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002136 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002137 * Set the read-only status based on the supported commands
2138 * and the write protect switch.
2139 */
2140 md->read_only = mmc_blk_readonly(card);
2141
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002142 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002143 if (md->disk == NULL) {
2144 ret = -ENOMEM;
2145 goto err_kfree;
2146 }
2147
2148 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002149 INIT_LIST_HEAD(&md->part);
Linus Walleij97548572017-09-20 10:02:00 +02002150 INIT_LIST_HEAD(&md->rpmbs);
Russell Kinga6f6c962006-01-03 22:38:44 +00002151 md->usage = 1;
2152
Adrian Hunterd09408a2011-06-23 13:40:28 +03002153 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002154 if (ret)
2155 goto err_putdisk;
2156
Linus Walleij7db30282016-11-18 13:36:15 +01002157 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002158
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002159 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002160 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002161 md->disk->fops = &mmc_bdops;
2162 md->disk->private_data = md;
2163 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002164 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002166 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002167 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002168 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002169
2170 /*
2171 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2172 *
2173 * - be set for removable media with permanent block devices
2174 * - be unset for removable block devices with permanent media
2175 *
2176 * Since MMC block devices clearly fall under the second
2177 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2178 * should use the block device creation/destruction hotplug
2179 * messages to tell when the card is present.
2180 */
2181
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002182 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002183 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002184
Saugata Dasa5075eb2012-05-17 16:32:21 +05302185 if (mmc_card_mmc(card))
2186 blk_queue_logical_block_size(md->queue.queue,
2187 card->ext_csd.data_sector_size);
2188 else
2189 blk_queue_logical_block_size(md->queue.queue, 512);
2190
Andrei Warkentin371a6892011-04-11 18:10:25 -05002191 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002192
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002193 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002194 if ((mmc_card_mmc(card) &&
2195 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002196 (mmc_card_sd(card) &&
2197 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2198 md->flags |= MMC_BLK_CMD23;
2199 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002200
2201 if (mmc_card_mmc(card) &&
2202 md->flags & MMC_BLK_CMD23 &&
2203 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2204 card->ext_csd.rel_sectors)) {
2205 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002206 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002207 }
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002210
2211 err_putdisk:
2212 put_disk(md->disk);
2213 err_kfree:
2214 kfree(md);
2215 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002216 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002217 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2221{
2222 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002223
2224 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2225 /*
2226 * The EXT_CSD sector count is in number or 512 byte
2227 * sectors.
2228 */
2229 size = card->ext_csd.sectors;
2230 } else {
2231 /*
2232 * The CSD capacity field is in units of read_blkbits.
2233 * set_capacity takes units of 512 bytes.
2234 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002235 size = (typeof(sector_t))card->csd.capacity
2236 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002237 }
2238
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002239 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002240 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002241}
2242
2243static int mmc_blk_alloc_part(struct mmc_card *card,
2244 struct mmc_blk_data *md,
2245 unsigned int part_type,
2246 sector_t size,
2247 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002248 const char *subname,
2249 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002250{
2251 char cap_str[10];
2252 struct mmc_blk_data *part_md;
2253
2254 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002255 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002256 if (IS_ERR(part_md))
2257 return PTR_ERR(part_md);
2258 part_md->part_type = part_type;
2259 list_add(&part_md->part, &md->part);
2260
James Bottomleyb9f28d82015-03-05 18:47:01 -08002261 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002262 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302263 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002264 part_md->disk->disk_name, mmc_card_id(card),
2265 mmc_card_name(card), part_md->part_type, cap_str);
2266 return 0;
2267}
2268
Linus Walleij97548572017-09-20 10:02:00 +02002269/**
2270 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2271 * @filp: the character device file
2272 * @cmd: the ioctl() command
2273 * @arg: the argument from userspace
2274 *
2275 * This will essentially just redirect the ioctl()s coming in over to
2276 * the main block device spawning the RPMB character device.
2277 */
2278static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2279 unsigned long arg)
2280{
2281 struct mmc_rpmb_data *rpmb = filp->private_data;
2282 int ret;
2283
2284 switch (cmd) {
2285 case MMC_IOC_CMD:
2286 ret = mmc_blk_ioctl_cmd(rpmb->md,
2287 (struct mmc_ioc_cmd __user *)arg,
2288 rpmb);
2289 break;
2290 case MMC_IOC_MULTI_CMD:
2291 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2292 (struct mmc_ioc_multi_cmd __user *)arg,
2293 rpmb);
2294 break;
2295 default:
2296 ret = -EINVAL;
2297 break;
2298 }
2299
2300 return 0;
2301}
2302
2303#ifdef CONFIG_COMPAT
2304static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2305 unsigned long arg)
2306{
2307 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2308}
2309#endif
2310
2311static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2312{
2313 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2314 struct mmc_rpmb_data, chrdev);
2315
2316 get_device(&rpmb->dev);
2317 filp->private_data = rpmb;
Linus Walleij1c87f732017-10-04 11:10:07 +02002318 mmc_blk_get(rpmb->md->disk);
Linus Walleij97548572017-09-20 10:02:00 +02002319
2320 return nonseekable_open(inode, filp);
2321}
2322
2323static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2324{
2325 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2326 struct mmc_rpmb_data, chrdev);
2327
2328 put_device(&rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002329 mmc_blk_put(rpmb->md);
Linus Walleij97548572017-09-20 10:02:00 +02002330
2331 return 0;
2332}
2333
2334static const struct file_operations mmc_rpmb_fileops = {
2335 .release = mmc_rpmb_chrdev_release,
2336 .open = mmc_rpmb_chrdev_open,
2337 .owner = THIS_MODULE,
2338 .llseek = no_llseek,
2339 .unlocked_ioctl = mmc_rpmb_ioctl,
2340#ifdef CONFIG_COMPAT
2341 .compat_ioctl = mmc_rpmb_ioctl_compat,
2342#endif
2343};
2344
Linus Walleij1c87f732017-10-04 11:10:07 +02002345static void mmc_blk_rpmb_device_release(struct device *dev)
2346{
2347 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2348
2349 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2350 kfree(rpmb);
2351}
Linus Walleij97548572017-09-20 10:02:00 +02002352
2353static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2354 struct mmc_blk_data *md,
2355 unsigned int part_index,
2356 sector_t size,
2357 const char *subname)
2358{
2359 int devidx, ret;
2360 char rpmb_name[DISK_NAME_LEN];
2361 char cap_str[10];
2362 struct mmc_rpmb_data *rpmb;
2363
2364 /* This creates the minor number for the RPMB char device */
2365 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2366 if (devidx < 0)
2367 return devidx;
2368
2369 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
Linus Walleij1c87f732017-10-04 11:10:07 +02002370 if (!rpmb) {
2371 ida_simple_remove(&mmc_rpmb_ida, devidx);
Linus Walleij97548572017-09-20 10:02:00 +02002372 return -ENOMEM;
Linus Walleij1c87f732017-10-04 11:10:07 +02002373 }
Linus Walleij97548572017-09-20 10:02:00 +02002374
2375 snprintf(rpmb_name, sizeof(rpmb_name),
2376 "mmcblk%u%s", card->host->index, subname ? subname : "");
2377
2378 rpmb->id = devidx;
2379 rpmb->part_index = part_index;
2380 rpmb->dev.init_name = rpmb_name;
2381 rpmb->dev.bus = &mmc_rpmb_bus_type;
2382 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2383 rpmb->dev.parent = &card->dev;
Linus Walleij1c87f732017-10-04 11:10:07 +02002384 rpmb->dev.release = mmc_blk_rpmb_device_release;
Linus Walleij97548572017-09-20 10:02:00 +02002385 device_initialize(&rpmb->dev);
2386 dev_set_drvdata(&rpmb->dev, rpmb);
2387 rpmb->md = md;
2388
2389 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2390 rpmb->chrdev.owner = THIS_MODULE;
2391 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2392 if (ret) {
2393 pr_err("%s: could not add character device\n", rpmb_name);
Linus Walleij1c87f732017-10-04 11:10:07 +02002394 goto out_put_device;
Linus Walleij97548572017-09-20 10:02:00 +02002395 }
2396
2397 list_add(&rpmb->node, &md->rpmbs);
2398
2399 string_get_size((u64)size, 512, STRING_UNITS_2,
2400 cap_str, sizeof(cap_str));
2401
2402 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2403 rpmb_name, mmc_card_id(card),
2404 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2405 MAJOR(mmc_rpmb_devt), rpmb->id);
2406
2407 return 0;
2408
Linus Walleij1c87f732017-10-04 11:10:07 +02002409out_put_device:
2410 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002411 return ret;
2412}
2413
2414static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
Linus Walleij1c87f732017-10-04 11:10:07 +02002415
Linus Walleij97548572017-09-20 10:02:00 +02002416{
2417 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
Linus Walleij1c87f732017-10-04 11:10:07 +02002418 put_device(&rpmb->dev);
Linus Walleij97548572017-09-20 10:02:00 +02002419}
2420
Namjae Jeone0c368d2011-10-06 23:41:38 +09002421/* MMC Physical partitions consist of two boot partitions and
2422 * up to four general purpose partitions.
2423 * For each partition enabled in EXT_CSD a block device will be allocatedi
2424 * to provide access to the partition.
2425 */
2426
Andrei Warkentin371a6892011-04-11 18:10:25 -05002427static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2428{
Linus Walleij97548572017-09-20 10:02:00 +02002429 int idx, ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002430
2431 if (!mmc_card_mmc(card))
2432 return 0;
2433
Namjae Jeone0c368d2011-10-06 23:41:38 +09002434 for (idx = 0; idx < card->nr_parts; idx++) {
Linus Walleij97548572017-09-20 10:02:00 +02002435 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2436 /*
2437 * RPMB partitions does not provide block access, they
2438 * are only accessed using ioctl():s. Thus create
2439 * special RPMB block devices that do not have a
2440 * backing block queue for these.
2441 */
2442 ret = mmc_blk_alloc_rpmb_part(card, md,
2443 card->part[idx].part_cfg,
2444 card->part[idx].size >> 9,
2445 card->part[idx].name);
2446 if (ret)
2447 return ret;
2448 } else if (card->part[idx].size) {
Namjae Jeone0c368d2011-10-06 23:41:38 +09002449 ret = mmc_blk_alloc_part(card, md,
2450 card->part[idx].part_cfg,
2451 card->part[idx].size >> 9,
2452 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002453 card->part[idx].name,
2454 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002455 if (ret)
2456 return ret;
2457 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002458 }
2459
Linus Walleij97548572017-09-20 10:02:00 +02002460 return 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002461}
2462
Andrei Warkentin371a6892011-04-11 18:10:25 -05002463static void mmc_blk_remove_req(struct mmc_blk_data *md)
2464{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002465 struct mmc_card *card;
2466
Andrei Warkentin371a6892011-04-11 18:10:25 -05002467 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002468 /*
2469 * Flush remaining requests and free queues. It
2470 * is freeing the queue that stops new requests
2471 * from being accepted.
2472 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002473 card = md->queue.card;
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002474 spin_lock_irq(md->queue.queue->queue_lock);
Shawn Lin7c84b8b2017-07-25 09:11:28 +08002475 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002476 spin_unlock_irq(md->queue.queue->queue_lock);
Grzegorz Slujabbdc74d2017-07-13 11:17:58 +02002477 blk_set_queue_dying(md->queue.queue);
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002478 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002479 if (md->disk->flags & GENHD_FL_UP) {
2480 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002481 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2482 card->ext_csd.boot_ro_lockable)
2483 device_remove_file(disk_to_dev(md->disk),
2484 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002485
Andrei Warkentin371a6892011-04-11 18:10:25 -05002486 del_gendisk(md->disk);
2487 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002488 mmc_blk_put(md);
2489 }
2490}
2491
2492static void mmc_blk_remove_parts(struct mmc_card *card,
2493 struct mmc_blk_data *md)
2494{
2495 struct list_head *pos, *q;
2496 struct mmc_blk_data *part_md;
Linus Walleij97548572017-09-20 10:02:00 +02002497 struct mmc_rpmb_data *rpmb;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002498
Linus Walleij97548572017-09-20 10:02:00 +02002499 /* Remove RPMB partitions */
2500 list_for_each_safe(pos, q, &md->rpmbs) {
2501 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2502 list_del(pos);
2503 mmc_blk_remove_rpmb_part(rpmb);
2504 }
2505 /* Remove block partitions */
Andrei Warkentin371a6892011-04-11 18:10:25 -05002506 list_for_each_safe(pos, q, &md->part) {
2507 part_md = list_entry(pos, struct mmc_blk_data, part);
2508 list_del(pos);
2509 mmc_blk_remove_req(part_md);
2510 }
2511}
2512
2513static int mmc_add_disk(struct mmc_blk_data *md)
2514{
2515 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002516 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002517
Dan Williams307d8e62016-06-20 10:40:44 -07002518 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002519 md->force_ro.show = force_ro_show;
2520 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302521 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002522 md->force_ro.attr.name = "force_ro";
2523 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2524 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2525 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002526 goto force_ro_fail;
2527
2528 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2529 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002530 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002531
2532 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2533 mode = S_IRUGO;
2534 else
2535 mode = S_IRUGO | S_IWUSR;
2536
2537 md->power_ro_lock.show = power_ro_lock_show;
2538 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002539 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002540 md->power_ro_lock.attr.mode = mode;
2541 md->power_ro_lock.attr.name =
2542 "ro_lock_until_next_power_on";
2543 ret = device_create_file(disk_to_dev(md->disk),
2544 &md->power_ro_lock);
2545 if (ret)
2546 goto power_ro_lock_fail;
2547 }
2548 return ret;
2549
2550power_ro_lock_fail:
2551 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2552force_ro_fail:
2553 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002554
2555 return ret;
2556}
2557
Linus Walleij627c3cc2017-08-20 23:39:08 +02002558#ifdef CONFIG_DEBUG_FS
2559
2560static int mmc_dbg_card_status_get(void *data, u64 *val)
2561{
2562 struct mmc_card *card = data;
2563 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2564 struct mmc_queue *mq = &md->queue;
2565 struct request *req;
2566 int ret;
2567
2568 /* Ask the block layer about the card status */
2569 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002570 if (IS_ERR(req))
2571 return PTR_ERR(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002572 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2573 blk_execute_rq(mq->queue, NULL, req, 0);
2574 ret = req_to_mmc_queue_req(req)->drv_op_result;
2575 if (ret >= 0) {
2576 *val = ret;
2577 ret = 0;
2578 }
Adrian Hunter34c089e2017-11-21 15:42:27 +02002579 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002580
2581 return ret;
2582}
2583DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2584 NULL, "%08llx\n");
2585
2586/* That is two digits * 512 + 1 for newline */
2587#define EXT_CSD_STR_LEN 1025
2588
2589static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2590{
2591 struct mmc_card *card = inode->i_private;
2592 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2593 struct mmc_queue *mq = &md->queue;
2594 struct request *req;
2595 char *buf;
2596 ssize_t n = 0;
2597 u8 *ext_csd;
2598 int err, i;
2599
2600 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2601 if (!buf)
2602 return -ENOMEM;
2603
2604 /* Ask the block layer for the EXT CSD */
2605 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Adrian Hunterfb8e456e2017-11-21 15:42:28 +02002606 if (IS_ERR(req)) {
2607 err = PTR_ERR(req);
2608 goto out_free;
2609 }
Linus Walleij627c3cc2017-08-20 23:39:08 +02002610 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2611 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2612 blk_execute_rq(mq->queue, NULL, req, 0);
2613 err = req_to_mmc_queue_req(req)->drv_op_result;
Adrian Hunter34c089e2017-11-21 15:42:27 +02002614 blk_put_request(req);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002615 if (err) {
2616 pr_err("FAILED %d\n", err);
2617 goto out_free;
2618 }
2619
2620 for (i = 0; i < 512; i++)
2621 n += sprintf(buf + n, "%02x", ext_csd[i]);
2622 n += sprintf(buf + n, "\n");
2623
2624 if (n != EXT_CSD_STR_LEN) {
2625 err = -EINVAL;
2626 goto out_free;
2627 }
2628
2629 filp->private_data = buf;
2630 kfree(ext_csd);
2631 return 0;
2632
2633out_free:
2634 kfree(buf);
2635 return err;
2636}
2637
2638static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2639 size_t cnt, loff_t *ppos)
2640{
2641 char *buf = filp->private_data;
2642
2643 return simple_read_from_buffer(ubuf, cnt, ppos,
2644 buf, EXT_CSD_STR_LEN);
2645}
2646
2647static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2648{
2649 kfree(file->private_data);
2650 return 0;
2651}
2652
2653static const struct file_operations mmc_dbg_ext_csd_fops = {
2654 .open = mmc_ext_csd_open,
2655 .read = mmc_ext_csd_read,
2656 .release = mmc_ext_csd_release,
2657 .llseek = default_llseek,
2658};
2659
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002660static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002661{
2662 struct dentry *root;
2663
2664 if (!card->debugfs_root)
2665 return 0;
2666
2667 root = card->debugfs_root;
2668
2669 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002670 md->status_dentry =
2671 debugfs_create_file("status", S_IRUSR, root, card,
2672 &mmc_dbg_card_status_fops);
2673 if (!md->status_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002674 return -EIO;
2675 }
2676
2677 if (mmc_card_mmc(card)) {
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002678 md->ext_csd_dentry =
2679 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2680 &mmc_dbg_ext_csd_fops);
2681 if (!md->ext_csd_dentry)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002682 return -EIO;
2683 }
2684
2685 return 0;
2686}
2687
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002688static void mmc_blk_remove_debugfs(struct mmc_card *card,
2689 struct mmc_blk_data *md)
2690{
2691 if (!card->debugfs_root)
2692 return;
2693
2694 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2695 debugfs_remove(md->status_dentry);
2696 md->status_dentry = NULL;
2697 }
2698
2699 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2700 debugfs_remove(md->ext_csd_dentry);
2701 md->ext_csd_dentry = NULL;
2702 }
2703}
Linus Walleij627c3cc2017-08-20 23:39:08 +02002704
2705#else
2706
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002707static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
Linus Walleij627c3cc2017-08-20 23:39:08 +02002708{
2709 return 0;
2710}
2711
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002712static void mmc_blk_remove_debugfs(struct mmc_card *card,
2713 struct mmc_blk_data *md)
2714{
2715}
2716
Linus Walleij627c3cc2017-08-20 23:39:08 +02002717#endif /* CONFIG_DEBUG_FS */
2718
Ulf Hansson96541ba2015-04-14 13:06:12 +02002719static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002721 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002722 char cap_str[10];
2723
Pierre Ossman912490d2005-05-21 10:27:02 +01002724 /*
2725 * Check that the card supports the command class(es) we need.
2726 */
2727 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return -ENODEV;
2729
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002730 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002733 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return PTR_ERR(md);
2735
James Bottomleyb9f28d82015-03-05 18:47:01 -08002736 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002737 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302738 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002740 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Andrei Warkentin371a6892011-04-11 18:10:25 -05002742 if (mmc_blk_alloc_parts(card, md))
2743 goto out;
2744
Ulf Hansson96541ba2015-04-14 13:06:12 +02002745 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002746
Andrei Warkentin371a6892011-04-11 18:10:25 -05002747 if (mmc_add_disk(md))
2748 goto out;
2749
2750 list_for_each_entry(part_md, &md->part, part) {
2751 if (mmc_add_disk(part_md))
2752 goto out;
2753 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002754
Linus Walleij627c3cc2017-08-20 23:39:08 +02002755 /* Add two debugfs entries */
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002756 mmc_blk_add_debugfs(card, md);
Linus Walleij627c3cc2017-08-20 23:39:08 +02002757
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002758 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2759 pm_runtime_use_autosuspend(&card->dev);
2760
2761 /*
2762 * Don't enable runtime PM for SD-combo cards here. Leave that
2763 * decision to be taken during the SDIO init sequence instead.
2764 */
2765 if (card->type != MMC_TYPE_SD_COMBO) {
2766 pm_runtime_set_active(&card->dev);
2767 pm_runtime_enable(&card->dev);
2768 }
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return 0;
2771
2772 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002773 mmc_blk_remove_parts(card, md);
2774 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Ulf Hansson96541ba2015-04-14 13:06:12 +02002778static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002780 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Adrian Hunterf9f0da92017-11-21 15:42:30 +02002782 mmc_blk_remove_debugfs(card, md);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002783 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002784 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002785 mmc_claim_host(card->host);
Linus Walleij1f797ed2017-08-20 23:39:10 +02002786 mmc_blk_part_switch(card, md->part_type);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002787 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002788 if (card->type != MMC_TYPE_SD_COMBO)
2789 pm_runtime_disable(&card->dev);
2790 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002791 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002792 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Ulf Hansson96541ba2015-04-14 13:06:12 +02002795static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002797 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002798 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 if (md) {
2801 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002802 list_for_each_entry(part_md, &md->part, part) {
2803 mmc_queue_suspend(&part_md->queue);
2804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
2806 return 0;
2807}
2808
Ulf Hansson96541ba2015-04-14 13:06:12 +02002809static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002810{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002811 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002812}
2813
Ulf Hansson0967edc2014-10-06 11:29:42 +02002814#ifdef CONFIG_PM_SLEEP
2815static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002816{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002817 struct mmc_card *card = mmc_dev_to_card(dev);
2818
2819 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002820}
2821
Ulf Hansson0967edc2014-10-06 11:29:42 +02002822static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002824 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002825 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002828 /*
2829 * Resume involves the card going into idle state,
2830 * so current partition is always the main one.
2831 */
2832 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002834 list_for_each_entry(part_md, &md->part, part) {
2835 mmc_queue_resume(&part_md->queue);
2836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 return 0;
2839}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840#endif
2841
Ulf Hansson0967edc2014-10-06 11:29:42 +02002842static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2843
Ulf Hansson96541ba2015-04-14 13:06:12 +02002844static struct mmc_driver mmc_driver = {
2845 .drv = {
2846 .name = "mmcblk",
2847 .pm = &mmc_blk_pm_ops,
2848 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 .probe = mmc_blk_probe,
2850 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002851 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852};
2853
2854static int __init mmc_blk_init(void)
2855{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002856 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Linus Walleij97548572017-09-20 10:02:00 +02002858 res = bus_register(&mmc_rpmb_bus_type);
2859 if (res < 0) {
2860 pr_err("mmcblk: could not register RPMB bus type\n");
2861 return res;
2862 }
2863 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
2864 if (res < 0) {
2865 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
2866 goto out_bus_unreg;
2867 }
2868
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002869 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2870 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2871
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002872 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002873
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002874 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2875 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002876 goto out_chrdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002878 res = mmc_register_driver(&mmc_driver);
2879 if (res)
Linus Walleij97548572017-09-20 10:02:00 +02002880 goto out_blkdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002882 return 0;
Linus Walleij97548572017-09-20 10:02:00 +02002883
2884out_blkdev_unreg:
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002885 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002886out_chrdev_unreg:
2887 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
2888out_bus_unreg:
2889 bus_unregister(&mmc_rpmb_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return res;
2891}
2892
2893static void __exit mmc_blk_exit(void)
2894{
2895 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002896 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Walleij97548572017-09-20 10:02:00 +02002897 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900module_init(mmc_blk_init);
2901module_exit(mmc_blk_exit);
2902
2903MODULE_LICENSE("GPL");
2904MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2905