blob: cb1698f268f19023429df520713f20a3de6f6d25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
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))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020069static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040070
71/*
72 * The defaults come from config options but can be overriden by module
73 * or bootarg options.
74 */
75static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
76
77/*
78 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000079 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020080 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040081 */
82static int max_devices;
83
Ben Hutchingsa26eba62014-11-06 03:35:09 +000084#define MAX_DEVICES 256
85
Ulf Hanssonb10fa992016-04-07 14:36:46 +020086static DEFINE_IDA(mmc_blk_ida);
87static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * There is one mmc_blk_data per slot.
91 */
92struct mmc_blk_data {
93 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070094 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct gendisk *disk;
96 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050097 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050099 unsigned int flags;
100#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000104 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500105 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300106 unsigned int reset_done;
107#define MMC_BLK_READ BIT(0)
108#define MMC_BLK_WRITE BIT(1)
109#define MMC_BLK_DISCARD BIT(2)
110#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111
112 /*
113 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200114 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 * track of the current selected device partition.
116 */
117 unsigned int part_curr;
118 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 struct device_attribute power_ro_lock;
120 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Arjan van de Vena621aae2006-01-12 18:43:35 +0000123static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400125module_param(perdev_minors, int, 0444);
126MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
127
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200128static inline int mmc_blk_part_switch(struct mmc_card *card,
129 struct mmc_blk_data *md);
130static int get_card_status(struct mmc_card *card, u32 *status, int retries);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
133{
134 struct mmc_blk_data *md;
135
Arjan van de Vena621aae2006-01-12 18:43:35 +0000136 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 md = disk->private_data;
138 if (md && md->usage == 0)
139 md = NULL;
140 if (md)
141 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return md;
145}
146
Andrei Warkentin371a6892011-04-11 18:10:25 -0500147static inline int mmc_get_devidx(struct gendisk *disk)
148{
Colin Cross382c55f2015-10-22 10:00:41 -0700149 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500150 return devidx;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void mmc_blk_put(struct mmc_blk_data *md)
154{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 md->usage--;
157 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500158 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800159 blk_cleanup_queue(md->queue.queue);
160
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200161 spin_lock(&mmc_blk_lock);
162 ida_remove(&mmc_blk_ida, devidx);
163 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kfree(md);
167 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000168 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Johan Rudholmadd710e2011-12-02 08:51:06 +0100171static ssize_t power_ro_lock_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
174 int ret;
175 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
176 struct mmc_card *card = md->queue.card;
177 int locked = 0;
178
179 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
180 locked = 2;
181 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
182 locked = 1;
183
184 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
185
Tomas Winkler9098f842015-07-16 15:50:45 +0200186 mmc_blk_put(md);
187
Johan Rudholmadd710e2011-12-02 08:51:06 +0100188 return ret;
189}
190
191static ssize_t power_ro_lock_store(struct device *dev,
192 struct device_attribute *attr, const char *buf, size_t count)
193{
194 int ret;
195 struct mmc_blk_data *md, *part_md;
196 struct mmc_card *card;
197 unsigned long set;
198
199 if (kstrtoul(buf, 0, &set))
200 return -EINVAL;
201
202 if (set != 1)
203 return count;
204
205 md = mmc_blk_get(dev_to_disk(dev));
206 card = md->queue.card;
207
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200208 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100209
210 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
211 card->ext_csd.boot_ro_lock |
212 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
213 card->ext_csd.part_time);
214 if (ret)
215 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
216 else
217 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
218
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200219 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100220
221 if (!ret) {
222 pr_info("%s: Locking boot partition ro until next power on\n",
223 md->disk->disk_name);
224 set_disk_ro(md->disk, 1);
225
226 list_for_each_entry(part_md, &md->part, part)
227 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
228 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
229 set_disk_ro(part_md->disk, 1);
230 }
231 }
232
233 mmc_blk_put(md);
234 return count;
235}
236
Andrei Warkentin371a6892011-04-11 18:10:25 -0500237static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
238 char *buf)
239{
240 int ret;
241 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
242
Baruch Siach0031a982014-09-22 10:12:51 +0300243 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500244 get_disk_ro(dev_to_disk(dev)) ^
245 md->read_only);
246 mmc_blk_put(md);
247 return ret;
248}
249
250static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
252{
253 int ret;
254 char *end;
255 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
256 unsigned long set = simple_strtoul(buf, &end, 0);
257 if (end == buf) {
258 ret = -EINVAL;
259 goto out;
260 }
261
262 set_disk_ro(dev_to_disk(dev), set || md->read_only);
263 ret = count;
264out:
265 mmc_blk_put(md);
266 return ret;
267}
268
Al Viroa5a15612008-03-02 10:33:30 -0500269static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Al Viroa5a15612008-03-02 10:33:30 -0500271 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int ret = -ENXIO;
273
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200274 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (md) {
276 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500277 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700279
Al Viroa5a15612008-03-02 10:33:30 -0500280 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700281 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700282 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200285 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return ret;
288}
289
Al Virodb2a1442013-05-05 21:52:57 -0400290static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Al Viroa5a15612008-03-02 10:33:30 -0500292 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200294 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200296 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800300mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800302 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
303 geo->heads = 4;
304 geo->sectors = 16;
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
John Calixtocb87ea22011-04-26 18:56:29 -0400308struct mmc_blk_ioc_data {
309 struct mmc_ioc_cmd ic;
310 unsigned char *buf;
311 u64 buf_bytes;
312};
313
314static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
315 struct mmc_ioc_cmd __user *user)
316{
317 struct mmc_blk_ioc_data *idata;
318 int err;
319
yalin wang1ff89502015-11-12 19:27:11 +0800320 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400321 if (!idata) {
322 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400323 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400324 }
325
326 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
327 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400328 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400329 }
330
331 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
332 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
333 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400334 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400335 }
336
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300337 if (!idata->buf_bytes) {
338 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100339 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300340 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100341
yalin wang1ff89502015-11-12 19:27:11 +0800342 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400343 if (!idata->buf) {
344 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400345 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400346 }
347
348 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
349 idata->ic.data_ptr, idata->buf_bytes)) {
350 err = -EFAULT;
351 goto copy_err;
352 }
353
354 return idata;
355
356copy_err:
357 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400358idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400359 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400360out:
John Calixtocb87ea22011-04-26 18:56:29 -0400361 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400362}
363
Jon Huntera5f57742015-09-22 10:27:53 +0100364static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
365 struct mmc_blk_ioc_data *idata)
366{
367 struct mmc_ioc_cmd *ic = &idata->ic;
368
369 if (copy_to_user(&(ic_ptr->response), ic->response,
370 sizeof(ic->response)))
371 return -EFAULT;
372
373 if (!idata->ic.write_flag) {
374 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
375 idata->buf, idata->buf_bytes))
376 return -EFAULT;
377 }
378
379 return 0;
380}
381
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200382static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
383 u32 retries_max)
384{
385 int err;
386 u32 retry_count = 0;
387
388 if (!status || !retries_max)
389 return -EINVAL;
390
391 do {
392 err = get_card_status(card, status, 5);
393 if (err)
394 break;
395
396 if (!R1_STATUS(*status) &&
397 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
398 break; /* RPMB programming operation complete */
399
400 /*
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
403 * frequently.
404 */
405 usleep_range(1000, 5000);
406 } while (++retry_count < retries_max);
407
408 if (retry_count == retries_max)
409 err = -EPERM;
410
411 return err;
412}
413
Maya Erez775a9362013-04-18 15:41:55 +0300414static int ioctl_do_sanitize(struct mmc_card *card)
415{
416 int err;
417
Ulf Hanssona2d10862013-12-16 14:37:26 +0100418 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300419 pr_warn("%s: %s - SANITIZE is not supported\n",
420 mmc_hostname(card->host), __func__);
421 err = -EOPNOTSUPP;
422 goto out;
423 }
424
425 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
426 mmc_hostname(card->host), __func__);
427
428 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
429 EXT_CSD_SANITIZE_START, 1,
430 MMC_SANITIZE_REQ_TIMEOUT);
431
432 if (err)
433 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
434 mmc_hostname(card->host), __func__, err);
435
436 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
437 __func__);
438out:
439 return err;
440}
441
Jon Huntera5f57742015-09-22 10:27:53 +0100442static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
443 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400444{
John Calixtocb87ea22011-04-26 18:56:29 -0400445 struct mmc_command cmd = {0};
446 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530447 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400448 struct scatterlist sg;
449 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200450 int is_rpmb = false;
451 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400452
Jon Huntera5f57742015-09-22 10:27:53 +0100453 if (!card || !md || !idata)
454 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400455
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200456 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
457 is_rpmb = true;
458
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100459 cmd.opcode = idata->ic.opcode;
460 cmd.arg = idata->ic.arg;
461 cmd.flags = idata->ic.flags;
462
463 if (idata->buf_bytes) {
464 data.sg = &sg;
465 data.sg_len = 1;
466 data.blksz = idata->ic.blksz;
467 data.blocks = idata->ic.blocks;
468
469 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
470
471 if (idata->ic.write_flag)
472 data.flags = MMC_DATA_WRITE;
473 else
474 data.flags = MMC_DATA_READ;
475
476 /* data.flags must already be set before doing this. */
477 mmc_set_data_timeout(&data, card);
478
479 /* Allow overriding the timeout_ns for empirical tuning. */
480 if (idata->ic.data_timeout_ns)
481 data.timeout_ns = idata->ic.data_timeout_ns;
482
483 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
484 /*
485 * Pretend this is a data transfer and rely on the
486 * host driver to compute timeout. When all host
487 * drivers support cmd.cmd_timeout for R1B, this
488 * can be changed to:
489 *
490 * mrq.data = NULL;
491 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
492 */
493 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
494 }
495
496 mrq.data = &data;
497 }
498
499 mrq.cmd = &cmd;
500
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200501 err = mmc_blk_part_switch(card, md);
502 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100503 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200504
John Calixtocb87ea22011-04-26 18:56:29 -0400505 if (idata->ic.is_acmd) {
506 err = mmc_app_cmd(card->host, card);
507 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100508 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400509 }
510
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200511 if (is_rpmb) {
512 err = mmc_set_blockcount(card, data.blocks,
513 idata->ic.write_flag & (1 << 31));
514 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100515 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200516 }
517
Yaniv Gardia82e4842013-06-05 14:13:08 +0300518 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
519 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300520 err = ioctl_do_sanitize(card);
521
522 if (err)
523 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
524 __func__, err);
525
Jon Huntera5f57742015-09-22 10:27:53 +0100526 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300527 }
528
John Calixtocb87ea22011-04-26 18:56:29 -0400529 mmc_wait_for_req(card->host, &mrq);
530
531 if (cmd.error) {
532 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
533 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100534 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400535 }
536 if (data.error) {
537 dev_err(mmc_dev(card->host), "%s: data error %d\n",
538 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100539 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400540 }
541
542 /*
543 * According to the SD specs, some commands require a delay after
544 * issuing the command.
545 */
546 if (idata->ic.postsleep_min_us)
547 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
548
Jon Huntera5f57742015-09-22 10:27:53 +0100549 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400550
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200551 if (is_rpmb) {
552 /*
553 * Ensure RPMB command has completed by polling CMD13
554 * "Send Status".
555 */
556 err = ioctl_rpmb_card_status_poll(card, &status, 5);
557 if (err)
558 dev_err(mmc_dev(card->host),
559 "%s: Card Status=0x%08X, error %d\n",
560 __func__, status, err);
561 }
562
Jon Huntera5f57742015-09-22 10:27:53 +0100563 return err;
564}
565
566static int mmc_blk_ioctl_cmd(struct block_device *bdev,
567 struct mmc_ioc_cmd __user *ic_ptr)
568{
569 struct mmc_blk_ioc_data *idata;
570 struct mmc_blk_data *md;
571 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700572 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100573
Shawn Lin83c742c2016-03-16 18:15:47 +0800574 /*
575 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
576 * whole block device, not on a partition. This prevents overspray
577 * between sibling partitions.
578 */
579 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
580 return -EPERM;
581
Jon Huntera5f57742015-09-22 10:27:53 +0100582 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
583 if (IS_ERR(idata))
584 return PTR_ERR(idata);
585
586 md = mmc_blk_get(bdev->bd_disk);
587 if (!md) {
588 err = -EINVAL;
589 goto cmd_err;
590 }
591
592 card = md->queue.card;
593 if (IS_ERR(card)) {
594 err = PTR_ERR(card);
595 goto cmd_done;
596 }
597
598 mmc_get_card(card);
599
Grant Grundlerb0934102015-09-23 18:30:33 -0700600 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100601
Adrian Hunter3c866562016-05-04 14:38:12 +0300602 /* Always switch back to main area after RPMB access */
603 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
604 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
605
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200606 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400607
Grant Grundlerb0934102015-09-23 18:30:33 -0700608 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100609
John Calixtocb87ea22011-04-26 18:56:29 -0400610cmd_done:
611 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300612cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400613 kfree(idata->buf);
614 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700615 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400616}
617
Jon Huntera5f57742015-09-22 10:27:53 +0100618static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
619 struct mmc_ioc_multi_cmd __user *user)
620{
621 struct mmc_blk_ioc_data **idata = NULL;
622 struct mmc_ioc_cmd __user *cmds = user->cmds;
623 struct mmc_card *card;
624 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700625 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100626 __u64 num_of_cmds;
627
Shawn Lin83c742c2016-03-16 18:15:47 +0800628 /*
629 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
630 * whole block device, not on a partition. This prevents overspray
631 * between sibling partitions.
632 */
633 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
634 return -EPERM;
635
Jon Huntera5f57742015-09-22 10:27:53 +0100636 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
637 sizeof(num_of_cmds)))
638 return -EFAULT;
639
640 if (num_of_cmds > MMC_IOC_MAX_CMDS)
641 return -EINVAL;
642
643 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
644 if (!idata)
645 return -ENOMEM;
646
647 for (i = 0; i < num_of_cmds; i++) {
648 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
649 if (IS_ERR(idata[i])) {
650 err = PTR_ERR(idata[i]);
651 num_of_cmds = i;
652 goto cmd_err;
653 }
654 }
655
656 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800657 if (!md) {
658 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100659 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800660 }
Jon Huntera5f57742015-09-22 10:27:53 +0100661
662 card = md->queue.card;
663 if (IS_ERR(card)) {
664 err = PTR_ERR(card);
665 goto cmd_done;
666 }
667
668 mmc_get_card(card);
669
Grant Grundlerb0934102015-09-23 18:30:33 -0700670 for (i = 0; i < num_of_cmds && !ioc_err; i++)
671 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100672
Adrian Hunter3c866562016-05-04 14:38:12 +0300673 /* Always switch back to main area after RPMB access */
674 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
675 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
676
Jon Huntera5f57742015-09-22 10:27:53 +0100677 mmc_put_card(card);
678
679 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700680 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100681 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100682
683cmd_done:
684 mmc_blk_put(md);
685cmd_err:
686 for (i = 0; i < num_of_cmds; i++) {
687 kfree(idata[i]->buf);
688 kfree(idata[i]);
689 }
690 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700691 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100692}
693
John Calixtocb87ea22011-04-26 18:56:29 -0400694static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
695 unsigned int cmd, unsigned long arg)
696{
Jon Huntera5f57742015-09-22 10:27:53 +0100697 switch (cmd) {
698 case MMC_IOC_CMD:
699 return mmc_blk_ioctl_cmd(bdev,
700 (struct mmc_ioc_cmd __user *)arg);
701 case MMC_IOC_MULTI_CMD:
702 return mmc_blk_ioctl_multi_cmd(bdev,
703 (struct mmc_ioc_multi_cmd __user *)arg);
704 default:
705 return -EINVAL;
706 }
John Calixtocb87ea22011-04-26 18:56:29 -0400707}
708
709#ifdef CONFIG_COMPAT
710static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
711 unsigned int cmd, unsigned long arg)
712{
713 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
714}
715#endif
716
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700717static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500718 .open = mmc_blk_open,
719 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400722 .ioctl = mmc_blk_ioctl,
723#ifdef CONFIG_COMPAT
724 .compat_ioctl = mmc_blk_compat_ioctl,
725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};
727
Andrei Warkentin371a6892011-04-11 18:10:25 -0500728static inline int mmc_blk_part_switch(struct mmc_card *card,
729 struct mmc_blk_data *md)
730{
731 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200732 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300733
Andrei Warkentin371a6892011-04-11 18:10:25 -0500734 if (main_md->part_curr == md->part_type)
735 return 0;
736
737 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300738 u8 part_config = card->ext_csd.part_config;
739
Adrian Hunter57da0c02016-05-04 14:38:13 +0300740 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
741 mmc_retune_pause(card->host);
742
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300743 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
744 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500745
746 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300747 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500748 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300749 if (ret) {
750 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
751 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500752 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300753 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300754
755 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300756
757 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
758 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300759 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500760
761 main_md->part_curr = md->part_type;
762 return 0;
763}
764
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700765static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
766{
767 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100768 u32 result;
769 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700770
Venkatraman Sad5fd972011-08-25 00:30:50 +0530771 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400772 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400773 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700774
775 struct scatterlist sg;
776
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700777 cmd.opcode = MMC_APP_CMD;
778 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700779 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700780
781 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700782 if (err)
783 return (u32)-1;
784 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700785 return (u32)-1;
786
787 memset(&cmd, 0, sizeof(struct mmc_command));
788
789 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
790 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700791 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700792
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700793 data.blksz = 4;
794 data.blocks = 1;
795 data.flags = MMC_DATA_READ;
796 data.sg = &sg;
797 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530798 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700799
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700800 mrq.cmd = &cmd;
801 mrq.data = &data;
802
Ben Dooks051913d2009-06-08 23:33:57 +0100803 blocks = kmalloc(4, GFP_KERNEL);
804 if (!blocks)
805 return (u32)-1;
806
807 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700808
809 mmc_wait_for_req(card->host, &mrq);
810
Ben Dooks051913d2009-06-08 23:33:57 +0100811 result = ntohl(*blocks);
812 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700813
Ben Dooks051913d2009-06-08 23:33:57 +0100814 if (cmd.error || data.error)
815 result = (u32)-1;
816
817 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700818}
819
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100820static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300821{
Chris Ball1278dba2011-04-13 23:40:30 -0400822 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300823 int err;
824
Adrian Hunter504f1912008-10-16 12:55:25 +0300825 cmd.opcode = MMC_SEND_STATUS;
826 if (!mmc_host_is_spi(card->host))
827 cmd.arg = card->rca << 16;
828 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100829 err = mmc_wait_for_cmd(card->host, &cmd, retries);
830 if (err == 0)
831 *status = cmd.resp[0];
832 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300833}
834
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100835static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100836 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100837{
838 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
839 int err = 0;
840 u32 status;
841
842 do {
843 err = get_card_status(card, &status, 5);
844 if (err) {
845 pr_err("%s: error %d requesting status\n",
846 req->rq_disk->disk_name, err);
847 return err;
848 }
849
850 if (status & R1_ERROR) {
851 pr_err("%s: %s: error sending status cmd, status %#x\n",
852 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100853 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100854 }
855
Ulf Hansson95a91292014-01-29 13:11:27 +0100856 /* We may rely on the host hw to handle busy detection.*/
857 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
858 hw_busy_detect)
859 break;
860
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100861 /*
862 * Timeout if the device never becomes ready for data and never
863 * leaves the program state.
864 */
865 if (time_after(jiffies, timeout)) {
866 pr_err("%s: Card stuck in programming state! %s %s\n",
867 mmc_hostname(card->host),
868 req->rq_disk->disk_name, __func__);
869 return -ETIMEDOUT;
870 }
871
872 /*
873 * Some cards mishandle the status bits,
874 * so make sure to check both the busy
875 * indication and the card state.
876 */
877 } while (!(status & R1_READY_FOR_DATA) ||
878 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
879
880 return err;
881}
882
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100883static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100884 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100885{
886 struct mmc_host *host = card->host;
887 struct mmc_command cmd = {0};
888 int err;
889 bool use_r1b_resp = rq_data_dir(req) == WRITE;
890
891 /*
892 * Normally we use R1B responses for WRITE, but in cases where the host
893 * has specified a max_busy_timeout we need to validate it. A failure
894 * means we need to prevent the host from doing hw busy detection, which
895 * is done by converting to a R1 response instead.
896 */
897 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
898 use_r1b_resp = false;
899
900 cmd.opcode = MMC_STOP_TRANSMISSION;
901 if (use_r1b_resp) {
902 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
903 cmd.busy_timeout = timeout_ms;
904 } else {
905 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
906 }
907
908 err = mmc_wait_for_cmd(host, &cmd, 5);
909 if (err)
910 return err;
911
912 *stop_status = cmd.resp[0];
913
914 /* No need to check card status in case of READ. */
915 if (rq_data_dir(req) == READ)
916 return 0;
917
918 if (!mmc_host_is_spi(host) &&
919 (*stop_status & R1_ERROR)) {
920 pr_err("%s: %s: general error sending stop command, resp %#x\n",
921 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100922 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100923 }
924
925 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
926}
927
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530928#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100929#define ERR_RETRY 2
930#define ERR_ABORT 1
931#define ERR_CONTINUE 0
932
933static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
934 bool status_valid, u32 status)
935{
936 switch (error) {
937 case -EILSEQ:
938 /* response crc error, retry the r/w cmd */
939 pr_err("%s: %s sending %s command, card status %#x\n",
940 req->rq_disk->disk_name, "response CRC error",
941 name, status);
942 return ERR_RETRY;
943
944 case -ETIMEDOUT:
945 pr_err("%s: %s sending %s command, card status %#x\n",
946 req->rq_disk->disk_name, "timed out", name, status);
947
948 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530949 if (!status_valid) {
950 pr_err("%s: status not valid, retrying timeout\n",
951 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100952 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530953 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100954
955 /*
956 * If it was a r/w cmd crc error, or illegal command
957 * (eg, issued in wrong state) then retry - we should
958 * have corrected the state problem above.
959 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530960 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
961 pr_err("%s: command error, retrying timeout\n",
962 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100963 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530964 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100965
966 /* Otherwise abort the command */
967 return ERR_ABORT;
968
969 default:
970 /* We don't understand the error code the driver gave us */
971 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
972 req->rq_disk->disk_name, error, status);
973 return ERR_ABORT;
974 }
975}
976
977/*
978 * Initial r/w and stop cmd error recovery.
979 * We don't know whether the card received the r/w cmd or not, so try to
980 * restore things back to a sane state. Essentially, we do this as follows:
981 * - Obtain card status. If the first attempt to obtain card status fails,
982 * the status word will reflect the failed status cmd, not the failed
983 * r/w cmd. If we fail to obtain card status, it suggests we can no
984 * longer communicate with the card.
985 * - Check the card state. If the card received the cmd but there was a
986 * transient problem with the response, it might still be in a data transfer
987 * mode. Try to send it a stop command. If this fails, we can't recover.
988 * - If the r/w cmd failed due to a response CRC error, it was probably
989 * transient, so retry the cmd.
990 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
991 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
992 * illegal cmd, retry.
993 * Otherwise we don't understand what happened, so abort.
994 */
995static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +0100996 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100997{
998 bool prev_cmd_status_valid = true;
999 u32 status, stop_status = 0;
1000 int err, retry;
1001
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301002 if (mmc_card_removed(card))
1003 return ERR_NOMEDIUM;
1004
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001005 /*
1006 * Try to get card status which indicates both the card state
1007 * and why there was no response. If the first attempt fails,
1008 * we can't be sure the returned status is for the r/w command.
1009 */
1010 for (retry = 2; retry >= 0; retry--) {
1011 err = get_card_status(card, &status, 0);
1012 if (!err)
1013 break;
1014
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001015 /* Re-tune if needed */
1016 mmc_retune_recheck(card->host);
1017
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001018 prev_cmd_status_valid = false;
1019 pr_err("%s: error %d sending status command, %sing\n",
1020 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1021 }
1022
1023 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301024 if (err) {
1025 /* Check if the card is removed */
1026 if (mmc_detect_card_removed(card->host))
1027 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001028 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301029 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001030
Adrian Hunter67716322011-08-29 16:42:15 +03001031 /* Flag ECC errors */
1032 if ((status & R1_CARD_ECC_FAILED) ||
1033 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1034 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001035 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001036
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001037 /* Flag General errors */
1038 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1039 if ((status & R1_ERROR) ||
1040 (brq->stop.resp[0] & R1_ERROR)) {
1041 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1042 req->rq_disk->disk_name, __func__,
1043 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001044 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001045 }
1046
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001047 /*
1048 * Check the current card state. If it is in some data transfer
1049 * mode, tell it to stop (and hopefully transition back to TRAN.)
1050 */
1051 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1052 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001053 err = send_stop(card,
1054 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1055 req, gen_err, &stop_status);
1056 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001057 pr_err("%s: error %d sending stop command\n",
1058 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001059 /*
1060 * If the stop cmd also timed out, the card is probably
1061 * not present, so abort. Other errors are bad news too.
1062 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001063 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001064 }
1065
Adrian Hunter67716322011-08-29 16:42:15 +03001066 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001067 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001068 }
1069
1070 /* Check for set block count errors */
1071 if (brq->sbc.error)
1072 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1073 prev_cmd_status_valid, status);
1074
1075 /* Check for r/w command errors */
1076 if (brq->cmd.error)
1077 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1078 prev_cmd_status_valid, status);
1079
Adrian Hunter67716322011-08-29 16:42:15 +03001080 /* Data errors */
1081 if (!brq->stop.error)
1082 return ERR_CONTINUE;
1083
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001084 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001085 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001086 req->rq_disk->disk_name, brq->stop.error,
1087 brq->cmd.resp[0], status);
1088
1089 /*
1090 * Subsitute in our own stop status as this will give the error
1091 * state which happened during the execution of the r/w command.
1092 */
1093 if (stop_status) {
1094 brq->stop.resp[0] = stop_status;
1095 brq->stop.error = 0;
1096 }
1097 return ERR_CONTINUE;
1098}
1099
Adrian Hunter67716322011-08-29 16:42:15 +03001100static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1101 int type)
1102{
1103 int err;
1104
1105 if (md->reset_done & type)
1106 return -EEXIST;
1107
1108 md->reset_done |= type;
1109 err = mmc_hw_reset(host);
1110 /* Ensure we switch back to the correct partition */
1111 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001112 struct mmc_blk_data *main_md =
1113 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001114 int part_err;
1115
1116 main_md->part_curr = main_md->part_type;
1117 part_err = mmc_blk_part_switch(host->card, md);
1118 if (part_err) {
1119 /*
1120 * We have failed to get back into the correct
1121 * partition, so we need to abort the whole request.
1122 */
1123 return -ENODEV;
1124 }
1125 }
1126 return err;
1127}
1128
1129static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1130{
1131 md->reset_done &= ~type;
1132}
1133
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001134int mmc_access_rpmb(struct mmc_queue *mq)
1135{
Linus Walleij7db30282016-11-18 13:36:15 +01001136 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001137 /*
1138 * If this is a RPMB partition access, return ture
1139 */
1140 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1141 return true;
1142
1143 return false;
1144}
1145
Adrian Hunterbd788c92010-08-11 14:17:47 -07001146static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1147{
Linus Walleij7db30282016-11-18 13:36:15 +01001148 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001149 struct mmc_card *card = md->queue.card;
1150 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001151 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001152
Adrian Hunterbd788c92010-08-11 14:17:47 -07001153 if (!mmc_can_erase(card)) {
1154 err = -EOPNOTSUPP;
1155 goto out;
1156 }
1157
1158 from = blk_rq_pos(req);
1159 nr = blk_rq_sectors(req);
1160
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001161 if (mmc_can_discard(card))
1162 arg = MMC_DISCARD_ARG;
1163 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001164 arg = MMC_TRIM_ARG;
1165 else
1166 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001167retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001168 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1169 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1170 INAND_CMD38_ARG_EXT_CSD,
1171 arg == MMC_TRIM_ARG ?
1172 INAND_CMD38_ARG_TRIM :
1173 INAND_CMD38_ARG_ERASE,
1174 0);
1175 if (err)
1176 goto out;
1177 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001178 err = mmc_erase(card, from, nr, arg);
1179out:
Adrian Hunter67716322011-08-29 16:42:15 +03001180 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1181 goto retry;
1182 if (!err)
1183 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301184 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001185
Adrian Hunterbd788c92010-08-11 14:17:47 -07001186 return err ? 0 : 1;
1187}
1188
Adrian Hunter49804542010-08-11 14:17:50 -07001189static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1190 struct request *req)
1191{
Linus Walleij7db30282016-11-18 13:36:15 +01001192 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001193 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001194 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001195 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001196
Maya Erez775a9362013-04-18 15:41:55 +03001197 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001198 err = -EOPNOTSUPP;
1199 goto out;
1200 }
1201
1202 from = blk_rq_pos(req);
1203 nr = blk_rq_sectors(req);
1204
Maya Erez775a9362013-04-18 15:41:55 +03001205 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1206 arg = MMC_SECURE_TRIM1_ARG;
1207 else
1208 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001209
Adrian Hunter67716322011-08-29 16:42:15 +03001210retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001211 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1212 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1213 INAND_CMD38_ARG_EXT_CSD,
1214 arg == MMC_SECURE_TRIM1_ARG ?
1215 INAND_CMD38_ARG_SECTRIM1 :
1216 INAND_CMD38_ARG_SECERASE,
1217 0);
1218 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001219 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001220 }
Adrian Hunter28302812012-04-05 14:45:48 +03001221
Adrian Hunter49804542010-08-11 14:17:50 -07001222 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001223 if (err == -EIO)
1224 goto out_retry;
1225 if (err)
1226 goto out;
1227
1228 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001229 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1230 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1231 INAND_CMD38_ARG_EXT_CSD,
1232 INAND_CMD38_ARG_SECTRIM2,
1233 0);
1234 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001235 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001236 }
Adrian Hunter28302812012-04-05 14:45:48 +03001237
Adrian Hunter49804542010-08-11 14:17:50 -07001238 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001239 if (err == -EIO)
1240 goto out_retry;
1241 if (err)
1242 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001243 }
Adrian Hunter28302812012-04-05 14:45:48 +03001244
Adrian Hunter28302812012-04-05 14:45:48 +03001245out_retry:
1246 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001247 goto retry;
1248 if (!err)
1249 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001250out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301251 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001252
Adrian Hunter49804542010-08-11 14:17:50 -07001253 return err ? 0 : 1;
1254}
1255
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001256static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1257{
Linus Walleij7db30282016-11-18 13:36:15 +01001258 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001259 struct mmc_card *card = md->queue.card;
1260 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001261
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001262 ret = mmc_flush_cache(card);
1263 if (ret)
1264 ret = -EIO;
1265
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301266 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001267
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001268 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001269}
1270
1271/*
1272 * Reformat current write as a reliable write, supporting
1273 * both legacy and the enhanced reliable write MMC cards.
1274 * In each transfer we'll handle only as much as a single
1275 * reliable write can handle, thus finish the request in
1276 * partial completions.
1277 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001278static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1279 struct mmc_card *card,
1280 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001281{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001282 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1283 /* Legacy mode imposes restrictions on transfers. */
1284 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1285 brq->data.blocks = 1;
1286
1287 if (brq->data.blocks > card->ext_csd.rel_sectors)
1288 brq->data.blocks = card->ext_csd.rel_sectors;
1289 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1290 brq->data.blocks = 1;
1291 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001292}
1293
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001294#define CMD_ERRORS \
1295 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1296 R1_ADDRESS_ERROR | /* Misaligned address */ \
1297 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1298 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1299 R1_CC_ERROR | /* Card controller error */ \
1300 R1_ERROR) /* General/unknown error */
1301
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001302static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1303 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001304{
Per Forlinee8a43a2011-07-01 18:55:33 +02001305 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1306 mmc_active);
1307 struct mmc_blk_request *brq = &mq_mrq->brq;
1308 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001309 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001310 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001311 bool gen_err = false;
Per Forlind78d4a82011-07-01 18:55:30 +02001312
1313 /*
1314 * sbc.error indicates a problem with the set block count
1315 * command. No data will have been transferred.
1316 *
1317 * cmd.error indicates a problem with the r/w command. No
1318 * data will have been transferred.
1319 *
1320 * stop.error indicates a problem with the stop command. Data
1321 * may have been transferred, or may still be transferring.
1322 */
Adrian Hunter67716322011-08-29 16:42:15 +03001323 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1324 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001325 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001326 case ERR_RETRY:
1327 return MMC_BLK_RETRY;
1328 case ERR_ABORT:
1329 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301330 case ERR_NOMEDIUM:
1331 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001332 case ERR_CONTINUE:
1333 break;
1334 }
1335 }
1336
1337 /*
1338 * Check for errors relating to the execution of the
1339 * initial command - such as address errors. No data
1340 * has been transferred.
1341 */
1342 if (brq->cmd.resp[0] & CMD_ERRORS) {
1343 pr_err("%s: r/w command failed, status = %#x\n",
1344 req->rq_disk->disk_name, brq->cmd.resp[0]);
1345 return MMC_BLK_ABORT;
1346 }
1347
1348 /*
1349 * Everything else is either success, or a data error of some
1350 * kind. If it was a write, we may have transitioned to
1351 * program mode, which we have to wait for it to complete.
1352 */
1353 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001354 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001355
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001356 /* Check stop command response */
1357 if (brq->stop.resp[0] & R1_ERROR) {
1358 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1359 req->rq_disk->disk_name, __func__,
1360 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001361 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001362 }
1363
Ulf Hansson95a91292014-01-29 13:11:27 +01001364 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1365 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001366 if (err)
1367 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001368 }
1369
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001370 /* if general error occurs, retry the write operation. */
1371 if (gen_err) {
1372 pr_warn("%s: retrying write for general error\n",
1373 req->rq_disk->disk_name);
1374 return MMC_BLK_RETRY;
1375 }
1376
Per Forlind78d4a82011-07-01 18:55:30 +02001377 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001378 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001379 pr_debug("%s: retrying because a re-tune was needed\n",
1380 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001381 brq->retune_retry_done = 1;
1382 return MMC_BLK_RETRY;
1383 }
Per Forlind78d4a82011-07-01 18:55:30 +02001384 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1385 req->rq_disk->disk_name, brq->data.error,
1386 (unsigned)blk_rq_pos(req),
1387 (unsigned)blk_rq_sectors(req),
1388 brq->cmd.resp[0], brq->stop.resp[0]);
1389
1390 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001391 if (ecc_err)
1392 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001393 return MMC_BLK_DATA_ERR;
1394 } else {
1395 return MMC_BLK_CMD_ERR;
1396 }
1397 }
1398
Adrian Hunter67716322011-08-29 16:42:15 +03001399 if (!brq->data.bytes_xfered)
1400 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001401
Adrian Hunter67716322011-08-29 16:42:15 +03001402 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1403 return MMC_BLK_PARTIAL;
1404
1405 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001406}
1407
Per Forlin54d49d72011-07-01 18:55:29 +02001408static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1409 struct mmc_card *card,
1410 int disable_multi,
1411 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Per Forlin54d49d72011-07-01 18:55:29 +02001413 u32 readcmd, writecmd;
1414 struct mmc_blk_request *brq = &mqrq->brq;
1415 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001416 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301417 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001419 /*
1420 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001421 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001422 */
Luca Porziod3df0462015-11-06 15:12:26 +00001423 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001424 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001425 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001426
Per Forlin54d49d72011-07-01 18:55:29 +02001427 memset(brq, 0, sizeof(struct mmc_blk_request));
1428 brq->mrq.cmd = &brq->cmd;
1429 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Per Forlin54d49d72011-07-01 18:55:29 +02001431 brq->cmd.arg = blk_rq_pos(req);
1432 if (!mmc_card_blockaddr(card))
1433 brq->cmd.arg <<= 9;
1434 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1435 brq->data.blksz = 512;
1436 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1437 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001438 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Per Forlin54d49d72011-07-01 18:55:29 +02001440 /*
1441 * The block layer doesn't support all sector count
1442 * restrictions, so we need to be prepared for too big
1443 * requests.
1444 */
1445 if (brq->data.blocks > card->host->max_blk_count)
1446 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001448 if (brq->data.blocks > 1) {
1449 /*
1450 * After a read error, we redo the request one sector
1451 * at a time in order to accurately determine which
1452 * sectors can be read successfully.
1453 */
1454 if (disable_multi)
1455 brq->data.blocks = 1;
1456
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001457 /*
1458 * Some controllers have HW issues while operating
1459 * in multiple I/O mode
1460 */
1461 if (card->host->ops->multi_io_quirk)
1462 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1463 (rq_data_dir(req) == READ) ?
1464 MMC_DATA_READ : MMC_DATA_WRITE,
1465 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001466 }
Per Forlin54d49d72011-07-01 18:55:29 +02001467
1468 if (brq->data.blocks > 1 || do_rel_wr) {
1469 /* SPI multiblock writes terminate using a special
1470 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001471 */
Per Forlin54d49d72011-07-01 18:55:29 +02001472 if (!mmc_host_is_spi(card->host) ||
1473 rq_data_dir(req) == READ)
1474 brq->mrq.stop = &brq->stop;
1475 readcmd = MMC_READ_MULTIPLE_BLOCK;
1476 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1477 } else {
1478 brq->mrq.stop = NULL;
1479 readcmd = MMC_READ_SINGLE_BLOCK;
1480 writecmd = MMC_WRITE_BLOCK;
1481 }
1482 if (rq_data_dir(req) == READ) {
1483 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001484 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001485 if (brq->mrq.stop)
1486 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1487 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001488 } else {
1489 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001490 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001491 if (brq->mrq.stop)
1492 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1493 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001494 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001495
Per Forlin54d49d72011-07-01 18:55:29 +02001496 if (do_rel_wr)
1497 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001498
Per Forlin54d49d72011-07-01 18:55:29 +02001499 /*
Saugata Das42659002011-12-21 13:09:17 +05301500 * Data tag is used only during writing meta data to speed
1501 * up write and any subsequent read of this meta data
1502 */
1503 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1504 (req->cmd_flags & REQ_META) &&
1505 (rq_data_dir(req) == WRITE) &&
1506 ((brq->data.blocks * brq->data.blksz) >=
1507 card->ext_csd.data_tag_unit_size);
1508
1509 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001510 * Pre-defined multi-block transfers are preferable to
1511 * open ended-ones (and necessary for reliable writes).
1512 * However, it is not sufficient to just send CMD23,
1513 * and avoid the final CMD12, as on an error condition
1514 * CMD12 (stop) needs to be sent anyway. This, coupled
1515 * with Auto-CMD23 enhancements provided by some
1516 * hosts, means that the complexity of dealing
1517 * with this is best left to the host. If CMD23 is
1518 * supported by card and host, we'll fill sbc in and let
1519 * the host deal with handling it correctly. This means
1520 * that for hosts that don't expose MMC_CAP_CMD23, no
1521 * change of behavior will be observed.
1522 *
1523 * N.B: Some MMC cards experience perf degradation.
1524 * We'll avoid using CMD23-bounded multiblock writes for
1525 * these, while retaining features like reliable writes.
1526 */
Saugata Das42659002011-12-21 13:09:17 +05301527 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1528 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1529 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001530 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1531 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301532 (do_rel_wr ? (1 << 31) : 0) |
1533 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001534 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1535 brq->mrq.sbc = &brq->sbc;
1536 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001537
Per Forlin54d49d72011-07-01 18:55:29 +02001538 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001539
Per Forlin54d49d72011-07-01 18:55:29 +02001540 brq->data.sg = mqrq->sg;
1541 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001542
Per Forlin54d49d72011-07-01 18:55:29 +02001543 /*
1544 * Adjust the sg list so it is the same size as the
1545 * request.
1546 */
1547 if (brq->data.blocks != blk_rq_sectors(req)) {
1548 int i, data_size = brq->data.blocks << 9;
1549 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001550
Per Forlin54d49d72011-07-01 18:55:29 +02001551 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1552 data_size -= sg->length;
1553 if (data_size <= 0) {
1554 sg->length += data_size;
1555 i++;
1556 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001557 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001558 }
Per Forlin54d49d72011-07-01 18:55:29 +02001559 brq->data.sg_len = i;
1560 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001561
Per Forlinee8a43a2011-07-01 18:55:33 +02001562 mqrq->mmc_active.mrq = &brq->mrq;
1563 mqrq->mmc_active.err_check = mmc_blk_err_check;
1564
Per Forlin54d49d72011-07-01 18:55:29 +02001565 mmc_queue_bounce_pre(mqrq);
1566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Adrian Hunter67716322011-08-29 16:42:15 +03001568static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1569 struct mmc_blk_request *brq, struct request *req,
1570 int ret)
1571{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001572 struct mmc_queue_req *mq_rq;
1573 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1574
Adrian Hunter67716322011-08-29 16:42:15 +03001575 /*
1576 * If this is an SD card and we're writing, we can first
1577 * mark the known good sectors as ok.
1578 *
1579 * If the card is not SD, we can still ok written sectors
1580 * as reported by the controller (which might be less than
1581 * the real number of written sectors, but never more).
1582 */
1583 if (mmc_card_sd(card)) {
1584 u32 blocks;
1585
1586 blocks = mmc_sd_num_wr_blocks(card);
1587 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301588 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001589 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001590 } else {
1591 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001592 }
1593 return ret;
1594}
1595
Per Forlinee8a43a2011-07-01 18:55:33 +02001596static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001597{
Linus Walleij7db30282016-11-18 13:36:15 +01001598 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001599 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001600 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001601 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001602 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001603 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001604 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001605 struct mmc_async_req *areq;
1606
1607 if (!rqc && !mq->mqrq_prev->req)
1608 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001609
1610 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001611 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301612 /*
1613 * When 4KB native sector is enabled, only 8 blocks
1614 * multiple read or write is allowed
1615 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001616 if (mmc_large_sector(card) &&
1617 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301618 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001619 rqc->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001620 mq_rq = mq->mqrq_cur;
Adrian Hunter5be80372016-11-29 12:09:09 +02001621 req = rqc;
1622 rqc = NULL;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301623 goto cmd_abort;
1624 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001625
Linus Walleij03d640a2016-11-25 10:35:00 +01001626 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001627 areq = &mq->mqrq_cur->mmc_active;
1628 } else
1629 areq = NULL;
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001630 areq = mmc_start_req(card->host, areq, &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001631 if (!areq) {
1632 if (status == MMC_BLK_NEW_REQUEST)
1633 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001634 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001635 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001636
Per Forlinee8a43a2011-07-01 18:55:33 +02001637 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1638 brq = &mq_rq->brq;
1639 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001640 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001641 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001642
Per Forlind78d4a82011-07-01 18:55:30 +02001643 switch (status) {
1644 case MMC_BLK_SUCCESS:
1645 case MMC_BLK_PARTIAL:
1646 /*
1647 * A block was successfully transferred.
1648 */
Adrian Hunter67716322011-08-29 16:42:15 +03001649 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001650
Linus Walleij03d640a2016-11-25 10:35:00 +01001651 ret = blk_end_request(req, 0,
1652 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001653
Adrian Hunter67716322011-08-29 16:42:15 +03001654 /*
1655 * If the blk_end_request function returns non-zero even
1656 * though all data has been transferred and no errors
1657 * were returned by the host controller, it's a bug.
1658 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001659 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301660 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001661 __func__, blk_rq_bytes(req),
1662 brq->data.bytes_xfered);
1663 rqc = NULL;
1664 goto cmd_abort;
1665 }
Per Forlind78d4a82011-07-01 18:55:30 +02001666 break;
1667 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001668 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08001669 if (mmc_blk_reset(md, card->host, type))
1670 goto cmd_abort;
1671 if (!ret)
1672 goto start_new_req;
1673 break;
Per Forlind78d4a82011-07-01 18:55:30 +02001674 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001675 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02001676 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001677 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001678 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001679 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001680 if (!mmc_blk_reset(md, card->host, type))
1681 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001682 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001683 case MMC_BLK_DATA_ERR: {
1684 int err;
1685
1686 err = mmc_blk_reset(md, card->host, type);
1687 if (!err)
1688 break;
Linus Walleij03d640a2016-11-25 10:35:00 +01001689 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001690 goto cmd_abort;
1691 /* Fall through */
1692 }
1693 case MMC_BLK_ECC_ERR:
1694 if (brq->data.blocks > 1) {
1695 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001696 pr_warn("%s: retrying using single block read\n",
1697 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001698 disable_multi = 1;
1699 break;
1700 }
Per Forlind78d4a82011-07-01 18:55:30 +02001701 /*
1702 * After an error, we redo I/O one sector at a
1703 * time, so we only reach here after trying to
1704 * read a single sector.
1705 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301706 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001707 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001708 if (!ret)
1709 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001710 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301711 case MMC_BLK_NOMEDIUM:
1712 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001713 default:
1714 pr_err("%s: Unhandled return value (%d)",
1715 req->rq_disk->disk_name, status);
1716 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001717 }
1718
Per Forlinee8a43a2011-07-01 18:55:33 +02001719 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001720 /*
1721 * In case of a incomplete request
1722 * prepare it again and resend.
1723 */
1724 mmc_blk_rw_rq_prep(mq_rq, card,
1725 disable_multi, mq);
1726 mmc_start_req(card->host,
1727 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001728 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } while (ret);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 1;
1733
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001734 cmd_abort:
Linus Walleij03d640a2016-11-25 10:35:00 +01001735 if (mmc_card_removed(card))
Linus Torvalds36869cb2016-12-13 10:19:16 -08001736 req->rq_flags |= RQF_QUIET;
Linus Walleij03d640a2016-11-25 10:35:00 +01001737 while (ret)
1738 ret = blk_end_request(req, -EIO,
1739 blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Per Forlinee8a43a2011-07-01 18:55:33 +02001741 start_new_req:
1742 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001743 if (mmc_card_removed(card)) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001744 rqc->rq_flags |= RQF_QUIET;
Seungwon Jeon7a819022013-01-22 19:48:07 +09001745 blk_end_request_all(rqc, -EIO);
1746 } else {
1747 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1748 mmc_start_req(card->host,
1749 &mq->mqrq_cur->mmc_active, NULL);
1750 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001751 }
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return 0;
1754}
1755
Linus Walleij29eb7bd2016-09-20 11:34:38 +02001756int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001757{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001758 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001759 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001760 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001761 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001762
Per Forlinee8a43a2011-07-01 18:55:33 +02001763 if (req && !mq->mqrq_prev->req)
1764 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001765 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001766
Andrei Warkentin371a6892011-04-11 18:10:25 -05001767 ret = mmc_blk_part_switch(card, md);
1768 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001769 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301770 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001771 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001772 ret = 0;
1773 goto out;
1774 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001775
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001776 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001777 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001778 /* complete ongoing async transfer before issuing discard */
1779 if (card->host->areq)
1780 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001781 ret = mmc_blk_issue_discard_rq(mq, req);
1782 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1783 /* complete ongoing async transfer before issuing secure erase*/
1784 if (card->host->areq)
1785 mmc_blk_issue_rw_rq(mq, NULL);
1786 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001787 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001788 /* complete ongoing async transfer before issuing flush */
1789 if (card->host->areq)
1790 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001791 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001792 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001793 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001794 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001795
Andrei Warkentin371a6892011-04-11 18:10:25 -05001796out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001797 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001798 /*
1799 * Release host when there are no more requests
1800 * and after special request(discard, flush) is done.
1801 * In case sepecial request, there is no reentry to
1802 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1803 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001804 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001805 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001806}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Russell Kinga6f6c962006-01-03 22:38:44 +00001808static inline int mmc_blk_readonly(struct mmc_card *card)
1809{
1810 return mmc_card_readonly(card) ||
1811 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1812}
1813
Andrei Warkentin371a6892011-04-11 18:10:25 -05001814static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1815 struct device *parent,
1816 sector_t size,
1817 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001818 const char *subname,
1819 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct mmc_blk_data *md;
1822 int devidx, ret;
1823
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001824again:
1825 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
1826 return ERR_PTR(-ENOMEM);
1827
1828 spin_lock(&mmc_blk_lock);
1829 ret = ida_get_new(&mmc_blk_ida, &devidx);
1830 spin_unlock(&mmc_blk_lock);
1831
1832 if (ret == -EAGAIN)
1833 goto again;
1834 else if (ret)
1835 return ERR_PTR(ret);
1836
1837 if (devidx >= max_devices) {
1838 ret = -ENOSPC;
1839 goto out;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001842 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001843 if (!md) {
1844 ret = -ENOMEM;
1845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001847
Johan Rudholmadd710e2011-12-02 08:51:06 +01001848 md->area_type = area_type;
1849
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001850 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001851 * Set the read-only status based on the supported commands
1852 * and the write protect switch.
1853 */
1854 md->read_only = mmc_blk_readonly(card);
1855
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001856 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001857 if (md->disk == NULL) {
1858 ret = -ENOMEM;
1859 goto err_kfree;
1860 }
1861
1862 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001863 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001864 md->usage = 1;
1865
Adrian Hunterd09408a2011-06-23 13:40:28 +03001866 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001867 if (ret)
1868 goto err_putdisk;
1869
Linus Walleij7db30282016-11-18 13:36:15 +01001870 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001871
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001872 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001873 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001874 md->disk->fops = &mmc_bdops;
1875 md->disk->private_data = md;
1876 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001877 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001878 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001879 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001880 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001881 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001882
1883 /*
1884 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1885 *
1886 * - be set for removable media with permanent block devices
1887 * - be unset for removable block devices with permanent media
1888 *
1889 * Since MMC block devices clearly fall under the second
1890 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1891 * should use the block device creation/destruction hotplug
1892 * messages to tell when the card is present.
1893 */
1894
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001895 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001896 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001897
Saugata Dasa5075eb2012-05-17 16:32:21 +05301898 if (mmc_card_mmc(card))
1899 blk_queue_logical_block_size(md->queue.queue,
1900 card->ext_csd.data_sector_size);
1901 else
1902 blk_queue_logical_block_size(md->queue.queue, 512);
1903
Andrei Warkentin371a6892011-04-11 18:10:25 -05001904 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001905
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001906 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001907 if ((mmc_card_mmc(card) &&
1908 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001909 (mmc_card_sd(card) &&
1910 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1911 md->flags |= MMC_BLK_CMD23;
1912 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001913
1914 if (mmc_card_mmc(card) &&
1915 md->flags & MMC_BLK_CMD23 &&
1916 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1917 card->ext_csd.rel_sectors)) {
1918 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001919 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001920 }
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001923
1924 err_putdisk:
1925 put_disk(md->disk);
1926 err_kfree:
1927 kfree(md);
1928 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001929 spin_lock(&mmc_blk_lock);
1930 ida_remove(&mmc_blk_ida, devidx);
1931 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00001932 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Andrei Warkentin371a6892011-04-11 18:10:25 -05001935static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1936{
1937 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001938
1939 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1940 /*
1941 * The EXT_CSD sector count is in number or 512 byte
1942 * sectors.
1943 */
1944 size = card->ext_csd.sectors;
1945 } else {
1946 /*
1947 * The CSD capacity field is in units of read_blkbits.
1948 * set_capacity takes units of 512 bytes.
1949 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001950 size = (typeof(sector_t))card->csd.capacity
1951 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001952 }
1953
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001954 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001955 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001956}
1957
1958static int mmc_blk_alloc_part(struct mmc_card *card,
1959 struct mmc_blk_data *md,
1960 unsigned int part_type,
1961 sector_t size,
1962 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001963 const char *subname,
1964 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001965{
1966 char cap_str[10];
1967 struct mmc_blk_data *part_md;
1968
1969 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001970 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001971 if (IS_ERR(part_md))
1972 return PTR_ERR(part_md);
1973 part_md->part_type = part_type;
1974 list_add(&part_md->part, &md->part);
1975
James Bottomleyb9f28d82015-03-05 18:47:01 -08001976 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001977 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301978 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001979 part_md->disk->disk_name, mmc_card_id(card),
1980 mmc_card_name(card), part_md->part_type, cap_str);
1981 return 0;
1982}
1983
Namjae Jeone0c368d2011-10-06 23:41:38 +09001984/* MMC Physical partitions consist of two boot partitions and
1985 * up to four general purpose partitions.
1986 * For each partition enabled in EXT_CSD a block device will be allocatedi
1987 * to provide access to the partition.
1988 */
1989
Andrei Warkentin371a6892011-04-11 18:10:25 -05001990static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1991{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001992 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001993
1994 if (!mmc_card_mmc(card))
1995 return 0;
1996
Namjae Jeone0c368d2011-10-06 23:41:38 +09001997 for (idx = 0; idx < card->nr_parts; idx++) {
1998 if (card->part[idx].size) {
1999 ret = mmc_blk_alloc_part(card, md,
2000 card->part[idx].part_cfg,
2001 card->part[idx].size >> 9,
2002 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002003 card->part[idx].name,
2004 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002005 if (ret)
2006 return ret;
2007 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002008 }
2009
2010 return ret;
2011}
2012
Andrei Warkentin371a6892011-04-11 18:10:25 -05002013static void mmc_blk_remove_req(struct mmc_blk_data *md)
2014{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002015 struct mmc_card *card;
2016
Andrei Warkentin371a6892011-04-11 18:10:25 -05002017 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002018 /*
2019 * Flush remaining requests and free queues. It
2020 * is freeing the queue that stops new requests
2021 * from being accepted.
2022 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002023 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002024 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002025 if (md->disk->flags & GENHD_FL_UP) {
2026 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002027 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2028 card->ext_csd.boot_ro_lockable)
2029 device_remove_file(disk_to_dev(md->disk),
2030 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002031
Andrei Warkentin371a6892011-04-11 18:10:25 -05002032 del_gendisk(md->disk);
2033 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002034 mmc_blk_put(md);
2035 }
2036}
2037
2038static void mmc_blk_remove_parts(struct mmc_card *card,
2039 struct mmc_blk_data *md)
2040{
2041 struct list_head *pos, *q;
2042 struct mmc_blk_data *part_md;
2043
2044 list_for_each_safe(pos, q, &md->part) {
2045 part_md = list_entry(pos, struct mmc_blk_data, part);
2046 list_del(pos);
2047 mmc_blk_remove_req(part_md);
2048 }
2049}
2050
2051static int mmc_add_disk(struct mmc_blk_data *md)
2052{
2053 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002054 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002055
Dan Williams307d8e62016-06-20 10:40:44 -07002056 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002057 md->force_ro.show = force_ro_show;
2058 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302059 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060 md->force_ro.attr.name = "force_ro";
2061 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2062 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2063 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002064 goto force_ro_fail;
2065
2066 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2067 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002068 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002069
2070 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2071 mode = S_IRUGO;
2072 else
2073 mode = S_IRUGO | S_IWUSR;
2074
2075 md->power_ro_lock.show = power_ro_lock_show;
2076 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002077 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002078 md->power_ro_lock.attr.mode = mode;
2079 md->power_ro_lock.attr.name =
2080 "ro_lock_until_next_power_on";
2081 ret = device_create_file(disk_to_dev(md->disk),
2082 &md->power_ro_lock);
2083 if (ret)
2084 goto power_ro_lock_fail;
2085 }
2086 return ret;
2087
2088power_ro_lock_fail:
2089 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2090force_ro_fail:
2091 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002092
2093 return ret;
2094}
2095
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002096static const struct mmc_fixup blk_fixups[] =
2097{
Chris Ballc59d4472011-11-11 22:01:43 -05002098 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2099 MMC_QUIRK_INAND_CMD38),
2100 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2101 MMC_QUIRK_INAND_CMD38),
2102 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2103 MMC_QUIRK_INAND_CMD38),
2104 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2105 MMC_QUIRK_INAND_CMD38),
2106 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2107 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002108
2109 /*
2110 * Some MMC cards experience performance degradation with CMD23
2111 * instead of CMD12-bounded multiblock transfers. For now we'll
2112 * black list what's bad...
2113 * - Certain Toshiba cards.
2114 *
2115 * N.B. This doesn't affect SD cards.
2116 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002117 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2118 MMC_QUIRK_BLK_NO_CMD23),
2119 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2120 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002121 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002122 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002123 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002124 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002125 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002126 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002127
2128 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002129 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002130 */
Chris Ballc59d4472011-11-11 22:01:43 -05002131 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002132 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002133 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2134 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002135
Ian Chen3550ccd2012-08-29 15:05:36 +09002136 /*
2137 * On these Samsung MoviNAND parts, performing secure erase or
2138 * secure trim can result in unrecoverable corruption due to a
2139 * firmware bug.
2140 */
2141 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2142 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2143 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2144 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2145 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2146 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2147 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2148 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2149 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2150 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2151 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2152 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2153 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2154 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2155 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2156 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2157
Shawn Linb5b4ff02015-08-12 13:08:32 +08002158 /*
2159 * On Some Kingston eMMCs, performing trim can result in
2160 * unrecoverable data conrruption occasionally due to a firmware bug.
2161 */
2162 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2163 MMC_QUIRK_TRIM_BROKEN),
2164 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2165 MMC_QUIRK_TRIM_BROKEN),
2166
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002167 END_FIXUP
2168};
2169
Ulf Hansson96541ba2015-04-14 13:06:12 +02002170static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002172 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002173 char cap_str[10];
2174
Pierre Ossman912490d2005-05-21 10:27:02 +01002175 /*
2176 * Check that the card supports the command class(es) we need.
2177 */
2178 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return -ENODEV;
2180
Lukas Czerner5204d002014-06-18 13:18:07 +02002181 mmc_fixup_device(card, blk_fixups);
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 md = mmc_blk_alloc(card);
2184 if (IS_ERR(md))
2185 return PTR_ERR(md);
2186
James Bottomleyb9f28d82015-03-05 18:47:01 -08002187 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002188 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302189 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002191 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Andrei Warkentin371a6892011-04-11 18:10:25 -05002193 if (mmc_blk_alloc_parts(card, md))
2194 goto out;
2195
Ulf Hansson96541ba2015-04-14 13:06:12 +02002196 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002197
Andrei Warkentin371a6892011-04-11 18:10:25 -05002198 if (mmc_add_disk(md))
2199 goto out;
2200
2201 list_for_each_entry(part_md, &md->part, part) {
2202 if (mmc_add_disk(part_md))
2203 goto out;
2204 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002205
2206 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2207 pm_runtime_use_autosuspend(&card->dev);
2208
2209 /*
2210 * Don't enable runtime PM for SD-combo cards here. Leave that
2211 * decision to be taken during the SDIO init sequence instead.
2212 */
2213 if (card->type != MMC_TYPE_SD_COMBO) {
2214 pm_runtime_set_active(&card->dev);
2215 pm_runtime_enable(&card->dev);
2216 }
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
2219
2220 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002221 mmc_blk_remove_parts(card, md);
2222 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Ulf Hansson96541ba2015-04-14 13:06:12 +02002226static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002228 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Andrei Warkentin371a6892011-04-11 18:10:25 -05002230 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002231 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002232 mmc_claim_host(card->host);
2233 mmc_blk_part_switch(card, md);
2234 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002235 if (card->type != MMC_TYPE_SD_COMBO)
2236 pm_runtime_disable(&card->dev);
2237 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002238 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002239 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Ulf Hansson96541ba2015-04-14 13:06:12 +02002242static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002245 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (md) {
2248 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002249 list_for_each_entry(part_md, &md->part, part) {
2250 mmc_queue_suspend(&part_md->queue);
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 return 0;
2254}
2255
Ulf Hansson96541ba2015-04-14 13:06:12 +02002256static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002257{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002258 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002259}
2260
Ulf Hansson0967edc2014-10-06 11:29:42 +02002261#ifdef CONFIG_PM_SLEEP
2262static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002263{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002264 struct mmc_card *card = mmc_dev_to_card(dev);
2265
2266 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002267}
2268
Ulf Hansson0967edc2014-10-06 11:29:42 +02002269static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002271 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002272 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002275 /*
2276 * Resume involves the card going into idle state,
2277 * so current partition is always the main one.
2278 */
2279 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002281 list_for_each_entry(part_md, &md->part, part) {
2282 mmc_queue_resume(&part_md->queue);
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 return 0;
2286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287#endif
2288
Ulf Hansson0967edc2014-10-06 11:29:42 +02002289static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2290
Ulf Hansson96541ba2015-04-14 13:06:12 +02002291static struct mmc_driver mmc_driver = {
2292 .drv = {
2293 .name = "mmcblk",
2294 .pm = &mmc_blk_pm_ops,
2295 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 .probe = mmc_blk_probe,
2297 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002298 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299};
2300
2301static int __init mmc_blk_init(void)
2302{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002303 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002305 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2306 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2307
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002308 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002309
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002310 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2311 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002314 res = mmc_register_driver(&mmc_driver);
2315 if (res)
2316 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002318 return 0;
2319 out2:
2320 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 out:
2322 return res;
2323}
2324
2325static void __exit mmc_blk_exit(void)
2326{
2327 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002328 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331module_init(mmc_blk_init);
2332module_exit(mmc_blk_exit);
2333
2334MODULE_LICENSE("GPL");
2335MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2336