blob: a8300beae918664bf681b47310d385a8bca53f6f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010050#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010051#include "card.h"
52#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010053#include "mmc_ops.h"
54#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000056MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040057#ifdef MODULE_PARAM_PREFIX
58#undef MODULE_PARAM_PREFIX
59#endif
60#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010061
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050062#define INAND_CMD38_ARG_EXT_CSD 113
63#define INAND_CMD38_ARG_ERASE 0x00
64#define INAND_CMD38_ARG_TRIM 0x01
65#define INAND_CMD38_ARG_SECERASE 0x80
66#define INAND_CMD38_ARG_SECTRIM1 0x81
67#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060068#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030069#define MMC_SANITIZE_REQ_TIMEOUT 240000
70#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050071
Luca Porziod3df0462015-11-06 15:12:26 +000072#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090073 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000084 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020085 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040086 */
87static int max_devices;
88
Ben Hutchingsa26eba62014-11-06 03:35:09 +000089#define MAX_DEVICES 256
90
Ulf Hanssonb10fa992016-04-07 14:36:46 +020091static DEFINE_IDA(mmc_blk_ida);
92static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * There is one mmc_blk_data per slot.
96 */
97struct mmc_blk_data {
98 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070099 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct gendisk *disk;
101 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500104 unsigned int flags;
105#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
106#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000109 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500110 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300111 unsigned int reset_done;
112#define MMC_BLK_READ BIT(0)
113#define MMC_BLK_WRITE BIT(1)
114#define MMC_BLK_DISCARD BIT(2)
115#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116
117 /*
118 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200119 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500120 * track of the current selected device partition.
121 */
122 unsigned int part_curr;
123 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 struct device_attribute power_ro_lock;
125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400130module_param(perdev_minors, int, 0444);
131MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
132
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200133static inline int mmc_blk_part_switch(struct mmc_card *card,
134 struct mmc_blk_data *md);
135static int get_card_status(struct mmc_card *card, u32 *status, int retries);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
138{
139 struct mmc_blk_data *md;
140
Arjan van de Vena621aae2006-01-12 18:43:35 +0000141 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 md = disk->private_data;
143 if (md && md->usage == 0)
144 md = NULL;
145 if (md)
146 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000147 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return md;
150}
151
Andrei Warkentin371a6892011-04-11 18:10:25 -0500152static inline int mmc_get_devidx(struct gendisk *disk)
153{
Colin Cross382c55f2015-10-22 10:00:41 -0700154 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500155 return devidx;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void mmc_blk_put(struct mmc_blk_data *md)
159{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000160 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 md->usage--;
162 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500163 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800164 blk_cleanup_queue(md->queue.queue);
165
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200166 spin_lock(&mmc_blk_lock);
167 ida_remove(&mmc_blk_ida, devidx);
168 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(md);
172 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000173 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Johan Rudholmadd710e2011-12-02 08:51:06 +0100176static ssize_t power_ro_lock_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
179 int ret;
180 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
181 struct mmc_card *card = md->queue.card;
182 int locked = 0;
183
184 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
185 locked = 2;
186 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
187 locked = 1;
188
189 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
190
Tomas Winkler9098f842015-07-16 15:50:45 +0200191 mmc_blk_put(md);
192
Johan Rudholmadd710e2011-12-02 08:51:06 +0100193 return ret;
194}
195
196static ssize_t power_ro_lock_store(struct device *dev,
197 struct device_attribute *attr, const char *buf, size_t count)
198{
199 int ret;
200 struct mmc_blk_data *md, *part_md;
201 struct mmc_card *card;
202 unsigned long set;
203
204 if (kstrtoul(buf, 0, &set))
205 return -EINVAL;
206
207 if (set != 1)
208 return count;
209
210 md = mmc_blk_get(dev_to_disk(dev));
211 card = md->queue.card;
212
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200213 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100214
215 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
216 card->ext_csd.boot_ro_lock |
217 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
218 card->ext_csd.part_time);
219 if (ret)
220 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
221 else
222 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
223
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200224 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225
226 if (!ret) {
227 pr_info("%s: Locking boot partition ro until next power on\n",
228 md->disk->disk_name);
229 set_disk_ro(md->disk, 1);
230
231 list_for_each_entry(part_md, &md->part, part)
232 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
233 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
234 set_disk_ro(part_md->disk, 1);
235 }
236 }
237
238 mmc_blk_put(md);
239 return count;
240}
241
Andrei Warkentin371a6892011-04-11 18:10:25 -0500242static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
243 char *buf)
244{
245 int ret;
246 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247
Baruch Siach0031a982014-09-22 10:12:51 +0300248 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500249 get_disk_ro(dev_to_disk(dev)) ^
250 md->read_only);
251 mmc_blk_put(md);
252 return ret;
253}
254
255static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
256 const char *buf, size_t count)
257{
258 int ret;
259 char *end;
260 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
261 unsigned long set = simple_strtoul(buf, &end, 0);
262 if (end == buf) {
263 ret = -EINVAL;
264 goto out;
265 }
266
267 set_disk_ro(dev_to_disk(dev), set || md->read_only);
268 ret = count;
269out:
270 mmc_blk_put(md);
271 return ret;
272}
273
Al Viroa5a15612008-03-02 10:33:30 -0500274static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Al Viroa5a15612008-03-02 10:33:30 -0500276 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int ret = -ENXIO;
278
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200279 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (md) {
281 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500282 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700284
Al Viroa5a15612008-03-02 10:33:30 -0500285 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700286 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700287 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200290 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return ret;
293}
294
Al Virodb2a1442013-05-05 21:52:57 -0400295static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Al Viroa5a15612008-03-02 10:33:30 -0500297 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200299 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200301 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800305mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800307 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
308 geo->heads = 4;
309 geo->sectors = 16;
310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
John Calixtocb87ea22011-04-26 18:56:29 -0400313struct mmc_blk_ioc_data {
314 struct mmc_ioc_cmd ic;
315 unsigned char *buf;
316 u64 buf_bytes;
317};
318
319static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
320 struct mmc_ioc_cmd __user *user)
321{
322 struct mmc_blk_ioc_data *idata;
323 int err;
324
yalin wang1ff89502015-11-12 19:27:11 +0800325 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400326 if (!idata) {
327 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400328 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400329 }
330
331 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
332 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400333 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400334 }
335
336 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
337 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
338 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400339 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400340 }
341
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300342 if (!idata->buf_bytes) {
343 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100344 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300345 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100346
yalin wang1ff89502015-11-12 19:27:11 +0800347 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400348 if (!idata->buf) {
349 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400350 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400351 }
352
353 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
354 idata->ic.data_ptr, idata->buf_bytes)) {
355 err = -EFAULT;
356 goto copy_err;
357 }
358
359 return idata;
360
361copy_err:
362 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400364 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400365out:
John Calixtocb87ea22011-04-26 18:56:29 -0400366 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400367}
368
Jon Huntera5f57742015-09-22 10:27:53 +0100369static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
370 struct mmc_blk_ioc_data *idata)
371{
372 struct mmc_ioc_cmd *ic = &idata->ic;
373
374 if (copy_to_user(&(ic_ptr->response), ic->response,
375 sizeof(ic->response)))
376 return -EFAULT;
377
378 if (!idata->ic.write_flag) {
379 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
380 idata->buf, idata->buf_bytes))
381 return -EFAULT;
382 }
383
384 return 0;
385}
386
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200387static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
388 u32 retries_max)
389{
390 int err;
391 u32 retry_count = 0;
392
393 if (!status || !retries_max)
394 return -EINVAL;
395
396 do {
397 err = get_card_status(card, status, 5);
398 if (err)
399 break;
400
401 if (!R1_STATUS(*status) &&
402 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
403 break; /* RPMB programming operation complete */
404
405 /*
406 * Rechedule to give the MMC device a chance to continue
407 * processing the previous command without being polled too
408 * frequently.
409 */
410 usleep_range(1000, 5000);
411 } while (++retry_count < retries_max);
412
413 if (retry_count == retries_max)
414 err = -EPERM;
415
416 return err;
417}
418
Maya Erez775a9362013-04-18 15:41:55 +0300419static int ioctl_do_sanitize(struct mmc_card *card)
420{
421 int err;
422
Ulf Hanssona2d10862013-12-16 14:37:26 +0100423 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300424 pr_warn("%s: %s - SANITIZE is not supported\n",
425 mmc_hostname(card->host), __func__);
426 err = -EOPNOTSUPP;
427 goto out;
428 }
429
430 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
431 mmc_hostname(card->host), __func__);
432
433 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
434 EXT_CSD_SANITIZE_START, 1,
435 MMC_SANITIZE_REQ_TIMEOUT);
436
437 if (err)
438 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
439 mmc_hostname(card->host), __func__, err);
440
441 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
442 __func__);
443out:
444 return err;
445}
446
Jon Huntera5f57742015-09-22 10:27:53 +0100447static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
448 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400449{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900450 struct mmc_command cmd = {};
451 struct mmc_data data = {};
452 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400453 struct scatterlist sg;
454 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200455 int is_rpmb = false;
456 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400457
Jon Huntera5f57742015-09-22 10:27:53 +0100458 if (!card || !md || !idata)
459 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400460
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200461 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
462 is_rpmb = true;
463
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100464 cmd.opcode = idata->ic.opcode;
465 cmd.arg = idata->ic.arg;
466 cmd.flags = idata->ic.flags;
467
468 if (idata->buf_bytes) {
469 data.sg = &sg;
470 data.sg_len = 1;
471 data.blksz = idata->ic.blksz;
472 data.blocks = idata->ic.blocks;
473
474 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
475
476 if (idata->ic.write_flag)
477 data.flags = MMC_DATA_WRITE;
478 else
479 data.flags = MMC_DATA_READ;
480
481 /* data.flags must already be set before doing this. */
482 mmc_set_data_timeout(&data, card);
483
484 /* Allow overriding the timeout_ns for empirical tuning. */
485 if (idata->ic.data_timeout_ns)
486 data.timeout_ns = idata->ic.data_timeout_ns;
487
488 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
489 /*
490 * Pretend this is a data transfer and rely on the
491 * host driver to compute timeout. When all host
492 * drivers support cmd.cmd_timeout for R1B, this
493 * can be changed to:
494 *
495 * mrq.data = NULL;
496 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
497 */
498 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
499 }
500
501 mrq.data = &data;
502 }
503
504 mrq.cmd = &cmd;
505
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200506 err = mmc_blk_part_switch(card, md);
507 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100508 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200509
John Calixtocb87ea22011-04-26 18:56:29 -0400510 if (idata->ic.is_acmd) {
511 err = mmc_app_cmd(card->host, card);
512 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100513 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400514 }
515
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200516 if (is_rpmb) {
517 err = mmc_set_blockcount(card, data.blocks,
518 idata->ic.write_flag & (1 << 31));
519 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100520 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200521 }
522
Yaniv Gardia82e4842013-06-05 14:13:08 +0300523 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
524 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300525 err = ioctl_do_sanitize(card);
526
527 if (err)
528 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
529 __func__, err);
530
Jon Huntera5f57742015-09-22 10:27:53 +0100531 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300532 }
533
John Calixtocb87ea22011-04-26 18:56:29 -0400534 mmc_wait_for_req(card->host, &mrq);
535
536 if (cmd.error) {
537 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
538 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100539 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400540 }
541 if (data.error) {
542 dev_err(mmc_dev(card->host), "%s: data error %d\n",
543 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100544 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400545 }
546
547 /*
548 * According to the SD specs, some commands require a delay after
549 * issuing the command.
550 */
551 if (idata->ic.postsleep_min_us)
552 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
553
Jon Huntera5f57742015-09-22 10:27:53 +0100554 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400555
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200556 if (is_rpmb) {
557 /*
558 * Ensure RPMB command has completed by polling CMD13
559 * "Send Status".
560 */
561 err = ioctl_rpmb_card_status_poll(card, &status, 5);
562 if (err)
563 dev_err(mmc_dev(card->host),
564 "%s: Card Status=0x%08X, error %d\n",
565 __func__, status, err);
566 }
567
Jon Huntera5f57742015-09-22 10:27:53 +0100568 return err;
569}
570
571static int mmc_blk_ioctl_cmd(struct block_device *bdev,
572 struct mmc_ioc_cmd __user *ic_ptr)
573{
574 struct mmc_blk_ioc_data *idata;
575 struct mmc_blk_data *md;
576 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700577 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100578
Shawn Lin83c742c2016-03-16 18:15:47 +0800579 /*
580 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
581 * whole block device, not on a partition. This prevents overspray
582 * between sibling partitions.
583 */
584 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
585 return -EPERM;
586
Jon Huntera5f57742015-09-22 10:27:53 +0100587 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
588 if (IS_ERR(idata))
589 return PTR_ERR(idata);
590
591 md = mmc_blk_get(bdev->bd_disk);
592 if (!md) {
593 err = -EINVAL;
594 goto cmd_err;
595 }
596
597 card = md->queue.card;
598 if (IS_ERR(card)) {
599 err = PTR_ERR(card);
600 goto cmd_done;
601 }
602
603 mmc_get_card(card);
604
Grant Grundlerb0934102015-09-23 18:30:33 -0700605 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100606
Adrian Hunter3c866562016-05-04 14:38:12 +0300607 /* Always switch back to main area after RPMB access */
608 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
609 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
610
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200611 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400612
Grant Grundlerb0934102015-09-23 18:30:33 -0700613 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100614
John Calixtocb87ea22011-04-26 18:56:29 -0400615cmd_done:
616 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300617cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400618 kfree(idata->buf);
619 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700620 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400621}
622
Jon Huntera5f57742015-09-22 10:27:53 +0100623static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
624 struct mmc_ioc_multi_cmd __user *user)
625{
626 struct mmc_blk_ioc_data **idata = NULL;
627 struct mmc_ioc_cmd __user *cmds = user->cmds;
628 struct mmc_card *card;
629 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700630 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100631 __u64 num_of_cmds;
632
Shawn Lin83c742c2016-03-16 18:15:47 +0800633 /*
634 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
635 * whole block device, not on a partition. This prevents overspray
636 * between sibling partitions.
637 */
638 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
639 return -EPERM;
640
Jon Huntera5f57742015-09-22 10:27:53 +0100641 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
642 sizeof(num_of_cmds)))
643 return -EFAULT;
644
645 if (num_of_cmds > MMC_IOC_MAX_CMDS)
646 return -EINVAL;
647
648 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
649 if (!idata)
650 return -ENOMEM;
651
652 for (i = 0; i < num_of_cmds; i++) {
653 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
654 if (IS_ERR(idata[i])) {
655 err = PTR_ERR(idata[i]);
656 num_of_cmds = i;
657 goto cmd_err;
658 }
659 }
660
661 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800662 if (!md) {
663 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100664 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800665 }
Jon Huntera5f57742015-09-22 10:27:53 +0100666
667 card = md->queue.card;
668 if (IS_ERR(card)) {
669 err = PTR_ERR(card);
670 goto cmd_done;
671 }
672
673 mmc_get_card(card);
674
Grant Grundlerb0934102015-09-23 18:30:33 -0700675 for (i = 0; i < num_of_cmds && !ioc_err; i++)
676 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100677
Adrian Hunter3c866562016-05-04 14:38:12 +0300678 /* Always switch back to main area after RPMB access */
679 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
680 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
681
Jon Huntera5f57742015-09-22 10:27:53 +0100682 mmc_put_card(card);
683
684 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700685 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100686 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100687
688cmd_done:
689 mmc_blk_put(md);
690cmd_err:
691 for (i = 0; i < num_of_cmds; i++) {
692 kfree(idata[i]->buf);
693 kfree(idata[i]);
694 }
695 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700696 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100697}
698
John Calixtocb87ea22011-04-26 18:56:29 -0400699static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
700 unsigned int cmd, unsigned long arg)
701{
Jon Huntera5f57742015-09-22 10:27:53 +0100702 switch (cmd) {
703 case MMC_IOC_CMD:
704 return mmc_blk_ioctl_cmd(bdev,
705 (struct mmc_ioc_cmd __user *)arg);
706 case MMC_IOC_MULTI_CMD:
707 return mmc_blk_ioctl_multi_cmd(bdev,
708 (struct mmc_ioc_multi_cmd __user *)arg);
709 default:
710 return -EINVAL;
711 }
John Calixtocb87ea22011-04-26 18:56:29 -0400712}
713
714#ifdef CONFIG_COMPAT
715static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
716 unsigned int cmd, unsigned long arg)
717{
718 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
719}
720#endif
721
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700722static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500723 .open = mmc_blk_open,
724 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800725 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400727 .ioctl = mmc_blk_ioctl,
728#ifdef CONFIG_COMPAT
729 .compat_ioctl = mmc_blk_compat_ioctl,
730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
Andrei Warkentin371a6892011-04-11 18:10:25 -0500733static inline int mmc_blk_part_switch(struct mmc_card *card,
734 struct mmc_blk_data *md)
735{
736 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200737 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300738
Andrei Warkentin371a6892011-04-11 18:10:25 -0500739 if (main_md->part_curr == md->part_type)
740 return 0;
741
742 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300743 u8 part_config = card->ext_csd.part_config;
744
Adrian Hunter57da0c02016-05-04 14:38:13 +0300745 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
746 mmc_retune_pause(card->host);
747
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300748 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
749 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500750
751 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300752 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500753 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300754 if (ret) {
755 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
756 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500757 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300758 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300759
760 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300761
762 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
763 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300764 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500765
766 main_md->part_curr = md->part_type;
767 return 0;
768}
769
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700770static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
771{
772 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100773 u32 result;
774 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700775
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900776 struct mmc_request mrq = {};
777 struct mmc_command cmd = {};
778 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700779
780 struct scatterlist sg;
781
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700782 cmd.opcode = MMC_APP_CMD;
783 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700784 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700785
786 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700787 if (err)
788 return (u32)-1;
789 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700790 return (u32)-1;
791
792 memset(&cmd, 0, sizeof(struct mmc_command));
793
794 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
795 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700796 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700797
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700798 data.blksz = 4;
799 data.blocks = 1;
800 data.flags = MMC_DATA_READ;
801 data.sg = &sg;
802 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530803 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700804
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700805 mrq.cmd = &cmd;
806 mrq.data = &data;
807
Ben Dooks051913d2009-06-08 23:33:57 +0100808 blocks = kmalloc(4, GFP_KERNEL);
809 if (!blocks)
810 return (u32)-1;
811
812 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813
814 mmc_wait_for_req(card->host, &mrq);
815
Ben Dooks051913d2009-06-08 23:33:57 +0100816 result = ntohl(*blocks);
817 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700818
Ben Dooks051913d2009-06-08 23:33:57 +0100819 if (cmd.error || data.error)
820 result = (u32)-1;
821
822 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700823}
824
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100825static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300826{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900827 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300828 int err;
829
Adrian Hunter504f1912008-10-16 12:55:25 +0300830 cmd.opcode = MMC_SEND_STATUS;
831 if (!mmc_host_is_spi(card->host))
832 cmd.arg = card->rca << 16;
833 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100834 err = mmc_wait_for_cmd(card->host, &cmd, retries);
835 if (err == 0)
836 *status = cmd.resp[0];
837 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300838}
839
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100840static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100841 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100842{
843 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
844 int err = 0;
845 u32 status;
846
847 do {
848 err = get_card_status(card, &status, 5);
849 if (err) {
850 pr_err("%s: error %d requesting status\n",
851 req->rq_disk->disk_name, err);
852 return err;
853 }
854
855 if (status & R1_ERROR) {
856 pr_err("%s: %s: error sending status cmd, status %#x\n",
857 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100858 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100859 }
860
Ulf Hansson95a91292014-01-29 13:11:27 +0100861 /* We may rely on the host hw to handle busy detection.*/
862 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
863 hw_busy_detect)
864 break;
865
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100866 /*
867 * Timeout if the device never becomes ready for data and never
868 * leaves the program state.
869 */
870 if (time_after(jiffies, timeout)) {
871 pr_err("%s: Card stuck in programming state! %s %s\n",
872 mmc_hostname(card->host),
873 req->rq_disk->disk_name, __func__);
874 return -ETIMEDOUT;
875 }
876
877 /*
878 * Some cards mishandle the status bits,
879 * so make sure to check both the busy
880 * indication and the card state.
881 */
882 } while (!(status & R1_READY_FOR_DATA) ||
883 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
884
885 return err;
886}
887
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100888static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100889 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100890{
891 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900892 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100893 int err;
894 bool use_r1b_resp = rq_data_dir(req) == WRITE;
895
896 /*
897 * Normally we use R1B responses for WRITE, but in cases where the host
898 * has specified a max_busy_timeout we need to validate it. A failure
899 * means we need to prevent the host from doing hw busy detection, which
900 * is done by converting to a R1 response instead.
901 */
902 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
903 use_r1b_resp = false;
904
905 cmd.opcode = MMC_STOP_TRANSMISSION;
906 if (use_r1b_resp) {
907 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
908 cmd.busy_timeout = timeout_ms;
909 } else {
910 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
911 }
912
913 err = mmc_wait_for_cmd(host, &cmd, 5);
914 if (err)
915 return err;
916
917 *stop_status = cmd.resp[0];
918
919 /* No need to check card status in case of READ. */
920 if (rq_data_dir(req) == READ)
921 return 0;
922
923 if (!mmc_host_is_spi(host) &&
924 (*stop_status & R1_ERROR)) {
925 pr_err("%s: %s: general error sending stop command, resp %#x\n",
926 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100927 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100928 }
929
930 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
931}
932
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530933#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100934#define ERR_RETRY 2
935#define ERR_ABORT 1
936#define ERR_CONTINUE 0
937
938static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
939 bool status_valid, u32 status)
940{
941 switch (error) {
942 case -EILSEQ:
943 /* response crc error, retry the r/w cmd */
944 pr_err("%s: %s sending %s command, card status %#x\n",
945 req->rq_disk->disk_name, "response CRC error",
946 name, status);
947 return ERR_RETRY;
948
949 case -ETIMEDOUT:
950 pr_err("%s: %s sending %s command, card status %#x\n",
951 req->rq_disk->disk_name, "timed out", name, status);
952
953 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530954 if (!status_valid) {
955 pr_err("%s: status not valid, retrying timeout\n",
956 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100957 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530958 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100959
960 /*
961 * If it was a r/w cmd crc error, or illegal command
962 * (eg, issued in wrong state) then retry - we should
963 * have corrected the state problem above.
964 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530965 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
966 pr_err("%s: command error, retrying timeout\n",
967 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100968 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530969 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100970
971 /* Otherwise abort the command */
972 return ERR_ABORT;
973
974 default:
975 /* We don't understand the error code the driver gave us */
976 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
977 req->rq_disk->disk_name, error, status);
978 return ERR_ABORT;
979 }
980}
981
982/*
983 * Initial r/w and stop cmd error recovery.
984 * We don't know whether the card received the r/w cmd or not, so try to
985 * restore things back to a sane state. Essentially, we do this as follows:
986 * - Obtain card status. If the first attempt to obtain card status fails,
987 * the status word will reflect the failed status cmd, not the failed
988 * r/w cmd. If we fail to obtain card status, it suggests we can no
989 * longer communicate with the card.
990 * - Check the card state. If the card received the cmd but there was a
991 * transient problem with the response, it might still be in a data transfer
992 * mode. Try to send it a stop command. If this fails, we can't recover.
993 * - If the r/w cmd failed due to a response CRC error, it was probably
994 * transient, so retry the cmd.
995 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
996 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
997 * illegal cmd, retry.
998 * Otherwise we don't understand what happened, so abort.
999 */
1000static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001001 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001002{
1003 bool prev_cmd_status_valid = true;
1004 u32 status, stop_status = 0;
1005 int err, retry;
1006
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301007 if (mmc_card_removed(card))
1008 return ERR_NOMEDIUM;
1009
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001010 /*
1011 * Try to get card status which indicates both the card state
1012 * and why there was no response. If the first attempt fails,
1013 * we can't be sure the returned status is for the r/w command.
1014 */
1015 for (retry = 2; retry >= 0; retry--) {
1016 err = get_card_status(card, &status, 0);
1017 if (!err)
1018 break;
1019
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001020 /* Re-tune if needed */
1021 mmc_retune_recheck(card->host);
1022
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001023 prev_cmd_status_valid = false;
1024 pr_err("%s: error %d sending status command, %sing\n",
1025 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1026 }
1027
1028 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301029 if (err) {
1030 /* Check if the card is removed */
1031 if (mmc_detect_card_removed(card->host))
1032 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001033 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301034 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001035
Adrian Hunter67716322011-08-29 16:42:15 +03001036 /* Flag ECC errors */
1037 if ((status & R1_CARD_ECC_FAILED) ||
1038 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1039 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001040 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001041
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001042 /* Flag General errors */
1043 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1044 if ((status & R1_ERROR) ||
1045 (brq->stop.resp[0] & R1_ERROR)) {
1046 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1047 req->rq_disk->disk_name, __func__,
1048 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001049 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001050 }
1051
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001052 /*
1053 * Check the current card state. If it is in some data transfer
1054 * mode, tell it to stop (and hopefully transition back to TRAN.)
1055 */
1056 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1057 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001058 err = send_stop(card,
1059 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1060 req, gen_err, &stop_status);
1061 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001062 pr_err("%s: error %d sending stop command\n",
1063 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001064 /*
1065 * If the stop cmd also timed out, the card is probably
1066 * not present, so abort. Other errors are bad news too.
1067 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001068 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001069 }
1070
Adrian Hunter67716322011-08-29 16:42:15 +03001071 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001072 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001073 }
1074
1075 /* Check for set block count errors */
1076 if (brq->sbc.error)
1077 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1078 prev_cmd_status_valid, status);
1079
1080 /* Check for r/w command errors */
1081 if (brq->cmd.error)
1082 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1083 prev_cmd_status_valid, status);
1084
Adrian Hunter67716322011-08-29 16:42:15 +03001085 /* Data errors */
1086 if (!brq->stop.error)
1087 return ERR_CONTINUE;
1088
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001089 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001090 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 +01001091 req->rq_disk->disk_name, brq->stop.error,
1092 brq->cmd.resp[0], status);
1093
1094 /*
1095 * Subsitute in our own stop status as this will give the error
1096 * state which happened during the execution of the r/w command.
1097 */
1098 if (stop_status) {
1099 brq->stop.resp[0] = stop_status;
1100 brq->stop.error = 0;
1101 }
1102 return ERR_CONTINUE;
1103}
1104
Adrian Hunter67716322011-08-29 16:42:15 +03001105static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1106 int type)
1107{
1108 int err;
1109
1110 if (md->reset_done & type)
1111 return -EEXIST;
1112
1113 md->reset_done |= type;
1114 err = mmc_hw_reset(host);
1115 /* Ensure we switch back to the correct partition */
1116 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001117 struct mmc_blk_data *main_md =
1118 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001119 int part_err;
1120
1121 main_md->part_curr = main_md->part_type;
1122 part_err = mmc_blk_part_switch(host->card, md);
1123 if (part_err) {
1124 /*
1125 * We have failed to get back into the correct
1126 * partition, so we need to abort the whole request.
1127 */
1128 return -ENODEV;
1129 }
1130 }
1131 return err;
1132}
1133
1134static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1135{
1136 md->reset_done &= ~type;
1137}
1138
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001139int mmc_access_rpmb(struct mmc_queue *mq)
1140{
Linus Walleij7db30282016-11-18 13:36:15 +01001141 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001142 /*
1143 * If this is a RPMB partition access, return ture
1144 */
1145 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1146 return true;
1147
1148 return false;
1149}
1150
Adrian Hunterbd788c92010-08-11 14:17:47 -07001151static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1152{
Linus Walleij7db30282016-11-18 13:36:15 +01001153 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001154 struct mmc_card *card = md->queue.card;
1155 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001156 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001157
Adrian Hunterbd788c92010-08-11 14:17:47 -07001158 if (!mmc_can_erase(card)) {
1159 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001160 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001161 }
1162
1163 from = blk_rq_pos(req);
1164 nr = blk_rq_sectors(req);
1165
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001166 if (mmc_can_discard(card))
1167 arg = MMC_DISCARD_ARG;
1168 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001169 arg = MMC_TRIM_ARG;
1170 else
1171 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001172 do {
1173 err = 0;
1174 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1175 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1176 INAND_CMD38_ARG_EXT_CSD,
1177 arg == MMC_TRIM_ARG ?
1178 INAND_CMD38_ARG_TRIM :
1179 INAND_CMD38_ARG_ERASE,
1180 0);
1181 }
1182 if (!err)
1183 err = mmc_erase(card, from, nr, arg);
1184 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001185 if (!err)
1186 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001187fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301188 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001189
Adrian Hunterbd788c92010-08-11 14:17:47 -07001190 return err ? 0 : 1;
1191}
1192
Adrian Hunter49804542010-08-11 14:17:50 -07001193static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1194 struct request *req)
1195{
Linus Walleij7db30282016-11-18 13:36:15 +01001196 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001197 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001198 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001199 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001200
Maya Erez775a9362013-04-18 15:41:55 +03001201 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001202 err = -EOPNOTSUPP;
1203 goto out;
1204 }
1205
1206 from = blk_rq_pos(req);
1207 nr = blk_rq_sectors(req);
1208
Maya Erez775a9362013-04-18 15:41:55 +03001209 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1210 arg = MMC_SECURE_TRIM1_ARG;
1211 else
1212 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001213
Adrian Hunter67716322011-08-29 16:42:15 +03001214retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001215 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1216 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1217 INAND_CMD38_ARG_EXT_CSD,
1218 arg == MMC_SECURE_TRIM1_ARG ?
1219 INAND_CMD38_ARG_SECTRIM1 :
1220 INAND_CMD38_ARG_SECERASE,
1221 0);
1222 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001223 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001224 }
Adrian Hunter28302812012-04-05 14:45:48 +03001225
Adrian Hunter49804542010-08-11 14:17:50 -07001226 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001227 if (err == -EIO)
1228 goto out_retry;
1229 if (err)
1230 goto out;
1231
1232 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001233 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1234 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1235 INAND_CMD38_ARG_EXT_CSD,
1236 INAND_CMD38_ARG_SECTRIM2,
1237 0);
1238 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001239 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001240 }
Adrian Hunter28302812012-04-05 14:45:48 +03001241
Adrian Hunter49804542010-08-11 14:17:50 -07001242 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001243 if (err == -EIO)
1244 goto out_retry;
1245 if (err)
1246 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001247 }
Adrian Hunter28302812012-04-05 14:45:48 +03001248
Adrian Hunter28302812012-04-05 14:45:48 +03001249out_retry:
1250 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001251 goto retry;
1252 if (!err)
1253 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001254out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301255 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001256
Adrian Hunter49804542010-08-11 14:17:50 -07001257 return err ? 0 : 1;
1258}
1259
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001260static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1261{
Linus Walleij7db30282016-11-18 13:36:15 +01001262 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001263 struct mmc_card *card = md->queue.card;
1264 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001265
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001266 ret = mmc_flush_cache(card);
1267 if (ret)
1268 ret = -EIO;
1269
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301270 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001271
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001272 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001273}
1274
1275/*
1276 * Reformat current write as a reliable write, supporting
1277 * both legacy and the enhanced reliable write MMC cards.
1278 * In each transfer we'll handle only as much as a single
1279 * reliable write can handle, thus finish the request in
1280 * partial completions.
1281 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001282static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1283 struct mmc_card *card,
1284 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001285{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001286 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1287 /* Legacy mode imposes restrictions on transfers. */
1288 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1289 brq->data.blocks = 1;
1290
1291 if (brq->data.blocks > card->ext_csd.rel_sectors)
1292 brq->data.blocks = card->ext_csd.rel_sectors;
1293 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1294 brq->data.blocks = 1;
1295 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001296}
1297
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001298#define CMD_ERRORS \
1299 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1300 R1_ADDRESS_ERROR | /* Misaligned address */ \
1301 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1302 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1303 R1_CC_ERROR | /* Card controller error */ \
1304 R1_ERROR) /* General/unknown error */
1305
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001306static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1307 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001308{
Per Forlinee8a43a2011-07-01 18:55:33 +02001309 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1310 mmc_active);
1311 struct mmc_blk_request *brq = &mq_mrq->brq;
1312 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001313 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001314 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001315 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001316
1317 /*
1318 * sbc.error indicates a problem with the set block count
1319 * command. No data will have been transferred.
1320 *
1321 * cmd.error indicates a problem with the r/w command. No
1322 * data will have been transferred.
1323 *
1324 * stop.error indicates a problem with the stop command. Data
1325 * may have been transferred, or may still be transferring.
1326 */
Adrian Hunter67716322011-08-29 16:42:15 +03001327 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1328 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001329 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001330 case ERR_RETRY:
1331 return MMC_BLK_RETRY;
1332 case ERR_ABORT:
1333 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301334 case ERR_NOMEDIUM:
1335 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001336 case ERR_CONTINUE:
1337 break;
1338 }
1339 }
1340
1341 /*
1342 * Check for errors relating to the execution of the
1343 * initial command - such as address errors. No data
1344 * has been transferred.
1345 */
1346 if (brq->cmd.resp[0] & CMD_ERRORS) {
1347 pr_err("%s: r/w command failed, status = %#x\n",
1348 req->rq_disk->disk_name, brq->cmd.resp[0]);
1349 return MMC_BLK_ABORT;
1350 }
1351
1352 /*
1353 * Everything else is either success, or a data error of some
1354 * kind. If it was a write, we may have transitioned to
1355 * program mode, which we have to wait for it to complete.
1356 */
1357 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001358 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001359
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001360 /* Check stop command response */
1361 if (brq->stop.resp[0] & R1_ERROR) {
1362 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1363 req->rq_disk->disk_name, __func__,
1364 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001365 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001366 }
1367
Ulf Hansson95a91292014-01-29 13:11:27 +01001368 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1369 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001370 if (err)
1371 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001372 }
1373
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001374 /* if general error occurs, retry the write operation. */
1375 if (gen_err) {
1376 pr_warn("%s: retrying write for general error\n",
1377 req->rq_disk->disk_name);
1378 return MMC_BLK_RETRY;
1379 }
1380
Per Forlind78d4a8a2011-07-01 18:55:30 +02001381 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001382 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001383 pr_debug("%s: retrying because a re-tune was needed\n",
1384 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001385 brq->retune_retry_done = 1;
1386 return MMC_BLK_RETRY;
1387 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001388 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1389 req->rq_disk->disk_name, brq->data.error,
1390 (unsigned)blk_rq_pos(req),
1391 (unsigned)blk_rq_sectors(req),
1392 brq->cmd.resp[0], brq->stop.resp[0]);
1393
1394 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001395 if (ecc_err)
1396 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001397 return MMC_BLK_DATA_ERR;
1398 } else {
1399 return MMC_BLK_CMD_ERR;
1400 }
1401 }
1402
Adrian Hunter67716322011-08-29 16:42:15 +03001403 if (!brq->data.bytes_xfered)
1404 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001405
Adrian Hunter67716322011-08-29 16:42:15 +03001406 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1407 return MMC_BLK_PARTIAL;
1408
1409 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001410}
1411
Per Forlin54d49d72011-07-01 18:55:29 +02001412static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1413 struct mmc_card *card,
1414 int disable_multi,
1415 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Per Forlin54d49d72011-07-01 18:55:29 +02001417 u32 readcmd, writecmd;
1418 struct mmc_blk_request *brq = &mqrq->brq;
1419 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001420 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301421 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001423 /*
1424 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001425 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001426 */
Luca Porziod3df0462015-11-06 15:12:26 +00001427 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001428 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001429 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001430
Per Forlin54d49d72011-07-01 18:55:29 +02001431 memset(brq, 0, sizeof(struct mmc_blk_request));
1432 brq->mrq.cmd = &brq->cmd;
1433 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Per Forlin54d49d72011-07-01 18:55:29 +02001435 brq->cmd.arg = blk_rq_pos(req);
1436 if (!mmc_card_blockaddr(card))
1437 brq->cmd.arg <<= 9;
1438 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1439 brq->data.blksz = 512;
1440 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1441 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001442 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Per Forlin54d49d72011-07-01 18:55:29 +02001444 /*
1445 * The block layer doesn't support all sector count
1446 * restrictions, so we need to be prepared for too big
1447 * requests.
1448 */
1449 if (brq->data.blocks > card->host->max_blk_count)
1450 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001452 if (brq->data.blocks > 1) {
1453 /*
1454 * After a read error, we redo the request one sector
1455 * at a time in order to accurately determine which
1456 * sectors can be read successfully.
1457 */
1458 if (disable_multi)
1459 brq->data.blocks = 1;
1460
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001461 /*
1462 * Some controllers have HW issues while operating
1463 * in multiple I/O mode
1464 */
1465 if (card->host->ops->multi_io_quirk)
1466 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1467 (rq_data_dir(req) == READ) ?
1468 MMC_DATA_READ : MMC_DATA_WRITE,
1469 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001470 }
Per Forlin54d49d72011-07-01 18:55:29 +02001471
1472 if (brq->data.blocks > 1 || do_rel_wr) {
1473 /* SPI multiblock writes terminate using a special
1474 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001475 */
Per Forlin54d49d72011-07-01 18:55:29 +02001476 if (!mmc_host_is_spi(card->host) ||
1477 rq_data_dir(req) == READ)
1478 brq->mrq.stop = &brq->stop;
1479 readcmd = MMC_READ_MULTIPLE_BLOCK;
1480 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1481 } else {
1482 brq->mrq.stop = NULL;
1483 readcmd = MMC_READ_SINGLE_BLOCK;
1484 writecmd = MMC_WRITE_BLOCK;
1485 }
1486 if (rq_data_dir(req) == READ) {
1487 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001488 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001489 if (brq->mrq.stop)
1490 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1491 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001492 } else {
1493 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001494 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001495 if (brq->mrq.stop)
1496 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1497 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001498 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001499
Per Forlin54d49d72011-07-01 18:55:29 +02001500 if (do_rel_wr)
1501 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001502
Per Forlin54d49d72011-07-01 18:55:29 +02001503 /*
Saugata Das42659002011-12-21 13:09:17 +05301504 * Data tag is used only during writing meta data to speed
1505 * up write and any subsequent read of this meta data
1506 */
1507 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1508 (req->cmd_flags & REQ_META) &&
1509 (rq_data_dir(req) == WRITE) &&
1510 ((brq->data.blocks * brq->data.blksz) >=
1511 card->ext_csd.data_tag_unit_size);
1512
1513 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001514 * Pre-defined multi-block transfers are preferable to
1515 * open ended-ones (and necessary for reliable writes).
1516 * However, it is not sufficient to just send CMD23,
1517 * and avoid the final CMD12, as on an error condition
1518 * CMD12 (stop) needs to be sent anyway. This, coupled
1519 * with Auto-CMD23 enhancements provided by some
1520 * hosts, means that the complexity of dealing
1521 * with this is best left to the host. If CMD23 is
1522 * supported by card and host, we'll fill sbc in and let
1523 * the host deal with handling it correctly. This means
1524 * that for hosts that don't expose MMC_CAP_CMD23, no
1525 * change of behavior will be observed.
1526 *
1527 * N.B: Some MMC cards experience perf degradation.
1528 * We'll avoid using CMD23-bounded multiblock writes for
1529 * these, while retaining features like reliable writes.
1530 */
Saugata Das42659002011-12-21 13:09:17 +05301531 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1532 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1533 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001534 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1535 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301536 (do_rel_wr ? (1 << 31) : 0) |
1537 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001538 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1539 brq->mrq.sbc = &brq->sbc;
1540 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001541
Per Forlin54d49d72011-07-01 18:55:29 +02001542 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001543
Per Forlin54d49d72011-07-01 18:55:29 +02001544 brq->data.sg = mqrq->sg;
1545 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001546
Per Forlin54d49d72011-07-01 18:55:29 +02001547 /*
1548 * Adjust the sg list so it is the same size as the
1549 * request.
1550 */
1551 if (brq->data.blocks != blk_rq_sectors(req)) {
1552 int i, data_size = brq->data.blocks << 9;
1553 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001554
Per Forlin54d49d72011-07-01 18:55:29 +02001555 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1556 data_size -= sg->length;
1557 if (data_size <= 0) {
1558 sg->length += data_size;
1559 i++;
1560 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001561 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001562 }
Per Forlin54d49d72011-07-01 18:55:29 +02001563 brq->data.sg_len = i;
1564 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001565
Per Forlinee8a43a2011-07-01 18:55:33 +02001566 mqrq->mmc_active.mrq = &brq->mrq;
1567 mqrq->mmc_active.err_check = mmc_blk_err_check;
1568
Per Forlin54d49d72011-07-01 18:55:29 +02001569 mmc_queue_bounce_pre(mqrq);
1570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Adrian Hunter67716322011-08-29 16:42:15 +03001572static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1573 struct mmc_blk_request *brq, struct request *req,
1574 int ret)
1575{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001576 struct mmc_queue_req *mq_rq;
1577 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1578
Adrian Hunter67716322011-08-29 16:42:15 +03001579 /*
1580 * If this is an SD card and we're writing, we can first
1581 * mark the known good sectors as ok.
1582 *
1583 * If the card is not SD, we can still ok written sectors
1584 * as reported by the controller (which might be less than
1585 * the real number of written sectors, but never more).
1586 */
1587 if (mmc_card_sd(card)) {
1588 u32 blocks;
1589
1590 blocks = mmc_sd_num_wr_blocks(card);
1591 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301592 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001593 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001594 } else {
1595 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001596 }
1597 return ret;
1598}
1599
Per Forlinee8a43a2011-07-01 18:55:33 +02001600static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001601{
Linus Walleij7db30282016-11-18 13:36:15 +01001602 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001603 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001604 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001605 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001606 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001607 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001608 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001609 struct mmc_async_req *areq;
1610
1611 if (!rqc && !mq->mqrq_prev->req)
1612 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001613
1614 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001615 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301616 /*
1617 * When 4KB native sector is enabled, only 8 blocks
1618 * multiple read or write is allowed
1619 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001620 if (mmc_large_sector(card) &&
1621 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301622 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001623 rqc->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001624 mq_rq = mq->mqrq_cur;
Adrian Hunter5be80372016-11-29 12:09:09 +02001625 req = rqc;
1626 rqc = NULL;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301627 goto cmd_abort;
1628 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001629
Linus Walleij03d640a2016-11-25 10:35:00 +01001630 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001631 areq = &mq->mqrq_cur->mmc_active;
1632 } else
1633 areq = NULL;
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001634 areq = mmc_start_req(card->host, areq, &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001635 if (!areq) {
1636 if (status == MMC_BLK_NEW_REQUEST)
1637 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001638 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001639 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001640
Per Forlinee8a43a2011-07-01 18:55:33 +02001641 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1642 brq = &mq_rq->brq;
1643 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001644 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001645 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001646
Per Forlind78d4a8a2011-07-01 18:55:30 +02001647 switch (status) {
1648 case MMC_BLK_SUCCESS:
1649 case MMC_BLK_PARTIAL:
1650 /*
1651 * A block was successfully transferred.
1652 */
Adrian Hunter67716322011-08-29 16:42:15 +03001653 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001654
Linus Walleij03d640a2016-11-25 10:35:00 +01001655 ret = blk_end_request(req, 0,
1656 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001657
Adrian Hunter67716322011-08-29 16:42:15 +03001658 /*
1659 * If the blk_end_request function returns non-zero even
1660 * though all data has been transferred and no errors
1661 * were returned by the host controller, it's a bug.
1662 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001663 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301664 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001665 __func__, blk_rq_bytes(req),
1666 brq->data.bytes_xfered);
1667 rqc = NULL;
1668 goto cmd_abort;
1669 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001670 break;
1671 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001672 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08001673 if (mmc_blk_reset(md, card->host, type))
1674 goto cmd_abort;
1675 if (!ret)
1676 goto start_new_req;
1677 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001678 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001679 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001680 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001681 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001682 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001683 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001684 if (!mmc_blk_reset(md, card->host, type))
1685 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001686 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001687 case MMC_BLK_DATA_ERR: {
1688 int err;
1689
1690 err = mmc_blk_reset(md, card->host, type);
1691 if (!err)
1692 break;
Linus Walleij03d640a2016-11-25 10:35:00 +01001693 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001694 goto cmd_abort;
1695 /* Fall through */
1696 }
1697 case MMC_BLK_ECC_ERR:
1698 if (brq->data.blocks > 1) {
1699 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001700 pr_warn("%s: retrying using single block read\n",
1701 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001702 disable_multi = 1;
1703 break;
1704 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001705 /*
1706 * After an error, we redo I/O one sector at a
1707 * time, so we only reach here after trying to
1708 * read a single sector.
1709 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301710 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001711 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001712 if (!ret)
1713 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001714 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301715 case MMC_BLK_NOMEDIUM:
1716 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001717 default:
1718 pr_err("%s: Unhandled return value (%d)",
1719 req->rq_disk->disk_name, status);
1720 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001721 }
1722
Per Forlinee8a43a2011-07-01 18:55:33 +02001723 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001724 /*
1725 * In case of a incomplete request
1726 * prepare it again and resend.
1727 */
1728 mmc_blk_rw_rq_prep(mq_rq, card,
1729 disable_multi, mq);
1730 mmc_start_req(card->host,
1731 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001732 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 } while (ret);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 1;
1737
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001738 cmd_abort:
Linus Walleij03d640a2016-11-25 10:35:00 +01001739 if (mmc_card_removed(card))
Linus Torvalds36869cb2016-12-13 10:19:16 -08001740 req->rq_flags |= RQF_QUIET;
Linus Walleij03d640a2016-11-25 10:35:00 +01001741 while (ret)
1742 ret = blk_end_request(req, -EIO,
1743 blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Per Forlinee8a43a2011-07-01 18:55:33 +02001745 start_new_req:
1746 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001747 if (mmc_card_removed(card)) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001748 rqc->rq_flags |= RQF_QUIET;
Seungwon Jeon7a819022013-01-22 19:48:07 +09001749 blk_end_request_all(rqc, -EIO);
1750 } else {
1751 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1752 mmc_start_req(card->host,
1753 &mq->mqrq_cur->mmc_active, NULL);
1754 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001755 }
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758}
1759
Linus Walleij29eb7bd2016-09-20 11:34:38 +02001760int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001761{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001762 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001763 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001764 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001765 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001766
Per Forlinee8a43a2011-07-01 18:55:33 +02001767 if (req && !mq->mqrq_prev->req)
1768 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001769 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001770
Andrei Warkentin371a6892011-04-11 18:10:25 -05001771 ret = mmc_blk_part_switch(card, md);
1772 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001773 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301774 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001775 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001776 ret = 0;
1777 goto out;
1778 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001779
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001780 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001781 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001782 /* complete ongoing async transfer before issuing discard */
1783 if (card->host->areq)
1784 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001785 ret = mmc_blk_issue_discard_rq(mq, req);
1786 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1787 /* complete ongoing async transfer before issuing secure erase*/
1788 if (card->host->areq)
1789 mmc_blk_issue_rw_rq(mq, NULL);
1790 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001791 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001792 /* complete ongoing async transfer before issuing flush */
1793 if (card->host->areq)
1794 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001795 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001796 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001797 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001798 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001799
Andrei Warkentin371a6892011-04-11 18:10:25 -05001800out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001801 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001802 /*
1803 * Release host when there are no more requests
1804 * and after special request(discard, flush) is done.
1805 * In case sepecial request, there is no reentry to
1806 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1807 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001808 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001809 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Russell Kinga6f6c962006-01-03 22:38:44 +00001812static inline int mmc_blk_readonly(struct mmc_card *card)
1813{
1814 return mmc_card_readonly(card) ||
1815 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1816}
1817
Andrei Warkentin371a6892011-04-11 18:10:25 -05001818static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1819 struct device *parent,
1820 sector_t size,
1821 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001822 const char *subname,
1823 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 struct mmc_blk_data *md;
1826 int devidx, ret;
1827
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001828again:
1829 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
1830 return ERR_PTR(-ENOMEM);
1831
1832 spin_lock(&mmc_blk_lock);
1833 ret = ida_get_new(&mmc_blk_ida, &devidx);
1834 spin_unlock(&mmc_blk_lock);
1835
1836 if (ret == -EAGAIN)
1837 goto again;
1838 else if (ret)
1839 return ERR_PTR(ret);
1840
1841 if (devidx >= max_devices) {
1842 ret = -ENOSPC;
1843 goto out;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001846 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001847 if (!md) {
1848 ret = -ENOMEM;
1849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001851
Johan Rudholmadd710e2011-12-02 08:51:06 +01001852 md->area_type = area_type;
1853
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001854 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001855 * Set the read-only status based on the supported commands
1856 * and the write protect switch.
1857 */
1858 md->read_only = mmc_blk_readonly(card);
1859
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001860 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001861 if (md->disk == NULL) {
1862 ret = -ENOMEM;
1863 goto err_kfree;
1864 }
1865
1866 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001867 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001868 md->usage = 1;
1869
Adrian Hunterd09408a2011-06-23 13:40:28 +03001870 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001871 if (ret)
1872 goto err_putdisk;
1873
Linus Walleij7db30282016-11-18 13:36:15 +01001874 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001875
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001876 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001877 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001878 md->disk->fops = &mmc_bdops;
1879 md->disk->private_data = md;
1880 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001881 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001882 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001883 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001884 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001885 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001886
1887 /*
1888 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1889 *
1890 * - be set for removable media with permanent block devices
1891 * - be unset for removable block devices with permanent media
1892 *
1893 * Since MMC block devices clearly fall under the second
1894 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1895 * should use the block device creation/destruction hotplug
1896 * messages to tell when the card is present.
1897 */
1898
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001899 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001900 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001901
Saugata Dasa5075eb2012-05-17 16:32:21 +05301902 if (mmc_card_mmc(card))
1903 blk_queue_logical_block_size(md->queue.queue,
1904 card->ext_csd.data_sector_size);
1905 else
1906 blk_queue_logical_block_size(md->queue.queue, 512);
1907
Andrei Warkentin371a6892011-04-11 18:10:25 -05001908 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001909
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001910 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001911 if ((mmc_card_mmc(card) &&
1912 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001913 (mmc_card_sd(card) &&
1914 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1915 md->flags |= MMC_BLK_CMD23;
1916 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001917
1918 if (mmc_card_mmc(card) &&
1919 md->flags & MMC_BLK_CMD23 &&
1920 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1921 card->ext_csd.rel_sectors)) {
1922 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001923 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001927
1928 err_putdisk:
1929 put_disk(md->disk);
1930 err_kfree:
1931 kfree(md);
1932 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001933 spin_lock(&mmc_blk_lock);
1934 ida_remove(&mmc_blk_ida, devidx);
1935 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00001936 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
Andrei Warkentin371a6892011-04-11 18:10:25 -05001939static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1940{
1941 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001942
1943 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1944 /*
1945 * The EXT_CSD sector count is in number or 512 byte
1946 * sectors.
1947 */
1948 size = card->ext_csd.sectors;
1949 } else {
1950 /*
1951 * The CSD capacity field is in units of read_blkbits.
1952 * set_capacity takes units of 512 bytes.
1953 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001954 size = (typeof(sector_t))card->csd.capacity
1955 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001956 }
1957
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001958 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001959 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001960}
1961
1962static int mmc_blk_alloc_part(struct mmc_card *card,
1963 struct mmc_blk_data *md,
1964 unsigned int part_type,
1965 sector_t size,
1966 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001967 const char *subname,
1968 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001969{
1970 char cap_str[10];
1971 struct mmc_blk_data *part_md;
1972
1973 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001974 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001975 if (IS_ERR(part_md))
1976 return PTR_ERR(part_md);
1977 part_md->part_type = part_type;
1978 list_add(&part_md->part, &md->part);
1979
James Bottomleyb9f28d82015-03-05 18:47:01 -08001980 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001981 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301982 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001983 part_md->disk->disk_name, mmc_card_id(card),
1984 mmc_card_name(card), part_md->part_type, cap_str);
1985 return 0;
1986}
1987
Namjae Jeone0c368d2011-10-06 23:41:38 +09001988/* MMC Physical partitions consist of two boot partitions and
1989 * up to four general purpose partitions.
1990 * For each partition enabled in EXT_CSD a block device will be allocatedi
1991 * to provide access to the partition.
1992 */
1993
Andrei Warkentin371a6892011-04-11 18:10:25 -05001994static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1995{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001996 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001997
1998 if (!mmc_card_mmc(card))
1999 return 0;
2000
Namjae Jeone0c368d2011-10-06 23:41:38 +09002001 for (idx = 0; idx < card->nr_parts; idx++) {
2002 if (card->part[idx].size) {
2003 ret = mmc_blk_alloc_part(card, md,
2004 card->part[idx].part_cfg,
2005 card->part[idx].size >> 9,
2006 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002007 card->part[idx].name,
2008 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002009 if (ret)
2010 return ret;
2011 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002012 }
2013
2014 return ret;
2015}
2016
Andrei Warkentin371a6892011-04-11 18:10:25 -05002017static void mmc_blk_remove_req(struct mmc_blk_data *md)
2018{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002019 struct mmc_card *card;
2020
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002022 /*
2023 * Flush remaining requests and free queues. It
2024 * is freeing the queue that stops new requests
2025 * from being accepted.
2026 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002027 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002028 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002029 if (md->disk->flags & GENHD_FL_UP) {
2030 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002031 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2032 card->ext_csd.boot_ro_lockable)
2033 device_remove_file(disk_to_dev(md->disk),
2034 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002035
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036 del_gendisk(md->disk);
2037 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002038 mmc_blk_put(md);
2039 }
2040}
2041
2042static void mmc_blk_remove_parts(struct mmc_card *card,
2043 struct mmc_blk_data *md)
2044{
2045 struct list_head *pos, *q;
2046 struct mmc_blk_data *part_md;
2047
2048 list_for_each_safe(pos, q, &md->part) {
2049 part_md = list_entry(pos, struct mmc_blk_data, part);
2050 list_del(pos);
2051 mmc_blk_remove_req(part_md);
2052 }
2053}
2054
2055static int mmc_add_disk(struct mmc_blk_data *md)
2056{
2057 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002058 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002059
Dan Williams307d8e62016-06-20 10:40:44 -07002060 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002061 md->force_ro.show = force_ro_show;
2062 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302063 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002064 md->force_ro.attr.name = "force_ro";
2065 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2066 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2067 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002068 goto force_ro_fail;
2069
2070 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2071 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002072 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002073
2074 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2075 mode = S_IRUGO;
2076 else
2077 mode = S_IRUGO | S_IWUSR;
2078
2079 md->power_ro_lock.show = power_ro_lock_show;
2080 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002081 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002082 md->power_ro_lock.attr.mode = mode;
2083 md->power_ro_lock.attr.name =
2084 "ro_lock_until_next_power_on";
2085 ret = device_create_file(disk_to_dev(md->disk),
2086 &md->power_ro_lock);
2087 if (ret)
2088 goto power_ro_lock_fail;
2089 }
2090 return ret;
2091
2092power_ro_lock_fail:
2093 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2094force_ro_fail:
2095 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002096
2097 return ret;
2098}
2099
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002100static const struct mmc_fixup blk_fixups[] =
2101{
Chris Ballc59d4472011-11-11 22:01:43 -05002102 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2103 MMC_QUIRK_INAND_CMD38),
2104 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2105 MMC_QUIRK_INAND_CMD38),
2106 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2107 MMC_QUIRK_INAND_CMD38),
2108 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2109 MMC_QUIRK_INAND_CMD38),
2110 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2111 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002112
2113 /*
2114 * Some MMC cards experience performance degradation with CMD23
2115 * instead of CMD12-bounded multiblock transfers. For now we'll
2116 * black list what's bad...
2117 * - Certain Toshiba cards.
2118 *
2119 * N.B. This doesn't affect SD cards.
2120 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002121 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2122 MMC_QUIRK_BLK_NO_CMD23),
2123 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2124 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002125 MMC_FIXUP("MMC08G", 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("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002128 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002129 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002130 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002131
2132 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002133 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002134 */
Chris Ballc59d4472011-11-11 22:01:43 -05002135 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002136 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002137 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2138 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002139
Ian Chen3550ccd2012-08-29 15:05:36 +09002140 /*
2141 * On these Samsung MoviNAND parts, performing secure erase or
2142 * secure trim can result in unrecoverable corruption due to a
2143 * firmware bug.
2144 */
2145 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2146 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2147 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2148 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2149 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2150 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2151 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2152 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2153 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2154 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2155 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2156 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2157 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2158 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2159 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2160 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2161
Shawn Linb5b4ff02015-08-12 13:08:32 +08002162 /*
2163 * On Some Kingston eMMCs, performing trim can result in
2164 * unrecoverable data conrruption occasionally due to a firmware bug.
2165 */
2166 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2167 MMC_QUIRK_TRIM_BROKEN),
2168 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2169 MMC_QUIRK_TRIM_BROKEN),
2170
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002171 END_FIXUP
2172};
2173
Ulf Hansson96541ba2015-04-14 13:06:12 +02002174static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002176 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002177 char cap_str[10];
2178
Pierre Ossman912490d2005-05-21 10:27:02 +01002179 /*
2180 * Check that the card supports the command class(es) we need.
2181 */
2182 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -ENODEV;
2184
Lukas Czerner5204d002014-06-18 13:18:07 +02002185 mmc_fixup_device(card, blk_fixups);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 md = mmc_blk_alloc(card);
2188 if (IS_ERR(md))
2189 return PTR_ERR(md);
2190
James Bottomleyb9f28d82015-03-05 18:47:01 -08002191 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002192 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302193 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002195 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Andrei Warkentin371a6892011-04-11 18:10:25 -05002197 if (mmc_blk_alloc_parts(card, md))
2198 goto out;
2199
Ulf Hansson96541ba2015-04-14 13:06:12 +02002200 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002201
Andrei Warkentin371a6892011-04-11 18:10:25 -05002202 if (mmc_add_disk(md))
2203 goto out;
2204
2205 list_for_each_entry(part_md, &md->part, part) {
2206 if (mmc_add_disk(part_md))
2207 goto out;
2208 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002209
2210 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2211 pm_runtime_use_autosuspend(&card->dev);
2212
2213 /*
2214 * Don't enable runtime PM for SD-combo cards here. Leave that
2215 * decision to be taken during the SDIO init sequence instead.
2216 */
2217 if (card->type != MMC_TYPE_SD_COMBO) {
2218 pm_runtime_set_active(&card->dev);
2219 pm_runtime_enable(&card->dev);
2220 }
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return 0;
2223
2224 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225 mmc_blk_remove_parts(card, md);
2226 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Ulf Hansson96541ba2015-04-14 13:06:12 +02002230static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002232 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Andrei Warkentin371a6892011-04-11 18:10:25 -05002234 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002235 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002236 mmc_claim_host(card->host);
2237 mmc_blk_part_switch(card, md);
2238 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002239 if (card->type != MMC_TYPE_SD_COMBO)
2240 pm_runtime_disable(&card->dev);
2241 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002242 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002243 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Ulf Hansson96541ba2015-04-14 13:06:12 +02002246static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002248 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002249 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 if (md) {
2252 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002253 list_for_each_entry(part_md, &md->part, part) {
2254 mmc_queue_suspend(&part_md->queue);
2255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 return 0;
2258}
2259
Ulf Hansson96541ba2015-04-14 13:06:12 +02002260static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002261{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002262 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002263}
2264
Ulf Hansson0967edc2014-10-06 11:29:42 +02002265#ifdef CONFIG_PM_SLEEP
2266static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002267{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002268 struct mmc_card *card = mmc_dev_to_card(dev);
2269
2270 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002271}
2272
Ulf Hansson0967edc2014-10-06 11:29:42 +02002273static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002275 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002276 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002279 /*
2280 * Resume involves the card going into idle state,
2281 * so current partition is always the main one.
2282 */
2283 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002285 list_for_each_entry(part_md, &md->part, part) {
2286 mmc_queue_resume(&part_md->queue);
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289 return 0;
2290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291#endif
2292
Ulf Hansson0967edc2014-10-06 11:29:42 +02002293static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2294
Ulf Hansson96541ba2015-04-14 13:06:12 +02002295static struct mmc_driver mmc_driver = {
2296 .drv = {
2297 .name = "mmcblk",
2298 .pm = &mmc_blk_pm_ops,
2299 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 .probe = mmc_blk_probe,
2301 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002302 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304
2305static int __init mmc_blk_init(void)
2306{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002307 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002309 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2310 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2311
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002312 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002313
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002314 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2315 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002318 res = mmc_register_driver(&mmc_driver);
2319 if (res)
2320 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002322 return 0;
2323 out2:
2324 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 out:
2326 return res;
2327}
2328
2329static void __exit mmc_blk_exit(void)
2330{
2331 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002332 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
2335module_init(mmc_blk_init);
2336module_exit(mmc_blk_exit);
2337
2338MODULE_LICENSE("GPL");
2339MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2340