blob: c3335112e68c29cfe4a16eae0aabf4368682d357 [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 Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000050MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040051#ifdef MODULE_PARAM_PREFIX
52#undef MODULE_PARAM_PREFIX
53#endif
54#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010055
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050056#define INAND_CMD38_ARG_EXT_CSD 113
57#define INAND_CMD38_ARG_ERASE 0x00
58#define INAND_CMD38_ARG_TRIM 0x01
59#define INAND_CMD38_ARG_SECERASE 0x80
60#define INAND_CMD38_ARG_SECTRIM1 0x81
61#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060062#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030063#define MMC_SANITIZE_REQ_TIMEOUT 240000
64#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050065
Luca Porziod3df0462015-11-06 15:12:26 +000066#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090067 (rq_data_dir(req) == WRITE))
68#define PACKED_CMD_VER 0x01
69#define PACKED_CMD_WR 0x02
70
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020071static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040072
73/*
74 * The defaults come from config options but can be overriden by module
75 * or bootarg options.
76 */
77static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
78
79/*
80 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000081 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020082 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040083 */
84static int max_devices;
85
Ben Hutchingsa26eba62014-11-06 03:35:09 +000086#define MAX_DEVICES 256
87
Ulf Hanssonb10fa992016-04-07 14:36:46 +020088static DEFINE_IDA(mmc_blk_ida);
89static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * There is one mmc_blk_data per slot.
93 */
94struct mmc_blk_data {
95 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070096 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900104#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000107 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200117 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
123 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Arjan van de Vena621aae2006-01-12 18:43:35 +0000126static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900128enum {
129 MMC_PACKED_NR_IDX = -1,
130 MMC_PACKED_NR_ZERO,
131 MMC_PACKED_NR_SINGLE,
132};
133
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400134module_param(perdev_minors, int, 0444);
135MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
136
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200137static inline int mmc_blk_part_switch(struct mmc_card *card,
138 struct mmc_blk_data *md);
139static int get_card_status(struct mmc_card *card, u32 *status, int retries);
140
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900141static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
142{
143 struct mmc_packed *packed = mqrq->packed;
144
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900145 mqrq->cmd_type = MMC_PACKED_NONE;
146 packed->nr_entries = MMC_PACKED_NR_ZERO;
147 packed->idx_failure = MMC_PACKED_NR_IDX;
148 packed->retries = 0;
149 packed->blocks = 0;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
153{
154 struct mmc_blk_data *md;
155
Arjan van de Vena621aae2006-01-12 18:43:35 +0000156 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 md = disk->private_data;
158 if (md && md->usage == 0)
159 md = NULL;
160 if (md)
161 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000162 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return md;
165}
166
Andrei Warkentin371a6892011-04-11 18:10:25 -0500167static inline int mmc_get_devidx(struct gendisk *disk)
168{
Colin Cross382c55f2015-10-22 10:00:41 -0700169 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500170 return devidx;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void mmc_blk_put(struct mmc_blk_data *md)
174{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000175 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 md->usage--;
177 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500178 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800179 blk_cleanup_queue(md->queue.queue);
180
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200181 spin_lock(&mmc_blk_lock);
182 ida_remove(&mmc_blk_ida, devidx);
183 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(md);
187 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000188 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Johan Rudholmadd710e2011-12-02 08:51:06 +0100191static ssize_t power_ro_lock_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 int ret;
195 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
196 struct mmc_card *card = md->queue.card;
197 int locked = 0;
198
199 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
200 locked = 2;
201 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
202 locked = 1;
203
204 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
205
Tomas Winkler9098f842015-07-16 15:50:45 +0200206 mmc_blk_put(md);
207
Johan Rudholmadd710e2011-12-02 08:51:06 +0100208 return ret;
209}
210
211static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213{
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
218
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
221
222 if (set != 1)
223 return count;
224
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
227
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200228 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100229
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
238
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200239 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255}
256
Andrei Warkentin371a6892011-04-11 18:10:25 -0500257static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
Baruch Siach0031a982014-09-22 10:12:51 +0300263 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268}
269
270static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284out:
285 mmc_blk_put(md);
286 return ret;
287}
288
Al Viroa5a15612008-03-02 10:33:30 -0500289static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Al Viroa5a15612008-03-02 10:33:30 -0500291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int ret = -ENXIO;
293
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200294 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (md) {
296 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500297 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700299
Al Viroa5a15612008-03-02 10:33:30 -0500300 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700301 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700302 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200305 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return ret;
308}
309
Al Virodb2a1442013-05-05 21:52:57 -0400310static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Al Viroa5a15612008-03-02 10:33:30 -0500312 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200316 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800320mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
John Calixtocb87ea22011-04-26 18:56:29 -0400328struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
332};
333
334static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
336{
337 struct mmc_blk_ioc_data *idata;
338 int err;
339
yalin wang1ff89502015-11-12 19:27:11 +0800340 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400341 if (!idata) {
342 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400343 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400344 }
345
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400355 }
356
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300357 if (!idata->buf_bytes) {
358 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100359 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300360 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100361
yalin wang1ff89502015-11-12 19:27:11 +0800362 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400363 if (!idata->buf) {
364 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400365 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400366 }
367
368 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
369 idata->ic.data_ptr, idata->buf_bytes)) {
370 err = -EFAULT;
371 goto copy_err;
372 }
373
374 return idata;
375
376copy_err:
377 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400378idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400379 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400380out:
John Calixtocb87ea22011-04-26 18:56:29 -0400381 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400382}
383
Jon Huntera5f57742015-09-22 10:27:53 +0100384static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
385 struct mmc_blk_ioc_data *idata)
386{
387 struct mmc_ioc_cmd *ic = &idata->ic;
388
389 if (copy_to_user(&(ic_ptr->response), ic->response,
390 sizeof(ic->response)))
391 return -EFAULT;
392
393 if (!idata->ic.write_flag) {
394 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
395 idata->buf, idata->buf_bytes))
396 return -EFAULT;
397 }
398
399 return 0;
400}
401
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200402static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
403 u32 retries_max)
404{
405 int err;
406 u32 retry_count = 0;
407
408 if (!status || !retries_max)
409 return -EINVAL;
410
411 do {
412 err = get_card_status(card, status, 5);
413 if (err)
414 break;
415
416 if (!R1_STATUS(*status) &&
417 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
418 break; /* RPMB programming operation complete */
419
420 /*
421 * Rechedule to give the MMC device a chance to continue
422 * processing the previous command without being polled too
423 * frequently.
424 */
425 usleep_range(1000, 5000);
426 } while (++retry_count < retries_max);
427
428 if (retry_count == retries_max)
429 err = -EPERM;
430
431 return err;
432}
433
Maya Erez775a9362013-04-18 15:41:55 +0300434static int ioctl_do_sanitize(struct mmc_card *card)
435{
436 int err;
437
Ulf Hanssona2d10862013-12-16 14:37:26 +0100438 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300439 pr_warn("%s: %s - SANITIZE is not supported\n",
440 mmc_hostname(card->host), __func__);
441 err = -EOPNOTSUPP;
442 goto out;
443 }
444
445 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
446 mmc_hostname(card->host), __func__);
447
448 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
449 EXT_CSD_SANITIZE_START, 1,
450 MMC_SANITIZE_REQ_TIMEOUT);
451
452 if (err)
453 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
454 mmc_hostname(card->host), __func__, err);
455
456 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
457 __func__);
458out:
459 return err;
460}
461
Jon Huntera5f57742015-09-22 10:27:53 +0100462static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
463 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400464{
John Calixtocb87ea22011-04-26 18:56:29 -0400465 struct mmc_command cmd = {0};
466 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530467 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400468 struct scatterlist sg;
469 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200470 int is_rpmb = false;
471 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400472
Jon Huntera5f57742015-09-22 10:27:53 +0100473 if (!card || !md || !idata)
474 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400475
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200476 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
477 is_rpmb = true;
478
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100479 cmd.opcode = idata->ic.opcode;
480 cmd.arg = idata->ic.arg;
481 cmd.flags = idata->ic.flags;
482
483 if (idata->buf_bytes) {
484 data.sg = &sg;
485 data.sg_len = 1;
486 data.blksz = idata->ic.blksz;
487 data.blocks = idata->ic.blocks;
488
489 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
490
491 if (idata->ic.write_flag)
492 data.flags = MMC_DATA_WRITE;
493 else
494 data.flags = MMC_DATA_READ;
495
496 /* data.flags must already be set before doing this. */
497 mmc_set_data_timeout(&data, card);
498
499 /* Allow overriding the timeout_ns for empirical tuning. */
500 if (idata->ic.data_timeout_ns)
501 data.timeout_ns = idata->ic.data_timeout_ns;
502
503 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
504 /*
505 * Pretend this is a data transfer and rely on the
506 * host driver to compute timeout. When all host
507 * drivers support cmd.cmd_timeout for R1B, this
508 * can be changed to:
509 *
510 * mrq.data = NULL;
511 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
512 */
513 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
514 }
515
516 mrq.data = &data;
517 }
518
519 mrq.cmd = &cmd;
520
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200521 err = mmc_blk_part_switch(card, md);
522 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100523 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200524
John Calixtocb87ea22011-04-26 18:56:29 -0400525 if (idata->ic.is_acmd) {
526 err = mmc_app_cmd(card->host, card);
527 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100528 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400529 }
530
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200531 if (is_rpmb) {
532 err = mmc_set_blockcount(card, data.blocks,
533 idata->ic.write_flag & (1 << 31));
534 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100535 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200536 }
537
Yaniv Gardia82e4842013-06-05 14:13:08 +0300538 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
539 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300540 err = ioctl_do_sanitize(card);
541
542 if (err)
543 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
544 __func__, err);
545
Jon Huntera5f57742015-09-22 10:27:53 +0100546 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300547 }
548
John Calixtocb87ea22011-04-26 18:56:29 -0400549 mmc_wait_for_req(card->host, &mrq);
550
551 if (cmd.error) {
552 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
553 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100554 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400555 }
556 if (data.error) {
557 dev_err(mmc_dev(card->host), "%s: data error %d\n",
558 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100559 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400560 }
561
562 /*
563 * According to the SD specs, some commands require a delay after
564 * issuing the command.
565 */
566 if (idata->ic.postsleep_min_us)
567 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
568
Jon Huntera5f57742015-09-22 10:27:53 +0100569 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400570
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200571 if (is_rpmb) {
572 /*
573 * Ensure RPMB command has completed by polling CMD13
574 * "Send Status".
575 */
576 err = ioctl_rpmb_card_status_poll(card, &status, 5);
577 if (err)
578 dev_err(mmc_dev(card->host),
579 "%s: Card Status=0x%08X, error %d\n",
580 __func__, status, err);
581 }
582
Jon Huntera5f57742015-09-22 10:27:53 +0100583 return err;
584}
585
586static int mmc_blk_ioctl_cmd(struct block_device *bdev,
587 struct mmc_ioc_cmd __user *ic_ptr)
588{
589 struct mmc_blk_ioc_data *idata;
590 struct mmc_blk_data *md;
591 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700592 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100593
Shawn Lin83c742c2016-03-16 18:15:47 +0800594 /*
595 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
596 * whole block device, not on a partition. This prevents overspray
597 * between sibling partitions.
598 */
599 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
600 return -EPERM;
601
Jon Huntera5f57742015-09-22 10:27:53 +0100602 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
603 if (IS_ERR(idata))
604 return PTR_ERR(idata);
605
606 md = mmc_blk_get(bdev->bd_disk);
607 if (!md) {
608 err = -EINVAL;
609 goto cmd_err;
610 }
611
612 card = md->queue.card;
613 if (IS_ERR(card)) {
614 err = PTR_ERR(card);
615 goto cmd_done;
616 }
617
618 mmc_get_card(card);
619
Grant Grundlerb0934102015-09-23 18:30:33 -0700620 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100621
Adrian Hunter3c866562016-05-04 14:38:12 +0300622 /* Always switch back to main area after RPMB access */
623 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
624 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
625
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200626 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400627
Grant Grundlerb0934102015-09-23 18:30:33 -0700628 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100629
John Calixtocb87ea22011-04-26 18:56:29 -0400630cmd_done:
631 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300632cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400633 kfree(idata->buf);
634 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700635 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400636}
637
Jon Huntera5f57742015-09-22 10:27:53 +0100638static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
639 struct mmc_ioc_multi_cmd __user *user)
640{
641 struct mmc_blk_ioc_data **idata = NULL;
642 struct mmc_ioc_cmd __user *cmds = user->cmds;
643 struct mmc_card *card;
644 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700645 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100646 __u64 num_of_cmds;
647
Shawn Lin83c742c2016-03-16 18:15:47 +0800648 /*
649 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
650 * whole block device, not on a partition. This prevents overspray
651 * between sibling partitions.
652 */
653 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
654 return -EPERM;
655
Jon Huntera5f57742015-09-22 10:27:53 +0100656 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
657 sizeof(num_of_cmds)))
658 return -EFAULT;
659
660 if (num_of_cmds > MMC_IOC_MAX_CMDS)
661 return -EINVAL;
662
663 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
664 if (!idata)
665 return -ENOMEM;
666
667 for (i = 0; i < num_of_cmds; i++) {
668 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
669 if (IS_ERR(idata[i])) {
670 err = PTR_ERR(idata[i]);
671 num_of_cmds = i;
672 goto cmd_err;
673 }
674 }
675
676 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800677 if (!md) {
678 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100679 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800680 }
Jon Huntera5f57742015-09-22 10:27:53 +0100681
682 card = md->queue.card;
683 if (IS_ERR(card)) {
684 err = PTR_ERR(card);
685 goto cmd_done;
686 }
687
688 mmc_get_card(card);
689
Grant Grundlerb0934102015-09-23 18:30:33 -0700690 for (i = 0; i < num_of_cmds && !ioc_err; i++)
691 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100692
Adrian Hunter3c866562016-05-04 14:38:12 +0300693 /* Always switch back to main area after RPMB access */
694 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
695 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
696
Jon Huntera5f57742015-09-22 10:27:53 +0100697 mmc_put_card(card);
698
699 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700700 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100701 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100702
703cmd_done:
704 mmc_blk_put(md);
705cmd_err:
706 for (i = 0; i < num_of_cmds; i++) {
707 kfree(idata[i]->buf);
708 kfree(idata[i]);
709 }
710 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700711 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100712}
713
John Calixtocb87ea22011-04-26 18:56:29 -0400714static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
715 unsigned int cmd, unsigned long arg)
716{
Jon Huntera5f57742015-09-22 10:27:53 +0100717 switch (cmd) {
718 case MMC_IOC_CMD:
719 return mmc_blk_ioctl_cmd(bdev,
720 (struct mmc_ioc_cmd __user *)arg);
721 case MMC_IOC_MULTI_CMD:
722 return mmc_blk_ioctl_multi_cmd(bdev,
723 (struct mmc_ioc_multi_cmd __user *)arg);
724 default:
725 return -EINVAL;
726 }
John Calixtocb87ea22011-04-26 18:56:29 -0400727}
728
729#ifdef CONFIG_COMPAT
730static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
731 unsigned int cmd, unsigned long arg)
732{
733 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
734}
735#endif
736
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700737static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500738 .open = mmc_blk_open,
739 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800740 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400742 .ioctl = mmc_blk_ioctl,
743#ifdef CONFIG_COMPAT
744 .compat_ioctl = mmc_blk_compat_ioctl,
745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746};
747
Andrei Warkentin371a6892011-04-11 18:10:25 -0500748static inline int mmc_blk_part_switch(struct mmc_card *card,
749 struct mmc_blk_data *md)
750{
751 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200752 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300753
Andrei Warkentin371a6892011-04-11 18:10:25 -0500754 if (main_md->part_curr == md->part_type)
755 return 0;
756
757 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300758 u8 part_config = card->ext_csd.part_config;
759
Adrian Hunter57da0c02016-05-04 14:38:13 +0300760 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
761 mmc_retune_pause(card->host);
762
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300763 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
764 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500765
766 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300767 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500768 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300769 if (ret) {
770 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
771 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500772 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300773 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300774
775 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300776
777 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
778 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300779 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500780
781 main_md->part_curr = md->part_type;
782 return 0;
783}
784
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700785static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
786{
787 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100788 u32 result;
789 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700790
Venkatraman Sad5fd972011-08-25 00:30:50 +0530791 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400792 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400793 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700794
795 struct scatterlist sg;
796
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700797 cmd.opcode = MMC_APP_CMD;
798 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700799 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700800
801 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700802 if (err)
803 return (u32)-1;
804 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700805 return (u32)-1;
806
807 memset(&cmd, 0, sizeof(struct mmc_command));
808
809 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
810 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700811 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700812
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813 data.blksz = 4;
814 data.blocks = 1;
815 data.flags = MMC_DATA_READ;
816 data.sg = &sg;
817 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530818 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700820 mrq.cmd = &cmd;
821 mrq.data = &data;
822
Ben Dooks051913d2009-06-08 23:33:57 +0100823 blocks = kmalloc(4, GFP_KERNEL);
824 if (!blocks)
825 return (u32)-1;
826
827 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700828
829 mmc_wait_for_req(card->host, &mrq);
830
Ben Dooks051913d2009-06-08 23:33:57 +0100831 result = ntohl(*blocks);
832 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700833
Ben Dooks051913d2009-06-08 23:33:57 +0100834 if (cmd.error || data.error)
835 result = (u32)-1;
836
837 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700838}
839
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100840static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300841{
Chris Ball1278dba2011-04-13 23:40:30 -0400842 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300843 int err;
844
Adrian Hunter504f1912008-10-16 12:55:25 +0300845 cmd.opcode = MMC_SEND_STATUS;
846 if (!mmc_host_is_spi(card->host))
847 cmd.arg = card->rca << 16;
848 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100849 err = mmc_wait_for_cmd(card->host, &cmd, retries);
850 if (err == 0)
851 *status = cmd.resp[0];
852 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300853}
854
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100855static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +0100856 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100857{
858 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
859 int err = 0;
860 u32 status;
861
862 do {
863 err = get_card_status(card, &status, 5);
864 if (err) {
865 pr_err("%s: error %d requesting status\n",
866 req->rq_disk->disk_name, err);
867 return err;
868 }
869
870 if (status & R1_ERROR) {
871 pr_err("%s: %s: error sending status cmd, status %#x\n",
872 req->rq_disk->disk_name, __func__, status);
873 *gen_err = 1;
874 }
875
Ulf Hansson95a91292014-01-29 13:11:27 +0100876 /* We may rely on the host hw to handle busy detection.*/
877 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
878 hw_busy_detect)
879 break;
880
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100881 /*
882 * Timeout if the device never becomes ready for data and never
883 * leaves the program state.
884 */
885 if (time_after(jiffies, timeout)) {
886 pr_err("%s: Card stuck in programming state! %s %s\n",
887 mmc_hostname(card->host),
888 req->rq_disk->disk_name, __func__);
889 return -ETIMEDOUT;
890 }
891
892 /*
893 * Some cards mishandle the status bits,
894 * so make sure to check both the busy
895 * indication and the card state.
896 */
897 } while (!(status & R1_READY_FOR_DATA) ||
898 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
899
900 return err;
901}
902
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100903static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
904 struct request *req, int *gen_err, u32 *stop_status)
905{
906 struct mmc_host *host = card->host;
907 struct mmc_command cmd = {0};
908 int err;
909 bool use_r1b_resp = rq_data_dir(req) == WRITE;
910
911 /*
912 * Normally we use R1B responses for WRITE, but in cases where the host
913 * has specified a max_busy_timeout we need to validate it. A failure
914 * means we need to prevent the host from doing hw busy detection, which
915 * is done by converting to a R1 response instead.
916 */
917 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
918 use_r1b_resp = false;
919
920 cmd.opcode = MMC_STOP_TRANSMISSION;
921 if (use_r1b_resp) {
922 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
923 cmd.busy_timeout = timeout_ms;
924 } else {
925 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
926 }
927
928 err = mmc_wait_for_cmd(host, &cmd, 5);
929 if (err)
930 return err;
931
932 *stop_status = cmd.resp[0];
933
934 /* No need to check card status in case of READ. */
935 if (rq_data_dir(req) == READ)
936 return 0;
937
938 if (!mmc_host_is_spi(host) &&
939 (*stop_status & R1_ERROR)) {
940 pr_err("%s: %s: general error sending stop command, resp %#x\n",
941 req->rq_disk->disk_name, __func__, *stop_status);
942 *gen_err = 1;
943 }
944
945 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
946}
947
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530948#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100949#define ERR_RETRY 2
950#define ERR_ABORT 1
951#define ERR_CONTINUE 0
952
953static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
954 bool status_valid, u32 status)
955{
956 switch (error) {
957 case -EILSEQ:
958 /* response crc error, retry the r/w cmd */
959 pr_err("%s: %s sending %s command, card status %#x\n",
960 req->rq_disk->disk_name, "response CRC error",
961 name, status);
962 return ERR_RETRY;
963
964 case -ETIMEDOUT:
965 pr_err("%s: %s sending %s command, card status %#x\n",
966 req->rq_disk->disk_name, "timed out", name, status);
967
968 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530969 if (!status_valid) {
970 pr_err("%s: status not valid, retrying timeout\n",
971 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100972 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530973 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100974
975 /*
976 * If it was a r/w cmd crc error, or illegal command
977 * (eg, issued in wrong state) then retry - we should
978 * have corrected the state problem above.
979 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530980 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
981 pr_err("%s: command error, retrying timeout\n",
982 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100983 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530984 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100985
986 /* Otherwise abort the command */
987 return ERR_ABORT;
988
989 default:
990 /* We don't understand the error code the driver gave us */
991 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
992 req->rq_disk->disk_name, error, status);
993 return ERR_ABORT;
994 }
995}
996
997/*
998 * Initial r/w and stop cmd error recovery.
999 * We don't know whether the card received the r/w cmd or not, so try to
1000 * restore things back to a sane state. Essentially, we do this as follows:
1001 * - Obtain card status. If the first attempt to obtain card status fails,
1002 * the status word will reflect the failed status cmd, not the failed
1003 * r/w cmd. If we fail to obtain card status, it suggests we can no
1004 * longer communicate with the card.
1005 * - Check the card state. If the card received the cmd but there was a
1006 * transient problem with the response, it might still be in a data transfer
1007 * mode. Try to send it a stop command. If this fails, we can't recover.
1008 * - If the r/w cmd failed due to a response CRC error, it was probably
1009 * transient, so retry the cmd.
1010 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1011 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1012 * illegal cmd, retry.
1013 * Otherwise we don't understand what happened, so abort.
1014 */
1015static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001016 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001017{
1018 bool prev_cmd_status_valid = true;
1019 u32 status, stop_status = 0;
1020 int err, retry;
1021
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301022 if (mmc_card_removed(card))
1023 return ERR_NOMEDIUM;
1024
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001025 /*
1026 * Try to get card status which indicates both the card state
1027 * and why there was no response. If the first attempt fails,
1028 * we can't be sure the returned status is for the r/w command.
1029 */
1030 for (retry = 2; retry >= 0; retry--) {
1031 err = get_card_status(card, &status, 0);
1032 if (!err)
1033 break;
1034
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001035 /* Re-tune if needed */
1036 mmc_retune_recheck(card->host);
1037
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001038 prev_cmd_status_valid = false;
1039 pr_err("%s: error %d sending status command, %sing\n",
1040 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1041 }
1042
1043 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301044 if (err) {
1045 /* Check if the card is removed */
1046 if (mmc_detect_card_removed(card->host))
1047 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001048 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301049 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001050
Adrian Hunter67716322011-08-29 16:42:15 +03001051 /* Flag ECC errors */
1052 if ((status & R1_CARD_ECC_FAILED) ||
1053 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1054 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1055 *ecc_err = 1;
1056
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001057 /* Flag General errors */
1058 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1059 if ((status & R1_ERROR) ||
1060 (brq->stop.resp[0] & R1_ERROR)) {
1061 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1062 req->rq_disk->disk_name, __func__,
1063 brq->stop.resp[0], status);
1064 *gen_err = 1;
1065 }
1066
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001067 /*
1068 * Check the current card state. If it is in some data transfer
1069 * mode, tell it to stop (and hopefully transition back to TRAN.)
1070 */
1071 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1072 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001073 err = send_stop(card,
1074 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1075 req, gen_err, &stop_status);
1076 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001077 pr_err("%s: error %d sending stop command\n",
1078 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001079 /*
1080 * If the stop cmd also timed out, the card is probably
1081 * not present, so abort. Other errors are bad news too.
1082 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001083 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001084 }
1085
Adrian Hunter67716322011-08-29 16:42:15 +03001086 if (stop_status & R1_CARD_ECC_FAILED)
1087 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001088 }
1089
1090 /* Check for set block count errors */
1091 if (brq->sbc.error)
1092 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1093 prev_cmd_status_valid, status);
1094
1095 /* Check for r/w command errors */
1096 if (brq->cmd.error)
1097 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1098 prev_cmd_status_valid, status);
1099
Adrian Hunter67716322011-08-29 16:42:15 +03001100 /* Data errors */
1101 if (!brq->stop.error)
1102 return ERR_CONTINUE;
1103
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001104 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001105 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001106 req->rq_disk->disk_name, brq->stop.error,
1107 brq->cmd.resp[0], status);
1108
1109 /*
1110 * Subsitute in our own stop status as this will give the error
1111 * state which happened during the execution of the r/w command.
1112 */
1113 if (stop_status) {
1114 brq->stop.resp[0] = stop_status;
1115 brq->stop.error = 0;
1116 }
1117 return ERR_CONTINUE;
1118}
1119
Adrian Hunter67716322011-08-29 16:42:15 +03001120static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1121 int type)
1122{
1123 int err;
1124
1125 if (md->reset_done & type)
1126 return -EEXIST;
1127
1128 md->reset_done |= type;
1129 err = mmc_hw_reset(host);
1130 /* Ensure we switch back to the correct partition */
1131 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001132 struct mmc_blk_data *main_md =
1133 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001134 int part_err;
1135
1136 main_md->part_curr = main_md->part_type;
1137 part_err = mmc_blk_part_switch(host->card, md);
1138 if (part_err) {
1139 /*
1140 * We have failed to get back into the correct
1141 * partition, so we need to abort the whole request.
1142 */
1143 return -ENODEV;
1144 }
1145 }
1146 return err;
1147}
1148
1149static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1150{
1151 md->reset_done &= ~type;
1152}
1153
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001154int mmc_access_rpmb(struct mmc_queue *mq)
1155{
1156 struct mmc_blk_data *md = mq->data;
1157 /*
1158 * If this is a RPMB partition access, return ture
1159 */
1160 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1161 return true;
1162
1163 return false;
1164}
1165
Adrian Hunterbd788c92010-08-11 14:17:47 -07001166static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1167{
1168 struct mmc_blk_data *md = mq->data;
1169 struct mmc_card *card = md->queue.card;
1170 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001171 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001172
Adrian Hunterbd788c92010-08-11 14:17:47 -07001173 if (!mmc_can_erase(card)) {
1174 err = -EOPNOTSUPP;
1175 goto out;
1176 }
1177
1178 from = blk_rq_pos(req);
1179 nr = blk_rq_sectors(req);
1180
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001181 if (mmc_can_discard(card))
1182 arg = MMC_DISCARD_ARG;
1183 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001184 arg = MMC_TRIM_ARG;
1185 else
1186 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001187retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001188 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1189 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1190 INAND_CMD38_ARG_EXT_CSD,
1191 arg == MMC_TRIM_ARG ?
1192 INAND_CMD38_ARG_TRIM :
1193 INAND_CMD38_ARG_ERASE,
1194 0);
1195 if (err)
1196 goto out;
1197 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001198 err = mmc_erase(card, from, nr, arg);
1199out:
Adrian Hunter67716322011-08-29 16:42:15 +03001200 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1201 goto retry;
1202 if (!err)
1203 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301204 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001205
Adrian Hunterbd788c92010-08-11 14:17:47 -07001206 return err ? 0 : 1;
1207}
1208
Adrian Hunter49804542010-08-11 14:17:50 -07001209static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1210 struct request *req)
1211{
1212 struct mmc_blk_data *md = mq->data;
1213 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001214 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001215 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001216
Maya Erez775a9362013-04-18 15:41:55 +03001217 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001218 err = -EOPNOTSUPP;
1219 goto out;
1220 }
1221
1222 from = blk_rq_pos(req);
1223 nr = blk_rq_sectors(req);
1224
Maya Erez775a9362013-04-18 15:41:55 +03001225 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1226 arg = MMC_SECURE_TRIM1_ARG;
1227 else
1228 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001229
Adrian Hunter67716322011-08-29 16:42:15 +03001230retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001231 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1232 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1233 INAND_CMD38_ARG_EXT_CSD,
1234 arg == MMC_SECURE_TRIM1_ARG ?
1235 INAND_CMD38_ARG_SECTRIM1 :
1236 INAND_CMD38_ARG_SECERASE,
1237 0);
1238 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001239 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001240 }
Adrian Hunter28302812012-04-05 14:45:48 +03001241
Adrian Hunter49804542010-08-11 14:17:50 -07001242 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001243 if (err == -EIO)
1244 goto out_retry;
1245 if (err)
1246 goto out;
1247
1248 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001249 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1250 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1251 INAND_CMD38_ARG_EXT_CSD,
1252 INAND_CMD38_ARG_SECTRIM2,
1253 0);
1254 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001255 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001256 }
Adrian Hunter28302812012-04-05 14:45:48 +03001257
Adrian Hunter49804542010-08-11 14:17:50 -07001258 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001259 if (err == -EIO)
1260 goto out_retry;
1261 if (err)
1262 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001263 }
Adrian Hunter28302812012-04-05 14:45:48 +03001264
Adrian Hunter28302812012-04-05 14:45:48 +03001265out_retry:
1266 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001267 goto retry;
1268 if (!err)
1269 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001270out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301271 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001272
Adrian Hunter49804542010-08-11 14:17:50 -07001273 return err ? 0 : 1;
1274}
1275
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001276static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1277{
1278 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001279 struct mmc_card *card = md->queue.card;
1280 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001281
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001282 ret = mmc_flush_cache(card);
1283 if (ret)
1284 ret = -EIO;
1285
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301286 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001287
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001288 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001289}
1290
1291/*
1292 * Reformat current write as a reliable write, supporting
1293 * both legacy and the enhanced reliable write MMC cards.
1294 * In each transfer we'll handle only as much as a single
1295 * reliable write can handle, thus finish the request in
1296 * partial completions.
1297 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001298static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1299 struct mmc_card *card,
1300 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001301{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001302 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1303 /* Legacy mode imposes restrictions on transfers. */
1304 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1305 brq->data.blocks = 1;
1306
1307 if (brq->data.blocks > card->ext_csd.rel_sectors)
1308 brq->data.blocks = card->ext_csd.rel_sectors;
1309 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1310 brq->data.blocks = 1;
1311 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001312}
1313
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001314#define CMD_ERRORS \
1315 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1316 R1_ADDRESS_ERROR | /* Misaligned address */ \
1317 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1318 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1319 R1_CC_ERROR | /* Card controller error */ \
1320 R1_ERROR) /* General/unknown error */
1321
Per Forlinee8a43a2011-07-01 18:55:33 +02001322static int mmc_blk_err_check(struct mmc_card *card,
1323 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001324{
Per Forlinee8a43a2011-07-01 18:55:33 +02001325 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1326 mmc_active);
1327 struct mmc_blk_request *brq = &mq_mrq->brq;
1328 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001329 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001330 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001331
1332 /*
1333 * sbc.error indicates a problem with the set block count
1334 * command. No data will have been transferred.
1335 *
1336 * cmd.error indicates a problem with the r/w command. No
1337 * data will have been transferred.
1338 *
1339 * stop.error indicates a problem with the stop command. Data
1340 * may have been transferred, or may still be transferring.
1341 */
Adrian Hunter67716322011-08-29 16:42:15 +03001342 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1343 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001344 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001345 case ERR_RETRY:
1346 return MMC_BLK_RETRY;
1347 case ERR_ABORT:
1348 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301349 case ERR_NOMEDIUM:
1350 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001351 case ERR_CONTINUE:
1352 break;
1353 }
1354 }
1355
1356 /*
1357 * Check for errors relating to the execution of the
1358 * initial command - such as address errors. No data
1359 * has been transferred.
1360 */
1361 if (brq->cmd.resp[0] & CMD_ERRORS) {
1362 pr_err("%s: r/w command failed, status = %#x\n",
1363 req->rq_disk->disk_name, brq->cmd.resp[0]);
1364 return MMC_BLK_ABORT;
1365 }
1366
1367 /*
1368 * Everything else is either success, or a data error of some
1369 * kind. If it was a write, we may have transitioned to
1370 * program mode, which we have to wait for it to complete.
1371 */
1372 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001373 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001374
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001375 /* Check stop command response */
1376 if (brq->stop.resp[0] & R1_ERROR) {
1377 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1378 req->rq_disk->disk_name, __func__,
1379 brq->stop.resp[0]);
1380 gen_err = 1;
1381 }
1382
Ulf Hansson95a91292014-01-29 13:11:27 +01001383 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1384 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001385 if (err)
1386 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001387 }
1388
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001389 /* if general error occurs, retry the write operation. */
1390 if (gen_err) {
1391 pr_warn("%s: retrying write for general error\n",
1392 req->rq_disk->disk_name);
1393 return MMC_BLK_RETRY;
1394 }
1395
Per Forlind78d4a82011-07-01 18:55:30 +02001396 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001397 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001398 pr_debug("%s: retrying because a re-tune was needed\n",
1399 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001400 brq->retune_retry_done = 1;
1401 return MMC_BLK_RETRY;
1402 }
Per Forlind78d4a82011-07-01 18:55:30 +02001403 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1404 req->rq_disk->disk_name, brq->data.error,
1405 (unsigned)blk_rq_pos(req),
1406 (unsigned)blk_rq_sectors(req),
1407 brq->cmd.resp[0], brq->stop.resp[0]);
1408
1409 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001410 if (ecc_err)
1411 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001412 return MMC_BLK_DATA_ERR;
1413 } else {
1414 return MMC_BLK_CMD_ERR;
1415 }
1416 }
1417
Adrian Hunter67716322011-08-29 16:42:15 +03001418 if (!brq->data.bytes_xfered)
1419 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001420
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001421 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1422 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1423 return MMC_BLK_PARTIAL;
1424 else
1425 return MMC_BLK_SUCCESS;
1426 }
1427
Adrian Hunter67716322011-08-29 16:42:15 +03001428 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1429 return MMC_BLK_PARTIAL;
1430
1431 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001432}
1433
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001434static int mmc_blk_packed_err_check(struct mmc_card *card,
1435 struct mmc_async_req *areq)
1436{
1437 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1438 mmc_active);
1439 struct request *req = mq_rq->req;
1440 struct mmc_packed *packed = mq_rq->packed;
1441 int err, check, status;
1442 u8 *ext_csd;
1443
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001444 packed->retries--;
1445 check = mmc_blk_err_check(card, areq);
1446 err = get_card_status(card, &status, 0);
1447 if (err) {
1448 pr_err("%s: error %d sending status command\n",
1449 req->rq_disk->disk_name, err);
1450 return MMC_BLK_ABORT;
1451 }
1452
1453 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001454 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001455 if (err) {
1456 pr_err("%s: error %d sending ext_csd\n",
1457 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001458 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001459 }
1460
1461 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1462 EXT_CSD_PACKED_FAILURE) &&
1463 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1464 EXT_CSD_PACKED_GENERIC_ERROR)) {
1465 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1466 EXT_CSD_PACKED_INDEXED_ERROR) {
1467 packed->idx_failure =
1468 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1469 check = MMC_BLK_PARTIAL;
1470 }
1471 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1472 "failure index: %d\n",
1473 req->rq_disk->disk_name, packed->nr_entries,
1474 packed->blocks, packed->idx_failure);
1475 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001476 kfree(ext_csd);
1477 }
1478
1479 return check;
1480}
1481
Per Forlin54d49d72011-07-01 18:55:29 +02001482static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1483 struct mmc_card *card,
1484 int disable_multi,
1485 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Per Forlin54d49d72011-07-01 18:55:29 +02001487 u32 readcmd, writecmd;
1488 struct mmc_blk_request *brq = &mqrq->brq;
1489 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301491 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001493 /*
1494 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001495 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001496 */
Luca Porziod3df0462015-11-06 15:12:26 +00001497 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001498 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001499 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001500
Per Forlin54d49d72011-07-01 18:55:29 +02001501 memset(brq, 0, sizeof(struct mmc_blk_request));
1502 brq->mrq.cmd = &brq->cmd;
1503 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Per Forlin54d49d72011-07-01 18:55:29 +02001505 brq->cmd.arg = blk_rq_pos(req);
1506 if (!mmc_card_blockaddr(card))
1507 brq->cmd.arg <<= 9;
1508 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1509 brq->data.blksz = 512;
1510 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1511 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001512 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Per Forlin54d49d72011-07-01 18:55:29 +02001514 /*
1515 * The block layer doesn't support all sector count
1516 * restrictions, so we need to be prepared for too big
1517 * requests.
1518 */
1519 if (brq->data.blocks > card->host->max_blk_count)
1520 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001522 if (brq->data.blocks > 1) {
1523 /*
1524 * After a read error, we redo the request one sector
1525 * at a time in order to accurately determine which
1526 * sectors can be read successfully.
1527 */
1528 if (disable_multi)
1529 brq->data.blocks = 1;
1530
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001531 /*
1532 * Some controllers have HW issues while operating
1533 * in multiple I/O mode
1534 */
1535 if (card->host->ops->multi_io_quirk)
1536 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1537 (rq_data_dir(req) == READ) ?
1538 MMC_DATA_READ : MMC_DATA_WRITE,
1539 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001540 }
Per Forlin54d49d72011-07-01 18:55:29 +02001541
1542 if (brq->data.blocks > 1 || do_rel_wr) {
1543 /* SPI multiblock writes terminate using a special
1544 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001545 */
Per Forlin54d49d72011-07-01 18:55:29 +02001546 if (!mmc_host_is_spi(card->host) ||
1547 rq_data_dir(req) == READ)
1548 brq->mrq.stop = &brq->stop;
1549 readcmd = MMC_READ_MULTIPLE_BLOCK;
1550 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1551 } else {
1552 brq->mrq.stop = NULL;
1553 readcmd = MMC_READ_SINGLE_BLOCK;
1554 writecmd = MMC_WRITE_BLOCK;
1555 }
1556 if (rq_data_dir(req) == READ) {
1557 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001558 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001559 if (brq->mrq.stop)
1560 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1561 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001562 } else {
1563 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001564 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001565 if (brq->mrq.stop)
1566 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1567 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001568 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001569
Per Forlin54d49d72011-07-01 18:55:29 +02001570 if (do_rel_wr)
1571 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001572
Per Forlin54d49d72011-07-01 18:55:29 +02001573 /*
Saugata Das42659002011-12-21 13:09:17 +05301574 * Data tag is used only during writing meta data to speed
1575 * up write and any subsequent read of this meta data
1576 */
1577 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1578 (req->cmd_flags & REQ_META) &&
1579 (rq_data_dir(req) == WRITE) &&
1580 ((brq->data.blocks * brq->data.blksz) >=
1581 card->ext_csd.data_tag_unit_size);
1582
1583 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001584 * Pre-defined multi-block transfers are preferable to
1585 * open ended-ones (and necessary for reliable writes).
1586 * However, it is not sufficient to just send CMD23,
1587 * and avoid the final CMD12, as on an error condition
1588 * CMD12 (stop) needs to be sent anyway. This, coupled
1589 * with Auto-CMD23 enhancements provided by some
1590 * hosts, means that the complexity of dealing
1591 * with this is best left to the host. If CMD23 is
1592 * supported by card and host, we'll fill sbc in and let
1593 * the host deal with handling it correctly. This means
1594 * that for hosts that don't expose MMC_CAP_CMD23, no
1595 * change of behavior will be observed.
1596 *
1597 * N.B: Some MMC cards experience perf degradation.
1598 * We'll avoid using CMD23-bounded multiblock writes for
1599 * these, while retaining features like reliable writes.
1600 */
Saugata Das42659002011-12-21 13:09:17 +05301601 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1602 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1603 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001604 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1605 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301606 (do_rel_wr ? (1 << 31) : 0) |
1607 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001608 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1609 brq->mrq.sbc = &brq->sbc;
1610 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001611
Per Forlin54d49d72011-07-01 18:55:29 +02001612 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001613
Per Forlin54d49d72011-07-01 18:55:29 +02001614 brq->data.sg = mqrq->sg;
1615 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001616
Per Forlin54d49d72011-07-01 18:55:29 +02001617 /*
1618 * Adjust the sg list so it is the same size as the
1619 * request.
1620 */
1621 if (brq->data.blocks != blk_rq_sectors(req)) {
1622 int i, data_size = brq->data.blocks << 9;
1623 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001624
Per Forlin54d49d72011-07-01 18:55:29 +02001625 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1626 data_size -= sg->length;
1627 if (data_size <= 0) {
1628 sg->length += data_size;
1629 i++;
1630 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001631 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001632 }
Per Forlin54d49d72011-07-01 18:55:29 +02001633 brq->data.sg_len = i;
1634 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001635
Per Forlinee8a43a2011-07-01 18:55:33 +02001636 mqrq->mmc_active.mrq = &brq->mrq;
1637 mqrq->mmc_active.err_check = mmc_blk_err_check;
1638
Per Forlin54d49d72011-07-01 18:55:29 +02001639 mmc_queue_bounce_pre(mqrq);
1640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001642static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1643 struct mmc_card *card)
1644{
1645 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1646 unsigned int max_seg_sz = queue_max_segment_size(q);
1647 unsigned int len, nr_segs = 0;
1648
1649 do {
1650 len = min(hdr_sz, max_seg_sz);
1651 hdr_sz -= len;
1652 nr_segs++;
1653 } while (hdr_sz);
1654
1655 return nr_segs;
1656}
1657
1658static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1659{
1660 struct request_queue *q = mq->queue;
1661 struct mmc_card *card = mq->card;
1662 struct request *cur = req, *next = NULL;
1663 struct mmc_blk_data *md = mq->data;
1664 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1665 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1666 unsigned int req_sectors = 0, phys_segments = 0;
1667 unsigned int max_blk_count, max_phys_segs;
1668 bool put_back = true;
1669 u8 max_packed_rw = 0;
1670 u8 reqs = 0;
1671
Shawn Lin96e52da2016-08-26 08:49:55 +08001672 /*
1673 * We don't need to check packed for any further
1674 * operation of packed stuff as we set MMC_PACKED_NONE
1675 * and return zero for reqs if geting null packed. Also
1676 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
1677 * it again when removing blk req.
1678 */
1679 if (!mqrq->packed) {
1680 md->flags &= (~MMC_BLK_PACKED_CMD);
1681 goto no_packed;
1682 }
1683
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001684 if (!(md->flags & MMC_BLK_PACKED_CMD))
1685 goto no_packed;
1686
1687 if ((rq_data_dir(cur) == WRITE) &&
1688 mmc_host_packed_wr(card->host))
1689 max_packed_rw = card->ext_csd.max_packed_writes;
1690
1691 if (max_packed_rw == 0)
1692 goto no_packed;
1693
1694 if (mmc_req_rel_wr(cur) &&
1695 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1696 goto no_packed;
1697
1698 if (mmc_large_sector(card) &&
1699 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1700 goto no_packed;
1701
1702 mmc_blk_clear_packed(mqrq);
1703
1704 max_blk_count = min(card->host->max_blk_count,
1705 card->host->max_req_size >> 9);
1706 if (unlikely(max_blk_count > 0xffff))
1707 max_blk_count = 0xffff;
1708
1709 max_phys_segs = queue_max_segments(q);
1710 req_sectors += blk_rq_sectors(cur);
1711 phys_segments += cur->nr_phys_segments;
1712
1713 if (rq_data_dir(cur) == WRITE) {
1714 req_sectors += mmc_large_sector(card) ? 8 : 1;
1715 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1716 }
1717
1718 do {
1719 if (reqs >= max_packed_rw - 1) {
1720 put_back = false;
1721 break;
1722 }
1723
1724 spin_lock_irq(q->queue_lock);
1725 next = blk_fetch_request(q);
1726 spin_unlock_irq(q->queue_lock);
1727 if (!next) {
1728 put_back = false;
1729 break;
1730 }
1731
1732 if (mmc_large_sector(card) &&
1733 !IS_ALIGNED(blk_rq_sectors(next), 8))
1734 break;
1735
Mike Christie3a5e02c2016-06-05 14:32:23 -05001736 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03001737 req_op(next) == REQ_OP_SECURE_ERASE ||
Mike Christie3a5e02c2016-06-05 14:32:23 -05001738 req_op(next) == REQ_OP_FLUSH)
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001739 break;
1740
1741 if (rq_data_dir(cur) != rq_data_dir(next))
1742 break;
1743
1744 if (mmc_req_rel_wr(next) &&
1745 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1746 break;
1747
1748 req_sectors += blk_rq_sectors(next);
1749 if (req_sectors > max_blk_count)
1750 break;
1751
1752 phys_segments += next->nr_phys_segments;
1753 if (phys_segments > max_phys_segs)
1754 break;
1755
1756 list_add_tail(&next->queuelist, &mqrq->packed->list);
1757 cur = next;
1758 reqs++;
1759 } while (1);
1760
1761 if (put_back) {
1762 spin_lock_irq(q->queue_lock);
1763 blk_requeue_request(q, next);
1764 spin_unlock_irq(q->queue_lock);
1765 }
1766
1767 if (reqs > 0) {
1768 list_add(&req->queuelist, &mqrq->packed->list);
1769 mqrq->packed->nr_entries = ++reqs;
1770 mqrq->packed->retries = reqs;
1771 return reqs;
1772 }
1773
1774no_packed:
1775 mqrq->cmd_type = MMC_PACKED_NONE;
1776 return 0;
1777}
1778
1779static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1780 struct mmc_card *card,
1781 struct mmc_queue *mq)
1782{
1783 struct mmc_blk_request *brq = &mqrq->brq;
1784 struct request *req = mqrq->req;
1785 struct request *prq;
1786 struct mmc_blk_data *md = mq->data;
1787 struct mmc_packed *packed = mqrq->packed;
1788 bool do_rel_wr, do_data_tag;
1789 u32 *packed_cmd_hdr;
1790 u8 hdr_blocks;
1791 u8 i = 1;
1792
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001793 mqrq->cmd_type = MMC_PACKED_WRITE;
1794 packed->blocks = 0;
1795 packed->idx_failure = MMC_PACKED_NR_IDX;
1796
1797 packed_cmd_hdr = packed->cmd_hdr;
1798 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001799 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
1800 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001801 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1802
1803 /*
1804 * Argument for each entry of packed group
1805 */
1806 list_for_each_entry(prq, &packed->list, queuelist) {
1807 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1808 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1809 (prq->cmd_flags & REQ_META) &&
1810 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03001811 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001812 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001813 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001814 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1815 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001816 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001817 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001818 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001819 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001820 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001821 packed->blocks += blk_rq_sectors(prq);
1822 i++;
1823 }
1824
1825 memset(brq, 0, sizeof(struct mmc_blk_request));
1826 brq->mrq.cmd = &brq->cmd;
1827 brq->mrq.data = &brq->data;
1828 brq->mrq.sbc = &brq->sbc;
1829 brq->mrq.stop = &brq->stop;
1830
1831 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1832 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1833 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1834
1835 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1836 brq->cmd.arg = blk_rq_pos(req);
1837 if (!mmc_card_blockaddr(card))
1838 brq->cmd.arg <<= 9;
1839 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1840
1841 brq->data.blksz = 512;
1842 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001843 brq->data.flags = MMC_DATA_WRITE;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001844
1845 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1846 brq->stop.arg = 0;
1847 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1848
1849 mmc_set_data_timeout(&brq->data, card);
1850
1851 brq->data.sg = mqrq->sg;
1852 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1853
1854 mqrq->mmc_active.mrq = &brq->mrq;
1855 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1856
1857 mmc_queue_bounce_pre(mqrq);
1858}
1859
Adrian Hunter67716322011-08-29 16:42:15 +03001860static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1861 struct mmc_blk_request *brq, struct request *req,
1862 int ret)
1863{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001864 struct mmc_queue_req *mq_rq;
1865 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1866
Adrian Hunter67716322011-08-29 16:42:15 +03001867 /*
1868 * If this is an SD card and we're writing, we can first
1869 * mark the known good sectors as ok.
1870 *
1871 * If the card is not SD, we can still ok written sectors
1872 * as reported by the controller (which might be less than
1873 * the real number of written sectors, but never more).
1874 */
1875 if (mmc_card_sd(card)) {
1876 u32 blocks;
1877
1878 blocks = mmc_sd_num_wr_blocks(card);
1879 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301880 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001881 }
1882 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001883 if (!mmc_packed_cmd(mq_rq->cmd_type))
1884 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001885 }
1886 return ret;
1887}
1888
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001889static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1890{
1891 struct request *prq;
1892 struct mmc_packed *packed = mq_rq->packed;
1893 int idx = packed->idx_failure, i = 0;
1894 int ret = 0;
1895
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001896 while (!list_empty(&packed->list)) {
1897 prq = list_entry_rq(packed->list.next);
1898 if (idx == i) {
1899 /* retry from error index */
1900 packed->nr_entries -= idx;
1901 mq_rq->req = prq;
1902 ret = 1;
1903
1904 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1905 list_del_init(&prq->queuelist);
1906 mmc_blk_clear_packed(mq_rq);
1907 }
1908 return ret;
1909 }
1910 list_del_init(&prq->queuelist);
1911 blk_end_request(prq, 0, blk_rq_bytes(prq));
1912 i++;
1913 }
1914
1915 mmc_blk_clear_packed(mq_rq);
1916 return ret;
1917}
1918
1919static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1920{
1921 struct request *prq;
1922 struct mmc_packed *packed = mq_rq->packed;
1923
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001924 while (!list_empty(&packed->list)) {
1925 prq = list_entry_rq(packed->list.next);
1926 list_del_init(&prq->queuelist);
1927 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1928 }
1929
1930 mmc_blk_clear_packed(mq_rq);
1931}
1932
1933static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1934 struct mmc_queue_req *mq_rq)
1935{
1936 struct request *prq;
1937 struct request_queue *q = mq->queue;
1938 struct mmc_packed *packed = mq_rq->packed;
1939
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001940 while (!list_empty(&packed->list)) {
1941 prq = list_entry_rq(packed->list.prev);
1942 if (prq->queuelist.prev != &packed->list) {
1943 list_del_init(&prq->queuelist);
1944 spin_lock_irq(q->queue_lock);
1945 blk_requeue_request(mq->queue, prq);
1946 spin_unlock_irq(q->queue_lock);
1947 } else {
1948 list_del_init(&prq->queuelist);
1949 }
1950 }
1951
1952 mmc_blk_clear_packed(mq_rq);
1953}
1954
Per Forlinee8a43a2011-07-01 18:55:33 +02001955static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001956{
1957 struct mmc_blk_data *md = mq->data;
1958 struct mmc_card *card = md->queue.card;
1959 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001960 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001961 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001962 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301963 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001964 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001965 const u8 packed_nr = 2;
1966 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001967
1968 if (!rqc && !mq->mqrq_prev->req)
1969 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001970
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001971 if (rqc)
1972 reqs = mmc_blk_prep_packed_list(mq, rqc);
1973
Per Forlin54d49d72011-07-01 18:55:29 +02001974 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001975 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301976 /*
1977 * When 4KB native sector is enabled, only 8 blocks
1978 * multiple read or write is allowed
1979 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001980 if (mmc_large_sector(card) &&
1981 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301982 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1983 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001984 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301985 goto cmd_abort;
1986 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001987
1988 if (reqs >= packed_nr)
1989 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1990 card, mq);
1991 else
1992 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001993 areq = &mq->mqrq_cur->mmc_active;
1994 } else
1995 areq = NULL;
1996 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001997 if (!areq) {
1998 if (status == MMC_BLK_NEW_REQUEST)
1999 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002000 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002001 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002002
Per Forlinee8a43a2011-07-01 18:55:33 +02002003 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2004 brq = &mq_rq->brq;
2005 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002006 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002007 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002008
Per Forlind78d4a82011-07-01 18:55:30 +02002009 switch (status) {
2010 case MMC_BLK_SUCCESS:
2011 case MMC_BLK_PARTIAL:
2012 /*
2013 * A block was successfully transferred.
2014 */
Adrian Hunter67716322011-08-29 16:42:15 +03002015 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002016
2017 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2018 ret = mmc_blk_end_packed_req(mq_rq);
2019 break;
2020 } else {
2021 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002022 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002023 }
2024
Adrian Hunter67716322011-08-29 16:42:15 +03002025 /*
2026 * If the blk_end_request function returns non-zero even
2027 * though all data has been transferred and no errors
2028 * were returned by the host controller, it's a bug.
2029 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002030 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302031 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002032 __func__, blk_rq_bytes(req),
2033 brq->data.bytes_xfered);
2034 rqc = NULL;
2035 goto cmd_abort;
2036 }
Per Forlind78d4a82011-07-01 18:55:30 +02002037 break;
2038 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002039 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002040 if (mmc_blk_reset(md, card->host, type))
2041 goto cmd_abort;
2042 if (!ret)
2043 goto start_new_req;
2044 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002045 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002046 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002047 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002048 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002049 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002050 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002051 if (!mmc_blk_reset(md, card->host, type))
2052 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002053 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002054 case MMC_BLK_DATA_ERR: {
2055 int err;
2056
2057 err = mmc_blk_reset(md, card->host, type);
2058 if (!err)
2059 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002060 if (err == -ENODEV ||
2061 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03002062 goto cmd_abort;
2063 /* Fall through */
2064 }
2065 case MMC_BLK_ECC_ERR:
2066 if (brq->data.blocks > 1) {
2067 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002068 pr_warn("%s: retrying using single block read\n",
2069 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002070 disable_multi = 1;
2071 break;
2072 }
Per Forlind78d4a82011-07-01 18:55:30 +02002073 /*
2074 * After an error, we redo I/O one sector at a
2075 * time, so we only reach here after trying to
2076 * read a single sector.
2077 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302078 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002079 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002080 if (!ret)
2081 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002082 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302083 case MMC_BLK_NOMEDIUM:
2084 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002085 default:
2086 pr_err("%s: Unhandled return value (%d)",
2087 req->rq_disk->disk_name, status);
2088 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002089 }
2090
Per Forlinee8a43a2011-07-01 18:55:33 +02002091 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002092 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2093 if (!mq_rq->packed->retries)
2094 goto cmd_abort;
2095 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2096 mmc_start_req(card->host,
2097 &mq_rq->mmc_active, NULL);
2098 } else {
2099
2100 /*
2101 * In case of a incomplete request
2102 * prepare it again and resend.
2103 */
2104 mmc_blk_rw_rq_prep(mq_rq, card,
2105 disable_multi, mq);
2106 mmc_start_req(card->host,
2107 &mq_rq->mmc_active, NULL);
2108 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002109 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 } while (ret);
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 1;
2114
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002115 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002116 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2117 mmc_blk_abort_packed_req(mq_rq);
2118 } else {
2119 if (mmc_card_removed(card))
2120 req->cmd_flags |= REQ_QUIET;
2121 while (ret)
2122 ret = blk_end_request(req, -EIO,
2123 blk_rq_cur_bytes(req));
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Per Forlinee8a43a2011-07-01 18:55:33 +02002126 start_new_req:
2127 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002128 if (mmc_card_removed(card)) {
2129 rqc->cmd_flags |= REQ_QUIET;
2130 blk_end_request_all(rqc, -EIO);
2131 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002132 /*
2133 * If current request is packed, it needs to put back.
2134 */
2135 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2136 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2137
Seungwon Jeon7a819022013-01-22 19:48:07 +09002138 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2139 mmc_start_req(card->host,
2140 &mq->mqrq_cur->mmc_active, NULL);
2141 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002142 }
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
2145}
2146
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002147int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002148{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002149 int ret;
2150 struct mmc_blk_data *md = mq->data;
2151 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002152 struct mmc_host *host = card->host;
2153 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002154 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002155
Per Forlinee8a43a2011-07-01 18:55:33 +02002156 if (req && !mq->mqrq_prev->req)
2157 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002158 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002159
Andrei Warkentin371a6892011-04-11 18:10:25 -05002160 ret = mmc_blk_part_switch(card, md);
2161 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002162 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302163 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002164 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165 ret = 0;
2166 goto out;
2167 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002168
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002169 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002170 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002171 /* complete ongoing async transfer before issuing discard */
2172 if (card->host->areq)
2173 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002174 ret = mmc_blk_issue_discard_rq(mq, req);
2175 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2176 /* complete ongoing async transfer before issuing secure erase*/
2177 if (card->host->areq)
2178 mmc_blk_issue_rw_rq(mq, NULL);
2179 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002180 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002181 /* complete ongoing async transfer before issuing flush */
2182 if (card->host->areq)
2183 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002184 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002185 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002186 if (!req && host->areq) {
2187 spin_lock_irqsave(&host->context_info.lock, flags);
2188 host->context_info.is_waiting_last_req = true;
2189 spin_unlock_irqrestore(&host->context_info.lock, flags);
2190 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002191 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002192 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002193
Andrei Warkentin371a6892011-04-11 18:10:25 -05002194out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002195 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002196 /*
2197 * Release host when there are no more requests
2198 * and after special request(discard, flush) is done.
2199 * In case sepecial request, there is no reentry to
2200 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2201 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002202 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002203 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Russell Kinga6f6c962006-01-03 22:38:44 +00002206static inline int mmc_blk_readonly(struct mmc_card *card)
2207{
2208 return mmc_card_readonly(card) ||
2209 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2210}
2211
Andrei Warkentin371a6892011-04-11 18:10:25 -05002212static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2213 struct device *parent,
2214 sector_t size,
2215 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002216 const char *subname,
2217 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct mmc_blk_data *md;
2220 int devidx, ret;
2221
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002222again:
2223 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2224 return ERR_PTR(-ENOMEM);
2225
2226 spin_lock(&mmc_blk_lock);
2227 ret = ida_get_new(&mmc_blk_ida, &devidx);
2228 spin_unlock(&mmc_blk_lock);
2229
2230 if (ret == -EAGAIN)
2231 goto again;
2232 else if (ret)
2233 return ERR_PTR(ret);
2234
2235 if (devidx >= max_devices) {
2236 ret = -ENOSPC;
2237 goto out;
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002240 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002241 if (!md) {
2242 ret = -ENOMEM;
2243 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002245
Johan Rudholmadd710e2011-12-02 08:51:06 +01002246 md->area_type = area_type;
2247
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002248 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002249 * Set the read-only status based on the supported commands
2250 * and the write protect switch.
2251 */
2252 md->read_only = mmc_blk_readonly(card);
2253
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002254 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002255 if (md->disk == NULL) {
2256 ret = -ENOMEM;
2257 goto err_kfree;
2258 }
2259
2260 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002261 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002262 md->usage = 1;
2263
Adrian Hunterd09408a2011-06-23 13:40:28 +03002264 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002265 if (ret)
2266 goto err_putdisk;
2267
Russell Kinga6f6c962006-01-03 22:38:44 +00002268 md->queue.data = md;
2269
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002270 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002271 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002272 md->disk->fops = &mmc_bdops;
2273 md->disk->private_data = md;
2274 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002275 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002276 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002277 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002278 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002279 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002280
2281 /*
2282 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2283 *
2284 * - be set for removable media with permanent block devices
2285 * - be unset for removable block devices with permanent media
2286 *
2287 * Since MMC block devices clearly fall under the second
2288 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2289 * should use the block device creation/destruction hotplug
2290 * messages to tell when the card is present.
2291 */
2292
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002293 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002294 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002295
Saugata Dasa5075eb2012-05-17 16:32:21 +05302296 if (mmc_card_mmc(card))
2297 blk_queue_logical_block_size(md->queue.queue,
2298 card->ext_csd.data_sector_size);
2299 else
2300 blk_queue_logical_block_size(md->queue.queue, 512);
2301
Andrei Warkentin371a6892011-04-11 18:10:25 -05002302 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002303
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002304 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002305 if ((mmc_card_mmc(card) &&
2306 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002307 (mmc_card_sd(card) &&
2308 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2309 md->flags |= MMC_BLK_CMD23;
2310 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002311
2312 if (mmc_card_mmc(card) &&
2313 md->flags & MMC_BLK_CMD23 &&
2314 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2315 card->ext_csd.rel_sectors)) {
2316 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002317 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002318 }
2319
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002320 if (mmc_card_mmc(card) &&
2321 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2322 (md->flags & MMC_BLK_CMD23) &&
2323 card->ext_csd.packed_event_en) {
2324 if (!mmc_packed_init(&md->queue, card))
2325 md->flags |= MMC_BLK_PACKED_CMD;
2326 }
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002329
2330 err_putdisk:
2331 put_disk(md->disk);
2332 err_kfree:
2333 kfree(md);
2334 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002335 spin_lock(&mmc_blk_lock);
2336 ida_remove(&mmc_blk_ida, devidx);
2337 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002338 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Andrei Warkentin371a6892011-04-11 18:10:25 -05002341static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2342{
2343 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002344
2345 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2346 /*
2347 * The EXT_CSD sector count is in number or 512 byte
2348 * sectors.
2349 */
2350 size = card->ext_csd.sectors;
2351 } else {
2352 /*
2353 * The CSD capacity field is in units of read_blkbits.
2354 * set_capacity takes units of 512 bytes.
2355 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002356 size = (typeof(sector_t))card->csd.capacity
2357 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002358 }
2359
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002360 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002361 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002362}
2363
2364static int mmc_blk_alloc_part(struct mmc_card *card,
2365 struct mmc_blk_data *md,
2366 unsigned int part_type,
2367 sector_t size,
2368 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002369 const char *subname,
2370 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002371{
2372 char cap_str[10];
2373 struct mmc_blk_data *part_md;
2374
2375 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002376 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002377 if (IS_ERR(part_md))
2378 return PTR_ERR(part_md);
2379 part_md->part_type = part_type;
2380 list_add(&part_md->part, &md->part);
2381
James Bottomleyb9f28d82015-03-05 18:47:01 -08002382 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002383 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302384 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002385 part_md->disk->disk_name, mmc_card_id(card),
2386 mmc_card_name(card), part_md->part_type, cap_str);
2387 return 0;
2388}
2389
Namjae Jeone0c368d2011-10-06 23:41:38 +09002390/* MMC Physical partitions consist of two boot partitions and
2391 * up to four general purpose partitions.
2392 * For each partition enabled in EXT_CSD a block device will be allocatedi
2393 * to provide access to the partition.
2394 */
2395
Andrei Warkentin371a6892011-04-11 18:10:25 -05002396static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2397{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002398 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002399
2400 if (!mmc_card_mmc(card))
2401 return 0;
2402
Namjae Jeone0c368d2011-10-06 23:41:38 +09002403 for (idx = 0; idx < card->nr_parts; idx++) {
2404 if (card->part[idx].size) {
2405 ret = mmc_blk_alloc_part(card, md,
2406 card->part[idx].part_cfg,
2407 card->part[idx].size >> 9,
2408 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002409 card->part[idx].name,
2410 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002411 if (ret)
2412 return ret;
2413 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002414 }
2415
2416 return ret;
2417}
2418
Andrei Warkentin371a6892011-04-11 18:10:25 -05002419static void mmc_blk_remove_req(struct mmc_blk_data *md)
2420{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002421 struct mmc_card *card;
2422
Andrei Warkentin371a6892011-04-11 18:10:25 -05002423 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002424 /*
2425 * Flush remaining requests and free queues. It
2426 * is freeing the queue that stops new requests
2427 * from being accepted.
2428 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002429 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002430 mmc_cleanup_queue(&md->queue);
2431 if (md->flags & MMC_BLK_PACKED_CMD)
2432 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002433 if (md->disk->flags & GENHD_FL_UP) {
2434 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002435 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2436 card->ext_csd.boot_ro_lockable)
2437 device_remove_file(disk_to_dev(md->disk),
2438 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002439
Andrei Warkentin371a6892011-04-11 18:10:25 -05002440 del_gendisk(md->disk);
2441 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002442 mmc_blk_put(md);
2443 }
2444}
2445
2446static void mmc_blk_remove_parts(struct mmc_card *card,
2447 struct mmc_blk_data *md)
2448{
2449 struct list_head *pos, *q;
2450 struct mmc_blk_data *part_md;
2451
2452 list_for_each_safe(pos, q, &md->part) {
2453 part_md = list_entry(pos, struct mmc_blk_data, part);
2454 list_del(pos);
2455 mmc_blk_remove_req(part_md);
2456 }
2457}
2458
2459static int mmc_add_disk(struct mmc_blk_data *md)
2460{
2461 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002462 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002463
Dan Williams307d8e62016-06-20 10:40:44 -07002464 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002465 md->force_ro.show = force_ro_show;
2466 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302467 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002468 md->force_ro.attr.name = "force_ro";
2469 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2470 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2471 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002472 goto force_ro_fail;
2473
2474 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2475 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002476 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002477
2478 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2479 mode = S_IRUGO;
2480 else
2481 mode = S_IRUGO | S_IWUSR;
2482
2483 md->power_ro_lock.show = power_ro_lock_show;
2484 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002485 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002486 md->power_ro_lock.attr.mode = mode;
2487 md->power_ro_lock.attr.name =
2488 "ro_lock_until_next_power_on";
2489 ret = device_create_file(disk_to_dev(md->disk),
2490 &md->power_ro_lock);
2491 if (ret)
2492 goto power_ro_lock_fail;
2493 }
2494 return ret;
2495
2496power_ro_lock_fail:
2497 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2498force_ro_fail:
2499 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002500
2501 return ret;
2502}
2503
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002504static const struct mmc_fixup blk_fixups[] =
2505{
Chris Ballc59d4472011-11-11 22:01:43 -05002506 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2507 MMC_QUIRK_INAND_CMD38),
2508 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2509 MMC_QUIRK_INAND_CMD38),
2510 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2511 MMC_QUIRK_INAND_CMD38),
2512 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2513 MMC_QUIRK_INAND_CMD38),
2514 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2515 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002516
2517 /*
2518 * Some MMC cards experience performance degradation with CMD23
2519 * instead of CMD12-bounded multiblock transfers. For now we'll
2520 * black list what's bad...
2521 * - Certain Toshiba cards.
2522 *
2523 * N.B. This doesn't affect SD cards.
2524 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002525 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2526 MMC_QUIRK_BLK_NO_CMD23),
2527 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2528 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002529 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002530 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002531 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002532 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002533 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002534 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002535
2536 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002537 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002538 */
Chris Ballc59d4472011-11-11 22:01:43 -05002539 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002540 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002541 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2542 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002543
Ian Chen3550ccd2012-08-29 15:05:36 +09002544 /*
2545 * On these Samsung MoviNAND parts, performing secure erase or
2546 * secure trim can result in unrecoverable corruption due to a
2547 * firmware bug.
2548 */
2549 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2550 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2551 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2552 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2553 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2554 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2555 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2556 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2557 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2558 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2559 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2560 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2561 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2562 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2563 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2564 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2565
Shawn Linb5b4ff02015-08-12 13:08:32 +08002566 /*
2567 * On Some Kingston eMMCs, performing trim can result in
2568 * unrecoverable data conrruption occasionally due to a firmware bug.
2569 */
2570 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2571 MMC_QUIRK_TRIM_BROKEN),
2572 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2573 MMC_QUIRK_TRIM_BROKEN),
2574
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002575 END_FIXUP
2576};
2577
Ulf Hansson96541ba2015-04-14 13:06:12 +02002578static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002580 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002581 char cap_str[10];
2582
Pierre Ossman912490d2005-05-21 10:27:02 +01002583 /*
2584 * Check that the card supports the command class(es) we need.
2585 */
2586 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return -ENODEV;
2588
Lukas Czerner5204d002014-06-18 13:18:07 +02002589 mmc_fixup_device(card, blk_fixups);
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 md = mmc_blk_alloc(card);
2592 if (IS_ERR(md))
2593 return PTR_ERR(md);
2594
James Bottomleyb9f28d82015-03-05 18:47:01 -08002595 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002596 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302597 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002599 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Andrei Warkentin371a6892011-04-11 18:10:25 -05002601 if (mmc_blk_alloc_parts(card, md))
2602 goto out;
2603
Ulf Hansson96541ba2015-04-14 13:06:12 +02002604 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002605
Andrei Warkentin371a6892011-04-11 18:10:25 -05002606 if (mmc_add_disk(md))
2607 goto out;
2608
2609 list_for_each_entry(part_md, &md->part, part) {
2610 if (mmc_add_disk(part_md))
2611 goto out;
2612 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002613
2614 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2615 pm_runtime_use_autosuspend(&card->dev);
2616
2617 /*
2618 * Don't enable runtime PM for SD-combo cards here. Leave that
2619 * decision to be taken during the SDIO init sequence instead.
2620 */
2621 if (card->type != MMC_TYPE_SD_COMBO) {
2622 pm_runtime_set_active(&card->dev);
2623 pm_runtime_enable(&card->dev);
2624 }
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return 0;
2627
2628 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002629 mmc_blk_remove_parts(card, md);
2630 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Ulf Hansson96541ba2015-04-14 13:06:12 +02002634static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002636 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Andrei Warkentin371a6892011-04-11 18:10:25 -05002638 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002639 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002640 mmc_claim_host(card->host);
2641 mmc_blk_part_switch(card, md);
2642 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002643 if (card->type != MMC_TYPE_SD_COMBO)
2644 pm_runtime_disable(&card->dev);
2645 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002646 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002647 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Ulf Hansson96541ba2015-04-14 13:06:12 +02002650static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002652 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002653 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 if (md) {
2656 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002657 list_for_each_entry(part_md, &md->part, part) {
2658 mmc_queue_suspend(&part_md->queue);
2659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661 return 0;
2662}
2663
Ulf Hansson96541ba2015-04-14 13:06:12 +02002664static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002665{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002666 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002667}
2668
Ulf Hansson0967edc2014-10-06 11:29:42 +02002669#ifdef CONFIG_PM_SLEEP
2670static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002671{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002672 struct mmc_card *card = mmc_dev_to_card(dev);
2673
2674 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002675}
2676
Ulf Hansson0967edc2014-10-06 11:29:42 +02002677static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002679 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002680 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002683 /*
2684 * Resume involves the card going into idle state,
2685 * so current partition is always the main one.
2686 */
2687 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002689 list_for_each_entry(part_md, &md->part, part) {
2690 mmc_queue_resume(&part_md->queue);
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 return 0;
2694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695#endif
2696
Ulf Hansson0967edc2014-10-06 11:29:42 +02002697static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2698
Ulf Hansson96541ba2015-04-14 13:06:12 +02002699static struct mmc_driver mmc_driver = {
2700 .drv = {
2701 .name = "mmcblk",
2702 .pm = &mmc_blk_pm_ops,
2703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 .probe = mmc_blk_probe,
2705 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002706 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707};
2708
2709static int __init mmc_blk_init(void)
2710{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002711 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002713 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2714 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2715
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002716 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002717
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002718 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2719 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002722 res = mmc_register_driver(&mmc_driver);
2723 if (res)
2724 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002726 return 0;
2727 out2:
2728 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 out:
2730 return res;
2731}
2732
2733static void __exit mmc_blk_exit(void)
2734{
2735 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002736 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739module_init(mmc_blk_init);
2740module_exit(mmc_blk_exit);
2741
2742MODULE_LICENSE("GPL");
2743MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2744