blob: 80b05b280241227c3392ed21cb5a33b046aaa8f5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060060#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000
62#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050063
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090064#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
65 (req->cmd_flags & REQ_META)) && \
66 (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
80 * limited to 256 / number of minors per device.
81 */
82static int max_devices;
83
84/* 256 minors, so at most 256 separate devices */
85static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050086static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * There is one mmc_blk_data per slot.
90 */
91struct mmc_blk_data {
92 spinlock_t lock;
93 struct gendisk *disk;
94 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050095 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050097 unsigned int flags;
98#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
99#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900100#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000103 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500104 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500105 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300106 unsigned int reset_done;
107#define MMC_BLK_READ BIT(0)
108#define MMC_BLK_WRITE BIT(1)
109#define MMC_BLK_DISCARD BIT(2)
110#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500111
112 /*
113 * Only set in main mmc_blk_data associated
114 * with mmc_card with mmc_set_drvdata, and keeps
115 * track of the current selected device partition.
116 */
117 unsigned int part_curr;
118 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 struct device_attribute power_ro_lock;
120 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Arjan van de Vena621aae2006-01-12 18:43:35 +0000123static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900125enum {
126 MMC_PACKED_NR_IDX = -1,
127 MMC_PACKED_NR_ZERO,
128 MMC_PACKED_NR_SINGLE,
129};
130
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400131module_param(perdev_minors, int, 0444);
132MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
133
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200134static inline int mmc_blk_part_switch(struct mmc_card *card,
135 struct mmc_blk_data *md);
136static int get_card_status(struct mmc_card *card, u32 *status, int retries);
137
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900138static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
139{
140 struct mmc_packed *packed = mqrq->packed;
141
142 BUG_ON(!packed);
143
144 mqrq->cmd_type = MMC_PACKED_NONE;
145 packed->nr_entries = MMC_PACKED_NR_ZERO;
146 packed->idx_failure = MMC_PACKED_NR_IDX;
147 packed->retries = 0;
148 packed->blocks = 0;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
152{
153 struct mmc_blk_data *md;
154
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 md = disk->private_data;
157 if (md && md->usage == 0)
158 md = NULL;
159 if (md)
160 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return md;
164}
165
Andrei Warkentin371a6892011-04-11 18:10:25 -0500166static inline int mmc_get_devidx(struct gendisk *disk)
167{
168 int devmaj = MAJOR(disk_devt(disk));
169 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
170
171 if (!devmaj)
172 devidx = disk->first_minor / perdev_minors;
173 return devidx;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static void mmc_blk_put(struct mmc_blk_data *md)
177{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 md->usage--;
180 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500181 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800182 blk_cleanup_queue(md->queue.queue);
183
David Woodhouse1dff3142007-11-21 18:45:12 +0100184 __clear_bit(devidx, dev_use);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(md);
188 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000189 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Johan Rudholmadd710e2011-12-02 08:51:06 +0100192static ssize_t power_ro_lock_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 int ret;
196 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
197 struct mmc_card *card = md->queue.card;
198 int locked = 0;
199
200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
201 locked = 2;
202 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
203 locked = 1;
204
205 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
206
207 return ret;
208}
209
210static ssize_t power_ro_lock_store(struct device *dev,
211 struct device_attribute *attr, const char *buf, size_t count)
212{
213 int ret;
214 struct mmc_blk_data *md, *part_md;
215 struct mmc_card *card;
216 unsigned long set;
217
218 if (kstrtoul(buf, 0, &set))
219 return -EINVAL;
220
221 if (set != 1)
222 return count;
223
224 md = mmc_blk_get(dev_to_disk(dev));
225 card = md->queue.card;
226
227 mmc_claim_host(card->host);
228
229 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
230 card->ext_csd.boot_ro_lock |
231 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
232 card->ext_csd.part_time);
233 if (ret)
234 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
235 else
236 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
237
238 mmc_release_host(card->host);
239
240 if (!ret) {
241 pr_info("%s: Locking boot partition ro until next power on\n",
242 md->disk->disk_name);
243 set_disk_ro(md->disk, 1);
244
245 list_for_each_entry(part_md, &md->part, part)
246 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
247 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
248 set_disk_ro(part_md->disk, 1);
249 }
250 }
251
252 mmc_blk_put(md);
253 return count;
254}
255
Andrei Warkentin371a6892011-04-11 18:10:25 -0500256static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
257 char *buf)
258{
259 int ret;
260 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
261
262 ret = snprintf(buf, PAGE_SIZE, "%d",
263 get_disk_ro(dev_to_disk(dev)) ^
264 md->read_only);
265 mmc_blk_put(md);
266 return ret;
267}
268
269static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
270 const char *buf, size_t count)
271{
272 int ret;
273 char *end;
274 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
275 unsigned long set = simple_strtoul(buf, &end, 0);
276 if (end == buf) {
277 ret = -EINVAL;
278 goto out;
279 }
280
281 set_disk_ro(dev_to_disk(dev), set || md->read_only);
282 ret = count;
283out:
284 mmc_blk_put(md);
285 return ret;
286}
287
Al Viroa5a15612008-03-02 10:33:30 -0500288static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Al Viroa5a15612008-03-02 10:33:30 -0500290 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int ret = -ENXIO;
292
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200293 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (md) {
295 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500296 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700298
Al Viroa5a15612008-03-02 10:33:30 -0500299 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700300 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700301 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200304 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 return ret;
307}
308
Al Virodb2a1442013-05-05 21:52:57 -0400309static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Al Viroa5a15612008-03-02 10:33:30 -0500311 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200313 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200315 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800319mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800321 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
322 geo->heads = 4;
323 geo->sectors = 16;
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
John Calixtocb87ea22011-04-26 18:56:29 -0400327struct mmc_blk_ioc_data {
328 struct mmc_ioc_cmd ic;
329 unsigned char *buf;
330 u64 buf_bytes;
331};
332
333static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
334 struct mmc_ioc_cmd __user *user)
335{
336 struct mmc_blk_ioc_data *idata;
337 int err;
338
339 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
340 if (!idata) {
341 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400342 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400343 }
344
345 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
346 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400347 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400348 }
349
350 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
351 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
352 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400353 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400354 }
355
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100356 if (!idata->buf_bytes)
357 return idata;
358
John Calixtocb87ea22011-04-26 18:56:29 -0400359 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
360 if (!idata->buf) {
361 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400362 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400363 }
364
365 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
366 idata->ic.data_ptr, idata->buf_bytes)) {
367 err = -EFAULT;
368 goto copy_err;
369 }
370
371 return idata;
372
373copy_err:
374 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400376 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400377out:
John Calixtocb87ea22011-04-26 18:56:29 -0400378 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400379}
380
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200381static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
382 u32 retries_max)
383{
384 int err;
385 u32 retry_count = 0;
386
387 if (!status || !retries_max)
388 return -EINVAL;
389
390 do {
391 err = get_card_status(card, status, 5);
392 if (err)
393 break;
394
395 if (!R1_STATUS(*status) &&
396 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
397 break; /* RPMB programming operation complete */
398
399 /*
400 * Rechedule to give the MMC device a chance to continue
401 * processing the previous command without being polled too
402 * frequently.
403 */
404 usleep_range(1000, 5000);
405 } while (++retry_count < retries_max);
406
407 if (retry_count == retries_max)
408 err = -EPERM;
409
410 return err;
411}
412
Maya Erez775a9362013-04-18 15:41:55 +0300413static int ioctl_do_sanitize(struct mmc_card *card)
414{
415 int err;
416
417 if (!(mmc_can_sanitize(card) &&
418 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
419 pr_warn("%s: %s - SANITIZE is not supported\n",
420 mmc_hostname(card->host), __func__);
421 err = -EOPNOTSUPP;
422 goto out;
423 }
424
425 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
426 mmc_hostname(card->host), __func__);
427
428 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
429 EXT_CSD_SANITIZE_START, 1,
430 MMC_SANITIZE_REQ_TIMEOUT);
431
432 if (err)
433 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
434 mmc_hostname(card->host), __func__, err);
435
436 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
437 __func__);
438out:
439 return err;
440}
441
John Calixtocb87ea22011-04-26 18:56:29 -0400442static int mmc_blk_ioctl_cmd(struct block_device *bdev,
443 struct mmc_ioc_cmd __user *ic_ptr)
444{
445 struct mmc_blk_ioc_data *idata;
446 struct mmc_blk_data *md;
447 struct mmc_card *card;
448 struct mmc_command cmd = {0};
449 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530450 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400451 struct scatterlist sg;
452 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200453 int is_rpmb = false;
454 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400455
456 /*
457 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
458 * whole block device, not on a partition. This prevents overspray
459 * between sibling partitions.
460 */
461 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
462 return -EPERM;
463
464 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
465 if (IS_ERR(idata))
466 return PTR_ERR(idata);
467
John Calixtocb87ea22011-04-26 18:56:29 -0400468 md = mmc_blk_get(bdev->bd_disk);
469 if (!md) {
470 err = -EINVAL;
Philippe De Swert1c02f002012-04-11 23:31:45 +0300471 goto cmd_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400472 }
473
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200474 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
475 is_rpmb = true;
476
John Calixtocb87ea22011-04-26 18:56:29 -0400477 card = md->queue.card;
478 if (IS_ERR(card)) {
479 err = PTR_ERR(card);
480 goto cmd_done;
481 }
482
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100483 cmd.opcode = idata->ic.opcode;
484 cmd.arg = idata->ic.arg;
485 cmd.flags = idata->ic.flags;
486
487 if (idata->buf_bytes) {
488 data.sg = &sg;
489 data.sg_len = 1;
490 data.blksz = idata->ic.blksz;
491 data.blocks = idata->ic.blocks;
492
493 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
494
495 if (idata->ic.write_flag)
496 data.flags = MMC_DATA_WRITE;
497 else
498 data.flags = MMC_DATA_READ;
499
500 /* data.flags must already be set before doing this. */
501 mmc_set_data_timeout(&data, card);
502
503 /* Allow overriding the timeout_ns for empirical tuning. */
504 if (idata->ic.data_timeout_ns)
505 data.timeout_ns = idata->ic.data_timeout_ns;
506
507 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
508 /*
509 * Pretend this is a data transfer and rely on the
510 * host driver to compute timeout. When all host
511 * drivers support cmd.cmd_timeout for R1B, this
512 * can be changed to:
513 *
514 * mrq.data = NULL;
515 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
516 */
517 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
518 }
519
520 mrq.data = &data;
521 }
522
523 mrq.cmd = &cmd;
524
John Calixtocb87ea22011-04-26 18:56:29 -0400525 mmc_claim_host(card->host);
526
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200527 err = mmc_blk_part_switch(card, md);
528 if (err)
529 goto cmd_rel_host;
530
John Calixtocb87ea22011-04-26 18:56:29 -0400531 if (idata->ic.is_acmd) {
532 err = mmc_app_cmd(card->host, card);
533 if (err)
534 goto cmd_rel_host;
535 }
536
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200537 if (is_rpmb) {
538 err = mmc_set_blockcount(card, data.blocks,
539 idata->ic.write_flag & (1 << 31));
540 if (err)
541 goto cmd_rel_host;
542 }
543
Maya Erez775a9362013-04-18 15:41:55 +0300544 if (MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) {
545 err = ioctl_do_sanitize(card);
546
547 if (err)
548 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
549 __func__, err);
550
551 goto cmd_rel_host;
552 }
553
John Calixtocb87ea22011-04-26 18:56:29 -0400554 mmc_wait_for_req(card->host, &mrq);
555
556 if (cmd.error) {
557 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
558 __func__, cmd.error);
559 err = cmd.error;
560 goto cmd_rel_host;
561 }
562 if (data.error) {
563 dev_err(mmc_dev(card->host), "%s: data error %d\n",
564 __func__, data.error);
565 err = data.error;
566 goto cmd_rel_host;
567 }
568
569 /*
570 * According to the SD specs, some commands require a delay after
571 * issuing the command.
572 */
573 if (idata->ic.postsleep_min_us)
574 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
575
576 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
577 err = -EFAULT;
578 goto cmd_rel_host;
579 }
580
581 if (!idata->ic.write_flag) {
582 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
583 idata->buf, idata->buf_bytes)) {
584 err = -EFAULT;
585 goto cmd_rel_host;
586 }
587 }
588
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200589 if (is_rpmb) {
590 /*
591 * Ensure RPMB command has completed by polling CMD13
592 * "Send Status".
593 */
594 err = ioctl_rpmb_card_status_poll(card, &status, 5);
595 if (err)
596 dev_err(mmc_dev(card->host),
597 "%s: Card Status=0x%08X, error %d\n",
598 __func__, status, err);
599 }
600
John Calixtocb87ea22011-04-26 18:56:29 -0400601cmd_rel_host:
602 mmc_release_host(card->host);
603
604cmd_done:
605 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300606cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400607 kfree(idata->buf);
608 kfree(idata);
609 return err;
610}
611
612static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
613 unsigned int cmd, unsigned long arg)
614{
615 int ret = -EINVAL;
616 if (cmd == MMC_IOC_CMD)
617 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
618 return ret;
619}
620
621#ifdef CONFIG_COMPAT
622static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
623 unsigned int cmd, unsigned long arg)
624{
625 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
626}
627#endif
628
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700629static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500630 .open = mmc_blk_open,
631 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800632 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400634 .ioctl = mmc_blk_ioctl,
635#ifdef CONFIG_COMPAT
636 .compat_ioctl = mmc_blk_compat_ioctl,
637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638};
639
Andrei Warkentin371a6892011-04-11 18:10:25 -0500640static inline int mmc_blk_part_switch(struct mmc_card *card,
641 struct mmc_blk_data *md)
642{
643 int ret;
644 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300645
Andrei Warkentin371a6892011-04-11 18:10:25 -0500646 if (main_md->part_curr == md->part_type)
647 return 0;
648
649 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300650 u8 part_config = card->ext_csd.part_config;
651
652 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
653 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500654
655 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300656 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500657 card->ext_csd.part_time);
658 if (ret)
659 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300660
661 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300662 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500663
664 main_md->part_curr = md->part_type;
665 return 0;
666}
667
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700668static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
669{
670 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100671 u32 result;
672 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700673
Venkatraman Sad5fd972011-08-25 00:30:50 +0530674 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400675 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400676 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700677
678 struct scatterlist sg;
679
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700680 cmd.opcode = MMC_APP_CMD;
681 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700682 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700683
684 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700685 if (err)
686 return (u32)-1;
687 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700688 return (u32)-1;
689
690 memset(&cmd, 0, sizeof(struct mmc_command));
691
692 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
693 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700694 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700695
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700696 data.blksz = 4;
697 data.blocks = 1;
698 data.flags = MMC_DATA_READ;
699 data.sg = &sg;
700 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530701 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700702
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700703 mrq.cmd = &cmd;
704 mrq.data = &data;
705
Ben Dooks051913d2009-06-08 23:33:57 +0100706 blocks = kmalloc(4, GFP_KERNEL);
707 if (!blocks)
708 return (u32)-1;
709
710 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700711
712 mmc_wait_for_req(card->host, &mrq);
713
Ben Dooks051913d2009-06-08 23:33:57 +0100714 result = ntohl(*blocks);
715 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700716
Ben Dooks051913d2009-06-08 23:33:57 +0100717 if (cmd.error || data.error)
718 result = (u32)-1;
719
720 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700721}
722
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100723static int send_stop(struct mmc_card *card, u32 *status)
724{
725 struct mmc_command cmd = {0};
726 int err;
727
728 cmd.opcode = MMC_STOP_TRANSMISSION;
729 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
730 err = mmc_wait_for_cmd(card->host, &cmd, 5);
731 if (err == 0)
732 *status = cmd.resp[0];
733 return err;
734}
735
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100736static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300737{
Chris Ball1278dba2011-04-13 23:40:30 -0400738 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300739 int err;
740
Adrian Hunter504f1912008-10-16 12:55:25 +0300741 cmd.opcode = MMC_SEND_STATUS;
742 if (!mmc_host_is_spi(card->host))
743 cmd.arg = card->rca << 16;
744 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100745 err = mmc_wait_for_cmd(card->host, &cmd, retries);
746 if (err == 0)
747 *status = cmd.resp[0];
748 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300749}
750
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530751#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100752#define ERR_RETRY 2
753#define ERR_ABORT 1
754#define ERR_CONTINUE 0
755
756static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
757 bool status_valid, u32 status)
758{
759 switch (error) {
760 case -EILSEQ:
761 /* response crc error, retry the r/w cmd */
762 pr_err("%s: %s sending %s command, card status %#x\n",
763 req->rq_disk->disk_name, "response CRC error",
764 name, status);
765 return ERR_RETRY;
766
767 case -ETIMEDOUT:
768 pr_err("%s: %s sending %s command, card status %#x\n",
769 req->rq_disk->disk_name, "timed out", name, status);
770
771 /* If the status cmd initially failed, retry the r/w cmd */
772 if (!status_valid)
773 return ERR_RETRY;
774
775 /*
776 * If it was a r/w cmd crc error, or illegal command
777 * (eg, issued in wrong state) then retry - we should
778 * have corrected the state problem above.
779 */
780 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
781 return ERR_RETRY;
782
783 /* Otherwise abort the command */
784 return ERR_ABORT;
785
786 default:
787 /* We don't understand the error code the driver gave us */
788 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
789 req->rq_disk->disk_name, error, status);
790 return ERR_ABORT;
791 }
792}
793
794/*
795 * Initial r/w and stop cmd error recovery.
796 * We don't know whether the card received the r/w cmd or not, so try to
797 * restore things back to a sane state. Essentially, we do this as follows:
798 * - Obtain card status. If the first attempt to obtain card status fails,
799 * the status word will reflect the failed status cmd, not the failed
800 * r/w cmd. If we fail to obtain card status, it suggests we can no
801 * longer communicate with the card.
802 * - Check the card state. If the card received the cmd but there was a
803 * transient problem with the response, it might still be in a data transfer
804 * mode. Try to send it a stop command. If this fails, we can't recover.
805 * - If the r/w cmd failed due to a response CRC error, it was probably
806 * transient, so retry the cmd.
807 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
808 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
809 * illegal cmd, retry.
810 * Otherwise we don't understand what happened, so abort.
811 */
812static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300813 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100814{
815 bool prev_cmd_status_valid = true;
816 u32 status, stop_status = 0;
817 int err, retry;
818
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530819 if (mmc_card_removed(card))
820 return ERR_NOMEDIUM;
821
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100822 /*
823 * Try to get card status which indicates both the card state
824 * and why there was no response. If the first attempt fails,
825 * we can't be sure the returned status is for the r/w command.
826 */
827 for (retry = 2; retry >= 0; retry--) {
828 err = get_card_status(card, &status, 0);
829 if (!err)
830 break;
831
832 prev_cmd_status_valid = false;
833 pr_err("%s: error %d sending status command, %sing\n",
834 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
835 }
836
837 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530838 if (err) {
839 /* Check if the card is removed */
840 if (mmc_detect_card_removed(card->host))
841 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100842 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530843 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100844
Adrian Hunter67716322011-08-29 16:42:15 +0300845 /* Flag ECC errors */
846 if ((status & R1_CARD_ECC_FAILED) ||
847 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
848 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
849 *ecc_err = 1;
850
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100851 /*
852 * Check the current card state. If it is in some data transfer
853 * mode, tell it to stop (and hopefully transition back to TRAN.)
854 */
855 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
856 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
857 err = send_stop(card, &stop_status);
858 if (err)
859 pr_err("%s: error %d sending stop command\n",
860 req->rq_disk->disk_name, err);
861
862 /*
863 * If the stop cmd also timed out, the card is probably
864 * not present, so abort. Other errors are bad news too.
865 */
866 if (err)
867 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300868 if (stop_status & R1_CARD_ECC_FAILED)
869 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100870 }
871
872 /* Check for set block count errors */
873 if (brq->sbc.error)
874 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
875 prev_cmd_status_valid, status);
876
877 /* Check for r/w command errors */
878 if (brq->cmd.error)
879 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
880 prev_cmd_status_valid, status);
881
Adrian Hunter67716322011-08-29 16:42:15 +0300882 /* Data errors */
883 if (!brq->stop.error)
884 return ERR_CONTINUE;
885
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100886 /* Now for stop errors. These aren't fatal to the transfer. */
887 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
888 req->rq_disk->disk_name, brq->stop.error,
889 brq->cmd.resp[0], status);
890
891 /*
892 * Subsitute in our own stop status as this will give the error
893 * state which happened during the execution of the r/w command.
894 */
895 if (stop_status) {
896 brq->stop.resp[0] = stop_status;
897 brq->stop.error = 0;
898 }
899 return ERR_CONTINUE;
900}
901
Adrian Hunter67716322011-08-29 16:42:15 +0300902static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
903 int type)
904{
905 int err;
906
907 if (md->reset_done & type)
908 return -EEXIST;
909
910 md->reset_done |= type;
911 err = mmc_hw_reset(host);
912 /* Ensure we switch back to the correct partition */
913 if (err != -EOPNOTSUPP) {
914 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
915 int part_err;
916
917 main_md->part_curr = main_md->part_type;
918 part_err = mmc_blk_part_switch(host->card, md);
919 if (part_err) {
920 /*
921 * We have failed to get back into the correct
922 * partition, so we need to abort the whole request.
923 */
924 return -ENODEV;
925 }
926 }
927 return err;
928}
929
930static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
931{
932 md->reset_done &= ~type;
933}
934
Adrian Hunterbd788c92010-08-11 14:17:47 -0700935static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
936{
937 struct mmc_blk_data *md = mq->data;
938 struct mmc_card *card = md->queue.card;
939 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300940 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700941
Adrian Hunterbd788c92010-08-11 14:17:47 -0700942 if (!mmc_can_erase(card)) {
943 err = -EOPNOTSUPP;
944 goto out;
945 }
946
947 from = blk_rq_pos(req);
948 nr = blk_rq_sectors(req);
949
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900950 if (mmc_can_discard(card))
951 arg = MMC_DISCARD_ARG;
952 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700953 arg = MMC_TRIM_ARG;
954 else
955 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300956retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500957 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
958 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
959 INAND_CMD38_ARG_EXT_CSD,
960 arg == MMC_TRIM_ARG ?
961 INAND_CMD38_ARG_TRIM :
962 INAND_CMD38_ARG_ERASE,
963 0);
964 if (err)
965 goto out;
966 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700967 err = mmc_erase(card, from, nr, arg);
968out:
Adrian Hunter67716322011-08-29 16:42:15 +0300969 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
970 goto retry;
971 if (!err)
972 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +0530973 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700974
Adrian Hunterbd788c92010-08-11 14:17:47 -0700975 return err ? 0 : 1;
976}
977
Adrian Hunter49804542010-08-11 14:17:50 -0700978static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
979 struct request *req)
980{
981 struct mmc_blk_data *md = mq->data;
982 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +0300983 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300984 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700985
Maya Erez775a9362013-04-18 15:41:55 +0300986 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700987 err = -EOPNOTSUPP;
988 goto out;
989 }
990
991 from = blk_rq_pos(req);
992 nr = blk_rq_sectors(req);
993
Maya Erez775a9362013-04-18 15:41:55 +0300994 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
995 arg = MMC_SECURE_TRIM1_ARG;
996 else
997 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +0300998
Adrian Hunter67716322011-08-29 16:42:15 +0300999retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001000 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1001 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1002 INAND_CMD38_ARG_EXT_CSD,
1003 arg == MMC_SECURE_TRIM1_ARG ?
1004 INAND_CMD38_ARG_SECTRIM1 :
1005 INAND_CMD38_ARG_SECERASE,
1006 0);
1007 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001008 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001009 }
Adrian Hunter28302812012-04-05 14:45:48 +03001010
Adrian Hunter49804542010-08-11 14:17:50 -07001011 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001012 if (err == -EIO)
1013 goto out_retry;
1014 if (err)
1015 goto out;
1016
1017 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001018 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1019 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1020 INAND_CMD38_ARG_EXT_CSD,
1021 INAND_CMD38_ARG_SECTRIM2,
1022 0);
1023 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001024 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001025 }
Adrian Hunter28302812012-04-05 14:45:48 +03001026
Adrian Hunter49804542010-08-11 14:17:50 -07001027 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001028 if (err == -EIO)
1029 goto out_retry;
1030 if (err)
1031 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001032 }
Adrian Hunter28302812012-04-05 14:45:48 +03001033
Adrian Hunter28302812012-04-05 14:45:48 +03001034out_retry:
1035 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001036 goto retry;
1037 if (!err)
1038 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001039out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301040 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001041
Adrian Hunter49804542010-08-11 14:17:50 -07001042 return err ? 0 : 1;
1043}
1044
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001045static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1046{
1047 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001048 struct mmc_card *card = md->queue.card;
1049 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001050
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001051 ret = mmc_flush_cache(card);
1052 if (ret)
1053 ret = -EIO;
1054
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301055 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001056
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001057 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001058}
1059
1060/*
1061 * Reformat current write as a reliable write, supporting
1062 * both legacy and the enhanced reliable write MMC cards.
1063 * In each transfer we'll handle only as much as a single
1064 * reliable write can handle, thus finish the request in
1065 * partial completions.
1066 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001067static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1068 struct mmc_card *card,
1069 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001070{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001071 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1072 /* Legacy mode imposes restrictions on transfers. */
1073 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1074 brq->data.blocks = 1;
1075
1076 if (brq->data.blocks > card->ext_csd.rel_sectors)
1077 brq->data.blocks = card->ext_csd.rel_sectors;
1078 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1079 brq->data.blocks = 1;
1080 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001081}
1082
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001083#define CMD_ERRORS \
1084 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1085 R1_ADDRESS_ERROR | /* Misaligned address */ \
1086 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1087 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1088 R1_CC_ERROR | /* Card controller error */ \
1089 R1_ERROR) /* General/unknown error */
1090
Per Forlinee8a43a2011-07-01 18:55:33 +02001091static int mmc_blk_err_check(struct mmc_card *card,
1092 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001093{
Per Forlinee8a43a2011-07-01 18:55:33 +02001094 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1095 mmc_active);
1096 struct mmc_blk_request *brq = &mq_mrq->brq;
1097 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001098 int ecc_err = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001099
1100 /*
1101 * sbc.error indicates a problem with the set block count
1102 * command. No data will have been transferred.
1103 *
1104 * cmd.error indicates a problem with the r/w command. No
1105 * data will have been transferred.
1106 *
1107 * stop.error indicates a problem with the stop command. Data
1108 * may have been transferred, or may still be transferring.
1109 */
Adrian Hunter67716322011-08-29 16:42:15 +03001110 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1111 brq->data.error) {
1112 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001113 case ERR_RETRY:
1114 return MMC_BLK_RETRY;
1115 case ERR_ABORT:
1116 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301117 case ERR_NOMEDIUM:
1118 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001119 case ERR_CONTINUE:
1120 break;
1121 }
1122 }
1123
1124 /*
1125 * Check for errors relating to the execution of the
1126 * initial command - such as address errors. No data
1127 * has been transferred.
1128 */
1129 if (brq->cmd.resp[0] & CMD_ERRORS) {
1130 pr_err("%s: r/w command failed, status = %#x\n",
1131 req->rq_disk->disk_name, brq->cmd.resp[0]);
1132 return MMC_BLK_ABORT;
1133 }
1134
1135 /*
1136 * Everything else is either success, or a data error of some
1137 * kind. If it was a write, we may have transitioned to
1138 * program mode, which we have to wait for it to complete.
1139 */
1140 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1141 u32 status;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001142 unsigned long timeout;
1143
1144 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a8a2011-07-01 18:55:30 +02001145 do {
1146 int err = get_card_status(card, &status, 5);
1147 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301148 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a8a2011-07-01 18:55:30 +02001149 req->rq_disk->disk_name, err);
1150 return MMC_BLK_CMD_ERR;
1151 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06001152
1153 /* Timeout if the device never becomes ready for data
1154 * and never leaves the program state.
1155 */
1156 if (time_after(jiffies, timeout)) {
1157 pr_err("%s: Card stuck in programming state!"\
1158 " %s %s\n", mmc_hostname(card->host),
1159 req->rq_disk->disk_name, __func__);
1160
1161 return MMC_BLK_CMD_ERR;
1162 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001163 /*
1164 * Some cards mishandle the status bits,
1165 * so make sure to check both the busy
1166 * indication and the card state.
1167 */
1168 } while (!(status & R1_READY_FOR_DATA) ||
1169 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1170 }
1171
1172 if (brq->data.error) {
1173 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1174 req->rq_disk->disk_name, brq->data.error,
1175 (unsigned)blk_rq_pos(req),
1176 (unsigned)blk_rq_sectors(req),
1177 brq->cmd.resp[0], brq->stop.resp[0]);
1178
1179 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001180 if (ecc_err)
1181 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001182 return MMC_BLK_DATA_ERR;
1183 } else {
1184 return MMC_BLK_CMD_ERR;
1185 }
1186 }
1187
Adrian Hunter67716322011-08-29 16:42:15 +03001188 if (!brq->data.bytes_xfered)
1189 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001190
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001191 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1192 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1193 return MMC_BLK_PARTIAL;
1194 else
1195 return MMC_BLK_SUCCESS;
1196 }
1197
Adrian Hunter67716322011-08-29 16:42:15 +03001198 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1199 return MMC_BLK_PARTIAL;
1200
1201 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001202}
1203
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001204static int mmc_blk_packed_err_check(struct mmc_card *card,
1205 struct mmc_async_req *areq)
1206{
1207 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1208 mmc_active);
1209 struct request *req = mq_rq->req;
1210 struct mmc_packed *packed = mq_rq->packed;
1211 int err, check, status;
1212 u8 *ext_csd;
1213
1214 BUG_ON(!packed);
1215
1216 packed->retries--;
1217 check = mmc_blk_err_check(card, areq);
1218 err = get_card_status(card, &status, 0);
1219 if (err) {
1220 pr_err("%s: error %d sending status command\n",
1221 req->rq_disk->disk_name, err);
1222 return MMC_BLK_ABORT;
1223 }
1224
1225 if (status & R1_EXCEPTION_EVENT) {
1226 ext_csd = kzalloc(512, GFP_KERNEL);
1227 if (!ext_csd) {
1228 pr_err("%s: unable to allocate buffer for ext_csd\n",
1229 req->rq_disk->disk_name);
1230 return -ENOMEM;
1231 }
1232
1233 err = mmc_send_ext_csd(card, ext_csd);
1234 if (err) {
1235 pr_err("%s: error %d sending ext_csd\n",
1236 req->rq_disk->disk_name, err);
1237 check = MMC_BLK_ABORT;
1238 goto free;
1239 }
1240
1241 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1242 EXT_CSD_PACKED_FAILURE) &&
1243 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1244 EXT_CSD_PACKED_GENERIC_ERROR)) {
1245 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1246 EXT_CSD_PACKED_INDEXED_ERROR) {
1247 packed->idx_failure =
1248 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1249 check = MMC_BLK_PARTIAL;
1250 }
1251 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1252 "failure index: %d\n",
1253 req->rq_disk->disk_name, packed->nr_entries,
1254 packed->blocks, packed->idx_failure);
1255 }
1256free:
1257 kfree(ext_csd);
1258 }
1259
1260 return check;
1261}
1262
Per Forlin54d49d72011-07-01 18:55:29 +02001263static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1264 struct mmc_card *card,
1265 int disable_multi,
1266 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Per Forlin54d49d72011-07-01 18:55:29 +02001268 u32 readcmd, writecmd;
1269 struct mmc_blk_request *brq = &mqrq->brq;
1270 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301272 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001274 /*
1275 * Reliable writes are used to implement Forced Unit Access and
1276 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001277 *
1278 * XXX: this really needs a good explanation of why REQ_META
1279 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001280 */
1281 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1282 (req->cmd_flags & REQ_META)) &&
1283 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001284 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001285
Per Forlin54d49d72011-07-01 18:55:29 +02001286 memset(brq, 0, sizeof(struct mmc_blk_request));
1287 brq->mrq.cmd = &brq->cmd;
1288 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Per Forlin54d49d72011-07-01 18:55:29 +02001290 brq->cmd.arg = blk_rq_pos(req);
1291 if (!mmc_card_blockaddr(card))
1292 brq->cmd.arg <<= 9;
1293 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1294 brq->data.blksz = 512;
1295 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1296 brq->stop.arg = 0;
1297 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1298 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Per Forlin54d49d72011-07-01 18:55:29 +02001300 /*
1301 * The block layer doesn't support all sector count
1302 * restrictions, so we need to be prepared for too big
1303 * requests.
1304 */
1305 if (brq->data.blocks > card->host->max_blk_count)
1306 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001308 if (brq->data.blocks > 1) {
1309 /*
1310 * After a read error, we redo the request one sector
1311 * at a time in order to accurately determine which
1312 * sectors can be read successfully.
1313 */
1314 if (disable_multi)
1315 brq->data.blocks = 1;
1316
1317 /* Some controllers can't do multiblock reads due to hw bugs */
1318 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1319 rq_data_dir(req) == READ)
1320 brq->data.blocks = 1;
1321 }
Per Forlin54d49d72011-07-01 18:55:29 +02001322
1323 if (brq->data.blocks > 1 || do_rel_wr) {
1324 /* SPI multiblock writes terminate using a special
1325 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001326 */
Per Forlin54d49d72011-07-01 18:55:29 +02001327 if (!mmc_host_is_spi(card->host) ||
1328 rq_data_dir(req) == READ)
1329 brq->mrq.stop = &brq->stop;
1330 readcmd = MMC_READ_MULTIPLE_BLOCK;
1331 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1332 } else {
1333 brq->mrq.stop = NULL;
1334 readcmd = MMC_READ_SINGLE_BLOCK;
1335 writecmd = MMC_WRITE_BLOCK;
1336 }
1337 if (rq_data_dir(req) == READ) {
1338 brq->cmd.opcode = readcmd;
1339 brq->data.flags |= MMC_DATA_READ;
1340 } else {
1341 brq->cmd.opcode = writecmd;
1342 brq->data.flags |= MMC_DATA_WRITE;
1343 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001344
Per Forlin54d49d72011-07-01 18:55:29 +02001345 if (do_rel_wr)
1346 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001347
Per Forlin54d49d72011-07-01 18:55:29 +02001348 /*
Saugata Das42659002011-12-21 13:09:17 +05301349 * Data tag is used only during writing meta data to speed
1350 * up write and any subsequent read of this meta data
1351 */
1352 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1353 (req->cmd_flags & REQ_META) &&
1354 (rq_data_dir(req) == WRITE) &&
1355 ((brq->data.blocks * brq->data.blksz) >=
1356 card->ext_csd.data_tag_unit_size);
1357
1358 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001359 * Pre-defined multi-block transfers are preferable to
1360 * open ended-ones (and necessary for reliable writes).
1361 * However, it is not sufficient to just send CMD23,
1362 * and avoid the final CMD12, as on an error condition
1363 * CMD12 (stop) needs to be sent anyway. This, coupled
1364 * with Auto-CMD23 enhancements provided by some
1365 * hosts, means that the complexity of dealing
1366 * with this is best left to the host. If CMD23 is
1367 * supported by card and host, we'll fill sbc in and let
1368 * the host deal with handling it correctly. This means
1369 * that for hosts that don't expose MMC_CAP_CMD23, no
1370 * change of behavior will be observed.
1371 *
1372 * N.B: Some MMC cards experience perf degradation.
1373 * We'll avoid using CMD23-bounded multiblock writes for
1374 * these, while retaining features like reliable writes.
1375 */
Saugata Das42659002011-12-21 13:09:17 +05301376 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1377 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1378 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001379 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1380 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301381 (do_rel_wr ? (1 << 31) : 0) |
1382 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001383 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1384 brq->mrq.sbc = &brq->sbc;
1385 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001386
Per Forlin54d49d72011-07-01 18:55:29 +02001387 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001388
Per Forlin54d49d72011-07-01 18:55:29 +02001389 brq->data.sg = mqrq->sg;
1390 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001391
Per Forlin54d49d72011-07-01 18:55:29 +02001392 /*
1393 * Adjust the sg list so it is the same size as the
1394 * request.
1395 */
1396 if (brq->data.blocks != blk_rq_sectors(req)) {
1397 int i, data_size = brq->data.blocks << 9;
1398 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001399
Per Forlin54d49d72011-07-01 18:55:29 +02001400 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1401 data_size -= sg->length;
1402 if (data_size <= 0) {
1403 sg->length += data_size;
1404 i++;
1405 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001406 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001407 }
Per Forlin54d49d72011-07-01 18:55:29 +02001408 brq->data.sg_len = i;
1409 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001410
Per Forlinee8a43a2011-07-01 18:55:33 +02001411 mqrq->mmc_active.mrq = &brq->mrq;
1412 mqrq->mmc_active.err_check = mmc_blk_err_check;
1413
Per Forlin54d49d72011-07-01 18:55:29 +02001414 mmc_queue_bounce_pre(mqrq);
1415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001417static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1418 struct mmc_card *card)
1419{
1420 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1421 unsigned int max_seg_sz = queue_max_segment_size(q);
1422 unsigned int len, nr_segs = 0;
1423
1424 do {
1425 len = min(hdr_sz, max_seg_sz);
1426 hdr_sz -= len;
1427 nr_segs++;
1428 } while (hdr_sz);
1429
1430 return nr_segs;
1431}
1432
1433static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1434{
1435 struct request_queue *q = mq->queue;
1436 struct mmc_card *card = mq->card;
1437 struct request *cur = req, *next = NULL;
1438 struct mmc_blk_data *md = mq->data;
1439 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1440 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1441 unsigned int req_sectors = 0, phys_segments = 0;
1442 unsigned int max_blk_count, max_phys_segs;
1443 bool put_back = true;
1444 u8 max_packed_rw = 0;
1445 u8 reqs = 0;
1446
1447 if (!(md->flags & MMC_BLK_PACKED_CMD))
1448 goto no_packed;
1449
1450 if ((rq_data_dir(cur) == WRITE) &&
1451 mmc_host_packed_wr(card->host))
1452 max_packed_rw = card->ext_csd.max_packed_writes;
1453
1454 if (max_packed_rw == 0)
1455 goto no_packed;
1456
1457 if (mmc_req_rel_wr(cur) &&
1458 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1459 goto no_packed;
1460
1461 if (mmc_large_sector(card) &&
1462 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1463 goto no_packed;
1464
1465 mmc_blk_clear_packed(mqrq);
1466
1467 max_blk_count = min(card->host->max_blk_count,
1468 card->host->max_req_size >> 9);
1469 if (unlikely(max_blk_count > 0xffff))
1470 max_blk_count = 0xffff;
1471
1472 max_phys_segs = queue_max_segments(q);
1473 req_sectors += blk_rq_sectors(cur);
1474 phys_segments += cur->nr_phys_segments;
1475
1476 if (rq_data_dir(cur) == WRITE) {
1477 req_sectors += mmc_large_sector(card) ? 8 : 1;
1478 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1479 }
1480
1481 do {
1482 if (reqs >= max_packed_rw - 1) {
1483 put_back = false;
1484 break;
1485 }
1486
1487 spin_lock_irq(q->queue_lock);
1488 next = blk_fetch_request(q);
1489 spin_unlock_irq(q->queue_lock);
1490 if (!next) {
1491 put_back = false;
1492 break;
1493 }
1494
1495 if (mmc_large_sector(card) &&
1496 !IS_ALIGNED(blk_rq_sectors(next), 8))
1497 break;
1498
1499 if (next->cmd_flags & REQ_DISCARD ||
1500 next->cmd_flags & REQ_FLUSH)
1501 break;
1502
1503 if (rq_data_dir(cur) != rq_data_dir(next))
1504 break;
1505
1506 if (mmc_req_rel_wr(next) &&
1507 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1508 break;
1509
1510 req_sectors += blk_rq_sectors(next);
1511 if (req_sectors > max_blk_count)
1512 break;
1513
1514 phys_segments += next->nr_phys_segments;
1515 if (phys_segments > max_phys_segs)
1516 break;
1517
1518 list_add_tail(&next->queuelist, &mqrq->packed->list);
1519 cur = next;
1520 reqs++;
1521 } while (1);
1522
1523 if (put_back) {
1524 spin_lock_irq(q->queue_lock);
1525 blk_requeue_request(q, next);
1526 spin_unlock_irq(q->queue_lock);
1527 }
1528
1529 if (reqs > 0) {
1530 list_add(&req->queuelist, &mqrq->packed->list);
1531 mqrq->packed->nr_entries = ++reqs;
1532 mqrq->packed->retries = reqs;
1533 return reqs;
1534 }
1535
1536no_packed:
1537 mqrq->cmd_type = MMC_PACKED_NONE;
1538 return 0;
1539}
1540
1541static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1542 struct mmc_card *card,
1543 struct mmc_queue *mq)
1544{
1545 struct mmc_blk_request *brq = &mqrq->brq;
1546 struct request *req = mqrq->req;
1547 struct request *prq;
1548 struct mmc_blk_data *md = mq->data;
1549 struct mmc_packed *packed = mqrq->packed;
1550 bool do_rel_wr, do_data_tag;
1551 u32 *packed_cmd_hdr;
1552 u8 hdr_blocks;
1553 u8 i = 1;
1554
1555 BUG_ON(!packed);
1556
1557 mqrq->cmd_type = MMC_PACKED_WRITE;
1558 packed->blocks = 0;
1559 packed->idx_failure = MMC_PACKED_NR_IDX;
1560
1561 packed_cmd_hdr = packed->cmd_hdr;
1562 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1563 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1564 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1565 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1566
1567 /*
1568 * Argument for each entry of packed group
1569 */
1570 list_for_each_entry(prq, &packed->list, queuelist) {
1571 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1572 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1573 (prq->cmd_flags & REQ_META) &&
1574 (rq_data_dir(prq) == WRITE) &&
1575 ((brq->data.blocks * brq->data.blksz) >=
1576 card->ext_csd.data_tag_unit_size);
1577 /* Argument of CMD23 */
1578 packed_cmd_hdr[(i * 2)] =
1579 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1580 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1581 blk_rq_sectors(prq);
1582 /* Argument of CMD18 or CMD25 */
1583 packed_cmd_hdr[((i * 2)) + 1] =
1584 mmc_card_blockaddr(card) ?
1585 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1586 packed->blocks += blk_rq_sectors(prq);
1587 i++;
1588 }
1589
1590 memset(brq, 0, sizeof(struct mmc_blk_request));
1591 brq->mrq.cmd = &brq->cmd;
1592 brq->mrq.data = &brq->data;
1593 brq->mrq.sbc = &brq->sbc;
1594 brq->mrq.stop = &brq->stop;
1595
1596 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1597 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1598 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1599
1600 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1601 brq->cmd.arg = blk_rq_pos(req);
1602 if (!mmc_card_blockaddr(card))
1603 brq->cmd.arg <<= 9;
1604 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1605
1606 brq->data.blksz = 512;
1607 brq->data.blocks = packed->blocks + hdr_blocks;
1608 brq->data.flags |= MMC_DATA_WRITE;
1609
1610 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1611 brq->stop.arg = 0;
1612 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1613
1614 mmc_set_data_timeout(&brq->data, card);
1615
1616 brq->data.sg = mqrq->sg;
1617 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1618
1619 mqrq->mmc_active.mrq = &brq->mrq;
1620 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1621
1622 mmc_queue_bounce_pre(mqrq);
1623}
1624
Adrian Hunter67716322011-08-29 16:42:15 +03001625static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1626 struct mmc_blk_request *brq, struct request *req,
1627 int ret)
1628{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001629 struct mmc_queue_req *mq_rq;
1630 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1631
Adrian Hunter67716322011-08-29 16:42:15 +03001632 /*
1633 * If this is an SD card and we're writing, we can first
1634 * mark the known good sectors as ok.
1635 *
1636 * If the card is not SD, we can still ok written sectors
1637 * as reported by the controller (which might be less than
1638 * the real number of written sectors, but never more).
1639 */
1640 if (mmc_card_sd(card)) {
1641 u32 blocks;
1642
1643 blocks = mmc_sd_num_wr_blocks(card);
1644 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301645 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001646 }
1647 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001648 if (!mmc_packed_cmd(mq_rq->cmd_type))
1649 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001650 }
1651 return ret;
1652}
1653
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001654static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1655{
1656 struct request *prq;
1657 struct mmc_packed *packed = mq_rq->packed;
1658 int idx = packed->idx_failure, i = 0;
1659 int ret = 0;
1660
1661 BUG_ON(!packed);
1662
1663 while (!list_empty(&packed->list)) {
1664 prq = list_entry_rq(packed->list.next);
1665 if (idx == i) {
1666 /* retry from error index */
1667 packed->nr_entries -= idx;
1668 mq_rq->req = prq;
1669 ret = 1;
1670
1671 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1672 list_del_init(&prq->queuelist);
1673 mmc_blk_clear_packed(mq_rq);
1674 }
1675 return ret;
1676 }
1677 list_del_init(&prq->queuelist);
1678 blk_end_request(prq, 0, blk_rq_bytes(prq));
1679 i++;
1680 }
1681
1682 mmc_blk_clear_packed(mq_rq);
1683 return ret;
1684}
1685
1686static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1687{
1688 struct request *prq;
1689 struct mmc_packed *packed = mq_rq->packed;
1690
1691 BUG_ON(!packed);
1692
1693 while (!list_empty(&packed->list)) {
1694 prq = list_entry_rq(packed->list.next);
1695 list_del_init(&prq->queuelist);
1696 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1697 }
1698
1699 mmc_blk_clear_packed(mq_rq);
1700}
1701
1702static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1703 struct mmc_queue_req *mq_rq)
1704{
1705 struct request *prq;
1706 struct request_queue *q = mq->queue;
1707 struct mmc_packed *packed = mq_rq->packed;
1708
1709 BUG_ON(!packed);
1710
1711 while (!list_empty(&packed->list)) {
1712 prq = list_entry_rq(packed->list.prev);
1713 if (prq->queuelist.prev != &packed->list) {
1714 list_del_init(&prq->queuelist);
1715 spin_lock_irq(q->queue_lock);
1716 blk_requeue_request(mq->queue, prq);
1717 spin_unlock_irq(q->queue_lock);
1718 } else {
1719 list_del_init(&prq->queuelist);
1720 }
1721 }
1722
1723 mmc_blk_clear_packed(mq_rq);
1724}
1725
Per Forlinee8a43a2011-07-01 18:55:33 +02001726static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001727{
1728 struct mmc_blk_data *md = mq->data;
1729 struct mmc_card *card = md->queue.card;
1730 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001731 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001732 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001733 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301734 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001735 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001736 const u8 packed_nr = 2;
1737 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001738
1739 if (!rqc && !mq->mqrq_prev->req)
1740 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001741
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001742 if (rqc)
1743 reqs = mmc_blk_prep_packed_list(mq, rqc);
1744
Per Forlin54d49d72011-07-01 18:55:29 +02001745 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001746 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301747 /*
1748 * When 4KB native sector is enabled, only 8 blocks
1749 * multiple read or write is allowed
1750 */
1751 if ((brq->data.blocks & 0x07) &&
1752 (card->ext_csd.data_sector_size == 4096)) {
1753 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1754 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001755 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301756 goto cmd_abort;
1757 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001758
1759 if (reqs >= packed_nr)
1760 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1761 card, mq);
1762 else
1763 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001764 areq = &mq->mqrq_cur->mmc_active;
1765 } else
1766 areq = NULL;
1767 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001768 if (!areq) {
1769 if (status == MMC_BLK_NEW_REQUEST)
1770 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001771 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001772 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001773
Per Forlinee8a43a2011-07-01 18:55:33 +02001774 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1775 brq = &mq_rq->brq;
1776 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001777 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001778 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001779
Per Forlind78d4a8a2011-07-01 18:55:30 +02001780 switch (status) {
1781 case MMC_BLK_SUCCESS:
1782 case MMC_BLK_PARTIAL:
1783 /*
1784 * A block was successfully transferred.
1785 */
Adrian Hunter67716322011-08-29 16:42:15 +03001786 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001787
1788 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1789 ret = mmc_blk_end_packed_req(mq_rq);
1790 break;
1791 } else {
1792 ret = blk_end_request(req, 0,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001793 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001794 }
1795
Adrian Hunter67716322011-08-29 16:42:15 +03001796 /*
1797 * If the blk_end_request function returns non-zero even
1798 * though all data has been transferred and no errors
1799 * were returned by the host controller, it's a bug.
1800 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001801 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301802 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001803 __func__, blk_rq_bytes(req),
1804 brq->data.bytes_xfered);
1805 rqc = NULL;
1806 goto cmd_abort;
1807 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001808 break;
1809 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001810 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1811 if (!mmc_blk_reset(md, card->host, type))
1812 break;
1813 goto cmd_abort;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001814 case MMC_BLK_RETRY:
1815 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001816 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001817 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001818 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001819 if (!mmc_blk_reset(md, card->host, type))
1820 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001821 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001822 case MMC_BLK_DATA_ERR: {
1823 int err;
1824
1825 err = mmc_blk_reset(md, card->host, type);
1826 if (!err)
1827 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001828 if (err == -ENODEV ||
1829 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03001830 goto cmd_abort;
1831 /* Fall through */
1832 }
1833 case MMC_BLK_ECC_ERR:
1834 if (brq->data.blocks > 1) {
1835 /* Redo read one sector at a time */
1836 pr_warning("%s: retrying using single block read\n",
1837 req->rq_disk->disk_name);
1838 disable_multi = 1;
1839 break;
1840 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001841 /*
1842 * After an error, we redo I/O one sector at a
1843 * time, so we only reach here after trying to
1844 * read a single sector.
1845 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301846 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001847 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001848 if (!ret)
1849 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001850 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301851 case MMC_BLK_NOMEDIUM:
1852 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001853 default:
1854 pr_err("%s: Unhandled return value (%d)",
1855 req->rq_disk->disk_name, status);
1856 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001857 }
1858
Per Forlinee8a43a2011-07-01 18:55:33 +02001859 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001860 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1861 if (!mq_rq->packed->retries)
1862 goto cmd_abort;
1863 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1864 mmc_start_req(card->host,
1865 &mq_rq->mmc_active, NULL);
1866 } else {
1867
1868 /*
1869 * In case of a incomplete request
1870 * prepare it again and resend.
1871 */
1872 mmc_blk_rw_rq_prep(mq_rq, card,
1873 disable_multi, mq);
1874 mmc_start_req(card->host,
1875 &mq_rq->mmc_active, NULL);
1876 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 } while (ret);
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 1;
1881
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001882 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001883 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1884 mmc_blk_abort_packed_req(mq_rq);
1885 } else {
1886 if (mmc_card_removed(card))
1887 req->cmd_flags |= REQ_QUIET;
1888 while (ret)
1889 ret = blk_end_request(req, -EIO,
1890 blk_rq_cur_bytes(req));
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Per Forlinee8a43a2011-07-01 18:55:33 +02001893 start_new_req:
1894 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001895 if (mmc_card_removed(card)) {
1896 rqc->cmd_flags |= REQ_QUIET;
1897 blk_end_request_all(rqc, -EIO);
1898 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001899 /*
1900 * If current request is packed, it needs to put back.
1901 */
1902 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1903 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1904
Seungwon Jeon7a819022013-01-22 19:48:07 +09001905 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1906 mmc_start_req(card->host,
1907 &mq->mqrq_cur->mmc_active, NULL);
1908 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001909 }
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return 0;
1912}
1913
Adrian Hunterbd788c92010-08-11 14:17:47 -07001914static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1915{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001916 int ret;
1917 struct mmc_blk_data *md = mq->data;
1918 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001919 struct mmc_host *host = card->host;
1920 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001921
Per Forlinee8a43a2011-07-01 18:55:33 +02001922 if (req && !mq->mqrq_prev->req)
1923 /* claim host only for the first request */
1924 mmc_claim_host(card->host);
1925
Andrei Warkentin371a6892011-04-11 18:10:25 -05001926 ret = mmc_blk_part_switch(card, md);
1927 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001928 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301929 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001930 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001931 ret = 0;
1932 goto out;
1933 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001934
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001935 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001936 if (req && req->cmd_flags & REQ_DISCARD) {
1937 /* complete ongoing async transfer before issuing discard */
1938 if (card->host->areq)
1939 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chen3550ccd2012-08-29 15:05:36 +09001940 if (req->cmd_flags & REQ_SECURE &&
1941 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001942 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001943 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001944 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001945 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001946 /* complete ongoing async transfer before issuing flush */
1947 if (card->host->areq)
1948 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001949 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001950 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001951 if (!req && host->areq) {
1952 spin_lock_irqsave(&host->context_info.lock, flags);
1953 host->context_info.is_waiting_last_req = true;
1954 spin_unlock_irqrestore(&host->context_info.lock, flags);
1955 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001956 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001957 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001958
Andrei Warkentin371a6892011-04-11 18:10:25 -05001959out:
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001960 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
1961 (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
1962 /*
1963 * Release host when there are no more requests
1964 * and after special request(discard, flush) is done.
1965 * In case sepecial request, there is no reentry to
1966 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1967 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001968 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001969 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001970}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Russell Kinga6f6c962006-01-03 22:38:44 +00001972static inline int mmc_blk_readonly(struct mmc_card *card)
1973{
1974 return mmc_card_readonly(card) ||
1975 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1976}
1977
Andrei Warkentin371a6892011-04-11 18:10:25 -05001978static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1979 struct device *parent,
1980 sector_t size,
1981 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001982 const char *subname,
1983 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 struct mmc_blk_data *md;
1986 int devidx, ret;
1987
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001988 devidx = find_first_zero_bit(dev_use, max_devices);
1989 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return ERR_PTR(-ENOSPC);
1991 __set_bit(devidx, dev_use);
1992
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001993 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001994 if (!md) {
1995 ret = -ENOMEM;
1996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001998
Russell Kinga6f6c962006-01-03 22:38:44 +00001999 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002000 * !subname implies we are creating main mmc_blk_data that will be
2001 * associated with mmc_card with mmc_set_drvdata. Due to device
2002 * partitions, devidx will not coincide with a per-physical card
2003 * index anymore so we keep track of a name index.
2004 */
2005 if (!subname) {
2006 md->name_idx = find_first_zero_bit(name_use, max_devices);
2007 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002008 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002009 md->name_idx = ((struct mmc_blk_data *)
2010 dev_to_disk(parent)->private_data)->name_idx;
2011
Johan Rudholmadd710e2011-12-02 08:51:06 +01002012 md->area_type = area_type;
2013
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002014 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002015 * Set the read-only status based on the supported commands
2016 * and the write protect switch.
2017 */
2018 md->read_only = mmc_blk_readonly(card);
2019
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002020 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002021 if (md->disk == NULL) {
2022 ret = -ENOMEM;
2023 goto err_kfree;
2024 }
2025
2026 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002027 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002028 md->usage = 1;
2029
Adrian Hunterd09408a2011-06-23 13:40:28 +03002030 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002031 if (ret)
2032 goto err_putdisk;
2033
Russell Kinga6f6c962006-01-03 22:38:44 +00002034 md->queue.issue_fn = mmc_blk_issue_rq;
2035 md->queue.data = md;
2036
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002037 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002038 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002039 md->disk->fops = &mmc_bdops;
2040 md->disk->private_data = md;
2041 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002042 md->disk->driverfs_dev = parent;
2043 set_disk_ro(md->disk, md->read_only || default_ro);
Loic Pallardy53d8f972012-08-06 17:12:28 +02002044 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2045 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002046
2047 /*
2048 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2049 *
2050 * - be set for removable media with permanent block devices
2051 * - be unset for removable block devices with permanent media
2052 *
2053 * Since MMC block devices clearly fall under the second
2054 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2055 * should use the block device creation/destruction hotplug
2056 * messages to tell when the card is present.
2057 */
2058
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002059 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2060 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002061
Saugata Dasa5075eb2012-05-17 16:32:21 +05302062 if (mmc_card_mmc(card))
2063 blk_queue_logical_block_size(md->queue.queue,
2064 card->ext_csd.data_sector_size);
2065 else
2066 blk_queue_logical_block_size(md->queue.queue, 512);
2067
Andrei Warkentin371a6892011-04-11 18:10:25 -05002068 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002069
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002070 if (mmc_host_cmd23(card->host)) {
2071 if (mmc_card_mmc(card) ||
2072 (mmc_card_sd(card) &&
2073 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2074 md->flags |= MMC_BLK_CMD23;
2075 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002076
2077 if (mmc_card_mmc(card) &&
2078 md->flags & MMC_BLK_CMD23 &&
2079 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2080 card->ext_csd.rel_sectors)) {
2081 md->flags |= MMC_BLK_REL_WR;
2082 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2083 }
2084
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002085 if (mmc_card_mmc(card) &&
2086 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2087 (md->flags & MMC_BLK_CMD23) &&
2088 card->ext_csd.packed_event_en) {
2089 if (!mmc_packed_init(&md->queue, card))
2090 md->flags |= MMC_BLK_PACKED_CMD;
2091 }
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002094
2095 err_putdisk:
2096 put_disk(md->disk);
2097 err_kfree:
2098 kfree(md);
2099 out:
2100 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Andrei Warkentin371a6892011-04-11 18:10:25 -05002103static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2104{
2105 sector_t size;
2106 struct mmc_blk_data *md;
2107
2108 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2109 /*
2110 * The EXT_CSD sector count is in number or 512 byte
2111 * sectors.
2112 */
2113 size = card->ext_csd.sectors;
2114 } else {
2115 /*
2116 * The CSD capacity field is in units of read_blkbits.
2117 * set_capacity takes units of 512 bytes.
2118 */
2119 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2120 }
2121
Johan Rudholmadd710e2011-12-02 08:51:06 +01002122 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2123 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002124 return md;
2125}
2126
2127static int mmc_blk_alloc_part(struct mmc_card *card,
2128 struct mmc_blk_data *md,
2129 unsigned int part_type,
2130 sector_t size,
2131 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002132 const char *subname,
2133 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002134{
2135 char cap_str[10];
2136 struct mmc_blk_data *part_md;
2137
2138 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002139 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002140 if (IS_ERR(part_md))
2141 return PTR_ERR(part_md);
2142 part_md->part_type = part_type;
2143 list_add(&part_md->part, &md->part);
2144
2145 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2146 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302147 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002148 part_md->disk->disk_name, mmc_card_id(card),
2149 mmc_card_name(card), part_md->part_type, cap_str);
2150 return 0;
2151}
2152
Namjae Jeone0c368d2011-10-06 23:41:38 +09002153/* MMC Physical partitions consist of two boot partitions and
2154 * up to four general purpose partitions.
2155 * For each partition enabled in EXT_CSD a block device will be allocatedi
2156 * to provide access to the partition.
2157 */
2158
Andrei Warkentin371a6892011-04-11 18:10:25 -05002159static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2160{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002161 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002162
2163 if (!mmc_card_mmc(card))
2164 return 0;
2165
Namjae Jeone0c368d2011-10-06 23:41:38 +09002166 for (idx = 0; idx < card->nr_parts; idx++) {
2167 if (card->part[idx].size) {
2168 ret = mmc_blk_alloc_part(card, md,
2169 card->part[idx].part_cfg,
2170 card->part[idx].size >> 9,
2171 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002172 card->part[idx].name,
2173 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002174 if (ret)
2175 return ret;
2176 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177 }
2178
2179 return ret;
2180}
2181
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182static void mmc_blk_remove_req(struct mmc_blk_data *md)
2183{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002184 struct mmc_card *card;
2185
Andrei Warkentin371a6892011-04-11 18:10:25 -05002186 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002187 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002188 if (md->disk->flags & GENHD_FL_UP) {
2189 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002190 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2191 card->ext_csd.boot_ro_lockable)
2192 device_remove_file(disk_to_dev(md->disk),
2193 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002194
2195 /* Stop new requests from getting into the queue */
2196 del_gendisk(md->disk);
2197 }
2198
2199 /* Then flush out any already in there */
2200 mmc_cleanup_queue(&md->queue);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002201 if (md->flags & MMC_BLK_PACKED_CMD)
2202 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203 mmc_blk_put(md);
2204 }
2205}
2206
2207static void mmc_blk_remove_parts(struct mmc_card *card,
2208 struct mmc_blk_data *md)
2209{
2210 struct list_head *pos, *q;
2211 struct mmc_blk_data *part_md;
2212
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002213 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002214 list_for_each_safe(pos, q, &md->part) {
2215 part_md = list_entry(pos, struct mmc_blk_data, part);
2216 list_del(pos);
2217 mmc_blk_remove_req(part_md);
2218 }
2219}
2220
2221static int mmc_add_disk(struct mmc_blk_data *md)
2222{
2223 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002224 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225
2226 add_disk(md->disk);
2227 md->force_ro.show = force_ro_show;
2228 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302229 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002230 md->force_ro.attr.name = "force_ro";
2231 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2232 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2233 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002234 goto force_ro_fail;
2235
2236 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2237 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002238 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002239
2240 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2241 mode = S_IRUGO;
2242 else
2243 mode = S_IRUGO | S_IWUSR;
2244
2245 md->power_ro_lock.show = power_ro_lock_show;
2246 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002247 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002248 md->power_ro_lock.attr.mode = mode;
2249 md->power_ro_lock.attr.name =
2250 "ro_lock_until_next_power_on";
2251 ret = device_create_file(disk_to_dev(md->disk),
2252 &md->power_ro_lock);
2253 if (ret)
2254 goto power_ro_lock_fail;
2255 }
2256 return ret;
2257
2258power_ro_lock_fail:
2259 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2260force_ro_fail:
2261 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002262
2263 return ret;
2264}
2265
Chris Ballc59d4472011-11-11 22:01:43 -05002266#define CID_MANFID_SANDISK 0x2
2267#define CID_MANFID_TOSHIBA 0x11
2268#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09002269#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002270
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002271static const struct mmc_fixup blk_fixups[] =
2272{
Chris Ballc59d4472011-11-11 22:01:43 -05002273 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2274 MMC_QUIRK_INAND_CMD38),
2275 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2276 MMC_QUIRK_INAND_CMD38),
2277 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2278 MMC_QUIRK_INAND_CMD38),
2279 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2280 MMC_QUIRK_INAND_CMD38),
2281 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2282 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002283
2284 /*
2285 * Some MMC cards experience performance degradation with CMD23
2286 * instead of CMD12-bounded multiblock transfers. For now we'll
2287 * black list what's bad...
2288 * - Certain Toshiba cards.
2289 *
2290 * N.B. This doesn't affect SD cards.
2291 */
Chris Ballc59d4472011-11-11 22:01:43 -05002292 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002293 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002294 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002295 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002296 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002297 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002298
2299 /*
2300 * Some Micron MMC cards needs longer data read timeout than
2301 * indicated in CSD.
2302 */
Chris Ballc59d4472011-11-11 22:01:43 -05002303 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002304 MMC_QUIRK_LONG_READ_TIME),
2305
Ian Chen3550ccd2012-08-29 15:05:36 +09002306 /*
2307 * On these Samsung MoviNAND parts, performing secure erase or
2308 * secure trim can result in unrecoverable corruption due to a
2309 * firmware bug.
2310 */
2311 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2312 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2313 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2314 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2315 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2317 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2319 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2321 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2323 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2325 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2326 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2327
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002328 END_FIXUP
2329};
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static int mmc_blk_probe(struct mmc_card *card)
2332{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002333 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002334 char cap_str[10];
2335
Pierre Ossman912490d2005-05-21 10:27:02 +01002336 /*
2337 * Check that the card supports the command class(es) we need.
2338 */
2339 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return -ENODEV;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 md = mmc_blk_alloc(card);
2343 if (IS_ERR(md))
2344 return PTR_ERR(md);
2345
Yi Li444122f2009-02-05 15:31:57 +08002346 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002347 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302348 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002350 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Andrei Warkentin371a6892011-04-11 18:10:25 -05002352 if (mmc_blk_alloc_parts(card, md))
2353 goto out;
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002356 mmc_fixup_device(card, blk_fixups);
2357
Andrei Warkentin371a6892011-04-11 18:10:25 -05002358 if (mmc_add_disk(md))
2359 goto out;
2360
2361 list_for_each_entry(part_md, &md->part, part) {
2362 if (mmc_add_disk(part_md))
2363 goto out;
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return 0;
2366
2367 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002368 mmc_blk_remove_parts(card, md);
2369 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373static void mmc_blk_remove(struct mmc_card *card)
2374{
2375 struct mmc_blk_data *md = mmc_get_drvdata(card);
2376
Andrei Warkentin371a6892011-04-11 18:10:25 -05002377 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002378 mmc_claim_host(card->host);
2379 mmc_blk_part_switch(card, md);
2380 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002381 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 mmc_set_drvdata(card, NULL);
2383}
2384
2385#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002386static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct mmc_blk_data *md = mmc_get_drvdata(card);
2390
2391 if (md) {
2392 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002393 list_for_each_entry(part_md, &md->part, part) {
2394 mmc_queue_suspend(&part_md->queue);
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397 return 0;
2398}
2399
2400static int mmc_blk_resume(struct mmc_card *card)
2401{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002402 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct mmc_blk_data *md = mmc_get_drvdata(card);
2404
2405 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002406 /*
2407 * Resume involves the card going into idle state,
2408 * so current partition is always the main one.
2409 */
2410 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002412 list_for_each_entry(part_md, &md->part, part) {
2413 mmc_queue_resume(&part_md->queue);
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 return 0;
2417}
2418#else
2419#define mmc_blk_suspend NULL
2420#define mmc_blk_resume NULL
2421#endif
2422
2423static struct mmc_driver mmc_driver = {
2424 .drv = {
2425 .name = "mmcblk",
2426 },
2427 .probe = mmc_blk_probe,
2428 .remove = mmc_blk_remove,
2429 .suspend = mmc_blk_suspend,
2430 .resume = mmc_blk_resume,
2431};
2432
2433static int __init mmc_blk_init(void)
2434{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002435 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002437 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2438 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2439
2440 max_devices = 256 / perdev_minors;
2441
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002442 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2443 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002446 res = mmc_register_driver(&mmc_driver);
2447 if (res)
2448 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002450 return 0;
2451 out2:
2452 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 out:
2454 return res;
2455}
2456
2457static void __exit mmc_blk_exit(void)
2458{
2459 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002460 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
2463module_init(mmc_blk_init);
2464module_exit(mmc_blk_exit);
2465
2466MODULE_LICENSE("GPL");
2467MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2468