blob: 41b5dbcd5b5a67deafae60c61afb4e97742644f3 [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"
51#include "mmc_ops.h"
52#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060066#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050069
Luca Porziod3df0462015-11-06 15:12:26 +000070#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090071 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
90static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000107 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200117 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
123 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Arjan van de Vena621aae2006-01-12 18:43:35 +0000126static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400128module_param(perdev_minors, int, 0444);
129MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
130
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200131static inline int mmc_blk_part_switch(struct mmc_card *card,
132 struct mmc_blk_data *md);
133static int get_card_status(struct mmc_card *card, u32 *status, int retries);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
136{
137 struct mmc_blk_data *md;
138
Arjan van de Vena621aae2006-01-12 18:43:35 +0000139 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 md = disk->private_data;
141 if (md && md->usage == 0)
142 md = NULL;
143 if (md)
144 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000145 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return md;
148}
149
Andrei Warkentin371a6892011-04-11 18:10:25 -0500150static inline int mmc_get_devidx(struct gendisk *disk)
151{
Colin Cross382c55f2015-10-22 10:00:41 -0700152 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500153 return devidx;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void mmc_blk_put(struct mmc_blk_data *md)
157{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000158 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 md->usage--;
160 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500161 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800162 blk_cleanup_queue(md->queue.queue);
163
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200164 spin_lock(&mmc_blk_lock);
165 ida_remove(&mmc_blk_ida, devidx);
166 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kfree(md);
170 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000171 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Johan Rudholmadd710e2011-12-02 08:51:06 +0100174static ssize_t power_ro_lock_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
176{
177 int ret;
178 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
179 struct mmc_card *card = md->queue.card;
180 int locked = 0;
181
182 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
183 locked = 2;
184 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
185 locked = 1;
186
187 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
188
Tomas Winkler9098f842015-07-16 15:50:45 +0200189 mmc_blk_put(md);
190
Johan Rudholmadd710e2011-12-02 08:51:06 +0100191 return ret;
192}
193
194static ssize_t power_ro_lock_store(struct device *dev,
195 struct device_attribute *attr, const char *buf, size_t count)
196{
197 int ret;
198 struct mmc_blk_data *md, *part_md;
199 struct mmc_card *card;
200 unsigned long set;
201
202 if (kstrtoul(buf, 0, &set))
203 return -EINVAL;
204
205 if (set != 1)
206 return count;
207
208 md = mmc_blk_get(dev_to_disk(dev));
209 card = md->queue.card;
210
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200211 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100212
213 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
214 card->ext_csd.boot_ro_lock |
215 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
216 card->ext_csd.part_time);
217 if (ret)
218 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
219 else
220 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
221
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200222 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100223
224 if (!ret) {
225 pr_info("%s: Locking boot partition ro until next power on\n",
226 md->disk->disk_name);
227 set_disk_ro(md->disk, 1);
228
229 list_for_each_entry(part_md, &md->part, part)
230 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
231 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
232 set_disk_ro(part_md->disk, 1);
233 }
234 }
235
236 mmc_blk_put(md);
237 return count;
238}
239
Andrei Warkentin371a6892011-04-11 18:10:25 -0500240static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
241 char *buf)
242{
243 int ret;
244 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
245
Baruch Siach0031a982014-09-22 10:12:51 +0300246 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500247 get_disk_ro(dev_to_disk(dev)) ^
248 md->read_only);
249 mmc_blk_put(md);
250 return ret;
251}
252
253static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
255{
256 int ret;
257 char *end;
258 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
259 unsigned long set = simple_strtoul(buf, &end, 0);
260 if (end == buf) {
261 ret = -EINVAL;
262 goto out;
263 }
264
265 set_disk_ro(dev_to_disk(dev), set || md->read_only);
266 ret = count;
267out:
268 mmc_blk_put(md);
269 return ret;
270}
271
Al Viroa5a15612008-03-02 10:33:30 -0500272static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Al Viroa5a15612008-03-02 10:33:30 -0500274 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int ret = -ENXIO;
276
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200277 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (md) {
279 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500280 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700282
Al Viroa5a15612008-03-02 10:33:30 -0500283 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700284 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700285 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200288 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return ret;
291}
292
Al Virodb2a1442013-05-05 21:52:57 -0400293static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Al Viroa5a15612008-03-02 10:33:30 -0500295 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200297 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200299 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800303mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800305 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
306 geo->heads = 4;
307 geo->sectors = 16;
308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
John Calixtocb87ea22011-04-26 18:56:29 -0400311struct mmc_blk_ioc_data {
312 struct mmc_ioc_cmd ic;
313 unsigned char *buf;
314 u64 buf_bytes;
315};
316
317static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
318 struct mmc_ioc_cmd __user *user)
319{
320 struct mmc_blk_ioc_data *idata;
321 int err;
322
yalin wang1ff89502015-11-12 19:27:11 +0800323 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400324 if (!idata) {
325 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400326 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400327 }
328
329 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
330 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400331 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400332 }
333
334 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
335 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
336 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400337 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400338 }
339
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300340 if (!idata->buf_bytes) {
341 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100342 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300343 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100344
yalin wang1ff89502015-11-12 19:27:11 +0800345 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400346 if (!idata->buf) {
347 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
352 idata->ic.data_ptr, idata->buf_bytes)) {
353 err = -EFAULT;
354 goto copy_err;
355 }
356
357 return idata;
358
359copy_err:
360 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400361idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400362 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363out:
John Calixtocb87ea22011-04-26 18:56:29 -0400364 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400365}
366
Jon Huntera5f57742015-09-22 10:27:53 +0100367static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
368 struct mmc_blk_ioc_data *idata)
369{
370 struct mmc_ioc_cmd *ic = &idata->ic;
371
372 if (copy_to_user(&(ic_ptr->response), ic->response,
373 sizeof(ic->response)))
374 return -EFAULT;
375
376 if (!idata->ic.write_flag) {
377 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
378 idata->buf, idata->buf_bytes))
379 return -EFAULT;
380 }
381
382 return 0;
383}
384
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200385static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
386 u32 retries_max)
387{
388 int err;
389 u32 retry_count = 0;
390
391 if (!status || !retries_max)
392 return -EINVAL;
393
394 do {
395 err = get_card_status(card, status, 5);
396 if (err)
397 break;
398
399 if (!R1_STATUS(*status) &&
400 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
401 break; /* RPMB programming operation complete */
402
403 /*
404 * Rechedule to give the MMC device a chance to continue
405 * processing the previous command without being polled too
406 * frequently.
407 */
408 usleep_range(1000, 5000);
409 } while (++retry_count < retries_max);
410
411 if (retry_count == retries_max)
412 err = -EPERM;
413
414 return err;
415}
416
Maya Erez775a9362013-04-18 15:41:55 +0300417static int ioctl_do_sanitize(struct mmc_card *card)
418{
419 int err;
420
Ulf Hanssona2d10862013-12-16 14:37:26 +0100421 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300422 pr_warn("%s: %s - SANITIZE is not supported\n",
423 mmc_hostname(card->host), __func__);
424 err = -EOPNOTSUPP;
425 goto out;
426 }
427
428 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
429 mmc_hostname(card->host), __func__);
430
431 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
432 EXT_CSD_SANITIZE_START, 1,
433 MMC_SANITIZE_REQ_TIMEOUT);
434
435 if (err)
436 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
437 mmc_hostname(card->host), __func__, err);
438
439 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
440 __func__);
441out:
442 return err;
443}
444
Jon Huntera5f57742015-09-22 10:27:53 +0100445static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
446 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400447{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900448 struct mmc_command cmd = {};
449 struct mmc_data data = {};
450 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400451 struct scatterlist sg;
452 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200453 int is_rpmb = false;
454 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400455
Jon Huntera5f57742015-09-22 10:27:53 +0100456 if (!card || !md || !idata)
457 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400458
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200459 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
460 is_rpmb = true;
461
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100462 cmd.opcode = idata->ic.opcode;
463 cmd.arg = idata->ic.arg;
464 cmd.flags = idata->ic.flags;
465
466 if (idata->buf_bytes) {
467 data.sg = &sg;
468 data.sg_len = 1;
469 data.blksz = idata->ic.blksz;
470 data.blocks = idata->ic.blocks;
471
472 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
473
474 if (idata->ic.write_flag)
475 data.flags = MMC_DATA_WRITE;
476 else
477 data.flags = MMC_DATA_READ;
478
479 /* data.flags must already be set before doing this. */
480 mmc_set_data_timeout(&data, card);
481
482 /* Allow overriding the timeout_ns for empirical tuning. */
483 if (idata->ic.data_timeout_ns)
484 data.timeout_ns = idata->ic.data_timeout_ns;
485
486 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
487 /*
488 * Pretend this is a data transfer and rely on the
489 * host driver to compute timeout. When all host
490 * drivers support cmd.cmd_timeout for R1B, this
491 * can be changed to:
492 *
493 * mrq.data = NULL;
494 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
495 */
496 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
497 }
498
499 mrq.data = &data;
500 }
501
502 mrq.cmd = &cmd;
503
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200504 err = mmc_blk_part_switch(card, md);
505 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100506 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507
John Calixtocb87ea22011-04-26 18:56:29 -0400508 if (idata->ic.is_acmd) {
509 err = mmc_app_cmd(card->host, card);
510 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100511 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400512 }
513
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200514 if (is_rpmb) {
515 err = mmc_set_blockcount(card, data.blocks,
516 idata->ic.write_flag & (1 << 31));
517 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100518 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200519 }
520
Yaniv Gardia82e4842013-06-05 14:13:08 +0300521 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
522 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300523 err = ioctl_do_sanitize(card);
524
525 if (err)
526 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
527 __func__, err);
528
Jon Huntera5f57742015-09-22 10:27:53 +0100529 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300530 }
531
John Calixtocb87ea22011-04-26 18:56:29 -0400532 mmc_wait_for_req(card->host, &mrq);
533
534 if (cmd.error) {
535 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
536 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100537 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400538 }
539 if (data.error) {
540 dev_err(mmc_dev(card->host), "%s: data error %d\n",
541 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100542 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400543 }
544
545 /*
546 * According to the SD specs, some commands require a delay after
547 * issuing the command.
548 */
549 if (idata->ic.postsleep_min_us)
550 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
551
Jon Huntera5f57742015-09-22 10:27:53 +0100552 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400553
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200554 if (is_rpmb) {
555 /*
556 * Ensure RPMB command has completed by polling CMD13
557 * "Send Status".
558 */
559 err = ioctl_rpmb_card_status_poll(card, &status, 5);
560 if (err)
561 dev_err(mmc_dev(card->host),
562 "%s: Card Status=0x%08X, error %d\n",
563 __func__, status, err);
564 }
565
Jon Huntera5f57742015-09-22 10:27:53 +0100566 return err;
567}
568
569static int mmc_blk_ioctl_cmd(struct block_device *bdev,
570 struct mmc_ioc_cmd __user *ic_ptr)
571{
572 struct mmc_blk_ioc_data *idata;
573 struct mmc_blk_data *md;
574 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700575 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100576
Shawn Lin83c742c2016-03-16 18:15:47 +0800577 /*
578 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
579 * whole block device, not on a partition. This prevents overspray
580 * between sibling partitions.
581 */
582 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
583 return -EPERM;
584
Jon Huntera5f57742015-09-22 10:27:53 +0100585 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
586 if (IS_ERR(idata))
587 return PTR_ERR(idata);
588
589 md = mmc_blk_get(bdev->bd_disk);
590 if (!md) {
591 err = -EINVAL;
592 goto cmd_err;
593 }
594
595 card = md->queue.card;
596 if (IS_ERR(card)) {
597 err = PTR_ERR(card);
598 goto cmd_done;
599 }
600
601 mmc_get_card(card);
602
Grant Grundlerb0934102015-09-23 18:30:33 -0700603 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100604
Adrian Hunter3c866562016-05-04 14:38:12 +0300605 /* Always switch back to main area after RPMB access */
606 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
607 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
608
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200609 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400610
Grant Grundlerb0934102015-09-23 18:30:33 -0700611 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100612
John Calixtocb87ea22011-04-26 18:56:29 -0400613cmd_done:
614 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300615cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400616 kfree(idata->buf);
617 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700618 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400619}
620
Jon Huntera5f57742015-09-22 10:27:53 +0100621static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
622 struct mmc_ioc_multi_cmd __user *user)
623{
624 struct mmc_blk_ioc_data **idata = NULL;
625 struct mmc_ioc_cmd __user *cmds = user->cmds;
626 struct mmc_card *card;
627 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700628 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100629 __u64 num_of_cmds;
630
Shawn Lin83c742c2016-03-16 18:15:47 +0800631 /*
632 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
633 * whole block device, not on a partition. This prevents overspray
634 * between sibling partitions.
635 */
636 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
637 return -EPERM;
638
Jon Huntera5f57742015-09-22 10:27:53 +0100639 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
640 sizeof(num_of_cmds)))
641 return -EFAULT;
642
643 if (num_of_cmds > MMC_IOC_MAX_CMDS)
644 return -EINVAL;
645
646 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
647 if (!idata)
648 return -ENOMEM;
649
650 for (i = 0; i < num_of_cmds; i++) {
651 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
652 if (IS_ERR(idata[i])) {
653 err = PTR_ERR(idata[i]);
654 num_of_cmds = i;
655 goto cmd_err;
656 }
657 }
658
659 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800660 if (!md) {
661 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100662 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800663 }
Jon Huntera5f57742015-09-22 10:27:53 +0100664
665 card = md->queue.card;
666 if (IS_ERR(card)) {
667 err = PTR_ERR(card);
668 goto cmd_done;
669 }
670
671 mmc_get_card(card);
672
Grant Grundlerb0934102015-09-23 18:30:33 -0700673 for (i = 0; i < num_of_cmds && !ioc_err; i++)
674 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100675
Adrian Hunter3c866562016-05-04 14:38:12 +0300676 /* Always switch back to main area after RPMB access */
677 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
678 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
679
Jon Huntera5f57742015-09-22 10:27:53 +0100680 mmc_put_card(card);
681
682 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700683 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100684 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100685
686cmd_done:
687 mmc_blk_put(md);
688cmd_err:
689 for (i = 0; i < num_of_cmds; i++) {
690 kfree(idata[i]->buf);
691 kfree(idata[i]);
692 }
693 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700694 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100695}
696
John Calixtocb87ea22011-04-26 18:56:29 -0400697static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
698 unsigned int cmd, unsigned long arg)
699{
Jon Huntera5f57742015-09-22 10:27:53 +0100700 switch (cmd) {
701 case MMC_IOC_CMD:
702 return mmc_blk_ioctl_cmd(bdev,
703 (struct mmc_ioc_cmd __user *)arg);
704 case MMC_IOC_MULTI_CMD:
705 return mmc_blk_ioctl_multi_cmd(bdev,
706 (struct mmc_ioc_multi_cmd __user *)arg);
707 default:
708 return -EINVAL;
709 }
John Calixtocb87ea22011-04-26 18:56:29 -0400710}
711
712#ifdef CONFIG_COMPAT
713static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
714 unsigned int cmd, unsigned long arg)
715{
716 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
717}
718#endif
719
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700720static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500721 .open = mmc_blk_open,
722 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800723 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400725 .ioctl = mmc_blk_ioctl,
726#ifdef CONFIG_COMPAT
727 .compat_ioctl = mmc_blk_compat_ioctl,
728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729};
730
Andrei Warkentin371a6892011-04-11 18:10:25 -0500731static inline int mmc_blk_part_switch(struct mmc_card *card,
732 struct mmc_blk_data *md)
733{
734 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200735 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300736
Andrei Warkentin371a6892011-04-11 18:10:25 -0500737 if (main_md->part_curr == md->part_type)
738 return 0;
739
740 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300741 u8 part_config = card->ext_csd.part_config;
742
Adrian Hunter57da0c02016-05-04 14:38:13 +0300743 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
744 mmc_retune_pause(card->host);
745
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300746 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
747 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500748
749 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300750 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500751 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300752 if (ret) {
753 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
754 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500755 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300756 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300757
758 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300759
760 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
761 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300762 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500763
764 main_md->part_curr = md->part_type;
765 return 0;
766}
767
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700768static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
769{
770 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100771 u32 result;
772 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700773
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900774 struct mmc_request mrq = {};
775 struct mmc_command cmd = {};
776 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700777
778 struct scatterlist sg;
779
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700780 cmd.opcode = MMC_APP_CMD;
781 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700782 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783
784 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700785 if (err)
786 return (u32)-1;
787 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700788 return (u32)-1;
789
790 memset(&cmd, 0, sizeof(struct mmc_command));
791
792 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
793 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700794 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700795
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700796 data.blksz = 4;
797 data.blocks = 1;
798 data.flags = MMC_DATA_READ;
799 data.sg = &sg;
800 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530801 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700802
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700803 mrq.cmd = &cmd;
804 mrq.data = &data;
805
Ben Dooks051913d2009-06-08 23:33:57 +0100806 blocks = kmalloc(4, GFP_KERNEL);
807 if (!blocks)
808 return (u32)-1;
809
810 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700811
812 mmc_wait_for_req(card->host, &mrq);
813
Ben Dooks051913d2009-06-08 23:33:57 +0100814 result = ntohl(*blocks);
815 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700816
Ben Dooks051913d2009-06-08 23:33:57 +0100817 if (cmd.error || data.error)
818 result = (u32)-1;
819
820 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700821}
822
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100823static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300824{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900825 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300826 int err;
827
Adrian Hunter504f1912008-10-16 12:55:25 +0300828 cmd.opcode = MMC_SEND_STATUS;
829 if (!mmc_host_is_spi(card->host))
830 cmd.arg = card->rca << 16;
831 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100832 err = mmc_wait_for_cmd(card->host, &cmd, retries);
833 if (err == 0)
834 *status = cmd.resp[0];
835 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300836}
837
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100838static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100839 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100840{
841 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
842 int err = 0;
843 u32 status;
844
845 do {
846 err = get_card_status(card, &status, 5);
847 if (err) {
848 pr_err("%s: error %d requesting status\n",
849 req->rq_disk->disk_name, err);
850 return err;
851 }
852
853 if (status & R1_ERROR) {
854 pr_err("%s: %s: error sending status cmd, status %#x\n",
855 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100856 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100857 }
858
Ulf Hansson95a91292014-01-29 13:11:27 +0100859 /* We may rely on the host hw to handle busy detection.*/
860 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
861 hw_busy_detect)
862 break;
863
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100864 /*
865 * Timeout if the device never becomes ready for data and never
866 * leaves the program state.
867 */
868 if (time_after(jiffies, timeout)) {
869 pr_err("%s: Card stuck in programming state! %s %s\n",
870 mmc_hostname(card->host),
871 req->rq_disk->disk_name, __func__);
872 return -ETIMEDOUT;
873 }
874
875 /*
876 * Some cards mishandle the status bits,
877 * so make sure to check both the busy
878 * indication and the card state.
879 */
880 } while (!(status & R1_READY_FOR_DATA) ||
881 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
882
883 return err;
884}
885
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100886static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100887 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100888{
889 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900890 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100891 int err;
892 bool use_r1b_resp = rq_data_dir(req) == WRITE;
893
894 /*
895 * Normally we use R1B responses for WRITE, but in cases where the host
896 * has specified a max_busy_timeout we need to validate it. A failure
897 * means we need to prevent the host from doing hw busy detection, which
898 * is done by converting to a R1 response instead.
899 */
900 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
901 use_r1b_resp = false;
902
903 cmd.opcode = MMC_STOP_TRANSMISSION;
904 if (use_r1b_resp) {
905 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
906 cmd.busy_timeout = timeout_ms;
907 } else {
908 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
909 }
910
911 err = mmc_wait_for_cmd(host, &cmd, 5);
912 if (err)
913 return err;
914
915 *stop_status = cmd.resp[0];
916
917 /* No need to check card status in case of READ. */
918 if (rq_data_dir(req) == READ)
919 return 0;
920
921 if (!mmc_host_is_spi(host) &&
922 (*stop_status & R1_ERROR)) {
923 pr_err("%s: %s: general error sending stop command, resp %#x\n",
924 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100925 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100926 }
927
928 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
929}
930
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530931#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100932#define ERR_RETRY 2
933#define ERR_ABORT 1
934#define ERR_CONTINUE 0
935
936static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
937 bool status_valid, u32 status)
938{
939 switch (error) {
940 case -EILSEQ:
941 /* response crc error, retry the r/w cmd */
942 pr_err("%s: %s sending %s command, card status %#x\n",
943 req->rq_disk->disk_name, "response CRC error",
944 name, status);
945 return ERR_RETRY;
946
947 case -ETIMEDOUT:
948 pr_err("%s: %s sending %s command, card status %#x\n",
949 req->rq_disk->disk_name, "timed out", name, status);
950
951 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530952 if (!status_valid) {
953 pr_err("%s: status not valid, retrying timeout\n",
954 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100955 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530956 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100957
958 /*
959 * If it was a r/w cmd crc error, or illegal command
960 * (eg, issued in wrong state) then retry - we should
961 * have corrected the state problem above.
962 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530963 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
964 pr_err("%s: command error, retrying timeout\n",
965 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100966 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530967 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100968
969 /* Otherwise abort the command */
970 return ERR_ABORT;
971
972 default:
973 /* We don't understand the error code the driver gave us */
974 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
975 req->rq_disk->disk_name, error, status);
976 return ERR_ABORT;
977 }
978}
979
980/*
981 * Initial r/w and stop cmd error recovery.
982 * We don't know whether the card received the r/w cmd or not, so try to
983 * restore things back to a sane state. Essentially, we do this as follows:
984 * - Obtain card status. If the first attempt to obtain card status fails,
985 * the status word will reflect the failed status cmd, not the failed
986 * r/w cmd. If we fail to obtain card status, it suggests we can no
987 * longer communicate with the card.
988 * - Check the card state. If the card received the cmd but there was a
989 * transient problem with the response, it might still be in a data transfer
990 * mode. Try to send it a stop command. If this fails, we can't recover.
991 * - If the r/w cmd failed due to a response CRC error, it was probably
992 * transient, so retry the cmd.
993 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
994 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
995 * illegal cmd, retry.
996 * Otherwise we don't understand what happened, so abort.
997 */
998static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +0100999 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001000{
1001 bool prev_cmd_status_valid = true;
1002 u32 status, stop_status = 0;
1003 int err, retry;
1004
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301005 if (mmc_card_removed(card))
1006 return ERR_NOMEDIUM;
1007
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001008 /*
1009 * Try to get card status which indicates both the card state
1010 * and why there was no response. If the first attempt fails,
1011 * we can't be sure the returned status is for the r/w command.
1012 */
1013 for (retry = 2; retry >= 0; retry--) {
1014 err = get_card_status(card, &status, 0);
1015 if (!err)
1016 break;
1017
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001018 /* Re-tune if needed */
1019 mmc_retune_recheck(card->host);
1020
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001021 prev_cmd_status_valid = false;
1022 pr_err("%s: error %d sending status command, %sing\n",
1023 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1024 }
1025
1026 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301027 if (err) {
1028 /* Check if the card is removed */
1029 if (mmc_detect_card_removed(card->host))
1030 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001031 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301032 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001033
Adrian Hunter67716322011-08-29 16:42:15 +03001034 /* Flag ECC errors */
1035 if ((status & R1_CARD_ECC_FAILED) ||
1036 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1037 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001038 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001039
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001040 /* Flag General errors */
1041 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1042 if ((status & R1_ERROR) ||
1043 (brq->stop.resp[0] & R1_ERROR)) {
1044 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1045 req->rq_disk->disk_name, __func__,
1046 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001047 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001048 }
1049
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001050 /*
1051 * Check the current card state. If it is in some data transfer
1052 * mode, tell it to stop (and hopefully transition back to TRAN.)
1053 */
1054 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1055 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001056 err = send_stop(card,
1057 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1058 req, gen_err, &stop_status);
1059 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001060 pr_err("%s: error %d sending stop command\n",
1061 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001062 /*
1063 * If the stop cmd also timed out, the card is probably
1064 * not present, so abort. Other errors are bad news too.
1065 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001066 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001067 }
1068
Adrian Hunter67716322011-08-29 16:42:15 +03001069 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001070 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001071 }
1072
1073 /* Check for set block count errors */
1074 if (brq->sbc.error)
1075 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1076 prev_cmd_status_valid, status);
1077
1078 /* Check for r/w command errors */
1079 if (brq->cmd.error)
1080 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1081 prev_cmd_status_valid, status);
1082
Adrian Hunter67716322011-08-29 16:42:15 +03001083 /* Data errors */
1084 if (!brq->stop.error)
1085 return ERR_CONTINUE;
1086
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001087 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001088 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001089 req->rq_disk->disk_name, brq->stop.error,
1090 brq->cmd.resp[0], status);
1091
1092 /*
1093 * Subsitute in our own stop status as this will give the error
1094 * state which happened during the execution of the r/w command.
1095 */
1096 if (stop_status) {
1097 brq->stop.resp[0] = stop_status;
1098 brq->stop.error = 0;
1099 }
1100 return ERR_CONTINUE;
1101}
1102
Adrian Hunter67716322011-08-29 16:42:15 +03001103static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1104 int type)
1105{
1106 int err;
1107
1108 if (md->reset_done & type)
1109 return -EEXIST;
1110
1111 md->reset_done |= type;
1112 err = mmc_hw_reset(host);
1113 /* Ensure we switch back to the correct partition */
1114 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001115 struct mmc_blk_data *main_md =
1116 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001117 int part_err;
1118
1119 main_md->part_curr = main_md->part_type;
1120 part_err = mmc_blk_part_switch(host->card, md);
1121 if (part_err) {
1122 /*
1123 * We have failed to get back into the correct
1124 * partition, so we need to abort the whole request.
1125 */
1126 return -ENODEV;
1127 }
1128 }
1129 return err;
1130}
1131
1132static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1133{
1134 md->reset_done &= ~type;
1135}
1136
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001137int mmc_access_rpmb(struct mmc_queue *mq)
1138{
Linus Walleij7db30282016-11-18 13:36:15 +01001139 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001140 /*
1141 * If this is a RPMB partition access, return ture
1142 */
1143 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1144 return true;
1145
1146 return false;
1147}
1148
Adrian Hunterbd788c92010-08-11 14:17:47 -07001149static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1150{
Linus Walleij7db30282016-11-18 13:36:15 +01001151 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001152 struct mmc_card *card = md->queue.card;
1153 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001154 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001155
Adrian Hunterbd788c92010-08-11 14:17:47 -07001156 if (!mmc_can_erase(card)) {
1157 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001158 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001159 }
1160
1161 from = blk_rq_pos(req);
1162 nr = blk_rq_sectors(req);
1163
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001164 if (mmc_can_discard(card))
1165 arg = MMC_DISCARD_ARG;
1166 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001167 arg = MMC_TRIM_ARG;
1168 else
1169 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001170 do {
1171 err = 0;
1172 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1173 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1174 INAND_CMD38_ARG_EXT_CSD,
1175 arg == MMC_TRIM_ARG ?
1176 INAND_CMD38_ARG_TRIM :
1177 INAND_CMD38_ARG_ERASE,
1178 0);
1179 }
1180 if (!err)
1181 err = mmc_erase(card, from, nr, arg);
1182 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001183 if (!err)
1184 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001185fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301186 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001187
Adrian Hunterbd788c92010-08-11 14:17:47 -07001188 return err ? 0 : 1;
1189}
1190
Adrian Hunter49804542010-08-11 14:17:50 -07001191static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1192 struct request *req)
1193{
Linus Walleij7db30282016-11-18 13:36:15 +01001194 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001195 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001196 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001197 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001198
Maya Erez775a9362013-04-18 15:41:55 +03001199 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001200 err = -EOPNOTSUPP;
1201 goto out;
1202 }
1203
1204 from = blk_rq_pos(req);
1205 nr = blk_rq_sectors(req);
1206
Maya Erez775a9362013-04-18 15:41:55 +03001207 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1208 arg = MMC_SECURE_TRIM1_ARG;
1209 else
1210 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001211
Adrian Hunter67716322011-08-29 16:42:15 +03001212retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001213 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1214 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1215 INAND_CMD38_ARG_EXT_CSD,
1216 arg == MMC_SECURE_TRIM1_ARG ?
1217 INAND_CMD38_ARG_SECTRIM1 :
1218 INAND_CMD38_ARG_SECERASE,
1219 0);
1220 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001221 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001222 }
Adrian Hunter28302812012-04-05 14:45:48 +03001223
Adrian Hunter49804542010-08-11 14:17:50 -07001224 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001225 if (err == -EIO)
1226 goto out_retry;
1227 if (err)
1228 goto out;
1229
1230 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001231 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1232 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1233 INAND_CMD38_ARG_EXT_CSD,
1234 INAND_CMD38_ARG_SECTRIM2,
1235 0);
1236 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001237 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001238 }
Adrian Hunter28302812012-04-05 14:45:48 +03001239
Adrian Hunter49804542010-08-11 14:17:50 -07001240 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001241 if (err == -EIO)
1242 goto out_retry;
1243 if (err)
1244 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001245 }
Adrian Hunter28302812012-04-05 14:45:48 +03001246
Adrian Hunter28302812012-04-05 14:45:48 +03001247out_retry:
1248 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001249 goto retry;
1250 if (!err)
1251 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001252out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301253 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001254
Adrian Hunter49804542010-08-11 14:17:50 -07001255 return err ? 0 : 1;
1256}
1257
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001258static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1259{
Linus Walleij7db30282016-11-18 13:36:15 +01001260 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001261 struct mmc_card *card = md->queue.card;
1262 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001263
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001264 ret = mmc_flush_cache(card);
1265 if (ret)
1266 ret = -EIO;
1267
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301268 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001269
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001270 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001271}
1272
1273/*
1274 * Reformat current write as a reliable write, supporting
1275 * both legacy and the enhanced reliable write MMC cards.
1276 * In each transfer we'll handle only as much as a single
1277 * reliable write can handle, thus finish the request in
1278 * partial completions.
1279 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001280static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1281 struct mmc_card *card,
1282 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001283{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001284 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1285 /* Legacy mode imposes restrictions on transfers. */
1286 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1287 brq->data.blocks = 1;
1288
1289 if (brq->data.blocks > card->ext_csd.rel_sectors)
1290 brq->data.blocks = card->ext_csd.rel_sectors;
1291 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1292 brq->data.blocks = 1;
1293 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001294}
1295
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001296#define CMD_ERRORS \
1297 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1298 R1_ADDRESS_ERROR | /* Misaligned address */ \
1299 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1300 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1301 R1_CC_ERROR | /* Card controller error */ \
1302 R1_ERROR) /* General/unknown error */
1303
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001304static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1305 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001306{
Per Forlinee8a43a2011-07-01 18:55:33 +02001307 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1308 mmc_active);
1309 struct mmc_blk_request *brq = &mq_mrq->brq;
1310 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001311 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001312 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001313 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001314
1315 /*
1316 * sbc.error indicates a problem with the set block count
1317 * command. No data will have been transferred.
1318 *
1319 * cmd.error indicates a problem with the r/w command. No
1320 * data will have been transferred.
1321 *
1322 * stop.error indicates a problem with the stop command. Data
1323 * may have been transferred, or may still be transferring.
1324 */
Adrian Hunter67716322011-08-29 16:42:15 +03001325 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1326 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001327 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001328 case ERR_RETRY:
1329 return MMC_BLK_RETRY;
1330 case ERR_ABORT:
1331 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301332 case ERR_NOMEDIUM:
1333 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001334 case ERR_CONTINUE:
1335 break;
1336 }
1337 }
1338
1339 /*
1340 * Check for errors relating to the execution of the
1341 * initial command - such as address errors. No data
1342 * has been transferred.
1343 */
1344 if (brq->cmd.resp[0] & CMD_ERRORS) {
1345 pr_err("%s: r/w command failed, status = %#x\n",
1346 req->rq_disk->disk_name, brq->cmd.resp[0]);
1347 return MMC_BLK_ABORT;
1348 }
1349
1350 /*
1351 * Everything else is either success, or a data error of some
1352 * kind. If it was a write, we may have transitioned to
1353 * program mode, which we have to wait for it to complete.
1354 */
1355 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001356 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001357
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001358 /* Check stop command response */
1359 if (brq->stop.resp[0] & R1_ERROR) {
1360 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1361 req->rq_disk->disk_name, __func__,
1362 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001363 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001364 }
1365
Ulf Hansson95a91292014-01-29 13:11:27 +01001366 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1367 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001368 if (err)
1369 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001370 }
1371
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001372 /* if general error occurs, retry the write operation. */
1373 if (gen_err) {
1374 pr_warn("%s: retrying write for general error\n",
1375 req->rq_disk->disk_name);
1376 return MMC_BLK_RETRY;
1377 }
1378
Per Forlind78d4a8a2011-07-01 18:55:30 +02001379 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001380 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001381 pr_debug("%s: retrying because a re-tune was needed\n",
1382 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001383 brq->retune_retry_done = 1;
1384 return MMC_BLK_RETRY;
1385 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001386 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1387 req->rq_disk->disk_name, brq->data.error,
1388 (unsigned)blk_rq_pos(req),
1389 (unsigned)blk_rq_sectors(req),
1390 brq->cmd.resp[0], brq->stop.resp[0]);
1391
1392 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001393 if (ecc_err)
1394 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001395 return MMC_BLK_DATA_ERR;
1396 } else {
1397 return MMC_BLK_CMD_ERR;
1398 }
1399 }
1400
Adrian Hunter67716322011-08-29 16:42:15 +03001401 if (!brq->data.bytes_xfered)
1402 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001403
Adrian Hunter67716322011-08-29 16:42:15 +03001404 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1405 return MMC_BLK_PARTIAL;
1406
1407 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001408}
1409
Per Forlin54d49d72011-07-01 18:55:29 +02001410static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1411 struct mmc_card *card,
1412 int disable_multi,
1413 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Per Forlin54d49d72011-07-01 18:55:29 +02001415 u32 readcmd, writecmd;
1416 struct mmc_blk_request *brq = &mqrq->brq;
1417 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001418 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301419 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001421 /*
1422 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001423 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001424 */
Luca Porziod3df0462015-11-06 15:12:26 +00001425 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001426 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001427 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001428
Per Forlin54d49d72011-07-01 18:55:29 +02001429 memset(brq, 0, sizeof(struct mmc_blk_request));
1430 brq->mrq.cmd = &brq->cmd;
1431 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Per Forlin54d49d72011-07-01 18:55:29 +02001433 brq->cmd.arg = blk_rq_pos(req);
1434 if (!mmc_card_blockaddr(card))
1435 brq->cmd.arg <<= 9;
1436 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1437 brq->data.blksz = 512;
1438 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1439 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001440 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Per Forlin54d49d72011-07-01 18:55:29 +02001442 /*
1443 * The block layer doesn't support all sector count
1444 * restrictions, so we need to be prepared for too big
1445 * requests.
1446 */
1447 if (brq->data.blocks > card->host->max_blk_count)
1448 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001450 if (brq->data.blocks > 1) {
1451 /*
1452 * After a read error, we redo the request one sector
1453 * at a time in order to accurately determine which
1454 * sectors can be read successfully.
1455 */
1456 if (disable_multi)
1457 brq->data.blocks = 1;
1458
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001459 /*
1460 * Some controllers have HW issues while operating
1461 * in multiple I/O mode
1462 */
1463 if (card->host->ops->multi_io_quirk)
1464 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1465 (rq_data_dir(req) == READ) ?
1466 MMC_DATA_READ : MMC_DATA_WRITE,
1467 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001468 }
Per Forlin54d49d72011-07-01 18:55:29 +02001469
1470 if (brq->data.blocks > 1 || do_rel_wr) {
1471 /* SPI multiblock writes terminate using a special
1472 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001473 */
Per Forlin54d49d72011-07-01 18:55:29 +02001474 if (!mmc_host_is_spi(card->host) ||
1475 rq_data_dir(req) == READ)
1476 brq->mrq.stop = &brq->stop;
1477 readcmd = MMC_READ_MULTIPLE_BLOCK;
1478 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1479 } else {
1480 brq->mrq.stop = NULL;
1481 readcmd = MMC_READ_SINGLE_BLOCK;
1482 writecmd = MMC_WRITE_BLOCK;
1483 }
1484 if (rq_data_dir(req) == READ) {
1485 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001486 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001487 if (brq->mrq.stop)
1488 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1489 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001490 } else {
1491 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001492 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001493 if (brq->mrq.stop)
1494 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1495 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001496 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001497
Per Forlin54d49d72011-07-01 18:55:29 +02001498 if (do_rel_wr)
1499 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001500
Per Forlin54d49d72011-07-01 18:55:29 +02001501 /*
Saugata Das42659002011-12-21 13:09:17 +05301502 * Data tag is used only during writing meta data to speed
1503 * up write and any subsequent read of this meta data
1504 */
1505 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1506 (req->cmd_flags & REQ_META) &&
1507 (rq_data_dir(req) == WRITE) &&
1508 ((brq->data.blocks * brq->data.blksz) >=
1509 card->ext_csd.data_tag_unit_size);
1510
1511 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001512 * Pre-defined multi-block transfers are preferable to
1513 * open ended-ones (and necessary for reliable writes).
1514 * However, it is not sufficient to just send CMD23,
1515 * and avoid the final CMD12, as on an error condition
1516 * CMD12 (stop) needs to be sent anyway. This, coupled
1517 * with Auto-CMD23 enhancements provided by some
1518 * hosts, means that the complexity of dealing
1519 * with this is best left to the host. If CMD23 is
1520 * supported by card and host, we'll fill sbc in and let
1521 * the host deal with handling it correctly. This means
1522 * that for hosts that don't expose MMC_CAP_CMD23, no
1523 * change of behavior will be observed.
1524 *
1525 * N.B: Some MMC cards experience perf degradation.
1526 * We'll avoid using CMD23-bounded multiblock writes for
1527 * these, while retaining features like reliable writes.
1528 */
Saugata Das42659002011-12-21 13:09:17 +05301529 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1530 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1531 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001532 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1533 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301534 (do_rel_wr ? (1 << 31) : 0) |
1535 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001536 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1537 brq->mrq.sbc = &brq->sbc;
1538 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001539
Per Forlin54d49d72011-07-01 18:55:29 +02001540 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001541
Per Forlin54d49d72011-07-01 18:55:29 +02001542 brq->data.sg = mqrq->sg;
1543 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001544
Per Forlin54d49d72011-07-01 18:55:29 +02001545 /*
1546 * Adjust the sg list so it is the same size as the
1547 * request.
1548 */
1549 if (brq->data.blocks != blk_rq_sectors(req)) {
1550 int i, data_size = brq->data.blocks << 9;
1551 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001552
Per Forlin54d49d72011-07-01 18:55:29 +02001553 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1554 data_size -= sg->length;
1555 if (data_size <= 0) {
1556 sg->length += data_size;
1557 i++;
1558 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001559 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001560 }
Per Forlin54d49d72011-07-01 18:55:29 +02001561 brq->data.sg_len = i;
1562 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001563
Per Forlinee8a43a2011-07-01 18:55:33 +02001564 mqrq->mmc_active.mrq = &brq->mrq;
1565 mqrq->mmc_active.err_check = mmc_blk_err_check;
1566
Per Forlin54d49d72011-07-01 18:55:29 +02001567 mmc_queue_bounce_pre(mqrq);
1568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Adrian Hunter67716322011-08-29 16:42:15 +03001570static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1571 struct mmc_blk_request *brq, struct request *req,
1572 int ret)
1573{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001574 struct mmc_queue_req *mq_rq;
1575 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1576
Adrian Hunter67716322011-08-29 16:42:15 +03001577 /*
1578 * If this is an SD card and we're writing, we can first
1579 * mark the known good sectors as ok.
1580 *
1581 * If the card is not SD, we can still ok written sectors
1582 * as reported by the controller (which might be less than
1583 * the real number of written sectors, but never more).
1584 */
1585 if (mmc_card_sd(card)) {
1586 u32 blocks;
1587
1588 blocks = mmc_sd_num_wr_blocks(card);
1589 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301590 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001591 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001592 } else {
1593 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001594 }
1595 return ret;
1596}
1597
Per Forlinee8a43a2011-07-01 18:55:33 +02001598static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001599{
Linus Walleij7db30282016-11-18 13:36:15 +01001600 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001601 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001602 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001603 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001604 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001605 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001606 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001607 struct mmc_async_req *areq;
1608
1609 if (!rqc && !mq->mqrq_prev->req)
1610 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001611
1612 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001613 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301614 /*
1615 * When 4KB native sector is enabled, only 8 blocks
1616 * multiple read or write is allowed
1617 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001618 if (mmc_large_sector(card) &&
1619 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301620 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001621 rqc->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001622 mq_rq = mq->mqrq_cur;
Adrian Hunter5be80372016-11-29 12:09:09 +02001623 req = rqc;
1624 rqc = NULL;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301625 goto cmd_abort;
1626 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001627
Linus Walleij03d640a2016-11-25 10:35:00 +01001628 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001629 areq = &mq->mqrq_cur->mmc_active;
1630 } else
1631 areq = NULL;
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001632 areq = mmc_start_req(card->host, areq, &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001633 if (!areq) {
1634 if (status == MMC_BLK_NEW_REQUEST)
1635 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001636 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001637 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001638
Per Forlinee8a43a2011-07-01 18:55:33 +02001639 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1640 brq = &mq_rq->brq;
1641 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001642 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001643 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001644
Per Forlind78d4a8a2011-07-01 18:55:30 +02001645 switch (status) {
1646 case MMC_BLK_SUCCESS:
1647 case MMC_BLK_PARTIAL:
1648 /*
1649 * A block was successfully transferred.
1650 */
Adrian Hunter67716322011-08-29 16:42:15 +03001651 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001652
Linus Walleij03d640a2016-11-25 10:35:00 +01001653 ret = blk_end_request(req, 0,
1654 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001655
Adrian Hunter67716322011-08-29 16:42:15 +03001656 /*
1657 * If the blk_end_request function returns non-zero even
1658 * though all data has been transferred and no errors
1659 * were returned by the host controller, it's a bug.
1660 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001661 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301662 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001663 __func__, blk_rq_bytes(req),
1664 brq->data.bytes_xfered);
1665 rqc = NULL;
1666 goto cmd_abort;
1667 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001668 break;
1669 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001670 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08001671 if (mmc_blk_reset(md, card->host, type))
1672 goto cmd_abort;
1673 if (!ret)
1674 goto start_new_req;
1675 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001676 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001677 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001678 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001679 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001680 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001681 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001682 if (!mmc_blk_reset(md, card->host, type))
1683 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001684 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001685 case MMC_BLK_DATA_ERR: {
1686 int err;
1687
1688 err = mmc_blk_reset(md, card->host, type);
1689 if (!err)
1690 break;
Linus Walleij03d640a2016-11-25 10:35:00 +01001691 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001692 goto cmd_abort;
1693 /* Fall through */
1694 }
1695 case MMC_BLK_ECC_ERR:
1696 if (brq->data.blocks > 1) {
1697 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001698 pr_warn("%s: retrying using single block read\n",
1699 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001700 disable_multi = 1;
1701 break;
1702 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001703 /*
1704 * After an error, we redo I/O one sector at a
1705 * time, so we only reach here after trying to
1706 * read a single sector.
1707 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301708 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001709 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001710 if (!ret)
1711 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001712 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301713 case MMC_BLK_NOMEDIUM:
1714 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001715 default:
1716 pr_err("%s: Unhandled return value (%d)",
1717 req->rq_disk->disk_name, status);
1718 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001719 }
1720
Per Forlinee8a43a2011-07-01 18:55:33 +02001721 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001722 /*
1723 * In case of a incomplete request
1724 * prepare it again and resend.
1725 */
1726 mmc_blk_rw_rq_prep(mq_rq, card,
1727 disable_multi, mq);
1728 mmc_start_req(card->host,
1729 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001730 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 } while (ret);
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return 1;
1735
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001736 cmd_abort:
Linus Walleij03d640a2016-11-25 10:35:00 +01001737 if (mmc_card_removed(card))
Linus Torvalds36869cb2016-12-13 10:19:16 -08001738 req->rq_flags |= RQF_QUIET;
Linus Walleij03d640a2016-11-25 10:35:00 +01001739 while (ret)
1740 ret = blk_end_request(req, -EIO,
1741 blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Per Forlinee8a43a2011-07-01 18:55:33 +02001743 start_new_req:
1744 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001745 if (mmc_card_removed(card)) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001746 rqc->rq_flags |= RQF_QUIET;
Seungwon Jeon7a819022013-01-22 19:48:07 +09001747 blk_end_request_all(rqc, -EIO);
1748 } else {
1749 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1750 mmc_start_req(card->host,
1751 &mq->mqrq_cur->mmc_active, NULL);
1752 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001753 }
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
1756}
1757
Linus Walleij29eb7bd2016-09-20 11:34:38 +02001758int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001759{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001760 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001761 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001762 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001763 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001764
Per Forlinee8a43a2011-07-01 18:55:33 +02001765 if (req && !mq->mqrq_prev->req)
1766 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001767 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001768
Andrei Warkentin371a6892011-04-11 18:10:25 -05001769 ret = mmc_blk_part_switch(card, md);
1770 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001771 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301772 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001773 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001774 ret = 0;
1775 goto out;
1776 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001777
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001778 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001779 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001780 /* complete ongoing async transfer before issuing discard */
1781 if (card->host->areq)
1782 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001783 ret = mmc_blk_issue_discard_rq(mq, req);
1784 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1785 /* complete ongoing async transfer before issuing secure erase*/
1786 if (card->host->areq)
1787 mmc_blk_issue_rw_rq(mq, NULL);
1788 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001789 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001790 /* complete ongoing async transfer before issuing flush */
1791 if (card->host->areq)
1792 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001793 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001794 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001795 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001796 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001797
Andrei Warkentin371a6892011-04-11 18:10:25 -05001798out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001799 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001800 /*
1801 * Release host when there are no more requests
1802 * and after special request(discard, flush) is done.
1803 * In case sepecial request, there is no reentry to
1804 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1805 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001806 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001807 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Russell Kinga6f6c962006-01-03 22:38:44 +00001810static inline int mmc_blk_readonly(struct mmc_card *card)
1811{
1812 return mmc_card_readonly(card) ||
1813 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1814}
1815
Andrei Warkentin371a6892011-04-11 18:10:25 -05001816static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1817 struct device *parent,
1818 sector_t size,
1819 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001820 const char *subname,
1821 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 struct mmc_blk_data *md;
1824 int devidx, ret;
1825
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001826again:
1827 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
1828 return ERR_PTR(-ENOMEM);
1829
1830 spin_lock(&mmc_blk_lock);
1831 ret = ida_get_new(&mmc_blk_ida, &devidx);
1832 spin_unlock(&mmc_blk_lock);
1833
1834 if (ret == -EAGAIN)
1835 goto again;
1836 else if (ret)
1837 return ERR_PTR(ret);
1838
1839 if (devidx >= max_devices) {
1840 ret = -ENOSPC;
1841 goto out;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001844 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001845 if (!md) {
1846 ret = -ENOMEM;
1847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001849
Johan Rudholmadd710e2011-12-02 08:51:06 +01001850 md->area_type = area_type;
1851
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001852 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001853 * Set the read-only status based on the supported commands
1854 * and the write protect switch.
1855 */
1856 md->read_only = mmc_blk_readonly(card);
1857
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001858 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001859 if (md->disk == NULL) {
1860 ret = -ENOMEM;
1861 goto err_kfree;
1862 }
1863
1864 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001865 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001866 md->usage = 1;
1867
Adrian Hunterd09408a2011-06-23 13:40:28 +03001868 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001869 if (ret)
1870 goto err_putdisk;
1871
Linus Walleij7db30282016-11-18 13:36:15 +01001872 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001873
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001874 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001875 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001876 md->disk->fops = &mmc_bdops;
1877 md->disk->private_data = md;
1878 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001879 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001880 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001881 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001882 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001883 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001884
1885 /*
1886 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1887 *
1888 * - be set for removable media with permanent block devices
1889 * - be unset for removable block devices with permanent media
1890 *
1891 * Since MMC block devices clearly fall under the second
1892 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1893 * should use the block device creation/destruction hotplug
1894 * messages to tell when the card is present.
1895 */
1896
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001897 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001898 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001899
Saugata Dasa5075eb2012-05-17 16:32:21 +05301900 if (mmc_card_mmc(card))
1901 blk_queue_logical_block_size(md->queue.queue,
1902 card->ext_csd.data_sector_size);
1903 else
1904 blk_queue_logical_block_size(md->queue.queue, 512);
1905
Andrei Warkentin371a6892011-04-11 18:10:25 -05001906 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001907
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001908 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001909 if ((mmc_card_mmc(card) &&
1910 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001911 (mmc_card_sd(card) &&
1912 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1913 md->flags |= MMC_BLK_CMD23;
1914 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001915
1916 if (mmc_card_mmc(card) &&
1917 md->flags & MMC_BLK_CMD23 &&
1918 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1919 card->ext_csd.rel_sectors)) {
1920 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001921 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001922 }
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001925
1926 err_putdisk:
1927 put_disk(md->disk);
1928 err_kfree:
1929 kfree(md);
1930 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001931 spin_lock(&mmc_blk_lock);
1932 ida_remove(&mmc_blk_ida, devidx);
1933 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00001934 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Andrei Warkentin371a6892011-04-11 18:10:25 -05001937static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1938{
1939 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001940
1941 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1942 /*
1943 * The EXT_CSD sector count is in number or 512 byte
1944 * sectors.
1945 */
1946 size = card->ext_csd.sectors;
1947 } else {
1948 /*
1949 * The CSD capacity field is in units of read_blkbits.
1950 * set_capacity takes units of 512 bytes.
1951 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001952 size = (typeof(sector_t))card->csd.capacity
1953 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001954 }
1955
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001956 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001957 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001958}
1959
1960static int mmc_blk_alloc_part(struct mmc_card *card,
1961 struct mmc_blk_data *md,
1962 unsigned int part_type,
1963 sector_t size,
1964 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001965 const char *subname,
1966 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001967{
1968 char cap_str[10];
1969 struct mmc_blk_data *part_md;
1970
1971 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001972 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001973 if (IS_ERR(part_md))
1974 return PTR_ERR(part_md);
1975 part_md->part_type = part_type;
1976 list_add(&part_md->part, &md->part);
1977
James Bottomleyb9f28d82015-03-05 18:47:01 -08001978 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001979 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301980 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001981 part_md->disk->disk_name, mmc_card_id(card),
1982 mmc_card_name(card), part_md->part_type, cap_str);
1983 return 0;
1984}
1985
Namjae Jeone0c368d2011-10-06 23:41:38 +09001986/* MMC Physical partitions consist of two boot partitions and
1987 * up to four general purpose partitions.
1988 * For each partition enabled in EXT_CSD a block device will be allocatedi
1989 * to provide access to the partition.
1990 */
1991
Andrei Warkentin371a6892011-04-11 18:10:25 -05001992static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1993{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001994 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001995
1996 if (!mmc_card_mmc(card))
1997 return 0;
1998
Namjae Jeone0c368d2011-10-06 23:41:38 +09001999 for (idx = 0; idx < card->nr_parts; idx++) {
2000 if (card->part[idx].size) {
2001 ret = mmc_blk_alloc_part(card, md,
2002 card->part[idx].part_cfg,
2003 card->part[idx].size >> 9,
2004 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002005 card->part[idx].name,
2006 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002007 if (ret)
2008 return ret;
2009 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002010 }
2011
2012 return ret;
2013}
2014
Andrei Warkentin371a6892011-04-11 18:10:25 -05002015static void mmc_blk_remove_req(struct mmc_blk_data *md)
2016{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002017 struct mmc_card *card;
2018
Andrei Warkentin371a6892011-04-11 18:10:25 -05002019 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002020 /*
2021 * Flush remaining requests and free queues. It
2022 * is freeing the queue that stops new requests
2023 * from being accepted.
2024 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002025 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002026 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002027 if (md->disk->flags & GENHD_FL_UP) {
2028 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002029 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2030 card->ext_csd.boot_ro_lockable)
2031 device_remove_file(disk_to_dev(md->disk),
2032 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002033
Andrei Warkentin371a6892011-04-11 18:10:25 -05002034 del_gendisk(md->disk);
2035 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036 mmc_blk_put(md);
2037 }
2038}
2039
2040static void mmc_blk_remove_parts(struct mmc_card *card,
2041 struct mmc_blk_data *md)
2042{
2043 struct list_head *pos, *q;
2044 struct mmc_blk_data *part_md;
2045
2046 list_for_each_safe(pos, q, &md->part) {
2047 part_md = list_entry(pos, struct mmc_blk_data, part);
2048 list_del(pos);
2049 mmc_blk_remove_req(part_md);
2050 }
2051}
2052
2053static int mmc_add_disk(struct mmc_blk_data *md)
2054{
2055 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002056 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002057
Dan Williams307d8e62016-06-20 10:40:44 -07002058 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002059 md->force_ro.show = force_ro_show;
2060 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302061 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002062 md->force_ro.attr.name = "force_ro";
2063 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2064 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2065 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002066 goto force_ro_fail;
2067
2068 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2069 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002070 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002071
2072 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2073 mode = S_IRUGO;
2074 else
2075 mode = S_IRUGO | S_IWUSR;
2076
2077 md->power_ro_lock.show = power_ro_lock_show;
2078 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002079 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002080 md->power_ro_lock.attr.mode = mode;
2081 md->power_ro_lock.attr.name =
2082 "ro_lock_until_next_power_on";
2083 ret = device_create_file(disk_to_dev(md->disk),
2084 &md->power_ro_lock);
2085 if (ret)
2086 goto power_ro_lock_fail;
2087 }
2088 return ret;
2089
2090power_ro_lock_fail:
2091 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2092force_ro_fail:
2093 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002094
2095 return ret;
2096}
2097
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002098static const struct mmc_fixup blk_fixups[] =
2099{
Chris Ballc59d4472011-11-11 22:01:43 -05002100 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2101 MMC_QUIRK_INAND_CMD38),
2102 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2103 MMC_QUIRK_INAND_CMD38),
2104 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2105 MMC_QUIRK_INAND_CMD38),
2106 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2107 MMC_QUIRK_INAND_CMD38),
2108 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2109 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002110
2111 /*
2112 * Some MMC cards experience performance degradation with CMD23
2113 * instead of CMD12-bounded multiblock transfers. For now we'll
2114 * black list what's bad...
2115 * - Certain Toshiba cards.
2116 *
2117 * N.B. This doesn't affect SD cards.
2118 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002119 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2120 MMC_QUIRK_BLK_NO_CMD23),
2121 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2122 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002123 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002124 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002125 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002126 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002127 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002128 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002129
2130 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002131 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002132 */
Chris Ballc59d4472011-11-11 22:01:43 -05002133 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002134 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002135 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2136 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002137
Ian Chen3550ccd2012-08-29 15:05:36 +09002138 /*
2139 * On these Samsung MoviNAND parts, performing secure erase or
2140 * secure trim can result in unrecoverable corruption due to a
2141 * firmware bug.
2142 */
2143 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2144 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2145 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2146 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2147 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2148 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2149 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2150 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2151 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2152 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2153 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2154 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2155 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2156 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2157 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2158 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2159
Shawn Linb5b4ff02015-08-12 13:08:32 +08002160 /*
2161 * On Some Kingston eMMCs, performing trim can result in
2162 * unrecoverable data conrruption occasionally due to a firmware bug.
2163 */
2164 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2165 MMC_QUIRK_TRIM_BROKEN),
2166 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2167 MMC_QUIRK_TRIM_BROKEN),
2168
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002169 END_FIXUP
2170};
2171
Ulf Hansson96541ba2015-04-14 13:06:12 +02002172static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002174 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002175 char cap_str[10];
2176
Pierre Ossman912490d2005-05-21 10:27:02 +01002177 /*
2178 * Check that the card supports the command class(es) we need.
2179 */
2180 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -ENODEV;
2182
Lukas Czerner5204d002014-06-18 13:18:07 +02002183 mmc_fixup_device(card, blk_fixups);
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 md = mmc_blk_alloc(card);
2186 if (IS_ERR(md))
2187 return PTR_ERR(md);
2188
James Bottomleyb9f28d82015-03-05 18:47:01 -08002189 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002190 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302191 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002193 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Andrei Warkentin371a6892011-04-11 18:10:25 -05002195 if (mmc_blk_alloc_parts(card, md))
2196 goto out;
2197
Ulf Hansson96541ba2015-04-14 13:06:12 +02002198 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002199
Andrei Warkentin371a6892011-04-11 18:10:25 -05002200 if (mmc_add_disk(md))
2201 goto out;
2202
2203 list_for_each_entry(part_md, &md->part, part) {
2204 if (mmc_add_disk(part_md))
2205 goto out;
2206 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002207
2208 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2209 pm_runtime_use_autosuspend(&card->dev);
2210
2211 /*
2212 * Don't enable runtime PM for SD-combo cards here. Leave that
2213 * decision to be taken during the SDIO init sequence instead.
2214 */
2215 if (card->type != MMC_TYPE_SD_COMBO) {
2216 pm_runtime_set_active(&card->dev);
2217 pm_runtime_enable(&card->dev);
2218 }
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 0;
2221
2222 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002223 mmc_blk_remove_parts(card, md);
2224 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Ulf Hansson96541ba2015-04-14 13:06:12 +02002228static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002230 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Andrei Warkentin371a6892011-04-11 18:10:25 -05002232 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002233 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002234 mmc_claim_host(card->host);
2235 mmc_blk_part_switch(card, md);
2236 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002237 if (card->type != MMC_TYPE_SD_COMBO)
2238 pm_runtime_disable(&card->dev);
2239 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002241 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Ulf Hansson96541ba2015-04-14 13:06:12 +02002244static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002246 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002247 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 if (md) {
2250 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002251 list_for_each_entry(part_md, &md->part, part) {
2252 mmc_queue_suspend(&part_md->queue);
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 return 0;
2256}
2257
Ulf Hansson96541ba2015-04-14 13:06:12 +02002258static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002259{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002260 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002261}
2262
Ulf Hansson0967edc2014-10-06 11:29:42 +02002263#ifdef CONFIG_PM_SLEEP
2264static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002265{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002266 struct mmc_card *card = mmc_dev_to_card(dev);
2267
2268 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002269}
2270
Ulf Hansson0967edc2014-10-06 11:29:42 +02002271static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002273 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002274 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002277 /*
2278 * Resume involves the card going into idle state,
2279 * so current partition is always the main one.
2280 */
2281 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002283 list_for_each_entry(part_md, &md->part, part) {
2284 mmc_queue_resume(&part_md->queue);
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287 return 0;
2288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289#endif
2290
Ulf Hansson0967edc2014-10-06 11:29:42 +02002291static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2292
Ulf Hansson96541ba2015-04-14 13:06:12 +02002293static struct mmc_driver mmc_driver = {
2294 .drv = {
2295 .name = "mmcblk",
2296 .pm = &mmc_blk_pm_ops,
2297 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 .probe = mmc_blk_probe,
2299 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002300 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301};
2302
2303static int __init mmc_blk_init(void)
2304{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002305 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002307 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2308 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2309
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002310 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002311
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002312 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2313 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002316 res = mmc_register_driver(&mmc_driver);
2317 if (res)
2318 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002320 return 0;
2321 out2:
2322 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 out:
2324 return res;
2325}
2326
2327static void __exit mmc_blk_exit(void)
2328{
2329 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002330 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
2333module_init(mmc_blk_init);
2334module_exit(mmc_blk_exit);
2335
2336MODULE_LICENSE("GPL");
2337MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2338