blob: 1fc5cc4e29db9c8c823009339efd66d5d04aaf61 [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 Ossman385e3222006-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
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200129enum {
130 MMC_PACKED_N_IDX = -1,
131 MMC_PACKED_N_ZERO,
132 MMC_PACKED_N_SINGLE,
133};
134
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400135module_param(perdev_minors, int, 0444);
136MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
137
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200138static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
139{
140 mqrq->packed_cmd = MMC_PACKED_NONE;
141 mqrq->packed_num = MMC_PACKED_N_ZERO;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
145{
146 struct mmc_blk_data *md;
147
Arjan van de Vena621aae2006-01-12 18:43:35 +0000148 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 md = disk->private_data;
150 if (md && md->usage == 0)
151 md = NULL;
152 if (md)
153 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000154 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return md;
157}
158
Andrei Warkentin371a6892011-04-11 18:10:25 -0500159static inline int mmc_get_devidx(struct gendisk *disk)
160{
Colin Cross71b54d42010-09-03 12:41:21 -0700161 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500162 return devidx;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void mmc_blk_put(struct mmc_blk_data *md)
166{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000167 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 md->usage--;
169 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500170 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800171 blk_cleanup_queue(md->queue.queue);
172
David Woodhouse1dff3142007-11-21 18:45:12 +0100173 __clear_bit(devidx, dev_use);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(md);
177 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Johan Rudholmadd710e2011-12-02 08:51:06 +0100181static ssize_t power_ro_lock_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
183{
184 int ret;
185 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
186 struct mmc_card *card = md->queue.card;
187 int locked = 0;
188
189 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
190 locked = 2;
191 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
192 locked = 1;
193
194 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
195
196 return ret;
197}
198
199static ssize_t power_ro_lock_store(struct device *dev,
200 struct device_attribute *attr, const char *buf, size_t count)
201{
202 int ret;
203 struct mmc_blk_data *md, *part_md;
204 struct mmc_card *card;
205 unsigned long set;
206
207 if (kstrtoul(buf, 0, &set))
208 return -EINVAL;
209
210 if (set != 1)
211 return count;
212
213 md = mmc_blk_get(dev_to_disk(dev));
214 card = md->queue.card;
215
216 mmc_claim_host(card->host);
217
218 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
219 card->ext_csd.boot_ro_lock |
220 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
221 card->ext_csd.part_time);
222 if (ret)
223 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
224 else
225 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
226
227 mmc_release_host(card->host);
228
229 if (!ret) {
230 pr_info("%s: Locking boot partition ro until next power on\n",
231 md->disk->disk_name);
232 set_disk_ro(md->disk, 1);
233
234 list_for_each_entry(part_md, &md->part, part)
235 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
236 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
237 set_disk_ro(part_md->disk, 1);
238 }
239 }
240
241 mmc_blk_put(md);
242 return count;
243}
244
Andrei Warkentin371a6892011-04-11 18:10:25 -0500245static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
246 char *buf)
247{
248 int ret;
249 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
250
251 ret = snprintf(buf, PAGE_SIZE, "%d",
252 get_disk_ro(dev_to_disk(dev)) ^
253 md->read_only);
254 mmc_blk_put(md);
255 return ret;
256}
257
258static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t count)
260{
261 int ret;
262 char *end;
263 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
264 unsigned long set = simple_strtoul(buf, &end, 0);
265 if (end == buf) {
266 ret = -EINVAL;
267 goto out;
268 }
269
270 set_disk_ro(dev_to_disk(dev), set || md->read_only);
271 ret = count;
272out:
273 mmc_blk_put(md);
274 return ret;
275}
276
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200277static ssize_t
278num_wr_reqs_to_start_packing_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
281 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
282 int num_wr_reqs_to_start_packing;
283 int ret;
284
285 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
286
287 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
288
289 mmc_blk_put(md);
290 return ret;
291}
292
293static ssize_t
294num_wr_reqs_to_start_packing_store(struct device *dev,
295 struct device_attribute *attr,
296 const char *buf, size_t count)
297{
298 int value;
299 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
300
301 sscanf(buf, "%d", &value);
302 if (value >= 0)
303 md->queue.num_wr_reqs_to_start_packing = value;
304
305 mmc_blk_put(md);
306 return count;
307}
308
Al Viroa5a15612008-03-02 10:33:30 -0500309static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Al Viroa5a15612008-03-02 10:33:30 -0500311 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int ret = -ENXIO;
313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (md) {
316 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500317 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700319
Al Viroa5a15612008-03-02 10:33:30 -0500320 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700321 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700322 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200325 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return ret;
328}
329
Al Viroa5a15612008-03-02 10:33:30 -0500330static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Al Viroa5a15612008-03-02 10:33:30 -0500332 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200334 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200336 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800341mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800343 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
344 geo->heads = 4;
345 geo->sectors = 16;
346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
John Calixtocb87ea22011-04-26 18:56:29 -0400349struct mmc_blk_ioc_data {
350 struct mmc_ioc_cmd ic;
351 unsigned char *buf;
352 u64 buf_bytes;
353};
354
355static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
356 struct mmc_ioc_cmd __user *user)
357{
358 struct mmc_blk_ioc_data *idata;
359 int err;
360
361 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
362 if (!idata) {
363 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400364 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400365 }
366
367 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
368 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400369 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400370 }
371
372 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
373 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
374 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400376 }
377
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100378 if (!idata->buf_bytes)
379 return idata;
380
John Calixtocb87ea22011-04-26 18:56:29 -0400381 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
382 if (!idata->buf) {
383 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400384 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400385 }
386
387 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
388 idata->ic.data_ptr, idata->buf_bytes)) {
389 err = -EFAULT;
390 goto copy_err;
391 }
392
393 return idata;
394
395copy_err:
396 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400397idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400398 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400399out:
John Calixtocb87ea22011-04-26 18:56:29 -0400400 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400401}
402
403static int mmc_blk_ioctl_cmd(struct block_device *bdev,
404 struct mmc_ioc_cmd __user *ic_ptr)
405{
406 struct mmc_blk_ioc_data *idata;
407 struct mmc_blk_data *md;
408 struct mmc_card *card;
409 struct mmc_command cmd = {0};
410 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530411 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400412 struct scatterlist sg;
413 int err;
414
415 /*
416 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
417 * whole block device, not on a partition. This prevents overspray
418 * between sibling partitions.
419 */
420 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
421 return -EPERM;
422
423 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
424 if (IS_ERR(idata))
425 return PTR_ERR(idata);
426
John Calixtocb87ea22011-04-26 18:56:29 -0400427 md = mmc_blk_get(bdev->bd_disk);
428 if (!md) {
429 err = -EINVAL;
430 goto cmd_done;
431 }
432
433 card = md->queue.card;
434 if (IS_ERR(card)) {
435 err = PTR_ERR(card);
436 goto cmd_done;
437 }
438
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100439 cmd.opcode = idata->ic.opcode;
440 cmd.arg = idata->ic.arg;
441 cmd.flags = idata->ic.flags;
442
443 if (idata->buf_bytes) {
444 data.sg = &sg;
445 data.sg_len = 1;
446 data.blksz = idata->ic.blksz;
447 data.blocks = idata->ic.blocks;
448
449 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
450
451 if (idata->ic.write_flag)
452 data.flags = MMC_DATA_WRITE;
453 else
454 data.flags = MMC_DATA_READ;
455
456 /* data.flags must already be set before doing this. */
457 mmc_set_data_timeout(&data, card);
458
459 /* Allow overriding the timeout_ns for empirical tuning. */
460 if (idata->ic.data_timeout_ns)
461 data.timeout_ns = idata->ic.data_timeout_ns;
462
463 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
464 /*
465 * Pretend this is a data transfer and rely on the
466 * host driver to compute timeout. When all host
467 * drivers support cmd.cmd_timeout for R1B, this
468 * can be changed to:
469 *
470 * mrq.data = NULL;
471 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
472 */
473 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
474 }
475
476 mrq.data = &data;
477 }
478
479 mrq.cmd = &cmd;
480
John Calixtocb87ea22011-04-26 18:56:29 -0400481 mmc_claim_host(card->host);
482
483 if (idata->ic.is_acmd) {
484 err = mmc_app_cmd(card->host, card);
485 if (err)
486 goto cmd_rel_host;
487 }
488
John Calixtocb87ea22011-04-26 18:56:29 -0400489 mmc_wait_for_req(card->host, &mrq);
490
491 if (cmd.error) {
492 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
493 __func__, cmd.error);
494 err = cmd.error;
495 goto cmd_rel_host;
496 }
497 if (data.error) {
498 dev_err(mmc_dev(card->host), "%s: data error %d\n",
499 __func__, data.error);
500 err = data.error;
501 goto cmd_rel_host;
502 }
503
504 /*
505 * According to the SD specs, some commands require a delay after
506 * issuing the command.
507 */
508 if (idata->ic.postsleep_min_us)
509 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
510
511 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
512 err = -EFAULT;
513 goto cmd_rel_host;
514 }
515
516 if (!idata->ic.write_flag) {
517 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
518 idata->buf, idata->buf_bytes)) {
519 err = -EFAULT;
520 goto cmd_rel_host;
521 }
522 }
523
524cmd_rel_host:
525 mmc_release_host(card->host);
526
527cmd_done:
528 mmc_blk_put(md);
529 kfree(idata->buf);
530 kfree(idata);
531 return err;
532}
533
534static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
535 unsigned int cmd, unsigned long arg)
536{
537 int ret = -EINVAL;
538 if (cmd == MMC_IOC_CMD)
539 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
540 return ret;
541}
542
543#ifdef CONFIG_COMPAT
544static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
545 unsigned int cmd, unsigned long arg)
546{
547 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
548}
549#endif
550
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700551static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500552 .open = mmc_blk_open,
553 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800554 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400556 .ioctl = mmc_blk_ioctl,
557#ifdef CONFIG_COMPAT
558 .compat_ioctl = mmc_blk_compat_ioctl,
559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560};
561
Andrei Warkentin371a6892011-04-11 18:10:25 -0500562static inline int mmc_blk_part_switch(struct mmc_card *card,
563 struct mmc_blk_data *md)
564{
565 int ret;
566 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300567
Andrei Warkentin371a6892011-04-11 18:10:25 -0500568 if (main_md->part_curr == md->part_type)
569 return 0;
570
571 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300572 u8 part_config = card->ext_csd.part_config;
573
574 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
575 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500576
577 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300578 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500579 card->ext_csd.part_time);
580 if (ret)
581 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300582
583 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300584 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500585
586 main_md->part_curr = md->part_type;
587 return 0;
588}
589
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700590static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
591{
592 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100593 u32 result;
594 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700595
Venkatraman Sad5fd972011-08-25 00:30:50 +0530596 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400597 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400598 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700599
600 struct scatterlist sg;
601
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700602 cmd.opcode = MMC_APP_CMD;
603 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700604 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700605
606 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700607 if (err)
608 return (u32)-1;
609 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700610 return (u32)-1;
611
612 memset(&cmd, 0, sizeof(struct mmc_command));
613
614 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
615 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700616 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700617
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700618 data.blksz = 4;
619 data.blocks = 1;
620 data.flags = MMC_DATA_READ;
621 data.sg = &sg;
622 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530623 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700624
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700625 mrq.cmd = &cmd;
626 mrq.data = &data;
627
Ben Dooks051913d2009-06-08 23:33:57 +0100628 blocks = kmalloc(4, GFP_KERNEL);
629 if (!blocks)
630 return (u32)-1;
631
632 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700633
634 mmc_wait_for_req(card->host, &mrq);
635
Ben Dooks051913d2009-06-08 23:33:57 +0100636 result = ntohl(*blocks);
637 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700638
Ben Dooks051913d2009-06-08 23:33:57 +0100639 if (cmd.error || data.error)
640 result = (u32)-1;
641
642 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700643}
644
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100645static int send_stop(struct mmc_card *card, u32 *status)
646{
647 struct mmc_command cmd = {0};
648 int err;
649
650 cmd.opcode = MMC_STOP_TRANSMISSION;
651 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
652 err = mmc_wait_for_cmd(card->host, &cmd, 5);
653 if (err == 0)
654 *status = cmd.resp[0];
655 return err;
656}
657
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100658static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300659{
Chris Ball1278dba2011-04-13 23:40:30 -0400660 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300661 int err;
662
Adrian Hunter504f1912008-10-16 12:55:25 +0300663 cmd.opcode = MMC_SEND_STATUS;
664 if (!mmc_host_is_spi(card->host))
665 cmd.arg = card->rca << 16;
666 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100667 err = mmc_wait_for_cmd(card->host, &cmd, retries);
668 if (err == 0)
669 *status = cmd.resp[0];
670 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300671}
672
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530673#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100674#define ERR_RETRY 2
675#define ERR_ABORT 1
676#define ERR_CONTINUE 0
677
678static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
679 bool status_valid, u32 status)
680{
681 switch (error) {
682 case -EILSEQ:
683 /* response crc error, retry the r/w cmd */
684 pr_err("%s: %s sending %s command, card status %#x\n",
685 req->rq_disk->disk_name, "response CRC error",
686 name, status);
687 return ERR_RETRY;
688
689 case -ETIMEDOUT:
690 pr_err("%s: %s sending %s command, card status %#x\n",
691 req->rq_disk->disk_name, "timed out", name, status);
692
693 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700694 if (!status_valid) {
695 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100696 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700697 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100698 /*
699 * If it was a r/w cmd crc error, or illegal command
700 * (eg, issued in wrong state) then retry - we should
701 * have corrected the state problem above.
702 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700703 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
704 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100705 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700706 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100707
708 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700709 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100710 return ERR_ABORT;
711
712 default:
713 /* We don't understand the error code the driver gave us */
714 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
715 req->rq_disk->disk_name, error, status);
716 return ERR_ABORT;
717 }
718}
719
720/*
721 * Initial r/w and stop cmd error recovery.
722 * We don't know whether the card received the r/w cmd or not, so try to
723 * restore things back to a sane state. Essentially, we do this as follows:
724 * - Obtain card status. If the first attempt to obtain card status fails,
725 * the status word will reflect the failed status cmd, not the failed
726 * r/w cmd. If we fail to obtain card status, it suggests we can no
727 * longer communicate with the card.
728 * - Check the card state. If the card received the cmd but there was a
729 * transient problem with the response, it might still be in a data transfer
730 * mode. Try to send it a stop command. If this fails, we can't recover.
731 * - If the r/w cmd failed due to a response CRC error, it was probably
732 * transient, so retry the cmd.
733 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
734 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
735 * illegal cmd, retry.
736 * Otherwise we don't understand what happened, so abort.
737 */
738static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300739 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100740{
741 bool prev_cmd_status_valid = true;
742 u32 status, stop_status = 0;
743 int err, retry;
744
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530745 if (mmc_card_removed(card))
746 return ERR_NOMEDIUM;
747
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100748 /*
749 * Try to get card status which indicates both the card state
750 * and why there was no response. If the first attempt fails,
751 * we can't be sure the returned status is for the r/w command.
752 */
753 for (retry = 2; retry >= 0; retry--) {
754 err = get_card_status(card, &status, 0);
755 if (!err)
756 break;
757
758 prev_cmd_status_valid = false;
759 pr_err("%s: error %d sending status command, %sing\n",
760 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
761 }
762
763 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530764 if (err) {
765 /* Check if the card is removed */
766 if (mmc_detect_card_removed(card->host))
767 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100768 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530769 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100770
Adrian Hunter67716322011-08-29 16:42:15 +0300771 /* Flag ECC errors */
772 if ((status & R1_CARD_ECC_FAILED) ||
773 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
774 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
775 *ecc_err = 1;
776
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100777 /*
778 * Check the current card state. If it is in some data transfer
779 * mode, tell it to stop (and hopefully transition back to TRAN.)
780 */
781 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
782 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
783 err = send_stop(card, &stop_status);
784 if (err)
785 pr_err("%s: error %d sending stop command\n",
786 req->rq_disk->disk_name, err);
787
788 /*
789 * If the stop cmd also timed out, the card is probably
790 * not present, so abort. Other errors are bad news too.
791 */
792 if (err)
793 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300794 if (stop_status & R1_CARD_ECC_FAILED)
795 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100796 }
797
798 /* Check for set block count errors */
799 if (brq->sbc.error)
800 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
801 prev_cmd_status_valid, status);
802
803 /* Check for r/w command errors */
804 if (brq->cmd.error)
805 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
806 prev_cmd_status_valid, status);
807
Adrian Hunter67716322011-08-29 16:42:15 +0300808 /* Data errors */
809 if (!brq->stop.error)
810 return ERR_CONTINUE;
811
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100812 /* Now for stop errors. These aren't fatal to the transfer. */
813 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
814 req->rq_disk->disk_name, brq->stop.error,
815 brq->cmd.resp[0], status);
816
817 /*
818 * Subsitute in our own stop status as this will give the error
819 * state which happened during the execution of the r/w command.
820 */
821 if (stop_status) {
822 brq->stop.resp[0] = stop_status;
823 brq->stop.error = 0;
824 }
825 return ERR_CONTINUE;
826}
827
Adrian Hunter67716322011-08-29 16:42:15 +0300828static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
829 int type)
830{
831 int err;
832
833 if (md->reset_done & type)
834 return -EEXIST;
835
836 md->reset_done |= type;
837 err = mmc_hw_reset(host);
838 /* Ensure we switch back to the correct partition */
839 if (err != -EOPNOTSUPP) {
840 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
841 int part_err;
842
843 main_md->part_curr = main_md->part_type;
844 part_err = mmc_blk_part_switch(host->card, md);
845 if (part_err) {
846 /*
847 * We have failed to get back into the correct
848 * partition, so we need to abort the whole request.
849 */
850 return -ENODEV;
851 }
852 }
853 return err;
854}
855
856static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
857{
858 md->reset_done &= ~type;
859}
860
Adrian Hunterbd788c92010-08-11 14:17:47 -0700861static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
862{
863 struct mmc_blk_data *md = mq->data;
864 struct mmc_card *card = md->queue.card;
865 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300866 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700867
Adrian Hunterbd788c92010-08-11 14:17:47 -0700868 if (!mmc_can_erase(card)) {
869 err = -EOPNOTSUPP;
870 goto out;
871 }
872
873 from = blk_rq_pos(req);
874 nr = blk_rq_sectors(req);
875
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900876 if (mmc_can_discard(card))
877 arg = MMC_DISCARD_ARG;
878 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700879 arg = MMC_TRIM_ARG;
880 else
881 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300882retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500883 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
884 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
885 INAND_CMD38_ARG_EXT_CSD,
886 arg == MMC_TRIM_ARG ?
887 INAND_CMD38_ARG_TRIM :
888 INAND_CMD38_ARG_ERASE,
889 0);
890 if (err)
891 goto out;
892 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700893 err = mmc_erase(card, from, nr, arg);
894out:
Adrian Hunter67716322011-08-29 16:42:15 +0300895 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
896 goto retry;
897 if (!err)
898 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530899 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700900
Adrian Hunterbd788c92010-08-11 14:17:47 -0700901 return err ? 0 : 1;
902}
903
Adrian Hunter49804542010-08-11 14:17:50 -0700904static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
905 struct request *req)
906{
907 struct mmc_blk_data *md = mq->data;
908 struct mmc_card *card = md->queue.card;
909 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300910 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700911
Maya Erez463bb952012-05-24 23:46:29 +0300912 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700913 err = -EOPNOTSUPP;
914 goto out;
915 }
916
917 from = blk_rq_pos(req);
918 nr = blk_rq_sectors(req);
919
920 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
921 arg = MMC_SECURE_TRIM1_ARG;
922 else
923 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300924retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500925 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
926 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
927 INAND_CMD38_ARG_EXT_CSD,
928 arg == MMC_SECURE_TRIM1_ARG ?
929 INAND_CMD38_ARG_SECTRIM1 :
930 INAND_CMD38_ARG_SECERASE,
931 0);
932 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300933 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500934 }
Adrian Hunter28302812012-04-05 14:45:48 +0300935
Adrian Hunter49804542010-08-11 14:17:50 -0700936 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300937 if (err == -EIO)
938 goto out_retry;
939 if (err)
940 goto out;
941
942 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500943 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
944 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
945 INAND_CMD38_ARG_EXT_CSD,
946 INAND_CMD38_ARG_SECTRIM2,
947 0);
948 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300949 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500950 }
Adrian Hunter28302812012-04-05 14:45:48 +0300951
Adrian Hunter49804542010-08-11 14:17:50 -0700952 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300953 if (err == -EIO)
954 goto out_retry;
955 if (err)
956 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500957 }
Adrian Hunter28302812012-04-05 14:45:48 +0300958
959 if (mmc_can_sanitize(card))
960 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
961 EXT_CSD_SANITIZE_START, 1, 0);
962out_retry:
963 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300964 goto retry;
965 if (!err)
966 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300967out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530968 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700969
Adrian Hunter49804542010-08-11 14:17:50 -0700970 return err ? 0 : 1;
971}
972
Maya Erez463bb952012-05-24 23:46:29 +0300973static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
974 struct request *req)
975{
976 struct mmc_blk_data *md = mq->data;
977 struct mmc_card *card = md->queue.card;
978 int err = 0;
979
980 BUG_ON(!card);
981 BUG_ON(!card->host);
982
983 if (!(mmc_can_sanitize(card) &&
984 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
985 pr_warning("%s: %s - SANITIZE is not supported\n",
986 mmc_hostname(card->host), __func__);
987 err = -EOPNOTSUPP;
988 goto out;
989 }
990
991 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
992 mmc_hostname(card->host), __func__);
993
994 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +0300995 EXT_CSD_SANITIZE_START, 1,
996 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +0300997
998 if (err)
999 pr_err("%s: %s - mmc_switch() with "
1000 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1001 mmc_hostname(card->host), __func__, err);
1002
1003 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1004 __func__);
1005
1006out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301007 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001008
1009 return err ? 0 : 1;
1010}
1011
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001012static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1013{
1014 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001015 struct mmc_card *card = md->queue.card;
1016 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001017
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001018 ret = mmc_flush_cache(card);
1019 if (ret)
1020 ret = -EIO;
1021
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301022 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001023
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001024 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001025}
1026
1027/*
1028 * Reformat current write as a reliable write, supporting
1029 * both legacy and the enhanced reliable write MMC cards.
1030 * In each transfer we'll handle only as much as a single
1031 * reliable write can handle, thus finish the request in
1032 * partial completions.
1033 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001034static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1035 struct mmc_card *card,
1036 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001037{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001038 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1039 /* Legacy mode imposes restrictions on transfers. */
1040 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1041 brq->data.blocks = 1;
1042
1043 if (brq->data.blocks > card->ext_csd.rel_sectors)
1044 brq->data.blocks = card->ext_csd.rel_sectors;
1045 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1046 brq->data.blocks = 1;
1047 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001048}
1049
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001050#define CMD_ERRORS \
1051 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1052 R1_ADDRESS_ERROR | /* Misaligned address */ \
1053 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1054 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1055 R1_CC_ERROR | /* Card controller error */ \
1056 R1_ERROR) /* General/unknown error */
1057
Per Forlinee8a43a2011-07-01 18:55:33 +02001058static int mmc_blk_err_check(struct mmc_card *card,
1059 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001060{
Per Forlinee8a43a2011-07-01 18:55:33 +02001061 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1062 mmc_active);
1063 struct mmc_blk_request *brq = &mq_mrq->brq;
1064 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001065 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001066
1067 /*
1068 * sbc.error indicates a problem with the set block count
1069 * command. No data will have been transferred.
1070 *
1071 * cmd.error indicates a problem with the r/w command. No
1072 * data will have been transferred.
1073 *
1074 * stop.error indicates a problem with the stop command. Data
1075 * may have been transferred, or may still be transferring.
1076 */
Adrian Hunter67716322011-08-29 16:42:15 +03001077 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1078 brq->data.error) {
1079 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001080 case ERR_RETRY:
1081 return MMC_BLK_RETRY;
1082 case ERR_ABORT:
1083 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301084 case ERR_NOMEDIUM:
1085 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001086 case ERR_CONTINUE:
1087 break;
1088 }
1089 }
1090
1091 /*
1092 * Check for errors relating to the execution of the
1093 * initial command - such as address errors. No data
1094 * has been transferred.
1095 */
1096 if (brq->cmd.resp[0] & CMD_ERRORS) {
1097 pr_err("%s: r/w command failed, status = %#x\n",
1098 req->rq_disk->disk_name, brq->cmd.resp[0]);
1099 return MMC_BLK_ABORT;
1100 }
1101
1102 /*
1103 * Everything else is either success, or a data error of some
1104 * kind. If it was a write, we may have transitioned to
1105 * program mode, which we have to wait for it to complete.
1106 */
1107 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1108 u32 status;
1109 do {
1110 int err = get_card_status(card, &status, 5);
1111 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301112 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001113 req->rq_disk->disk_name, err);
1114 return MMC_BLK_CMD_ERR;
1115 }
1116 /*
1117 * Some cards mishandle the status bits,
1118 * so make sure to check both the busy
1119 * indication and the card state.
1120 */
1121 } while (!(status & R1_READY_FOR_DATA) ||
1122 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1123 }
1124
1125 if (brq->data.error) {
1126 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1127 req->rq_disk->disk_name, brq->data.error,
1128 (unsigned)blk_rq_pos(req),
1129 (unsigned)blk_rq_sectors(req),
1130 brq->cmd.resp[0], brq->stop.resp[0]);
1131
1132 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001133 if (ecc_err)
1134 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001135 return MMC_BLK_DATA_ERR;
1136 } else {
1137 return MMC_BLK_CMD_ERR;
1138 }
1139 }
1140
Adrian Hunter67716322011-08-29 16:42:15 +03001141 if (!brq->data.bytes_xfered)
1142 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001143
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001144 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1145 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1146 return MMC_BLK_PARTIAL;
1147 else
1148 return MMC_BLK_SUCCESS;
1149 }
1150
Adrian Hunter67716322011-08-29 16:42:15 +03001151 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1152 return MMC_BLK_PARTIAL;
1153
1154 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001155}
1156
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001157static int mmc_blk_packed_err_check(struct mmc_card *card,
1158 struct mmc_async_req *areq)
1159{
1160 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1161 mmc_active);
1162 struct request *req = mq_rq->req;
1163 int err, check, status;
1164 u8 ext_csd[512];
1165
1166 mq_rq->packed_retries--;
1167 check = mmc_blk_err_check(card, areq);
1168 err = get_card_status(card, &status, 0);
1169 if (err) {
1170 pr_err("%s: error %d sending status command\n",
1171 req->rq_disk->disk_name, err);
1172 return MMC_BLK_ABORT;
1173 }
1174
1175 if (status & R1_EXCEPTION_EVENT) {
1176 err = mmc_send_ext_csd(card, ext_csd);
1177 if (err) {
1178 pr_err("%s: error %d sending ext_csd\n",
1179 req->rq_disk->disk_name, err);
1180 return MMC_BLK_ABORT;
1181 }
1182
1183 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1184 EXT_CSD_PACKED_FAILURE) &&
1185 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1186 EXT_CSD_PACKED_GENERIC_ERROR)) {
1187 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1188 EXT_CSD_PACKED_INDEXED_ERROR) {
1189 mq_rq->packed_fail_idx =
1190 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1191 return MMC_BLK_PARTIAL;
1192 }
1193 }
1194 }
1195
1196 return check;
1197}
1198
Per Forlin54d49d772011-07-01 18:55:29 +02001199static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1200 struct mmc_card *card,
1201 int disable_multi,
1202 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Per Forlin54d49d772011-07-01 18:55:29 +02001204 u32 readcmd, writecmd;
1205 struct mmc_blk_request *brq = &mqrq->brq;
1206 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301208 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001210 /*
1211 * Reliable writes are used to implement Forced Unit Access and
1212 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001213 *
1214 * XXX: this really needs a good explanation of why REQ_META
1215 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001216 */
1217 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1218 (req->cmd_flags & REQ_META)) &&
1219 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001220 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001221
Per Forlin54d49d772011-07-01 18:55:29 +02001222 memset(brq, 0, sizeof(struct mmc_blk_request));
1223 brq->mrq.cmd = &brq->cmd;
1224 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Per Forlin54d49d772011-07-01 18:55:29 +02001226 brq->cmd.arg = blk_rq_pos(req);
1227 if (!mmc_card_blockaddr(card))
1228 brq->cmd.arg <<= 9;
1229 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1230 brq->data.blksz = 512;
1231 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1232 brq->stop.arg = 0;
1233 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1234 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Asutosh Das1a897142012-07-27 18:10:19 +05301236 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001237 /*
1238 * The block layer doesn't support all sector count
1239 * restrictions, so we need to be prepared for too big
1240 * requests.
1241 */
1242 if (brq->data.blocks > card->host->max_blk_count)
1243 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001245 if (brq->data.blocks > 1) {
1246 /*
1247 * After a read error, we redo the request one sector
1248 * at a time in order to accurately determine which
1249 * sectors can be read successfully.
1250 */
1251 if (disable_multi)
1252 brq->data.blocks = 1;
1253
1254 /* Some controllers can't do multiblock reads due to hw bugs */
1255 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1256 rq_data_dir(req) == READ)
1257 brq->data.blocks = 1;
1258 }
Per Forlin54d49d772011-07-01 18:55:29 +02001259
1260 if (brq->data.blocks > 1 || do_rel_wr) {
1261 /* SPI multiblock writes terminate using a special
1262 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001263 */
Per Forlin54d49d772011-07-01 18:55:29 +02001264 if (!mmc_host_is_spi(card->host) ||
1265 rq_data_dir(req) == READ)
1266 brq->mrq.stop = &brq->stop;
1267 readcmd = MMC_READ_MULTIPLE_BLOCK;
1268 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1269 } else {
1270 brq->mrq.stop = NULL;
1271 readcmd = MMC_READ_SINGLE_BLOCK;
1272 writecmd = MMC_WRITE_BLOCK;
1273 }
1274 if (rq_data_dir(req) == READ) {
1275 brq->cmd.opcode = readcmd;
1276 brq->data.flags |= MMC_DATA_READ;
1277 } else {
1278 brq->cmd.opcode = writecmd;
1279 brq->data.flags |= MMC_DATA_WRITE;
1280 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001281
Per Forlin54d49d772011-07-01 18:55:29 +02001282 if (do_rel_wr)
1283 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001284
Per Forlin54d49d772011-07-01 18:55:29 +02001285 /*
Saugata Das42659002011-12-21 13:09:17 +05301286 * Data tag is used only during writing meta data to speed
1287 * up write and any subsequent read of this meta data
1288 */
1289 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1290 (req->cmd_flags & REQ_META) &&
1291 (rq_data_dir(req) == WRITE) &&
1292 ((brq->data.blocks * brq->data.blksz) >=
1293 card->ext_csd.data_tag_unit_size);
1294
1295 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001296 * Pre-defined multi-block transfers are preferable to
1297 * open ended-ones (and necessary for reliable writes).
1298 * However, it is not sufficient to just send CMD23,
1299 * and avoid the final CMD12, as on an error condition
1300 * CMD12 (stop) needs to be sent anyway. This, coupled
1301 * with Auto-CMD23 enhancements provided by some
1302 * hosts, means that the complexity of dealing
1303 * with this is best left to the host. If CMD23 is
1304 * supported by card and host, we'll fill sbc in and let
1305 * the host deal with handling it correctly. This means
1306 * that for hosts that don't expose MMC_CAP_CMD23, no
1307 * change of behavior will be observed.
1308 *
1309 * N.B: Some MMC cards experience perf degradation.
1310 * We'll avoid using CMD23-bounded multiblock writes for
1311 * these, while retaining features like reliable writes.
1312 */
Saugata Das42659002011-12-21 13:09:17 +05301313 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1314 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1315 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001316 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1317 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301318 (do_rel_wr ? (1 << 31) : 0) |
1319 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001320 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1321 brq->mrq.sbc = &brq->sbc;
1322 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001323
Per Forlin54d49d772011-07-01 18:55:29 +02001324 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001325
Per Forlin54d49d772011-07-01 18:55:29 +02001326 brq->data.sg = mqrq->sg;
1327 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001328
Per Forlin54d49d772011-07-01 18:55:29 +02001329 /*
1330 * Adjust the sg list so it is the same size as the
1331 * request.
1332 */
1333 if (brq->data.blocks != blk_rq_sectors(req)) {
1334 int i, data_size = brq->data.blocks << 9;
1335 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001336
Per Forlin54d49d772011-07-01 18:55:29 +02001337 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1338 data_size -= sg->length;
1339 if (data_size <= 0) {
1340 sg->length += data_size;
1341 i++;
1342 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001343 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001344 }
Per Forlin54d49d772011-07-01 18:55:29 +02001345 brq->data.sg_len = i;
1346 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001347
Per Forlinee8a43a2011-07-01 18:55:33 +02001348 mqrq->mmc_active.mrq = &brq->mrq;
1349 mqrq->mmc_active.err_check = mmc_blk_err_check;
1350
Per Forlin54d49d772011-07-01 18:55:29 +02001351 mmc_queue_bounce_pre(mqrq);
1352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001354static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1355 struct request *req)
1356{
1357 struct mmc_host *host = mq->card->host;
1358 int data_dir;
1359
1360 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1361 return;
1362
1363 /*
1364 * In case the packing control is not supported by the host, it should
1365 * not have an effect on the write packing. Therefore we have to enable
1366 * the write packing
1367 */
1368 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1369 mq->wr_packing_enabled = true;
1370 return;
1371 }
1372
1373 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1374 if (mq->num_of_potential_packed_wr_reqs >
1375 mq->num_wr_reqs_to_start_packing)
1376 mq->wr_packing_enabled = true;
1377 return;
1378 }
1379
1380 data_dir = rq_data_dir(req);
1381
1382 if (data_dir == READ) {
1383 mq->num_of_potential_packed_wr_reqs = 0;
1384 mq->wr_packing_enabled = false;
1385 return;
1386 } else if (data_dir == WRITE) {
1387 mq->num_of_potential_packed_wr_reqs++;
1388 }
1389
1390 if (mq->num_of_potential_packed_wr_reqs >
1391 mq->num_wr_reqs_to_start_packing)
1392 mq->wr_packing_enabled = true;
1393
1394}
1395
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001396struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1397{
1398 if (!card)
1399 return NULL;
1400
1401 return &card->wr_pack_stats;
1402}
1403EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1404
1405void mmc_blk_init_packed_statistics(struct mmc_card *card)
1406{
1407 int max_num_of_packed_reqs = 0;
1408
1409 if (!card || !card->wr_pack_stats.packing_events)
1410 return;
1411
1412 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1413
1414 spin_lock(&card->wr_pack_stats.lock);
1415 memset(card->wr_pack_stats.packing_events, 0,
1416 (max_num_of_packed_reqs + 1) *
1417 sizeof(*card->wr_pack_stats.packing_events));
1418 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1419 sizeof(card->wr_pack_stats.pack_stop_reason));
1420 card->wr_pack_stats.enabled = true;
1421 spin_unlock(&card->wr_pack_stats.lock);
1422}
1423EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1424
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001425void print_mmc_packing_stats(struct mmc_card *card)
1426{
1427 int i;
1428 int max_num_of_packed_reqs = 0;
1429
1430 if ((!card) || (!card->wr_pack_stats.packing_events))
1431 return;
1432
1433 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1434
1435 spin_lock(&card->wr_pack_stats.lock);
1436
1437 pr_info("%s: write packing statistics:\n",
1438 mmc_hostname(card->host));
1439
1440 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1441 if (card->wr_pack_stats.packing_events[i] != 0)
1442 pr_info("%s: Packed %d reqs - %d times\n",
1443 mmc_hostname(card->host), i,
1444 card->wr_pack_stats.packing_events[i]);
1445 }
1446
1447 pr_info("%s: stopped packing due to the following reasons:\n",
1448 mmc_hostname(card->host));
1449
1450 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1451 pr_info("%s: %d times: exceedmax num of segments\n",
1452 mmc_hostname(card->host),
1453 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1454 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1455 pr_info("%s: %d times: exceeding the max num of sectors\n",
1456 mmc_hostname(card->host),
1457 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1458 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1459 pr_info("%s: %d times: wrong data direction\n",
1460 mmc_hostname(card->host),
1461 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1462 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1463 pr_info("%s: %d times: flush or discard\n",
1464 mmc_hostname(card->host),
1465 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1466 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1467 pr_info("%s: %d times: empty queue\n",
1468 mmc_hostname(card->host),
1469 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1470 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1471 pr_info("%s: %d times: rel write\n",
1472 mmc_hostname(card->host),
1473 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1474 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1475 pr_info("%s: %d times: Threshold\n",
1476 mmc_hostname(card->host),
1477 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1478
1479 spin_unlock(&card->wr_pack_stats.lock);
1480}
1481EXPORT_SYMBOL(print_mmc_packing_stats);
1482
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001483static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1484{
1485 struct request_queue *q = mq->queue;
1486 struct mmc_card *card = mq->card;
1487 struct request *cur = req, *next = NULL;
1488 struct mmc_blk_data *md = mq->data;
1489 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1490 unsigned int req_sectors = 0, phys_segments = 0;
1491 unsigned int max_blk_count, max_phys_segs;
1492 u8 put_back = 0;
1493 u8 max_packed_rw = 0;
1494 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001495 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001496
1497 mmc_blk_clear_packed(mq->mqrq_cur);
1498
1499 if (!(md->flags & MMC_BLK_CMD23) ||
1500 !card->ext_csd.packed_event_en)
1501 goto no_packed;
1502
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001503 if (!mq->wr_packing_enabled)
1504 goto no_packed;
1505
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001506 if ((rq_data_dir(cur) == WRITE) &&
1507 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1508 max_packed_rw = card->ext_csd.max_packed_writes;
1509
1510 if (max_packed_rw == 0)
1511 goto no_packed;
1512
1513 if (mmc_req_rel_wr(cur) &&
1514 (md->flags & MMC_BLK_REL_WR) &&
1515 !en_rel_wr)
1516 goto no_packed;
1517
1518 if (mmc_large_sec(card) &&
1519 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1520 goto no_packed;
1521
1522 max_blk_count = min(card->host->max_blk_count,
1523 card->host->max_req_size >> 9);
1524 if (unlikely(max_blk_count > 0xffff))
1525 max_blk_count = 0xffff;
1526
1527 max_phys_segs = queue_max_segments(q);
1528 req_sectors += blk_rq_sectors(cur);
1529 phys_segments += cur->nr_phys_segments;
1530
1531 if (rq_data_dir(cur) == WRITE) {
1532 req_sectors++;
1533 phys_segments++;
1534 }
1535
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001536 spin_lock(&stats->lock);
1537
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001538 while (reqs < max_packed_rw - 1) {
1539 spin_lock_irq(q->queue_lock);
1540 next = blk_fetch_request(q);
1541 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001542 if (!next) {
1543 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001544 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001545 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001546
1547 if (mmc_large_sec(card) &&
1548 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001549 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001550 put_back = 1;
1551 break;
1552 }
1553
1554 if (next->cmd_flags & REQ_DISCARD ||
1555 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001556 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001557 put_back = 1;
1558 break;
1559 }
1560
1561 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001562 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001563 put_back = 1;
1564 break;
1565 }
1566
1567 if (mmc_req_rel_wr(next) &&
1568 (md->flags & MMC_BLK_REL_WR) &&
1569 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001570 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001571 put_back = 1;
1572 break;
1573 }
1574
1575 req_sectors += blk_rq_sectors(next);
1576 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001577 if (stats->enabled)
1578 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001579 put_back = 1;
1580 break;
1581 }
1582
1583 phys_segments += next->nr_phys_segments;
1584 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001585 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001586 put_back = 1;
1587 break;
1588 }
1589
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001590 if (rq_data_dir(next) == WRITE)
1591 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001592 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1593 cur = next;
1594 reqs++;
1595 }
1596
1597 if (put_back) {
1598 spin_lock_irq(q->queue_lock);
1599 blk_requeue_request(q, next);
1600 spin_unlock_irq(q->queue_lock);
1601 }
1602
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001603 if (stats->enabled) {
1604 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1605 stats->packing_events[reqs + 1]++;
1606 if (reqs + 1 == max_packed_rw)
1607 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1608 }
1609
1610 spin_unlock(&stats->lock);
1611
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001612 if (reqs > 0) {
1613 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1614 mq->mqrq_cur->packed_num = ++reqs;
1615 mq->mqrq_cur->packed_retries = reqs;
1616 return reqs;
1617 }
1618
1619no_packed:
1620 mmc_blk_clear_packed(mq->mqrq_cur);
1621 return 0;
1622}
1623
1624static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1625 struct mmc_card *card,
1626 struct mmc_queue *mq)
1627{
1628 struct mmc_blk_request *brq = &mqrq->brq;
1629 struct request *req = mqrq->req;
1630 struct request *prq;
1631 struct mmc_blk_data *md = mq->data;
1632 bool do_rel_wr, do_data_tag;
1633 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1634 u8 i = 1;
1635
1636 mqrq->packed_cmd = MMC_PACKED_WRITE;
1637 mqrq->packed_blocks = 0;
1638 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1639
1640 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1641 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1642 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1643
1644 /*
1645 * Argument for each entry of packed group
1646 */
1647 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1648 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1649 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1650 (prq->cmd_flags & REQ_META) &&
1651 (rq_data_dir(prq) == WRITE) &&
1652 ((brq->data.blocks * brq->data.blksz) >=
1653 card->ext_csd.data_tag_unit_size);
1654 /* Argument of CMD23 */
1655 packed_cmd_hdr[(i * 2)] =
1656 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1657 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1658 blk_rq_sectors(prq);
1659 /* Argument of CMD18 or CMD25 */
1660 packed_cmd_hdr[((i * 2)) + 1] =
1661 mmc_card_blockaddr(card) ?
1662 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1663 mqrq->packed_blocks += blk_rq_sectors(prq);
1664 i++;
1665 }
1666
1667 memset(brq, 0, sizeof(struct mmc_blk_request));
1668 brq->mrq.cmd = &brq->cmd;
1669 brq->mrq.data = &brq->data;
1670 brq->mrq.sbc = &brq->sbc;
1671 brq->mrq.stop = &brq->stop;
1672
1673 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1674 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1675 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1676
1677 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1678 brq->cmd.arg = blk_rq_pos(req);
1679 if (!mmc_card_blockaddr(card))
1680 brq->cmd.arg <<= 9;
1681 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1682
1683 brq->data.blksz = 512;
1684 brq->data.blocks = mqrq->packed_blocks + 1;
1685 brq->data.flags |= MMC_DATA_WRITE;
1686
1687 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1688 brq->stop.arg = 0;
1689 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1690
1691 mmc_set_data_timeout(&brq->data, card);
1692
1693 brq->data.sg = mqrq->sg;
1694 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1695
1696 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001697
1698 /*
1699 * This is intended for packed commands tests usage - in case these
1700 * functions are not in use the respective pointers are NULL
1701 */
1702 if (mq->err_check_fn)
1703 mqrq->mmc_active.err_check = mq->err_check_fn;
1704 else
1705 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1706
1707 if (mq->packed_test_fn)
1708 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001709
1710 mmc_queue_bounce_pre(mqrq);
1711}
1712
Adrian Hunter67716322011-08-29 16:42:15 +03001713static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1714 struct mmc_blk_request *brq, struct request *req,
1715 int ret)
1716{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001717 struct mmc_queue_req *mq_rq;
1718 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1719
Adrian Hunter67716322011-08-29 16:42:15 +03001720 /*
1721 * If this is an SD card and we're writing, we can first
1722 * mark the known good sectors as ok.
1723 *
1724 * If the card is not SD, we can still ok written sectors
1725 * as reported by the controller (which might be less than
1726 * the real number of written sectors, but never more).
1727 */
1728 if (mmc_card_sd(card)) {
1729 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301730 if (!brq->data.fault_injected) {
1731 blocks = mmc_sd_num_wr_blocks(card);
1732 if (blocks != (u32)-1)
1733 ret = blk_end_request(req, 0, blocks << 9);
1734 } else
1735 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001736 } else {
1737 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1738 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1739 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001740 return ret;
1741}
1742
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001743static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1744{
1745 struct request *prq;
1746 int idx = mq_rq->packed_fail_idx, i = 0;
1747 int ret = 0;
1748
1749 while (!list_empty(&mq_rq->packed_list)) {
1750 prq = list_entry_rq(mq_rq->packed_list.next);
1751 if (idx == i) {
1752 /* retry from error index */
1753 mq_rq->packed_num -= idx;
1754 mq_rq->req = prq;
1755 ret = 1;
1756
1757 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1758 list_del_init(&prq->queuelist);
1759 mmc_blk_clear_packed(mq_rq);
1760 }
1761 return ret;
1762 }
1763 list_del_init(&prq->queuelist);
1764 blk_end_request(prq, 0, blk_rq_bytes(prq));
1765 i++;
1766 }
1767
1768 mmc_blk_clear_packed(mq_rq);
1769 return ret;
1770}
1771static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1772{
1773 struct request *prq;
1774
1775 while (!list_empty(&mq_rq->packed_list)) {
1776 prq = list_entry_rq(mq_rq->packed_list.next);
1777 list_del_init(&prq->queuelist);
1778 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1779 }
1780
1781 mmc_blk_clear_packed(mq_rq);
1782}
1783
1784static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1785 struct mmc_queue_req *mq_rq)
1786{
1787 struct request *prq;
1788 struct request_queue *q = mq->queue;
1789
1790 while (!list_empty(&mq_rq->packed_list)) {
1791 prq = list_entry_rq(mq_rq->packed_list.prev);
1792 if (prq->queuelist.prev != &mq_rq->packed_list) {
1793 list_del_init(&prq->queuelist);
1794 spin_lock_irq(q->queue_lock);
1795 blk_requeue_request(mq->queue, prq);
1796 spin_unlock_irq(q->queue_lock);
1797 } else {
1798 list_del_init(&prq->queuelist);
1799 }
1800 }
1801
1802 mmc_blk_clear_packed(mq_rq);
1803}
1804
Per Forlinee8a43a2011-07-01 18:55:33 +02001805static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001806{
1807 struct mmc_blk_data *md = mq->data;
1808 struct mmc_card *card = md->queue.card;
1809 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001810 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001811 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001812 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001813 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001814 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001815 const u8 packed_num = 2;
1816 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001817
1818 if (!rqc && !mq->mqrq_prev->req)
1819 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001820
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001821 if (rqc)
1822 reqs = mmc_blk_prep_packed_list(mq, rqc);
1823
Per Forlin54d49d772011-07-01 18:55:29 +02001824 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001825 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001826 if (reqs >= packed_num)
1827 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1828 card, mq);
1829 else
1830 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001831 areq = &mq->mqrq_cur->mmc_active;
1832 } else
1833 areq = NULL;
1834 areq = mmc_start_req(card->host, areq, (int *) &status);
1835 if (!areq)
1836 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001837
Per Forlinee8a43a2011-07-01 18:55:33 +02001838 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1839 brq = &mq_rq->brq;
1840 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001841 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001842 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001843
Per Forlind78d4a82011-07-01 18:55:30 +02001844 switch (status) {
1845 case MMC_BLK_SUCCESS:
1846 case MMC_BLK_PARTIAL:
1847 /*
1848 * A block was successfully transferred.
1849 */
Adrian Hunter67716322011-08-29 16:42:15 +03001850 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001851
1852 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1853 ret = mmc_blk_end_packed_req(mq_rq);
1854 break;
1855 } else {
1856 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001857 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001858 }
1859
Adrian Hunter67716322011-08-29 16:42:15 +03001860 /*
1861 * If the blk_end_request function returns non-zero even
1862 * though all data has been transferred and no errors
1863 * were returned by the host controller, it's a bug.
1864 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001865 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301866 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001867 __func__, blk_rq_bytes(req),
1868 brq->data.bytes_xfered);
1869 rqc = NULL;
1870 goto cmd_abort;
1871 }
Per Forlind78d4a82011-07-01 18:55:30 +02001872 break;
1873 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001874 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1875 if (!mmc_blk_reset(md, card->host, type))
1876 break;
1877 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001878 case MMC_BLK_RETRY:
1879 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001880 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001881 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001882 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001883 if (!mmc_blk_reset(md, card->host, type))
1884 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001885 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001886 case MMC_BLK_DATA_ERR: {
1887 int err;
1888
1889 err = mmc_blk_reset(md, card->host, type);
1890 if (!err)
1891 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001892 if (err == -ENODEV ||
1893 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001894 goto cmd_abort;
1895 /* Fall through */
1896 }
1897 case MMC_BLK_ECC_ERR:
1898 if (brq->data.blocks > 1) {
1899 /* Redo read one sector at a time */
1900 pr_warning("%s: retrying using single block read\n",
1901 req->rq_disk->disk_name);
1902 disable_multi = 1;
1903 break;
1904 }
Per Forlind78d4a82011-07-01 18:55:30 +02001905 /*
1906 * After an error, we redo I/O one sector at a
1907 * time, so we only reach here after trying to
1908 * read a single sector.
1909 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301910 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001911 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001912 if (!ret)
1913 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001914 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301915 case MMC_BLK_NOMEDIUM:
1916 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001917 }
1918
Per Forlinee8a43a2011-07-01 18:55:33 +02001919 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001920 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1921 /*
1922 * In case of a incomplete request
1923 * prepare it again and resend.
1924 */
1925 mmc_blk_rw_rq_prep(mq_rq, card,
1926 disable_multi, mq);
1927 mmc_start_req(card->host,
1928 &mq_rq->mmc_active, NULL);
1929 } else {
1930 if (!mq_rq->packed_retries)
1931 goto cmd_abort;
1932 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1933 mmc_start_req(card->host,
1934 &mq_rq->mmc_active, NULL);
1935 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 } while (ret);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return 1;
1940
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001941 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001942 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1943 if (mmc_card_removed(card))
1944 req->cmd_flags |= REQ_QUIET;
1945 while (ret)
1946 ret = blk_end_request(req, -EIO,
1947 blk_rq_cur_bytes(req));
1948 } else {
1949 mmc_blk_abort_packed_req(mq_rq);
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Per Forlinee8a43a2011-07-01 18:55:33 +02001952 start_new_req:
1953 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001954 /*
1955 * If current request is packed, it needs to put back.
1956 */
1957 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1958 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1959
Per Forlinee8a43a2011-07-01 18:55:33 +02001960 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1961 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1962 }
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965}
1966
Adrian Hunterbd788c92010-08-11 14:17:47 -07001967static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1968{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001969 int ret;
1970 struct mmc_blk_data *md = mq->data;
1971 struct mmc_card *card = md->queue.card;
1972
San Mehat148c57a2009-07-30 08:21:19 -07001973#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1974 if (mmc_bus_needs_resume(card->host)) {
1975 mmc_resume_bus(card->host);
1976 mmc_blk_set_blksize(md, card);
1977 }
1978#endif
1979
Per Forlinee8a43a2011-07-01 18:55:33 +02001980 if (req && !mq->mqrq_prev->req)
1981 /* claim host only for the first request */
1982 mmc_claim_host(card->host);
1983
Andrei Warkentin371a6892011-04-11 18:10:25 -05001984 ret = mmc_blk_part_switch(card, md);
1985 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001986 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301987 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001988 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001989 ret = 0;
1990 goto out;
1991 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001992
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001993 mmc_blk_write_packing_control(mq, req);
1994
Maya Erez463bb952012-05-24 23:46:29 +03001995 if (req && req->cmd_flags & REQ_SANITIZE) {
1996 /* complete ongoing async transfer before issuing sanitize */
1997 if (card->host && card->host->areq)
1998 mmc_blk_issue_rw_rq(mq, NULL);
1999 ret = mmc_blk_issue_sanitize_rq(mq, req);
2000 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002001 /* complete ongoing async transfer before issuing discard */
2002 if (card->host->areq)
2003 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002004 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002005 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002006 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002007 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002008 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002009 /* complete ongoing async transfer before issuing flush */
2010 if (card->host->areq)
2011 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002012 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002013 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002014 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002015 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002016
Andrei Warkentin371a6892011-04-11 18:10:25 -05002017out:
Per Forlinee8a43a2011-07-01 18:55:33 +02002018 if (!req)
2019 /* release host only when there are no more requests */
2020 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002021 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Russell Kinga6f6c962006-01-03 22:38:44 +00002024static inline int mmc_blk_readonly(struct mmc_card *card)
2025{
2026 return mmc_card_readonly(card) ||
2027 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2028}
2029
Andrei Warkentin371a6892011-04-11 18:10:25 -05002030static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2031 struct device *parent,
2032 sector_t size,
2033 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002034 const char *subname,
2035 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 struct mmc_blk_data *md;
2038 int devidx, ret;
2039
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002040 devidx = find_first_zero_bit(dev_use, max_devices);
2041 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return ERR_PTR(-ENOSPC);
2043 __set_bit(devidx, dev_use);
2044
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002045 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002046 if (!md) {
2047 ret = -ENOMEM;
2048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002050
Russell Kinga6f6c962006-01-03 22:38:44 +00002051 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002052 * !subname implies we are creating main mmc_blk_data that will be
2053 * associated with mmc_card with mmc_set_drvdata. Due to device
2054 * partitions, devidx will not coincide with a per-physical card
2055 * index anymore so we keep track of a name index.
2056 */
2057 if (!subname) {
2058 md->name_idx = find_first_zero_bit(name_use, max_devices);
2059 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002060 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002061 md->name_idx = ((struct mmc_blk_data *)
2062 dev_to_disk(parent)->private_data)->name_idx;
2063
Johan Rudholmadd710e2011-12-02 08:51:06 +01002064 md->area_type = area_type;
2065
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002066 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002067 * Set the read-only status based on the supported commands
2068 * and the write protect switch.
2069 */
2070 md->read_only = mmc_blk_readonly(card);
2071
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002072 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002073 if (md->disk == NULL) {
2074 ret = -ENOMEM;
2075 goto err_kfree;
2076 }
2077
2078 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002079 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002080 md->usage = 1;
2081
Adrian Hunterd09408a2011-06-23 13:40:28 +03002082 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002083 if (ret)
2084 goto err_putdisk;
2085
Russell Kinga6f6c962006-01-03 22:38:44 +00002086 md->queue.issue_fn = mmc_blk_issue_rq;
2087 md->queue.data = md;
2088
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002089 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002090 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002091 md->disk->fops = &mmc_bdops;
2092 md->disk->private_data = md;
2093 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002094 md->disk->driverfs_dev = parent;
2095 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002096 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002097
2098 /*
2099 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2100 *
2101 * - be set for removable media with permanent block devices
2102 * - be unset for removable block devices with permanent media
2103 *
2104 * Since MMC block devices clearly fall under the second
2105 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2106 * should use the block device creation/destruction hotplug
2107 * messages to tell when the card is present.
2108 */
2109
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002110 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2111 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002112
Martin K. Petersene1defc42009-05-22 17:17:49 -04002113 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002114 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002115
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002116 if (mmc_host_cmd23(card->host)) {
2117 if (mmc_card_mmc(card) ||
2118 (mmc_card_sd(card) &&
2119 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2120 md->flags |= MMC_BLK_CMD23;
2121 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002122
2123 if (mmc_card_mmc(card) &&
2124 md->flags & MMC_BLK_CMD23 &&
2125 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2126 card->ext_csd.rel_sectors)) {
2127 md->flags |= MMC_BLK_REL_WR;
2128 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2129 }
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002132
2133 err_putdisk:
2134 put_disk(md->disk);
2135 err_kfree:
2136 kfree(md);
2137 out:
2138 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Andrei Warkentin371a6892011-04-11 18:10:25 -05002141static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2142{
2143 sector_t size;
2144 struct mmc_blk_data *md;
2145
2146 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2147 /*
2148 * The EXT_CSD sector count is in number or 512 byte
2149 * sectors.
2150 */
2151 size = card->ext_csd.sectors;
2152 } else {
2153 /*
2154 * The CSD capacity field is in units of read_blkbits.
2155 * set_capacity takes units of 512 bytes.
2156 */
2157 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2158 }
2159
Johan Rudholmadd710e2011-12-02 08:51:06 +01002160 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2161 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002162 return md;
2163}
2164
2165static int mmc_blk_alloc_part(struct mmc_card *card,
2166 struct mmc_blk_data *md,
2167 unsigned int part_type,
2168 sector_t size,
2169 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002170 const char *subname,
2171 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002172{
2173 char cap_str[10];
2174 struct mmc_blk_data *part_md;
2175
2176 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002177 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002178 if (IS_ERR(part_md))
2179 return PTR_ERR(part_md);
2180 part_md->part_type = part_type;
2181 list_add(&part_md->part, &md->part);
2182
2183 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2184 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302185 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002186 part_md->disk->disk_name, mmc_card_id(card),
2187 mmc_card_name(card), part_md->part_type, cap_str);
2188 return 0;
2189}
2190
Namjae Jeone0c368d2011-10-06 23:41:38 +09002191/* MMC Physical partitions consist of two boot partitions and
2192 * up to four general purpose partitions.
2193 * For each partition enabled in EXT_CSD a block device will be allocatedi
2194 * to provide access to the partition.
2195 */
2196
Andrei Warkentin371a6892011-04-11 18:10:25 -05002197static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2198{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002199 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002200
2201 if (!mmc_card_mmc(card))
2202 return 0;
2203
Namjae Jeone0c368d2011-10-06 23:41:38 +09002204 for (idx = 0; idx < card->nr_parts; idx++) {
2205 if (card->part[idx].size) {
2206 ret = mmc_blk_alloc_part(card, md,
2207 card->part[idx].part_cfg,
2208 card->part[idx].size >> 9,
2209 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002210 card->part[idx].name,
2211 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002212 if (ret)
2213 return ret;
2214 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002215 }
2216
2217 return ret;
2218}
2219
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220static void mmc_blk_remove_req(struct mmc_blk_data *md)
2221{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002222 struct mmc_card *card;
2223
Andrei Warkentin371a6892011-04-11 18:10:25 -05002224 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002225 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002226 device_remove_file(disk_to_dev(md->disk),
2227 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002228 if (md->disk->flags & GENHD_FL_UP) {
2229 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002230 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2231 card->ext_csd.boot_ro_lockable)
2232 device_remove_file(disk_to_dev(md->disk),
2233 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002234
2235 /* Stop new requests from getting into the queue */
2236 del_gendisk(md->disk);
2237 }
2238
2239 /* Then flush out any already in there */
2240 mmc_cleanup_queue(&md->queue);
2241 mmc_blk_put(md);
2242 }
2243}
2244
2245static void mmc_blk_remove_parts(struct mmc_card *card,
2246 struct mmc_blk_data *md)
2247{
2248 struct list_head *pos, *q;
2249 struct mmc_blk_data *part_md;
2250
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002251 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002252 list_for_each_safe(pos, q, &md->part) {
2253 part_md = list_entry(pos, struct mmc_blk_data, part);
2254 list_del(pos);
2255 mmc_blk_remove_req(part_md);
2256 }
2257}
2258
2259static int mmc_add_disk(struct mmc_blk_data *md)
2260{
2261 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002262 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002263
2264 add_disk(md->disk);
2265 md->force_ro.show = force_ro_show;
2266 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302267 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002268 md->force_ro.attr.name = "force_ro";
2269 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2270 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2271 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002272 goto force_ro_fail;
2273
2274 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2275 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002276 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002277
2278 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2279 mode = S_IRUGO;
2280 else
2281 mode = S_IRUGO | S_IWUSR;
2282
2283 md->power_ro_lock.show = power_ro_lock_show;
2284 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002285 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002286 md->power_ro_lock.attr.mode = mode;
2287 md->power_ro_lock.attr.name =
2288 "ro_lock_until_next_power_on";
2289 ret = device_create_file(disk_to_dev(md->disk),
2290 &md->power_ro_lock);
2291 if (ret)
2292 goto power_ro_lock_fail;
2293 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002294
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002295 md->num_wr_reqs_to_start_packing.show =
2296 num_wr_reqs_to_start_packing_show;
2297 md->num_wr_reqs_to_start_packing.store =
2298 num_wr_reqs_to_start_packing_store;
2299 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2300 md->num_wr_reqs_to_start_packing.attr.name =
2301 "num_wr_reqs_to_start_packing";
2302 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2303 ret = device_create_file(disk_to_dev(md->disk),
2304 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002305 if (ret)
2306 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002307
Johan Rudholmadd710e2011-12-02 08:51:06 +01002308 return ret;
2309
2310power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002311 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002312force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002313 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002314
2315 return ret;
2316}
2317
Chris Ballc59d4472011-11-11 22:01:43 -05002318#define CID_MANFID_SANDISK 0x2
2319#define CID_MANFID_TOSHIBA 0x11
2320#define CID_MANFID_MICRON 0x13
2321
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002322static const struct mmc_fixup blk_fixups[] =
2323{
Chris Ballc59d4472011-11-11 22:01:43 -05002324 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2325 MMC_QUIRK_INAND_CMD38),
2326 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2327 MMC_QUIRK_INAND_CMD38),
2328 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2329 MMC_QUIRK_INAND_CMD38),
2330 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2331 MMC_QUIRK_INAND_CMD38),
2332 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2333 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002334
2335 /*
2336 * Some MMC cards experience performance degradation with CMD23
2337 * instead of CMD12-bounded multiblock transfers. For now we'll
2338 * black list what's bad...
2339 * - Certain Toshiba cards.
2340 *
2341 * N.B. This doesn't affect SD cards.
2342 */
Chris Ballc59d4472011-11-11 22:01:43 -05002343 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002344 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002345 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002346 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002347 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002348 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002349
2350 /*
2351 * Some Micron MMC cards needs longer data read timeout than
2352 * indicated in CSD.
2353 */
Chris Ballc59d4472011-11-11 22:01:43 -05002354 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002355 MMC_QUIRK_LONG_READ_TIME),
2356
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302357 /* Some INAND MCP devices advertise incorrect timeout values */
2358 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2359 MMC_QUIRK_INAND_DATA_TIMEOUT),
2360
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002361 END_FIXUP
2362};
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364static int mmc_blk_probe(struct mmc_card *card)
2365{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002366 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002367 char cap_str[10];
2368
Pierre Ossman912490d2005-05-21 10:27:02 +01002369 /*
2370 * Check that the card supports the command class(es) we need.
2371 */
2372 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return -ENODEV;
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 md = mmc_blk_alloc(card);
2376 if (IS_ERR(md))
2377 return PTR_ERR(md);
2378
Yi Li444122f2009-02-05 15:31:57 +08002379 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002380 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302381 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002383 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Andrei Warkentin371a6892011-04-11 18:10:25 -05002385 if (mmc_blk_alloc_parts(card, md))
2386 goto out;
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002389 mmc_fixup_device(card, blk_fixups);
2390
San Mehat148c57a2009-07-30 08:21:19 -07002391#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2392 mmc_set_bus_resume_policy(card->host, 1);
2393#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002394 if (mmc_add_disk(md))
2395 goto out;
2396
2397 list_for_each_entry(part_md, &md->part, part) {
2398 if (mmc_add_disk(part_md))
2399 goto out;
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return 0;
2402
2403 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002404 mmc_blk_remove_parts(card, md);
2405 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static void mmc_blk_remove(struct mmc_card *card)
2410{
2411 struct mmc_blk_data *md = mmc_get_drvdata(card);
2412
Andrei Warkentin371a6892011-04-11 18:10:25 -05002413 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002414 mmc_claim_host(card->host);
2415 mmc_blk_part_switch(card, md);
2416 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002417 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002419#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2420 mmc_set_bus_resume_policy(card->host, 0);
2421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002425static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002427 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct mmc_blk_data *md = mmc_get_drvdata(card);
2429
2430 if (md) {
2431 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002432 list_for_each_entry(part_md, &md->part, part) {
2433 mmc_queue_suspend(&part_md->queue);
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436 return 0;
2437}
2438
2439static int mmc_blk_resume(struct mmc_card *card)
2440{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002441 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct mmc_blk_data *md = mmc_get_drvdata(card);
2443
2444 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002445 /*
2446 * Resume involves the card going into idle state,
2447 * so current partition is always the main one.
2448 */
2449 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002451 list_for_each_entry(part_md, &md->part, part) {
2452 mmc_queue_resume(&part_md->queue);
2453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 return 0;
2456}
2457#else
2458#define mmc_blk_suspend NULL
2459#define mmc_blk_resume NULL
2460#endif
2461
2462static struct mmc_driver mmc_driver = {
2463 .drv = {
2464 .name = "mmcblk",
2465 },
2466 .probe = mmc_blk_probe,
2467 .remove = mmc_blk_remove,
2468 .suspend = mmc_blk_suspend,
2469 .resume = mmc_blk_resume,
2470};
2471
2472static int __init mmc_blk_init(void)
2473{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002474 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002476 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2477 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2478
2479 max_devices = 256 / perdev_minors;
2480
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002481 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2482 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002485 res = mmc_register_driver(&mmc_driver);
2486 if (res)
2487 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002489 return 0;
2490 out2:
2491 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 out:
2493 return res;
2494}
2495
2496static void __exit mmc_blk_exit(void)
2497{
2498 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002499 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502module_init(mmc_blk_init);
2503module_exit(mmc_blk_exit);
2504
2505MODULE_LICENSE("GPL");
2506MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2507