blob: cd909aecfdedfb2cf7b91657bb26736ec7e42b59 [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"
55#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000057MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040058#ifdef MODULE_PARAM_PREFIX
59#undef MODULE_PARAM_PREFIX
60#endif
61#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010062
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050063#define INAND_CMD38_ARG_EXT_CSD 113
64#define INAND_CMD38_ARG_ERASE 0x00
65#define INAND_CMD38_ARG_TRIM 0x01
66#define INAND_CMD38_ARG_SECERASE 0x80
67#define INAND_CMD38_ARG_SECTRIM1 0x81
68#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060069#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030070#define MMC_SANITIZE_REQ_TIMEOUT 240000
71#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050072
Luca Porziod3df0462015-11-06 15:12:26 +000073#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090074 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020075static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040076
77/*
78 * The defaults come from config options but can be overriden by module
79 * or bootarg options.
80 */
81static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82
83/*
84 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020086 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040087 */
88static int max_devices;
89
Ben Hutchingsa26eba62014-11-06 03:35:09 +000090#define MAX_DEVICES 256
91
Ulf Hanssonb10fa992016-04-07 14:36:46 +020092static DEFINE_IDA(mmc_blk_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * There is one mmc_blk_data per slot.
96 */
97struct mmc_blk_data {
98 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070099 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct gendisk *disk;
101 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500104 unsigned int flags;
105#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
106#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000109 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500110 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300111 unsigned int reset_done;
112#define MMC_BLK_READ BIT(0)
113#define MMC_BLK_WRITE BIT(1)
114#define MMC_BLK_DISCARD BIT(2)
115#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116
117 /*
118 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200119 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500120 * track of the current selected device partition.
121 */
122 unsigned int part_curr;
123 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 struct device_attribute power_ro_lock;
125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400130module_param(perdev_minors, int, 0444);
131MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
132
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200133static inline int mmc_blk_part_switch(struct mmc_card *card,
134 struct mmc_blk_data *md);
135static int get_card_status(struct mmc_card *card, u32 *status, int retries);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
138{
139 struct mmc_blk_data *md;
140
Arjan van de Vena621aae2006-01-12 18:43:35 +0000141 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 md = disk->private_data;
143 if (md && md->usage == 0)
144 md = NULL;
145 if (md)
146 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000147 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return md;
150}
151
Andrei Warkentin371a6892011-04-11 18:10:25 -0500152static inline int mmc_get_devidx(struct gendisk *disk)
153{
Colin Cross382c55f2015-10-22 10:00:41 -0700154 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500155 return devidx;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void mmc_blk_put(struct mmc_blk_data *md)
159{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000160 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 md->usage--;
162 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500163 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800164 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100165 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 kfree(md);
168 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000169 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Johan Rudholmadd710e2011-12-02 08:51:06 +0100172static ssize_t power_ro_lock_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 int ret;
176 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
177 struct mmc_card *card = md->queue.card;
178 int locked = 0;
179
180 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
181 locked = 2;
182 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
183 locked = 1;
184
185 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
186
Tomas Winkler9098f842015-07-16 15:50:45 +0200187 mmc_blk_put(md);
188
Johan Rudholmadd710e2011-12-02 08:51:06 +0100189 return ret;
190}
191
192static ssize_t power_ro_lock_store(struct device *dev,
193 struct device_attribute *attr, const char *buf, size_t count)
194{
195 int ret;
196 struct mmc_blk_data *md, *part_md;
197 struct mmc_card *card;
198 unsigned long set;
199
200 if (kstrtoul(buf, 0, &set))
201 return -EINVAL;
202
203 if (set != 1)
204 return count;
205
206 md = mmc_blk_get(dev_to_disk(dev));
207 card = md->queue.card;
208
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200209 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100210
211 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
212 card->ext_csd.boot_ro_lock |
213 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
214 card->ext_csd.part_time);
215 if (ret)
216 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
217 else
218 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
219
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200220 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100221
222 if (!ret) {
223 pr_info("%s: Locking boot partition ro until next power on\n",
224 md->disk->disk_name);
225 set_disk_ro(md->disk, 1);
226
227 list_for_each_entry(part_md, &md->part, part)
228 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
229 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
230 set_disk_ro(part_md->disk, 1);
231 }
232 }
233
234 mmc_blk_put(md);
235 return count;
236}
237
Andrei Warkentin371a6892011-04-11 18:10:25 -0500238static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
239 char *buf)
240{
241 int ret;
242 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
243
Baruch Siach0031a982014-09-22 10:12:51 +0300244 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500245 get_disk_ro(dev_to_disk(dev)) ^
246 md->read_only);
247 mmc_blk_put(md);
248 return ret;
249}
250
251static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
253{
254 int ret;
255 char *end;
256 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
257 unsigned long set = simple_strtoul(buf, &end, 0);
258 if (end == buf) {
259 ret = -EINVAL;
260 goto out;
261 }
262
263 set_disk_ro(dev_to_disk(dev), set || md->read_only);
264 ret = count;
265out:
266 mmc_blk_put(md);
267 return ret;
268}
269
Al Viroa5a15612008-03-02 10:33:30 -0500270static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Al Viroa5a15612008-03-02 10:33:30 -0500272 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int ret = -ENXIO;
274
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200275 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (md) {
277 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500278 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700280
Al Viroa5a15612008-03-02 10:33:30 -0500281 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700282 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700283 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200286 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return ret;
289}
290
Al Virodb2a1442013-05-05 21:52:57 -0400291static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Al Viroa5a15612008-03-02 10:33:30 -0500293 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200295 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200297 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800301mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800303 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
304 geo->heads = 4;
305 geo->sectors = 16;
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
John Calixtocb87ea22011-04-26 18:56:29 -0400309struct mmc_blk_ioc_data {
310 struct mmc_ioc_cmd ic;
311 unsigned char *buf;
312 u64 buf_bytes;
313};
314
315static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
316 struct mmc_ioc_cmd __user *user)
317{
318 struct mmc_blk_ioc_data *idata;
319 int err;
320
yalin wang1ff89502015-11-12 19:27:11 +0800321 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400322 if (!idata) {
323 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400324 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400325 }
326
327 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
328 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400329 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400330 }
331
332 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
333 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
334 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400335 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400336 }
337
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300338 if (!idata->buf_bytes) {
339 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100340 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300341 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100342
yalin wang1ff89502015-11-12 19:27:11 +0800343 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400344 if (!idata->buf) {
345 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400346 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400347 }
348
349 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
350 idata->ic.data_ptr, idata->buf_bytes)) {
351 err = -EFAULT;
352 goto copy_err;
353 }
354
355 return idata;
356
357copy_err:
358 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400359idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400360 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400361out:
John Calixtocb87ea22011-04-26 18:56:29 -0400362 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400363}
364
Jon Huntera5f57742015-09-22 10:27:53 +0100365static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
366 struct mmc_blk_ioc_data *idata)
367{
368 struct mmc_ioc_cmd *ic = &idata->ic;
369
370 if (copy_to_user(&(ic_ptr->response), ic->response,
371 sizeof(ic->response)))
372 return -EFAULT;
373
374 if (!idata->ic.write_flag) {
375 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
376 idata->buf, idata->buf_bytes))
377 return -EFAULT;
378 }
379
380 return 0;
381}
382
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200383static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
384 u32 retries_max)
385{
386 int err;
387 u32 retry_count = 0;
388
389 if (!status || !retries_max)
390 return -EINVAL;
391
392 do {
393 err = get_card_status(card, status, 5);
394 if (err)
395 break;
396
397 if (!R1_STATUS(*status) &&
398 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
399 break; /* RPMB programming operation complete */
400
401 /*
402 * Rechedule to give the MMC device a chance to continue
403 * processing the previous command without being polled too
404 * frequently.
405 */
406 usleep_range(1000, 5000);
407 } while (++retry_count < retries_max);
408
409 if (retry_count == retries_max)
410 err = -EPERM;
411
412 return err;
413}
414
Maya Erez775a9362013-04-18 15:41:55 +0300415static int ioctl_do_sanitize(struct mmc_card *card)
416{
417 int err;
418
Ulf Hanssona2d10862013-12-16 14:37:26 +0100419 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300420 pr_warn("%s: %s - SANITIZE is not supported\n",
421 mmc_hostname(card->host), __func__);
422 err = -EOPNOTSUPP;
423 goto out;
424 }
425
426 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
427 mmc_hostname(card->host), __func__);
428
429 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
430 EXT_CSD_SANITIZE_START, 1,
431 MMC_SANITIZE_REQ_TIMEOUT);
432
433 if (err)
434 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
435 mmc_hostname(card->host), __func__, err);
436
437 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
438 __func__);
439out:
440 return err;
441}
442
Jon Huntera5f57742015-09-22 10:27:53 +0100443static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
444 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400445{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900446 struct mmc_command cmd = {};
447 struct mmc_data data = {};
448 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400449 struct scatterlist sg;
450 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200451 int is_rpmb = false;
452 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400453
Jon Huntera5f57742015-09-22 10:27:53 +0100454 if (!card || !md || !idata)
455 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400456
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200457 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
458 is_rpmb = true;
459
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100460 cmd.opcode = idata->ic.opcode;
461 cmd.arg = idata->ic.arg;
462 cmd.flags = idata->ic.flags;
463
464 if (idata->buf_bytes) {
465 data.sg = &sg;
466 data.sg_len = 1;
467 data.blksz = idata->ic.blksz;
468 data.blocks = idata->ic.blocks;
469
470 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
471
472 if (idata->ic.write_flag)
473 data.flags = MMC_DATA_WRITE;
474 else
475 data.flags = MMC_DATA_READ;
476
477 /* data.flags must already be set before doing this. */
478 mmc_set_data_timeout(&data, card);
479
480 /* Allow overriding the timeout_ns for empirical tuning. */
481 if (idata->ic.data_timeout_ns)
482 data.timeout_ns = idata->ic.data_timeout_ns;
483
484 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
485 /*
486 * Pretend this is a data transfer and rely on the
487 * host driver to compute timeout. When all host
488 * drivers support cmd.cmd_timeout for R1B, this
489 * can be changed to:
490 *
491 * mrq.data = NULL;
492 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
493 */
494 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
495 }
496
497 mrq.data = &data;
498 }
499
500 mrq.cmd = &cmd;
501
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200502 err = mmc_blk_part_switch(card, md);
503 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100504 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200505
John Calixtocb87ea22011-04-26 18:56:29 -0400506 if (idata->ic.is_acmd) {
507 err = mmc_app_cmd(card->host, card);
508 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100509 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400510 }
511
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200512 if (is_rpmb) {
513 err = mmc_set_blockcount(card, data.blocks,
514 idata->ic.write_flag & (1 << 31));
515 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100516 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200517 }
518
Yaniv Gardia82e4842013-06-05 14:13:08 +0300519 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
520 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300521 err = ioctl_do_sanitize(card);
522
523 if (err)
524 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
525 __func__, err);
526
Jon Huntera5f57742015-09-22 10:27:53 +0100527 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300528 }
529
John Calixtocb87ea22011-04-26 18:56:29 -0400530 mmc_wait_for_req(card->host, &mrq);
531
532 if (cmd.error) {
533 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
534 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100535 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400536 }
537 if (data.error) {
538 dev_err(mmc_dev(card->host), "%s: data error %d\n",
539 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100540 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400541 }
542
543 /*
544 * According to the SD specs, some commands require a delay after
545 * issuing the command.
546 */
547 if (idata->ic.postsleep_min_us)
548 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
549
Jon Huntera5f57742015-09-22 10:27:53 +0100550 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400551
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200552 if (is_rpmb) {
553 /*
554 * Ensure RPMB command has completed by polling CMD13
555 * "Send Status".
556 */
557 err = ioctl_rpmb_card_status_poll(card, &status, 5);
558 if (err)
559 dev_err(mmc_dev(card->host),
560 "%s: Card Status=0x%08X, error %d\n",
561 __func__, status, err);
562 }
563
Jon Huntera5f57742015-09-22 10:27:53 +0100564 return err;
565}
566
567static int mmc_blk_ioctl_cmd(struct block_device *bdev,
568 struct mmc_ioc_cmd __user *ic_ptr)
569{
570 struct mmc_blk_ioc_data *idata;
571 struct mmc_blk_data *md;
572 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700573 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100574
Shawn Lin83c742c2016-03-16 18:15:47 +0800575 /*
576 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
577 * whole block device, not on a partition. This prevents overspray
578 * between sibling partitions.
579 */
580 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
581 return -EPERM;
582
Jon Huntera5f57742015-09-22 10:27:53 +0100583 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
584 if (IS_ERR(idata))
585 return PTR_ERR(idata);
586
587 md = mmc_blk_get(bdev->bd_disk);
588 if (!md) {
589 err = -EINVAL;
590 goto cmd_err;
591 }
592
593 card = md->queue.card;
594 if (IS_ERR(card)) {
595 err = PTR_ERR(card);
596 goto cmd_done;
597 }
598
599 mmc_get_card(card);
600
Grant Grundlerb0934102015-09-23 18:30:33 -0700601 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100602
Adrian Hunter3c866562016-05-04 14:38:12 +0300603 /* Always switch back to main area after RPMB access */
604 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
605 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
606
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200607 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400608
Grant Grundlerb0934102015-09-23 18:30:33 -0700609 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100610
John Calixtocb87ea22011-04-26 18:56:29 -0400611cmd_done:
612 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300613cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400614 kfree(idata->buf);
615 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700616 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400617}
618
Jon Huntera5f57742015-09-22 10:27:53 +0100619static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
620 struct mmc_ioc_multi_cmd __user *user)
621{
622 struct mmc_blk_ioc_data **idata = NULL;
623 struct mmc_ioc_cmd __user *cmds = user->cmds;
624 struct mmc_card *card;
625 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700626 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100627 __u64 num_of_cmds;
628
Shawn Lin83c742c2016-03-16 18:15:47 +0800629 /*
630 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
631 * whole block device, not on a partition. This prevents overspray
632 * between sibling partitions.
633 */
634 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
635 return -EPERM;
636
Jon Huntera5f57742015-09-22 10:27:53 +0100637 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
638 sizeof(num_of_cmds)))
639 return -EFAULT;
640
641 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
669 mmc_get_card(card);
670
Grant Grundlerb0934102015-09-23 18:30:33 -0700671 for (i = 0; i < num_of_cmds && !ioc_err; i++)
672 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100673
Adrian Hunter3c866562016-05-04 14:38:12 +0300674 /* Always switch back to main area after RPMB access */
675 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
676 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
677
Jon Huntera5f57742015-09-22 10:27:53 +0100678 mmc_put_card(card);
679
680 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700681 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100682 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100683
684cmd_done:
685 mmc_blk_put(md);
686cmd_err:
687 for (i = 0; i < num_of_cmds; i++) {
688 kfree(idata[i]->buf);
689 kfree(idata[i]);
690 }
691 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700692 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100693}
694
John Calixtocb87ea22011-04-26 18:56:29 -0400695static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
696 unsigned int cmd, unsigned long arg)
697{
Jon Huntera5f57742015-09-22 10:27:53 +0100698 switch (cmd) {
699 case MMC_IOC_CMD:
700 return mmc_blk_ioctl_cmd(bdev,
701 (struct mmc_ioc_cmd __user *)arg);
702 case MMC_IOC_MULTI_CMD:
703 return mmc_blk_ioctl_multi_cmd(bdev,
704 (struct mmc_ioc_multi_cmd __user *)arg);
705 default:
706 return -EINVAL;
707 }
John Calixtocb87ea22011-04-26 18:56:29 -0400708}
709
710#ifdef CONFIG_COMPAT
711static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
712 unsigned int cmd, unsigned long arg)
713{
714 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
715}
716#endif
717
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700718static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500719 .open = mmc_blk_open,
720 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800721 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400723 .ioctl = mmc_blk_ioctl,
724#ifdef CONFIG_COMPAT
725 .compat_ioctl = mmc_blk_compat_ioctl,
726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727};
728
Andrei Warkentin371a6892011-04-11 18:10:25 -0500729static inline int mmc_blk_part_switch(struct mmc_card *card,
730 struct mmc_blk_data *md)
731{
732 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200733 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300734
Andrei Warkentin371a6892011-04-11 18:10:25 -0500735 if (main_md->part_curr == md->part_type)
736 return 0;
737
738 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300739 u8 part_config = card->ext_csd.part_config;
740
Adrian Hunter57da0c02016-05-04 14:38:13 +0300741 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
742 mmc_retune_pause(card->host);
743
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300744 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
745 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500746
747 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300748 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500749 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300750 if (ret) {
751 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
752 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500753 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300754 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300755
756 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300757
758 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
759 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300760 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500761
762 main_md->part_curr = md->part_type;
763 return 0;
764}
765
Linus Walleij169f03a2017-02-01 13:47:57 +0100766static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700767{
768 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100769 u32 result;
770 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700771
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900772 struct mmc_request mrq = {};
773 struct mmc_command cmd = {};
774 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700775
776 struct scatterlist sg;
777
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700778 cmd.opcode = MMC_APP_CMD;
779 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700780 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700781
782 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700783 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100784 return err;
David Brownell7213d172007-08-08 09:10:23 -0700785 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100786 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700787
788 memset(&cmd, 0, sizeof(struct mmc_command));
789
790 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
791 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700792 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700793
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700794 data.blksz = 4;
795 data.blocks = 1;
796 data.flags = MMC_DATA_READ;
797 data.sg = &sg;
798 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530799 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700800
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700801 mrq.cmd = &cmd;
802 mrq.data = &data;
803
Ben Dooks051913d2009-06-08 23:33:57 +0100804 blocks = kmalloc(4, GFP_KERNEL);
805 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100806 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100807
808 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700809
810 mmc_wait_for_req(card->host, &mrq);
811
Ben Dooks051913d2009-06-08 23:33:57 +0100812 result = ntohl(*blocks);
813 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700814
Ben Dooks051913d2009-06-08 23:33:57 +0100815 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100816 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100817
Linus Walleij169f03a2017-02-01 13:47:57 +0100818 *written_blocks = result;
819
820 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700821}
822
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100823static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300824{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900825 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300826 int err;
827
Adrian Hunter504f1912008-10-16 12:55:25 +0300828 cmd.opcode = MMC_SEND_STATUS;
829 if (!mmc_host_is_spi(card->host))
830 cmd.arg = card->rca << 16;
831 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100832 err = mmc_wait_for_cmd(card->host, &cmd, retries);
833 if (err == 0)
834 *status = cmd.resp[0];
835 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300836}
837
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100838static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100839 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100840{
841 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
842 int err = 0;
843 u32 status;
844
845 do {
846 err = get_card_status(card, &status, 5);
847 if (err) {
848 pr_err("%s: error %d requesting status\n",
849 req->rq_disk->disk_name, err);
850 return err;
851 }
852
853 if (status & R1_ERROR) {
854 pr_err("%s: %s: error sending status cmd, status %#x\n",
855 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100856 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100857 }
858
Ulf Hansson95a91292014-01-29 13:11:27 +0100859 /* We may rely on the host hw to handle busy detection.*/
860 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
861 hw_busy_detect)
862 break;
863
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100864 /*
865 * Timeout if the device never becomes ready for data and never
866 * leaves the program state.
867 */
868 if (time_after(jiffies, timeout)) {
869 pr_err("%s: Card stuck in programming state! %s %s\n",
870 mmc_hostname(card->host),
871 req->rq_disk->disk_name, __func__);
872 return -ETIMEDOUT;
873 }
874
875 /*
876 * Some cards mishandle the status bits,
877 * so make sure to check both the busy
878 * indication and the card state.
879 */
880 } while (!(status & R1_READY_FOR_DATA) ||
881 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
882
883 return err;
884}
885
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100886static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100887 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100888{
889 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900890 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100891 int err;
892 bool use_r1b_resp = rq_data_dir(req) == WRITE;
893
894 /*
895 * Normally we use R1B responses for WRITE, but in cases where the host
896 * has specified a max_busy_timeout we need to validate it. A failure
897 * means we need to prevent the host from doing hw busy detection, which
898 * is done by converting to a R1 response instead.
899 */
900 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
901 use_r1b_resp = false;
902
903 cmd.opcode = MMC_STOP_TRANSMISSION;
904 if (use_r1b_resp) {
905 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
906 cmd.busy_timeout = timeout_ms;
907 } else {
908 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
909 }
910
911 err = mmc_wait_for_cmd(host, &cmd, 5);
912 if (err)
913 return err;
914
915 *stop_status = cmd.resp[0];
916
917 /* No need to check card status in case of READ. */
918 if (rq_data_dir(req) == READ)
919 return 0;
920
921 if (!mmc_host_is_spi(host) &&
922 (*stop_status & R1_ERROR)) {
923 pr_err("%s: %s: general error sending stop command, resp %#x\n",
924 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100925 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100926 }
927
928 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
929}
930
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530931#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100932#define ERR_RETRY 2
933#define ERR_ABORT 1
934#define ERR_CONTINUE 0
935
936static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
937 bool status_valid, u32 status)
938{
939 switch (error) {
940 case -EILSEQ:
941 /* response crc error, retry the r/w cmd */
942 pr_err("%s: %s sending %s command, card status %#x\n",
943 req->rq_disk->disk_name, "response CRC error",
944 name, status);
945 return ERR_RETRY;
946
947 case -ETIMEDOUT:
948 pr_err("%s: %s sending %s command, card status %#x\n",
949 req->rq_disk->disk_name, "timed out", name, status);
950
951 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530952 if (!status_valid) {
953 pr_err("%s: status not valid, retrying timeout\n",
954 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100955 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530956 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100957
958 /*
959 * If it was a r/w cmd crc error, or illegal command
960 * (eg, issued in wrong state) then retry - we should
961 * have corrected the state problem above.
962 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530963 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
964 pr_err("%s: command error, retrying timeout\n",
965 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100966 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530967 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100968
969 /* Otherwise abort the command */
970 return ERR_ABORT;
971
972 default:
973 /* We don't understand the error code the driver gave us */
974 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
975 req->rq_disk->disk_name, error, status);
976 return ERR_ABORT;
977 }
978}
979
980/*
981 * Initial r/w and stop cmd error recovery.
982 * We don't know whether the card received the r/w cmd or not, so try to
983 * restore things back to a sane state. Essentially, we do this as follows:
984 * - Obtain card status. If the first attempt to obtain card status fails,
985 * the status word will reflect the failed status cmd, not the failed
986 * r/w cmd. If we fail to obtain card status, it suggests we can no
987 * longer communicate with the card.
988 * - Check the card state. If the card received the cmd but there was a
989 * transient problem with the response, it might still be in a data transfer
990 * mode. Try to send it a stop command. If this fails, we can't recover.
991 * - If the r/w cmd failed due to a response CRC error, it was probably
992 * transient, so retry the cmd.
993 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
994 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
995 * illegal cmd, retry.
996 * Otherwise we don't understand what happened, so abort.
997 */
998static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +0100999 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001000{
1001 bool prev_cmd_status_valid = true;
1002 u32 status, stop_status = 0;
1003 int err, retry;
1004
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301005 if (mmc_card_removed(card))
1006 return ERR_NOMEDIUM;
1007
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001008 /*
1009 * Try to get card status which indicates both the card state
1010 * and why there was no response. If the first attempt fails,
1011 * we can't be sure the returned status is for the r/w command.
1012 */
1013 for (retry = 2; retry >= 0; retry--) {
1014 err = get_card_status(card, &status, 0);
1015 if (!err)
1016 break;
1017
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001018 /* Re-tune if needed */
1019 mmc_retune_recheck(card->host);
1020
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001021 prev_cmd_status_valid = false;
1022 pr_err("%s: error %d sending status command, %sing\n",
1023 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1024 }
1025
1026 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301027 if (err) {
1028 /* Check if the card is removed */
1029 if (mmc_detect_card_removed(card->host))
1030 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001031 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301032 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001033
Adrian Hunter67716322011-08-29 16:42:15 +03001034 /* Flag ECC errors */
1035 if ((status & R1_CARD_ECC_FAILED) ||
1036 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1037 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001038 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001039
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001040 /* Flag General errors */
1041 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1042 if ((status & R1_ERROR) ||
1043 (brq->stop.resp[0] & R1_ERROR)) {
1044 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1045 req->rq_disk->disk_name, __func__,
1046 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001047 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001048 }
1049
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001050 /*
1051 * Check the current card state. If it is in some data transfer
1052 * mode, tell it to stop (and hopefully transition back to TRAN.)
1053 */
1054 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1055 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001056 err = send_stop(card,
1057 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1058 req, gen_err, &stop_status);
1059 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001060 pr_err("%s: error %d sending stop command\n",
1061 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001062 /*
1063 * If the stop cmd also timed out, the card is probably
1064 * not present, so abort. Other errors are bad news too.
1065 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001066 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001067 }
1068
Adrian Hunter67716322011-08-29 16:42:15 +03001069 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001070 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001071 }
1072
1073 /* Check for set block count errors */
1074 if (brq->sbc.error)
1075 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1076 prev_cmd_status_valid, status);
1077
1078 /* Check for r/w command errors */
1079 if (brq->cmd.error)
1080 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1081 prev_cmd_status_valid, status);
1082
Adrian Hunter67716322011-08-29 16:42:15 +03001083 /* Data errors */
1084 if (!brq->stop.error)
1085 return ERR_CONTINUE;
1086
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001087 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001088 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 +01001089 req->rq_disk->disk_name, brq->stop.error,
1090 brq->cmd.resp[0], status);
1091
1092 /*
1093 * Subsitute in our own stop status as this will give the error
1094 * state which happened during the execution of the r/w command.
1095 */
1096 if (stop_status) {
1097 brq->stop.resp[0] = stop_status;
1098 brq->stop.error = 0;
1099 }
1100 return ERR_CONTINUE;
1101}
1102
Adrian Hunter67716322011-08-29 16:42:15 +03001103static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1104 int type)
1105{
1106 int err;
1107
1108 if (md->reset_done & type)
1109 return -EEXIST;
1110
1111 md->reset_done |= type;
1112 err = mmc_hw_reset(host);
1113 /* Ensure we switch back to the correct partition */
1114 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001115 struct mmc_blk_data *main_md =
1116 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001117 int part_err;
1118
1119 main_md->part_curr = main_md->part_type;
1120 part_err = mmc_blk_part_switch(host->card, md);
1121 if (part_err) {
1122 /*
1123 * We have failed to get back into the correct
1124 * partition, so we need to abort the whole request.
1125 */
1126 return -ENODEV;
1127 }
1128 }
1129 return err;
1130}
1131
1132static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1133{
1134 md->reset_done &= ~type;
1135}
1136
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001137int mmc_access_rpmb(struct mmc_queue *mq)
1138{
Linus Walleij7db30282016-11-18 13:36:15 +01001139 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001140 /*
1141 * If this is a RPMB partition access, return ture
1142 */
1143 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1144 return true;
1145
1146 return false;
1147}
1148
Linus Walleijdf061582017-01-24 11:17:57 +01001149static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001150{
Linus Walleij7db30282016-11-18 13:36:15 +01001151 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001152 struct mmc_card *card = md->queue.card;
1153 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001154 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001155
Adrian Hunterbd788c92010-08-11 14:17:47 -07001156 if (!mmc_can_erase(card)) {
1157 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001158 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001159 }
1160
1161 from = blk_rq_pos(req);
1162 nr = blk_rq_sectors(req);
1163
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001164 if (mmc_can_discard(card))
1165 arg = MMC_DISCARD_ARG;
1166 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001167 arg = MMC_TRIM_ARG;
1168 else
1169 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001170 do {
1171 err = 0;
1172 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1173 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1174 INAND_CMD38_ARG_EXT_CSD,
1175 arg == MMC_TRIM_ARG ?
1176 INAND_CMD38_ARG_TRIM :
1177 INAND_CMD38_ARG_ERASE,
1178 0);
1179 }
1180 if (!err)
1181 err = mmc_erase(card, from, nr, arg);
1182 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001183 if (!err)
1184 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001185fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301186 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001187}
1188
Linus Walleijdf061582017-01-24 11:17:57 +01001189static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001190 struct request *req)
1191{
Linus Walleij7db30282016-11-18 13:36:15 +01001192 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001193 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001194 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001195 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001196
Maya Erez775a9362013-04-18 15:41:55 +03001197 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001198 err = -EOPNOTSUPP;
1199 goto out;
1200 }
1201
1202 from = blk_rq_pos(req);
1203 nr = blk_rq_sectors(req);
1204
Maya Erez775a9362013-04-18 15:41:55 +03001205 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1206 arg = MMC_SECURE_TRIM1_ARG;
1207 else
1208 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001209
Adrian Hunter67716322011-08-29 16:42:15 +03001210retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001211 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1212 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1213 INAND_CMD38_ARG_EXT_CSD,
1214 arg == MMC_SECURE_TRIM1_ARG ?
1215 INAND_CMD38_ARG_SECTRIM1 :
1216 INAND_CMD38_ARG_SECERASE,
1217 0);
1218 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001219 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001220 }
Adrian Hunter28302812012-04-05 14:45:48 +03001221
Adrian Hunter49804542010-08-11 14:17:50 -07001222 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001223 if (err == -EIO)
1224 goto out_retry;
1225 if (err)
1226 goto out;
1227
1228 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001229 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1230 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1231 INAND_CMD38_ARG_EXT_CSD,
1232 INAND_CMD38_ARG_SECTRIM2,
1233 0);
1234 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001235 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001236 }
Adrian Hunter28302812012-04-05 14:45:48 +03001237
Adrian Hunter49804542010-08-11 14:17:50 -07001238 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001239 if (err == -EIO)
1240 goto out_retry;
1241 if (err)
1242 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001243 }
Adrian Hunter28302812012-04-05 14:45:48 +03001244
Adrian Hunter28302812012-04-05 14:45:48 +03001245out_retry:
1246 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001247 goto retry;
1248 if (!err)
1249 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001250out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301251 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001252}
1253
Linus Walleijdf061582017-01-24 11:17:57 +01001254static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001255{
Linus Walleij7db30282016-11-18 13:36:15 +01001256 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001257 struct mmc_card *card = md->queue.card;
1258 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001259
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001260 ret = mmc_flush_cache(card);
1261 if (ret)
1262 ret = -EIO;
1263
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301264 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001265}
1266
1267/*
1268 * Reformat current write as a reliable write, supporting
1269 * both legacy and the enhanced reliable write MMC cards.
1270 * In each transfer we'll handle only as much as a single
1271 * reliable write can handle, thus finish the request in
1272 * partial completions.
1273 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001274static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1275 struct mmc_card *card,
1276 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001277{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001278 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1279 /* Legacy mode imposes restrictions on transfers. */
1280 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1281 brq->data.blocks = 1;
1282
1283 if (brq->data.blocks > card->ext_csd.rel_sectors)
1284 brq->data.blocks = card->ext_csd.rel_sectors;
1285 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1286 brq->data.blocks = 1;
1287 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001288}
1289
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001290#define CMD_ERRORS \
1291 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1292 R1_ADDRESS_ERROR | /* Misaligned address */ \
1293 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1294 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1295 R1_CC_ERROR | /* Card controller error */ \
1296 R1_ERROR) /* General/unknown error */
1297
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001298static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1299 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001300{
Per Forlinee8a43a2011-07-01 18:55:33 +02001301 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001302 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001303 struct mmc_blk_request *brq = &mq_mrq->brq;
1304 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001305 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001306 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001307 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001308
1309 /*
1310 * sbc.error indicates a problem with the set block count
1311 * command. No data will have been transferred.
1312 *
1313 * cmd.error indicates a problem with the r/w command. No
1314 * data will have been transferred.
1315 *
1316 * stop.error indicates a problem with the stop command. Data
1317 * may have been transferred, or may still be transferring.
1318 */
Adrian Hunter67716322011-08-29 16:42:15 +03001319 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1320 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001321 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001322 case ERR_RETRY:
1323 return MMC_BLK_RETRY;
1324 case ERR_ABORT:
1325 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301326 case ERR_NOMEDIUM:
1327 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001328 case ERR_CONTINUE:
1329 break;
1330 }
1331 }
1332
1333 /*
1334 * Check for errors relating to the execution of the
1335 * initial command - such as address errors. No data
1336 * has been transferred.
1337 */
1338 if (brq->cmd.resp[0] & CMD_ERRORS) {
1339 pr_err("%s: r/w command failed, status = %#x\n",
1340 req->rq_disk->disk_name, brq->cmd.resp[0]);
1341 return MMC_BLK_ABORT;
1342 }
1343
1344 /*
1345 * Everything else is either success, or a data error of some
1346 * kind. If it was a write, we may have transitioned to
1347 * program mode, which we have to wait for it to complete.
1348 */
1349 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001350 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001351
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001352 /* Check stop command response */
1353 if (brq->stop.resp[0] & R1_ERROR) {
1354 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1355 req->rq_disk->disk_name, __func__,
1356 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001357 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001358 }
1359
Ulf Hansson95a91292014-01-29 13:11:27 +01001360 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1361 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001362 if (err)
1363 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001364 }
1365
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001366 /* if general error occurs, retry the write operation. */
1367 if (gen_err) {
1368 pr_warn("%s: retrying write for general error\n",
1369 req->rq_disk->disk_name);
1370 return MMC_BLK_RETRY;
1371 }
1372
Per Forlind78d4a8a2011-07-01 18:55:30 +02001373 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001374 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001375 pr_debug("%s: retrying because a re-tune was needed\n",
1376 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001377 brq->retune_retry_done = 1;
1378 return MMC_BLK_RETRY;
1379 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001380 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1381 req->rq_disk->disk_name, brq->data.error,
1382 (unsigned)blk_rq_pos(req),
1383 (unsigned)blk_rq_sectors(req),
1384 brq->cmd.resp[0], brq->stop.resp[0]);
1385
1386 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001387 if (ecc_err)
1388 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001389 return MMC_BLK_DATA_ERR;
1390 } else {
1391 return MMC_BLK_CMD_ERR;
1392 }
1393 }
1394
Adrian Hunter67716322011-08-29 16:42:15 +03001395 if (!brq->data.bytes_xfered)
1396 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001397
Adrian Hunter67716322011-08-29 16:42:15 +03001398 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1399 return MMC_BLK_PARTIAL;
1400
1401 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001402}
1403
Per Forlin54d49d72011-07-01 18:55:29 +02001404static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1405 struct mmc_card *card,
1406 int disable_multi,
1407 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Per Forlin54d49d72011-07-01 18:55:29 +02001409 u32 readcmd, writecmd;
1410 struct mmc_blk_request *brq = &mqrq->brq;
1411 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001412 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301413 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001415 /*
1416 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001417 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001418 */
Luca Porziod3df0462015-11-06 15:12:26 +00001419 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001421 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001422
Per Forlin54d49d72011-07-01 18:55:29 +02001423 memset(brq, 0, sizeof(struct mmc_blk_request));
1424 brq->mrq.cmd = &brq->cmd;
1425 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Per Forlin54d49d72011-07-01 18:55:29 +02001427 brq->cmd.arg = blk_rq_pos(req);
1428 if (!mmc_card_blockaddr(card))
1429 brq->cmd.arg <<= 9;
1430 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1431 brq->data.blksz = 512;
1432 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1433 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001434 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Per Forlin54d49d72011-07-01 18:55:29 +02001436 /*
1437 * The block layer doesn't support all sector count
1438 * restrictions, so we need to be prepared for too big
1439 * requests.
1440 */
1441 if (brq->data.blocks > card->host->max_blk_count)
1442 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001444 if (brq->data.blocks > 1) {
1445 /*
1446 * After a read error, we redo the request one sector
1447 * at a time in order to accurately determine which
1448 * sectors can be read successfully.
1449 */
1450 if (disable_multi)
1451 brq->data.blocks = 1;
1452
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001453 /*
1454 * Some controllers have HW issues while operating
1455 * in multiple I/O mode
1456 */
1457 if (card->host->ops->multi_io_quirk)
1458 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1459 (rq_data_dir(req) == READ) ?
1460 MMC_DATA_READ : MMC_DATA_WRITE,
1461 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001462 }
Per Forlin54d49d72011-07-01 18:55:29 +02001463
1464 if (brq->data.blocks > 1 || do_rel_wr) {
1465 /* SPI multiblock writes terminate using a special
1466 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001467 */
Per Forlin54d49d72011-07-01 18:55:29 +02001468 if (!mmc_host_is_spi(card->host) ||
1469 rq_data_dir(req) == READ)
1470 brq->mrq.stop = &brq->stop;
1471 readcmd = MMC_READ_MULTIPLE_BLOCK;
1472 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1473 } else {
1474 brq->mrq.stop = NULL;
1475 readcmd = MMC_READ_SINGLE_BLOCK;
1476 writecmd = MMC_WRITE_BLOCK;
1477 }
1478 if (rq_data_dir(req) == READ) {
1479 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001480 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001481 if (brq->mrq.stop)
1482 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1483 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001484 } else {
1485 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001486 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001487 if (brq->mrq.stop)
1488 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1489 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001490 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001491
Per Forlin54d49d72011-07-01 18:55:29 +02001492 if (do_rel_wr)
1493 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001494
Per Forlin54d49d72011-07-01 18:55:29 +02001495 /*
Saugata Das42659002011-12-21 13:09:17 +05301496 * Data tag is used only during writing meta data to speed
1497 * up write and any subsequent read of this meta data
1498 */
1499 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1500 (req->cmd_flags & REQ_META) &&
1501 (rq_data_dir(req) == WRITE) &&
1502 ((brq->data.blocks * brq->data.blksz) >=
1503 card->ext_csd.data_tag_unit_size);
1504
1505 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001506 * Pre-defined multi-block transfers are preferable to
1507 * open ended-ones (and necessary for reliable writes).
1508 * However, it is not sufficient to just send CMD23,
1509 * and avoid the final CMD12, as on an error condition
1510 * CMD12 (stop) needs to be sent anyway. This, coupled
1511 * with Auto-CMD23 enhancements provided by some
1512 * hosts, means that the complexity of dealing
1513 * with this is best left to the host. If CMD23 is
1514 * supported by card and host, we'll fill sbc in and let
1515 * the host deal with handling it correctly. This means
1516 * that for hosts that don't expose MMC_CAP_CMD23, no
1517 * change of behavior will be observed.
1518 *
1519 * N.B: Some MMC cards experience perf degradation.
1520 * We'll avoid using CMD23-bounded multiblock writes for
1521 * these, while retaining features like reliable writes.
1522 */
Saugata Das42659002011-12-21 13:09:17 +05301523 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1524 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1525 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001526 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1527 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301528 (do_rel_wr ? (1 << 31) : 0) |
1529 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001530 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1531 brq->mrq.sbc = &brq->sbc;
1532 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001533
Per Forlin54d49d72011-07-01 18:55:29 +02001534 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001535
Per Forlin54d49d72011-07-01 18:55:29 +02001536 brq->data.sg = mqrq->sg;
1537 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001538
Per Forlin54d49d72011-07-01 18:55:29 +02001539 /*
1540 * Adjust the sg list so it is the same size as the
1541 * request.
1542 */
1543 if (brq->data.blocks != blk_rq_sectors(req)) {
1544 int i, data_size = brq->data.blocks << 9;
1545 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001546
Per Forlin54d49d72011-07-01 18:55:29 +02001547 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1548 data_size -= sg->length;
1549 if (data_size <= 0) {
1550 sg->length += data_size;
1551 i++;
1552 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001553 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001554 }
Per Forlin54d49d72011-07-01 18:55:29 +02001555 brq->data.sg_len = i;
1556 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001557
Linus Walleij74f5ba32017-02-01 13:47:55 +01001558 mqrq->areq.mrq = &brq->mrq;
1559 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001560
Per Forlin54d49d72011-07-01 18:55:29 +02001561 mmc_queue_bounce_pre(mqrq);
1562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Walleij0e65f102017-02-01 13:47:58 +01001564static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1565 struct mmc_blk_request *brq, struct request *req,
1566 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001567{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001568 struct mmc_queue_req *mq_rq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001569 bool req_pending;
1570
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001571 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1572
Adrian Hunter67716322011-08-29 16:42:15 +03001573 /*
1574 * If this is an SD card and we're writing, we can first
1575 * mark the known good sectors as ok.
1576 *
1577 * If the card is not SD, we can still ok written sectors
1578 * as reported by the controller (which might be less than
1579 * the real number of written sectors, but never more).
1580 */
1581 if (mmc_card_sd(card)) {
1582 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001583 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001584
Linus Walleij169f03a2017-02-01 13:47:57 +01001585 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001586 if (err)
1587 req_pending = old_req_pending;
1588 else
1589 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001590 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001591 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001592 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001593 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001594}
1595
Linus Walleij4e1f7802017-01-24 11:17:52 +01001596static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1597{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001598 if (mmc_card_removed(card))
1599 req->rq_flags |= RQF_QUIET;
Linus Walleij0e65f102017-02-01 13:47:58 +01001600 while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req)));
Linus Walleij4e1f7802017-01-24 11:17:52 +01001601}
1602
Linus Walleijb2928e12017-02-01 13:47:54 +01001603/**
1604 * mmc_blk_rw_try_restart() - tries to restart the current async request
1605 * @mq: the queue with the card and host to restart
1606 * @req: a new request that want to be started after the current one
1607 */
1608static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req)
Linus Walleijefb5a052017-01-24 11:17:53 +01001609{
1610 if (!req)
1611 return;
1612
Linus Walleijb2928e12017-02-01 13:47:54 +01001613 /*
1614 * If the card was removed, just cancel everything and return.
1615 */
1616 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001617 req->rq_flags |= RQF_QUIET;
1618 blk_end_request_all(req, -EIO);
Linus Walleijb2928e12017-02-01 13:47:54 +01001619 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001620 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001621 /* Else proceed and try to restart the current async request */
1622 mmc_blk_rw_rq_prep(mq->mqrq_cur, mq->card, 0, mq);
Linus Walleij74f5ba32017-02-01 13:47:55 +01001623 mmc_start_areq(mq->card->host, &mq->mqrq_cur->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001624}
1625
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001626static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001627{
Linus Walleij7db30282016-11-18 13:36:15 +01001628 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001629 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001630 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001631 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001632 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001633 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001634 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001635 struct mmc_async_req *new_areq;
1636 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001637 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001638
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001639 if (!new_req && !mq->mqrq_prev->req)
Linus Walleijdf061582017-01-24 11:17:57 +01001640 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001641
1642 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001643 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301644 /*
1645 * When 4KB native sector is enabled, only 8 blocks
1646 * multiple read or write is allowed
1647 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001648 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001649 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301650 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001651 new_req->rq_disk->disk_name);
1652 mmc_blk_rw_cmd_abort(card, new_req);
Linus Walleijdf061582017-01-24 11:17:57 +01001653 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301654 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001655
Linus Walleij03d640a2016-11-25 10:35:00 +01001656 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Linus Walleij74f5ba32017-02-01 13:47:55 +01001657 new_areq = &mq->mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001658 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001659 new_areq = NULL;
1660
Linus Walleijc3399ef2017-02-01 13:47:53 +01001661 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001662 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001663 /*
1664 * We have just put the first request into the pipeline
1665 * and there is nothing more to do until it is
1666 * complete.
1667 */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001668 if (status == MMC_BLK_NEW_REQUEST)
Linus Walleij9491be52017-02-01 13:47:56 +01001669 mq->new_request = true;
Linus Walleijdf061582017-01-24 11:17:57 +01001670 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001671 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001672
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001673 /*
1674 * An asynchronous request has been completed and we proceed
1675 * to handle the result of it.
1676 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001677 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001678 brq = &mq_rq->brq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001679 old_req = mq_rq->req;
1680 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001681 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001682
Per Forlind78d4a8a2011-07-01 18:55:30 +02001683 switch (status) {
1684 case MMC_BLK_SUCCESS:
1685 case MMC_BLK_PARTIAL:
1686 /*
1687 * A block was successfully transferred.
1688 */
Adrian Hunter67716322011-08-29 16:42:15 +03001689 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001690
Linus Walleij0e65f102017-02-01 13:47:58 +01001691 req_pending = blk_end_request(old_req, 0,
1692 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001693 /*
1694 * If the blk_end_request function returns non-zero even
1695 * though all data has been transferred and no errors
1696 * were returned by the host controller, it's a bug.
1697 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001698 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301699 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001700 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001701 brq->data.bytes_xfered);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001702 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijdf061582017-01-24 11:17:57 +01001703 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001704 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001705 break;
1706 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001707 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001708 if (mmc_blk_reset(md, card->host, type)) {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001709 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001710 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001711 return;
1712 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001713 if (!req_pending) {
Linus Walleijb2928e12017-02-01 13:47:54 +01001714 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001715 return;
1716 }
Ding Wang29535f72015-05-18 20:14:15 +08001717 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001718 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001719 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001720 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001721 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001722 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001723 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001724 if (!mmc_blk_reset(md, card->host, type))
1725 break;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001726 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001727 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001728 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001729 case MMC_BLK_DATA_ERR: {
1730 int err;
1731
1732 err = mmc_blk_reset(md, card->host, type);
1733 if (!err)
1734 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001735 if (err == -ENODEV) {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001736 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001737 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001738 return;
1739 }
Adrian Hunter67716322011-08-29 16:42:15 +03001740 /* Fall through */
1741 }
1742 case MMC_BLK_ECC_ERR:
1743 if (brq->data.blocks > 1) {
1744 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001745 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001746 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001747 disable_multi = 1;
1748 break;
1749 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001750 /*
1751 * After an error, we redo I/O one sector at a
1752 * time, so we only reach here after trying to
1753 * read a single sector.
1754 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001755 req_pending = blk_end_request(old_req, -EIO,
1756 brq->data.blksz);
1757 if (!req_pending) {
Linus Walleijb2928e12017-02-01 13:47:54 +01001758 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001759 return;
1760 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001761 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301762 case MMC_BLK_NOMEDIUM:
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001763 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001764 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001765 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001766 default:
1767 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001768 old_req->rq_disk->disk_name, status);
1769 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001770 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001771 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001772 }
1773
Linus Walleij0e65f102017-02-01 13:47:58 +01001774 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001775 /*
1776 * In case of a incomplete request
1777 * prepare it again and resend.
1778 */
1779 mmc_blk_rw_rq_prep(mq_rq, card,
1780 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001781 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001782 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001783 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001784 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001785 } while (req_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Linus Walleijdf061582017-01-24 11:17:57 +01001788void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001789{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001790 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001791 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001792 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001793 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001794
Per Forlinee8a43a2011-07-01 18:55:33 +02001795 if (req && !mq->mqrq_prev->req)
1796 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001797 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001798
Andrei Warkentin371a6892011-04-11 18:10:25 -05001799 ret = mmc_blk_part_switch(card, md);
1800 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001801 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301802 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001803 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001804 goto out;
1805 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001806
Linus Walleij9491be52017-02-01 13:47:56 +01001807 mq->new_request = false;
Mike Christiec2df40d2016-06-05 14:32:17 -05001808 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001809 /* complete ongoing async transfer before issuing discard */
1810 if (card->host->areq)
1811 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001812 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001813 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1814 /* complete ongoing async transfer before issuing secure erase*/
1815 if (card->host->areq)
1816 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001817 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001818 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001819 /* complete ongoing async transfer before issuing flush */
1820 if (card->host->areq)
1821 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001822 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001823 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001824 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001825 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001826
Andrei Warkentin371a6892011-04-11 18:10:25 -05001827out:
Linus Walleij9491be52017-02-01 13:47:56 +01001828 if ((!req && !mq->new_request) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001829 /*
1830 * Release host when there are no more requests
1831 * and after special request(discard, flush) is done.
1832 * In case sepecial request, there is no reentry to
1833 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1834 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001835 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Russell Kinga6f6c962006-01-03 22:38:44 +00001838static inline int mmc_blk_readonly(struct mmc_card *card)
1839{
1840 return mmc_card_readonly(card) ||
1841 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1842}
1843
Andrei Warkentin371a6892011-04-11 18:10:25 -05001844static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1845 struct device *parent,
1846 sector_t size,
1847 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001848 const char *subname,
1849 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct mmc_blk_data *md;
1852 int devidx, ret;
1853
Heiner Kallweita04848c2017-02-01 19:44:22 +01001854 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1855 if (devidx < 0)
1856 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001858 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001859 if (!md) {
1860 ret = -ENOMEM;
1861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001863
Johan Rudholmadd710e2011-12-02 08:51:06 +01001864 md->area_type = area_type;
1865
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001866 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001867 * Set the read-only status based on the supported commands
1868 * and the write protect switch.
1869 */
1870 md->read_only = mmc_blk_readonly(card);
1871
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001872 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001873 if (md->disk == NULL) {
1874 ret = -ENOMEM;
1875 goto err_kfree;
1876 }
1877
1878 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001879 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001880 md->usage = 1;
1881
Adrian Hunterd09408a2011-06-23 13:40:28 +03001882 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001883 if (ret)
1884 goto err_putdisk;
1885
Linus Walleij7db30282016-11-18 13:36:15 +01001886 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001887
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001888 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001889 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001890 md->disk->fops = &mmc_bdops;
1891 md->disk->private_data = md;
1892 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001893 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001894 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001895 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001896 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001897 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001898
1899 /*
1900 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1901 *
1902 * - be set for removable media with permanent block devices
1903 * - be unset for removable block devices with permanent media
1904 *
1905 * Since MMC block devices clearly fall under the second
1906 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1907 * should use the block device creation/destruction hotplug
1908 * messages to tell when the card is present.
1909 */
1910
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001911 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001912 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001913
Saugata Dasa5075eb2012-05-17 16:32:21 +05301914 if (mmc_card_mmc(card))
1915 blk_queue_logical_block_size(md->queue.queue,
1916 card->ext_csd.data_sector_size);
1917 else
1918 blk_queue_logical_block_size(md->queue.queue, 512);
1919
Andrei Warkentin371a6892011-04-11 18:10:25 -05001920 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001921
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001922 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001923 if ((mmc_card_mmc(card) &&
1924 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001925 (mmc_card_sd(card) &&
1926 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1927 md->flags |= MMC_BLK_CMD23;
1928 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001929
1930 if (mmc_card_mmc(card) &&
1931 md->flags & MMC_BLK_CMD23 &&
1932 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1933 card->ext_csd.rel_sectors)) {
1934 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001935 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001936 }
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001939
1940 err_putdisk:
1941 put_disk(md->disk);
1942 err_kfree:
1943 kfree(md);
1944 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01001945 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00001946 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Andrei Warkentin371a6892011-04-11 18:10:25 -05001949static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1950{
1951 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001952
1953 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1954 /*
1955 * The EXT_CSD sector count is in number or 512 byte
1956 * sectors.
1957 */
1958 size = card->ext_csd.sectors;
1959 } else {
1960 /*
1961 * The CSD capacity field is in units of read_blkbits.
1962 * set_capacity takes units of 512 bytes.
1963 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001964 size = (typeof(sector_t))card->csd.capacity
1965 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001966 }
1967
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001968 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001969 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001970}
1971
1972static int mmc_blk_alloc_part(struct mmc_card *card,
1973 struct mmc_blk_data *md,
1974 unsigned int part_type,
1975 sector_t size,
1976 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001977 const char *subname,
1978 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001979{
1980 char cap_str[10];
1981 struct mmc_blk_data *part_md;
1982
1983 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001984 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001985 if (IS_ERR(part_md))
1986 return PTR_ERR(part_md);
1987 part_md->part_type = part_type;
1988 list_add(&part_md->part, &md->part);
1989
James Bottomleyb9f28d82015-03-05 18:47:01 -08001990 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001991 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301992 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001993 part_md->disk->disk_name, mmc_card_id(card),
1994 mmc_card_name(card), part_md->part_type, cap_str);
1995 return 0;
1996}
1997
Namjae Jeone0c368d2011-10-06 23:41:38 +09001998/* MMC Physical partitions consist of two boot partitions and
1999 * up to four general purpose partitions.
2000 * For each partition enabled in EXT_CSD a block device will be allocatedi
2001 * to provide access to the partition.
2002 */
2003
Andrei Warkentin371a6892011-04-11 18:10:25 -05002004static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2005{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002006 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002007
2008 if (!mmc_card_mmc(card))
2009 return 0;
2010
Namjae Jeone0c368d2011-10-06 23:41:38 +09002011 for (idx = 0; idx < card->nr_parts; idx++) {
2012 if (card->part[idx].size) {
2013 ret = mmc_blk_alloc_part(card, md,
2014 card->part[idx].part_cfg,
2015 card->part[idx].size >> 9,
2016 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002017 card->part[idx].name,
2018 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002019 if (ret)
2020 return ret;
2021 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002022 }
2023
2024 return ret;
2025}
2026
Andrei Warkentin371a6892011-04-11 18:10:25 -05002027static void mmc_blk_remove_req(struct mmc_blk_data *md)
2028{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002029 struct mmc_card *card;
2030
Andrei Warkentin371a6892011-04-11 18:10:25 -05002031 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002032 /*
2033 * Flush remaining requests and free queues. It
2034 * is freeing the queue that stops new requests
2035 * from being accepted.
2036 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002037 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002038 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039 if (md->disk->flags & GENHD_FL_UP) {
2040 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002041 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2042 card->ext_csd.boot_ro_lockable)
2043 device_remove_file(disk_to_dev(md->disk),
2044 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002045
Andrei Warkentin371a6892011-04-11 18:10:25 -05002046 del_gendisk(md->disk);
2047 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002048 mmc_blk_put(md);
2049 }
2050}
2051
2052static void mmc_blk_remove_parts(struct mmc_card *card,
2053 struct mmc_blk_data *md)
2054{
2055 struct list_head *pos, *q;
2056 struct mmc_blk_data *part_md;
2057
2058 list_for_each_safe(pos, q, &md->part) {
2059 part_md = list_entry(pos, struct mmc_blk_data, part);
2060 list_del(pos);
2061 mmc_blk_remove_req(part_md);
2062 }
2063}
2064
2065static int mmc_add_disk(struct mmc_blk_data *md)
2066{
2067 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002068 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002069
Dan Williams307d8e62016-06-20 10:40:44 -07002070 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002071 md->force_ro.show = force_ro_show;
2072 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302073 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002074 md->force_ro.attr.name = "force_ro";
2075 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2076 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2077 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002078 goto force_ro_fail;
2079
2080 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2081 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002082 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002083
2084 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2085 mode = S_IRUGO;
2086 else
2087 mode = S_IRUGO | S_IWUSR;
2088
2089 md->power_ro_lock.show = power_ro_lock_show;
2090 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002091 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002092 md->power_ro_lock.attr.mode = mode;
2093 md->power_ro_lock.attr.name =
2094 "ro_lock_until_next_power_on";
2095 ret = device_create_file(disk_to_dev(md->disk),
2096 &md->power_ro_lock);
2097 if (ret)
2098 goto power_ro_lock_fail;
2099 }
2100 return ret;
2101
2102power_ro_lock_fail:
2103 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2104force_ro_fail:
2105 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002106
2107 return ret;
2108}
2109
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002110static const struct mmc_fixup blk_fixups[] =
2111{
Chris Ballc59d4472011-11-11 22:01:43 -05002112 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2113 MMC_QUIRK_INAND_CMD38),
2114 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2115 MMC_QUIRK_INAND_CMD38),
2116 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2117 MMC_QUIRK_INAND_CMD38),
2118 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2119 MMC_QUIRK_INAND_CMD38),
2120 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2121 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002122
2123 /*
2124 * Some MMC cards experience performance degradation with CMD23
2125 * instead of CMD12-bounded multiblock transfers. For now we'll
2126 * black list what's bad...
2127 * - Certain Toshiba cards.
2128 *
2129 * N.B. This doesn't affect SD cards.
2130 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002131 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2132 MMC_QUIRK_BLK_NO_CMD23),
2133 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2134 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002135 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002136 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002137 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002138 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002139 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002140 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002141
2142 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002143 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002144 */
Chris Ballc59d4472011-11-11 22:01:43 -05002145 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002146 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002147 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2148 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002149
Ian Chen3550ccd2012-08-29 15:05:36 +09002150 /*
2151 * On these Samsung MoviNAND parts, performing secure erase or
2152 * secure trim can result in unrecoverable corruption due to a
2153 * firmware bug.
2154 */
2155 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2156 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2157 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2158 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2159 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2160 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2161 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2162 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2163 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2164 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2165 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2166 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2167 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2168 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2169 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2170 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2171
Shawn Linb5b4ff02015-08-12 13:08:32 +08002172 /*
2173 * On Some Kingston eMMCs, performing trim can result in
2174 * unrecoverable data conrruption occasionally due to a firmware bug.
2175 */
2176 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2177 MMC_QUIRK_TRIM_BROKEN),
2178 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2179 MMC_QUIRK_TRIM_BROKEN),
2180
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002181 END_FIXUP
2182};
2183
Ulf Hansson96541ba2015-04-14 13:06:12 +02002184static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002186 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002187 char cap_str[10];
2188
Pierre Ossman912490d2005-05-21 10:27:02 +01002189 /*
2190 * Check that the card supports the command class(es) we need.
2191 */
2192 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return -ENODEV;
2194
Lukas Czerner5204d002014-06-18 13:18:07 +02002195 mmc_fixup_device(card, blk_fixups);
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 md = mmc_blk_alloc(card);
2198 if (IS_ERR(md))
2199 return PTR_ERR(md);
2200
James Bottomleyb9f28d82015-03-05 18:47:01 -08002201 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002202 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302203 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002205 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Andrei Warkentin371a6892011-04-11 18:10:25 -05002207 if (mmc_blk_alloc_parts(card, md))
2208 goto out;
2209
Ulf Hansson96541ba2015-04-14 13:06:12 +02002210 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002211
Andrei Warkentin371a6892011-04-11 18:10:25 -05002212 if (mmc_add_disk(md))
2213 goto out;
2214
2215 list_for_each_entry(part_md, &md->part, part) {
2216 if (mmc_add_disk(part_md))
2217 goto out;
2218 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002219
2220 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2221 pm_runtime_use_autosuspend(&card->dev);
2222
2223 /*
2224 * Don't enable runtime PM for SD-combo cards here. Leave that
2225 * decision to be taken during the SDIO init sequence instead.
2226 */
2227 if (card->type != MMC_TYPE_SD_COMBO) {
2228 pm_runtime_set_active(&card->dev);
2229 pm_runtime_enable(&card->dev);
2230 }
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return 0;
2233
2234 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 mmc_blk_remove_parts(card, md);
2236 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Ulf Hansson96541ba2015-04-14 13:06:12 +02002240static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002242 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002245 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002246 mmc_claim_host(card->host);
2247 mmc_blk_part_switch(card, md);
2248 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002249 if (card->type != MMC_TYPE_SD_COMBO)
2250 pm_runtime_disable(&card->dev);
2251 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002252 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002253 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Ulf Hansson96541ba2015-04-14 13:06:12 +02002256static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002258 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002259 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 if (md) {
2262 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002263 list_for_each_entry(part_md, &md->part, part) {
2264 mmc_queue_suspend(&part_md->queue);
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 return 0;
2268}
2269
Ulf Hansson96541ba2015-04-14 13:06:12 +02002270static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002271{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002272 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002273}
2274
Ulf Hansson0967edc2014-10-06 11:29:42 +02002275#ifdef CONFIG_PM_SLEEP
2276static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002277{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002278 struct mmc_card *card = mmc_dev_to_card(dev);
2279
2280 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002281}
2282
Ulf Hansson0967edc2014-10-06 11:29:42 +02002283static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002285 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002286 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002289 /*
2290 * Resume involves the card going into idle state,
2291 * so current partition is always the main one.
2292 */
2293 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002295 list_for_each_entry(part_md, &md->part, part) {
2296 mmc_queue_resume(&part_md->queue);
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 return 0;
2300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301#endif
2302
Ulf Hansson0967edc2014-10-06 11:29:42 +02002303static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2304
Ulf Hansson96541ba2015-04-14 13:06:12 +02002305static struct mmc_driver mmc_driver = {
2306 .drv = {
2307 .name = "mmcblk",
2308 .pm = &mmc_blk_pm_ops,
2309 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 .probe = mmc_blk_probe,
2311 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002312 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313};
2314
2315static int __init mmc_blk_init(void)
2316{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002317 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002319 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2320 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2321
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002322 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002323
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002324 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2325 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002328 res = mmc_register_driver(&mmc_driver);
2329 if (res)
2330 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002332 return 0;
2333 out2:
2334 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 out:
2336 return res;
2337}
2338
2339static void __exit mmc_blk_exit(void)
2340{
2341 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002342 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345module_init(mmc_blk_init);
2346module_exit(mmc_blk_exit);
2347
2348MODULE_LICENSE("GPL");
2349MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2350