blob: 727699c75ca4838cc38a864b7efccfdbcfb8458e [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;
Linus Walleij0493f6f2017-05-19 15:37:30 +0200191 struct mmc_queue *mq;
192 struct request *req;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100193 unsigned long set;
194
195 if (kstrtoul(buf, 0, &set))
196 return -EINVAL;
197
198 if (set != 1)
199 return count;
200
201 md = mmc_blk_get(dev_to_disk(dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +0200202 mq = &md->queue;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100203
Linus Walleij0493f6f2017-05-19 15:37:30 +0200204 /* Dispatch locking to the block layer */
205 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
206 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
207 blk_execute_rq(mq->queue, NULL, req, 0);
208 ret = req_to_mmc_queue_req(req)->drv_op_result;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100209
210 if (!ret) {
211 pr_info("%s: Locking boot partition ro until next power on\n",
212 md->disk->disk_name);
213 set_disk_ro(md->disk, 1);
214
215 list_for_each_entry(part_md, &md->part, part)
216 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
217 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
218 set_disk_ro(part_md->disk, 1);
219 }
220 }
221
222 mmc_blk_put(md);
223 return count;
224}
225
Andrei Warkentin371a6892011-04-11 18:10:25 -0500226static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
227 char *buf)
228{
229 int ret;
230 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
231
Baruch Siach0031a982014-09-22 10:12:51 +0300232 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500233 get_disk_ro(dev_to_disk(dev)) ^
234 md->read_only);
235 mmc_blk_put(md);
236 return ret;
237}
238
239static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t count)
241{
242 int ret;
243 char *end;
244 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
245 unsigned long set = simple_strtoul(buf, &end, 0);
246 if (end == buf) {
247 ret = -EINVAL;
248 goto out;
249 }
250
251 set_disk_ro(dev_to_disk(dev), set || md->read_only);
252 ret = count;
253out:
254 mmc_blk_put(md);
255 return ret;
256}
257
Al Viroa5a15612008-03-02 10:33:30 -0500258static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Al Viroa5a15612008-03-02 10:33:30 -0500260 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int ret = -ENXIO;
262
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200263 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (md) {
265 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500266 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700268
Al Viroa5a15612008-03-02 10:33:30 -0500269 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700270 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700271 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200274 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return ret;
277}
278
Al Virodb2a1442013-05-05 21:52:57 -0400279static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Al Viroa5a15612008-03-02 10:33:30 -0500281 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200283 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200285 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800289mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800291 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
292 geo->heads = 4;
293 geo->sectors = 16;
294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
John Calixtocb87ea22011-04-26 18:56:29 -0400297struct mmc_blk_ioc_data {
298 struct mmc_ioc_cmd ic;
299 unsigned char *buf;
300 u64 buf_bytes;
301};
302
303static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
304 struct mmc_ioc_cmd __user *user)
305{
306 struct mmc_blk_ioc_data *idata;
307 int err;
308
yalin wang1ff89502015-11-12 19:27:11 +0800309 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400310 if (!idata) {
311 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400312 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400313 }
314
315 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
316 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400317 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400318 }
319
320 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
321 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
322 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400323 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400324 }
325
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300326 if (!idata->buf_bytes) {
327 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100328 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300329 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100330
yalin wang1ff89502015-11-12 19:27:11 +0800331 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400332 if (!idata->buf) {
333 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400334 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400335 }
336
337 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
338 idata->ic.data_ptr, idata->buf_bytes)) {
339 err = -EFAULT;
340 goto copy_err;
341 }
342
343 return idata;
344
345copy_err:
346 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400347idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400348 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400349out:
John Calixtocb87ea22011-04-26 18:56:29 -0400350 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400351}
352
Jon Huntera5f57742015-09-22 10:27:53 +0100353static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
354 struct mmc_blk_ioc_data *idata)
355{
356 struct mmc_ioc_cmd *ic = &idata->ic;
357
358 if (copy_to_user(&(ic_ptr->response), ic->response,
359 sizeof(ic->response)))
360 return -EFAULT;
361
362 if (!idata->ic.write_flag) {
363 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
364 idata->buf, idata->buf_bytes))
365 return -EFAULT;
366 }
367
368 return 0;
369}
370
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200371static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
372 u32 retries_max)
373{
374 int err;
375 u32 retry_count = 0;
376
377 if (!status || !retries_max)
378 return -EINVAL;
379
380 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200381 err = __mmc_send_status(card, status, 5);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200382 if (err)
383 break;
384
385 if (!R1_STATUS(*status) &&
386 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
387 break; /* RPMB programming operation complete */
388
389 /*
390 * Rechedule to give the MMC device a chance to continue
391 * processing the previous command without being polled too
392 * frequently.
393 */
394 usleep_range(1000, 5000);
395 } while (++retry_count < retries_max);
396
397 if (retry_count == retries_max)
398 err = -EPERM;
399
400 return err;
401}
402
Maya Erez775a9362013-04-18 15:41:55 +0300403static int ioctl_do_sanitize(struct mmc_card *card)
404{
405 int err;
406
Ulf Hanssona2d10862013-12-16 14:37:26 +0100407 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300408 pr_warn("%s: %s - SANITIZE is not supported\n",
409 mmc_hostname(card->host), __func__);
410 err = -EOPNOTSUPP;
411 goto out;
412 }
413
414 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
415 mmc_hostname(card->host), __func__);
416
417 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
418 EXT_CSD_SANITIZE_START, 1,
419 MMC_SANITIZE_REQ_TIMEOUT);
420
421 if (err)
422 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
423 mmc_hostname(card->host), __func__, err);
424
425 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
426 __func__);
427out:
428 return err;
429}
430
Jon Huntera5f57742015-09-22 10:27:53 +0100431static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
432 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400433{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900434 struct mmc_command cmd = {};
435 struct mmc_data data = {};
436 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400437 struct scatterlist sg;
438 int err;
Linus Walleij829043c2017-05-18 11:29:33 +0200439 bool is_rpmb = false;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200440 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400441
Jon Huntera5f57742015-09-22 10:27:53 +0100442 if (!card || !md || !idata)
443 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400444
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200445 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
446 is_rpmb = true;
447
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100448 cmd.opcode = idata->ic.opcode;
449 cmd.arg = idata->ic.arg;
450 cmd.flags = idata->ic.flags;
451
452 if (idata->buf_bytes) {
453 data.sg = &sg;
454 data.sg_len = 1;
455 data.blksz = idata->ic.blksz;
456 data.blocks = idata->ic.blocks;
457
458 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
459
460 if (idata->ic.write_flag)
461 data.flags = MMC_DATA_WRITE;
462 else
463 data.flags = MMC_DATA_READ;
464
465 /* data.flags must already be set before doing this. */
466 mmc_set_data_timeout(&data, card);
467
468 /* Allow overriding the timeout_ns for empirical tuning. */
469 if (idata->ic.data_timeout_ns)
470 data.timeout_ns = idata->ic.data_timeout_ns;
471
472 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
473 /*
474 * Pretend this is a data transfer and rely on the
475 * host driver to compute timeout. When all host
476 * drivers support cmd.cmd_timeout for R1B, this
477 * can be changed to:
478 *
479 * mrq.data = NULL;
480 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
481 */
482 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
483 }
484
485 mrq.data = &data;
486 }
487
488 mrq.cmd = &cmd;
489
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200490 err = mmc_blk_part_switch(card, md);
491 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100492 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200493
John Calixtocb87ea22011-04-26 18:56:29 -0400494 if (idata->ic.is_acmd) {
495 err = mmc_app_cmd(card->host, card);
496 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100497 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400498 }
499
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200500 if (is_rpmb) {
501 err = mmc_set_blockcount(card, data.blocks,
502 idata->ic.write_flag & (1 << 31));
503 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100504 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200505 }
506
Yaniv Gardia82e4842013-06-05 14:13:08 +0300507 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
508 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300509 err = ioctl_do_sanitize(card);
510
511 if (err)
512 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
513 __func__, err);
514
Jon Huntera5f57742015-09-22 10:27:53 +0100515 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300516 }
517
John Calixtocb87ea22011-04-26 18:56:29 -0400518 mmc_wait_for_req(card->host, &mrq);
519
520 if (cmd.error) {
521 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
522 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100523 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400524 }
525 if (data.error) {
526 dev_err(mmc_dev(card->host), "%s: data error %d\n",
527 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100528 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400529 }
530
531 /*
532 * According to the SD specs, some commands require a delay after
533 * issuing the command.
534 */
535 if (idata->ic.postsleep_min_us)
536 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
537
Jon Huntera5f57742015-09-22 10:27:53 +0100538 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400539
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200540 if (is_rpmb) {
541 /*
542 * Ensure RPMB command has completed by polling CMD13
543 * "Send Status".
544 */
545 err = ioctl_rpmb_card_status_poll(card, &status, 5);
546 if (err)
547 dev_err(mmc_dev(card->host),
548 "%s: Card Status=0x%08X, error %d\n",
549 __func__, status, err);
550 }
551
Jon Huntera5f57742015-09-22 10:27:53 +0100552 return err;
553}
554
555static int mmc_blk_ioctl_cmd(struct block_device *bdev,
556 struct mmc_ioc_cmd __user *ic_ptr)
557{
558 struct mmc_blk_ioc_data *idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200559 struct mmc_blk_ioc_data *idatas[1];
Jon Huntera5f57742015-09-22 10:27:53 +0100560 struct mmc_blk_data *md;
Linus Walleij614f0382017-05-18 11:29:34 +0200561 struct mmc_queue *mq;
Jon Huntera5f57742015-09-22 10:27:53 +0100562 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700563 int err = 0, ioc_err = 0;
Linus Walleij614f0382017-05-18 11:29:34 +0200564 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100565
Shawn Lin83c742c2016-03-16 18:15:47 +0800566 /*
567 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
568 * whole block device, not on a partition. This prevents overspray
569 * between sibling partitions.
570 */
571 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
572 return -EPERM;
573
Jon Huntera5f57742015-09-22 10:27:53 +0100574 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
575 if (IS_ERR(idata))
576 return PTR_ERR(idata);
577
578 md = mmc_blk_get(bdev->bd_disk);
579 if (!md) {
580 err = -EINVAL;
581 goto cmd_err;
582 }
583
584 card = md->queue.card;
585 if (IS_ERR(card)) {
586 err = PTR_ERR(card);
587 goto cmd_done;
588 }
589
Linus Walleij614f0382017-05-18 11:29:34 +0200590 /*
591 * Dispatch the ioctl() into the block request queue.
592 */
593 mq = &md->queue;
594 req = blk_get_request(mq->queue,
595 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
596 __GFP_RECLAIM);
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200597 idatas[0] = idata;
Linus Walleij02166a02017-05-19 15:37:28 +0200598 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200599 req_to_mmc_queue_req(req)->drv_op_data = idatas;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200600 req_to_mmc_queue_req(req)->ioc_count = 1;
Linus Walleij614f0382017-05-18 11:29:34 +0200601 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200602 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Grant Grundlerb0934102015-09-23 18:30:33 -0700603 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Linus Walleij614f0382017-05-18 11:29:34 +0200604 blk_put_request(req);
Jon Huntera5f57742015-09-22 10:27:53 +0100605
John Calixtocb87ea22011-04-26 18:56:29 -0400606cmd_done:
607 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300608cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400609 kfree(idata->buf);
610 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700611 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400612}
613
Jon Huntera5f57742015-09-22 10:27:53 +0100614static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
615 struct mmc_ioc_multi_cmd __user *user)
616{
617 struct mmc_blk_ioc_data **idata = NULL;
618 struct mmc_ioc_cmd __user *cmds = user->cmds;
619 struct mmc_card *card;
620 struct mmc_blk_data *md;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200621 struct mmc_queue *mq;
Grant Grundlerb0934102015-09-23 18:30:33 -0700622 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100623 __u64 num_of_cmds;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200624 struct request *req;
Jon Huntera5f57742015-09-22 10:27:53 +0100625
Shawn Lin83c742c2016-03-16 18:15:47 +0800626 /*
627 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
628 * whole block device, not on a partition. This prevents overspray
629 * between sibling partitions.
630 */
631 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
632 return -EPERM;
633
Jon Huntera5f57742015-09-22 10:27:53 +0100634 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
635 sizeof(num_of_cmds)))
636 return -EFAULT;
637
Geert Uytterhoevenaab2ee02017-07-05 17:09:42 +0200638 if (!num_of_cmds)
639 return 0;
640
Jon Huntera5f57742015-09-22 10:27:53 +0100641 if (num_of_cmds > MMC_IOC_MAX_CMDS)
642 return -EINVAL;
643
644 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
645 if (!idata)
646 return -ENOMEM;
647
648 for (i = 0; i < num_of_cmds; i++) {
649 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
650 if (IS_ERR(idata[i])) {
651 err = PTR_ERR(idata[i]);
652 num_of_cmds = i;
653 goto cmd_err;
654 }
655 }
656
657 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800658 if (!md) {
659 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100660 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800661 }
Jon Huntera5f57742015-09-22 10:27:53 +0100662
663 card = md->queue.card;
664 if (IS_ERR(card)) {
665 err = PTR_ERR(card);
666 goto cmd_done;
667 }
668
Jon Huntera5f57742015-09-22 10:27:53 +0100669
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200670 /*
671 * Dispatch the ioctl()s into the block request queue.
672 */
673 mq = &md->queue;
674 req = blk_get_request(mq->queue,
675 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
676 __GFP_RECLAIM);
Linus Walleij02166a02017-05-19 15:37:28 +0200677 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
Linus Walleij69f75992017-08-20 23:39:06 +0200678 req_to_mmc_queue_req(req)->drv_op_data = idata;
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200679 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
680 blk_execute_rq(mq->queue, NULL, req, 0);
Linus Walleij0493f6f2017-05-19 15:37:30 +0200681 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
Jon Huntera5f57742015-09-22 10:27:53 +0100682
683 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700684 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100685 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100686
Linus Walleij3ecd8cf2017-05-18 11:29:35 +0200687 blk_put_request(req);
688
Jon Huntera5f57742015-09-22 10:27:53 +0100689cmd_done:
690 mmc_blk_put(md);
691cmd_err:
692 for (i = 0; i < num_of_cmds; i++) {
693 kfree(idata[i]->buf);
694 kfree(idata[i]);
695 }
696 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700697 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100698}
699
John Calixtocb87ea22011-04-26 18:56:29 -0400700static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
701 unsigned int cmd, unsigned long arg)
702{
Jon Huntera5f57742015-09-22 10:27:53 +0100703 switch (cmd) {
704 case MMC_IOC_CMD:
705 return mmc_blk_ioctl_cmd(bdev,
706 (struct mmc_ioc_cmd __user *)arg);
707 case MMC_IOC_MULTI_CMD:
708 return mmc_blk_ioctl_multi_cmd(bdev,
709 (struct mmc_ioc_multi_cmd __user *)arg);
710 default:
711 return -EINVAL;
712 }
John Calixtocb87ea22011-04-26 18:56:29 -0400713}
714
715#ifdef CONFIG_COMPAT
716static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
717 unsigned int cmd, unsigned long arg)
718{
719 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
720}
721#endif
722
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700723static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500724 .open = mmc_blk_open,
725 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800726 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400728 .ioctl = mmc_blk_ioctl,
729#ifdef CONFIG_COMPAT
730 .compat_ioctl = mmc_blk_compat_ioctl,
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Adrian Hunter025e3d52017-03-13 14:36:39 +0200734static int mmc_blk_part_switch_pre(struct mmc_card *card,
735 unsigned int part_type)
736{
737 int ret = 0;
738
739 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
740 if (card->ext_csd.cmdq_en) {
741 ret = mmc_cmdq_disable(card);
742 if (ret)
743 return ret;
744 }
745 mmc_retune_pause(card->host);
746 }
747
748 return ret;
749}
750
751static int mmc_blk_part_switch_post(struct mmc_card *card,
752 unsigned int part_type)
753{
754 int ret = 0;
755
756 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
757 mmc_retune_unpause(card->host);
758 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
759 ret = mmc_cmdq_enable(card);
760 }
761
762 return ret;
763}
764
Andrei Warkentin371a6892011-04-11 18:10:25 -0500765static inline int mmc_blk_part_switch(struct mmc_card *card,
766 struct mmc_blk_data *md)
767{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200768 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200769 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300770
Andrei Warkentin371a6892011-04-11 18:10:25 -0500771 if (main_md->part_curr == md->part_type)
772 return 0;
773
774 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300775 u8 part_config = card->ext_csd.part_config;
776
Adrian Hunter025e3d52017-03-13 14:36:39 +0200777 ret = mmc_blk_part_switch_pre(card, md->part_type);
778 if (ret)
779 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300780
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300781 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
782 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500783
784 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300785 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500786 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300787 if (ret) {
Adrian Hunter025e3d52017-03-13 14:36:39 +0200788 mmc_blk_part_switch_post(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500789 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300790 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300791
792 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300793
Adrian Hunter025e3d52017-03-13 14:36:39 +0200794 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300795 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500796
797 main_md->part_curr = md->part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200798 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500799}
800
Linus Walleij169f03a2017-02-01 13:47:57 +0100801static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700802{
803 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100804 u32 result;
805 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700806
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900807 struct mmc_request mrq = {};
808 struct mmc_command cmd = {};
809 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700810
811 struct scatterlist sg;
812
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813 cmd.opcode = MMC_APP_CMD;
814 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700815 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700816
817 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700818 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100819 return err;
David Brownell7213d172007-08-08 09:10:23 -0700820 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100821 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700822
823 memset(&cmd, 0, sizeof(struct mmc_command));
824
825 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
826 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700827 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700828
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700829 data.blksz = 4;
830 data.blocks = 1;
831 data.flags = MMC_DATA_READ;
832 data.sg = &sg;
833 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530834 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700835
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700836 mrq.cmd = &cmd;
837 mrq.data = &data;
838
Ben Dooks051913d2009-06-08 23:33:57 +0100839 blocks = kmalloc(4, GFP_KERNEL);
840 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100841 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100842
843 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700844
845 mmc_wait_for_req(card->host, &mrq);
846
Ben Dooks051913d2009-06-08 23:33:57 +0100847 result = ntohl(*blocks);
848 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700849
Ben Dooks051913d2009-06-08 23:33:57 +0100850 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100851 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100852
Linus Walleij169f03a2017-02-01 13:47:57 +0100853 *written_blocks = result;
854
855 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700856}
857
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100858static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100859 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100860{
861 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
862 int err = 0;
863 u32 status;
864
865 do {
Ulf Hansson2185bc22017-05-22 10:23:58 +0200866 err = __mmc_send_status(card, &status, 5);
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100867 if (err) {
868 pr_err("%s: error %d requesting status\n",
869 req->rq_disk->disk_name, err);
870 return err;
871 }
872
873 if (status & R1_ERROR) {
874 pr_err("%s: %s: error sending status cmd, status %#x\n",
875 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100876 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100877 }
878
Ulf Hansson95a91292014-01-29 13:11:27 +0100879 /* We may rely on the host hw to handle busy detection.*/
880 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
881 hw_busy_detect)
882 break;
883
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100884 /*
885 * Timeout if the device never becomes ready for data and never
886 * leaves the program state.
887 */
888 if (time_after(jiffies, timeout)) {
889 pr_err("%s: Card stuck in programming state! %s %s\n",
890 mmc_hostname(card->host),
891 req->rq_disk->disk_name, __func__);
892 return -ETIMEDOUT;
893 }
894
895 /*
896 * Some cards mishandle the status bits,
897 * so make sure to check both the busy
898 * indication and the card state.
899 */
900 } while (!(status & R1_READY_FOR_DATA) ||
901 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
902
903 return err;
904}
905
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100906static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100907 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100908{
909 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900910 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100911 int err;
912 bool use_r1b_resp = rq_data_dir(req) == WRITE;
913
914 /*
915 * Normally we use R1B responses for WRITE, but in cases where the host
916 * has specified a max_busy_timeout we need to validate it. A failure
917 * means we need to prevent the host from doing hw busy detection, which
918 * is done by converting to a R1 response instead.
919 */
920 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
921 use_r1b_resp = false;
922
923 cmd.opcode = MMC_STOP_TRANSMISSION;
924 if (use_r1b_resp) {
925 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
926 cmd.busy_timeout = timeout_ms;
927 } else {
928 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
929 }
930
931 err = mmc_wait_for_cmd(host, &cmd, 5);
932 if (err)
933 return err;
934
935 *stop_status = cmd.resp[0];
936
937 /* No need to check card status in case of READ. */
938 if (rq_data_dir(req) == READ)
939 return 0;
940
941 if (!mmc_host_is_spi(host) &&
942 (*stop_status & R1_ERROR)) {
943 pr_err("%s: %s: general error sending stop command, resp %#x\n",
944 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100945 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100946 }
947
948 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
949}
950
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530951#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100952#define ERR_RETRY 2
953#define ERR_ABORT 1
954#define ERR_CONTINUE 0
955
956static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
957 bool status_valid, u32 status)
958{
959 switch (error) {
960 case -EILSEQ:
961 /* response crc error, retry the r/w cmd */
962 pr_err("%s: %s sending %s command, card status %#x\n",
963 req->rq_disk->disk_name, "response CRC error",
964 name, status);
965 return ERR_RETRY;
966
967 case -ETIMEDOUT:
968 pr_err("%s: %s sending %s command, card status %#x\n",
969 req->rq_disk->disk_name, "timed out", name, status);
970
971 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530972 if (!status_valid) {
973 pr_err("%s: status not valid, retrying timeout\n",
974 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100975 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530976 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100977
978 /*
979 * If it was a r/w cmd crc error, or illegal command
980 * (eg, issued in wrong state) then retry - we should
981 * have corrected the state problem above.
982 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530983 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
984 pr_err("%s: command error, retrying timeout\n",
985 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100986 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530987 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100988
989 /* Otherwise abort the command */
990 return ERR_ABORT;
991
992 default:
993 /* We don't understand the error code the driver gave us */
994 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
995 req->rq_disk->disk_name, error, status);
996 return ERR_ABORT;
997 }
998}
999
1000/*
1001 * Initial r/w and stop cmd error recovery.
1002 * We don't know whether the card received the r/w cmd or not, so try to
1003 * restore things back to a sane state. Essentially, we do this as follows:
1004 * - Obtain card status. If the first attempt to obtain card status fails,
1005 * the status word will reflect the failed status cmd, not the failed
1006 * r/w cmd. If we fail to obtain card status, it suggests we can no
1007 * longer communicate with the card.
1008 * - Check the card state. If the card received the cmd but there was a
1009 * transient problem with the response, it might still be in a data transfer
1010 * mode. Try to send it a stop command. If this fails, we can't recover.
1011 * - If the r/w cmd failed due to a response CRC error, it was probably
1012 * transient, so retry the cmd.
1013 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1014 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1015 * illegal cmd, retry.
1016 * Otherwise we don't understand what happened, so abort.
1017 */
1018static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001019 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001020{
1021 bool prev_cmd_status_valid = true;
1022 u32 status, stop_status = 0;
1023 int err, retry;
1024
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301025 if (mmc_card_removed(card))
1026 return ERR_NOMEDIUM;
1027
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001028 /*
1029 * Try to get card status which indicates both the card state
1030 * and why there was no response. If the first attempt fails,
1031 * we can't be sure the returned status is for the r/w command.
1032 */
1033 for (retry = 2; retry >= 0; retry--) {
Ulf Hansson2185bc22017-05-22 10:23:58 +02001034 err = __mmc_send_status(card, &status, 0);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001035 if (!err)
1036 break;
1037
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001038 /* Re-tune if needed */
1039 mmc_retune_recheck(card->host);
1040
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001041 prev_cmd_status_valid = false;
1042 pr_err("%s: error %d sending status command, %sing\n",
1043 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1044 }
1045
1046 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301047 if (err) {
1048 /* Check if the card is removed */
1049 if (mmc_detect_card_removed(card->host))
1050 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001051 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301052 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053
Adrian Hunter67716322011-08-29 16:42:15 +03001054 /* Flag ECC errors */
1055 if ((status & R1_CARD_ECC_FAILED) ||
1056 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1057 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001058 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001059
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001060 /* Flag General errors */
1061 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1062 if ((status & R1_ERROR) ||
1063 (brq->stop.resp[0] & R1_ERROR)) {
1064 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1065 req->rq_disk->disk_name, __func__,
1066 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001067 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001068 }
1069
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001070 /*
1071 * Check the current card state. If it is in some data transfer
1072 * mode, tell it to stop (and hopefully transition back to TRAN.)
1073 */
1074 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1075 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001076 err = send_stop(card,
1077 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1078 req, gen_err, &stop_status);
1079 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001080 pr_err("%s: error %d sending stop command\n",
1081 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001082 /*
1083 * If the stop cmd also timed out, the card is probably
1084 * not present, so abort. Other errors are bad news too.
1085 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001086 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001087 }
1088
Adrian Hunter67716322011-08-29 16:42:15 +03001089 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001090 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001091 }
1092
1093 /* Check for set block count errors */
1094 if (brq->sbc.error)
1095 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1096 prev_cmd_status_valid, status);
1097
1098 /* Check for r/w command errors */
1099 if (brq->cmd.error)
1100 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1101 prev_cmd_status_valid, status);
1102
Adrian Hunter67716322011-08-29 16:42:15 +03001103 /* Data errors */
1104 if (!brq->stop.error)
1105 return ERR_CONTINUE;
1106
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001107 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001108 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 +01001109 req->rq_disk->disk_name, brq->stop.error,
1110 brq->cmd.resp[0], status);
1111
1112 /*
1113 * Subsitute in our own stop status as this will give the error
1114 * state which happened during the execution of the r/w command.
1115 */
1116 if (stop_status) {
1117 brq->stop.resp[0] = stop_status;
1118 brq->stop.error = 0;
1119 }
1120 return ERR_CONTINUE;
1121}
1122
Adrian Hunter67716322011-08-29 16:42:15 +03001123static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1124 int type)
1125{
1126 int err;
1127
1128 if (md->reset_done & type)
1129 return -EEXIST;
1130
1131 md->reset_done |= type;
1132 err = mmc_hw_reset(host);
1133 /* Ensure we switch back to the correct partition */
1134 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001135 struct mmc_blk_data *main_md =
1136 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001137 int part_err;
1138
1139 main_md->part_curr = main_md->part_type;
1140 part_err = mmc_blk_part_switch(host->card, md);
1141 if (part_err) {
1142 /*
1143 * We have failed to get back into the correct
1144 * partition, so we need to abort the whole request.
1145 */
1146 return -ENODEV;
1147 }
1148 }
1149 return err;
1150}
1151
1152static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1153{
1154 md->reset_done &= ~type;
1155}
1156
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001157int mmc_access_rpmb(struct mmc_queue *mq)
1158{
Linus Walleij7db30282016-11-18 13:36:15 +01001159 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001160 /*
1161 * If this is a RPMB partition access, return ture
1162 */
1163 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1164 return true;
1165
1166 return false;
1167}
1168
Linus Walleij5ec12392017-05-19 15:37:29 +02001169/*
1170 * The non-block commands come back from the block layer after it queued it and
1171 * processed it with all other requests and then they get issued in this
1172 * function.
1173 */
1174static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1175{
1176 struct mmc_queue_req *mq_rq;
1177 struct mmc_card *card = mq->card;
1178 struct mmc_blk_data *md = mq->blkdata;
Linus Walleij69f75992017-08-20 23:39:06 +02001179 struct mmc_blk_ioc_data **idata;
Linus Walleij0493f6f2017-05-19 15:37:30 +02001180 int ret;
Linus Walleij5ec12392017-05-19 15:37:29 +02001181 int i;
1182
1183 mq_rq = req_to_mmc_queue_req(req);
1184
1185 switch (mq_rq->drv_op) {
1186 case MMC_DRV_OP_IOCTL:
Linus Walleij69f75992017-08-20 23:39:06 +02001187 idata = mq_rq->drv_op_data;
Geert Uytterhoeven7432b492017-07-05 17:09:41 +02001188 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
Linus Walleij69f75992017-08-20 23:39:06 +02001189 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Linus Walleij0493f6f2017-05-19 15:37:30 +02001190 if (ret)
Linus Walleij5ec12392017-05-19 15:37:29 +02001191 break;
1192 }
Linus Walleij5ec12392017-05-19 15:37:29 +02001193 /* Always switch back to main area after RPMB access */
1194 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1195 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
Linus Walleij0493f6f2017-05-19 15:37:30 +02001196 break;
1197 case MMC_DRV_OP_BOOT_WP:
1198 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1199 card->ext_csd.boot_ro_lock |
1200 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1201 card->ext_csd.part_time);
1202 if (ret)
1203 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1204 md->disk->disk_name, ret);
1205 else
1206 card->ext_csd.boot_ro_lock |=
1207 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
Linus Walleij5ec12392017-05-19 15:37:29 +02001208 break;
1209 default:
Linus Walleij0493f6f2017-05-19 15:37:30 +02001210 pr_err("%s: unknown driver specific operation\n",
1211 md->disk->disk_name);
1212 ret = -EINVAL;
Linus Walleij5ec12392017-05-19 15:37:29 +02001213 break;
1214 }
Linus Walleij0493f6f2017-05-19 15:37:30 +02001215 mq_rq->drv_op_result = ret;
1216 blk_end_request_all(req, ret);
Linus Walleij5ec12392017-05-19 15:37:29 +02001217}
1218
Linus Walleijdf061582017-01-24 11:17:57 +01001219static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001220{
Linus Walleij7db30282016-11-18 13:36:15 +01001221 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001222 struct mmc_card *card = md->queue.card;
1223 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001224 int err = 0, type = MMC_BLK_DISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001225 blk_status_t status = BLK_STS_OK;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001226
Adrian Hunterbd788c92010-08-11 14:17:47 -07001227 if (!mmc_can_erase(card)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001228 status = BLK_STS_NOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001229 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001230 }
1231
1232 from = blk_rq_pos(req);
1233 nr = blk_rq_sectors(req);
1234
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001235 if (mmc_can_discard(card))
1236 arg = MMC_DISCARD_ARG;
1237 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001238 arg = MMC_TRIM_ARG;
1239 else
1240 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001241 do {
1242 err = 0;
1243 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245 INAND_CMD38_ARG_EXT_CSD,
1246 arg == MMC_TRIM_ARG ?
1247 INAND_CMD38_ARG_TRIM :
1248 INAND_CMD38_ARG_ERASE,
1249 0);
1250 }
1251 if (!err)
1252 err = mmc_erase(card, from, nr, arg);
1253 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001254 if (err)
1255 status = BLK_STS_IOERR;
1256 else
Adrian Hunter67716322011-08-29 16:42:15 +03001257 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001258fail:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001259 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001260}
1261
Linus Walleijdf061582017-01-24 11:17:57 +01001262static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001263 struct request *req)
1264{
Linus Walleij7db30282016-11-18 13:36:15 +01001265 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001266 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001267 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001268 int err = 0, type = MMC_BLK_SECDISCARD;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001269 blk_status_t status = BLK_STS_OK;
Adrian Hunter49804542010-08-11 14:17:50 -07001270
Maya Erez775a9362013-04-18 15:41:55 +03001271 if (!(mmc_can_secure_erase_trim(card))) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001272 status = BLK_STS_NOTSUPP;
Adrian Hunter49804542010-08-11 14:17:50 -07001273 goto out;
1274 }
1275
1276 from = blk_rq_pos(req);
1277 nr = blk_rq_sectors(req);
1278
Maya Erez775a9362013-04-18 15:41:55 +03001279 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1280 arg = MMC_SECURE_TRIM1_ARG;
1281 else
1282 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001283
Adrian Hunter67716322011-08-29 16:42:15 +03001284retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001285 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1286 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1287 INAND_CMD38_ARG_EXT_CSD,
1288 arg == MMC_SECURE_TRIM1_ARG ?
1289 INAND_CMD38_ARG_SECTRIM1 :
1290 INAND_CMD38_ARG_SECERASE,
1291 0);
1292 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001293 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001294 }
Adrian Hunter28302812012-04-05 14:45:48 +03001295
Adrian Hunter49804542010-08-11 14:17:50 -07001296 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001297 if (err == -EIO)
1298 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001299 if (err) {
1300 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001301 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001302 }
Adrian Hunter28302812012-04-05 14:45:48 +03001303
1304 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001305 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1306 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1307 INAND_CMD38_ARG_EXT_CSD,
1308 INAND_CMD38_ARG_SECTRIM2,
1309 0);
1310 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001311 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001312 }
Adrian Hunter28302812012-04-05 14:45:48 +03001313
Adrian Hunter49804542010-08-11 14:17:50 -07001314 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001315 if (err == -EIO)
1316 goto out_retry;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001317 if (err) {
1318 status = BLK_STS_IOERR;
Adrian Hunter28302812012-04-05 14:45:48 +03001319 goto out;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001320 }
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001321 }
Adrian Hunter28302812012-04-05 14:45:48 +03001322
Adrian Hunter28302812012-04-05 14:45:48 +03001323out_retry:
1324 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001325 goto retry;
1326 if (!err)
1327 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001328out:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001329 blk_end_request(req, status, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001330}
1331
Linus Walleijdf061582017-01-24 11:17:57 +01001332static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001333{
Linus Walleij7db30282016-11-18 13:36:15 +01001334 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001335 struct mmc_card *card = md->queue.card;
1336 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001337
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001338 ret = mmc_flush_cache(card);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001339 blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001340}
1341
1342/*
1343 * Reformat current write as a reliable write, supporting
1344 * both legacy and the enhanced reliable write MMC cards.
1345 * In each transfer we'll handle only as much as a single
1346 * reliable write can handle, thus finish the request in
1347 * partial completions.
1348 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001349static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1350 struct mmc_card *card,
1351 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001352{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001353 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1354 /* Legacy mode imposes restrictions on transfers. */
Adrian Hunter9cb38f72017-03-13 14:36:40 +02001355 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001356 brq->data.blocks = 1;
1357
1358 if (brq->data.blocks > card->ext_csd.rel_sectors)
1359 brq->data.blocks = card->ext_csd.rel_sectors;
1360 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1361 brq->data.blocks = 1;
1362 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001363}
1364
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001365#define CMD_ERRORS \
1366 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1367 R1_ADDRESS_ERROR | /* Misaligned address */ \
1368 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1369 R1_WP_VIOLATION | /* Tried to write to protected block */ \
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001370 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001371 R1_CC_ERROR | /* Card controller error */ \
1372 R1_ERROR) /* General/unknown error */
1373
Shawn Lind83c2db2017-08-18 09:16:08 +08001374static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001375{
Shawn Lind83c2db2017-08-18 09:16:08 +08001376 u32 val;
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001377
Shawn Lind83c2db2017-08-18 09:16:08 +08001378 /*
1379 * Per the SD specification(physical layer version 4.10)[1],
1380 * section 4.3.3, it explicitly states that "When the last
1381 * block of user area is read using CMD18, the host should
1382 * ignore OUT_OF_RANGE error that may occur even the sequence
1383 * is correct". And JESD84-B51 for eMMC also has a similar
1384 * statement on section 6.8.3.
1385 *
1386 * Multiple block read/write could be done by either predefined
1387 * method, namely CMD23, or open-ending mode. For open-ending mode,
1388 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1389 *
1390 * However the spec[1] doesn't tell us whether we should also
1391 * ignore that for predefined method. But per the spec[1], section
1392 * 4.15 Set Block Count Command, it says"If illegal block count
1393 * is set, out of range error will be indicated during read/write
1394 * operation (For example, data transfer is stopped at user area
1395 * boundary)." In another word, we could expect a out of range error
1396 * in the response for the following CMD18/25. And if argument of
1397 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1398 * we could also expect to get a -ETIMEDOUT or any error number from
1399 * the host drivers due to missing data response(for write)/data(for
1400 * read), as the cards will stop the data transfer by itself per the
1401 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1402 */
1403
1404 if (!brq->stop.error) {
1405 bool oor_with_open_end;
1406 /* If there is no error yet, check R1 response */
1407
1408 val = brq->stop.resp[0] & CMD_ERRORS;
1409 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1410
1411 if (val && !oor_with_open_end)
1412 brq->stop.error = -EIO;
1413 }
Wolfram Sanga04e6ba2017-04-08 22:20:05 +02001414}
1415
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001416static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1417 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001418{
Per Forlinee8a43a2011-07-01 18:55:33 +02001419 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001420 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001421 struct mmc_blk_request *brq = &mq_mrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001422 struct request *req = mmc_queue_req_to_req(mq_mrq);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001423 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001424 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001425 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001426
1427 /*
1428 * sbc.error indicates a problem with the set block count
1429 * command. No data will have been transferred.
1430 *
1431 * cmd.error indicates a problem with the r/w command. No
1432 * data will have been transferred.
1433 *
1434 * stop.error indicates a problem with the stop command. Data
1435 * may have been transferred, or may still be transferring.
1436 */
Shawn Lind83c2db2017-08-18 09:16:08 +08001437
1438 mmc_blk_eval_resp_error(brq);
1439
1440 if (brq->sbc.error || brq->cmd.error ||
1441 brq->stop.error || brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001442 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001443 case ERR_RETRY:
1444 return MMC_BLK_RETRY;
1445 case ERR_ABORT:
1446 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301447 case ERR_NOMEDIUM:
1448 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001449 case ERR_CONTINUE:
1450 break;
1451 }
1452 }
1453
1454 /*
1455 * Check for errors relating to the execution of the
1456 * initial command - such as address errors. No data
1457 * has been transferred.
1458 */
1459 if (brq->cmd.resp[0] & CMD_ERRORS) {
1460 pr_err("%s: r/w command failed, status = %#x\n",
1461 req->rq_disk->disk_name, brq->cmd.resp[0]);
1462 return MMC_BLK_ABORT;
1463 }
1464
1465 /*
1466 * Everything else is either success, or a data error of some
1467 * kind. If it was a write, we may have transitioned to
1468 * program mode, which we have to wait for it to complete.
1469 */
1470 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001471 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001472
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001473 /* Check stop command response */
1474 if (brq->stop.resp[0] & R1_ERROR) {
1475 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1476 req->rq_disk->disk_name, __func__,
1477 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001478 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001479 }
1480
Ulf Hansson95a91292014-01-29 13:11:27 +01001481 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1482 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001483 if (err)
1484 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001485 }
1486
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001487 /* if general error occurs, retry the write operation. */
1488 if (gen_err) {
1489 pr_warn("%s: retrying write for general error\n",
1490 req->rq_disk->disk_name);
1491 return MMC_BLK_RETRY;
1492 }
1493
Wolfram Sang9820a5b2017-04-08 22:20:06 +02001494 /* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1495 if (brq->data.error || brq->stop.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001496 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001497 pr_debug("%s: retrying because a re-tune was needed\n",
1498 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001499 brq->retune_retry_done = 1;
1500 return MMC_BLK_RETRY;
1501 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001502 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 +02001503 req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001504 (unsigned)blk_rq_pos(req),
1505 (unsigned)blk_rq_sectors(req),
1506 brq->cmd.resp[0], brq->stop.resp[0]);
1507
1508 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001509 if (ecc_err)
1510 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001511 return MMC_BLK_DATA_ERR;
1512 } else {
1513 return MMC_BLK_CMD_ERR;
1514 }
1515 }
1516
Adrian Hunter67716322011-08-29 16:42:15 +03001517 if (!brq->data.bytes_xfered)
1518 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001519
Adrian Hunter67716322011-08-29 16:42:15 +03001520 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1521 return MMC_BLK_PARTIAL;
1522
1523 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001524}
1525
Adrian Hunterca5717f2017-03-13 14:36:41 +02001526static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1527 int disable_multi, bool *do_rel_wr,
1528 bool *do_data_tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Adrian Hunterca5717f2017-03-13 14:36:41 +02001530 struct mmc_blk_data *md = mq->blkdata;
1531 struct mmc_card *card = md->queue.card;
Per Forlin54d49d72011-07-01 18:55:29 +02001532 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001533 struct request *req = mmc_queue_req_to_req(mqrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001535 /*
1536 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001537 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001538 */
Adrian Hunterca5717f2017-03-13 14:36:41 +02001539 *do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1540 rq_data_dir(req) == WRITE &&
1541 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001542
Per Forlin54d49d72011-07-01 18:55:29 +02001543 memset(brq, 0, sizeof(struct mmc_blk_request));
Adrian Hunterca5717f2017-03-13 14:36:41 +02001544
Per Forlin54d49d72011-07-01 18:55:29 +02001545 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Per Forlin54d49d72011-07-01 18:55:29 +02001547 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1548 brq->stop.arg = 0;
Adrian Hunterca5717f2017-03-13 14:36:41 +02001549
1550 if (rq_data_dir(req) == READ) {
1551 brq->data.flags = MMC_DATA_READ;
1552 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1553 } else {
1554 brq->data.flags = MMC_DATA_WRITE;
1555 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1556 }
1557
1558 brq->data.blksz = 512;
Per Forlin54d49d72011-07-01 18:55:29 +02001559 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Per Forlin54d49d72011-07-01 18:55:29 +02001561 /*
1562 * The block layer doesn't support all sector count
1563 * restrictions, so we need to be prepared for too big
1564 * requests.
1565 */
1566 if (brq->data.blocks > card->host->max_blk_count)
1567 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001569 if (brq->data.blocks > 1) {
1570 /*
1571 * After a read error, we redo the request one sector
1572 * at a time in order to accurately determine which
1573 * sectors can be read successfully.
1574 */
1575 if (disable_multi)
1576 brq->data.blocks = 1;
1577
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001578 /*
1579 * Some controllers have HW issues while operating
1580 * in multiple I/O mode
1581 */
1582 if (card->host->ops->multi_io_quirk)
1583 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1584 (rq_data_dir(req) == READ) ?
1585 MMC_DATA_READ : MMC_DATA_WRITE,
1586 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001587 }
Per Forlin54d49d72011-07-01 18:55:29 +02001588
Adrian Hunterca5717f2017-03-13 14:36:41 +02001589 if (*do_rel_wr)
1590 mmc_apply_rel_rw(brq, card, req);
1591
1592 /*
1593 * Data tag is used only during writing meta data to speed
1594 * up write and any subsequent read of this meta data
1595 */
1596 *do_data_tag = card->ext_csd.data_tag_unit_size &&
1597 (req->cmd_flags & REQ_META) &&
1598 (rq_data_dir(req) == WRITE) &&
1599 ((brq->data.blocks * brq->data.blksz) >=
1600 card->ext_csd.data_tag_unit_size);
1601
1602 mmc_set_data_timeout(&brq->data, card);
1603
1604 brq->data.sg = mqrq->sg;
1605 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1606
1607 /*
1608 * Adjust the sg list so it is the same size as the
1609 * request.
1610 */
1611 if (brq->data.blocks != blk_rq_sectors(req)) {
1612 int i, data_size = brq->data.blocks << 9;
1613 struct scatterlist *sg;
1614
1615 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1616 data_size -= sg->length;
1617 if (data_size <= 0) {
1618 sg->length += data_size;
1619 i++;
1620 break;
1621 }
1622 }
1623 brq->data.sg_len = i;
1624 }
1625
1626 mqrq->areq.mrq = &brq->mrq;
1627
1628 mmc_queue_bounce_pre(mqrq);
1629}
1630
1631static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1632 struct mmc_card *card,
1633 int disable_multi,
1634 struct mmc_queue *mq)
1635{
1636 u32 readcmd, writecmd;
1637 struct mmc_blk_request *brq = &mqrq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001638 struct request *req = mmc_queue_req_to_req(mqrq);
Adrian Hunterca5717f2017-03-13 14:36:41 +02001639 struct mmc_blk_data *md = mq->blkdata;
1640 bool do_rel_wr, do_data_tag;
1641
1642 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1643
1644 brq->mrq.cmd = &brq->cmd;
1645
1646 brq->cmd.arg = blk_rq_pos(req);
1647 if (!mmc_card_blockaddr(card))
1648 brq->cmd.arg <<= 9;
1649 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1650
Per Forlin54d49d72011-07-01 18:55:29 +02001651 if (brq->data.blocks > 1 || do_rel_wr) {
1652 /* SPI multiblock writes terminate using a special
1653 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001654 */
Per Forlin54d49d72011-07-01 18:55:29 +02001655 if (!mmc_host_is_spi(card->host) ||
1656 rq_data_dir(req) == READ)
1657 brq->mrq.stop = &brq->stop;
1658 readcmd = MMC_READ_MULTIPLE_BLOCK;
1659 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1660 } else {
1661 brq->mrq.stop = NULL;
1662 readcmd = MMC_READ_SINGLE_BLOCK;
1663 writecmd = MMC_WRITE_BLOCK;
1664 }
Adrian Hunterca5717f2017-03-13 14:36:41 +02001665 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
Saugata Das42659002011-12-21 13:09:17 +05301666
1667 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001668 * Pre-defined multi-block transfers are preferable to
1669 * open ended-ones (and necessary for reliable writes).
1670 * However, it is not sufficient to just send CMD23,
1671 * and avoid the final CMD12, as on an error condition
1672 * CMD12 (stop) needs to be sent anyway. This, coupled
1673 * with Auto-CMD23 enhancements provided by some
1674 * hosts, means that the complexity of dealing
1675 * with this is best left to the host. If CMD23 is
1676 * supported by card and host, we'll fill sbc in and let
1677 * the host deal with handling it correctly. This means
1678 * that for hosts that don't expose MMC_CAP_CMD23, no
1679 * change of behavior will be observed.
1680 *
1681 * N.B: Some MMC cards experience perf degradation.
1682 * We'll avoid using CMD23-bounded multiblock writes for
1683 * these, while retaining features like reliable writes.
1684 */
Saugata Das42659002011-12-21 13:09:17 +05301685 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1686 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1687 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001688 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1689 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301690 (do_rel_wr ? (1 << 31) : 0) |
1691 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001692 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1693 brq->mrq.sbc = &brq->sbc;
1694 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001695
Linus Walleij74f5ba32017-02-01 13:47:55 +01001696 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlin54d49d72011-07-01 18:55:29 +02001697}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Linus Walleij0e65f102017-02-01 13:47:58 +01001699static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1700 struct mmc_blk_request *brq, struct request *req,
1701 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001702{
Linus Walleij0e65f102017-02-01 13:47:58 +01001703 bool req_pending;
1704
Adrian Hunter67716322011-08-29 16:42:15 +03001705 /*
1706 * If this is an SD card and we're writing, we can first
1707 * mark the known good sectors as ok.
1708 *
1709 * If the card is not SD, we can still ok written sectors
1710 * as reported by the controller (which might be less than
1711 * the real number of written sectors, but never more).
1712 */
1713 if (mmc_card_sd(card)) {
1714 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001715 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001716
Linus Walleij169f03a2017-02-01 13:47:57 +01001717 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001718 if (err)
1719 req_pending = old_req_pending;
1720 else
1721 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001722 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001723 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001724 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001725 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001726}
1727
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001728static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1729 struct request *req,
1730 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001731{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001732 if (mmc_card_removed(card))
1733 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001734 while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
Linus Walleij304419d2017-05-18 11:29:32 +02001735 mq->qcnt--;
Linus Walleij4e1f7802017-01-24 11:17:52 +01001736}
1737
Linus Walleijb2928e12017-02-01 13:47:54 +01001738/**
1739 * mmc_blk_rw_try_restart() - tries to restart the current async request
1740 * @mq: the queue with the card and host to restart
1741 * @req: a new request that want to be started after the current one
1742 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001743static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1744 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001745{
1746 if (!req)
1747 return;
1748
Linus Walleijb2928e12017-02-01 13:47:54 +01001749 /*
1750 * If the card was removed, just cancel everything and return.
1751 */
1752 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001753 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001754 blk_end_request_all(req, BLK_STS_IOERR);
Linus Walleij304419d2017-05-18 11:29:32 +02001755 mq->qcnt--; /* FIXME: just set to 0? */
Linus Walleijb2928e12017-02-01 13:47:54 +01001756 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001757 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001758 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001759 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1760 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001761}
1762
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001763static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001764{
Linus Walleij7db30282016-11-18 13:36:15 +01001765 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001766 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001767 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001768 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001769 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001770 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001771 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001772 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001773 struct mmc_async_req *new_areq;
1774 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001775 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001776
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001777 if (new_req) {
Linus Walleij304419d2017-05-18 11:29:32 +02001778 mqrq_cur = req_to_mmc_queue_req(new_req);
1779 mq->qcnt++;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001780 }
1781
1782 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001783 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001784
1785 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001786 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301787 /*
1788 * When 4KB native sector is enabled, only 8 blocks
1789 * multiple read or write is allowed
1790 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001791 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001792 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301793 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001794 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001795 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001796 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301797 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001798
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001799 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1800 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001801 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001802 new_areq = NULL;
1803
Linus Walleijc3399ef2017-02-01 13:47:53 +01001804 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001805 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001806 /*
1807 * We have just put the first request into the pipeline
1808 * and there is nothing more to do until it is
1809 * complete.
1810 */
Linus Walleijdf061582017-01-24 11:17:57 +01001811 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001812 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001813
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001814 /*
1815 * An asynchronous request has been completed and we proceed
1816 * to handle the result of it.
1817 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001818 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001819 brq = &mq_rq->brq;
Linus Walleij67e69d52017-05-19 15:37:27 +02001820 old_req = mmc_queue_req_to_req(mq_rq);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001821 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001822 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001823
Per Forlind78d4a8a2011-07-01 18:55:30 +02001824 switch (status) {
1825 case MMC_BLK_SUCCESS:
1826 case MMC_BLK_PARTIAL:
1827 /*
1828 * A block was successfully transferred.
1829 */
Adrian Hunter67716322011-08-29 16:42:15 +03001830 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001831
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001832 req_pending = blk_end_request(old_req, BLK_STS_OK,
Linus Walleij0e65f102017-02-01 13:47:58 +01001833 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001834 /*
1835 * If the blk_end_request function returns non-zero even
1836 * though all data has been transferred and no errors
1837 * were returned by the host controller, it's a bug.
1838 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001839 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301840 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001841 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001842 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001843 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001844 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001845 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001846 break;
1847 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001848 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001849 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001850 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001851 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1852 else
Linus Walleij304419d2017-05-18 11:29:32 +02001853 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001854 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001855 return;
1856 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001857 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001858 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001859 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001860 return;
1861 }
Ding Wang29535f72015-05-18 20:14:15 +08001862 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001863 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001864 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001865 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001866 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001867 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001868 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001869 if (!mmc_blk_reset(md, card->host, type))
1870 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001871 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001872 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001873 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001874 case MMC_BLK_DATA_ERR: {
1875 int err;
1876
1877 err = mmc_blk_reset(md, card->host, type);
1878 if (!err)
1879 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001880 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001881 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001882 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001883 return;
1884 }
Adrian Hunter67716322011-08-29 16:42:15 +03001885 /* Fall through */
1886 }
1887 case MMC_BLK_ECC_ERR:
1888 if (brq->data.blocks > 1) {
1889 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001890 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001891 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001892 disable_multi = 1;
1893 break;
1894 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001895 /*
1896 * After an error, we redo I/O one sector at a
1897 * time, so we only reach here after trying to
1898 * read a single sector.
1899 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001900 req_pending = blk_end_request(old_req, BLK_STS_IOERR,
Linus Walleij0e65f102017-02-01 13:47:58 +01001901 brq->data.blksz);
1902 if (!req_pending) {
Linus Walleij304419d2017-05-18 11:29:32 +02001903 mq->qcnt--;
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001904 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001905 return;
1906 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001907 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301908 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001909 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001910 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001911 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001912 default:
1913 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001914 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001915 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001916 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001917 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001918 }
1919
Linus Walleij0e65f102017-02-01 13:47:58 +01001920 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001921 /*
1922 * In case of a incomplete request
1923 * prepare it again and resend.
1924 */
1925 mmc_blk_rw_rq_prep(mq_rq, card,
1926 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001927 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001928 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001929 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001930 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001931 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001932
Linus Walleij304419d2017-05-18 11:29:32 +02001933 mq->qcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Linus Walleijdf061582017-01-24 11:17:57 +01001936void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001937{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001938 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001939 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001940 struct mmc_card *card = md->queue.card;
1941
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001942 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001943 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001944 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001945
Andrei Warkentin371a6892011-04-11 18:10:25 -05001946 ret = mmc_blk_part_switch(card, md);
1947 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001948 if (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001949 blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001950 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001951 goto out;
1952 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001953
Linus Walleij614f0382017-05-18 11:29:34 +02001954 if (req) {
1955 switch (req_op(req)) {
1956 case REQ_OP_DRV_IN:
1957 case REQ_OP_DRV_OUT:
1958 /*
1959 * Complete ongoing async transfer before issuing
1960 * ioctl()s
1961 */
1962 if (mq->qcnt)
1963 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleij02166a02017-05-19 15:37:28 +02001964 mmc_blk_issue_drv_op(mq, req);
Linus Walleij614f0382017-05-18 11:29:34 +02001965 break;
1966 case REQ_OP_DISCARD:
1967 /*
1968 * Complete ongoing async transfer before issuing
1969 * discard.
1970 */
1971 if (mq->qcnt)
1972 mmc_blk_issue_rw_rq(mq, NULL);
1973 mmc_blk_issue_discard_rq(mq, req);
1974 break;
1975 case REQ_OP_SECURE_ERASE:
1976 /*
1977 * Complete ongoing async transfer before issuing
1978 * secure erase.
1979 */
1980 if (mq->qcnt)
1981 mmc_blk_issue_rw_rq(mq, NULL);
1982 mmc_blk_issue_secdiscard_rq(mq, req);
1983 break;
1984 case REQ_OP_FLUSH:
1985 /*
1986 * Complete ongoing async transfer before issuing
1987 * flush.
1988 */
1989 if (mq->qcnt)
1990 mmc_blk_issue_rw_rq(mq, NULL);
1991 mmc_blk_issue_flush(mq, req);
1992 break;
1993 default:
1994 /* Normal request, just issue it */
1995 mmc_blk_issue_rw_rq(mq, req);
1996 card->host->context_info.is_waiting_last_req = false;
1997 break;
Wu Fengguang73222382017-05-23 05:11:33 +08001998 }
Adrian Hunter49804542010-08-11 14:17:50 -07001999 } else {
Linus Walleij614f0382017-05-18 11:29:34 +02002000 /* No request, flushing the pipeline with NULL */
2001 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter2602b742017-03-13 14:36:32 +02002002 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07002003 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002004
Andrei Warkentin371a6892011-04-11 18:10:25 -05002005out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02002006 if (!mq->qcnt)
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002007 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07002008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Russell Kinga6f6c962006-01-03 22:38:44 +00002010static inline int mmc_blk_readonly(struct mmc_card *card)
2011{
2012 return mmc_card_readonly(card) ||
2013 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2014}
2015
Andrei Warkentin371a6892011-04-11 18:10:25 -05002016static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2017 struct device *parent,
2018 sector_t size,
2019 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002020 const char *subname,
2021 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct mmc_blk_data *md;
2024 int devidx, ret;
2025
Heiner Kallweita04848c2017-02-01 19:44:22 +01002026 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2027 if (devidx < 0)
2028 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002030 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002031 if (!md) {
2032 ret = -ENOMEM;
2033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002035
Johan Rudholmadd710e2011-12-02 08:51:06 +01002036 md->area_type = area_type;
2037
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002038 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002039 * Set the read-only status based on the supported commands
2040 * and the write protect switch.
2041 */
2042 md->read_only = mmc_blk_readonly(card);
2043
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002044 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002045 if (md->disk == NULL) {
2046 ret = -ENOMEM;
2047 goto err_kfree;
2048 }
2049
2050 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002051 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002052 md->usage = 1;
2053
Adrian Hunterd09408a2011-06-23 13:40:28 +03002054 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002055 if (ret)
2056 goto err_putdisk;
2057
Linus Walleij7db30282016-11-18 13:36:15 +01002058 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002059
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002060 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002061 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002062 md->disk->fops = &mmc_bdops;
2063 md->disk->private_data = md;
2064 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002065 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002066 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002067 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002068 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002069 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002070
2071 /*
2072 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2073 *
2074 * - be set for removable media with permanent block devices
2075 * - be unset for removable block devices with permanent media
2076 *
2077 * Since MMC block devices clearly fall under the second
2078 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2079 * should use the block device creation/destruction hotplug
2080 * messages to tell when the card is present.
2081 */
2082
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002083 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002084 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002085
Saugata Dasa5075eb2012-05-17 16:32:21 +05302086 if (mmc_card_mmc(card))
2087 blk_queue_logical_block_size(md->queue.queue,
2088 card->ext_csd.data_sector_size);
2089 else
2090 blk_queue_logical_block_size(md->queue.queue, 512);
2091
Andrei Warkentin371a6892011-04-11 18:10:25 -05002092 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002093
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002094 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002095 if ((mmc_card_mmc(card) &&
2096 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002097 (mmc_card_sd(card) &&
2098 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2099 md->flags |= MMC_BLK_CMD23;
2100 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002101
2102 if (mmc_card_mmc(card) &&
2103 md->flags & MMC_BLK_CMD23 &&
2104 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2105 card->ext_csd.rel_sectors)) {
2106 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002107 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002108 }
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002111
2112 err_putdisk:
2113 put_disk(md->disk);
2114 err_kfree:
2115 kfree(md);
2116 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01002117 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00002118 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Andrei Warkentin371a6892011-04-11 18:10:25 -05002121static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2122{
2123 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002124
2125 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2126 /*
2127 * The EXT_CSD sector count is in number or 512 byte
2128 * sectors.
2129 */
2130 size = card->ext_csd.sectors;
2131 } else {
2132 /*
2133 * The CSD capacity field is in units of read_blkbits.
2134 * set_capacity takes units of 512 bytes.
2135 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002136 size = (typeof(sector_t))card->csd.capacity
2137 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002138 }
2139
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002140 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002141 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002142}
2143
2144static int mmc_blk_alloc_part(struct mmc_card *card,
2145 struct mmc_blk_data *md,
2146 unsigned int part_type,
2147 sector_t size,
2148 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002149 const char *subname,
2150 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002151{
2152 char cap_str[10];
2153 struct mmc_blk_data *part_md;
2154
2155 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002156 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002157 if (IS_ERR(part_md))
2158 return PTR_ERR(part_md);
2159 part_md->part_type = part_type;
2160 list_add(&part_md->part, &md->part);
2161
James Bottomleyb9f28d82015-03-05 18:47:01 -08002162 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002163 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302164 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165 part_md->disk->disk_name, mmc_card_id(card),
2166 mmc_card_name(card), part_md->part_type, cap_str);
2167 return 0;
2168}
2169
Namjae Jeone0c368d2011-10-06 23:41:38 +09002170/* MMC Physical partitions consist of two boot partitions and
2171 * up to four general purpose partitions.
2172 * For each partition enabled in EXT_CSD a block device will be allocatedi
2173 * to provide access to the partition.
2174 */
2175
Andrei Warkentin371a6892011-04-11 18:10:25 -05002176static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2177{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002178 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002179
2180 if (!mmc_card_mmc(card))
2181 return 0;
2182
Namjae Jeone0c368d2011-10-06 23:41:38 +09002183 for (idx = 0; idx < card->nr_parts; idx++) {
2184 if (card->part[idx].size) {
2185 ret = mmc_blk_alloc_part(card, md,
2186 card->part[idx].part_cfg,
2187 card->part[idx].size >> 9,
2188 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002189 card->part[idx].name,
2190 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002191 if (ret)
2192 return ret;
2193 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002194 }
2195
2196 return ret;
2197}
2198
Andrei Warkentin371a6892011-04-11 18:10:25 -05002199static void mmc_blk_remove_req(struct mmc_blk_data *md)
2200{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002201 struct mmc_card *card;
2202
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002204 /*
2205 * Flush remaining requests and free queues. It
2206 * is freeing the queue that stops new requests
2207 * from being accepted.
2208 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002209 card = md->queue.card;
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002210 spin_lock_irq(md->queue.queue->queue_lock);
Shawn Lin7c84b8b2017-07-25 09:11:28 +08002211 queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
Michał Mirosław3f8b23a2017-08-09 01:48:59 +02002212 spin_unlock_irq(md->queue.queue->queue_lock);
Grzegorz Slujabbdc74d2017-07-13 11:17:58 +02002213 blk_set_queue_dying(md->queue.queue);
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002214 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002215 if (md->disk->flags & GENHD_FL_UP) {
2216 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002217 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2218 card->ext_csd.boot_ro_lockable)
2219 device_remove_file(disk_to_dev(md->disk),
2220 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002221
Andrei Warkentin371a6892011-04-11 18:10:25 -05002222 del_gendisk(md->disk);
2223 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002224 mmc_blk_put(md);
2225 }
2226}
2227
2228static void mmc_blk_remove_parts(struct mmc_card *card,
2229 struct mmc_blk_data *md)
2230{
2231 struct list_head *pos, *q;
2232 struct mmc_blk_data *part_md;
2233
2234 list_for_each_safe(pos, q, &md->part) {
2235 part_md = list_entry(pos, struct mmc_blk_data, part);
2236 list_del(pos);
2237 mmc_blk_remove_req(part_md);
2238 }
2239}
2240
2241static int mmc_add_disk(struct mmc_blk_data *md)
2242{
2243 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002244 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002245
Dan Williams307d8e62016-06-20 10:40:44 -07002246 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002247 md->force_ro.show = force_ro_show;
2248 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302249 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002250 md->force_ro.attr.name = "force_ro";
2251 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2252 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2253 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002254 goto force_ro_fail;
2255
2256 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2257 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002258 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002259
2260 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2261 mode = S_IRUGO;
2262 else
2263 mode = S_IRUGO | S_IWUSR;
2264
2265 md->power_ro_lock.show = power_ro_lock_show;
2266 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002267 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002268 md->power_ro_lock.attr.mode = mode;
2269 md->power_ro_lock.attr.name =
2270 "ro_lock_until_next_power_on";
2271 ret = device_create_file(disk_to_dev(md->disk),
2272 &md->power_ro_lock);
2273 if (ret)
2274 goto power_ro_lock_fail;
2275 }
2276 return ret;
2277
2278power_ro_lock_fail:
2279 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2280force_ro_fail:
2281 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002282
2283 return ret;
2284}
2285
Ulf Hansson96541ba2015-04-14 13:06:12 +02002286static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002288 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002289 char cap_str[10];
2290
Pierre Ossman912490d2005-05-21 10:27:02 +01002291 /*
2292 * Check that the card supports the command class(es) we need.
2293 */
2294 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -ENODEV;
2296
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002297 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 md = mmc_blk_alloc(card);
Linus Walleij304419d2017-05-18 11:29:32 +02002300 if (IS_ERR(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return PTR_ERR(md);
2302
James Bottomleyb9f28d82015-03-05 18:47:01 -08002303 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002304 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302305 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002307 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 if (mmc_blk_alloc_parts(card, md))
2310 goto out;
2311
Ulf Hansson96541ba2015-04-14 13:06:12 +02002312 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002313
Andrei Warkentin371a6892011-04-11 18:10:25 -05002314 if (mmc_add_disk(md))
2315 goto out;
2316
2317 list_for_each_entry(part_md, &md->part, part) {
2318 if (mmc_add_disk(part_md))
2319 goto out;
2320 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002321
2322 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2323 pm_runtime_use_autosuspend(&card->dev);
2324
2325 /*
2326 * Don't enable runtime PM for SD-combo cards here. Leave that
2327 * decision to be taken during the SDIO init sequence instead.
2328 */
2329 if (card->type != MMC_TYPE_SD_COMBO) {
2330 pm_runtime_set_active(&card->dev);
2331 pm_runtime_enable(&card->dev);
2332 }
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return 0;
2335
2336 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002337 mmc_blk_remove_parts(card, md);
2338 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Ulf Hansson96541ba2015-04-14 13:06:12 +02002342static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002344 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Andrei Warkentin371a6892011-04-11 18:10:25 -05002346 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002347 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002348 mmc_claim_host(card->host);
2349 mmc_blk_part_switch(card, md);
2350 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002351 if (card->type != MMC_TYPE_SD_COMBO)
2352 pm_runtime_disable(&card->dev);
2353 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002354 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002355 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Ulf Hansson96541ba2015-04-14 13:06:12 +02002358static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002360 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002361 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 if (md) {
2364 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002365 list_for_each_entry(part_md, &md->part, part) {
2366 mmc_queue_suspend(&part_md->queue);
2367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 return 0;
2370}
2371
Ulf Hansson96541ba2015-04-14 13:06:12 +02002372static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002373{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002374 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002375}
2376
Ulf Hansson0967edc2014-10-06 11:29:42 +02002377#ifdef CONFIG_PM_SLEEP
2378static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002379{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002380 struct mmc_card *card = mmc_dev_to_card(dev);
2381
2382 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002383}
2384
Ulf Hansson0967edc2014-10-06 11:29:42 +02002385static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002387 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002388 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002391 /*
2392 * Resume involves the card going into idle state,
2393 * so current partition is always the main one.
2394 */
2395 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002397 list_for_each_entry(part_md, &md->part, part) {
2398 mmc_queue_resume(&part_md->queue);
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401 return 0;
2402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403#endif
2404
Ulf Hansson0967edc2014-10-06 11:29:42 +02002405static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2406
Ulf Hansson96541ba2015-04-14 13:06:12 +02002407static struct mmc_driver mmc_driver = {
2408 .drv = {
2409 .name = "mmcblk",
2410 .pm = &mmc_blk_pm_ops,
2411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 .probe = mmc_blk_probe,
2413 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002414 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415};
2416
2417static int __init mmc_blk_init(void)
2418{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002419 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002421 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2422 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2423
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002424 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002425
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002426 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2427 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002430 res = mmc_register_driver(&mmc_driver);
2431 if (res)
2432 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002434 return 0;
2435 out2:
2436 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 out:
2438 return res;
2439}
2440
2441static void __exit mmc_blk_exit(void)
2442{
2443 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002444 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
2447module_init(mmc_blk_init);
2448module_exit(mmc_blk_exit);
2449
2450MODULE_LICENSE("GPL");
2451MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2452