blob: d189e8c4436e2f96bf0b6c9302bed1a6a952f76f [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
Adrian Hunter025e3d52017-03-13 14:36:39 +0200731static int mmc_blk_part_switch_pre(struct mmc_card *card,
732 unsigned int part_type)
733{
734 int ret = 0;
735
736 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
737 if (card->ext_csd.cmdq_en) {
738 ret = mmc_cmdq_disable(card);
739 if (ret)
740 return ret;
741 }
742 mmc_retune_pause(card->host);
743 }
744
745 return ret;
746}
747
748static int mmc_blk_part_switch_post(struct mmc_card *card,
749 unsigned int part_type)
750{
751 int ret = 0;
752
753 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
754 mmc_retune_unpause(card->host);
755 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
756 ret = mmc_cmdq_enable(card);
757 }
758
759 return ret;
760}
761
Andrei Warkentin371a6892011-04-11 18:10:25 -0500762static inline int mmc_blk_part_switch(struct mmc_card *card,
763 struct mmc_blk_data *md)
764{
Adrian Hunter025e3d52017-03-13 14:36:39 +0200765 int ret = 0;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200766 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300767
Andrei Warkentin371a6892011-04-11 18:10:25 -0500768 if (main_md->part_curr == md->part_type)
769 return 0;
770
771 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300772 u8 part_config = card->ext_csd.part_config;
773
Adrian Hunter025e3d52017-03-13 14:36:39 +0200774 ret = mmc_blk_part_switch_pre(card, md->part_type);
775 if (ret)
776 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300777
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300778 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
779 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500780
781 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300782 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500783 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300784 if (ret) {
Adrian Hunter025e3d52017-03-13 14:36:39 +0200785 mmc_blk_part_switch_post(card, md->part_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500786 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300787 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300788
789 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300790
Adrian Hunter025e3d52017-03-13 14:36:39 +0200791 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
Adrian Hunter67716322011-08-29 16:42:15 +0300792 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500793
794 main_md->part_curr = md->part_type;
Adrian Hunter025e3d52017-03-13 14:36:39 +0200795 return ret;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500796}
797
Linus Walleij169f03a2017-02-01 13:47:57 +0100798static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700799{
800 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100801 u32 result;
802 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700803
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900804 struct mmc_request mrq = {};
805 struct mmc_command cmd = {};
806 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700807
808 struct scatterlist sg;
809
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700810 cmd.opcode = MMC_APP_CMD;
811 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700812 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813
814 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700815 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100816 return err;
David Brownell7213d172007-08-08 09:10:23 -0700817 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100818 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819
820 memset(&cmd, 0, sizeof(struct mmc_command));
821
822 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
823 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700824 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700825
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700826 data.blksz = 4;
827 data.blocks = 1;
828 data.flags = MMC_DATA_READ;
829 data.sg = &sg;
830 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530831 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700832
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700833 mrq.cmd = &cmd;
834 mrq.data = &data;
835
Ben Dooks051913d2009-06-08 23:33:57 +0100836 blocks = kmalloc(4, GFP_KERNEL);
837 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100838 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100839
840 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700841
842 mmc_wait_for_req(card->host, &mrq);
843
Ben Dooks051913d2009-06-08 23:33:57 +0100844 result = ntohl(*blocks);
845 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700846
Ben Dooks051913d2009-06-08 23:33:57 +0100847 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100848 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100849
Linus Walleij169f03a2017-02-01 13:47:57 +0100850 *written_blocks = result;
851
852 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700853}
854
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100855static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300856{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900857 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300858 int err;
859
Adrian Hunter504f1912008-10-16 12:55:25 +0300860 cmd.opcode = MMC_SEND_STATUS;
861 if (!mmc_host_is_spi(card->host))
862 cmd.arg = card->rca << 16;
863 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100864 err = mmc_wait_for_cmd(card->host, &cmd, retries);
865 if (err == 0)
866 *status = cmd.resp[0];
867 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300868}
869
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100870static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100871 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100872{
873 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
874 int err = 0;
875 u32 status;
876
877 do {
878 err = get_card_status(card, &status, 5);
879 if (err) {
880 pr_err("%s: error %d requesting status\n",
881 req->rq_disk->disk_name, err);
882 return err;
883 }
884
885 if (status & R1_ERROR) {
886 pr_err("%s: %s: error sending status cmd, status %#x\n",
887 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100888 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100889 }
890
Ulf Hansson95a91292014-01-29 13:11:27 +0100891 /* We may rely on the host hw to handle busy detection.*/
892 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
893 hw_busy_detect)
894 break;
895
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100896 /*
897 * Timeout if the device never becomes ready for data and never
898 * leaves the program state.
899 */
900 if (time_after(jiffies, timeout)) {
901 pr_err("%s: Card stuck in programming state! %s %s\n",
902 mmc_hostname(card->host),
903 req->rq_disk->disk_name, __func__);
904 return -ETIMEDOUT;
905 }
906
907 /*
908 * Some cards mishandle the status bits,
909 * so make sure to check both the busy
910 * indication and the card state.
911 */
912 } while (!(status & R1_READY_FOR_DATA) ||
913 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
914
915 return err;
916}
917
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100918static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100919 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100920{
921 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900922 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100923 int err;
924 bool use_r1b_resp = rq_data_dir(req) == WRITE;
925
926 /*
927 * Normally we use R1B responses for WRITE, but in cases where the host
928 * has specified a max_busy_timeout we need to validate it. A failure
929 * means we need to prevent the host from doing hw busy detection, which
930 * is done by converting to a R1 response instead.
931 */
932 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
933 use_r1b_resp = false;
934
935 cmd.opcode = MMC_STOP_TRANSMISSION;
936 if (use_r1b_resp) {
937 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
938 cmd.busy_timeout = timeout_ms;
939 } else {
940 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
941 }
942
943 err = mmc_wait_for_cmd(host, &cmd, 5);
944 if (err)
945 return err;
946
947 *stop_status = cmd.resp[0];
948
949 /* No need to check card status in case of READ. */
950 if (rq_data_dir(req) == READ)
951 return 0;
952
953 if (!mmc_host_is_spi(host) &&
954 (*stop_status & R1_ERROR)) {
955 pr_err("%s: %s: general error sending stop command, resp %#x\n",
956 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100957 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100958 }
959
960 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
961}
962
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530963#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100964#define ERR_RETRY 2
965#define ERR_ABORT 1
966#define ERR_CONTINUE 0
967
968static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
969 bool status_valid, u32 status)
970{
971 switch (error) {
972 case -EILSEQ:
973 /* response crc error, retry the r/w cmd */
974 pr_err("%s: %s sending %s command, card status %#x\n",
975 req->rq_disk->disk_name, "response CRC error",
976 name, status);
977 return ERR_RETRY;
978
979 case -ETIMEDOUT:
980 pr_err("%s: %s sending %s command, card status %#x\n",
981 req->rq_disk->disk_name, "timed out", name, status);
982
983 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530984 if (!status_valid) {
985 pr_err("%s: status not valid, retrying timeout\n",
986 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100987 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530988 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100989
990 /*
991 * If it was a r/w cmd crc error, or illegal command
992 * (eg, issued in wrong state) then retry - we should
993 * have corrected the state problem above.
994 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530995 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
996 pr_err("%s: command error, retrying timeout\n",
997 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100998 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530999 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001000
1001 /* Otherwise abort the command */
1002 return ERR_ABORT;
1003
1004 default:
1005 /* We don't understand the error code the driver gave us */
1006 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1007 req->rq_disk->disk_name, error, status);
1008 return ERR_ABORT;
1009 }
1010}
1011
1012/*
1013 * Initial r/w and stop cmd error recovery.
1014 * We don't know whether the card received the r/w cmd or not, so try to
1015 * restore things back to a sane state. Essentially, we do this as follows:
1016 * - Obtain card status. If the first attempt to obtain card status fails,
1017 * the status word will reflect the failed status cmd, not the failed
1018 * r/w cmd. If we fail to obtain card status, it suggests we can no
1019 * longer communicate with the card.
1020 * - Check the card state. If the card received the cmd but there was a
1021 * transient problem with the response, it might still be in a data transfer
1022 * mode. Try to send it a stop command. If this fails, we can't recover.
1023 * - If the r/w cmd failed due to a response CRC error, it was probably
1024 * transient, so retry the cmd.
1025 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1026 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1027 * illegal cmd, retry.
1028 * Otherwise we don't understand what happened, so abort.
1029 */
1030static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001031 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001032{
1033 bool prev_cmd_status_valid = true;
1034 u32 status, stop_status = 0;
1035 int err, retry;
1036
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301037 if (mmc_card_removed(card))
1038 return ERR_NOMEDIUM;
1039
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001040 /*
1041 * Try to get card status which indicates both the card state
1042 * and why there was no response. If the first attempt fails,
1043 * we can't be sure the returned status is for the r/w command.
1044 */
1045 for (retry = 2; retry >= 0; retry--) {
1046 err = get_card_status(card, &status, 0);
1047 if (!err)
1048 break;
1049
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001050 /* Re-tune if needed */
1051 mmc_retune_recheck(card->host);
1052
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053 prev_cmd_status_valid = false;
1054 pr_err("%s: error %d sending status command, %sing\n",
1055 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1056 }
1057
1058 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301059 if (err) {
1060 /* Check if the card is removed */
1061 if (mmc_detect_card_removed(card->host))
1062 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001063 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301064 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001065
Adrian Hunter67716322011-08-29 16:42:15 +03001066 /* Flag ECC errors */
1067 if ((status & R1_CARD_ECC_FAILED) ||
1068 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1069 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001070 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001071
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001072 /* Flag General errors */
1073 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1074 if ((status & R1_ERROR) ||
1075 (brq->stop.resp[0] & R1_ERROR)) {
1076 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1077 req->rq_disk->disk_name, __func__,
1078 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001079 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001080 }
1081
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001082 /*
1083 * Check the current card state. If it is in some data transfer
1084 * mode, tell it to stop (and hopefully transition back to TRAN.)
1085 */
1086 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1087 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001088 err = send_stop(card,
1089 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1090 req, gen_err, &stop_status);
1091 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001092 pr_err("%s: error %d sending stop command\n",
1093 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001094 /*
1095 * If the stop cmd also timed out, the card is probably
1096 * not present, so abort. Other errors are bad news too.
1097 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001098 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001099 }
1100
Adrian Hunter67716322011-08-29 16:42:15 +03001101 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001102 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001103 }
1104
1105 /* Check for set block count errors */
1106 if (brq->sbc.error)
1107 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1108 prev_cmd_status_valid, status);
1109
1110 /* Check for r/w command errors */
1111 if (brq->cmd.error)
1112 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1113 prev_cmd_status_valid, status);
1114
Adrian Hunter67716322011-08-29 16:42:15 +03001115 /* Data errors */
1116 if (!brq->stop.error)
1117 return ERR_CONTINUE;
1118
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001119 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001120 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 +01001121 req->rq_disk->disk_name, brq->stop.error,
1122 brq->cmd.resp[0], status);
1123
1124 /*
1125 * Subsitute in our own stop status as this will give the error
1126 * state which happened during the execution of the r/w command.
1127 */
1128 if (stop_status) {
1129 brq->stop.resp[0] = stop_status;
1130 brq->stop.error = 0;
1131 }
1132 return ERR_CONTINUE;
1133}
1134
Adrian Hunter67716322011-08-29 16:42:15 +03001135static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1136 int type)
1137{
1138 int err;
1139
1140 if (md->reset_done & type)
1141 return -EEXIST;
1142
1143 md->reset_done |= type;
1144 err = mmc_hw_reset(host);
1145 /* Ensure we switch back to the correct partition */
1146 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001147 struct mmc_blk_data *main_md =
1148 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001149 int part_err;
1150
1151 main_md->part_curr = main_md->part_type;
1152 part_err = mmc_blk_part_switch(host->card, md);
1153 if (part_err) {
1154 /*
1155 * We have failed to get back into the correct
1156 * partition, so we need to abort the whole request.
1157 */
1158 return -ENODEV;
1159 }
1160 }
1161 return err;
1162}
1163
1164static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1165{
1166 md->reset_done &= ~type;
1167}
1168
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001169int mmc_access_rpmb(struct mmc_queue *mq)
1170{
Linus Walleij7db30282016-11-18 13:36:15 +01001171 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001172 /*
1173 * If this is a RPMB partition access, return ture
1174 */
1175 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1176 return true;
1177
1178 return false;
1179}
1180
Linus Walleijdf061582017-01-24 11:17:57 +01001181static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001182{
Linus Walleij7db30282016-11-18 13:36:15 +01001183 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001184 struct mmc_card *card = md->queue.card;
1185 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001186 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001187
Adrian Hunterbd788c92010-08-11 14:17:47 -07001188 if (!mmc_can_erase(card)) {
1189 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001190 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001191 }
1192
1193 from = blk_rq_pos(req);
1194 nr = blk_rq_sectors(req);
1195
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001196 if (mmc_can_discard(card))
1197 arg = MMC_DISCARD_ARG;
1198 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001199 arg = MMC_TRIM_ARG;
1200 else
1201 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001202 do {
1203 err = 0;
1204 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1205 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1206 INAND_CMD38_ARG_EXT_CSD,
1207 arg == MMC_TRIM_ARG ?
1208 INAND_CMD38_ARG_TRIM :
1209 INAND_CMD38_ARG_ERASE,
1210 0);
1211 }
1212 if (!err)
1213 err = mmc_erase(card, from, nr, arg);
1214 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001215 if (!err)
1216 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001217fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301218 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001219}
1220
Linus Walleijdf061582017-01-24 11:17:57 +01001221static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001222 struct request *req)
1223{
Linus Walleij7db30282016-11-18 13:36:15 +01001224 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001225 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001226 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001227 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001228
Maya Erez775a9362013-04-18 15:41:55 +03001229 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001230 err = -EOPNOTSUPP;
1231 goto out;
1232 }
1233
1234 from = blk_rq_pos(req);
1235 nr = blk_rq_sectors(req);
1236
Maya Erez775a9362013-04-18 15:41:55 +03001237 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1238 arg = MMC_SECURE_TRIM1_ARG;
1239 else
1240 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001241
Adrian Hunter67716322011-08-29 16:42:15 +03001242retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001243 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245 INAND_CMD38_ARG_EXT_CSD,
1246 arg == MMC_SECURE_TRIM1_ARG ?
1247 INAND_CMD38_ARG_SECTRIM1 :
1248 INAND_CMD38_ARG_SECERASE,
1249 0);
1250 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001251 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001252 }
Adrian Hunter28302812012-04-05 14:45:48 +03001253
Adrian Hunter49804542010-08-11 14:17:50 -07001254 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001255 if (err == -EIO)
1256 goto out_retry;
1257 if (err)
1258 goto out;
1259
1260 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001261 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1262 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1263 INAND_CMD38_ARG_EXT_CSD,
1264 INAND_CMD38_ARG_SECTRIM2,
1265 0);
1266 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001267 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001268 }
Adrian Hunter28302812012-04-05 14:45:48 +03001269
Adrian Hunter49804542010-08-11 14:17:50 -07001270 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001271 if (err == -EIO)
1272 goto out_retry;
1273 if (err)
1274 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001275 }
Adrian Hunter28302812012-04-05 14:45:48 +03001276
Adrian Hunter28302812012-04-05 14:45:48 +03001277out_retry:
1278 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001279 goto retry;
1280 if (!err)
1281 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001282out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301283 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001284}
1285
Linus Walleijdf061582017-01-24 11:17:57 +01001286static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001287{
Linus Walleij7db30282016-11-18 13:36:15 +01001288 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001289 struct mmc_card *card = md->queue.card;
1290 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001291
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001292 ret = mmc_flush_cache(card);
1293 if (ret)
1294 ret = -EIO;
1295
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301296 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001297}
1298
1299/*
1300 * Reformat current write as a reliable write, supporting
1301 * both legacy and the enhanced reliable write MMC cards.
1302 * In each transfer we'll handle only as much as a single
1303 * reliable write can handle, thus finish the request in
1304 * partial completions.
1305 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001306static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1307 struct mmc_card *card,
1308 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001309{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001310 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1311 /* Legacy mode imposes restrictions on transfers. */
1312 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1313 brq->data.blocks = 1;
1314
1315 if (brq->data.blocks > card->ext_csd.rel_sectors)
1316 brq->data.blocks = card->ext_csd.rel_sectors;
1317 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1318 brq->data.blocks = 1;
1319 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001320}
1321
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001322#define CMD_ERRORS \
1323 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1324 R1_ADDRESS_ERROR | /* Misaligned address */ \
1325 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1326 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1327 R1_CC_ERROR | /* Card controller error */ \
1328 R1_ERROR) /* General/unknown error */
1329
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001330static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1331 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001332{
Per Forlinee8a43a2011-07-01 18:55:33 +02001333 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001334 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001335 struct mmc_blk_request *brq = &mq_mrq->brq;
1336 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001337 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001338 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001339 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001340
1341 /*
1342 * sbc.error indicates a problem with the set block count
1343 * command. No data will have been transferred.
1344 *
1345 * cmd.error indicates a problem with the r/w command. No
1346 * data will have been transferred.
1347 *
1348 * stop.error indicates a problem with the stop command. Data
1349 * may have been transferred, or may still be transferring.
1350 */
Adrian Hunter67716322011-08-29 16:42:15 +03001351 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1352 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001353 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001354 case ERR_RETRY:
1355 return MMC_BLK_RETRY;
1356 case ERR_ABORT:
1357 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301358 case ERR_NOMEDIUM:
1359 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001360 case ERR_CONTINUE:
1361 break;
1362 }
1363 }
1364
1365 /*
1366 * Check for errors relating to the execution of the
1367 * initial command - such as address errors. No data
1368 * has been transferred.
1369 */
1370 if (brq->cmd.resp[0] & CMD_ERRORS) {
1371 pr_err("%s: r/w command failed, status = %#x\n",
1372 req->rq_disk->disk_name, brq->cmd.resp[0]);
1373 return MMC_BLK_ABORT;
1374 }
1375
1376 /*
1377 * Everything else is either success, or a data error of some
1378 * kind. If it was a write, we may have transitioned to
1379 * program mode, which we have to wait for it to complete.
1380 */
1381 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001382 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001383
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001384 /* Check stop command response */
1385 if (brq->stop.resp[0] & R1_ERROR) {
1386 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1387 req->rq_disk->disk_name, __func__,
1388 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001389 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001390 }
1391
Ulf Hansson95a91292014-01-29 13:11:27 +01001392 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1393 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001394 if (err)
1395 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001396 }
1397
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001398 /* if general error occurs, retry the write operation. */
1399 if (gen_err) {
1400 pr_warn("%s: retrying write for general error\n",
1401 req->rq_disk->disk_name);
1402 return MMC_BLK_RETRY;
1403 }
1404
Per Forlind78d4a8a2011-07-01 18:55:30 +02001405 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001406 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001407 pr_debug("%s: retrying because a re-tune was needed\n",
1408 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001409 brq->retune_retry_done = 1;
1410 return MMC_BLK_RETRY;
1411 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001412 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1413 req->rq_disk->disk_name, brq->data.error,
1414 (unsigned)blk_rq_pos(req),
1415 (unsigned)blk_rq_sectors(req),
1416 brq->cmd.resp[0], brq->stop.resp[0]);
1417
1418 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001419 if (ecc_err)
1420 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001421 return MMC_BLK_DATA_ERR;
1422 } else {
1423 return MMC_BLK_CMD_ERR;
1424 }
1425 }
1426
Adrian Hunter67716322011-08-29 16:42:15 +03001427 if (!brq->data.bytes_xfered)
1428 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001429
Adrian Hunter67716322011-08-29 16:42:15 +03001430 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1431 return MMC_BLK_PARTIAL;
1432
1433 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001434}
1435
Per Forlin54d49d72011-07-01 18:55:29 +02001436static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1437 struct mmc_card *card,
1438 int disable_multi,
1439 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Per Forlin54d49d72011-07-01 18:55:29 +02001441 u32 readcmd, writecmd;
1442 struct mmc_blk_request *brq = &mqrq->brq;
1443 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001444 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301445 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001447 /*
1448 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001449 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001450 */
Luca Porziod3df0462015-11-06 15:12:26 +00001451 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001452 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001453 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001454
Per Forlin54d49d72011-07-01 18:55:29 +02001455 memset(brq, 0, sizeof(struct mmc_blk_request));
1456 brq->mrq.cmd = &brq->cmd;
1457 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Per Forlin54d49d72011-07-01 18:55:29 +02001459 brq->cmd.arg = blk_rq_pos(req);
1460 if (!mmc_card_blockaddr(card))
1461 brq->cmd.arg <<= 9;
1462 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1463 brq->data.blksz = 512;
1464 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1465 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001466 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Per Forlin54d49d72011-07-01 18:55:29 +02001468 /*
1469 * The block layer doesn't support all sector count
1470 * restrictions, so we need to be prepared for too big
1471 * requests.
1472 */
1473 if (brq->data.blocks > card->host->max_blk_count)
1474 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001476 if (brq->data.blocks > 1) {
1477 /*
1478 * After a read error, we redo the request one sector
1479 * at a time in order to accurately determine which
1480 * sectors can be read successfully.
1481 */
1482 if (disable_multi)
1483 brq->data.blocks = 1;
1484
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001485 /*
1486 * Some controllers have HW issues while operating
1487 * in multiple I/O mode
1488 */
1489 if (card->host->ops->multi_io_quirk)
1490 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1491 (rq_data_dir(req) == READ) ?
1492 MMC_DATA_READ : MMC_DATA_WRITE,
1493 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001494 }
Per Forlin54d49d72011-07-01 18:55:29 +02001495
1496 if (brq->data.blocks > 1 || do_rel_wr) {
1497 /* SPI multiblock writes terminate using a special
1498 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001499 */
Per Forlin54d49d72011-07-01 18:55:29 +02001500 if (!mmc_host_is_spi(card->host) ||
1501 rq_data_dir(req) == READ)
1502 brq->mrq.stop = &brq->stop;
1503 readcmd = MMC_READ_MULTIPLE_BLOCK;
1504 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1505 } else {
1506 brq->mrq.stop = NULL;
1507 readcmd = MMC_READ_SINGLE_BLOCK;
1508 writecmd = MMC_WRITE_BLOCK;
1509 }
1510 if (rq_data_dir(req) == READ) {
1511 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001512 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001513 if (brq->mrq.stop)
1514 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1515 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001516 } else {
1517 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001518 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001519 if (brq->mrq.stop)
1520 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1521 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001522 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001523
Per Forlin54d49d72011-07-01 18:55:29 +02001524 if (do_rel_wr)
1525 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001526
Per Forlin54d49d72011-07-01 18:55:29 +02001527 /*
Saugata Das42659002011-12-21 13:09:17 +05301528 * Data tag is used only during writing meta data to speed
1529 * up write and any subsequent read of this meta data
1530 */
1531 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1532 (req->cmd_flags & REQ_META) &&
1533 (rq_data_dir(req) == WRITE) &&
1534 ((brq->data.blocks * brq->data.blksz) >=
1535 card->ext_csd.data_tag_unit_size);
1536
1537 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001538 * Pre-defined multi-block transfers are preferable to
1539 * open ended-ones (and necessary for reliable writes).
1540 * However, it is not sufficient to just send CMD23,
1541 * and avoid the final CMD12, as on an error condition
1542 * CMD12 (stop) needs to be sent anyway. This, coupled
1543 * with Auto-CMD23 enhancements provided by some
1544 * hosts, means that the complexity of dealing
1545 * with this is best left to the host. If CMD23 is
1546 * supported by card and host, we'll fill sbc in and let
1547 * the host deal with handling it correctly. This means
1548 * that for hosts that don't expose MMC_CAP_CMD23, no
1549 * change of behavior will be observed.
1550 *
1551 * N.B: Some MMC cards experience perf degradation.
1552 * We'll avoid using CMD23-bounded multiblock writes for
1553 * these, while retaining features like reliable writes.
1554 */
Saugata Das42659002011-12-21 13:09:17 +05301555 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1556 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1557 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001558 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1559 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301560 (do_rel_wr ? (1 << 31) : 0) |
1561 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001562 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1563 brq->mrq.sbc = &brq->sbc;
1564 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001565
Per Forlin54d49d72011-07-01 18:55:29 +02001566 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001567
Per Forlin54d49d72011-07-01 18:55:29 +02001568 brq->data.sg = mqrq->sg;
1569 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001570
Per Forlin54d49d72011-07-01 18:55:29 +02001571 /*
1572 * Adjust the sg list so it is the same size as the
1573 * request.
1574 */
1575 if (brq->data.blocks != blk_rq_sectors(req)) {
1576 int i, data_size = brq->data.blocks << 9;
1577 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001578
Per Forlin54d49d72011-07-01 18:55:29 +02001579 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1580 data_size -= sg->length;
1581 if (data_size <= 0) {
1582 sg->length += data_size;
1583 i++;
1584 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001585 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001586 }
Per Forlin54d49d72011-07-01 18:55:29 +02001587 brq->data.sg_len = i;
1588 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001589
Linus Walleij74f5ba32017-02-01 13:47:55 +01001590 mqrq->areq.mrq = &brq->mrq;
1591 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001592
Per Forlin54d49d72011-07-01 18:55:29 +02001593 mmc_queue_bounce_pre(mqrq);
1594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Walleij0e65f102017-02-01 13:47:58 +01001596static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1597 struct mmc_blk_request *brq, struct request *req,
1598 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001599{
Linus Walleij0e65f102017-02-01 13:47:58 +01001600 bool req_pending;
1601
Adrian Hunter67716322011-08-29 16:42:15 +03001602 /*
1603 * If this is an SD card and we're writing, we can first
1604 * mark the known good sectors as ok.
1605 *
1606 * If the card is not SD, we can still ok written sectors
1607 * as reported by the controller (which might be less than
1608 * the real number of written sectors, but never more).
1609 */
1610 if (mmc_card_sd(card)) {
1611 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001612 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001613
Linus Walleij169f03a2017-02-01 13:47:57 +01001614 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001615 if (err)
1616 req_pending = old_req_pending;
1617 else
1618 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001619 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001620 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001621 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001622 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001623}
1624
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001625static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1626 struct request *req,
1627 struct mmc_queue_req *mqrq)
Linus Walleij4e1f7802017-01-24 11:17:52 +01001628{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001629 if (mmc_card_removed(card))
1630 req->rq_flags |= RQF_QUIET;
Linus Walleij0e65f102017-02-01 13:47:58 +01001631 while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req)));
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001632 mmc_queue_req_free(mq, mqrq);
Linus Walleij4e1f7802017-01-24 11:17:52 +01001633}
1634
Linus Walleijb2928e12017-02-01 13:47:54 +01001635/**
1636 * mmc_blk_rw_try_restart() - tries to restart the current async request
1637 * @mq: the queue with the card and host to restart
1638 * @req: a new request that want to be started after the current one
1639 */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001640static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1641 struct mmc_queue_req *mqrq)
Linus Walleijefb5a052017-01-24 11:17:53 +01001642{
1643 if (!req)
1644 return;
1645
Linus Walleijb2928e12017-02-01 13:47:54 +01001646 /*
1647 * If the card was removed, just cancel everything and return.
1648 */
1649 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001650 req->rq_flags |= RQF_QUIET;
1651 blk_end_request_all(req, -EIO);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001652 mmc_queue_req_free(mq, mqrq);
Linus Walleijb2928e12017-02-01 13:47:54 +01001653 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001654 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001655 /* Else proceed and try to restart the current async request */
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001656 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1657 mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001658}
1659
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001660static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001661{
Linus Walleij7db30282016-11-18 13:36:15 +01001662 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001663 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001664 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001665 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001666 enum mmc_blk_status status;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001667 struct mmc_queue_req *mqrq_cur = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +02001668 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001669 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001670 struct mmc_async_req *new_areq;
1671 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001672 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001673
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001674 if (new_req) {
1675 mqrq_cur = mmc_queue_req_find(mq, new_req);
1676 if (!mqrq_cur) {
1677 WARN_ON(1);
1678 mmc_blk_requeue(mq->queue, new_req);
1679 new_req = NULL;
1680 }
1681 }
1682
1683 if (!mq->qcnt)
Linus Walleijdf061582017-01-24 11:17:57 +01001684 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001685
1686 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001687 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301688 /*
1689 * When 4KB native sector is enabled, only 8 blocks
1690 * multiple read or write is allowed
1691 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001692 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001693 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301694 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001695 new_req->rq_disk->disk_name);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001696 mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
Linus Walleijdf061582017-01-24 11:17:57 +01001697 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301698 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001699
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001700 mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1701 new_areq = &mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001702 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001703 new_areq = NULL;
1704
Linus Walleijc3399ef2017-02-01 13:47:53 +01001705 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001706 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001707 /*
1708 * We have just put the first request into the pipeline
1709 * and there is nothing more to do until it is
1710 * complete.
1711 */
Linus Walleijdf061582017-01-24 11:17:57 +01001712 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001713 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001714
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001715 /*
1716 * An asynchronous request has been completed and we proceed
1717 * to handle the result of it.
1718 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001719 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001720 brq = &mq_rq->brq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001721 old_req = mq_rq->req;
1722 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001723 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001724
Per Forlind78d4a8a2011-07-01 18:55:30 +02001725 switch (status) {
1726 case MMC_BLK_SUCCESS:
1727 case MMC_BLK_PARTIAL:
1728 /*
1729 * A block was successfully transferred.
1730 */
Adrian Hunter67716322011-08-29 16:42:15 +03001731 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001732
Linus Walleij0e65f102017-02-01 13:47:58 +01001733 req_pending = blk_end_request(old_req, 0,
1734 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001735 /*
1736 * If the blk_end_request function returns non-zero even
1737 * though all data has been transferred and no errors
1738 * were returned by the host controller, it's a bug.
1739 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001740 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301741 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001742 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001743 brq->data.bytes_xfered);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001744 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Linus Walleijdf061582017-01-24 11:17:57 +01001745 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001746 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001747 break;
1748 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001749 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001750 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001751 if (req_pending)
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001752 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1753 else
1754 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001755 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001756 return;
1757 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001758 if (!req_pending) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001759 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001760 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001761 return;
1762 }
Ding Wang29535f72015-05-18 20:14:15 +08001763 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001764 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001765 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001766 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001767 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001768 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001769 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001770 if (!mmc_blk_reset(md, card->host, type))
1771 break;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001772 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001773 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001774 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001775 case MMC_BLK_DATA_ERR: {
1776 int err;
1777
1778 err = mmc_blk_reset(md, card->host, type);
1779 if (!err)
1780 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001781 if (err == -ENODEV) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001782 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001783 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001784 return;
1785 }
Adrian Hunter67716322011-08-29 16:42:15 +03001786 /* Fall through */
1787 }
1788 case MMC_BLK_ECC_ERR:
1789 if (brq->data.blocks > 1) {
1790 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001791 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001792 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001793 disable_multi = 1;
1794 break;
1795 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001796 /*
1797 * After an error, we redo I/O one sector at a
1798 * time, so we only reach here after trying to
1799 * read a single sector.
1800 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001801 req_pending = blk_end_request(old_req, -EIO,
1802 brq->data.blksz);
1803 if (!req_pending) {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001804 mmc_queue_req_free(mq, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001805 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001806 return;
1807 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001808 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301809 case MMC_BLK_NOMEDIUM:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001810 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001811 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001812 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001813 default:
1814 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001815 old_req->rq_disk->disk_name, status);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001816 mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
Adrian Hunter8ddfe072017-03-13 14:36:34 +02001817 mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
Linus Walleijdb435502017-02-01 13:47:51 +01001818 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001819 }
1820
Linus Walleij0e65f102017-02-01 13:47:58 +01001821 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001822 /*
1823 * In case of a incomplete request
1824 * prepare it again and resend.
1825 */
1826 mmc_blk_rw_rq_prep(mq_rq, card,
1827 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001828 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001829 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001830 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001831 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001832 } while (req_pending);
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001833
1834 mmc_queue_req_free(mq, mq_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Linus Walleijdf061582017-01-24 11:17:57 +01001837void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001838{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001839 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001840 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001841 struct mmc_card *card = md->queue.card;
1842
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001843 if (req && !mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001844 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001845 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001846
Andrei Warkentin371a6892011-04-11 18:10:25 -05001847 ret = mmc_blk_part_switch(card, md);
1848 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001849 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301850 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001851 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001852 goto out;
1853 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001854
Mike Christiec2df40d2016-06-05 14:32:17 -05001855 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001856 /* complete ongoing async transfer before issuing discard */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001857 if (mq->qcnt)
Per Forlinee8a43a2011-07-01 18:55:33 +02001858 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001859 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001860 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1861 /* complete ongoing async transfer before issuing secure erase*/
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001862 if (mq->qcnt)
Christoph Hellwig288dab82016-06-09 16:00:36 +02001863 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001864 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001865 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001866 /* complete ongoing async transfer before issuing flush */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001867 if (mq->qcnt)
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001868 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001869 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001870 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001871 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter2602b742017-03-13 14:36:32 +02001872 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07001873 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001874
Andrei Warkentin371a6892011-04-11 18:10:25 -05001875out:
Adrian Huntercdf8a6f2017-03-13 14:36:35 +02001876 if (!mq->qcnt)
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001877 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Russell Kinga6f6c962006-01-03 22:38:44 +00001880static inline int mmc_blk_readonly(struct mmc_card *card)
1881{
1882 return mmc_card_readonly(card) ||
1883 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1884}
1885
Andrei Warkentin371a6892011-04-11 18:10:25 -05001886static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1887 struct device *parent,
1888 sector_t size,
1889 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001890 const char *subname,
1891 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct mmc_blk_data *md;
1894 int devidx, ret;
1895
Heiner Kallweita04848c2017-02-01 19:44:22 +01001896 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1897 if (devidx < 0)
1898 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001900 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001901 if (!md) {
1902 ret = -ENOMEM;
1903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001905
Johan Rudholmadd710e2011-12-02 08:51:06 +01001906 md->area_type = area_type;
1907
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001908 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001909 * Set the read-only status based on the supported commands
1910 * and the write protect switch.
1911 */
1912 md->read_only = mmc_blk_readonly(card);
1913
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001914 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001915 if (md->disk == NULL) {
1916 ret = -ENOMEM;
1917 goto err_kfree;
1918 }
1919
1920 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001921 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001922 md->usage = 1;
1923
Adrian Hunterd09408a2011-06-23 13:40:28 +03001924 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001925 if (ret)
1926 goto err_putdisk;
1927
Linus Walleij7db30282016-11-18 13:36:15 +01001928 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001929
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001930 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001931 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001932 md->disk->fops = &mmc_bdops;
1933 md->disk->private_data = md;
1934 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001935 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001936 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001937 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001938 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001939 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001940
1941 /*
1942 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1943 *
1944 * - be set for removable media with permanent block devices
1945 * - be unset for removable block devices with permanent media
1946 *
1947 * Since MMC block devices clearly fall under the second
1948 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1949 * should use the block device creation/destruction hotplug
1950 * messages to tell when the card is present.
1951 */
1952
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001953 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001954 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001955
Saugata Dasa5075eb2012-05-17 16:32:21 +05301956 if (mmc_card_mmc(card))
1957 blk_queue_logical_block_size(md->queue.queue,
1958 card->ext_csd.data_sector_size);
1959 else
1960 blk_queue_logical_block_size(md->queue.queue, 512);
1961
Andrei Warkentin371a6892011-04-11 18:10:25 -05001962 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001963
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001964 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001965 if ((mmc_card_mmc(card) &&
1966 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001967 (mmc_card_sd(card) &&
1968 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1969 md->flags |= MMC_BLK_CMD23;
1970 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001971
1972 if (mmc_card_mmc(card) &&
1973 md->flags & MMC_BLK_CMD23 &&
1974 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1975 card->ext_csd.rel_sectors)) {
1976 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001977 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001978 }
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001981
1982 err_putdisk:
1983 put_disk(md->disk);
1984 err_kfree:
1985 kfree(md);
1986 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01001987 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00001988 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Andrei Warkentin371a6892011-04-11 18:10:25 -05001991static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1992{
1993 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001994
1995 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1996 /*
1997 * The EXT_CSD sector count is in number or 512 byte
1998 * sectors.
1999 */
2000 size = card->ext_csd.sectors;
2001 } else {
2002 /*
2003 * The CSD capacity field is in units of read_blkbits.
2004 * set_capacity takes units of 512 bytes.
2005 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002006 size = (typeof(sector_t))card->csd.capacity
2007 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002008 }
2009
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002010 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002011 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002012}
2013
2014static int mmc_blk_alloc_part(struct mmc_card *card,
2015 struct mmc_blk_data *md,
2016 unsigned int part_type,
2017 sector_t size,
2018 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002019 const char *subname,
2020 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021{
2022 char cap_str[10];
2023 struct mmc_blk_data *part_md;
2024
2025 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002026 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002027 if (IS_ERR(part_md))
2028 return PTR_ERR(part_md);
2029 part_md->part_type = part_type;
2030 list_add(&part_md->part, &md->part);
2031
James Bottomleyb9f28d82015-03-05 18:47:01 -08002032 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002033 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302034 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002035 part_md->disk->disk_name, mmc_card_id(card),
2036 mmc_card_name(card), part_md->part_type, cap_str);
2037 return 0;
2038}
2039
Namjae Jeone0c368d2011-10-06 23:41:38 +09002040/* MMC Physical partitions consist of two boot partitions and
2041 * up to four general purpose partitions.
2042 * For each partition enabled in EXT_CSD a block device will be allocatedi
2043 * to provide access to the partition.
2044 */
2045
Andrei Warkentin371a6892011-04-11 18:10:25 -05002046static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2047{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002048 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002049
2050 if (!mmc_card_mmc(card))
2051 return 0;
2052
Namjae Jeone0c368d2011-10-06 23:41:38 +09002053 for (idx = 0; idx < card->nr_parts; idx++) {
2054 if (card->part[idx].size) {
2055 ret = mmc_blk_alloc_part(card, md,
2056 card->part[idx].part_cfg,
2057 card->part[idx].size >> 9,
2058 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002059 card->part[idx].name,
2060 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002061 if (ret)
2062 return ret;
2063 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002064 }
2065
2066 return ret;
2067}
2068
Andrei Warkentin371a6892011-04-11 18:10:25 -05002069static void mmc_blk_remove_req(struct mmc_blk_data *md)
2070{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002071 struct mmc_card *card;
2072
Andrei Warkentin371a6892011-04-11 18:10:25 -05002073 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002074 /*
2075 * Flush remaining requests and free queues. It
2076 * is freeing the queue that stops new requests
2077 * from being accepted.
2078 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002079 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002080 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002081 if (md->disk->flags & GENHD_FL_UP) {
2082 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002083 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2084 card->ext_csd.boot_ro_lockable)
2085 device_remove_file(disk_to_dev(md->disk),
2086 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002087
Andrei Warkentin371a6892011-04-11 18:10:25 -05002088 del_gendisk(md->disk);
2089 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002090 mmc_blk_put(md);
2091 }
2092}
2093
2094static void mmc_blk_remove_parts(struct mmc_card *card,
2095 struct mmc_blk_data *md)
2096{
2097 struct list_head *pos, *q;
2098 struct mmc_blk_data *part_md;
2099
2100 list_for_each_safe(pos, q, &md->part) {
2101 part_md = list_entry(pos, struct mmc_blk_data, part);
2102 list_del(pos);
2103 mmc_blk_remove_req(part_md);
2104 }
2105}
2106
2107static int mmc_add_disk(struct mmc_blk_data *md)
2108{
2109 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002110 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002111
Dan Williams307d8e62016-06-20 10:40:44 -07002112 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002113 md->force_ro.show = force_ro_show;
2114 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302115 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002116 md->force_ro.attr.name = "force_ro";
2117 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2118 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2119 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002120 goto force_ro_fail;
2121
2122 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2123 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002124 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002125
2126 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2127 mode = S_IRUGO;
2128 else
2129 mode = S_IRUGO | S_IWUSR;
2130
2131 md->power_ro_lock.show = power_ro_lock_show;
2132 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002133 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002134 md->power_ro_lock.attr.mode = mode;
2135 md->power_ro_lock.attr.name =
2136 "ro_lock_until_next_power_on";
2137 ret = device_create_file(disk_to_dev(md->disk),
2138 &md->power_ro_lock);
2139 if (ret)
2140 goto power_ro_lock_fail;
2141 }
2142 return ret;
2143
2144power_ro_lock_fail:
2145 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2146force_ro_fail:
2147 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002148
2149 return ret;
2150}
2151
Ulf Hansson96541ba2015-04-14 13:06:12 +02002152static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002154 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002155 char cap_str[10];
Adrian Hunter7b410d02017-03-13 14:36:36 +02002156 int ret;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002157
Pierre Ossman912490d2005-05-21 10:27:02 +01002158 /*
2159 * Check that the card supports the command class(es) we need.
2160 */
2161 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return -ENODEV;
2163
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002164 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002165
Adrian Hunter7b410d02017-03-13 14:36:36 +02002166 ret = mmc_queue_alloc_shared_queue(card);
2167 if (ret)
2168 return ret;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 md = mmc_blk_alloc(card);
Adrian Hunter7b410d02017-03-13 14:36:36 +02002171 if (IS_ERR(md)) {
2172 mmc_queue_free_shared_queue(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return PTR_ERR(md);
Adrian Hunter7b410d02017-03-13 14:36:36 +02002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
James Bottomleyb9f28d82015-03-05 18:47:01 -08002176 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002177 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302178 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002180 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182 if (mmc_blk_alloc_parts(card, md))
2183 goto out;
2184
Ulf Hansson96541ba2015-04-14 13:06:12 +02002185 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002186
Andrei Warkentin371a6892011-04-11 18:10:25 -05002187 if (mmc_add_disk(md))
2188 goto out;
2189
2190 list_for_each_entry(part_md, &md->part, part) {
2191 if (mmc_add_disk(part_md))
2192 goto out;
2193 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002194
2195 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2196 pm_runtime_use_autosuspend(&card->dev);
2197
2198 /*
2199 * Don't enable runtime PM for SD-combo cards here. Leave that
2200 * decision to be taken during the SDIO init sequence instead.
2201 */
2202 if (card->type != MMC_TYPE_SD_COMBO) {
2203 pm_runtime_set_active(&card->dev);
2204 pm_runtime_enable(&card->dev);
2205 }
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return 0;
2208
2209 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002210 mmc_blk_remove_parts(card, md);
2211 mmc_blk_remove_req(md);
Adrian Hunter7b410d02017-03-13 14:36:36 +02002212 mmc_queue_free_shared_queue(card);
Ulf Hansson5865f282012-03-22 11:47:26 +01002213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Ulf Hansson96541ba2015-04-14 13:06:12 +02002216static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002218 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002221 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002222 mmc_claim_host(card->host);
2223 mmc_blk_part_switch(card, md);
2224 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002225 if (card->type != MMC_TYPE_SD_COMBO)
2226 pm_runtime_disable(&card->dev);
2227 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002228 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002229 dev_set_drvdata(&card->dev, NULL);
Adrian Hunter7b410d02017-03-13 14:36:36 +02002230 mmc_queue_free_shared_queue(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Ulf Hansson96541ba2015-04-14 13:06:12 +02002233static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002236 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 if (md) {
2239 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240 list_for_each_entry(part_md, &md->part, part) {
2241 mmc_queue_suspend(&part_md->queue);
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244 return 0;
2245}
2246
Ulf Hansson96541ba2015-04-14 13:06:12 +02002247static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002248{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002249 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002250}
2251
Ulf Hansson0967edc2014-10-06 11:29:42 +02002252#ifdef CONFIG_PM_SLEEP
2253static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002254{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002255 struct mmc_card *card = mmc_dev_to_card(dev);
2256
2257 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002258}
2259
Ulf Hansson0967edc2014-10-06 11:29:42 +02002260static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002262 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002263 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002266 /*
2267 * Resume involves the card going into idle state,
2268 * so current partition is always the main one.
2269 */
2270 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002272 list_for_each_entry(part_md, &md->part, part) {
2273 mmc_queue_resume(&part_md->queue);
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 return 0;
2277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#endif
2279
Ulf Hansson0967edc2014-10-06 11:29:42 +02002280static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2281
Ulf Hansson96541ba2015-04-14 13:06:12 +02002282static struct mmc_driver mmc_driver = {
2283 .drv = {
2284 .name = "mmcblk",
2285 .pm = &mmc_blk_pm_ops,
2286 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 .probe = mmc_blk_probe,
2288 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002289 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290};
2291
2292static int __init mmc_blk_init(void)
2293{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002294 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002296 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2297 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2298
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002299 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002300
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002301 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2302 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002305 res = mmc_register_driver(&mmc_driver);
2306 if (res)
2307 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002309 return 0;
2310 out2:
2311 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 out:
2313 return res;
2314}
2315
2316static void __exit mmc_blk_exit(void)
2317{
2318 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002319 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
2322module_init(mmc_blk_init);
2323module_exit(mmc_blk_exit);
2324
2325MODULE_LICENSE("GPL");
2326MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2327