blob: 43f1f1b24b83c1c06780298c9bba01772c50b046 [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
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700766static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
767{
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)
784 return (u32)-1;
785 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700786 return (u32)-1;
787
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)
806 return (u32)-1;
807
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)
816 result = (u32)-1;
817
818 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819}
820
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100821static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300822{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900823 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300824 int err;
825
Adrian Hunter504f1912008-10-16 12:55:25 +0300826 cmd.opcode = MMC_SEND_STATUS;
827 if (!mmc_host_is_spi(card->host))
828 cmd.arg = card->rca << 16;
829 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100830 err = mmc_wait_for_cmd(card->host, &cmd, retries);
831 if (err == 0)
832 *status = cmd.resp[0];
833 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300834}
835
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100836static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100837 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100838{
839 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
840 int err = 0;
841 u32 status;
842
843 do {
844 err = get_card_status(card, &status, 5);
845 if (err) {
846 pr_err("%s: error %d requesting status\n",
847 req->rq_disk->disk_name, err);
848 return err;
849 }
850
851 if (status & R1_ERROR) {
852 pr_err("%s: %s: error sending status cmd, status %#x\n",
853 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100854 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100855 }
856
Ulf Hansson95a91292014-01-29 13:11:27 +0100857 /* We may rely on the host hw to handle busy detection.*/
858 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
859 hw_busy_detect)
860 break;
861
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100862 /*
863 * Timeout if the device never becomes ready for data and never
864 * leaves the program state.
865 */
866 if (time_after(jiffies, timeout)) {
867 pr_err("%s: Card stuck in programming state! %s %s\n",
868 mmc_hostname(card->host),
869 req->rq_disk->disk_name, __func__);
870 return -ETIMEDOUT;
871 }
872
873 /*
874 * Some cards mishandle the status bits,
875 * so make sure to check both the busy
876 * indication and the card state.
877 */
878 } while (!(status & R1_READY_FOR_DATA) ||
879 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
880
881 return err;
882}
883
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100884static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100885 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100886{
887 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900888 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100889 int err;
890 bool use_r1b_resp = rq_data_dir(req) == WRITE;
891
892 /*
893 * Normally we use R1B responses for WRITE, but in cases where the host
894 * has specified a max_busy_timeout we need to validate it. A failure
895 * means we need to prevent the host from doing hw busy detection, which
896 * is done by converting to a R1 response instead.
897 */
898 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
899 use_r1b_resp = false;
900
901 cmd.opcode = MMC_STOP_TRANSMISSION;
902 if (use_r1b_resp) {
903 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
904 cmd.busy_timeout = timeout_ms;
905 } else {
906 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
907 }
908
909 err = mmc_wait_for_cmd(host, &cmd, 5);
910 if (err)
911 return err;
912
913 *stop_status = cmd.resp[0];
914
915 /* No need to check card status in case of READ. */
916 if (rq_data_dir(req) == READ)
917 return 0;
918
919 if (!mmc_host_is_spi(host) &&
920 (*stop_status & R1_ERROR)) {
921 pr_err("%s: %s: general error sending stop command, resp %#x\n",
922 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100923 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100924 }
925
926 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
927}
928
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530929#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100930#define ERR_RETRY 2
931#define ERR_ABORT 1
932#define ERR_CONTINUE 0
933
934static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
935 bool status_valid, u32 status)
936{
937 switch (error) {
938 case -EILSEQ:
939 /* response crc error, retry the r/w cmd */
940 pr_err("%s: %s sending %s command, card status %#x\n",
941 req->rq_disk->disk_name, "response CRC error",
942 name, status);
943 return ERR_RETRY;
944
945 case -ETIMEDOUT:
946 pr_err("%s: %s sending %s command, card status %#x\n",
947 req->rq_disk->disk_name, "timed out", name, status);
948
949 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530950 if (!status_valid) {
951 pr_err("%s: status not valid, retrying timeout\n",
952 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100953 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530954 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100955
956 /*
957 * If it was a r/w cmd crc error, or illegal command
958 * (eg, issued in wrong state) then retry - we should
959 * have corrected the state problem above.
960 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530961 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
962 pr_err("%s: command error, retrying timeout\n",
963 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100964 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530965 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100966
967 /* Otherwise abort the command */
968 return ERR_ABORT;
969
970 default:
971 /* We don't understand the error code the driver gave us */
972 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
973 req->rq_disk->disk_name, error, status);
974 return ERR_ABORT;
975 }
976}
977
978/*
979 * Initial r/w and stop cmd error recovery.
980 * We don't know whether the card received the r/w cmd or not, so try to
981 * restore things back to a sane state. Essentially, we do this as follows:
982 * - Obtain card status. If the first attempt to obtain card status fails,
983 * the status word will reflect the failed status cmd, not the failed
984 * r/w cmd. If we fail to obtain card status, it suggests we can no
985 * longer communicate with the card.
986 * - Check the card state. If the card received the cmd but there was a
987 * transient problem with the response, it might still be in a data transfer
988 * mode. Try to send it a stop command. If this fails, we can't recover.
989 * - If the r/w cmd failed due to a response CRC error, it was probably
990 * transient, so retry the cmd.
991 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
992 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
993 * illegal cmd, retry.
994 * Otherwise we don't understand what happened, so abort.
995 */
996static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +0100997 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100998{
999 bool prev_cmd_status_valid = true;
1000 u32 status, stop_status = 0;
1001 int err, retry;
1002
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301003 if (mmc_card_removed(card))
1004 return ERR_NOMEDIUM;
1005
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001006 /*
1007 * Try to get card status which indicates both the card state
1008 * and why there was no response. If the first attempt fails,
1009 * we can't be sure the returned status is for the r/w command.
1010 */
1011 for (retry = 2; retry >= 0; retry--) {
1012 err = get_card_status(card, &status, 0);
1013 if (!err)
1014 break;
1015
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001016 /* Re-tune if needed */
1017 mmc_retune_recheck(card->host);
1018
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001019 prev_cmd_status_valid = false;
1020 pr_err("%s: error %d sending status command, %sing\n",
1021 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1022 }
1023
1024 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301025 if (err) {
1026 /* Check if the card is removed */
1027 if (mmc_detect_card_removed(card->host))
1028 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001029 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301030 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001031
Adrian Hunter67716322011-08-29 16:42:15 +03001032 /* Flag ECC errors */
1033 if ((status & R1_CARD_ECC_FAILED) ||
1034 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1035 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001036 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001037
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001038 /* Flag General errors */
1039 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1040 if ((status & R1_ERROR) ||
1041 (brq->stop.resp[0] & R1_ERROR)) {
1042 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1043 req->rq_disk->disk_name, __func__,
1044 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001045 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001046 }
1047
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001048 /*
1049 * Check the current card state. If it is in some data transfer
1050 * mode, tell it to stop (and hopefully transition back to TRAN.)
1051 */
1052 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1053 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001054 err = send_stop(card,
1055 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1056 req, gen_err, &stop_status);
1057 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001058 pr_err("%s: error %d sending stop command\n",
1059 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001060 /*
1061 * If the stop cmd also timed out, the card is probably
1062 * not present, so abort. Other errors are bad news too.
1063 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001064 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001065 }
1066
Adrian Hunter67716322011-08-29 16:42:15 +03001067 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001068 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069 }
1070
1071 /* Check for set block count errors */
1072 if (brq->sbc.error)
1073 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1074 prev_cmd_status_valid, status);
1075
1076 /* Check for r/w command errors */
1077 if (brq->cmd.error)
1078 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1079 prev_cmd_status_valid, status);
1080
Adrian Hunter67716322011-08-29 16:42:15 +03001081 /* Data errors */
1082 if (!brq->stop.error)
1083 return ERR_CONTINUE;
1084
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001085 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001086 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 +01001087 req->rq_disk->disk_name, brq->stop.error,
1088 brq->cmd.resp[0], status);
1089
1090 /*
1091 * Subsitute in our own stop status as this will give the error
1092 * state which happened during the execution of the r/w command.
1093 */
1094 if (stop_status) {
1095 brq->stop.resp[0] = stop_status;
1096 brq->stop.error = 0;
1097 }
1098 return ERR_CONTINUE;
1099}
1100
Adrian Hunter67716322011-08-29 16:42:15 +03001101static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1102 int type)
1103{
1104 int err;
1105
1106 if (md->reset_done & type)
1107 return -EEXIST;
1108
1109 md->reset_done |= type;
1110 err = mmc_hw_reset(host);
1111 /* Ensure we switch back to the correct partition */
1112 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001113 struct mmc_blk_data *main_md =
1114 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001115 int part_err;
1116
1117 main_md->part_curr = main_md->part_type;
1118 part_err = mmc_blk_part_switch(host->card, md);
1119 if (part_err) {
1120 /*
1121 * We have failed to get back into the correct
1122 * partition, so we need to abort the whole request.
1123 */
1124 return -ENODEV;
1125 }
1126 }
1127 return err;
1128}
1129
1130static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1131{
1132 md->reset_done &= ~type;
1133}
1134
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001135int mmc_access_rpmb(struct mmc_queue *mq)
1136{
Linus Walleij7db30282016-11-18 13:36:15 +01001137 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001138 /*
1139 * If this is a RPMB partition access, return ture
1140 */
1141 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1142 return true;
1143
1144 return false;
1145}
1146
Linus Walleijdf061582017-01-24 11:17:57 +01001147static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001148{
Linus Walleij7db30282016-11-18 13:36:15 +01001149 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001150 struct mmc_card *card = md->queue.card;
1151 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001152 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001153
Adrian Hunterbd788c92010-08-11 14:17:47 -07001154 if (!mmc_can_erase(card)) {
1155 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001156 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001157 }
1158
1159 from = blk_rq_pos(req);
1160 nr = blk_rq_sectors(req);
1161
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001162 if (mmc_can_discard(card))
1163 arg = MMC_DISCARD_ARG;
1164 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001165 arg = MMC_TRIM_ARG;
1166 else
1167 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001168 do {
1169 err = 0;
1170 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1171 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1172 INAND_CMD38_ARG_EXT_CSD,
1173 arg == MMC_TRIM_ARG ?
1174 INAND_CMD38_ARG_TRIM :
1175 INAND_CMD38_ARG_ERASE,
1176 0);
1177 }
1178 if (!err)
1179 err = mmc_erase(card, from, nr, arg);
1180 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001181 if (!err)
1182 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001183fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301184 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001185}
1186
Linus Walleijdf061582017-01-24 11:17:57 +01001187static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001188 struct request *req)
1189{
Linus Walleij7db30282016-11-18 13:36:15 +01001190 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001191 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001192 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001193 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001194
Maya Erez775a9362013-04-18 15:41:55 +03001195 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001196 err = -EOPNOTSUPP;
1197 goto out;
1198 }
1199
1200 from = blk_rq_pos(req);
1201 nr = blk_rq_sectors(req);
1202
Maya Erez775a9362013-04-18 15:41:55 +03001203 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1204 arg = MMC_SECURE_TRIM1_ARG;
1205 else
1206 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001207
Adrian Hunter67716322011-08-29 16:42:15 +03001208retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001209 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1210 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1211 INAND_CMD38_ARG_EXT_CSD,
1212 arg == MMC_SECURE_TRIM1_ARG ?
1213 INAND_CMD38_ARG_SECTRIM1 :
1214 INAND_CMD38_ARG_SECERASE,
1215 0);
1216 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001217 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001218 }
Adrian Hunter28302812012-04-05 14:45:48 +03001219
Adrian Hunter49804542010-08-11 14:17:50 -07001220 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001221 if (err == -EIO)
1222 goto out_retry;
1223 if (err)
1224 goto out;
1225
1226 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001227 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1228 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1229 INAND_CMD38_ARG_EXT_CSD,
1230 INAND_CMD38_ARG_SECTRIM2,
1231 0);
1232 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001233 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001234 }
Adrian Hunter28302812012-04-05 14:45:48 +03001235
Adrian Hunter49804542010-08-11 14:17:50 -07001236 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001237 if (err == -EIO)
1238 goto out_retry;
1239 if (err)
1240 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001241 }
Adrian Hunter28302812012-04-05 14:45:48 +03001242
Adrian Hunter28302812012-04-05 14:45:48 +03001243out_retry:
1244 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001245 goto retry;
1246 if (!err)
1247 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001248out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301249 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001250}
1251
Linus Walleijdf061582017-01-24 11:17:57 +01001252static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001253{
Linus Walleij7db30282016-11-18 13:36:15 +01001254 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001255 struct mmc_card *card = md->queue.card;
1256 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001257
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001258 ret = mmc_flush_cache(card);
1259 if (ret)
1260 ret = -EIO;
1261
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301262 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001263}
1264
1265/*
1266 * Reformat current write as a reliable write, supporting
1267 * both legacy and the enhanced reliable write MMC cards.
1268 * In each transfer we'll handle only as much as a single
1269 * reliable write can handle, thus finish the request in
1270 * partial completions.
1271 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001272static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1273 struct mmc_card *card,
1274 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001275{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001276 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1277 /* Legacy mode imposes restrictions on transfers. */
1278 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1279 brq->data.blocks = 1;
1280
1281 if (brq->data.blocks > card->ext_csd.rel_sectors)
1282 brq->data.blocks = card->ext_csd.rel_sectors;
1283 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1284 brq->data.blocks = 1;
1285 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001286}
1287
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001288#define CMD_ERRORS \
1289 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1290 R1_ADDRESS_ERROR | /* Misaligned address */ \
1291 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1292 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1293 R1_CC_ERROR | /* Card controller error */ \
1294 R1_ERROR) /* General/unknown error */
1295
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001296static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1297 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001298{
Per Forlinee8a43a2011-07-01 18:55:33 +02001299 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1300 mmc_active);
1301 struct mmc_blk_request *brq = &mq_mrq->brq;
1302 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001303 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001304 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001305 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001306
1307 /*
1308 * sbc.error indicates a problem with the set block count
1309 * command. No data will have been transferred.
1310 *
1311 * cmd.error indicates a problem with the r/w command. No
1312 * data will have been transferred.
1313 *
1314 * stop.error indicates a problem with the stop command. Data
1315 * may have been transferred, or may still be transferring.
1316 */
Adrian Hunter67716322011-08-29 16:42:15 +03001317 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1318 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001319 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001320 case ERR_RETRY:
1321 return MMC_BLK_RETRY;
1322 case ERR_ABORT:
1323 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301324 case ERR_NOMEDIUM:
1325 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001326 case ERR_CONTINUE:
1327 break;
1328 }
1329 }
1330
1331 /*
1332 * Check for errors relating to the execution of the
1333 * initial command - such as address errors. No data
1334 * has been transferred.
1335 */
1336 if (brq->cmd.resp[0] & CMD_ERRORS) {
1337 pr_err("%s: r/w command failed, status = %#x\n",
1338 req->rq_disk->disk_name, brq->cmd.resp[0]);
1339 return MMC_BLK_ABORT;
1340 }
1341
1342 /*
1343 * Everything else is either success, or a data error of some
1344 * kind. If it was a write, we may have transitioned to
1345 * program mode, which we have to wait for it to complete.
1346 */
1347 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001348 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001349
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001350 /* Check stop command response */
1351 if (brq->stop.resp[0] & R1_ERROR) {
1352 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1353 req->rq_disk->disk_name, __func__,
1354 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001355 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001356 }
1357
Ulf Hansson95a91292014-01-29 13:11:27 +01001358 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1359 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001360 if (err)
1361 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001362 }
1363
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001364 /* if general error occurs, retry the write operation. */
1365 if (gen_err) {
1366 pr_warn("%s: retrying write for general error\n",
1367 req->rq_disk->disk_name);
1368 return MMC_BLK_RETRY;
1369 }
1370
Per Forlind78d4a8a2011-07-01 18:55:30 +02001371 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001372 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001373 pr_debug("%s: retrying because a re-tune was needed\n",
1374 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001375 brq->retune_retry_done = 1;
1376 return MMC_BLK_RETRY;
1377 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001378 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1379 req->rq_disk->disk_name, brq->data.error,
1380 (unsigned)blk_rq_pos(req),
1381 (unsigned)blk_rq_sectors(req),
1382 brq->cmd.resp[0], brq->stop.resp[0]);
1383
1384 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001385 if (ecc_err)
1386 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001387 return MMC_BLK_DATA_ERR;
1388 } else {
1389 return MMC_BLK_CMD_ERR;
1390 }
1391 }
1392
Adrian Hunter67716322011-08-29 16:42:15 +03001393 if (!brq->data.bytes_xfered)
1394 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001395
Adrian Hunter67716322011-08-29 16:42:15 +03001396 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1397 return MMC_BLK_PARTIAL;
1398
1399 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001400}
1401
Per Forlin54d49d72011-07-01 18:55:29 +02001402static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1403 struct mmc_card *card,
1404 int disable_multi,
1405 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Per Forlin54d49d72011-07-01 18:55:29 +02001407 u32 readcmd, writecmd;
1408 struct mmc_blk_request *brq = &mqrq->brq;
1409 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001410 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301411 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001413 /*
1414 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001415 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001416 */
Luca Porziod3df0462015-11-06 15:12:26 +00001417 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001418 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001419 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420
Per Forlin54d49d72011-07-01 18:55:29 +02001421 memset(brq, 0, sizeof(struct mmc_blk_request));
1422 brq->mrq.cmd = &brq->cmd;
1423 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Per Forlin54d49d72011-07-01 18:55:29 +02001425 brq->cmd.arg = blk_rq_pos(req);
1426 if (!mmc_card_blockaddr(card))
1427 brq->cmd.arg <<= 9;
1428 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1429 brq->data.blksz = 512;
1430 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1431 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001432 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Per Forlin54d49d72011-07-01 18:55:29 +02001434 /*
1435 * The block layer doesn't support all sector count
1436 * restrictions, so we need to be prepared for too big
1437 * requests.
1438 */
1439 if (brq->data.blocks > card->host->max_blk_count)
1440 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001442 if (brq->data.blocks > 1) {
1443 /*
1444 * After a read error, we redo the request one sector
1445 * at a time in order to accurately determine which
1446 * sectors can be read successfully.
1447 */
1448 if (disable_multi)
1449 brq->data.blocks = 1;
1450
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001451 /*
1452 * Some controllers have HW issues while operating
1453 * in multiple I/O mode
1454 */
1455 if (card->host->ops->multi_io_quirk)
1456 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1457 (rq_data_dir(req) == READ) ?
1458 MMC_DATA_READ : MMC_DATA_WRITE,
1459 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001460 }
Per Forlin54d49d72011-07-01 18:55:29 +02001461
1462 if (brq->data.blocks > 1 || do_rel_wr) {
1463 /* SPI multiblock writes terminate using a special
1464 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001465 */
Per Forlin54d49d72011-07-01 18:55:29 +02001466 if (!mmc_host_is_spi(card->host) ||
1467 rq_data_dir(req) == READ)
1468 brq->mrq.stop = &brq->stop;
1469 readcmd = MMC_READ_MULTIPLE_BLOCK;
1470 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1471 } else {
1472 brq->mrq.stop = NULL;
1473 readcmd = MMC_READ_SINGLE_BLOCK;
1474 writecmd = MMC_WRITE_BLOCK;
1475 }
1476 if (rq_data_dir(req) == READ) {
1477 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001478 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001479 if (brq->mrq.stop)
1480 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1481 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001482 } else {
1483 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001484 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001485 if (brq->mrq.stop)
1486 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1487 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001488 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001489
Per Forlin54d49d72011-07-01 18:55:29 +02001490 if (do_rel_wr)
1491 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001492
Per Forlin54d49d72011-07-01 18:55:29 +02001493 /*
Saugata Das42659002011-12-21 13:09:17 +05301494 * Data tag is used only during writing meta data to speed
1495 * up write and any subsequent read of this meta data
1496 */
1497 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1498 (req->cmd_flags & REQ_META) &&
1499 (rq_data_dir(req) == WRITE) &&
1500 ((brq->data.blocks * brq->data.blksz) >=
1501 card->ext_csd.data_tag_unit_size);
1502
1503 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001504 * Pre-defined multi-block transfers are preferable to
1505 * open ended-ones (and necessary for reliable writes).
1506 * However, it is not sufficient to just send CMD23,
1507 * and avoid the final CMD12, as on an error condition
1508 * CMD12 (stop) needs to be sent anyway. This, coupled
1509 * with Auto-CMD23 enhancements provided by some
1510 * hosts, means that the complexity of dealing
1511 * with this is best left to the host. If CMD23 is
1512 * supported by card and host, we'll fill sbc in and let
1513 * the host deal with handling it correctly. This means
1514 * that for hosts that don't expose MMC_CAP_CMD23, no
1515 * change of behavior will be observed.
1516 *
1517 * N.B: Some MMC cards experience perf degradation.
1518 * We'll avoid using CMD23-bounded multiblock writes for
1519 * these, while retaining features like reliable writes.
1520 */
Saugata Das42659002011-12-21 13:09:17 +05301521 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1522 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1523 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001524 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1525 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301526 (do_rel_wr ? (1 << 31) : 0) |
1527 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001528 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1529 brq->mrq.sbc = &brq->sbc;
1530 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001531
Per Forlin54d49d72011-07-01 18:55:29 +02001532 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001533
Per Forlin54d49d72011-07-01 18:55:29 +02001534 brq->data.sg = mqrq->sg;
1535 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001536
Per Forlin54d49d72011-07-01 18:55:29 +02001537 /*
1538 * Adjust the sg list so it is the same size as the
1539 * request.
1540 */
1541 if (brq->data.blocks != blk_rq_sectors(req)) {
1542 int i, data_size = brq->data.blocks << 9;
1543 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001544
Per Forlin54d49d72011-07-01 18:55:29 +02001545 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1546 data_size -= sg->length;
1547 if (data_size <= 0) {
1548 sg->length += data_size;
1549 i++;
1550 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001551 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001552 }
Per Forlin54d49d72011-07-01 18:55:29 +02001553 brq->data.sg_len = i;
1554 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001555
Per Forlinee8a43a2011-07-01 18:55:33 +02001556 mqrq->mmc_active.mrq = &brq->mrq;
1557 mqrq->mmc_active.err_check = mmc_blk_err_check;
1558
Per Forlin54d49d72011-07-01 18:55:29 +02001559 mmc_queue_bounce_pre(mqrq);
1560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Adrian Hunter67716322011-08-29 16:42:15 +03001562static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1563 struct mmc_blk_request *brq, struct request *req,
1564 int ret)
1565{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001566 struct mmc_queue_req *mq_rq;
1567 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1568
Adrian Hunter67716322011-08-29 16:42:15 +03001569 /*
1570 * If this is an SD card and we're writing, we can first
1571 * mark the known good sectors as ok.
1572 *
1573 * If the card is not SD, we can still ok written sectors
1574 * as reported by the controller (which might be less than
1575 * the real number of written sectors, but never more).
1576 */
1577 if (mmc_card_sd(card)) {
1578 u32 blocks;
1579
1580 blocks = mmc_sd_num_wr_blocks(card);
1581 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301582 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001583 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001584 } else {
1585 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001586 }
1587 return ret;
1588}
1589
Linus Walleij4e1f7802017-01-24 11:17:52 +01001590static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1591{
1592 int ret = 1;
1593
1594 if (mmc_card_removed(card))
1595 req->rq_flags |= RQF_QUIET;
1596 while (ret)
1597 ret = blk_end_request(req, -EIO,
1598 blk_rq_cur_bytes(req));
1599}
1600
Linus Walleijefb5a052017-01-24 11:17:53 +01001601static void mmc_blk_rw_start_new(struct mmc_queue *mq, struct mmc_card *card,
1602 struct request *req)
1603{
1604 if (!req)
1605 return;
1606
1607 if (mmc_card_removed(card)) {
1608 req->rq_flags |= RQF_QUIET;
1609 blk_end_request_all(req, -EIO);
1610 } else {
1611 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1612 mmc_start_req(card->host,
1613 &mq->mqrq_cur->mmc_active, NULL);
1614 }
1615}
1616
Linus Walleijdf061582017-01-24 11:17:57 +01001617static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001618{
Linus Walleij7db30282016-11-18 13:36:15 +01001619 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001620 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001621 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001622 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001623 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001624 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001625 struct request *req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001626 struct mmc_async_req *new_areq;
1627 struct mmc_async_req *old_areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001628
1629 if (!rqc && !mq->mqrq_prev->req)
Linus Walleijdf061582017-01-24 11:17:57 +01001630 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001631
1632 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001633 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301634 /*
1635 * When 4KB native sector is enabled, only 8 blocks
1636 * multiple read or write is allowed
1637 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001638 if (mmc_large_sector(card) &&
1639 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301640 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001641 rqc->rq_disk->disk_name);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001642 mmc_blk_rw_cmd_abort(card, rqc);
Linus Walleijdf061582017-01-24 11:17:57 +01001643 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301644 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001645
Linus Walleij03d640a2016-11-25 10:35:00 +01001646 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Linus Walleij7d552a42017-01-24 11:17:56 +01001647 new_areq = &mq->mqrq_cur->mmc_active;
Per Forlinee8a43a2011-07-01 18:55:33 +02001648 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001649 new_areq = NULL;
1650
1651 old_areq = mmc_start_req(card->host, new_areq, &status);
1652 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001653 /*
1654 * We have just put the first request into the pipeline
1655 * and there is nothing more to do until it is
1656 * complete.
1657 */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001658 if (status == MMC_BLK_NEW_REQUEST)
1659 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Linus Walleijdf061582017-01-24 11:17:57 +01001660 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001661 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001662
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001663 /*
1664 * An asynchronous request has been completed and we proceed
1665 * to handle the result of it.
1666 */
Linus Walleij7d552a42017-01-24 11:17:56 +01001667 mq_rq = container_of(old_areq, struct mmc_queue_req, mmc_active);
Per Forlinee8a43a2011-07-01 18:55:33 +02001668 brq = &mq_rq->brq;
1669 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001670 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001671 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001672
Per Forlind78d4a8a2011-07-01 18:55:30 +02001673 switch (status) {
1674 case MMC_BLK_SUCCESS:
1675 case MMC_BLK_PARTIAL:
1676 /*
1677 * A block was successfully transferred.
1678 */
Adrian Hunter67716322011-08-29 16:42:15 +03001679 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001680
Linus Walleij03d640a2016-11-25 10:35:00 +01001681 ret = blk_end_request(req, 0,
1682 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001683
Adrian Hunter67716322011-08-29 16:42:15 +03001684 /*
1685 * If the blk_end_request function returns non-zero even
1686 * though all data has been transferred and no errors
1687 * were returned by the host controller, it's a bug.
1688 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001689 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301690 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001691 __func__, blk_rq_bytes(req),
1692 brq->data.bytes_xfered);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001693 mmc_blk_rw_cmd_abort(card, req);
Linus Walleijdf061582017-01-24 11:17:57 +01001694 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001695 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001696 break;
1697 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001698 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Linus Walleijdb435502017-02-01 13:47:51 +01001699 if (mmc_blk_reset(md, card->host, type)) {
1700 mmc_blk_rw_cmd_abort(card, req);
1701 mmc_blk_rw_start_new(mq, card, rqc);
1702 return;
1703 }
1704 if (!ret) {
1705 mmc_blk_rw_start_new(mq, card, rqc);
1706 return;
1707 }
Ding Wang29535f72015-05-18 20:14:15 +08001708 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001709 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001710 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001711 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001712 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001713 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001714 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001715 if (!mmc_blk_reset(md, card->host, type))
1716 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001717 mmc_blk_rw_cmd_abort(card, req);
1718 mmc_blk_rw_start_new(mq, card, rqc);
1719 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001720 case MMC_BLK_DATA_ERR: {
1721 int err;
1722
1723 err = mmc_blk_reset(md, card->host, type);
1724 if (!err)
1725 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001726 if (err == -ENODEV) {
1727 mmc_blk_rw_cmd_abort(card, req);
1728 mmc_blk_rw_start_new(mq, card, rqc);
1729 return;
1730 }
Adrian Hunter67716322011-08-29 16:42:15 +03001731 /* Fall through */
1732 }
1733 case MMC_BLK_ECC_ERR:
1734 if (brq->data.blocks > 1) {
1735 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001736 pr_warn("%s: retrying using single block read\n",
1737 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001738 disable_multi = 1;
1739 break;
1740 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001741 /*
1742 * After an error, we redo I/O one sector at a
1743 * time, so we only reach here after trying to
1744 * read a single sector.
1745 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301746 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001747 brq->data.blksz);
Linus Walleijdb435502017-02-01 13:47:51 +01001748 if (!ret) {
1749 mmc_blk_rw_start_new(mq, card, rqc);
1750 return;
1751 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001752 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301753 case MMC_BLK_NOMEDIUM:
Linus Walleijdb435502017-02-01 13:47:51 +01001754 mmc_blk_rw_cmd_abort(card, req);
1755 mmc_blk_rw_start_new(mq, card, rqc);
1756 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001757 default:
1758 pr_err("%s: Unhandled return value (%d)",
1759 req->rq_disk->disk_name, status);
Linus Walleijdb435502017-02-01 13:47:51 +01001760 mmc_blk_rw_cmd_abort(card, req);
1761 mmc_blk_rw_start_new(mq, card, rqc);
1762 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001763 }
1764
Per Forlinee8a43a2011-07-01 18:55:33 +02001765 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001766 /*
1767 * In case of a incomplete request
1768 * prepare it again and resend.
1769 */
1770 mmc_blk_rw_rq_prep(mq_rq, card,
1771 disable_multi, mq);
1772 mmc_start_req(card->host,
1773 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001774 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 } while (ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Linus Walleijdf061582017-01-24 11:17:57 +01001779void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001780{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001781 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001782 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001783 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001784 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001785
Per Forlinee8a43a2011-07-01 18:55:33 +02001786 if (req && !mq->mqrq_prev->req)
1787 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001788 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001789
Andrei Warkentin371a6892011-04-11 18:10:25 -05001790 ret = mmc_blk_part_switch(card, md);
1791 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001792 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301793 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001794 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001795 goto out;
1796 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001797
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001798 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001799 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001800 /* complete ongoing async transfer before issuing discard */
1801 if (card->host->areq)
1802 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001803 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001804 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1805 /* complete ongoing async transfer before issuing secure erase*/
1806 if (card->host->areq)
1807 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001808 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001809 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001810 /* complete ongoing async transfer before issuing flush */
1811 if (card->host->areq)
1812 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001813 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001814 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001815 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001816 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001817
Andrei Warkentin371a6892011-04-11 18:10:25 -05001818out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001819 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001820 /*
1821 * Release host when there are no more requests
1822 * and after special request(discard, flush) is done.
1823 * In case sepecial request, there is no reentry to
1824 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1825 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001826 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Russell Kinga6f6c962006-01-03 22:38:44 +00001829static inline int mmc_blk_readonly(struct mmc_card *card)
1830{
1831 return mmc_card_readonly(card) ||
1832 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1833}
1834
Andrei Warkentin371a6892011-04-11 18:10:25 -05001835static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1836 struct device *parent,
1837 sector_t size,
1838 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001839 const char *subname,
1840 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct mmc_blk_data *md;
1843 int devidx, ret;
1844
Heiner Kallweita04848c2017-02-01 19:44:22 +01001845 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1846 if (devidx < 0)
1847 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001849 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001850 if (!md) {
1851 ret = -ENOMEM;
1852 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001854
Johan Rudholmadd710e2011-12-02 08:51:06 +01001855 md->area_type = area_type;
1856
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001857 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001858 * Set the read-only status based on the supported commands
1859 * and the write protect switch.
1860 */
1861 md->read_only = mmc_blk_readonly(card);
1862
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001863 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001864 if (md->disk == NULL) {
1865 ret = -ENOMEM;
1866 goto err_kfree;
1867 }
1868
1869 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001870 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001871 md->usage = 1;
1872
Adrian Hunterd09408a2011-06-23 13:40:28 +03001873 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001874 if (ret)
1875 goto err_putdisk;
1876
Linus Walleij7db30282016-11-18 13:36:15 +01001877 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001878
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001879 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001880 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001881 md->disk->fops = &mmc_bdops;
1882 md->disk->private_data = md;
1883 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001884 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001885 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001886 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001887 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001888 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001889
1890 /*
1891 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1892 *
1893 * - be set for removable media with permanent block devices
1894 * - be unset for removable block devices with permanent media
1895 *
1896 * Since MMC block devices clearly fall under the second
1897 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1898 * should use the block device creation/destruction hotplug
1899 * messages to tell when the card is present.
1900 */
1901
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001902 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001903 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001904
Saugata Dasa5075eb2012-05-17 16:32:21 +05301905 if (mmc_card_mmc(card))
1906 blk_queue_logical_block_size(md->queue.queue,
1907 card->ext_csd.data_sector_size);
1908 else
1909 blk_queue_logical_block_size(md->queue.queue, 512);
1910
Andrei Warkentin371a6892011-04-11 18:10:25 -05001911 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001912
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001913 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001914 if ((mmc_card_mmc(card) &&
1915 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001916 (mmc_card_sd(card) &&
1917 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1918 md->flags |= MMC_BLK_CMD23;
1919 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001920
1921 if (mmc_card_mmc(card) &&
1922 md->flags & MMC_BLK_CMD23 &&
1923 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1924 card->ext_csd.rel_sectors)) {
1925 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001926 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001927 }
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001930
1931 err_putdisk:
1932 put_disk(md->disk);
1933 err_kfree:
1934 kfree(md);
1935 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01001936 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00001937 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Andrei Warkentin371a6892011-04-11 18:10:25 -05001940static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1941{
1942 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001943
1944 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1945 /*
1946 * The EXT_CSD sector count is in number or 512 byte
1947 * sectors.
1948 */
1949 size = card->ext_csd.sectors;
1950 } else {
1951 /*
1952 * The CSD capacity field is in units of read_blkbits.
1953 * set_capacity takes units of 512 bytes.
1954 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001955 size = (typeof(sector_t))card->csd.capacity
1956 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001957 }
1958
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001959 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001960 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001961}
1962
1963static int mmc_blk_alloc_part(struct mmc_card *card,
1964 struct mmc_blk_data *md,
1965 unsigned int part_type,
1966 sector_t size,
1967 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001968 const char *subname,
1969 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001970{
1971 char cap_str[10];
1972 struct mmc_blk_data *part_md;
1973
1974 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001975 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001976 if (IS_ERR(part_md))
1977 return PTR_ERR(part_md);
1978 part_md->part_type = part_type;
1979 list_add(&part_md->part, &md->part);
1980
James Bottomleyb9f28d82015-03-05 18:47:01 -08001981 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001982 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301983 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001984 part_md->disk->disk_name, mmc_card_id(card),
1985 mmc_card_name(card), part_md->part_type, cap_str);
1986 return 0;
1987}
1988
Namjae Jeone0c368d2011-10-06 23:41:38 +09001989/* MMC Physical partitions consist of two boot partitions and
1990 * up to four general purpose partitions.
1991 * For each partition enabled in EXT_CSD a block device will be allocatedi
1992 * to provide access to the partition.
1993 */
1994
Andrei Warkentin371a6892011-04-11 18:10:25 -05001995static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1996{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001997 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001998
1999 if (!mmc_card_mmc(card))
2000 return 0;
2001
Namjae Jeone0c368d2011-10-06 23:41:38 +09002002 for (idx = 0; idx < card->nr_parts; idx++) {
2003 if (card->part[idx].size) {
2004 ret = mmc_blk_alloc_part(card, md,
2005 card->part[idx].part_cfg,
2006 card->part[idx].size >> 9,
2007 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002008 card->part[idx].name,
2009 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002010 if (ret)
2011 return ret;
2012 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002013 }
2014
2015 return ret;
2016}
2017
Andrei Warkentin371a6892011-04-11 18:10:25 -05002018static void mmc_blk_remove_req(struct mmc_blk_data *md)
2019{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002020 struct mmc_card *card;
2021
Andrei Warkentin371a6892011-04-11 18:10:25 -05002022 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002023 /*
2024 * Flush remaining requests and free queues. It
2025 * is freeing the queue that stops new requests
2026 * from being accepted.
2027 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002028 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002029 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002030 if (md->disk->flags & GENHD_FL_UP) {
2031 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002032 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2033 card->ext_csd.boot_ro_lockable)
2034 device_remove_file(disk_to_dev(md->disk),
2035 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036
Andrei Warkentin371a6892011-04-11 18:10:25 -05002037 del_gendisk(md->disk);
2038 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039 mmc_blk_put(md);
2040 }
2041}
2042
2043static void mmc_blk_remove_parts(struct mmc_card *card,
2044 struct mmc_blk_data *md)
2045{
2046 struct list_head *pos, *q;
2047 struct mmc_blk_data *part_md;
2048
2049 list_for_each_safe(pos, q, &md->part) {
2050 part_md = list_entry(pos, struct mmc_blk_data, part);
2051 list_del(pos);
2052 mmc_blk_remove_req(part_md);
2053 }
2054}
2055
2056static int mmc_add_disk(struct mmc_blk_data *md)
2057{
2058 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002059 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060
Dan Williams307d8e62016-06-20 10:40:44 -07002061 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002062 md->force_ro.show = force_ro_show;
2063 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302064 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002065 md->force_ro.attr.name = "force_ro";
2066 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2067 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2068 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002069 goto force_ro_fail;
2070
2071 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2072 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002073 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002074
2075 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2076 mode = S_IRUGO;
2077 else
2078 mode = S_IRUGO | S_IWUSR;
2079
2080 md->power_ro_lock.show = power_ro_lock_show;
2081 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002082 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002083 md->power_ro_lock.attr.mode = mode;
2084 md->power_ro_lock.attr.name =
2085 "ro_lock_until_next_power_on";
2086 ret = device_create_file(disk_to_dev(md->disk),
2087 &md->power_ro_lock);
2088 if (ret)
2089 goto power_ro_lock_fail;
2090 }
2091 return ret;
2092
2093power_ro_lock_fail:
2094 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2095force_ro_fail:
2096 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002097
2098 return ret;
2099}
2100
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002101static const struct mmc_fixup blk_fixups[] =
2102{
Chris Ballc59d4472011-11-11 22:01:43 -05002103 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2104 MMC_QUIRK_INAND_CMD38),
2105 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2106 MMC_QUIRK_INAND_CMD38),
2107 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2108 MMC_QUIRK_INAND_CMD38),
2109 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2110 MMC_QUIRK_INAND_CMD38),
2111 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2112 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002113
2114 /*
2115 * Some MMC cards experience performance degradation with CMD23
2116 * instead of CMD12-bounded multiblock transfers. For now we'll
2117 * black list what's bad...
2118 * - Certain Toshiba cards.
2119 *
2120 * N.B. This doesn't affect SD cards.
2121 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002122 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2123 MMC_QUIRK_BLK_NO_CMD23),
2124 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2125 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002126 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002127 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002128 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002129 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002130 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002131 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002132
2133 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002134 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002135 */
Chris Ballc59d4472011-11-11 22:01:43 -05002136 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002137 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002138 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2139 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002140
Ian Chen3550ccd2012-08-29 15:05:36 +09002141 /*
2142 * On these Samsung MoviNAND parts, performing secure erase or
2143 * secure trim can result in unrecoverable corruption due to a
2144 * firmware bug.
2145 */
2146 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2147 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2148 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2149 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2150 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2151 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2152 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2153 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2154 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2155 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2156 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2157 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2158 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2159 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2160 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2161 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2162
Shawn Linb5b4ff02015-08-12 13:08:32 +08002163 /*
2164 * On Some Kingston eMMCs, performing trim can result in
2165 * unrecoverable data conrruption occasionally due to a firmware bug.
2166 */
2167 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2168 MMC_QUIRK_TRIM_BROKEN),
2169 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2170 MMC_QUIRK_TRIM_BROKEN),
2171
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002172 END_FIXUP
2173};
2174
Ulf Hansson96541ba2015-04-14 13:06:12 +02002175static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002178 char cap_str[10];
2179
Pierre Ossman912490d2005-05-21 10:27:02 +01002180 /*
2181 * Check that the card supports the command class(es) we need.
2182 */
2183 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return -ENODEV;
2185
Lukas Czerner5204d002014-06-18 13:18:07 +02002186 mmc_fixup_device(card, blk_fixups);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 md = mmc_blk_alloc(card);
2189 if (IS_ERR(md))
2190 return PTR_ERR(md);
2191
James Bottomleyb9f28d82015-03-05 18:47:01 -08002192 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002193 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302194 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002196 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Andrei Warkentin371a6892011-04-11 18:10:25 -05002198 if (mmc_blk_alloc_parts(card, md))
2199 goto out;
2200
Ulf Hansson96541ba2015-04-14 13:06:12 +02002201 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002202
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203 if (mmc_add_disk(md))
2204 goto out;
2205
2206 list_for_each_entry(part_md, &md->part, part) {
2207 if (mmc_add_disk(part_md))
2208 goto out;
2209 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002210
2211 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2212 pm_runtime_use_autosuspend(&card->dev);
2213
2214 /*
2215 * Don't enable runtime PM for SD-combo cards here. Leave that
2216 * decision to be taken during the SDIO init sequence instead.
2217 */
2218 if (card->type != MMC_TYPE_SD_COMBO) {
2219 pm_runtime_set_active(&card->dev);
2220 pm_runtime_enable(&card->dev);
2221 }
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 0;
2224
2225 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002226 mmc_blk_remove_parts(card, md);
2227 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
Ulf Hansson96541ba2015-04-14 13:06:12 +02002231static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002233 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002236 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002237 mmc_claim_host(card->host);
2238 mmc_blk_part_switch(card, md);
2239 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002240 if (card->type != MMC_TYPE_SD_COMBO)
2241 pm_runtime_disable(&card->dev);
2242 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002243 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002244 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Ulf Hansson96541ba2015-04-14 13:06:12 +02002247static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002249 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002250 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 if (md) {
2253 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002254 list_for_each_entry(part_md, &md->part, part) {
2255 mmc_queue_suspend(&part_md->queue);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 return 0;
2259}
2260
Ulf Hansson96541ba2015-04-14 13:06:12 +02002261static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002262{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002263 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002264}
2265
Ulf Hansson0967edc2014-10-06 11:29:42 +02002266#ifdef CONFIG_PM_SLEEP
2267static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002268{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002269 struct mmc_card *card = mmc_dev_to_card(dev);
2270
2271 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002272}
2273
Ulf Hansson0967edc2014-10-06 11:29:42 +02002274static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002276 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002277 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002280 /*
2281 * Resume involves the card going into idle state,
2282 * so current partition is always the main one.
2283 */
2284 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002286 list_for_each_entry(part_md, &md->part, part) {
2287 mmc_queue_resume(&part_md->queue);
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 return 0;
2291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292#endif
2293
Ulf Hansson0967edc2014-10-06 11:29:42 +02002294static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2295
Ulf Hansson96541ba2015-04-14 13:06:12 +02002296static struct mmc_driver mmc_driver = {
2297 .drv = {
2298 .name = "mmcblk",
2299 .pm = &mmc_blk_pm_ops,
2300 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .probe = mmc_blk_probe,
2302 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002303 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304};
2305
2306static int __init mmc_blk_init(void)
2307{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002308 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002310 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2311 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2312
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002313 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002314
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002315 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2316 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002319 res = mmc_register_driver(&mmc_driver);
2320 if (res)
2321 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002323 return 0;
2324 out2:
2325 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 out:
2327 return res;
2328}
2329
2330static void __exit mmc_blk_exit(void)
2331{
2332 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002333 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336module_init(mmc_blk_init);
2337module_exit(mmc_blk_exit);
2338
2339MODULE_LICENSE("GPL");
2340MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2341