blob: a496df07b3c1ad7da939de4e58089339f370c8c0 [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 */
Seungwon Jeon968c7742012-05-31 11:54:47 +030062#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
63 (req->cmd_flags & REQ_META)) && \
64 (rq_data_dir(req) == WRITE))
65#define PACKED_CMD_VER 0x01
66#define PACKED_CMD_WR 0x02
Maya Ereze544d7002012-06-04 06:09:47 +030067#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
68 do { \
69 if (stats->enabled) \
70 stats->pack_stop_reason[reason]++; \
71 } while (0)
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030072
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020073static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040074
75/*
76 * The defaults come from config options but can be overriden by module
77 * or bootarg options.
78 */
79static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80
81/*
82 * We've only got one major, so number of mmcblk devices is
83 * limited to 256 / number of minors per device.
84 */
85static int max_devices;
86
87/* 256 minors, so at most 256 separate devices */
88static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050089static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * There is one mmc_blk_data per slot.
93 */
94struct mmc_blk_data {
95 spinlock_t lock;
96 struct gendisk *disk;
97 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050098 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500100 unsigned int flags;
101#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
102#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000105 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500106 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500107 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300108 unsigned int reset_done;
109#define MMC_BLK_READ BIT(0)
110#define MMC_BLK_WRITE BIT(1)
111#define MMC_BLK_DISCARD BIT(2)
112#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500113
114 /*
115 * Only set in main mmc_blk_data associated
116 * with mmc_card with mmc_set_drvdata, and keeps
117 * track of the current selected device partition.
118 */
119 unsigned int part_curr;
120 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100121 struct device_attribute power_ro_lock;
122 int area_type;
Maya Erez63c61d62012-05-31 21:00:18 +0300123 struct device_attribute num_wr_reqs_to_start_packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Arjan van de Vena621aae2006-01-12 18:43:35 +0000126static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Seungwon Jeon968c7742012-05-31 11:54:47 +0300128enum {
129 MMC_PACKED_N_IDX = -1,
130 MMC_PACKED_N_ZERO,
131 MMC_PACKED_N_SINGLE,
132};
133
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400134module_param(perdev_minors, int, 0444);
135MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
136
Seungwon Jeon968c7742012-05-31 11:54:47 +0300137static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
138{
139 mqrq->packed_cmd = MMC_PACKED_NONE;
140 mqrq->packed_num = MMC_PACKED_N_ZERO;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
144{
145 struct mmc_blk_data *md;
146
Arjan van de Vena621aae2006-01-12 18:43:35 +0000147 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 md = disk->private_data;
149 if (md && md->usage == 0)
150 md = NULL;
151 if (md)
152 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return md;
156}
157
Andrei Warkentin371a6892011-04-11 18:10:25 -0500158static inline int mmc_get_devidx(struct gendisk *disk)
159{
Colin Cross71b54d42010-09-03 12:41:21 -0700160 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500161 return devidx;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void mmc_blk_put(struct mmc_blk_data *md)
165{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000166 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 md->usage--;
168 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500169 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800170 blk_cleanup_queue(md->queue.queue);
171
David Woodhouse1dff3142007-11-21 18:45:12 +0100172 __clear_bit(devidx, dev_use);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kfree(md);
176 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000177 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Johan Rudholmadd710e2011-12-02 08:51:06 +0100180static ssize_t power_ro_lock_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
183 int ret;
184 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
185 struct mmc_card *card = md->queue.card;
186 int locked = 0;
187
188 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
189 locked = 2;
190 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
191 locked = 1;
192
193 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
194
195 return ret;
196}
197
198static ssize_t power_ro_lock_store(struct device *dev,
199 struct device_attribute *attr, const char *buf, size_t count)
200{
201 int ret;
202 struct mmc_blk_data *md, *part_md;
203 struct mmc_card *card;
204 unsigned long set;
205
206 if (kstrtoul(buf, 0, &set))
207 return -EINVAL;
208
209 if (set != 1)
210 return count;
211
212 md = mmc_blk_get(dev_to_disk(dev));
213 card = md->queue.card;
214
215 mmc_claim_host(card->host);
216
217 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
218 card->ext_csd.boot_ro_lock |
219 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
220 card->ext_csd.part_time);
221 if (ret)
222 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
223 else
224 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
225
226 mmc_release_host(card->host);
227
228 if (!ret) {
229 pr_info("%s: Locking boot partition ro until next power on\n",
230 md->disk->disk_name);
231 set_disk_ro(md->disk, 1);
232
233 list_for_each_entry(part_md, &md->part, part)
234 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
235 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
236 set_disk_ro(part_md->disk, 1);
237 }
238 }
239
240 mmc_blk_put(md);
241 return count;
242}
243
Andrei Warkentin371a6892011-04-11 18:10:25 -0500244static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
245 char *buf)
246{
247 int ret;
248 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
249
250 ret = snprintf(buf, PAGE_SIZE, "%d",
251 get_disk_ro(dev_to_disk(dev)) ^
252 md->read_only);
253 mmc_blk_put(md);
254 return ret;
255}
256
257static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
258 const char *buf, size_t count)
259{
260 int ret;
261 char *end;
262 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
263 unsigned long set = simple_strtoul(buf, &end, 0);
264 if (end == buf) {
265 ret = -EINVAL;
266 goto out;
267 }
268
269 set_disk_ro(dev_to_disk(dev), set || md->read_only);
270 ret = count;
271out:
272 mmc_blk_put(md);
273 return ret;
274}
275
Maya Erez63c61d62012-05-31 21:00:18 +0300276static ssize_t
277num_wr_reqs_to_start_packing_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
279{
280 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
281 int num_wr_reqs_to_start_packing;
282 int ret;
283
284 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
285
286 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
287
288 mmc_blk_put(md);
289 return ret;
290}
291
292static ssize_t
293num_wr_reqs_to_start_packing_store(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf, size_t count)
296{
297 int value;
298 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
299
300 sscanf(buf, "%d", &value);
301 if (value >= 0)
302 md->queue.num_wr_reqs_to_start_packing = value;
303
304 mmc_blk_put(md);
305 return count;
306}
307
Al Viroa5a15612008-03-02 10:33:30 -0500308static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Al Viroa5a15612008-03-02 10:33:30 -0500310 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int ret = -ENXIO;
312
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200313 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (md) {
315 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500316 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700318
Al Viroa5a15612008-03-02 10:33:30 -0500319 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700320 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700321 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200324 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 return ret;
327}
328
Al Viroa5a15612008-03-02 10:33:30 -0500329static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Al Viroa5a15612008-03-02 10:33:30 -0500331 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200333 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200335 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
339static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800340mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800342 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
343 geo->heads = 4;
344 geo->sectors = 16;
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
John Calixtocb87ea22011-04-26 18:56:29 -0400348struct mmc_blk_ioc_data {
349 struct mmc_ioc_cmd ic;
350 unsigned char *buf;
351 u64 buf_bytes;
352};
353
354static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
355 struct mmc_ioc_cmd __user *user)
356{
357 struct mmc_blk_ioc_data *idata;
358 int err;
359
360 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
361 if (!idata) {
362 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400364 }
365
366 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
367 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400368 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400369 }
370
371 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
372 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
373 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400374 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400375 }
376
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100377 if (!idata->buf_bytes)
378 return idata;
379
John Calixtocb87ea22011-04-26 18:56:29 -0400380 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
381 if (!idata->buf) {
382 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400383 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400384 }
385
386 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
387 idata->ic.data_ptr, idata->buf_bytes)) {
388 err = -EFAULT;
389 goto copy_err;
390 }
391
392 return idata;
393
394copy_err:
395 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400396idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400397 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400398out:
John Calixtocb87ea22011-04-26 18:56:29 -0400399 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400400}
401
402static int mmc_blk_ioctl_cmd(struct block_device *bdev,
403 struct mmc_ioc_cmd __user *ic_ptr)
404{
405 struct mmc_blk_ioc_data *idata;
406 struct mmc_blk_data *md;
407 struct mmc_card *card;
408 struct mmc_command cmd = {0};
409 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530410 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400411 struct scatterlist sg;
412 int err;
413
414 /*
415 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
416 * whole block device, not on a partition. This prevents overspray
417 * between sibling partitions.
418 */
419 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
420 return -EPERM;
421
422 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
423 if (IS_ERR(idata))
424 return PTR_ERR(idata);
425
John Calixtocb87ea22011-04-26 18:56:29 -0400426 md = mmc_blk_get(bdev->bd_disk);
427 if (!md) {
428 err = -EINVAL;
429 goto cmd_done;
430 }
431
432 card = md->queue.card;
433 if (IS_ERR(card)) {
434 err = PTR_ERR(card);
435 goto cmd_done;
436 }
437
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100438 cmd.opcode = idata->ic.opcode;
439 cmd.arg = idata->ic.arg;
440 cmd.flags = idata->ic.flags;
441
442 if (idata->buf_bytes) {
443 data.sg = &sg;
444 data.sg_len = 1;
445 data.blksz = idata->ic.blksz;
446 data.blocks = idata->ic.blocks;
447
448 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
449
450 if (idata->ic.write_flag)
451 data.flags = MMC_DATA_WRITE;
452 else
453 data.flags = MMC_DATA_READ;
454
455 /* data.flags must already be set before doing this. */
456 mmc_set_data_timeout(&data, card);
457
458 /* Allow overriding the timeout_ns for empirical tuning. */
459 if (idata->ic.data_timeout_ns)
460 data.timeout_ns = idata->ic.data_timeout_ns;
461
462 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
463 /*
464 * Pretend this is a data transfer and rely on the
465 * host driver to compute timeout. When all host
466 * drivers support cmd.cmd_timeout for R1B, this
467 * can be changed to:
468 *
469 * mrq.data = NULL;
470 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
471 */
472 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
473 }
474
475 mrq.data = &data;
476 }
477
478 mrq.cmd = &cmd;
479
John Calixtocb87ea22011-04-26 18:56:29 -0400480 mmc_claim_host(card->host);
481
482 if (idata->ic.is_acmd) {
483 err = mmc_app_cmd(card->host, card);
484 if (err)
485 goto cmd_rel_host;
486 }
487
John Calixtocb87ea22011-04-26 18:56:29 -0400488 mmc_wait_for_req(card->host, &mrq);
489
490 if (cmd.error) {
491 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
492 __func__, cmd.error);
493 err = cmd.error;
494 goto cmd_rel_host;
495 }
496 if (data.error) {
497 dev_err(mmc_dev(card->host), "%s: data error %d\n",
498 __func__, data.error);
499 err = data.error;
500 goto cmd_rel_host;
501 }
502
503 /*
504 * According to the SD specs, some commands require a delay after
505 * issuing the command.
506 */
507 if (idata->ic.postsleep_min_us)
508 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
509
510 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
511 err = -EFAULT;
512 goto cmd_rel_host;
513 }
514
515 if (!idata->ic.write_flag) {
516 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
517 idata->buf, idata->buf_bytes)) {
518 err = -EFAULT;
519 goto cmd_rel_host;
520 }
521 }
522
523cmd_rel_host:
524 mmc_release_host(card->host);
525
526cmd_done:
527 mmc_blk_put(md);
528 kfree(idata->buf);
529 kfree(idata);
530 return err;
531}
532
533static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
534 unsigned int cmd, unsigned long arg)
535{
536 int ret = -EINVAL;
537 if (cmd == MMC_IOC_CMD)
538 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
539 return ret;
540}
541
542#ifdef CONFIG_COMPAT
543static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
544 unsigned int cmd, unsigned long arg)
545{
546 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
547}
548#endif
549
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700550static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500551 .open = mmc_blk_open,
552 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800553 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400555 .ioctl = mmc_blk_ioctl,
556#ifdef CONFIG_COMPAT
557 .compat_ioctl = mmc_blk_compat_ioctl,
558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Andrei Warkentin371a6892011-04-11 18:10:25 -0500561static inline int mmc_blk_part_switch(struct mmc_card *card,
562 struct mmc_blk_data *md)
563{
564 int ret;
565 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300566
Andrei Warkentin371a6892011-04-11 18:10:25 -0500567 if (main_md->part_curr == md->part_type)
568 return 0;
569
570 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300571 u8 part_config = card->ext_csd.part_config;
572
573 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
574 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500575
576 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300577 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500578 card->ext_csd.part_time);
579 if (ret)
580 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300581
582 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300583 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500584
585 main_md->part_curr = md->part_type;
586 return 0;
587}
588
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700589static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
590{
591 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100592 u32 result;
593 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700594
Venkatraman Sad5fd972011-08-25 00:30:50 +0530595 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400596 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400597 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700598
599 struct scatterlist sg;
600
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700601 cmd.opcode = MMC_APP_CMD;
602 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700603 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700604
605 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700606 if (err)
607 return (u32)-1;
608 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700609 return (u32)-1;
610
611 memset(&cmd, 0, sizeof(struct mmc_command));
612
613 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
614 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700615 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700616
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700617 data.blksz = 4;
618 data.blocks = 1;
619 data.flags = MMC_DATA_READ;
620 data.sg = &sg;
621 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530622 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700623
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700624 mrq.cmd = &cmd;
625 mrq.data = &data;
626
Ben Dooks051913d2009-06-08 23:33:57 +0100627 blocks = kmalloc(4, GFP_KERNEL);
628 if (!blocks)
629 return (u32)-1;
630
631 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700632
633 mmc_wait_for_req(card->host, &mrq);
634
Ben Dooks051913d2009-06-08 23:33:57 +0100635 result = ntohl(*blocks);
636 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700637
Ben Dooks051913d2009-06-08 23:33:57 +0100638 if (cmd.error || data.error)
639 result = (u32)-1;
640
641 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700642}
643
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100644static int send_stop(struct mmc_card *card, u32 *status)
645{
646 struct mmc_command cmd = {0};
647 int err;
648
649 cmd.opcode = MMC_STOP_TRANSMISSION;
650 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
651 err = mmc_wait_for_cmd(card->host, &cmd, 5);
652 if (err == 0)
653 *status = cmd.resp[0];
654 return err;
655}
656
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100657static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300658{
Chris Ball1278dba2011-04-13 23:40:30 -0400659 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300660 int err;
661
Adrian Hunter504f1912008-10-16 12:55:25 +0300662 cmd.opcode = MMC_SEND_STATUS;
663 if (!mmc_host_is_spi(card->host))
664 cmd.arg = card->rca << 16;
665 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100666 err = mmc_wait_for_cmd(card->host, &cmd, retries);
667 if (err == 0)
668 *status = cmd.resp[0];
669 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300670}
671
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530672#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100673#define ERR_RETRY 2
674#define ERR_ABORT 1
675#define ERR_CONTINUE 0
676
677static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
678 bool status_valid, u32 status)
679{
680 switch (error) {
681 case -EILSEQ:
682 /* response crc error, retry the r/w cmd */
683 pr_err("%s: %s sending %s command, card status %#x\n",
684 req->rq_disk->disk_name, "response CRC error",
685 name, status);
686 return ERR_RETRY;
687
688 case -ETIMEDOUT:
689 pr_err("%s: %s sending %s command, card status %#x\n",
690 req->rq_disk->disk_name, "timed out", name, status);
691
692 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700693 if (!status_valid) {
694 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100695 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700696 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100697 /*
698 * If it was a r/w cmd crc error, or illegal command
699 * (eg, issued in wrong state) then retry - we should
700 * have corrected the state problem above.
701 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700702 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
703 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100704 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700705 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100706
707 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700708 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100709 return ERR_ABORT;
710
711 default:
712 /* We don't understand the error code the driver gave us */
713 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
714 req->rq_disk->disk_name, error, status);
715 return ERR_ABORT;
716 }
717}
718
719/*
720 * Initial r/w and stop cmd error recovery.
721 * We don't know whether the card received the r/w cmd or not, so try to
722 * restore things back to a sane state. Essentially, we do this as follows:
723 * - Obtain card status. If the first attempt to obtain card status fails,
724 * the status word will reflect the failed status cmd, not the failed
725 * r/w cmd. If we fail to obtain card status, it suggests we can no
726 * longer communicate with the card.
727 * - Check the card state. If the card received the cmd but there was a
728 * transient problem with the response, it might still be in a data transfer
729 * mode. Try to send it a stop command. If this fails, we can't recover.
730 * - If the r/w cmd failed due to a response CRC error, it was probably
731 * transient, so retry the cmd.
732 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
733 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
734 * illegal cmd, retry.
735 * Otherwise we don't understand what happened, so abort.
736 */
737static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300738 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100739{
740 bool prev_cmd_status_valid = true;
741 u32 status, stop_status = 0;
742 int err, retry;
743
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530744 if (mmc_card_removed(card))
745 return ERR_NOMEDIUM;
746
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100747 /*
748 * Try to get card status which indicates both the card state
749 * and why there was no response. If the first attempt fails,
750 * we can't be sure the returned status is for the r/w command.
751 */
752 for (retry = 2; retry >= 0; retry--) {
753 err = get_card_status(card, &status, 0);
754 if (!err)
755 break;
756
757 prev_cmd_status_valid = false;
758 pr_err("%s: error %d sending status command, %sing\n",
759 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
760 }
761
762 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530763 if (err) {
764 /* Check if the card is removed */
765 if (mmc_detect_card_removed(card->host))
766 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100767 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530768 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100769
Adrian Hunter67716322011-08-29 16:42:15 +0300770 /* Flag ECC errors */
771 if ((status & R1_CARD_ECC_FAILED) ||
772 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
773 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
774 *ecc_err = 1;
775
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100776 /*
777 * Check the current card state. If it is in some data transfer
778 * mode, tell it to stop (and hopefully transition back to TRAN.)
779 */
780 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
781 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
782 err = send_stop(card, &stop_status);
783 if (err)
784 pr_err("%s: error %d sending stop command\n",
785 req->rq_disk->disk_name, err);
786
787 /*
788 * If the stop cmd also timed out, the card is probably
789 * not present, so abort. Other errors are bad news too.
790 */
791 if (err)
792 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300793 if (stop_status & R1_CARD_ECC_FAILED)
794 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100795 }
796
797 /* Check for set block count errors */
798 if (brq->sbc.error)
799 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
800 prev_cmd_status_valid, status);
801
802 /* Check for r/w command errors */
803 if (brq->cmd.error)
804 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
805 prev_cmd_status_valid, status);
806
Adrian Hunter67716322011-08-29 16:42:15 +0300807 /* Data errors */
808 if (!brq->stop.error)
809 return ERR_CONTINUE;
810
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100811 /* Now for stop errors. These aren't fatal to the transfer. */
812 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
813 req->rq_disk->disk_name, brq->stop.error,
814 brq->cmd.resp[0], status);
815
816 /*
817 * Subsitute in our own stop status as this will give the error
818 * state which happened during the execution of the r/w command.
819 */
820 if (stop_status) {
821 brq->stop.resp[0] = stop_status;
822 brq->stop.error = 0;
823 }
824 return ERR_CONTINUE;
825}
826
Adrian Hunter67716322011-08-29 16:42:15 +0300827static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
828 int type)
829{
830 int err;
831
832 if (md->reset_done & type)
833 return -EEXIST;
834
835 md->reset_done |= type;
836 err = mmc_hw_reset(host);
837 /* Ensure we switch back to the correct partition */
838 if (err != -EOPNOTSUPP) {
839 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
840 int part_err;
841
842 main_md->part_curr = main_md->part_type;
843 part_err = mmc_blk_part_switch(host->card, md);
844 if (part_err) {
845 /*
846 * We have failed to get back into the correct
847 * partition, so we need to abort the whole request.
848 */
849 return -ENODEV;
850 }
851 }
852 return err;
853}
854
855static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
856{
857 md->reset_done &= ~type;
858}
859
Adrian Hunterbd788c92010-08-11 14:17:47 -0700860static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
861{
862 struct mmc_blk_data *md = mq->data;
863 struct mmc_card *card = md->queue.card;
864 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300865 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700866
Adrian Hunterbd788c92010-08-11 14:17:47 -0700867 if (!mmc_can_erase(card)) {
868 err = -EOPNOTSUPP;
869 goto out;
870 }
871
872 from = blk_rq_pos(req);
873 nr = blk_rq_sectors(req);
874
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900875 if (mmc_can_discard(card))
876 arg = MMC_DISCARD_ARG;
877 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700878 arg = MMC_TRIM_ARG;
879 else
880 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300881retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500882 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
883 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
884 INAND_CMD38_ARG_EXT_CSD,
885 arg == MMC_TRIM_ARG ?
886 INAND_CMD38_ARG_TRIM :
887 INAND_CMD38_ARG_ERASE,
888 0);
889 if (err)
890 goto out;
891 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700892 err = mmc_erase(card, from, nr, arg);
893out:
Adrian Hunter67716322011-08-29 16:42:15 +0300894 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
895 goto retry;
896 if (!err)
897 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530898 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700899
Adrian Hunterbd788c92010-08-11 14:17:47 -0700900 return err ? 0 : 1;
901}
902
Adrian Hunter49804542010-08-11 14:17:50 -0700903static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
904 struct request *req)
905{
906 struct mmc_blk_data *md = mq->data;
907 struct mmc_card *card = md->queue.card;
908 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300909 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700910
Maya Erez463bb952012-05-24 23:46:29 +0300911 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700912 err = -EOPNOTSUPP;
913 goto out;
914 }
915
916 from = blk_rq_pos(req);
917 nr = blk_rq_sectors(req);
918
919 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
920 arg = MMC_SECURE_TRIM1_ARG;
921 else
922 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300923retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500924 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
925 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
926 INAND_CMD38_ARG_EXT_CSD,
927 arg == MMC_SECURE_TRIM1_ARG ?
928 INAND_CMD38_ARG_SECTRIM1 :
929 INAND_CMD38_ARG_SECERASE,
930 0);
931 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300932 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500933 }
Adrian Hunter28302812012-04-05 14:45:48 +0300934
Adrian Hunter49804542010-08-11 14:17:50 -0700935 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300936 if (err == -EIO)
937 goto out_retry;
938 if (err)
939 goto out;
940
941 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500942 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
943 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
944 INAND_CMD38_ARG_EXT_CSD,
945 INAND_CMD38_ARG_SECTRIM2,
946 0);
947 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300948 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500949 }
Adrian Hunter28302812012-04-05 14:45:48 +0300950
Adrian Hunter49804542010-08-11 14:17:50 -0700951 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300952 if (err == -EIO)
953 goto out_retry;
954 if (err)
955 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500956 }
Adrian Hunter28302812012-04-05 14:45:48 +0300957
958 if (mmc_can_sanitize(card))
959 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
960 EXT_CSD_SANITIZE_START, 1, 0);
961out_retry:
962 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300963 goto retry;
964 if (!err)
965 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300966out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530967 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700968
Adrian Hunter49804542010-08-11 14:17:50 -0700969 return err ? 0 : 1;
970}
971
Maya Erez463bb952012-05-24 23:46:29 +0300972static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
973 struct request *req)
974{
975 struct mmc_blk_data *md = mq->data;
976 struct mmc_card *card = md->queue.card;
977 int err = 0;
978
979 BUG_ON(!card);
980 BUG_ON(!card->host);
981
982 if (!(mmc_can_sanitize(card) &&
983 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
984 pr_warning("%s: %s - SANITIZE is not supported\n",
985 mmc_hostname(card->host), __func__);
986 err = -EOPNOTSUPP;
987 goto out;
988 }
989
990 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
991 mmc_hostname(card->host), __func__);
992
993 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +0300994 EXT_CSD_SANITIZE_START, 1,
995 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +0300996
997 if (err)
998 pr_err("%s: %s - mmc_switch() with "
999 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1000 mmc_hostname(card->host), __func__, err);
1001
1002 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1003 __func__);
1004
1005out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301006 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001007
1008 return err ? 0 : 1;
1009}
1010
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001011static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1012{
1013 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001014 struct mmc_card *card = md->queue.card;
1015 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001016
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001017 ret = mmc_flush_cache(card);
1018 if (ret)
1019 ret = -EIO;
1020
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301021 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001022
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001023 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001024}
1025
1026/*
1027 * Reformat current write as a reliable write, supporting
1028 * both legacy and the enhanced reliable write MMC cards.
1029 * In each transfer we'll handle only as much as a single
1030 * reliable write can handle, thus finish the request in
1031 * partial completions.
1032 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001033static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1034 struct mmc_card *card,
1035 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001036{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001037 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1038 /* Legacy mode imposes restrictions on transfers. */
1039 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1040 brq->data.blocks = 1;
1041
1042 if (brq->data.blocks > card->ext_csd.rel_sectors)
1043 brq->data.blocks = card->ext_csd.rel_sectors;
1044 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1045 brq->data.blocks = 1;
1046 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001047}
1048
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001049#define CMD_ERRORS \
1050 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1051 R1_ADDRESS_ERROR | /* Misaligned address */ \
1052 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1053 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1054 R1_CC_ERROR | /* Card controller error */ \
1055 R1_ERROR) /* General/unknown error */
1056
Per Forlinee8a43a2011-07-01 18:55:33 +02001057static int mmc_blk_err_check(struct mmc_card *card,
1058 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001059{
Per Forlinee8a43a2011-07-01 18:55:33 +02001060 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1061 mmc_active);
1062 struct mmc_blk_request *brq = &mq_mrq->brq;
1063 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001064 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001065
1066 /*
1067 * sbc.error indicates a problem with the set block count
1068 * command. No data will have been transferred.
1069 *
1070 * cmd.error indicates a problem with the r/w command. No
1071 * data will have been transferred.
1072 *
1073 * stop.error indicates a problem with the stop command. Data
1074 * may have been transferred, or may still be transferring.
1075 */
Adrian Hunter67716322011-08-29 16:42:15 +03001076 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1077 brq->data.error) {
1078 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001079 case ERR_RETRY:
1080 return MMC_BLK_RETRY;
1081 case ERR_ABORT:
1082 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301083 case ERR_NOMEDIUM:
1084 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001085 case ERR_CONTINUE:
1086 break;
1087 }
1088 }
1089
1090 /*
1091 * Check for errors relating to the execution of the
1092 * initial command - such as address errors. No data
1093 * has been transferred.
1094 */
1095 if (brq->cmd.resp[0] & CMD_ERRORS) {
1096 pr_err("%s: r/w command failed, status = %#x\n",
1097 req->rq_disk->disk_name, brq->cmd.resp[0]);
1098 return MMC_BLK_ABORT;
1099 }
1100
1101 /*
1102 * Everything else is either success, or a data error of some
1103 * kind. If it was a write, we may have transitioned to
1104 * program mode, which we have to wait for it to complete.
1105 */
1106 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1107 u32 status;
1108 do {
1109 int err = get_card_status(card, &status, 5);
1110 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301111 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001112 req->rq_disk->disk_name, err);
1113 return MMC_BLK_CMD_ERR;
1114 }
1115 /*
1116 * Some cards mishandle the status bits,
1117 * so make sure to check both the busy
1118 * indication and the card state.
1119 */
1120 } while (!(status & R1_READY_FOR_DATA) ||
1121 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1122 }
1123
1124 if (brq->data.error) {
1125 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1126 req->rq_disk->disk_name, brq->data.error,
1127 (unsigned)blk_rq_pos(req),
1128 (unsigned)blk_rq_sectors(req),
1129 brq->cmd.resp[0], brq->stop.resp[0]);
1130
1131 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001132 if (ecc_err)
1133 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001134 return MMC_BLK_DATA_ERR;
1135 } else {
1136 return MMC_BLK_CMD_ERR;
1137 }
1138 }
1139
Adrian Hunter67716322011-08-29 16:42:15 +03001140 if (!brq->data.bytes_xfered)
1141 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001142
Seungwon Jeon968c7742012-05-31 11:54:47 +03001143 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1144 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1145 return MMC_BLK_PARTIAL;
1146 else
1147 return MMC_BLK_SUCCESS;
1148 }
1149
Adrian Hunter67716322011-08-29 16:42:15 +03001150 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1151 return MMC_BLK_PARTIAL;
1152
1153 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001154}
1155
Seungwon Jeon968c7742012-05-31 11:54:47 +03001156static int mmc_blk_packed_err_check(struct mmc_card *card,
1157 struct mmc_async_req *areq)
1158{
1159 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1160 mmc_active);
1161 struct request *req = mq_rq->req;
1162 int err, check, status;
1163 u8 ext_csd[512];
1164
1165 check = mmc_blk_err_check(card, areq);
1166 err = get_card_status(card, &status, 0);
1167 if (err) {
1168 pr_err("%s: error %d sending status command\n",
1169 req->rq_disk->disk_name, err);
1170 return MMC_BLK_ABORT;
1171 }
1172
1173 if (status & R1_EXP_EVENT) {
1174 err = mmc_send_ext_csd(card, ext_csd);
1175 if (err) {
1176 pr_err("%s: error %d sending ext_csd\n",
1177 req->rq_disk->disk_name, err);
1178 return MMC_BLK_ABORT;
1179 }
1180
1181 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1182 EXT_CSD_PACKED_FAILURE) &&
1183 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1184 EXT_CSD_PACKED_GENERIC_ERROR)) {
1185 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1186 EXT_CSD_PACKED_INDEXED_ERROR) {
1187 mq_rq->packed_fail_idx =
1188 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1189 return MMC_BLK_PARTIAL;
1190 }
1191 }
1192 }
1193
1194 return check;
1195}
1196
Per Forlin54d49d772011-07-01 18:55:29 +02001197static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1198 struct mmc_card *card,
1199 int disable_multi,
1200 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Per Forlin54d49d772011-07-01 18:55:29 +02001202 u32 readcmd, writecmd;
1203 struct mmc_blk_request *brq = &mqrq->brq;
1204 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301206 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001208 /*
1209 * Reliable writes are used to implement Forced Unit Access and
1210 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001211 *
1212 * XXX: this really needs a good explanation of why REQ_META
1213 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001214 */
1215 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1216 (req->cmd_flags & REQ_META)) &&
1217 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001218 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001219
Per Forlin54d49d772011-07-01 18:55:29 +02001220 memset(brq, 0, sizeof(struct mmc_blk_request));
1221 brq->mrq.cmd = &brq->cmd;
1222 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Per Forlin54d49d772011-07-01 18:55:29 +02001224 brq->cmd.arg = blk_rq_pos(req);
1225 if (!mmc_card_blockaddr(card))
1226 brq->cmd.arg <<= 9;
1227 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1228 brq->data.blksz = 512;
1229 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1230 brq->stop.arg = 0;
1231 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1232 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Per Forlin54d49d772011-07-01 18:55:29 +02001234 /*
1235 * The block layer doesn't support all sector count
1236 * restrictions, so we need to be prepared for too big
1237 * requests.
1238 */
1239 if (brq->data.blocks > card->host->max_blk_count)
1240 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001242 if (brq->data.blocks > 1) {
1243 /*
1244 * After a read error, we redo the request one sector
1245 * at a time in order to accurately determine which
1246 * sectors can be read successfully.
1247 */
1248 if (disable_multi)
1249 brq->data.blocks = 1;
1250
1251 /* Some controllers can't do multiblock reads due to hw bugs */
1252 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1253 rq_data_dir(req) == READ)
1254 brq->data.blocks = 1;
1255 }
Per Forlin54d49d772011-07-01 18:55:29 +02001256
1257 if (brq->data.blocks > 1 || do_rel_wr) {
1258 /* SPI multiblock writes terminate using a special
1259 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001260 */
Per Forlin54d49d772011-07-01 18:55:29 +02001261 if (!mmc_host_is_spi(card->host) ||
1262 rq_data_dir(req) == READ)
1263 brq->mrq.stop = &brq->stop;
1264 readcmd = MMC_READ_MULTIPLE_BLOCK;
1265 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1266 } else {
1267 brq->mrq.stop = NULL;
1268 readcmd = MMC_READ_SINGLE_BLOCK;
1269 writecmd = MMC_WRITE_BLOCK;
1270 }
1271 if (rq_data_dir(req) == READ) {
1272 brq->cmd.opcode = readcmd;
1273 brq->data.flags |= MMC_DATA_READ;
1274 } else {
1275 brq->cmd.opcode = writecmd;
1276 brq->data.flags |= MMC_DATA_WRITE;
1277 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001278
Per Forlin54d49d772011-07-01 18:55:29 +02001279 if (do_rel_wr)
1280 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001281
Per Forlin54d49d772011-07-01 18:55:29 +02001282 /*
Saugata Das42659002011-12-21 13:09:17 +05301283 * Data tag is used only during writing meta data to speed
1284 * up write and any subsequent read of this meta data
1285 */
1286 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1287 (req->cmd_flags & REQ_META) &&
1288 (rq_data_dir(req) == WRITE) &&
1289 ((brq->data.blocks * brq->data.blksz) >=
1290 card->ext_csd.data_tag_unit_size);
1291
1292 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001293 * Pre-defined multi-block transfers are preferable to
1294 * open ended-ones (and necessary for reliable writes).
1295 * However, it is not sufficient to just send CMD23,
1296 * and avoid the final CMD12, as on an error condition
1297 * CMD12 (stop) needs to be sent anyway. This, coupled
1298 * with Auto-CMD23 enhancements provided by some
1299 * hosts, means that the complexity of dealing
1300 * with this is best left to the host. If CMD23 is
1301 * supported by card and host, we'll fill sbc in and let
1302 * the host deal with handling it correctly. This means
1303 * that for hosts that don't expose MMC_CAP_CMD23, no
1304 * change of behavior will be observed.
1305 *
1306 * N.B: Some MMC cards experience perf degradation.
1307 * We'll avoid using CMD23-bounded multiblock writes for
1308 * these, while retaining features like reliable writes.
1309 */
Saugata Das42659002011-12-21 13:09:17 +05301310 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1311 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1312 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001313 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1314 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301315 (do_rel_wr ? (1 << 31) : 0) |
1316 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001317 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1318 brq->mrq.sbc = &brq->sbc;
1319 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001320
Per Forlin54d49d772011-07-01 18:55:29 +02001321 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001322
Per Forlin54d49d772011-07-01 18:55:29 +02001323 brq->data.sg = mqrq->sg;
1324 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001325
Per Forlin54d49d772011-07-01 18:55:29 +02001326 /*
1327 * Adjust the sg list so it is the same size as the
1328 * request.
1329 */
1330 if (brq->data.blocks != blk_rq_sectors(req)) {
1331 int i, data_size = brq->data.blocks << 9;
1332 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001333
Per Forlin54d49d772011-07-01 18:55:29 +02001334 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1335 data_size -= sg->length;
1336 if (data_size <= 0) {
1337 sg->length += data_size;
1338 i++;
1339 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001340 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001341 }
Per Forlin54d49d772011-07-01 18:55:29 +02001342 brq->data.sg_len = i;
1343 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001344
Per Forlinee8a43a2011-07-01 18:55:33 +02001345 mqrq->mmc_active.mrq = &brq->mrq;
1346 mqrq->mmc_active.err_check = mmc_blk_err_check;
1347
Per Forlin54d49d772011-07-01 18:55:29 +02001348 mmc_queue_bounce_pre(mqrq);
1349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Maya Erez63c61d62012-05-31 21:00:18 +03001351static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1352 struct request *req)
1353{
1354 struct mmc_host *host = mq->card->host;
1355 int data_dir;
1356
1357 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1358 return;
1359
1360 /*
1361 * In case the packing control is not supported by the host, it should
1362 * not have an effect on the write packing. Therefore we have to enable
1363 * the write packing
1364 */
1365 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1366 mq->wr_packing_enabled = true;
1367 return;
1368 }
1369
1370 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1371 if (mq->num_of_potential_packed_wr_reqs >
1372 mq->num_wr_reqs_to_start_packing)
1373 mq->wr_packing_enabled = true;
Maya Ereze2ecb582012-06-14 10:29:36 +03001374 mq->num_of_potential_packed_wr_reqs = 0;
Maya Erez63c61d62012-05-31 21:00:18 +03001375 return;
1376 }
1377
1378 data_dir = rq_data_dir(req);
1379
1380 if (data_dir == READ) {
1381 mq->num_of_potential_packed_wr_reqs = 0;
1382 mq->wr_packing_enabled = false;
1383 return;
1384 } else if (data_dir == WRITE) {
1385 mq->num_of_potential_packed_wr_reqs++;
1386 }
1387
1388 if (mq->num_of_potential_packed_wr_reqs >
1389 mq->num_wr_reqs_to_start_packing)
1390 mq->wr_packing_enabled = true;
1391
1392}
1393
Maya Ereze544d7002012-06-04 06:09:47 +03001394struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1395{
1396 if (!card)
1397 return NULL;
1398
1399 return &card->wr_pack_stats;
1400}
1401EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1402
1403void mmc_blk_init_packed_statistics(struct mmc_card *card)
1404{
1405 int max_num_of_packed_reqs = 0;
1406
1407 if (!card || !card->wr_pack_stats.packing_events)
1408 return;
1409
1410 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1411
1412 spin_lock(&card->wr_pack_stats.lock);
1413 memset(card->wr_pack_stats.packing_events, 0,
1414 (max_num_of_packed_reqs + 1) *
1415 sizeof(*card->wr_pack_stats.packing_events));
1416 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1417 sizeof(card->wr_pack_stats.pack_stop_reason));
1418 card->wr_pack_stats.enabled = true;
1419 spin_unlock(&card->wr_pack_stats.lock);
1420}
1421EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1422
Maya Erez9b54d882012-07-05 15:44:17 +03001423void print_mmc_packing_stats(struct mmc_card *card)
1424{
1425 int i;
1426 int max_num_of_packed_reqs = 0;
1427
1428 if ((!card) || (!card->wr_pack_stats.packing_events))
1429 return;
1430
1431 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1432
1433 spin_lock(&card->wr_pack_stats.lock);
1434
1435 pr_info("%s: write packing statistics:\n",
1436 mmc_hostname(card->host));
1437
1438 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1439 if (card->wr_pack_stats.packing_events[i] != 0)
1440 pr_info("%s: Packed %d reqs - %d times\n",
1441 mmc_hostname(card->host), i,
1442 card->wr_pack_stats.packing_events[i]);
1443 }
1444
1445 pr_info("%s: stopped packing due to the following reasons:\n",
1446 mmc_hostname(card->host));
1447
1448 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1449 pr_info("%s: %d times: exceedmax num of segments\n",
1450 mmc_hostname(card->host),
1451 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1452 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1453 pr_info("%s: %d times: exceeding the max num of sectors\n",
1454 mmc_hostname(card->host),
1455 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1456 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1457 pr_info("%s: %d times: wrong data direction\n",
1458 mmc_hostname(card->host),
1459 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1460 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1461 pr_info("%s: %d times: flush or discard\n",
1462 mmc_hostname(card->host),
1463 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1464 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1465 pr_info("%s: %d times: empty queue\n",
1466 mmc_hostname(card->host),
1467 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1468 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1469 pr_info("%s: %d times: rel write\n",
1470 mmc_hostname(card->host),
1471 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1472 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1473 pr_info("%s: %d times: Threshold\n",
1474 mmc_hostname(card->host),
1475 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1476
1477 spin_unlock(&card->wr_pack_stats.lock);
1478}
1479EXPORT_SYMBOL(print_mmc_packing_stats);
1480
Seungwon Jeon968c7742012-05-31 11:54:47 +03001481static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1482{
1483 struct request_queue *q = mq->queue;
1484 struct mmc_card *card = mq->card;
1485 struct request *cur = req, *next = NULL;
1486 struct mmc_blk_data *md = mq->data;
1487 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1488 unsigned int req_sectors = 0, phys_segments = 0;
1489 unsigned int max_blk_count, max_phys_segs;
1490 u8 put_back = 0;
1491 u8 max_packed_rw = 0;
1492 u8 reqs = 0;
Maya Ereze544d7002012-06-04 06:09:47 +03001493 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001494
1495 mmc_blk_clear_packed(mq->mqrq_cur);
1496
1497 if (!(md->flags & MMC_BLK_CMD23) ||
1498 !card->ext_csd.packed_event_en)
1499 goto no_packed;
1500
Maya Erez63c61d62012-05-31 21:00:18 +03001501 if (!mq->wr_packing_enabled)
1502 goto no_packed;
1503
Seungwon Jeon968c7742012-05-31 11:54:47 +03001504 if ((rq_data_dir(cur) == WRITE) &&
1505 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1506 max_packed_rw = card->ext_csd.max_packed_writes;
1507
1508 if (max_packed_rw == 0)
1509 goto no_packed;
1510
1511 if (mmc_req_rel_wr(cur) &&
1512 (md->flags & MMC_BLK_REL_WR) &&
1513 !en_rel_wr) {
1514 goto no_packed;
1515 }
1516
1517 max_blk_count = min(card->host->max_blk_count,
1518 card->host->max_req_size >> 9);
1519 if (unlikely(max_blk_count > 0xffff))
1520 max_blk_count = 0xffff;
1521
1522 max_phys_segs = queue_max_segments(q);
1523 req_sectors += blk_rq_sectors(cur);
1524 phys_segments += cur->nr_phys_segments;
1525
1526 if (rq_data_dir(cur) == WRITE) {
1527 req_sectors++;
1528 phys_segments++;
1529 }
1530
Maya Ereze544d7002012-06-04 06:09:47 +03001531 spin_lock(&stats->lock);
1532
Seungwon Jeon968c7742012-05-31 11:54:47 +03001533 while (reqs < max_packed_rw - 1) {
1534 spin_lock_irq(q->queue_lock);
1535 next = blk_fetch_request(q);
1536 spin_unlock_irq(q->queue_lock);
Maya Ereze544d7002012-06-04 06:09:47 +03001537 if (!next) {
1538 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001539 break;
Maya Ereze544d7002012-06-04 06:09:47 +03001540 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001541
1542 if (next->cmd_flags & REQ_DISCARD ||
1543 next->cmd_flags & REQ_FLUSH) {
Maya Ereze544d7002012-06-04 06:09:47 +03001544 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001545 put_back = 1;
1546 break;
1547 }
1548
1549 if (rq_data_dir(cur) != rq_data_dir(next)) {
Maya Ereze544d7002012-06-04 06:09:47 +03001550 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001551 put_back = 1;
1552 break;
1553 }
1554
1555 if (mmc_req_rel_wr(next) &&
1556 (md->flags & MMC_BLK_REL_WR) &&
1557 !en_rel_wr) {
Maya Ereze544d7002012-06-04 06:09:47 +03001558 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001559 put_back = 1;
1560 break;
1561 }
1562
1563 req_sectors += blk_rq_sectors(next);
1564 if (req_sectors > max_blk_count) {
Maya Ereze544d7002012-06-04 06:09:47 +03001565 if (stats->enabled)
1566 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001567 put_back = 1;
1568 break;
1569 }
1570
1571 phys_segments += next->nr_phys_segments;
1572 if (phys_segments > max_phys_segs) {
Maya Ereze544d7002012-06-04 06:09:47 +03001573 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001574 put_back = 1;
1575 break;
1576 }
1577
Maya Erez63c61d62012-05-31 21:00:18 +03001578 if (rq_data_dir(next) == WRITE)
1579 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001580 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1581 cur = next;
1582 reqs++;
1583 }
1584
1585 if (put_back) {
1586 spin_lock_irq(q->queue_lock);
1587 blk_requeue_request(q, next);
1588 spin_unlock_irq(q->queue_lock);
1589 }
1590
Maya Ereze544d7002012-06-04 06:09:47 +03001591 if (stats->enabled) {
1592 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1593 stats->packing_events[reqs + 1]++;
1594 if (reqs + 1 == max_packed_rw)
1595 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1596 }
1597
1598 spin_unlock(&stats->lock);
1599
Seungwon Jeon968c7742012-05-31 11:54:47 +03001600 if (reqs > 0) {
1601 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1602 mq->mqrq_cur->packed_num = ++reqs;
1603 return reqs;
1604 }
1605
1606no_packed:
1607 mmc_blk_clear_packed(mq->mqrq_cur);
1608 return 0;
1609}
1610
1611static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1612 struct mmc_card *card,
1613 struct mmc_queue *mq)
1614{
1615 struct mmc_blk_request *brq = &mqrq->brq;
1616 struct request *req = mqrq->req;
1617 struct request *prq;
1618 struct mmc_blk_data *md = mq->data;
1619 bool do_rel_wr;
1620 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1621 u8 i = 1;
1622
1623 mqrq->packed_cmd = MMC_PACKED_WRITE;
1624 mqrq->packed_blocks = 0;
1625 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1626
1627 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1628 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1629 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1630
1631 /*
1632 * Argument for each entry of packed group
1633 */
1634 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1635 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1636 /* Argument of CMD23*/
1637 packed_cmd_hdr[(i * 2)] =
1638 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1639 blk_rq_sectors(prq);
1640 /* Argument of CMD18 or CMD25 */
1641 packed_cmd_hdr[((i * 2)) + 1] =
1642 mmc_card_blockaddr(card) ?
1643 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1644 mqrq->packed_blocks += blk_rq_sectors(prq);
1645 i++;
1646 }
1647
1648 memset(brq, 0, sizeof(struct mmc_blk_request));
1649 brq->mrq.cmd = &brq->cmd;
1650 brq->mrq.data = &brq->data;
1651 brq->mrq.sbc = &brq->sbc;
1652 brq->mrq.stop = &brq->stop;
1653
1654 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1655 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1656 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1657
1658 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1659 brq->cmd.arg = blk_rq_pos(req);
1660 if (!mmc_card_blockaddr(card))
1661 brq->cmd.arg <<= 9;
1662 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1663
1664 brq->data.blksz = 512;
1665 brq->data.blocks = mqrq->packed_blocks + 1;
1666 brq->data.flags |= MMC_DATA_WRITE;
1667
1668 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1669 brq->stop.arg = 0;
1670 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1671
1672 mmc_set_data_timeout(&brq->data, card);
1673
1674 brq->data.sg = mqrq->sg;
1675 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1676
1677 mqrq->mmc_active.mrq = &brq->mrq;
Maya Erez9b54d882012-07-05 15:44:17 +03001678
1679 /*
1680 * This is intended for packed commands tests usage - in case these
1681 * functions are not in use the respective pointers are NULL
1682 */
1683 if (mq->err_check_fn)
1684 mqrq->mmc_active.err_check = mq->err_check_fn;
1685 else
1686 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1687
1688 if (mq->packed_test_fn)
1689 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001690
1691 mmc_queue_bounce_pre(mqrq);
1692}
1693
Adrian Hunter67716322011-08-29 16:42:15 +03001694static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1695 struct mmc_blk_request *brq, struct request *req,
1696 int ret)
1697{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001698 struct mmc_queue_req *mq_rq;
1699 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1700
Adrian Hunter67716322011-08-29 16:42:15 +03001701 /*
1702 * If this is an SD card and we're writing, we can first
1703 * mark the known good sectors as ok.
1704 *
1705 * If the card is not SD, we can still ok written sectors
1706 * as reported by the controller (which might be less than
1707 * the real number of written sectors, but never more).
1708 */
1709 if (mmc_card_sd(card)) {
1710 u32 blocks;
1711
1712 blocks = mmc_sd_num_wr_blocks(card);
1713 if (blocks != (u32)-1) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301714 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001715 }
1716 } else {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001717 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301718 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001719 }
Adrian Hunter67716322011-08-29 16:42:15 +03001720 }
1721 return ret;
1722}
1723
Seungwon Jeon968c7742012-05-31 11:54:47 +03001724static int mmc_blk_end_packed_req(struct mmc_queue *mq,
1725 struct mmc_queue_req *mq_rq)
1726{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001727 struct request *prq;
1728 int idx = mq_rq->packed_fail_idx, i = 0;
1729 int ret = 0;
1730
1731 while (!list_empty(&mq_rq->packed_list)) {
1732 prq = list_entry_rq(mq_rq->packed_list.next);
1733 if (idx == i) {
1734 /* retry from error index */
1735 mq_rq->packed_num -= idx;
1736 mq_rq->req = prq;
1737 ret = 1;
1738
1739 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1740 list_del_init(&prq->queuelist);
1741 mmc_blk_clear_packed(mq_rq);
1742 }
1743 return ret;
1744 }
1745 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301746 blk_end_request(prq, 0, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001747 i++;
1748 }
1749
1750 mmc_blk_clear_packed(mq_rq);
1751 return ret;
1752}
1753
Per Forlinee8a43a2011-07-01 18:55:33 +02001754static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001755{
1756 struct mmc_blk_data *md = mq->data;
1757 struct mmc_card *card = md->queue.card;
1758 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001759 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001760 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001761 struct mmc_queue_req *mq_rq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001762 struct request *req, *prq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001763 struct mmc_async_req *areq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001764 const u8 packed_num = 2;
1765 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001766
1767 if (!rqc && !mq->mqrq_prev->req)
1768 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001769
Seungwon Jeon968c7742012-05-31 11:54:47 +03001770 if (rqc)
1771 reqs = mmc_blk_prep_packed_list(mq, rqc);
1772
Per Forlin54d49d772011-07-01 18:55:29 +02001773 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001774 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001775 if (reqs >= packed_num)
1776 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1777 card, mq);
1778 else
1779 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001780 areq = &mq->mqrq_cur->mmc_active;
1781 } else
1782 areq = NULL;
1783 areq = mmc_start_req(card->host, areq, (int *) &status);
1784 if (!areq)
1785 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001786
Per Forlinee8a43a2011-07-01 18:55:33 +02001787 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1788 brq = &mq_rq->brq;
1789 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001790 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001791 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001792
Jaehoon Chungf886c802012-05-28 10:33:35 +03001793 /*
1794 * Check BKOPS urgency from each R1 response
1795 */
1796 if (mmc_card_mmc(card) &&
1797 (brq->cmd.resp[0] & R1_EXCEPTION_EVENT))
1798 mmc_card_set_check_bkops(card);
1799
Per Forlind78d4a82011-07-01 18:55:30 +02001800 switch (status) {
1801 case MMC_BLK_SUCCESS:
1802 case MMC_BLK_PARTIAL:
1803 /*
1804 * A block was successfully transferred.
1805 */
Adrian Hunter67716322011-08-29 16:42:15 +03001806 mmc_blk_reset_success(md, type);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001807
1808 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1809 ret = mmc_blk_end_packed_req(mq, mq_rq);
1810 break;
1811 } else {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301812 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001813 brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001814 }
1815
Adrian Hunter67716322011-08-29 16:42:15 +03001816 /*
1817 * If the blk_end_request function returns non-zero even
1818 * though all data has been transferred and no errors
1819 * were returned by the host controller, it's a bug.
1820 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001821 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301822 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001823 __func__, blk_rq_bytes(req),
1824 brq->data.bytes_xfered);
1825 rqc = NULL;
1826 goto cmd_abort;
1827 }
Per Forlind78d4a82011-07-01 18:55:30 +02001828 break;
1829 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001830 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1831 if (!mmc_blk_reset(md, card->host, type))
1832 break;
1833 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001834 case MMC_BLK_RETRY:
1835 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001836 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001837 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001838 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001839 if (!mmc_blk_reset(md, card->host, type))
1840 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001841 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001842 case MMC_BLK_DATA_ERR: {
1843 int err;
1844
1845 err = mmc_blk_reset(md, card->host, type);
1846 if (!err)
1847 break;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001848 if (err == -ENODEV ||
1849 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001850 goto cmd_abort;
1851 /* Fall through */
1852 }
1853 case MMC_BLK_ECC_ERR:
1854 if (brq->data.blocks > 1) {
1855 /* Redo read one sector at a time */
1856 pr_warning("%s: retrying using single block read\n",
1857 req->rq_disk->disk_name);
1858 disable_multi = 1;
1859 break;
1860 }
Per Forlind78d4a82011-07-01 18:55:30 +02001861 /*
1862 * After an error, we redo I/O one sector at a
1863 * time, so we only reach here after trying to
1864 * read a single sector.
1865 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301866 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001867 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001868 if (!ret)
1869 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001870 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301871 case MMC_BLK_NOMEDIUM:
1872 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001873 }
1874
Per Forlinee8a43a2011-07-01 18:55:33 +02001875 if (ret) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001876 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1877 /*
1878 * In case of a incomplete request
1879 * prepare it again and resend.
1880 */
1881 mmc_blk_rw_rq_prep(mq_rq, card,
1882 disable_multi, mq);
1883 mmc_start_req(card->host,
1884 &mq_rq->mmc_active, NULL);
1885 } else {
1886 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1887 mmc_start_req(card->host,
1888 &mq_rq->mmc_active, NULL);
1889 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } while (ret);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return 1;
1894
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001895 cmd_abort:
Seungwon Jeon968c7742012-05-31 11:54:47 +03001896 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001897 if (mmc_card_removed(card))
1898 req->cmd_flags |= REQ_QUIET;
1899 while (ret)
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301900 ret = blk_end_request(req, -EIO,
Seungwon Jeon968c7742012-05-31 11:54:47 +03001901 blk_rq_cur_bytes(req));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001902 } else {
1903 while (!list_empty(&mq_rq->packed_list)) {
1904 prq = list_entry_rq(mq_rq->packed_list.next);
1905 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301906 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001907 }
1908 mmc_blk_clear_packed(mq_rq);
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Per Forlinee8a43a2011-07-01 18:55:33 +02001911 start_new_req:
1912 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001913 /*
1914 * If current request is packed, it needs to put back.
1915 */
1916 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE) {
1917 while (!list_empty(&mq->mqrq_cur->packed_list)) {
1918 prq = list_entry_rq(
1919 mq->mqrq_cur->packed_list.prev);
1920 if (prq->queuelist.prev !=
1921 &mq->mqrq_cur->packed_list) {
1922 list_del_init(&prq->queuelist);
1923 spin_lock_irq(mq->queue->queue_lock);
1924 blk_requeue_request(mq->queue, prq);
1925 spin_unlock_irq(mq->queue->queue_lock);
1926 } else {
1927 list_del_init(&prq->queuelist);
1928 }
1929 }
1930 mmc_blk_clear_packed(mq->mqrq_cur);
1931 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001932 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1933 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1934 }
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938
Adrian Hunterbd788c92010-08-11 14:17:47 -07001939static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1940{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001941 int ret;
1942 struct mmc_blk_data *md = mq->data;
1943 struct mmc_card *card = md->queue.card;
1944
San Mehat148c57a2009-07-30 08:21:19 -07001945#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1946 if (mmc_bus_needs_resume(card->host)) {
1947 mmc_resume_bus(card->host);
1948 mmc_blk_set_blksize(md, card);
1949 }
1950#endif
1951
Per Forlinee8a43a2011-07-01 18:55:33 +02001952 if (req && !mq->mqrq_prev->req)
1953 /* claim host only for the first request */
1954 mmc_claim_host(card->host);
1955
Andrei Warkentin371a6892011-04-11 18:10:25 -05001956 ret = mmc_blk_part_switch(card, md);
1957 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001958 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301959 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001960 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001961 ret = 0;
1962 goto out;
1963 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001964
Maya Erez63c61d62012-05-31 21:00:18 +03001965 mmc_blk_write_packing_control(mq, req);
1966
Maya Erez463bb952012-05-24 23:46:29 +03001967 if (req && req->cmd_flags & REQ_SANITIZE) {
1968 /* complete ongoing async transfer before issuing sanitize */
1969 if (card->host && card->host->areq)
1970 mmc_blk_issue_rw_rq(mq, NULL);
1971 ret = mmc_blk_issue_sanitize_rq(mq, req);
1972 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001973 /* complete ongoing async transfer before issuing discard */
1974 if (card->host->areq)
1975 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001976 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001977 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001978 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001979 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001980 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001981 /* complete ongoing async transfer before issuing flush */
1982 if (card->host->areq)
1983 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001984 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001985 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001986 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001987 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001988
Andrei Warkentin371a6892011-04-11 18:10:25 -05001989out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001990 if (!req)
1991 /* release host only when there are no more requests */
1992 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001993 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Russell Kinga6f6c962006-01-03 22:38:44 +00001996static inline int mmc_blk_readonly(struct mmc_card *card)
1997{
1998 return mmc_card_readonly(card) ||
1999 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2000}
2001
Andrei Warkentin371a6892011-04-11 18:10:25 -05002002static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2003 struct device *parent,
2004 sector_t size,
2005 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002006 const char *subname,
2007 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct mmc_blk_data *md;
2010 int devidx, ret;
2011
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002012 devidx = find_first_zero_bit(dev_use, max_devices);
2013 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return ERR_PTR(-ENOSPC);
2015 __set_bit(devidx, dev_use);
2016
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002017 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002018 if (!md) {
2019 ret = -ENOMEM;
2020 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002022
Russell Kinga6f6c962006-01-03 22:38:44 +00002023 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002024 * !subname implies we are creating main mmc_blk_data that will be
2025 * associated with mmc_card with mmc_set_drvdata. Due to device
2026 * partitions, devidx will not coincide with a per-physical card
2027 * index anymore so we keep track of a name index.
2028 */
2029 if (!subname) {
2030 md->name_idx = find_first_zero_bit(name_use, max_devices);
2031 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002032 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002033 md->name_idx = ((struct mmc_blk_data *)
2034 dev_to_disk(parent)->private_data)->name_idx;
2035
Johan Rudholmadd710e2011-12-02 08:51:06 +01002036 md->area_type = area_type;
2037
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002038 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002039 * Set the read-only status based on the supported commands
2040 * and the write protect switch.
2041 */
2042 md->read_only = mmc_blk_readonly(card);
2043
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002044 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002045 if (md->disk == NULL) {
2046 ret = -ENOMEM;
2047 goto err_kfree;
2048 }
2049
2050 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002051 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002052 md->usage = 1;
2053
Adrian Hunterd09408a2011-06-23 13:40:28 +03002054 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002055 if (ret)
2056 goto err_putdisk;
2057
Russell Kinga6f6c962006-01-03 22:38:44 +00002058 md->queue.issue_fn = mmc_blk_issue_rq;
2059 md->queue.data = md;
2060
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002061 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002062 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002063 md->disk->fops = &mmc_bdops;
2064 md->disk->private_data = md;
2065 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002066 md->disk->driverfs_dev = parent;
2067 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002068 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002069
2070 /*
2071 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2072 *
2073 * - be set for removable media with permanent block devices
2074 * - be unset for removable block devices with permanent media
2075 *
2076 * Since MMC block devices clearly fall under the second
2077 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2078 * should use the block device creation/destruction hotplug
2079 * messages to tell when the card is present.
2080 */
2081
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002082 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2083 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002084
Martin K. Petersene1defc42009-05-22 17:17:49 -04002085 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002086 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002087
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002088 if (mmc_host_cmd23(card->host)) {
2089 if (mmc_card_mmc(card) ||
2090 (mmc_card_sd(card) &&
2091 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2092 md->flags |= MMC_BLK_CMD23;
2093 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002094
2095 if (mmc_card_mmc(card) &&
2096 md->flags & MMC_BLK_CMD23 &&
2097 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2098 card->ext_csd.rel_sectors)) {
2099 md->flags |= MMC_BLK_REL_WR;
2100 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2101 }
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002104
2105 err_putdisk:
2106 put_disk(md->disk);
2107 err_kfree:
2108 kfree(md);
2109 out:
2110 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Andrei Warkentin371a6892011-04-11 18:10:25 -05002113static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2114{
2115 sector_t size;
2116 struct mmc_blk_data *md;
2117
2118 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2119 /*
2120 * The EXT_CSD sector count is in number or 512 byte
2121 * sectors.
2122 */
2123 size = card->ext_csd.sectors;
2124 } else {
2125 /*
2126 * The CSD capacity field is in units of read_blkbits.
2127 * set_capacity takes units of 512 bytes.
2128 */
2129 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2130 }
2131
Johan Rudholmadd710e2011-12-02 08:51:06 +01002132 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2133 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002134 return md;
2135}
2136
2137static int mmc_blk_alloc_part(struct mmc_card *card,
2138 struct mmc_blk_data *md,
2139 unsigned int part_type,
2140 sector_t size,
2141 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002142 const char *subname,
2143 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002144{
2145 char cap_str[10];
2146 struct mmc_blk_data *part_md;
2147
2148 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002149 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002150 if (IS_ERR(part_md))
2151 return PTR_ERR(part_md);
2152 part_md->part_type = part_type;
2153 list_add(&part_md->part, &md->part);
2154
2155 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2156 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302157 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002158 part_md->disk->disk_name, mmc_card_id(card),
2159 mmc_card_name(card), part_md->part_type, cap_str);
2160 return 0;
2161}
2162
Namjae Jeone0c368d2011-10-06 23:41:38 +09002163/* MMC Physical partitions consist of two boot partitions and
2164 * up to four general purpose partitions.
2165 * For each partition enabled in EXT_CSD a block device will be allocatedi
2166 * to provide access to the partition.
2167 */
2168
Andrei Warkentin371a6892011-04-11 18:10:25 -05002169static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2170{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002171 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002172
2173 if (!mmc_card_mmc(card))
2174 return 0;
2175
Namjae Jeone0c368d2011-10-06 23:41:38 +09002176 for (idx = 0; idx < card->nr_parts; idx++) {
2177 if (card->part[idx].size) {
2178 ret = mmc_blk_alloc_part(card, md,
2179 card->part[idx].part_cfg,
2180 card->part[idx].size >> 9,
2181 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002182 card->part[idx].name,
2183 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002184 if (ret)
2185 return ret;
2186 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002187 }
2188
2189 return ret;
2190}
2191
Andrei Warkentin371a6892011-04-11 18:10:25 -05002192static void mmc_blk_remove_req(struct mmc_blk_data *md)
2193{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002194 struct mmc_card *card;
2195
Andrei Warkentin371a6892011-04-11 18:10:25 -05002196 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002197 card = md->queue.card;
Maya Erez63c61d62012-05-31 21:00:18 +03002198 device_remove_file(disk_to_dev(md->disk),
2199 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002200 if (md->disk->flags & GENHD_FL_UP) {
2201 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002202 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2203 card->ext_csd.boot_ro_lockable)
2204 device_remove_file(disk_to_dev(md->disk),
2205 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206
2207 /* Stop new requests from getting into the queue */
2208 del_gendisk(md->disk);
2209 }
2210
2211 /* Then flush out any already in there */
2212 mmc_cleanup_queue(&md->queue);
2213 mmc_blk_put(md);
2214 }
2215}
2216
2217static void mmc_blk_remove_parts(struct mmc_card *card,
2218 struct mmc_blk_data *md)
2219{
2220 struct list_head *pos, *q;
2221 struct mmc_blk_data *part_md;
2222
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002223 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002224 list_for_each_safe(pos, q, &md->part) {
2225 part_md = list_entry(pos, struct mmc_blk_data, part);
2226 list_del(pos);
2227 mmc_blk_remove_req(part_md);
2228 }
2229}
2230
2231static int mmc_add_disk(struct mmc_blk_data *md)
2232{
2233 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002234 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235
2236 add_disk(md->disk);
2237 md->force_ro.show = force_ro_show;
2238 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302239 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240 md->force_ro.attr.name = "force_ro";
2241 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2242 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2243 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002244 goto force_ro_fail;
2245
2246 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2247 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002248 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002249
2250 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2251 mode = S_IRUGO;
2252 else
2253 mode = S_IRUGO | S_IWUSR;
2254
2255 md->power_ro_lock.show = power_ro_lock_show;
2256 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002257 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002258 md->power_ro_lock.attr.mode = mode;
2259 md->power_ro_lock.attr.name =
2260 "ro_lock_until_next_power_on";
2261 ret = device_create_file(disk_to_dev(md->disk),
2262 &md->power_ro_lock);
2263 if (ret)
2264 goto power_ro_lock_fail;
2265 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002266
Maya Erez63c61d62012-05-31 21:00:18 +03002267 md->num_wr_reqs_to_start_packing.show =
2268 num_wr_reqs_to_start_packing_show;
2269 md->num_wr_reqs_to_start_packing.store =
2270 num_wr_reqs_to_start_packing_store;
2271 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2272 md->num_wr_reqs_to_start_packing.attr.name =
2273 "num_wr_reqs_to_start_packing";
2274 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2275 ret = device_create_file(disk_to_dev(md->disk),
2276 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002277 if (ret)
2278 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002279
Johan Rudholmadd710e2011-12-02 08:51:06 +01002280 return ret;
2281
2282power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002283 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002284force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002285 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002286
2287 return ret;
2288}
2289
Chris Ballc59d4472011-11-11 22:01:43 -05002290#define CID_MANFID_SANDISK 0x2
2291#define CID_MANFID_TOSHIBA 0x11
2292#define CID_MANFID_MICRON 0x13
2293
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002294static const struct mmc_fixup blk_fixups[] =
2295{
Chris Ballc59d4472011-11-11 22:01:43 -05002296 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2297 MMC_QUIRK_INAND_CMD38),
2298 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2299 MMC_QUIRK_INAND_CMD38),
2300 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2301 MMC_QUIRK_INAND_CMD38),
2302 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2303 MMC_QUIRK_INAND_CMD38),
2304 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2305 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002306
2307 /*
2308 * Some MMC cards experience performance degradation with CMD23
2309 * instead of CMD12-bounded multiblock transfers. For now we'll
2310 * black list what's bad...
2311 * - Certain Toshiba cards.
2312 *
2313 * N.B. This doesn't affect SD cards.
2314 */
Chris Ballc59d4472011-11-11 22:01:43 -05002315 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002316 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002317 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002318 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002319 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002320 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002321
2322 /*
2323 * Some Micron MMC cards needs longer data read timeout than
2324 * indicated in CSD.
2325 */
Chris Ballc59d4472011-11-11 22:01:43 -05002326 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002327 MMC_QUIRK_LONG_READ_TIME),
2328
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302329 /* Some INAND MCP devices advertise incorrect timeout values */
2330 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2331 MMC_QUIRK_INAND_DATA_TIMEOUT),
2332
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002333 END_FIXUP
2334};
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336static int mmc_blk_probe(struct mmc_card *card)
2337{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002338 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002339 char cap_str[10];
2340
Pierre Ossman912490d2005-05-21 10:27:02 +01002341 /*
2342 * Check that the card supports the command class(es) we need.
2343 */
2344 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return -ENODEV;
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 md = mmc_blk_alloc(card);
2348 if (IS_ERR(md))
2349 return PTR_ERR(md);
2350
Yi Li444122f2009-02-05 15:31:57 +08002351 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002352 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302353 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002355 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Andrei Warkentin371a6892011-04-11 18:10:25 -05002357 if (mmc_blk_alloc_parts(card, md))
2358 goto out;
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002361 mmc_fixup_device(card, blk_fixups);
2362
San Mehat148c57a2009-07-30 08:21:19 -07002363#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2364 mmc_set_bus_resume_policy(card->host, 1);
2365#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002366 if (mmc_add_disk(md))
2367 goto out;
2368
2369 list_for_each_entry(part_md, &md->part, part) {
2370 if (mmc_add_disk(part_md))
2371 goto out;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
2374
2375 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002376 mmc_blk_remove_parts(card, md);
2377 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
2381static void mmc_blk_remove(struct mmc_card *card)
2382{
2383 struct mmc_blk_data *md = mmc_get_drvdata(card);
2384
Andrei Warkentin371a6892011-04-11 18:10:25 -05002385 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002386 mmc_claim_host(card->host);
2387 mmc_blk_part_switch(card, md);
2388 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002389 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002391#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2392 mmc_set_bus_resume_policy(card->host, 0);
2393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002397static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002399 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct mmc_blk_data *md = mmc_get_drvdata(card);
2401
2402 if (md) {
2403 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002404 list_for_each_entry(part_md, &md->part, part) {
2405 mmc_queue_suspend(&part_md->queue);
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408 return 0;
2409}
2410
2411static int mmc_blk_resume(struct mmc_card *card)
2412{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002413 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct mmc_blk_data *md = mmc_get_drvdata(card);
2415
2416 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002417 /*
2418 * Resume involves the card going into idle state,
2419 * so current partition is always the main one.
2420 */
2421 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002423 list_for_each_entry(part_md, &md->part, part) {
2424 mmc_queue_resume(&part_md->queue);
2425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 return 0;
2428}
2429#else
2430#define mmc_blk_suspend NULL
2431#define mmc_blk_resume NULL
2432#endif
2433
2434static struct mmc_driver mmc_driver = {
2435 .drv = {
2436 .name = "mmcblk",
2437 },
2438 .probe = mmc_blk_probe,
2439 .remove = mmc_blk_remove,
2440 .suspend = mmc_blk_suspend,
2441 .resume = mmc_blk_resume,
2442};
2443
2444static int __init mmc_blk_init(void)
2445{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002446 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002448 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2449 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2450
2451 max_devices = 256 / perdev_minors;
2452
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002453 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2454 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002457 res = mmc_register_driver(&mmc_driver);
2458 if (res)
2459 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002461 return 0;
2462 out2:
2463 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 out:
2465 return res;
2466}
2467
2468static void __exit mmc_blk_exit(void)
2469{
2470 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002471 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474module_init(mmc_blk_init);
2475module_exit(mmc_blk_exit);
2476
2477MODULE_LICENSE("GPL");
2478MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2479