blob: bb6dfa27a4af9f208362b198bbebe9da7be6bd73 [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
1412 /*
1413 * In case the packing control is not supported by the host, it should
1414 * not have an effect on the write packing. Therefore we have to enable
1415 * the write packing
1416 */
1417 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1418 mq->wr_packing_enabled = true;
1419 return;
1420 }
1421
1422 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1423 if (mq->num_of_potential_packed_wr_reqs >
1424 mq->num_wr_reqs_to_start_packing)
1425 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001426 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001427 return;
1428 }
1429
1430 data_dir = rq_data_dir(req);
1431
1432 if (data_dir == READ) {
1433 mq->num_of_potential_packed_wr_reqs = 0;
1434 mq->wr_packing_enabled = false;
1435 return;
1436 } else if (data_dir == WRITE) {
1437 mq->num_of_potential_packed_wr_reqs++;
1438 }
1439
1440 if (mq->num_of_potential_packed_wr_reqs >
1441 mq->num_wr_reqs_to_start_packing)
1442 mq->wr_packing_enabled = true;
1443
1444}
1445
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001446struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1447{
1448 if (!card)
1449 return NULL;
1450
1451 return &card->wr_pack_stats;
1452}
1453EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1454
1455void mmc_blk_init_packed_statistics(struct mmc_card *card)
1456{
1457 int max_num_of_packed_reqs = 0;
1458
1459 if (!card || !card->wr_pack_stats.packing_events)
1460 return;
1461
1462 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1463
1464 spin_lock(&card->wr_pack_stats.lock);
1465 memset(card->wr_pack_stats.packing_events, 0,
1466 (max_num_of_packed_reqs + 1) *
1467 sizeof(*card->wr_pack_stats.packing_events));
1468 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1469 sizeof(card->wr_pack_stats.pack_stop_reason));
1470 card->wr_pack_stats.enabled = true;
1471 spin_unlock(&card->wr_pack_stats.lock);
1472}
1473EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1474
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001475void print_mmc_packing_stats(struct mmc_card *card)
1476{
1477 int i;
1478 int max_num_of_packed_reqs = 0;
1479
1480 if ((!card) || (!card->wr_pack_stats.packing_events))
1481 return;
1482
1483 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1484
1485 spin_lock(&card->wr_pack_stats.lock);
1486
1487 pr_info("%s: write packing statistics:\n",
1488 mmc_hostname(card->host));
1489
1490 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1491 if (card->wr_pack_stats.packing_events[i] != 0)
1492 pr_info("%s: Packed %d reqs - %d times\n",
1493 mmc_hostname(card->host), i,
1494 card->wr_pack_stats.packing_events[i]);
1495 }
1496
1497 pr_info("%s: stopped packing due to the following reasons:\n",
1498 mmc_hostname(card->host));
1499
1500 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1501 pr_info("%s: %d times: exceedmax num of segments\n",
1502 mmc_hostname(card->host),
1503 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1504 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1505 pr_info("%s: %d times: exceeding the max num of sectors\n",
1506 mmc_hostname(card->host),
1507 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1508 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1509 pr_info("%s: %d times: wrong data direction\n",
1510 mmc_hostname(card->host),
1511 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1512 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1513 pr_info("%s: %d times: flush or discard\n",
1514 mmc_hostname(card->host),
1515 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1516 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1517 pr_info("%s: %d times: empty queue\n",
1518 mmc_hostname(card->host),
1519 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1520 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1521 pr_info("%s: %d times: rel write\n",
1522 mmc_hostname(card->host),
1523 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1524 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1525 pr_info("%s: %d times: Threshold\n",
1526 mmc_hostname(card->host),
1527 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1528
1529 spin_unlock(&card->wr_pack_stats.lock);
1530}
1531EXPORT_SYMBOL(print_mmc_packing_stats);
1532
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001533static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1534{
1535 struct request_queue *q = mq->queue;
1536 struct mmc_card *card = mq->card;
1537 struct request *cur = req, *next = NULL;
1538 struct mmc_blk_data *md = mq->data;
1539 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1540 unsigned int req_sectors = 0, phys_segments = 0;
1541 unsigned int max_blk_count, max_phys_segs;
1542 u8 put_back = 0;
1543 u8 max_packed_rw = 0;
1544 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001545 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001546
1547 mmc_blk_clear_packed(mq->mqrq_cur);
1548
1549 if (!(md->flags & MMC_BLK_CMD23) ||
1550 !card->ext_csd.packed_event_en)
1551 goto no_packed;
1552
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001553 if (!mq->wr_packing_enabled)
1554 goto no_packed;
1555
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001556 if ((rq_data_dir(cur) == WRITE) &&
1557 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1558 max_packed_rw = card->ext_csd.max_packed_writes;
1559
1560 if (max_packed_rw == 0)
1561 goto no_packed;
1562
1563 if (mmc_req_rel_wr(cur) &&
1564 (md->flags & MMC_BLK_REL_WR) &&
1565 !en_rel_wr)
1566 goto no_packed;
1567
1568 if (mmc_large_sec(card) &&
1569 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1570 goto no_packed;
1571
1572 max_blk_count = min(card->host->max_blk_count,
1573 card->host->max_req_size >> 9);
1574 if (unlikely(max_blk_count > 0xffff))
1575 max_blk_count = 0xffff;
1576
1577 max_phys_segs = queue_max_segments(q);
1578 req_sectors += blk_rq_sectors(cur);
1579 phys_segments += cur->nr_phys_segments;
1580
1581 if (rq_data_dir(cur) == WRITE) {
1582 req_sectors++;
1583 phys_segments++;
1584 }
1585
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001586 spin_lock(&stats->lock);
1587
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001588 while (reqs < max_packed_rw - 1) {
1589 spin_lock_irq(q->queue_lock);
1590 next = blk_fetch_request(q);
1591 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001592 if (!next) {
1593 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001594 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001595 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001596
1597 if (mmc_large_sec(card) &&
1598 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001599 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001600 put_back = 1;
1601 break;
1602 }
1603
1604 if (next->cmd_flags & REQ_DISCARD ||
1605 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001606 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001607 put_back = 1;
1608 break;
1609 }
1610
1611 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001612 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001613 put_back = 1;
1614 break;
1615 }
1616
1617 if (mmc_req_rel_wr(next) &&
1618 (md->flags & MMC_BLK_REL_WR) &&
1619 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001620 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001621 put_back = 1;
1622 break;
1623 }
1624
1625 req_sectors += blk_rq_sectors(next);
1626 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001627 if (stats->enabled)
1628 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001629 put_back = 1;
1630 break;
1631 }
1632
1633 phys_segments += next->nr_phys_segments;
1634 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001635 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001636 put_back = 1;
1637 break;
1638 }
1639
Maya Erez5f360692012-10-10 03:47:54 +02001640 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001641 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5f360692012-10-10 03:47:54 +02001642 if (card->ext_csd.bkops_en)
1643 card->bkops_info.sectors_changed +=
1644 blk_rq_sectors(next);
1645 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001646 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1647 cur = next;
1648 reqs++;
1649 }
1650
1651 if (put_back) {
1652 spin_lock_irq(q->queue_lock);
1653 blk_requeue_request(q, next);
1654 spin_unlock_irq(q->queue_lock);
1655 }
1656
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001657 if (stats->enabled) {
1658 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1659 stats->packing_events[reqs + 1]++;
1660 if (reqs + 1 == max_packed_rw)
1661 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1662 }
1663
1664 spin_unlock(&stats->lock);
1665
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001666 if (reqs > 0) {
1667 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1668 mq->mqrq_cur->packed_num = ++reqs;
1669 mq->mqrq_cur->packed_retries = reqs;
1670 return reqs;
1671 }
1672
1673no_packed:
1674 mmc_blk_clear_packed(mq->mqrq_cur);
1675 return 0;
1676}
1677
1678static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1679 struct mmc_card *card,
1680 struct mmc_queue *mq)
1681{
1682 struct mmc_blk_request *brq = &mqrq->brq;
1683 struct request *req = mqrq->req;
1684 struct request *prq;
1685 struct mmc_blk_data *md = mq->data;
1686 bool do_rel_wr, do_data_tag;
1687 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1688 u8 i = 1;
1689
1690 mqrq->packed_cmd = MMC_PACKED_WRITE;
1691 mqrq->packed_blocks = 0;
1692 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1693
1694 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1695 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1696 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1697
1698 /*
1699 * Argument for each entry of packed group
1700 */
1701 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1702 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1703 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1704 (prq->cmd_flags & REQ_META) &&
1705 (rq_data_dir(prq) == WRITE) &&
1706 ((brq->data.blocks * brq->data.blksz) >=
1707 card->ext_csd.data_tag_unit_size);
1708 /* Argument of CMD23 */
1709 packed_cmd_hdr[(i * 2)] =
1710 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1711 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1712 blk_rq_sectors(prq);
1713 /* Argument of CMD18 or CMD25 */
1714 packed_cmd_hdr[((i * 2)) + 1] =
1715 mmc_card_blockaddr(card) ?
1716 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1717 mqrq->packed_blocks += blk_rq_sectors(prq);
1718 i++;
1719 }
1720
1721 memset(brq, 0, sizeof(struct mmc_blk_request));
1722 brq->mrq.cmd = &brq->cmd;
1723 brq->mrq.data = &brq->data;
1724 brq->mrq.sbc = &brq->sbc;
1725 brq->mrq.stop = &brq->stop;
1726
1727 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1728 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1729 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1730
1731 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1732 brq->cmd.arg = blk_rq_pos(req);
1733 if (!mmc_card_blockaddr(card))
1734 brq->cmd.arg <<= 9;
1735 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1736
1737 brq->data.blksz = 512;
1738 brq->data.blocks = mqrq->packed_blocks + 1;
1739 brq->data.flags |= MMC_DATA_WRITE;
1740
1741 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1742 brq->stop.arg = 0;
1743 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1744
1745 mmc_set_data_timeout(&brq->data, card);
1746
1747 brq->data.sg = mqrq->sg;
1748 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1749
1750 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001751
1752 /*
1753 * This is intended for packed commands tests usage - in case these
1754 * functions are not in use the respective pointers are NULL
1755 */
1756 if (mq->err_check_fn)
1757 mqrq->mmc_active.err_check = mq->err_check_fn;
1758 else
1759 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1760
1761 if (mq->packed_test_fn)
1762 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001763
1764 mmc_queue_bounce_pre(mqrq);
1765}
1766
Adrian Hunter67716322011-08-29 16:42:15 +03001767static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1768 struct mmc_blk_request *brq, struct request *req,
1769 int ret)
1770{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001771 struct mmc_queue_req *mq_rq;
1772 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1773
Adrian Hunter67716322011-08-29 16:42:15 +03001774 /*
1775 * If this is an SD card and we're writing, we can first
1776 * mark the known good sectors as ok.
1777 *
1778 * If the card is not SD, we can still ok written sectors
1779 * as reported by the controller (which might be less than
1780 * the real number of written sectors, but never more).
1781 */
1782 if (mmc_card_sd(card)) {
1783 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301784 if (!brq->data.fault_injected) {
1785 blocks = mmc_sd_num_wr_blocks(card);
1786 if (blocks != (u32)-1)
1787 ret = blk_end_request(req, 0, blocks << 9);
1788 } else
1789 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001790 } else {
1791 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1792 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1793 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001794 return ret;
1795}
1796
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001797static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1798{
1799 struct request *prq;
1800 int idx = mq_rq->packed_fail_idx, i = 0;
1801 int ret = 0;
1802
1803 while (!list_empty(&mq_rq->packed_list)) {
1804 prq = list_entry_rq(mq_rq->packed_list.next);
1805 if (idx == i) {
1806 /* retry from error index */
1807 mq_rq->packed_num -= idx;
1808 mq_rq->req = prq;
1809 ret = 1;
1810
1811 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1812 list_del_init(&prq->queuelist);
1813 mmc_blk_clear_packed(mq_rq);
1814 }
1815 return ret;
1816 }
1817 list_del_init(&prq->queuelist);
1818 blk_end_request(prq, 0, blk_rq_bytes(prq));
1819 i++;
1820 }
1821
1822 mmc_blk_clear_packed(mq_rq);
1823 return ret;
1824}
1825static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1826{
1827 struct request *prq;
1828
1829 while (!list_empty(&mq_rq->packed_list)) {
1830 prq = list_entry_rq(mq_rq->packed_list.next);
1831 list_del_init(&prq->queuelist);
1832 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1833 }
1834
1835 mmc_blk_clear_packed(mq_rq);
1836}
1837
1838static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1839 struct mmc_queue_req *mq_rq)
1840{
1841 struct request *prq;
1842 struct request_queue *q = mq->queue;
1843
1844 while (!list_empty(&mq_rq->packed_list)) {
1845 prq = list_entry_rq(mq_rq->packed_list.prev);
1846 if (prq->queuelist.prev != &mq_rq->packed_list) {
1847 list_del_init(&prq->queuelist);
1848 spin_lock_irq(q->queue_lock);
1849 blk_requeue_request(mq->queue, prq);
1850 spin_unlock_irq(q->queue_lock);
1851 } else {
1852 list_del_init(&prq->queuelist);
1853 }
1854 }
1855
1856 mmc_blk_clear_packed(mq_rq);
1857}
1858
Per Forlinee8a43a2011-07-01 18:55:33 +02001859static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001860{
1861 struct mmc_blk_data *md = mq->data;
1862 struct mmc_card *card = md->queue.card;
1863 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001864 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001865 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001866 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001867 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001868 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001869 const u8 packed_num = 2;
1870 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001871
1872 if (!rqc && !mq->mqrq_prev->req)
1873 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001874
Maya Erez5f360692012-10-10 03:47:54 +02001875 if (rqc) {
1876 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1877 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001878 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001879 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001880
Per Forlin54d49d772011-07-01 18:55:29 +02001881 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001882 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001883 if (reqs >= packed_num)
1884 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1885 card, mq);
1886 else
1887 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001888 areq = &mq->mqrq_cur->mmc_active;
1889 } else
1890 areq = NULL;
1891 areq = mmc_start_req(card->host, areq, (int *) &status);
1892 if (!areq)
1893 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001894
Per Forlinee8a43a2011-07-01 18:55:33 +02001895 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1896 brq = &mq_rq->brq;
1897 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001898 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001899 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001900
Per Forlind78d4a82011-07-01 18:55:30 +02001901 switch (status) {
1902 case MMC_BLK_SUCCESS:
1903 case MMC_BLK_PARTIAL:
1904 /*
1905 * A block was successfully transferred.
1906 */
Adrian Hunter67716322011-08-29 16:42:15 +03001907 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001908
1909 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1910 ret = mmc_blk_end_packed_req(mq_rq);
1911 break;
1912 } else {
1913 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001914 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001915 }
1916
Adrian Hunter67716322011-08-29 16:42:15 +03001917 /*
1918 * If the blk_end_request function returns non-zero even
1919 * though all data has been transferred and no errors
1920 * were returned by the host controller, it's a bug.
1921 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001922 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301923 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001924 __func__, blk_rq_bytes(req),
1925 brq->data.bytes_xfered);
1926 rqc = NULL;
1927 goto cmd_abort;
1928 }
Per Forlind78d4a82011-07-01 18:55:30 +02001929 break;
1930 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001931 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1932 if (!mmc_blk_reset(md, card->host, type))
1933 break;
1934 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001935 case MMC_BLK_RETRY:
1936 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001937 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001938 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001939 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001940 if (!mmc_blk_reset(md, card->host, type))
1941 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001942 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001943 case MMC_BLK_DATA_ERR: {
1944 int err;
1945
1946 err = mmc_blk_reset(md, card->host, type);
1947 if (!err)
1948 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001949 if (err == -ENODEV ||
1950 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001951 goto cmd_abort;
1952 /* Fall through */
1953 }
1954 case MMC_BLK_ECC_ERR:
1955 if (brq->data.blocks > 1) {
1956 /* Redo read one sector at a time */
1957 pr_warning("%s: retrying using single block read\n",
1958 req->rq_disk->disk_name);
1959 disable_multi = 1;
1960 break;
1961 }
Per Forlind78d4a82011-07-01 18:55:30 +02001962 /*
1963 * After an error, we redo I/O one sector at a
1964 * time, so we only reach here after trying to
1965 * read a single sector.
1966 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301967 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001968 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001969 if (!ret)
1970 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001971 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301972 case MMC_BLK_NOMEDIUM:
1973 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001974 }
1975
Per Forlinee8a43a2011-07-01 18:55:33 +02001976 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001977 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1978 /*
1979 * In case of a incomplete request
1980 * prepare it again and resend.
1981 */
1982 mmc_blk_rw_rq_prep(mq_rq, card,
1983 disable_multi, mq);
1984 mmc_start_req(card->host,
1985 &mq_rq->mmc_active, NULL);
1986 } else {
1987 if (!mq_rq->packed_retries)
1988 goto cmd_abort;
1989 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1990 mmc_start_req(card->host,
1991 &mq_rq->mmc_active, NULL);
1992 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } while (ret);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 1;
1997
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001998 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001999 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2000 if (mmc_card_removed(card))
2001 req->cmd_flags |= REQ_QUIET;
2002 while (ret)
2003 ret = blk_end_request(req, -EIO,
2004 blk_rq_cur_bytes(req));
2005 } else {
2006 mmc_blk_abort_packed_req(mq_rq);
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Per Forlinee8a43a2011-07-01 18:55:33 +02002009 start_new_req:
2010 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002011 /*
2012 * If current request is packed, it needs to put back.
2013 */
2014 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2015 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2016
Per Forlinee8a43a2011-07-01 18:55:33 +02002017 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2018 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2019 }
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return 0;
2022}
2023
Adrian Hunterbd788c92010-08-11 14:17:47 -07002024static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2025{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002026 int ret;
2027 struct mmc_blk_data *md = mq->data;
2028 struct mmc_card *card = md->queue.card;
2029
San Mehat148c57a2009-07-30 08:21:19 -07002030#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2031 if (mmc_bus_needs_resume(card->host)) {
2032 mmc_resume_bus(card->host);
2033 mmc_blk_set_blksize(md, card);
2034 }
2035#endif
2036
Per Forlinee8a43a2011-07-01 18:55:33 +02002037 if (req && !mq->mqrq_prev->req)
2038 /* claim host only for the first request */
2039 mmc_claim_host(card->host);
2040
Andrei Warkentin371a6892011-04-11 18:10:25 -05002041 ret = mmc_blk_part_switch(card, md);
2042 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002043 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302044 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002045 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002046 ret = 0;
2047 goto out;
2048 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002049
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002050 mmc_blk_write_packing_control(mq, req);
2051
Maya Erez463bb952012-05-24 23:46:29 +03002052 if (req && req->cmd_flags & REQ_SANITIZE) {
2053 /* complete ongoing async transfer before issuing sanitize */
2054 if (card->host && card->host->areq)
2055 mmc_blk_issue_rw_rq(mq, NULL);
2056 ret = mmc_blk_issue_sanitize_rq(mq, req);
2057 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002058 /* complete ongoing async transfer before issuing discard */
2059 if (card->host->areq)
2060 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002061 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002062 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002063 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002064 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002065 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002066 /* complete ongoing async transfer before issuing flush */
2067 if (card->host->areq)
2068 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002069 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002070 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002071 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002072 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002073
Andrei Warkentin371a6892011-04-11 18:10:25 -05002074out:
Per Forlinee8a43a2011-07-01 18:55:33 +02002075 if (!req)
2076 /* release host only when there are no more requests */
2077 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002078 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Russell Kinga6f6c962006-01-03 22:38:44 +00002081static inline int mmc_blk_readonly(struct mmc_card *card)
2082{
2083 return mmc_card_readonly(card) ||
2084 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2085}
2086
Andrei Warkentin371a6892011-04-11 18:10:25 -05002087static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2088 struct device *parent,
2089 sector_t size,
2090 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002091 const char *subname,
2092 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 struct mmc_blk_data *md;
2095 int devidx, ret;
2096
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002097 devidx = find_first_zero_bit(dev_use, max_devices);
2098 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return ERR_PTR(-ENOSPC);
2100 __set_bit(devidx, dev_use);
2101
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002102 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002103 if (!md) {
2104 ret = -ENOMEM;
2105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002107
Russell Kinga6f6c962006-01-03 22:38:44 +00002108 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002109 * !subname implies we are creating main mmc_blk_data that will be
2110 * associated with mmc_card with mmc_set_drvdata. Due to device
2111 * partitions, devidx will not coincide with a per-physical card
2112 * index anymore so we keep track of a name index.
2113 */
2114 if (!subname) {
2115 md->name_idx = find_first_zero_bit(name_use, max_devices);
2116 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002117 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002118 md->name_idx = ((struct mmc_blk_data *)
2119 dev_to_disk(parent)->private_data)->name_idx;
2120
Johan Rudholmadd710e2011-12-02 08:51:06 +01002121 md->area_type = area_type;
2122
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002123 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002124 * Set the read-only status based on the supported commands
2125 * and the write protect switch.
2126 */
2127 md->read_only = mmc_blk_readonly(card);
2128
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002129 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002130 if (md->disk == NULL) {
2131 ret = -ENOMEM;
2132 goto err_kfree;
2133 }
2134
2135 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002136 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002137 md->usage = 1;
2138
Adrian Hunterd09408a2011-06-23 13:40:28 +03002139 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002140 if (ret)
2141 goto err_putdisk;
2142
Russell Kinga6f6c962006-01-03 22:38:44 +00002143 md->queue.issue_fn = mmc_blk_issue_rq;
2144 md->queue.data = md;
2145
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002146 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002147 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002148 md->disk->fops = &mmc_bdops;
2149 md->disk->private_data = md;
2150 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002151 md->disk->driverfs_dev = parent;
2152 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002153 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002154
2155 /*
2156 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2157 *
2158 * - be set for removable media with permanent block devices
2159 * - be unset for removable block devices with permanent media
2160 *
2161 * Since MMC block devices clearly fall under the second
2162 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2163 * should use the block device creation/destruction hotplug
2164 * messages to tell when the card is present.
2165 */
2166
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002167 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2168 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002169
Martin K. Petersene1defc42009-05-22 17:17:49 -04002170 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002172
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002173 if (mmc_host_cmd23(card->host)) {
2174 if (mmc_card_mmc(card) ||
2175 (mmc_card_sd(card) &&
2176 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2177 md->flags |= MMC_BLK_CMD23;
2178 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002179
2180 if (mmc_card_mmc(card) &&
2181 md->flags & MMC_BLK_CMD23 &&
2182 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2183 card->ext_csd.rel_sectors)) {
2184 md->flags |= MMC_BLK_REL_WR;
2185 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2186 }
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002189
2190 err_putdisk:
2191 put_disk(md->disk);
2192 err_kfree:
2193 kfree(md);
2194 out:
2195 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Andrei Warkentin371a6892011-04-11 18:10:25 -05002198static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2199{
2200 sector_t size;
2201 struct mmc_blk_data *md;
2202
2203 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2204 /*
2205 * The EXT_CSD sector count is in number or 512 byte
2206 * sectors.
2207 */
2208 size = card->ext_csd.sectors;
2209 } else {
2210 /*
2211 * The CSD capacity field is in units of read_blkbits.
2212 * set_capacity takes units of 512 bytes.
2213 */
2214 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2215 }
2216
Johan Rudholmadd710e2011-12-02 08:51:06 +01002217 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2218 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002219 return md;
2220}
2221
2222static int mmc_blk_alloc_part(struct mmc_card *card,
2223 struct mmc_blk_data *md,
2224 unsigned int part_type,
2225 sector_t size,
2226 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002227 const char *subname,
2228 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002229{
2230 char cap_str[10];
2231 struct mmc_blk_data *part_md;
2232
2233 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002234 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 if (IS_ERR(part_md))
2236 return PTR_ERR(part_md);
2237 part_md->part_type = part_type;
2238 list_add(&part_md->part, &md->part);
2239
2240 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2241 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302242 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002243 part_md->disk->disk_name, mmc_card_id(card),
2244 mmc_card_name(card), part_md->part_type, cap_str);
2245 return 0;
2246}
2247
Namjae Jeone0c368d2011-10-06 23:41:38 +09002248/* MMC Physical partitions consist of two boot partitions and
2249 * up to four general purpose partitions.
2250 * For each partition enabled in EXT_CSD a block device will be allocatedi
2251 * to provide access to the partition.
2252 */
2253
Andrei Warkentin371a6892011-04-11 18:10:25 -05002254static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2255{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002256 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002257
2258 if (!mmc_card_mmc(card))
2259 return 0;
2260
Namjae Jeone0c368d2011-10-06 23:41:38 +09002261 for (idx = 0; idx < card->nr_parts; idx++) {
2262 if (card->part[idx].size) {
2263 ret = mmc_blk_alloc_part(card, md,
2264 card->part[idx].part_cfg,
2265 card->part[idx].size >> 9,
2266 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002267 card->part[idx].name,
2268 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002269 if (ret)
2270 return ret;
2271 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002272 }
2273
2274 return ret;
2275}
2276
Andrei Warkentin371a6892011-04-11 18:10:25 -05002277static void mmc_blk_remove_req(struct mmc_blk_data *md)
2278{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002279 struct mmc_card *card;
2280
Andrei Warkentin371a6892011-04-11 18:10:25 -05002281 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002282 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002283 device_remove_file(disk_to_dev(md->disk),
2284 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002285 if (md->disk->flags & GENHD_FL_UP) {
2286 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002287 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2288 card->ext_csd.boot_ro_lockable)
2289 device_remove_file(disk_to_dev(md->disk),
2290 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002291
2292 /* Stop new requests from getting into the queue */
2293 del_gendisk(md->disk);
2294 }
2295
2296 /* Then flush out any already in there */
2297 mmc_cleanup_queue(&md->queue);
2298 mmc_blk_put(md);
2299 }
2300}
2301
2302static void mmc_blk_remove_parts(struct mmc_card *card,
2303 struct mmc_blk_data *md)
2304{
2305 struct list_head *pos, *q;
2306 struct mmc_blk_data *part_md;
2307
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002308 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 list_for_each_safe(pos, q, &md->part) {
2310 part_md = list_entry(pos, struct mmc_blk_data, part);
2311 list_del(pos);
2312 mmc_blk_remove_req(part_md);
2313 }
2314}
2315
2316static int mmc_add_disk(struct mmc_blk_data *md)
2317{
2318 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002319 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002320
2321 add_disk(md->disk);
2322 md->force_ro.show = force_ro_show;
2323 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302324 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002325 md->force_ro.attr.name = "force_ro";
2326 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2327 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2328 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002329 goto force_ro_fail;
2330
2331 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2332 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002333 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002334
2335 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2336 mode = S_IRUGO;
2337 else
2338 mode = S_IRUGO | S_IWUSR;
2339
2340 md->power_ro_lock.show = power_ro_lock_show;
2341 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002342 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002343 md->power_ro_lock.attr.mode = mode;
2344 md->power_ro_lock.attr.name =
2345 "ro_lock_until_next_power_on";
2346 ret = device_create_file(disk_to_dev(md->disk),
2347 &md->power_ro_lock);
2348 if (ret)
2349 goto power_ro_lock_fail;
2350 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002351
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002352 md->num_wr_reqs_to_start_packing.show =
2353 num_wr_reqs_to_start_packing_show;
2354 md->num_wr_reqs_to_start_packing.store =
2355 num_wr_reqs_to_start_packing_store;
2356 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2357 md->num_wr_reqs_to_start_packing.attr.name =
2358 "num_wr_reqs_to_start_packing";
2359 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2360 ret = device_create_file(disk_to_dev(md->disk),
2361 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002362 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002363 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002364
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002365 md->min_sectors_to_check_bkops_status.show =
2366 min_sectors_to_check_bkops_status_show;
2367 md->min_sectors_to_check_bkops_status.store =
2368 min_sectors_to_check_bkops_status_store;
2369 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2370 md->min_sectors_to_check_bkops_status.attr.name =
2371 "min_sectors_to_check_bkops_status";
2372 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2373 ret = device_create_file(disk_to_dev(md->disk),
2374 &md->min_sectors_to_check_bkops_status);
2375 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002376 goto min_sectors_to_check_bkops_status_fails;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002377
Johan Rudholmadd710e2011-12-02 08:51:06 +01002378 return ret;
2379
Maya Erez8d006d32012-10-30 09:02:39 +02002380min_sectors_to_check_bkops_status_fails:
2381 device_remove_file(disk_to_dev(md->disk),
2382 &md->num_wr_reqs_to_start_packing);
2383num_wr_reqs_to_start_packing_fail:
2384 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002385power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002386 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002387force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002388 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002389
2390 return ret;
2391}
2392
Chris Ballc59d4472011-11-11 22:01:43 -05002393#define CID_MANFID_SANDISK 0x2
2394#define CID_MANFID_TOSHIBA 0x11
2395#define CID_MANFID_MICRON 0x13
2396
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002397static const struct mmc_fixup blk_fixups[] =
2398{
Chris Ballc59d4472011-11-11 22:01:43 -05002399 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2400 MMC_QUIRK_INAND_CMD38),
2401 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2402 MMC_QUIRK_INAND_CMD38),
2403 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2404 MMC_QUIRK_INAND_CMD38),
2405 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2406 MMC_QUIRK_INAND_CMD38),
2407 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2408 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002409
2410 /*
2411 * Some MMC cards experience performance degradation with CMD23
2412 * instead of CMD12-bounded multiblock transfers. For now we'll
2413 * black list what's bad...
2414 * - Certain Toshiba cards.
2415 *
2416 * N.B. This doesn't affect SD cards.
2417 */
Chris Ballc59d4472011-11-11 22:01:43 -05002418 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002419 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002420 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002421 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002422 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002423 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002424
2425 /*
2426 * Some Micron MMC cards needs longer data read timeout than
2427 * indicated in CSD.
2428 */
Chris Ballc59d4472011-11-11 22:01:43 -05002429 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002430 MMC_QUIRK_LONG_READ_TIME),
2431
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302432 /* Some INAND MCP devices advertise incorrect timeout values */
2433 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2434 MMC_QUIRK_INAND_DATA_TIMEOUT),
2435
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002436 END_FIXUP
2437};
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static int mmc_blk_probe(struct mmc_card *card)
2440{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002441 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002442 char cap_str[10];
2443
Pierre Ossman912490d2005-05-21 10:27:02 +01002444 /*
2445 * Check that the card supports the command class(es) we need.
2446 */
2447 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return -ENODEV;
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 md = mmc_blk_alloc(card);
2451 if (IS_ERR(md))
2452 return PTR_ERR(md);
2453
Yi Li444122f2009-02-05 15:31:57 +08002454 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002455 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302456 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002458 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Andrei Warkentin371a6892011-04-11 18:10:25 -05002460 if (mmc_blk_alloc_parts(card, md))
2461 goto out;
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002464 mmc_fixup_device(card, blk_fixups);
2465
San Mehat148c57a2009-07-30 08:21:19 -07002466#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2467 mmc_set_bus_resume_policy(card->host, 1);
2468#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002469 if (mmc_add_disk(md))
2470 goto out;
2471
2472 list_for_each_entry(part_md, &md->part, part) {
2473 if (mmc_add_disk(part_md))
2474 goto out;
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
2477
2478 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002479 mmc_blk_remove_parts(card, md);
2480 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484static void mmc_blk_remove(struct mmc_card *card)
2485{
2486 struct mmc_blk_data *md = mmc_get_drvdata(card);
2487
Andrei Warkentin371a6892011-04-11 18:10:25 -05002488 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002489 mmc_claim_host(card->host);
2490 mmc_blk_part_switch(card, md);
2491 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002492 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002494#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2495 mmc_set_bus_resume_policy(card->host, 0);
2496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
2499#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002500static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002502 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct mmc_blk_data *md = mmc_get_drvdata(card);
2504
2505 if (md) {
2506 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002507 list_for_each_entry(part_md, &md->part, part) {
2508 mmc_queue_suspend(&part_md->queue);
2509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 }
2511 return 0;
2512}
2513
2514static int mmc_blk_resume(struct mmc_card *card)
2515{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002516 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct mmc_blk_data *md = mmc_get_drvdata(card);
2518
2519 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002520 /*
2521 * Resume involves the card going into idle state,
2522 * so current partition is always the main one.
2523 */
2524 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002526 list_for_each_entry(part_md, &md->part, part) {
2527 mmc_queue_resume(&part_md->queue);
2528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530 return 0;
2531}
2532#else
2533#define mmc_blk_suspend NULL
2534#define mmc_blk_resume NULL
2535#endif
2536
2537static struct mmc_driver mmc_driver = {
2538 .drv = {
2539 .name = "mmcblk",
2540 },
2541 .probe = mmc_blk_probe,
2542 .remove = mmc_blk_remove,
2543 .suspend = mmc_blk_suspend,
2544 .resume = mmc_blk_resume,
2545};
2546
2547static int __init mmc_blk_init(void)
2548{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002549 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002551 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2552 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2553
2554 max_devices = 256 / perdev_minors;
2555
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002556 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2557 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002560 res = mmc_register_driver(&mmc_driver);
2561 if (res)
2562 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002564 return 0;
2565 out2:
2566 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 out:
2568 return res;
2569}
2570
2571static void __exit mmc_blk_exit(void)
2572{
2573 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002574 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577module_init(mmc_blk_init);
2578module_exit(mmc_blk_exit);
2579
2580MODULE_LICENSE("GPL");
2581MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2582