blob: 709a872ed484a9da1ce620238c3222190c612f86 [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"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060063#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
71
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
90static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900105#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000108 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200118 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900129enum {
130 MMC_PACKED_NR_IDX = -1,
131 MMC_PACKED_NR_ZERO,
132 MMC_PACKED_NR_SINGLE,
133};
134
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400135module_param(perdev_minors, int, 0444);
136MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
137
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200138static inline int mmc_blk_part_switch(struct mmc_card *card,
139 struct mmc_blk_data *md);
140static int get_card_status(struct mmc_card *card, u32 *status, int retries);
141
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900142static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
143{
144 struct mmc_packed *packed = mqrq->packed;
145
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900146 mqrq->cmd_type = MMC_PACKED_NONE;
147 packed->nr_entries = MMC_PACKED_NR_ZERO;
148 packed->idx_failure = MMC_PACKED_NR_IDX;
149 packed->retries = 0;
150 packed->blocks = 0;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
154{
155 struct mmc_blk_data *md;
156
Arjan van de Vena621aae2006-01-12 18:43:35 +0000157 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 md = disk->private_data;
159 if (md && md->usage == 0)
160 md = NULL;
161 if (md)
162 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000163 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return md;
166}
167
Andrei Warkentin371a6892011-04-11 18:10:25 -0500168static inline int mmc_get_devidx(struct gendisk *disk)
169{
Colin Cross382c55f2015-10-22 10:00:41 -0700170 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500171 return devidx;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void mmc_blk_put(struct mmc_blk_data *md)
175{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000176 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 md->usage--;
178 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800180 blk_cleanup_queue(md->queue.queue);
181
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200182 spin_lock(&mmc_blk_lock);
183 ida_remove(&mmc_blk_ida, devidx);
184 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(md);
188 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000189 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Johan Rudholmadd710e2011-12-02 08:51:06 +0100192static ssize_t power_ro_lock_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 int ret;
196 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
197 struct mmc_card *card = md->queue.card;
198 int locked = 0;
199
200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
201 locked = 2;
202 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
203 locked = 1;
204
205 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
206
Tomas Winkler9098f842015-07-16 15:50:45 +0200207 mmc_blk_put(md);
208
Johan Rudholmadd710e2011-12-02 08:51:06 +0100209 return ret;
210}
211
212static ssize_t power_ro_lock_store(struct device *dev,
213 struct device_attribute *attr, const char *buf, size_t count)
214{
215 int ret;
216 struct mmc_blk_data *md, *part_md;
217 struct mmc_card *card;
218 unsigned long set;
219
220 if (kstrtoul(buf, 0, &set))
221 return -EINVAL;
222
223 if (set != 1)
224 return count;
225
226 md = mmc_blk_get(dev_to_disk(dev));
227 card = md->queue.card;
228
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200229 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100230
231 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
232 card->ext_csd.boot_ro_lock |
233 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
234 card->ext_csd.part_time);
235 if (ret)
236 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
237 else
238 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
239
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200240 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100241
242 if (!ret) {
243 pr_info("%s: Locking boot partition ro until next power on\n",
244 md->disk->disk_name);
245 set_disk_ro(md->disk, 1);
246
247 list_for_each_entry(part_md, &md->part, part)
248 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
249 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
250 set_disk_ro(part_md->disk, 1);
251 }
252 }
253
254 mmc_blk_put(md);
255 return count;
256}
257
Andrei Warkentin371a6892011-04-11 18:10:25 -0500258static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
259 char *buf)
260{
261 int ret;
262 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
263
Baruch Siach0031a982014-09-22 10:12:51 +0300264 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500265 get_disk_ro(dev_to_disk(dev)) ^
266 md->read_only);
267 mmc_blk_put(md);
268 return ret;
269}
270
271static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
273{
274 int ret;
275 char *end;
276 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
277 unsigned long set = simple_strtoul(buf, &end, 0);
278 if (end == buf) {
279 ret = -EINVAL;
280 goto out;
281 }
282
283 set_disk_ro(dev_to_disk(dev), set || md->read_only);
284 ret = count;
285out:
286 mmc_blk_put(md);
287 return ret;
288}
289
Al Viroa5a15612008-03-02 10:33:30 -0500290static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Al Viroa5a15612008-03-02 10:33:30 -0500292 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int ret = -ENXIO;
294
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200295 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (md) {
297 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500298 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700300
Al Viroa5a15612008-03-02 10:33:30 -0500301 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700302 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700303 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200306 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 return ret;
309}
310
Al Virodb2a1442013-05-05 21:52:57 -0400311static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Al Viroa5a15612008-03-02 10:33:30 -0500313 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200315 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200317 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800321mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800323 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
324 geo->heads = 4;
325 geo->sectors = 16;
326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
John Calixtocb87ea22011-04-26 18:56:29 -0400329struct mmc_blk_ioc_data {
330 struct mmc_ioc_cmd ic;
331 unsigned char *buf;
332 u64 buf_bytes;
333};
334
335static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
336 struct mmc_ioc_cmd __user *user)
337{
338 struct mmc_blk_ioc_data *idata;
339 int err;
340
yalin wang1ff89502015-11-12 19:27:11 +0800341 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400342 if (!idata) {
343 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400344 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400345 }
346
347 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
348 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400349 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400350 }
351
352 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
353 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
354 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400355 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400356 }
357
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300358 if (!idata->buf_bytes) {
359 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100360 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300361 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100362
yalin wang1ff89502015-11-12 19:27:11 +0800363 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400364 if (!idata->buf) {
365 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400366 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400367 }
368
369 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
370 idata->ic.data_ptr, idata->buf_bytes)) {
371 err = -EFAULT;
372 goto copy_err;
373 }
374
375 return idata;
376
377copy_err:
378 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400379idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400380 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400381out:
John Calixtocb87ea22011-04-26 18:56:29 -0400382 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400383}
384
Jon Huntera5f57742015-09-22 10:27:53 +0100385static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
386 struct mmc_blk_ioc_data *idata)
387{
388 struct mmc_ioc_cmd *ic = &idata->ic;
389
390 if (copy_to_user(&(ic_ptr->response), ic->response,
391 sizeof(ic->response)))
392 return -EFAULT;
393
394 if (!idata->ic.write_flag) {
395 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
396 idata->buf, idata->buf_bytes))
397 return -EFAULT;
398 }
399
400 return 0;
401}
402
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200403static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
404 u32 retries_max)
405{
406 int err;
407 u32 retry_count = 0;
408
409 if (!status || !retries_max)
410 return -EINVAL;
411
412 do {
413 err = get_card_status(card, status, 5);
414 if (err)
415 break;
416
417 if (!R1_STATUS(*status) &&
418 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
419 break; /* RPMB programming operation complete */
420
421 /*
422 * Rechedule to give the MMC device a chance to continue
423 * processing the previous command without being polled too
424 * frequently.
425 */
426 usleep_range(1000, 5000);
427 } while (++retry_count < retries_max);
428
429 if (retry_count == retries_max)
430 err = -EPERM;
431
432 return err;
433}
434
Maya Erez775a9362013-04-18 15:41:55 +0300435static int ioctl_do_sanitize(struct mmc_card *card)
436{
437 int err;
438
Ulf Hanssona2d10862013-12-16 14:37:26 +0100439 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300440 pr_warn("%s: %s - SANITIZE is not supported\n",
441 mmc_hostname(card->host), __func__);
442 err = -EOPNOTSUPP;
443 goto out;
444 }
445
446 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
447 mmc_hostname(card->host), __func__);
448
449 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450 EXT_CSD_SANITIZE_START, 1,
451 MMC_SANITIZE_REQ_TIMEOUT);
452
453 if (err)
454 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
455 mmc_hostname(card->host), __func__, err);
456
457 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
458 __func__);
459out:
460 return err;
461}
462
Jon Huntera5f57742015-09-22 10:27:53 +0100463static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
464 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400465{
John Calixtocb87ea22011-04-26 18:56:29 -0400466 struct mmc_command cmd = {0};
467 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530468 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400469 struct scatterlist sg;
470 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200471 int is_rpmb = false;
472 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400473
Jon Huntera5f57742015-09-22 10:27:53 +0100474 if (!card || !md || !idata)
475 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400476
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200477 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
478 is_rpmb = true;
479
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100480 cmd.opcode = idata->ic.opcode;
481 cmd.arg = idata->ic.arg;
482 cmd.flags = idata->ic.flags;
483
484 if (idata->buf_bytes) {
485 data.sg = &sg;
486 data.sg_len = 1;
487 data.blksz = idata->ic.blksz;
488 data.blocks = idata->ic.blocks;
489
490 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
491
492 if (idata->ic.write_flag)
493 data.flags = MMC_DATA_WRITE;
494 else
495 data.flags = MMC_DATA_READ;
496
497 /* data.flags must already be set before doing this. */
498 mmc_set_data_timeout(&data, card);
499
500 /* Allow overriding the timeout_ns for empirical tuning. */
501 if (idata->ic.data_timeout_ns)
502 data.timeout_ns = idata->ic.data_timeout_ns;
503
504 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
505 /*
506 * Pretend this is a data transfer and rely on the
507 * host driver to compute timeout. When all host
508 * drivers support cmd.cmd_timeout for R1B, this
509 * can be changed to:
510 *
511 * mrq.data = NULL;
512 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
513 */
514 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
515 }
516
517 mrq.data = &data;
518 }
519
520 mrq.cmd = &cmd;
521
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200522 err = mmc_blk_part_switch(card, md);
523 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100524 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200525
John Calixtocb87ea22011-04-26 18:56:29 -0400526 if (idata->ic.is_acmd) {
527 err = mmc_app_cmd(card->host, card);
528 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100529 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400530 }
531
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200532 if (is_rpmb) {
533 err = mmc_set_blockcount(card, data.blocks,
534 idata->ic.write_flag & (1 << 31));
535 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100536 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200537 }
538
Yaniv Gardia82e4842013-06-05 14:13:08 +0300539 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
540 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300541 err = ioctl_do_sanitize(card);
542
543 if (err)
544 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
545 __func__, err);
546
Jon Huntera5f57742015-09-22 10:27:53 +0100547 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300548 }
549
John Calixtocb87ea22011-04-26 18:56:29 -0400550 mmc_wait_for_req(card->host, &mrq);
551
552 if (cmd.error) {
553 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
554 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100555 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400556 }
557 if (data.error) {
558 dev_err(mmc_dev(card->host), "%s: data error %d\n",
559 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100560 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400561 }
562
563 /*
564 * According to the SD specs, some commands require a delay after
565 * issuing the command.
566 */
567 if (idata->ic.postsleep_min_us)
568 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
569
Jon Huntera5f57742015-09-22 10:27:53 +0100570 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400571
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200572 if (is_rpmb) {
573 /*
574 * Ensure RPMB command has completed by polling CMD13
575 * "Send Status".
576 */
577 err = ioctl_rpmb_card_status_poll(card, &status, 5);
578 if (err)
579 dev_err(mmc_dev(card->host),
580 "%s: Card Status=0x%08X, error %d\n",
581 __func__, status, err);
582 }
583
Jon Huntera5f57742015-09-22 10:27:53 +0100584 return err;
585}
586
587static int mmc_blk_ioctl_cmd(struct block_device *bdev,
588 struct mmc_ioc_cmd __user *ic_ptr)
589{
590 struct mmc_blk_ioc_data *idata;
591 struct mmc_blk_data *md;
592 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700593 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100594
Shawn Lin83c742c2016-03-16 18:15:47 +0800595 /*
596 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
597 * whole block device, not on a partition. This prevents overspray
598 * between sibling partitions.
599 */
600 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
601 return -EPERM;
602
Jon Huntera5f57742015-09-22 10:27:53 +0100603 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
604 if (IS_ERR(idata))
605 return PTR_ERR(idata);
606
607 md = mmc_blk_get(bdev->bd_disk);
608 if (!md) {
609 err = -EINVAL;
610 goto cmd_err;
611 }
612
613 card = md->queue.card;
614 if (IS_ERR(card)) {
615 err = PTR_ERR(card);
616 goto cmd_done;
617 }
618
619 mmc_get_card(card);
620
Grant Grundlerb0934102015-09-23 18:30:33 -0700621 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100622
Adrian Hunter3c866562016-05-04 14:38:12 +0300623 /* Always switch back to main area after RPMB access */
624 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
625 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
626
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200627 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400628
Grant Grundlerb0934102015-09-23 18:30:33 -0700629 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100630
John Calixtocb87ea22011-04-26 18:56:29 -0400631cmd_done:
632 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300633cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400634 kfree(idata->buf);
635 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700636 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400637}
638
Jon Huntera5f57742015-09-22 10:27:53 +0100639static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
640 struct mmc_ioc_multi_cmd __user *user)
641{
642 struct mmc_blk_ioc_data **idata = NULL;
643 struct mmc_ioc_cmd __user *cmds = user->cmds;
644 struct mmc_card *card;
645 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700646 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100647 __u64 num_of_cmds;
648
Shawn Lin83c742c2016-03-16 18:15:47 +0800649 /*
650 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
651 * whole block device, not on a partition. This prevents overspray
652 * between sibling partitions.
653 */
654 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
655 return -EPERM;
656
Jon Huntera5f57742015-09-22 10:27:53 +0100657 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
658 sizeof(num_of_cmds)))
659 return -EFAULT;
660
661 if (num_of_cmds > MMC_IOC_MAX_CMDS)
662 return -EINVAL;
663
664 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
665 if (!idata)
666 return -ENOMEM;
667
668 for (i = 0; i < num_of_cmds; i++) {
669 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
670 if (IS_ERR(idata[i])) {
671 err = PTR_ERR(idata[i]);
672 num_of_cmds = i;
673 goto cmd_err;
674 }
675 }
676
677 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800678 if (!md) {
679 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100680 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800681 }
Jon Huntera5f57742015-09-22 10:27:53 +0100682
683 card = md->queue.card;
684 if (IS_ERR(card)) {
685 err = PTR_ERR(card);
686 goto cmd_done;
687 }
688
689 mmc_get_card(card);
690
Grant Grundlerb0934102015-09-23 18:30:33 -0700691 for (i = 0; i < num_of_cmds && !ioc_err; i++)
692 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100693
Adrian Hunter3c866562016-05-04 14:38:12 +0300694 /* Always switch back to main area after RPMB access */
695 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
696 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
697
Jon Huntera5f57742015-09-22 10:27:53 +0100698 mmc_put_card(card);
699
700 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700701 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100702 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100703
704cmd_done:
705 mmc_blk_put(md);
706cmd_err:
707 for (i = 0; i < num_of_cmds; i++) {
708 kfree(idata[i]->buf);
709 kfree(idata[i]);
710 }
711 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700712 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100713}
714
John Calixtocb87ea22011-04-26 18:56:29 -0400715static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
716 unsigned int cmd, unsigned long arg)
717{
Jon Huntera5f57742015-09-22 10:27:53 +0100718 switch (cmd) {
719 case MMC_IOC_CMD:
720 return mmc_blk_ioctl_cmd(bdev,
721 (struct mmc_ioc_cmd __user *)arg);
722 case MMC_IOC_MULTI_CMD:
723 return mmc_blk_ioctl_multi_cmd(bdev,
724 (struct mmc_ioc_multi_cmd __user *)arg);
725 default:
726 return -EINVAL;
727 }
John Calixtocb87ea22011-04-26 18:56:29 -0400728}
729
730#ifdef CONFIG_COMPAT
731static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
732 unsigned int cmd, unsigned long arg)
733{
734 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
735}
736#endif
737
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700738static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500739 .open = mmc_blk_open,
740 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800741 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400743 .ioctl = mmc_blk_ioctl,
744#ifdef CONFIG_COMPAT
745 .compat_ioctl = mmc_blk_compat_ioctl,
746#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747};
748
Andrei Warkentin371a6892011-04-11 18:10:25 -0500749static inline int mmc_blk_part_switch(struct mmc_card *card,
750 struct mmc_blk_data *md)
751{
752 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200753 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300754
Andrei Warkentin371a6892011-04-11 18:10:25 -0500755 if (main_md->part_curr == md->part_type)
756 return 0;
757
758 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300759 u8 part_config = card->ext_csd.part_config;
760
Adrian Hunter57da0c02016-05-04 14:38:13 +0300761 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
762 mmc_retune_pause(card->host);
763
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300764 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
765 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500766
767 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300768 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500769 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300770 if (ret) {
771 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
772 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500773 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300774 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300775
776 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300777
778 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
779 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300780 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500781
782 main_md->part_curr = md->part_type;
783 return 0;
784}
785
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700786static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
787{
788 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100789 u32 result;
790 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700791
Venkatraman Sad5fd972011-08-25 00:30:50 +0530792 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400793 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400794 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700795
796 struct scatterlist sg;
797
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700798 cmd.opcode = MMC_APP_CMD;
799 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700800 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700801
802 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700803 if (err)
804 return (u32)-1;
805 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700806 return (u32)-1;
807
808 memset(&cmd, 0, sizeof(struct mmc_command));
809
810 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
811 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700812 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700814 data.blksz = 4;
815 data.blocks = 1;
816 data.flags = MMC_DATA_READ;
817 data.sg = &sg;
818 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530819 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700820
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700821 mrq.cmd = &cmd;
822 mrq.data = &data;
823
Ben Dooks051913d2009-06-08 23:33:57 +0100824 blocks = kmalloc(4, GFP_KERNEL);
825 if (!blocks)
826 return (u32)-1;
827
828 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700829
830 mmc_wait_for_req(card->host, &mrq);
831
Ben Dooks051913d2009-06-08 23:33:57 +0100832 result = ntohl(*blocks);
833 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700834
Ben Dooks051913d2009-06-08 23:33:57 +0100835 if (cmd.error || data.error)
836 result = (u32)-1;
837
838 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700839}
840
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100841static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300842{
Chris Ball1278dba2011-04-13 23:40:30 -0400843 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300844 int err;
845
Adrian Hunter504f1912008-10-16 12:55:25 +0300846 cmd.opcode = MMC_SEND_STATUS;
847 if (!mmc_host_is_spi(card->host))
848 cmd.arg = card->rca << 16;
849 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100850 err = mmc_wait_for_cmd(card->host, &cmd, retries);
851 if (err == 0)
852 *status = cmd.resp[0];
853 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300854}
855
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100856static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +0100857 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100858{
859 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
860 int err = 0;
861 u32 status;
862
863 do {
864 err = get_card_status(card, &status, 5);
865 if (err) {
866 pr_err("%s: error %d requesting status\n",
867 req->rq_disk->disk_name, err);
868 return err;
869 }
870
871 if (status & R1_ERROR) {
872 pr_err("%s: %s: error sending status cmd, status %#x\n",
873 req->rq_disk->disk_name, __func__, status);
874 *gen_err = 1;
875 }
876
Ulf Hansson95a91292014-01-29 13:11:27 +0100877 /* We may rely on the host hw to handle busy detection.*/
878 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
879 hw_busy_detect)
880 break;
881
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100882 /*
883 * Timeout if the device never becomes ready for data and never
884 * leaves the program state.
885 */
886 if (time_after(jiffies, timeout)) {
887 pr_err("%s: Card stuck in programming state! %s %s\n",
888 mmc_hostname(card->host),
889 req->rq_disk->disk_name, __func__);
890 return -ETIMEDOUT;
891 }
892
893 /*
894 * Some cards mishandle the status bits,
895 * so make sure to check both the busy
896 * indication and the card state.
897 */
898 } while (!(status & R1_READY_FOR_DATA) ||
899 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
900
901 return err;
902}
903
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100904static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
905 struct request *req, int *gen_err, u32 *stop_status)
906{
907 struct mmc_host *host = card->host;
908 struct mmc_command cmd = {0};
909 int err;
910 bool use_r1b_resp = rq_data_dir(req) == WRITE;
911
912 /*
913 * Normally we use R1B responses for WRITE, but in cases where the host
914 * has specified a max_busy_timeout we need to validate it. A failure
915 * means we need to prevent the host from doing hw busy detection, which
916 * is done by converting to a R1 response instead.
917 */
918 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
919 use_r1b_resp = false;
920
921 cmd.opcode = MMC_STOP_TRANSMISSION;
922 if (use_r1b_resp) {
923 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
924 cmd.busy_timeout = timeout_ms;
925 } else {
926 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
927 }
928
929 err = mmc_wait_for_cmd(host, &cmd, 5);
930 if (err)
931 return err;
932
933 *stop_status = cmd.resp[0];
934
935 /* No need to check card status in case of READ. */
936 if (rq_data_dir(req) == READ)
937 return 0;
938
939 if (!mmc_host_is_spi(host) &&
940 (*stop_status & R1_ERROR)) {
941 pr_err("%s: %s: general error sending stop command, resp %#x\n",
942 req->rq_disk->disk_name, __func__, *stop_status);
943 *gen_err = 1;
944 }
945
946 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
947}
948
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530949#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100950#define ERR_RETRY 2
951#define ERR_ABORT 1
952#define ERR_CONTINUE 0
953
954static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
955 bool status_valid, u32 status)
956{
957 switch (error) {
958 case -EILSEQ:
959 /* response crc error, retry the r/w cmd */
960 pr_err("%s: %s sending %s command, card status %#x\n",
961 req->rq_disk->disk_name, "response CRC error",
962 name, status);
963 return ERR_RETRY;
964
965 case -ETIMEDOUT:
966 pr_err("%s: %s sending %s command, card status %#x\n",
967 req->rq_disk->disk_name, "timed out", name, status);
968
969 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530970 if (!status_valid) {
971 pr_err("%s: status not valid, retrying timeout\n",
972 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100973 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530974 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100975
976 /*
977 * If it was a r/w cmd crc error, or illegal command
978 * (eg, issued in wrong state) then retry - we should
979 * have corrected the state problem above.
980 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530981 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
982 pr_err("%s: command error, retrying timeout\n",
983 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100984 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530985 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100986
987 /* Otherwise abort the command */
988 return ERR_ABORT;
989
990 default:
991 /* We don't understand the error code the driver gave us */
992 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
993 req->rq_disk->disk_name, error, status);
994 return ERR_ABORT;
995 }
996}
997
998/*
999 * Initial r/w and stop cmd error recovery.
1000 * We don't know whether the card received the r/w cmd or not, so try to
1001 * restore things back to a sane state. Essentially, we do this as follows:
1002 * - Obtain card status. If the first attempt to obtain card status fails,
1003 * the status word will reflect the failed status cmd, not the failed
1004 * r/w cmd. If we fail to obtain card status, it suggests we can no
1005 * longer communicate with the card.
1006 * - Check the card state. If the card received the cmd but there was a
1007 * transient problem with the response, it might still be in a data transfer
1008 * mode. Try to send it a stop command. If this fails, we can't recover.
1009 * - If the r/w cmd failed due to a response CRC error, it was probably
1010 * transient, so retry the cmd.
1011 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1012 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1013 * illegal cmd, retry.
1014 * Otherwise we don't understand what happened, so abort.
1015 */
1016static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001017 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001018{
1019 bool prev_cmd_status_valid = true;
1020 u32 status, stop_status = 0;
1021 int err, retry;
1022
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301023 if (mmc_card_removed(card))
1024 return ERR_NOMEDIUM;
1025
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001026 /*
1027 * Try to get card status which indicates both the card state
1028 * and why there was no response. If the first attempt fails,
1029 * we can't be sure the returned status is for the r/w command.
1030 */
1031 for (retry = 2; retry >= 0; retry--) {
1032 err = get_card_status(card, &status, 0);
1033 if (!err)
1034 break;
1035
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001036 /* Re-tune if needed */
1037 mmc_retune_recheck(card->host);
1038
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001039 prev_cmd_status_valid = false;
1040 pr_err("%s: error %d sending status command, %sing\n",
1041 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1042 }
1043
1044 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301045 if (err) {
1046 /* Check if the card is removed */
1047 if (mmc_detect_card_removed(card->host))
1048 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001049 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301050 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001051
Adrian Hunter67716322011-08-29 16:42:15 +03001052 /* Flag ECC errors */
1053 if ((status & R1_CARD_ECC_FAILED) ||
1054 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1055 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1056 *ecc_err = 1;
1057
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001058 /* Flag General errors */
1059 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1060 if ((status & R1_ERROR) ||
1061 (brq->stop.resp[0] & R1_ERROR)) {
1062 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1063 req->rq_disk->disk_name, __func__,
1064 brq->stop.resp[0], status);
1065 *gen_err = 1;
1066 }
1067
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001068 /*
1069 * Check the current card state. If it is in some data transfer
1070 * mode, tell it to stop (and hopefully transition back to TRAN.)
1071 */
1072 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1073 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001074 err = send_stop(card,
1075 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1076 req, gen_err, &stop_status);
1077 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001078 pr_err("%s: error %d sending stop command\n",
1079 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001080 /*
1081 * If the stop cmd also timed out, the card is probably
1082 * not present, so abort. Other errors are bad news too.
1083 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001084 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001085 }
1086
Adrian Hunter67716322011-08-29 16:42:15 +03001087 if (stop_status & R1_CARD_ECC_FAILED)
1088 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001089 }
1090
1091 /* Check for set block count errors */
1092 if (brq->sbc.error)
1093 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1094 prev_cmd_status_valid, status);
1095
1096 /* Check for r/w command errors */
1097 if (brq->cmd.error)
1098 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1099 prev_cmd_status_valid, status);
1100
Adrian Hunter67716322011-08-29 16:42:15 +03001101 /* Data errors */
1102 if (!brq->stop.error)
1103 return ERR_CONTINUE;
1104
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001105 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001106 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 +01001107 req->rq_disk->disk_name, brq->stop.error,
1108 brq->cmd.resp[0], status);
1109
1110 /*
1111 * Subsitute in our own stop status as this will give the error
1112 * state which happened during the execution of the r/w command.
1113 */
1114 if (stop_status) {
1115 brq->stop.resp[0] = stop_status;
1116 brq->stop.error = 0;
1117 }
1118 return ERR_CONTINUE;
1119}
1120
Adrian Hunter67716322011-08-29 16:42:15 +03001121static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1122 int type)
1123{
1124 int err;
1125
1126 if (md->reset_done & type)
1127 return -EEXIST;
1128
1129 md->reset_done |= type;
1130 err = mmc_hw_reset(host);
1131 /* Ensure we switch back to the correct partition */
1132 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001133 struct mmc_blk_data *main_md =
1134 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001135 int part_err;
1136
1137 main_md->part_curr = main_md->part_type;
1138 part_err = mmc_blk_part_switch(host->card, md);
1139 if (part_err) {
1140 /*
1141 * We have failed to get back into the correct
1142 * partition, so we need to abort the whole request.
1143 */
1144 return -ENODEV;
1145 }
1146 }
1147 return err;
1148}
1149
1150static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1151{
1152 md->reset_done &= ~type;
1153}
1154
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001155int mmc_access_rpmb(struct mmc_queue *mq)
1156{
1157 struct mmc_blk_data *md = mq->data;
1158 /*
1159 * If this is a RPMB partition access, return ture
1160 */
1161 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1162 return true;
1163
1164 return false;
1165}
1166
Adrian Hunterbd788c92010-08-11 14:17:47 -07001167static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1168{
1169 struct mmc_blk_data *md = mq->data;
1170 struct mmc_card *card = md->queue.card;
1171 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001172 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001173
Adrian Hunterbd788c92010-08-11 14:17:47 -07001174 if (!mmc_can_erase(card)) {
1175 err = -EOPNOTSUPP;
1176 goto out;
1177 }
1178
1179 from = blk_rq_pos(req);
1180 nr = blk_rq_sectors(req);
1181
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001182 if (mmc_can_discard(card))
1183 arg = MMC_DISCARD_ARG;
1184 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001185 arg = MMC_TRIM_ARG;
1186 else
1187 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001188retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001189 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1190 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1191 INAND_CMD38_ARG_EXT_CSD,
1192 arg == MMC_TRIM_ARG ?
1193 INAND_CMD38_ARG_TRIM :
1194 INAND_CMD38_ARG_ERASE,
1195 0);
1196 if (err)
1197 goto out;
1198 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001199 err = mmc_erase(card, from, nr, arg);
1200out:
Adrian Hunter67716322011-08-29 16:42:15 +03001201 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1202 goto retry;
1203 if (!err)
1204 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301205 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001206
Adrian Hunterbd788c92010-08-11 14:17:47 -07001207 return err ? 0 : 1;
1208}
1209
Adrian Hunter49804542010-08-11 14:17:50 -07001210static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1211 struct request *req)
1212{
1213 struct mmc_blk_data *md = mq->data;
1214 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001215 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001216 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001217
Maya Erez775a9362013-04-18 15:41:55 +03001218 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001219 err = -EOPNOTSUPP;
1220 goto out;
1221 }
1222
1223 from = blk_rq_pos(req);
1224 nr = blk_rq_sectors(req);
1225
Maya Erez775a9362013-04-18 15:41:55 +03001226 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1227 arg = MMC_SECURE_TRIM1_ARG;
1228 else
1229 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001230
Adrian Hunter67716322011-08-29 16:42:15 +03001231retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001232 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1233 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1234 INAND_CMD38_ARG_EXT_CSD,
1235 arg == MMC_SECURE_TRIM1_ARG ?
1236 INAND_CMD38_ARG_SECTRIM1 :
1237 INAND_CMD38_ARG_SECERASE,
1238 0);
1239 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001240 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001241 }
Adrian Hunter28302812012-04-05 14:45:48 +03001242
Adrian Hunter49804542010-08-11 14:17:50 -07001243 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001244 if (err == -EIO)
1245 goto out_retry;
1246 if (err)
1247 goto out;
1248
1249 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001250 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1251 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1252 INAND_CMD38_ARG_EXT_CSD,
1253 INAND_CMD38_ARG_SECTRIM2,
1254 0);
1255 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001256 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001257 }
Adrian Hunter28302812012-04-05 14:45:48 +03001258
Adrian Hunter49804542010-08-11 14:17:50 -07001259 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001260 if (err == -EIO)
1261 goto out_retry;
1262 if (err)
1263 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001264 }
Adrian Hunter28302812012-04-05 14:45:48 +03001265
Adrian Hunter28302812012-04-05 14:45:48 +03001266out_retry:
1267 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001268 goto retry;
1269 if (!err)
1270 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001271out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301272 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001273
Adrian Hunter49804542010-08-11 14:17:50 -07001274 return err ? 0 : 1;
1275}
1276
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001277static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1278{
1279 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001280 struct mmc_card *card = md->queue.card;
1281 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001282
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001283 ret = mmc_flush_cache(card);
1284 if (ret)
1285 ret = -EIO;
1286
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301287 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001288
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001289 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001290}
1291
1292/*
1293 * Reformat current write as a reliable write, supporting
1294 * both legacy and the enhanced reliable write MMC cards.
1295 * In each transfer we'll handle only as much as a single
1296 * reliable write can handle, thus finish the request in
1297 * partial completions.
1298 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001299static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1300 struct mmc_card *card,
1301 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001302{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001303 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1304 /* Legacy mode imposes restrictions on transfers. */
1305 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1306 brq->data.blocks = 1;
1307
1308 if (brq->data.blocks > card->ext_csd.rel_sectors)
1309 brq->data.blocks = card->ext_csd.rel_sectors;
1310 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1311 brq->data.blocks = 1;
1312 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001313}
1314
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001315#define CMD_ERRORS \
1316 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1317 R1_ADDRESS_ERROR | /* Misaligned address */ \
1318 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1319 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1320 R1_CC_ERROR | /* Card controller error */ \
1321 R1_ERROR) /* General/unknown error */
1322
Per Forlinee8a43a2011-07-01 18:55:33 +02001323static int mmc_blk_err_check(struct mmc_card *card,
1324 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001325{
Per Forlinee8a43a2011-07-01 18:55:33 +02001326 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1327 mmc_active);
1328 struct mmc_blk_request *brq = &mq_mrq->brq;
1329 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001330 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001331 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001332
1333 /*
1334 * sbc.error indicates a problem with the set block count
1335 * command. No data will have been transferred.
1336 *
1337 * cmd.error indicates a problem with the r/w command. No
1338 * data will have been transferred.
1339 *
1340 * stop.error indicates a problem with the stop command. Data
1341 * may have been transferred, or may still be transferring.
1342 */
Adrian Hunter67716322011-08-29 16:42:15 +03001343 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1344 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001345 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001346 case ERR_RETRY:
1347 return MMC_BLK_RETRY;
1348 case ERR_ABORT:
1349 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301350 case ERR_NOMEDIUM:
1351 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001352 case ERR_CONTINUE:
1353 break;
1354 }
1355 }
1356
1357 /*
1358 * Check for errors relating to the execution of the
1359 * initial command - such as address errors. No data
1360 * has been transferred.
1361 */
1362 if (brq->cmd.resp[0] & CMD_ERRORS) {
1363 pr_err("%s: r/w command failed, status = %#x\n",
1364 req->rq_disk->disk_name, brq->cmd.resp[0]);
1365 return MMC_BLK_ABORT;
1366 }
1367
1368 /*
1369 * Everything else is either success, or a data error of some
1370 * kind. If it was a write, we may have transitioned to
1371 * program mode, which we have to wait for it to complete.
1372 */
1373 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001374 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001375
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001376 /* Check stop command response */
1377 if (brq->stop.resp[0] & R1_ERROR) {
1378 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1379 req->rq_disk->disk_name, __func__,
1380 brq->stop.resp[0]);
1381 gen_err = 1;
1382 }
1383
Ulf Hansson95a91292014-01-29 13:11:27 +01001384 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1385 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001386 if (err)
1387 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001388 }
1389
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001390 /* if general error occurs, retry the write operation. */
1391 if (gen_err) {
1392 pr_warn("%s: retrying write for general error\n",
1393 req->rq_disk->disk_name);
1394 return MMC_BLK_RETRY;
1395 }
1396
Per Forlind78d4a82011-07-01 18:55:30 +02001397 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001398 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001399 pr_debug("%s: retrying because a re-tune was needed\n",
1400 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001401 brq->retune_retry_done = 1;
1402 return MMC_BLK_RETRY;
1403 }
Per Forlind78d4a82011-07-01 18:55:30 +02001404 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1405 req->rq_disk->disk_name, brq->data.error,
1406 (unsigned)blk_rq_pos(req),
1407 (unsigned)blk_rq_sectors(req),
1408 brq->cmd.resp[0], brq->stop.resp[0]);
1409
1410 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001411 if (ecc_err)
1412 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001413 return MMC_BLK_DATA_ERR;
1414 } else {
1415 return MMC_BLK_CMD_ERR;
1416 }
1417 }
1418
Adrian Hunter67716322011-08-29 16:42:15 +03001419 if (!brq->data.bytes_xfered)
1420 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001421
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001422 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1423 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1424 return MMC_BLK_PARTIAL;
1425 else
1426 return MMC_BLK_SUCCESS;
1427 }
1428
Adrian Hunter67716322011-08-29 16:42:15 +03001429 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1430 return MMC_BLK_PARTIAL;
1431
1432 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001433}
1434
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001435static int mmc_blk_packed_err_check(struct mmc_card *card,
1436 struct mmc_async_req *areq)
1437{
1438 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1439 mmc_active);
1440 struct request *req = mq_rq->req;
1441 struct mmc_packed *packed = mq_rq->packed;
1442 int err, check, status;
1443 u8 *ext_csd;
1444
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001445 packed->retries--;
1446 check = mmc_blk_err_check(card, areq);
1447 err = get_card_status(card, &status, 0);
1448 if (err) {
1449 pr_err("%s: error %d sending status command\n",
1450 req->rq_disk->disk_name, err);
1451 return MMC_BLK_ABORT;
1452 }
1453
1454 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001455 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001456 if (err) {
1457 pr_err("%s: error %d sending ext_csd\n",
1458 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001459 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001460 }
1461
1462 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1463 EXT_CSD_PACKED_FAILURE) &&
1464 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1465 EXT_CSD_PACKED_GENERIC_ERROR)) {
1466 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1467 EXT_CSD_PACKED_INDEXED_ERROR) {
1468 packed->idx_failure =
1469 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1470 check = MMC_BLK_PARTIAL;
1471 }
1472 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1473 "failure index: %d\n",
1474 req->rq_disk->disk_name, packed->nr_entries,
1475 packed->blocks, packed->idx_failure);
1476 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001477 kfree(ext_csd);
1478 }
1479
1480 return check;
1481}
1482
Per Forlin54d49d72011-07-01 18:55:29 +02001483static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1484 struct mmc_card *card,
1485 int disable_multi,
1486 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Per Forlin54d49d72011-07-01 18:55:29 +02001488 u32 readcmd, writecmd;
1489 struct mmc_blk_request *brq = &mqrq->brq;
1490 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301492 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001494 /*
1495 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001496 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001497 */
Luca Porziod3df0462015-11-06 15:12:26 +00001498 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001499 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001500 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001501
Per Forlin54d49d72011-07-01 18:55:29 +02001502 memset(brq, 0, sizeof(struct mmc_blk_request));
1503 brq->mrq.cmd = &brq->cmd;
1504 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Per Forlin54d49d72011-07-01 18:55:29 +02001506 brq->cmd.arg = blk_rq_pos(req);
1507 if (!mmc_card_blockaddr(card))
1508 brq->cmd.arg <<= 9;
1509 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1510 brq->data.blksz = 512;
1511 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1512 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001513 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Per Forlin54d49d72011-07-01 18:55:29 +02001515 /*
1516 * The block layer doesn't support all sector count
1517 * restrictions, so we need to be prepared for too big
1518 * requests.
1519 */
1520 if (brq->data.blocks > card->host->max_blk_count)
1521 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001523 if (brq->data.blocks > 1) {
1524 /*
1525 * After a read error, we redo the request one sector
1526 * at a time in order to accurately determine which
1527 * sectors can be read successfully.
1528 */
1529 if (disable_multi)
1530 brq->data.blocks = 1;
1531
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001532 /*
1533 * Some controllers have HW issues while operating
1534 * in multiple I/O mode
1535 */
1536 if (card->host->ops->multi_io_quirk)
1537 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1538 (rq_data_dir(req) == READ) ?
1539 MMC_DATA_READ : MMC_DATA_WRITE,
1540 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001541 }
Per Forlin54d49d72011-07-01 18:55:29 +02001542
1543 if (brq->data.blocks > 1 || do_rel_wr) {
1544 /* SPI multiblock writes terminate using a special
1545 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001546 */
Per Forlin54d49d72011-07-01 18:55:29 +02001547 if (!mmc_host_is_spi(card->host) ||
1548 rq_data_dir(req) == READ)
1549 brq->mrq.stop = &brq->stop;
1550 readcmd = MMC_READ_MULTIPLE_BLOCK;
1551 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1552 } else {
1553 brq->mrq.stop = NULL;
1554 readcmd = MMC_READ_SINGLE_BLOCK;
1555 writecmd = MMC_WRITE_BLOCK;
1556 }
1557 if (rq_data_dir(req) == READ) {
1558 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001559 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001560 if (brq->mrq.stop)
1561 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1562 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001563 } else {
1564 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001565 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001566 if (brq->mrq.stop)
1567 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1568 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001569 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001570
Per Forlin54d49d72011-07-01 18:55:29 +02001571 if (do_rel_wr)
1572 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001573
Per Forlin54d49d72011-07-01 18:55:29 +02001574 /*
Saugata Das42659002011-12-21 13:09:17 +05301575 * Data tag is used only during writing meta data to speed
1576 * up write and any subsequent read of this meta data
1577 */
1578 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1579 (req->cmd_flags & REQ_META) &&
1580 (rq_data_dir(req) == WRITE) &&
1581 ((brq->data.blocks * brq->data.blksz) >=
1582 card->ext_csd.data_tag_unit_size);
1583
1584 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001585 * Pre-defined multi-block transfers are preferable to
1586 * open ended-ones (and necessary for reliable writes).
1587 * However, it is not sufficient to just send CMD23,
1588 * and avoid the final CMD12, as on an error condition
1589 * CMD12 (stop) needs to be sent anyway. This, coupled
1590 * with Auto-CMD23 enhancements provided by some
1591 * hosts, means that the complexity of dealing
1592 * with this is best left to the host. If CMD23 is
1593 * supported by card and host, we'll fill sbc in and let
1594 * the host deal with handling it correctly. This means
1595 * that for hosts that don't expose MMC_CAP_CMD23, no
1596 * change of behavior will be observed.
1597 *
1598 * N.B: Some MMC cards experience perf degradation.
1599 * We'll avoid using CMD23-bounded multiblock writes for
1600 * these, while retaining features like reliable writes.
1601 */
Saugata Das42659002011-12-21 13:09:17 +05301602 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1603 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1604 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001605 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1606 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301607 (do_rel_wr ? (1 << 31) : 0) |
1608 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001609 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1610 brq->mrq.sbc = &brq->sbc;
1611 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001612
Per Forlin54d49d72011-07-01 18:55:29 +02001613 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001614
Per Forlin54d49d72011-07-01 18:55:29 +02001615 brq->data.sg = mqrq->sg;
1616 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001617
Per Forlin54d49d72011-07-01 18:55:29 +02001618 /*
1619 * Adjust the sg list so it is the same size as the
1620 * request.
1621 */
1622 if (brq->data.blocks != blk_rq_sectors(req)) {
1623 int i, data_size = brq->data.blocks << 9;
1624 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001625
Per Forlin54d49d72011-07-01 18:55:29 +02001626 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1627 data_size -= sg->length;
1628 if (data_size <= 0) {
1629 sg->length += data_size;
1630 i++;
1631 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001632 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001633 }
Per Forlin54d49d72011-07-01 18:55:29 +02001634 brq->data.sg_len = i;
1635 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001636
Per Forlinee8a43a2011-07-01 18:55:33 +02001637 mqrq->mmc_active.mrq = &brq->mrq;
1638 mqrq->mmc_active.err_check = mmc_blk_err_check;
1639
Per Forlin54d49d72011-07-01 18:55:29 +02001640 mmc_queue_bounce_pre(mqrq);
1641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001643static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1644 struct mmc_card *card)
1645{
1646 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1647 unsigned int max_seg_sz = queue_max_segment_size(q);
1648 unsigned int len, nr_segs = 0;
1649
1650 do {
1651 len = min(hdr_sz, max_seg_sz);
1652 hdr_sz -= len;
1653 nr_segs++;
1654 } while (hdr_sz);
1655
1656 return nr_segs;
1657}
1658
1659static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1660{
1661 struct request_queue *q = mq->queue;
1662 struct mmc_card *card = mq->card;
1663 struct request *cur = req, *next = NULL;
1664 struct mmc_blk_data *md = mq->data;
1665 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1666 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1667 unsigned int req_sectors = 0, phys_segments = 0;
1668 unsigned int max_blk_count, max_phys_segs;
1669 bool put_back = true;
1670 u8 max_packed_rw = 0;
1671 u8 reqs = 0;
1672
Shawn Lin96e52da2016-08-26 08:49:55 +08001673 /*
1674 * We don't need to check packed for any further
1675 * operation of packed stuff as we set MMC_PACKED_NONE
1676 * and return zero for reqs if geting null packed. Also
1677 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
1678 * it again when removing blk req.
1679 */
1680 if (!mqrq->packed) {
1681 md->flags &= (~MMC_BLK_PACKED_CMD);
1682 goto no_packed;
1683 }
1684
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001685 if (!(md->flags & MMC_BLK_PACKED_CMD))
1686 goto no_packed;
1687
1688 if ((rq_data_dir(cur) == WRITE) &&
1689 mmc_host_packed_wr(card->host))
1690 max_packed_rw = card->ext_csd.max_packed_writes;
1691
1692 if (max_packed_rw == 0)
1693 goto no_packed;
1694
1695 if (mmc_req_rel_wr(cur) &&
1696 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1697 goto no_packed;
1698
1699 if (mmc_large_sector(card) &&
1700 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1701 goto no_packed;
1702
1703 mmc_blk_clear_packed(mqrq);
1704
1705 max_blk_count = min(card->host->max_blk_count,
1706 card->host->max_req_size >> 9);
1707 if (unlikely(max_blk_count > 0xffff))
1708 max_blk_count = 0xffff;
1709
1710 max_phys_segs = queue_max_segments(q);
1711 req_sectors += blk_rq_sectors(cur);
1712 phys_segments += cur->nr_phys_segments;
1713
1714 if (rq_data_dir(cur) == WRITE) {
1715 req_sectors += mmc_large_sector(card) ? 8 : 1;
1716 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1717 }
1718
1719 do {
1720 if (reqs >= max_packed_rw - 1) {
1721 put_back = false;
1722 break;
1723 }
1724
1725 spin_lock_irq(q->queue_lock);
1726 next = blk_fetch_request(q);
1727 spin_unlock_irq(q->queue_lock);
1728 if (!next) {
1729 put_back = false;
1730 break;
1731 }
1732
1733 if (mmc_large_sector(card) &&
1734 !IS_ALIGNED(blk_rq_sectors(next), 8))
1735 break;
1736
Mike Christie3a5e02c2016-06-05 14:32:23 -05001737 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03001738 req_op(next) == REQ_OP_SECURE_ERASE ||
Mike Christie3a5e02c2016-06-05 14:32:23 -05001739 req_op(next) == REQ_OP_FLUSH)
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001740 break;
1741
1742 if (rq_data_dir(cur) != rq_data_dir(next))
1743 break;
1744
1745 if (mmc_req_rel_wr(next) &&
1746 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1747 break;
1748
1749 req_sectors += blk_rq_sectors(next);
1750 if (req_sectors > max_blk_count)
1751 break;
1752
1753 phys_segments += next->nr_phys_segments;
1754 if (phys_segments > max_phys_segs)
1755 break;
1756
1757 list_add_tail(&next->queuelist, &mqrq->packed->list);
1758 cur = next;
1759 reqs++;
1760 } while (1);
1761
1762 if (put_back) {
1763 spin_lock_irq(q->queue_lock);
1764 blk_requeue_request(q, next);
1765 spin_unlock_irq(q->queue_lock);
1766 }
1767
1768 if (reqs > 0) {
1769 list_add(&req->queuelist, &mqrq->packed->list);
1770 mqrq->packed->nr_entries = ++reqs;
1771 mqrq->packed->retries = reqs;
1772 return reqs;
1773 }
1774
1775no_packed:
1776 mqrq->cmd_type = MMC_PACKED_NONE;
1777 return 0;
1778}
1779
1780static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1781 struct mmc_card *card,
1782 struct mmc_queue *mq)
1783{
1784 struct mmc_blk_request *brq = &mqrq->brq;
1785 struct request *req = mqrq->req;
1786 struct request *prq;
1787 struct mmc_blk_data *md = mq->data;
1788 struct mmc_packed *packed = mqrq->packed;
1789 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02001790 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001791 u8 hdr_blocks;
1792 u8 i = 1;
1793
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001794 mqrq->cmd_type = MMC_PACKED_WRITE;
1795 packed->blocks = 0;
1796 packed->idx_failure = MMC_PACKED_NR_IDX;
1797
1798 packed_cmd_hdr = packed->cmd_hdr;
1799 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001800 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
1801 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001802 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1803
1804 /*
1805 * Argument for each entry of packed group
1806 */
1807 list_for_each_entry(prq, &packed->list, queuelist) {
1808 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1809 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1810 (prq->cmd_flags & REQ_META) &&
1811 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03001812 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001813 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001814 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001815 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1816 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001817 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001818 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001819 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001820 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00001821 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001822 packed->blocks += blk_rq_sectors(prq);
1823 i++;
1824 }
1825
1826 memset(brq, 0, sizeof(struct mmc_blk_request));
1827 brq->mrq.cmd = &brq->cmd;
1828 brq->mrq.data = &brq->data;
1829 brq->mrq.sbc = &brq->sbc;
1830 brq->mrq.stop = &brq->stop;
1831
1832 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1833 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1834 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1835
1836 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1837 brq->cmd.arg = blk_rq_pos(req);
1838 if (!mmc_card_blockaddr(card))
1839 brq->cmd.arg <<= 9;
1840 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1841
1842 brq->data.blksz = 512;
1843 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001844 brq->data.flags = MMC_DATA_WRITE;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001845
1846 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1847 brq->stop.arg = 0;
1848 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1849
1850 mmc_set_data_timeout(&brq->data, card);
1851
1852 brq->data.sg = mqrq->sg;
1853 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1854
1855 mqrq->mmc_active.mrq = &brq->mrq;
1856 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1857
1858 mmc_queue_bounce_pre(mqrq);
1859}
1860
Adrian Hunter67716322011-08-29 16:42:15 +03001861static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1862 struct mmc_blk_request *brq, struct request *req,
1863 int ret)
1864{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001865 struct mmc_queue_req *mq_rq;
1866 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1867
Adrian Hunter67716322011-08-29 16:42:15 +03001868 /*
1869 * If this is an SD card and we're writing, we can first
1870 * mark the known good sectors as ok.
1871 *
1872 * If the card is not SD, we can still ok written sectors
1873 * as reported by the controller (which might be less than
1874 * the real number of written sectors, but never more).
1875 */
1876 if (mmc_card_sd(card)) {
1877 u32 blocks;
1878
1879 blocks = mmc_sd_num_wr_blocks(card);
1880 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301881 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001882 }
1883 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001884 if (!mmc_packed_cmd(mq_rq->cmd_type))
1885 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001886 }
1887 return ret;
1888}
1889
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001890static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1891{
1892 struct request *prq;
1893 struct mmc_packed *packed = mq_rq->packed;
1894 int idx = packed->idx_failure, i = 0;
1895 int ret = 0;
1896
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001897 while (!list_empty(&packed->list)) {
1898 prq = list_entry_rq(packed->list.next);
1899 if (idx == i) {
1900 /* retry from error index */
1901 packed->nr_entries -= idx;
1902 mq_rq->req = prq;
1903 ret = 1;
1904
1905 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1906 list_del_init(&prq->queuelist);
1907 mmc_blk_clear_packed(mq_rq);
1908 }
1909 return ret;
1910 }
1911 list_del_init(&prq->queuelist);
1912 blk_end_request(prq, 0, blk_rq_bytes(prq));
1913 i++;
1914 }
1915
1916 mmc_blk_clear_packed(mq_rq);
1917 return ret;
1918}
1919
1920static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1921{
1922 struct request *prq;
1923 struct mmc_packed *packed = mq_rq->packed;
1924
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001925 while (!list_empty(&packed->list)) {
1926 prq = list_entry_rq(packed->list.next);
1927 list_del_init(&prq->queuelist);
1928 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1929 }
1930
1931 mmc_blk_clear_packed(mq_rq);
1932}
1933
1934static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1935 struct mmc_queue_req *mq_rq)
1936{
1937 struct request *prq;
1938 struct request_queue *q = mq->queue;
1939 struct mmc_packed *packed = mq_rq->packed;
1940
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001941 while (!list_empty(&packed->list)) {
1942 prq = list_entry_rq(packed->list.prev);
1943 if (prq->queuelist.prev != &packed->list) {
1944 list_del_init(&prq->queuelist);
1945 spin_lock_irq(q->queue_lock);
1946 blk_requeue_request(mq->queue, prq);
1947 spin_unlock_irq(q->queue_lock);
1948 } else {
1949 list_del_init(&prq->queuelist);
1950 }
1951 }
1952
1953 mmc_blk_clear_packed(mq_rq);
1954}
1955
Per Forlinee8a43a2011-07-01 18:55:33 +02001956static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001957{
1958 struct mmc_blk_data *md = mq->data;
1959 struct mmc_card *card = md->queue.card;
1960 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001961 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001962 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001963 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301964 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001965 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001966 const u8 packed_nr = 2;
1967 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001968
1969 if (!rqc && !mq->mqrq_prev->req)
1970 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001971
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001972 if (rqc)
1973 reqs = mmc_blk_prep_packed_list(mq, rqc);
1974
Per Forlin54d49d72011-07-01 18:55:29 +02001975 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001976 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301977 /*
1978 * When 4KB native sector is enabled, only 8 blocks
1979 * multiple read or write is allowed
1980 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001981 if (mmc_large_sector(card) &&
1982 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301983 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1984 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001985 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301986 goto cmd_abort;
1987 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001988
1989 if (reqs >= packed_nr)
1990 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1991 card, mq);
1992 else
1993 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001994 areq = &mq->mqrq_cur->mmc_active;
1995 } else
1996 areq = NULL;
1997 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001998 if (!areq) {
1999 if (status == MMC_BLK_NEW_REQUEST)
2000 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002001 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002002 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002003
Per Forlinee8a43a2011-07-01 18:55:33 +02002004 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2005 brq = &mq_rq->brq;
2006 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002007 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002008 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002009
Per Forlind78d4a82011-07-01 18:55:30 +02002010 switch (status) {
2011 case MMC_BLK_SUCCESS:
2012 case MMC_BLK_PARTIAL:
2013 /*
2014 * A block was successfully transferred.
2015 */
Adrian Hunter67716322011-08-29 16:42:15 +03002016 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002017
2018 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2019 ret = mmc_blk_end_packed_req(mq_rq);
2020 break;
2021 } else {
2022 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002023 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002024 }
2025
Adrian Hunter67716322011-08-29 16:42:15 +03002026 /*
2027 * If the blk_end_request function returns non-zero even
2028 * though all data has been transferred and no errors
2029 * were returned by the host controller, it's a bug.
2030 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002031 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302032 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002033 __func__, blk_rq_bytes(req),
2034 brq->data.bytes_xfered);
2035 rqc = NULL;
2036 goto cmd_abort;
2037 }
Per Forlind78d4a82011-07-01 18:55:30 +02002038 break;
2039 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002040 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002041 if (mmc_blk_reset(md, card->host, type))
2042 goto cmd_abort;
2043 if (!ret)
2044 goto start_new_req;
2045 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002046 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002047 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002048 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002049 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002050 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002051 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002052 if (!mmc_blk_reset(md, card->host, type))
2053 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002054 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002055 case MMC_BLK_DATA_ERR: {
2056 int err;
2057
2058 err = mmc_blk_reset(md, card->host, type);
2059 if (!err)
2060 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002061 if (err == -ENODEV ||
2062 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03002063 goto cmd_abort;
2064 /* Fall through */
2065 }
2066 case MMC_BLK_ECC_ERR:
2067 if (brq->data.blocks > 1) {
2068 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002069 pr_warn("%s: retrying using single block read\n",
2070 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002071 disable_multi = 1;
2072 break;
2073 }
Per Forlind78d4a82011-07-01 18:55:30 +02002074 /*
2075 * After an error, we redo I/O one sector at a
2076 * time, so we only reach here after trying to
2077 * read a single sector.
2078 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302079 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002080 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002081 if (!ret)
2082 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002083 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302084 case MMC_BLK_NOMEDIUM:
2085 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002086 default:
2087 pr_err("%s: Unhandled return value (%d)",
2088 req->rq_disk->disk_name, status);
2089 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002090 }
2091
Per Forlinee8a43a2011-07-01 18:55:33 +02002092 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002093 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2094 if (!mq_rq->packed->retries)
2095 goto cmd_abort;
2096 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2097 mmc_start_req(card->host,
2098 &mq_rq->mmc_active, NULL);
2099 } else {
2100
2101 /*
2102 * In case of a incomplete request
2103 * prepare it again and resend.
2104 */
2105 mmc_blk_rw_rq_prep(mq_rq, card,
2106 disable_multi, mq);
2107 mmc_start_req(card->host,
2108 &mq_rq->mmc_active, NULL);
2109 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002110 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 } while (ret);
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 1;
2115
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002116 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002117 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2118 mmc_blk_abort_packed_req(mq_rq);
2119 } else {
2120 if (mmc_card_removed(card))
2121 req->cmd_flags |= REQ_QUIET;
2122 while (ret)
2123 ret = blk_end_request(req, -EIO,
2124 blk_rq_cur_bytes(req));
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Per Forlinee8a43a2011-07-01 18:55:33 +02002127 start_new_req:
2128 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002129 if (mmc_card_removed(card)) {
2130 rqc->cmd_flags |= REQ_QUIET;
2131 blk_end_request_all(rqc, -EIO);
2132 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002133 /*
2134 * If current request is packed, it needs to put back.
2135 */
2136 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2137 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2138
Seungwon Jeon7a819022013-01-22 19:48:07 +09002139 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2140 mmc_start_req(card->host,
2141 &mq->mqrq_cur->mmc_active, NULL);
2142 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002143 }
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 0;
2146}
2147
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002148int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002149{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002150 int ret;
2151 struct mmc_blk_data *md = mq->data;
2152 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002153 struct mmc_host *host = card->host;
2154 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002155 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002156
Per Forlinee8a43a2011-07-01 18:55:33 +02002157 if (req && !mq->mqrq_prev->req)
2158 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002159 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002160
Andrei Warkentin371a6892011-04-11 18:10:25 -05002161 ret = mmc_blk_part_switch(card, md);
2162 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002163 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302164 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002165 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002166 ret = 0;
2167 goto out;
2168 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002169
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002170 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002171 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002172 /* complete ongoing async transfer before issuing discard */
2173 if (card->host->areq)
2174 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002175 ret = mmc_blk_issue_discard_rq(mq, req);
2176 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2177 /* complete ongoing async transfer before issuing secure erase*/
2178 if (card->host->areq)
2179 mmc_blk_issue_rw_rq(mq, NULL);
2180 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002181 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002182 /* complete ongoing async transfer before issuing flush */
2183 if (card->host->areq)
2184 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002185 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002186 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002187 if (!req && host->areq) {
2188 spin_lock_irqsave(&host->context_info.lock, flags);
2189 host->context_info.is_waiting_last_req = true;
2190 spin_unlock_irqrestore(&host->context_info.lock, flags);
2191 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002192 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002193 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002194
Andrei Warkentin371a6892011-04-11 18:10:25 -05002195out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002196 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002197 /*
2198 * Release host when there are no more requests
2199 * and after special request(discard, flush) is done.
2200 * In case sepecial request, there is no reentry to
2201 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2202 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002203 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002204 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Russell Kinga6f6c962006-01-03 22:38:44 +00002207static inline int mmc_blk_readonly(struct mmc_card *card)
2208{
2209 return mmc_card_readonly(card) ||
2210 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2211}
2212
Andrei Warkentin371a6892011-04-11 18:10:25 -05002213static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2214 struct device *parent,
2215 sector_t size,
2216 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002217 const char *subname,
2218 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct mmc_blk_data *md;
2221 int devidx, ret;
2222
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002223again:
2224 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2225 return ERR_PTR(-ENOMEM);
2226
2227 spin_lock(&mmc_blk_lock);
2228 ret = ida_get_new(&mmc_blk_ida, &devidx);
2229 spin_unlock(&mmc_blk_lock);
2230
2231 if (ret == -EAGAIN)
2232 goto again;
2233 else if (ret)
2234 return ERR_PTR(ret);
2235
2236 if (devidx >= max_devices) {
2237 ret = -ENOSPC;
2238 goto out;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002241 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002242 if (!md) {
2243 ret = -ENOMEM;
2244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002246
Johan Rudholmadd710e2011-12-02 08:51:06 +01002247 md->area_type = area_type;
2248
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002249 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002250 * Set the read-only status based on the supported commands
2251 * and the write protect switch.
2252 */
2253 md->read_only = mmc_blk_readonly(card);
2254
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002255 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002256 if (md->disk == NULL) {
2257 ret = -ENOMEM;
2258 goto err_kfree;
2259 }
2260
2261 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002262 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002263 md->usage = 1;
2264
Adrian Hunterd09408a2011-06-23 13:40:28 +03002265 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002266 if (ret)
2267 goto err_putdisk;
2268
Russell Kinga6f6c962006-01-03 22:38:44 +00002269 md->queue.data = md;
2270
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002271 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002272 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002273 md->disk->fops = &mmc_bdops;
2274 md->disk->private_data = md;
2275 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002276 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002277 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002278 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002279 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002280 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002281
2282 /*
2283 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2284 *
2285 * - be set for removable media with permanent block devices
2286 * - be unset for removable block devices with permanent media
2287 *
2288 * Since MMC block devices clearly fall under the second
2289 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2290 * should use the block device creation/destruction hotplug
2291 * messages to tell when the card is present.
2292 */
2293
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002294 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002295 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002296
Saugata Dasa5075eb2012-05-17 16:32:21 +05302297 if (mmc_card_mmc(card))
2298 blk_queue_logical_block_size(md->queue.queue,
2299 card->ext_csd.data_sector_size);
2300 else
2301 blk_queue_logical_block_size(md->queue.queue, 512);
2302
Andrei Warkentin371a6892011-04-11 18:10:25 -05002303 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002304
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002305 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002306 if ((mmc_card_mmc(card) &&
2307 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002308 (mmc_card_sd(card) &&
2309 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2310 md->flags |= MMC_BLK_CMD23;
2311 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002312
2313 if (mmc_card_mmc(card) &&
2314 md->flags & MMC_BLK_CMD23 &&
2315 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2316 card->ext_csd.rel_sectors)) {
2317 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002318 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002319 }
2320
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002321 if (mmc_card_mmc(card) &&
2322 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2323 (md->flags & MMC_BLK_CMD23) &&
2324 card->ext_csd.packed_event_en) {
2325 if (!mmc_packed_init(&md->queue, card))
2326 md->flags |= MMC_BLK_PACKED_CMD;
2327 }
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002330
2331 err_putdisk:
2332 put_disk(md->disk);
2333 err_kfree:
2334 kfree(md);
2335 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002336 spin_lock(&mmc_blk_lock);
2337 ida_remove(&mmc_blk_ida, devidx);
2338 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002339 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Andrei Warkentin371a6892011-04-11 18:10:25 -05002342static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2343{
2344 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002345
2346 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2347 /*
2348 * The EXT_CSD sector count is in number or 512 byte
2349 * sectors.
2350 */
2351 size = card->ext_csd.sectors;
2352 } else {
2353 /*
2354 * The CSD capacity field is in units of read_blkbits.
2355 * set_capacity takes units of 512 bytes.
2356 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002357 size = (typeof(sector_t))card->csd.capacity
2358 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002359 }
2360
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002361 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002362 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002363}
2364
2365static int mmc_blk_alloc_part(struct mmc_card *card,
2366 struct mmc_blk_data *md,
2367 unsigned int part_type,
2368 sector_t size,
2369 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002370 const char *subname,
2371 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002372{
2373 char cap_str[10];
2374 struct mmc_blk_data *part_md;
2375
2376 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002377 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002378 if (IS_ERR(part_md))
2379 return PTR_ERR(part_md);
2380 part_md->part_type = part_type;
2381 list_add(&part_md->part, &md->part);
2382
James Bottomleyb9f28d82015-03-05 18:47:01 -08002383 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002384 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302385 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002386 part_md->disk->disk_name, mmc_card_id(card),
2387 mmc_card_name(card), part_md->part_type, cap_str);
2388 return 0;
2389}
2390
Namjae Jeone0c368d2011-10-06 23:41:38 +09002391/* MMC Physical partitions consist of two boot partitions and
2392 * up to four general purpose partitions.
2393 * For each partition enabled in EXT_CSD a block device will be allocatedi
2394 * to provide access to the partition.
2395 */
2396
Andrei Warkentin371a6892011-04-11 18:10:25 -05002397static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2398{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002399 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002400
2401 if (!mmc_card_mmc(card))
2402 return 0;
2403
Namjae Jeone0c368d2011-10-06 23:41:38 +09002404 for (idx = 0; idx < card->nr_parts; idx++) {
2405 if (card->part[idx].size) {
2406 ret = mmc_blk_alloc_part(card, md,
2407 card->part[idx].part_cfg,
2408 card->part[idx].size >> 9,
2409 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002410 card->part[idx].name,
2411 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002412 if (ret)
2413 return ret;
2414 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002415 }
2416
2417 return ret;
2418}
2419
Andrei Warkentin371a6892011-04-11 18:10:25 -05002420static void mmc_blk_remove_req(struct mmc_blk_data *md)
2421{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002422 struct mmc_card *card;
2423
Andrei Warkentin371a6892011-04-11 18:10:25 -05002424 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002425 /*
2426 * Flush remaining requests and free queues. It
2427 * is freeing the queue that stops new requests
2428 * from being accepted.
2429 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002430 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002431 mmc_cleanup_queue(&md->queue);
2432 if (md->flags & MMC_BLK_PACKED_CMD)
2433 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002434 if (md->disk->flags & GENHD_FL_UP) {
2435 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002436 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2437 card->ext_csd.boot_ro_lockable)
2438 device_remove_file(disk_to_dev(md->disk),
2439 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002440
Andrei Warkentin371a6892011-04-11 18:10:25 -05002441 del_gendisk(md->disk);
2442 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002443 mmc_blk_put(md);
2444 }
2445}
2446
2447static void mmc_blk_remove_parts(struct mmc_card *card,
2448 struct mmc_blk_data *md)
2449{
2450 struct list_head *pos, *q;
2451 struct mmc_blk_data *part_md;
2452
2453 list_for_each_safe(pos, q, &md->part) {
2454 part_md = list_entry(pos, struct mmc_blk_data, part);
2455 list_del(pos);
2456 mmc_blk_remove_req(part_md);
2457 }
2458}
2459
2460static int mmc_add_disk(struct mmc_blk_data *md)
2461{
2462 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002463 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002464
Dan Williams307d8e62016-06-20 10:40:44 -07002465 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002466 md->force_ro.show = force_ro_show;
2467 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302468 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002469 md->force_ro.attr.name = "force_ro";
2470 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2471 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2472 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002473 goto force_ro_fail;
2474
2475 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2476 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002477 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002478
2479 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2480 mode = S_IRUGO;
2481 else
2482 mode = S_IRUGO | S_IWUSR;
2483
2484 md->power_ro_lock.show = power_ro_lock_show;
2485 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002486 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002487 md->power_ro_lock.attr.mode = mode;
2488 md->power_ro_lock.attr.name =
2489 "ro_lock_until_next_power_on";
2490 ret = device_create_file(disk_to_dev(md->disk),
2491 &md->power_ro_lock);
2492 if (ret)
2493 goto power_ro_lock_fail;
2494 }
2495 return ret;
2496
2497power_ro_lock_fail:
2498 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2499force_ro_fail:
2500 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002501
2502 return ret;
2503}
2504
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002505static const struct mmc_fixup blk_fixups[] =
2506{
Chris Ballc59d4472011-11-11 22:01:43 -05002507 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2508 MMC_QUIRK_INAND_CMD38),
2509 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2510 MMC_QUIRK_INAND_CMD38),
2511 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2512 MMC_QUIRK_INAND_CMD38),
2513 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2514 MMC_QUIRK_INAND_CMD38),
2515 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2516 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002517
2518 /*
2519 * Some MMC cards experience performance degradation with CMD23
2520 * instead of CMD12-bounded multiblock transfers. For now we'll
2521 * black list what's bad...
2522 * - Certain Toshiba cards.
2523 *
2524 * N.B. This doesn't affect SD cards.
2525 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002526 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2527 MMC_QUIRK_BLK_NO_CMD23),
2528 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002530 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002531 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002532 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002533 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002534 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002535 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002536
2537 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002538 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002539 */
Chris Ballc59d4472011-11-11 22:01:43 -05002540 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002541 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002542 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2543 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002544
Ian Chen3550ccd2012-08-29 15:05:36 +09002545 /*
2546 * On these Samsung MoviNAND parts, performing secure erase or
2547 * secure trim can result in unrecoverable corruption due to a
2548 * firmware bug.
2549 */
2550 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2551 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2552 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2553 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2554 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2555 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2556 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2557 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2558 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2559 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2560 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2561 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2562 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2563 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2564 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2565 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2566
Shawn Linb5b4ff02015-08-12 13:08:32 +08002567 /*
2568 * On Some Kingston eMMCs, performing trim can result in
2569 * unrecoverable data conrruption occasionally due to a firmware bug.
2570 */
2571 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2572 MMC_QUIRK_TRIM_BROKEN),
2573 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2574 MMC_QUIRK_TRIM_BROKEN),
2575
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002576 END_FIXUP
2577};
2578
Ulf Hansson96541ba2015-04-14 13:06:12 +02002579static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002581 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002582 char cap_str[10];
2583
Pierre Ossman912490d2005-05-21 10:27:02 +01002584 /*
2585 * Check that the card supports the command class(es) we need.
2586 */
2587 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return -ENODEV;
2589
Lukas Czerner5204d002014-06-18 13:18:07 +02002590 mmc_fixup_device(card, blk_fixups);
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 md = mmc_blk_alloc(card);
2593 if (IS_ERR(md))
2594 return PTR_ERR(md);
2595
James Bottomleyb9f28d82015-03-05 18:47:01 -08002596 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002597 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302598 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002600 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Andrei Warkentin371a6892011-04-11 18:10:25 -05002602 if (mmc_blk_alloc_parts(card, md))
2603 goto out;
2604
Ulf Hansson96541ba2015-04-14 13:06:12 +02002605 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002606
Andrei Warkentin371a6892011-04-11 18:10:25 -05002607 if (mmc_add_disk(md))
2608 goto out;
2609
2610 list_for_each_entry(part_md, &md->part, part) {
2611 if (mmc_add_disk(part_md))
2612 goto out;
2613 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002614
2615 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2616 pm_runtime_use_autosuspend(&card->dev);
2617
2618 /*
2619 * Don't enable runtime PM for SD-combo cards here. Leave that
2620 * decision to be taken during the SDIO init sequence instead.
2621 */
2622 if (card->type != MMC_TYPE_SD_COMBO) {
2623 pm_runtime_set_active(&card->dev);
2624 pm_runtime_enable(&card->dev);
2625 }
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return 0;
2628
2629 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002630 mmc_blk_remove_parts(card, md);
2631 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Ulf Hansson96541ba2015-04-14 13:06:12 +02002635static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002637 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Andrei Warkentin371a6892011-04-11 18:10:25 -05002639 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002640 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002641 mmc_claim_host(card->host);
2642 mmc_blk_part_switch(card, md);
2643 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002644 if (card->type != MMC_TYPE_SD_COMBO)
2645 pm_runtime_disable(&card->dev);
2646 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002647 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002648 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}
2650
Ulf Hansson96541ba2015-04-14 13:06:12 +02002651static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002653 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002654 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 if (md) {
2657 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002658 list_for_each_entry(part_md, &md->part, part) {
2659 mmc_queue_suspend(&part_md->queue);
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 return 0;
2663}
2664
Ulf Hansson96541ba2015-04-14 13:06:12 +02002665static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002666{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002667 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002668}
2669
Ulf Hansson0967edc2014-10-06 11:29:42 +02002670#ifdef CONFIG_PM_SLEEP
2671static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002672{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002673 struct mmc_card *card = mmc_dev_to_card(dev);
2674
2675 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002676}
2677
Ulf Hansson0967edc2014-10-06 11:29:42 +02002678static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002680 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002681 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002684 /*
2685 * Resume involves the card going into idle state,
2686 * so current partition is always the main one.
2687 */
2688 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002690 list_for_each_entry(part_md, &md->part, part) {
2691 mmc_queue_resume(&part_md->queue);
2692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694 return 0;
2695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696#endif
2697
Ulf Hansson0967edc2014-10-06 11:29:42 +02002698static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2699
Ulf Hansson96541ba2015-04-14 13:06:12 +02002700static struct mmc_driver mmc_driver = {
2701 .drv = {
2702 .name = "mmcblk",
2703 .pm = &mmc_blk_pm_ops,
2704 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .probe = mmc_blk_probe,
2706 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002707 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708};
2709
2710static int __init mmc_blk_init(void)
2711{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002712 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002714 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2715 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2716
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002717 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002718
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002719 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2720 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002723 res = mmc_register_driver(&mmc_driver);
2724 if (res)
2725 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002727 return 0;
2728 out2:
2729 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 out:
2731 return res;
2732}
2733
2734static void __exit mmc_blk_exit(void)
2735{
2736 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002737 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740module_init(mmc_blk_init);
2741module_exit(mmc_blk_exit);
2742
2743MODULE_LICENSE("GPL");
2744MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2745