blob: 6c8be1a80551416ea03793b1df469ea43a4fbec5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010050#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010051#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010052#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010054#include "mmc_ops.h"
55#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000057MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040058#ifdef MODULE_PARAM_PREFIX
59#undef MODULE_PARAM_PREFIX
60#endif
61#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010062
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050063#define INAND_CMD38_ARG_EXT_CSD 113
64#define INAND_CMD38_ARG_ERASE 0x00
65#define INAND_CMD38_ARG_TRIM 0x01
66#define INAND_CMD38_ARG_SECERASE 0x80
67#define INAND_CMD38_ARG_SECTRIM1 0x81
68#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060069#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030070#define MMC_SANITIZE_REQ_TIMEOUT 240000
71#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050072
Luca Porziod3df0462015-11-06 15:12:26 +000073#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090074 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020075static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040076
77/*
78 * The defaults come from config options but can be overriden by module
79 * or bootarg options.
80 */
81static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82
83/*
84 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020086 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040087 */
88static int max_devices;
89
Ben Hutchingsa26eba62014-11-06 03:35:09 +000090#define MAX_DEVICES 256
91
Ulf Hanssonb10fa992016-04-07 14:36:46 +020092static DEFINE_IDA(mmc_blk_ida);
93static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * There is one mmc_blk_data per slot.
97 */
98struct mmc_blk_data {
99 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700100 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct gendisk *disk;
102 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500103 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500105 unsigned int flags;
106#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
107#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000110 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300112 unsigned int reset_done;
113#define MMC_BLK_READ BIT(0)
114#define MMC_BLK_WRITE BIT(1)
115#define MMC_BLK_DISCARD BIT(2)
116#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500117
118 /*
119 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200120 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 * track of the current selected device partition.
122 */
123 unsigned int part_curr;
124 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 struct device_attribute power_ro_lock;
126 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Arjan van de Vena621aae2006-01-12 18:43:35 +0000129static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400131module_param(perdev_minors, int, 0444);
132MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
133
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200134static inline int mmc_blk_part_switch(struct mmc_card *card,
135 struct mmc_blk_data *md);
136static int get_card_status(struct mmc_card *card, u32 *status, int retries);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
139{
140 struct mmc_blk_data *md;
141
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 md = disk->private_data;
144 if (md && md->usage == 0)
145 md = NULL;
146 if (md)
147 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000148 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return md;
151}
152
Andrei Warkentin371a6892011-04-11 18:10:25 -0500153static inline int mmc_get_devidx(struct gendisk *disk)
154{
Colin Cross382c55f2015-10-22 10:00:41 -0700155 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500156 return devidx;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void mmc_blk_put(struct mmc_blk_data *md)
160{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 md->usage--;
163 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800165 blk_cleanup_queue(md->queue.queue);
166
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200167 spin_lock(&mmc_blk_lock);
168 ida_remove(&mmc_blk_ida, devidx);
169 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(md);
173 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000174 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Johan Rudholmadd710e2011-12-02 08:51:06 +0100177static ssize_t power_ro_lock_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 int ret;
181 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
182 struct mmc_card *card = md->queue.card;
183 int locked = 0;
184
185 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
186 locked = 2;
187 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
188 locked = 1;
189
190 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
191
Tomas Winkler9098f842015-07-16 15:50:45 +0200192 mmc_blk_put(md);
193
Johan Rudholmadd710e2011-12-02 08:51:06 +0100194 return ret;
195}
196
197static ssize_t power_ro_lock_store(struct device *dev,
198 struct device_attribute *attr, const char *buf, size_t count)
199{
200 int ret;
201 struct mmc_blk_data *md, *part_md;
202 struct mmc_card *card;
203 unsigned long set;
204
205 if (kstrtoul(buf, 0, &set))
206 return -EINVAL;
207
208 if (set != 1)
209 return count;
210
211 md = mmc_blk_get(dev_to_disk(dev));
212 card = md->queue.card;
213
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200214 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100215
216 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
217 card->ext_csd.boot_ro_lock |
218 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
219 card->ext_csd.part_time);
220 if (ret)
221 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
222 else
223 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
224
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200225 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100226
227 if (!ret) {
228 pr_info("%s: Locking boot partition ro until next power on\n",
229 md->disk->disk_name);
230 set_disk_ro(md->disk, 1);
231
232 list_for_each_entry(part_md, &md->part, part)
233 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
234 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
235 set_disk_ro(part_md->disk, 1);
236 }
237 }
238
239 mmc_blk_put(md);
240 return count;
241}
242
Andrei Warkentin371a6892011-04-11 18:10:25 -0500243static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
244 char *buf)
245{
246 int ret;
247 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
248
Baruch Siach0031a982014-09-22 10:12:51 +0300249 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500250 get_disk_ro(dev_to_disk(dev)) ^
251 md->read_only);
252 mmc_blk_put(md);
253 return ret;
254}
255
256static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
258{
259 int ret;
260 char *end;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262 unsigned long set = simple_strtoul(buf, &end, 0);
263 if (end == buf) {
264 ret = -EINVAL;
265 goto out;
266 }
267
268 set_disk_ro(dev_to_disk(dev), set || md->read_only);
269 ret = count;
270out:
271 mmc_blk_put(md);
272 return ret;
273}
274
Al Viroa5a15612008-03-02 10:33:30 -0500275static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Al Viroa5a15612008-03-02 10:33:30 -0500277 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int ret = -ENXIO;
279
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200280 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (md) {
282 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500283 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700285
Al Viroa5a15612008-03-02 10:33:30 -0500286 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700287 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700288 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200291 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return ret;
294}
295
Al Virodb2a1442013-05-05 21:52:57 -0400296static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Al Viroa5a15612008-03-02 10:33:30 -0500298 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200300 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200302 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800306mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800308 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
309 geo->heads = 4;
310 geo->sectors = 16;
311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
John Calixtocb87ea22011-04-26 18:56:29 -0400314struct mmc_blk_ioc_data {
315 struct mmc_ioc_cmd ic;
316 unsigned char *buf;
317 u64 buf_bytes;
318};
319
320static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
321 struct mmc_ioc_cmd __user *user)
322{
323 struct mmc_blk_ioc_data *idata;
324 int err;
325
yalin wang1ff89502015-11-12 19:27:11 +0800326 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400327 if (!idata) {
328 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400329 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400330 }
331
332 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
333 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400334 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400335 }
336
337 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
338 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
339 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400340 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400341 }
342
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300343 if (!idata->buf_bytes) {
344 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100345 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300346 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100347
yalin wang1ff89502015-11-12 19:27:11 +0800348 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400349 if (!idata->buf) {
350 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400351 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400352 }
353
354 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
355 idata->ic.data_ptr, idata->buf_bytes)) {
356 err = -EFAULT;
357 goto copy_err;
358 }
359
360 return idata;
361
362copy_err:
363 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400364idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400365 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400366out:
John Calixtocb87ea22011-04-26 18:56:29 -0400367 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400368}
369
Jon Huntera5f57742015-09-22 10:27:53 +0100370static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
371 struct mmc_blk_ioc_data *idata)
372{
373 struct mmc_ioc_cmd *ic = &idata->ic;
374
375 if (copy_to_user(&(ic_ptr->response), ic->response,
376 sizeof(ic->response)))
377 return -EFAULT;
378
379 if (!idata->ic.write_flag) {
380 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
381 idata->buf, idata->buf_bytes))
382 return -EFAULT;
383 }
384
385 return 0;
386}
387
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200388static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
389 u32 retries_max)
390{
391 int err;
392 u32 retry_count = 0;
393
394 if (!status || !retries_max)
395 return -EINVAL;
396
397 do {
398 err = get_card_status(card, status, 5);
399 if (err)
400 break;
401
402 if (!R1_STATUS(*status) &&
403 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
404 break; /* RPMB programming operation complete */
405
406 /*
407 * Rechedule to give the MMC device a chance to continue
408 * processing the previous command without being polled too
409 * frequently.
410 */
411 usleep_range(1000, 5000);
412 } while (++retry_count < retries_max);
413
414 if (retry_count == retries_max)
415 err = -EPERM;
416
417 return err;
418}
419
Maya Erez775a9362013-04-18 15:41:55 +0300420static int ioctl_do_sanitize(struct mmc_card *card)
421{
422 int err;
423
Ulf Hanssona2d10862013-12-16 14:37:26 +0100424 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300425 pr_warn("%s: %s - SANITIZE is not supported\n",
426 mmc_hostname(card->host), __func__);
427 err = -EOPNOTSUPP;
428 goto out;
429 }
430
431 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
432 mmc_hostname(card->host), __func__);
433
434 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
435 EXT_CSD_SANITIZE_START, 1,
436 MMC_SANITIZE_REQ_TIMEOUT);
437
438 if (err)
439 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
440 mmc_hostname(card->host), __func__, err);
441
442 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
443 __func__);
444out:
445 return err;
446}
447
Jon Huntera5f57742015-09-22 10:27:53 +0100448static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
449 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400450{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900451 struct mmc_command cmd = {};
452 struct mmc_data data = {};
453 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400454 struct scatterlist sg;
455 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200456 int is_rpmb = false;
457 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400458
Jon Huntera5f57742015-09-22 10:27:53 +0100459 if (!card || !md || !idata)
460 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400461
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200462 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
463 is_rpmb = true;
464
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100465 cmd.opcode = idata->ic.opcode;
466 cmd.arg = idata->ic.arg;
467 cmd.flags = idata->ic.flags;
468
469 if (idata->buf_bytes) {
470 data.sg = &sg;
471 data.sg_len = 1;
472 data.blksz = idata->ic.blksz;
473 data.blocks = idata->ic.blocks;
474
475 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
476
477 if (idata->ic.write_flag)
478 data.flags = MMC_DATA_WRITE;
479 else
480 data.flags = MMC_DATA_READ;
481
482 /* data.flags must already be set before doing this. */
483 mmc_set_data_timeout(&data, card);
484
485 /* Allow overriding the timeout_ns for empirical tuning. */
486 if (idata->ic.data_timeout_ns)
487 data.timeout_ns = idata->ic.data_timeout_ns;
488
489 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
490 /*
491 * Pretend this is a data transfer and rely on the
492 * host driver to compute timeout. When all host
493 * drivers support cmd.cmd_timeout for R1B, this
494 * can be changed to:
495 *
496 * mrq.data = NULL;
497 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
498 */
499 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
500 }
501
502 mrq.data = &data;
503 }
504
505 mrq.cmd = &cmd;
506
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507 err = mmc_blk_part_switch(card, md);
508 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100509 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200510
John Calixtocb87ea22011-04-26 18:56:29 -0400511 if (idata->ic.is_acmd) {
512 err = mmc_app_cmd(card->host, card);
513 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100514 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400515 }
516
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200517 if (is_rpmb) {
518 err = mmc_set_blockcount(card, data.blocks,
519 idata->ic.write_flag & (1 << 31));
520 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100521 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200522 }
523
Yaniv Gardia82e4842013-06-05 14:13:08 +0300524 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
525 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300526 err = ioctl_do_sanitize(card);
527
528 if (err)
529 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
530 __func__, err);
531
Jon Huntera5f57742015-09-22 10:27:53 +0100532 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300533 }
534
John Calixtocb87ea22011-04-26 18:56:29 -0400535 mmc_wait_for_req(card->host, &mrq);
536
537 if (cmd.error) {
538 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
539 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100540 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400541 }
542 if (data.error) {
543 dev_err(mmc_dev(card->host), "%s: data error %d\n",
544 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100545 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400546 }
547
548 /*
549 * According to the SD specs, some commands require a delay after
550 * issuing the command.
551 */
552 if (idata->ic.postsleep_min_us)
553 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
554
Jon Huntera5f57742015-09-22 10:27:53 +0100555 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400556
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200557 if (is_rpmb) {
558 /*
559 * Ensure RPMB command has completed by polling CMD13
560 * "Send Status".
561 */
562 err = ioctl_rpmb_card_status_poll(card, &status, 5);
563 if (err)
564 dev_err(mmc_dev(card->host),
565 "%s: Card Status=0x%08X, error %d\n",
566 __func__, status, err);
567 }
568
Jon Huntera5f57742015-09-22 10:27:53 +0100569 return err;
570}
571
572static int mmc_blk_ioctl_cmd(struct block_device *bdev,
573 struct mmc_ioc_cmd __user *ic_ptr)
574{
575 struct mmc_blk_ioc_data *idata;
576 struct mmc_blk_data *md;
577 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700578 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100579
Shawn Lin83c742c2016-03-16 18:15:47 +0800580 /*
581 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
582 * whole block device, not on a partition. This prevents overspray
583 * between sibling partitions.
584 */
585 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
586 return -EPERM;
587
Jon Huntera5f57742015-09-22 10:27:53 +0100588 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
589 if (IS_ERR(idata))
590 return PTR_ERR(idata);
591
592 md = mmc_blk_get(bdev->bd_disk);
593 if (!md) {
594 err = -EINVAL;
595 goto cmd_err;
596 }
597
598 card = md->queue.card;
599 if (IS_ERR(card)) {
600 err = PTR_ERR(card);
601 goto cmd_done;
602 }
603
604 mmc_get_card(card);
605
Grant Grundlerb0934102015-09-23 18:30:33 -0700606 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100607
Adrian Hunter3c866562016-05-04 14:38:12 +0300608 /* Always switch back to main area after RPMB access */
609 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
610 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
611
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200612 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400613
Grant Grundlerb0934102015-09-23 18:30:33 -0700614 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100615
John Calixtocb87ea22011-04-26 18:56:29 -0400616cmd_done:
617 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300618cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400619 kfree(idata->buf);
620 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700621 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400622}
623
Jon Huntera5f57742015-09-22 10:27:53 +0100624static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
625 struct mmc_ioc_multi_cmd __user *user)
626{
627 struct mmc_blk_ioc_data **idata = NULL;
628 struct mmc_ioc_cmd __user *cmds = user->cmds;
629 struct mmc_card *card;
630 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700631 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100632 __u64 num_of_cmds;
633
Shawn Lin83c742c2016-03-16 18:15:47 +0800634 /*
635 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
636 * whole block device, not on a partition. This prevents overspray
637 * between sibling partitions.
638 */
639 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
640 return -EPERM;
641
Jon Huntera5f57742015-09-22 10:27:53 +0100642 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
643 sizeof(num_of_cmds)))
644 return -EFAULT;
645
646 if (num_of_cmds > MMC_IOC_MAX_CMDS)
647 return -EINVAL;
648
649 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
650 if (!idata)
651 return -ENOMEM;
652
653 for (i = 0; i < num_of_cmds; i++) {
654 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
655 if (IS_ERR(idata[i])) {
656 err = PTR_ERR(idata[i]);
657 num_of_cmds = i;
658 goto cmd_err;
659 }
660 }
661
662 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800663 if (!md) {
664 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100665 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800666 }
Jon Huntera5f57742015-09-22 10:27:53 +0100667
668 card = md->queue.card;
669 if (IS_ERR(card)) {
670 err = PTR_ERR(card);
671 goto cmd_done;
672 }
673
674 mmc_get_card(card);
675
Grant Grundlerb0934102015-09-23 18:30:33 -0700676 for (i = 0; i < num_of_cmds && !ioc_err; i++)
677 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100678
Adrian Hunter3c866562016-05-04 14:38:12 +0300679 /* Always switch back to main area after RPMB access */
680 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
681 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
682
Jon Huntera5f57742015-09-22 10:27:53 +0100683 mmc_put_card(card);
684
685 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700686 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100687 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100688
689cmd_done:
690 mmc_blk_put(md);
691cmd_err:
692 for (i = 0; i < num_of_cmds; i++) {
693 kfree(idata[i]->buf);
694 kfree(idata[i]);
695 }
696 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700697 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100698}
699
John Calixtocb87ea22011-04-26 18:56:29 -0400700static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
701 unsigned int cmd, unsigned long arg)
702{
Jon Huntera5f57742015-09-22 10:27:53 +0100703 switch (cmd) {
704 case MMC_IOC_CMD:
705 return mmc_blk_ioctl_cmd(bdev,
706 (struct mmc_ioc_cmd __user *)arg);
707 case MMC_IOC_MULTI_CMD:
708 return mmc_blk_ioctl_multi_cmd(bdev,
709 (struct mmc_ioc_multi_cmd __user *)arg);
710 default:
711 return -EINVAL;
712 }
John Calixtocb87ea22011-04-26 18:56:29 -0400713}
714
715#ifdef CONFIG_COMPAT
716static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
717 unsigned int cmd, unsigned long arg)
718{
719 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
720}
721#endif
722
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700723static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500724 .open = mmc_blk_open,
725 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800726 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400728 .ioctl = mmc_blk_ioctl,
729#ifdef CONFIG_COMPAT
730 .compat_ioctl = mmc_blk_compat_ioctl,
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Andrei Warkentin371a6892011-04-11 18:10:25 -0500734static inline int mmc_blk_part_switch(struct mmc_card *card,
735 struct mmc_blk_data *md)
736{
737 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200738 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300739
Andrei Warkentin371a6892011-04-11 18:10:25 -0500740 if (main_md->part_curr == md->part_type)
741 return 0;
742
743 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300744 u8 part_config = card->ext_csd.part_config;
745
Adrian Hunter57da0c02016-05-04 14:38:13 +0300746 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
747 mmc_retune_pause(card->host);
748
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300749 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
750 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500751
752 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300753 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500754 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300755 if (ret) {
756 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
757 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500758 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300759 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300760
761 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300762
763 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
764 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300765 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500766
767 main_md->part_curr = md->part_type;
768 return 0;
769}
770
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700771static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
772{
773 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100774 u32 result;
775 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700776
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900777 struct mmc_request mrq = {};
778 struct mmc_command cmd = {};
779 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700780
781 struct scatterlist sg;
782
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783 cmd.opcode = MMC_APP_CMD;
784 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700785 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700786
787 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700788 if (err)
789 return (u32)-1;
790 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700791 return (u32)-1;
792
793 memset(&cmd, 0, sizeof(struct mmc_command));
794
795 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
796 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700797 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700798
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700799 data.blksz = 4;
800 data.blocks = 1;
801 data.flags = MMC_DATA_READ;
802 data.sg = &sg;
803 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530804 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700805
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700806 mrq.cmd = &cmd;
807 mrq.data = &data;
808
Ben Dooks051913d2009-06-08 23:33:57 +0100809 blocks = kmalloc(4, GFP_KERNEL);
810 if (!blocks)
811 return (u32)-1;
812
813 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700814
815 mmc_wait_for_req(card->host, &mrq);
816
Ben Dooks051913d2009-06-08 23:33:57 +0100817 result = ntohl(*blocks);
818 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819
Ben Dooks051913d2009-06-08 23:33:57 +0100820 if (cmd.error || data.error)
821 result = (u32)-1;
822
823 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700824}
825
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100826static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300827{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900828 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300829 int err;
830
Adrian Hunter504f1912008-10-16 12:55:25 +0300831 cmd.opcode = MMC_SEND_STATUS;
832 if (!mmc_host_is_spi(card->host))
833 cmd.arg = card->rca << 16;
834 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100835 err = mmc_wait_for_cmd(card->host, &cmd, retries);
836 if (err == 0)
837 *status = cmd.resp[0];
838 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300839}
840
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100841static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100842 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100843{
844 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
845 int err = 0;
846 u32 status;
847
848 do {
849 err = get_card_status(card, &status, 5);
850 if (err) {
851 pr_err("%s: error %d requesting status\n",
852 req->rq_disk->disk_name, err);
853 return err;
854 }
855
856 if (status & R1_ERROR) {
857 pr_err("%s: %s: error sending status cmd, status %#x\n",
858 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100859 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100860 }
861
Ulf Hansson95a91292014-01-29 13:11:27 +0100862 /* We may rely on the host hw to handle busy detection.*/
863 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
864 hw_busy_detect)
865 break;
866
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100867 /*
868 * Timeout if the device never becomes ready for data and never
869 * leaves the program state.
870 */
871 if (time_after(jiffies, timeout)) {
872 pr_err("%s: Card stuck in programming state! %s %s\n",
873 mmc_hostname(card->host),
874 req->rq_disk->disk_name, __func__);
875 return -ETIMEDOUT;
876 }
877
878 /*
879 * Some cards mishandle the status bits,
880 * so make sure to check both the busy
881 * indication and the card state.
882 */
883 } while (!(status & R1_READY_FOR_DATA) ||
884 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
885
886 return err;
887}
888
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100889static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100890 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100891{
892 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900893 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100894 int err;
895 bool use_r1b_resp = rq_data_dir(req) == WRITE;
896
897 /*
898 * Normally we use R1B responses for WRITE, but in cases where the host
899 * has specified a max_busy_timeout we need to validate it. A failure
900 * means we need to prevent the host from doing hw busy detection, which
901 * is done by converting to a R1 response instead.
902 */
903 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
904 use_r1b_resp = false;
905
906 cmd.opcode = MMC_STOP_TRANSMISSION;
907 if (use_r1b_resp) {
908 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
909 cmd.busy_timeout = timeout_ms;
910 } else {
911 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
912 }
913
914 err = mmc_wait_for_cmd(host, &cmd, 5);
915 if (err)
916 return err;
917
918 *stop_status = cmd.resp[0];
919
920 /* No need to check card status in case of READ. */
921 if (rq_data_dir(req) == READ)
922 return 0;
923
924 if (!mmc_host_is_spi(host) &&
925 (*stop_status & R1_ERROR)) {
926 pr_err("%s: %s: general error sending stop command, resp %#x\n",
927 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100928 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100929 }
930
931 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
932}
933
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530934#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100935#define ERR_RETRY 2
936#define ERR_ABORT 1
937#define ERR_CONTINUE 0
938
939static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
940 bool status_valid, u32 status)
941{
942 switch (error) {
943 case -EILSEQ:
944 /* response crc error, retry the r/w cmd */
945 pr_err("%s: %s sending %s command, card status %#x\n",
946 req->rq_disk->disk_name, "response CRC error",
947 name, status);
948 return ERR_RETRY;
949
950 case -ETIMEDOUT:
951 pr_err("%s: %s sending %s command, card status %#x\n",
952 req->rq_disk->disk_name, "timed out", name, status);
953
954 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530955 if (!status_valid) {
956 pr_err("%s: status not valid, retrying timeout\n",
957 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100958 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530959 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100960
961 /*
962 * If it was a r/w cmd crc error, or illegal command
963 * (eg, issued in wrong state) then retry - we should
964 * have corrected the state problem above.
965 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530966 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
967 pr_err("%s: command error, retrying timeout\n",
968 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100969 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530970 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100971
972 /* Otherwise abort the command */
973 return ERR_ABORT;
974
975 default:
976 /* We don't understand the error code the driver gave us */
977 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
978 req->rq_disk->disk_name, error, status);
979 return ERR_ABORT;
980 }
981}
982
983/*
984 * Initial r/w and stop cmd error recovery.
985 * We don't know whether the card received the r/w cmd or not, so try to
986 * restore things back to a sane state. Essentially, we do this as follows:
987 * - Obtain card status. If the first attempt to obtain card status fails,
988 * the status word will reflect the failed status cmd, not the failed
989 * r/w cmd. If we fail to obtain card status, it suggests we can no
990 * longer communicate with the card.
991 * - Check the card state. If the card received the cmd but there was a
992 * transient problem with the response, it might still be in a data transfer
993 * mode. Try to send it a stop command. If this fails, we can't recover.
994 * - If the r/w cmd failed due to a response CRC error, it was probably
995 * transient, so retry the cmd.
996 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
997 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
998 * illegal cmd, retry.
999 * Otherwise we don't understand what happened, so abort.
1000 */
1001static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001002 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001003{
1004 bool prev_cmd_status_valid = true;
1005 u32 status, stop_status = 0;
1006 int err, retry;
1007
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301008 if (mmc_card_removed(card))
1009 return ERR_NOMEDIUM;
1010
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001011 /*
1012 * Try to get card status which indicates both the card state
1013 * and why there was no response. If the first attempt fails,
1014 * we can't be sure the returned status is for the r/w command.
1015 */
1016 for (retry = 2; retry >= 0; retry--) {
1017 err = get_card_status(card, &status, 0);
1018 if (!err)
1019 break;
1020
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001021 /* Re-tune if needed */
1022 mmc_retune_recheck(card->host);
1023
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001024 prev_cmd_status_valid = false;
1025 pr_err("%s: error %d sending status command, %sing\n",
1026 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1027 }
1028
1029 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301030 if (err) {
1031 /* Check if the card is removed */
1032 if (mmc_detect_card_removed(card->host))
1033 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001034 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301035 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001036
Adrian Hunter67716322011-08-29 16:42:15 +03001037 /* Flag ECC errors */
1038 if ((status & R1_CARD_ECC_FAILED) ||
1039 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1040 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001041 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001042
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001043 /* Flag General errors */
1044 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1045 if ((status & R1_ERROR) ||
1046 (brq->stop.resp[0] & R1_ERROR)) {
1047 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1048 req->rq_disk->disk_name, __func__,
1049 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001050 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001051 }
1052
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053 /*
1054 * Check the current card state. If it is in some data transfer
1055 * mode, tell it to stop (and hopefully transition back to TRAN.)
1056 */
1057 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1058 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001059 err = send_stop(card,
1060 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1061 req, gen_err, &stop_status);
1062 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001063 pr_err("%s: error %d sending stop command\n",
1064 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001065 /*
1066 * If the stop cmd also timed out, the card is probably
1067 * not present, so abort. Other errors are bad news too.
1068 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001070 }
1071
Adrian Hunter67716322011-08-29 16:42:15 +03001072 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001073 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001074 }
1075
1076 /* Check for set block count errors */
1077 if (brq->sbc.error)
1078 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1079 prev_cmd_status_valid, status);
1080
1081 /* Check for r/w command errors */
1082 if (brq->cmd.error)
1083 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1084 prev_cmd_status_valid, status);
1085
Adrian Hunter67716322011-08-29 16:42:15 +03001086 /* Data errors */
1087 if (!brq->stop.error)
1088 return ERR_CONTINUE;
1089
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001090 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001091 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 +01001092 req->rq_disk->disk_name, brq->stop.error,
1093 brq->cmd.resp[0], status);
1094
1095 /*
1096 * Subsitute in our own stop status as this will give the error
1097 * state which happened during the execution of the r/w command.
1098 */
1099 if (stop_status) {
1100 brq->stop.resp[0] = stop_status;
1101 brq->stop.error = 0;
1102 }
1103 return ERR_CONTINUE;
1104}
1105
Adrian Hunter67716322011-08-29 16:42:15 +03001106static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1107 int type)
1108{
1109 int err;
1110
1111 if (md->reset_done & type)
1112 return -EEXIST;
1113
1114 md->reset_done |= type;
1115 err = mmc_hw_reset(host);
1116 /* Ensure we switch back to the correct partition */
1117 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001118 struct mmc_blk_data *main_md =
1119 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001120 int part_err;
1121
1122 main_md->part_curr = main_md->part_type;
1123 part_err = mmc_blk_part_switch(host->card, md);
1124 if (part_err) {
1125 /*
1126 * We have failed to get back into the correct
1127 * partition, so we need to abort the whole request.
1128 */
1129 return -ENODEV;
1130 }
1131 }
1132 return err;
1133}
1134
1135static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1136{
1137 md->reset_done &= ~type;
1138}
1139
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001140int mmc_access_rpmb(struct mmc_queue *mq)
1141{
Linus Walleij7db30282016-11-18 13:36:15 +01001142 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001143 /*
1144 * If this is a RPMB partition access, return ture
1145 */
1146 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1147 return true;
1148
1149 return false;
1150}
1151
Linus Walleijdf061582017-01-24 11:17:57 +01001152static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001153{
Linus Walleij7db30282016-11-18 13:36:15 +01001154 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001155 struct mmc_card *card = md->queue.card;
1156 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001157 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001158
Adrian Hunterbd788c92010-08-11 14:17:47 -07001159 if (!mmc_can_erase(card)) {
1160 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001161 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001162 }
1163
1164 from = blk_rq_pos(req);
1165 nr = blk_rq_sectors(req);
1166
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001167 if (mmc_can_discard(card))
1168 arg = MMC_DISCARD_ARG;
1169 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001170 arg = MMC_TRIM_ARG;
1171 else
1172 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001173 do {
1174 err = 0;
1175 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1176 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1177 INAND_CMD38_ARG_EXT_CSD,
1178 arg == MMC_TRIM_ARG ?
1179 INAND_CMD38_ARG_TRIM :
1180 INAND_CMD38_ARG_ERASE,
1181 0);
1182 }
1183 if (!err)
1184 err = mmc_erase(card, from, nr, arg);
1185 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001186 if (!err)
1187 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001188fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301189 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001190}
1191
Linus Walleijdf061582017-01-24 11:17:57 +01001192static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001193 struct request *req)
1194{
Linus Walleij7db30282016-11-18 13:36:15 +01001195 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001196 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001197 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001198 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001199
Maya Erez775a9362013-04-18 15:41:55 +03001200 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001201 err = -EOPNOTSUPP;
1202 goto out;
1203 }
1204
1205 from = blk_rq_pos(req);
1206 nr = blk_rq_sectors(req);
1207
Maya Erez775a9362013-04-18 15:41:55 +03001208 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1209 arg = MMC_SECURE_TRIM1_ARG;
1210 else
1211 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001212
Adrian Hunter67716322011-08-29 16:42:15 +03001213retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001214 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1215 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1216 INAND_CMD38_ARG_EXT_CSD,
1217 arg == MMC_SECURE_TRIM1_ARG ?
1218 INAND_CMD38_ARG_SECTRIM1 :
1219 INAND_CMD38_ARG_SECERASE,
1220 0);
1221 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001222 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001223 }
Adrian Hunter28302812012-04-05 14:45:48 +03001224
Adrian Hunter49804542010-08-11 14:17:50 -07001225 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001226 if (err == -EIO)
1227 goto out_retry;
1228 if (err)
1229 goto out;
1230
1231 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001232 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1233 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1234 INAND_CMD38_ARG_EXT_CSD,
1235 INAND_CMD38_ARG_SECTRIM2,
1236 0);
1237 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001238 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001239 }
Adrian Hunter28302812012-04-05 14:45:48 +03001240
Adrian Hunter49804542010-08-11 14:17:50 -07001241 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001242 if (err == -EIO)
1243 goto out_retry;
1244 if (err)
1245 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001246 }
Adrian Hunter28302812012-04-05 14:45:48 +03001247
Adrian Hunter28302812012-04-05 14:45:48 +03001248out_retry:
1249 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001250 goto retry;
1251 if (!err)
1252 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001253out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301254 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001255}
1256
Linus Walleijdf061582017-01-24 11:17:57 +01001257static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001258{
Linus Walleij7db30282016-11-18 13:36:15 +01001259 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001260 struct mmc_card *card = md->queue.card;
1261 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001262
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001263 ret = mmc_flush_cache(card);
1264 if (ret)
1265 ret = -EIO;
1266
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301267 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001268}
1269
1270/*
1271 * Reformat current write as a reliable write, supporting
1272 * both legacy and the enhanced reliable write MMC cards.
1273 * In each transfer we'll handle only as much as a single
1274 * reliable write can handle, thus finish the request in
1275 * partial completions.
1276 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001277static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1278 struct mmc_card *card,
1279 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001280{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001281 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1282 /* Legacy mode imposes restrictions on transfers. */
1283 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1284 brq->data.blocks = 1;
1285
1286 if (brq->data.blocks > card->ext_csd.rel_sectors)
1287 brq->data.blocks = card->ext_csd.rel_sectors;
1288 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1289 brq->data.blocks = 1;
1290 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001291}
1292
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001293#define CMD_ERRORS \
1294 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1295 R1_ADDRESS_ERROR | /* Misaligned address */ \
1296 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1297 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1298 R1_CC_ERROR | /* Card controller error */ \
1299 R1_ERROR) /* General/unknown error */
1300
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001301static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1302 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001303{
Per Forlinee8a43a2011-07-01 18:55:33 +02001304 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1305 mmc_active);
1306 struct mmc_blk_request *brq = &mq_mrq->brq;
1307 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001308 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001309 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001310 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001311
1312 /*
1313 * sbc.error indicates a problem with the set block count
1314 * command. No data will have been transferred.
1315 *
1316 * cmd.error indicates a problem with the r/w command. No
1317 * data will have been transferred.
1318 *
1319 * stop.error indicates a problem with the stop command. Data
1320 * may have been transferred, or may still be transferring.
1321 */
Adrian Hunter67716322011-08-29 16:42:15 +03001322 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1323 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001324 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001325 case ERR_RETRY:
1326 return MMC_BLK_RETRY;
1327 case ERR_ABORT:
1328 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301329 case ERR_NOMEDIUM:
1330 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001331 case ERR_CONTINUE:
1332 break;
1333 }
1334 }
1335
1336 /*
1337 * Check for errors relating to the execution of the
1338 * initial command - such as address errors. No data
1339 * has been transferred.
1340 */
1341 if (brq->cmd.resp[0] & CMD_ERRORS) {
1342 pr_err("%s: r/w command failed, status = %#x\n",
1343 req->rq_disk->disk_name, brq->cmd.resp[0]);
1344 return MMC_BLK_ABORT;
1345 }
1346
1347 /*
1348 * Everything else is either success, or a data error of some
1349 * kind. If it was a write, we may have transitioned to
1350 * program mode, which we have to wait for it to complete.
1351 */
1352 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001353 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001354
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001355 /* Check stop command response */
1356 if (brq->stop.resp[0] & R1_ERROR) {
1357 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1358 req->rq_disk->disk_name, __func__,
1359 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001360 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001361 }
1362
Ulf Hansson95a91292014-01-29 13:11:27 +01001363 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1364 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001365 if (err)
1366 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001367 }
1368
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001369 /* if general error occurs, retry the write operation. */
1370 if (gen_err) {
1371 pr_warn("%s: retrying write for general error\n",
1372 req->rq_disk->disk_name);
1373 return MMC_BLK_RETRY;
1374 }
1375
Per Forlind78d4a8a2011-07-01 18:55:30 +02001376 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001377 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001378 pr_debug("%s: retrying because a re-tune was needed\n",
1379 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001380 brq->retune_retry_done = 1;
1381 return MMC_BLK_RETRY;
1382 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001383 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1384 req->rq_disk->disk_name, brq->data.error,
1385 (unsigned)blk_rq_pos(req),
1386 (unsigned)blk_rq_sectors(req),
1387 brq->cmd.resp[0], brq->stop.resp[0]);
1388
1389 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001390 if (ecc_err)
1391 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001392 return MMC_BLK_DATA_ERR;
1393 } else {
1394 return MMC_BLK_CMD_ERR;
1395 }
1396 }
1397
Adrian Hunter67716322011-08-29 16:42:15 +03001398 if (!brq->data.bytes_xfered)
1399 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001400
Adrian Hunter67716322011-08-29 16:42:15 +03001401 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1402 return MMC_BLK_PARTIAL;
1403
1404 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001405}
1406
Per Forlin54d49d72011-07-01 18:55:29 +02001407static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1408 struct mmc_card *card,
1409 int disable_multi,
1410 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Per Forlin54d49d72011-07-01 18:55:29 +02001412 u32 readcmd, writecmd;
1413 struct mmc_blk_request *brq = &mqrq->brq;
1414 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001415 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301416 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001418 /*
1419 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001420 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001421 */
Luca Porziod3df0462015-11-06 15:12:26 +00001422 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001423 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001424 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001425
Per Forlin54d49d72011-07-01 18:55:29 +02001426 memset(brq, 0, sizeof(struct mmc_blk_request));
1427 brq->mrq.cmd = &brq->cmd;
1428 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Per Forlin54d49d72011-07-01 18:55:29 +02001430 brq->cmd.arg = blk_rq_pos(req);
1431 if (!mmc_card_blockaddr(card))
1432 brq->cmd.arg <<= 9;
1433 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1434 brq->data.blksz = 512;
1435 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1436 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001437 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Per Forlin54d49d72011-07-01 18:55:29 +02001439 /*
1440 * The block layer doesn't support all sector count
1441 * restrictions, so we need to be prepared for too big
1442 * requests.
1443 */
1444 if (brq->data.blocks > card->host->max_blk_count)
1445 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001447 if (brq->data.blocks > 1) {
1448 /*
1449 * After a read error, we redo the request one sector
1450 * at a time in order to accurately determine which
1451 * sectors can be read successfully.
1452 */
1453 if (disable_multi)
1454 brq->data.blocks = 1;
1455
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001456 /*
1457 * Some controllers have HW issues while operating
1458 * in multiple I/O mode
1459 */
1460 if (card->host->ops->multi_io_quirk)
1461 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1462 (rq_data_dir(req) == READ) ?
1463 MMC_DATA_READ : MMC_DATA_WRITE,
1464 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001465 }
Per Forlin54d49d72011-07-01 18:55:29 +02001466
1467 if (brq->data.blocks > 1 || do_rel_wr) {
1468 /* SPI multiblock writes terminate using a special
1469 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001470 */
Per Forlin54d49d72011-07-01 18:55:29 +02001471 if (!mmc_host_is_spi(card->host) ||
1472 rq_data_dir(req) == READ)
1473 brq->mrq.stop = &brq->stop;
1474 readcmd = MMC_READ_MULTIPLE_BLOCK;
1475 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1476 } else {
1477 brq->mrq.stop = NULL;
1478 readcmd = MMC_READ_SINGLE_BLOCK;
1479 writecmd = MMC_WRITE_BLOCK;
1480 }
1481 if (rq_data_dir(req) == READ) {
1482 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001483 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001484 if (brq->mrq.stop)
1485 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1486 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001487 } else {
1488 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001489 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001490 if (brq->mrq.stop)
1491 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1492 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001493 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001494
Per Forlin54d49d72011-07-01 18:55:29 +02001495 if (do_rel_wr)
1496 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001497
Per Forlin54d49d72011-07-01 18:55:29 +02001498 /*
Saugata Das42659002011-12-21 13:09:17 +05301499 * Data tag is used only during writing meta data to speed
1500 * up write and any subsequent read of this meta data
1501 */
1502 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1503 (req->cmd_flags & REQ_META) &&
1504 (rq_data_dir(req) == WRITE) &&
1505 ((brq->data.blocks * brq->data.blksz) >=
1506 card->ext_csd.data_tag_unit_size);
1507
1508 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001509 * Pre-defined multi-block transfers are preferable to
1510 * open ended-ones (and necessary for reliable writes).
1511 * However, it is not sufficient to just send CMD23,
1512 * and avoid the final CMD12, as on an error condition
1513 * CMD12 (stop) needs to be sent anyway. This, coupled
1514 * with Auto-CMD23 enhancements provided by some
1515 * hosts, means that the complexity of dealing
1516 * with this is best left to the host. If CMD23 is
1517 * supported by card and host, we'll fill sbc in and let
1518 * the host deal with handling it correctly. This means
1519 * that for hosts that don't expose MMC_CAP_CMD23, no
1520 * change of behavior will be observed.
1521 *
1522 * N.B: Some MMC cards experience perf degradation.
1523 * We'll avoid using CMD23-bounded multiblock writes for
1524 * these, while retaining features like reliable writes.
1525 */
Saugata Das42659002011-12-21 13:09:17 +05301526 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1527 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1528 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001529 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1530 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301531 (do_rel_wr ? (1 << 31) : 0) |
1532 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001533 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1534 brq->mrq.sbc = &brq->sbc;
1535 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001536
Per Forlin54d49d72011-07-01 18:55:29 +02001537 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001538
Per Forlin54d49d72011-07-01 18:55:29 +02001539 brq->data.sg = mqrq->sg;
1540 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001541
Per Forlin54d49d72011-07-01 18:55:29 +02001542 /*
1543 * Adjust the sg list so it is the same size as the
1544 * request.
1545 */
1546 if (brq->data.blocks != blk_rq_sectors(req)) {
1547 int i, data_size = brq->data.blocks << 9;
1548 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001549
Per Forlin54d49d72011-07-01 18:55:29 +02001550 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1551 data_size -= sg->length;
1552 if (data_size <= 0) {
1553 sg->length += data_size;
1554 i++;
1555 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001556 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001557 }
Per Forlin54d49d72011-07-01 18:55:29 +02001558 brq->data.sg_len = i;
1559 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001560
Per Forlinee8a43a2011-07-01 18:55:33 +02001561 mqrq->mmc_active.mrq = &brq->mrq;
1562 mqrq->mmc_active.err_check = mmc_blk_err_check;
1563
Per Forlin54d49d72011-07-01 18:55:29 +02001564 mmc_queue_bounce_pre(mqrq);
1565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Adrian Hunter67716322011-08-29 16:42:15 +03001567static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1568 struct mmc_blk_request *brq, struct request *req,
1569 int ret)
1570{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001571 struct mmc_queue_req *mq_rq;
1572 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1573
Adrian Hunter67716322011-08-29 16:42:15 +03001574 /*
1575 * If this is an SD card and we're writing, we can first
1576 * mark the known good sectors as ok.
1577 *
1578 * If the card is not SD, we can still ok written sectors
1579 * as reported by the controller (which might be less than
1580 * the real number of written sectors, but never more).
1581 */
1582 if (mmc_card_sd(card)) {
1583 u32 blocks;
1584
1585 blocks = mmc_sd_num_wr_blocks(card);
1586 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301587 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001588 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001589 } else {
1590 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001591 }
1592 return ret;
1593}
1594
Linus Walleij4e1f7802017-01-24 11:17:52 +01001595static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1596{
1597 int ret = 1;
1598
1599 if (mmc_card_removed(card))
1600 req->rq_flags |= RQF_QUIET;
1601 while (ret)
1602 ret = blk_end_request(req, -EIO,
1603 blk_rq_cur_bytes(req));
1604}
1605
Linus Walleijefb5a052017-01-24 11:17:53 +01001606static void mmc_blk_rw_start_new(struct mmc_queue *mq, struct mmc_card *card,
1607 struct request *req)
1608{
1609 if (!req)
1610 return;
1611
1612 if (mmc_card_removed(card)) {
1613 req->rq_flags |= RQF_QUIET;
1614 blk_end_request_all(req, -EIO);
1615 } else {
1616 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1617 mmc_start_req(card->host,
1618 &mq->mqrq_cur->mmc_active, NULL);
1619 }
1620}
1621
Linus Walleijdf061582017-01-24 11:17:57 +01001622static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001623{
Linus Walleij7db30282016-11-18 13:36:15 +01001624 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001625 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001626 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001627 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001628 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001629 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001630 struct request *req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001631 struct mmc_async_req *new_areq;
1632 struct mmc_async_req *old_areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001633
1634 if (!rqc && !mq->mqrq_prev->req)
Linus Walleijdf061582017-01-24 11:17:57 +01001635 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001636
1637 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001638 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301639 /*
1640 * When 4KB native sector is enabled, only 8 blocks
1641 * multiple read or write is allowed
1642 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001643 if (mmc_large_sector(card) &&
1644 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301645 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001646 rqc->rq_disk->disk_name);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001647 mmc_blk_rw_cmd_abort(card, rqc);
Linus Walleijdf061582017-01-24 11:17:57 +01001648 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301649 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001650
Linus Walleij03d640a2016-11-25 10:35:00 +01001651 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Linus Walleij7d552a42017-01-24 11:17:56 +01001652 new_areq = &mq->mqrq_cur->mmc_active;
Per Forlinee8a43a2011-07-01 18:55:33 +02001653 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001654 new_areq = NULL;
1655
1656 old_areq = mmc_start_req(card->host, new_areq, &status);
1657 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001658 /*
1659 * We have just put the first request into the pipeline
1660 * and there is nothing more to do until it is
1661 * complete.
1662 */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001663 if (status == MMC_BLK_NEW_REQUEST)
1664 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Linus Walleijdf061582017-01-24 11:17:57 +01001665 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001666 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001667
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001668 /*
1669 * An asynchronous request has been completed and we proceed
1670 * to handle the result of it.
1671 */
Linus Walleij7d552a42017-01-24 11:17:56 +01001672 mq_rq = container_of(old_areq, struct mmc_queue_req, mmc_active);
Per Forlinee8a43a2011-07-01 18:55:33 +02001673 brq = &mq_rq->brq;
1674 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001675 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001676 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001677
Per Forlind78d4a8a2011-07-01 18:55:30 +02001678 switch (status) {
1679 case MMC_BLK_SUCCESS:
1680 case MMC_BLK_PARTIAL:
1681 /*
1682 * A block was successfully transferred.
1683 */
Adrian Hunter67716322011-08-29 16:42:15 +03001684 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001685
Linus Walleij03d640a2016-11-25 10:35:00 +01001686 ret = blk_end_request(req, 0,
1687 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001688
Adrian Hunter67716322011-08-29 16:42:15 +03001689 /*
1690 * If the blk_end_request function returns non-zero even
1691 * though all data has been transferred and no errors
1692 * were returned by the host controller, it's a bug.
1693 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001694 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301695 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001696 __func__, blk_rq_bytes(req),
1697 brq->data.bytes_xfered);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001698 mmc_blk_rw_cmd_abort(card, req);
Linus Walleijdf061582017-01-24 11:17:57 +01001699 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001700 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001701 break;
1702 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001703 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08001704 if (mmc_blk_reset(md, card->host, type))
1705 goto cmd_abort;
1706 if (!ret)
1707 goto start_new_req;
1708 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001709 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001710 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001711 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001712 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001713 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001714 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001715 if (!mmc_blk_reset(md, card->host, type))
1716 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001717 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001718 case MMC_BLK_DATA_ERR: {
1719 int err;
1720
1721 err = mmc_blk_reset(md, card->host, type);
1722 if (!err)
1723 break;
Linus Walleij03d640a2016-11-25 10:35:00 +01001724 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001725 goto cmd_abort;
1726 /* Fall through */
1727 }
1728 case MMC_BLK_ECC_ERR:
1729 if (brq->data.blocks > 1) {
1730 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001731 pr_warn("%s: retrying using single block read\n",
1732 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001733 disable_multi = 1;
1734 break;
1735 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001736 /*
1737 * After an error, we redo I/O one sector at a
1738 * time, so we only reach here after trying to
1739 * read a single sector.
1740 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301741 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001742 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001743 if (!ret)
1744 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001745 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301746 case MMC_BLK_NOMEDIUM:
1747 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001748 default:
1749 pr_err("%s: Unhandled return value (%d)",
1750 req->rq_disk->disk_name, status);
1751 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001752 }
1753
Per Forlinee8a43a2011-07-01 18:55:33 +02001754 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001755 /*
1756 * In case of a incomplete request
1757 * prepare it again and resend.
1758 */
1759 mmc_blk_rw_rq_prep(mq_rq, card,
1760 disable_multi, mq);
1761 mmc_start_req(card->host,
1762 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001763 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } while (ret);
1766
Linus Walleijdf061582017-01-24 11:17:57 +01001767 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001769 cmd_abort:
Linus Walleij4e1f7802017-01-24 11:17:52 +01001770 mmc_blk_rw_cmd_abort(card, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Per Forlinee8a43a2011-07-01 18:55:33 +02001772 start_new_req:
Linus Walleijefb5a052017-01-24 11:17:53 +01001773 mmc_blk_rw_start_new(mq, card, rqc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Linus Walleijdf061582017-01-24 11:17:57 +01001776void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001777{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001778 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001779 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001780 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001781 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001782
Per Forlinee8a43a2011-07-01 18:55:33 +02001783 if (req && !mq->mqrq_prev->req)
1784 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001785 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001786
Andrei Warkentin371a6892011-04-11 18:10:25 -05001787 ret = mmc_blk_part_switch(card, md);
1788 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001789 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301790 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001791 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001792 goto out;
1793 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001794
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001795 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001796 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001797 /* complete ongoing async transfer before issuing discard */
1798 if (card->host->areq)
1799 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001800 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001801 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1802 /* complete ongoing async transfer before issuing secure erase*/
1803 if (card->host->areq)
1804 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001805 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001806 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001807 /* complete ongoing async transfer before issuing flush */
1808 if (card->host->areq)
1809 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001810 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001811 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001812 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001813 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001814
Andrei Warkentin371a6892011-04-11 18:10:25 -05001815out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001816 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001817 /*
1818 * Release host when there are no more requests
1819 * and after special request(discard, flush) is done.
1820 * In case sepecial request, there is no reentry to
1821 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1822 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001823 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Russell Kinga6f6c962006-01-03 22:38:44 +00001826static inline int mmc_blk_readonly(struct mmc_card *card)
1827{
1828 return mmc_card_readonly(card) ||
1829 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1830}
1831
Andrei Warkentin371a6892011-04-11 18:10:25 -05001832static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1833 struct device *parent,
1834 sector_t size,
1835 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001836 const char *subname,
1837 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 struct mmc_blk_data *md;
1840 int devidx, ret;
1841
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001842again:
1843 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
1844 return ERR_PTR(-ENOMEM);
1845
1846 spin_lock(&mmc_blk_lock);
1847 ret = ida_get_new(&mmc_blk_ida, &devidx);
1848 spin_unlock(&mmc_blk_lock);
1849
1850 if (ret == -EAGAIN)
1851 goto again;
1852 else if (ret)
1853 return ERR_PTR(ret);
1854
1855 if (devidx >= max_devices) {
1856 ret = -ENOSPC;
1857 goto out;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001860 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001861 if (!md) {
1862 ret = -ENOMEM;
1863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001865
Johan Rudholmadd710e2011-12-02 08:51:06 +01001866 md->area_type = area_type;
1867
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001868 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001869 * Set the read-only status based on the supported commands
1870 * and the write protect switch.
1871 */
1872 md->read_only = mmc_blk_readonly(card);
1873
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001874 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001875 if (md->disk == NULL) {
1876 ret = -ENOMEM;
1877 goto err_kfree;
1878 }
1879
1880 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001881 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001882 md->usage = 1;
1883
Adrian Hunterd09408a2011-06-23 13:40:28 +03001884 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001885 if (ret)
1886 goto err_putdisk;
1887
Linus Walleij7db30282016-11-18 13:36:15 +01001888 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001889
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001890 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001891 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001892 md->disk->fops = &mmc_bdops;
1893 md->disk->private_data = md;
1894 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001895 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001896 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001897 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001898 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001899 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001900
1901 /*
1902 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1903 *
1904 * - be set for removable media with permanent block devices
1905 * - be unset for removable block devices with permanent media
1906 *
1907 * Since MMC block devices clearly fall under the second
1908 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1909 * should use the block device creation/destruction hotplug
1910 * messages to tell when the card is present.
1911 */
1912
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001913 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001914 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001915
Saugata Dasa5075eb2012-05-17 16:32:21 +05301916 if (mmc_card_mmc(card))
1917 blk_queue_logical_block_size(md->queue.queue,
1918 card->ext_csd.data_sector_size);
1919 else
1920 blk_queue_logical_block_size(md->queue.queue, 512);
1921
Andrei Warkentin371a6892011-04-11 18:10:25 -05001922 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001923
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001924 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001925 if ((mmc_card_mmc(card) &&
1926 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001927 (mmc_card_sd(card) &&
1928 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1929 md->flags |= MMC_BLK_CMD23;
1930 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001931
1932 if (mmc_card_mmc(card) &&
1933 md->flags & MMC_BLK_CMD23 &&
1934 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1935 card->ext_csd.rel_sectors)) {
1936 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001937 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001938 }
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001941
1942 err_putdisk:
1943 put_disk(md->disk);
1944 err_kfree:
1945 kfree(md);
1946 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001947 spin_lock(&mmc_blk_lock);
1948 ida_remove(&mmc_blk_ida, devidx);
1949 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00001950 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Andrei Warkentin371a6892011-04-11 18:10:25 -05001953static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1954{
1955 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001956
1957 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1958 /*
1959 * The EXT_CSD sector count is in number or 512 byte
1960 * sectors.
1961 */
1962 size = card->ext_csd.sectors;
1963 } else {
1964 /*
1965 * The CSD capacity field is in units of read_blkbits.
1966 * set_capacity takes units of 512 bytes.
1967 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001968 size = (typeof(sector_t))card->csd.capacity
1969 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001970 }
1971
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001972 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001973 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001974}
1975
1976static int mmc_blk_alloc_part(struct mmc_card *card,
1977 struct mmc_blk_data *md,
1978 unsigned int part_type,
1979 sector_t size,
1980 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001981 const char *subname,
1982 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001983{
1984 char cap_str[10];
1985 struct mmc_blk_data *part_md;
1986
1987 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001988 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001989 if (IS_ERR(part_md))
1990 return PTR_ERR(part_md);
1991 part_md->part_type = part_type;
1992 list_add(&part_md->part, &md->part);
1993
James Bottomleyb9f28d82015-03-05 18:47:01 -08001994 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001995 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301996 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001997 part_md->disk->disk_name, mmc_card_id(card),
1998 mmc_card_name(card), part_md->part_type, cap_str);
1999 return 0;
2000}
2001
Namjae Jeone0c368d2011-10-06 23:41:38 +09002002/* MMC Physical partitions consist of two boot partitions and
2003 * up to four general purpose partitions.
2004 * For each partition enabled in EXT_CSD a block device will be allocatedi
2005 * to provide access to the partition.
2006 */
2007
Andrei Warkentin371a6892011-04-11 18:10:25 -05002008static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2009{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002010 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002011
2012 if (!mmc_card_mmc(card))
2013 return 0;
2014
Namjae Jeone0c368d2011-10-06 23:41:38 +09002015 for (idx = 0; idx < card->nr_parts; idx++) {
2016 if (card->part[idx].size) {
2017 ret = mmc_blk_alloc_part(card, md,
2018 card->part[idx].part_cfg,
2019 card->part[idx].size >> 9,
2020 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002021 card->part[idx].name,
2022 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002023 if (ret)
2024 return ret;
2025 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002026 }
2027
2028 return ret;
2029}
2030
Andrei Warkentin371a6892011-04-11 18:10:25 -05002031static void mmc_blk_remove_req(struct mmc_blk_data *md)
2032{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002033 struct mmc_card *card;
2034
Andrei Warkentin371a6892011-04-11 18:10:25 -05002035 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002036 /*
2037 * Flush remaining requests and free queues. It
2038 * is freeing the queue that stops new requests
2039 * from being accepted.
2040 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002041 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002042 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002043 if (md->disk->flags & GENHD_FL_UP) {
2044 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002045 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2046 card->ext_csd.boot_ro_lockable)
2047 device_remove_file(disk_to_dev(md->disk),
2048 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002049
Andrei Warkentin371a6892011-04-11 18:10:25 -05002050 del_gendisk(md->disk);
2051 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002052 mmc_blk_put(md);
2053 }
2054}
2055
2056static void mmc_blk_remove_parts(struct mmc_card *card,
2057 struct mmc_blk_data *md)
2058{
2059 struct list_head *pos, *q;
2060 struct mmc_blk_data *part_md;
2061
2062 list_for_each_safe(pos, q, &md->part) {
2063 part_md = list_entry(pos, struct mmc_blk_data, part);
2064 list_del(pos);
2065 mmc_blk_remove_req(part_md);
2066 }
2067}
2068
2069static int mmc_add_disk(struct mmc_blk_data *md)
2070{
2071 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002072 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002073
Dan Williams307d8e62016-06-20 10:40:44 -07002074 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002075 md->force_ro.show = force_ro_show;
2076 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302077 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002078 md->force_ro.attr.name = "force_ro";
2079 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2080 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2081 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002082 goto force_ro_fail;
2083
2084 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2085 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002086 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002087
2088 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2089 mode = S_IRUGO;
2090 else
2091 mode = S_IRUGO | S_IWUSR;
2092
2093 md->power_ro_lock.show = power_ro_lock_show;
2094 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002095 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002096 md->power_ro_lock.attr.mode = mode;
2097 md->power_ro_lock.attr.name =
2098 "ro_lock_until_next_power_on";
2099 ret = device_create_file(disk_to_dev(md->disk),
2100 &md->power_ro_lock);
2101 if (ret)
2102 goto power_ro_lock_fail;
2103 }
2104 return ret;
2105
2106power_ro_lock_fail:
2107 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2108force_ro_fail:
2109 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002110
2111 return ret;
2112}
2113
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002114static const struct mmc_fixup blk_fixups[] =
2115{
Chris Ballc59d4472011-11-11 22:01:43 -05002116 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2117 MMC_QUIRK_INAND_CMD38),
2118 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2119 MMC_QUIRK_INAND_CMD38),
2120 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2121 MMC_QUIRK_INAND_CMD38),
2122 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2123 MMC_QUIRK_INAND_CMD38),
2124 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2125 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002126
2127 /*
2128 * Some MMC cards experience performance degradation with CMD23
2129 * instead of CMD12-bounded multiblock transfers. For now we'll
2130 * black list what's bad...
2131 * - Certain Toshiba cards.
2132 *
2133 * N.B. This doesn't affect SD cards.
2134 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002135 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2136 MMC_QUIRK_BLK_NO_CMD23),
2137 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2138 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002139 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002140 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002141 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002142 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002143 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002144 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002145
2146 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002147 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002148 */
Chris Ballc59d4472011-11-11 22:01:43 -05002149 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002150 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002151 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2152 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002153
Ian Chen3550ccd2012-08-29 15:05:36 +09002154 /*
2155 * On these Samsung MoviNAND parts, performing secure erase or
2156 * secure trim can result in unrecoverable corruption due to a
2157 * firmware bug.
2158 */
2159 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2160 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2161 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2162 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2163 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2164 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2165 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2166 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2167 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2168 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2169 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2170 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2171 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2172 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2173 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2174 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2175
Shawn Linb5b4ff02015-08-12 13:08:32 +08002176 /*
2177 * On Some Kingston eMMCs, performing trim can result in
2178 * unrecoverable data conrruption occasionally due to a firmware bug.
2179 */
2180 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2181 MMC_QUIRK_TRIM_BROKEN),
2182 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2183 MMC_QUIRK_TRIM_BROKEN),
2184
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002185 END_FIXUP
2186};
2187
Ulf Hansson96541ba2015-04-14 13:06:12 +02002188static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002190 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002191 char cap_str[10];
2192
Pierre Ossman912490d2005-05-21 10:27:02 +01002193 /*
2194 * Check that the card supports the command class(es) we need.
2195 */
2196 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return -ENODEV;
2198
Lukas Czerner5204d002014-06-18 13:18:07 +02002199 mmc_fixup_device(card, blk_fixups);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 md = mmc_blk_alloc(card);
2202 if (IS_ERR(md))
2203 return PTR_ERR(md);
2204
James Bottomleyb9f28d82015-03-05 18:47:01 -08002205 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002206 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302207 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002209 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Andrei Warkentin371a6892011-04-11 18:10:25 -05002211 if (mmc_blk_alloc_parts(card, md))
2212 goto out;
2213
Ulf Hansson96541ba2015-04-14 13:06:12 +02002214 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002215
Andrei Warkentin371a6892011-04-11 18:10:25 -05002216 if (mmc_add_disk(md))
2217 goto out;
2218
2219 list_for_each_entry(part_md, &md->part, part) {
2220 if (mmc_add_disk(part_md))
2221 goto out;
2222 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002223
2224 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2225 pm_runtime_use_autosuspend(&card->dev);
2226
2227 /*
2228 * Don't enable runtime PM for SD-combo cards here. Leave that
2229 * decision to be taken during the SDIO init sequence instead.
2230 */
2231 if (card->type != MMC_TYPE_SD_COMBO) {
2232 pm_runtime_set_active(&card->dev);
2233 pm_runtime_enable(&card->dev);
2234 }
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237
2238 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002239 mmc_blk_remove_parts(card, md);
2240 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Ulf Hansson96541ba2015-04-14 13:06:12 +02002244static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002246 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Andrei Warkentin371a6892011-04-11 18:10:25 -05002248 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002249 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002250 mmc_claim_host(card->host);
2251 mmc_blk_part_switch(card, md);
2252 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002253 if (card->type != MMC_TYPE_SD_COMBO)
2254 pm_runtime_disable(&card->dev);
2255 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002256 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002257 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Ulf Hansson96541ba2015-04-14 13:06:12 +02002260static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002262 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002263 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if (md) {
2266 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002267 list_for_each_entry(part_md, &md->part, part) {
2268 mmc_queue_suspend(&part_md->queue);
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271 return 0;
2272}
2273
Ulf Hansson96541ba2015-04-14 13:06:12 +02002274static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002275{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002276 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002277}
2278
Ulf Hansson0967edc2014-10-06 11:29:42 +02002279#ifdef CONFIG_PM_SLEEP
2280static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002281{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002282 struct mmc_card *card = mmc_dev_to_card(dev);
2283
2284 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002285}
2286
Ulf Hansson0967edc2014-10-06 11:29:42 +02002287static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002289 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002290 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002293 /*
2294 * Resume involves the card going into idle state,
2295 * so current partition is always the main one.
2296 */
2297 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002299 list_for_each_entry(part_md, &md->part, part) {
2300 mmc_queue_resume(&part_md->queue);
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 return 0;
2304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305#endif
2306
Ulf Hansson0967edc2014-10-06 11:29:42 +02002307static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2308
Ulf Hansson96541ba2015-04-14 13:06:12 +02002309static struct mmc_driver mmc_driver = {
2310 .drv = {
2311 .name = "mmcblk",
2312 .pm = &mmc_blk_pm_ops,
2313 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .probe = mmc_blk_probe,
2315 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002316 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317};
2318
2319static int __init mmc_blk_init(void)
2320{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002321 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002323 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2324 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2325
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002326 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002327
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002328 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2329 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002332 res = mmc_register_driver(&mmc_driver);
2333 if (res)
2334 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002336 return 0;
2337 out2:
2338 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 out:
2340 return res;
2341}
2342
2343static void __exit mmc_blk_exit(void)
2344{
2345 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002346 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349module_init(mmc_blk_init);
2350module_exit(mmc_blk_exit);
2351
2352MODULE_LICENSE("GPL");
2353MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2354