blob: fc71b6256d351e6dfcaad114937f98acb7b654cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010050#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010051#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010052#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010054#include "mmc_ops.h"
55#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000057MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040058#ifdef MODULE_PARAM_PREFIX
59#undef MODULE_PARAM_PREFIX
60#endif
61#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010062
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050063#define INAND_CMD38_ARG_EXT_CSD 113
64#define INAND_CMD38_ARG_ERASE 0x00
65#define INAND_CMD38_ARG_TRIM 0x01
66#define INAND_CMD38_ARG_SECERASE 0x80
67#define INAND_CMD38_ARG_SECTRIM1 0x81
68#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060069#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030070#define MMC_SANITIZE_REQ_TIMEOUT 240000
71#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050072
Luca Porziod3df0462015-11-06 15:12:26 +000073#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090074 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020075static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040076
77/*
78 * The defaults come from config options but can be overriden by module
79 * or bootarg options.
80 */
81static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82
83/*
84 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020086 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040087 */
88static int max_devices;
89
Ben Hutchingsa26eba62014-11-06 03:35:09 +000090#define MAX_DEVICES 256
91
Ulf Hanssonb10fa992016-04-07 14:36:46 +020092static DEFINE_IDA(mmc_blk_ida);
93static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * There is one mmc_blk_data per slot.
97 */
98struct mmc_blk_data {
99 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700100 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct gendisk *disk;
102 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500103 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500105 unsigned int flags;
106#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
107#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000110 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300112 unsigned int reset_done;
113#define MMC_BLK_READ BIT(0)
114#define MMC_BLK_WRITE BIT(1)
115#define MMC_BLK_DISCARD BIT(2)
116#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500117
118 /*
119 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200120 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 * track of the current selected device partition.
122 */
123 unsigned int part_curr;
124 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 struct device_attribute power_ro_lock;
126 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Arjan van de Vena621aae2006-01-12 18:43:35 +0000129static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400131module_param(perdev_minors, int, 0444);
132MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
133
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200134static inline int mmc_blk_part_switch(struct mmc_card *card,
135 struct mmc_blk_data *md);
136static int get_card_status(struct mmc_card *card, u32 *status, int retries);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
139{
140 struct mmc_blk_data *md;
141
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 md = disk->private_data;
144 if (md && md->usage == 0)
145 md = NULL;
146 if (md)
147 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000148 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return md;
151}
152
Andrei Warkentin371a6892011-04-11 18:10:25 -0500153static inline int mmc_get_devidx(struct gendisk *disk)
154{
Colin Cross382c55f2015-10-22 10:00:41 -0700155 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500156 return devidx;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void mmc_blk_put(struct mmc_blk_data *md)
160{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 md->usage--;
163 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800165 blk_cleanup_queue(md->queue.queue);
166
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200167 spin_lock(&mmc_blk_lock);
168 ida_remove(&mmc_blk_ida, devidx);
169 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(md);
173 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000174 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Johan Rudholmadd710e2011-12-02 08:51:06 +0100177static ssize_t power_ro_lock_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 int ret;
181 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
182 struct mmc_card *card = md->queue.card;
183 int locked = 0;
184
185 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
186 locked = 2;
187 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
188 locked = 1;
189
190 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
191
Tomas Winkler9098f842015-07-16 15:50:45 +0200192 mmc_blk_put(md);
193
Johan Rudholmadd710e2011-12-02 08:51:06 +0100194 return ret;
195}
196
197static ssize_t power_ro_lock_store(struct device *dev,
198 struct device_attribute *attr, const char *buf, size_t count)
199{
200 int ret;
201 struct mmc_blk_data *md, *part_md;
202 struct mmc_card *card;
203 unsigned long set;
204
205 if (kstrtoul(buf, 0, &set))
206 return -EINVAL;
207
208 if (set != 1)
209 return count;
210
211 md = mmc_blk_get(dev_to_disk(dev));
212 card = md->queue.card;
213
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200214 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100215
216 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
217 card->ext_csd.boot_ro_lock |
218 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
219 card->ext_csd.part_time);
220 if (ret)
221 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
222 else
223 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
224
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200225 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100226
227 if (!ret) {
228 pr_info("%s: Locking boot partition ro until next power on\n",
229 md->disk->disk_name);
230 set_disk_ro(md->disk, 1);
231
232 list_for_each_entry(part_md, &md->part, part)
233 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
234 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
235 set_disk_ro(part_md->disk, 1);
236 }
237 }
238
239 mmc_blk_put(md);
240 return count;
241}
242
Andrei Warkentin371a6892011-04-11 18:10:25 -0500243static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
244 char *buf)
245{
246 int ret;
247 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
248
Baruch Siach0031a982014-09-22 10:12:51 +0300249 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500250 get_disk_ro(dev_to_disk(dev)) ^
251 md->read_only);
252 mmc_blk_put(md);
253 return ret;
254}
255
256static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
258{
259 int ret;
260 char *end;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262 unsigned long set = simple_strtoul(buf, &end, 0);
263 if (end == buf) {
264 ret = -EINVAL;
265 goto out;
266 }
267
268 set_disk_ro(dev_to_disk(dev), set || md->read_only);
269 ret = count;
270out:
271 mmc_blk_put(md);
272 return ret;
273}
274
Al Viroa5a15612008-03-02 10:33:30 -0500275static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Al Viroa5a15612008-03-02 10:33:30 -0500277 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int ret = -ENXIO;
279
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200280 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (md) {
282 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500283 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700285
Al Viroa5a15612008-03-02 10:33:30 -0500286 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700287 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700288 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200291 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return ret;
294}
295
Al Virodb2a1442013-05-05 21:52:57 -0400296static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Al Viroa5a15612008-03-02 10:33:30 -0500298 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200300 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200302 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800306mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800308 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
309 geo->heads = 4;
310 geo->sectors = 16;
311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
John Calixtocb87ea22011-04-26 18:56:29 -0400314struct mmc_blk_ioc_data {
315 struct mmc_ioc_cmd ic;
316 unsigned char *buf;
317 u64 buf_bytes;
318};
319
320static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
321 struct mmc_ioc_cmd __user *user)
322{
323 struct mmc_blk_ioc_data *idata;
324 int err;
325
yalin wang1ff89502015-11-12 19:27:11 +0800326 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400327 if (!idata) {
328 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400329 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400330 }
331
332 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
333 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400334 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400335 }
336
337 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
338 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
339 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400340 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400341 }
342
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300343 if (!idata->buf_bytes) {
344 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100345 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300346 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100347
yalin wang1ff89502015-11-12 19:27:11 +0800348 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400349 if (!idata->buf) {
350 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400351 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400352 }
353
354 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
355 idata->ic.data_ptr, idata->buf_bytes)) {
356 err = -EFAULT;
357 goto copy_err;
358 }
359
360 return idata;
361
362copy_err:
363 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400364idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400365 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400366out:
John Calixtocb87ea22011-04-26 18:56:29 -0400367 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400368}
369
Jon Huntera5f57742015-09-22 10:27:53 +0100370static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
371 struct mmc_blk_ioc_data *idata)
372{
373 struct mmc_ioc_cmd *ic = &idata->ic;
374
375 if (copy_to_user(&(ic_ptr->response), ic->response,
376 sizeof(ic->response)))
377 return -EFAULT;
378
379 if (!idata->ic.write_flag) {
380 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
381 idata->buf, idata->buf_bytes))
382 return -EFAULT;
383 }
384
385 return 0;
386}
387
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200388static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
389 u32 retries_max)
390{
391 int err;
392 u32 retry_count = 0;
393
394 if (!status || !retries_max)
395 return -EINVAL;
396
397 do {
398 err = get_card_status(card, status, 5);
399 if (err)
400 break;
401
402 if (!R1_STATUS(*status) &&
403 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
404 break; /* RPMB programming operation complete */
405
406 /*
407 * Rechedule to give the MMC device a chance to continue
408 * processing the previous command without being polled too
409 * frequently.
410 */
411 usleep_range(1000, 5000);
412 } while (++retry_count < retries_max);
413
414 if (retry_count == retries_max)
415 err = -EPERM;
416
417 return err;
418}
419
Maya Erez775a9362013-04-18 15:41:55 +0300420static int ioctl_do_sanitize(struct mmc_card *card)
421{
422 int err;
423
Ulf Hanssona2d10862013-12-16 14:37:26 +0100424 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300425 pr_warn("%s: %s - SANITIZE is not supported\n",
426 mmc_hostname(card->host), __func__);
427 err = -EOPNOTSUPP;
428 goto out;
429 }
430
431 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
432 mmc_hostname(card->host), __func__);
433
434 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
435 EXT_CSD_SANITIZE_START, 1,
436 MMC_SANITIZE_REQ_TIMEOUT);
437
438 if (err)
439 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
440 mmc_hostname(card->host), __func__, err);
441
442 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
443 __func__);
444out:
445 return err;
446}
447
Jon Huntera5f57742015-09-22 10:27:53 +0100448static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
449 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400450{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900451 struct mmc_command cmd = {};
452 struct mmc_data data = {};
453 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400454 struct scatterlist sg;
455 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200456 int is_rpmb = false;
457 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400458
Jon Huntera5f57742015-09-22 10:27:53 +0100459 if (!card || !md || !idata)
460 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400461
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200462 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
463 is_rpmb = true;
464
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100465 cmd.opcode = idata->ic.opcode;
466 cmd.arg = idata->ic.arg;
467 cmd.flags = idata->ic.flags;
468
469 if (idata->buf_bytes) {
470 data.sg = &sg;
471 data.sg_len = 1;
472 data.blksz = idata->ic.blksz;
473 data.blocks = idata->ic.blocks;
474
475 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
476
477 if (idata->ic.write_flag)
478 data.flags = MMC_DATA_WRITE;
479 else
480 data.flags = MMC_DATA_READ;
481
482 /* data.flags must already be set before doing this. */
483 mmc_set_data_timeout(&data, card);
484
485 /* Allow overriding the timeout_ns for empirical tuning. */
486 if (idata->ic.data_timeout_ns)
487 data.timeout_ns = idata->ic.data_timeout_ns;
488
489 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
490 /*
491 * Pretend this is a data transfer and rely on the
492 * host driver to compute timeout. When all host
493 * drivers support cmd.cmd_timeout for R1B, this
494 * can be changed to:
495 *
496 * mrq.data = NULL;
497 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
498 */
499 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
500 }
501
502 mrq.data = &data;
503 }
504
505 mrq.cmd = &cmd;
506
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507 err = mmc_blk_part_switch(card, md);
508 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100509 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200510
John Calixtocb87ea22011-04-26 18:56:29 -0400511 if (idata->ic.is_acmd) {
512 err = mmc_app_cmd(card->host, card);
513 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100514 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400515 }
516
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200517 if (is_rpmb) {
518 err = mmc_set_blockcount(card, data.blocks,
519 idata->ic.write_flag & (1 << 31));
520 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100521 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200522 }
523
Yaniv Gardia82e4842013-06-05 14:13:08 +0300524 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
525 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300526 err = ioctl_do_sanitize(card);
527
528 if (err)
529 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
530 __func__, err);
531
Jon Huntera5f57742015-09-22 10:27:53 +0100532 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300533 }
534
John Calixtocb87ea22011-04-26 18:56:29 -0400535 mmc_wait_for_req(card->host, &mrq);
536
537 if (cmd.error) {
538 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
539 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100540 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400541 }
542 if (data.error) {
543 dev_err(mmc_dev(card->host), "%s: data error %d\n",
544 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100545 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400546 }
547
548 /*
549 * According to the SD specs, some commands require a delay after
550 * issuing the command.
551 */
552 if (idata->ic.postsleep_min_us)
553 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
554
Jon Huntera5f57742015-09-22 10:27:53 +0100555 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400556
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200557 if (is_rpmb) {
558 /*
559 * Ensure RPMB command has completed by polling CMD13
560 * "Send Status".
561 */
562 err = ioctl_rpmb_card_status_poll(card, &status, 5);
563 if (err)
564 dev_err(mmc_dev(card->host),
565 "%s: Card Status=0x%08X, error %d\n",
566 __func__, status, err);
567 }
568
Jon Huntera5f57742015-09-22 10:27:53 +0100569 return err;
570}
571
572static int mmc_blk_ioctl_cmd(struct block_device *bdev,
573 struct mmc_ioc_cmd __user *ic_ptr)
574{
575 struct mmc_blk_ioc_data *idata;
576 struct mmc_blk_data *md;
577 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700578 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100579
Shawn Lin83c742c2016-03-16 18:15:47 +0800580 /*
581 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
582 * whole block device, not on a partition. This prevents overspray
583 * between sibling partitions.
584 */
585 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
586 return -EPERM;
587
Jon Huntera5f57742015-09-22 10:27:53 +0100588 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
589 if (IS_ERR(idata))
590 return PTR_ERR(idata);
591
592 md = mmc_blk_get(bdev->bd_disk);
593 if (!md) {
594 err = -EINVAL;
595 goto cmd_err;
596 }
597
598 card = md->queue.card;
599 if (IS_ERR(card)) {
600 err = PTR_ERR(card);
601 goto cmd_done;
602 }
603
604 mmc_get_card(card);
605
Grant Grundlerb0934102015-09-23 18:30:33 -0700606 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100607
Adrian Hunter3c866562016-05-04 14:38:12 +0300608 /* Always switch back to main area after RPMB access */
609 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
610 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
611
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200612 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400613
Grant Grundlerb0934102015-09-23 18:30:33 -0700614 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100615
John Calixtocb87ea22011-04-26 18:56:29 -0400616cmd_done:
617 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300618cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400619 kfree(idata->buf);
620 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700621 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400622}
623
Jon Huntera5f57742015-09-22 10:27:53 +0100624static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
625 struct mmc_ioc_multi_cmd __user *user)
626{
627 struct mmc_blk_ioc_data **idata = NULL;
628 struct mmc_ioc_cmd __user *cmds = user->cmds;
629 struct mmc_card *card;
630 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700631 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100632 __u64 num_of_cmds;
633
Shawn Lin83c742c2016-03-16 18:15:47 +0800634 /*
635 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
636 * whole block device, not on a partition. This prevents overspray
637 * between sibling partitions.
638 */
639 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
640 return -EPERM;
641
Jon Huntera5f57742015-09-22 10:27:53 +0100642 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
643 sizeof(num_of_cmds)))
644 return -EFAULT;
645
646 if (num_of_cmds > MMC_IOC_MAX_CMDS)
647 return -EINVAL;
648
649 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
650 if (!idata)
651 return -ENOMEM;
652
653 for (i = 0; i < num_of_cmds; i++) {
654 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
655 if (IS_ERR(idata[i])) {
656 err = PTR_ERR(idata[i]);
657 num_of_cmds = i;
658 goto cmd_err;
659 }
660 }
661
662 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800663 if (!md) {
664 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100665 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800666 }
Jon Huntera5f57742015-09-22 10:27:53 +0100667
668 card = md->queue.card;
669 if (IS_ERR(card)) {
670 err = PTR_ERR(card);
671 goto cmd_done;
672 }
673
674 mmc_get_card(card);
675
Grant Grundlerb0934102015-09-23 18:30:33 -0700676 for (i = 0; i < num_of_cmds && !ioc_err; i++)
677 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100678
Adrian Hunter3c866562016-05-04 14:38:12 +0300679 /* Always switch back to main area after RPMB access */
680 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
681 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
682
Jon Huntera5f57742015-09-22 10:27:53 +0100683 mmc_put_card(card);
684
685 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700686 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100687 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100688
689cmd_done:
690 mmc_blk_put(md);
691cmd_err:
692 for (i = 0; i < num_of_cmds; i++) {
693 kfree(idata[i]->buf);
694 kfree(idata[i]);
695 }
696 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700697 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100698}
699
John Calixtocb87ea22011-04-26 18:56:29 -0400700static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
701 unsigned int cmd, unsigned long arg)
702{
Jon Huntera5f57742015-09-22 10:27:53 +0100703 switch (cmd) {
704 case MMC_IOC_CMD:
705 return mmc_blk_ioctl_cmd(bdev,
706 (struct mmc_ioc_cmd __user *)arg);
707 case MMC_IOC_MULTI_CMD:
708 return mmc_blk_ioctl_multi_cmd(bdev,
709 (struct mmc_ioc_multi_cmd __user *)arg);
710 default:
711 return -EINVAL;
712 }
John Calixtocb87ea22011-04-26 18:56:29 -0400713}
714
715#ifdef CONFIG_COMPAT
716static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
717 unsigned int cmd, unsigned long arg)
718{
719 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
720}
721#endif
722
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700723static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500724 .open = mmc_blk_open,
725 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800726 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400728 .ioctl = mmc_blk_ioctl,
729#ifdef CONFIG_COMPAT
730 .compat_ioctl = mmc_blk_compat_ioctl,
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Andrei Warkentin371a6892011-04-11 18:10:25 -0500734static inline int mmc_blk_part_switch(struct mmc_card *card,
735 struct mmc_blk_data *md)
736{
737 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200738 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300739
Andrei Warkentin371a6892011-04-11 18:10:25 -0500740 if (main_md->part_curr == md->part_type)
741 return 0;
742
743 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300744 u8 part_config = card->ext_csd.part_config;
745
Adrian Hunter57da0c02016-05-04 14:38:13 +0300746 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
747 mmc_retune_pause(card->host);
748
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300749 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
750 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500751
752 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300753 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500754 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300755 if (ret) {
756 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
757 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500758 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300759 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300760
761 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300762
763 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
764 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300765 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500766
767 main_md->part_curr = md->part_type;
768 return 0;
769}
770
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700771static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
772{
773 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100774 u32 result;
775 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700776
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900777 struct mmc_request mrq = {};
778 struct mmc_command cmd = {};
779 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700780
781 struct scatterlist sg;
782
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783 cmd.opcode = MMC_APP_CMD;
784 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700785 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700786
787 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700788 if (err)
789 return (u32)-1;
790 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700791 return (u32)-1;
792
793 memset(&cmd, 0, sizeof(struct mmc_command));
794
795 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
796 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700797 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700798
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700799 data.blksz = 4;
800 data.blocks = 1;
801 data.flags = MMC_DATA_READ;
802 data.sg = &sg;
803 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530804 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700805
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700806 mrq.cmd = &cmd;
807 mrq.data = &data;
808
Ben Dooks051913d2009-06-08 23:33:57 +0100809 blocks = kmalloc(4, GFP_KERNEL);
810 if (!blocks)
811 return (u32)-1;
812
813 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700814
815 mmc_wait_for_req(card->host, &mrq);
816
Ben Dooks051913d2009-06-08 23:33:57 +0100817 result = ntohl(*blocks);
818 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819
Ben Dooks051913d2009-06-08 23:33:57 +0100820 if (cmd.error || data.error)
821 result = (u32)-1;
822
823 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700824}
825
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100826static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300827{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900828 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300829 int err;
830
Adrian Hunter504f1912008-10-16 12:55:25 +0300831 cmd.opcode = MMC_SEND_STATUS;
832 if (!mmc_host_is_spi(card->host))
833 cmd.arg = card->rca << 16;
834 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100835 err = mmc_wait_for_cmd(card->host, &cmd, retries);
836 if (err == 0)
837 *status = cmd.resp[0];
838 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300839}
840
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100841static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100842 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100843{
844 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
845 int err = 0;
846 u32 status;
847
848 do {
849 err = get_card_status(card, &status, 5);
850 if (err) {
851 pr_err("%s: error %d requesting status\n",
852 req->rq_disk->disk_name, err);
853 return err;
854 }
855
856 if (status & R1_ERROR) {
857 pr_err("%s: %s: error sending status cmd, status %#x\n",
858 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100859 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100860 }
861
Ulf Hansson95a91292014-01-29 13:11:27 +0100862 /* We may rely on the host hw to handle busy detection.*/
863 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
864 hw_busy_detect)
865 break;
866
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100867 /*
868 * Timeout if the device never becomes ready for data and never
869 * leaves the program state.
870 */
871 if (time_after(jiffies, timeout)) {
872 pr_err("%s: Card stuck in programming state! %s %s\n",
873 mmc_hostname(card->host),
874 req->rq_disk->disk_name, __func__);
875 return -ETIMEDOUT;
876 }
877
878 /*
879 * Some cards mishandle the status bits,
880 * so make sure to check both the busy
881 * indication and the card state.
882 */
883 } while (!(status & R1_READY_FOR_DATA) ||
884 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
885
886 return err;
887}
888
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100889static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100890 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100891{
892 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900893 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100894 int err;
895 bool use_r1b_resp = rq_data_dir(req) == WRITE;
896
897 /*
898 * Normally we use R1B responses for WRITE, but in cases where the host
899 * has specified a max_busy_timeout we need to validate it. A failure
900 * means we need to prevent the host from doing hw busy detection, which
901 * is done by converting to a R1 response instead.
902 */
903 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
904 use_r1b_resp = false;
905
906 cmd.opcode = MMC_STOP_TRANSMISSION;
907 if (use_r1b_resp) {
908 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
909 cmd.busy_timeout = timeout_ms;
910 } else {
911 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
912 }
913
914 err = mmc_wait_for_cmd(host, &cmd, 5);
915 if (err)
916 return err;
917
918 *stop_status = cmd.resp[0];
919
920 /* No need to check card status in case of READ. */
921 if (rq_data_dir(req) == READ)
922 return 0;
923
924 if (!mmc_host_is_spi(host) &&
925 (*stop_status & R1_ERROR)) {
926 pr_err("%s: %s: general error sending stop command, resp %#x\n",
927 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100928 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100929 }
930
931 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
932}
933
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530934#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100935#define ERR_RETRY 2
936#define ERR_ABORT 1
937#define ERR_CONTINUE 0
938
939static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
940 bool status_valid, u32 status)
941{
942 switch (error) {
943 case -EILSEQ:
944 /* response crc error, retry the r/w cmd */
945 pr_err("%s: %s sending %s command, card status %#x\n",
946 req->rq_disk->disk_name, "response CRC error",
947 name, status);
948 return ERR_RETRY;
949
950 case -ETIMEDOUT:
951 pr_err("%s: %s sending %s command, card status %#x\n",
952 req->rq_disk->disk_name, "timed out", name, status);
953
954 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530955 if (!status_valid) {
956 pr_err("%s: status not valid, retrying timeout\n",
957 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100958 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530959 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100960
961 /*
962 * If it was a r/w cmd crc error, or illegal command
963 * (eg, issued in wrong state) then retry - we should
964 * have corrected the state problem above.
965 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530966 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
967 pr_err("%s: command error, retrying timeout\n",
968 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100969 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530970 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100971
972 /* Otherwise abort the command */
973 return ERR_ABORT;
974
975 default:
976 /* We don't understand the error code the driver gave us */
977 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
978 req->rq_disk->disk_name, error, status);
979 return ERR_ABORT;
980 }
981}
982
983/*
984 * Initial r/w and stop cmd error recovery.
985 * We don't know whether the card received the r/w cmd or not, so try to
986 * restore things back to a sane state. Essentially, we do this as follows:
987 * - Obtain card status. If the first attempt to obtain card status fails,
988 * the status word will reflect the failed status cmd, not the failed
989 * r/w cmd. If we fail to obtain card status, it suggests we can no
990 * longer communicate with the card.
991 * - Check the card state. If the card received the cmd but there was a
992 * transient problem with the response, it might still be in a data transfer
993 * mode. Try to send it a stop command. If this fails, we can't recover.
994 * - If the r/w cmd failed due to a response CRC error, it was probably
995 * transient, so retry the cmd.
996 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
997 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
998 * illegal cmd, retry.
999 * Otherwise we don't understand what happened, so abort.
1000 */
1001static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +01001002 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001003{
1004 bool prev_cmd_status_valid = true;
1005 u32 status, stop_status = 0;
1006 int err, retry;
1007
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301008 if (mmc_card_removed(card))
1009 return ERR_NOMEDIUM;
1010
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001011 /*
1012 * Try to get card status which indicates both the card state
1013 * and why there was no response. If the first attempt fails,
1014 * we can't be sure the returned status is for the r/w command.
1015 */
1016 for (retry = 2; retry >= 0; retry--) {
1017 err = get_card_status(card, &status, 0);
1018 if (!err)
1019 break;
1020
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001021 /* Re-tune if needed */
1022 mmc_retune_recheck(card->host);
1023
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001024 prev_cmd_status_valid = false;
1025 pr_err("%s: error %d sending status command, %sing\n",
1026 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1027 }
1028
1029 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301030 if (err) {
1031 /* Check if the card is removed */
1032 if (mmc_detect_card_removed(card->host))
1033 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001034 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301035 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001036
Adrian Hunter67716322011-08-29 16:42:15 +03001037 /* Flag ECC errors */
1038 if ((status & R1_CARD_ECC_FAILED) ||
1039 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1040 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001041 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001042
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001043 /* Flag General errors */
1044 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1045 if ((status & R1_ERROR) ||
1046 (brq->stop.resp[0] & R1_ERROR)) {
1047 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1048 req->rq_disk->disk_name, __func__,
1049 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001050 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001051 }
1052
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053 /*
1054 * Check the current card state. If it is in some data transfer
1055 * mode, tell it to stop (and hopefully transition back to TRAN.)
1056 */
1057 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1058 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001059 err = send_stop(card,
1060 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1061 req, gen_err, &stop_status);
1062 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001063 pr_err("%s: error %d sending stop command\n",
1064 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001065 /*
1066 * If the stop cmd also timed out, the card is probably
1067 * not present, so abort. Other errors are bad news too.
1068 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001070 }
1071
Adrian Hunter67716322011-08-29 16:42:15 +03001072 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001073 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001074 }
1075
1076 /* Check for set block count errors */
1077 if (brq->sbc.error)
1078 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1079 prev_cmd_status_valid, status);
1080
1081 /* Check for r/w command errors */
1082 if (brq->cmd.error)
1083 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1084 prev_cmd_status_valid, status);
1085
Adrian Hunter67716322011-08-29 16:42:15 +03001086 /* Data errors */
1087 if (!brq->stop.error)
1088 return ERR_CONTINUE;
1089
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001090 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001091 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 +01001092 req->rq_disk->disk_name, brq->stop.error,
1093 brq->cmd.resp[0], status);
1094
1095 /*
1096 * Subsitute in our own stop status as this will give the error
1097 * state which happened during the execution of the r/w command.
1098 */
1099 if (stop_status) {
1100 brq->stop.resp[0] = stop_status;
1101 brq->stop.error = 0;
1102 }
1103 return ERR_CONTINUE;
1104}
1105
Adrian Hunter67716322011-08-29 16:42:15 +03001106static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1107 int type)
1108{
1109 int err;
1110
1111 if (md->reset_done & type)
1112 return -EEXIST;
1113
1114 md->reset_done |= type;
1115 err = mmc_hw_reset(host);
1116 /* Ensure we switch back to the correct partition */
1117 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001118 struct mmc_blk_data *main_md =
1119 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001120 int part_err;
1121
1122 main_md->part_curr = main_md->part_type;
1123 part_err = mmc_blk_part_switch(host->card, md);
1124 if (part_err) {
1125 /*
1126 * We have failed to get back into the correct
1127 * partition, so we need to abort the whole request.
1128 */
1129 return -ENODEV;
1130 }
1131 }
1132 return err;
1133}
1134
1135static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1136{
1137 md->reset_done &= ~type;
1138}
1139
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001140int mmc_access_rpmb(struct mmc_queue *mq)
1141{
Linus Walleij7db30282016-11-18 13:36:15 +01001142 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001143 /*
1144 * If this is a RPMB partition access, return ture
1145 */
1146 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1147 return true;
1148
1149 return false;
1150}
1151
Adrian Hunterbd788c92010-08-11 14:17:47 -07001152static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1153{
Linus Walleij7db30282016-11-18 13:36:15 +01001154 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001155 struct mmc_card *card = md->queue.card;
1156 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001157 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001158
Adrian Hunterbd788c92010-08-11 14:17:47 -07001159 if (!mmc_can_erase(card)) {
1160 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001161 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001162 }
1163
1164 from = blk_rq_pos(req);
1165 nr = blk_rq_sectors(req);
1166
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001167 if (mmc_can_discard(card))
1168 arg = MMC_DISCARD_ARG;
1169 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001170 arg = MMC_TRIM_ARG;
1171 else
1172 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001173 do {
1174 err = 0;
1175 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1176 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1177 INAND_CMD38_ARG_EXT_CSD,
1178 arg == MMC_TRIM_ARG ?
1179 INAND_CMD38_ARG_TRIM :
1180 INAND_CMD38_ARG_ERASE,
1181 0);
1182 }
1183 if (!err)
1184 err = mmc_erase(card, from, nr, arg);
1185 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001186 if (!err)
1187 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001188fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301189 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001190
Adrian Hunterbd788c92010-08-11 14:17:47 -07001191 return err ? 0 : 1;
1192}
1193
Adrian Hunter49804542010-08-11 14:17:50 -07001194static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1195 struct request *req)
1196{
Linus Walleij7db30282016-11-18 13:36:15 +01001197 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001198 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001199 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001200 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001201
Maya Erez775a9362013-04-18 15:41:55 +03001202 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001203 err = -EOPNOTSUPP;
1204 goto out;
1205 }
1206
1207 from = blk_rq_pos(req);
1208 nr = blk_rq_sectors(req);
1209
Maya Erez775a9362013-04-18 15:41:55 +03001210 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1211 arg = MMC_SECURE_TRIM1_ARG;
1212 else
1213 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001214
Adrian Hunter67716322011-08-29 16:42:15 +03001215retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001216 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1217 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1218 INAND_CMD38_ARG_EXT_CSD,
1219 arg == MMC_SECURE_TRIM1_ARG ?
1220 INAND_CMD38_ARG_SECTRIM1 :
1221 INAND_CMD38_ARG_SECERASE,
1222 0);
1223 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001224 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001225 }
Adrian Hunter28302812012-04-05 14:45:48 +03001226
Adrian Hunter49804542010-08-11 14:17:50 -07001227 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001228 if (err == -EIO)
1229 goto out_retry;
1230 if (err)
1231 goto out;
1232
1233 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001234 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1235 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1236 INAND_CMD38_ARG_EXT_CSD,
1237 INAND_CMD38_ARG_SECTRIM2,
1238 0);
1239 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001240 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001241 }
Adrian Hunter28302812012-04-05 14:45:48 +03001242
Adrian Hunter49804542010-08-11 14:17:50 -07001243 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001244 if (err == -EIO)
1245 goto out_retry;
1246 if (err)
1247 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001248 }
Adrian Hunter28302812012-04-05 14:45:48 +03001249
Adrian Hunter28302812012-04-05 14:45:48 +03001250out_retry:
1251 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001252 goto retry;
1253 if (!err)
1254 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001255out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301256 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001257
Adrian Hunter49804542010-08-11 14:17:50 -07001258 return err ? 0 : 1;
1259}
1260
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001261static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1262{
Linus Walleij7db30282016-11-18 13:36:15 +01001263 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001264 struct mmc_card *card = md->queue.card;
1265 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001266
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001267 ret = mmc_flush_cache(card);
1268 if (ret)
1269 ret = -EIO;
1270
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301271 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001272
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001273 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001274}
1275
1276/*
1277 * Reformat current write as a reliable write, supporting
1278 * both legacy and the enhanced reliable write MMC cards.
1279 * In each transfer we'll handle only as much as a single
1280 * reliable write can handle, thus finish the request in
1281 * partial completions.
1282 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001283static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1284 struct mmc_card *card,
1285 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001286{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001287 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1288 /* Legacy mode imposes restrictions on transfers. */
1289 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1290 brq->data.blocks = 1;
1291
1292 if (brq->data.blocks > card->ext_csd.rel_sectors)
1293 brq->data.blocks = card->ext_csd.rel_sectors;
1294 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1295 brq->data.blocks = 1;
1296 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001297}
1298
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001299#define CMD_ERRORS \
1300 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1301 R1_ADDRESS_ERROR | /* Misaligned address */ \
1302 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1303 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1304 R1_CC_ERROR | /* Card controller error */ \
1305 R1_ERROR) /* General/unknown error */
1306
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001307static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1308 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001309{
Per Forlinee8a43a2011-07-01 18:55:33 +02001310 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1311 mmc_active);
1312 struct mmc_blk_request *brq = &mq_mrq->brq;
1313 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001314 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001315 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001316 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001317
1318 /*
1319 * sbc.error indicates a problem with the set block count
1320 * command. No data will have been transferred.
1321 *
1322 * cmd.error indicates a problem with the r/w command. No
1323 * data will have been transferred.
1324 *
1325 * stop.error indicates a problem with the stop command. Data
1326 * may have been transferred, or may still be transferring.
1327 */
Adrian Hunter67716322011-08-29 16:42:15 +03001328 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1329 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001330 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001331 case ERR_RETRY:
1332 return MMC_BLK_RETRY;
1333 case ERR_ABORT:
1334 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301335 case ERR_NOMEDIUM:
1336 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001337 case ERR_CONTINUE:
1338 break;
1339 }
1340 }
1341
1342 /*
1343 * Check for errors relating to the execution of the
1344 * initial command - such as address errors. No data
1345 * has been transferred.
1346 */
1347 if (brq->cmd.resp[0] & CMD_ERRORS) {
1348 pr_err("%s: r/w command failed, status = %#x\n",
1349 req->rq_disk->disk_name, brq->cmd.resp[0]);
1350 return MMC_BLK_ABORT;
1351 }
1352
1353 /*
1354 * Everything else is either success, or a data error of some
1355 * kind. If it was a write, we may have transitioned to
1356 * program mode, which we have to wait for it to complete.
1357 */
1358 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001359 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001360
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001361 /* Check stop command response */
1362 if (brq->stop.resp[0] & R1_ERROR) {
1363 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1364 req->rq_disk->disk_name, __func__,
1365 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001366 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001367 }
1368
Ulf Hansson95a91292014-01-29 13:11:27 +01001369 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1370 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001371 if (err)
1372 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001373 }
1374
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001375 /* if general error occurs, retry the write operation. */
1376 if (gen_err) {
1377 pr_warn("%s: retrying write for general error\n",
1378 req->rq_disk->disk_name);
1379 return MMC_BLK_RETRY;
1380 }
1381
Per Forlind78d4a8a2011-07-01 18:55:30 +02001382 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001383 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001384 pr_debug("%s: retrying because a re-tune was needed\n",
1385 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001386 brq->retune_retry_done = 1;
1387 return MMC_BLK_RETRY;
1388 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001389 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1390 req->rq_disk->disk_name, brq->data.error,
1391 (unsigned)blk_rq_pos(req),
1392 (unsigned)blk_rq_sectors(req),
1393 brq->cmd.resp[0], brq->stop.resp[0]);
1394
1395 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001396 if (ecc_err)
1397 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001398 return MMC_BLK_DATA_ERR;
1399 } else {
1400 return MMC_BLK_CMD_ERR;
1401 }
1402 }
1403
Adrian Hunter67716322011-08-29 16:42:15 +03001404 if (!brq->data.bytes_xfered)
1405 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001406
Adrian Hunter67716322011-08-29 16:42:15 +03001407 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1408 return MMC_BLK_PARTIAL;
1409
1410 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001411}
1412
Per Forlin54d49d72011-07-01 18:55:29 +02001413static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1414 struct mmc_card *card,
1415 int disable_multi,
1416 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Per Forlin54d49d72011-07-01 18:55:29 +02001418 u32 readcmd, writecmd;
1419 struct mmc_blk_request *brq = &mqrq->brq;
1420 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001421 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301422 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001424 /*
1425 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001426 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001427 */
Luca Porziod3df0462015-11-06 15:12:26 +00001428 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001429 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001430 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001431
Per Forlin54d49d72011-07-01 18:55:29 +02001432 memset(brq, 0, sizeof(struct mmc_blk_request));
1433 brq->mrq.cmd = &brq->cmd;
1434 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Per Forlin54d49d72011-07-01 18:55:29 +02001436 brq->cmd.arg = blk_rq_pos(req);
1437 if (!mmc_card_blockaddr(card))
1438 brq->cmd.arg <<= 9;
1439 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1440 brq->data.blksz = 512;
1441 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1442 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001443 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Per Forlin54d49d72011-07-01 18:55:29 +02001445 /*
1446 * The block layer doesn't support all sector count
1447 * restrictions, so we need to be prepared for too big
1448 * requests.
1449 */
1450 if (brq->data.blocks > card->host->max_blk_count)
1451 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001453 if (brq->data.blocks > 1) {
1454 /*
1455 * After a read error, we redo the request one sector
1456 * at a time in order to accurately determine which
1457 * sectors can be read successfully.
1458 */
1459 if (disable_multi)
1460 brq->data.blocks = 1;
1461
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001462 /*
1463 * Some controllers have HW issues while operating
1464 * in multiple I/O mode
1465 */
1466 if (card->host->ops->multi_io_quirk)
1467 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1468 (rq_data_dir(req) == READ) ?
1469 MMC_DATA_READ : MMC_DATA_WRITE,
1470 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001471 }
Per Forlin54d49d72011-07-01 18:55:29 +02001472
1473 if (brq->data.blocks > 1 || do_rel_wr) {
1474 /* SPI multiblock writes terminate using a special
1475 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001476 */
Per Forlin54d49d72011-07-01 18:55:29 +02001477 if (!mmc_host_is_spi(card->host) ||
1478 rq_data_dir(req) == READ)
1479 brq->mrq.stop = &brq->stop;
1480 readcmd = MMC_READ_MULTIPLE_BLOCK;
1481 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1482 } else {
1483 brq->mrq.stop = NULL;
1484 readcmd = MMC_READ_SINGLE_BLOCK;
1485 writecmd = MMC_WRITE_BLOCK;
1486 }
1487 if (rq_data_dir(req) == READ) {
1488 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001489 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001490 if (brq->mrq.stop)
1491 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1492 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001493 } else {
1494 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001495 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001496 if (brq->mrq.stop)
1497 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1498 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001499 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001500
Per Forlin54d49d72011-07-01 18:55:29 +02001501 if (do_rel_wr)
1502 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001503
Per Forlin54d49d72011-07-01 18:55:29 +02001504 /*
Saugata Das42659002011-12-21 13:09:17 +05301505 * Data tag is used only during writing meta data to speed
1506 * up write and any subsequent read of this meta data
1507 */
1508 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1509 (req->cmd_flags & REQ_META) &&
1510 (rq_data_dir(req) == WRITE) &&
1511 ((brq->data.blocks * brq->data.blksz) >=
1512 card->ext_csd.data_tag_unit_size);
1513
1514 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001515 * Pre-defined multi-block transfers are preferable to
1516 * open ended-ones (and necessary for reliable writes).
1517 * However, it is not sufficient to just send CMD23,
1518 * and avoid the final CMD12, as on an error condition
1519 * CMD12 (stop) needs to be sent anyway. This, coupled
1520 * with Auto-CMD23 enhancements provided by some
1521 * hosts, means that the complexity of dealing
1522 * with this is best left to the host. If CMD23 is
1523 * supported by card and host, we'll fill sbc in and let
1524 * the host deal with handling it correctly. This means
1525 * that for hosts that don't expose MMC_CAP_CMD23, no
1526 * change of behavior will be observed.
1527 *
1528 * N.B: Some MMC cards experience perf degradation.
1529 * We'll avoid using CMD23-bounded multiblock writes for
1530 * these, while retaining features like reliable writes.
1531 */
Saugata Das42659002011-12-21 13:09:17 +05301532 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1533 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1534 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001535 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1536 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301537 (do_rel_wr ? (1 << 31) : 0) |
1538 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001539 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1540 brq->mrq.sbc = &brq->sbc;
1541 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001542
Per Forlin54d49d72011-07-01 18:55:29 +02001543 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001544
Per Forlin54d49d72011-07-01 18:55:29 +02001545 brq->data.sg = mqrq->sg;
1546 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001547
Per Forlin54d49d72011-07-01 18:55:29 +02001548 /*
1549 * Adjust the sg list so it is the same size as the
1550 * request.
1551 */
1552 if (brq->data.blocks != blk_rq_sectors(req)) {
1553 int i, data_size = brq->data.blocks << 9;
1554 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001555
Per Forlin54d49d72011-07-01 18:55:29 +02001556 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1557 data_size -= sg->length;
1558 if (data_size <= 0) {
1559 sg->length += data_size;
1560 i++;
1561 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001562 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001563 }
Per Forlin54d49d72011-07-01 18:55:29 +02001564 brq->data.sg_len = i;
1565 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001566
Per Forlinee8a43a2011-07-01 18:55:33 +02001567 mqrq->mmc_active.mrq = &brq->mrq;
1568 mqrq->mmc_active.err_check = mmc_blk_err_check;
1569
Per Forlin54d49d72011-07-01 18:55:29 +02001570 mmc_queue_bounce_pre(mqrq);
1571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Adrian Hunter67716322011-08-29 16:42:15 +03001573static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1574 struct mmc_blk_request *brq, struct request *req,
1575 int ret)
1576{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001577 struct mmc_queue_req *mq_rq;
1578 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1579
Adrian Hunter67716322011-08-29 16:42:15 +03001580 /*
1581 * If this is an SD card and we're writing, we can first
1582 * mark the known good sectors as ok.
1583 *
1584 * If the card is not SD, we can still ok written sectors
1585 * as reported by the controller (which might be less than
1586 * the real number of written sectors, but never more).
1587 */
1588 if (mmc_card_sd(card)) {
1589 u32 blocks;
1590
1591 blocks = mmc_sd_num_wr_blocks(card);
1592 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301593 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001594 }
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001595 } else {
1596 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001597 }
1598 return ret;
1599}
1600
Linus Walleij4e1f7802017-01-24 11:17:52 +01001601static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1602{
1603 int ret = 1;
1604
1605 if (mmc_card_removed(card))
1606 req->rq_flags |= RQF_QUIET;
1607 while (ret)
1608 ret = blk_end_request(req, -EIO,
1609 blk_rq_cur_bytes(req));
1610}
1611
Linus Walleijefb5a052017-01-24 11:17:53 +01001612static void mmc_blk_rw_start_new(struct mmc_queue *mq, struct mmc_card *card,
1613 struct request *req)
1614{
1615 if (!req)
1616 return;
1617
1618 if (mmc_card_removed(card)) {
1619 req->rq_flags |= RQF_QUIET;
1620 blk_end_request_all(req, -EIO);
1621 } else {
1622 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1623 mmc_start_req(card->host,
1624 &mq->mqrq_cur->mmc_active, NULL);
1625 }
1626}
1627
Per Forlinee8a43a2011-07-01 18:55:33 +02001628static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001629{
Linus Walleij7db30282016-11-18 13:36:15 +01001630 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001631 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001632 struct mmc_blk_request *brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001633 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001634 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001635 struct mmc_queue_req *mq_rq;
Adrian Hunter5be80372016-11-29 12:09:09 +02001636 struct request *req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001637 struct mmc_async_req *new_areq;
1638 struct mmc_async_req *old_areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001639
1640 if (!rqc && !mq->mqrq_prev->req)
1641 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001642
1643 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001644 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301645 /*
1646 * When 4KB native sector is enabled, only 8 blocks
1647 * multiple read or write is allowed
1648 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001649 if (mmc_large_sector(card) &&
1650 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301651 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Adrian Hunter5be80372016-11-29 12:09:09 +02001652 rqc->rq_disk->disk_name);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001653 mmc_blk_rw_cmd_abort(card, rqc);
1654 return 0;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301655 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001656
Linus Walleij03d640a2016-11-25 10:35:00 +01001657 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Linus Walleij7d552a42017-01-24 11:17:56 +01001658 new_areq = &mq->mqrq_cur->mmc_active;
Per Forlinee8a43a2011-07-01 18:55:33 +02001659 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001660 new_areq = NULL;
1661
1662 old_areq = mmc_start_req(card->host, new_areq, &status);
1663 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001664 /*
1665 * We have just put the first request into the pipeline
1666 * and there is nothing more to do until it is
1667 * complete.
1668 */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001669 if (status == MMC_BLK_NEW_REQUEST)
1670 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001671 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001672 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001673
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001674 /*
1675 * An asynchronous request has been completed and we proceed
1676 * to handle the result of it.
1677 */
Linus Walleij7d552a42017-01-24 11:17:56 +01001678 mq_rq = container_of(old_areq, struct mmc_queue_req, mmc_active);
Per Forlinee8a43a2011-07-01 18:55:33 +02001679 brq = &mq_rq->brq;
1680 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001681 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001682 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001683
Per Forlind78d4a8a2011-07-01 18:55:30 +02001684 switch (status) {
1685 case MMC_BLK_SUCCESS:
1686 case MMC_BLK_PARTIAL:
1687 /*
1688 * A block was successfully transferred.
1689 */
Adrian Hunter67716322011-08-29 16:42:15 +03001690 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001691
Linus Walleij03d640a2016-11-25 10:35:00 +01001692 ret = blk_end_request(req, 0,
1693 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001694
Adrian Hunter67716322011-08-29 16:42:15 +03001695 /*
1696 * If the blk_end_request function returns non-zero even
1697 * though all data has been transferred and no errors
1698 * were returned by the host controller, it's a bug.
1699 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001700 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301701 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001702 __func__, blk_rq_bytes(req),
1703 brq->data.bytes_xfered);
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001704 mmc_blk_rw_cmd_abort(card, req);
1705 return 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001706 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001707 break;
1708 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001709 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08001710 if (mmc_blk_reset(md, card->host, type))
1711 goto cmd_abort;
1712 if (!ret)
1713 goto start_new_req;
1714 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001715 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001716 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001717 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001718 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001719 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001720 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001721 if (!mmc_blk_reset(md, card->host, type))
1722 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001723 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001724 case MMC_BLK_DATA_ERR: {
1725 int err;
1726
1727 err = mmc_blk_reset(md, card->host, type);
1728 if (!err)
1729 break;
Linus Walleij03d640a2016-11-25 10:35:00 +01001730 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001731 goto cmd_abort;
1732 /* Fall through */
1733 }
1734 case MMC_BLK_ECC_ERR:
1735 if (brq->data.blocks > 1) {
1736 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001737 pr_warn("%s: retrying using single block read\n",
1738 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001739 disable_multi = 1;
1740 break;
1741 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001742 /*
1743 * After an error, we redo I/O one sector at a
1744 * time, so we only reach here after trying to
1745 * read a single sector.
1746 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301747 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001748 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001749 if (!ret)
1750 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001751 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301752 case MMC_BLK_NOMEDIUM:
1753 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001754 default:
1755 pr_err("%s: Unhandled return value (%d)",
1756 req->rq_disk->disk_name, status);
1757 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001758 }
1759
Per Forlinee8a43a2011-07-01 18:55:33 +02001760 if (ret) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001761 /*
1762 * In case of a incomplete request
1763 * prepare it again and resend.
1764 */
1765 mmc_blk_rw_rq_prep(mq_rq, card,
1766 disable_multi, mq);
1767 mmc_start_req(card->host,
1768 &mq_rq->mmc_active, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001769 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 } while (ret);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 1;
1774
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001775 cmd_abort:
Linus Walleij4e1f7802017-01-24 11:17:52 +01001776 mmc_blk_rw_cmd_abort(card, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Per Forlinee8a43a2011-07-01 18:55:33 +02001778 start_new_req:
Linus Walleijefb5a052017-01-24 11:17:53 +01001779 mmc_blk_rw_start_new(mq, card, rqc);
Per Forlinee8a43a2011-07-01 18:55:33 +02001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return 0;
1782}
1783
Linus Walleij29eb7bd2016-09-20 11:34:38 +02001784int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001785{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001786 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001787 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001788 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001789 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001790
Per Forlinee8a43a2011-07-01 18:55:33 +02001791 if (req && !mq->mqrq_prev->req)
1792 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001793 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001794
Andrei Warkentin371a6892011-04-11 18:10:25 -05001795 ret = mmc_blk_part_switch(card, md);
1796 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001797 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301798 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001799 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001800 ret = 0;
1801 goto out;
1802 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001803
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001804 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05001805 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001806 /* complete ongoing async transfer before issuing discard */
1807 if (card->host->areq)
1808 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001809 ret = mmc_blk_issue_discard_rq(mq, req);
1810 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1811 /* complete ongoing async transfer before issuing secure erase*/
1812 if (card->host->areq)
1813 mmc_blk_issue_rw_rq(mq, NULL);
1814 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001815 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001816 /* complete ongoing async transfer before issuing flush */
1817 if (card->host->areq)
1818 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001819 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001820 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001821 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001822 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001823
Andrei Warkentin371a6892011-04-11 18:10:25 -05001824out:
Adrian Hunter869c5542016-08-25 14:11:43 -06001825 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001826 /*
1827 * Release host when there are no more requests
1828 * and after special request(discard, flush) is done.
1829 * In case sepecial request, there is no reentry to
1830 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1831 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001832 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001833 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Russell Kinga6f6c962006-01-03 22:38:44 +00001836static inline int mmc_blk_readonly(struct mmc_card *card)
1837{
1838 return mmc_card_readonly(card) ||
1839 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1840}
1841
Andrei Warkentin371a6892011-04-11 18:10:25 -05001842static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1843 struct device *parent,
1844 sector_t size,
1845 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001846 const char *subname,
1847 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct mmc_blk_data *md;
1850 int devidx, ret;
1851
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001852again:
1853 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
1854 return ERR_PTR(-ENOMEM);
1855
1856 spin_lock(&mmc_blk_lock);
1857 ret = ida_get_new(&mmc_blk_ida, &devidx);
1858 spin_unlock(&mmc_blk_lock);
1859
1860 if (ret == -EAGAIN)
1861 goto again;
1862 else if (ret)
1863 return ERR_PTR(ret);
1864
1865 if (devidx >= max_devices) {
1866 ret = -ENOSPC;
1867 goto out;
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001870 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001871 if (!md) {
1872 ret = -ENOMEM;
1873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001875
Johan Rudholmadd710e2011-12-02 08:51:06 +01001876 md->area_type = area_type;
1877
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001878 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001879 * Set the read-only status based on the supported commands
1880 * and the write protect switch.
1881 */
1882 md->read_only = mmc_blk_readonly(card);
1883
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001884 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001885 if (md->disk == NULL) {
1886 ret = -ENOMEM;
1887 goto err_kfree;
1888 }
1889
1890 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001891 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001892 md->usage = 1;
1893
Adrian Hunterd09408a2011-06-23 13:40:28 +03001894 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001895 if (ret)
1896 goto err_putdisk;
1897
Linus Walleij7db30282016-11-18 13:36:15 +01001898 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001899
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001900 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001901 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001902 md->disk->fops = &mmc_bdops;
1903 md->disk->private_data = md;
1904 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001905 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001906 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001907 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001908 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001909 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001910
1911 /*
1912 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1913 *
1914 * - be set for removable media with permanent block devices
1915 * - be unset for removable block devices with permanent media
1916 *
1917 * Since MMC block devices clearly fall under the second
1918 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1919 * should use the block device creation/destruction hotplug
1920 * messages to tell when the card is present.
1921 */
1922
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001923 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001924 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001925
Saugata Dasa5075eb2012-05-17 16:32:21 +05301926 if (mmc_card_mmc(card))
1927 blk_queue_logical_block_size(md->queue.queue,
1928 card->ext_csd.data_sector_size);
1929 else
1930 blk_queue_logical_block_size(md->queue.queue, 512);
1931
Andrei Warkentin371a6892011-04-11 18:10:25 -05001932 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001933
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001934 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001935 if ((mmc_card_mmc(card) &&
1936 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001937 (mmc_card_sd(card) &&
1938 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1939 md->flags |= MMC_BLK_CMD23;
1940 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001941
1942 if (mmc_card_mmc(card) &&
1943 md->flags & MMC_BLK_CMD23 &&
1944 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1945 card->ext_csd.rel_sectors)) {
1946 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001947 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001948 }
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001951
1952 err_putdisk:
1953 put_disk(md->disk);
1954 err_kfree:
1955 kfree(md);
1956 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02001957 spin_lock(&mmc_blk_lock);
1958 ida_remove(&mmc_blk_ida, devidx);
1959 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00001960 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Andrei Warkentin371a6892011-04-11 18:10:25 -05001963static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1964{
1965 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001966
1967 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1968 /*
1969 * The EXT_CSD sector count is in number or 512 byte
1970 * sectors.
1971 */
1972 size = card->ext_csd.sectors;
1973 } else {
1974 /*
1975 * The CSD capacity field is in units of read_blkbits.
1976 * set_capacity takes units of 512 bytes.
1977 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001978 size = (typeof(sector_t))card->csd.capacity
1979 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001980 }
1981
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001982 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001983 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001984}
1985
1986static int mmc_blk_alloc_part(struct mmc_card *card,
1987 struct mmc_blk_data *md,
1988 unsigned int part_type,
1989 sector_t size,
1990 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001991 const char *subname,
1992 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001993{
1994 char cap_str[10];
1995 struct mmc_blk_data *part_md;
1996
1997 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001998 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001999 if (IS_ERR(part_md))
2000 return PTR_ERR(part_md);
2001 part_md->part_type = part_type;
2002 list_add(&part_md->part, &md->part);
2003
James Bottomleyb9f28d82015-03-05 18:47:01 -08002004 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002005 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302006 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002007 part_md->disk->disk_name, mmc_card_id(card),
2008 mmc_card_name(card), part_md->part_type, cap_str);
2009 return 0;
2010}
2011
Namjae Jeone0c368d2011-10-06 23:41:38 +09002012/* MMC Physical partitions consist of two boot partitions and
2013 * up to four general purpose partitions.
2014 * For each partition enabled in EXT_CSD a block device will be allocatedi
2015 * to provide access to the partition.
2016 */
2017
Andrei Warkentin371a6892011-04-11 18:10:25 -05002018static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2019{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002020 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021
2022 if (!mmc_card_mmc(card))
2023 return 0;
2024
Namjae Jeone0c368d2011-10-06 23:41:38 +09002025 for (idx = 0; idx < card->nr_parts; idx++) {
2026 if (card->part[idx].size) {
2027 ret = mmc_blk_alloc_part(card, md,
2028 card->part[idx].part_cfg,
2029 card->part[idx].size >> 9,
2030 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002031 card->part[idx].name,
2032 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002033 if (ret)
2034 return ret;
2035 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036 }
2037
2038 return ret;
2039}
2040
Andrei Warkentin371a6892011-04-11 18:10:25 -05002041static void mmc_blk_remove_req(struct mmc_blk_data *md)
2042{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002043 struct mmc_card *card;
2044
Andrei Warkentin371a6892011-04-11 18:10:25 -05002045 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002046 /*
2047 * Flush remaining requests and free queues. It
2048 * is freeing the queue that stops new requests
2049 * from being accepted.
2050 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002051 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002052 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002053 if (md->disk->flags & GENHD_FL_UP) {
2054 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002055 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2056 card->ext_csd.boot_ro_lockable)
2057 device_remove_file(disk_to_dev(md->disk),
2058 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002059
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060 del_gendisk(md->disk);
2061 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002062 mmc_blk_put(md);
2063 }
2064}
2065
2066static void mmc_blk_remove_parts(struct mmc_card *card,
2067 struct mmc_blk_data *md)
2068{
2069 struct list_head *pos, *q;
2070 struct mmc_blk_data *part_md;
2071
2072 list_for_each_safe(pos, q, &md->part) {
2073 part_md = list_entry(pos, struct mmc_blk_data, part);
2074 list_del(pos);
2075 mmc_blk_remove_req(part_md);
2076 }
2077}
2078
2079static int mmc_add_disk(struct mmc_blk_data *md)
2080{
2081 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002082 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002083
Dan Williams307d8e62016-06-20 10:40:44 -07002084 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002085 md->force_ro.show = force_ro_show;
2086 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302087 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002088 md->force_ro.attr.name = "force_ro";
2089 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2090 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2091 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002092 goto force_ro_fail;
2093
2094 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2095 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002096 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002097
2098 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2099 mode = S_IRUGO;
2100 else
2101 mode = S_IRUGO | S_IWUSR;
2102
2103 md->power_ro_lock.show = power_ro_lock_show;
2104 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002105 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002106 md->power_ro_lock.attr.mode = mode;
2107 md->power_ro_lock.attr.name =
2108 "ro_lock_until_next_power_on";
2109 ret = device_create_file(disk_to_dev(md->disk),
2110 &md->power_ro_lock);
2111 if (ret)
2112 goto power_ro_lock_fail;
2113 }
2114 return ret;
2115
2116power_ro_lock_fail:
2117 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2118force_ro_fail:
2119 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002120
2121 return ret;
2122}
2123
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002124static const struct mmc_fixup blk_fixups[] =
2125{
Chris Ballc59d4472011-11-11 22:01:43 -05002126 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2127 MMC_QUIRK_INAND_CMD38),
2128 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2129 MMC_QUIRK_INAND_CMD38),
2130 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2131 MMC_QUIRK_INAND_CMD38),
2132 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2133 MMC_QUIRK_INAND_CMD38),
2134 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2135 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002136
2137 /*
2138 * Some MMC cards experience performance degradation with CMD23
2139 * instead of CMD12-bounded multiblock transfers. For now we'll
2140 * black list what's bad...
2141 * - Certain Toshiba cards.
2142 *
2143 * N.B. This doesn't affect SD cards.
2144 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002145 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2146 MMC_QUIRK_BLK_NO_CMD23),
2147 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2148 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002149 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002150 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002151 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002152 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002153 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002154 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002155
2156 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002157 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002158 */
Chris Ballc59d4472011-11-11 22:01:43 -05002159 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002160 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002161 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2162 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002163
Ian Chen3550ccd2012-08-29 15:05:36 +09002164 /*
2165 * On these Samsung MoviNAND parts, performing secure erase or
2166 * secure trim can result in unrecoverable corruption due to a
2167 * firmware bug.
2168 */
2169 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2170 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2171 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2172 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2173 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2174 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2175 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2176 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2177 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2178 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2179 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2180 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2181 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2182 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2183 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2184 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2185
Shawn Linb5b4ff02015-08-12 13:08:32 +08002186 /*
2187 * On Some Kingston eMMCs, performing trim can result in
2188 * unrecoverable data conrruption occasionally due to a firmware bug.
2189 */
2190 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2191 MMC_QUIRK_TRIM_BROKEN),
2192 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2193 MMC_QUIRK_TRIM_BROKEN),
2194
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002195 END_FIXUP
2196};
2197
Ulf Hansson96541ba2015-04-14 13:06:12 +02002198static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002200 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002201 char cap_str[10];
2202
Pierre Ossman912490d2005-05-21 10:27:02 +01002203 /*
2204 * Check that the card supports the command class(es) we need.
2205 */
2206 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return -ENODEV;
2208
Lukas Czerner5204d002014-06-18 13:18:07 +02002209 mmc_fixup_device(card, blk_fixups);
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 md = mmc_blk_alloc(card);
2212 if (IS_ERR(md))
2213 return PTR_ERR(md);
2214
James Bottomleyb9f28d82015-03-05 18:47:01 -08002215 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002216 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302217 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002219 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Andrei Warkentin371a6892011-04-11 18:10:25 -05002221 if (mmc_blk_alloc_parts(card, md))
2222 goto out;
2223
Ulf Hansson96541ba2015-04-14 13:06:12 +02002224 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002225
Andrei Warkentin371a6892011-04-11 18:10:25 -05002226 if (mmc_add_disk(md))
2227 goto out;
2228
2229 list_for_each_entry(part_md, &md->part, part) {
2230 if (mmc_add_disk(part_md))
2231 goto out;
2232 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002233
2234 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2235 pm_runtime_use_autosuspend(&card->dev);
2236
2237 /*
2238 * Don't enable runtime PM for SD-combo cards here. Leave that
2239 * decision to be taken during the SDIO init sequence instead.
2240 */
2241 if (card->type != MMC_TYPE_SD_COMBO) {
2242 pm_runtime_set_active(&card->dev);
2243 pm_runtime_enable(&card->dev);
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
2247
2248 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002249 mmc_blk_remove_parts(card, md);
2250 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Ulf Hansson96541ba2015-04-14 13:06:12 +02002254static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002256 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Andrei Warkentin371a6892011-04-11 18:10:25 -05002258 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002259 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002260 mmc_claim_host(card->host);
2261 mmc_blk_part_switch(card, md);
2262 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002263 if (card->type != MMC_TYPE_SD_COMBO)
2264 pm_runtime_disable(&card->dev);
2265 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002266 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002267 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
Ulf Hansson96541ba2015-04-14 13:06:12 +02002270static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002272 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002273 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (md) {
2276 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002277 list_for_each_entry(part_md, &md->part, part) {
2278 mmc_queue_suspend(&part_md->queue);
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281 return 0;
2282}
2283
Ulf Hansson96541ba2015-04-14 13:06:12 +02002284static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002285{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002286 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002287}
2288
Ulf Hansson0967edc2014-10-06 11:29:42 +02002289#ifdef CONFIG_PM_SLEEP
2290static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002291{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002292 struct mmc_card *card = mmc_dev_to_card(dev);
2293
2294 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002295}
2296
Ulf Hansson0967edc2014-10-06 11:29:42 +02002297static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002299 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002300 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002303 /*
2304 * Resume involves the card going into idle state,
2305 * so current partition is always the main one.
2306 */
2307 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 list_for_each_entry(part_md, &md->part, part) {
2310 mmc_queue_resume(&part_md->queue);
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 return 0;
2314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315#endif
2316
Ulf Hansson0967edc2014-10-06 11:29:42 +02002317static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2318
Ulf Hansson96541ba2015-04-14 13:06:12 +02002319static struct mmc_driver mmc_driver = {
2320 .drv = {
2321 .name = "mmcblk",
2322 .pm = &mmc_blk_pm_ops,
2323 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 .probe = mmc_blk_probe,
2325 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002326 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327};
2328
2329static int __init mmc_blk_init(void)
2330{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002331 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002333 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2334 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2335
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002336 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002337
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002338 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2339 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002342 res = mmc_register_driver(&mmc_driver);
2343 if (res)
2344 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002346 return 0;
2347 out2:
2348 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 out:
2350 return res;
2351}
2352
2353static void __exit mmc_blk_exit(void)
2354{
2355 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002356 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359module_init(mmc_blk_init);
2360module_exit(mmc_blk_exit);
2361
2362MODULE_LICENSE("GPL");
2363MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2364