blob: 4920ea1ece38a9b6b71c0d724290726fefcf1acf [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>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010050#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010051#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010052#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010054#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080055#include "quirks.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010056#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000058MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040059#ifdef MODULE_PARAM_PREFIX
60#undef MODULE_PARAM_PREFIX
61#endif
62#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010063
Trey Ramsay8fee4762012-11-16 09:31:41 -060064#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020070static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040071
72/*
73 * The defaults come from config options but can be overriden by module
74 * or bootarg options.
75 */
76static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
77
78/*
79 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000080 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020081 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040082 */
83static int max_devices;
84
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085#define MAX_DEVICES 256
86
Ulf Hanssonb10fa992016-04-07 14:36:46 +020087static DEFINE_IDA(mmc_blk_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * There is one mmc_blk_data per slot.
91 */
92struct mmc_blk_data {
93 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070094 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct gendisk *disk;
96 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050097 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050099 unsigned int flags;
100#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000104 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500105 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300106 unsigned int reset_done;
107#define MMC_BLK_READ BIT(0)
108#define MMC_BLK_WRITE BIT(1)
109#define MMC_BLK_DISCARD BIT(2)
110#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111
112 /*
113 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200114 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 * track of the current selected device partition.
116 */
117 unsigned int part_curr;
118 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 struct device_attribute power_ro_lock;
120 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Arjan van de Vena621aae2006-01-12 18:43:35 +0000123static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400125module_param(perdev_minors, int, 0444);
126MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
127
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200128static inline int mmc_blk_part_switch(struct mmc_card *card,
129 struct mmc_blk_data *md);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
132{
133 struct mmc_blk_data *md;
134
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 md = disk->private_data;
137 if (md && md->usage == 0)
138 md = NULL;
139 if (md)
140 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000141 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return md;
144}
145
Andrei Warkentin371a6892011-04-11 18:10:25 -0500146static inline int mmc_get_devidx(struct gendisk *disk)
147{
Colin Cross382c55f2015-10-22 10:00:41 -0700148 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500149 return devidx;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static void mmc_blk_put(struct mmc_blk_data *md)
153{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000154 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 md->usage--;
156 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500157 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800158 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100159 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 kfree(md);
162 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000163 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Johan Rudholmadd710e2011-12-02 08:51:06 +0100166static ssize_t power_ro_lock_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
168{
169 int ret;
170 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
171 struct mmc_card *card = md->queue.card;
172 int locked = 0;
173
174 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
175 locked = 2;
176 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
177 locked = 1;
178
179 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
180
Tomas Winkler9098f842015-07-16 15:50:45 +0200181 mmc_blk_put(md);
182
Johan Rudholmadd710e2011-12-02 08:51:06 +0100183 return ret;
184}
185
186static ssize_t power_ro_lock_store(struct device *dev,
187 struct device_attribute *attr, const char *buf, size_t count)
188{
189 int ret;
190 struct mmc_blk_data *md, *part_md;
191 struct mmc_card *card;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200192 struct mmc_queue *mq;
193 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100194 unsigned long set;
195
196 if (kstrtoul(buf, 0, &set))
197 return -EINVAL;
198
199 if (set != 1)
200 return count;
201
202 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200203 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100204 card = md->queue.card;
205
Linus Walleij0493f6f2017-05-19 15:37:30 +0200206 /* Dispatch locking to the block layer */
207 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
208 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
209 blk_execute_rq(mq->queue, NULL, req, 0);
210 ret = req_to_mmc_queue_req(req)->drv_op_result;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100211
212 if (!ret) {
213 pr_info("%s: Locking boot partition ro until next power on\n",
214 md->disk->disk_name);
215 set_disk_ro(md->disk, 1);
216
217 list_for_each_entry(part_md, &md->part, part)
218 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
219 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
220 set_disk_ro(part_md->disk, 1);
221 }
222 }
223
224 mmc_blk_put(md);
225 return count;
226}
227
Andrei Warkentin371a6892011-04-11 18:10:25 -0500228static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
230{
231 int ret;
232 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
233
Baruch Siach0031a982014-09-22 10:12:51 +0300234 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500235 get_disk_ro(dev_to_disk(dev)) ^
236 md->read_only);
237 mmc_blk_put(md);
238 return ret;
239}
240
241static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
243{
244 int ret;
245 char *end;
246 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247 unsigned long set = simple_strtoul(buf, &end, 0);
248 if (end == buf) {
249 ret = -EINVAL;
250 goto out;
251 }
252
253 set_disk_ro(dev_to_disk(dev), set || md->read_only);
254 ret = count;
255out:
256 mmc_blk_put(md);
257 return ret;
258}
259
Al Viroa5a15612008-03-02 10:33:30 -0500260static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Al Viroa5a15612008-03-02 10:33:30 -0500262 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int ret = -ENXIO;
264
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200265 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (md) {
267 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500268 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700270
Al Viroa5a15612008-03-02 10:33:30 -0500271 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700272 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700273 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200276 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return ret;
279}
280
Al Virodb2a1442013-05-05 21:52:57 -0400281static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Al Viroa5a15612008-03-02 10:33:30 -0500283 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200285 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200287 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800291mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800293 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
294 geo->heads = 4;
295 geo->sectors = 16;
296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
John Calixtocb87ea22011-04-26 18:56:29 -0400299struct mmc_blk_ioc_data {
300 struct mmc_ioc_cmd ic;
301 unsigned char *buf;
302 u64 buf_bytes;
303};
304
305static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
306 struct mmc_ioc_cmd __user *user)
307{
308 struct mmc_blk_ioc_data *idata;
309 int err;
310
yalin wang1ff89502015-11-12 19:27:11 +0800311 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400312 if (!idata) {
313 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400314 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400315 }
316
317 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
318 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400319 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400320 }
321
322 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
323 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
324 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400325 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400326 }
327
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300328 if (!idata->buf_bytes) {
329 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100330 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300331 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100332
yalin wang1ff89502015-11-12 19:27:11 +0800333 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400334 if (!idata->buf) {
335 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400336 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400337 }
338
339 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
340 idata->ic.data_ptr, idata->buf_bytes)) {
341 err = -EFAULT;
342 goto copy_err;
343 }
344
345 return idata;
346
347copy_err:
348 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400349idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400350 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400351out:
John Calixtocb87ea22011-04-26 18:56:29 -0400352 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400353}
354
Jon Huntera5f57742015-09-22 10:27:53 +0100355static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
356 struct mmc_blk_ioc_data *idata)
357{
358 struct mmc_ioc_cmd *ic = &idata->ic;
359
360 if (copy_to_user(&(ic_ptr->response), ic->response,
361 sizeof(ic->response)))
362 return -EFAULT;
363
364 if (!idata->ic.write_flag) {
365 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
366 idata->buf, idata->buf_bytes))
367 return -EFAULT;
368 }
369
370 return 0;
371}
372
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200373static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
374 u32 retries_max)
375{
376 int err;
377 u32 retry_count = 0;
378
379 if (!status || !retries_max)
380 return -EINVAL;
381
382 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200383 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200384 if (err)
385 break;
386
387 if (!R1_STATUS(*status) &&
388 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
389 break; /* RPMB programming operation complete */
390
391 /*
392 * Rechedule to give the MMC device a chance to continue
393 * processing the previous command without being polled too
394 * frequently.
395 */
396 usleep_range(1000, 5000);
397 } while (++retry_count < retries_max);
398
399 if (retry_count == retries_max)
400 err = -EPERM;
401
402 return err;
403}
404
Maya Erez775a9362013-04-18 15:41:55 +0300405static int ioctl_do_sanitize(struct mmc_card *card)
406{
407 int err;
408
Ulf Hanssona2d10862013-12-16 14:37:26 +0100409 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300410 pr_warn("%s: %s - SANITIZE is not supported\n",
411 mmc_hostname(card->host), __func__);
412 err = -EOPNOTSUPP;
413 goto out;
414 }
415
416 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
417 mmc_hostname(card->host), __func__);
418
419 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
420 EXT_CSD_SANITIZE_START, 1,
421 MMC_SANITIZE_REQ_TIMEOUT);
422
423 if (err)
424 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
425 mmc_hostname(card->host), __func__, err);
426
427 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
428 __func__);
429out:
430 return err;
431}
432
Jon Huntera5f57742015-09-22 10:27:53 +0100433static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
434 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400435{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900436 struct mmc_command cmd = {};
437 struct mmc_data data = {};
438 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400439 struct scatterlist sg;
440 int err;
Linus Walleij829043c2017-05-18 11:29:33 +0200441 bool is_rpmb = false;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200442 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400443
Jon Huntera5f57742015-09-22 10:27:53 +0100444 if (!card || !md || !idata)
445 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400446
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200447 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
448 is_rpmb = true;
449
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100450 cmd.opcode = idata->ic.opcode;
451 cmd.arg = idata->ic.arg;
452 cmd.flags = idata->ic.flags;
453
454 if (idata->buf_bytes) {
455 data.sg = &sg;
456 data.sg_len = 1;
457 data.blksz = idata->ic.blksz;
458 data.blocks = idata->ic.blocks;
459
460 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
461
462 if (idata->ic.write_flag)
463 data.flags = MMC_DATA_WRITE;
464 else
465 data.flags = MMC_DATA_READ;
466
467 /* data.flags must already be set before doing this. */
468 mmc_set_data_timeout(&data, card);
469
470 /* Allow overriding the timeout_ns for empirical tuning. */
471 if (idata->ic.data_timeout_ns)
472 data.timeout_ns = idata->ic.data_timeout_ns;
473
474 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
475 /*
476 * Pretend this is a data transfer and rely on the
477 * host driver to compute timeout. When all host
478 * drivers support cmd.cmd_timeout for R1B, this
479 * can be changed to:
480 *
481 * mrq.data = NULL;
482 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
483 */
484 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
485 }
486
487 mrq.data = &data;
488 }
489
490 mrq.cmd = &cmd;
491
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200492 err = mmc_blk_part_switch(card, md);
493 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100494 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200495
John Calixtocb87ea22011-04-26 18:56:29 -0400496 if (idata->ic.is_acmd) {
497 err = mmc_app_cmd(card->host, card);
498 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100499 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400500 }
501
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200502 if (is_rpmb) {
503 err = mmc_set_blockcount(card, data.blocks,
504 idata->ic.write_flag & (1 << 31));
505 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100506 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507 }
508
Yaniv Gardia82e4842013-06-05 14:13:08 +0300509 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
510 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300511 err = ioctl_do_sanitize(card);
512
513 if (err)
514 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
515 __func__, err);
516
Jon Huntera5f57742015-09-22 10:27:53 +0100517 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300518 }
519
John Calixtocb87ea22011-04-26 18:56:29 -0400520 mmc_wait_for_req(card->host, &mrq);
521
522 if (cmd.error) {
523 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
524 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100525 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400526 }
527 if (data.error) {
528 dev_err(mmc_dev(card->host), "%s: data error %d\n",
529 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100530 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400531 }
532
533 /*
534 * According to the SD specs, some commands require a delay after
535 * issuing the command.
536 */
537 if (idata->ic.postsleep_min_us)
538 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
539
Jon Huntera5f57742015-09-22 10:27:53 +0100540 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400541
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200542 if (is_rpmb) {
543 /*
544 * Ensure RPMB command has completed by polling CMD13
545 * "Send Status".
546 */
547 err = ioctl_rpmb_card_status_poll(card, &status, 5);
548 if (err)
549 dev_err(mmc_dev(card->host),
550 "%s: Card Status=0x%08X, error %d\n",
551 __func__, status, err);
552 }
553
Jon Huntera5f57742015-09-22 10:27:53 +0100554 return err;
555}
556
557static int mmc_blk_ioctl_cmd(struct block_device *bdev,
558 struct mmc_ioc_cmd __user *ic_ptr)
559{
560 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200561 struct mmc_blk_ioc_data *idatas[1];
Jon Huntera5f57742015-09-22 10:27:53 +0100562 struct mmc_blk_data *md;
Linus Walleij614f0382017-05-18 11:29:34 +0200563 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100564 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700565 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200566 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100567
Shawn Lin83c742c2016-03-16 18:15:47 +0800568 /*
569 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
570 * whole block device, not on a partition. This prevents overspray
571 * between sibling partitions.
572 */
573 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
574 return -EPERM;
575
Jon Huntera5f57742015-09-22 10:27:53 +0100576 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
577 if (IS_ERR(idata))
578 return PTR_ERR(idata);
579
580 md = mmc_blk_get(bdev->bd_disk);
581 if (!md) {
582 err = -EINVAL;
583 goto cmd_err;
584 }
585
586 card = md->queue.card;
587 if (IS_ERR(card)) {
588 err = PTR_ERR(card);
589 goto cmd_done;
590 }
591
Linus Walleij614f0382017-05-18 11:29:34 +0200592 /*
593 * Dispatch the ioctl() into the block request queue.
594 */
595 mq = &md->queue;
596 req = blk_get_request(mq->queue,
597 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
598 __GFP_RECLAIM);
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200599 idatas[0] = idata;
Linus Walleij02166a02017-05-19 15:37:28 +0200600 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200601 req_to_mmc_queue_req(req)->idata = idatas;
602 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200603 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200604 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700605 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200606 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100607
John Calixtocb87ea22011-04-26 18:56:29 -0400608cmd_done:
609 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300610cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400611 kfree(idata->buf);
612 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700613 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400614}
615
Jon Huntera5f57742015-09-22 10:27:53 +0100616static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
617 struct mmc_ioc_multi_cmd __user *user)
618{
619 struct mmc_blk_ioc_data **idata = NULL;
620 struct mmc_ioc_cmd __user *cmds = user->cmds;
621 struct mmc_card *card;
622 struct mmc_blk_data *md;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200623 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700624 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100625 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200626 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100627
Shawn Lin83c742c2016-03-16 18:15:47 +0800628 /*
629 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
630 * whole block device, not on a partition. This prevents overspray
631 * between sibling partitions.
632 */
633 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
634 return -EPERM;
635
Jon Huntera5f57742015-09-22 10:27:53 +0100636 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
637 sizeof(num_of_cmds)))
638 return -EFAULT;
639
640 if (num_of_cmds > MMC_IOC_MAX_CMDS)
641 return -EINVAL;
642
643 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
644 if (!idata)
645 return -ENOMEM;
646
647 for (i = 0; i < num_of_cmds; i++) {
648 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
649 if (IS_ERR(idata[i])) {
650 err = PTR_ERR(idata[i]);
651 num_of_cmds = i;
652 goto cmd_err;
653 }
654 }
655
656 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800657 if (!md) {
658 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100659 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800660 }
Jon Huntera5f57742015-09-22 10:27:53 +0100661
662 card = md->queue.card;
663 if (IS_ERR(card)) {
664 err = PTR_ERR(card);
665 goto cmd_done;
666 }
667
Jon Huntera5f57742015-09-22 10:27:53 +0100668
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200669 /*
670 * Dispatch the ioctl()s into the block request queue.
671 */
672 mq = &md->queue;
673 req = blk_get_request(mq->queue,
674 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
675 __GFP_RECLAIM);
Linus Walleij02166a02017-05-19 15:37:28 +0200676 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200677 req_to_mmc_queue_req(req)->idata = idata;
678 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
679 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200680 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100681
682 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700683 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100684 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100685
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200686 blk_put_request(req);
687
Jon Huntera5f57742015-09-22 10:27:53 +0100688cmd_done:
689 mmc_blk_put(md);
690cmd_err:
691 for (i = 0; i < num_of_cmds; i++) {
692 kfree(idata[i]->buf);
693 kfree(idata[i]);
694 }
695 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700696 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100697}
698
John Calixtocb87ea22011-04-26 18:56:29 -0400699static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
700 unsigned int cmd, unsigned long arg)
701{
Jon Huntera5f57742015-09-22 10:27:53 +0100702 switch (cmd) {
703 case MMC_IOC_CMD:
704 return mmc_blk_ioctl_cmd(bdev,
705 (struct mmc_ioc_cmd __user *)arg);
706 case MMC_IOC_MULTI_CMD:
707 return mmc_blk_ioctl_multi_cmd(bdev,
708 (struct mmc_ioc_multi_cmd __user *)arg);
709 default:
710 return -EINVAL;
711 }
John Calixtocb87ea22011-04-26 18:56:29 -0400712}
713
714#ifdef CONFIG_COMPAT
715static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
716 unsigned int cmd, unsigned long arg)
717{
718 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
719}
720#endif
721
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700722static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500723 .open = mmc_blk_open,
724 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800725 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400727 .ioctl = mmc_blk_ioctl,
728#ifdef CONFIG_COMPAT
729 .compat_ioctl = mmc_blk_compat_ioctl,
730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
Adrian Hunter025e3d52017-03-13 14:36:39 +0200733static int mmc_blk_part_switch_pre(struct mmc_card *card,
734 unsigned int part_type)
735{
736 int ret = 0;
737
738 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
739 if (card->ext_csd.cmdq_en) {
740 ret = mmc_cmdq_disable(card);
741 if (ret)
742 return ret;
743 }
744 mmc_retune_pause(card->host);
745 }
746
747 return ret;
748}
749
750static int mmc_blk_part_switch_post(struct mmc_card *card,
751 unsigned int part_type)
752{
753 int ret = 0;
754
755 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
756 mmc_retune_unpause(card->host);
757 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
758 ret = mmc_cmdq_enable(card);
759 }
760
761 return ret;
762}
763
Andrei Warkentin371a6892011-04-11 18:10:25 -0500764static inline int mmc_blk_part_switch(struct mmc_card *card,
765 struct mmc_blk_data *md)
766{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200767 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200768 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300769
Andrei Warkentin371a6892011-04-11 18:10:25 -0500770 if (main_md->part_curr == md->part_type)
771 return 0;
772
773 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300774 u8 part_config = card->ext_csd.part_config;
775
Adrian Hunter025e3d52017-03-13 14:36:39 +0200776 ret = mmc_blk_part_switch_pre(card, md->part_type);
777 if (ret)
778 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300779
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300780 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
781 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500782
783 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300784 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500785 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300786 if (ret) {
Adrian Hunter025e3d52017-03-13 14:36:39 +0200787 mmc_blk_part_switch_post(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500788 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300789 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300790
791 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300792
Adrian Hunter025e3d52017-03-13 14:36:39 +0200793 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300794 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500795
796 main_md->part_curr = md->part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200797 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500798}
799
Linus Walleij169f03a2017-02-01 13:47:57 +0100800static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700801{
802 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100803 u32 result;
804 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700805
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900806 struct mmc_request mrq = {};
807 struct mmc_command cmd = {};
808 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700809
810 struct scatterlist sg;
811
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700812 cmd.opcode = MMC_APP_CMD;
813 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700814 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700815
816 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700817 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100818 return err;
David Brownell7213d172007-08-08 09:10:23 -0700819 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100820 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700821
822 memset(&cmd, 0, sizeof(struct mmc_command));
823
824 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
825 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700826 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700827
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700828 data.blksz = 4;
829 data.blocks = 1;
830 data.flags = MMC_DATA_READ;
831 data.sg = &sg;
832 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530833 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700834
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700835 mrq.cmd = &cmd;
836 mrq.data = &data;
837
Ben Dooks051913d2009-06-08 23:33:57 +0100838 blocks = kmalloc(4, GFP_KERNEL);
839 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100840 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100841
842 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700843
844 mmc_wait_for_req(card->host, &mrq);
845
Ben Dooks051913d2009-06-08 23:33:57 +0100846 result = ntohl(*blocks);
847 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700848
Ben Dooks051913d2009-06-08 23:33:57 +0100849 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100850 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100851
Linus Walleij169f03a2017-02-01 13:47:57 +0100852 *written_blocks = result;
853
854 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700855}
856
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100857static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100858 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100859{
860 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
861 int err = 0;
862 u32 status;
863
864 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200865 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100866 if (err) {
867 pr_err("%s: error %d requesting status\n",
868 req->rq_disk->disk_name, err);
869 return err;
870 }
871
872 if (status & R1_ERROR) {
873 pr_err("%s: %s: error sending status cmd, status %#x\n",
874 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100875 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100876 }
877
Ulf Hansson95a91292014-01-29 13:11:27 +0100878 /* We may rely on the host hw to handle busy detection.*/
879 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
880 hw_busy_detect)
881 break;
882
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100883 /*
884 * Timeout if the device never becomes ready for data and never
885 * leaves the program state.
886 */
887 if (time_after(jiffies, timeout)) {
888 pr_err("%s: Card stuck in programming state! %s %s\n",
889 mmc_hostname(card->host),
890 req->rq_disk->disk_name, __func__);
891 return -ETIMEDOUT;
892 }
893
894 /*
895 * Some cards mishandle the status bits,
896 * so make sure to check both the busy
897 * indication and the card state.
898 */
899 } while (!(status & R1_READY_FOR_DATA) ||
900 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
901
902 return err;
903}
904
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100905static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100906 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100907{
908 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900909 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100910 int err;
911 bool use_r1b_resp = rq_data_dir(req) == WRITE;
912
913 /*
914 * Normally we use R1B responses for WRITE, but in cases where the host
915 * has specified a max_busy_timeout we need to validate it. A failure
916 * means we need to prevent the host from doing hw busy detection, which
917 * is done by converting to a R1 response instead.
918 */
919 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
920 use_r1b_resp = false;
921
922 cmd.opcode = MMC_STOP_TRANSMISSION;
923 if (use_r1b_resp) {
924 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
925 cmd.busy_timeout = timeout_ms;
926 } else {
927 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
928 }
929
930 err = mmc_wait_for_cmd(host, &cmd, 5);
931 if (err)
932 return err;
933
934 *stop_status = cmd.resp[0];
935
936 /* No need to check card status in case of READ. */
937 if (rq_data_dir(req) == READ)
938 return 0;
939
940 if (!mmc_host_is_spi(host) &&
941 (*stop_status & R1_ERROR)) {
942 pr_err("%s: %s: general error sending stop command, resp %#x\n",
943 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100944 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100945 }
946
947 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
948}
949
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530950#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100951#define ERR_RETRY 2
952#define ERR_ABORT 1
953#define ERR_CONTINUE 0
954
955static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
956 bool status_valid, u32 status)
957{
958 switch (error) {
959 case -EILSEQ:
960 /* response crc error, retry the r/w cmd */
961 pr_err("%s: %s sending %s command, card status %#x\n",
962 req->rq_disk->disk_name, "response CRC error",
963 name, status);
964 return ERR_RETRY;
965
966 case -ETIMEDOUT:
967 pr_err("%s: %s sending %s command, card status %#x\n",
968 req->rq_disk->disk_name, "timed out", name, status);
969
970 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530971 if (!status_valid) {
972 pr_err("%s: status not valid, retrying timeout\n",
973 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100974 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530975 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100976
977 /*
978 * If it was a r/w cmd crc error, or illegal command
979 * (eg, issued in wrong state) then retry - we should
980 * have corrected the state problem above.
981 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530982 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
983 pr_err("%s: command error, retrying timeout\n",
984 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100985 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530986 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100987
988 /* Otherwise abort the command */
989 return ERR_ABORT;
990
991 default:
992 /* We don't understand the error code the driver gave us */
993 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
994 req->rq_disk->disk_name, error, status);
995 return ERR_ABORT;
996 }
997}
998
999/*
1000 * Initial r/w and stop cmd error recovery.
1001 * We don't know whether the card received the r/w cmd or not, so try to
1002 * restore things back to a sane state. Essentially, we do this as follows:
1003 * - Obtain card status. If the first attempt to obtain card status fails,
1004 * the status word will reflect the failed status cmd, not the failed
1005 * r/w cmd. If we fail to obtain card status, it suggests we can no
1006 * longer communicate with the card.
1007 * - Check the card state. If the card received the cmd but there was a
1008 * transient problem with the response, it might still be in a data transfer
1009 * mode. Try to send it a stop command. If this fails, we can't recover.
1010 * - If the r/w cmd failed due to a response CRC error, it was probably
1011 * transient, so retry the cmd.
1012 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1013 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1014 * illegal cmd, retry.
1015 * Otherwise we don't understand what happened, so abort.
1016 */
1017static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001018 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001019{
1020 bool prev_cmd_status_valid = true;
1021 u32 status, stop_status = 0;
1022 int err, retry;
1023
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301024 if (mmc_card_removed(card))
1025 return ERR_NOMEDIUM;
1026
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001027 /*
1028 * Try to get card status which indicates both the card state
1029 * and why there was no response. If the first attempt fails,
1030 * we can't be sure the returned status is for the r/w command.
1031 */
1032 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001033 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001034 if (!err)
1035 break;
1036
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001037 /* Re-tune if needed */
1038 mmc_retune_recheck(card->host);
1039
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001040 prev_cmd_status_valid = false;
1041 pr_err("%s: error %d sending status command, %sing\n",
1042 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1043 }
1044
1045 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301046 if (err) {
1047 /* Check if the card is removed */
1048 if (mmc_detect_card_removed(card->host))
1049 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001050 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301051 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001052
Adrian Hunter67716322011-08-29 16:42:15 +03001053 /* Flag ECC errors */
1054 if ((status & R1_CARD_ECC_FAILED) ||
1055 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1056 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001057 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001058
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001059 /* Flag General errors */
1060 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1061 if ((status & R1_ERROR) ||
1062 (brq->stop.resp[0] & R1_ERROR)) {
1063 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1064 req->rq_disk->disk_name, __func__,
1065 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001066 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001067 }
1068
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069 /*
1070 * Check the current card state. If it is in some data transfer
1071 * mode, tell it to stop (and hopefully transition back to TRAN.)
1072 */
1073 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1074 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001075 err = send_stop(card,
1076 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1077 req, gen_err, &stop_status);
1078 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001079 pr_err("%s: error %d sending stop command\n",
1080 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001081 /*
1082 * If the stop cmd also timed out, the card is probably
1083 * not present, so abort. Other errors are bad news too.
1084 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001085 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001086 }
1087
Adrian Hunter67716322011-08-29 16:42:15 +03001088 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001089 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001090 }
1091
1092 /* Check for set block count errors */
1093 if (brq->sbc.error)
1094 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1095 prev_cmd_status_valid, status);
1096
1097 /* Check for r/w command errors */
1098 if (brq->cmd.error)
1099 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1100 prev_cmd_status_valid, status);
1101
Adrian Hunter67716322011-08-29 16:42:15 +03001102 /* Data errors */
1103 if (!brq->stop.error)
1104 return ERR_CONTINUE;
1105
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001106 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001107 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 +01001108 req->rq_disk->disk_name, brq->stop.error,
1109 brq->cmd.resp[0], status);
1110
1111 /*
1112 * Subsitute in our own stop status as this will give the error
1113 * state which happened during the execution of the r/w command.
1114 */
1115 if (stop_status) {
1116 brq->stop.resp[0] = stop_status;
1117 brq->stop.error = 0;
1118 }
1119 return ERR_CONTINUE;
1120}
1121
Adrian Hunter67716322011-08-29 16:42:15 +03001122static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1123 int type)
1124{
1125 int err;
1126
1127 if (md->reset_done & type)
1128 return -EEXIST;
1129
1130 md->reset_done |= type;
1131 err = mmc_hw_reset(host);
1132 /* Ensure we switch back to the correct partition */
1133 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001134 struct mmc_blk_data *main_md =
1135 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001136 int part_err;
1137
1138 main_md->part_curr = main_md->part_type;
1139 part_err = mmc_blk_part_switch(host->card, md);
1140 if (part_err) {
1141 /*
1142 * We have failed to get back into the correct
1143 * partition, so we need to abort the whole request.
1144 */
1145 return -ENODEV;
1146 }
1147 }
1148 return err;
1149}
1150
1151static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1152{
1153 md->reset_done &= ~type;
1154}
1155
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001156int mmc_access_rpmb(struct mmc_queue *mq)
1157{
Linus Walleij7db30282016-11-18 13:36:15 +01001158 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001159 /*
1160 * If this is a RPMB partition access, return ture
1161 */
1162 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1163 return true;
1164
1165 return false;
1166}
1167
Linus Walleij5ec12392017-05-19 15:37:29 +02001168/*
1169 * The non-block commands come back from the block layer after it queued it and
1170 * processed it with all other requests and then they get issued in this
1171 * function.
1172 */
1173static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1174{
1175 struct mmc_queue_req *mq_rq;
1176 struct mmc_card *card = mq->card;
1177 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001178 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001179 int i;
1180
1181 mq_rq = req_to_mmc_queue_req(req);
1182
1183 switch (mq_rq->drv_op) {
1184 case MMC_DRV_OP_IOCTL:
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001185 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij0493f6f2017-05-19 15:37:30 +02001186 ret = __mmc_blk_ioctl_cmd(card, md, mq_rq->idata[i]);
1187 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001188 break;
1189 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001190 /* Always switch back to main area after RPMB access */
1191 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1192 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +02001193 break;
1194 case MMC_DRV_OP_BOOT_WP:
1195 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1196 card->ext_csd.boot_ro_lock |
1197 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1198 card->ext_csd.part_time);
1199 if (ret)
1200 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1201 md->disk->disk_name, ret);
1202 else
1203 card->ext_csd.boot_ro_lock |=
1204 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001205 break;
1206 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001207 pr_err("%s: unknown driver specific operation\n",
1208 md->disk->disk_name);
1209 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001210 break;
1211 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001212 mq_rq->drv_op_result = ret;
1213 blk_end_request_all(req, ret);
Linus Walleij5ec12392017-05-19 15:37:29 +02001214}
1215
Linus Walleijdf061582017-01-24 11:17:57 +01001216static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001217{
Linus Walleij7db30282016-11-18 13:36:15 +01001218 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001219 struct mmc_card *card = md->queue.card;
1220 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001221 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001222 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001223
Adrian Hunterbd788c92010-08-11 14:17:47 -07001224 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001225 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001226 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001227 }
1228
1229 from = blk_rq_pos(req);
1230 nr = blk_rq_sectors(req);
1231
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001232 if (mmc_can_discard(card))
1233 arg = MMC_DISCARD_ARG;
1234 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001235 arg = MMC_TRIM_ARG;
1236 else
1237 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001238 do {
1239 err = 0;
1240 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1241 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1242 INAND_CMD38_ARG_EXT_CSD,
1243 arg == MMC_TRIM_ARG ?
1244 INAND_CMD38_ARG_TRIM :
1245 INAND_CMD38_ARG_ERASE,
1246 0);
1247 }
1248 if (!err)
1249 err = mmc_erase(card, from, nr, arg);
1250 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001251 if (err)
1252 status = BLK_STS_IOERR;
1253 else
Adrian Hunter67716322011-08-29 16:42:15 +03001254 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001255fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001256 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001257}
1258
Linus Walleijdf061582017-01-24 11:17:57 +01001259static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001260 struct request *req)
1261{
Linus Walleij7db30282016-11-18 13:36:15 +01001262 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001263 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001264 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001265 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001266 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001267
Maya Erez775a9362013-04-18 15:41:55 +03001268 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001269 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001270 goto out;
1271 }
1272
1273 from = blk_rq_pos(req);
1274 nr = blk_rq_sectors(req);
1275
Maya Erez775a9362013-04-18 15:41:55 +03001276 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1277 arg = MMC_SECURE_TRIM1_ARG;
1278 else
1279 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001280
Adrian Hunter67716322011-08-29 16:42:15 +03001281retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001282 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1283 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1284 INAND_CMD38_ARG_EXT_CSD,
1285 arg == MMC_SECURE_TRIM1_ARG ?
1286 INAND_CMD38_ARG_SECTRIM1 :
1287 INAND_CMD38_ARG_SECERASE,
1288 0);
1289 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001290 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001291 }
Adrian Hunter28302812012-04-05 14:45:48 +03001292
Adrian Hunter49804542010-08-11 14:17:50 -07001293 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001294 if (err == -EIO)
1295 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001296 if (err) {
1297 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001298 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001299 }
Adrian Hunter28302812012-04-05 14:45:48 +03001300
1301 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001302 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1303 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1304 INAND_CMD38_ARG_EXT_CSD,
1305 INAND_CMD38_ARG_SECTRIM2,
1306 0);
1307 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001308 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001309 }
Adrian Hunter28302812012-04-05 14:45:48 +03001310
Adrian Hunter49804542010-08-11 14:17:50 -07001311 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001312 if (err == -EIO)
1313 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001314 if (err) {
1315 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001316 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001317 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001318 }
Adrian Hunter28302812012-04-05 14:45:48 +03001319
Adrian Hunter28302812012-04-05 14:45:48 +03001320out_retry:
1321 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001322 goto retry;
1323 if (!err)
1324 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001325out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001326 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001327}
1328
Linus Walleijdf061582017-01-24 11:17:57 +01001329static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001330{
Linus Walleij7db30282016-11-18 13:36:15 +01001331 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001332 struct mmc_card *card = md->queue.card;
1333 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001334
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001335 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001336 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001337}
1338
1339/*
1340 * Reformat current write as a reliable write, supporting
1341 * both legacy and the enhanced reliable write MMC cards.
1342 * In each transfer we'll handle only as much as a single
1343 * reliable write can handle, thus finish the request in
1344 * partial completions.
1345 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001346static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1347 struct mmc_card *card,
1348 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001349{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001350 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1351 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001352 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001353 brq->data.blocks = 1;
1354
1355 if (brq->data.blocks > card->ext_csd.rel_sectors)
1356 brq->data.blocks = card->ext_csd.rel_sectors;
1357 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1358 brq->data.blocks = 1;
1359 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001360}
1361
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001362#define CMD_ERRORS \
1363 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1364 R1_ADDRESS_ERROR | /* Misaligned address */ \
1365 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1366 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001367 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001368 R1_CC_ERROR | /* Card controller error */ \
1369 R1_ERROR) /* General/unknown error */
1370
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001371static bool mmc_blk_has_cmd_err(struct mmc_command *cmd)
1372{
1373 if (!cmd->error && cmd->resp[0] & CMD_ERRORS)
1374 cmd->error = -EIO;
1375
1376 return cmd->error;
1377}
1378
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001379static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1380 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001381{
Per Forlinee8a43a2011-07-01 18:55:33 +02001382 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001383 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001384 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001385 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001386 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001387 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001388 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001389
1390 /*
1391 * sbc.error indicates a problem with the set block count
1392 * command. No data will have been transferred.
1393 *
1394 * cmd.error indicates a problem with the r/w command. No
1395 * data will have been transferred.
1396 *
1397 * stop.error indicates a problem with the stop command. Data
1398 * may have been transferred, or may still be transferring.
1399 */
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001400 if (brq->sbc.error || brq->cmd.error || mmc_blk_has_cmd_err(&brq->stop) ||
Adrian Hunter67716322011-08-29 16:42:15 +03001401 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001402 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001403 case ERR_RETRY:
1404 return MMC_BLK_RETRY;
1405 case ERR_ABORT:
1406 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301407 case ERR_NOMEDIUM:
1408 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001409 case ERR_CONTINUE:
1410 break;
1411 }
1412 }
1413
1414 /*
1415 * Check for errors relating to the execution of the
1416 * initial command - such as address errors. No data
1417 * has been transferred.
1418 */
1419 if (brq->cmd.resp[0] & CMD_ERRORS) {
1420 pr_err("%s: r/w command failed, status = %#x\n",
1421 req->rq_disk->disk_name, brq->cmd.resp[0]);
1422 return MMC_BLK_ABORT;
1423 }
1424
1425 /*
1426 * Everything else is either success, or a data error of some
1427 * kind. If it was a write, we may have transitioned to
1428 * program mode, which we have to wait for it to complete.
1429 */
1430 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001431 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001432
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001433 /* Check stop command response */
1434 if (brq->stop.resp[0] & R1_ERROR) {
1435 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1436 req->rq_disk->disk_name, __func__,
1437 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001438 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001439 }
1440
Ulf Hansson95a91292014-01-29 13:11:27 +01001441 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1442 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001443 if (err)
1444 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001445 }
1446
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001447 /* if general error occurs, retry the write operation. */
1448 if (gen_err) {
1449 pr_warn("%s: retrying write for general error\n",
1450 req->rq_disk->disk_name);
1451 return MMC_BLK_RETRY;
1452 }
1453
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001454 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1455 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001456 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001457 pr_debug("%s: retrying because a re-tune was needed\n",
1458 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001459 brq->retune_retry_done = 1;
1460 return MMC_BLK_RETRY;
1461 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001462 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 +02001463 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001464 (unsigned)blk_rq_pos(req),
1465 (unsigned)blk_rq_sectors(req),
1466 brq->cmd.resp[0], brq->stop.resp[0]);
1467
1468 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001469 if (ecc_err)
1470 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001471 return MMC_BLK_DATA_ERR;
1472 } else {
1473 return MMC_BLK_CMD_ERR;
1474 }
1475 }
1476
Adrian Hunter67716322011-08-29 16:42:15 +03001477 if (!brq->data.bytes_xfered)
1478 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001479
Adrian Hunter67716322011-08-29 16:42:15 +03001480 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1481 return MMC_BLK_PARTIAL;
1482
1483 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001484}
1485
Adrian Hunterca5717f2017-03-13 14:36:41 +02001486static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1487 int disable_multi, bool *do_rel_wr,
1488 bool *do_data_tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001490 struct mmc_blk_data *md = mq->blkdata;
1491 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001492 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001493 struct request *req = mmc_queue_req_to_req(mqrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001495 /*
1496 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001497 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001498 */
Adrian Hunterca5717f2017-03-13 14:36:41 +02001499 *do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1500 rq_data_dir(req) == WRITE &&
1501 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001502
Per Forlin54d49d72011-07-01 18:55:29 +02001503 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001504
Per Forlin54d49d72011-07-01 18:55:29 +02001505 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Per Forlin54d49d72011-07-01 18:55:29 +02001507 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1508 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001509
1510 if (rq_data_dir(req) == READ) {
1511 brq->data.flags = MMC_DATA_READ;
1512 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1513 } else {
1514 brq->data.flags = MMC_DATA_WRITE;
1515 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1516 }
1517
1518 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001519 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Per Forlin54d49d72011-07-01 18:55:29 +02001521 /*
1522 * The block layer doesn't support all sector count
1523 * restrictions, so we need to be prepared for too big
1524 * requests.
1525 */
1526 if (brq->data.blocks > card->host->max_blk_count)
1527 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001529 if (brq->data.blocks > 1) {
1530 /*
1531 * After a read error, we redo the request one sector
1532 * at a time in order to accurately determine which
1533 * sectors can be read successfully.
1534 */
1535 if (disable_multi)
1536 brq->data.blocks = 1;
1537
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001538 /*
1539 * Some controllers have HW issues while operating
1540 * in multiple I/O mode
1541 */
1542 if (card->host->ops->multi_io_quirk)
1543 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1544 (rq_data_dir(req) == READ) ?
1545 MMC_DATA_READ : MMC_DATA_WRITE,
1546 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001547 }
Per Forlin54d49d72011-07-01 18:55:29 +02001548
Adrian Hunterca5717f2017-03-13 14:36:41 +02001549 if (*do_rel_wr)
1550 mmc_apply_rel_rw(brq, card, req);
1551
1552 /*
1553 * Data tag is used only during writing meta data to speed
1554 * up write and any subsequent read of this meta data
1555 */
1556 *do_data_tag = card->ext_csd.data_tag_unit_size &&
1557 (req->cmd_flags & REQ_META) &&
1558 (rq_data_dir(req) == WRITE) &&
1559 ((brq->data.blocks * brq->data.blksz) >=
1560 card->ext_csd.data_tag_unit_size);
1561
1562 mmc_set_data_timeout(&brq->data, card);
1563
1564 brq->data.sg = mqrq->sg;
1565 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1566
1567 /*
1568 * Adjust the sg list so it is the same size as the
1569 * request.
1570 */
1571 if (brq->data.blocks != blk_rq_sectors(req)) {
1572 int i, data_size = brq->data.blocks << 9;
1573 struct scatterlist *sg;
1574
1575 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1576 data_size -= sg->length;
1577 if (data_size <= 0) {
1578 sg->length += data_size;
1579 i++;
1580 break;
1581 }
1582 }
1583 brq->data.sg_len = i;
1584 }
1585
1586 mqrq->areq.mrq = &brq->mrq;
1587
1588 mmc_queue_bounce_pre(mqrq);
1589}
1590
1591static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1592 struct mmc_card *card,
1593 int disable_multi,
1594 struct mmc_queue *mq)
1595{
1596 u32 readcmd, writecmd;
1597 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001598 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001599 struct mmc_blk_data *md = mq->blkdata;
1600 bool do_rel_wr, do_data_tag;
1601
1602 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1603
1604 brq->mrq.cmd = &brq->cmd;
1605
1606 brq->cmd.arg = blk_rq_pos(req);
1607 if (!mmc_card_blockaddr(card))
1608 brq->cmd.arg <<= 9;
1609 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1610
Per Forlin54d49d72011-07-01 18:55:29 +02001611 if (brq->data.blocks > 1 || do_rel_wr) {
1612 /* SPI multiblock writes terminate using a special
1613 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001614 */
Per Forlin54d49d72011-07-01 18:55:29 +02001615 if (!mmc_host_is_spi(card->host) ||
1616 rq_data_dir(req) == READ)
1617 brq->mrq.stop = &brq->stop;
1618 readcmd = MMC_READ_MULTIPLE_BLOCK;
1619 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1620 } else {
1621 brq->mrq.stop = NULL;
1622 readcmd = MMC_READ_SINGLE_BLOCK;
1623 writecmd = MMC_WRITE_BLOCK;
1624 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001625 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301626
1627 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001628 * Pre-defined multi-block transfers are preferable to
1629 * open ended-ones (and necessary for reliable writes).
1630 * However, it is not sufficient to just send CMD23,
1631 * and avoid the final CMD12, as on an error condition
1632 * CMD12 (stop) needs to be sent anyway. This, coupled
1633 * with Auto-CMD23 enhancements provided by some
1634 * hosts, means that the complexity of dealing
1635 * with this is best left to the host. If CMD23 is
1636 * supported by card and host, we'll fill sbc in and let
1637 * the host deal with handling it correctly. This means
1638 * that for hosts that don't expose MMC_CAP_CMD23, no
1639 * change of behavior will be observed.
1640 *
1641 * N.B: Some MMC cards experience perf degradation.
1642 * We'll avoid using CMD23-bounded multiblock writes for
1643 * these, while retaining features like reliable writes.
1644 */
Saugata Das42659002011-12-21 13:09:17 +05301645 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1646 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1647 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001648 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1649 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301650 (do_rel_wr ? (1 << 31) : 0) |
1651 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001652 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1653 brq->mrq.sbc = &brq->sbc;
1654 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001655
Linus Walleij74f5ba32017-02-01 13:47:55 +01001656 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Linus Walleij0e65f102017-02-01 13:47:58 +01001659static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1660 struct mmc_blk_request *brq, struct request *req,
1661 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001662{
Linus Walleij0e65f102017-02-01 13:47:58 +01001663 bool req_pending;
1664
Adrian Hunter67716322011-08-29 16:42:15 +03001665 /*
1666 * If this is an SD card and we're writing, we can first
1667 * mark the known good sectors as ok.
1668 *
1669 * If the card is not SD, we can still ok written sectors
1670 * as reported by the controller (which might be less than
1671 * the real number of written sectors, but never more).
1672 */
1673 if (mmc_card_sd(card)) {
1674 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001675 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001676
Linus Walleij169f03a2017-02-01 13:47:57 +01001677 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001678 if (err)
1679 req_pending = old_req_pending;
1680 else
1681 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001682 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001683 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001684 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001685 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001686}
1687
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001688static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1689 struct request *req,
1690 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001691{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001692 if (mmc_card_removed(card))
1693 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001694 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001695 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001696}
1697
Linus Walleijb2928e12017-02-01 13:47:54 +01001698/**
1699 * mmc_blk_rw_try_restart() - tries to restart the current async request
1700 * @mq: the queue with the card and host to restart
1701 * @req: a new request that want to be started after the current one
1702 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001703static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1704 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001705{
1706 if (!req)
1707 return;
1708
Linus Walleijb2928e12017-02-01 13:47:54 +01001709 /*
1710 * If the card was removed, just cancel everything and return.
1711 */
1712 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001713 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001714 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001715 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001716 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001717 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001718 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001719 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1720 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001721}
1722
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001723static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001724{
Linus Walleij7db30282016-11-18 13:36:15 +01001725 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001726 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001727 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001728 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001729 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001730 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001731 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001732 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001733 struct mmc_async_req *new_areq;
1734 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001735 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001736
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001737 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001738 mqrq_cur = req_to_mmc_queue_req(new_req);
1739 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001740 }
1741
1742 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001743 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001744
1745 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001746 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301747 /*
1748 * When 4KB native sector is enabled, only 8 blocks
1749 * multiple read or write is allowed
1750 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001751 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001752 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301753 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001754 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001755 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001756 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301757 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001758
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001759 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1760 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001761 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001762 new_areq = NULL;
1763
Linus Walleijc3399ef2017-02-01 13:47:53 +01001764 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001765 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001766 /*
1767 * We have just put the first request into the pipeline
1768 * and there is nothing more to do until it is
1769 * complete.
1770 */
Linus Walleijdf061582017-01-24 11:17:57 +01001771 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001772 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001773
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001774 /*
1775 * An asynchronous request has been completed and we proceed
1776 * to handle the result of it.
1777 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001778 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001779 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001780 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001781 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001782 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001783
Per Forlind78d4a8a2011-07-01 18:55:30 +02001784 switch (status) {
1785 case MMC_BLK_SUCCESS:
1786 case MMC_BLK_PARTIAL:
1787 /*
1788 * A block was successfully transferred.
1789 */
Adrian Hunter67716322011-08-29 16:42:15 +03001790 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001791
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001792 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001793 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001794 /*
1795 * If the blk_end_request function returns non-zero even
1796 * though all data has been transferred and no errors
1797 * were returned by the host controller, it's a bug.
1798 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001799 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301800 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001801 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001802 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001803 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001804 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001805 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001806 break;
1807 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001808 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001809 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001810 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001811 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1812 else
Linus Walleij304419d2017-05-18 11:29:32 +02001813 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001814 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001815 return;
1816 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001817 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001818 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001819 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001820 return;
1821 }
Ding Wang29535f72015-05-18 20:14:15 +08001822 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001823 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001824 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001825 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001826 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001827 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001828 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001829 if (!mmc_blk_reset(md, card->host, type))
1830 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001831 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001832 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001833 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001834 case MMC_BLK_DATA_ERR: {
1835 int err;
1836
1837 err = mmc_blk_reset(md, card->host, type);
1838 if (!err)
1839 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001840 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001841 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001842 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001843 return;
1844 }
Adrian Hunter67716322011-08-29 16:42:15 +03001845 /* Fall through */
1846 }
1847 case MMC_BLK_ECC_ERR:
1848 if (brq->data.blocks > 1) {
1849 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001850 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001851 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001852 disable_multi = 1;
1853 break;
1854 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001855 /*
1856 * After an error, we redo I/O one sector at a
1857 * time, so we only reach here after trying to
1858 * read a single sector.
1859 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001860 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001861 brq->data.blksz);
1862 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001863 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001864 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001865 return;
1866 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001867 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301868 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001869 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001870 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001871 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001872 default:
1873 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001874 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001875 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001876 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001877 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001878 }
1879
Linus Walleij0e65f102017-02-01 13:47:58 +01001880 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001881 /*
1882 * In case of a incomplete request
1883 * prepare it again and resend.
1884 */
1885 mmc_blk_rw_rq_prep(mq_rq, card,
1886 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001887 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001888 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001889 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001890 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001891 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001892
Linus Walleij304419d2017-05-18 11:29:32 +02001893 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Linus Walleijdf061582017-01-24 11:17:57 +01001896void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001897{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001898 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001899 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001900 struct mmc_card *card = md->queue.card;
1901
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001902 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001903 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001904 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001905
Andrei Warkentin371a6892011-04-11 18:10:25 -05001906 ret = mmc_blk_part_switch(card, md);
1907 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001908 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001909 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001910 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001911 goto out;
1912 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001913
Linus Walleij614f0382017-05-18 11:29:34 +02001914 if (req) {
1915 switch (req_op(req)) {
1916 case REQ_OP_DRV_IN:
1917 case REQ_OP_DRV_OUT:
1918 /*
1919 * Complete ongoing async transfer before issuing
1920 * ioctl()s
1921 */
1922 if (mq->qcnt)
1923 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02001924 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02001925 break;
1926 case REQ_OP_DISCARD:
1927 /*
1928 * Complete ongoing async transfer before issuing
1929 * discard.
1930 */
1931 if (mq->qcnt)
1932 mmc_blk_issue_rw_rq(mq, NULL);
1933 mmc_blk_issue_discard_rq(mq, req);
1934 break;
1935 case REQ_OP_SECURE_ERASE:
1936 /*
1937 * Complete ongoing async transfer before issuing
1938 * secure erase.
1939 */
1940 if (mq->qcnt)
1941 mmc_blk_issue_rw_rq(mq, NULL);
1942 mmc_blk_issue_secdiscard_rq(mq, req);
1943 break;
1944 case REQ_OP_FLUSH:
1945 /*
1946 * Complete ongoing async transfer before issuing
1947 * flush.
1948 */
1949 if (mq->qcnt)
1950 mmc_blk_issue_rw_rq(mq, NULL);
1951 mmc_blk_issue_flush(mq, req);
1952 break;
1953 default:
1954 /* Normal request, just issue it */
1955 mmc_blk_issue_rw_rq(mq, req);
1956 card->host->context_info.is_waiting_last_req = false;
1957 break;
Wu Fengguang73222382017-05-23 05:11:33 +08001958 }
Adrian Hunter49804542010-08-11 14:17:50 -07001959 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02001960 /* No request, flushing the pipeline with NULL */
1961 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02001962 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07001963 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001964
Andrei Warkentin371a6892011-04-11 18:10:25 -05001965out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001966 if (!mq->qcnt)
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001967 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Russell Kinga6f6c962006-01-03 22:38:44 +00001970static inline int mmc_blk_readonly(struct mmc_card *card)
1971{
1972 return mmc_card_readonly(card) ||
1973 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1974}
1975
Andrei Warkentin371a6892011-04-11 18:10:25 -05001976static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1977 struct device *parent,
1978 sector_t size,
1979 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001980 const char *subname,
1981 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 struct mmc_blk_data *md;
1984 int devidx, ret;
1985
Heiner Kallweita04848c2017-02-01 19:44:22 +01001986 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1987 if (devidx < 0)
1988 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001990 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001991 if (!md) {
1992 ret = -ENOMEM;
1993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001995
Johan Rudholmadd710e2011-12-02 08:51:06 +01001996 md->area_type = area_type;
1997
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001998 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001999 * Set the read-only status based on the supported commands
2000 * and the write protect switch.
2001 */
2002 md->read_only = mmc_blk_readonly(card);
2003
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002004 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002005 if (md->disk == NULL) {
2006 ret = -ENOMEM;
2007 goto err_kfree;
2008 }
2009
2010 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002011 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002012 md->usage = 1;
2013
Adrian Hunterd09408a2011-06-23 13:40:28 +03002014 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002015 if (ret)
2016 goto err_putdisk;
2017
Linus Walleij7db30282016-11-18 13:36:15 +01002018 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002019
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002020 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002021 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002022 md->disk->fops = &mmc_bdops;
2023 md->disk->private_data = md;
2024 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002025 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002026 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002027 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002028 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002029 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002030
2031 /*
2032 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2033 *
2034 * - be set for removable media with permanent block devices
2035 * - be unset for removable block devices with permanent media
2036 *
2037 * Since MMC block devices clearly fall under the second
2038 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2039 * should use the block device creation/destruction hotplug
2040 * messages to tell when the card is present.
2041 */
2042
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002043 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002044 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002045
Saugata Dasa5075eb2012-05-17 16:32:21 +05302046 if (mmc_card_mmc(card))
2047 blk_queue_logical_block_size(md->queue.queue,
2048 card->ext_csd.data_sector_size);
2049 else
2050 blk_queue_logical_block_size(md->queue.queue, 512);
2051
Andrei Warkentin371a6892011-04-11 18:10:25 -05002052 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002053
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002054 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002055 if ((mmc_card_mmc(card) &&
2056 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002057 (mmc_card_sd(card) &&
2058 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2059 md->flags |= MMC_BLK_CMD23;
2060 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002061
2062 if (mmc_card_mmc(card) &&
2063 md->flags & MMC_BLK_CMD23 &&
2064 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2065 card->ext_csd.rel_sectors)) {
2066 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002067 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002071
2072 err_putdisk:
2073 put_disk(md->disk);
2074 err_kfree:
2075 kfree(md);
2076 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002077 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002078 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Andrei Warkentin371a6892011-04-11 18:10:25 -05002081static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2082{
2083 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002084
2085 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2086 /*
2087 * The EXT_CSD sector count is in number or 512 byte
2088 * sectors.
2089 */
2090 size = card->ext_csd.sectors;
2091 } else {
2092 /*
2093 * The CSD capacity field is in units of read_blkbits.
2094 * set_capacity takes units of 512 bytes.
2095 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002096 size = (typeof(sector_t))card->csd.capacity
2097 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002098 }
2099
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002100 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002101 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002102}
2103
2104static int mmc_blk_alloc_part(struct mmc_card *card,
2105 struct mmc_blk_data *md,
2106 unsigned int part_type,
2107 sector_t size,
2108 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002109 const char *subname,
2110 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002111{
2112 char cap_str[10];
2113 struct mmc_blk_data *part_md;
2114
2115 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002116 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002117 if (IS_ERR(part_md))
2118 return PTR_ERR(part_md);
2119 part_md->part_type = part_type;
2120 list_add(&part_md->part, &md->part);
2121
James Bottomleyb9f28d82015-03-05 18:47:01 -08002122 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002123 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302124 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002125 part_md->disk->disk_name, mmc_card_id(card),
2126 mmc_card_name(card), part_md->part_type, cap_str);
2127 return 0;
2128}
2129
Namjae Jeone0c368d2011-10-06 23:41:38 +09002130/* MMC Physical partitions consist of two boot partitions and
2131 * up to four general purpose partitions.
2132 * For each partition enabled in EXT_CSD a block device will be allocatedi
2133 * to provide access to the partition.
2134 */
2135
Andrei Warkentin371a6892011-04-11 18:10:25 -05002136static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2137{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002138 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002139
2140 if (!mmc_card_mmc(card))
2141 return 0;
2142
Namjae Jeone0c368d2011-10-06 23:41:38 +09002143 for (idx = 0; idx < card->nr_parts; idx++) {
2144 if (card->part[idx].size) {
2145 ret = mmc_blk_alloc_part(card, md,
2146 card->part[idx].part_cfg,
2147 card->part[idx].size >> 9,
2148 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002149 card->part[idx].name,
2150 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002151 if (ret)
2152 return ret;
2153 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002154 }
2155
2156 return ret;
2157}
2158
Andrei Warkentin371a6892011-04-11 18:10:25 -05002159static void mmc_blk_remove_req(struct mmc_blk_data *md)
2160{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002161 struct mmc_card *card;
2162
Andrei Warkentin371a6892011-04-11 18:10:25 -05002163 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002164 /*
2165 * Flush remaining requests and free queues. It
2166 * is freeing the queue that stops new requests
2167 * from being accepted.
2168 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002169 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002170 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171 if (md->disk->flags & GENHD_FL_UP) {
2172 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002173 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2174 card->ext_csd.boot_ro_lockable)
2175 device_remove_file(disk_to_dev(md->disk),
2176 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177
Andrei Warkentin371a6892011-04-11 18:10:25 -05002178 del_gendisk(md->disk);
2179 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002180 mmc_blk_put(md);
2181 }
2182}
2183
2184static void mmc_blk_remove_parts(struct mmc_card *card,
2185 struct mmc_blk_data *md)
2186{
2187 struct list_head *pos, *q;
2188 struct mmc_blk_data *part_md;
2189
2190 list_for_each_safe(pos, q, &md->part) {
2191 part_md = list_entry(pos, struct mmc_blk_data, part);
2192 list_del(pos);
2193 mmc_blk_remove_req(part_md);
2194 }
2195}
2196
2197static int mmc_add_disk(struct mmc_blk_data *md)
2198{
2199 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002200 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002201
Dan Williams307d8e62016-06-20 10:40:44 -07002202 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203 md->force_ro.show = force_ro_show;
2204 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302205 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206 md->force_ro.attr.name = "force_ro";
2207 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2208 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2209 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002210 goto force_ro_fail;
2211
2212 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2213 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002214 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002215
2216 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2217 mode = S_IRUGO;
2218 else
2219 mode = S_IRUGO | S_IWUSR;
2220
2221 md->power_ro_lock.show = power_ro_lock_show;
2222 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002223 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002224 md->power_ro_lock.attr.mode = mode;
2225 md->power_ro_lock.attr.name =
2226 "ro_lock_until_next_power_on";
2227 ret = device_create_file(disk_to_dev(md->disk),
2228 &md->power_ro_lock);
2229 if (ret)
2230 goto power_ro_lock_fail;
2231 }
2232 return ret;
2233
2234power_ro_lock_fail:
2235 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2236force_ro_fail:
2237 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002238
2239 return ret;
2240}
2241
Ulf Hansson96541ba2015-04-14 13:06:12 +02002242static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002245 char cap_str[10];
2246
Pierre Ossman912490d2005-05-21 10:27:02 +01002247 /*
2248 * Check that the card supports the command class(es) we need.
2249 */
2250 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return -ENODEV;
2252
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002253 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002256 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return PTR_ERR(md);
2258
James Bottomleyb9f28d82015-03-05 18:47:01 -08002259 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002260 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302261 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002263 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Andrei Warkentin371a6892011-04-11 18:10:25 -05002265 if (mmc_blk_alloc_parts(card, md))
2266 goto out;
2267
Ulf Hansson96541ba2015-04-14 13:06:12 +02002268 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002269
Andrei Warkentin371a6892011-04-11 18:10:25 -05002270 if (mmc_add_disk(md))
2271 goto out;
2272
2273 list_for_each_entry(part_md, &md->part, part) {
2274 if (mmc_add_disk(part_md))
2275 goto out;
2276 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002277
2278 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2279 pm_runtime_use_autosuspend(&card->dev);
2280
2281 /*
2282 * Don't enable runtime PM for SD-combo cards here. Leave that
2283 * decision to be taken during the SDIO init sequence instead.
2284 */
2285 if (card->type != MMC_TYPE_SD_COMBO) {
2286 pm_runtime_set_active(&card->dev);
2287 pm_runtime_enable(&card->dev);
2288 }
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return 0;
2291
2292 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002293 mmc_blk_remove_parts(card, md);
2294 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Ulf Hansson96541ba2015-04-14 13:06:12 +02002298static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002300 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Andrei Warkentin371a6892011-04-11 18:10:25 -05002302 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002303 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002304 mmc_claim_host(card->host);
2305 mmc_blk_part_switch(card, md);
2306 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002307 if (card->type != MMC_TYPE_SD_COMBO)
2308 pm_runtime_disable(&card->dev);
2309 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002310 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002311 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Ulf Hansson96541ba2015-04-14 13:06:12 +02002314static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002316 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002317 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 if (md) {
2320 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002321 list_for_each_entry(part_md, &md->part, part) {
2322 mmc_queue_suspend(&part_md->queue);
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 return 0;
2326}
2327
Ulf Hansson96541ba2015-04-14 13:06:12 +02002328static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002329{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002330 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002331}
2332
Ulf Hansson0967edc2014-10-06 11:29:42 +02002333#ifdef CONFIG_PM_SLEEP
2334static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002335{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002336 struct mmc_card *card = mmc_dev_to_card(dev);
2337
2338 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002339}
2340
Ulf Hansson0967edc2014-10-06 11:29:42 +02002341static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002343 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002344 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002347 /*
2348 * Resume involves the card going into idle state,
2349 * so current partition is always the main one.
2350 */
2351 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002353 list_for_each_entry(part_md, &md->part, part) {
2354 mmc_queue_resume(&part_md->queue);
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 return 0;
2358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#endif
2360
Ulf Hansson0967edc2014-10-06 11:29:42 +02002361static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2362
Ulf Hansson96541ba2015-04-14 13:06:12 +02002363static struct mmc_driver mmc_driver = {
2364 .drv = {
2365 .name = "mmcblk",
2366 .pm = &mmc_blk_pm_ops,
2367 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 .probe = mmc_blk_probe,
2369 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002370 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371};
2372
2373static int __init mmc_blk_init(void)
2374{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002375 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002377 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2378 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2379
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002380 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002381
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002382 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2383 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002386 res = mmc_register_driver(&mmc_driver);
2387 if (res)
2388 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002390 return 0;
2391 out2:
2392 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 out:
2394 return res;
2395}
2396
2397static void __exit mmc_blk_exit(void)
2398{
2399 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002400 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403module_init(mmc_blk_init);
2404module_exit(mmc_blk_exit);
2405
2406MODULE_LICENSE("GPL");
2407MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2408