blob: ff3da960c4736147b2c1348681412953508ac49b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010050#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010051#include "card.h"
Ulf Hansson5857b292017-01-13 14:14:15 +010052#include "host.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010053#include "bus.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010054#include "mmc_ops.h"
Shawn Lin28fc64a2017-02-15 16:35:28 +080055#include "quirks.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010056#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000058MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040059#ifdef MODULE_PARAM_PREFIX
60#undef MODULE_PARAM_PREFIX
61#endif
62#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010063
Trey Ramsay8fee4762012-11-16 09:31:41 -060064#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020070static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040071
72/*
73 * The defaults come from config options but can be overriden by module
74 * or bootarg options.
75 */
76static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
77
78/*
79 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000080 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020081 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040082 */
83static int max_devices;
84
Ben Hutchingsa26eba62014-11-06 03:35:09 +000085#define MAX_DEVICES 256
86
Ulf Hanssonb10fa992016-04-07 14:36:46 +020087static DEFINE_IDA(mmc_blk_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * There is one mmc_blk_data per slot.
91 */
92struct mmc_blk_data {
93 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070094 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct gendisk *disk;
96 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050097 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050099 unsigned int flags;
100#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000104 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500105 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300106 unsigned int reset_done;
107#define MMC_BLK_READ BIT(0)
108#define MMC_BLK_WRITE BIT(1)
109#define MMC_BLK_DISCARD BIT(2)
110#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111
112 /*
113 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200114 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 * track of the current selected device partition.
116 */
117 unsigned int part_curr;
118 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 struct device_attribute power_ro_lock;
120 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Arjan van de Vena621aae2006-01-12 18:43:35 +0000123static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400125module_param(perdev_minors, int, 0444);
126MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
127
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200128static inline int mmc_blk_part_switch(struct mmc_card *card,
129 struct mmc_blk_data *md);
130static int get_card_status(struct mmc_card *card, u32 *status, int retries);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
133{
134 struct mmc_blk_data *md;
135
Arjan van de Vena621aae2006-01-12 18:43:35 +0000136 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 md = disk->private_data;
138 if (md && md->usage == 0)
139 md = NULL;
140 if (md)
141 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return md;
145}
146
Andrei Warkentin371a6892011-04-11 18:10:25 -0500147static inline int mmc_get_devidx(struct gendisk *disk)
148{
Colin Cross382c55f2015-10-22 10:00:41 -0700149 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500150 return devidx;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void mmc_blk_put(struct mmc_blk_data *md)
154{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 md->usage--;
157 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500158 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800159 blk_cleanup_queue(md->queue.queue);
Heiner Kallweita04848c2017-02-01 19:44:22 +0100160 ida_simple_remove(&mmc_blk_ida, devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 kfree(md);
163 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000164 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Johan Rudholmadd710e2011-12-02 08:51:06 +0100167static ssize_t power_ro_lock_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
170 int ret;
171 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 struct mmc_card *card = md->queue.card;
173 int locked = 0;
174
175 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
176 locked = 2;
177 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
178 locked = 1;
179
180 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
181
Tomas Winkler9098f842015-07-16 15:50:45 +0200182 mmc_blk_put(md);
183
Johan Rudholmadd710e2011-12-02 08:51:06 +0100184 return ret;
185}
186
187static ssize_t power_ro_lock_store(struct device *dev,
188 struct device_attribute *attr, const char *buf, size_t count)
189{
190 int ret;
191 struct mmc_blk_data *md, *part_md;
192 struct mmc_card *card;
193 unsigned long set;
194
195 if (kstrtoul(buf, 0, &set))
196 return -EINVAL;
197
198 if (set != 1)
199 return count;
200
201 md = mmc_blk_get(dev_to_disk(dev));
202 card = md->queue.card;
203
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200204 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100205
206 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
207 card->ext_csd.boot_ro_lock |
208 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
209 card->ext_csd.part_time);
210 if (ret)
211 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
212 else
213 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
214
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200215 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100216
217 if (!ret) {
218 pr_info("%s: Locking boot partition ro until next power on\n",
219 md->disk->disk_name);
220 set_disk_ro(md->disk, 1);
221
222 list_for_each_entry(part_md, &md->part, part)
223 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
224 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
225 set_disk_ro(part_md->disk, 1);
226 }
227 }
228
229 mmc_blk_put(md);
230 return count;
231}
232
Andrei Warkentin371a6892011-04-11 18:10:25 -0500233static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
234 char *buf)
235{
236 int ret;
237 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
238
Baruch Siach0031a982014-09-22 10:12:51 +0300239 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500240 get_disk_ro(dev_to_disk(dev)) ^
241 md->read_only);
242 mmc_blk_put(md);
243 return ret;
244}
245
246static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
247 const char *buf, size_t count)
248{
249 int ret;
250 char *end;
251 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
252 unsigned long set = simple_strtoul(buf, &end, 0);
253 if (end == buf) {
254 ret = -EINVAL;
255 goto out;
256 }
257
258 set_disk_ro(dev_to_disk(dev), set || md->read_only);
259 ret = count;
260out:
261 mmc_blk_put(md);
262 return ret;
263}
264
Al Viroa5a15612008-03-02 10:33:30 -0500265static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Al Viroa5a15612008-03-02 10:33:30 -0500267 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int ret = -ENXIO;
269
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200270 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (md) {
272 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500273 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700275
Al Viroa5a15612008-03-02 10:33:30 -0500276 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700277 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700278 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200281 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return ret;
284}
285
Al Virodb2a1442013-05-05 21:52:57 -0400286static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Al Viroa5a15612008-03-02 10:33:30 -0500288 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200290 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200292 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800296mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800298 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
299 geo->heads = 4;
300 geo->sectors = 16;
301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
John Calixtocb87ea22011-04-26 18:56:29 -0400304struct mmc_blk_ioc_data {
305 struct mmc_ioc_cmd ic;
306 unsigned char *buf;
307 u64 buf_bytes;
308};
309
310static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
311 struct mmc_ioc_cmd __user *user)
312{
313 struct mmc_blk_ioc_data *idata;
314 int err;
315
yalin wang1ff89502015-11-12 19:27:11 +0800316 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400317 if (!idata) {
318 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400319 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400320 }
321
322 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
323 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400324 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400325 }
326
327 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
328 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
329 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400330 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400331 }
332
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300333 if (!idata->buf_bytes) {
334 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100335 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300336 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100337
yalin wang1ff89502015-11-12 19:27:11 +0800338 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400339 if (!idata->buf) {
340 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400341 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400342 }
343
344 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
345 idata->ic.data_ptr, idata->buf_bytes)) {
346 err = -EFAULT;
347 goto copy_err;
348 }
349
350 return idata;
351
352copy_err:
353 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400355 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400356out:
John Calixtocb87ea22011-04-26 18:56:29 -0400357 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400358}
359
Jon Huntera5f57742015-09-22 10:27:53 +0100360static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
361 struct mmc_blk_ioc_data *idata)
362{
363 struct mmc_ioc_cmd *ic = &idata->ic;
364
365 if (copy_to_user(&(ic_ptr->response), ic->response,
366 sizeof(ic->response)))
367 return -EFAULT;
368
369 if (!idata->ic.write_flag) {
370 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
371 idata->buf, idata->buf_bytes))
372 return -EFAULT;
373 }
374
375 return 0;
376}
377
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200378static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
379 u32 retries_max)
380{
381 int err;
382 u32 retry_count = 0;
383
384 if (!status || !retries_max)
385 return -EINVAL;
386
387 do {
388 err = get_card_status(card, status, 5);
389 if (err)
390 break;
391
392 if (!R1_STATUS(*status) &&
393 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
394 break; /* RPMB programming operation complete */
395
396 /*
397 * Rechedule to give the MMC device a chance to continue
398 * processing the previous command without being polled too
399 * frequently.
400 */
401 usleep_range(1000, 5000);
402 } while (++retry_count < retries_max);
403
404 if (retry_count == retries_max)
405 err = -EPERM;
406
407 return err;
408}
409
Maya Erez775a9362013-04-18 15:41:55 +0300410static int ioctl_do_sanitize(struct mmc_card *card)
411{
412 int err;
413
Ulf Hanssona2d10862013-12-16 14:37:26 +0100414 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300415 pr_warn("%s: %s - SANITIZE is not supported\n",
416 mmc_hostname(card->host), __func__);
417 err = -EOPNOTSUPP;
418 goto out;
419 }
420
421 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
422 mmc_hostname(card->host), __func__);
423
424 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
425 EXT_CSD_SANITIZE_START, 1,
426 MMC_SANITIZE_REQ_TIMEOUT);
427
428 if (err)
429 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
430 mmc_hostname(card->host), __func__, err);
431
432 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
433 __func__);
434out:
435 return err;
436}
437
Jon Huntera5f57742015-09-22 10:27:53 +0100438static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
439 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400440{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900441 struct mmc_command cmd = {};
442 struct mmc_data data = {};
443 struct mmc_request mrq = {};
John Calixtocb87ea22011-04-26 18:56:29 -0400444 struct scatterlist sg;
445 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200446 int is_rpmb = false;
447 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400448
Jon Huntera5f57742015-09-22 10:27:53 +0100449 if (!card || !md || !idata)
450 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400451
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200452 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
453 is_rpmb = true;
454
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100455 cmd.opcode = idata->ic.opcode;
456 cmd.arg = idata->ic.arg;
457 cmd.flags = idata->ic.flags;
458
459 if (idata->buf_bytes) {
460 data.sg = &sg;
461 data.sg_len = 1;
462 data.blksz = idata->ic.blksz;
463 data.blocks = idata->ic.blocks;
464
465 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
466
467 if (idata->ic.write_flag)
468 data.flags = MMC_DATA_WRITE;
469 else
470 data.flags = MMC_DATA_READ;
471
472 /* data.flags must already be set before doing this. */
473 mmc_set_data_timeout(&data, card);
474
475 /* Allow overriding the timeout_ns for empirical tuning. */
476 if (idata->ic.data_timeout_ns)
477 data.timeout_ns = idata->ic.data_timeout_ns;
478
479 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
480 /*
481 * Pretend this is a data transfer and rely on the
482 * host driver to compute timeout. When all host
483 * drivers support cmd.cmd_timeout for R1B, this
484 * can be changed to:
485 *
486 * mrq.data = NULL;
487 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
488 */
489 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
490 }
491
492 mrq.data = &data;
493 }
494
495 mrq.cmd = &cmd;
496
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200497 err = mmc_blk_part_switch(card, md);
498 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100499 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200500
John Calixtocb87ea22011-04-26 18:56:29 -0400501 if (idata->ic.is_acmd) {
502 err = mmc_app_cmd(card->host, card);
503 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100504 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400505 }
506
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200507 if (is_rpmb) {
508 err = mmc_set_blockcount(card, data.blocks,
509 idata->ic.write_flag & (1 << 31));
510 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100511 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200512 }
513
Yaniv Gardia82e4842013-06-05 14:13:08 +0300514 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
515 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300516 err = ioctl_do_sanitize(card);
517
518 if (err)
519 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
520 __func__, err);
521
Jon Huntera5f57742015-09-22 10:27:53 +0100522 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300523 }
524
John Calixtocb87ea22011-04-26 18:56:29 -0400525 mmc_wait_for_req(card->host, &mrq);
526
527 if (cmd.error) {
528 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
529 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100530 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400531 }
532 if (data.error) {
533 dev_err(mmc_dev(card->host), "%s: data error %d\n",
534 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100535 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400536 }
537
538 /*
539 * According to the SD specs, some commands require a delay after
540 * issuing the command.
541 */
542 if (idata->ic.postsleep_min_us)
543 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
544
Jon Huntera5f57742015-09-22 10:27:53 +0100545 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400546
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200547 if (is_rpmb) {
548 /*
549 * Ensure RPMB command has completed by polling CMD13
550 * "Send Status".
551 */
552 err = ioctl_rpmb_card_status_poll(card, &status, 5);
553 if (err)
554 dev_err(mmc_dev(card->host),
555 "%s: Card Status=0x%08X, error %d\n",
556 __func__, status, err);
557 }
558
Jon Huntera5f57742015-09-22 10:27:53 +0100559 return err;
560}
561
562static int mmc_blk_ioctl_cmd(struct block_device *bdev,
563 struct mmc_ioc_cmd __user *ic_ptr)
564{
565 struct mmc_blk_ioc_data *idata;
566 struct mmc_blk_data *md;
567 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700568 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100569
Shawn Lin83c742c2016-03-16 18:15:47 +0800570 /*
571 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
572 * whole block device, not on a partition. This prevents overspray
573 * between sibling partitions.
574 */
575 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
576 return -EPERM;
577
Jon Huntera5f57742015-09-22 10:27:53 +0100578 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
579 if (IS_ERR(idata))
580 return PTR_ERR(idata);
581
582 md = mmc_blk_get(bdev->bd_disk);
583 if (!md) {
584 err = -EINVAL;
585 goto cmd_err;
586 }
587
588 card = md->queue.card;
589 if (IS_ERR(card)) {
590 err = PTR_ERR(card);
591 goto cmd_done;
592 }
593
594 mmc_get_card(card);
595
Grant Grundlerb0934102015-09-23 18:30:33 -0700596 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100597
Adrian Hunter3c866562016-05-04 14:38:12 +0300598 /* Always switch back to main area after RPMB access */
599 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
600 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
601
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200602 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400603
Grant Grundlerb0934102015-09-23 18:30:33 -0700604 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100605
John Calixtocb87ea22011-04-26 18:56:29 -0400606cmd_done:
607 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300608cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400609 kfree(idata->buf);
610 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700611 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400612}
613
Jon Huntera5f57742015-09-22 10:27:53 +0100614static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
615 struct mmc_ioc_multi_cmd __user *user)
616{
617 struct mmc_blk_ioc_data **idata = NULL;
618 struct mmc_ioc_cmd __user *cmds = user->cmds;
619 struct mmc_card *card;
620 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700621 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100622 __u64 num_of_cmds;
623
Shawn Lin83c742c2016-03-16 18:15:47 +0800624 /*
625 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
626 * whole block device, not on a partition. This prevents overspray
627 * between sibling partitions.
628 */
629 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
630 return -EPERM;
631
Jon Huntera5f57742015-09-22 10:27:53 +0100632 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
633 sizeof(num_of_cmds)))
634 return -EFAULT;
635
636 if (num_of_cmds > MMC_IOC_MAX_CMDS)
637 return -EINVAL;
638
639 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
640 if (!idata)
641 return -ENOMEM;
642
643 for (i = 0; i < num_of_cmds; i++) {
644 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
645 if (IS_ERR(idata[i])) {
646 err = PTR_ERR(idata[i]);
647 num_of_cmds = i;
648 goto cmd_err;
649 }
650 }
651
652 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800653 if (!md) {
654 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100655 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800656 }
Jon Huntera5f57742015-09-22 10:27:53 +0100657
658 card = md->queue.card;
659 if (IS_ERR(card)) {
660 err = PTR_ERR(card);
661 goto cmd_done;
662 }
663
664 mmc_get_card(card);
665
Grant Grundlerb0934102015-09-23 18:30:33 -0700666 for (i = 0; i < num_of_cmds && !ioc_err; i++)
667 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100668
Adrian Hunter3c866562016-05-04 14:38:12 +0300669 /* Always switch back to main area after RPMB access */
670 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
671 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
672
Jon Huntera5f57742015-09-22 10:27:53 +0100673 mmc_put_card(card);
674
675 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700676 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100677 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100678
679cmd_done:
680 mmc_blk_put(md);
681cmd_err:
682 for (i = 0; i < num_of_cmds; i++) {
683 kfree(idata[i]->buf);
684 kfree(idata[i]);
685 }
686 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700687 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100688}
689
John Calixtocb87ea22011-04-26 18:56:29 -0400690static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
691 unsigned int cmd, unsigned long arg)
692{
Jon Huntera5f57742015-09-22 10:27:53 +0100693 switch (cmd) {
694 case MMC_IOC_CMD:
695 return mmc_blk_ioctl_cmd(bdev,
696 (struct mmc_ioc_cmd __user *)arg);
697 case MMC_IOC_MULTI_CMD:
698 return mmc_blk_ioctl_multi_cmd(bdev,
699 (struct mmc_ioc_multi_cmd __user *)arg);
700 default:
701 return -EINVAL;
702 }
John Calixtocb87ea22011-04-26 18:56:29 -0400703}
704
705#ifdef CONFIG_COMPAT
706static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
707 unsigned int cmd, unsigned long arg)
708{
709 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
710}
711#endif
712
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700713static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500714 .open = mmc_blk_open,
715 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800716 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400718 .ioctl = mmc_blk_ioctl,
719#ifdef CONFIG_COMPAT
720 .compat_ioctl = mmc_blk_compat_ioctl,
721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
723
Andrei Warkentin371a6892011-04-11 18:10:25 -0500724static inline int mmc_blk_part_switch(struct mmc_card *card,
725 struct mmc_blk_data *md)
726{
727 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200728 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300729
Andrei Warkentin371a6892011-04-11 18:10:25 -0500730 if (main_md->part_curr == md->part_type)
731 return 0;
732
733 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300734 u8 part_config = card->ext_csd.part_config;
735
Adrian Hunter57da0c02016-05-04 14:38:13 +0300736 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
737 mmc_retune_pause(card->host);
738
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300739 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
740 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500741
742 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300743 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500744 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +0300745 if (ret) {
746 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
747 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500748 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300749 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300750
751 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +0300752
753 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
754 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +0300755 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500756
757 main_md->part_curr = md->part_type;
758 return 0;
759}
760
Linus Walleij169f03a2017-02-01 13:47:57 +0100761static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700762{
763 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100764 u32 result;
765 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700766
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900767 struct mmc_request mrq = {};
768 struct mmc_command cmd = {};
769 struct mmc_data data = {};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700770
771 struct scatterlist sg;
772
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700773 cmd.opcode = MMC_APP_CMD;
774 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700775 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700776
777 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700778 if (err)
Linus Walleij169f03a2017-02-01 13:47:57 +0100779 return err;
David Brownell7213d172007-08-08 09:10:23 -0700780 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Linus Walleij169f03a2017-02-01 13:47:57 +0100781 return -EIO;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700782
783 memset(&cmd, 0, sizeof(struct mmc_command));
784
785 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
786 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700787 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700788
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700789 data.blksz = 4;
790 data.blocks = 1;
791 data.flags = MMC_DATA_READ;
792 data.sg = &sg;
793 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530794 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700795
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700796 mrq.cmd = &cmd;
797 mrq.data = &data;
798
Ben Dooks051913d2009-06-08 23:33:57 +0100799 blocks = kmalloc(4, GFP_KERNEL);
800 if (!blocks)
Linus Walleij169f03a2017-02-01 13:47:57 +0100801 return -ENOMEM;
Ben Dooks051913d2009-06-08 23:33:57 +0100802
803 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700804
805 mmc_wait_for_req(card->host, &mrq);
806
Ben Dooks051913d2009-06-08 23:33:57 +0100807 result = ntohl(*blocks);
808 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700809
Ben Dooks051913d2009-06-08 23:33:57 +0100810 if (cmd.error || data.error)
Linus Walleij169f03a2017-02-01 13:47:57 +0100811 return -EIO;
Ben Dooks051913d2009-06-08 23:33:57 +0100812
Linus Walleij169f03a2017-02-01 13:47:57 +0100813 *written_blocks = result;
814
815 return 0;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700816}
817
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100818static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300819{
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900820 struct mmc_command cmd = {};
Adrian Hunter504f1912008-10-16 12:55:25 +0300821 int err;
822
Adrian Hunter504f1912008-10-16 12:55:25 +0300823 cmd.opcode = MMC_SEND_STATUS;
824 if (!mmc_host_is_spi(card->host))
825 cmd.arg = card->rca << 16;
826 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100827 err = mmc_wait_for_cmd(card->host, &cmd, retries);
828 if (err == 0)
829 *status = cmd.resp[0];
830 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300831}
832
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100833static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100834 bool hw_busy_detect, struct request *req, bool *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100835{
836 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
837 int err = 0;
838 u32 status;
839
840 do {
841 err = get_card_status(card, &status, 5);
842 if (err) {
843 pr_err("%s: error %d requesting status\n",
844 req->rq_disk->disk_name, err);
845 return err;
846 }
847
848 if (status & R1_ERROR) {
849 pr_err("%s: %s: error sending status cmd, status %#x\n",
850 req->rq_disk->disk_name, __func__, status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100851 *gen_err = true;
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100852 }
853
Ulf Hansson95a91292014-01-29 13:11:27 +0100854 /* We may rely on the host hw to handle busy detection.*/
855 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
856 hw_busy_detect)
857 break;
858
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100859 /*
860 * Timeout if the device never becomes ready for data and never
861 * leaves the program state.
862 */
863 if (time_after(jiffies, timeout)) {
864 pr_err("%s: Card stuck in programming state! %s %s\n",
865 mmc_hostname(card->host),
866 req->rq_disk->disk_name, __func__);
867 return -ETIMEDOUT;
868 }
869
870 /*
871 * Some cards mishandle the status bits,
872 * so make sure to check both the busy
873 * indication and the card state.
874 */
875 } while (!(status & R1_READY_FOR_DATA) ||
876 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
877
878 return err;
879}
880
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100881static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100882 struct request *req, bool *gen_err, u32 *stop_status)
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100883{
884 struct mmc_host *host = card->host;
Masahiro Yamadac7836d12016-12-19 20:51:18 +0900885 struct mmc_command cmd = {};
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100886 int err;
887 bool use_r1b_resp = rq_data_dir(req) == WRITE;
888
889 /*
890 * Normally we use R1B responses for WRITE, but in cases where the host
891 * has specified a max_busy_timeout we need to validate it. A failure
892 * means we need to prevent the host from doing hw busy detection, which
893 * is done by converting to a R1 response instead.
894 */
895 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
896 use_r1b_resp = false;
897
898 cmd.opcode = MMC_STOP_TRANSMISSION;
899 if (use_r1b_resp) {
900 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
901 cmd.busy_timeout = timeout_ms;
902 } else {
903 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
904 }
905
906 err = mmc_wait_for_cmd(host, &cmd, 5);
907 if (err)
908 return err;
909
910 *stop_status = cmd.resp[0];
911
912 /* No need to check card status in case of READ. */
913 if (rq_data_dir(req) == READ)
914 return 0;
915
916 if (!mmc_host_is_spi(host) &&
917 (*stop_status & R1_ERROR)) {
918 pr_err("%s: %s: general error sending stop command, resp %#x\n",
919 req->rq_disk->disk_name, __func__, *stop_status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +0100920 *gen_err = true;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100921 }
922
923 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
924}
925
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530926#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100927#define ERR_RETRY 2
928#define ERR_ABORT 1
929#define ERR_CONTINUE 0
930
931static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
932 bool status_valid, u32 status)
933{
934 switch (error) {
935 case -EILSEQ:
936 /* response crc error, retry the r/w cmd */
937 pr_err("%s: %s sending %s command, card status %#x\n",
938 req->rq_disk->disk_name, "response CRC error",
939 name, status);
940 return ERR_RETRY;
941
942 case -ETIMEDOUT:
943 pr_err("%s: %s sending %s command, card status %#x\n",
944 req->rq_disk->disk_name, "timed out", name, status);
945
946 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530947 if (!status_valid) {
948 pr_err("%s: status not valid, retrying timeout\n",
949 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100950 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530951 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100952
953 /*
954 * If it was a r/w cmd crc error, or illegal command
955 * (eg, issued in wrong state) then retry - we should
956 * have corrected the state problem above.
957 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530958 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
959 pr_err("%s: command error, retrying timeout\n",
960 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100961 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +0530962 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100963
964 /* Otherwise abort the command */
965 return ERR_ABORT;
966
967 default:
968 /* We don't understand the error code the driver gave us */
969 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
970 req->rq_disk->disk_name, error, status);
971 return ERR_ABORT;
972 }
973}
974
975/*
976 * Initial r/w and stop cmd error recovery.
977 * We don't know whether the card received the r/w cmd or not, so try to
978 * restore things back to a sane state. Essentially, we do this as follows:
979 * - Obtain card status. If the first attempt to obtain card status fails,
980 * the status word will reflect the failed status cmd, not the failed
981 * r/w cmd. If we fail to obtain card status, it suggests we can no
982 * longer communicate with the card.
983 * - Check the card state. If the card received the cmd but there was a
984 * transient problem with the response, it might still be in a data transfer
985 * mode. Try to send it a stop command. If this fails, we can't recover.
986 * - If the r/w cmd failed due to a response CRC error, it was probably
987 * transient, so retry the cmd.
988 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
989 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
990 * illegal cmd, retry.
991 * Otherwise we don't understand what happened, so abort.
992 */
993static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Linus Walleij2cc64582016-11-04 11:05:18 +0100994 struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100995{
996 bool prev_cmd_status_valid = true;
997 u32 status, stop_status = 0;
998 int err, retry;
999
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301000 if (mmc_card_removed(card))
1001 return ERR_NOMEDIUM;
1002
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001003 /*
1004 * Try to get card status which indicates both the card state
1005 * and why there was no response. If the first attempt fails,
1006 * we can't be sure the returned status is for the r/w command.
1007 */
1008 for (retry = 2; retry >= 0; retry--) {
1009 err = get_card_status(card, &status, 0);
1010 if (!err)
1011 break;
1012
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001013 /* Re-tune if needed */
1014 mmc_retune_recheck(card->host);
1015
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001016 prev_cmd_status_valid = false;
1017 pr_err("%s: error %d sending status command, %sing\n",
1018 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1019 }
1020
1021 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301022 if (err) {
1023 /* Check if the card is removed */
1024 if (mmc_detect_card_removed(card->host))
1025 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001026 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301027 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001028
Adrian Hunter67716322011-08-29 16:42:15 +03001029 /* Flag ECC errors */
1030 if ((status & R1_CARD_ECC_FAILED) ||
1031 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1032 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
Linus Walleij2cc64582016-11-04 11:05:18 +01001033 *ecc_err = true;
Adrian Hunter67716322011-08-29 16:42:15 +03001034
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001035 /* Flag General errors */
1036 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1037 if ((status & R1_ERROR) ||
1038 (brq->stop.resp[0] & R1_ERROR)) {
1039 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1040 req->rq_disk->disk_name, __func__,
1041 brq->stop.resp[0], status);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001042 *gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001043 }
1044
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001045 /*
1046 * Check the current card state. If it is in some data transfer
1047 * mode, tell it to stop (and hopefully transition back to TRAN.)
1048 */
1049 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1050 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001051 err = send_stop(card,
1052 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1053 req, gen_err, &stop_status);
1054 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001055 pr_err("%s: error %d sending stop command\n",
1056 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001057 /*
1058 * If the stop cmd also timed out, the card is probably
1059 * not present, so abort. Other errors are bad news too.
1060 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001061 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001062 }
1063
Adrian Hunter67716322011-08-29 16:42:15 +03001064 if (stop_status & R1_CARD_ECC_FAILED)
Linus Walleij2cc64582016-11-04 11:05:18 +01001065 *ecc_err = true;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001066 }
1067
1068 /* Check for set block count errors */
1069 if (brq->sbc.error)
1070 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1071 prev_cmd_status_valid, status);
1072
1073 /* Check for r/w command errors */
1074 if (brq->cmd.error)
1075 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1076 prev_cmd_status_valid, status);
1077
Adrian Hunter67716322011-08-29 16:42:15 +03001078 /* Data errors */
1079 if (!brq->stop.error)
1080 return ERR_CONTINUE;
1081
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001082 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001083 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 +01001084 req->rq_disk->disk_name, brq->stop.error,
1085 brq->cmd.resp[0], status);
1086
1087 /*
1088 * Subsitute in our own stop status as this will give the error
1089 * state which happened during the execution of the r/w command.
1090 */
1091 if (stop_status) {
1092 brq->stop.resp[0] = stop_status;
1093 brq->stop.error = 0;
1094 }
1095 return ERR_CONTINUE;
1096}
1097
Adrian Hunter67716322011-08-29 16:42:15 +03001098static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1099 int type)
1100{
1101 int err;
1102
1103 if (md->reset_done & type)
1104 return -EEXIST;
1105
1106 md->reset_done |= type;
1107 err = mmc_hw_reset(host);
1108 /* Ensure we switch back to the correct partition */
1109 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001110 struct mmc_blk_data *main_md =
1111 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001112 int part_err;
1113
1114 main_md->part_curr = main_md->part_type;
1115 part_err = mmc_blk_part_switch(host->card, md);
1116 if (part_err) {
1117 /*
1118 * We have failed to get back into the correct
1119 * partition, so we need to abort the whole request.
1120 */
1121 return -ENODEV;
1122 }
1123 }
1124 return err;
1125}
1126
1127static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1128{
1129 md->reset_done &= ~type;
1130}
1131
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001132int mmc_access_rpmb(struct mmc_queue *mq)
1133{
Linus Walleij7db30282016-11-18 13:36:15 +01001134 struct mmc_blk_data *md = mq->blkdata;
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001135 /*
1136 * If this is a RPMB partition access, return ture
1137 */
1138 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1139 return true;
1140
1141 return false;
1142}
1143
Linus Walleijdf061582017-01-24 11:17:57 +01001144static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001145{
Linus Walleij7db30282016-11-18 13:36:15 +01001146 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001147 struct mmc_card *card = md->queue.card;
1148 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001149 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001150
Adrian Hunterbd788c92010-08-11 14:17:47 -07001151 if (!mmc_can_erase(card)) {
1152 err = -EOPNOTSUPP;
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001153 goto fail;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001154 }
1155
1156 from = blk_rq_pos(req);
1157 nr = blk_rq_sectors(req);
1158
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001159 if (mmc_can_discard(card))
1160 arg = MMC_DISCARD_ARG;
1161 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001162 arg = MMC_TRIM_ARG;
1163 else
1164 arg = MMC_ERASE_ARG;
Geert Uytterhoeven164b50b2016-12-19 15:03:45 +01001165 do {
1166 err = 0;
1167 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1168 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1169 INAND_CMD38_ARG_EXT_CSD,
1170 arg == MMC_TRIM_ARG ?
1171 INAND_CMD38_ARG_TRIM :
1172 INAND_CMD38_ARG_ERASE,
1173 0);
1174 }
1175 if (!err)
1176 err = mmc_erase(card, from, nr, arg);
1177 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
Adrian Hunter67716322011-08-29 16:42:15 +03001178 if (!err)
1179 mmc_blk_reset_success(md, type);
Geert Uytterhoeven8cb6ed12016-12-19 15:03:44 +01001180fail:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301181 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001182}
1183
Linus Walleijdf061582017-01-24 11:17:57 +01001184static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
Adrian Hunter49804542010-08-11 14:17:50 -07001185 struct request *req)
1186{
Linus Walleij7db30282016-11-18 13:36:15 +01001187 struct mmc_blk_data *md = mq->blkdata;
Adrian Hunter49804542010-08-11 14:17:50 -07001188 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001189 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001190 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001191
Maya Erez775a9362013-04-18 15:41:55 +03001192 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001193 err = -EOPNOTSUPP;
1194 goto out;
1195 }
1196
1197 from = blk_rq_pos(req);
1198 nr = blk_rq_sectors(req);
1199
Maya Erez775a9362013-04-18 15:41:55 +03001200 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1201 arg = MMC_SECURE_TRIM1_ARG;
1202 else
1203 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001204
Adrian Hunter67716322011-08-29 16:42:15 +03001205retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001206 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1207 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1208 INAND_CMD38_ARG_EXT_CSD,
1209 arg == MMC_SECURE_TRIM1_ARG ?
1210 INAND_CMD38_ARG_SECTRIM1 :
1211 INAND_CMD38_ARG_SECERASE,
1212 0);
1213 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001214 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001215 }
Adrian Hunter28302812012-04-05 14:45:48 +03001216
Adrian Hunter49804542010-08-11 14:17:50 -07001217 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001218 if (err == -EIO)
1219 goto out_retry;
1220 if (err)
1221 goto out;
1222
1223 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001224 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1225 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1226 INAND_CMD38_ARG_EXT_CSD,
1227 INAND_CMD38_ARG_SECTRIM2,
1228 0);
1229 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001230 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001231 }
Adrian Hunter28302812012-04-05 14:45:48 +03001232
Adrian Hunter49804542010-08-11 14:17:50 -07001233 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001234 if (err == -EIO)
1235 goto out_retry;
1236 if (err)
1237 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001238 }
Adrian Hunter28302812012-04-05 14:45:48 +03001239
Adrian Hunter28302812012-04-05 14:45:48 +03001240out_retry:
1241 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001242 goto retry;
1243 if (!err)
1244 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001245out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301246 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001247}
1248
Linus Walleijdf061582017-01-24 11:17:57 +01001249static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001250{
Linus Walleij7db30282016-11-18 13:36:15 +01001251 struct mmc_blk_data *md = mq->blkdata;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001252 struct mmc_card *card = md->queue.card;
1253 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001254
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001255 ret = mmc_flush_cache(card);
1256 if (ret)
1257 ret = -EIO;
1258
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301259 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001260}
1261
1262/*
1263 * Reformat current write as a reliable write, supporting
1264 * both legacy and the enhanced reliable write MMC cards.
1265 * In each transfer we'll handle only as much as a single
1266 * reliable write can handle, thus finish the request in
1267 * partial completions.
1268 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001269static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1270 struct mmc_card *card,
1271 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001272{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001273 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1274 /* Legacy mode imposes restrictions on transfers. */
1275 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1276 brq->data.blocks = 1;
1277
1278 if (brq->data.blocks > card->ext_csd.rel_sectors)
1279 brq->data.blocks = card->ext_csd.rel_sectors;
1280 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1281 brq->data.blocks = 1;
1282 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001283}
1284
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001285#define CMD_ERRORS \
1286 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1287 R1_ADDRESS_ERROR | /* Misaligned address */ \
1288 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1289 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1290 R1_CC_ERROR | /* Card controller error */ \
1291 R1_ERROR) /* General/unknown error */
1292
Linus Walleij8e8b3f52016-11-04 11:05:19 +01001293static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1294 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001295{
Per Forlinee8a43a2011-07-01 18:55:33 +02001296 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001297 areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001298 struct mmc_blk_request *brq = &mq_mrq->brq;
1299 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001300 int need_retune = card->host->need_retune;
Linus Walleij2cc64582016-11-04 11:05:18 +01001301 bool ecc_err = false;
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001302 bool gen_err = false;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001303
1304 /*
1305 * sbc.error indicates a problem with the set block count
1306 * command. No data will have been transferred.
1307 *
1308 * cmd.error indicates a problem with the r/w command. No
1309 * data will have been transferred.
1310 *
1311 * stop.error indicates a problem with the stop command. Data
1312 * may have been transferred, or may still be transferring.
1313 */
Adrian Hunter67716322011-08-29 16:42:15 +03001314 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1315 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001316 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001317 case ERR_RETRY:
1318 return MMC_BLK_RETRY;
1319 case ERR_ABORT:
1320 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301321 case ERR_NOMEDIUM:
1322 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001323 case ERR_CONTINUE:
1324 break;
1325 }
1326 }
1327
1328 /*
1329 * Check for errors relating to the execution of the
1330 * initial command - such as address errors. No data
1331 * has been transferred.
1332 */
1333 if (brq->cmd.resp[0] & CMD_ERRORS) {
1334 pr_err("%s: r/w command failed, status = %#x\n",
1335 req->rq_disk->disk_name, brq->cmd.resp[0]);
1336 return MMC_BLK_ABORT;
1337 }
1338
1339 /*
1340 * Everything else is either success, or a data error of some
1341 * kind. If it was a write, we may have transitioned to
1342 * program mode, which we have to wait for it to complete.
1343 */
1344 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001345 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001346
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001347 /* Check stop command response */
1348 if (brq->stop.resp[0] & R1_ERROR) {
1349 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1350 req->rq_disk->disk_name, __func__,
1351 brq->stop.resp[0]);
Linus Walleijc44d6ce2016-11-04 11:05:17 +01001352 gen_err = true;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001353 }
1354
Ulf Hansson95a91292014-01-29 13:11:27 +01001355 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1356 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001357 if (err)
1358 return MMC_BLK_CMD_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001359 }
1360
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001361 /* if general error occurs, retry the write operation. */
1362 if (gen_err) {
1363 pr_warn("%s: retrying write for general error\n",
1364 req->rq_disk->disk_name);
1365 return MMC_BLK_RETRY;
1366 }
1367
Per Forlind78d4a8a2011-07-01 18:55:30 +02001368 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001369 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001370 pr_debug("%s: retrying because a re-tune was needed\n",
1371 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001372 brq->retune_retry_done = 1;
1373 return MMC_BLK_RETRY;
1374 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001375 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1376 req->rq_disk->disk_name, brq->data.error,
1377 (unsigned)blk_rq_pos(req),
1378 (unsigned)blk_rq_sectors(req),
1379 brq->cmd.resp[0], brq->stop.resp[0]);
1380
1381 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001382 if (ecc_err)
1383 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001384 return MMC_BLK_DATA_ERR;
1385 } else {
1386 return MMC_BLK_CMD_ERR;
1387 }
1388 }
1389
Adrian Hunter67716322011-08-29 16:42:15 +03001390 if (!brq->data.bytes_xfered)
1391 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001392
Adrian Hunter67716322011-08-29 16:42:15 +03001393 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1394 return MMC_BLK_PARTIAL;
1395
1396 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001397}
1398
Per Forlin54d49d72011-07-01 18:55:29 +02001399static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1400 struct mmc_card *card,
1401 int disable_multi,
1402 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Per Forlin54d49d72011-07-01 18:55:29 +02001404 u32 readcmd, writecmd;
1405 struct mmc_blk_request *brq = &mqrq->brq;
1406 struct request *req = mqrq->req;
Linus Walleij7db30282016-11-18 13:36:15 +01001407 struct mmc_blk_data *md = mq->blkdata;
Saugata Das42659002011-12-21 13:09:17 +05301408 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001410 /*
1411 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001412 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001413 */
Luca Porziod3df0462015-11-06 15:12:26 +00001414 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001415 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001416 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001417
Per Forlin54d49d72011-07-01 18:55:29 +02001418 memset(brq, 0, sizeof(struct mmc_blk_request));
1419 brq->mrq.cmd = &brq->cmd;
1420 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Per Forlin54d49d72011-07-01 18:55:29 +02001422 brq->cmd.arg = blk_rq_pos(req);
1423 if (!mmc_card_blockaddr(card))
1424 brq->cmd.arg <<= 9;
1425 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1426 brq->data.blksz = 512;
1427 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1428 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001429 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Per Forlin54d49d72011-07-01 18:55:29 +02001431 /*
1432 * The block layer doesn't support all sector count
1433 * restrictions, so we need to be prepared for too big
1434 * requests.
1435 */
1436 if (brq->data.blocks > card->host->max_blk_count)
1437 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001439 if (brq->data.blocks > 1) {
1440 /*
1441 * After a read error, we redo the request one sector
1442 * at a time in order to accurately determine which
1443 * sectors can be read successfully.
1444 */
1445 if (disable_multi)
1446 brq->data.blocks = 1;
1447
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001448 /*
1449 * Some controllers have HW issues while operating
1450 * in multiple I/O mode
1451 */
1452 if (card->host->ops->multi_io_quirk)
1453 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1454 (rq_data_dir(req) == READ) ?
1455 MMC_DATA_READ : MMC_DATA_WRITE,
1456 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001457 }
Per Forlin54d49d72011-07-01 18:55:29 +02001458
1459 if (brq->data.blocks > 1 || do_rel_wr) {
1460 /* SPI multiblock writes terminate using a special
1461 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001462 */
Per Forlin54d49d72011-07-01 18:55:29 +02001463 if (!mmc_host_is_spi(card->host) ||
1464 rq_data_dir(req) == READ)
1465 brq->mrq.stop = &brq->stop;
1466 readcmd = MMC_READ_MULTIPLE_BLOCK;
1467 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1468 } else {
1469 brq->mrq.stop = NULL;
1470 readcmd = MMC_READ_SINGLE_BLOCK;
1471 writecmd = MMC_WRITE_BLOCK;
1472 }
1473 if (rq_data_dir(req) == READ) {
1474 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001475 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001476 if (brq->mrq.stop)
1477 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1478 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001479 } else {
1480 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001481 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001482 if (brq->mrq.stop)
1483 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1484 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001485 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001486
Per Forlin54d49d72011-07-01 18:55:29 +02001487 if (do_rel_wr)
1488 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001489
Per Forlin54d49d72011-07-01 18:55:29 +02001490 /*
Saugata Das42659002011-12-21 13:09:17 +05301491 * Data tag is used only during writing meta data to speed
1492 * up write and any subsequent read of this meta data
1493 */
1494 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1495 (req->cmd_flags & REQ_META) &&
1496 (rq_data_dir(req) == WRITE) &&
1497 ((brq->data.blocks * brq->data.blksz) >=
1498 card->ext_csd.data_tag_unit_size);
1499
1500 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001501 * Pre-defined multi-block transfers are preferable to
1502 * open ended-ones (and necessary for reliable writes).
1503 * However, it is not sufficient to just send CMD23,
1504 * and avoid the final CMD12, as on an error condition
1505 * CMD12 (stop) needs to be sent anyway. This, coupled
1506 * with Auto-CMD23 enhancements provided by some
1507 * hosts, means that the complexity of dealing
1508 * with this is best left to the host. If CMD23 is
1509 * supported by card and host, we'll fill sbc in and let
1510 * the host deal with handling it correctly. This means
1511 * that for hosts that don't expose MMC_CAP_CMD23, no
1512 * change of behavior will be observed.
1513 *
1514 * N.B: Some MMC cards experience perf degradation.
1515 * We'll avoid using CMD23-bounded multiblock writes for
1516 * these, while retaining features like reliable writes.
1517 */
Saugata Das42659002011-12-21 13:09:17 +05301518 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1519 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1520 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001521 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1522 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301523 (do_rel_wr ? (1 << 31) : 0) |
1524 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001525 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1526 brq->mrq.sbc = &brq->sbc;
1527 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001528
Per Forlin54d49d72011-07-01 18:55:29 +02001529 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001530
Per Forlin54d49d72011-07-01 18:55:29 +02001531 brq->data.sg = mqrq->sg;
1532 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001533
Per Forlin54d49d72011-07-01 18:55:29 +02001534 /*
1535 * Adjust the sg list so it is the same size as the
1536 * request.
1537 */
1538 if (brq->data.blocks != blk_rq_sectors(req)) {
1539 int i, data_size = brq->data.blocks << 9;
1540 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001541
Per Forlin54d49d72011-07-01 18:55:29 +02001542 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1543 data_size -= sg->length;
1544 if (data_size <= 0) {
1545 sg->length += data_size;
1546 i++;
1547 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001548 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001549 }
Per Forlin54d49d72011-07-01 18:55:29 +02001550 brq->data.sg_len = i;
1551 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001552
Linus Walleij74f5ba32017-02-01 13:47:55 +01001553 mqrq->areq.mrq = &brq->mrq;
1554 mqrq->areq.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001555
Per Forlin54d49d72011-07-01 18:55:29 +02001556 mmc_queue_bounce_pre(mqrq);
1557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Walleij0e65f102017-02-01 13:47:58 +01001559static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1560 struct mmc_blk_request *brq, struct request *req,
1561 bool old_req_pending)
Adrian Hunter67716322011-08-29 16:42:15 +03001562{
Linus Walleij0e65f102017-02-01 13:47:58 +01001563 bool req_pending;
1564
Adrian Hunter67716322011-08-29 16:42:15 +03001565 /*
1566 * If this is an SD card and we're writing, we can first
1567 * mark the known good sectors as ok.
1568 *
1569 * If the card is not SD, we can still ok written sectors
1570 * as reported by the controller (which might be less than
1571 * the real number of written sectors, but never more).
1572 */
1573 if (mmc_card_sd(card)) {
1574 u32 blocks;
Linus Walleij169f03a2017-02-01 13:47:57 +01001575 int err;
Adrian Hunter67716322011-08-29 16:42:15 +03001576
Linus Walleij169f03a2017-02-01 13:47:57 +01001577 err = mmc_sd_num_wr_blocks(card, &blocks);
Linus Walleij0e65f102017-02-01 13:47:58 +01001578 if (err)
1579 req_pending = old_req_pending;
1580 else
1581 req_pending = blk_end_request(req, 0, blocks << 9);
Adrian Hunter5dd784d22016-11-29 12:09:08 +02001582 } else {
Linus Walleij0e65f102017-02-01 13:47:58 +01001583 req_pending = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001584 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001585 return req_pending;
Adrian Hunter67716322011-08-29 16:42:15 +03001586}
1587
Linus Walleij4e1f7802017-01-24 11:17:52 +01001588static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req)
1589{
Linus Walleij4e1f7802017-01-24 11:17:52 +01001590 if (mmc_card_removed(card))
1591 req->rq_flags |= RQF_QUIET;
Linus Walleij0e65f102017-02-01 13:47:58 +01001592 while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req)));
Linus Walleij4e1f7802017-01-24 11:17:52 +01001593}
1594
Linus Walleijb2928e12017-02-01 13:47:54 +01001595/**
1596 * mmc_blk_rw_try_restart() - tries to restart the current async request
1597 * @mq: the queue with the card and host to restart
1598 * @req: a new request that want to be started after the current one
1599 */
1600static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req)
Linus Walleijefb5a052017-01-24 11:17:53 +01001601{
1602 if (!req)
1603 return;
1604
Linus Walleijb2928e12017-02-01 13:47:54 +01001605 /*
1606 * If the card was removed, just cancel everything and return.
1607 */
1608 if (mmc_card_removed(mq->card)) {
Linus Walleijefb5a052017-01-24 11:17:53 +01001609 req->rq_flags |= RQF_QUIET;
1610 blk_end_request_all(req, -EIO);
Linus Walleijb2928e12017-02-01 13:47:54 +01001611 return;
Linus Walleijefb5a052017-01-24 11:17:53 +01001612 }
Linus Walleijb2928e12017-02-01 13:47:54 +01001613 /* Else proceed and try to restart the current async request */
1614 mmc_blk_rw_rq_prep(mq->mqrq_cur, mq->card, 0, mq);
Linus Walleij74f5ba32017-02-01 13:47:55 +01001615 mmc_start_areq(mq->card->host, &mq->mqrq_cur->areq, NULL);
Linus Walleijefb5a052017-01-24 11:17:53 +01001616}
1617
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001618static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
Per Forlin54d49d72011-07-01 18:55:29 +02001619{
Linus Walleij7db30282016-11-18 13:36:15 +01001620 struct mmc_blk_data *md = mq->blkdata;
Per Forlin54d49d72011-07-01 18:55:29 +02001621 struct mmc_card *card = md->queue.card;
Adrian Hunter5be80372016-11-29 12:09:09 +02001622 struct mmc_blk_request *brq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001623 int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001624 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001625 struct mmc_queue_req *mq_rq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001626 struct request *old_req;
Linus Walleij7d552a42017-01-24 11:17:56 +01001627 struct mmc_async_req *new_areq;
1628 struct mmc_async_req *old_areq;
Linus Walleij0e65f102017-02-01 13:47:58 +01001629 bool req_pending = true;
Per Forlinee8a43a2011-07-01 18:55:33 +02001630
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001631 if (!new_req && !mq->mqrq_prev->req)
Linus Walleijdf061582017-01-24 11:17:57 +01001632 return;
Per Forlin54d49d72011-07-01 18:55:29 +02001633
1634 do {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001635 if (new_req) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301636 /*
1637 * When 4KB native sector is enabled, only 8 blocks
1638 * multiple read or write is allowed
1639 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00001640 if (mmc_large_sector(card) &&
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001641 !IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301642 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001643 new_req->rq_disk->disk_name);
1644 mmc_blk_rw_cmd_abort(card, new_req);
Linus Walleijdf061582017-01-24 11:17:57 +01001645 return;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301646 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001647
Linus Walleij03d640a2016-11-25 10:35:00 +01001648 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Linus Walleij74f5ba32017-02-01 13:47:55 +01001649 new_areq = &mq->mqrq_cur->areq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001650 } else
Linus Walleij7d552a42017-01-24 11:17:56 +01001651 new_areq = NULL;
1652
Linus Walleijc3399ef2017-02-01 13:47:53 +01001653 old_areq = mmc_start_areq(card->host, new_areq, &status);
Linus Walleij7d552a42017-01-24 11:17:56 +01001654 if (!old_areq) {
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001655 /*
1656 * We have just put the first request into the pipeline
1657 * and there is nothing more to do until it is
1658 * complete.
1659 */
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001660 if (status == MMC_BLK_NEW_REQUEST)
Linus Walleij9491be52017-02-01 13:47:56 +01001661 mq->new_request = true;
Linus Walleijdf061582017-01-24 11:17:57 +01001662 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001663 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001664
Linus Walleijda0dbaf2017-01-24 11:17:55 +01001665 /*
1666 * An asynchronous request has been completed and we proceed
1667 * to handle the result of it.
1668 */
Linus Walleij74f5ba32017-02-01 13:47:55 +01001669 mq_rq = container_of(old_areq, struct mmc_queue_req, areq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001670 brq = &mq_rq->brq;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001671 old_req = mq_rq->req;
1672 type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001673 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001674
Per Forlind78d4a8a2011-07-01 18:55:30 +02001675 switch (status) {
1676 case MMC_BLK_SUCCESS:
1677 case MMC_BLK_PARTIAL:
1678 /*
1679 * A block was successfully transferred.
1680 */
Adrian Hunter67716322011-08-29 16:42:15 +03001681 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001682
Linus Walleij0e65f102017-02-01 13:47:58 +01001683 req_pending = blk_end_request(old_req, 0,
1684 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001685 /*
1686 * If the blk_end_request function returns non-zero even
1687 * though all data has been transferred and no errors
1688 * were returned by the host controller, it's a bug.
1689 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001690 if (status == MMC_BLK_SUCCESS && req_pending) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301691 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001692 __func__, blk_rq_bytes(old_req),
Per Forlinee8a43a2011-07-01 18:55:33 +02001693 brq->data.bytes_xfered);
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001694 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijdf061582017-01-24 11:17:57 +01001695 return;
Per Forlinee8a43a2011-07-01 18:55:33 +02001696 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001697 break;
1698 case MMC_BLK_CMD_ERR:
Linus Walleij0e65f102017-02-01 13:47:58 +01001699 req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
Linus Walleijdb435502017-02-01 13:47:51 +01001700 if (mmc_blk_reset(md, card->host, type)) {
Adrian Hunter8ecc3442017-03-13 14:36:33 +02001701 if (req_pending)
1702 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001703 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001704 return;
1705 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001706 if (!req_pending) {
Linus Walleijb2928e12017-02-01 13:47:54 +01001707 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001708 return;
1709 }
Ding Wang29535f72015-05-18 20:14:15 +08001710 break;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001711 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03001712 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001713 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001714 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001715 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001716 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001717 if (!mmc_blk_reset(md, card->host, type))
1718 break;
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001719 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001720 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001721 return;
Adrian Hunter67716322011-08-29 16:42:15 +03001722 case MMC_BLK_DATA_ERR: {
1723 int err;
1724
1725 err = mmc_blk_reset(md, card->host, type);
1726 if (!err)
1727 break;
Linus Walleijdb435502017-02-01 13:47:51 +01001728 if (err == -ENODEV) {
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001729 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001730 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001731 return;
1732 }
Adrian Hunter67716322011-08-29 16:42:15 +03001733 /* Fall through */
1734 }
1735 case MMC_BLK_ECC_ERR:
1736 if (brq->data.blocks > 1) {
1737 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07001738 pr_warn("%s: retrying using single block read\n",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001739 old_req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03001740 disable_multi = 1;
1741 break;
1742 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001743 /*
1744 * After an error, we redo I/O one sector at a
1745 * time, so we only reach here after trying to
1746 * read a single sector.
1747 */
Linus Walleij0e65f102017-02-01 13:47:58 +01001748 req_pending = blk_end_request(old_req, -EIO,
1749 brq->data.blksz);
1750 if (!req_pending) {
Linus Walleijb2928e12017-02-01 13:47:54 +01001751 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001752 return;
1753 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001754 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301755 case MMC_BLK_NOMEDIUM:
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001756 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001757 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001758 return;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001759 default:
1760 pr_err("%s: Unhandled return value (%d)",
Linus Walleijacd8dbd2017-02-01 13:47:52 +01001761 old_req->rq_disk->disk_name, status);
1762 mmc_blk_rw_cmd_abort(card, old_req);
Linus Walleijb2928e12017-02-01 13:47:54 +01001763 mmc_blk_rw_try_restart(mq, new_req);
Linus Walleijdb435502017-02-01 13:47:51 +01001764 return;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001765 }
1766
Linus Walleij0e65f102017-02-01 13:47:58 +01001767 if (req_pending) {
Linus Walleij03d640a2016-11-25 10:35:00 +01001768 /*
1769 * In case of a incomplete request
1770 * prepare it again and resend.
1771 */
1772 mmc_blk_rw_rq_prep(mq_rq, card,
1773 disable_multi, mq);
Linus Walleijc3399ef2017-02-01 13:47:53 +01001774 mmc_start_areq(card->host,
Linus Walleij74f5ba32017-02-01 13:47:55 +01001775 &mq_rq->areq, NULL);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001776 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02001777 }
Linus Walleij0e65f102017-02-01 13:47:58 +01001778 } while (req_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Linus Walleijdf061582017-01-24 11:17:57 +01001781void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07001782{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001783 int ret;
Linus Walleij7db30282016-11-18 13:36:15 +01001784 struct mmc_blk_data *md = mq->blkdata;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001785 struct mmc_card *card = md->queue.card;
Adrian Hunter869c5542016-08-25 14:11:43 -06001786 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001787
Per Forlinee8a43a2011-07-01 18:55:33 +02001788 if (req && !mq->mqrq_prev->req)
1789 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001790 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001791
Andrei Warkentin371a6892011-04-11 18:10:25 -05001792 ret = mmc_blk_part_switch(card, md);
1793 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001794 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301795 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001796 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001797 goto out;
1798 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001799
Linus Walleij9491be52017-02-01 13:47:56 +01001800 mq->new_request = false;
Mike Christiec2df40d2016-06-05 14:32:17 -05001801 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001802 /* complete ongoing async transfer before issuing discard */
1803 if (card->host->areq)
1804 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001805 mmc_blk_issue_discard_rq(mq, req);
Christoph Hellwig288dab82016-06-09 16:00:36 +02001806 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
1807 /* complete ongoing async transfer before issuing secure erase*/
1808 if (card->host->areq)
1809 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001810 mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001811 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001812 /* complete ongoing async transfer before issuing flush */
1813 if (card->host->areq)
1814 mmc_blk_issue_rw_rq(mq, NULL);
Linus Walleijdf061582017-01-24 11:17:57 +01001815 mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001816 } else {
Linus Walleijdf061582017-01-24 11:17:57 +01001817 mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter2602b742017-03-13 14:36:32 +02001818 card->host->context_info.is_waiting_last_req = false;
Adrian Hunter49804542010-08-11 14:17:50 -07001819 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001820
Andrei Warkentin371a6892011-04-11 18:10:25 -05001821out:
Linus Walleij9491be52017-02-01 13:47:56 +01001822 if ((!req && !mq->new_request) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001823 /*
1824 * Release host when there are no more requests
1825 * and after special request(discard, flush) is done.
1826 * In case sepecial request, there is no reentry to
1827 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1828 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001829 mmc_put_card(card);
Adrian Hunterbd788c92010-08-11 14:17:47 -07001830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Russell Kinga6f6c962006-01-03 22:38:44 +00001832static inline int mmc_blk_readonly(struct mmc_card *card)
1833{
1834 return mmc_card_readonly(card) ||
1835 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1836}
1837
Andrei Warkentin371a6892011-04-11 18:10:25 -05001838static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1839 struct device *parent,
1840 sector_t size,
1841 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001842 const char *subname,
1843 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 struct mmc_blk_data *md;
1846 int devidx, ret;
1847
Heiner Kallweita04848c2017-02-01 19:44:22 +01001848 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
1849 if (devidx < 0)
1850 return ERR_PTR(devidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001852 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001853 if (!md) {
1854 ret = -ENOMEM;
1855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001857
Johan Rudholmadd710e2011-12-02 08:51:06 +01001858 md->area_type = area_type;
1859
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001860 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001861 * Set the read-only status based on the supported commands
1862 * and the write protect switch.
1863 */
1864 md->read_only = mmc_blk_readonly(card);
1865
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001866 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001867 if (md->disk == NULL) {
1868 ret = -ENOMEM;
1869 goto err_kfree;
1870 }
1871
1872 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001873 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001874 md->usage = 1;
1875
Adrian Hunterd09408a2011-06-23 13:40:28 +03001876 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001877 if (ret)
1878 goto err_putdisk;
1879
Linus Walleij7db30282016-11-18 13:36:15 +01001880 md->queue.blkdata = md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001881
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001882 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001883 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001884 md->disk->fops = &mmc_bdops;
1885 md->disk->private_data = md;
1886 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07001887 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001888 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07001889 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02001890 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02001891 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001892
1893 /*
1894 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1895 *
1896 * - be set for removable media with permanent block devices
1897 * - be unset for removable block devices with permanent media
1898 *
1899 * Since MMC block devices clearly fall under the second
1900 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1901 * should use the block device creation/destruction hotplug
1902 * messages to tell when the card is present.
1903 */
1904
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001905 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02001906 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001907
Saugata Dasa5075eb2012-05-17 16:32:21 +05301908 if (mmc_card_mmc(card))
1909 blk_queue_logical_block_size(md->queue.queue,
1910 card->ext_csd.data_sector_size);
1911 else
1912 blk_queue_logical_block_size(md->queue.queue, 512);
1913
Andrei Warkentin371a6892011-04-11 18:10:25 -05001914 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001915
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001916 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02001917 if ((mmc_card_mmc(card) &&
1918 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001919 (mmc_card_sd(card) &&
1920 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1921 md->flags |= MMC_BLK_CMD23;
1922 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001923
1924 if (mmc_card_mmc(card) &&
1925 md->flags & MMC_BLK_CMD23 &&
1926 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1927 card->ext_csd.rel_sectors)) {
1928 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06001929 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001930 }
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001933
1934 err_putdisk:
1935 put_disk(md->disk);
1936 err_kfree:
1937 kfree(md);
1938 out:
Heiner Kallweita04848c2017-02-01 19:44:22 +01001939 ida_simple_remove(&mmc_blk_ida, devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +00001940 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Andrei Warkentin371a6892011-04-11 18:10:25 -05001943static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1944{
1945 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001946
1947 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1948 /*
1949 * The EXT_CSD sector count is in number or 512 byte
1950 * sectors.
1951 */
1952 size = card->ext_csd.sectors;
1953 } else {
1954 /*
1955 * The CSD capacity field is in units of read_blkbits.
1956 * set_capacity takes units of 512 bytes.
1957 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00001958 size = (typeof(sector_t))card->csd.capacity
1959 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001960 }
1961
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01001962 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001963 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001964}
1965
1966static int mmc_blk_alloc_part(struct mmc_card *card,
1967 struct mmc_blk_data *md,
1968 unsigned int part_type,
1969 sector_t size,
1970 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001971 const char *subname,
1972 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001973{
1974 char cap_str[10];
1975 struct mmc_blk_data *part_md;
1976
1977 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001978 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001979 if (IS_ERR(part_md))
1980 return PTR_ERR(part_md);
1981 part_md->part_type = part_type;
1982 list_add(&part_md->part, &md->part);
1983
James Bottomleyb9f28d82015-03-05 18:47:01 -08001984 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001985 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301986 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001987 part_md->disk->disk_name, mmc_card_id(card),
1988 mmc_card_name(card), part_md->part_type, cap_str);
1989 return 0;
1990}
1991
Namjae Jeone0c368d2011-10-06 23:41:38 +09001992/* MMC Physical partitions consist of two boot partitions and
1993 * up to four general purpose partitions.
1994 * For each partition enabled in EXT_CSD a block device will be allocatedi
1995 * to provide access to the partition.
1996 */
1997
Andrei Warkentin371a6892011-04-11 18:10:25 -05001998static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1999{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002000 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002001
2002 if (!mmc_card_mmc(card))
2003 return 0;
2004
Namjae Jeone0c368d2011-10-06 23:41:38 +09002005 for (idx = 0; idx < card->nr_parts; idx++) {
2006 if (card->part[idx].size) {
2007 ret = mmc_blk_alloc_part(card, md,
2008 card->part[idx].part_cfg,
2009 card->part[idx].size >> 9,
2010 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002011 card->part[idx].name,
2012 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002013 if (ret)
2014 return ret;
2015 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002016 }
2017
2018 return ret;
2019}
2020
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021static void mmc_blk_remove_req(struct mmc_blk_data *md)
2022{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002023 struct mmc_card *card;
2024
Andrei Warkentin371a6892011-04-11 18:10:25 -05002025 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002026 /*
2027 * Flush remaining requests and free queues. It
2028 * is freeing the queue that stops new requests
2029 * from being accepted.
2030 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002031 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002032 mmc_cleanup_queue(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002033 if (md->disk->flags & GENHD_FL_UP) {
2034 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002035 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2036 card->ext_csd.boot_ro_lockable)
2037 device_remove_file(disk_to_dev(md->disk),
2038 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039
Andrei Warkentin371a6892011-04-11 18:10:25 -05002040 del_gendisk(md->disk);
2041 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002042 mmc_blk_put(md);
2043 }
2044}
2045
2046static void mmc_blk_remove_parts(struct mmc_card *card,
2047 struct mmc_blk_data *md)
2048{
2049 struct list_head *pos, *q;
2050 struct mmc_blk_data *part_md;
2051
2052 list_for_each_safe(pos, q, &md->part) {
2053 part_md = list_entry(pos, struct mmc_blk_data, part);
2054 list_del(pos);
2055 mmc_blk_remove_req(part_md);
2056 }
2057}
2058
2059static int mmc_add_disk(struct mmc_blk_data *md)
2060{
2061 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002062 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002063
Dan Williams307d8e62016-06-20 10:40:44 -07002064 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002065 md->force_ro.show = force_ro_show;
2066 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302067 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002068 md->force_ro.attr.name = "force_ro";
2069 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2070 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2071 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002072 goto force_ro_fail;
2073
2074 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2075 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002076 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002077
2078 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2079 mode = S_IRUGO;
2080 else
2081 mode = S_IRUGO | S_IWUSR;
2082
2083 md->power_ro_lock.show = power_ro_lock_show;
2084 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002085 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002086 md->power_ro_lock.attr.mode = mode;
2087 md->power_ro_lock.attr.name =
2088 "ro_lock_until_next_power_on";
2089 ret = device_create_file(disk_to_dev(md->disk),
2090 &md->power_ro_lock);
2091 if (ret)
2092 goto power_ro_lock_fail;
2093 }
2094 return ret;
2095
2096power_ro_lock_fail:
2097 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2098force_ro_fail:
2099 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002100
2101 return ret;
2102}
2103
Ulf Hansson96541ba2015-04-14 13:06:12 +02002104static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002106 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002107 char cap_str[10];
2108
Pierre Ossman912490d2005-05-21 10:27:02 +01002109 /*
2110 * Check that the card supports the command class(es) we need.
2111 */
2112 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return -ENODEV;
2114
Shawn Lin8c7cdbf2017-02-15 16:36:47 +08002115 mmc_fixup_device(card, mmc_blk_fixups);
Lukas Czerner5204d002014-06-18 13:18:07 +02002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 md = mmc_blk_alloc(card);
2118 if (IS_ERR(md))
2119 return PTR_ERR(md);
2120
James Bottomleyb9f28d82015-03-05 18:47:01 -08002121 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002122 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302123 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002125 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Andrei Warkentin371a6892011-04-11 18:10:25 -05002127 if (mmc_blk_alloc_parts(card, md))
2128 goto out;
2129
Ulf Hansson96541ba2015-04-14 13:06:12 +02002130 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002131
Andrei Warkentin371a6892011-04-11 18:10:25 -05002132 if (mmc_add_disk(md))
2133 goto out;
2134
2135 list_for_each_entry(part_md, &md->part, part) {
2136 if (mmc_add_disk(part_md))
2137 goto out;
2138 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002139
2140 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2141 pm_runtime_use_autosuspend(&card->dev);
2142
2143 /*
2144 * Don't enable runtime PM for SD-combo cards here. Leave that
2145 * decision to be taken during the SDIO init sequence instead.
2146 */
2147 if (card->type != MMC_TYPE_SD_COMBO) {
2148 pm_runtime_set_active(&card->dev);
2149 pm_runtime_enable(&card->dev);
2150 }
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
2153
2154 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002155 mmc_blk_remove_parts(card, md);
2156 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Ulf Hansson96541ba2015-04-14 13:06:12 +02002160static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002162 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Andrei Warkentin371a6892011-04-11 18:10:25 -05002164 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002165 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002166 mmc_claim_host(card->host);
2167 mmc_blk_part_switch(card, md);
2168 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002169 if (card->type != MMC_TYPE_SD_COMBO)
2170 pm_runtime_disable(&card->dev);
2171 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002172 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002173 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Ulf Hansson96541ba2015-04-14 13:06:12 +02002176static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002178 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002179 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 if (md) {
2182 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002183 list_for_each_entry(part_md, &md->part, part) {
2184 mmc_queue_suspend(&part_md->queue);
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 return 0;
2188}
2189
Ulf Hansson96541ba2015-04-14 13:06:12 +02002190static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002191{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002192 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002193}
2194
Ulf Hansson0967edc2014-10-06 11:29:42 +02002195#ifdef CONFIG_PM_SLEEP
2196static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002197{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002198 struct mmc_card *card = mmc_dev_to_card(dev);
2199
2200 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002201}
2202
Ulf Hansson0967edc2014-10-06 11:29:42 +02002203static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002205 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002206 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002209 /*
2210 * Resume involves the card going into idle state,
2211 * so current partition is always the main one.
2212 */
2213 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002215 list_for_each_entry(part_md, &md->part, part) {
2216 mmc_queue_resume(&part_md->queue);
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219 return 0;
2220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221#endif
2222
Ulf Hansson0967edc2014-10-06 11:29:42 +02002223static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2224
Ulf Hansson96541ba2015-04-14 13:06:12 +02002225static struct mmc_driver mmc_driver = {
2226 .drv = {
2227 .name = "mmcblk",
2228 .pm = &mmc_blk_pm_ops,
2229 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 .probe = mmc_blk_probe,
2231 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002232 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233};
2234
2235static int __init mmc_blk_init(void)
2236{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002237 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002239 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2240 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2241
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002242 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002243
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002244 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2245 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002248 res = mmc_register_driver(&mmc_driver);
2249 if (res)
2250 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002252 return 0;
2253 out2:
2254 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 out:
2256 return res;
2257}
2258
2259static void __exit mmc_blk_exit(void)
2260{
2261 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002262 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265module_init(mmc_blk_init);
2266module_exit(mmc_blk_exit);
2267
2268MODULE_LICENSE("GPL");
2269MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2270