blob: 16c313a6212916d4652768487e9d8fbdfdeb29d6 [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"
Shawn Lin28fc64a2017-02-15 16:35:28 +080055#include "quirks.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010056#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000058MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040059#ifdef MODULE_PARAM_PREFIX
60#undef MODULE_PARAM_PREFIX
61#endif
62#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010063
Trey Ramsay8fee4762012-11-16 09:31:41 -060064#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020070static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040071
72/*
73 * The defaults come from config options but can be overriden by module
74 * or bootarg options.
75 */
76static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
77
78/*
79 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000080 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020081 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040082 */
83static int max_devices;
84
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085#define MAX_DEVICES 256
86
Ulf Hanssonb10fa992016-04-07 14:36:46 +020087static DEFINE_IDA(mmc_blk_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * There is one mmc_blk_data per slot.
91 */
92struct mmc_blk_data {
93 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070094 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct gendisk *disk;
96 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050097 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050099 unsigned int flags;
100#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000104 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500105 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300106 unsigned int reset_done;
107#define MMC_BLK_READ BIT(0)
108#define MMC_BLK_WRITE BIT(1)
109#define MMC_BLK_DISCARD BIT(2)
110#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111
112 /*
113 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200114 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 * track of the current selected device partition.
116 */
117 unsigned int part_curr;
118 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 struct device_attribute power_ro_lock;
120 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Arjan van de Vena621aae2006-01-12 18:43:35 +0000123static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400125module_param(perdev_minors, int, 0444);
126MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
127
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200128static inline int mmc_blk_part_switch(struct mmc_card *card,
129 struct mmc_blk_data *md);
130static int get_card_status(struct mmc_card *card, u32 *status, int retries);
131
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200132static void mmc_blk_requeue(struct request_queue *q, struct request *req)
133{
134 spin_lock_irq(q->queue_lock);
135 blk_requeue_request(q, req);
136 spin_unlock_irq(q->queue_lock);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
140{
141 struct mmc_blk_data *md;
142
Arjan van de Vena621aae2006-01-12 18:43:35 +0000143 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 md = disk->private_data;
145 if (md && md->usage == 0)
146 md = NULL;
147 if (md)
148 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000149 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return md;
152}
153
Andrei Warkentin371a6892011-04-11 18:10:25 -0500154static inline int mmc_get_devidx(struct gendisk *disk)
155{
Colin Cross382c55f2015-10-22 10:00:41 -0700156 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500157 return devidx;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void mmc_blk_put(struct mmc_blk_data *md)
161{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000162 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 md->usage--;
164 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500165 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800166 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100167 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kfree(md);
170 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000171 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Johan Rudholmadd710e2011-12-02 08:51:06 +0100174static ssize_t power_ro_lock_show(struct device *dev,
175 struct device_attribute *attr, char *buf)
176{
177 int ret;
178 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
179 struct mmc_card *card = md->queue.card;
180 int locked = 0;
181
182 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
183 locked = 2;
184 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
185 locked = 1;
186
187 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
188
Tomas Winkler9098f842015-07-16 15:50:45 +0200189 mmc_blk_put(md);
190
Johan Rudholmadd710e2011-12-02 08:51:06 +0100191 return ret;
192}
193
194static ssize_t power_ro_lock_store(struct device *dev,
195 struct device_attribute *attr, const char *buf, size_t count)
196{
197 int ret;
198 struct mmc_blk_data *md, *part_md;
199 struct mmc_card *card;
200 unsigned long set;
201
202 if (kstrtoul(buf, 0, &set))
203 return -EINVAL;
204
205 if (set != 1)
206 return count;
207
208 md = mmc_blk_get(dev_to_disk(dev));
209 card = md->queue.card;
210
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200211 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100212
213 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
214 card->ext_csd.boot_ro_lock |
215 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
216 card->ext_csd.part_time);
217 if (ret)
218 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
219 else
220 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
221
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200222 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100223
224 if (!ret) {
225 pr_info("%s: Locking boot partition ro until next power on\n",
226 md->disk->disk_name);
227 set_disk_ro(md->disk, 1);
228
229 list_for_each_entry(part_md, &md->part, part)
230 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
231 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
232 set_disk_ro(part_md->disk, 1);
233 }
234 }
235
236 mmc_blk_put(md);
237 return count;
238}
239
Andrei Warkentin371a6892011-04-11 18:10:25 -0500240static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
241 char *buf)
242{
243 int ret;
244 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
245
Baruch Siach0031a982014-09-22 10:12:51 +0300246 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500247 get_disk_ro(dev_to_disk(dev)) ^
248 md->read_only);
249 mmc_blk_put(md);
250 return ret;
251}
252
253static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
255{
256 int ret;
257 char *end;
258 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
259 unsigned long set = simple_strtoul(buf, &end, 0);
260 if (end == buf) {
261 ret = -EINVAL;
262 goto out;
263 }
264
265 set_disk_ro(dev_to_disk(dev), set || md->read_only);
266 ret = count;
267out:
268 mmc_blk_put(md);
269 return ret;
270}
271
Al Viroa5a15612008-03-02 10:33:30 -0500272static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Al Viroa5a15612008-03-02 10:33:30 -0500274 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int ret = -ENXIO;
276
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200277 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (md) {
279 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500280 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700282
Al Viroa5a15612008-03-02 10:33:30 -0500283 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700284 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700285 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200288 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return ret;
291}
292
Al Virodb2a1442013-05-05 21:52:57 -0400293static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Al Viroa5a15612008-03-02 10:33:30 -0500295 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200297 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200299 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800303mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800305 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
306 geo->heads = 4;
307 geo->sectors = 16;
308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
John Calixtocb87ea22011-04-26 18:56:29 -0400311struct mmc_blk_ioc_data {
312 struct mmc_ioc_cmd ic;
313 unsigned char *buf;
314 u64 buf_bytes;
315};
316
317static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
318 struct mmc_ioc_cmd __user *user)
319{
320 struct mmc_blk_ioc_data *idata;
321 int err;
322
yalin wang1ff89502015-11-12 19:27:11 +0800323 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400324 if (!idata) {
325 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400326 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400327 }
328
329 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
330 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400331 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400332 }
333
334 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
335 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
336 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400337 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400338 }
339
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300340 if (!idata->buf_bytes) {
341 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100342 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300343 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100344
yalin wang1ff89502015-11-12 19:27:11 +0800345 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400346 if (!idata->buf) {
347 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
352 idata->ic.data_ptr, idata->buf_bytes)) {
353 err = -EFAULT;
354 goto copy_err;
355 }
356
357 return idata;
358
359copy_err:
360 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400361idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400362 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363out:
John Calixtocb87ea22011-04-26 18:56:29 -0400364 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400365}
366
Jon Huntera5f57742015-09-22 10:27:53 +0100367static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
368 struct mmc_blk_ioc_data *idata)
369{
370 struct mmc_ioc_cmd *ic = &idata->ic;
371
372 if (copy_to_user(&(ic_ptr->response), ic->response,
373 sizeof(ic->response)))
374 return -EFAULT;
375
376 if (!idata->ic.write_flag) {
377 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
378 idata->buf, idata->buf_bytes))
379 return -EFAULT;
380 }
381
382 return 0;
383}
384
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200385static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
386 u32 retries_max)
387{
388 int err;
389 u32 retry_count = 0;
390
391 if (!status || !retries_max)
392 return -EINVAL;
393
394 do {
395 err = get_card_status(card, status, 5);
396 if (err)
397 break;
398
399 if (!R1_STATUS(*status) &&
400 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
401 break; /* RPMB programming operation complete */
402
403 /*
404 * Rechedule to give the MMC device a chance to continue
405 * processing the previous command without being polled too
406 * frequently.
407 */
408 usleep_range(1000, 5000);
409 } while (++retry_count < retries_max);
410
411 if (retry_count == retries_max)
412 err = -EPERM;
413
414 return err;
415}
416
Maya Erez775a9362013-04-18 15:41:55 +0300417static int ioctl_do_sanitize(struct mmc_card *card)
418{
419 int err;
420
Ulf Hanssona2d10862013-12-16 14:37:26 +0100421 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300422 pr_warn("%s: %s - SANITIZE is not supported\n",
423 mmc_hostname(card->host), __func__);
424 err = -EOPNOTSUPP;
425 goto out;
426 }
427
428 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
429 mmc_hostname(card->host), __func__);
430
431 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
432 EXT_CSD_SANITIZE_START, 1,
433 MMC_SANITIZE_REQ_TIMEOUT);
434
435 if (err)
436 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
437 mmc_hostname(card->host), __func__, err);
438
439 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
440 __func__);
441out:
442 return err;
443}
444
Jon Huntera5f57742015-09-22 10:27:53 +0100445static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
446 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400447{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900448 struct mmc_command cmd = {};
449 struct mmc_data data = {};
450 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400451 struct scatterlist sg;
452 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200453 int is_rpmb = false;
454 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400455
Jon Huntera5f57742015-09-22 10:27:53 +0100456 if (!card || !md || !idata)
457 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400458
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200459 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
460 is_rpmb = true;
461
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100462 cmd.opcode = idata->ic.opcode;
463 cmd.arg = idata->ic.arg;
464 cmd.flags = idata->ic.flags;
465
466 if (idata->buf_bytes) {
467 data.sg = &sg;
468 data.sg_len = 1;
469 data.blksz = idata->ic.blksz;
470 data.blocks = idata->ic.blocks;
471
472 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
473
474 if (idata->ic.write_flag)
475 data.flags = MMC_DATA_WRITE;
476 else
477 data.flags = MMC_DATA_READ;
478
479 /* data.flags must already be set before doing this. */
480 mmc_set_data_timeout(&data, card);
481
482 /* Allow overriding the timeout_ns for empirical tuning. */
483 if (idata->ic.data_timeout_ns)
484 data.timeout_ns = idata->ic.data_timeout_ns;
485
486 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
487 /*
488 * Pretend this is a data transfer and rely on the
489 * host driver to compute timeout. When all host
490 * drivers support cmd.cmd_timeout for R1B, this
491 * can be changed to:
492 *
493 * mrq.data = NULL;
494 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
495 */
496 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
497 }
498
499 mrq.data = &data;
500 }
501
502 mrq.cmd = &cmd;
503
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200504 err = mmc_blk_part_switch(card, md);
505 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100506 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507
John Calixtocb87ea22011-04-26 18:56:29 -0400508 if (idata->ic.is_acmd) {
509 err = mmc_app_cmd(card->host, card);
510 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100511 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400512 }
513
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200514 if (is_rpmb) {
515 err = mmc_set_blockcount(card, data.blocks,
516 idata->ic.write_flag & (1 << 31));
517 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100518 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200519 }
520
Yaniv Gardia82e4842013-06-05 14:13:08 +0300521 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
522 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300523 err = ioctl_do_sanitize(card);
524
525 if (err)
526 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
527 __func__, err);
528
Jon Huntera5f57742015-09-22 10:27:53 +0100529 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300530 }
531
John Calixtocb87ea22011-04-26 18:56:29 -0400532 mmc_wait_for_req(card->host, &mrq);
533
534 if (cmd.error) {
535 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
536 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100537 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400538 }
539 if (data.error) {
540 dev_err(mmc_dev(card->host), "%s: data error %d\n",
541 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100542 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400543 }
544
545 /*
546 * According to the SD specs, some commands require a delay after
547 * issuing the command.
548 */
549 if (idata->ic.postsleep_min_us)
550 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
551
Jon Huntera5f57742015-09-22 10:27:53 +0100552 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400553
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200554 if (is_rpmb) {
555 /*
556 * Ensure RPMB command has completed by polling CMD13
557 * "Send Status".
558 */
559 err = ioctl_rpmb_card_status_poll(card, &status, 5);
560 if (err)
561 dev_err(mmc_dev(card->host),
562 "%s: Card Status=0x%08X, error %d\n",
563 __func__, status, err);
564 }
565
Jon Huntera5f57742015-09-22 10:27:53 +0100566 return err;
567}
568
569static int mmc_blk_ioctl_cmd(struct block_device *bdev,
570 struct mmc_ioc_cmd __user *ic_ptr)
571{
572 struct mmc_blk_ioc_data *idata;
573 struct mmc_blk_data *md;
574 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700575 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100576
Shawn Lin83c742c2016-03-16 18:15:47 +0800577 /*
578 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
579 * whole block device, not on a partition. This prevents overspray
580 * between sibling partitions.
581 */
582 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
583 return -EPERM;
584
Jon Huntera5f57742015-09-22 10:27:53 +0100585 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
586 if (IS_ERR(idata))
587 return PTR_ERR(idata);
588
589 md = mmc_blk_get(bdev->bd_disk);
590 if (!md) {
591 err = -EINVAL;
592 goto cmd_err;
593 }
594
595 card = md->queue.card;
596 if (IS_ERR(card)) {
597 err = PTR_ERR(card);
598 goto cmd_done;
599 }
600
601 mmc_get_card(card);
602
Grant Grundlerb0934102015-09-23 18:30:33 -0700603 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100604
Adrian Hunter3c866562016-05-04 14:38:12 +0300605 /* Always switch back to main area after RPMB access */
606 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
607 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
608
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200609 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400610
Grant Grundlerb0934102015-09-23 18:30:33 -0700611 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100612
John Calixtocb87ea22011-04-26 18:56:29 -0400613cmd_done:
614 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300615cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400616 kfree(idata->buf);
617 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700618 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400619}
620
Jon Huntera5f57742015-09-22 10:27:53 +0100621static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
622 struct mmc_ioc_multi_cmd __user *user)
623{
624 struct mmc_blk_ioc_data **idata = NULL;
625 struct mmc_ioc_cmd __user *cmds = user->cmds;
626 struct mmc_card *card;
627 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700628 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100629 __u64 num_of_cmds;
630
Shawn Lin83c742c2016-03-16 18:15:47 +0800631 /*
632 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
633 * whole block device, not on a partition. This prevents overspray
634 * between sibling partitions.
635 */
636 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
637 return -EPERM;
638
Jon Huntera5f57742015-09-22 10:27:53 +0100639 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
640 sizeof(num_of_cmds)))
641 return -EFAULT;
642
643 if (num_of_cmds > MMC_IOC_MAX_CMDS)
644 return -EINVAL;
645
646 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
647 if (!idata)
648 return -ENOMEM;
649
650 for (i = 0; i < num_of_cmds; i++) {
651 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
652 if (IS_ERR(idata[i])) {
653 err = PTR_ERR(idata[i]);
654 num_of_cmds = i;
655 goto cmd_err;
656 }
657 }
658
659 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800660 if (!md) {
661 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100662 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800663 }
Jon Huntera5f57742015-09-22 10:27:53 +0100664
665 card = md->queue.card;
666 if (IS_ERR(card)) {
667 err = PTR_ERR(card);
668 goto cmd_done;
669 }
670
671 mmc_get_card(card);
672
Grant Grundlerb0934102015-09-23 18:30:33 -0700673 for (i = 0; i < num_of_cmds && !ioc_err; i++)
674 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100675
Adrian Hunter3c866562016-05-04 14:38:12 +0300676 /* Always switch back to main area after RPMB access */
677 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
678 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
679
Jon Huntera5f57742015-09-22 10:27:53 +0100680 mmc_put_card(card);
681
682 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700683 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100684 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100685
686cmd_done:
687 mmc_blk_put(md);
688cmd_err:
689 for (i = 0; i < num_of_cmds; i++) {
690 kfree(idata[i]->buf);
691 kfree(idata[i]);
692 }
693 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700694 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100695}
696
John Calixtocb87ea22011-04-26 18:56:29 -0400697static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
698 unsigned int cmd, unsigned long arg)
699{
Jon Huntera5f57742015-09-22 10:27:53 +0100700 switch (cmd) {
701 case MMC_IOC_CMD:
702 return mmc_blk_ioctl_cmd(bdev,
703 (struct mmc_ioc_cmd __user *)arg);
704 case MMC_IOC_MULTI_CMD:
705 return mmc_blk_ioctl_multi_cmd(bdev,
706 (struct mmc_ioc_multi_cmd __user *)arg);
707 default:
708 return -EINVAL;
709 }
John Calixtocb87ea22011-04-26 18:56:29 -0400710}
711
712#ifdef CONFIG_COMPAT
713static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
714 unsigned int cmd, unsigned long arg)
715{
716 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
717}
718#endif
719
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700720static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500721 .open = mmc_blk_open,
722 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800723 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400725 .ioctl = mmc_blk_ioctl,
726#ifdef CONFIG_COMPAT
727 .compat_ioctl = mmc_blk_compat_ioctl,
728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729};
730
Andrei Warkentin371a6892011-04-11 18:10:25 -0500731static inline int mmc_blk_part_switch(struct mmc_card *card,
732 struct mmc_blk_data *md)
733{
734 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200735 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300736
Andrei Warkentin371a6892011-04-11 18:10:25 -0500737 if (main_md->part_curr == md->part_type)
738 return 0;
739
740 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300741 u8 part_config = card->ext_csd.part_config;
742
Adrian Hunter57da0c02016-05-04 14:38:13 +0300743 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
744 mmc_retune_pause(card->host);
745
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300746 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
747 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500748
749 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300750 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500751 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300752 if (ret) {
753 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
754 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500755 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300756 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300757
758 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300759
760 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
761 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300762 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500763
764 main_md->part_curr = md->part_type;
765 return 0;
766}
767
Linus Walleij169f03a2017-02-01 13:47:57 +0100768static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700769{
770 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100771 u32 result;
772 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700773
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900774 struct mmc_request mrq = {};
775 struct mmc_command cmd = {};
776 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700777
778 struct scatterlist sg;
779
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700780 cmd.opcode = MMC_APP_CMD;
781 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700782 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783
784 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700785 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100786 return err;
David Brownell7213d172007-08-08 09:10:23 -0700787 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100788 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700789
790 memset(&cmd, 0, sizeof(struct mmc_command));
791
792 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
793 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700794 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700795
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700796 data.blksz = 4;
797 data.blocks = 1;
798 data.flags = MMC_DATA_READ;
799 data.sg = &sg;
800 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530801 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700802
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700803 mrq.cmd = &cmd;
804 mrq.data = &data;
805
Ben Dooks051913d2009-06-08 23:33:57 +0100806 blocks = kmalloc(4, GFP_KERNEL);
807 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100808 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100809
810 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700811
812 mmc_wait_for_req(card->host, &mrq);
813
Ben Dooks051913d2009-06-08 23:33:57 +0100814 result = ntohl(*blocks);
815 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700816
Ben Dooks051913d2009-06-08 23:33:57 +0100817 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100818 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100819
Linus Walleij169f03a2017-02-01 13:47:57 +0100820 *written_blocks = result;
821
822 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700823}
824
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100825static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300826{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900827 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300828 int err;
829
Adrian Hunter504f1912008-10-16 12:55:25 +0300830 cmd.opcode = MMC_SEND_STATUS;
831 if (!mmc_host_is_spi(card->host))
832 cmd.arg = card->rca << 16;
833 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100834 err = mmc_wait_for_cmd(card->host, &cmd, retries);
835 if (err == 0)
836 *status = cmd.resp[0];
837 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300838}
839
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100840static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100841 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100842{
843 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
844 int err = 0;
845 u32 status;
846
847 do {
848 err = get_card_status(card, &status, 5);
849 if (err) {
850 pr_err("%s: error %d requesting status\n",
851 req->rq_disk->disk_name, err);
852 return err;
853 }
854
855 if (status & R1_ERROR) {
856 pr_err("%s: %s: error sending status cmd, status %#x\n",
857 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100858 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100859 }
860
Ulf Hansson95a91292014-01-29 13:11:27 +0100861 /* We may rely on the host hw to handle busy detection.*/
862 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
863 hw_busy_detect)
864 break;
865
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100866 /*
867 * Timeout if the device never becomes ready for data and never
868 * leaves the program state.
869 */
870 if (time_after(jiffies, timeout)) {
871 pr_err("%s: Card stuck in programming state! %s %s\n",
872 mmc_hostname(card->host),
873 req->rq_disk->disk_name, __func__);
874 return -ETIMEDOUT;
875 }
876
877 /*
878 * Some cards mishandle the status bits,
879 * so make sure to check both the busy
880 * indication and the card state.
881 */
882 } while (!(status & R1_READY_FOR_DATA) ||
883 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
884
885 return err;
886}
887
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100888static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100889 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100890{
891 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900892 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100893 int err;
894 bool use_r1b_resp = rq_data_dir(req) == WRITE;
895
896 /*
897 * Normally we use R1B responses for WRITE, but in cases where the host
898 * has specified a max_busy_timeout we need to validate it. A failure
899 * means we need to prevent the host from doing hw busy detection, which
900 * is done by converting to a R1 response instead.
901 */
902 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
903 use_r1b_resp = false;
904
905 cmd.opcode = MMC_STOP_TRANSMISSION;
906 if (use_r1b_resp) {
907 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
908 cmd.busy_timeout = timeout_ms;
909 } else {
910 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
911 }
912
913 err = mmc_wait_for_cmd(host, &cmd, 5);
914 if (err)
915 return err;
916
917 *stop_status = cmd.resp[0];
918
919 /* No need to check card status in case of READ. */
920 if (rq_data_dir(req) == READ)
921 return 0;
922
923 if (!mmc_host_is_spi(host) &&
924 (*stop_status & R1_ERROR)) {
925 pr_err("%s: %s: general error sending stop command, resp %#x\n",
926 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100927 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100928 }
929
930 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
931}
932
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530933#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100934#define ERR_RETRY 2
935#define ERR_ABORT 1
936#define ERR_CONTINUE 0
937
938static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
939 bool status_valid, u32 status)
940{
941 switch (error) {
942 case -EILSEQ:
943 /* response crc error, retry the r/w cmd */
944 pr_err("%s: %s sending %s command, card status %#x\n",
945 req->rq_disk->disk_name, "response CRC error",
946 name, status);
947 return ERR_RETRY;
948
949 case -ETIMEDOUT:
950 pr_err("%s: %s sending %s command, card status %#x\n",
951 req->rq_disk->disk_name, "timed out", name, status);
952
953 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530954 if (!status_valid) {
955 pr_err("%s: status not valid, retrying timeout\n",
956 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100957 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530958 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100959
960 /*
961 * If it was a r/w cmd crc error, or illegal command
962 * (eg, issued in wrong state) then retry - we should
963 * have corrected the state problem above.
964 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530965 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
966 pr_err("%s: command error, retrying timeout\n",
967 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100968 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530969 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100970
971 /* Otherwise abort the command */
972 return ERR_ABORT;
973
974 default:
975 /* We don't understand the error code the driver gave us */
976 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
977 req->rq_disk->disk_name, error, status);
978 return ERR_ABORT;
979 }
980}
981
982/*
983 * Initial r/w and stop cmd error recovery.
984 * We don't know whether the card received the r/w cmd or not, so try to
985 * restore things back to a sane state. Essentially, we do this as follows:
986 * - Obtain card status. If the first attempt to obtain card status fails,
987 * the status word will reflect the failed status cmd, not the failed
988 * r/w cmd. If we fail to obtain card status, it suggests we can no
989 * longer communicate with the card.
990 * - Check the card state. If the card received the cmd but there was a
991 * transient problem with the response, it might still be in a data transfer
992 * mode. Try to send it a stop command. If this fails, we can't recover.
993 * - If the r/w cmd failed due to a response CRC error, it was probably
994 * transient, so retry the cmd.
995 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
996 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
997 * illegal cmd, retry.
998 * Otherwise we don't understand what happened, so abort.
999 */
1000static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001001 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001002{
1003 bool prev_cmd_status_valid = true;
1004 u32 status, stop_status = 0;
1005 int err, retry;
1006
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301007 if (mmc_card_removed(card))
1008 return ERR_NOMEDIUM;
1009
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001010 /*
1011 * Try to get card status which indicates both the card state
1012 * and why there was no response. If the first attempt fails,
1013 * we can't be sure the returned status is for the r/w command.
1014 */
1015 for (retry = 2; retry >= 0; retry--) {
1016 err = get_card_status(card, &status, 0);
1017 if (!err)
1018 break;
1019
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001020 /* Re-tune if needed */
1021 mmc_retune_recheck(card->host);
1022
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001023 prev_cmd_status_valid = false;
1024 pr_err("%s: error %d sending status command, %sing\n",
1025 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1026 }
1027
1028 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301029 if (err) {
1030 /* Check if the card is removed */
1031 if (mmc_detect_card_removed(card->host))
1032 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001033 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301034 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001035
Adrian Hunter67716322011-08-29 16:42:15 +03001036 /* Flag ECC errors */
1037 if ((status & R1_CARD_ECC_FAILED) ||
1038 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1039 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001040 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001041
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001042 /* Flag General errors */
1043 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1044 if ((status & R1_ERROR) ||
1045 (brq->stop.resp[0] & R1_ERROR)) {
1046 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1047 req->rq_disk->disk_name, __func__,
1048 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001049 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001050 }
1051
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001052 /*
1053 * Check the current card state. If it is in some data transfer
1054 * mode, tell it to stop (and hopefully transition back to TRAN.)
1055 */
1056 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1057 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001058 err = send_stop(card,
1059 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1060 req, gen_err, &stop_status);
1061 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001062 pr_err("%s: error %d sending stop command\n",
1063 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001064 /*
1065 * If the stop cmd also timed out, the card is probably
1066 * not present, so abort. Other errors are bad news too.
1067 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001068 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001069 }
1070
Adrian Hunter67716322011-08-29 16:42:15 +03001071 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001072 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001073 }
1074
1075 /* Check for set block count errors */
1076 if (brq->sbc.error)
1077 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1078 prev_cmd_status_valid, status);
1079
1080 /* Check for r/w command errors */
1081 if (brq->cmd.error)
1082 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1083 prev_cmd_status_valid, status);
1084
Adrian Hunter67716322011-08-29 16:42:15 +03001085 /* Data errors */
1086 if (!brq->stop.error)
1087 return ERR_CONTINUE;
1088
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001089 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001090 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001091 req->rq_disk->disk_name, brq->stop.error,
1092 brq->cmd.resp[0], status);
1093
1094 /*
1095 * Subsitute in our own stop status as this will give the error
1096 * state which happened during the execution of the r/w command.
1097 */
1098 if (stop_status) {
1099 brq->stop.resp[0] = stop_status;
1100 brq->stop.error = 0;
1101 }
1102 return ERR_CONTINUE;
1103}
1104
Adrian Hunter67716322011-08-29 16:42:15 +03001105static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1106 int type)
1107{
1108 int err;
1109
1110 if (md->reset_done & type)
1111 return -EEXIST;
1112
1113 md->reset_done |= type;
1114 err = mmc_hw_reset(host);
1115 /* Ensure we switch back to the correct partition */
1116 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001117 struct mmc_blk_data *main_md =
1118 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001119 int part_err;
1120
1121 main_md->part_curr = main_md->part_type;
1122 part_err = mmc_blk_part_switch(host->card, md);
1123 if (part_err) {
1124 /*
1125 * We have failed to get back into the correct
1126 * partition, so we need to abort the whole request.
1127 */
1128 return -ENODEV;
1129 }
1130 }
1131 return err;
1132}
1133
1134static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1135{
1136 md->reset_done &= ~type;
1137}
1138
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001139int mmc_access_rpmb(struct mmc_queue *mq)
1140{
Linus Walleij7db30282016-11-18 13:36:15 +01001141 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001142 /*
1143 * If this is a RPMB partition access, return ture
1144 */
1145 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1146 return true;
1147
1148 return false;
1149}
1150
Linus Walleijdf061582017-01-24 11:17:57 +01001151static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001152{
Linus Walleij7db30282016-11-18 13:36:15 +01001153 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001154 struct mmc_card *card = md->queue.card;
1155 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001156 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001157
Adrian Hunterbd788c92010-08-11 14:17:47 -07001158 if (!mmc_can_erase(card)) {
1159 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001160 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001161 }
1162
1163 from = blk_rq_pos(req);
1164 nr = blk_rq_sectors(req);
1165
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001166 if (mmc_can_discard(card))
1167 arg = MMC_DISCARD_ARG;
1168 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001169 arg = MMC_TRIM_ARG;
1170 else
1171 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001172 do {
1173 err = 0;
1174 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1175 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1176 INAND_CMD38_ARG_EXT_CSD,
1177 arg == MMC_TRIM_ARG ?
1178 INAND_CMD38_ARG_TRIM :
1179 INAND_CMD38_ARG_ERASE,
1180 0);
1181 }
1182 if (!err)
1183 err = mmc_erase(card, from, nr, arg);
1184 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001185 if (!err)
1186 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001187fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301188 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001189}
1190
Linus Walleijdf061582017-01-24 11:17:57 +01001191static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001192 struct request *req)
1193{
Linus Walleij7db30282016-11-18 13:36:15 +01001194 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001195 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001196 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001197 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001198
Maya Erez775a9362013-04-18 15:41:55 +03001199 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001200 err = -EOPNOTSUPP;
1201 goto out;
1202 }
1203
1204 from = blk_rq_pos(req);
1205 nr = blk_rq_sectors(req);
1206
Maya Erez775a9362013-04-18 15:41:55 +03001207 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1208 arg = MMC_SECURE_TRIM1_ARG;
1209 else
1210 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001211
Adrian Hunter67716322011-08-29 16:42:15 +03001212retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001213 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1214 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1215 INAND_CMD38_ARG_EXT_CSD,
1216 arg == MMC_SECURE_TRIM1_ARG ?
1217 INAND_CMD38_ARG_SECTRIM1 :
1218 INAND_CMD38_ARG_SECERASE,
1219 0);
1220 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001221 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001222 }
Adrian Hunter28302812012-04-05 14:45:48 +03001223
Adrian Hunter49804542010-08-11 14:17:50 -07001224 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001225 if (err == -EIO)
1226 goto out_retry;
1227 if (err)
1228 goto out;
1229
1230 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001231 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1232 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1233 INAND_CMD38_ARG_EXT_CSD,
1234 INAND_CMD38_ARG_SECTRIM2,
1235 0);
1236 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001237 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001238 }
Adrian Hunter28302812012-04-05 14:45:48 +03001239
Adrian Hunter49804542010-08-11 14:17:50 -07001240 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001241 if (err == -EIO)
1242 goto out_retry;
1243 if (err)
1244 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001245 }
Adrian Hunter28302812012-04-05 14:45:48 +03001246
Adrian Hunter28302812012-04-05 14:45:48 +03001247out_retry:
1248 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001249 goto retry;
1250 if (!err)
1251 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001252out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301253 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001254}
1255
Linus Walleijdf061582017-01-24 11:17:57 +01001256static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001257{
Linus Walleij7db30282016-11-18 13:36:15 +01001258 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001259 struct mmc_card *card = md->queue.card;
1260 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001261
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001262 ret = mmc_flush_cache(card);
1263 if (ret)
1264 ret = -EIO;
1265
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301266 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001267}
1268
1269/*
1270 * Reformat current write as a reliable write, supporting
1271 * both legacy and the enhanced reliable write MMC cards.
1272 * In each transfer we'll handle only as much as a single
1273 * reliable write can handle, thus finish the request in
1274 * partial completions.
1275 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001276static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1277 struct mmc_card *card,
1278 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001279{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001280 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1281 /* Legacy mode imposes restrictions on transfers. */
1282 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1283 brq->data.blocks = 1;
1284
1285 if (brq->data.blocks > card->ext_csd.rel_sectors)
1286 brq->data.blocks = card->ext_csd.rel_sectors;
1287 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1288 brq->data.blocks = 1;
1289 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001290}
1291
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001292#define CMD_ERRORS \
1293 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1294 R1_ADDRESS_ERROR | /* Misaligned address */ \
1295 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1296 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1297 R1_CC_ERROR | /* Card controller error */ \
1298 R1_ERROR) /* General/unknown error */
1299
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001300static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1301 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001302{
Per Forlinee8a43a2011-07-01 18:55:33 +02001303 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001304 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001305 struct mmc_blk_request *brq = &mq_mrq->brq;
1306 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001307 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001308 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001309 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001310
1311 /*
1312 * sbc.error indicates a problem with the set block count
1313 * command. No data will have been transferred.
1314 *
1315 * cmd.error indicates a problem with the r/w command. No
1316 * data will have been transferred.
1317 *
1318 * stop.error indicates a problem with the stop command. Data
1319 * may have been transferred, or may still be transferring.
1320 */
Adrian Hunter67716322011-08-29 16:42:15 +03001321 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1322 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001323 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001324 case ERR_RETRY:
1325 return MMC_BLK_RETRY;
1326 case ERR_ABORT:
1327 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301328 case ERR_NOMEDIUM:
1329 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001330 case ERR_CONTINUE:
1331 break;
1332 }
1333 }
1334
1335 /*
1336 * Check for errors relating to the execution of the
1337 * initial command - such as address errors. No data
1338 * has been transferred.
1339 */
1340 if (brq->cmd.resp[0] & CMD_ERRORS) {
1341 pr_err("%s: r/w command failed, status = %#x\n",
1342 req->rq_disk->disk_name, brq->cmd.resp[0]);
1343 return MMC_BLK_ABORT;
1344 }
1345
1346 /*
1347 * Everything else is either success, or a data error of some
1348 * kind. If it was a write, we may have transitioned to
1349 * program mode, which we have to wait for it to complete.
1350 */
1351 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001352 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001353
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001354 /* Check stop command response */
1355 if (brq->stop.resp[0] & R1_ERROR) {
1356 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1357 req->rq_disk->disk_name, __func__,
1358 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001359 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001360 }
1361
Ulf Hansson95a91292014-01-29 13:11:27 +01001362 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1363 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001364 if (err)
1365 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001366 }
1367
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001368 /* if general error occurs, retry the write operation. */
1369 if (gen_err) {
1370 pr_warn("%s: retrying write for general error\n",
1371 req->rq_disk->disk_name);
1372 return MMC_BLK_RETRY;
1373 }
1374
Per Forlind78d4a8a2011-07-01 18:55:30 +02001375 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001376 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001377 pr_debug("%s: retrying because a re-tune was needed\n",
1378 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001379 brq->retune_retry_done = 1;
1380 return MMC_BLK_RETRY;
1381 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001382 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1383 req->rq_disk->disk_name, brq->data.error,
1384 (unsigned)blk_rq_pos(req),
1385 (unsigned)blk_rq_sectors(req),
1386 brq->cmd.resp[0], brq->stop.resp[0]);
1387
1388 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001389 if (ecc_err)
1390 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001391 return MMC_BLK_DATA_ERR;
1392 } else {
1393 return MMC_BLK_CMD_ERR;
1394 }
1395 }
1396
Adrian Hunter67716322011-08-29 16:42:15 +03001397 if (!brq->data.bytes_xfered)
1398 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001399
Adrian Hunter67716322011-08-29 16:42:15 +03001400 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1401 return MMC_BLK_PARTIAL;
1402
1403 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001404}
1405
Per Forlin54d49d72011-07-01 18:55:29 +02001406static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1407 struct mmc_card *card,
1408 int disable_multi,
1409 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Per Forlin54d49d72011-07-01 18:55:29 +02001411 u32 readcmd, writecmd;
1412 struct mmc_blk_request *brq = &mqrq->brq;
1413 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001414 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301415 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001417 /*
1418 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001419 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420 */
Luca Porziod3df0462015-11-06 15:12:26 +00001421 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001422 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001423 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001424
Per Forlin54d49d72011-07-01 18:55:29 +02001425 memset(brq, 0, sizeof(struct mmc_blk_request));
1426 brq->mrq.cmd = &brq->cmd;
1427 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Per Forlin54d49d72011-07-01 18:55:29 +02001429 brq->cmd.arg = blk_rq_pos(req);
1430 if (!mmc_card_blockaddr(card))
1431 brq->cmd.arg <<= 9;
1432 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1433 brq->data.blksz = 512;
1434 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1435 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001436 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Per Forlin54d49d72011-07-01 18:55:29 +02001438 /*
1439 * The block layer doesn't support all sector count
1440 * restrictions, so we need to be prepared for too big
1441 * requests.
1442 */
1443 if (brq->data.blocks > card->host->max_blk_count)
1444 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001446 if (brq->data.blocks > 1) {
1447 /*
1448 * After a read error, we redo the request one sector
1449 * at a time in order to accurately determine which
1450 * sectors can be read successfully.
1451 */
1452 if (disable_multi)
1453 brq->data.blocks = 1;
1454
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001455 /*
1456 * Some controllers have HW issues while operating
1457 * in multiple I/O mode
1458 */
1459 if (card->host->ops->multi_io_quirk)
1460 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1461 (rq_data_dir(req) == READ) ?
1462 MMC_DATA_READ : MMC_DATA_WRITE,
1463 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001464 }
Per Forlin54d49d72011-07-01 18:55:29 +02001465
1466 if (brq->data.blocks > 1 || do_rel_wr) {
1467 /* SPI multiblock writes terminate using a special
1468 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001469 */
Per Forlin54d49d72011-07-01 18:55:29 +02001470 if (!mmc_host_is_spi(card->host) ||
1471 rq_data_dir(req) == READ)
1472 brq->mrq.stop = &brq->stop;
1473 readcmd = MMC_READ_MULTIPLE_BLOCK;
1474 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1475 } else {
1476 brq->mrq.stop = NULL;
1477 readcmd = MMC_READ_SINGLE_BLOCK;
1478 writecmd = MMC_WRITE_BLOCK;
1479 }
1480 if (rq_data_dir(req) == READ) {
1481 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001482 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001483 if (brq->mrq.stop)
1484 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1485 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001486 } else {
1487 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001488 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001489 if (brq->mrq.stop)
1490 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1491 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001492 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001493
Per Forlin54d49d72011-07-01 18:55:29 +02001494 if (do_rel_wr)
1495 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001496
Per Forlin54d49d72011-07-01 18:55:29 +02001497 /*
Saugata Das42659002011-12-21 13:09:17 +05301498 * Data tag is used only during writing meta data to speed
1499 * up write and any subsequent read of this meta data
1500 */
1501 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1502 (req->cmd_flags & REQ_META) &&
1503 (rq_data_dir(req) == WRITE) &&
1504 ((brq->data.blocks * brq->data.blksz) >=
1505 card->ext_csd.data_tag_unit_size);
1506
1507 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001508 * Pre-defined multi-block transfers are preferable to
1509 * open ended-ones (and necessary for reliable writes).
1510 * However, it is not sufficient to just send CMD23,
1511 * and avoid the final CMD12, as on an error condition
1512 * CMD12 (stop) needs to be sent anyway. This, coupled
1513 * with Auto-CMD23 enhancements provided by some
1514 * hosts, means that the complexity of dealing
1515 * with this is best left to the host. If CMD23 is
1516 * supported by card and host, we'll fill sbc in and let
1517 * the host deal with handling it correctly. This means
1518 * that for hosts that don't expose MMC_CAP_CMD23, no
1519 * change of behavior will be observed.
1520 *
1521 * N.B: Some MMC cards experience perf degradation.
1522 * We'll avoid using CMD23-bounded multiblock writes for
1523 * these, while retaining features like reliable writes.
1524 */
Saugata Das42659002011-12-21 13:09:17 +05301525 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1526 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1527 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001528 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1529 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301530 (do_rel_wr ? (1 << 31) : 0) |
1531 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001532 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1533 brq->mrq.sbc = &brq->sbc;
1534 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001535
Per Forlin54d49d72011-07-01 18:55:29 +02001536 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001537
Per Forlin54d49d72011-07-01 18:55:29 +02001538 brq->data.sg = mqrq->sg;
1539 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001540
Per Forlin54d49d72011-07-01 18:55:29 +02001541 /*
1542 * Adjust the sg list so it is the same size as the
1543 * request.
1544 */
1545 if (brq->data.blocks != blk_rq_sectors(req)) {
1546 int i, data_size = brq->data.blocks << 9;
1547 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001548
Per Forlin54d49d72011-07-01 18:55:29 +02001549 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1550 data_size -= sg->length;
1551 if (data_size <= 0) {
1552 sg->length += data_size;
1553 i++;
1554 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001555 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001556 }
Per Forlin54d49d72011-07-01 18:55:29 +02001557 brq->data.sg_len = i;
1558 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001559
Linus Walleij74f5ba32017-02-01 13:47:55 +01001560 mqrq->areq.mrq = &brq->mrq;
1561 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001562
Per Forlin54d49d72011-07-01 18:55:29 +02001563 mmc_queue_bounce_pre(mqrq);
1564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Linus Walleij0e65f102017-02-01 13:47:58 +01001566static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1567 struct mmc_blk_request *brq, struct request *req,
1568 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001569{
Linus Walleij0e65f102017-02-01 13:47:58 +01001570 bool req_pending;
1571
Adrian Hunter67716322011-08-29 16:42:15 +03001572 /*
1573 * If this is an SD card and we're writing, we can first
1574 * mark the known good sectors as ok.
1575 *
1576 * If the card is not SD, we can still ok written sectors
1577 * as reported by the controller (which might be less than
1578 * the real number of written sectors, but never more).
1579 */
1580 if (mmc_card_sd(card)) {
1581 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001582 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001583
Linus Walleij169f03a2017-02-01 13:47:57 +01001584 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001585 if (err)
1586 req_pending = old_req_pending;
1587 else
1588 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001589 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001590 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001591 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001592 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001593}
1594
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001595static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1596 struct request *req,
1597 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001598{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001599 if (mmc_card_removed(card))
1600 req->rq_flags |= RQF_QUIET;
Linus Walleij0e65f102017-02-01 13:47:58 +01001601 while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req)));
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001602 mmc_queue_req_free(mq, mqrq);
Linus Walleij4e1f7802017-01-24 11:17:52 +01001603}
1604
Linus Walleijb2928e12017-02-01 13:47:54 +01001605/**
1606 * mmc_blk_rw_try_restart() - tries to restart the current async request
1607 * @mq: the queue with the card and host to restart
1608 * @req: a new request that want to be started after the current one
1609 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001610static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1611 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001612{
1613 if (!req)
1614 return;
1615
Linus Walleijb2928e12017-02-01 13:47:54 +01001616 /*
1617 * If the card was removed, just cancel everything and return.
1618 */
1619 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001620 req->rq_flags |= RQF_QUIET;
1621 blk_end_request_all(req, -EIO);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001622 mmc_queue_req_free(mq, mqrq);
Linus Walleijb2928e12017-02-01 13:47:54 +01001623 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001624 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001625 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001626 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1627 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001628}
1629
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001630static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001631{
Linus Walleij7db30282016-11-18 13:36:15 +01001632 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001633 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001634 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001635 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001636 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001637 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001638 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001639 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001640 struct mmc_async_req *new_areq;
1641 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001642 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001643
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001644 if (new_req) {
1645 mqrq_cur = mmc_queue_req_find(mq, new_req);
1646 if (!mqrq_cur) {
1647 WARN_ON(1);
1648 mmc_blk_requeue(mq->queue, new_req);
1649 new_req = NULL;
1650 }
1651 }
1652
1653 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001654 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001655
1656 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001657 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301658 /*
1659 * When 4KB native sector is enabled, only 8 blocks
1660 * multiple read or write is allowed
1661 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001662 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001663 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301664 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001665 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001666 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001667 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301668 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001669
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001670 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1671 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001672 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001673 new_areq = NULL;
1674
Linus Walleijc3399ef2017-02-01 13:47:53 +01001675 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001676 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001677 /*
1678 * We have just put the first request into the pipeline
1679 * and there is nothing more to do until it is
1680 * complete.
1681 */
Linus Walleijdf061582017-01-24 11:17:57 +01001682 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001683 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001684
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001685 /*
1686 * An asynchronous request has been completed and we proceed
1687 * to handle the result of it.
1688 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001689 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001690 brq = &mq_rq->brq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001691 old_req = mq_rq->req;
1692 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001693 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001694
Per Forlind78d4a8a2011-07-01 18:55:30 +02001695 switch (status) {
1696 case MMC_BLK_SUCCESS:
1697 case MMC_BLK_PARTIAL:
1698 /*
1699 * A block was successfully transferred.
1700 */
Adrian Hunter67716322011-08-29 16:42:15 +03001701 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001702
Linus Walleij0e65f102017-02-01 13:47:58 +01001703 req_pending = blk_end_request(old_req, 0,
1704 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001705 /*
1706 * If the blk_end_request function returns non-zero even
1707 * though all data has been transferred and no errors
1708 * were returned by the host controller, it's a bug.
1709 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001710 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301711 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001712 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001713 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001714 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001715 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001716 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001717 break;
1718 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001719 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001720 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001721 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001722 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1723 else
1724 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001725 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001726 return;
1727 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001728 if (!req_pending) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001729 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001730 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001731 return;
1732 }
Ding Wang29535f72015-05-18 20:14:15 +08001733 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001734 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001735 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001736 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001737 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001738 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001739 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001740 if (!mmc_blk_reset(md, card->host, type))
1741 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001742 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001743 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001744 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001745 case MMC_BLK_DATA_ERR: {
1746 int err;
1747
1748 err = mmc_blk_reset(md, card->host, type);
1749 if (!err)
1750 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001751 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001752 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001753 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001754 return;
1755 }
Adrian Hunter67716322011-08-29 16:42:15 +03001756 /* Fall through */
1757 }
1758 case MMC_BLK_ECC_ERR:
1759 if (brq->data.blocks > 1) {
1760 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001761 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001762 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001763 disable_multi = 1;
1764 break;
1765 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001766 /*
1767 * After an error, we redo I/O one sector at a
1768 * time, so we only reach here after trying to
1769 * read a single sector.
1770 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001771 req_pending = blk_end_request(old_req, -EIO,
1772 brq->data.blksz);
1773 if (!req_pending) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001774 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001775 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001776 return;
1777 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001778 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301779 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001780 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001781 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001782 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001783 default:
1784 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001785 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001786 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001787 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001788 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001789 }
1790
Linus Walleij0e65f102017-02-01 13:47:58 +01001791 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001792 /*
1793 * In case of a incomplete request
1794 * prepare it again and resend.
1795 */
1796 mmc_blk_rw_rq_prep(mq_rq, card,
1797 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001798 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001799 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001800 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001801 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001802 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001803
1804 mmc_queue_req_free(mq, mq_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Linus Walleijdf061582017-01-24 11:17:57 +01001807void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001808{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001809 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001810 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001811 struct mmc_card *card = md->queue.card;
1812
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001813 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001814 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001815 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001816
Andrei Warkentin371a6892011-04-11 18:10:25 -05001817 ret = mmc_blk_part_switch(card, md);
1818 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001819 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301820 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001821 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001822 goto out;
1823 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001824
Mike Christiec2df40d2016-06-05 14:32:17 -05001825 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001826 /* complete ongoing async transfer before issuing discard */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001827 if (mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001828 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001829 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001830 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1831 /* complete ongoing async transfer before issuing secure erase*/
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001832 if (mq->qcnt)
Christoph Hellwig288dab82016-06-09 16:00:36 +02001833 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001834 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001835 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001836 /* complete ongoing async transfer before issuing flush */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001837 if (mq->qcnt)
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001838 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001839 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001840 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001841 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter2602b742017-03-13 14:36:32 +02001842 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07001843 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001844
Andrei Warkentin371a6892011-04-11 18:10:25 -05001845out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001846 if (!mq->qcnt)
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001847 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Russell Kinga6f6c962006-01-03 22:38:44 +00001850static inline int mmc_blk_readonly(struct mmc_card *card)
1851{
1852 return mmc_card_readonly(card) ||
1853 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1854}
1855
Andrei Warkentin371a6892011-04-11 18:10:25 -05001856static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1857 struct device *parent,
1858 sector_t size,
1859 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001860 const char *subname,
1861 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct mmc_blk_data *md;
1864 int devidx, ret;
1865
Heiner Kallweita04848c2017-02-01 19:44:22 +01001866 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1867 if (devidx < 0)
1868 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001870 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001871 if (!md) {
1872 ret = -ENOMEM;
1873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001875
Johan Rudholmadd710e2011-12-02 08:51:06 +01001876 md->area_type = area_type;
1877
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001878 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001879 * Set the read-only status based on the supported commands
1880 * and the write protect switch.
1881 */
1882 md->read_only = mmc_blk_readonly(card);
1883
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001884 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001885 if (md->disk == NULL) {
1886 ret = -ENOMEM;
1887 goto err_kfree;
1888 }
1889
1890 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001891 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001892 md->usage = 1;
1893
Adrian Hunterd09408a2011-06-23 13:40:28 +03001894 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001895 if (ret)
1896 goto err_putdisk;
1897
Linus Walleij7db30282016-11-18 13:36:15 +01001898 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001899
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001900 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001901 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001902 md->disk->fops = &mmc_bdops;
1903 md->disk->private_data = md;
1904 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001905 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001906 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001907 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001908 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001909 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001910
1911 /*
1912 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1913 *
1914 * - be set for removable media with permanent block devices
1915 * - be unset for removable block devices with permanent media
1916 *
1917 * Since MMC block devices clearly fall under the second
1918 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1919 * should use the block device creation/destruction hotplug
1920 * messages to tell when the card is present.
1921 */
1922
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001923 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001924 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001925
Saugata Dasa5075eb2012-05-17 16:32:21 +05301926 if (mmc_card_mmc(card))
1927 blk_queue_logical_block_size(md->queue.queue,
1928 card->ext_csd.data_sector_size);
1929 else
1930 blk_queue_logical_block_size(md->queue.queue, 512);
1931
Andrei Warkentin371a6892011-04-11 18:10:25 -05001932 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001933
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001934 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001935 if ((mmc_card_mmc(card) &&
1936 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001937 (mmc_card_sd(card) &&
1938 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1939 md->flags |= MMC_BLK_CMD23;
1940 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001941
1942 if (mmc_card_mmc(card) &&
1943 md->flags & MMC_BLK_CMD23 &&
1944 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1945 card->ext_csd.rel_sectors)) {
1946 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001947 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001948 }
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001951
1952 err_putdisk:
1953 put_disk(md->disk);
1954 err_kfree:
1955 kfree(md);
1956 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01001957 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00001958 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Andrei Warkentin371a6892011-04-11 18:10:25 -05001961static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1962{
1963 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001964
1965 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1966 /*
1967 * The EXT_CSD sector count is in number or 512 byte
1968 * sectors.
1969 */
1970 size = card->ext_csd.sectors;
1971 } else {
1972 /*
1973 * The CSD capacity field is in units of read_blkbits.
1974 * set_capacity takes units of 512 bytes.
1975 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001976 size = (typeof(sector_t))card->csd.capacity
1977 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001978 }
1979
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001980 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001981 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001982}
1983
1984static int mmc_blk_alloc_part(struct mmc_card *card,
1985 struct mmc_blk_data *md,
1986 unsigned int part_type,
1987 sector_t size,
1988 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001989 const char *subname,
1990 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001991{
1992 char cap_str[10];
1993 struct mmc_blk_data *part_md;
1994
1995 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001996 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001997 if (IS_ERR(part_md))
1998 return PTR_ERR(part_md);
1999 part_md->part_type = part_type;
2000 list_add(&part_md->part, &md->part);
2001
James Bottomleyb9f28d82015-03-05 18:47:01 -08002002 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002003 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302004 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002005 part_md->disk->disk_name, mmc_card_id(card),
2006 mmc_card_name(card), part_md->part_type, cap_str);
2007 return 0;
2008}
2009
Namjae Jeone0c368d2011-10-06 23:41:38 +09002010/* MMC Physical partitions consist of two boot partitions and
2011 * up to four general purpose partitions.
2012 * For each partition enabled in EXT_CSD a block device will be allocatedi
2013 * to provide access to the partition.
2014 */
2015
Andrei Warkentin371a6892011-04-11 18:10:25 -05002016static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2017{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002018 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002019
2020 if (!mmc_card_mmc(card))
2021 return 0;
2022
Namjae Jeone0c368d2011-10-06 23:41:38 +09002023 for (idx = 0; idx < card->nr_parts; idx++) {
2024 if (card->part[idx].size) {
2025 ret = mmc_blk_alloc_part(card, md,
2026 card->part[idx].part_cfg,
2027 card->part[idx].size >> 9,
2028 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002029 card->part[idx].name,
2030 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002031 if (ret)
2032 return ret;
2033 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002034 }
2035
2036 return ret;
2037}
2038
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039static void mmc_blk_remove_req(struct mmc_blk_data *md)
2040{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002041 struct mmc_card *card;
2042
Andrei Warkentin371a6892011-04-11 18:10:25 -05002043 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002044 /*
2045 * Flush remaining requests and free queues. It
2046 * is freeing the queue that stops new requests
2047 * from being accepted.
2048 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002049 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002050 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002051 if (md->disk->flags & GENHD_FL_UP) {
2052 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002053 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2054 card->ext_csd.boot_ro_lockable)
2055 device_remove_file(disk_to_dev(md->disk),
2056 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002057
Andrei Warkentin371a6892011-04-11 18:10:25 -05002058 del_gendisk(md->disk);
2059 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060 mmc_blk_put(md);
2061 }
2062}
2063
2064static void mmc_blk_remove_parts(struct mmc_card *card,
2065 struct mmc_blk_data *md)
2066{
2067 struct list_head *pos, *q;
2068 struct mmc_blk_data *part_md;
2069
2070 list_for_each_safe(pos, q, &md->part) {
2071 part_md = list_entry(pos, struct mmc_blk_data, part);
2072 list_del(pos);
2073 mmc_blk_remove_req(part_md);
2074 }
2075}
2076
2077static int mmc_add_disk(struct mmc_blk_data *md)
2078{
2079 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002080 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002081
Dan Williams307d8e62016-06-20 10:40:44 -07002082 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002083 md->force_ro.show = force_ro_show;
2084 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302085 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002086 md->force_ro.attr.name = "force_ro";
2087 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2088 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2089 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002090 goto force_ro_fail;
2091
2092 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2093 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002094 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002095
2096 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2097 mode = S_IRUGO;
2098 else
2099 mode = S_IRUGO | S_IWUSR;
2100
2101 md->power_ro_lock.show = power_ro_lock_show;
2102 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002103 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002104 md->power_ro_lock.attr.mode = mode;
2105 md->power_ro_lock.attr.name =
2106 "ro_lock_until_next_power_on";
2107 ret = device_create_file(disk_to_dev(md->disk),
2108 &md->power_ro_lock);
2109 if (ret)
2110 goto power_ro_lock_fail;
2111 }
2112 return ret;
2113
2114power_ro_lock_fail:
2115 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2116force_ro_fail:
2117 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002118
2119 return ret;
2120}
2121
Ulf Hansson96541ba2015-04-14 13:06:12 +02002122static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002124 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002125 char cap_str[10];
2126
Pierre Ossman912490d2005-05-21 10:27:02 +01002127 /*
2128 * Check that the card supports the command class(es) we need.
2129 */
2130 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return -ENODEV;
2132
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002133 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 md = mmc_blk_alloc(card);
2136 if (IS_ERR(md))
2137 return PTR_ERR(md);
2138
James Bottomleyb9f28d82015-03-05 18:47:01 -08002139 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002140 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302141 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002143 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Andrei Warkentin371a6892011-04-11 18:10:25 -05002145 if (mmc_blk_alloc_parts(card, md))
2146 goto out;
2147
Ulf Hansson96541ba2015-04-14 13:06:12 +02002148 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002149
Andrei Warkentin371a6892011-04-11 18:10:25 -05002150 if (mmc_add_disk(md))
2151 goto out;
2152
2153 list_for_each_entry(part_md, &md->part, part) {
2154 if (mmc_add_disk(part_md))
2155 goto out;
2156 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002157
2158 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2159 pm_runtime_use_autosuspend(&card->dev);
2160
2161 /*
2162 * Don't enable runtime PM for SD-combo cards here. Leave that
2163 * decision to be taken during the SDIO init sequence instead.
2164 */
2165 if (card->type != MMC_TYPE_SD_COMBO) {
2166 pm_runtime_set_active(&card->dev);
2167 pm_runtime_enable(&card->dev);
2168 }
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171
2172 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002173 mmc_blk_remove_parts(card, md);
2174 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Ulf Hansson96541ba2015-04-14 13:06:12 +02002178static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002180 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002183 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002184 mmc_claim_host(card->host);
2185 mmc_blk_part_switch(card, md);
2186 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002187 if (card->type != MMC_TYPE_SD_COMBO)
2188 pm_runtime_disable(&card->dev);
2189 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002190 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002191 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Ulf Hansson96541ba2015-04-14 13:06:12 +02002194static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002196 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002197 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 if (md) {
2200 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002201 list_for_each_entry(part_md, &md->part, part) {
2202 mmc_queue_suspend(&part_md->queue);
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 return 0;
2206}
2207
Ulf Hansson96541ba2015-04-14 13:06:12 +02002208static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002209{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002210 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002211}
2212
Ulf Hansson0967edc2014-10-06 11:29:42 +02002213#ifdef CONFIG_PM_SLEEP
2214static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002215{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002216 struct mmc_card *card = mmc_dev_to_card(dev);
2217
2218 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002219}
2220
Ulf Hansson0967edc2014-10-06 11:29:42 +02002221static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002223 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002224 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002227 /*
2228 * Resume involves the card going into idle state,
2229 * so current partition is always the main one.
2230 */
2231 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002233 list_for_each_entry(part_md, &md->part, part) {
2234 mmc_queue_resume(&part_md->queue);
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 return 0;
2238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239#endif
2240
Ulf Hansson0967edc2014-10-06 11:29:42 +02002241static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2242
Ulf Hansson96541ba2015-04-14 13:06:12 +02002243static struct mmc_driver mmc_driver = {
2244 .drv = {
2245 .name = "mmcblk",
2246 .pm = &mmc_blk_pm_ops,
2247 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 .probe = mmc_blk_probe,
2249 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002250 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251};
2252
2253static int __init mmc_blk_init(void)
2254{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002255 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002257 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2258 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2259
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002260 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002261
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002262 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2263 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002266 res = mmc_register_driver(&mmc_driver);
2267 if (res)
2268 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002270 return 0;
2271 out2:
2272 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 out:
2274 return res;
2275}
2276
2277static void __exit mmc_blk_exit(void)
2278{
2279 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002280 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283module_init(mmc_blk_init);
2284module_exit(mmc_blk_exit);
2285
2286MODULE_LICENSE("GPL");
2287MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2288