blob: a39014c6d8f22d90091b7d6366d51c147e3fdabe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200124 struct device_attribute min_sectors_to_check_bkops_status;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +0200130enum mmc_blk_status {
131 MMC_BLK_SUCCESS = 0,
132 MMC_BLK_PARTIAL,
133 MMC_BLK_CMD_ERR,
134 MMC_BLK_RETRY,
135 MMC_BLK_ABORT,
136 MMC_BLK_DATA_ERR,
137 MMC_BLK_ECC_ERR,
138 MMC_BLK_NOMEDIUM,
Seungwon Jeon968c7742012-05-31 11:54:47 +0300139};
140
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200141enum {
142 MMC_PACKED_N_IDX = -1,
143 MMC_PACKED_N_ZERO,
144 MMC_PACKED_N_SINGLE,
145};
146
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400147module_param(perdev_minors, int, 0444);
148MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
149
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200150static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
151{
152 mqrq->packed_cmd = MMC_PACKED_NONE;
153 mqrq->packed_num = MMC_PACKED_N_ZERO;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
157{
158 struct mmc_blk_data *md;
159
Arjan van de Vena621aae2006-01-12 18:43:35 +0000160 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 md = disk->private_data;
162 if (md && md->usage == 0)
163 md = NULL;
164 if (md)
165 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000166 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 return md;
169}
170
Andrei Warkentin371a6892011-04-11 18:10:25 -0500171static inline int mmc_get_devidx(struct gendisk *disk)
172{
Colin Cross71b54d42010-09-03 12:41:21 -0700173 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500174 return devidx;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static void mmc_blk_put(struct mmc_blk_data *md)
178{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000179 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 md->usage--;
181 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500182 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800183 blk_cleanup_queue(md->queue.queue);
184
David Woodhouse1dff3142007-11-21 18:45:12 +0100185 __clear_bit(devidx, dev_use);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(md);
189 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Johan Rudholmadd710e2011-12-02 08:51:06 +0100193static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 int ret;
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
199 int locked = 0;
200
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 locked = 2;
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
204 locked = 1;
205
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
207
208 return ret;
209}
210
211static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213{
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
218
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
221
222 if (set != 1)
223 return count;
224
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
227
228 mmc_claim_host(card->host);
229
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
238
239 mmc_release_host(card->host);
240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255}
256
Andrei Warkentin371a6892011-04-11 18:10:25 -0500257static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
263 ret = snprintf(buf, PAGE_SIZE, "%d",
264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268}
269
270static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284out:
285 mmc_blk_put(md);
286 return ret;
287}
288
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200289static ssize_t
290num_wr_reqs_to_start_packing_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
294 int num_wr_reqs_to_start_packing;
295 int ret;
296
297 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
298
299 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
300
301 mmc_blk_put(md);
302 return ret;
303}
304
305static ssize_t
306num_wr_reqs_to_start_packing_store(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int value;
311 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
312
313 sscanf(buf, "%d", &value);
314 if (value >= 0)
315 md->queue.num_wr_reqs_to_start_packing = value;
316
317 mmc_blk_put(md);
318 return count;
319}
320
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200321static ssize_t
322min_sectors_to_check_bkops_status_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
326 unsigned int min_sectors_to_check_bkops_status;
327 struct mmc_card *card = md->queue.card;
328 int ret;
329
330 if (!card)
331 return -EINVAL;
332
333 min_sectors_to_check_bkops_status =
334 card->bkops_info.min_sectors_to_queue_delayed_work;
335
336 ret = snprintf(buf, PAGE_SIZE, "%d\n",
337 min_sectors_to_check_bkops_status);
338
339 mmc_blk_put(md);
340 return ret;
341}
342
343static ssize_t
344min_sectors_to_check_bkops_status_store(struct device *dev,
345 struct device_attribute *attr,
346 const char *buf, size_t count)
347{
348 int value;
349 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
350 struct mmc_card *card = md->queue.card;
351
352 if (!card)
353 return -EINVAL;
354
355 sscanf(buf, "%d", &value);
356 if (value >= 0)
357 card->bkops_info.min_sectors_to_queue_delayed_work = value;
358
359 mmc_blk_put(md);
360 return count;
361}
362
Al Viroa5a15612008-03-02 10:33:30 -0500363static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Al Viroa5a15612008-03-02 10:33:30 -0500365 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int ret = -ENXIO;
367
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200368 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (md) {
370 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500371 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700373
Al Viroa5a15612008-03-02 10:33:30 -0500374 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700375 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700376 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200379 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return ret;
382}
383
Al Viroa5a15612008-03-02 10:33:30 -0500384static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Al Viroa5a15612008-03-02 10:33:30 -0500386 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200388 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200390 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
394static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800395mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800397 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
398 geo->heads = 4;
399 geo->sectors = 16;
400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
John Calixtocb87ea22011-04-26 18:56:29 -0400403struct mmc_blk_ioc_data {
404 struct mmc_ioc_cmd ic;
405 unsigned char *buf;
406 u64 buf_bytes;
407};
408
409static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
410 struct mmc_ioc_cmd __user *user)
411{
412 struct mmc_blk_ioc_data *idata;
413 int err;
414
415 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
416 if (!idata) {
417 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400418 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400419 }
420
421 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
422 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400423 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400424 }
425
426 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
427 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
428 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400429 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400430 }
431
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100432 if (!idata->buf_bytes)
433 return idata;
434
John Calixtocb87ea22011-04-26 18:56:29 -0400435 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
436 if (!idata->buf) {
437 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400438 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400439 }
440
441 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
442 idata->ic.data_ptr, idata->buf_bytes)) {
443 err = -EFAULT;
444 goto copy_err;
445 }
446
447 return idata;
448
449copy_err:
450 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400451idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400452 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400453out:
John Calixtocb87ea22011-04-26 18:56:29 -0400454 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400455}
456
457static int mmc_blk_ioctl_cmd(struct block_device *bdev,
458 struct mmc_ioc_cmd __user *ic_ptr)
459{
460 struct mmc_blk_ioc_data *idata;
461 struct mmc_blk_data *md;
462 struct mmc_card *card;
463 struct mmc_command cmd = {0};
464 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530465 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400466 struct scatterlist sg;
467 int err;
468
469 /*
470 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
471 * whole block device, not on a partition. This prevents overspray
472 * between sibling partitions.
473 */
474 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
475 return -EPERM;
476
477 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
478 if (IS_ERR(idata))
479 return PTR_ERR(idata);
480
John Calixtocb87ea22011-04-26 18:56:29 -0400481 md = mmc_blk_get(bdev->bd_disk);
482 if (!md) {
483 err = -EINVAL;
484 goto cmd_done;
485 }
486
487 card = md->queue.card;
488 if (IS_ERR(card)) {
489 err = PTR_ERR(card);
490 goto cmd_done;
491 }
492
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100493 cmd.opcode = idata->ic.opcode;
494 cmd.arg = idata->ic.arg;
495 cmd.flags = idata->ic.flags;
496
497 if (idata->buf_bytes) {
498 data.sg = &sg;
499 data.sg_len = 1;
500 data.blksz = idata->ic.blksz;
501 data.blocks = idata->ic.blocks;
502
503 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
504
505 if (idata->ic.write_flag)
506 data.flags = MMC_DATA_WRITE;
507 else
508 data.flags = MMC_DATA_READ;
509
510 /* data.flags must already be set before doing this. */
511 mmc_set_data_timeout(&data, card);
512
513 /* Allow overriding the timeout_ns for empirical tuning. */
514 if (idata->ic.data_timeout_ns)
515 data.timeout_ns = idata->ic.data_timeout_ns;
516
517 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
518 /*
519 * Pretend this is a data transfer and rely on the
520 * host driver to compute timeout. When all host
521 * drivers support cmd.cmd_timeout for R1B, this
522 * can be changed to:
523 *
524 * mrq.data = NULL;
525 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
526 */
527 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
528 }
529
530 mrq.data = &data;
531 }
532
533 mrq.cmd = &cmd;
534
John Calixtocb87ea22011-04-26 18:56:29 -0400535 mmc_claim_host(card->host);
536
537 if (idata->ic.is_acmd) {
538 err = mmc_app_cmd(card->host, card);
539 if (err)
540 goto cmd_rel_host;
541 }
542
John Calixtocb87ea22011-04-26 18:56:29 -0400543 mmc_wait_for_req(card->host, &mrq);
544
545 if (cmd.error) {
546 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
547 __func__, cmd.error);
548 err = cmd.error;
549 goto cmd_rel_host;
550 }
551 if (data.error) {
552 dev_err(mmc_dev(card->host), "%s: data error %d\n",
553 __func__, data.error);
554 err = data.error;
555 goto cmd_rel_host;
556 }
557
558 /*
559 * According to the SD specs, some commands require a delay after
560 * issuing the command.
561 */
562 if (idata->ic.postsleep_min_us)
563 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
564
565 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
566 err = -EFAULT;
567 goto cmd_rel_host;
568 }
569
570 if (!idata->ic.write_flag) {
571 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
572 idata->buf, idata->buf_bytes)) {
573 err = -EFAULT;
574 goto cmd_rel_host;
575 }
576 }
577
578cmd_rel_host:
579 mmc_release_host(card->host);
580
581cmd_done:
582 mmc_blk_put(md);
583 kfree(idata->buf);
584 kfree(idata);
585 return err;
586}
587
588static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
589 unsigned int cmd, unsigned long arg)
590{
591 int ret = -EINVAL;
592 if (cmd == MMC_IOC_CMD)
593 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
594 return ret;
595}
596
597#ifdef CONFIG_COMPAT
598static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
599 unsigned int cmd, unsigned long arg)
600{
601 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
602}
603#endif
604
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700605static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500606 .open = mmc_blk_open,
607 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800608 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400610 .ioctl = mmc_blk_ioctl,
611#ifdef CONFIG_COMPAT
612 .compat_ioctl = mmc_blk_compat_ioctl,
613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614};
615
Andrei Warkentin371a6892011-04-11 18:10:25 -0500616static inline int mmc_blk_part_switch(struct mmc_card *card,
617 struct mmc_blk_data *md)
618{
619 int ret;
620 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300621
Andrei Warkentin371a6892011-04-11 18:10:25 -0500622 if (main_md->part_curr == md->part_type)
623 return 0;
624
625 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300626 u8 part_config = card->ext_csd.part_config;
627
628 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
629 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500630
631 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300632 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500633 card->ext_csd.part_time);
634 if (ret)
635 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300636
637 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300638 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500639
640 main_md->part_curr = md->part_type;
641 return 0;
642}
643
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700644static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
645{
646 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100647 u32 result;
648 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700649
Venkatraman Sad5fd972011-08-25 00:30:50 +0530650 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400651 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400652 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700653
654 struct scatterlist sg;
655
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700656 cmd.opcode = MMC_APP_CMD;
657 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700658 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700659
660 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700661 if (err)
662 return (u32)-1;
663 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700664 return (u32)-1;
665
666 memset(&cmd, 0, sizeof(struct mmc_command));
667
668 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
669 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700670 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700671
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700672 data.blksz = 4;
673 data.blocks = 1;
674 data.flags = MMC_DATA_READ;
675 data.sg = &sg;
676 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530677 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700678
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700679 mrq.cmd = &cmd;
680 mrq.data = &data;
681
Ben Dooks051913d2009-06-08 23:33:57 +0100682 blocks = kmalloc(4, GFP_KERNEL);
683 if (!blocks)
684 return (u32)-1;
685
686 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700687
688 mmc_wait_for_req(card->host, &mrq);
689
Ben Dooks051913d2009-06-08 23:33:57 +0100690 result = ntohl(*blocks);
691 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700692
Ben Dooks051913d2009-06-08 23:33:57 +0100693 if (cmd.error || data.error)
694 result = (u32)-1;
695
696 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700697}
698
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100699static int send_stop(struct mmc_card *card, u32 *status)
700{
701 struct mmc_command cmd = {0};
702 int err;
703
704 cmd.opcode = MMC_STOP_TRANSMISSION;
705 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
706 err = mmc_wait_for_cmd(card->host, &cmd, 5);
707 if (err == 0)
708 *status = cmd.resp[0];
709 return err;
710}
711
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100712static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300713{
Chris Ball1278dba2011-04-13 23:40:30 -0400714 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300715 int err;
716
Adrian Hunter504f1912008-10-16 12:55:25 +0300717 cmd.opcode = MMC_SEND_STATUS;
718 if (!mmc_host_is_spi(card->host))
719 cmd.arg = card->rca << 16;
720 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100721 err = mmc_wait_for_cmd(card->host, &cmd, retries);
722 if (err == 0)
723 *status = cmd.resp[0];
724 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300725}
726
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530727#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100728#define ERR_RETRY 2
729#define ERR_ABORT 1
730#define ERR_CONTINUE 0
731
732static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
733 bool status_valid, u32 status)
734{
735 switch (error) {
736 case -EILSEQ:
737 /* response crc error, retry the r/w cmd */
738 pr_err("%s: %s sending %s command, card status %#x\n",
739 req->rq_disk->disk_name, "response CRC error",
740 name, status);
741 return ERR_RETRY;
742
743 case -ETIMEDOUT:
744 pr_err("%s: %s sending %s command, card status %#x\n",
745 req->rq_disk->disk_name, "timed out", name, status);
746
747 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700748 if (!status_valid) {
749 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100750 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700751 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100752 /*
753 * If it was a r/w cmd crc error, or illegal command
754 * (eg, issued in wrong state) then retry - we should
755 * have corrected the state problem above.
756 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700757 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
758 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100759 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700760 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100761
762 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700763 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100764 return ERR_ABORT;
765
766 default:
767 /* We don't understand the error code the driver gave us */
768 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
769 req->rq_disk->disk_name, error, status);
770 return ERR_ABORT;
771 }
772}
773
774/*
775 * Initial r/w and stop cmd error recovery.
776 * We don't know whether the card received the r/w cmd or not, so try to
777 * restore things back to a sane state. Essentially, we do this as follows:
778 * - Obtain card status. If the first attempt to obtain card status fails,
779 * the status word will reflect the failed status cmd, not the failed
780 * r/w cmd. If we fail to obtain card status, it suggests we can no
781 * longer communicate with the card.
782 * - Check the card state. If the card received the cmd but there was a
783 * transient problem with the response, it might still be in a data transfer
784 * mode. Try to send it a stop command. If this fails, we can't recover.
785 * - If the r/w cmd failed due to a response CRC error, it was probably
786 * transient, so retry the cmd.
787 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
788 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
789 * illegal cmd, retry.
790 * Otherwise we don't understand what happened, so abort.
791 */
792static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300793 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100794{
795 bool prev_cmd_status_valid = true;
796 u32 status, stop_status = 0;
797 int err, retry;
798
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530799 if (mmc_card_removed(card))
800 return ERR_NOMEDIUM;
801
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100802 /*
803 * Try to get card status which indicates both the card state
804 * and why there was no response. If the first attempt fails,
805 * we can't be sure the returned status is for the r/w command.
806 */
807 for (retry = 2; retry >= 0; retry--) {
808 err = get_card_status(card, &status, 0);
809 if (!err)
810 break;
811
812 prev_cmd_status_valid = false;
813 pr_err("%s: error %d sending status command, %sing\n",
814 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
815 }
816
817 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530818 if (err) {
819 /* Check if the card is removed */
820 if (mmc_detect_card_removed(card->host))
821 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100822 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530823 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100824
Adrian Hunter67716322011-08-29 16:42:15 +0300825 /* Flag ECC errors */
826 if ((status & R1_CARD_ECC_FAILED) ||
827 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
828 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
829 *ecc_err = 1;
830
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100831 /*
832 * Check the current card state. If it is in some data transfer
833 * mode, tell it to stop (and hopefully transition back to TRAN.)
834 */
835 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
836 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
837 err = send_stop(card, &stop_status);
838 if (err)
839 pr_err("%s: error %d sending stop command\n",
840 req->rq_disk->disk_name, err);
841
842 /*
843 * If the stop cmd also timed out, the card is probably
844 * not present, so abort. Other errors are bad news too.
845 */
846 if (err)
847 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300848 if (stop_status & R1_CARD_ECC_FAILED)
849 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100850 }
851
852 /* Check for set block count errors */
853 if (brq->sbc.error)
854 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
855 prev_cmd_status_valid, status);
856
857 /* Check for r/w command errors */
858 if (brq->cmd.error)
859 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
860 prev_cmd_status_valid, status);
861
Adrian Hunter67716322011-08-29 16:42:15 +0300862 /* Data errors */
863 if (!brq->stop.error)
864 return ERR_CONTINUE;
865
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100866 /* Now for stop errors. These aren't fatal to the transfer. */
867 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
868 req->rq_disk->disk_name, brq->stop.error,
869 brq->cmd.resp[0], status);
870
871 /*
872 * Subsitute in our own stop status as this will give the error
873 * state which happened during the execution of the r/w command.
874 */
875 if (stop_status) {
876 brq->stop.resp[0] = stop_status;
877 brq->stop.error = 0;
878 }
879 return ERR_CONTINUE;
880}
881
Adrian Hunter67716322011-08-29 16:42:15 +0300882static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
883 int type)
884{
885 int err;
886
887 if (md->reset_done & type)
888 return -EEXIST;
889
890 md->reset_done |= type;
891 err = mmc_hw_reset(host);
892 /* Ensure we switch back to the correct partition */
893 if (err != -EOPNOTSUPP) {
894 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
895 int part_err;
896
897 main_md->part_curr = main_md->part_type;
898 part_err = mmc_blk_part_switch(host->card, md);
899 if (part_err) {
900 /*
901 * We have failed to get back into the correct
902 * partition, so we need to abort the whole request.
903 */
904 return -ENODEV;
905 }
906 }
907 return err;
908}
909
910static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
911{
912 md->reset_done &= ~type;
913}
914
Adrian Hunterbd788c92010-08-11 14:17:47 -0700915static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
916{
917 struct mmc_blk_data *md = mq->data;
918 struct mmc_card *card = md->queue.card;
919 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300920 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700921
Adrian Hunterbd788c92010-08-11 14:17:47 -0700922 if (!mmc_can_erase(card)) {
923 err = -EOPNOTSUPP;
924 goto out;
925 }
926
927 from = blk_rq_pos(req);
928 nr = blk_rq_sectors(req);
929
Maya Erez5f360692012-10-10 03:47:54 +0200930 if (card->ext_csd.bkops_en)
931 card->bkops_info.sectors_changed += blk_rq_sectors(req);
932
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900933 if (mmc_can_discard(card))
934 arg = MMC_DISCARD_ARG;
935 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700936 arg = MMC_TRIM_ARG;
937 else
938 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300939retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500940 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
941 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
942 INAND_CMD38_ARG_EXT_CSD,
943 arg == MMC_TRIM_ARG ?
944 INAND_CMD38_ARG_TRIM :
945 INAND_CMD38_ARG_ERASE,
946 0);
947 if (err)
948 goto out;
949 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700950 err = mmc_erase(card, from, nr, arg);
951out:
Adrian Hunter67716322011-08-29 16:42:15 +0300952 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
953 goto retry;
954 if (!err)
955 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530956 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700957
Adrian Hunterbd788c92010-08-11 14:17:47 -0700958 return err ? 0 : 1;
959}
960
Adrian Hunter49804542010-08-11 14:17:50 -0700961static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
962 struct request *req)
963{
964 struct mmc_blk_data *md = mq->data;
965 struct mmc_card *card = md->queue.card;
966 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300967 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700968
Maya Erez463bb952012-05-24 23:46:29 +0300969 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700970 err = -EOPNOTSUPP;
971 goto out;
972 }
973
974 from = blk_rq_pos(req);
975 nr = blk_rq_sectors(req);
976
977 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
978 arg = MMC_SECURE_TRIM1_ARG;
979 else
980 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300981retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500982 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
983 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
984 INAND_CMD38_ARG_EXT_CSD,
985 arg == MMC_SECURE_TRIM1_ARG ?
986 INAND_CMD38_ARG_SECTRIM1 :
987 INAND_CMD38_ARG_SECERASE,
988 0);
989 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300990 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500991 }
Adrian Hunter28302812012-04-05 14:45:48 +0300992
Adrian Hunter49804542010-08-11 14:17:50 -0700993 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300994 if (err == -EIO)
995 goto out_retry;
996 if (err)
997 goto out;
998
999 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001000 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1001 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1002 INAND_CMD38_ARG_EXT_CSD,
1003 INAND_CMD38_ARG_SECTRIM2,
1004 0);
1005 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001006 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001007 }
Adrian Hunter28302812012-04-05 14:45:48 +03001008
Adrian Hunter49804542010-08-11 14:17:50 -07001009 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001010 if (err == -EIO)
1011 goto out_retry;
1012 if (err)
1013 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001014 }
Adrian Hunter28302812012-04-05 14:45:48 +03001015
1016 if (mmc_can_sanitize(card))
1017 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1018 EXT_CSD_SANITIZE_START, 1, 0);
1019out_retry:
1020 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001021 goto retry;
1022 if (!err)
1023 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001024out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301025 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001026
Adrian Hunter49804542010-08-11 14:17:50 -07001027 return err ? 0 : 1;
1028}
1029
Maya Erez463bb952012-05-24 23:46:29 +03001030static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1031 struct request *req)
1032{
1033 struct mmc_blk_data *md = mq->data;
1034 struct mmc_card *card = md->queue.card;
1035 int err = 0;
1036
1037 BUG_ON(!card);
1038 BUG_ON(!card->host);
1039
1040 if (!(mmc_can_sanitize(card) &&
1041 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1042 pr_warning("%s: %s - SANITIZE is not supported\n",
1043 mmc_hostname(card->host), __func__);
1044 err = -EOPNOTSUPP;
1045 goto out;
1046 }
1047
1048 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1049 mmc_hostname(card->host), __func__);
1050
1051 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001052 EXT_CSD_SANITIZE_START, 1,
1053 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001054
1055 if (err)
1056 pr_err("%s: %s - mmc_switch() with "
1057 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1058 mmc_hostname(card->host), __func__, err);
1059
1060 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1061 __func__);
1062
1063out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301064 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001065
1066 return err ? 0 : 1;
1067}
1068
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001069static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1070{
1071 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001072 struct mmc_card *card = md->queue.card;
1073 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001074
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001075 ret = mmc_flush_cache(card);
1076 if (ret)
1077 ret = -EIO;
1078
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301079 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001080
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001081 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001082}
1083
1084/*
1085 * Reformat current write as a reliable write, supporting
1086 * both legacy and the enhanced reliable write MMC cards.
1087 * In each transfer we'll handle only as much as a single
1088 * reliable write can handle, thus finish the request in
1089 * partial completions.
1090 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001091static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1092 struct mmc_card *card,
1093 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001094{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001095 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1096 /* Legacy mode imposes restrictions on transfers. */
1097 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1098 brq->data.blocks = 1;
1099
1100 if (brq->data.blocks > card->ext_csd.rel_sectors)
1101 brq->data.blocks = card->ext_csd.rel_sectors;
1102 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1103 brq->data.blocks = 1;
1104 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001105}
1106
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001107#define CMD_ERRORS \
1108 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1109 R1_ADDRESS_ERROR | /* Misaligned address */ \
1110 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1111 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1112 R1_CC_ERROR | /* Card controller error */ \
1113 R1_ERROR) /* General/unknown error */
1114
Per Forlinee8a43a2011-07-01 18:55:33 +02001115static int mmc_blk_err_check(struct mmc_card *card,
1116 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001117{
Per Forlinee8a43a2011-07-01 18:55:33 +02001118 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1119 mmc_active);
1120 struct mmc_blk_request *brq = &mq_mrq->brq;
1121 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001122 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001123
1124 /*
1125 * sbc.error indicates a problem with the set block count
1126 * command. No data will have been transferred.
1127 *
1128 * cmd.error indicates a problem with the r/w command. No
1129 * data will have been transferred.
1130 *
1131 * stop.error indicates a problem with the stop command. Data
1132 * may have been transferred, or may still be transferring.
1133 */
Adrian Hunter67716322011-08-29 16:42:15 +03001134 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1135 brq->data.error) {
1136 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001137 case ERR_RETRY:
1138 return MMC_BLK_RETRY;
1139 case ERR_ABORT:
1140 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301141 case ERR_NOMEDIUM:
1142 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001143 case ERR_CONTINUE:
1144 break;
1145 }
1146 }
1147
1148 /*
1149 * Check for errors relating to the execution of the
1150 * initial command - such as address errors. No data
1151 * has been transferred.
1152 */
1153 if (brq->cmd.resp[0] & CMD_ERRORS) {
1154 pr_err("%s: r/w command failed, status = %#x\n",
1155 req->rq_disk->disk_name, brq->cmd.resp[0]);
1156 return MMC_BLK_ABORT;
1157 }
1158
1159 /*
1160 * Everything else is either success, or a data error of some
1161 * kind. If it was a write, we may have transitioned to
1162 * program mode, which we have to wait for it to complete.
1163 */
1164 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1165 u32 status;
1166 do {
1167 int err = get_card_status(card, &status, 5);
1168 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301169 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001170 req->rq_disk->disk_name, err);
1171 return MMC_BLK_CMD_ERR;
1172 }
1173 /*
1174 * Some cards mishandle the status bits,
1175 * so make sure to check both the busy
1176 * indication and the card state.
1177 */
1178 } while (!(status & R1_READY_FOR_DATA) ||
1179 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1180 }
1181
1182 if (brq->data.error) {
1183 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1184 req->rq_disk->disk_name, brq->data.error,
1185 (unsigned)blk_rq_pos(req),
1186 (unsigned)blk_rq_sectors(req),
1187 brq->cmd.resp[0], brq->stop.resp[0]);
1188
1189 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001190 if (ecc_err)
1191 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001192 return MMC_BLK_DATA_ERR;
1193 } else {
1194 return MMC_BLK_CMD_ERR;
1195 }
1196 }
1197
Adrian Hunter67716322011-08-29 16:42:15 +03001198 if (!brq->data.bytes_xfered)
1199 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001200
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001201 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1202 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1203 return MMC_BLK_PARTIAL;
1204 else
1205 return MMC_BLK_SUCCESS;
1206 }
1207
Adrian Hunter67716322011-08-29 16:42:15 +03001208 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1209 return MMC_BLK_PARTIAL;
1210
1211 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001212}
1213
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001214static int mmc_blk_packed_err_check(struct mmc_card *card,
1215 struct mmc_async_req *areq)
1216{
1217 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1218 mmc_active);
1219 struct request *req = mq_rq->req;
1220 int err, check, status;
1221 u8 ext_csd[512];
1222
1223 mq_rq->packed_retries--;
1224 check = mmc_blk_err_check(card, areq);
1225 err = get_card_status(card, &status, 0);
1226 if (err) {
1227 pr_err("%s: error %d sending status command\n",
1228 req->rq_disk->disk_name, err);
1229 return MMC_BLK_ABORT;
1230 }
1231
1232 if (status & R1_EXCEPTION_EVENT) {
1233 err = mmc_send_ext_csd(card, ext_csd);
1234 if (err) {
1235 pr_err("%s: error %d sending ext_csd\n",
1236 req->rq_disk->disk_name, err);
1237 return MMC_BLK_ABORT;
1238 }
1239
1240 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1241 EXT_CSD_PACKED_FAILURE) &&
1242 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1243 EXT_CSD_PACKED_GENERIC_ERROR)) {
1244 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1245 EXT_CSD_PACKED_INDEXED_ERROR) {
1246 mq_rq->packed_fail_idx =
1247 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1248 return MMC_BLK_PARTIAL;
1249 }
1250 }
1251 }
1252
1253 return check;
1254}
1255
Per Forlin54d49d772011-07-01 18:55:29 +02001256static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1257 struct mmc_card *card,
1258 int disable_multi,
1259 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Per Forlin54d49d772011-07-01 18:55:29 +02001261 u32 readcmd, writecmd;
1262 struct mmc_blk_request *brq = &mqrq->brq;
1263 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301265 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001267 /*
1268 * Reliable writes are used to implement Forced Unit Access and
1269 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001270 *
1271 * XXX: this really needs a good explanation of why REQ_META
1272 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001273 */
1274 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1275 (req->cmd_flags & REQ_META)) &&
1276 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001277 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001278
Per Forlin54d49d772011-07-01 18:55:29 +02001279 memset(brq, 0, sizeof(struct mmc_blk_request));
1280 brq->mrq.cmd = &brq->cmd;
1281 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Per Forlin54d49d772011-07-01 18:55:29 +02001283 brq->cmd.arg = blk_rq_pos(req);
1284 if (!mmc_card_blockaddr(card))
1285 brq->cmd.arg <<= 9;
1286 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1287 brq->data.blksz = 512;
1288 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1289 brq->stop.arg = 0;
1290 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1291 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Asutosh Das1a897142012-07-27 18:10:19 +05301293 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001294 /*
1295 * The block layer doesn't support all sector count
1296 * restrictions, so we need to be prepared for too big
1297 * requests.
1298 */
1299 if (brq->data.blocks > card->host->max_blk_count)
1300 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001302 if (brq->data.blocks > 1) {
1303 /*
1304 * After a read error, we redo the request one sector
1305 * at a time in order to accurately determine which
1306 * sectors can be read successfully.
1307 */
1308 if (disable_multi)
1309 brq->data.blocks = 1;
1310
1311 /* Some controllers can't do multiblock reads due to hw bugs */
1312 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1313 rq_data_dir(req) == READ)
1314 brq->data.blocks = 1;
1315 }
Per Forlin54d49d772011-07-01 18:55:29 +02001316
1317 if (brq->data.blocks > 1 || do_rel_wr) {
1318 /* SPI multiblock writes terminate using a special
1319 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001320 */
Per Forlin54d49d772011-07-01 18:55:29 +02001321 if (!mmc_host_is_spi(card->host) ||
1322 rq_data_dir(req) == READ)
1323 brq->mrq.stop = &brq->stop;
1324 readcmd = MMC_READ_MULTIPLE_BLOCK;
1325 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1326 } else {
1327 brq->mrq.stop = NULL;
1328 readcmd = MMC_READ_SINGLE_BLOCK;
1329 writecmd = MMC_WRITE_BLOCK;
1330 }
1331 if (rq_data_dir(req) == READ) {
1332 brq->cmd.opcode = readcmd;
1333 brq->data.flags |= MMC_DATA_READ;
1334 } else {
1335 brq->cmd.opcode = writecmd;
1336 brq->data.flags |= MMC_DATA_WRITE;
1337 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001338
Per Forlin54d49d772011-07-01 18:55:29 +02001339 if (do_rel_wr)
1340 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001341
Per Forlin54d49d772011-07-01 18:55:29 +02001342 /*
Saugata Das42659002011-12-21 13:09:17 +05301343 * Data tag is used only during writing meta data to speed
1344 * up write and any subsequent read of this meta data
1345 */
1346 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1347 (req->cmd_flags & REQ_META) &&
1348 (rq_data_dir(req) == WRITE) &&
1349 ((brq->data.blocks * brq->data.blksz) >=
1350 card->ext_csd.data_tag_unit_size);
1351
1352 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001353 * Pre-defined multi-block transfers are preferable to
1354 * open ended-ones (and necessary for reliable writes).
1355 * However, it is not sufficient to just send CMD23,
1356 * and avoid the final CMD12, as on an error condition
1357 * CMD12 (stop) needs to be sent anyway. This, coupled
1358 * with Auto-CMD23 enhancements provided by some
1359 * hosts, means that the complexity of dealing
1360 * with this is best left to the host. If CMD23 is
1361 * supported by card and host, we'll fill sbc in and let
1362 * the host deal with handling it correctly. This means
1363 * that for hosts that don't expose MMC_CAP_CMD23, no
1364 * change of behavior will be observed.
1365 *
1366 * N.B: Some MMC cards experience perf degradation.
1367 * We'll avoid using CMD23-bounded multiblock writes for
1368 * these, while retaining features like reliable writes.
1369 */
Saugata Das42659002011-12-21 13:09:17 +05301370 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1371 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1372 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001373 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1374 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301375 (do_rel_wr ? (1 << 31) : 0) |
1376 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001377 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1378 brq->mrq.sbc = &brq->sbc;
1379 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001380
Per Forlin54d49d772011-07-01 18:55:29 +02001381 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001382
Per Forlin54d49d772011-07-01 18:55:29 +02001383 brq->data.sg = mqrq->sg;
1384 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001385
Per Forlin54d49d772011-07-01 18:55:29 +02001386 /*
1387 * Adjust the sg list so it is the same size as the
1388 * request.
1389 */
1390 if (brq->data.blocks != blk_rq_sectors(req)) {
1391 int i, data_size = brq->data.blocks << 9;
1392 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001393
Per Forlin54d49d772011-07-01 18:55:29 +02001394 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1395 data_size -= sg->length;
1396 if (data_size <= 0) {
1397 sg->length += data_size;
1398 i++;
1399 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001400 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001401 }
Per Forlin54d49d772011-07-01 18:55:29 +02001402 brq->data.sg_len = i;
1403 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001404
Per Forlinee8a43a2011-07-01 18:55:33 +02001405 mqrq->mmc_active.mrq = &brq->mrq;
1406 mqrq->mmc_active.err_check = mmc_blk_err_check;
1407
Per Forlin54d49d772011-07-01 18:55:29 +02001408 mmc_queue_bounce_pre(mqrq);
1409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001411static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1412 struct request *req)
1413{
1414 struct mmc_host *host = mq->card->host;
1415 int data_dir;
1416
1417 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1418 return;
1419
1420 /*
1421 * In case the packing control is not supported by the host, it should
1422 * not have an effect on the write packing. Therefore we have to enable
1423 * the write packing
1424 */
1425 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1426 mq->wr_packing_enabled = true;
1427 return;
1428 }
1429
1430 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1431 if (mq->num_of_potential_packed_wr_reqs >
1432 mq->num_wr_reqs_to_start_packing)
1433 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001434 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001435 return;
1436 }
1437
1438 data_dir = rq_data_dir(req);
1439
1440 if (data_dir == READ) {
1441 mq->num_of_potential_packed_wr_reqs = 0;
1442 mq->wr_packing_enabled = false;
1443 return;
1444 } else if (data_dir == WRITE) {
1445 mq->num_of_potential_packed_wr_reqs++;
1446 }
1447
1448 if (mq->num_of_potential_packed_wr_reqs >
1449 mq->num_wr_reqs_to_start_packing)
1450 mq->wr_packing_enabled = true;
1451
1452}
1453
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001454struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1455{
1456 if (!card)
1457 return NULL;
1458
1459 return &card->wr_pack_stats;
1460}
1461EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1462
1463void mmc_blk_init_packed_statistics(struct mmc_card *card)
1464{
1465 int max_num_of_packed_reqs = 0;
1466
1467 if (!card || !card->wr_pack_stats.packing_events)
1468 return;
1469
1470 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1471
1472 spin_lock(&card->wr_pack_stats.lock);
1473 memset(card->wr_pack_stats.packing_events, 0,
1474 (max_num_of_packed_reqs + 1) *
1475 sizeof(*card->wr_pack_stats.packing_events));
1476 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1477 sizeof(card->wr_pack_stats.pack_stop_reason));
1478 card->wr_pack_stats.enabled = true;
1479 spin_unlock(&card->wr_pack_stats.lock);
1480}
1481EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1482
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001483static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1484{
1485 struct request_queue *q = mq->queue;
1486 struct mmc_card *card = mq->card;
1487 struct request *cur = req, *next = NULL;
1488 struct mmc_blk_data *md = mq->data;
1489 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1490 unsigned int req_sectors = 0, phys_segments = 0;
1491 unsigned int max_blk_count, max_phys_segs;
1492 u8 put_back = 0;
1493 u8 max_packed_rw = 0;
1494 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001495 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001496
1497 mmc_blk_clear_packed(mq->mqrq_cur);
1498
1499 if (!(md->flags & MMC_BLK_CMD23) ||
1500 !card->ext_csd.packed_event_en)
1501 goto no_packed;
1502
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001503 if (!mq->wr_packing_enabled)
1504 goto no_packed;
1505
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001506 if ((rq_data_dir(cur) == WRITE) &&
1507 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1508 max_packed_rw = card->ext_csd.max_packed_writes;
1509
1510 if (max_packed_rw == 0)
1511 goto no_packed;
1512
1513 if (mmc_req_rel_wr(cur) &&
1514 (md->flags & MMC_BLK_REL_WR) &&
1515 !en_rel_wr)
1516 goto no_packed;
1517
1518 if (mmc_large_sec(card) &&
1519 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1520 goto no_packed;
1521
1522 max_blk_count = min(card->host->max_blk_count,
1523 card->host->max_req_size >> 9);
1524 if (unlikely(max_blk_count > 0xffff))
1525 max_blk_count = 0xffff;
1526
1527 max_phys_segs = queue_max_segments(q);
1528 req_sectors += blk_rq_sectors(cur);
1529 phys_segments += cur->nr_phys_segments;
1530
1531 if (rq_data_dir(cur) == WRITE) {
1532 req_sectors++;
1533 phys_segments++;
1534 }
1535
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001536 spin_lock(&stats->lock);
1537
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001538 while (reqs < max_packed_rw - 1) {
1539 spin_lock_irq(q->queue_lock);
1540 next = blk_fetch_request(q);
1541 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001542 if (!next) {
1543 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001544 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001545 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001546
1547 if (mmc_large_sec(card) &&
1548 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001549 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001550 put_back = 1;
1551 break;
1552 }
1553
1554 if (next->cmd_flags & REQ_DISCARD ||
1555 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001556 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001557 put_back = 1;
1558 break;
1559 }
1560
1561 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001562 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001563 put_back = 1;
1564 break;
1565 }
1566
1567 if (mmc_req_rel_wr(next) &&
1568 (md->flags & MMC_BLK_REL_WR) &&
1569 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001570 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001571 put_back = 1;
1572 break;
1573 }
1574
1575 req_sectors += blk_rq_sectors(next);
1576 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001577 if (stats->enabled)
1578 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001579 put_back = 1;
1580 break;
1581 }
1582
1583 phys_segments += next->nr_phys_segments;
1584 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001585 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001586 put_back = 1;
1587 break;
1588 }
1589
Maya Erez5f360692012-10-10 03:47:54 +02001590 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001591 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5f360692012-10-10 03:47:54 +02001592 if (card->ext_csd.bkops_en)
1593 card->bkops_info.sectors_changed +=
1594 blk_rq_sectors(next);
1595 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001596 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1597 cur = next;
1598 reqs++;
1599 }
1600
1601 if (put_back) {
1602 spin_lock_irq(q->queue_lock);
1603 blk_requeue_request(q, next);
1604 spin_unlock_irq(q->queue_lock);
1605 }
1606
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001607 if (stats->enabled) {
1608 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1609 stats->packing_events[reqs + 1]++;
1610 if (reqs + 1 == max_packed_rw)
1611 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1612 }
1613
1614 spin_unlock(&stats->lock);
1615
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001616 if (reqs > 0) {
1617 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1618 mq->mqrq_cur->packed_num = ++reqs;
1619 mq->mqrq_cur->packed_retries = reqs;
1620 return reqs;
1621 }
1622
1623no_packed:
1624 mmc_blk_clear_packed(mq->mqrq_cur);
1625 return 0;
1626}
1627
1628static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1629 struct mmc_card *card,
1630 struct mmc_queue *mq)
1631{
1632 struct mmc_blk_request *brq = &mqrq->brq;
1633 struct request *req = mqrq->req;
1634 struct request *prq;
1635 struct mmc_blk_data *md = mq->data;
1636 bool do_rel_wr, do_data_tag;
1637 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1638 u8 i = 1;
1639
1640 mqrq->packed_cmd = MMC_PACKED_WRITE;
1641 mqrq->packed_blocks = 0;
1642 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1643
1644 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1645 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1646 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1647
1648 /*
1649 * Argument for each entry of packed group
1650 */
1651 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1652 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1653 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1654 (prq->cmd_flags & REQ_META) &&
1655 (rq_data_dir(prq) == WRITE) &&
1656 ((brq->data.blocks * brq->data.blksz) >=
1657 card->ext_csd.data_tag_unit_size);
1658 /* Argument of CMD23 */
1659 packed_cmd_hdr[(i * 2)] =
1660 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1661 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1662 blk_rq_sectors(prq);
1663 /* Argument of CMD18 or CMD25 */
1664 packed_cmd_hdr[((i * 2)) + 1] =
1665 mmc_card_blockaddr(card) ?
1666 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1667 mqrq->packed_blocks += blk_rq_sectors(prq);
1668 i++;
1669 }
1670
1671 memset(brq, 0, sizeof(struct mmc_blk_request));
1672 brq->mrq.cmd = &brq->cmd;
1673 brq->mrq.data = &brq->data;
1674 brq->mrq.sbc = &brq->sbc;
1675 brq->mrq.stop = &brq->stop;
1676
1677 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1678 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1679 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1680
1681 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1682 brq->cmd.arg = blk_rq_pos(req);
1683 if (!mmc_card_blockaddr(card))
1684 brq->cmd.arg <<= 9;
1685 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1686
1687 brq->data.blksz = 512;
1688 brq->data.blocks = mqrq->packed_blocks + 1;
1689 brq->data.flags |= MMC_DATA_WRITE;
1690
1691 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1692 brq->stop.arg = 0;
1693 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1694
1695 mmc_set_data_timeout(&brq->data, card);
1696
1697 brq->data.sg = mqrq->sg;
1698 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1699
1700 mqrq->mmc_active.mrq = &brq->mrq;
1701 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1702
1703 mmc_queue_bounce_pre(mqrq);
1704}
1705
Adrian Hunter67716322011-08-29 16:42:15 +03001706static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1707 struct mmc_blk_request *brq, struct request *req,
1708 int ret)
1709{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001710 struct mmc_queue_req *mq_rq;
1711 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1712
Adrian Hunter67716322011-08-29 16:42:15 +03001713 /*
1714 * If this is an SD card and we're writing, we can first
1715 * mark the known good sectors as ok.
1716 *
1717 * If the card is not SD, we can still ok written sectors
1718 * as reported by the controller (which might be less than
1719 * the real number of written sectors, but never more).
1720 */
1721 if (mmc_card_sd(card)) {
1722 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301723 if (!brq->data.fault_injected) {
1724 blocks = mmc_sd_num_wr_blocks(card);
1725 if (blocks != (u32)-1)
1726 ret = blk_end_request(req, 0, blocks << 9);
1727 } else
1728 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001729 } else {
1730 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1731 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1732 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001733 return ret;
1734}
1735
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001736static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1737{
1738 struct request *prq;
1739 int idx = mq_rq->packed_fail_idx, i = 0;
1740 int ret = 0;
1741
1742 while (!list_empty(&mq_rq->packed_list)) {
1743 prq = list_entry_rq(mq_rq->packed_list.next);
1744 if (idx == i) {
1745 /* retry from error index */
1746 mq_rq->packed_num -= idx;
1747 mq_rq->req = prq;
1748 ret = 1;
1749
1750 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1751 list_del_init(&prq->queuelist);
1752 mmc_blk_clear_packed(mq_rq);
1753 }
1754 return ret;
1755 }
1756 list_del_init(&prq->queuelist);
1757 blk_end_request(prq, 0, blk_rq_bytes(prq));
1758 i++;
1759 }
1760
1761 mmc_blk_clear_packed(mq_rq);
1762 return ret;
1763}
1764static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1765{
1766 struct request *prq;
1767
1768 while (!list_empty(&mq_rq->packed_list)) {
1769 prq = list_entry_rq(mq_rq->packed_list.next);
1770 list_del_init(&prq->queuelist);
1771 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1772 }
1773
1774 mmc_blk_clear_packed(mq_rq);
1775}
1776
1777static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1778 struct mmc_queue_req *mq_rq)
1779{
1780 struct request *prq;
1781 struct request_queue *q = mq->queue;
1782
1783 while (!list_empty(&mq_rq->packed_list)) {
1784 prq = list_entry_rq(mq_rq->packed_list.prev);
1785 if (prq->queuelist.prev != &mq_rq->packed_list) {
1786 list_del_init(&prq->queuelist);
1787 spin_lock_irq(q->queue_lock);
1788 blk_requeue_request(mq->queue, prq);
1789 spin_unlock_irq(q->queue_lock);
1790 } else {
1791 list_del_init(&prq->queuelist);
1792 }
1793 }
1794
1795 mmc_blk_clear_packed(mq_rq);
1796}
1797
Per Forlinee8a43a2011-07-01 18:55:33 +02001798static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001799{
1800 struct mmc_blk_data *md = mq->data;
1801 struct mmc_card *card = md->queue.card;
1802 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001803 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001804 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001805 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001806 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001807 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001808 const u8 packed_num = 2;
1809 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001810
1811 if (!rqc && !mq->mqrq_prev->req)
1812 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001813
Maya Erez5f360692012-10-10 03:47:54 +02001814 if (rqc) {
1815 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1816 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001817 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001818 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001819
Per Forlin54d49d772011-07-01 18:55:29 +02001820 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001821 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001822 if (reqs >= packed_num)
1823 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1824 card, mq);
1825 else
1826 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001827 areq = &mq->mqrq_cur->mmc_active;
1828 } else
1829 areq = NULL;
1830 areq = mmc_start_req(card->host, areq, (int *) &status);
1831 if (!areq)
1832 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001833
Per Forlinee8a43a2011-07-01 18:55:33 +02001834 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1835 brq = &mq_rq->brq;
1836 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001837 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001838 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001839
Per Forlind78d4a82011-07-01 18:55:30 +02001840 switch (status) {
1841 case MMC_BLK_SUCCESS:
1842 case MMC_BLK_PARTIAL:
1843 /*
1844 * A block was successfully transferred.
1845 */
Adrian Hunter67716322011-08-29 16:42:15 +03001846 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001847
1848 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1849 ret = mmc_blk_end_packed_req(mq_rq);
1850 break;
1851 } else {
1852 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001853 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001854 }
1855
Adrian Hunter67716322011-08-29 16:42:15 +03001856 /*
1857 * If the blk_end_request function returns non-zero even
1858 * though all data has been transferred and no errors
1859 * were returned by the host controller, it's a bug.
1860 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001861 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301862 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001863 __func__, blk_rq_bytes(req),
1864 brq->data.bytes_xfered);
1865 rqc = NULL;
1866 goto cmd_abort;
1867 }
Per Forlind78d4a82011-07-01 18:55:30 +02001868 break;
1869 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001870 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1871 if (!mmc_blk_reset(md, card->host, type))
1872 break;
1873 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001874 case MMC_BLK_RETRY:
1875 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001876 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001877 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001878 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001879 if (!mmc_blk_reset(md, card->host, type))
1880 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001881 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001882 case MMC_BLK_DATA_ERR: {
1883 int err;
1884
1885 err = mmc_blk_reset(md, card->host, type);
1886 if (!err)
1887 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001888 if (err == -ENODEV ||
1889 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001890 goto cmd_abort;
1891 /* Fall through */
1892 }
1893 case MMC_BLK_ECC_ERR:
1894 if (brq->data.blocks > 1) {
1895 /* Redo read one sector at a time */
1896 pr_warning("%s: retrying using single block read\n",
1897 req->rq_disk->disk_name);
1898 disable_multi = 1;
1899 break;
1900 }
Per Forlind78d4a82011-07-01 18:55:30 +02001901 /*
1902 * After an error, we redo I/O one sector at a
1903 * time, so we only reach here after trying to
1904 * read a single sector.
1905 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301906 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001907 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001908 if (!ret)
1909 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001910 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301911 case MMC_BLK_NOMEDIUM:
1912 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001913 }
1914
Per Forlinee8a43a2011-07-01 18:55:33 +02001915 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001916 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1917 /*
1918 * In case of a incomplete request
1919 * prepare it again and resend.
1920 */
1921 mmc_blk_rw_rq_prep(mq_rq, card,
1922 disable_multi, mq);
1923 mmc_start_req(card->host,
1924 &mq_rq->mmc_active, NULL);
1925 } else {
1926 if (!mq_rq->packed_retries)
1927 goto cmd_abort;
1928 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1929 mmc_start_req(card->host,
1930 &mq_rq->mmc_active, NULL);
1931 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 } while (ret);
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 1;
1936
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001937 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001938 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1939 if (mmc_card_removed(card))
1940 req->cmd_flags |= REQ_QUIET;
1941 while (ret)
1942 ret = blk_end_request(req, -EIO,
1943 blk_rq_cur_bytes(req));
1944 } else {
1945 mmc_blk_abort_packed_req(mq_rq);
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Per Forlinee8a43a2011-07-01 18:55:33 +02001948 start_new_req:
1949 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001950 /*
1951 * If current request is packed, it needs to put back.
1952 */
1953 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1954 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1955
Per Forlinee8a43a2011-07-01 18:55:33 +02001956 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1957 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return 0;
1961}
1962
Adrian Hunterbd788c92010-08-11 14:17:47 -07001963static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1964{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001965 int ret;
1966 struct mmc_blk_data *md = mq->data;
1967 struct mmc_card *card = md->queue.card;
1968
San Mehat148c57a2009-07-30 08:21:19 -07001969#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1970 if (mmc_bus_needs_resume(card->host)) {
1971 mmc_resume_bus(card->host);
1972 mmc_blk_set_blksize(md, card);
1973 }
1974#endif
1975
Per Forlinee8a43a2011-07-01 18:55:33 +02001976 if (req && !mq->mqrq_prev->req)
1977 /* claim host only for the first request */
1978 mmc_claim_host(card->host);
1979
Andrei Warkentin371a6892011-04-11 18:10:25 -05001980 ret = mmc_blk_part_switch(card, md);
1981 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001982 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301983 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001984 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001985 ret = 0;
1986 goto out;
1987 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001988
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001989 mmc_blk_write_packing_control(mq, req);
1990
Maya Erez463bb952012-05-24 23:46:29 +03001991 if (req && req->cmd_flags & REQ_SANITIZE) {
1992 /* complete ongoing async transfer before issuing sanitize */
1993 if (card->host && card->host->areq)
1994 mmc_blk_issue_rw_rq(mq, NULL);
1995 ret = mmc_blk_issue_sanitize_rq(mq, req);
1996 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001997 /* complete ongoing async transfer before issuing discard */
1998 if (card->host->areq)
1999 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002000 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002001 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002002 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002003 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002004 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002005 /* complete ongoing async transfer before issuing flush */
2006 if (card->host->areq)
2007 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002008 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002009 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002010 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002011 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002012
Andrei Warkentin371a6892011-04-11 18:10:25 -05002013out:
Per Forlinee8a43a2011-07-01 18:55:33 +02002014 if (!req)
2015 /* release host only when there are no more requests */
2016 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002017 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Russell Kinga6f6c962006-01-03 22:38:44 +00002020static inline int mmc_blk_readonly(struct mmc_card *card)
2021{
2022 return mmc_card_readonly(card) ||
2023 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2024}
2025
Andrei Warkentin371a6892011-04-11 18:10:25 -05002026static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2027 struct device *parent,
2028 sector_t size,
2029 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002030 const char *subname,
2031 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct mmc_blk_data *md;
2034 int devidx, ret;
2035
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002036 devidx = find_first_zero_bit(dev_use, max_devices);
2037 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return ERR_PTR(-ENOSPC);
2039 __set_bit(devidx, dev_use);
2040
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002041 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002042 if (!md) {
2043 ret = -ENOMEM;
2044 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002046
Russell Kinga6f6c962006-01-03 22:38:44 +00002047 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002048 * !subname implies we are creating main mmc_blk_data that will be
2049 * associated with mmc_card with mmc_set_drvdata. Due to device
2050 * partitions, devidx will not coincide with a per-physical card
2051 * index anymore so we keep track of a name index.
2052 */
2053 if (!subname) {
2054 md->name_idx = find_first_zero_bit(name_use, max_devices);
2055 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002056 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002057 md->name_idx = ((struct mmc_blk_data *)
2058 dev_to_disk(parent)->private_data)->name_idx;
2059
Johan Rudholmadd710e2011-12-02 08:51:06 +01002060 md->area_type = area_type;
2061
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002062 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002063 * Set the read-only status based on the supported commands
2064 * and the write protect switch.
2065 */
2066 md->read_only = mmc_blk_readonly(card);
2067
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002068 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002069 if (md->disk == NULL) {
2070 ret = -ENOMEM;
2071 goto err_kfree;
2072 }
2073
2074 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002075 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002076 md->usage = 1;
2077
Adrian Hunterd09408a2011-06-23 13:40:28 +03002078 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002079 if (ret)
2080 goto err_putdisk;
2081
Russell Kinga6f6c962006-01-03 22:38:44 +00002082 md->queue.issue_fn = mmc_blk_issue_rq;
2083 md->queue.data = md;
2084
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002085 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002086 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002087 md->disk->fops = &mmc_bdops;
2088 md->disk->private_data = md;
2089 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002090 md->disk->driverfs_dev = parent;
2091 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002092 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002093
2094 /*
2095 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2096 *
2097 * - be set for removable media with permanent block devices
2098 * - be unset for removable block devices with permanent media
2099 *
2100 * Since MMC block devices clearly fall under the second
2101 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2102 * should use the block device creation/destruction hotplug
2103 * messages to tell when the card is present.
2104 */
2105
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002106 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2107 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002108
Martin K. Petersene1defc42009-05-22 17:17:49 -04002109 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002110 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002111
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002112 if (mmc_host_cmd23(card->host)) {
2113 if (mmc_card_mmc(card) ||
2114 (mmc_card_sd(card) &&
2115 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2116 md->flags |= MMC_BLK_CMD23;
2117 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002118
2119 if (mmc_card_mmc(card) &&
2120 md->flags & MMC_BLK_CMD23 &&
2121 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2122 card->ext_csd.rel_sectors)) {
2123 md->flags |= MMC_BLK_REL_WR;
2124 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2125 }
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002128
2129 err_putdisk:
2130 put_disk(md->disk);
2131 err_kfree:
2132 kfree(md);
2133 out:
2134 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Andrei Warkentin371a6892011-04-11 18:10:25 -05002137static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2138{
2139 sector_t size;
2140 struct mmc_blk_data *md;
2141
2142 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2143 /*
2144 * The EXT_CSD sector count is in number or 512 byte
2145 * sectors.
2146 */
2147 size = card->ext_csd.sectors;
2148 } else {
2149 /*
2150 * The CSD capacity field is in units of read_blkbits.
2151 * set_capacity takes units of 512 bytes.
2152 */
2153 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2154 }
2155
Johan Rudholmadd710e2011-12-02 08:51:06 +01002156 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2157 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002158 return md;
2159}
2160
2161static int mmc_blk_alloc_part(struct mmc_card *card,
2162 struct mmc_blk_data *md,
2163 unsigned int part_type,
2164 sector_t size,
2165 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002166 const char *subname,
2167 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002168{
2169 char cap_str[10];
2170 struct mmc_blk_data *part_md;
2171
2172 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002173 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002174 if (IS_ERR(part_md))
2175 return PTR_ERR(part_md);
2176 part_md->part_type = part_type;
2177 list_add(&part_md->part, &md->part);
2178
2179 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2180 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302181 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182 part_md->disk->disk_name, mmc_card_id(card),
2183 mmc_card_name(card), part_md->part_type, cap_str);
2184 return 0;
2185}
2186
Namjae Jeone0c368d2011-10-06 23:41:38 +09002187/* MMC Physical partitions consist of two boot partitions and
2188 * up to four general purpose partitions.
2189 * For each partition enabled in EXT_CSD a block device will be allocatedi
2190 * to provide access to the partition.
2191 */
2192
Andrei Warkentin371a6892011-04-11 18:10:25 -05002193static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2194{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002195 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002196
2197 if (!mmc_card_mmc(card))
2198 return 0;
2199
Namjae Jeone0c368d2011-10-06 23:41:38 +09002200 for (idx = 0; idx < card->nr_parts; idx++) {
2201 if (card->part[idx].size) {
2202 ret = mmc_blk_alloc_part(card, md,
2203 card->part[idx].part_cfg,
2204 card->part[idx].size >> 9,
2205 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002206 card->part[idx].name,
2207 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002208 if (ret)
2209 return ret;
2210 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002211 }
2212
2213 return ret;
2214}
2215
Andrei Warkentin371a6892011-04-11 18:10:25 -05002216static void mmc_blk_remove_req(struct mmc_blk_data *md)
2217{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002218 struct mmc_card *card;
2219
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002221 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002222 device_remove_file(disk_to_dev(md->disk),
2223 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002224 if (md->disk->flags & GENHD_FL_UP) {
2225 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002226 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2227 card->ext_csd.boot_ro_lockable)
2228 device_remove_file(disk_to_dev(md->disk),
2229 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002230
2231 /* Stop new requests from getting into the queue */
2232 del_gendisk(md->disk);
2233 }
2234
2235 /* Then flush out any already in there */
2236 mmc_cleanup_queue(&md->queue);
2237 mmc_blk_put(md);
2238 }
2239}
2240
2241static void mmc_blk_remove_parts(struct mmc_card *card,
2242 struct mmc_blk_data *md)
2243{
2244 struct list_head *pos, *q;
2245 struct mmc_blk_data *part_md;
2246
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002247 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002248 list_for_each_safe(pos, q, &md->part) {
2249 part_md = list_entry(pos, struct mmc_blk_data, part);
2250 list_del(pos);
2251 mmc_blk_remove_req(part_md);
2252 }
2253}
2254
2255static int mmc_add_disk(struct mmc_blk_data *md)
2256{
2257 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002258 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002259
2260 add_disk(md->disk);
2261 md->force_ro.show = force_ro_show;
2262 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302263 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002264 md->force_ro.attr.name = "force_ro";
2265 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2266 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2267 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002268 goto force_ro_fail;
2269
2270 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2271 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002272 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002273
2274 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2275 mode = S_IRUGO;
2276 else
2277 mode = S_IRUGO | S_IWUSR;
2278
2279 md->power_ro_lock.show = power_ro_lock_show;
2280 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002281 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002282 md->power_ro_lock.attr.mode = mode;
2283 md->power_ro_lock.attr.name =
2284 "ro_lock_until_next_power_on";
2285 ret = device_create_file(disk_to_dev(md->disk),
2286 &md->power_ro_lock);
2287 if (ret)
2288 goto power_ro_lock_fail;
2289 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002290
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002291 md->num_wr_reqs_to_start_packing.show =
2292 num_wr_reqs_to_start_packing_show;
2293 md->num_wr_reqs_to_start_packing.store =
2294 num_wr_reqs_to_start_packing_store;
2295 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2296 md->num_wr_reqs_to_start_packing.attr.name =
2297 "num_wr_reqs_to_start_packing";
2298 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2299 ret = device_create_file(disk_to_dev(md->disk),
2300 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002301 if (ret)
2302 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002303
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002304 md->min_sectors_to_check_bkops_status.show =
2305 min_sectors_to_check_bkops_status_show;
2306 md->min_sectors_to_check_bkops_status.store =
2307 min_sectors_to_check_bkops_status_store;
2308 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2309 md->min_sectors_to_check_bkops_status.attr.name =
2310 "min_sectors_to_check_bkops_status";
2311 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2312 ret = device_create_file(disk_to_dev(md->disk),
2313 &md->min_sectors_to_check_bkops_status);
2314 if (ret)
2315 goto power_ro_lock_fail;
2316
Johan Rudholmadd710e2011-12-02 08:51:06 +01002317 return ret;
2318
2319power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002320 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002321force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002322 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002323
2324 return ret;
2325}
2326
Chris Ballc59d4472011-11-11 22:01:43 -05002327#define CID_MANFID_SANDISK 0x2
2328#define CID_MANFID_TOSHIBA 0x11
2329#define CID_MANFID_MICRON 0x13
2330
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002331static const struct mmc_fixup blk_fixups[] =
2332{
Chris Ballc59d4472011-11-11 22:01:43 -05002333 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2334 MMC_QUIRK_INAND_CMD38),
2335 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2336 MMC_QUIRK_INAND_CMD38),
2337 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2338 MMC_QUIRK_INAND_CMD38),
2339 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2340 MMC_QUIRK_INAND_CMD38),
2341 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2342 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002343
2344 /*
2345 * Some MMC cards experience performance degradation with CMD23
2346 * instead of CMD12-bounded multiblock transfers. For now we'll
2347 * black list what's bad...
2348 * - Certain Toshiba cards.
2349 *
2350 * N.B. This doesn't affect SD cards.
2351 */
Chris Ballc59d4472011-11-11 22:01:43 -05002352 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002353 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002354 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002355 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002356 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002357 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002358
2359 /*
2360 * Some Micron MMC cards needs longer data read timeout than
2361 * indicated in CSD.
2362 */
Chris Ballc59d4472011-11-11 22:01:43 -05002363 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002364 MMC_QUIRK_LONG_READ_TIME),
2365
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302366 /* Some INAND MCP devices advertise incorrect timeout values */
2367 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2368 MMC_QUIRK_INAND_DATA_TIMEOUT),
2369
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002370 END_FIXUP
2371};
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373static int mmc_blk_probe(struct mmc_card *card)
2374{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002375 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002376 char cap_str[10];
2377
Pierre Ossman912490d2005-05-21 10:27:02 +01002378 /*
2379 * Check that the card supports the command class(es) we need.
2380 */
2381 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return -ENODEV;
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 md = mmc_blk_alloc(card);
2385 if (IS_ERR(md))
2386 return PTR_ERR(md);
2387
Yi Li444122f2009-02-05 15:31:57 +08002388 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002389 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302390 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002392 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Andrei Warkentin371a6892011-04-11 18:10:25 -05002394 if (mmc_blk_alloc_parts(card, md))
2395 goto out;
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002398 mmc_fixup_device(card, blk_fixups);
2399
San Mehat148c57a2009-07-30 08:21:19 -07002400#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2401 mmc_set_bus_resume_policy(card->host, 1);
2402#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002403 if (mmc_add_disk(md))
2404 goto out;
2405
2406 list_for_each_entry(part_md, &md->part, part) {
2407 if (mmc_add_disk(part_md))
2408 goto out;
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 0;
2411
2412 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002413 mmc_blk_remove_parts(card, md);
2414 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418static void mmc_blk_remove(struct mmc_card *card)
2419{
2420 struct mmc_blk_data *md = mmc_get_drvdata(card);
2421
Andrei Warkentin371a6892011-04-11 18:10:25 -05002422 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002423 mmc_claim_host(card->host);
2424 mmc_blk_part_switch(card, md);
2425 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002426 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002428#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2429 mmc_set_bus_resume_policy(card->host, 0);
2430#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002434static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002436 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 struct mmc_blk_data *md = mmc_get_drvdata(card);
2438
2439 if (md) {
2440 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002441 list_for_each_entry(part_md, &md->part, part) {
2442 mmc_queue_suspend(&part_md->queue);
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 return 0;
2446}
2447
2448static int mmc_blk_resume(struct mmc_card *card)
2449{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002450 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct mmc_blk_data *md = mmc_get_drvdata(card);
2452
2453 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002454 /*
2455 * Resume involves the card going into idle state,
2456 * so current partition is always the main one.
2457 */
2458 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002460 list_for_each_entry(part_md, &md->part, part) {
2461 mmc_queue_resume(&part_md->queue);
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 return 0;
2465}
2466#else
2467#define mmc_blk_suspend NULL
2468#define mmc_blk_resume NULL
2469#endif
2470
2471static struct mmc_driver mmc_driver = {
2472 .drv = {
2473 .name = "mmcblk",
2474 },
2475 .probe = mmc_blk_probe,
2476 .remove = mmc_blk_remove,
2477 .suspend = mmc_blk_suspend,
2478 .resume = mmc_blk_resume,
2479};
2480
2481static int __init mmc_blk_init(void)
2482{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002483 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002485 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2486 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2487
2488 max_devices = 256 / perdev_minors;
2489
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002490 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2491 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002494 res = mmc_register_driver(&mmc_driver);
2495 if (res)
2496 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002498 return 0;
2499 out2:
2500 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 out:
2502 return res;
2503}
2504
2505static void __exit mmc_blk_exit(void)
2506{
2507 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002508 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511module_init(mmc_blk_init);
2512module_exit(mmc_blk_exit);
2513
2514MODULE_LICENSE("GPL");
2515MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2516