blob: b9cd8ecd5573ea474195eedb98936f71f3462a7f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon121b8e82012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman10217bc2012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon121b8e82012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200129enum {
130 MMC_PACKED_N_IDX = -1,
131 MMC_PACKED_N_ZERO,
132 MMC_PACKED_N_SINGLE,
133};
134
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400135module_param(perdev_minors, int, 0444);
136MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
137
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200138static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
139{
140 mqrq->packed_cmd = MMC_PACKED_NONE;
141 mqrq->packed_num = MMC_PACKED_N_ZERO;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
145{
146 struct mmc_blk_data *md;
147
Arjan van de Vena621aae2006-01-12 18:43:35 +0000148 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 md = disk->private_data;
150 if (md && md->usage == 0)
151 md = NULL;
152 if (md)
153 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000154 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return md;
157}
158
Andrei Warkentin371a6892011-04-11 18:10:25 -0500159static inline int mmc_get_devidx(struct gendisk *disk)
160{
Colin Cross71b54d42010-09-03 12:41:21 -0700161 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500162 return devidx;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void mmc_blk_put(struct mmc_blk_data *md)
166{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000167 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 md->usage--;
169 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500170 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800171 blk_cleanup_queue(md->queue.queue);
172
David Woodhouse1dff3142007-11-21 18:45:12 +0100173 __clear_bit(devidx, dev_use);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(md);
177 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Johan Rudholmadd710e2011-12-02 08:51:06 +0100181static ssize_t power_ro_lock_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
183{
184 int ret;
185 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
186 struct mmc_card *card = md->queue.card;
187 int locked = 0;
188
189 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
190 locked = 2;
191 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
192 locked = 1;
193
194 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
195
196 return ret;
197}
198
199static ssize_t power_ro_lock_store(struct device *dev,
200 struct device_attribute *attr, const char *buf, size_t count)
201{
202 int ret;
203 struct mmc_blk_data *md, *part_md;
204 struct mmc_card *card;
205 unsigned long set;
206
207 if (kstrtoul(buf, 0, &set))
208 return -EINVAL;
209
210 if (set != 1)
211 return count;
212
213 md = mmc_blk_get(dev_to_disk(dev));
214 card = md->queue.card;
215
216 mmc_claim_host(card->host);
217
218 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
219 card->ext_csd.boot_ro_lock |
220 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
221 card->ext_csd.part_time);
222 if (ret)
223 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
224 else
225 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
226
227 mmc_release_host(card->host);
228
229 if (!ret) {
230 pr_info("%s: Locking boot partition ro until next power on\n",
231 md->disk->disk_name);
232 set_disk_ro(md->disk, 1);
233
234 list_for_each_entry(part_md, &md->part, part)
235 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
236 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
237 set_disk_ro(part_md->disk, 1);
238 }
239 }
240
241 mmc_blk_put(md);
242 return count;
243}
244
Andrei Warkentin371a6892011-04-11 18:10:25 -0500245static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
246 char *buf)
247{
248 int ret;
249 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
250
251 ret = snprintf(buf, PAGE_SIZE, "%d",
252 get_disk_ro(dev_to_disk(dev)) ^
253 md->read_only);
254 mmc_blk_put(md);
255 return ret;
256}
257
258static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t count)
260{
261 int ret;
262 char *end;
263 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
264 unsigned long set = simple_strtoul(buf, &end, 0);
265 if (end == buf) {
266 ret = -EINVAL;
267 goto out;
268 }
269
270 set_disk_ro(dev_to_disk(dev), set || md->read_only);
271 ret = count;
272out:
273 mmc_blk_put(md);
274 return ret;
275}
276
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200277static ssize_t
278num_wr_reqs_to_start_packing_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
281 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
282 int num_wr_reqs_to_start_packing;
283 int ret;
284
285 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
286
287 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
288
289 mmc_blk_put(md);
290 return ret;
291}
292
293static ssize_t
294num_wr_reqs_to_start_packing_store(struct device *dev,
295 struct device_attribute *attr,
296 const char *buf, size_t count)
297{
298 int value;
299 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
300
301 sscanf(buf, "%d", &value);
302 if (value >= 0)
303 md->queue.num_wr_reqs_to_start_packing = value;
304
305 mmc_blk_put(md);
306 return count;
307}
308
Al Viroa5a15612008-03-02 10:33:30 -0500309static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Al Viroa5a15612008-03-02 10:33:30 -0500311 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int ret = -ENXIO;
313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (md) {
316 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500317 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700319
Al Viroa5a15612008-03-02 10:33:30 -0500320 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700321 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700322 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200325 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return ret;
328}
329
Al Viroa5a15612008-03-02 10:33:30 -0500330static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Al Viroa5a15612008-03-02 10:33:30 -0500332 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200334 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200336 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
340static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800341mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800343 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
344 geo->heads = 4;
345 geo->sectors = 16;
346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
John Calixtocb87ea22011-04-26 18:56:29 -0400349struct mmc_blk_ioc_data {
350 struct mmc_ioc_cmd ic;
351 unsigned char *buf;
352 u64 buf_bytes;
353};
354
355static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
356 struct mmc_ioc_cmd __user *user)
357{
358 struct mmc_blk_ioc_data *idata;
359 int err;
360
361 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
362 if (!idata) {
363 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400364 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400365 }
366
367 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
368 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400369 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400370 }
371
372 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
373 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
374 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400376 }
377
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100378 if (!idata->buf_bytes)
379 return idata;
380
John Calixtocb87ea22011-04-26 18:56:29 -0400381 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
382 if (!idata->buf) {
383 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400384 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400385 }
386
387 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
388 idata->ic.data_ptr, idata->buf_bytes)) {
389 err = -EFAULT;
390 goto copy_err;
391 }
392
393 return idata;
394
395copy_err:
396 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400397idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400398 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400399out:
John Calixtocb87ea22011-04-26 18:56:29 -0400400 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400401}
402
403static int mmc_blk_ioctl_cmd(struct block_device *bdev,
404 struct mmc_ioc_cmd __user *ic_ptr)
405{
406 struct mmc_blk_ioc_data *idata;
407 struct mmc_blk_data *md;
408 struct mmc_card *card;
409 struct mmc_command cmd = {0};
410 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530411 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400412 struct scatterlist sg;
413 int err;
414
415 /*
416 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
417 * whole block device, not on a partition. This prevents overspray
418 * between sibling partitions.
419 */
420 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
421 return -EPERM;
422
423 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
424 if (IS_ERR(idata))
425 return PTR_ERR(idata);
426
John Calixtocb87ea22011-04-26 18:56:29 -0400427 md = mmc_blk_get(bdev->bd_disk);
428 if (!md) {
429 err = -EINVAL;
430 goto cmd_done;
431 }
432
433 card = md->queue.card;
434 if (IS_ERR(card)) {
435 err = PTR_ERR(card);
436 goto cmd_done;
437 }
438
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100439 cmd.opcode = idata->ic.opcode;
440 cmd.arg = idata->ic.arg;
441 cmd.flags = idata->ic.flags;
442
443 if (idata->buf_bytes) {
444 data.sg = &sg;
445 data.sg_len = 1;
446 data.blksz = idata->ic.blksz;
447 data.blocks = idata->ic.blocks;
448
449 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
450
451 if (idata->ic.write_flag)
452 data.flags = MMC_DATA_WRITE;
453 else
454 data.flags = MMC_DATA_READ;
455
456 /* data.flags must already be set before doing this. */
457 mmc_set_data_timeout(&data, card);
458
459 /* Allow overriding the timeout_ns for empirical tuning. */
460 if (idata->ic.data_timeout_ns)
461 data.timeout_ns = idata->ic.data_timeout_ns;
462
463 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
464 /*
465 * Pretend this is a data transfer and rely on the
466 * host driver to compute timeout. When all host
467 * drivers support cmd.cmd_timeout for R1B, this
468 * can be changed to:
469 *
470 * mrq.data = NULL;
471 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
472 */
473 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
474 }
475
476 mrq.data = &data;
477 }
478
479 mrq.cmd = &cmd;
480
John Calixtocb87ea22011-04-26 18:56:29 -0400481 mmc_claim_host(card->host);
482
483 if (idata->ic.is_acmd) {
484 err = mmc_app_cmd(card->host, card);
485 if (err)
486 goto cmd_rel_host;
487 }
488
John Calixtocb87ea22011-04-26 18:56:29 -0400489 mmc_wait_for_req(card->host, &mrq);
490
491 if (cmd.error) {
492 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
493 __func__, cmd.error);
494 err = cmd.error;
495 goto cmd_rel_host;
496 }
497 if (data.error) {
498 dev_err(mmc_dev(card->host), "%s: data error %d\n",
499 __func__, data.error);
500 err = data.error;
501 goto cmd_rel_host;
502 }
503
504 /*
505 * According to the SD specs, some commands require a delay after
506 * issuing the command.
507 */
508 if (idata->ic.postsleep_min_us)
509 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
510
511 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
512 err = -EFAULT;
513 goto cmd_rel_host;
514 }
515
516 if (!idata->ic.write_flag) {
517 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
518 idata->buf, idata->buf_bytes)) {
519 err = -EFAULT;
520 goto cmd_rel_host;
521 }
522 }
523
524cmd_rel_host:
525 mmc_release_host(card->host);
526
527cmd_done:
528 mmc_blk_put(md);
529 kfree(idata->buf);
530 kfree(idata);
531 return err;
532}
533
534static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
535 unsigned int cmd, unsigned long arg)
536{
537 int ret = -EINVAL;
538 if (cmd == MMC_IOC_CMD)
539 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
540 return ret;
541}
542
543#ifdef CONFIG_COMPAT
544static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
545 unsigned int cmd, unsigned long arg)
546{
547 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
548}
549#endif
550
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700551static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500552 .open = mmc_blk_open,
553 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800554 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400556 .ioctl = mmc_blk_ioctl,
557#ifdef CONFIG_COMPAT
558 .compat_ioctl = mmc_blk_compat_ioctl,
559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560};
561
Andrei Warkentin371a6892011-04-11 18:10:25 -0500562static inline int mmc_blk_part_switch(struct mmc_card *card,
563 struct mmc_blk_data *md)
564{
565 int ret;
566 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300567
Andrei Warkentin371a6892011-04-11 18:10:25 -0500568 if (main_md->part_curr == md->part_type)
569 return 0;
570
571 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300572 u8 part_config = card->ext_csd.part_config;
573
574 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
575 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500576
577 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300578 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500579 card->ext_csd.part_time);
580 if (ret)
581 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300582
583 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300584 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500585
586 main_md->part_curr = md->part_type;
587 return 0;
588}
589
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700590static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
591{
592 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100593 u32 result;
594 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700595
Venkatraman Sad5fd972011-08-25 00:30:50 +0530596 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400597 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400598 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700599
600 struct scatterlist sg;
601
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700602 cmd.opcode = MMC_APP_CMD;
603 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700604 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700605
606 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700607 if (err)
608 return (u32)-1;
609 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700610 return (u32)-1;
611
612 memset(&cmd, 0, sizeof(struct mmc_command));
613
614 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
615 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700616 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700617
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700618 data.blksz = 4;
619 data.blocks = 1;
620 data.flags = MMC_DATA_READ;
621 data.sg = &sg;
622 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530623 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700624
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700625 mrq.cmd = &cmd;
626 mrq.data = &data;
627
Ben Dooks051913d2009-06-08 23:33:57 +0100628 blocks = kmalloc(4, GFP_KERNEL);
629 if (!blocks)
630 return (u32)-1;
631
632 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700633
634 mmc_wait_for_req(card->host, &mrq);
635
Ben Dooks051913d2009-06-08 23:33:57 +0100636 result = ntohl(*blocks);
637 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700638
Ben Dooks051913d2009-06-08 23:33:57 +0100639 if (cmd.error || data.error)
640 result = (u32)-1;
641
642 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700643}
644
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100645static int send_stop(struct mmc_card *card, u32 *status)
646{
647 struct mmc_command cmd = {0};
648 int err;
649
650 cmd.opcode = MMC_STOP_TRANSMISSION;
651 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
652 err = mmc_wait_for_cmd(card->host, &cmd, 5);
653 if (err == 0)
654 *status = cmd.resp[0];
655 return err;
656}
657
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100658static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300659{
Chris Ball1278dba2011-04-13 23:40:30 -0400660 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300661 int err;
662
Adrian Hunter504f1912008-10-16 12:55:25 +0300663 cmd.opcode = MMC_SEND_STATUS;
664 if (!mmc_host_is_spi(card->host))
665 cmd.arg = card->rca << 16;
666 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100667 err = mmc_wait_for_cmd(card->host, &cmd, retries);
668 if (err == 0)
669 *status = cmd.resp[0];
670 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300671}
672
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530673#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100674#define ERR_RETRY 2
675#define ERR_ABORT 1
676#define ERR_CONTINUE 0
677
678static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
679 bool status_valid, u32 status)
680{
681 switch (error) {
682 case -EILSEQ:
683 /* response crc error, retry the r/w cmd */
684 pr_err("%s: %s sending %s command, card status %#x\n",
685 req->rq_disk->disk_name, "response CRC error",
686 name, status);
687 return ERR_RETRY;
688
689 case -ETIMEDOUT:
690 pr_err("%s: %s sending %s command, card status %#x\n",
691 req->rq_disk->disk_name, "timed out", name, status);
692
693 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700694 if (!status_valid) {
695 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100696 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700697 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100698 /*
699 * If it was a r/w cmd crc error, or illegal command
700 * (eg, issued in wrong state) then retry - we should
701 * have corrected the state problem above.
702 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700703 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
704 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100705 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700706 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100707
708 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700709 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100710 return ERR_ABORT;
711
712 default:
713 /* We don't understand the error code the driver gave us */
714 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
715 req->rq_disk->disk_name, error, status);
716 return ERR_ABORT;
717 }
718}
719
720/*
721 * Initial r/w and stop cmd error recovery.
722 * We don't know whether the card received the r/w cmd or not, so try to
723 * restore things back to a sane state. Essentially, we do this as follows:
724 * - Obtain card status. If the first attempt to obtain card status fails,
725 * the status word will reflect the failed status cmd, not the failed
726 * r/w cmd. If we fail to obtain card status, it suggests we can no
727 * longer communicate with the card.
728 * - Check the card state. If the card received the cmd but there was a
729 * transient problem with the response, it might still be in a data transfer
730 * mode. Try to send it a stop command. If this fails, we can't recover.
731 * - If the r/w cmd failed due to a response CRC error, it was probably
732 * transient, so retry the cmd.
733 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
734 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
735 * illegal cmd, retry.
736 * Otherwise we don't understand what happened, so abort.
737 */
738static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300739 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100740{
741 bool prev_cmd_status_valid = true;
742 u32 status, stop_status = 0;
743 int err, retry;
744
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530745 if (mmc_card_removed(card))
746 return ERR_NOMEDIUM;
747
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100748 /*
749 * Try to get card status which indicates both the card state
750 * and why there was no response. If the first attempt fails,
751 * we can't be sure the returned status is for the r/w command.
752 */
753 for (retry = 2; retry >= 0; retry--) {
754 err = get_card_status(card, &status, 0);
755 if (!err)
756 break;
757
758 prev_cmd_status_valid = false;
759 pr_err("%s: error %d sending status command, %sing\n",
760 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
761 }
762
763 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530764 if (err) {
765 /* Check if the card is removed */
766 if (mmc_detect_card_removed(card->host))
767 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100768 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530769 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100770
Adrian Hunter67716322011-08-29 16:42:15 +0300771 /* Flag ECC errors */
772 if ((status & R1_CARD_ECC_FAILED) ||
773 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
774 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
775 *ecc_err = 1;
776
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100777 /*
778 * Check the current card state. If it is in some data transfer
779 * mode, tell it to stop (and hopefully transition back to TRAN.)
780 */
781 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
782 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
783 err = send_stop(card, &stop_status);
784 if (err)
785 pr_err("%s: error %d sending stop command\n",
786 req->rq_disk->disk_name, err);
787
788 /*
789 * If the stop cmd also timed out, the card is probably
790 * not present, so abort. Other errors are bad news too.
791 */
792 if (err)
793 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300794 if (stop_status & R1_CARD_ECC_FAILED)
795 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100796 }
797
798 /* Check for set block count errors */
799 if (brq->sbc.error)
800 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
801 prev_cmd_status_valid, status);
802
803 /* Check for r/w command errors */
804 if (brq->cmd.error)
805 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
806 prev_cmd_status_valid, status);
807
Adrian Hunter67716322011-08-29 16:42:15 +0300808 /* Data errors */
809 if (!brq->stop.error)
810 return ERR_CONTINUE;
811
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100812 /* Now for stop errors. These aren't fatal to the transfer. */
813 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
814 req->rq_disk->disk_name, brq->stop.error,
815 brq->cmd.resp[0], status);
816
817 /*
818 * Subsitute in our own stop status as this will give the error
819 * state which happened during the execution of the r/w command.
820 */
821 if (stop_status) {
822 brq->stop.resp[0] = stop_status;
823 brq->stop.error = 0;
824 }
825 return ERR_CONTINUE;
826}
827
Adrian Hunter67716322011-08-29 16:42:15 +0300828static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
829 int type)
830{
831 int err;
832
833 if (md->reset_done & type)
834 return -EEXIST;
835
836 md->reset_done |= type;
837 err = mmc_hw_reset(host);
838 /* Ensure we switch back to the correct partition */
839 if (err != -EOPNOTSUPP) {
840 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
841 int part_err;
842
843 main_md->part_curr = main_md->part_type;
844 part_err = mmc_blk_part_switch(host->card, md);
845 if (part_err) {
846 /*
847 * We have failed to get back into the correct
848 * partition, so we need to abort the whole request.
849 */
850 return -ENODEV;
851 }
852 }
853 return err;
854}
855
856static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
857{
858 md->reset_done &= ~type;
859}
860
Adrian Hunterbd788c92010-08-11 14:17:47 -0700861static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
862{
863 struct mmc_blk_data *md = mq->data;
864 struct mmc_card *card = md->queue.card;
865 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300866 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700867
Adrian Hunterbd788c92010-08-11 14:17:47 -0700868 if (!mmc_can_erase(card)) {
869 err = -EOPNOTSUPP;
870 goto out;
871 }
872
873 from = blk_rq_pos(req);
874 nr = blk_rq_sectors(req);
875
Maya Erez5c46dad2012-10-10 03:47:54 +0200876 if (card->ext_csd.bkops_en)
877 card->bkops_info.sectors_changed += blk_rq_sectors(req);
878
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900879 if (mmc_can_discard(card))
880 arg = MMC_DISCARD_ARG;
881 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700882 arg = MMC_TRIM_ARG;
883 else
884 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300885retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500886 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
887 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
888 INAND_CMD38_ARG_EXT_CSD,
889 arg == MMC_TRIM_ARG ?
890 INAND_CMD38_ARG_TRIM :
891 INAND_CMD38_ARG_ERASE,
892 0);
893 if (err)
894 goto out;
895 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700896 err = mmc_erase(card, from, nr, arg);
897out:
Adrian Hunter67716322011-08-29 16:42:15 +0300898 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
899 goto retry;
900 if (!err)
901 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530902 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700903
Adrian Hunterbd788c92010-08-11 14:17:47 -0700904 return err ? 0 : 1;
905}
906
Adrian Hunter49804542010-08-11 14:17:50 -0700907static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
908 struct request *req)
909{
910 struct mmc_blk_data *md = mq->data;
911 struct mmc_card *card = md->queue.card;
912 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300913 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700914
Maya Erez463bb952012-05-24 23:46:29 +0300915 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700916 err = -EOPNOTSUPP;
917 goto out;
918 }
919
920 from = blk_rq_pos(req);
921 nr = blk_rq_sectors(req);
922
923 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
924 arg = MMC_SECURE_TRIM1_ARG;
925 else
926 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300927retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500928 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
929 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
930 INAND_CMD38_ARG_EXT_CSD,
931 arg == MMC_SECURE_TRIM1_ARG ?
932 INAND_CMD38_ARG_SECTRIM1 :
933 INAND_CMD38_ARG_SECERASE,
934 0);
935 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300936 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500937 }
Adrian Hunter28302812012-04-05 14:45:48 +0300938
Adrian Hunter49804542010-08-11 14:17:50 -0700939 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300940 if (err == -EIO)
941 goto out_retry;
942 if (err)
943 goto out;
944
945 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
947 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
948 INAND_CMD38_ARG_EXT_CSD,
949 INAND_CMD38_ARG_SECTRIM2,
950 0);
951 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300952 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500953 }
Adrian Hunter28302812012-04-05 14:45:48 +0300954
Adrian Hunter49804542010-08-11 14:17:50 -0700955 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300956 if (err == -EIO)
957 goto out_retry;
958 if (err)
959 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500960 }
Adrian Hunter28302812012-04-05 14:45:48 +0300961
962 if (mmc_can_sanitize(card))
963 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
964 EXT_CSD_SANITIZE_START, 1, 0);
965out_retry:
966 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300967 goto retry;
968 if (!err)
969 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300970out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530971 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700972
Adrian Hunter49804542010-08-11 14:17:50 -0700973 return err ? 0 : 1;
974}
975
Maya Erez463bb952012-05-24 23:46:29 +0300976static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
977 struct request *req)
978{
979 struct mmc_blk_data *md = mq->data;
980 struct mmc_card *card = md->queue.card;
981 int err = 0;
982
983 BUG_ON(!card);
984 BUG_ON(!card->host);
985
986 if (!(mmc_can_sanitize(card) &&
987 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
988 pr_warning("%s: %s - SANITIZE is not supported\n",
989 mmc_hostname(card->host), __func__);
990 err = -EOPNOTSUPP;
991 goto out;
992 }
993
994 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
995 mmc_hostname(card->host), __func__);
996
997 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +0300998 EXT_CSD_SANITIZE_START, 1,
999 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001000
1001 if (err)
1002 pr_err("%s: %s - mmc_switch() with "
1003 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1004 mmc_hostname(card->host), __func__, err);
1005
1006 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1007 __func__);
1008
1009out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301010 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001011
1012 return err ? 0 : 1;
1013}
1014
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001015static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1016{
1017 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001018 struct mmc_card *card = md->queue.card;
1019 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001020
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001021 ret = mmc_flush_cache(card);
1022 if (ret)
1023 ret = -EIO;
1024
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301025 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001026
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001027 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001028}
1029
1030/*
1031 * Reformat current write as a reliable write, supporting
1032 * both legacy and the enhanced reliable write MMC cards.
1033 * In each transfer we'll handle only as much as a single
1034 * reliable write can handle, thus finish the request in
1035 * partial completions.
1036 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001037static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1038 struct mmc_card *card,
1039 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001040{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001041 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1042 /* Legacy mode imposes restrictions on transfers. */
1043 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1044 brq->data.blocks = 1;
1045
1046 if (brq->data.blocks > card->ext_csd.rel_sectors)
1047 brq->data.blocks = card->ext_csd.rel_sectors;
1048 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1049 brq->data.blocks = 1;
1050 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001051}
1052
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001053#define CMD_ERRORS \
1054 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1055 R1_ADDRESS_ERROR | /* Misaligned address */ \
1056 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1057 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1058 R1_CC_ERROR | /* Card controller error */ \
1059 R1_ERROR) /* General/unknown error */
1060
Per Forlinee8a43a2011-07-01 18:55:33 +02001061static int mmc_blk_err_check(struct mmc_card *card,
1062 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001063{
Per Forlinee8a43a2011-07-01 18:55:33 +02001064 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1065 mmc_active);
1066 struct mmc_blk_request *brq = &mq_mrq->brq;
1067 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001068 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001069
1070 /*
1071 * sbc.error indicates a problem with the set block count
1072 * command. No data will have been transferred.
1073 *
1074 * cmd.error indicates a problem with the r/w command. No
1075 * data will have been transferred.
1076 *
1077 * stop.error indicates a problem with the stop command. Data
1078 * may have been transferred, or may still be transferring.
1079 */
Adrian Hunter67716322011-08-29 16:42:15 +03001080 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1081 brq->data.error) {
1082 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001083 case ERR_RETRY:
1084 return MMC_BLK_RETRY;
1085 case ERR_ABORT:
1086 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301087 case ERR_NOMEDIUM:
1088 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001089 case ERR_CONTINUE:
1090 break;
1091 }
1092 }
1093
1094 /*
1095 * Check for errors relating to the execution of the
1096 * initial command - such as address errors. No data
1097 * has been transferred.
1098 */
1099 if (brq->cmd.resp[0] & CMD_ERRORS) {
1100 pr_err("%s: r/w command failed, status = %#x\n",
1101 req->rq_disk->disk_name, brq->cmd.resp[0]);
1102 return MMC_BLK_ABORT;
1103 }
1104
1105 /*
1106 * Everything else is either success, or a data error of some
1107 * kind. If it was a write, we may have transitioned to
1108 * program mode, which we have to wait for it to complete.
1109 */
1110 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1111 u32 status;
1112 do {
1113 int err = get_card_status(card, &status, 5);
1114 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301115 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001116 req->rq_disk->disk_name, err);
1117 return MMC_BLK_CMD_ERR;
1118 }
1119 /*
1120 * Some cards mishandle the status bits,
1121 * so make sure to check both the busy
1122 * indication and the card state.
1123 */
1124 } while (!(status & R1_READY_FOR_DATA) ||
1125 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1126 }
1127
1128 if (brq->data.error) {
1129 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1130 req->rq_disk->disk_name, brq->data.error,
1131 (unsigned)blk_rq_pos(req),
1132 (unsigned)blk_rq_sectors(req),
1133 brq->cmd.resp[0], brq->stop.resp[0]);
1134
1135 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001136 if (ecc_err)
1137 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001138 return MMC_BLK_DATA_ERR;
1139 } else {
1140 return MMC_BLK_CMD_ERR;
1141 }
1142 }
1143
Adrian Hunter67716322011-08-29 16:42:15 +03001144 if (!brq->data.bytes_xfered)
1145 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001146
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001147 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1148 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1149 return MMC_BLK_PARTIAL;
1150 else
1151 return MMC_BLK_SUCCESS;
1152 }
1153
Adrian Hunter67716322011-08-29 16:42:15 +03001154 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1155 return MMC_BLK_PARTIAL;
1156
1157 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001158}
1159
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001160static int mmc_blk_packed_err_check(struct mmc_card *card,
1161 struct mmc_async_req *areq)
1162{
1163 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1164 mmc_active);
1165 struct request *req = mq_rq->req;
1166 int err, check, status;
1167 u8 ext_csd[512];
1168
1169 mq_rq->packed_retries--;
1170 check = mmc_blk_err_check(card, areq);
1171 err = get_card_status(card, &status, 0);
1172 if (err) {
1173 pr_err("%s: error %d sending status command\n",
1174 req->rq_disk->disk_name, err);
1175 return MMC_BLK_ABORT;
1176 }
1177
1178 if (status & R1_EXCEPTION_EVENT) {
1179 err = mmc_send_ext_csd(card, ext_csd);
1180 if (err) {
1181 pr_err("%s: error %d sending ext_csd\n",
1182 req->rq_disk->disk_name, err);
1183 return MMC_BLK_ABORT;
1184 }
1185
1186 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1187 EXT_CSD_PACKED_FAILURE) &&
1188 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1189 EXT_CSD_PACKED_GENERIC_ERROR)) {
1190 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1191 EXT_CSD_PACKED_INDEXED_ERROR) {
1192 mq_rq->packed_fail_idx =
1193 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1194 return MMC_BLK_PARTIAL;
1195 }
1196 }
1197 }
1198
1199 return check;
1200}
1201
Per Forlin54d49d772011-07-01 18:55:29 +02001202static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1203 struct mmc_card *card,
1204 int disable_multi,
1205 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Per Forlin54d49d772011-07-01 18:55:29 +02001207 u32 readcmd, writecmd;
1208 struct mmc_blk_request *brq = &mqrq->brq;
1209 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301211 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001213 /*
1214 * Reliable writes are used to implement Forced Unit Access and
1215 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001216 *
1217 * XXX: this really needs a good explanation of why REQ_META
1218 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001219 */
1220 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1221 (req->cmd_flags & REQ_META)) &&
1222 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001223 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001224
Per Forlin54d49d772011-07-01 18:55:29 +02001225 memset(brq, 0, sizeof(struct mmc_blk_request));
1226 brq->mrq.cmd = &brq->cmd;
1227 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Per Forlin54d49d772011-07-01 18:55:29 +02001229 brq->cmd.arg = blk_rq_pos(req);
1230 if (!mmc_card_blockaddr(card))
1231 brq->cmd.arg <<= 9;
1232 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1233 brq->data.blksz = 512;
1234 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1235 brq->stop.arg = 0;
1236 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1237 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Asutosh Das1a897142012-07-27 18:10:19 +05301239 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001240 /*
1241 * The block layer doesn't support all sector count
1242 * restrictions, so we need to be prepared for too big
1243 * requests.
1244 */
1245 if (brq->data.blocks > card->host->max_blk_count)
1246 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001248 if (brq->data.blocks > 1) {
1249 /*
1250 * After a read error, we redo the request one sector
1251 * at a time in order to accurately determine which
1252 * sectors can be read successfully.
1253 */
1254 if (disable_multi)
1255 brq->data.blocks = 1;
1256
1257 /* Some controllers can't do multiblock reads due to hw bugs */
1258 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1259 rq_data_dir(req) == READ)
1260 brq->data.blocks = 1;
1261 }
Per Forlin54d49d772011-07-01 18:55:29 +02001262
1263 if (brq->data.blocks > 1 || do_rel_wr) {
1264 /* SPI multiblock writes terminate using a special
1265 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001266 */
Per Forlin54d49d772011-07-01 18:55:29 +02001267 if (!mmc_host_is_spi(card->host) ||
1268 rq_data_dir(req) == READ)
1269 brq->mrq.stop = &brq->stop;
1270 readcmd = MMC_READ_MULTIPLE_BLOCK;
1271 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1272 } else {
1273 brq->mrq.stop = NULL;
1274 readcmd = MMC_READ_SINGLE_BLOCK;
1275 writecmd = MMC_WRITE_BLOCK;
1276 }
1277 if (rq_data_dir(req) == READ) {
1278 brq->cmd.opcode = readcmd;
1279 brq->data.flags |= MMC_DATA_READ;
1280 } else {
1281 brq->cmd.opcode = writecmd;
1282 brq->data.flags |= MMC_DATA_WRITE;
1283 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001284
Per Forlin54d49d772011-07-01 18:55:29 +02001285 if (do_rel_wr)
1286 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001287
Per Forlin54d49d772011-07-01 18:55:29 +02001288 /*
Saugata Das42659002011-12-21 13:09:17 +05301289 * Data tag is used only during writing meta data to speed
1290 * up write and any subsequent read of this meta data
1291 */
1292 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1293 (req->cmd_flags & REQ_META) &&
1294 (rq_data_dir(req) == WRITE) &&
1295 ((brq->data.blocks * brq->data.blksz) >=
1296 card->ext_csd.data_tag_unit_size);
1297
1298 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001299 * Pre-defined multi-block transfers are preferable to
1300 * open ended-ones (and necessary for reliable writes).
1301 * However, it is not sufficient to just send CMD23,
1302 * and avoid the final CMD12, as on an error condition
1303 * CMD12 (stop) needs to be sent anyway. This, coupled
1304 * with Auto-CMD23 enhancements provided by some
1305 * hosts, means that the complexity of dealing
1306 * with this is best left to the host. If CMD23 is
1307 * supported by card and host, we'll fill sbc in and let
1308 * the host deal with handling it correctly. This means
1309 * that for hosts that don't expose MMC_CAP_CMD23, no
1310 * change of behavior will be observed.
1311 *
1312 * N.B: Some MMC cards experience perf degradation.
1313 * We'll avoid using CMD23-bounded multiblock writes for
1314 * these, while retaining features like reliable writes.
1315 */
Saugata Das42659002011-12-21 13:09:17 +05301316 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1317 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1318 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001319 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1320 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301321 (do_rel_wr ? (1 << 31) : 0) |
1322 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001323 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1324 brq->mrq.sbc = &brq->sbc;
1325 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001326
Per Forlin54d49d772011-07-01 18:55:29 +02001327 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001328
Per Forlin54d49d772011-07-01 18:55:29 +02001329 brq->data.sg = mqrq->sg;
1330 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001331
Per Forlin54d49d772011-07-01 18:55:29 +02001332 /*
1333 * Adjust the sg list so it is the same size as the
1334 * request.
1335 */
1336 if (brq->data.blocks != blk_rq_sectors(req)) {
1337 int i, data_size = brq->data.blocks << 9;
1338 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001339
Per Forlin54d49d772011-07-01 18:55:29 +02001340 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1341 data_size -= sg->length;
1342 if (data_size <= 0) {
1343 sg->length += data_size;
1344 i++;
1345 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001346 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001347 }
Per Forlin54d49d772011-07-01 18:55:29 +02001348 brq->data.sg_len = i;
1349 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001350
Per Forlinee8a43a2011-07-01 18:55:33 +02001351 mqrq->mmc_active.mrq = &brq->mrq;
1352 mqrq->mmc_active.err_check = mmc_blk_err_check;
1353
Per Forlin54d49d772011-07-01 18:55:29 +02001354 mmc_queue_bounce_pre(mqrq);
1355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001357static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1358 struct request *req)
1359{
1360 struct mmc_host *host = mq->card->host;
1361 int data_dir;
1362
1363 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1364 return;
1365
1366 /*
1367 * In case the packing control is not supported by the host, it should
1368 * not have an effect on the write packing. Therefore we have to enable
1369 * the write packing
1370 */
1371 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1372 mq->wr_packing_enabled = true;
1373 return;
1374 }
1375
1376 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1377 if (mq->num_of_potential_packed_wr_reqs >
1378 mq->num_wr_reqs_to_start_packing)
1379 mq->wr_packing_enabled = true;
Tatyana Brokhmanc9989122012-10-07 10:26:27 +02001380 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001381 return;
1382 }
1383
1384 data_dir = rq_data_dir(req);
1385
1386 if (data_dir == READ) {
1387 mq->num_of_potential_packed_wr_reqs = 0;
1388 mq->wr_packing_enabled = false;
1389 return;
1390 } else if (data_dir == WRITE) {
1391 mq->num_of_potential_packed_wr_reqs++;
1392 }
1393
1394 if (mq->num_of_potential_packed_wr_reqs >
1395 mq->num_wr_reqs_to_start_packing)
1396 mq->wr_packing_enabled = true;
1397
1398}
1399
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001400struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1401{
1402 if (!card)
1403 return NULL;
1404
1405 return &card->wr_pack_stats;
1406}
1407EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1408
1409void mmc_blk_init_packed_statistics(struct mmc_card *card)
1410{
1411 int max_num_of_packed_reqs = 0;
1412
1413 if (!card || !card->wr_pack_stats.packing_events)
1414 return;
1415
1416 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1417
1418 spin_lock(&card->wr_pack_stats.lock);
1419 memset(card->wr_pack_stats.packing_events, 0,
1420 (max_num_of_packed_reqs + 1) *
1421 sizeof(*card->wr_pack_stats.packing_events));
1422 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1423 sizeof(card->wr_pack_stats.pack_stop_reason));
1424 card->wr_pack_stats.enabled = true;
1425 spin_unlock(&card->wr_pack_stats.lock);
1426}
1427EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1428
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001429void print_mmc_packing_stats(struct mmc_card *card)
1430{
1431 int i;
1432 int max_num_of_packed_reqs = 0;
1433
1434 if ((!card) || (!card->wr_pack_stats.packing_events))
1435 return;
1436
1437 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1438
1439 spin_lock(&card->wr_pack_stats.lock);
1440
1441 pr_info("%s: write packing statistics:\n",
1442 mmc_hostname(card->host));
1443
1444 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1445 if (card->wr_pack_stats.packing_events[i] != 0)
1446 pr_info("%s: Packed %d reqs - %d times\n",
1447 mmc_hostname(card->host), i,
1448 card->wr_pack_stats.packing_events[i]);
1449 }
1450
1451 pr_info("%s: stopped packing due to the following reasons:\n",
1452 mmc_hostname(card->host));
1453
1454 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1455 pr_info("%s: %d times: exceedmax num of segments\n",
1456 mmc_hostname(card->host),
1457 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1458 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1459 pr_info("%s: %d times: exceeding the max num of sectors\n",
1460 mmc_hostname(card->host),
1461 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1462 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1463 pr_info("%s: %d times: wrong data direction\n",
1464 mmc_hostname(card->host),
1465 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1466 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1467 pr_info("%s: %d times: flush or discard\n",
1468 mmc_hostname(card->host),
1469 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1470 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1471 pr_info("%s: %d times: empty queue\n",
1472 mmc_hostname(card->host),
1473 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1474 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1475 pr_info("%s: %d times: rel write\n",
1476 mmc_hostname(card->host),
1477 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1478 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1479 pr_info("%s: %d times: Threshold\n",
1480 mmc_hostname(card->host),
1481 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1482
1483 spin_unlock(&card->wr_pack_stats.lock);
1484}
1485EXPORT_SYMBOL(print_mmc_packing_stats);
1486
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001487static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1488{
1489 struct request_queue *q = mq->queue;
1490 struct mmc_card *card = mq->card;
1491 struct request *cur = req, *next = NULL;
1492 struct mmc_blk_data *md = mq->data;
1493 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1494 unsigned int req_sectors = 0, phys_segments = 0;
1495 unsigned int max_blk_count, max_phys_segs;
1496 u8 put_back = 0;
1497 u8 max_packed_rw = 0;
1498 u8 reqs = 0;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001499 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001500
1501 mmc_blk_clear_packed(mq->mqrq_cur);
1502
1503 if (!(md->flags & MMC_BLK_CMD23) ||
1504 !card->ext_csd.packed_event_en)
1505 goto no_packed;
1506
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001507 if (!mq->wr_packing_enabled)
1508 goto no_packed;
1509
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001510 if ((rq_data_dir(cur) == WRITE) &&
1511 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1512 max_packed_rw = card->ext_csd.max_packed_writes;
1513
1514 if (max_packed_rw == 0)
1515 goto no_packed;
1516
1517 if (mmc_req_rel_wr(cur) &&
1518 (md->flags & MMC_BLK_REL_WR) &&
1519 !en_rel_wr)
1520 goto no_packed;
1521
1522 if (mmc_large_sec(card) &&
1523 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1524 goto no_packed;
1525
1526 max_blk_count = min(card->host->max_blk_count,
1527 card->host->max_req_size >> 9);
1528 if (unlikely(max_blk_count > 0xffff))
1529 max_blk_count = 0xffff;
1530
1531 max_phys_segs = queue_max_segments(q);
1532 req_sectors += blk_rq_sectors(cur);
1533 phys_segments += cur->nr_phys_segments;
1534
1535 if (rq_data_dir(cur) == WRITE) {
1536 req_sectors++;
1537 phys_segments++;
1538 }
1539
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001540 spin_lock(&stats->lock);
1541
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001542 while (reqs < max_packed_rw - 1) {
1543 spin_lock_irq(q->queue_lock);
1544 next = blk_fetch_request(q);
1545 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001546 if (!next) {
1547 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001548 break;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001549 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001550
1551 if (mmc_large_sec(card) &&
1552 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhmana07f7712012-10-04 11:11:08 +02001553 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001554 put_back = 1;
1555 break;
1556 }
1557
1558 if (next->cmd_flags & REQ_DISCARD ||
1559 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001560 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001561 put_back = 1;
1562 break;
1563 }
1564
1565 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001566 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001567 put_back = 1;
1568 break;
1569 }
1570
1571 if (mmc_req_rel_wr(next) &&
1572 (md->flags & MMC_BLK_REL_WR) &&
1573 !en_rel_wr) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001574 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001575 put_back = 1;
1576 break;
1577 }
1578
1579 req_sectors += blk_rq_sectors(next);
1580 if (req_sectors > max_blk_count) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001581 if (stats->enabled)
1582 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001583 put_back = 1;
1584 break;
1585 }
1586
1587 phys_segments += next->nr_phys_segments;
1588 if (phys_segments > max_phys_segs) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001589 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001590 put_back = 1;
1591 break;
1592 }
1593
Maya Erez5c46dad2012-10-10 03:47:54 +02001594 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001595 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5c46dad2012-10-10 03:47:54 +02001596 if (card->ext_csd.bkops_en)
1597 card->bkops_info.sectors_changed +=
1598 blk_rq_sectors(next);
1599 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001600 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1601 cur = next;
1602 reqs++;
1603 }
1604
1605 if (put_back) {
1606 spin_lock_irq(q->queue_lock);
1607 blk_requeue_request(q, next);
1608 spin_unlock_irq(q->queue_lock);
1609 }
1610
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001611 if (stats->enabled) {
1612 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1613 stats->packing_events[reqs + 1]++;
1614 if (reqs + 1 == max_packed_rw)
1615 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1616 }
1617
1618 spin_unlock(&stats->lock);
1619
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001620 if (reqs > 0) {
1621 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1622 mq->mqrq_cur->packed_num = ++reqs;
1623 mq->mqrq_cur->packed_retries = reqs;
1624 return reqs;
1625 }
1626
1627no_packed:
1628 mmc_blk_clear_packed(mq->mqrq_cur);
1629 return 0;
1630}
1631
1632static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1633 struct mmc_card *card,
1634 struct mmc_queue *mq)
1635{
1636 struct mmc_blk_request *brq = &mqrq->brq;
1637 struct request *req = mqrq->req;
1638 struct request *prq;
1639 struct mmc_blk_data *md = mq->data;
1640 bool do_rel_wr, do_data_tag;
1641 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1642 u8 i = 1;
1643
1644 mqrq->packed_cmd = MMC_PACKED_WRITE;
1645 mqrq->packed_blocks = 0;
1646 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1647
1648 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1649 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1650 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1651
1652 /*
1653 * Argument for each entry of packed group
1654 */
1655 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1656 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1657 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1658 (prq->cmd_flags & REQ_META) &&
1659 (rq_data_dir(prq) == WRITE) &&
1660 ((brq->data.blocks * brq->data.blksz) >=
1661 card->ext_csd.data_tag_unit_size);
1662 /* Argument of CMD23 */
1663 packed_cmd_hdr[(i * 2)] =
1664 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1665 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1666 blk_rq_sectors(prq);
1667 /* Argument of CMD18 or CMD25 */
1668 packed_cmd_hdr[((i * 2)) + 1] =
1669 mmc_card_blockaddr(card) ?
1670 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1671 mqrq->packed_blocks += blk_rq_sectors(prq);
1672 i++;
1673 }
1674
1675 memset(brq, 0, sizeof(struct mmc_blk_request));
1676 brq->mrq.cmd = &brq->cmd;
1677 brq->mrq.data = &brq->data;
1678 brq->mrq.sbc = &brq->sbc;
1679 brq->mrq.stop = &brq->stop;
1680
1681 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1682 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1683 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1684
1685 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1686 brq->cmd.arg = blk_rq_pos(req);
1687 if (!mmc_card_blockaddr(card))
1688 brq->cmd.arg <<= 9;
1689 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1690
1691 brq->data.blksz = 512;
1692 brq->data.blocks = mqrq->packed_blocks + 1;
1693 brq->data.flags |= MMC_DATA_WRITE;
1694
1695 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1696 brq->stop.arg = 0;
1697 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1698
1699 mmc_set_data_timeout(&brq->data, card);
1700
1701 brq->data.sg = mqrq->sg;
1702 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1703
1704 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001705
1706 /*
1707 * This is intended for packed commands tests usage - in case these
1708 * functions are not in use the respective pointers are NULL
1709 */
1710 if (mq->err_check_fn)
1711 mqrq->mmc_active.err_check = mq->err_check_fn;
1712 else
1713 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1714
1715 if (mq->packed_test_fn)
1716 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001717
1718 mmc_queue_bounce_pre(mqrq);
1719}
1720
Adrian Hunter67716322011-08-29 16:42:15 +03001721static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1722 struct mmc_blk_request *brq, struct request *req,
1723 int ret)
1724{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001725 struct mmc_queue_req *mq_rq;
1726 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1727
Adrian Hunter67716322011-08-29 16:42:15 +03001728 /*
1729 * If this is an SD card and we're writing, we can first
1730 * mark the known good sectors as ok.
1731 *
1732 * If the card is not SD, we can still ok written sectors
1733 * as reported by the controller (which might be less than
1734 * the real number of written sectors, but never more).
1735 */
1736 if (mmc_card_sd(card)) {
1737 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301738 if (!brq->data.fault_injected) {
1739 blocks = mmc_sd_num_wr_blocks(card);
1740 if (blocks != (u32)-1)
1741 ret = blk_end_request(req, 0, blocks << 9);
1742 } else
1743 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001744 } else {
1745 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1746 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1747 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001748 return ret;
1749}
1750
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001751static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1752{
1753 struct request *prq;
1754 int idx = mq_rq->packed_fail_idx, i = 0;
1755 int ret = 0;
1756
1757 while (!list_empty(&mq_rq->packed_list)) {
1758 prq = list_entry_rq(mq_rq->packed_list.next);
1759 if (idx == i) {
1760 /* retry from error index */
1761 mq_rq->packed_num -= idx;
1762 mq_rq->req = prq;
1763 ret = 1;
1764
1765 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1766 list_del_init(&prq->queuelist);
1767 mmc_blk_clear_packed(mq_rq);
1768 }
1769 return ret;
1770 }
1771 list_del_init(&prq->queuelist);
1772 blk_end_request(prq, 0, blk_rq_bytes(prq));
1773 i++;
1774 }
1775
1776 mmc_blk_clear_packed(mq_rq);
1777 return ret;
1778}
1779static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1780{
1781 struct request *prq;
1782
1783 while (!list_empty(&mq_rq->packed_list)) {
1784 prq = list_entry_rq(mq_rq->packed_list.next);
1785 list_del_init(&prq->queuelist);
1786 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1787 }
1788
1789 mmc_blk_clear_packed(mq_rq);
1790}
1791
1792static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1793 struct mmc_queue_req *mq_rq)
1794{
1795 struct request *prq;
1796 struct request_queue *q = mq->queue;
1797
1798 while (!list_empty(&mq_rq->packed_list)) {
1799 prq = list_entry_rq(mq_rq->packed_list.prev);
1800 if (prq->queuelist.prev != &mq_rq->packed_list) {
1801 list_del_init(&prq->queuelist);
1802 spin_lock_irq(q->queue_lock);
1803 blk_requeue_request(mq->queue, prq);
1804 spin_unlock_irq(q->queue_lock);
1805 } else {
1806 list_del_init(&prq->queuelist);
1807 }
1808 }
1809
1810 mmc_blk_clear_packed(mq_rq);
1811}
1812
Per Forlinee8a43a2011-07-01 18:55:33 +02001813static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001814{
1815 struct mmc_blk_data *md = mq->data;
1816 struct mmc_card *card = md->queue.card;
1817 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001818 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001819 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001820 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001821 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001822 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001823 const u8 packed_num = 2;
1824 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001825
1826 if (!rqc && !mq->mqrq_prev->req)
1827 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001828
Maya Erez5c46dad2012-10-10 03:47:54 +02001829 if (rqc) {
1830 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1831 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001832 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5c46dad2012-10-10 03:47:54 +02001833 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001834
Per Forlin54d49d772011-07-01 18:55:29 +02001835 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001836 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001837 if (reqs >= packed_num)
1838 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1839 card, mq);
1840 else
1841 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001842 areq = &mq->mqrq_cur->mmc_active;
1843 } else
1844 areq = NULL;
1845 areq = mmc_start_req(card->host, areq, (int *) &status);
1846 if (!areq)
1847 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001848
Per Forlinee8a43a2011-07-01 18:55:33 +02001849 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1850 brq = &mq_rq->brq;
1851 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001852 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001853 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001854
Per Forlind78d4a82011-07-01 18:55:30 +02001855 switch (status) {
1856 case MMC_BLK_SUCCESS:
1857 case MMC_BLK_PARTIAL:
1858 /*
1859 * A block was successfully transferred.
1860 */
Adrian Hunter67716322011-08-29 16:42:15 +03001861 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001862
1863 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1864 ret = mmc_blk_end_packed_req(mq_rq);
1865 break;
1866 } else {
1867 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001868 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001869 }
1870
Adrian Hunter67716322011-08-29 16:42:15 +03001871 /*
1872 * If the blk_end_request function returns non-zero even
1873 * though all data has been transferred and no errors
1874 * were returned by the host controller, it's a bug.
1875 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001876 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301877 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001878 __func__, blk_rq_bytes(req),
1879 brq->data.bytes_xfered);
1880 rqc = NULL;
1881 goto cmd_abort;
1882 }
Per Forlind78d4a82011-07-01 18:55:30 +02001883 break;
1884 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001885 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1886 if (!mmc_blk_reset(md, card->host, type))
1887 break;
1888 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001889 case MMC_BLK_RETRY:
1890 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001891 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001892 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001893 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001894 if (!mmc_blk_reset(md, card->host, type))
1895 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001896 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001897 case MMC_BLK_DATA_ERR: {
1898 int err;
1899
1900 err = mmc_blk_reset(md, card->host, type);
1901 if (!err)
1902 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001903 if (err == -ENODEV ||
1904 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001905 goto cmd_abort;
1906 /* Fall through */
1907 }
1908 case MMC_BLK_ECC_ERR:
1909 if (brq->data.blocks > 1) {
1910 /* Redo read one sector at a time */
1911 pr_warning("%s: retrying using single block read\n",
1912 req->rq_disk->disk_name);
1913 disable_multi = 1;
1914 break;
1915 }
Per Forlind78d4a82011-07-01 18:55:30 +02001916 /*
1917 * After an error, we redo I/O one sector at a
1918 * time, so we only reach here after trying to
1919 * read a single sector.
1920 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301921 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001922 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001923 if (!ret)
1924 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001925 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301926 case MMC_BLK_NOMEDIUM:
1927 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001928 }
1929
Per Forlinee8a43a2011-07-01 18:55:33 +02001930 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001931 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1932 /*
1933 * In case of a incomplete request
1934 * prepare it again and resend.
1935 */
1936 mmc_blk_rw_rq_prep(mq_rq, card,
1937 disable_multi, mq);
1938 mmc_start_req(card->host,
1939 &mq_rq->mmc_active, NULL);
1940 } else {
1941 if (!mq_rq->packed_retries)
1942 goto cmd_abort;
1943 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1944 mmc_start_req(card->host,
1945 &mq_rq->mmc_active, NULL);
1946 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 } while (ret);
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 1;
1951
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001952 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001953 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1954 if (mmc_card_removed(card))
1955 req->cmd_flags |= REQ_QUIET;
1956 while (ret)
1957 ret = blk_end_request(req, -EIO,
1958 blk_rq_cur_bytes(req));
1959 } else {
1960 mmc_blk_abort_packed_req(mq_rq);
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Per Forlinee8a43a2011-07-01 18:55:33 +02001963 start_new_req:
1964 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001965 /*
1966 * If current request is packed, it needs to put back.
1967 */
1968 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1969 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1970
Per Forlinee8a43a2011-07-01 18:55:33 +02001971 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1972 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1973 }
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
1976}
1977
Adrian Hunterbd788c92010-08-11 14:17:47 -07001978static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1979{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001980 int ret;
1981 struct mmc_blk_data *md = mq->data;
1982 struct mmc_card *card = md->queue.card;
1983
San Mehat148c57a2009-07-30 08:21:19 -07001984#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1985 if (mmc_bus_needs_resume(card->host)) {
1986 mmc_resume_bus(card->host);
1987 mmc_blk_set_blksize(md, card);
1988 }
1989#endif
1990
Per Forlinee8a43a2011-07-01 18:55:33 +02001991 if (req && !mq->mqrq_prev->req)
1992 /* claim host only for the first request */
1993 mmc_claim_host(card->host);
1994
Andrei Warkentin371a6892011-04-11 18:10:25 -05001995 ret = mmc_blk_part_switch(card, md);
1996 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001997 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301998 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001999 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002000 ret = 0;
2001 goto out;
2002 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002003
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002004 mmc_blk_write_packing_control(mq, req);
2005
Maya Erez463bb952012-05-24 23:46:29 +03002006 if (req && req->cmd_flags & REQ_SANITIZE) {
2007 /* complete ongoing async transfer before issuing sanitize */
2008 if (card->host && card->host->areq)
2009 mmc_blk_issue_rw_rq(mq, NULL);
2010 ret = mmc_blk_issue_sanitize_rq(mq, req);
2011 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002012 /* complete ongoing async transfer before issuing discard */
2013 if (card->host->areq)
2014 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002015 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002016 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002017 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002018 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002019 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002020 /* complete ongoing async transfer before issuing flush */
2021 if (card->host->areq)
2022 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002023 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002024 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002025 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002026 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002027
Andrei Warkentin371a6892011-04-11 18:10:25 -05002028out:
Per Forlinee8a43a2011-07-01 18:55:33 +02002029 if (!req)
2030 /* release host only when there are no more requests */
2031 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002032 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Russell Kinga6f6c962006-01-03 22:38:44 +00002035static inline int mmc_blk_readonly(struct mmc_card *card)
2036{
2037 return mmc_card_readonly(card) ||
2038 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2039}
2040
Andrei Warkentin371a6892011-04-11 18:10:25 -05002041static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2042 struct device *parent,
2043 sector_t size,
2044 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002045 const char *subname,
2046 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct mmc_blk_data *md;
2049 int devidx, ret;
2050
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002051 devidx = find_first_zero_bit(dev_use, max_devices);
2052 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return ERR_PTR(-ENOSPC);
2054 __set_bit(devidx, dev_use);
2055
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002056 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002057 if (!md) {
2058 ret = -ENOMEM;
2059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002061
Russell Kinga6f6c962006-01-03 22:38:44 +00002062 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002063 * !subname implies we are creating main mmc_blk_data that will be
2064 * associated with mmc_card with mmc_set_drvdata. Due to device
2065 * partitions, devidx will not coincide with a per-physical card
2066 * index anymore so we keep track of a name index.
2067 */
2068 if (!subname) {
2069 md->name_idx = find_first_zero_bit(name_use, max_devices);
2070 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002071 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002072 md->name_idx = ((struct mmc_blk_data *)
2073 dev_to_disk(parent)->private_data)->name_idx;
2074
Johan Rudholmadd710e2011-12-02 08:51:06 +01002075 md->area_type = area_type;
2076
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002077 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002078 * Set the read-only status based on the supported commands
2079 * and the write protect switch.
2080 */
2081 md->read_only = mmc_blk_readonly(card);
2082
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002083 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002084 if (md->disk == NULL) {
2085 ret = -ENOMEM;
2086 goto err_kfree;
2087 }
2088
2089 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002090 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002091 md->usage = 1;
2092
Adrian Hunterd09408a2011-06-23 13:40:28 +03002093 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002094 if (ret)
2095 goto err_putdisk;
2096
Russell Kinga6f6c962006-01-03 22:38:44 +00002097 md->queue.issue_fn = mmc_blk_issue_rq;
2098 md->queue.data = md;
2099
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002100 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002101 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002102 md->disk->fops = &mmc_bdops;
2103 md->disk->private_data = md;
2104 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002105 md->disk->driverfs_dev = parent;
2106 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002107 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002108
2109 /*
2110 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2111 *
2112 * - be set for removable media with permanent block devices
2113 * - be unset for removable block devices with permanent media
2114 *
2115 * Since MMC block devices clearly fall under the second
2116 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2117 * should use the block device creation/destruction hotplug
2118 * messages to tell when the card is present.
2119 */
2120
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002121 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2122 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002123
Martin K. Petersene1defc42009-05-22 17:17:49 -04002124 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002125 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002126
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002127 if (mmc_host_cmd23(card->host)) {
2128 if (mmc_card_mmc(card) ||
2129 (mmc_card_sd(card) &&
2130 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2131 md->flags |= MMC_BLK_CMD23;
2132 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002133
2134 if (mmc_card_mmc(card) &&
2135 md->flags & MMC_BLK_CMD23 &&
2136 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2137 card->ext_csd.rel_sectors)) {
2138 md->flags |= MMC_BLK_REL_WR;
2139 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2140 }
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002143
2144 err_putdisk:
2145 put_disk(md->disk);
2146 err_kfree:
2147 kfree(md);
2148 out:
2149 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Andrei Warkentin371a6892011-04-11 18:10:25 -05002152static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2153{
2154 sector_t size;
2155 struct mmc_blk_data *md;
2156
2157 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2158 /*
2159 * The EXT_CSD sector count is in number or 512 byte
2160 * sectors.
2161 */
2162 size = card->ext_csd.sectors;
2163 } else {
2164 /*
2165 * The CSD capacity field is in units of read_blkbits.
2166 * set_capacity takes units of 512 bytes.
2167 */
2168 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2169 }
2170
Johan Rudholmadd710e2011-12-02 08:51:06 +01002171 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2172 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002173 return md;
2174}
2175
2176static int mmc_blk_alloc_part(struct mmc_card *card,
2177 struct mmc_blk_data *md,
2178 unsigned int part_type,
2179 sector_t size,
2180 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002181 const char *subname,
2182 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002183{
2184 char cap_str[10];
2185 struct mmc_blk_data *part_md;
2186
2187 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002188 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002189 if (IS_ERR(part_md))
2190 return PTR_ERR(part_md);
2191 part_md->part_type = part_type;
2192 list_add(&part_md->part, &md->part);
2193
2194 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2195 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302196 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002197 part_md->disk->disk_name, mmc_card_id(card),
2198 mmc_card_name(card), part_md->part_type, cap_str);
2199 return 0;
2200}
2201
Namjae Jeone0c368d2011-10-06 23:41:38 +09002202/* MMC Physical partitions consist of two boot partitions and
2203 * up to four general purpose partitions.
2204 * For each partition enabled in EXT_CSD a block device will be allocatedi
2205 * to provide access to the partition.
2206 */
2207
Andrei Warkentin371a6892011-04-11 18:10:25 -05002208static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2209{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002210 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002211
2212 if (!mmc_card_mmc(card))
2213 return 0;
2214
Namjae Jeone0c368d2011-10-06 23:41:38 +09002215 for (idx = 0; idx < card->nr_parts; idx++) {
2216 if (card->part[idx].size) {
2217 ret = mmc_blk_alloc_part(card, md,
2218 card->part[idx].part_cfg,
2219 card->part[idx].size >> 9,
2220 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002221 card->part[idx].name,
2222 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002223 if (ret)
2224 return ret;
2225 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002226 }
2227
2228 return ret;
2229}
2230
Andrei Warkentin371a6892011-04-11 18:10:25 -05002231static void mmc_blk_remove_req(struct mmc_blk_data *md)
2232{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002233 struct mmc_card *card;
2234
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002236 card = md->queue.card;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002237 device_remove_file(disk_to_dev(md->disk),
2238 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002239 if (md->disk->flags & GENHD_FL_UP) {
2240 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002241 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2242 card->ext_csd.boot_ro_lockable)
2243 device_remove_file(disk_to_dev(md->disk),
2244 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002245
2246 /* Stop new requests from getting into the queue */
2247 del_gendisk(md->disk);
2248 }
2249
2250 /* Then flush out any already in there */
2251 mmc_cleanup_queue(&md->queue);
2252 mmc_blk_put(md);
2253 }
2254}
2255
2256static void mmc_blk_remove_parts(struct mmc_card *card,
2257 struct mmc_blk_data *md)
2258{
2259 struct list_head *pos, *q;
2260 struct mmc_blk_data *part_md;
2261
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002262 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002263 list_for_each_safe(pos, q, &md->part) {
2264 part_md = list_entry(pos, struct mmc_blk_data, part);
2265 list_del(pos);
2266 mmc_blk_remove_req(part_md);
2267 }
2268}
2269
2270static int mmc_add_disk(struct mmc_blk_data *md)
2271{
2272 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002273 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002274
2275 add_disk(md->disk);
2276 md->force_ro.show = force_ro_show;
2277 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302278 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002279 md->force_ro.attr.name = "force_ro";
2280 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2281 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2282 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002283 goto force_ro_fail;
2284
2285 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2286 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002287 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002288
2289 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2290 mode = S_IRUGO;
2291 else
2292 mode = S_IRUGO | S_IWUSR;
2293
2294 md->power_ro_lock.show = power_ro_lock_show;
2295 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002296 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002297 md->power_ro_lock.attr.mode = mode;
2298 md->power_ro_lock.attr.name =
2299 "ro_lock_until_next_power_on";
2300 ret = device_create_file(disk_to_dev(md->disk),
2301 &md->power_ro_lock);
2302 if (ret)
2303 goto power_ro_lock_fail;
2304 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002305
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002306 md->num_wr_reqs_to_start_packing.show =
2307 num_wr_reqs_to_start_packing_show;
2308 md->num_wr_reqs_to_start_packing.store =
2309 num_wr_reqs_to_start_packing_store;
2310 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2311 md->num_wr_reqs_to_start_packing.attr.name =
2312 "num_wr_reqs_to_start_packing";
2313 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2314 ret = device_create_file(disk_to_dev(md->disk),
2315 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002316 if (ret)
2317 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002318
Johan Rudholmadd710e2011-12-02 08:51:06 +01002319 return ret;
2320
2321power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002322 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002323force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002324 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002325
2326 return ret;
2327}
2328
Chris Ballc59d4472011-11-11 22:01:43 -05002329#define CID_MANFID_SANDISK 0x2
2330#define CID_MANFID_TOSHIBA 0x11
2331#define CID_MANFID_MICRON 0x13
2332
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002333static const struct mmc_fixup blk_fixups[] =
2334{
Chris Ballc59d4472011-11-11 22:01:43 -05002335 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2336 MMC_QUIRK_INAND_CMD38),
2337 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2338 MMC_QUIRK_INAND_CMD38),
2339 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2340 MMC_QUIRK_INAND_CMD38),
2341 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2342 MMC_QUIRK_INAND_CMD38),
2343 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2344 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002345
2346 /*
2347 * Some MMC cards experience performance degradation with CMD23
2348 * instead of CMD12-bounded multiblock transfers. For now we'll
2349 * black list what's bad...
2350 * - Certain Toshiba cards.
2351 *
2352 * N.B. This doesn't affect SD cards.
2353 */
Chris Ballc59d4472011-11-11 22:01:43 -05002354 MMC_FIXUP("MMC08G", 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("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002357 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002358 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002359 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002360
2361 /*
2362 * Some Micron MMC cards needs longer data read timeout than
2363 * indicated in CSD.
2364 */
Chris Ballc59d4472011-11-11 22:01:43 -05002365 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002366 MMC_QUIRK_LONG_READ_TIME),
2367
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302368 /* Some INAND MCP devices advertise incorrect timeout values */
2369 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2370 MMC_QUIRK_INAND_DATA_TIMEOUT),
2371
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002372 END_FIXUP
2373};
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375static int mmc_blk_probe(struct mmc_card *card)
2376{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002377 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002378 char cap_str[10];
2379
Pierre Ossman912490d2005-05-21 10:27:02 +01002380 /*
2381 * Check that the card supports the command class(es) we need.
2382 */
2383 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return -ENODEV;
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 md = mmc_blk_alloc(card);
2387 if (IS_ERR(md))
2388 return PTR_ERR(md);
2389
Yi Li444122f2009-02-05 15:31:57 +08002390 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002391 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302392 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002394 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Andrei Warkentin371a6892011-04-11 18:10:25 -05002396 if (mmc_blk_alloc_parts(card, md))
2397 goto out;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002400 mmc_fixup_device(card, blk_fixups);
2401
San Mehat148c57a2009-07-30 08:21:19 -07002402#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2403 mmc_set_bus_resume_policy(card->host, 1);
2404#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002405 if (mmc_add_disk(md))
2406 goto out;
2407
2408 list_for_each_entry(part_md, &md->part, part) {
2409 if (mmc_add_disk(part_md))
2410 goto out;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return 0;
2413
2414 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002415 mmc_blk_remove_parts(card, md);
2416 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420static void mmc_blk_remove(struct mmc_card *card)
2421{
2422 struct mmc_blk_data *md = mmc_get_drvdata(card);
2423
Andrei Warkentin371a6892011-04-11 18:10:25 -05002424 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002425 mmc_claim_host(card->host);
2426 mmc_blk_part_switch(card, md);
2427 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002428 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002430#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2431 mmc_set_bus_resume_policy(card->host, 0);
2432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002436static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002438 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct mmc_blk_data *md = mmc_get_drvdata(card);
2440
2441 if (md) {
2442 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002443 list_for_each_entry(part_md, &md->part, part) {
2444 mmc_queue_suspend(&part_md->queue);
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447 return 0;
2448}
2449
2450static int mmc_blk_resume(struct mmc_card *card)
2451{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002452 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct mmc_blk_data *md = mmc_get_drvdata(card);
2454
2455 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002456 /*
2457 * Resume involves the card going into idle state,
2458 * so current partition is always the main one.
2459 */
2460 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002462 list_for_each_entry(part_md, &md->part, part) {
2463 mmc_queue_resume(&part_md->queue);
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466 return 0;
2467}
2468#else
2469#define mmc_blk_suspend NULL
2470#define mmc_blk_resume NULL
2471#endif
2472
2473static struct mmc_driver mmc_driver = {
2474 .drv = {
2475 .name = "mmcblk",
2476 },
2477 .probe = mmc_blk_probe,
2478 .remove = mmc_blk_remove,
2479 .suspend = mmc_blk_suspend,
2480 .resume = mmc_blk_resume,
2481};
2482
2483static int __init mmc_blk_init(void)
2484{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002485 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002487 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2488 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2489
2490 max_devices = 256 / perdev_minors;
2491
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002492 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2493 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002496 res = mmc_register_driver(&mmc_driver);
2497 if (res)
2498 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002500 return 0;
2501 out2:
2502 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 out:
2504 return res;
2505}
2506
2507static void __exit mmc_blk_exit(void)
2508{
2509 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002510 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513module_init(mmc_blk_init);
2514module_exit(mmc_blk_exit);
2515
2516MODULE_LICENSE("GPL");
2517MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2518