blob: b3cf4c7ddebf19e4a9fcdd09a7faa13a1e31db5b [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 Jeon121b8e82012-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 Brokhman10217bc2012-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 Jeon121b8e82012-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 Brokhman98e67812012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Yaniv Gardiab0b6b02012-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 Jeon121b8e82012-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 Jeon121b8e82012-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 Brokhman98e67812012-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 Gardiab0b6b02012-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 Erez5c46dad2012-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
Adrian Hunter28302812012-04-05 14:45:48 +03001005out_retry:
1006 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001007 goto retry;
1008 if (!err)
1009 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001010out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301011 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001012
Adrian Hunter49804542010-08-11 14:17:50 -07001013 return err ? 0 : 1;
1014}
1015
Maya Erez463bb952012-05-24 23:46:29 +03001016static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1017 struct request *req)
1018{
1019 struct mmc_blk_data *md = mq->data;
1020 struct mmc_card *card = md->queue.card;
1021 int err = 0;
1022
1023 BUG_ON(!card);
1024 BUG_ON(!card->host);
1025
1026 if (!(mmc_can_sanitize(card) &&
1027 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1028 pr_warning("%s: %s - SANITIZE is not supported\n",
1029 mmc_hostname(card->host), __func__);
1030 err = -EOPNOTSUPP;
1031 goto out;
1032 }
1033
1034 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1035 mmc_hostname(card->host), __func__);
1036
1037 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001038 EXT_CSD_SANITIZE_START, 1,
1039 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001040
1041 if (err)
1042 pr_err("%s: %s - mmc_switch() with "
1043 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1044 mmc_hostname(card->host), __func__, err);
1045
1046 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1047 __func__);
1048
1049out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301050 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001051
1052 return err ? 0 : 1;
1053}
1054
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001055static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1056{
1057 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001058 struct mmc_card *card = md->queue.card;
1059 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001060
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001061 ret = mmc_flush_cache(card);
1062 if (ret)
1063 ret = -EIO;
1064
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301065 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001066
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001067 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001068}
1069
1070/*
1071 * Reformat current write as a reliable write, supporting
1072 * both legacy and the enhanced reliable write MMC cards.
1073 * In each transfer we'll handle only as much as a single
1074 * reliable write can handle, thus finish the request in
1075 * partial completions.
1076 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001077static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1078 struct mmc_card *card,
1079 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001080{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001081 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1082 /* Legacy mode imposes restrictions on transfers. */
1083 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1084 brq->data.blocks = 1;
1085
1086 if (brq->data.blocks > card->ext_csd.rel_sectors)
1087 brq->data.blocks = card->ext_csd.rel_sectors;
1088 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1089 brq->data.blocks = 1;
1090 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001091}
1092
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001093#define CMD_ERRORS \
1094 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1095 R1_ADDRESS_ERROR | /* Misaligned address */ \
1096 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1097 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1098 R1_CC_ERROR | /* Card controller error */ \
1099 R1_ERROR) /* General/unknown error */
1100
Per Forlinee8a43a2011-07-01 18:55:33 +02001101static int mmc_blk_err_check(struct mmc_card *card,
1102 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001103{
Per Forlinee8a43a2011-07-01 18:55:33 +02001104 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1105 mmc_active);
1106 struct mmc_blk_request *brq = &mq_mrq->brq;
1107 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001108 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001109
1110 /*
1111 * sbc.error indicates a problem with the set block count
1112 * command. No data will have been transferred.
1113 *
1114 * cmd.error indicates a problem with the r/w command. No
1115 * data will have been transferred.
1116 *
1117 * stop.error indicates a problem with the stop command. Data
1118 * may have been transferred, or may still be transferring.
1119 */
Adrian Hunter67716322011-08-29 16:42:15 +03001120 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1121 brq->data.error) {
1122 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001123 case ERR_RETRY:
1124 return MMC_BLK_RETRY;
1125 case ERR_ABORT:
1126 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301127 case ERR_NOMEDIUM:
1128 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001129 case ERR_CONTINUE:
1130 break;
1131 }
1132 }
1133
1134 /*
1135 * Check for errors relating to the execution of the
1136 * initial command - such as address errors. No data
1137 * has been transferred.
1138 */
1139 if (brq->cmd.resp[0] & CMD_ERRORS) {
1140 pr_err("%s: r/w command failed, status = %#x\n",
1141 req->rq_disk->disk_name, brq->cmd.resp[0]);
1142 return MMC_BLK_ABORT;
1143 }
1144
1145 /*
1146 * Everything else is either success, or a data error of some
1147 * kind. If it was a write, we may have transitioned to
1148 * program mode, which we have to wait for it to complete.
1149 */
1150 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1151 u32 status;
1152 do {
1153 int err = get_card_status(card, &status, 5);
1154 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301155 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001156 req->rq_disk->disk_name, err);
1157 return MMC_BLK_CMD_ERR;
1158 }
1159 /*
1160 * Some cards mishandle the status bits,
1161 * so make sure to check both the busy
1162 * indication and the card state.
1163 */
1164 } while (!(status & R1_READY_FOR_DATA) ||
1165 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1166 }
1167
1168 if (brq->data.error) {
1169 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1170 req->rq_disk->disk_name, brq->data.error,
1171 (unsigned)blk_rq_pos(req),
1172 (unsigned)blk_rq_sectors(req),
1173 brq->cmd.resp[0], brq->stop.resp[0]);
1174
1175 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001176 if (ecc_err)
1177 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001178 return MMC_BLK_DATA_ERR;
1179 } else {
1180 return MMC_BLK_CMD_ERR;
1181 }
1182 }
1183
Adrian Hunter67716322011-08-29 16:42:15 +03001184 if (!brq->data.bytes_xfered)
1185 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001186
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001187 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1188 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1189 return MMC_BLK_PARTIAL;
1190 else
1191 return MMC_BLK_SUCCESS;
1192 }
1193
Adrian Hunter67716322011-08-29 16:42:15 +03001194 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1195 return MMC_BLK_PARTIAL;
1196
1197 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001198}
1199
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001200static int mmc_blk_packed_err_check(struct mmc_card *card,
1201 struct mmc_async_req *areq)
1202{
1203 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1204 mmc_active);
1205 struct request *req = mq_rq->req;
1206 int err, check, status;
1207 u8 ext_csd[512];
1208
1209 mq_rq->packed_retries--;
1210 check = mmc_blk_err_check(card, areq);
1211 err = get_card_status(card, &status, 0);
1212 if (err) {
1213 pr_err("%s: error %d sending status command\n",
1214 req->rq_disk->disk_name, err);
1215 return MMC_BLK_ABORT;
1216 }
1217
1218 if (status & R1_EXCEPTION_EVENT) {
1219 err = mmc_send_ext_csd(card, ext_csd);
1220 if (err) {
1221 pr_err("%s: error %d sending ext_csd\n",
1222 req->rq_disk->disk_name, err);
1223 return MMC_BLK_ABORT;
1224 }
1225
1226 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1227 EXT_CSD_PACKED_FAILURE) &&
1228 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1229 EXT_CSD_PACKED_GENERIC_ERROR)) {
1230 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1231 EXT_CSD_PACKED_INDEXED_ERROR) {
1232 mq_rq->packed_fail_idx =
1233 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1234 return MMC_BLK_PARTIAL;
1235 }
1236 }
1237 }
1238
1239 return check;
1240}
1241
Per Forlin54d49d772011-07-01 18:55:29 +02001242static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1243 struct mmc_card *card,
1244 int disable_multi,
1245 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Per Forlin54d49d772011-07-01 18:55:29 +02001247 u32 readcmd, writecmd;
1248 struct mmc_blk_request *brq = &mqrq->brq;
1249 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301251 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001253 /*
1254 * Reliable writes are used to implement Forced Unit Access and
1255 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001256 *
1257 * XXX: this really needs a good explanation of why REQ_META
1258 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001259 */
1260 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1261 (req->cmd_flags & REQ_META)) &&
1262 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001263 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001264
Per Forlin54d49d772011-07-01 18:55:29 +02001265 memset(brq, 0, sizeof(struct mmc_blk_request));
1266 brq->mrq.cmd = &brq->cmd;
1267 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Per Forlin54d49d772011-07-01 18:55:29 +02001269 brq->cmd.arg = blk_rq_pos(req);
1270 if (!mmc_card_blockaddr(card))
1271 brq->cmd.arg <<= 9;
1272 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1273 brq->data.blksz = 512;
1274 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1275 brq->stop.arg = 0;
1276 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1277 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Asutosh Das1a897142012-07-27 18:10:19 +05301279 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001280 /*
1281 * The block layer doesn't support all sector count
1282 * restrictions, so we need to be prepared for too big
1283 * requests.
1284 */
1285 if (brq->data.blocks > card->host->max_blk_count)
1286 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001288 if (brq->data.blocks > 1) {
1289 /*
1290 * After a read error, we redo the request one sector
1291 * at a time in order to accurately determine which
1292 * sectors can be read successfully.
1293 */
1294 if (disable_multi)
1295 brq->data.blocks = 1;
1296
1297 /* Some controllers can't do multiblock reads due to hw bugs */
1298 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1299 rq_data_dir(req) == READ)
1300 brq->data.blocks = 1;
1301 }
Per Forlin54d49d772011-07-01 18:55:29 +02001302
1303 if (brq->data.blocks > 1 || do_rel_wr) {
1304 /* SPI multiblock writes terminate using a special
1305 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001306 */
Per Forlin54d49d772011-07-01 18:55:29 +02001307 if (!mmc_host_is_spi(card->host) ||
1308 rq_data_dir(req) == READ)
1309 brq->mrq.stop = &brq->stop;
1310 readcmd = MMC_READ_MULTIPLE_BLOCK;
1311 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1312 } else {
1313 brq->mrq.stop = NULL;
1314 readcmd = MMC_READ_SINGLE_BLOCK;
1315 writecmd = MMC_WRITE_BLOCK;
1316 }
1317 if (rq_data_dir(req) == READ) {
1318 brq->cmd.opcode = readcmd;
1319 brq->data.flags |= MMC_DATA_READ;
1320 } else {
1321 brq->cmd.opcode = writecmd;
1322 brq->data.flags |= MMC_DATA_WRITE;
1323 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001324
Per Forlin54d49d772011-07-01 18:55:29 +02001325 if (do_rel_wr)
1326 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001327
Per Forlin54d49d772011-07-01 18:55:29 +02001328 /*
Saugata Das42659002011-12-21 13:09:17 +05301329 * Data tag is used only during writing meta data to speed
1330 * up write and any subsequent read of this meta data
1331 */
1332 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1333 (req->cmd_flags & REQ_META) &&
1334 (rq_data_dir(req) == WRITE) &&
1335 ((brq->data.blocks * brq->data.blksz) >=
1336 card->ext_csd.data_tag_unit_size);
1337
1338 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001339 * Pre-defined multi-block transfers are preferable to
1340 * open ended-ones (and necessary for reliable writes).
1341 * However, it is not sufficient to just send CMD23,
1342 * and avoid the final CMD12, as on an error condition
1343 * CMD12 (stop) needs to be sent anyway. This, coupled
1344 * with Auto-CMD23 enhancements provided by some
1345 * hosts, means that the complexity of dealing
1346 * with this is best left to the host. If CMD23 is
1347 * supported by card and host, we'll fill sbc in and let
1348 * the host deal with handling it correctly. This means
1349 * that for hosts that don't expose MMC_CAP_CMD23, no
1350 * change of behavior will be observed.
1351 *
1352 * N.B: Some MMC cards experience perf degradation.
1353 * We'll avoid using CMD23-bounded multiblock writes for
1354 * these, while retaining features like reliable writes.
1355 */
Saugata Das42659002011-12-21 13:09:17 +05301356 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1357 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1358 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001359 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1360 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301361 (do_rel_wr ? (1 << 31) : 0) |
1362 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001363 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1364 brq->mrq.sbc = &brq->sbc;
1365 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001366
Per Forlin54d49d772011-07-01 18:55:29 +02001367 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001368
Per Forlin54d49d772011-07-01 18:55:29 +02001369 brq->data.sg = mqrq->sg;
1370 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001371
Per Forlin54d49d772011-07-01 18:55:29 +02001372 /*
1373 * Adjust the sg list so it is the same size as the
1374 * request.
1375 */
1376 if (brq->data.blocks != blk_rq_sectors(req)) {
1377 int i, data_size = brq->data.blocks << 9;
1378 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001379
Per Forlin54d49d772011-07-01 18:55:29 +02001380 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1381 data_size -= sg->length;
1382 if (data_size <= 0) {
1383 sg->length += data_size;
1384 i++;
1385 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001386 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001387 }
Per Forlin54d49d772011-07-01 18:55:29 +02001388 brq->data.sg_len = i;
1389 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001390
Per Forlinee8a43a2011-07-01 18:55:33 +02001391 mqrq->mmc_active.mrq = &brq->mrq;
1392 mqrq->mmc_active.err_check = mmc_blk_err_check;
1393
Per Forlin54d49d772011-07-01 18:55:29 +02001394 mmc_queue_bounce_pre(mqrq);
1395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001397static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1398 struct request *req)
1399{
1400 struct mmc_host *host = mq->card->host;
1401 int data_dir;
1402
1403 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1404 return;
1405
1406 /*
1407 * In case the packing control is not supported by the host, it should
1408 * not have an effect on the write packing. Therefore we have to enable
1409 * the write packing
1410 */
1411 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1412 mq->wr_packing_enabled = true;
1413 return;
1414 }
1415
1416 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1417 if (mq->num_of_potential_packed_wr_reqs >
1418 mq->num_wr_reqs_to_start_packing)
1419 mq->wr_packing_enabled = true;
Tatyana Brokhmanc9989122012-10-07 10:26:27 +02001420 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001421 return;
1422 }
1423
1424 data_dir = rq_data_dir(req);
1425
1426 if (data_dir == READ) {
1427 mq->num_of_potential_packed_wr_reqs = 0;
1428 mq->wr_packing_enabled = false;
1429 return;
1430 } else if (data_dir == WRITE) {
1431 mq->num_of_potential_packed_wr_reqs++;
1432 }
1433
1434 if (mq->num_of_potential_packed_wr_reqs >
1435 mq->num_wr_reqs_to_start_packing)
1436 mq->wr_packing_enabled = true;
1437
1438}
1439
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001440struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1441{
1442 if (!card)
1443 return NULL;
1444
1445 return &card->wr_pack_stats;
1446}
1447EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1448
1449void mmc_blk_init_packed_statistics(struct mmc_card *card)
1450{
1451 int max_num_of_packed_reqs = 0;
1452
1453 if (!card || !card->wr_pack_stats.packing_events)
1454 return;
1455
1456 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1457
1458 spin_lock(&card->wr_pack_stats.lock);
1459 memset(card->wr_pack_stats.packing_events, 0,
1460 (max_num_of_packed_reqs + 1) *
1461 sizeof(*card->wr_pack_stats.packing_events));
1462 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1463 sizeof(card->wr_pack_stats.pack_stop_reason));
1464 card->wr_pack_stats.enabled = true;
1465 spin_unlock(&card->wr_pack_stats.lock);
1466}
1467EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1468
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001469void print_mmc_packing_stats(struct mmc_card *card)
1470{
1471 int i;
1472 int max_num_of_packed_reqs = 0;
1473
1474 if ((!card) || (!card->wr_pack_stats.packing_events))
1475 return;
1476
1477 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1478
1479 spin_lock(&card->wr_pack_stats.lock);
1480
1481 pr_info("%s: write packing statistics:\n",
1482 mmc_hostname(card->host));
1483
1484 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1485 if (card->wr_pack_stats.packing_events[i] != 0)
1486 pr_info("%s: Packed %d reqs - %d times\n",
1487 mmc_hostname(card->host), i,
1488 card->wr_pack_stats.packing_events[i]);
1489 }
1490
1491 pr_info("%s: stopped packing due to the following reasons:\n",
1492 mmc_hostname(card->host));
1493
1494 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1495 pr_info("%s: %d times: exceedmax num of segments\n",
1496 mmc_hostname(card->host),
1497 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1498 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1499 pr_info("%s: %d times: exceeding the max num of sectors\n",
1500 mmc_hostname(card->host),
1501 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1502 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1503 pr_info("%s: %d times: wrong data direction\n",
1504 mmc_hostname(card->host),
1505 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1506 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1507 pr_info("%s: %d times: flush or discard\n",
1508 mmc_hostname(card->host),
1509 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1510 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1511 pr_info("%s: %d times: empty queue\n",
1512 mmc_hostname(card->host),
1513 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1514 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1515 pr_info("%s: %d times: rel write\n",
1516 mmc_hostname(card->host),
1517 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1518 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1519 pr_info("%s: %d times: Threshold\n",
1520 mmc_hostname(card->host),
1521 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1522
1523 spin_unlock(&card->wr_pack_stats.lock);
1524}
1525EXPORT_SYMBOL(print_mmc_packing_stats);
1526
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001527static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1528{
1529 struct request_queue *q = mq->queue;
1530 struct mmc_card *card = mq->card;
1531 struct request *cur = req, *next = NULL;
1532 struct mmc_blk_data *md = mq->data;
1533 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1534 unsigned int req_sectors = 0, phys_segments = 0;
1535 unsigned int max_blk_count, max_phys_segs;
1536 u8 put_back = 0;
1537 u8 max_packed_rw = 0;
1538 u8 reqs = 0;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001539 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001540
1541 mmc_blk_clear_packed(mq->mqrq_cur);
1542
1543 if (!(md->flags & MMC_BLK_CMD23) ||
1544 !card->ext_csd.packed_event_en)
1545 goto no_packed;
1546
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001547 if (!mq->wr_packing_enabled)
1548 goto no_packed;
1549
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001550 if ((rq_data_dir(cur) == WRITE) &&
1551 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1552 max_packed_rw = card->ext_csd.max_packed_writes;
1553
1554 if (max_packed_rw == 0)
1555 goto no_packed;
1556
1557 if (mmc_req_rel_wr(cur) &&
1558 (md->flags & MMC_BLK_REL_WR) &&
1559 !en_rel_wr)
1560 goto no_packed;
1561
1562 if (mmc_large_sec(card) &&
1563 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1564 goto no_packed;
1565
1566 max_blk_count = min(card->host->max_blk_count,
1567 card->host->max_req_size >> 9);
1568 if (unlikely(max_blk_count > 0xffff))
1569 max_blk_count = 0xffff;
1570
1571 max_phys_segs = queue_max_segments(q);
1572 req_sectors += blk_rq_sectors(cur);
1573 phys_segments += cur->nr_phys_segments;
1574
1575 if (rq_data_dir(cur) == WRITE) {
1576 req_sectors++;
1577 phys_segments++;
1578 }
1579
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001580 spin_lock(&stats->lock);
1581
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001582 while (reqs < max_packed_rw - 1) {
1583 spin_lock_irq(q->queue_lock);
1584 next = blk_fetch_request(q);
1585 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001586 if (!next) {
1587 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001588 break;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001589 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001590
1591 if (mmc_large_sec(card) &&
1592 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhmana07f7712012-10-04 11:11:08 +02001593 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001594 put_back = 1;
1595 break;
1596 }
1597
1598 if (next->cmd_flags & REQ_DISCARD ||
1599 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001600 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001601 put_back = 1;
1602 break;
1603 }
1604
1605 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001606 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001607 put_back = 1;
1608 break;
1609 }
1610
1611 if (mmc_req_rel_wr(next) &&
1612 (md->flags & MMC_BLK_REL_WR) &&
1613 !en_rel_wr) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001614 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001615 put_back = 1;
1616 break;
1617 }
1618
1619 req_sectors += blk_rq_sectors(next);
1620 if (req_sectors > max_blk_count) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001621 if (stats->enabled)
1622 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001623 put_back = 1;
1624 break;
1625 }
1626
1627 phys_segments += next->nr_phys_segments;
1628 if (phys_segments > max_phys_segs) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001629 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001630 put_back = 1;
1631 break;
1632 }
1633
Maya Erez5c46dad2012-10-10 03:47:54 +02001634 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001635 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5c46dad2012-10-10 03:47:54 +02001636 if (card->ext_csd.bkops_en)
1637 card->bkops_info.sectors_changed +=
1638 blk_rq_sectors(next);
1639 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001640 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1641 cur = next;
1642 reqs++;
1643 }
1644
1645 if (put_back) {
1646 spin_lock_irq(q->queue_lock);
1647 blk_requeue_request(q, next);
1648 spin_unlock_irq(q->queue_lock);
1649 }
1650
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001651 if (stats->enabled) {
1652 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1653 stats->packing_events[reqs + 1]++;
1654 if (reqs + 1 == max_packed_rw)
1655 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1656 }
1657
1658 spin_unlock(&stats->lock);
1659
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001660 if (reqs > 0) {
1661 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1662 mq->mqrq_cur->packed_num = ++reqs;
1663 mq->mqrq_cur->packed_retries = reqs;
1664 return reqs;
1665 }
1666
1667no_packed:
1668 mmc_blk_clear_packed(mq->mqrq_cur);
1669 return 0;
1670}
1671
1672static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1673 struct mmc_card *card,
1674 struct mmc_queue *mq)
1675{
1676 struct mmc_blk_request *brq = &mqrq->brq;
1677 struct request *req = mqrq->req;
1678 struct request *prq;
1679 struct mmc_blk_data *md = mq->data;
1680 bool do_rel_wr, do_data_tag;
1681 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1682 u8 i = 1;
1683
1684 mqrq->packed_cmd = MMC_PACKED_WRITE;
1685 mqrq->packed_blocks = 0;
1686 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1687
1688 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1689 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1690 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1691
1692 /*
1693 * Argument for each entry of packed group
1694 */
1695 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1696 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1697 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1698 (prq->cmd_flags & REQ_META) &&
1699 (rq_data_dir(prq) == WRITE) &&
1700 ((brq->data.blocks * brq->data.blksz) >=
1701 card->ext_csd.data_tag_unit_size);
1702 /* Argument of CMD23 */
1703 packed_cmd_hdr[(i * 2)] =
1704 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1705 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1706 blk_rq_sectors(prq);
1707 /* Argument of CMD18 or CMD25 */
1708 packed_cmd_hdr[((i * 2)) + 1] =
1709 mmc_card_blockaddr(card) ?
1710 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1711 mqrq->packed_blocks += blk_rq_sectors(prq);
1712 i++;
1713 }
1714
1715 memset(brq, 0, sizeof(struct mmc_blk_request));
1716 brq->mrq.cmd = &brq->cmd;
1717 brq->mrq.data = &brq->data;
1718 brq->mrq.sbc = &brq->sbc;
1719 brq->mrq.stop = &brq->stop;
1720
1721 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1722 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1723 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1724
1725 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1726 brq->cmd.arg = blk_rq_pos(req);
1727 if (!mmc_card_blockaddr(card))
1728 brq->cmd.arg <<= 9;
1729 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1730
1731 brq->data.blksz = 512;
1732 brq->data.blocks = mqrq->packed_blocks + 1;
1733 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez867be742012-10-29 20:19:01 +02001734 brq->data.fault_injected = false;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001735
1736 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1737 brq->stop.arg = 0;
1738 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1739
1740 mmc_set_data_timeout(&brq->data, card);
1741
1742 brq->data.sg = mqrq->sg;
1743 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1744
1745 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001746
1747 /*
1748 * This is intended for packed commands tests usage - in case these
1749 * functions are not in use the respective pointers are NULL
1750 */
1751 if (mq->err_check_fn)
1752 mqrq->mmc_active.err_check = mq->err_check_fn;
1753 else
1754 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1755
1756 if (mq->packed_test_fn)
1757 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001758
1759 mmc_queue_bounce_pre(mqrq);
1760}
1761
Adrian Hunter67716322011-08-29 16:42:15 +03001762static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1763 struct mmc_blk_request *brq, struct request *req,
1764 int ret)
1765{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001766 struct mmc_queue_req *mq_rq;
1767 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1768
Adrian Hunter67716322011-08-29 16:42:15 +03001769 /*
1770 * If this is an SD card and we're writing, we can first
1771 * mark the known good sectors as ok.
1772 *
1773 * If the card is not SD, we can still ok written sectors
1774 * as reported by the controller (which might be less than
1775 * the real number of written sectors, but never more).
1776 */
1777 if (mmc_card_sd(card)) {
1778 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301779 if (!brq->data.fault_injected) {
1780 blocks = mmc_sd_num_wr_blocks(card);
1781 if (blocks != (u32)-1)
1782 ret = blk_end_request(req, 0, blocks << 9);
1783 } else
1784 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001785 } else {
1786 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1787 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1788 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001789 return ret;
1790}
1791
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001792static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1793{
1794 struct request *prq;
1795 int idx = mq_rq->packed_fail_idx, i = 0;
1796 int ret = 0;
1797
1798 while (!list_empty(&mq_rq->packed_list)) {
1799 prq = list_entry_rq(mq_rq->packed_list.next);
1800 if (idx == i) {
1801 /* retry from error index */
1802 mq_rq->packed_num -= idx;
1803 mq_rq->req = prq;
1804 ret = 1;
1805
1806 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1807 list_del_init(&prq->queuelist);
1808 mmc_blk_clear_packed(mq_rq);
1809 }
1810 return ret;
1811 }
1812 list_del_init(&prq->queuelist);
1813 blk_end_request(prq, 0, blk_rq_bytes(prq));
1814 i++;
1815 }
1816
1817 mmc_blk_clear_packed(mq_rq);
1818 return ret;
1819}
1820static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1821{
1822 struct request *prq;
1823
1824 while (!list_empty(&mq_rq->packed_list)) {
1825 prq = list_entry_rq(mq_rq->packed_list.next);
1826 list_del_init(&prq->queuelist);
1827 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1828 }
1829
1830 mmc_blk_clear_packed(mq_rq);
1831}
1832
1833static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1834 struct mmc_queue_req *mq_rq)
1835{
1836 struct request *prq;
1837 struct request_queue *q = mq->queue;
1838
1839 while (!list_empty(&mq_rq->packed_list)) {
1840 prq = list_entry_rq(mq_rq->packed_list.prev);
1841 if (prq->queuelist.prev != &mq_rq->packed_list) {
1842 list_del_init(&prq->queuelist);
1843 spin_lock_irq(q->queue_lock);
1844 blk_requeue_request(mq->queue, prq);
1845 spin_unlock_irq(q->queue_lock);
1846 } else {
1847 list_del_init(&prq->queuelist);
1848 }
1849 }
1850
1851 mmc_blk_clear_packed(mq_rq);
1852}
1853
Per Forlinee8a43a2011-07-01 18:55:33 +02001854static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001855{
1856 struct mmc_blk_data *md = mq->data;
1857 struct mmc_card *card = md->queue.card;
1858 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001859 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001860 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001861 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001862 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001863 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001864 const u8 packed_num = 2;
1865 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001866
1867 if (!rqc && !mq->mqrq_prev->req)
1868 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001869
Maya Erez5c46dad2012-10-10 03:47:54 +02001870 if (rqc) {
1871 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1872 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001873 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5c46dad2012-10-10 03:47:54 +02001874 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001875
Per Forlin54d49d772011-07-01 18:55:29 +02001876 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001877 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001878 if (reqs >= packed_num)
1879 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1880 card, mq);
1881 else
1882 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001883 areq = &mq->mqrq_cur->mmc_active;
1884 } else
1885 areq = NULL;
1886 areq = mmc_start_req(card->host, areq, (int *) &status);
1887 if (!areq)
1888 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001889
Per Forlinee8a43a2011-07-01 18:55:33 +02001890 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1891 brq = &mq_rq->brq;
1892 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001893 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001894 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001895
Per Forlind78d4a82011-07-01 18:55:30 +02001896 switch (status) {
1897 case MMC_BLK_SUCCESS:
1898 case MMC_BLK_PARTIAL:
1899 /*
1900 * A block was successfully transferred.
1901 */
Adrian Hunter67716322011-08-29 16:42:15 +03001902 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001903
1904 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1905 ret = mmc_blk_end_packed_req(mq_rq);
1906 break;
1907 } else {
1908 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001909 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001910 }
1911
Adrian Hunter67716322011-08-29 16:42:15 +03001912 /*
1913 * If the blk_end_request function returns non-zero even
1914 * though all data has been transferred and no errors
1915 * were returned by the host controller, it's a bug.
1916 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001917 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301918 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001919 __func__, blk_rq_bytes(req),
1920 brq->data.bytes_xfered);
1921 rqc = NULL;
1922 goto cmd_abort;
1923 }
Per Forlind78d4a82011-07-01 18:55:30 +02001924 break;
1925 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001926 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1927 if (!mmc_blk_reset(md, card->host, type))
1928 break;
1929 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001930 case MMC_BLK_RETRY:
1931 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001932 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001933 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001934 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001935 if (!mmc_blk_reset(md, card->host, type))
1936 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001937 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001938 case MMC_BLK_DATA_ERR: {
1939 int err;
1940
1941 err = mmc_blk_reset(md, card->host, type);
1942 if (!err)
1943 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001944 if (err == -ENODEV ||
1945 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001946 goto cmd_abort;
1947 /* Fall through */
1948 }
1949 case MMC_BLK_ECC_ERR:
1950 if (brq->data.blocks > 1) {
1951 /* Redo read one sector at a time */
1952 pr_warning("%s: retrying using single block read\n",
1953 req->rq_disk->disk_name);
1954 disable_multi = 1;
1955 break;
1956 }
Per Forlind78d4a82011-07-01 18:55:30 +02001957 /*
1958 * After an error, we redo I/O one sector at a
1959 * time, so we only reach here after trying to
1960 * read a single sector.
1961 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301962 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001963 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001964 if (!ret)
1965 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001966 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301967 case MMC_BLK_NOMEDIUM:
1968 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001969 }
1970
Per Forlinee8a43a2011-07-01 18:55:33 +02001971 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001972 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1973 /*
1974 * In case of a incomplete request
1975 * prepare it again and resend.
1976 */
1977 mmc_blk_rw_rq_prep(mq_rq, card,
1978 disable_multi, mq);
1979 mmc_start_req(card->host,
1980 &mq_rq->mmc_active, NULL);
1981 } else {
1982 if (!mq_rq->packed_retries)
1983 goto cmd_abort;
1984 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1985 mmc_start_req(card->host,
1986 &mq_rq->mmc_active, NULL);
1987 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 } while (ret);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 1;
1992
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001993 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001994 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1995 if (mmc_card_removed(card))
1996 req->cmd_flags |= REQ_QUIET;
1997 while (ret)
1998 ret = blk_end_request(req, -EIO,
1999 blk_rq_cur_bytes(req));
2000 } else {
2001 mmc_blk_abort_packed_req(mq_rq);
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Per Forlinee8a43a2011-07-01 18:55:33 +02002004 start_new_req:
2005 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02002006 /*
2007 * If current request is packed, it needs to put back.
2008 */
2009 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2010 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2011
Per Forlinee8a43a2011-07-01 18:55:33 +02002012 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2013 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2014 }
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return 0;
2017}
2018
Adrian Hunterbd788c92010-08-11 14:17:47 -07002019static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2020{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002021 int ret;
2022 struct mmc_blk_data *md = mq->data;
2023 struct mmc_card *card = md->queue.card;
2024
San Mehat148c57a2009-07-30 08:21:19 -07002025#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2026 if (mmc_bus_needs_resume(card->host)) {
2027 mmc_resume_bus(card->host);
2028 mmc_blk_set_blksize(md, card);
2029 }
2030#endif
2031
Maya Erezec0bbad2013-01-16 09:43:45 +02002032 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002033 /* claim host only for the first request */
2034 mmc_claim_host(card->host);
Maya Erezec0bbad2013-01-16 09:43:45 +02002035 if (card->ext_csd.bkops_en)
2036 mmc_stop_bkops(card);
2037 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002038
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039 ret = mmc_blk_part_switch(card, md);
2040 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002041 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302042 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002043 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002044 ret = 0;
2045 goto out;
2046 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002047
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002048 mmc_blk_write_packing_control(mq, req);
2049
Maya Erez463bb952012-05-24 23:46:29 +03002050 if (req && req->cmd_flags & REQ_SANITIZE) {
2051 /* complete ongoing async transfer before issuing sanitize */
2052 if (card->host && card->host->areq)
2053 mmc_blk_issue_rw_rq(mq, NULL);
2054 ret = mmc_blk_issue_sanitize_rq(mq, req);
2055 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002056 /* complete ongoing async transfer before issuing discard */
2057 if (card->host->areq)
2058 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002059 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002060 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002061 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002062 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002063 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002064 /* complete ongoing async transfer before issuing flush */
2065 if (card->host->areq)
2066 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002067 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002068 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002069 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002070 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002071
Andrei Warkentin371a6892011-04-11 18:10:25 -05002072out:
Maya Erezec0bbad2013-01-16 09:43:45 +02002073 if (!req) {
2074 if (mmc_card_need_bkops(card))
2075 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002076 /* release host only when there are no more requests */
2077 mmc_release_host(card->host);
Maya Erezec0bbad2013-01-16 09:43:45 +02002078 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002079 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Russell Kinga6f6c962006-01-03 22:38:44 +00002082static inline int mmc_blk_readonly(struct mmc_card *card)
2083{
2084 return mmc_card_readonly(card) ||
2085 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2086}
2087
Andrei Warkentin371a6892011-04-11 18:10:25 -05002088static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2089 struct device *parent,
2090 sector_t size,
2091 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002092 const char *subname,
2093 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct mmc_blk_data *md;
2096 int devidx, ret;
2097
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002098 devidx = find_first_zero_bit(dev_use, max_devices);
2099 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return ERR_PTR(-ENOSPC);
2101 __set_bit(devidx, dev_use);
2102
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002103 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002104 if (!md) {
2105 ret = -ENOMEM;
2106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002108
Russell Kinga6f6c962006-01-03 22:38:44 +00002109 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002110 * !subname implies we are creating main mmc_blk_data that will be
2111 * associated with mmc_card with mmc_set_drvdata. Due to device
2112 * partitions, devidx will not coincide with a per-physical card
2113 * index anymore so we keep track of a name index.
2114 */
2115 if (!subname) {
2116 md->name_idx = find_first_zero_bit(name_use, max_devices);
2117 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002118 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002119 md->name_idx = ((struct mmc_blk_data *)
2120 dev_to_disk(parent)->private_data)->name_idx;
2121
Johan Rudholmadd710e2011-12-02 08:51:06 +01002122 md->area_type = area_type;
2123
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002124 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002125 * Set the read-only status based on the supported commands
2126 * and the write protect switch.
2127 */
2128 md->read_only = mmc_blk_readonly(card);
2129
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002130 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002131 if (md->disk == NULL) {
2132 ret = -ENOMEM;
2133 goto err_kfree;
2134 }
2135
2136 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002137 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002138 md->usage = 1;
2139
Adrian Hunterd09408a2011-06-23 13:40:28 +03002140 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002141 if (ret)
2142 goto err_putdisk;
2143
Russell Kinga6f6c962006-01-03 22:38:44 +00002144 md->queue.issue_fn = mmc_blk_issue_rq;
2145 md->queue.data = md;
2146
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002147 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002148 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002149 md->disk->fops = &mmc_bdops;
2150 md->disk->private_data = md;
2151 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002152 md->disk->driverfs_dev = parent;
2153 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002154 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002155
2156 /*
2157 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2158 *
2159 * - be set for removable media with permanent block devices
2160 * - be unset for removable block devices with permanent media
2161 *
2162 * Since MMC block devices clearly fall under the second
2163 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2164 * should use the block device creation/destruction hotplug
2165 * messages to tell when the card is present.
2166 */
2167
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002168 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2169 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002170
Martin K. Petersene1defc42009-05-22 17:17:49 -04002171 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002172 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002173
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002174 if (mmc_host_cmd23(card->host)) {
2175 if (mmc_card_mmc(card) ||
2176 (mmc_card_sd(card) &&
2177 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2178 md->flags |= MMC_BLK_CMD23;
2179 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002180
2181 if (mmc_card_mmc(card) &&
2182 md->flags & MMC_BLK_CMD23 &&
2183 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2184 card->ext_csd.rel_sectors)) {
2185 md->flags |= MMC_BLK_REL_WR;
2186 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2187 }
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002190
2191 err_putdisk:
2192 put_disk(md->disk);
2193 err_kfree:
2194 kfree(md);
2195 out:
2196 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Andrei Warkentin371a6892011-04-11 18:10:25 -05002199static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2200{
2201 sector_t size;
2202 struct mmc_blk_data *md;
2203
2204 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2205 /*
2206 * The EXT_CSD sector count is in number or 512 byte
2207 * sectors.
2208 */
2209 size = card->ext_csd.sectors;
2210 } else {
2211 /*
2212 * The CSD capacity field is in units of read_blkbits.
2213 * set_capacity takes units of 512 bytes.
2214 */
2215 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2216 }
2217
Johan Rudholmadd710e2011-12-02 08:51:06 +01002218 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2219 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 return md;
2221}
2222
2223static int mmc_blk_alloc_part(struct mmc_card *card,
2224 struct mmc_blk_data *md,
2225 unsigned int part_type,
2226 sector_t size,
2227 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002228 const char *subname,
2229 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002230{
2231 char cap_str[10];
2232 struct mmc_blk_data *part_md;
2233
2234 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002235 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002236 if (IS_ERR(part_md))
2237 return PTR_ERR(part_md);
2238 part_md->part_type = part_type;
2239 list_add(&part_md->part, &md->part);
2240
2241 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2242 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302243 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 part_md->disk->disk_name, mmc_card_id(card),
2245 mmc_card_name(card), part_md->part_type, cap_str);
2246 return 0;
2247}
2248
Namjae Jeone0c368d2011-10-06 23:41:38 +09002249/* MMC Physical partitions consist of two boot partitions and
2250 * up to four general purpose partitions.
2251 * For each partition enabled in EXT_CSD a block device will be allocatedi
2252 * to provide access to the partition.
2253 */
2254
Andrei Warkentin371a6892011-04-11 18:10:25 -05002255static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2256{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002257 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002258
2259 if (!mmc_card_mmc(card))
2260 return 0;
2261
Namjae Jeone0c368d2011-10-06 23:41:38 +09002262 for (idx = 0; idx < card->nr_parts; idx++) {
2263 if (card->part[idx].size) {
2264 ret = mmc_blk_alloc_part(card, md,
2265 card->part[idx].part_cfg,
2266 card->part[idx].size >> 9,
2267 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002268 card->part[idx].name,
2269 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002270 if (ret)
2271 return ret;
2272 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002273 }
2274
2275 return ret;
2276}
2277
Andrei Warkentin371a6892011-04-11 18:10:25 -05002278static void mmc_blk_remove_req(struct mmc_blk_data *md)
2279{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002280 struct mmc_card *card;
2281
Andrei Warkentin371a6892011-04-11 18:10:25 -05002282 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002283 card = md->queue.card;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002284 device_remove_file(disk_to_dev(md->disk),
2285 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002286 if (md->disk->flags & GENHD_FL_UP) {
2287 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002288 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2289 card->ext_csd.boot_ro_lockable)
2290 device_remove_file(disk_to_dev(md->disk),
2291 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002292
2293 /* Stop new requests from getting into the queue */
2294 del_gendisk(md->disk);
2295 }
2296
2297 /* Then flush out any already in there */
2298 mmc_cleanup_queue(&md->queue);
2299 mmc_blk_put(md);
2300 }
2301}
2302
2303static void mmc_blk_remove_parts(struct mmc_card *card,
2304 struct mmc_blk_data *md)
2305{
2306 struct list_head *pos, *q;
2307 struct mmc_blk_data *part_md;
2308
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002309 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002310 list_for_each_safe(pos, q, &md->part) {
2311 part_md = list_entry(pos, struct mmc_blk_data, part);
2312 list_del(pos);
2313 mmc_blk_remove_req(part_md);
2314 }
2315}
2316
2317static int mmc_add_disk(struct mmc_blk_data *md)
2318{
2319 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002320 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002321
2322 add_disk(md->disk);
2323 md->force_ro.show = force_ro_show;
2324 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302325 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002326 md->force_ro.attr.name = "force_ro";
2327 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2328 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2329 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002330 goto force_ro_fail;
2331
2332 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2333 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002334 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002335
2336 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2337 mode = S_IRUGO;
2338 else
2339 mode = S_IRUGO | S_IWUSR;
2340
2341 md->power_ro_lock.show = power_ro_lock_show;
2342 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002343 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002344 md->power_ro_lock.attr.mode = mode;
2345 md->power_ro_lock.attr.name =
2346 "ro_lock_until_next_power_on";
2347 ret = device_create_file(disk_to_dev(md->disk),
2348 &md->power_ro_lock);
2349 if (ret)
2350 goto power_ro_lock_fail;
2351 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002352
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002353 md->num_wr_reqs_to_start_packing.show =
2354 num_wr_reqs_to_start_packing_show;
2355 md->num_wr_reqs_to_start_packing.store =
2356 num_wr_reqs_to_start_packing_store;
2357 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2358 md->num_wr_reqs_to_start_packing.attr.name =
2359 "num_wr_reqs_to_start_packing";
2360 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2361 ret = device_create_file(disk_to_dev(md->disk),
2362 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002363 if (ret)
Maya Ereza317a4c2012-10-30 09:02:39 +02002364 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002365
Yaniv Gardiab0b6b02012-10-11 11:36:24 +02002366 md->min_sectors_to_check_bkops_status.show =
2367 min_sectors_to_check_bkops_status_show;
2368 md->min_sectors_to_check_bkops_status.store =
2369 min_sectors_to_check_bkops_status_store;
2370 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2371 md->min_sectors_to_check_bkops_status.attr.name =
2372 "min_sectors_to_check_bkops_status";
2373 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2374 ret = device_create_file(disk_to_dev(md->disk),
2375 &md->min_sectors_to_check_bkops_status);
2376 if (ret)
Maya Ereza317a4c2012-10-30 09:02:39 +02002377 goto min_sectors_to_check_bkops_status_fails;
Yaniv Gardiab0b6b02012-10-11 11:36:24 +02002378
Johan Rudholmadd710e2011-12-02 08:51:06 +01002379 return ret;
2380
Maya Ereza317a4c2012-10-30 09:02:39 +02002381min_sectors_to_check_bkops_status_fails:
2382 device_remove_file(disk_to_dev(md->disk),
2383 &md->num_wr_reqs_to_start_packing);
2384num_wr_reqs_to_start_packing_fail:
2385 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002386power_ro_lock_fail:
Maya Ereza317a4c2012-10-30 09:02:39 +02002387 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002388force_ro_fail:
Maya Ereza317a4c2012-10-30 09:02:39 +02002389 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002390
2391 return ret;
2392}
2393
Chris Ballc59d4472011-11-11 22:01:43 -05002394#define CID_MANFID_SANDISK 0x2
2395#define CID_MANFID_TOSHIBA 0x11
2396#define CID_MANFID_MICRON 0x13
2397
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002398static const struct mmc_fixup blk_fixups[] =
2399{
Chris Ballc59d4472011-11-11 22:01:43 -05002400 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2401 MMC_QUIRK_INAND_CMD38),
2402 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2403 MMC_QUIRK_INAND_CMD38),
2404 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2405 MMC_QUIRK_INAND_CMD38),
2406 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2407 MMC_QUIRK_INAND_CMD38),
2408 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2409 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002410
2411 /*
2412 * Some MMC cards experience performance degradation with CMD23
2413 * instead of CMD12-bounded multiblock transfers. For now we'll
2414 * black list what's bad...
2415 * - Certain Toshiba cards.
2416 *
2417 * N.B. This doesn't affect SD cards.
2418 */
Chris Ballc59d4472011-11-11 22:01:43 -05002419 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002420 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002421 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002422 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002423 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002424 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002425
2426 /*
2427 * Some Micron MMC cards needs longer data read timeout than
2428 * indicated in CSD.
2429 */
Chris Ballc59d4472011-11-11 22:01:43 -05002430 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002431 MMC_QUIRK_LONG_READ_TIME),
2432
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302433 /* Some INAND MCP devices advertise incorrect timeout values */
2434 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2435 MMC_QUIRK_INAND_DATA_TIMEOUT),
2436
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002437 END_FIXUP
2438};
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static int mmc_blk_probe(struct mmc_card *card)
2441{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002442 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002443 char cap_str[10];
2444
Pierre Ossman912490d2005-05-21 10:27:02 +01002445 /*
2446 * Check that the card supports the command class(es) we need.
2447 */
2448 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return -ENODEV;
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 md = mmc_blk_alloc(card);
2452 if (IS_ERR(md))
2453 return PTR_ERR(md);
2454
Yi Li444122f2009-02-05 15:31:57 +08002455 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002456 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302457 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002459 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Andrei Warkentin371a6892011-04-11 18:10:25 -05002461 if (mmc_blk_alloc_parts(card, md))
2462 goto out;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002465 mmc_fixup_device(card, blk_fixups);
2466
San Mehat148c57a2009-07-30 08:21:19 -07002467#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2468 mmc_set_bus_resume_policy(card->host, 1);
2469#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002470 if (mmc_add_disk(md))
2471 goto out;
2472
2473 list_for_each_entry(part_md, &md->part, part) {
2474 if (mmc_add_disk(part_md))
2475 goto out;
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
2478
2479 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002480 mmc_blk_remove_parts(card, md);
2481 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485static void mmc_blk_remove(struct mmc_card *card)
2486{
2487 struct mmc_blk_data *md = mmc_get_drvdata(card);
2488
Andrei Warkentin371a6892011-04-11 18:10:25 -05002489 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002490 mmc_claim_host(card->host);
2491 mmc_blk_part_switch(card, md);
2492 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002493 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002495#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2496 mmc_set_bus_resume_policy(card->host, 0);
2497#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
2500#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002501static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002503 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 struct mmc_blk_data *md = mmc_get_drvdata(card);
2505
2506 if (md) {
2507 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002508 list_for_each_entry(part_md, &md->part, part) {
2509 mmc_queue_suspend(&part_md->queue);
2510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512 return 0;
2513}
2514
2515static int mmc_blk_resume(struct mmc_card *card)
2516{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002517 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct mmc_blk_data *md = mmc_get_drvdata(card);
2519
2520 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002521 /*
2522 * Resume involves the card going into idle state,
2523 * so current partition is always the main one.
2524 */
2525 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002527 list_for_each_entry(part_md, &md->part, part) {
2528 mmc_queue_resume(&part_md->queue);
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 return 0;
2532}
2533#else
2534#define mmc_blk_suspend NULL
2535#define mmc_blk_resume NULL
2536#endif
2537
2538static struct mmc_driver mmc_driver = {
2539 .drv = {
2540 .name = "mmcblk",
2541 },
2542 .probe = mmc_blk_probe,
2543 .remove = mmc_blk_remove,
2544 .suspend = mmc_blk_suspend,
2545 .resume = mmc_blk_resume,
2546};
2547
2548static int __init mmc_blk_init(void)
2549{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002550 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002552 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2553 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2554
2555 max_devices = 256 / perdev_minors;
2556
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002557 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2558 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002561 res = mmc_register_driver(&mmc_driver);
2562 if (res)
2563 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002565 return 0;
2566 out2:
2567 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 out:
2569 return res;
2570}
2571
2572static void __exit mmc_blk_exit(void)
2573{
2574 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002575 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578module_init(mmc_blk_init);
2579module_exit(mmc_blk_exit);
2580
2581MODULE_LICENSE("GPL");
2582MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2583