blob: 32daf433a9fb2622f28b97d8348d3e17c1f93c62 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
John Calixtocb87ea22011-04-26 18:56:29 -040039#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020041#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010042#include <linux/mmc/mmc.h>
43#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040050#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010054
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050055#define INAND_CMD38_ARG_EXT_CSD 113
56#define INAND_CMD38_ARG_ERASE 0x00
57#define INAND_CMD38_ARG_TRIM 0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060061#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030062#define MMC_SANITIZE_REQ_TIMEOUT 240000
63#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050064
Luca Porziod3df0462015-11-06 15:12:26 +000065#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090066 (rq_data_dir(req) == WRITE))
67#define PACKED_CMD_VER 0x01
68#define PACKED_CMD_WR 0x02
69
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
81 * currently limited by the size of the static bitmaps 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
87/* TODO: Replace these with struct ida */
88static DECLARE_BITMAP(dev_use, MAX_DEVICES);
89static DECLARE_BITMAP(name_use, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * There is one mmc_blk_data per slot.
93 */
94struct mmc_blk_data {
95 spinlock_t lock;
96 struct gendisk *disk;
97 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050098 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500100 unsigned int flags;
101#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
102#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900103#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200117 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
123 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Arjan van de Vena621aae2006-01-12 18:43:35 +0000126static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900128enum {
129 MMC_PACKED_NR_IDX = -1,
130 MMC_PACKED_NR_ZERO,
131 MMC_PACKED_NR_SINGLE,
132};
133
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400134module_param(perdev_minors, int, 0444);
135MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
136
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200137static inline int mmc_blk_part_switch(struct mmc_card *card,
138 struct mmc_blk_data *md);
139static int get_card_status(struct mmc_card *card, u32 *status, int retries);
140
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900141static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
142{
143 struct mmc_packed *packed = mqrq->packed;
144
145 BUG_ON(!packed);
146
147 mqrq->cmd_type = MMC_PACKED_NONE;
148 packed->nr_entries = MMC_PACKED_NR_ZERO;
149 packed->idx_failure = MMC_PACKED_NR_IDX;
150 packed->retries = 0;
151 packed->blocks = 0;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
155{
156 struct mmc_blk_data *md;
157
Arjan van de Vena621aae2006-01-12 18:43:35 +0000158 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 md = disk->private_data;
160 if (md && md->usage == 0)
161 md = NULL;
162 if (md)
163 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000164 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return md;
167}
168
Andrei Warkentin371a6892011-04-11 18:10:25 -0500169static inline int mmc_get_devidx(struct gendisk *disk)
170{
Colin Cross382c55f2015-10-22 10:00:41 -0700171 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500172 return devidx;
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static void mmc_blk_put(struct mmc_blk_data *md)
176{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000177 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 md->usage--;
179 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500180 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800181 blk_cleanup_queue(md->queue.queue);
182
David Woodhouse1dff3142007-11-21 18:45:12 +0100183 __clear_bit(devidx, dev_use);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(md);
187 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000188 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Johan Rudholmadd710e2011-12-02 08:51:06 +0100191static ssize_t power_ro_lock_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 int ret;
195 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
196 struct mmc_card *card = md->queue.card;
197 int locked = 0;
198
199 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
200 locked = 2;
201 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
202 locked = 1;
203
204 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
205
Tomas Winkler9098f842015-07-16 15:50:45 +0200206 mmc_blk_put(md);
207
Johan Rudholmadd710e2011-12-02 08:51:06 +0100208 return ret;
209}
210
211static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213{
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
218
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
221
222 if (set != 1)
223 return count;
224
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
227
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200228 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100229
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
238
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200239 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255}
256
Andrei Warkentin371a6892011-04-11 18:10:25 -0500257static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
Baruch Siach0031a982014-09-22 10:12:51 +0300263 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268}
269
270static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284out:
285 mmc_blk_put(md);
286 return ret;
287}
288
Al Viroa5a15612008-03-02 10:33:30 -0500289static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Al Viroa5a15612008-03-02 10:33:30 -0500291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int ret = -ENXIO;
293
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200294 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (md) {
296 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500297 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700299
Al Viroa5a15612008-03-02 10:33:30 -0500300 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700301 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700302 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200305 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return ret;
308}
309
Al Virodb2a1442013-05-05 21:52:57 -0400310static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Al Viroa5a15612008-03-02 10:33:30 -0500312 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200316 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800320mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
John Calixtocb87ea22011-04-26 18:56:29 -0400328struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
332};
333
334static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
336{
337 struct mmc_blk_ioc_data *idata;
338 int err;
339
yalin wang1ff89502015-11-12 19:27:11 +0800340 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400341 if (!idata) {
342 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400343 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400344 }
345
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400355 }
356
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100357 if (!idata->buf_bytes)
358 return idata;
359
yalin wang1ff89502015-11-12 19:27:11 +0800360 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400361 if (!idata->buf) {
362 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400364 }
365
366 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
367 idata->ic.data_ptr, idata->buf_bytes)) {
368 err = -EFAULT;
369 goto copy_err;
370 }
371
372 return idata;
373
374copy_err:
375 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400376idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400377 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400378out:
John Calixtocb87ea22011-04-26 18:56:29 -0400379 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400380}
381
Jon Huntera5f57742015-09-22 10:27:53 +0100382static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
383 struct mmc_blk_ioc_data *idata)
384{
385 struct mmc_ioc_cmd *ic = &idata->ic;
386
387 if (copy_to_user(&(ic_ptr->response), ic->response,
388 sizeof(ic->response)))
389 return -EFAULT;
390
391 if (!idata->ic.write_flag) {
392 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
393 idata->buf, idata->buf_bytes))
394 return -EFAULT;
395 }
396
397 return 0;
398}
399
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200400static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
401 u32 retries_max)
402{
403 int err;
404 u32 retry_count = 0;
405
406 if (!status || !retries_max)
407 return -EINVAL;
408
409 do {
410 err = get_card_status(card, status, 5);
411 if (err)
412 break;
413
414 if (!R1_STATUS(*status) &&
415 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
416 break; /* RPMB programming operation complete */
417
418 /*
419 * Rechedule to give the MMC device a chance to continue
420 * processing the previous command without being polled too
421 * frequently.
422 */
423 usleep_range(1000, 5000);
424 } while (++retry_count < retries_max);
425
426 if (retry_count == retries_max)
427 err = -EPERM;
428
429 return err;
430}
431
Maya Erez775a9362013-04-18 15:41:55 +0300432static int ioctl_do_sanitize(struct mmc_card *card)
433{
434 int err;
435
Ulf Hanssona2d10862013-12-16 14:37:26 +0100436 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300437 pr_warn("%s: %s - SANITIZE is not supported\n",
438 mmc_hostname(card->host), __func__);
439 err = -EOPNOTSUPP;
440 goto out;
441 }
442
443 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
444 mmc_hostname(card->host), __func__);
445
446 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
447 EXT_CSD_SANITIZE_START, 1,
448 MMC_SANITIZE_REQ_TIMEOUT);
449
450 if (err)
451 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
452 mmc_hostname(card->host), __func__, err);
453
454 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
455 __func__);
456out:
457 return err;
458}
459
Jon Huntera5f57742015-09-22 10:27:53 +0100460static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
461 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400462{
John Calixtocb87ea22011-04-26 18:56:29 -0400463 struct mmc_command cmd = {0};
464 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530465 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400466 struct scatterlist sg;
467 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200468 int is_rpmb = false;
469 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400470
Jon Huntera5f57742015-09-22 10:27:53 +0100471 if (!card || !md || !idata)
472 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400473
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200474 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
475 is_rpmb = true;
476
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100477 cmd.opcode = idata->ic.opcode;
478 cmd.arg = idata->ic.arg;
479 cmd.flags = idata->ic.flags;
480
481 if (idata->buf_bytes) {
482 data.sg = &sg;
483 data.sg_len = 1;
484 data.blksz = idata->ic.blksz;
485 data.blocks = idata->ic.blocks;
486
487 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
488
489 if (idata->ic.write_flag)
490 data.flags = MMC_DATA_WRITE;
491 else
492 data.flags = MMC_DATA_READ;
493
494 /* data.flags must already be set before doing this. */
495 mmc_set_data_timeout(&data, card);
496
497 /* Allow overriding the timeout_ns for empirical tuning. */
498 if (idata->ic.data_timeout_ns)
499 data.timeout_ns = idata->ic.data_timeout_ns;
500
501 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
502 /*
503 * Pretend this is a data transfer and rely on the
504 * host driver to compute timeout. When all host
505 * drivers support cmd.cmd_timeout for R1B, this
506 * can be changed to:
507 *
508 * mrq.data = NULL;
509 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
510 */
511 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
512 }
513
514 mrq.data = &data;
515 }
516
517 mrq.cmd = &cmd;
518
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200519 err = mmc_blk_part_switch(card, md);
520 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100521 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200522
John Calixtocb87ea22011-04-26 18:56:29 -0400523 if (idata->ic.is_acmd) {
524 err = mmc_app_cmd(card->host, card);
525 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100526 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400527 }
528
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200529 if (is_rpmb) {
530 err = mmc_set_blockcount(card, data.blocks,
531 idata->ic.write_flag & (1 << 31));
532 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100533 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200534 }
535
Yaniv Gardia82e4842013-06-05 14:13:08 +0300536 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
537 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300538 err = ioctl_do_sanitize(card);
539
540 if (err)
541 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
542 __func__, err);
543
Jon Huntera5f57742015-09-22 10:27:53 +0100544 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300545 }
546
John Calixtocb87ea22011-04-26 18:56:29 -0400547 mmc_wait_for_req(card->host, &mrq);
548
549 if (cmd.error) {
550 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
551 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100552 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400553 }
554 if (data.error) {
555 dev_err(mmc_dev(card->host), "%s: data error %d\n",
556 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100557 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400558 }
559
560 /*
561 * According to the SD specs, some commands require a delay after
562 * issuing the command.
563 */
564 if (idata->ic.postsleep_min_us)
565 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
566
Jon Huntera5f57742015-09-22 10:27:53 +0100567 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400568
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200569 if (is_rpmb) {
570 /*
571 * Ensure RPMB command has completed by polling CMD13
572 * "Send Status".
573 */
574 err = ioctl_rpmb_card_status_poll(card, &status, 5);
575 if (err)
576 dev_err(mmc_dev(card->host),
577 "%s: Card Status=0x%08X, error %d\n",
578 __func__, status, err);
579 }
580
Jon Huntera5f57742015-09-22 10:27:53 +0100581 return err;
582}
583
584static int mmc_blk_ioctl_cmd(struct block_device *bdev,
585 struct mmc_ioc_cmd __user *ic_ptr)
586{
587 struct mmc_blk_ioc_data *idata;
588 struct mmc_blk_data *md;
589 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700590 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100591
Shawn Lin83c742c2016-03-16 18:15:47 +0800592 /*
593 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
594 * whole block device, not on a partition. This prevents overspray
595 * between sibling partitions.
596 */
597 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
598 return -EPERM;
599
Jon Huntera5f57742015-09-22 10:27:53 +0100600 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
601 if (IS_ERR(idata))
602 return PTR_ERR(idata);
603
604 md = mmc_blk_get(bdev->bd_disk);
605 if (!md) {
606 err = -EINVAL;
607 goto cmd_err;
608 }
609
610 card = md->queue.card;
611 if (IS_ERR(card)) {
612 err = PTR_ERR(card);
613 goto cmd_done;
614 }
615
616 mmc_get_card(card);
617
Grant Grundlerb0934102015-09-23 18:30:33 -0700618 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100619
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200620 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400621
Grant Grundlerb0934102015-09-23 18:30:33 -0700622 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100623
John Calixtocb87ea22011-04-26 18:56:29 -0400624cmd_done:
625 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300626cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400627 kfree(idata->buf);
628 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700629 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400630}
631
Jon Huntera5f57742015-09-22 10:27:53 +0100632static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
633 struct mmc_ioc_multi_cmd __user *user)
634{
635 struct mmc_blk_ioc_data **idata = NULL;
636 struct mmc_ioc_cmd __user *cmds = user->cmds;
637 struct mmc_card *card;
638 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700639 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100640 __u64 num_of_cmds;
641
Shawn Lin83c742c2016-03-16 18:15:47 +0800642 /*
643 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
644 * whole block device, not on a partition. This prevents overspray
645 * between sibling partitions.
646 */
647 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
648 return -EPERM;
649
Jon Huntera5f57742015-09-22 10:27:53 +0100650 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
651 sizeof(num_of_cmds)))
652 return -EFAULT;
653
654 if (num_of_cmds > MMC_IOC_MAX_CMDS)
655 return -EINVAL;
656
657 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
658 if (!idata)
659 return -ENOMEM;
660
661 for (i = 0; i < num_of_cmds; i++) {
662 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
663 if (IS_ERR(idata[i])) {
664 err = PTR_ERR(idata[i]);
665 num_of_cmds = i;
666 goto cmd_err;
667 }
668 }
669
670 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800671 if (!md) {
672 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100673 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800674 }
Jon Huntera5f57742015-09-22 10:27:53 +0100675
676 card = md->queue.card;
677 if (IS_ERR(card)) {
678 err = PTR_ERR(card);
679 goto cmd_done;
680 }
681
682 mmc_get_card(card);
683
Grant Grundlerb0934102015-09-23 18:30:33 -0700684 for (i = 0; i < num_of_cmds && !ioc_err; i++)
685 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100686
687 mmc_put_card(card);
688
689 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700690 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100691 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100692
693cmd_done:
694 mmc_blk_put(md);
695cmd_err:
696 for (i = 0; i < num_of_cmds; i++) {
697 kfree(idata[i]->buf);
698 kfree(idata[i]);
699 }
700 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700701 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100702}
703
John Calixtocb87ea22011-04-26 18:56:29 -0400704static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
705 unsigned int cmd, unsigned long arg)
706{
Jon Huntera5f57742015-09-22 10:27:53 +0100707 switch (cmd) {
708 case MMC_IOC_CMD:
709 return mmc_blk_ioctl_cmd(bdev,
710 (struct mmc_ioc_cmd __user *)arg);
711 case MMC_IOC_MULTI_CMD:
712 return mmc_blk_ioctl_multi_cmd(bdev,
713 (struct mmc_ioc_multi_cmd __user *)arg);
714 default:
715 return -EINVAL;
716 }
John Calixtocb87ea22011-04-26 18:56:29 -0400717}
718
719#ifdef CONFIG_COMPAT
720static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
721 unsigned int cmd, unsigned long arg)
722{
723 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
724}
725#endif
726
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700727static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500728 .open = mmc_blk_open,
729 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800730 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400732 .ioctl = mmc_blk_ioctl,
733#ifdef CONFIG_COMPAT
734 .compat_ioctl = mmc_blk_compat_ioctl,
735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736};
737
Andrei Warkentin371a6892011-04-11 18:10:25 -0500738static inline int mmc_blk_part_switch(struct mmc_card *card,
739 struct mmc_blk_data *md)
740{
741 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200742 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300743
Andrei Warkentin371a6892011-04-11 18:10:25 -0500744 if (main_md->part_curr == md->part_type)
745 return 0;
746
747 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300748 u8 part_config = card->ext_csd.part_config;
749
750 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
751 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500752
753 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300754 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500755 card->ext_csd.part_time);
756 if (ret)
757 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300758
759 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300760 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500761
762 main_md->part_curr = md->part_type;
763 return 0;
764}
765
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700766static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
767{
768 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100769 u32 result;
770 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700771
Venkatraman Sad5fd972011-08-25 00:30:50 +0530772 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400773 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400774 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700775
776 struct scatterlist sg;
777
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700778 cmd.opcode = MMC_APP_CMD;
779 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700780 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700781
782 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700783 if (err)
784 return (u32)-1;
785 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700786 return (u32)-1;
787
788 memset(&cmd, 0, sizeof(struct mmc_command));
789
790 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
791 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700792 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700793
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700794 data.blksz = 4;
795 data.blocks = 1;
796 data.flags = MMC_DATA_READ;
797 data.sg = &sg;
798 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530799 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700800
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700801 mrq.cmd = &cmd;
802 mrq.data = &data;
803
Ben Dooks051913d2009-06-08 23:33:57 +0100804 blocks = kmalloc(4, GFP_KERNEL);
805 if (!blocks)
806 return (u32)-1;
807
808 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700809
810 mmc_wait_for_req(card->host, &mrq);
811
Ben Dooks051913d2009-06-08 23:33:57 +0100812 result = ntohl(*blocks);
813 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700814
Ben Dooks051913d2009-06-08 23:33:57 +0100815 if (cmd.error || data.error)
816 result = (u32)-1;
817
818 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700819}
820
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100821static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300822{
Chris Ball1278dba2011-04-13 23:40:30 -0400823 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300824 int err;
825
Adrian Hunter504f1912008-10-16 12:55:25 +0300826 cmd.opcode = MMC_SEND_STATUS;
827 if (!mmc_host_is_spi(card->host))
828 cmd.arg = card->rca << 16;
829 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100830 err = mmc_wait_for_cmd(card->host, &cmd, retries);
831 if (err == 0)
832 *status = cmd.resp[0];
833 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300834}
835
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100836static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +0100837 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100838{
839 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
840 int err = 0;
841 u32 status;
842
843 do {
844 err = get_card_status(card, &status, 5);
845 if (err) {
846 pr_err("%s: error %d requesting status\n",
847 req->rq_disk->disk_name, err);
848 return err;
849 }
850
851 if (status & R1_ERROR) {
852 pr_err("%s: %s: error sending status cmd, status %#x\n",
853 req->rq_disk->disk_name, __func__, status);
854 *gen_err = 1;
855 }
856
Ulf Hansson95a91292014-01-29 13:11:27 +0100857 /* We may rely on the host hw to handle busy detection.*/
858 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
859 hw_busy_detect)
860 break;
861
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100862 /*
863 * Timeout if the device never becomes ready for data and never
864 * leaves the program state.
865 */
866 if (time_after(jiffies, timeout)) {
867 pr_err("%s: Card stuck in programming state! %s %s\n",
868 mmc_hostname(card->host),
869 req->rq_disk->disk_name, __func__);
870 return -ETIMEDOUT;
871 }
872
873 /*
874 * Some cards mishandle the status bits,
875 * so make sure to check both the busy
876 * indication and the card state.
877 */
878 } while (!(status & R1_READY_FOR_DATA) ||
879 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
880
881 return err;
882}
883
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100884static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
885 struct request *req, int *gen_err, u32 *stop_status)
886{
887 struct mmc_host *host = card->host;
888 struct mmc_command cmd = {0};
889 int err;
890 bool use_r1b_resp = rq_data_dir(req) == WRITE;
891
892 /*
893 * Normally we use R1B responses for WRITE, but in cases where the host
894 * has specified a max_busy_timeout we need to validate it. A failure
895 * means we need to prevent the host from doing hw busy detection, which
896 * is done by converting to a R1 response instead.
897 */
898 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
899 use_r1b_resp = false;
900
901 cmd.opcode = MMC_STOP_TRANSMISSION;
902 if (use_r1b_resp) {
903 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
904 cmd.busy_timeout = timeout_ms;
905 } else {
906 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
907 }
908
909 err = mmc_wait_for_cmd(host, &cmd, 5);
910 if (err)
911 return err;
912
913 *stop_status = cmd.resp[0];
914
915 /* No need to check card status in case of READ. */
916 if (rq_data_dir(req) == READ)
917 return 0;
918
919 if (!mmc_host_is_spi(host) &&
920 (*stop_status & R1_ERROR)) {
921 pr_err("%s: %s: general error sending stop command, resp %#x\n",
922 req->rq_disk->disk_name, __func__, *stop_status);
923 *gen_err = 1;
924 }
925
926 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
927}
928
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530929#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100930#define ERR_RETRY 2
931#define ERR_ABORT 1
932#define ERR_CONTINUE 0
933
934static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
935 bool status_valid, u32 status)
936{
937 switch (error) {
938 case -EILSEQ:
939 /* response crc error, retry the r/w cmd */
940 pr_err("%s: %s sending %s command, card status %#x\n",
941 req->rq_disk->disk_name, "response CRC error",
942 name, status);
943 return ERR_RETRY;
944
945 case -ETIMEDOUT:
946 pr_err("%s: %s sending %s command, card status %#x\n",
947 req->rq_disk->disk_name, "timed out", name, status);
948
949 /* If the status cmd initially failed, retry the r/w cmd */
950 if (!status_valid)
951 return ERR_RETRY;
952
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 */
958 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
959 return ERR_RETRY;
960
961 /* Otherwise abort the command */
962 return ERR_ABORT;
963
964 default:
965 /* We don't understand the error code the driver gave us */
966 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
967 req->rq_disk->disk_name, error, status);
968 return ERR_ABORT;
969 }
970}
971
972/*
973 * Initial r/w and stop cmd error recovery.
974 * We don't know whether the card received the r/w cmd or not, so try to
975 * restore things back to a sane state. Essentially, we do this as follows:
976 * - Obtain card status. If the first attempt to obtain card status fails,
977 * the status word will reflect the failed status cmd, not the failed
978 * r/w cmd. If we fail to obtain card status, it suggests we can no
979 * longer communicate with the card.
980 * - Check the card state. If the card received the cmd but there was a
981 * transient problem with the response, it might still be in a data transfer
982 * mode. Try to send it a stop command. If this fails, we can't recover.
983 * - If the r/w cmd failed due to a response CRC error, it was probably
984 * transient, so retry the cmd.
985 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
986 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
987 * illegal cmd, retry.
988 * Otherwise we don't understand what happened, so abort.
989 */
990static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +0900991 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100992{
993 bool prev_cmd_status_valid = true;
994 u32 status, stop_status = 0;
995 int err, retry;
996
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530997 if (mmc_card_removed(card))
998 return ERR_NOMEDIUM;
999
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001000 /*
1001 * Try to get card status which indicates both the card state
1002 * and why there was no response. If the first attempt fails,
1003 * we can't be sure the returned status is for the r/w command.
1004 */
1005 for (retry = 2; retry >= 0; retry--) {
1006 err = get_card_status(card, &status, 0);
1007 if (!err)
1008 break;
1009
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001010 /* Re-tune if needed */
1011 mmc_retune_recheck(card->host);
1012
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001013 prev_cmd_status_valid = false;
1014 pr_err("%s: error %d sending status command, %sing\n",
1015 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1016 }
1017
1018 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301019 if (err) {
1020 /* Check if the card is removed */
1021 if (mmc_detect_card_removed(card->host))
1022 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001023 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301024 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001025
Adrian Hunter67716322011-08-29 16:42:15 +03001026 /* Flag ECC errors */
1027 if ((status & R1_CARD_ECC_FAILED) ||
1028 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1029 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1030 *ecc_err = 1;
1031
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001032 /* Flag General errors */
1033 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1034 if ((status & R1_ERROR) ||
1035 (brq->stop.resp[0] & R1_ERROR)) {
1036 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1037 req->rq_disk->disk_name, __func__,
1038 brq->stop.resp[0], status);
1039 *gen_err = 1;
1040 }
1041
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001042 /*
1043 * Check the current card state. If it is in some data transfer
1044 * mode, tell it to stop (and hopefully transition back to TRAN.)
1045 */
1046 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1047 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001048 err = send_stop(card,
1049 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1050 req, gen_err, &stop_status);
1051 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001052 pr_err("%s: error %d sending stop command\n",
1053 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001054 /*
1055 * If the stop cmd also timed out, the card is probably
1056 * not present, so abort. Other errors are bad news too.
1057 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001058 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001059 }
1060
Adrian Hunter67716322011-08-29 16:42:15 +03001061 if (stop_status & R1_CARD_ECC_FAILED)
1062 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001063 }
1064
1065 /* Check for set block count errors */
1066 if (brq->sbc.error)
1067 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1068 prev_cmd_status_valid, status);
1069
1070 /* Check for r/w command errors */
1071 if (brq->cmd.error)
1072 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1073 prev_cmd_status_valid, status);
1074
Adrian Hunter67716322011-08-29 16:42:15 +03001075 /* Data errors */
1076 if (!brq->stop.error)
1077 return ERR_CONTINUE;
1078
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001079 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001080 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001081 req->rq_disk->disk_name, brq->stop.error,
1082 brq->cmd.resp[0], status);
1083
1084 /*
1085 * Subsitute in our own stop status as this will give the error
1086 * state which happened during the execution of the r/w command.
1087 */
1088 if (stop_status) {
1089 brq->stop.resp[0] = stop_status;
1090 brq->stop.error = 0;
1091 }
1092 return ERR_CONTINUE;
1093}
1094
Adrian Hunter67716322011-08-29 16:42:15 +03001095static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1096 int type)
1097{
1098 int err;
1099
1100 if (md->reset_done & type)
1101 return -EEXIST;
1102
1103 md->reset_done |= type;
1104 err = mmc_hw_reset(host);
1105 /* Ensure we switch back to the correct partition */
1106 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001107 struct mmc_blk_data *main_md =
1108 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001109 int part_err;
1110
1111 main_md->part_curr = main_md->part_type;
1112 part_err = mmc_blk_part_switch(host->card, md);
1113 if (part_err) {
1114 /*
1115 * We have failed to get back into the correct
1116 * partition, so we need to abort the whole request.
1117 */
1118 return -ENODEV;
1119 }
1120 }
1121 return err;
1122}
1123
1124static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1125{
1126 md->reset_done &= ~type;
1127}
1128
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001129int mmc_access_rpmb(struct mmc_queue *mq)
1130{
1131 struct mmc_blk_data *md = mq->data;
1132 /*
1133 * If this is a RPMB partition access, return ture
1134 */
1135 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1136 return true;
1137
1138 return false;
1139}
1140
Adrian Hunterbd788c92010-08-11 14:17:47 -07001141static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1142{
1143 struct mmc_blk_data *md = mq->data;
1144 struct mmc_card *card = md->queue.card;
1145 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001146 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001147
Adrian Hunterbd788c92010-08-11 14:17:47 -07001148 if (!mmc_can_erase(card)) {
1149 err = -EOPNOTSUPP;
1150 goto out;
1151 }
1152
1153 from = blk_rq_pos(req);
1154 nr = blk_rq_sectors(req);
1155
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001156 if (mmc_can_discard(card))
1157 arg = MMC_DISCARD_ARG;
1158 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001159 arg = MMC_TRIM_ARG;
1160 else
1161 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001162retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001163 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1164 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1165 INAND_CMD38_ARG_EXT_CSD,
1166 arg == MMC_TRIM_ARG ?
1167 INAND_CMD38_ARG_TRIM :
1168 INAND_CMD38_ARG_ERASE,
1169 0);
1170 if (err)
1171 goto out;
1172 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001173 err = mmc_erase(card, from, nr, arg);
1174out:
Adrian Hunter67716322011-08-29 16:42:15 +03001175 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1176 goto retry;
1177 if (!err)
1178 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301179 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001180
Adrian Hunterbd788c92010-08-11 14:17:47 -07001181 return err ? 0 : 1;
1182}
1183
Adrian Hunter49804542010-08-11 14:17:50 -07001184static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1185 struct request *req)
1186{
1187 struct mmc_blk_data *md = mq->data;
1188 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
Adrian Hunter49804542010-08-11 14:17:50 -07001248 return err ? 0 : 1;
1249}
1250
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001251static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1252{
1253 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001254 struct mmc_card *card = md->queue.card;
1255 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001256
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001257 ret = mmc_flush_cache(card);
1258 if (ret)
1259 ret = -EIO;
1260
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301261 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001262
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001263 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001264}
1265
1266/*
1267 * Reformat current write as a reliable write, supporting
1268 * both legacy and the enhanced reliable write MMC cards.
1269 * In each transfer we'll handle only as much as a single
1270 * reliable write can handle, thus finish the request in
1271 * partial completions.
1272 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001273static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1274 struct mmc_card *card,
1275 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001276{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001277 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1278 /* Legacy mode imposes restrictions on transfers. */
1279 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1280 brq->data.blocks = 1;
1281
1282 if (brq->data.blocks > card->ext_csd.rel_sectors)
1283 brq->data.blocks = card->ext_csd.rel_sectors;
1284 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1285 brq->data.blocks = 1;
1286 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001287}
1288
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001289#define CMD_ERRORS \
1290 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1291 R1_ADDRESS_ERROR | /* Misaligned address */ \
1292 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1293 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1294 R1_CC_ERROR | /* Card controller error */ \
1295 R1_ERROR) /* General/unknown error */
1296
Per Forlinee8a43a2011-07-01 18:55:33 +02001297static int mmc_blk_err_check(struct mmc_card *card,
1298 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001299{
Per Forlinee8a43a2011-07-01 18:55:33 +02001300 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1301 mmc_active);
1302 struct mmc_blk_request *brq = &mq_mrq->brq;
1303 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001304 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001305 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001306
1307 /*
1308 * sbc.error indicates a problem with the set block count
1309 * command. No data will have been transferred.
1310 *
1311 * cmd.error indicates a problem with the r/w command. No
1312 * data will have been transferred.
1313 *
1314 * stop.error indicates a problem with the stop command. Data
1315 * may have been transferred, or may still be transferring.
1316 */
Adrian Hunter67716322011-08-29 16:42:15 +03001317 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1318 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001319 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001320 case ERR_RETRY:
1321 return MMC_BLK_RETRY;
1322 case ERR_ABORT:
1323 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301324 case ERR_NOMEDIUM:
1325 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001326 case ERR_CONTINUE:
1327 break;
1328 }
1329 }
1330
1331 /*
1332 * Check for errors relating to the execution of the
1333 * initial command - such as address errors. No data
1334 * has been transferred.
1335 */
1336 if (brq->cmd.resp[0] & CMD_ERRORS) {
1337 pr_err("%s: r/w command failed, status = %#x\n",
1338 req->rq_disk->disk_name, brq->cmd.resp[0]);
1339 return MMC_BLK_ABORT;
1340 }
1341
1342 /*
1343 * Everything else is either success, or a data error of some
1344 * kind. If it was a write, we may have transitioned to
1345 * program mode, which we have to wait for it to complete.
1346 */
1347 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001348 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001349
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001350 /* Check stop command response */
1351 if (brq->stop.resp[0] & R1_ERROR) {
1352 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1353 req->rq_disk->disk_name, __func__,
1354 brq->stop.resp[0]);
1355 gen_err = 1;
1356 }
1357
Ulf Hansson95a91292014-01-29 13:11:27 +01001358 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1359 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001360 if (err)
1361 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001362 }
1363
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001364 /* if general error occurs, retry the write operation. */
1365 if (gen_err) {
1366 pr_warn("%s: retrying write for general error\n",
1367 req->rq_disk->disk_name);
1368 return MMC_BLK_RETRY;
1369 }
1370
Per Forlind78d4a82011-07-01 18:55:30 +02001371 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001372 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001373 pr_debug("%s: retrying because a re-tune was needed\n",
1374 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001375 brq->retune_retry_done = 1;
1376 return MMC_BLK_RETRY;
1377 }
Per Forlind78d4a82011-07-01 18:55:30 +02001378 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1379 req->rq_disk->disk_name, brq->data.error,
1380 (unsigned)blk_rq_pos(req),
1381 (unsigned)blk_rq_sectors(req),
1382 brq->cmd.resp[0], brq->stop.resp[0]);
1383
1384 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001385 if (ecc_err)
1386 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001387 return MMC_BLK_DATA_ERR;
1388 } else {
1389 return MMC_BLK_CMD_ERR;
1390 }
1391 }
1392
Adrian Hunter67716322011-08-29 16:42:15 +03001393 if (!brq->data.bytes_xfered)
1394 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001395
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001396 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1397 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1398 return MMC_BLK_PARTIAL;
1399 else
1400 return MMC_BLK_SUCCESS;
1401 }
1402
Adrian Hunter67716322011-08-29 16:42:15 +03001403 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1404 return MMC_BLK_PARTIAL;
1405
1406 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001407}
1408
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001409static int mmc_blk_packed_err_check(struct mmc_card *card,
1410 struct mmc_async_req *areq)
1411{
1412 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1413 mmc_active);
1414 struct request *req = mq_rq->req;
1415 struct mmc_packed *packed = mq_rq->packed;
1416 int err, check, status;
1417 u8 *ext_csd;
1418
1419 BUG_ON(!packed);
1420
1421 packed->retries--;
1422 check = mmc_blk_err_check(card, areq);
1423 err = get_card_status(card, &status, 0);
1424 if (err) {
1425 pr_err("%s: error %d sending status command\n",
1426 req->rq_disk->disk_name, err);
1427 return MMC_BLK_ABORT;
1428 }
1429
1430 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001431 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001432 if (err) {
1433 pr_err("%s: error %d sending ext_csd\n",
1434 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001435 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001436 }
1437
1438 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1439 EXT_CSD_PACKED_FAILURE) &&
1440 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1441 EXT_CSD_PACKED_GENERIC_ERROR)) {
1442 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1443 EXT_CSD_PACKED_INDEXED_ERROR) {
1444 packed->idx_failure =
1445 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1446 check = MMC_BLK_PARTIAL;
1447 }
1448 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1449 "failure index: %d\n",
1450 req->rq_disk->disk_name, packed->nr_entries,
1451 packed->blocks, packed->idx_failure);
1452 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001453 kfree(ext_csd);
1454 }
1455
1456 return check;
1457}
1458
Per Forlin54d49d72011-07-01 18:55:29 +02001459static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1460 struct mmc_card *card,
1461 int disable_multi,
1462 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Per Forlin54d49d72011-07-01 18:55:29 +02001464 u32 readcmd, writecmd;
1465 struct mmc_blk_request *brq = &mqrq->brq;
1466 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301468 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001470 /*
1471 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001472 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001473 */
Luca Porziod3df0462015-11-06 15:12:26 +00001474 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001475 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001476 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001477
Per Forlin54d49d72011-07-01 18:55:29 +02001478 memset(brq, 0, sizeof(struct mmc_blk_request));
1479 brq->mrq.cmd = &brq->cmd;
1480 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Per Forlin54d49d72011-07-01 18:55:29 +02001482 brq->cmd.arg = blk_rq_pos(req);
1483 if (!mmc_card_blockaddr(card))
1484 brq->cmd.arg <<= 9;
1485 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1486 brq->data.blksz = 512;
1487 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1488 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001489 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Per Forlin54d49d72011-07-01 18:55:29 +02001491 /*
1492 * The block layer doesn't support all sector count
1493 * restrictions, so we need to be prepared for too big
1494 * requests.
1495 */
1496 if (brq->data.blocks > card->host->max_blk_count)
1497 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001499 if (brq->data.blocks > 1) {
1500 /*
1501 * After a read error, we redo the request one sector
1502 * at a time in order to accurately determine which
1503 * sectors can be read successfully.
1504 */
1505 if (disable_multi)
1506 brq->data.blocks = 1;
1507
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001508 /*
1509 * Some controllers have HW issues while operating
1510 * in multiple I/O mode
1511 */
1512 if (card->host->ops->multi_io_quirk)
1513 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1514 (rq_data_dir(req) == READ) ?
1515 MMC_DATA_READ : MMC_DATA_WRITE,
1516 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001517 }
Per Forlin54d49d72011-07-01 18:55:29 +02001518
1519 if (brq->data.blocks > 1 || do_rel_wr) {
1520 /* SPI multiblock writes terminate using a special
1521 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001522 */
Per Forlin54d49d72011-07-01 18:55:29 +02001523 if (!mmc_host_is_spi(card->host) ||
1524 rq_data_dir(req) == READ)
1525 brq->mrq.stop = &brq->stop;
1526 readcmd = MMC_READ_MULTIPLE_BLOCK;
1527 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1528 } else {
1529 brq->mrq.stop = NULL;
1530 readcmd = MMC_READ_SINGLE_BLOCK;
1531 writecmd = MMC_WRITE_BLOCK;
1532 }
1533 if (rq_data_dir(req) == READ) {
1534 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001535 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001536 if (brq->mrq.stop)
1537 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1538 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001539 } else {
1540 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001541 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001542 if (brq->mrq.stop)
1543 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1544 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001545 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001546
Per Forlin54d49d72011-07-01 18:55:29 +02001547 if (do_rel_wr)
1548 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001549
Per Forlin54d49d72011-07-01 18:55:29 +02001550 /*
Saugata Das42659002011-12-21 13:09:17 +05301551 * Data tag is used only during writing meta data to speed
1552 * up write and any subsequent read of this meta data
1553 */
1554 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1555 (req->cmd_flags & REQ_META) &&
1556 (rq_data_dir(req) == WRITE) &&
1557 ((brq->data.blocks * brq->data.blksz) >=
1558 card->ext_csd.data_tag_unit_size);
1559
1560 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001561 * Pre-defined multi-block transfers are preferable to
1562 * open ended-ones (and necessary for reliable writes).
1563 * However, it is not sufficient to just send CMD23,
1564 * and avoid the final CMD12, as on an error condition
1565 * CMD12 (stop) needs to be sent anyway. This, coupled
1566 * with Auto-CMD23 enhancements provided by some
1567 * hosts, means that the complexity of dealing
1568 * with this is best left to the host. If CMD23 is
1569 * supported by card and host, we'll fill sbc in and let
1570 * the host deal with handling it correctly. This means
1571 * that for hosts that don't expose MMC_CAP_CMD23, no
1572 * change of behavior will be observed.
1573 *
1574 * N.B: Some MMC cards experience perf degradation.
1575 * We'll avoid using CMD23-bounded multiblock writes for
1576 * these, while retaining features like reliable writes.
1577 */
Saugata Das42659002011-12-21 13:09:17 +05301578 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1579 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1580 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001581 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1582 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301583 (do_rel_wr ? (1 << 31) : 0) |
1584 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001585 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1586 brq->mrq.sbc = &brq->sbc;
1587 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001588
Per Forlin54d49d72011-07-01 18:55:29 +02001589 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001590
Per Forlin54d49d72011-07-01 18:55:29 +02001591 brq->data.sg = mqrq->sg;
1592 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001593
Per Forlin54d49d72011-07-01 18:55:29 +02001594 /*
1595 * Adjust the sg list so it is the same size as the
1596 * request.
1597 */
1598 if (brq->data.blocks != blk_rq_sectors(req)) {
1599 int i, data_size = brq->data.blocks << 9;
1600 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001601
Per Forlin54d49d72011-07-01 18:55:29 +02001602 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1603 data_size -= sg->length;
1604 if (data_size <= 0) {
1605 sg->length += data_size;
1606 i++;
1607 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001608 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001609 }
Per Forlin54d49d72011-07-01 18:55:29 +02001610 brq->data.sg_len = i;
1611 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001612
Per Forlinee8a43a2011-07-01 18:55:33 +02001613 mqrq->mmc_active.mrq = &brq->mrq;
1614 mqrq->mmc_active.err_check = mmc_blk_err_check;
1615
Per Forlin54d49d72011-07-01 18:55:29 +02001616 mmc_queue_bounce_pre(mqrq);
1617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001619static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1620 struct mmc_card *card)
1621{
1622 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1623 unsigned int max_seg_sz = queue_max_segment_size(q);
1624 unsigned int len, nr_segs = 0;
1625
1626 do {
1627 len = min(hdr_sz, max_seg_sz);
1628 hdr_sz -= len;
1629 nr_segs++;
1630 } while (hdr_sz);
1631
1632 return nr_segs;
1633}
1634
1635static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1636{
1637 struct request_queue *q = mq->queue;
1638 struct mmc_card *card = mq->card;
1639 struct request *cur = req, *next = NULL;
1640 struct mmc_blk_data *md = mq->data;
1641 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1642 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1643 unsigned int req_sectors = 0, phys_segments = 0;
1644 unsigned int max_blk_count, max_phys_segs;
1645 bool put_back = true;
1646 u8 max_packed_rw = 0;
1647 u8 reqs = 0;
1648
1649 if (!(md->flags & MMC_BLK_PACKED_CMD))
1650 goto no_packed;
1651
1652 if ((rq_data_dir(cur) == WRITE) &&
1653 mmc_host_packed_wr(card->host))
1654 max_packed_rw = card->ext_csd.max_packed_writes;
1655
1656 if (max_packed_rw == 0)
1657 goto no_packed;
1658
1659 if (mmc_req_rel_wr(cur) &&
1660 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1661 goto no_packed;
1662
1663 if (mmc_large_sector(card) &&
1664 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1665 goto no_packed;
1666
1667 mmc_blk_clear_packed(mqrq);
1668
1669 max_blk_count = min(card->host->max_blk_count,
1670 card->host->max_req_size >> 9);
1671 if (unlikely(max_blk_count > 0xffff))
1672 max_blk_count = 0xffff;
1673
1674 max_phys_segs = queue_max_segments(q);
1675 req_sectors += blk_rq_sectors(cur);
1676 phys_segments += cur->nr_phys_segments;
1677
1678 if (rq_data_dir(cur) == WRITE) {
1679 req_sectors += mmc_large_sector(card) ? 8 : 1;
1680 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1681 }
1682
1683 do {
1684 if (reqs >= max_packed_rw - 1) {
1685 put_back = false;
1686 break;
1687 }
1688
1689 spin_lock_irq(q->queue_lock);
1690 next = blk_fetch_request(q);
1691 spin_unlock_irq(q->queue_lock);
1692 if (!next) {
1693 put_back = false;
1694 break;
1695 }
1696
1697 if (mmc_large_sector(card) &&
1698 !IS_ALIGNED(blk_rq_sectors(next), 8))
1699 break;
1700
1701 if (next->cmd_flags & REQ_DISCARD ||
1702 next->cmd_flags & REQ_FLUSH)
1703 break;
1704
1705 if (rq_data_dir(cur) != rq_data_dir(next))
1706 break;
1707
1708 if (mmc_req_rel_wr(next) &&
1709 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1710 break;
1711
1712 req_sectors += blk_rq_sectors(next);
1713 if (req_sectors > max_blk_count)
1714 break;
1715
1716 phys_segments += next->nr_phys_segments;
1717 if (phys_segments > max_phys_segs)
1718 break;
1719
1720 list_add_tail(&next->queuelist, &mqrq->packed->list);
1721 cur = next;
1722 reqs++;
1723 } while (1);
1724
1725 if (put_back) {
1726 spin_lock_irq(q->queue_lock);
1727 blk_requeue_request(q, next);
1728 spin_unlock_irq(q->queue_lock);
1729 }
1730
1731 if (reqs > 0) {
1732 list_add(&req->queuelist, &mqrq->packed->list);
1733 mqrq->packed->nr_entries = ++reqs;
1734 mqrq->packed->retries = reqs;
1735 return reqs;
1736 }
1737
1738no_packed:
1739 mqrq->cmd_type = MMC_PACKED_NONE;
1740 return 0;
1741}
1742
1743static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1744 struct mmc_card *card,
1745 struct mmc_queue *mq)
1746{
1747 struct mmc_blk_request *brq = &mqrq->brq;
1748 struct request *req = mqrq->req;
1749 struct request *prq;
1750 struct mmc_blk_data *md = mq->data;
1751 struct mmc_packed *packed = mqrq->packed;
1752 bool do_rel_wr, do_data_tag;
1753 u32 *packed_cmd_hdr;
1754 u8 hdr_blocks;
1755 u8 i = 1;
1756
1757 BUG_ON(!packed);
1758
1759 mqrq->cmd_type = MMC_PACKED_WRITE;
1760 packed->blocks = 0;
1761 packed->idx_failure = MMC_PACKED_NR_IDX;
1762
1763 packed_cmd_hdr = packed->cmd_hdr;
1764 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1765 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1766 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1767 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1768
1769 /*
1770 * Argument for each entry of packed group
1771 */
1772 list_for_each_entry(prq, &packed->list, queuelist) {
1773 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1774 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1775 (prq->cmd_flags & REQ_META) &&
1776 (rq_data_dir(prq) == WRITE) &&
1777 ((brq->data.blocks * brq->data.blksz) >=
1778 card->ext_csd.data_tag_unit_size);
1779 /* Argument of CMD23 */
1780 packed_cmd_hdr[(i * 2)] =
1781 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1782 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1783 blk_rq_sectors(prq);
1784 /* Argument of CMD18 or CMD25 */
1785 packed_cmd_hdr[((i * 2)) + 1] =
1786 mmc_card_blockaddr(card) ?
1787 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1788 packed->blocks += blk_rq_sectors(prq);
1789 i++;
1790 }
1791
1792 memset(brq, 0, sizeof(struct mmc_blk_request));
1793 brq->mrq.cmd = &brq->cmd;
1794 brq->mrq.data = &brq->data;
1795 brq->mrq.sbc = &brq->sbc;
1796 brq->mrq.stop = &brq->stop;
1797
1798 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1799 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1800 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1801
1802 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1803 brq->cmd.arg = blk_rq_pos(req);
1804 if (!mmc_card_blockaddr(card))
1805 brq->cmd.arg <<= 9;
1806 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1807
1808 brq->data.blksz = 512;
1809 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001810 brq->data.flags = MMC_DATA_WRITE;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001811
1812 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1813 brq->stop.arg = 0;
1814 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1815
1816 mmc_set_data_timeout(&brq->data, card);
1817
1818 brq->data.sg = mqrq->sg;
1819 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1820
1821 mqrq->mmc_active.mrq = &brq->mrq;
1822 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1823
1824 mmc_queue_bounce_pre(mqrq);
1825}
1826
Adrian Hunter67716322011-08-29 16:42:15 +03001827static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1828 struct mmc_blk_request *brq, struct request *req,
1829 int ret)
1830{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001831 struct mmc_queue_req *mq_rq;
1832 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1833
Adrian Hunter67716322011-08-29 16:42:15 +03001834 /*
1835 * If this is an SD card and we're writing, we can first
1836 * mark the known good sectors as ok.
1837 *
1838 * If the card is not SD, we can still ok written sectors
1839 * as reported by the controller (which might be less than
1840 * the real number of written sectors, but never more).
1841 */
1842 if (mmc_card_sd(card)) {
1843 u32 blocks;
1844
1845 blocks = mmc_sd_num_wr_blocks(card);
1846 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301847 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001848 }
1849 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001850 if (!mmc_packed_cmd(mq_rq->cmd_type))
1851 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001852 }
1853 return ret;
1854}
1855
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001856static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1857{
1858 struct request *prq;
1859 struct mmc_packed *packed = mq_rq->packed;
1860 int idx = packed->idx_failure, i = 0;
1861 int ret = 0;
1862
1863 BUG_ON(!packed);
1864
1865 while (!list_empty(&packed->list)) {
1866 prq = list_entry_rq(packed->list.next);
1867 if (idx == i) {
1868 /* retry from error index */
1869 packed->nr_entries -= idx;
1870 mq_rq->req = prq;
1871 ret = 1;
1872
1873 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1874 list_del_init(&prq->queuelist);
1875 mmc_blk_clear_packed(mq_rq);
1876 }
1877 return ret;
1878 }
1879 list_del_init(&prq->queuelist);
1880 blk_end_request(prq, 0, blk_rq_bytes(prq));
1881 i++;
1882 }
1883
1884 mmc_blk_clear_packed(mq_rq);
1885 return ret;
1886}
1887
1888static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1889{
1890 struct request *prq;
1891 struct mmc_packed *packed = mq_rq->packed;
1892
1893 BUG_ON(!packed);
1894
1895 while (!list_empty(&packed->list)) {
1896 prq = list_entry_rq(packed->list.next);
1897 list_del_init(&prq->queuelist);
1898 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1899 }
1900
1901 mmc_blk_clear_packed(mq_rq);
1902}
1903
1904static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1905 struct mmc_queue_req *mq_rq)
1906{
1907 struct request *prq;
1908 struct request_queue *q = mq->queue;
1909 struct mmc_packed *packed = mq_rq->packed;
1910
1911 BUG_ON(!packed);
1912
1913 while (!list_empty(&packed->list)) {
1914 prq = list_entry_rq(packed->list.prev);
1915 if (prq->queuelist.prev != &packed->list) {
1916 list_del_init(&prq->queuelist);
1917 spin_lock_irq(q->queue_lock);
1918 blk_requeue_request(mq->queue, prq);
1919 spin_unlock_irq(q->queue_lock);
1920 } else {
1921 list_del_init(&prq->queuelist);
1922 }
1923 }
1924
1925 mmc_blk_clear_packed(mq_rq);
1926}
1927
Per Forlinee8a43a2011-07-01 18:55:33 +02001928static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001929{
1930 struct mmc_blk_data *md = mq->data;
1931 struct mmc_card *card = md->queue.card;
1932 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001933 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001934 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001935 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301936 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001937 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001938 const u8 packed_nr = 2;
1939 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001940
1941 if (!rqc && !mq->mqrq_prev->req)
1942 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001943
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001944 if (rqc)
1945 reqs = mmc_blk_prep_packed_list(mq, rqc);
1946
Per Forlin54d49d72011-07-01 18:55:29 +02001947 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001948 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301949 /*
1950 * When 4KB native sector is enabled, only 8 blocks
1951 * multiple read or write is allowed
1952 */
1953 if ((brq->data.blocks & 0x07) &&
1954 (card->ext_csd.data_sector_size == 4096)) {
1955 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1956 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001957 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301958 goto cmd_abort;
1959 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001960
1961 if (reqs >= packed_nr)
1962 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1963 card, mq);
1964 else
1965 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001966 areq = &mq->mqrq_cur->mmc_active;
1967 } else
1968 areq = NULL;
1969 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001970 if (!areq) {
1971 if (status == MMC_BLK_NEW_REQUEST)
1972 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001973 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001974 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001975
Per Forlinee8a43a2011-07-01 18:55:33 +02001976 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1977 brq = &mq_rq->brq;
1978 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001979 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001980 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001981
Per Forlind78d4a82011-07-01 18:55:30 +02001982 switch (status) {
1983 case MMC_BLK_SUCCESS:
1984 case MMC_BLK_PARTIAL:
1985 /*
1986 * A block was successfully transferred.
1987 */
Adrian Hunter67716322011-08-29 16:42:15 +03001988 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001989
1990 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1991 ret = mmc_blk_end_packed_req(mq_rq);
1992 break;
1993 } else {
1994 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001995 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001996 }
1997
Adrian Hunter67716322011-08-29 16:42:15 +03001998 /*
1999 * If the blk_end_request function returns non-zero even
2000 * though all data has been transferred and no errors
2001 * were returned by the host controller, it's a bug.
2002 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002003 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302004 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002005 __func__, blk_rq_bytes(req),
2006 brq->data.bytes_xfered);
2007 rqc = NULL;
2008 goto cmd_abort;
2009 }
Per Forlind78d4a82011-07-01 18:55:30 +02002010 break;
2011 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002012 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002013 if (mmc_blk_reset(md, card->host, type))
2014 goto cmd_abort;
2015 if (!ret)
2016 goto start_new_req;
2017 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002018 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002019 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002020 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002021 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002022 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002023 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002024 if (!mmc_blk_reset(md, card->host, type))
2025 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002026 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002027 case MMC_BLK_DATA_ERR: {
2028 int err;
2029
2030 err = mmc_blk_reset(md, card->host, type);
2031 if (!err)
2032 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002033 if (err == -ENODEV ||
2034 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03002035 goto cmd_abort;
2036 /* Fall through */
2037 }
2038 case MMC_BLK_ECC_ERR:
2039 if (brq->data.blocks > 1) {
2040 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002041 pr_warn("%s: retrying using single block read\n",
2042 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002043 disable_multi = 1;
2044 break;
2045 }
Per Forlind78d4a82011-07-01 18:55:30 +02002046 /*
2047 * After an error, we redo I/O one sector at a
2048 * time, so we only reach here after trying to
2049 * read a single sector.
2050 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302051 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002052 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002053 if (!ret)
2054 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002055 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302056 case MMC_BLK_NOMEDIUM:
2057 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002058 default:
2059 pr_err("%s: Unhandled return value (%d)",
2060 req->rq_disk->disk_name, status);
2061 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002062 }
2063
Per Forlinee8a43a2011-07-01 18:55:33 +02002064 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002065 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2066 if (!mq_rq->packed->retries)
2067 goto cmd_abort;
2068 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2069 mmc_start_req(card->host,
2070 &mq_rq->mmc_active, NULL);
2071 } else {
2072
2073 /*
2074 * In case of a incomplete request
2075 * prepare it again and resend.
2076 */
2077 mmc_blk_rw_rq_prep(mq_rq, card,
2078 disable_multi, mq);
2079 mmc_start_req(card->host,
2080 &mq_rq->mmc_active, NULL);
2081 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002082 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 } while (ret);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 1;
2087
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002088 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002089 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2090 mmc_blk_abort_packed_req(mq_rq);
2091 } else {
2092 if (mmc_card_removed(card))
2093 req->cmd_flags |= REQ_QUIET;
2094 while (ret)
2095 ret = blk_end_request(req, -EIO,
2096 blk_rq_cur_bytes(req));
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Per Forlinee8a43a2011-07-01 18:55:33 +02002099 start_new_req:
2100 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002101 if (mmc_card_removed(card)) {
2102 rqc->cmd_flags |= REQ_QUIET;
2103 blk_end_request_all(rqc, -EIO);
2104 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002105 /*
2106 * If current request is packed, it needs to put back.
2107 */
2108 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2109 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2110
Seungwon Jeon7a819022013-01-22 19:48:07 +09002111 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2112 mmc_start_req(card->host,
2113 &mq->mqrq_cur->mmc_active, NULL);
2114 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return 0;
2118}
2119
Adrian Hunterbd788c92010-08-11 14:17:47 -07002120static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2121{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002122 int ret;
2123 struct mmc_blk_data *md = mq->data;
2124 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002125 struct mmc_host *host = card->host;
2126 unsigned long flags;
Ray Juif662ae42013-10-26 11:03:44 -07002127 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002128
Per Forlinee8a43a2011-07-01 18:55:33 +02002129 if (req && !mq->mqrq_prev->req)
2130 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002131 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002132
Andrei Warkentin371a6892011-04-11 18:10:25 -05002133 ret = mmc_blk_part_switch(card, md);
2134 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002135 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302136 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002137 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002138 ret = 0;
2139 goto out;
2140 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002141
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002142 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Ray Juif662ae42013-10-26 11:03:44 -07002143 if (cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002144 /* complete ongoing async transfer before issuing discard */
2145 if (card->host->areq)
2146 mmc_blk_issue_rw_rq(mq, NULL);
Lukas Czerner5204d002014-06-18 13:18:07 +02002147 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002148 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002149 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002150 ret = mmc_blk_issue_discard_rq(mq, req);
Ray Juif662ae42013-10-26 11:03:44 -07002151 } else if (cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002152 /* complete ongoing async transfer before issuing flush */
2153 if (card->host->areq)
2154 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002155 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002156 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002157 if (!req && host->areq) {
2158 spin_lock_irqsave(&host->context_info.lock, flags);
2159 host->context_info.is_waiting_last_req = true;
2160 spin_unlock_irqrestore(&host->context_info.lock, flags);
2161 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002162 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002163 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002164
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165out:
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002166 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Ray Juif662ae42013-10-26 11:03:44 -07002167 (cmd_flags & MMC_REQ_SPECIAL_MASK))
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002168 /*
2169 * Release host when there are no more requests
2170 * and after special request(discard, flush) is done.
2171 * In case sepecial request, there is no reentry to
2172 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2173 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002174 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002175 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Russell Kinga6f6c962006-01-03 22:38:44 +00002178static inline int mmc_blk_readonly(struct mmc_card *card)
2179{
2180 return mmc_card_readonly(card) ||
2181 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2182}
2183
Andrei Warkentin371a6892011-04-11 18:10:25 -05002184static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2185 struct device *parent,
2186 sector_t size,
2187 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002188 const char *subname,
2189 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct mmc_blk_data *md;
2192 int devidx, ret;
2193
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002194 devidx = find_first_zero_bit(dev_use, max_devices);
2195 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return ERR_PTR(-ENOSPC);
2197 __set_bit(devidx, dev_use);
2198
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002199 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002200 if (!md) {
2201 ret = -ENOMEM;
2202 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002204
Russell Kinga6f6c962006-01-03 22:38:44 +00002205 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002206 * !subname implies we are creating main mmc_blk_data that will be
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002207 * associated with mmc_card with dev_set_drvdata. Due to device
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002208 * partitions, devidx will not coincide with a per-physical card
2209 * index anymore so we keep track of a name index.
2210 */
2211 if (!subname) {
2212 md->name_idx = find_first_zero_bit(name_use, max_devices);
2213 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002214 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002215 md->name_idx = ((struct mmc_blk_data *)
2216 dev_to_disk(parent)->private_data)->name_idx;
2217
Johan Rudholmadd710e2011-12-02 08:51:06 +01002218 md->area_type = area_type;
2219
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002220 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002221 * Set the read-only status based on the supported commands
2222 * and the write protect switch.
2223 */
2224 md->read_only = mmc_blk_readonly(card);
2225
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002226 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002227 if (md->disk == NULL) {
2228 ret = -ENOMEM;
2229 goto err_kfree;
2230 }
2231
2232 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002233 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002234 md->usage = 1;
2235
Adrian Hunterd09408a2011-06-23 13:40:28 +03002236 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002237 if (ret)
2238 goto err_putdisk;
2239
Russell Kinga6f6c962006-01-03 22:38:44 +00002240 md->queue.issue_fn = mmc_blk_issue_rq;
2241 md->queue.data = md;
2242
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002243 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002244 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002245 md->disk->fops = &mmc_bdops;
2246 md->disk->private_data = md;
2247 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002248 md->disk->driverfs_dev = parent;
2249 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002250 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002251 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002252 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002253
2254 /*
2255 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2256 *
2257 * - be set for removable media with permanent block devices
2258 * - be unset for removable block devices with permanent media
2259 *
2260 * Since MMC block devices clearly fall under the second
2261 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2262 * should use the block device creation/destruction hotplug
2263 * messages to tell when the card is present.
2264 */
2265
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002266 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Asaf Vertzfe821912015-01-20 18:33:35 +02002267 "mmcblk%u%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002268
Saugata Dasa5075eb2012-05-17 16:32:21 +05302269 if (mmc_card_mmc(card))
2270 blk_queue_logical_block_size(md->queue.queue,
2271 card->ext_csd.data_sector_size);
2272 else
2273 blk_queue_logical_block_size(md->queue.queue, 512);
2274
Andrei Warkentin371a6892011-04-11 18:10:25 -05002275 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002276
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002277 if (mmc_host_cmd23(card->host)) {
2278 if (mmc_card_mmc(card) ||
2279 (mmc_card_sd(card) &&
2280 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2281 md->flags |= MMC_BLK_CMD23;
2282 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002283
2284 if (mmc_card_mmc(card) &&
2285 md->flags & MMC_BLK_CMD23 &&
2286 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2287 card->ext_csd.rel_sectors)) {
2288 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002289 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002290 }
2291
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002292 if (mmc_card_mmc(card) &&
2293 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2294 (md->flags & MMC_BLK_CMD23) &&
2295 card->ext_csd.packed_event_en) {
2296 if (!mmc_packed_init(&md->queue, card))
2297 md->flags |= MMC_BLK_PACKED_CMD;
2298 }
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002301
2302 err_putdisk:
2303 put_disk(md->disk);
2304 err_kfree:
2305 kfree(md);
2306 out:
2307 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Andrei Warkentin371a6892011-04-11 18:10:25 -05002310static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2311{
2312 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002313
2314 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2315 /*
2316 * The EXT_CSD sector count is in number or 512 byte
2317 * sectors.
2318 */
2319 size = card->ext_csd.sectors;
2320 } else {
2321 /*
2322 * The CSD capacity field is in units of read_blkbits.
2323 * set_capacity takes units of 512 bytes.
2324 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002325 size = (typeof(sector_t))card->csd.capacity
2326 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002327 }
2328
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002329 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002330 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002331}
2332
2333static int mmc_blk_alloc_part(struct mmc_card *card,
2334 struct mmc_blk_data *md,
2335 unsigned int part_type,
2336 sector_t size,
2337 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002338 const char *subname,
2339 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002340{
2341 char cap_str[10];
2342 struct mmc_blk_data *part_md;
2343
2344 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002345 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002346 if (IS_ERR(part_md))
2347 return PTR_ERR(part_md);
2348 part_md->part_type = part_type;
2349 list_add(&part_md->part, &md->part);
2350
James Bottomleyb9f28d82015-03-05 18:47:01 -08002351 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002352 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302353 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002354 part_md->disk->disk_name, mmc_card_id(card),
2355 mmc_card_name(card), part_md->part_type, cap_str);
2356 return 0;
2357}
2358
Namjae Jeone0c368d2011-10-06 23:41:38 +09002359/* MMC Physical partitions consist of two boot partitions and
2360 * up to four general purpose partitions.
2361 * For each partition enabled in EXT_CSD a block device will be allocatedi
2362 * to provide access to the partition.
2363 */
2364
Andrei Warkentin371a6892011-04-11 18:10:25 -05002365static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2366{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002367 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002368
2369 if (!mmc_card_mmc(card))
2370 return 0;
2371
Namjae Jeone0c368d2011-10-06 23:41:38 +09002372 for (idx = 0; idx < card->nr_parts; idx++) {
2373 if (card->part[idx].size) {
2374 ret = mmc_blk_alloc_part(card, md,
2375 card->part[idx].part_cfg,
2376 card->part[idx].size >> 9,
2377 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002378 card->part[idx].name,
2379 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002380 if (ret)
2381 return ret;
2382 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002383 }
2384
2385 return ret;
2386}
2387
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388static void mmc_blk_remove_req(struct mmc_blk_data *md)
2389{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002390 struct mmc_card *card;
2391
Andrei Warkentin371a6892011-04-11 18:10:25 -05002392 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002393 /*
2394 * Flush remaining requests and free queues. It
2395 * is freeing the queue that stops new requests
2396 * from being accepted.
2397 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002398 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002399 mmc_cleanup_queue(&md->queue);
2400 if (md->flags & MMC_BLK_PACKED_CMD)
2401 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002402 if (md->disk->flags & GENHD_FL_UP) {
2403 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002404 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2405 card->ext_csd.boot_ro_lockable)
2406 device_remove_file(disk_to_dev(md->disk),
2407 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002408
Andrei Warkentin371a6892011-04-11 18:10:25 -05002409 del_gendisk(md->disk);
2410 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002411 mmc_blk_put(md);
2412 }
2413}
2414
2415static void mmc_blk_remove_parts(struct mmc_card *card,
2416 struct mmc_blk_data *md)
2417{
2418 struct list_head *pos, *q;
2419 struct mmc_blk_data *part_md;
2420
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002421 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002422 list_for_each_safe(pos, q, &md->part) {
2423 part_md = list_entry(pos, struct mmc_blk_data, part);
2424 list_del(pos);
2425 mmc_blk_remove_req(part_md);
2426 }
2427}
2428
2429static int mmc_add_disk(struct mmc_blk_data *md)
2430{
2431 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002432 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002433
2434 add_disk(md->disk);
2435 md->force_ro.show = force_ro_show;
2436 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302437 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002438 md->force_ro.attr.name = "force_ro";
2439 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2440 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2441 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002442 goto force_ro_fail;
2443
2444 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2445 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002446 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002447
2448 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2449 mode = S_IRUGO;
2450 else
2451 mode = S_IRUGO | S_IWUSR;
2452
2453 md->power_ro_lock.show = power_ro_lock_show;
2454 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002455 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002456 md->power_ro_lock.attr.mode = mode;
2457 md->power_ro_lock.attr.name =
2458 "ro_lock_until_next_power_on";
2459 ret = device_create_file(disk_to_dev(md->disk),
2460 &md->power_ro_lock);
2461 if (ret)
2462 goto power_ro_lock_fail;
2463 }
2464 return ret;
2465
2466power_ro_lock_fail:
2467 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2468force_ro_fail:
2469 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002470
2471 return ret;
2472}
2473
Chris Ballc59d4472011-11-11 22:01:43 -05002474#define CID_MANFID_SANDISK 0x2
2475#define CID_MANFID_TOSHIBA 0x11
2476#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09002477#define CID_MANFID_SAMSUNG 0x15
Shawn Linb5b4ff02015-08-12 13:08:32 +08002478#define CID_MANFID_KINGSTON 0x70
Chris Ballc59d4472011-11-11 22:01:43 -05002479
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002480static const struct mmc_fixup blk_fixups[] =
2481{
Chris Ballc59d4472011-11-11 22:01:43 -05002482 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2483 MMC_QUIRK_INAND_CMD38),
2484 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2485 MMC_QUIRK_INAND_CMD38),
2486 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2487 MMC_QUIRK_INAND_CMD38),
2488 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2489 MMC_QUIRK_INAND_CMD38),
2490 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2491 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002492
2493 /*
2494 * Some MMC cards experience performance degradation with CMD23
2495 * instead of CMD12-bounded multiblock transfers. For now we'll
2496 * black list what's bad...
2497 * - Certain Toshiba cards.
2498 *
2499 * N.B. This doesn't affect SD cards.
2500 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002501 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2502 MMC_QUIRK_BLK_NO_CMD23),
2503 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2504 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002505 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002506 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002507 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002508 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002509 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002510 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002511
2512 /*
2513 * Some Micron MMC cards needs longer data read timeout than
2514 * indicated in CSD.
2515 */
Chris Ballc59d4472011-11-11 22:01:43 -05002516 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002517 MMC_QUIRK_LONG_READ_TIME),
2518
Ian Chen3550ccd2012-08-29 15:05:36 +09002519 /*
2520 * On these Samsung MoviNAND parts, performing secure erase or
2521 * secure trim can result in unrecoverable corruption due to a
2522 * firmware bug.
2523 */
2524 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2525 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2526 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2527 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2528 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2530 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2531 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2532 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2533 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2534 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2535 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2536 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2537 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2538 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2539 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2540
Shawn Linb5b4ff02015-08-12 13:08:32 +08002541 /*
2542 * On Some Kingston eMMCs, performing trim can result in
2543 * unrecoverable data conrruption occasionally due to a firmware bug.
2544 */
2545 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2546 MMC_QUIRK_TRIM_BROKEN),
2547 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2548 MMC_QUIRK_TRIM_BROKEN),
2549
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002550 END_FIXUP
2551};
2552
Ulf Hansson96541ba2015-04-14 13:06:12 +02002553static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002555 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002556 char cap_str[10];
2557
Pierre Ossman912490d2005-05-21 10:27:02 +01002558 /*
2559 * Check that the card supports the command class(es) we need.
2560 */
2561 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return -ENODEV;
2563
Lukas Czerner5204d002014-06-18 13:18:07 +02002564 mmc_fixup_device(card, blk_fixups);
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 md = mmc_blk_alloc(card);
2567 if (IS_ERR(md))
2568 return PTR_ERR(md);
2569
James Bottomleyb9f28d82015-03-05 18:47:01 -08002570 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002571 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302572 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002574 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Andrei Warkentin371a6892011-04-11 18:10:25 -05002576 if (mmc_blk_alloc_parts(card, md))
2577 goto out;
2578
Ulf Hansson96541ba2015-04-14 13:06:12 +02002579 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002580
Andrei Warkentin371a6892011-04-11 18:10:25 -05002581 if (mmc_add_disk(md))
2582 goto out;
2583
2584 list_for_each_entry(part_md, &md->part, part) {
2585 if (mmc_add_disk(part_md))
2586 goto out;
2587 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002588
2589 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2590 pm_runtime_use_autosuspend(&card->dev);
2591
2592 /*
2593 * Don't enable runtime PM for SD-combo cards here. Leave that
2594 * decision to be taken during the SDIO init sequence instead.
2595 */
2596 if (card->type != MMC_TYPE_SD_COMBO) {
2597 pm_runtime_set_active(&card->dev);
2598 pm_runtime_enable(&card->dev);
2599 }
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602
2603 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002604 mmc_blk_remove_parts(card, md);
2605 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Ulf Hansson96541ba2015-04-14 13:06:12 +02002609static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002611 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Andrei Warkentin371a6892011-04-11 18:10:25 -05002613 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002614 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002615 mmc_claim_host(card->host);
2616 mmc_blk_part_switch(card, md);
2617 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002618 if (card->type != MMC_TYPE_SD_COMBO)
2619 pm_runtime_disable(&card->dev);
2620 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002621 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002622 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Ulf Hansson96541ba2015-04-14 13:06:12 +02002625static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002627 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002628 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 if (md) {
2631 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002632 list_for_each_entry(part_md, &md->part, part) {
2633 mmc_queue_suspend(&part_md->queue);
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636 return 0;
2637}
2638
Ulf Hansson96541ba2015-04-14 13:06:12 +02002639static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002640{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002641 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002642}
2643
Ulf Hansson0967edc2014-10-06 11:29:42 +02002644#ifdef CONFIG_PM_SLEEP
2645static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002646{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002647 struct mmc_card *card = mmc_dev_to_card(dev);
2648
2649 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002650}
2651
Ulf Hansson0967edc2014-10-06 11:29:42 +02002652static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002654 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002655 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002658 /*
2659 * Resume involves the card going into idle state,
2660 * so current partition is always the main one.
2661 */
2662 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002664 list_for_each_entry(part_md, &md->part, part) {
2665 mmc_queue_resume(&part_md->queue);
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
2668 return 0;
2669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670#endif
2671
Ulf Hansson0967edc2014-10-06 11:29:42 +02002672static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2673
Ulf Hansson96541ba2015-04-14 13:06:12 +02002674static struct mmc_driver mmc_driver = {
2675 .drv = {
2676 .name = "mmcblk",
2677 .pm = &mmc_blk_pm_ops,
2678 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .probe = mmc_blk_probe,
2680 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002681 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
2683
2684static int __init mmc_blk_init(void)
2685{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002686 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002688 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2689 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2690
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002691 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002692
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002693 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2694 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002697 res = mmc_register_driver(&mmc_driver);
2698 if (res)
2699 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002701 return 0;
2702 out2:
2703 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 out:
2705 return res;
2706}
2707
2708static void __exit mmc_blk_exit(void)
2709{
2710 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002711 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
2714module_init(mmc_blk_init);
2715module_exit(mmc_blk_exit);
2716
2717MODULE_LICENSE("GPL");
2718MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2719