blob: 9fbce4bc5750321b4fa627e61023cad4be672580 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200124 struct device_attribute min_sectors_to_check_bkops_status;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200130enum {
131 MMC_PACKED_N_IDX = -1,
132 MMC_PACKED_N_ZERO,
133 MMC_PACKED_N_SINGLE,
134};
135
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400136module_param(perdev_minors, int, 0444);
137MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
138
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200139static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
140{
141 mqrq->packed_cmd = MMC_PACKED_NONE;
142 mqrq->packed_num = MMC_PACKED_N_ZERO;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
146{
147 struct mmc_blk_data *md;
148
Arjan van de Vena621aae2006-01-12 18:43:35 +0000149 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 md = disk->private_data;
151 if (md && md->usage == 0)
152 md = NULL;
153 if (md)
154 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return md;
158}
159
Andrei Warkentin371a6892011-04-11 18:10:25 -0500160static inline int mmc_get_devidx(struct gendisk *disk)
161{
Colin Cross71b54d42010-09-03 12:41:21 -0700162 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500163 return devidx;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void mmc_blk_put(struct mmc_blk_data *md)
167{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000168 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 md->usage--;
170 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500171 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800172 blk_cleanup_queue(md->queue.queue);
173
David Woodhouse1dff3142007-11-21 18:45:12 +0100174 __clear_bit(devidx, dev_use);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(md);
178 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000179 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Johan Rudholmadd710e2011-12-02 08:51:06 +0100182static ssize_t power_ro_lock_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 int ret;
186 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
187 struct mmc_card *card = md->queue.card;
188 int locked = 0;
189
190 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
191 locked = 2;
192 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
193 locked = 1;
194
195 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
196
197 return ret;
198}
199
200static ssize_t power_ro_lock_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count)
202{
203 int ret;
204 struct mmc_blk_data *md, *part_md;
205 struct mmc_card *card;
206 unsigned long set;
207
208 if (kstrtoul(buf, 0, &set))
209 return -EINVAL;
210
211 if (set != 1)
212 return count;
213
214 md = mmc_blk_get(dev_to_disk(dev));
215 card = md->queue.card;
216
217 mmc_claim_host(card->host);
218
219 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
220 card->ext_csd.boot_ro_lock |
221 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
222 card->ext_csd.part_time);
223 if (ret)
224 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
225 else
226 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
227
228 mmc_release_host(card->host);
229
230 if (!ret) {
231 pr_info("%s: Locking boot partition ro until next power on\n",
232 md->disk->disk_name);
233 set_disk_ro(md->disk, 1);
234
235 list_for_each_entry(part_md, &md->part, part)
236 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
237 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
238 set_disk_ro(part_md->disk, 1);
239 }
240 }
241
242 mmc_blk_put(md);
243 return count;
244}
245
Andrei Warkentin371a6892011-04-11 18:10:25 -0500246static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
247 char *buf)
248{
249 int ret;
250 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
251
252 ret = snprintf(buf, PAGE_SIZE, "%d",
253 get_disk_ro(dev_to_disk(dev)) ^
254 md->read_only);
255 mmc_blk_put(md);
256 return ret;
257}
258
259static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
260 const char *buf, size_t count)
261{
262 int ret;
263 char *end;
264 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
265 unsigned long set = simple_strtoul(buf, &end, 0);
266 if (end == buf) {
267 ret = -EINVAL;
268 goto out;
269 }
270
271 set_disk_ro(dev_to_disk(dev), set || md->read_only);
272 ret = count;
273out:
274 mmc_blk_put(md);
275 return ret;
276}
277
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200278static ssize_t
279num_wr_reqs_to_start_packing_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
283 int num_wr_reqs_to_start_packing;
284 int ret;
285
286 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
287
288 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
289
290 mmc_blk_put(md);
291 return ret;
292}
293
294static ssize_t
295num_wr_reqs_to_start_packing_store(struct device *dev,
296 struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 int value;
300 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
301
302 sscanf(buf, "%d", &value);
303 if (value >= 0)
304 md->queue.num_wr_reqs_to_start_packing = value;
305
306 mmc_blk_put(md);
307 return count;
308}
309
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200310static ssize_t
311min_sectors_to_check_bkops_status_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
314 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
315 unsigned int min_sectors_to_check_bkops_status;
316 struct mmc_card *card = md->queue.card;
317 int ret;
318
319 if (!card)
320 return -EINVAL;
321
322 min_sectors_to_check_bkops_status =
323 card->bkops_info.min_sectors_to_queue_delayed_work;
324
325 ret = snprintf(buf, PAGE_SIZE, "%d\n",
326 min_sectors_to_check_bkops_status);
327
328 mmc_blk_put(md);
329 return ret;
330}
331
332static ssize_t
333min_sectors_to_check_bkops_status_store(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 int value;
338 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
339 struct mmc_card *card = md->queue.card;
340
341 if (!card)
342 return -EINVAL;
343
344 sscanf(buf, "%d", &value);
345 if (value >= 0)
346 card->bkops_info.min_sectors_to_queue_delayed_work = value;
347
348 mmc_blk_put(md);
349 return count;
350}
351
Al Viroa5a15612008-03-02 10:33:30 -0500352static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Al Viroa5a15612008-03-02 10:33:30 -0500354 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int ret = -ENXIO;
356
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200357 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (md) {
359 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500360 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700362
Al Viroa5a15612008-03-02 10:33:30 -0500363 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700364 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700365 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200368 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 return ret;
371}
372
Al Viroa5a15612008-03-02 10:33:30 -0500373static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Al Viroa5a15612008-03-02 10:33:30 -0500375 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200377 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200379 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381}
382
383static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800384mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800386 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
387 geo->heads = 4;
388 geo->sectors = 16;
389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
John Calixtocb87ea22011-04-26 18:56:29 -0400392struct mmc_blk_ioc_data {
393 struct mmc_ioc_cmd ic;
394 unsigned char *buf;
395 u64 buf_bytes;
396};
397
398static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
399 struct mmc_ioc_cmd __user *user)
400{
401 struct mmc_blk_ioc_data *idata;
402 int err;
403
404 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
405 if (!idata) {
406 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400407 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400408 }
409
410 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
411 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400412 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400413 }
414
415 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
416 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
417 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400418 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400419 }
420
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100421 if (!idata->buf_bytes)
422 return idata;
423
John Calixtocb87ea22011-04-26 18:56:29 -0400424 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
425 if (!idata->buf) {
426 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400427 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400428 }
429
430 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
431 idata->ic.data_ptr, idata->buf_bytes)) {
432 err = -EFAULT;
433 goto copy_err;
434 }
435
436 return idata;
437
438copy_err:
439 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400440idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400441 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400442out:
John Calixtocb87ea22011-04-26 18:56:29 -0400443 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400444}
445
446static int mmc_blk_ioctl_cmd(struct block_device *bdev,
447 struct mmc_ioc_cmd __user *ic_ptr)
448{
449 struct mmc_blk_ioc_data *idata;
450 struct mmc_blk_data *md;
451 struct mmc_card *card;
452 struct mmc_command cmd = {0};
453 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530454 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400455 struct scatterlist sg;
456 int err;
457
458 /*
459 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
460 * whole block device, not on a partition. This prevents overspray
461 * between sibling partitions.
462 */
463 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
464 return -EPERM;
465
466 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
467 if (IS_ERR(idata))
468 return PTR_ERR(idata);
469
John Calixtocb87ea22011-04-26 18:56:29 -0400470 md = mmc_blk_get(bdev->bd_disk);
471 if (!md) {
472 err = -EINVAL;
473 goto cmd_done;
474 }
475
476 card = md->queue.card;
477 if (IS_ERR(card)) {
478 err = PTR_ERR(card);
479 goto cmd_done;
480 }
481
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100482 cmd.opcode = idata->ic.opcode;
483 cmd.arg = idata->ic.arg;
484 cmd.flags = idata->ic.flags;
485
486 if (idata->buf_bytes) {
487 data.sg = &sg;
488 data.sg_len = 1;
489 data.blksz = idata->ic.blksz;
490 data.blocks = idata->ic.blocks;
491
492 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
493
494 if (idata->ic.write_flag)
495 data.flags = MMC_DATA_WRITE;
496 else
497 data.flags = MMC_DATA_READ;
498
499 /* data.flags must already be set before doing this. */
500 mmc_set_data_timeout(&data, card);
501
502 /* Allow overriding the timeout_ns for empirical tuning. */
503 if (idata->ic.data_timeout_ns)
504 data.timeout_ns = idata->ic.data_timeout_ns;
505
506 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
507 /*
508 * Pretend this is a data transfer and rely on the
509 * host driver to compute timeout. When all host
510 * drivers support cmd.cmd_timeout for R1B, this
511 * can be changed to:
512 *
513 * mrq.data = NULL;
514 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
515 */
516 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
517 }
518
519 mrq.data = &data;
520 }
521
522 mrq.cmd = &cmd;
523
John Calixtocb87ea22011-04-26 18:56:29 -0400524 mmc_claim_host(card->host);
525
526 if (idata->ic.is_acmd) {
527 err = mmc_app_cmd(card->host, card);
528 if (err)
529 goto cmd_rel_host;
530 }
531
John Calixtocb87ea22011-04-26 18:56:29 -0400532 mmc_wait_for_req(card->host, &mrq);
533
534 if (cmd.error) {
535 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
536 __func__, cmd.error);
537 err = cmd.error;
538 goto cmd_rel_host;
539 }
540 if (data.error) {
541 dev_err(mmc_dev(card->host), "%s: data error %d\n",
542 __func__, data.error);
543 err = data.error;
544 goto cmd_rel_host;
545 }
546
547 /*
548 * According to the SD specs, some commands require a delay after
549 * issuing the command.
550 */
551 if (idata->ic.postsleep_min_us)
552 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
553
554 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
555 err = -EFAULT;
556 goto cmd_rel_host;
557 }
558
559 if (!idata->ic.write_flag) {
560 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
561 idata->buf, idata->buf_bytes)) {
562 err = -EFAULT;
563 goto cmd_rel_host;
564 }
565 }
566
567cmd_rel_host:
568 mmc_release_host(card->host);
569
570cmd_done:
571 mmc_blk_put(md);
572 kfree(idata->buf);
573 kfree(idata);
574 return err;
575}
576
577static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
578 unsigned int cmd, unsigned long arg)
579{
580 int ret = -EINVAL;
581 if (cmd == MMC_IOC_CMD)
582 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
583 return ret;
584}
585
586#ifdef CONFIG_COMPAT
587static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
588 unsigned int cmd, unsigned long arg)
589{
590 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
591}
592#endif
593
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700594static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500595 .open = mmc_blk_open,
596 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800597 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400599 .ioctl = mmc_blk_ioctl,
600#ifdef CONFIG_COMPAT
601 .compat_ioctl = mmc_blk_compat_ioctl,
602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
Andrei Warkentin371a6892011-04-11 18:10:25 -0500605static inline int mmc_blk_part_switch(struct mmc_card *card,
606 struct mmc_blk_data *md)
607{
608 int ret;
609 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300610
Andrei Warkentin371a6892011-04-11 18:10:25 -0500611 if (main_md->part_curr == md->part_type)
612 return 0;
613
614 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300615 u8 part_config = card->ext_csd.part_config;
616
617 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
618 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500619
620 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300621 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500622 card->ext_csd.part_time);
623 if (ret)
624 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300625
626 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300627 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500628
629 main_md->part_curr = md->part_type;
630 return 0;
631}
632
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700633static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
634{
635 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100636 u32 result;
637 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700638
Venkatraman Sad5fd972011-08-25 00:30:50 +0530639 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400640 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400641 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700642
643 struct scatterlist sg;
644
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700645 cmd.opcode = MMC_APP_CMD;
646 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700647 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700648
649 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700650 if (err)
651 return (u32)-1;
652 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700653 return (u32)-1;
654
655 memset(&cmd, 0, sizeof(struct mmc_command));
656
657 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
658 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700659 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700660
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700661 data.blksz = 4;
662 data.blocks = 1;
663 data.flags = MMC_DATA_READ;
664 data.sg = &sg;
665 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530666 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700667
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700668 mrq.cmd = &cmd;
669 mrq.data = &data;
670
Ben Dooks051913d2009-06-08 23:33:57 +0100671 blocks = kmalloc(4, GFP_KERNEL);
672 if (!blocks)
673 return (u32)-1;
674
675 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700676
677 mmc_wait_for_req(card->host, &mrq);
678
Ben Dooks051913d2009-06-08 23:33:57 +0100679 result = ntohl(*blocks);
680 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700681
Ben Dooks051913d2009-06-08 23:33:57 +0100682 if (cmd.error || data.error)
683 result = (u32)-1;
684
685 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700686}
687
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100688static int send_stop(struct mmc_card *card, u32 *status)
689{
690 struct mmc_command cmd = {0};
691 int err;
692
693 cmd.opcode = MMC_STOP_TRANSMISSION;
694 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
695 err = mmc_wait_for_cmd(card->host, &cmd, 5);
696 if (err == 0)
697 *status = cmd.resp[0];
698 return err;
699}
700
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100701static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300702{
Chris Ball1278dba2011-04-13 23:40:30 -0400703 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300704 int err;
705
Adrian Hunter504f1912008-10-16 12:55:25 +0300706 cmd.opcode = MMC_SEND_STATUS;
707 if (!mmc_host_is_spi(card->host))
708 cmd.arg = card->rca << 16;
709 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100710 err = mmc_wait_for_cmd(card->host, &cmd, retries);
711 if (err == 0)
712 *status = cmd.resp[0];
713 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300714}
715
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530716#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100717#define ERR_RETRY 2
718#define ERR_ABORT 1
719#define ERR_CONTINUE 0
720
721static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
722 bool status_valid, u32 status)
723{
724 switch (error) {
725 case -EILSEQ:
726 /* response crc error, retry the r/w cmd */
727 pr_err("%s: %s sending %s command, card status %#x\n",
728 req->rq_disk->disk_name, "response CRC error",
729 name, status);
730 return ERR_RETRY;
731
732 case -ETIMEDOUT:
733 pr_err("%s: %s sending %s command, card status %#x\n",
734 req->rq_disk->disk_name, "timed out", name, status);
735
736 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700737 if (!status_valid) {
738 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100739 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700740 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100741 /*
742 * If it was a r/w cmd crc error, or illegal command
743 * (eg, issued in wrong state) then retry - we should
744 * have corrected the state problem above.
745 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700746 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
747 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100748 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700749 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100750
751 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700752 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100753 return ERR_ABORT;
754
755 default:
756 /* We don't understand the error code the driver gave us */
757 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
758 req->rq_disk->disk_name, error, status);
759 return ERR_ABORT;
760 }
761}
762
763/*
764 * Initial r/w and stop cmd error recovery.
765 * We don't know whether the card received the r/w cmd or not, so try to
766 * restore things back to a sane state. Essentially, we do this as follows:
767 * - Obtain card status. If the first attempt to obtain card status fails,
768 * the status word will reflect the failed status cmd, not the failed
769 * r/w cmd. If we fail to obtain card status, it suggests we can no
770 * longer communicate with the card.
771 * - Check the card state. If the card received the cmd but there was a
772 * transient problem with the response, it might still be in a data transfer
773 * mode. Try to send it a stop command. If this fails, we can't recover.
774 * - If the r/w cmd failed due to a response CRC error, it was probably
775 * transient, so retry the cmd.
776 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
777 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
778 * illegal cmd, retry.
779 * Otherwise we don't understand what happened, so abort.
780 */
781static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300782 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100783{
784 bool prev_cmd_status_valid = true;
785 u32 status, stop_status = 0;
786 int err, retry;
787
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530788 if (mmc_card_removed(card))
789 return ERR_NOMEDIUM;
790
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100791 /*
792 * Try to get card status which indicates both the card state
793 * and why there was no response. If the first attempt fails,
794 * we can't be sure the returned status is for the r/w command.
795 */
796 for (retry = 2; retry >= 0; retry--) {
797 err = get_card_status(card, &status, 0);
798 if (!err)
799 break;
800
801 prev_cmd_status_valid = false;
802 pr_err("%s: error %d sending status command, %sing\n",
803 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
804 }
805
806 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530807 if (err) {
808 /* Check if the card is removed */
809 if (mmc_detect_card_removed(card->host))
810 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100811 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530812 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100813
Adrian Hunter67716322011-08-29 16:42:15 +0300814 /* Flag ECC errors */
815 if ((status & R1_CARD_ECC_FAILED) ||
816 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
817 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
818 *ecc_err = 1;
819
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100820 /*
821 * Check the current card state. If it is in some data transfer
822 * mode, tell it to stop (and hopefully transition back to TRAN.)
823 */
824 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
825 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
826 err = send_stop(card, &stop_status);
827 if (err)
828 pr_err("%s: error %d sending stop command\n",
829 req->rq_disk->disk_name, err);
830
831 /*
832 * If the stop cmd also timed out, the card is probably
833 * not present, so abort. Other errors are bad news too.
834 */
835 if (err)
836 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300837 if (stop_status & R1_CARD_ECC_FAILED)
838 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100839 }
840
841 /* Check for set block count errors */
842 if (brq->sbc.error)
843 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
844 prev_cmd_status_valid, status);
845
846 /* Check for r/w command errors */
847 if (brq->cmd.error)
848 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
849 prev_cmd_status_valid, status);
850
Adrian Hunter67716322011-08-29 16:42:15 +0300851 /* Data errors */
852 if (!brq->stop.error)
853 return ERR_CONTINUE;
854
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100855 /* Now for stop errors. These aren't fatal to the transfer. */
856 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
857 req->rq_disk->disk_name, brq->stop.error,
858 brq->cmd.resp[0], status);
859
860 /*
861 * Subsitute in our own stop status as this will give the error
862 * state which happened during the execution of the r/w command.
863 */
864 if (stop_status) {
865 brq->stop.resp[0] = stop_status;
866 brq->stop.error = 0;
867 }
868 return ERR_CONTINUE;
869}
870
Adrian Hunter67716322011-08-29 16:42:15 +0300871static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
872 int type)
873{
874 int err;
875
876 if (md->reset_done & type)
877 return -EEXIST;
878
879 md->reset_done |= type;
880 err = mmc_hw_reset(host);
881 /* Ensure we switch back to the correct partition */
882 if (err != -EOPNOTSUPP) {
883 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
884 int part_err;
885
886 main_md->part_curr = main_md->part_type;
887 part_err = mmc_blk_part_switch(host->card, md);
888 if (part_err) {
889 /*
890 * We have failed to get back into the correct
891 * partition, so we need to abort the whole request.
892 */
893 return -ENODEV;
894 }
895 }
896 return err;
897}
898
899static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
900{
901 md->reset_done &= ~type;
902}
903
Adrian Hunterbd788c92010-08-11 14:17:47 -0700904static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, 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_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700910
Adrian Hunterbd788c92010-08-11 14:17:47 -0700911 if (!mmc_can_erase(card)) {
912 err = -EOPNOTSUPP;
913 goto out;
914 }
915
916 from = blk_rq_pos(req);
917 nr = blk_rq_sectors(req);
918
Maya Erez5f360692012-10-10 03:47:54 +0200919 if (card->ext_csd.bkops_en)
920 card->bkops_info.sectors_changed += blk_rq_sectors(req);
921
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900922 if (mmc_can_discard(card))
923 arg = MMC_DISCARD_ARG;
924 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700925 arg = MMC_TRIM_ARG;
926 else
927 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300928retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500929 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
930 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
931 INAND_CMD38_ARG_EXT_CSD,
932 arg == MMC_TRIM_ARG ?
933 INAND_CMD38_ARG_TRIM :
934 INAND_CMD38_ARG_ERASE,
935 0);
936 if (err)
937 goto out;
938 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700939 err = mmc_erase(card, from, nr, arg);
940out:
Adrian Hunter67716322011-08-29 16:42:15 +0300941 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
942 goto retry;
943 if (!err)
944 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530945 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700946
Adrian Hunterbd788c92010-08-11 14:17:47 -0700947 return err ? 0 : 1;
948}
949
Adrian Hunter49804542010-08-11 14:17:50 -0700950static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
951 struct request *req)
952{
953 struct mmc_blk_data *md = mq->data;
954 struct mmc_card *card = md->queue.card;
955 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300956 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700957
Maya Erez463bb952012-05-24 23:46:29 +0300958 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700959 err = -EOPNOTSUPP;
960 goto out;
961 }
962
963 from = blk_rq_pos(req);
964 nr = blk_rq_sectors(req);
965
966 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
967 arg = MMC_SECURE_TRIM1_ARG;
968 else
969 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300970retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500971 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
972 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
973 INAND_CMD38_ARG_EXT_CSD,
974 arg == MMC_SECURE_TRIM1_ARG ?
975 INAND_CMD38_ARG_SECTRIM1 :
976 INAND_CMD38_ARG_SECERASE,
977 0);
978 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300979 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500980 }
Adrian Hunter28302812012-04-05 14:45:48 +0300981
Adrian Hunter49804542010-08-11 14:17:50 -0700982 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300983 if (err == -EIO)
984 goto out_retry;
985 if (err)
986 goto out;
987
988 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500989 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
990 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
991 INAND_CMD38_ARG_EXT_CSD,
992 INAND_CMD38_ARG_SECTRIM2,
993 0);
994 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300995 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500996 }
Adrian Hunter28302812012-04-05 14:45:48 +0300997
Adrian Hunter49804542010-08-11 14:17:50 -0700998 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300999 if (err == -EIO)
1000 goto out_retry;
1001 if (err)
1002 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001003 }
Adrian Hunter28302812012-04-05 14:45:48 +03001004
1005 if (mmc_can_sanitize(card))
1006 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1007 EXT_CSD_SANITIZE_START, 1, 0);
1008out_retry:
1009 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001010 goto retry;
1011 if (!err)
1012 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001013out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301014 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001015
Adrian Hunter49804542010-08-11 14:17:50 -07001016 return err ? 0 : 1;
1017}
1018
Maya Erez463bb952012-05-24 23:46:29 +03001019static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1020 struct request *req)
1021{
1022 struct mmc_blk_data *md = mq->data;
1023 struct mmc_card *card = md->queue.card;
1024 int err = 0;
1025
1026 BUG_ON(!card);
1027 BUG_ON(!card->host);
1028
1029 if (!(mmc_can_sanitize(card) &&
1030 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1031 pr_warning("%s: %s - SANITIZE is not supported\n",
1032 mmc_hostname(card->host), __func__);
1033 err = -EOPNOTSUPP;
1034 goto out;
1035 }
1036
1037 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1038 mmc_hostname(card->host), __func__);
1039
1040 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001041 EXT_CSD_SANITIZE_START, 1,
1042 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001043
1044 if (err)
1045 pr_err("%s: %s - mmc_switch() with "
1046 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1047 mmc_hostname(card->host), __func__, err);
1048
1049 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1050 __func__);
1051
1052out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301053 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001054
1055 return err ? 0 : 1;
1056}
1057
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001058static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1059{
1060 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001061 struct mmc_card *card = md->queue.card;
1062 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001063
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001064 ret = mmc_flush_cache(card);
1065 if (ret)
1066 ret = -EIO;
1067
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301068 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001069
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001070 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001071}
1072
1073/*
1074 * Reformat current write as a reliable write, supporting
1075 * both legacy and the enhanced reliable write MMC cards.
1076 * In each transfer we'll handle only as much as a single
1077 * reliable write can handle, thus finish the request in
1078 * partial completions.
1079 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001080static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1081 struct mmc_card *card,
1082 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001083{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001084 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1085 /* Legacy mode imposes restrictions on transfers. */
1086 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1087 brq->data.blocks = 1;
1088
1089 if (brq->data.blocks > card->ext_csd.rel_sectors)
1090 brq->data.blocks = card->ext_csd.rel_sectors;
1091 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1092 brq->data.blocks = 1;
1093 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001094}
1095
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001096#define CMD_ERRORS \
1097 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1098 R1_ADDRESS_ERROR | /* Misaligned address */ \
1099 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1100 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1101 R1_CC_ERROR | /* Card controller error */ \
1102 R1_ERROR) /* General/unknown error */
1103
Per Forlinee8a43a2011-07-01 18:55:33 +02001104static int mmc_blk_err_check(struct mmc_card *card,
1105 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001106{
Per Forlinee8a43a2011-07-01 18:55:33 +02001107 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1108 mmc_active);
1109 struct mmc_blk_request *brq = &mq_mrq->brq;
1110 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001111 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001112
1113 /*
1114 * sbc.error indicates a problem with the set block count
1115 * command. No data will have been transferred.
1116 *
1117 * cmd.error indicates a problem with the r/w command. No
1118 * data will have been transferred.
1119 *
1120 * stop.error indicates a problem with the stop command. Data
1121 * may have been transferred, or may still be transferring.
1122 */
Adrian Hunter67716322011-08-29 16:42:15 +03001123 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1124 brq->data.error) {
1125 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001126 case ERR_RETRY:
1127 return MMC_BLK_RETRY;
1128 case ERR_ABORT:
1129 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301130 case ERR_NOMEDIUM:
1131 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001132 case ERR_CONTINUE:
1133 break;
1134 }
1135 }
1136
1137 /*
1138 * Check for errors relating to the execution of the
1139 * initial command - such as address errors. No data
1140 * has been transferred.
1141 */
1142 if (brq->cmd.resp[0] & CMD_ERRORS) {
1143 pr_err("%s: r/w command failed, status = %#x\n",
1144 req->rq_disk->disk_name, brq->cmd.resp[0]);
1145 return MMC_BLK_ABORT;
1146 }
1147
1148 /*
1149 * Everything else is either success, or a data error of some
1150 * kind. If it was a write, we may have transitioned to
1151 * program mode, which we have to wait for it to complete.
1152 */
1153 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1154 u32 status;
1155 do {
1156 int err = get_card_status(card, &status, 5);
1157 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301158 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001159 req->rq_disk->disk_name, err);
1160 return MMC_BLK_CMD_ERR;
1161 }
1162 /*
1163 * Some cards mishandle the status bits,
1164 * so make sure to check both the busy
1165 * indication and the card state.
1166 */
1167 } while (!(status & R1_READY_FOR_DATA) ||
1168 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1169 }
1170
1171 if (brq->data.error) {
1172 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1173 req->rq_disk->disk_name, brq->data.error,
1174 (unsigned)blk_rq_pos(req),
1175 (unsigned)blk_rq_sectors(req),
1176 brq->cmd.resp[0], brq->stop.resp[0]);
1177
1178 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001179 if (ecc_err)
1180 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001181 return MMC_BLK_DATA_ERR;
1182 } else {
1183 return MMC_BLK_CMD_ERR;
1184 }
1185 }
1186
Adrian Hunter67716322011-08-29 16:42:15 +03001187 if (!brq->data.bytes_xfered)
1188 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001189
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001190 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1191 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1192 return MMC_BLK_PARTIAL;
1193 else
1194 return MMC_BLK_SUCCESS;
1195 }
1196
Adrian Hunter67716322011-08-29 16:42:15 +03001197 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1198 return MMC_BLK_PARTIAL;
1199
1200 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001201}
1202
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001203static int mmc_blk_packed_err_check(struct mmc_card *card,
1204 struct mmc_async_req *areq)
1205{
1206 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1207 mmc_active);
1208 struct request *req = mq_rq->req;
1209 int err, check, status;
1210 u8 ext_csd[512];
1211
1212 mq_rq->packed_retries--;
1213 check = mmc_blk_err_check(card, areq);
1214 err = get_card_status(card, &status, 0);
1215 if (err) {
1216 pr_err("%s: error %d sending status command\n",
1217 req->rq_disk->disk_name, err);
1218 return MMC_BLK_ABORT;
1219 }
1220
1221 if (status & R1_EXCEPTION_EVENT) {
1222 err = mmc_send_ext_csd(card, ext_csd);
1223 if (err) {
1224 pr_err("%s: error %d sending ext_csd\n",
1225 req->rq_disk->disk_name, err);
1226 return MMC_BLK_ABORT;
1227 }
1228
1229 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1230 EXT_CSD_PACKED_FAILURE) &&
1231 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1232 EXT_CSD_PACKED_GENERIC_ERROR)) {
1233 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1234 EXT_CSD_PACKED_INDEXED_ERROR) {
1235 mq_rq->packed_fail_idx =
1236 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1237 return MMC_BLK_PARTIAL;
1238 }
1239 }
1240 }
1241
1242 return check;
1243}
1244
Per Forlin54d49d772011-07-01 18:55:29 +02001245static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1246 struct mmc_card *card,
1247 int disable_multi,
1248 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Per Forlin54d49d772011-07-01 18:55:29 +02001250 u32 readcmd, writecmd;
1251 struct mmc_blk_request *brq = &mqrq->brq;
1252 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301254 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001256 /*
1257 * Reliable writes are used to implement Forced Unit Access and
1258 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001259 *
1260 * XXX: this really needs a good explanation of why REQ_META
1261 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001262 */
1263 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1264 (req->cmd_flags & REQ_META)) &&
1265 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001266 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001267
Per Forlin54d49d772011-07-01 18:55:29 +02001268 memset(brq, 0, sizeof(struct mmc_blk_request));
1269 brq->mrq.cmd = &brq->cmd;
1270 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Per Forlin54d49d772011-07-01 18:55:29 +02001272 brq->cmd.arg = blk_rq_pos(req);
1273 if (!mmc_card_blockaddr(card))
1274 brq->cmd.arg <<= 9;
1275 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1276 brq->data.blksz = 512;
1277 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1278 brq->stop.arg = 0;
1279 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1280 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Asutosh Das1a897142012-07-27 18:10:19 +05301282 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001283 /*
1284 * The block layer doesn't support all sector count
1285 * restrictions, so we need to be prepared for too big
1286 * requests.
1287 */
1288 if (brq->data.blocks > card->host->max_blk_count)
1289 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001291 if (brq->data.blocks > 1) {
1292 /*
1293 * After a read error, we redo the request one sector
1294 * at a time in order to accurately determine which
1295 * sectors can be read successfully.
1296 */
1297 if (disable_multi)
1298 brq->data.blocks = 1;
1299
1300 /* Some controllers can't do multiblock reads due to hw bugs */
1301 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1302 rq_data_dir(req) == READ)
1303 brq->data.blocks = 1;
1304 }
Per Forlin54d49d772011-07-01 18:55:29 +02001305
1306 if (brq->data.blocks > 1 || do_rel_wr) {
1307 /* SPI multiblock writes terminate using a special
1308 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001309 */
Per Forlin54d49d772011-07-01 18:55:29 +02001310 if (!mmc_host_is_spi(card->host) ||
1311 rq_data_dir(req) == READ)
1312 brq->mrq.stop = &brq->stop;
1313 readcmd = MMC_READ_MULTIPLE_BLOCK;
1314 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1315 } else {
1316 brq->mrq.stop = NULL;
1317 readcmd = MMC_READ_SINGLE_BLOCK;
1318 writecmd = MMC_WRITE_BLOCK;
1319 }
1320 if (rq_data_dir(req) == READ) {
1321 brq->cmd.opcode = readcmd;
1322 brq->data.flags |= MMC_DATA_READ;
1323 } else {
1324 brq->cmd.opcode = writecmd;
1325 brq->data.flags |= MMC_DATA_WRITE;
1326 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001327
Per Forlin54d49d772011-07-01 18:55:29 +02001328 if (do_rel_wr)
1329 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001330
Per Forlin54d49d772011-07-01 18:55:29 +02001331 /*
Saugata Das42659002011-12-21 13:09:17 +05301332 * Data tag is used only during writing meta data to speed
1333 * up write and any subsequent read of this meta data
1334 */
1335 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1336 (req->cmd_flags & REQ_META) &&
1337 (rq_data_dir(req) == WRITE) &&
1338 ((brq->data.blocks * brq->data.blksz) >=
1339 card->ext_csd.data_tag_unit_size);
1340
1341 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001342 * Pre-defined multi-block transfers are preferable to
1343 * open ended-ones (and necessary for reliable writes).
1344 * However, it is not sufficient to just send CMD23,
1345 * and avoid the final CMD12, as on an error condition
1346 * CMD12 (stop) needs to be sent anyway. This, coupled
1347 * with Auto-CMD23 enhancements provided by some
1348 * hosts, means that the complexity of dealing
1349 * with this is best left to the host. If CMD23 is
1350 * supported by card and host, we'll fill sbc in and let
1351 * the host deal with handling it correctly. This means
1352 * that for hosts that don't expose MMC_CAP_CMD23, no
1353 * change of behavior will be observed.
1354 *
1355 * N.B: Some MMC cards experience perf degradation.
1356 * We'll avoid using CMD23-bounded multiblock writes for
1357 * these, while retaining features like reliable writes.
1358 */
Saugata Das42659002011-12-21 13:09:17 +05301359 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1360 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1361 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001362 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1363 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301364 (do_rel_wr ? (1 << 31) : 0) |
1365 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001366 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1367 brq->mrq.sbc = &brq->sbc;
1368 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001369
Per Forlin54d49d772011-07-01 18:55:29 +02001370 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001371
Per Forlin54d49d772011-07-01 18:55:29 +02001372 brq->data.sg = mqrq->sg;
1373 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001374
Per Forlin54d49d772011-07-01 18:55:29 +02001375 /*
1376 * Adjust the sg list so it is the same size as the
1377 * request.
1378 */
1379 if (brq->data.blocks != blk_rq_sectors(req)) {
1380 int i, data_size = brq->data.blocks << 9;
1381 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001382
Per Forlin54d49d772011-07-01 18:55:29 +02001383 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1384 data_size -= sg->length;
1385 if (data_size <= 0) {
1386 sg->length += data_size;
1387 i++;
1388 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001389 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001390 }
Per Forlin54d49d772011-07-01 18:55:29 +02001391 brq->data.sg_len = i;
1392 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001393
Per Forlinee8a43a2011-07-01 18:55:33 +02001394 mqrq->mmc_active.mrq = &brq->mrq;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001395 if (mq->err_check_fn)
1396 mqrq->mmc_active.err_check = mq->err_check_fn;
1397 else
1398 mqrq->mmc_active.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001399
Per Forlin54d49d772011-07-01 18:55:29 +02001400 mmc_queue_bounce_pre(mqrq);
1401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001403static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1404 struct request *req)
1405{
1406 struct mmc_host *host = mq->card->host;
1407 int data_dir;
1408
1409 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1410 return;
1411
Maya Erez6f219eb2012-12-02 13:27:15 +02001412 /* Support for the write packing on eMMC 4.5 or later */
1413 if (mq->card->ext_csd.rev <= 5)
1414 return;
1415
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001416 /*
1417 * In case the packing control is not supported by the host, it should
1418 * not have an effect on the write packing. Therefore we have to enable
1419 * the write packing
1420 */
1421 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1422 mq->wr_packing_enabled = true;
1423 return;
1424 }
1425
1426 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1427 if (mq->num_of_potential_packed_wr_reqs >
1428 mq->num_wr_reqs_to_start_packing)
1429 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001430 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001431 return;
1432 }
1433
1434 data_dir = rq_data_dir(req);
1435
1436 if (data_dir == READ) {
1437 mq->num_of_potential_packed_wr_reqs = 0;
1438 mq->wr_packing_enabled = false;
1439 return;
1440 } else if (data_dir == WRITE) {
1441 mq->num_of_potential_packed_wr_reqs++;
1442 }
1443
1444 if (mq->num_of_potential_packed_wr_reqs >
1445 mq->num_wr_reqs_to_start_packing)
1446 mq->wr_packing_enabled = true;
1447
1448}
1449
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001450struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1451{
1452 if (!card)
1453 return NULL;
1454
1455 return &card->wr_pack_stats;
1456}
1457EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1458
1459void mmc_blk_init_packed_statistics(struct mmc_card *card)
1460{
1461 int max_num_of_packed_reqs = 0;
1462
1463 if (!card || !card->wr_pack_stats.packing_events)
1464 return;
1465
1466 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1467
1468 spin_lock(&card->wr_pack_stats.lock);
1469 memset(card->wr_pack_stats.packing_events, 0,
1470 (max_num_of_packed_reqs + 1) *
1471 sizeof(*card->wr_pack_stats.packing_events));
1472 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1473 sizeof(card->wr_pack_stats.pack_stop_reason));
1474 card->wr_pack_stats.enabled = true;
1475 spin_unlock(&card->wr_pack_stats.lock);
1476}
1477EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1478
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001479static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1480{
1481 struct request_queue *q = mq->queue;
1482 struct mmc_card *card = mq->card;
1483 struct request *cur = req, *next = NULL;
1484 struct mmc_blk_data *md = mq->data;
1485 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1486 unsigned int req_sectors = 0, phys_segments = 0;
1487 unsigned int max_blk_count, max_phys_segs;
1488 u8 put_back = 0;
1489 u8 max_packed_rw = 0;
1490 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001491 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001492
1493 mmc_blk_clear_packed(mq->mqrq_cur);
1494
1495 if (!(md->flags & MMC_BLK_CMD23) ||
1496 !card->ext_csd.packed_event_en)
1497 goto no_packed;
1498
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001499 if (!mq->wr_packing_enabled)
1500 goto no_packed;
1501
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001502 if ((rq_data_dir(cur) == WRITE) &&
1503 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1504 max_packed_rw = card->ext_csd.max_packed_writes;
1505
1506 if (max_packed_rw == 0)
1507 goto no_packed;
1508
1509 if (mmc_req_rel_wr(cur) &&
1510 (md->flags & MMC_BLK_REL_WR) &&
1511 !en_rel_wr)
1512 goto no_packed;
1513
1514 if (mmc_large_sec(card) &&
1515 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1516 goto no_packed;
1517
1518 max_blk_count = min(card->host->max_blk_count,
1519 card->host->max_req_size >> 9);
1520 if (unlikely(max_blk_count > 0xffff))
1521 max_blk_count = 0xffff;
1522
1523 max_phys_segs = queue_max_segments(q);
1524 req_sectors += blk_rq_sectors(cur);
1525 phys_segments += cur->nr_phys_segments;
1526
1527 if (rq_data_dir(cur) == WRITE) {
1528 req_sectors++;
1529 phys_segments++;
1530 }
1531
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001532 spin_lock(&stats->lock);
1533
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001534 while (reqs < max_packed_rw - 1) {
1535 spin_lock_irq(q->queue_lock);
1536 next = blk_fetch_request(q);
1537 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001538 if (!next) {
1539 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001540 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001541 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001542
1543 if (mmc_large_sec(card) &&
1544 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001545 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001546 put_back = 1;
1547 break;
1548 }
1549
1550 if (next->cmd_flags & REQ_DISCARD ||
1551 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001552 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001553 put_back = 1;
1554 break;
1555 }
1556
1557 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001558 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001559 put_back = 1;
1560 break;
1561 }
1562
1563 if (mmc_req_rel_wr(next) &&
1564 (md->flags & MMC_BLK_REL_WR) &&
1565 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001566 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001567 put_back = 1;
1568 break;
1569 }
1570
1571 req_sectors += blk_rq_sectors(next);
1572 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001573 if (stats->enabled)
1574 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001575 put_back = 1;
1576 break;
1577 }
1578
1579 phys_segments += next->nr_phys_segments;
1580 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001581 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001582 put_back = 1;
1583 break;
1584 }
1585
Maya Erez5f360692012-10-10 03:47:54 +02001586 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001587 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5f360692012-10-10 03:47:54 +02001588 if (card->ext_csd.bkops_en)
1589 card->bkops_info.sectors_changed +=
1590 blk_rq_sectors(next);
1591 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001592 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1593 cur = next;
1594 reqs++;
1595 }
1596
1597 if (put_back) {
1598 spin_lock_irq(q->queue_lock);
1599 blk_requeue_request(q, next);
1600 spin_unlock_irq(q->queue_lock);
1601 }
1602
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001603 if (stats->enabled) {
1604 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1605 stats->packing_events[reqs + 1]++;
1606 if (reqs + 1 == max_packed_rw)
1607 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1608 }
1609
1610 spin_unlock(&stats->lock);
1611
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001612 if (reqs > 0) {
1613 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1614 mq->mqrq_cur->packed_num = ++reqs;
1615 mq->mqrq_cur->packed_retries = reqs;
1616 return reqs;
1617 }
1618
1619no_packed:
1620 mmc_blk_clear_packed(mq->mqrq_cur);
1621 return 0;
1622}
1623
1624static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1625 struct mmc_card *card,
1626 struct mmc_queue *mq)
1627{
1628 struct mmc_blk_request *brq = &mqrq->brq;
1629 struct request *req = mqrq->req;
1630 struct request *prq;
1631 struct mmc_blk_data *md = mq->data;
1632 bool do_rel_wr, do_data_tag;
1633 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1634 u8 i = 1;
1635
1636 mqrq->packed_cmd = MMC_PACKED_WRITE;
1637 mqrq->packed_blocks = 0;
1638 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1639
1640 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1641 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1642 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1643
1644 /*
1645 * Argument for each entry of packed group
1646 */
1647 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1648 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1649 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1650 (prq->cmd_flags & REQ_META) &&
1651 (rq_data_dir(prq) == WRITE) &&
1652 ((brq->data.blocks * brq->data.blksz) >=
1653 card->ext_csd.data_tag_unit_size);
1654 /* Argument of CMD23 */
1655 packed_cmd_hdr[(i * 2)] =
1656 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1657 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1658 blk_rq_sectors(prq);
1659 /* Argument of CMD18 or CMD25 */
1660 packed_cmd_hdr[((i * 2)) + 1] =
1661 mmc_card_blockaddr(card) ?
1662 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1663 mqrq->packed_blocks += blk_rq_sectors(prq);
1664 i++;
1665 }
1666
1667 memset(brq, 0, sizeof(struct mmc_blk_request));
1668 brq->mrq.cmd = &brq->cmd;
1669 brq->mrq.data = &brq->data;
1670 brq->mrq.sbc = &brq->sbc;
1671 brq->mrq.stop = &brq->stop;
1672
1673 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1674 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1675 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1676
1677 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1678 brq->cmd.arg = blk_rq_pos(req);
1679 if (!mmc_card_blockaddr(card))
1680 brq->cmd.arg <<= 9;
1681 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1682
1683 brq->data.blksz = 512;
1684 brq->data.blocks = mqrq->packed_blocks + 1;
1685 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02001686 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001687
1688 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1689 brq->stop.arg = 0;
1690 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1691
1692 mmc_set_data_timeout(&brq->data, card);
1693
1694 brq->data.sg = mqrq->sg;
1695 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1696
1697 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001698
1699 /*
1700 * This is intended for packed commands tests usage - in case these
1701 * functions are not in use the respective pointers are NULL
1702 */
1703 if (mq->err_check_fn)
1704 mqrq->mmc_active.err_check = mq->err_check_fn;
1705 else
1706 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1707
1708 if (mq->packed_test_fn)
1709 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001710
1711 mmc_queue_bounce_pre(mqrq);
1712}
1713
Adrian Hunter67716322011-08-29 16:42:15 +03001714static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1715 struct mmc_blk_request *brq, struct request *req,
1716 int ret)
1717{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001718 struct mmc_queue_req *mq_rq;
1719 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1720
Adrian Hunter67716322011-08-29 16:42:15 +03001721 /*
1722 * If this is an SD card and we're writing, we can first
1723 * mark the known good sectors as ok.
1724 *
1725 * If the card is not SD, we can still ok written sectors
1726 * as reported by the controller (which might be less than
1727 * the real number of written sectors, but never more).
1728 */
1729 if (mmc_card_sd(card)) {
1730 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301731 if (!brq->data.fault_injected) {
1732 blocks = mmc_sd_num_wr_blocks(card);
1733 if (blocks != (u32)-1)
1734 ret = blk_end_request(req, 0, blocks << 9);
1735 } else
1736 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001737 } else {
1738 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1739 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1740 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001741 return ret;
1742}
1743
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001744static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1745{
1746 struct request *prq;
1747 int idx = mq_rq->packed_fail_idx, i = 0;
1748 int ret = 0;
1749
1750 while (!list_empty(&mq_rq->packed_list)) {
1751 prq = list_entry_rq(mq_rq->packed_list.next);
1752 if (idx == i) {
1753 /* retry from error index */
1754 mq_rq->packed_num -= idx;
1755 mq_rq->req = prq;
1756 ret = 1;
1757
1758 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1759 list_del_init(&prq->queuelist);
1760 mmc_blk_clear_packed(mq_rq);
1761 }
1762 return ret;
1763 }
1764 list_del_init(&prq->queuelist);
1765 blk_end_request(prq, 0, blk_rq_bytes(prq));
1766 i++;
1767 }
1768
1769 mmc_blk_clear_packed(mq_rq);
1770 return ret;
1771}
1772static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1773{
1774 struct request *prq;
1775
1776 while (!list_empty(&mq_rq->packed_list)) {
1777 prq = list_entry_rq(mq_rq->packed_list.next);
1778 list_del_init(&prq->queuelist);
1779 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1780 }
1781
1782 mmc_blk_clear_packed(mq_rq);
1783}
1784
1785static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1786 struct mmc_queue_req *mq_rq)
1787{
1788 struct request *prq;
1789 struct request_queue *q = mq->queue;
1790
1791 while (!list_empty(&mq_rq->packed_list)) {
1792 prq = list_entry_rq(mq_rq->packed_list.prev);
1793 if (prq->queuelist.prev != &mq_rq->packed_list) {
1794 list_del_init(&prq->queuelist);
1795 spin_lock_irq(q->queue_lock);
1796 blk_requeue_request(mq->queue, prq);
1797 spin_unlock_irq(q->queue_lock);
1798 } else {
1799 list_del_init(&prq->queuelist);
1800 }
1801 }
1802
1803 mmc_blk_clear_packed(mq_rq);
1804}
1805
Per Forlinee8a43a2011-07-01 18:55:33 +02001806static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001807{
1808 struct mmc_blk_data *md = mq->data;
1809 struct mmc_card *card = md->queue.card;
1810 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001811 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001812 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001813 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001814 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001815 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001816 const u8 packed_num = 2;
1817 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001818
1819 if (!rqc && !mq->mqrq_prev->req)
1820 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001821
Maya Erez5f360692012-10-10 03:47:54 +02001822 if (rqc) {
1823 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1824 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001825 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001826 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001827
Per Forlin54d49d772011-07-01 18:55:29 +02001828 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001829 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001830 if (reqs >= packed_num)
1831 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1832 card, mq);
1833 else
1834 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001835 areq = &mq->mqrq_cur->mmc_active;
1836 } else
1837 areq = NULL;
1838 areq = mmc_start_req(card->host, areq, (int *) &status);
1839 if (!areq)
1840 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001841
Per Forlinee8a43a2011-07-01 18:55:33 +02001842 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1843 brq = &mq_rq->brq;
1844 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001845 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001846 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001847
Per Forlind78d4a82011-07-01 18:55:30 +02001848 switch (status) {
1849 case MMC_BLK_SUCCESS:
1850 case MMC_BLK_PARTIAL:
1851 /*
1852 * A block was successfully transferred.
1853 */
Adrian Hunter67716322011-08-29 16:42:15 +03001854 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001855
1856 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1857 ret = mmc_blk_end_packed_req(mq_rq);
1858 break;
1859 } else {
1860 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001861 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001862 }
1863
Adrian Hunter67716322011-08-29 16:42:15 +03001864 /*
1865 * If the blk_end_request function returns non-zero even
1866 * though all data has been transferred and no errors
1867 * were returned by the host controller, it's a bug.
1868 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001869 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301870 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001871 __func__, blk_rq_bytes(req),
1872 brq->data.bytes_xfered);
1873 rqc = NULL;
1874 goto cmd_abort;
1875 }
Per Forlind78d4a82011-07-01 18:55:30 +02001876 break;
1877 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001878 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1879 if (!mmc_blk_reset(md, card->host, type))
1880 break;
1881 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001882 case MMC_BLK_RETRY:
1883 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001884 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001885 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001886 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001887 if (!mmc_blk_reset(md, card->host, type))
1888 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001889 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001890 case MMC_BLK_DATA_ERR: {
1891 int err;
1892
1893 err = mmc_blk_reset(md, card->host, type);
1894 if (!err)
1895 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001896 if (err == -ENODEV ||
1897 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001898 goto cmd_abort;
1899 /* Fall through */
1900 }
1901 case MMC_BLK_ECC_ERR:
1902 if (brq->data.blocks > 1) {
1903 /* Redo read one sector at a time */
1904 pr_warning("%s: retrying using single block read\n",
1905 req->rq_disk->disk_name);
1906 disable_multi = 1;
1907 break;
1908 }
Per Forlind78d4a82011-07-01 18:55:30 +02001909 /*
1910 * After an error, we redo I/O one sector at a
1911 * time, so we only reach here after trying to
1912 * read a single sector.
1913 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301914 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001915 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001916 if (!ret)
1917 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001918 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301919 case MMC_BLK_NOMEDIUM:
1920 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001921 }
1922
Per Forlinee8a43a2011-07-01 18:55:33 +02001923 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001924 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1925 /*
1926 * In case of a incomplete request
1927 * prepare it again and resend.
1928 */
1929 mmc_blk_rw_rq_prep(mq_rq, card,
1930 disable_multi, mq);
1931 mmc_start_req(card->host,
1932 &mq_rq->mmc_active, NULL);
1933 } else {
1934 if (!mq_rq->packed_retries)
1935 goto cmd_abort;
1936 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1937 mmc_start_req(card->host,
1938 &mq_rq->mmc_active, NULL);
1939 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 } while (ret);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 1;
1944
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001945 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001946 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1947 if (mmc_card_removed(card))
1948 req->cmd_flags |= REQ_QUIET;
1949 while (ret)
1950 ret = blk_end_request(req, -EIO,
1951 blk_rq_cur_bytes(req));
1952 } else {
1953 mmc_blk_abort_packed_req(mq_rq);
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Per Forlinee8a43a2011-07-01 18:55:33 +02001956 start_new_req:
1957 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001958 /*
1959 * If current request is packed, it needs to put back.
1960 */
1961 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1962 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1963
Per Forlinee8a43a2011-07-01 18:55:33 +02001964 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1965 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1966 }
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
1970
Adrian Hunterbd788c92010-08-11 14:17:47 -07001971static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1972{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001973 int ret;
1974 struct mmc_blk_data *md = mq->data;
1975 struct mmc_card *card = md->queue.card;
1976
San Mehat148c57a2009-07-30 08:21:19 -07001977#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1978 if (mmc_bus_needs_resume(card->host)) {
1979 mmc_resume_bus(card->host);
1980 mmc_blk_set_blksize(md, card);
1981 }
1982#endif
1983
Per Forlinee8a43a2011-07-01 18:55:33 +02001984 if (req && !mq->mqrq_prev->req)
1985 /* claim host only for the first request */
1986 mmc_claim_host(card->host);
1987
Andrei Warkentin371a6892011-04-11 18:10:25 -05001988 ret = mmc_blk_part_switch(card, md);
1989 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001990 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301991 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001992 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001993 ret = 0;
1994 goto out;
1995 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001996
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001997 mmc_blk_write_packing_control(mq, req);
1998
Maya Erez463bb952012-05-24 23:46:29 +03001999 if (req && req->cmd_flags & REQ_SANITIZE) {
2000 /* complete ongoing async transfer before issuing sanitize */
2001 if (card->host && card->host->areq)
2002 mmc_blk_issue_rw_rq(mq, NULL);
2003 ret = mmc_blk_issue_sanitize_rq(mq, req);
2004 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002005 /* complete ongoing async transfer before issuing discard */
2006 if (card->host->areq)
2007 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302008 if (req->cmd_flags & REQ_SECURE &&
2009 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002010 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002011 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002012 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002013 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002014 /* complete ongoing async transfer before issuing flush */
2015 if (card->host->areq)
2016 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002017 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002018 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002019 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002020 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002021
Andrei Warkentin371a6892011-04-11 18:10:25 -05002022out:
Per Forlinee8a43a2011-07-01 18:55:33 +02002023 if (!req)
2024 /* release host only when there are no more requests */
2025 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002026 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Russell Kinga6f6c962006-01-03 22:38:44 +00002029static inline int mmc_blk_readonly(struct mmc_card *card)
2030{
2031 return mmc_card_readonly(card) ||
2032 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2033}
2034
Andrei Warkentin371a6892011-04-11 18:10:25 -05002035static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2036 struct device *parent,
2037 sector_t size,
2038 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002039 const char *subname,
2040 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 struct mmc_blk_data *md;
2043 int devidx, ret;
2044
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002045 devidx = find_first_zero_bit(dev_use, max_devices);
2046 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return ERR_PTR(-ENOSPC);
2048 __set_bit(devidx, dev_use);
2049
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002050 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002051 if (!md) {
2052 ret = -ENOMEM;
2053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002055
Russell Kinga6f6c962006-01-03 22:38:44 +00002056 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002057 * !subname implies we are creating main mmc_blk_data that will be
2058 * associated with mmc_card with mmc_set_drvdata. Due to device
2059 * partitions, devidx will not coincide with a per-physical card
2060 * index anymore so we keep track of a name index.
2061 */
2062 if (!subname) {
2063 md->name_idx = find_first_zero_bit(name_use, max_devices);
2064 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002065 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002066 md->name_idx = ((struct mmc_blk_data *)
2067 dev_to_disk(parent)->private_data)->name_idx;
2068
Johan Rudholmadd710e2011-12-02 08:51:06 +01002069 md->area_type = area_type;
2070
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002071 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002072 * Set the read-only status based on the supported commands
2073 * and the write protect switch.
2074 */
2075 md->read_only = mmc_blk_readonly(card);
2076
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002077 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002078 if (md->disk == NULL) {
2079 ret = -ENOMEM;
2080 goto err_kfree;
2081 }
2082
2083 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002084 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002085 md->usage = 1;
2086
Adrian Hunterd09408a2011-06-23 13:40:28 +03002087 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002088 if (ret)
2089 goto err_putdisk;
2090
Russell Kinga6f6c962006-01-03 22:38:44 +00002091 md->queue.issue_fn = mmc_blk_issue_rq;
2092 md->queue.data = md;
2093
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002094 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002095 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002096 md->disk->fops = &mmc_bdops;
2097 md->disk->private_data = md;
2098 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002099 md->disk->driverfs_dev = parent;
2100 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002101 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002102
2103 /*
2104 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2105 *
2106 * - be set for removable media with permanent block devices
2107 * - be unset for removable block devices with permanent media
2108 *
2109 * Since MMC block devices clearly fall under the second
2110 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2111 * should use the block device creation/destruction hotplug
2112 * messages to tell when the card is present.
2113 */
2114
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002115 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2116 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002117
Martin K. Petersene1defc42009-05-22 17:17:49 -04002118 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002119 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002120
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002121 if (mmc_host_cmd23(card->host)) {
2122 if (mmc_card_mmc(card) ||
2123 (mmc_card_sd(card) &&
2124 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2125 md->flags |= MMC_BLK_CMD23;
2126 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002127
2128 if (mmc_card_mmc(card) &&
2129 md->flags & MMC_BLK_CMD23 &&
2130 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2131 card->ext_csd.rel_sectors)) {
2132 md->flags |= MMC_BLK_REL_WR;
2133 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2134 }
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002137
2138 err_putdisk:
2139 put_disk(md->disk);
2140 err_kfree:
2141 kfree(md);
2142 out:
2143 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Andrei Warkentin371a6892011-04-11 18:10:25 -05002146static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2147{
2148 sector_t size;
2149 struct mmc_blk_data *md;
2150
2151 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2152 /*
2153 * The EXT_CSD sector count is in number or 512 byte
2154 * sectors.
2155 */
2156 size = card->ext_csd.sectors;
2157 } else {
2158 /*
2159 * The CSD capacity field is in units of read_blkbits.
2160 * set_capacity takes units of 512 bytes.
2161 */
2162 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2163 }
2164
Johan Rudholmadd710e2011-12-02 08:51:06 +01002165 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2166 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002167 return md;
2168}
2169
2170static int mmc_blk_alloc_part(struct mmc_card *card,
2171 struct mmc_blk_data *md,
2172 unsigned int part_type,
2173 sector_t size,
2174 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002175 const char *subname,
2176 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177{
2178 char cap_str[10];
2179 struct mmc_blk_data *part_md;
2180
2181 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002182 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002183 if (IS_ERR(part_md))
2184 return PTR_ERR(part_md);
2185 part_md->part_type = part_type;
2186 list_add(&part_md->part, &md->part);
2187
2188 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2189 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302190 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002191 part_md->disk->disk_name, mmc_card_id(card),
2192 mmc_card_name(card), part_md->part_type, cap_str);
2193 return 0;
2194}
2195
Namjae Jeone0c368d2011-10-06 23:41:38 +09002196/* MMC Physical partitions consist of two boot partitions and
2197 * up to four general purpose partitions.
2198 * For each partition enabled in EXT_CSD a block device will be allocatedi
2199 * to provide access to the partition.
2200 */
2201
Andrei Warkentin371a6892011-04-11 18:10:25 -05002202static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2203{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002204 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002205
2206 if (!mmc_card_mmc(card))
2207 return 0;
2208
Namjae Jeone0c368d2011-10-06 23:41:38 +09002209 for (idx = 0; idx < card->nr_parts; idx++) {
2210 if (card->part[idx].size) {
2211 ret = mmc_blk_alloc_part(card, md,
2212 card->part[idx].part_cfg,
2213 card->part[idx].size >> 9,
2214 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002215 card->part[idx].name,
2216 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002217 if (ret)
2218 return ret;
2219 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 }
2221
2222 return ret;
2223}
2224
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225static void mmc_blk_remove_req(struct mmc_blk_data *md)
2226{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002227 struct mmc_card *card;
2228
Andrei Warkentin371a6892011-04-11 18:10:25 -05002229 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002230 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002231 device_remove_file(disk_to_dev(md->disk),
2232 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002233 if (md->disk->flags & GENHD_FL_UP) {
2234 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002235 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2236 card->ext_csd.boot_ro_lockable)
2237 device_remove_file(disk_to_dev(md->disk),
2238 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002239
2240 /* Stop new requests from getting into the queue */
2241 del_gendisk(md->disk);
2242 }
2243
2244 /* Then flush out any already in there */
2245 mmc_cleanup_queue(&md->queue);
2246 mmc_blk_put(md);
2247 }
2248}
2249
2250static void mmc_blk_remove_parts(struct mmc_card *card,
2251 struct mmc_blk_data *md)
2252{
2253 struct list_head *pos, *q;
2254 struct mmc_blk_data *part_md;
2255
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002256 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002257 list_for_each_safe(pos, q, &md->part) {
2258 part_md = list_entry(pos, struct mmc_blk_data, part);
2259 list_del(pos);
2260 mmc_blk_remove_req(part_md);
2261 }
2262}
2263
2264static int mmc_add_disk(struct mmc_blk_data *md)
2265{
2266 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002267 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002268
2269 add_disk(md->disk);
2270 md->force_ro.show = force_ro_show;
2271 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302272 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002273 md->force_ro.attr.name = "force_ro";
2274 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2275 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2276 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002277 goto force_ro_fail;
2278
2279 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2280 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002281 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002282
2283 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2284 mode = S_IRUGO;
2285 else
2286 mode = S_IRUGO | S_IWUSR;
2287
2288 md->power_ro_lock.show = power_ro_lock_show;
2289 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002290 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002291 md->power_ro_lock.attr.mode = mode;
2292 md->power_ro_lock.attr.name =
2293 "ro_lock_until_next_power_on";
2294 ret = device_create_file(disk_to_dev(md->disk),
2295 &md->power_ro_lock);
2296 if (ret)
2297 goto power_ro_lock_fail;
2298 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002299
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002300 md->num_wr_reqs_to_start_packing.show =
2301 num_wr_reqs_to_start_packing_show;
2302 md->num_wr_reqs_to_start_packing.store =
2303 num_wr_reqs_to_start_packing_store;
2304 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2305 md->num_wr_reqs_to_start_packing.attr.name =
2306 "num_wr_reqs_to_start_packing";
2307 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2308 ret = device_create_file(disk_to_dev(md->disk),
2309 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002310 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002311 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002312
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002313 md->min_sectors_to_check_bkops_status.show =
2314 min_sectors_to_check_bkops_status_show;
2315 md->min_sectors_to_check_bkops_status.store =
2316 min_sectors_to_check_bkops_status_store;
2317 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2318 md->min_sectors_to_check_bkops_status.attr.name =
2319 "min_sectors_to_check_bkops_status";
2320 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2321 ret = device_create_file(disk_to_dev(md->disk),
2322 &md->min_sectors_to_check_bkops_status);
2323 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002324 goto min_sectors_to_check_bkops_status_fails;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002325
Johan Rudholmadd710e2011-12-02 08:51:06 +01002326 return ret;
2327
Maya Erez8d006d32012-10-30 09:02:39 +02002328min_sectors_to_check_bkops_status_fails:
2329 device_remove_file(disk_to_dev(md->disk),
2330 &md->num_wr_reqs_to_start_packing);
2331num_wr_reqs_to_start_packing_fail:
2332 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002333power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002334 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002335force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002336 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002337
2338 return ret;
2339}
2340
Chris Ballc59d4472011-11-11 22:01:43 -05002341#define CID_MANFID_SANDISK 0x2
2342#define CID_MANFID_TOSHIBA 0x11
2343#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302344#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002345
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002346static const struct mmc_fixup blk_fixups[] =
2347{
Chris Ballc59d4472011-11-11 22:01:43 -05002348 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2349 MMC_QUIRK_INAND_CMD38),
2350 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2351 MMC_QUIRK_INAND_CMD38),
2352 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2353 MMC_QUIRK_INAND_CMD38),
2354 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2355 MMC_QUIRK_INAND_CMD38),
2356 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2357 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002358
2359 /*
2360 * Some MMC cards experience performance degradation with CMD23
2361 * instead of CMD12-bounded multiblock transfers. For now we'll
2362 * black list what's bad...
2363 * - Certain Toshiba cards.
2364 *
2365 * N.B. This doesn't affect SD cards.
2366 */
Chris Ballc59d4472011-11-11 22:01:43 -05002367 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002368 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002369 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002370 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002371 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002372 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002373
2374 /*
2375 * Some Micron MMC cards needs longer data read timeout than
2376 * indicated in CSD.
2377 */
Chris Ballc59d4472011-11-11 22:01:43 -05002378 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002379 MMC_QUIRK_LONG_READ_TIME),
2380
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302381 /* Some INAND MCP devices advertise incorrect timeout values */
2382 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2383 MMC_QUIRK_INAND_DATA_TIMEOUT),
2384
Ian Chenc4856122012-11-01 13:26:21 +05302385 /*
2386 * On these Samsung MoviNAND parts, performing secure erase or
2387 * secure trim can result in unrecoverable corruption due to a
2388 * firmware bug.
2389 */
2390 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2391 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2392 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2393 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2394 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2395 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2396 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2397 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2398 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2399 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2400 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2401 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2402 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2403 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2404 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2405 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2406
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002407 END_FIXUP
2408};
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410static int mmc_blk_probe(struct mmc_card *card)
2411{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002412 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002413 char cap_str[10];
2414
Pierre Ossman912490d2005-05-21 10:27:02 +01002415 /*
2416 * Check that the card supports the command class(es) we need.
2417 */
2418 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return -ENODEV;
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 md = mmc_blk_alloc(card);
2422 if (IS_ERR(md))
2423 return PTR_ERR(md);
2424
Yi Li444122f2009-02-05 15:31:57 +08002425 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002426 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302427 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002429 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Andrei Warkentin371a6892011-04-11 18:10:25 -05002431 if (mmc_blk_alloc_parts(card, md))
2432 goto out;
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002435 mmc_fixup_device(card, blk_fixups);
2436
San Mehat148c57a2009-07-30 08:21:19 -07002437#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2438 mmc_set_bus_resume_policy(card->host, 1);
2439#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002440 if (mmc_add_disk(md))
2441 goto out;
2442
2443 list_for_each_entry(part_md, &md->part, part) {
2444 if (mmc_add_disk(part_md))
2445 goto out;
2446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return 0;
2448
2449 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002450 mmc_blk_remove_parts(card, md);
2451 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455static void mmc_blk_remove(struct mmc_card *card)
2456{
2457 struct mmc_blk_data *md = mmc_get_drvdata(card);
2458
Andrei Warkentin371a6892011-04-11 18:10:25 -05002459 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002460 mmc_claim_host(card->host);
2461 mmc_blk_part_switch(card, md);
2462 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002463 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002465#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2466 mmc_set_bus_resume_policy(card->host, 0);
2467#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
2470#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002471static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002473 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct mmc_blk_data *md = mmc_get_drvdata(card);
2475
2476 if (md) {
2477 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002478 list_for_each_entry(part_md, &md->part, part) {
2479 mmc_queue_suspend(&part_md->queue);
2480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482 return 0;
2483}
2484
2485static int mmc_blk_resume(struct mmc_card *card)
2486{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002487 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct mmc_blk_data *md = mmc_get_drvdata(card);
2489
2490 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002491 /*
2492 * Resume involves the card going into idle state,
2493 * so current partition is always the main one.
2494 */
2495 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002497 list_for_each_entry(part_md, &md->part, part) {
2498 mmc_queue_resume(&part_md->queue);
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501 return 0;
2502}
2503#else
2504#define mmc_blk_suspend NULL
2505#define mmc_blk_resume NULL
2506#endif
2507
2508static struct mmc_driver mmc_driver = {
2509 .drv = {
2510 .name = "mmcblk",
2511 },
2512 .probe = mmc_blk_probe,
2513 .remove = mmc_blk_remove,
2514 .suspend = mmc_blk_suspend,
2515 .resume = mmc_blk_resume,
2516};
2517
2518static int __init mmc_blk_init(void)
2519{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002520 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002522 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2523 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2524
2525 max_devices = 256 / perdev_minors;
2526
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002527 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2528 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002531 res = mmc_register_driver(&mmc_driver);
2532 if (res)
2533 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002535 return 0;
2536 out2:
2537 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 out:
2539 return res;
2540}
2541
2542static void __exit mmc_blk_exit(void)
2543{
2544 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002545 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548module_init(mmc_blk_init);
2549module_exit(mmc_blk_exit);
2550
2551MODULE_LICENSE("GPL");
2552MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2553