blob: 2343653d5a31f0d2e5724bb24456739440fb420b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
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
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +0200129enum mmc_blk_status {
130 MMC_BLK_SUCCESS = 0,
131 MMC_BLK_PARTIAL,
132 MMC_BLK_CMD_ERR,
133 MMC_BLK_RETRY,
134 MMC_BLK_ABORT,
135 MMC_BLK_DATA_ERR,
136 MMC_BLK_ECC_ERR,
137 MMC_BLK_NOMEDIUM,
Seungwon Jeon968c7742012-05-31 11:54:47 +0300138};
139
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200140enum {
141 MMC_PACKED_N_IDX = -1,
142 MMC_PACKED_N_ZERO,
143 MMC_PACKED_N_SINGLE,
144};
145
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400146module_param(perdev_minors, int, 0444);
147MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
148
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200149static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
150{
151 mqrq->packed_cmd = MMC_PACKED_NONE;
152 mqrq->packed_num = MMC_PACKED_N_ZERO;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
156{
157 struct mmc_blk_data *md;
158
Arjan van de Vena621aae2006-01-12 18:43:35 +0000159 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 md = disk->private_data;
161 if (md && md->usage == 0)
162 md = NULL;
163 if (md)
164 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return md;
168}
169
Andrei Warkentin371a6892011-04-11 18:10:25 -0500170static inline int mmc_get_devidx(struct gendisk *disk)
171{
Colin Cross71b54d42010-09-03 12:41:21 -0700172 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500173 return devidx;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static void mmc_blk_put(struct mmc_blk_data *md)
177{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 md->usage--;
180 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500181 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800182 blk_cleanup_queue(md->queue.queue);
183
David Woodhouse1dff3142007-11-21 18:45:12 +0100184 __clear_bit(devidx, dev_use);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(md);
188 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000189 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Johan Rudholmadd710e2011-12-02 08:51:06 +0100192static ssize_t power_ro_lock_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 int ret;
196 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
197 struct mmc_card *card = md->queue.card;
198 int locked = 0;
199
200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
201 locked = 2;
202 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
203 locked = 1;
204
205 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
206
207 return ret;
208}
209
210static ssize_t power_ro_lock_store(struct device *dev,
211 struct device_attribute *attr, const char *buf, size_t count)
212{
213 int ret;
214 struct mmc_blk_data *md, *part_md;
215 struct mmc_card *card;
216 unsigned long set;
217
218 if (kstrtoul(buf, 0, &set))
219 return -EINVAL;
220
221 if (set != 1)
222 return count;
223
224 md = mmc_blk_get(dev_to_disk(dev));
225 card = md->queue.card;
226
227 mmc_claim_host(card->host);
228
229 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
230 card->ext_csd.boot_ro_lock |
231 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
232 card->ext_csd.part_time);
233 if (ret)
234 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
235 else
236 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
237
238 mmc_release_host(card->host);
239
240 if (!ret) {
241 pr_info("%s: Locking boot partition ro until next power on\n",
242 md->disk->disk_name);
243 set_disk_ro(md->disk, 1);
244
245 list_for_each_entry(part_md, &md->part, part)
246 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
247 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
248 set_disk_ro(part_md->disk, 1);
249 }
250 }
251
252 mmc_blk_put(md);
253 return count;
254}
255
Andrei Warkentin371a6892011-04-11 18:10:25 -0500256static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
257 char *buf)
258{
259 int ret;
260 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
261
262 ret = snprintf(buf, PAGE_SIZE, "%d",
263 get_disk_ro(dev_to_disk(dev)) ^
264 md->read_only);
265 mmc_blk_put(md);
266 return ret;
267}
268
269static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
270 const char *buf, size_t count)
271{
272 int ret;
273 char *end;
274 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
275 unsigned long set = simple_strtoul(buf, &end, 0);
276 if (end == buf) {
277 ret = -EINVAL;
278 goto out;
279 }
280
281 set_disk_ro(dev_to_disk(dev), set || md->read_only);
282 ret = count;
283out:
284 mmc_blk_put(md);
285 return ret;
286}
287
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200288static ssize_t
289num_wr_reqs_to_start_packing_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
293 int num_wr_reqs_to_start_packing;
294 int ret;
295
296 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
297
298 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
299
300 mmc_blk_put(md);
301 return ret;
302}
303
304static ssize_t
305num_wr_reqs_to_start_packing_store(struct device *dev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
308{
309 int value;
310 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
311
312 sscanf(buf, "%d", &value);
313 if (value >= 0)
314 md->queue.num_wr_reqs_to_start_packing = value;
315
316 mmc_blk_put(md);
317 return count;
318}
319
Al Viroa5a15612008-03-02 10:33:30 -0500320static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Al Viroa5a15612008-03-02 10:33:30 -0500322 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int ret = -ENXIO;
324
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200325 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (md) {
327 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500328 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700330
Al Viroa5a15612008-03-02 10:33:30 -0500331 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700332 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700333 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200336 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return ret;
339}
340
Al Viroa5a15612008-03-02 10:33:30 -0500341static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Al Viroa5a15612008-03-02 10:33:30 -0500343 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200345 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200347 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349}
350
351static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800352mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800354 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
355 geo->heads = 4;
356 geo->sectors = 16;
357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
John Calixtocb87ea22011-04-26 18:56:29 -0400360struct mmc_blk_ioc_data {
361 struct mmc_ioc_cmd ic;
362 unsigned char *buf;
363 u64 buf_bytes;
364};
365
366static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
367 struct mmc_ioc_cmd __user *user)
368{
369 struct mmc_blk_ioc_data *idata;
370 int err;
371
372 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
373 if (!idata) {
374 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400376 }
377
378 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
379 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400380 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400381 }
382
383 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
384 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
385 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400386 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400387 }
388
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100389 if (!idata->buf_bytes)
390 return idata;
391
John Calixtocb87ea22011-04-26 18:56:29 -0400392 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
393 if (!idata->buf) {
394 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400395 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400396 }
397
398 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
399 idata->ic.data_ptr, idata->buf_bytes)) {
400 err = -EFAULT;
401 goto copy_err;
402 }
403
404 return idata;
405
406copy_err:
407 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400408idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400409 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400410out:
John Calixtocb87ea22011-04-26 18:56:29 -0400411 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400412}
413
414static int mmc_blk_ioctl_cmd(struct block_device *bdev,
415 struct mmc_ioc_cmd __user *ic_ptr)
416{
417 struct mmc_blk_ioc_data *idata;
418 struct mmc_blk_data *md;
419 struct mmc_card *card;
420 struct mmc_command cmd = {0};
421 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530422 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400423 struct scatterlist sg;
424 int err;
425
426 /*
427 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
428 * whole block device, not on a partition. This prevents overspray
429 * between sibling partitions.
430 */
431 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
432 return -EPERM;
433
434 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
435 if (IS_ERR(idata))
436 return PTR_ERR(idata);
437
John Calixtocb87ea22011-04-26 18:56:29 -0400438 md = mmc_blk_get(bdev->bd_disk);
439 if (!md) {
440 err = -EINVAL;
441 goto cmd_done;
442 }
443
444 card = md->queue.card;
445 if (IS_ERR(card)) {
446 err = PTR_ERR(card);
447 goto cmd_done;
448 }
449
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100450 cmd.opcode = idata->ic.opcode;
451 cmd.arg = idata->ic.arg;
452 cmd.flags = idata->ic.flags;
453
454 if (idata->buf_bytes) {
455 data.sg = &sg;
456 data.sg_len = 1;
457 data.blksz = idata->ic.blksz;
458 data.blocks = idata->ic.blocks;
459
460 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
461
462 if (idata->ic.write_flag)
463 data.flags = MMC_DATA_WRITE;
464 else
465 data.flags = MMC_DATA_READ;
466
467 /* data.flags must already be set before doing this. */
468 mmc_set_data_timeout(&data, card);
469
470 /* Allow overriding the timeout_ns for empirical tuning. */
471 if (idata->ic.data_timeout_ns)
472 data.timeout_ns = idata->ic.data_timeout_ns;
473
474 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
475 /*
476 * Pretend this is a data transfer and rely on the
477 * host driver to compute timeout. When all host
478 * drivers support cmd.cmd_timeout for R1B, this
479 * can be changed to:
480 *
481 * mrq.data = NULL;
482 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
483 */
484 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
485 }
486
487 mrq.data = &data;
488 }
489
490 mrq.cmd = &cmd;
491
John Calixtocb87ea22011-04-26 18:56:29 -0400492 mmc_claim_host(card->host);
493
494 if (idata->ic.is_acmd) {
495 err = mmc_app_cmd(card->host, card);
496 if (err)
497 goto cmd_rel_host;
498 }
499
John Calixtocb87ea22011-04-26 18:56:29 -0400500 mmc_wait_for_req(card->host, &mrq);
501
502 if (cmd.error) {
503 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
504 __func__, cmd.error);
505 err = cmd.error;
506 goto cmd_rel_host;
507 }
508 if (data.error) {
509 dev_err(mmc_dev(card->host), "%s: data error %d\n",
510 __func__, data.error);
511 err = data.error;
512 goto cmd_rel_host;
513 }
514
515 /*
516 * According to the SD specs, some commands require a delay after
517 * issuing the command.
518 */
519 if (idata->ic.postsleep_min_us)
520 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
521
522 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
523 err = -EFAULT;
524 goto cmd_rel_host;
525 }
526
527 if (!idata->ic.write_flag) {
528 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
529 idata->buf, idata->buf_bytes)) {
530 err = -EFAULT;
531 goto cmd_rel_host;
532 }
533 }
534
535cmd_rel_host:
536 mmc_release_host(card->host);
537
538cmd_done:
539 mmc_blk_put(md);
540 kfree(idata->buf);
541 kfree(idata);
542 return err;
543}
544
545static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
546 unsigned int cmd, unsigned long arg)
547{
548 int ret = -EINVAL;
549 if (cmd == MMC_IOC_CMD)
550 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
551 return ret;
552}
553
554#ifdef CONFIG_COMPAT
555static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
556 unsigned int cmd, unsigned long arg)
557{
558 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
559}
560#endif
561
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700562static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500563 .open = mmc_blk_open,
564 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800565 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400567 .ioctl = mmc_blk_ioctl,
568#ifdef CONFIG_COMPAT
569 .compat_ioctl = mmc_blk_compat_ioctl,
570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571};
572
Andrei Warkentin371a6892011-04-11 18:10:25 -0500573static inline int mmc_blk_part_switch(struct mmc_card *card,
574 struct mmc_blk_data *md)
575{
576 int ret;
577 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300578
Andrei Warkentin371a6892011-04-11 18:10:25 -0500579 if (main_md->part_curr == md->part_type)
580 return 0;
581
582 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300583 u8 part_config = card->ext_csd.part_config;
584
585 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
586 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500587
588 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300589 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500590 card->ext_csd.part_time);
591 if (ret)
592 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300593
594 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300595 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500596
597 main_md->part_curr = md->part_type;
598 return 0;
599}
600
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700601static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
602{
603 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100604 u32 result;
605 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700606
Venkatraman Sad5fd972011-08-25 00:30:50 +0530607 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400608 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400609 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700610
611 struct scatterlist sg;
612
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700613 cmd.opcode = MMC_APP_CMD;
614 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700615 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700616
617 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700618 if (err)
619 return (u32)-1;
620 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700621 return (u32)-1;
622
623 memset(&cmd, 0, sizeof(struct mmc_command));
624
625 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
626 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700627 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700628
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700629 data.blksz = 4;
630 data.blocks = 1;
631 data.flags = MMC_DATA_READ;
632 data.sg = &sg;
633 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530634 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700635
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700636 mrq.cmd = &cmd;
637 mrq.data = &data;
638
Ben Dooks051913d2009-06-08 23:33:57 +0100639 blocks = kmalloc(4, GFP_KERNEL);
640 if (!blocks)
641 return (u32)-1;
642
643 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700644
645 mmc_wait_for_req(card->host, &mrq);
646
Ben Dooks051913d2009-06-08 23:33:57 +0100647 result = ntohl(*blocks);
648 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700649
Ben Dooks051913d2009-06-08 23:33:57 +0100650 if (cmd.error || data.error)
651 result = (u32)-1;
652
653 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700654}
655
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100656static int send_stop(struct mmc_card *card, u32 *status)
657{
658 struct mmc_command cmd = {0};
659 int err;
660
661 cmd.opcode = MMC_STOP_TRANSMISSION;
662 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
663 err = mmc_wait_for_cmd(card->host, &cmd, 5);
664 if (err == 0)
665 *status = cmd.resp[0];
666 return err;
667}
668
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100669static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300670{
Chris Ball1278dba2011-04-13 23:40:30 -0400671 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300672 int err;
673
Adrian Hunter504f1912008-10-16 12:55:25 +0300674 cmd.opcode = MMC_SEND_STATUS;
675 if (!mmc_host_is_spi(card->host))
676 cmd.arg = card->rca << 16;
677 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100678 err = mmc_wait_for_cmd(card->host, &cmd, retries);
679 if (err == 0)
680 *status = cmd.resp[0];
681 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300682}
683
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530684#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100685#define ERR_RETRY 2
686#define ERR_ABORT 1
687#define ERR_CONTINUE 0
688
689static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
690 bool status_valid, u32 status)
691{
692 switch (error) {
693 case -EILSEQ:
694 /* response crc error, retry the r/w cmd */
695 pr_err("%s: %s sending %s command, card status %#x\n",
696 req->rq_disk->disk_name, "response CRC error",
697 name, status);
698 return ERR_RETRY;
699
700 case -ETIMEDOUT:
701 pr_err("%s: %s sending %s command, card status %#x\n",
702 req->rq_disk->disk_name, "timed out", name, status);
703
704 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700705 if (!status_valid) {
706 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100707 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700708 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100709 /*
710 * If it was a r/w cmd crc error, or illegal command
711 * (eg, issued in wrong state) then retry - we should
712 * have corrected the state problem above.
713 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700714 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
715 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100716 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700717 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100718
719 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700720 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100721 return ERR_ABORT;
722
723 default:
724 /* We don't understand the error code the driver gave us */
725 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
726 req->rq_disk->disk_name, error, status);
727 return ERR_ABORT;
728 }
729}
730
731/*
732 * Initial r/w and stop cmd error recovery.
733 * We don't know whether the card received the r/w cmd or not, so try to
734 * restore things back to a sane state. Essentially, we do this as follows:
735 * - Obtain card status. If the first attempt to obtain card status fails,
736 * the status word will reflect the failed status cmd, not the failed
737 * r/w cmd. If we fail to obtain card status, it suggests we can no
738 * longer communicate with the card.
739 * - Check the card state. If the card received the cmd but there was a
740 * transient problem with the response, it might still be in a data transfer
741 * mode. Try to send it a stop command. If this fails, we can't recover.
742 * - If the r/w cmd failed due to a response CRC error, it was probably
743 * transient, so retry the cmd.
744 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
745 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
746 * illegal cmd, retry.
747 * Otherwise we don't understand what happened, so abort.
748 */
749static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300750 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100751{
752 bool prev_cmd_status_valid = true;
753 u32 status, stop_status = 0;
754 int err, retry;
755
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530756 if (mmc_card_removed(card))
757 return ERR_NOMEDIUM;
758
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100759 /*
760 * Try to get card status which indicates both the card state
761 * and why there was no response. If the first attempt fails,
762 * we can't be sure the returned status is for the r/w command.
763 */
764 for (retry = 2; retry >= 0; retry--) {
765 err = get_card_status(card, &status, 0);
766 if (!err)
767 break;
768
769 prev_cmd_status_valid = false;
770 pr_err("%s: error %d sending status command, %sing\n",
771 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
772 }
773
774 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530775 if (err) {
776 /* Check if the card is removed */
777 if (mmc_detect_card_removed(card->host))
778 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100779 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530780 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100781
Adrian Hunter67716322011-08-29 16:42:15 +0300782 /* Flag ECC errors */
783 if ((status & R1_CARD_ECC_FAILED) ||
784 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
785 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
786 *ecc_err = 1;
787
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100788 /*
789 * Check the current card state. If it is in some data transfer
790 * mode, tell it to stop (and hopefully transition back to TRAN.)
791 */
792 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
793 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
794 err = send_stop(card, &stop_status);
795 if (err)
796 pr_err("%s: error %d sending stop command\n",
797 req->rq_disk->disk_name, err);
798
799 /*
800 * If the stop cmd also timed out, the card is probably
801 * not present, so abort. Other errors are bad news too.
802 */
803 if (err)
804 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300805 if (stop_status & R1_CARD_ECC_FAILED)
806 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100807 }
808
809 /* Check for set block count errors */
810 if (brq->sbc.error)
811 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
812 prev_cmd_status_valid, status);
813
814 /* Check for r/w command errors */
815 if (brq->cmd.error)
816 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
817 prev_cmd_status_valid, status);
818
Adrian Hunter67716322011-08-29 16:42:15 +0300819 /* Data errors */
820 if (!brq->stop.error)
821 return ERR_CONTINUE;
822
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100823 /* Now for stop errors. These aren't fatal to the transfer. */
824 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
825 req->rq_disk->disk_name, brq->stop.error,
826 brq->cmd.resp[0], status);
827
828 /*
829 * Subsitute in our own stop status as this will give the error
830 * state which happened during the execution of the r/w command.
831 */
832 if (stop_status) {
833 brq->stop.resp[0] = stop_status;
834 brq->stop.error = 0;
835 }
836 return ERR_CONTINUE;
837}
838
Adrian Hunter67716322011-08-29 16:42:15 +0300839static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
840 int type)
841{
842 int err;
843
844 if (md->reset_done & type)
845 return -EEXIST;
846
847 md->reset_done |= type;
848 err = mmc_hw_reset(host);
849 /* Ensure we switch back to the correct partition */
850 if (err != -EOPNOTSUPP) {
851 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
852 int part_err;
853
854 main_md->part_curr = main_md->part_type;
855 part_err = mmc_blk_part_switch(host->card, md);
856 if (part_err) {
857 /*
858 * We have failed to get back into the correct
859 * partition, so we need to abort the whole request.
860 */
861 return -ENODEV;
862 }
863 }
864 return err;
865}
866
867static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
868{
869 md->reset_done &= ~type;
870}
871
Adrian Hunterbd788c92010-08-11 14:17:47 -0700872static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
873{
874 struct mmc_blk_data *md = mq->data;
875 struct mmc_card *card = md->queue.card;
876 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300877 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700878
Adrian Hunterbd788c92010-08-11 14:17:47 -0700879 if (!mmc_can_erase(card)) {
880 err = -EOPNOTSUPP;
881 goto out;
882 }
883
884 from = blk_rq_pos(req);
885 nr = blk_rq_sectors(req);
886
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900887 if (mmc_can_discard(card))
888 arg = MMC_DISCARD_ARG;
889 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700890 arg = MMC_TRIM_ARG;
891 else
892 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300893retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500894 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
895 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
896 INAND_CMD38_ARG_EXT_CSD,
897 arg == MMC_TRIM_ARG ?
898 INAND_CMD38_ARG_TRIM :
899 INAND_CMD38_ARG_ERASE,
900 0);
901 if (err)
902 goto out;
903 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700904 err = mmc_erase(card, from, nr, arg);
905out:
Adrian Hunter67716322011-08-29 16:42:15 +0300906 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
907 goto retry;
908 if (!err)
909 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530910 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700911
Adrian Hunterbd788c92010-08-11 14:17:47 -0700912 return err ? 0 : 1;
913}
914
Adrian Hunter49804542010-08-11 14:17:50 -0700915static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
916 struct request *req)
917{
918 struct mmc_blk_data *md = mq->data;
919 struct mmc_card *card = md->queue.card;
920 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300921 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700922
Maya Erez463bb952012-05-24 23:46:29 +0300923 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700924 err = -EOPNOTSUPP;
925 goto out;
926 }
927
928 from = blk_rq_pos(req);
929 nr = blk_rq_sectors(req);
930
931 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
932 arg = MMC_SECURE_TRIM1_ARG;
933 else
934 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300935retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500936 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
937 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
938 INAND_CMD38_ARG_EXT_CSD,
939 arg == MMC_SECURE_TRIM1_ARG ?
940 INAND_CMD38_ARG_SECTRIM1 :
941 INAND_CMD38_ARG_SECERASE,
942 0);
943 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300944 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500945 }
Adrian Hunter28302812012-04-05 14:45:48 +0300946
Adrian Hunter49804542010-08-11 14:17:50 -0700947 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300948 if (err == -EIO)
949 goto out_retry;
950 if (err)
951 goto out;
952
953 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500954 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
955 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
956 INAND_CMD38_ARG_EXT_CSD,
957 INAND_CMD38_ARG_SECTRIM2,
958 0);
959 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300960 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500961 }
Adrian Hunter28302812012-04-05 14:45:48 +0300962
Adrian Hunter49804542010-08-11 14:17:50 -0700963 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300964 if (err == -EIO)
965 goto out_retry;
966 if (err)
967 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500968 }
Adrian Hunter28302812012-04-05 14:45:48 +0300969
970 if (mmc_can_sanitize(card))
971 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
972 EXT_CSD_SANITIZE_START, 1, 0);
973out_retry:
974 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300975 goto retry;
976 if (!err)
977 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300978out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530979 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700980
Adrian Hunter49804542010-08-11 14:17:50 -0700981 return err ? 0 : 1;
982}
983
Maya Erez463bb952012-05-24 23:46:29 +0300984static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
985 struct request *req)
986{
987 struct mmc_blk_data *md = mq->data;
988 struct mmc_card *card = md->queue.card;
989 int err = 0;
990
991 BUG_ON(!card);
992 BUG_ON(!card->host);
993
994 if (!(mmc_can_sanitize(card) &&
995 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
996 pr_warning("%s: %s - SANITIZE is not supported\n",
997 mmc_hostname(card->host), __func__);
998 err = -EOPNOTSUPP;
999 goto out;
1000 }
1001
1002 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1003 mmc_hostname(card->host), __func__);
1004
1005 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001006 EXT_CSD_SANITIZE_START, 1,
1007 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001008
1009 if (err)
1010 pr_err("%s: %s - mmc_switch() with "
1011 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1012 mmc_hostname(card->host), __func__, err);
1013
1014 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1015 __func__);
1016
1017out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301018 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001019
1020 return err ? 0 : 1;
1021}
1022
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001023static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1024{
1025 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001026 struct mmc_card *card = md->queue.card;
1027 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001028
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001029 ret = mmc_flush_cache(card);
1030 if (ret)
1031 ret = -EIO;
1032
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301033 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001034
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001035 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001036}
1037
1038/*
1039 * Reformat current write as a reliable write, supporting
1040 * both legacy and the enhanced reliable write MMC cards.
1041 * In each transfer we'll handle only as much as a single
1042 * reliable write can handle, thus finish the request in
1043 * partial completions.
1044 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001045static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1046 struct mmc_card *card,
1047 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001048{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001049 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1050 /* Legacy mode imposes restrictions on transfers. */
1051 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1052 brq->data.blocks = 1;
1053
1054 if (brq->data.blocks > card->ext_csd.rel_sectors)
1055 brq->data.blocks = card->ext_csd.rel_sectors;
1056 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1057 brq->data.blocks = 1;
1058 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001059}
1060
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001061#define CMD_ERRORS \
1062 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1063 R1_ADDRESS_ERROR | /* Misaligned address */ \
1064 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1065 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1066 R1_CC_ERROR | /* Card controller error */ \
1067 R1_ERROR) /* General/unknown error */
1068
Per Forlinee8a43a2011-07-01 18:55:33 +02001069static int mmc_blk_err_check(struct mmc_card *card,
1070 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001071{
Per Forlinee8a43a2011-07-01 18:55:33 +02001072 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1073 mmc_active);
1074 struct mmc_blk_request *brq = &mq_mrq->brq;
1075 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001076 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001077
1078 /*
1079 * sbc.error indicates a problem with the set block count
1080 * command. No data will have been transferred.
1081 *
1082 * cmd.error indicates a problem with the r/w command. No
1083 * data will have been transferred.
1084 *
1085 * stop.error indicates a problem with the stop command. Data
1086 * may have been transferred, or may still be transferring.
1087 */
Adrian Hunter67716322011-08-29 16:42:15 +03001088 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1089 brq->data.error) {
1090 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001091 case ERR_RETRY:
1092 return MMC_BLK_RETRY;
1093 case ERR_ABORT:
1094 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301095 case ERR_NOMEDIUM:
1096 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001097 case ERR_CONTINUE:
1098 break;
1099 }
1100 }
1101
1102 /*
1103 * Check for errors relating to the execution of the
1104 * initial command - such as address errors. No data
1105 * has been transferred.
1106 */
1107 if (brq->cmd.resp[0] & CMD_ERRORS) {
1108 pr_err("%s: r/w command failed, status = %#x\n",
1109 req->rq_disk->disk_name, brq->cmd.resp[0]);
1110 return MMC_BLK_ABORT;
1111 }
1112
1113 /*
1114 * Everything else is either success, or a data error of some
1115 * kind. If it was a write, we may have transitioned to
1116 * program mode, which we have to wait for it to complete.
1117 */
1118 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1119 u32 status;
1120 do {
1121 int err = get_card_status(card, &status, 5);
1122 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301123 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001124 req->rq_disk->disk_name, err);
1125 return MMC_BLK_CMD_ERR;
1126 }
1127 /*
1128 * Some cards mishandle the status bits,
1129 * so make sure to check both the busy
1130 * indication and the card state.
1131 */
1132 } while (!(status & R1_READY_FOR_DATA) ||
1133 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1134 }
1135
1136 if (brq->data.error) {
1137 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1138 req->rq_disk->disk_name, brq->data.error,
1139 (unsigned)blk_rq_pos(req),
1140 (unsigned)blk_rq_sectors(req),
1141 brq->cmd.resp[0], brq->stop.resp[0]);
1142
1143 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001144 if (ecc_err)
1145 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001146 return MMC_BLK_DATA_ERR;
1147 } else {
1148 return MMC_BLK_CMD_ERR;
1149 }
1150 }
1151
Adrian Hunter67716322011-08-29 16:42:15 +03001152 if (!brq->data.bytes_xfered)
1153 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001154
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001155 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1156 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1157 return MMC_BLK_PARTIAL;
1158 else
1159 return MMC_BLK_SUCCESS;
1160 }
1161
Adrian Hunter67716322011-08-29 16:42:15 +03001162 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1163 return MMC_BLK_PARTIAL;
1164
1165 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001166}
1167
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001168static int mmc_blk_packed_err_check(struct mmc_card *card,
1169 struct mmc_async_req *areq)
1170{
1171 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1172 mmc_active);
1173 struct request *req = mq_rq->req;
1174 int err, check, status;
1175 u8 ext_csd[512];
1176
1177 mq_rq->packed_retries--;
1178 check = mmc_blk_err_check(card, areq);
1179 err = get_card_status(card, &status, 0);
1180 if (err) {
1181 pr_err("%s: error %d sending status command\n",
1182 req->rq_disk->disk_name, err);
1183 return MMC_BLK_ABORT;
1184 }
1185
1186 if (status & R1_EXCEPTION_EVENT) {
1187 err = mmc_send_ext_csd(card, ext_csd);
1188 if (err) {
1189 pr_err("%s: error %d sending ext_csd\n",
1190 req->rq_disk->disk_name, err);
1191 return MMC_BLK_ABORT;
1192 }
1193
1194 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1195 EXT_CSD_PACKED_FAILURE) &&
1196 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1197 EXT_CSD_PACKED_GENERIC_ERROR)) {
1198 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1199 EXT_CSD_PACKED_INDEXED_ERROR) {
1200 mq_rq->packed_fail_idx =
1201 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1202 return MMC_BLK_PARTIAL;
1203 }
1204 }
1205 }
1206
1207 return check;
1208}
1209
Per Forlin54d49d772011-07-01 18:55:29 +02001210static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1211 struct mmc_card *card,
1212 int disable_multi,
1213 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Per Forlin54d49d772011-07-01 18:55:29 +02001215 u32 readcmd, writecmd;
1216 struct mmc_blk_request *brq = &mqrq->brq;
1217 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301219 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001221 /*
1222 * Reliable writes are used to implement Forced Unit Access and
1223 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001224 *
1225 * XXX: this really needs a good explanation of why REQ_META
1226 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001227 */
1228 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1229 (req->cmd_flags & REQ_META)) &&
1230 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001231 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001232
Per Forlin54d49d772011-07-01 18:55:29 +02001233 memset(brq, 0, sizeof(struct mmc_blk_request));
1234 brq->mrq.cmd = &brq->cmd;
1235 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Per Forlin54d49d772011-07-01 18:55:29 +02001237 brq->cmd.arg = blk_rq_pos(req);
1238 if (!mmc_card_blockaddr(card))
1239 brq->cmd.arg <<= 9;
1240 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1241 brq->data.blksz = 512;
1242 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1243 brq->stop.arg = 0;
1244 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1245 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Asutosh Das1a897142012-07-27 18:10:19 +05301247 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001248 /*
1249 * The block layer doesn't support all sector count
1250 * restrictions, so we need to be prepared for too big
1251 * requests.
1252 */
1253 if (brq->data.blocks > card->host->max_blk_count)
1254 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001256 if (brq->data.blocks > 1) {
1257 /*
1258 * After a read error, we redo the request one sector
1259 * at a time in order to accurately determine which
1260 * sectors can be read successfully.
1261 */
1262 if (disable_multi)
1263 brq->data.blocks = 1;
1264
1265 /* Some controllers can't do multiblock reads due to hw bugs */
1266 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1267 rq_data_dir(req) == READ)
1268 brq->data.blocks = 1;
1269 }
Per Forlin54d49d772011-07-01 18:55:29 +02001270
1271 if (brq->data.blocks > 1 || do_rel_wr) {
1272 /* SPI multiblock writes terminate using a special
1273 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001274 */
Per Forlin54d49d772011-07-01 18:55:29 +02001275 if (!mmc_host_is_spi(card->host) ||
1276 rq_data_dir(req) == READ)
1277 brq->mrq.stop = &brq->stop;
1278 readcmd = MMC_READ_MULTIPLE_BLOCK;
1279 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1280 } else {
1281 brq->mrq.stop = NULL;
1282 readcmd = MMC_READ_SINGLE_BLOCK;
1283 writecmd = MMC_WRITE_BLOCK;
1284 }
1285 if (rq_data_dir(req) == READ) {
1286 brq->cmd.opcode = readcmd;
1287 brq->data.flags |= MMC_DATA_READ;
1288 } else {
1289 brq->cmd.opcode = writecmd;
1290 brq->data.flags |= MMC_DATA_WRITE;
1291 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001292
Per Forlin54d49d772011-07-01 18:55:29 +02001293 if (do_rel_wr)
1294 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001295
Per Forlin54d49d772011-07-01 18:55:29 +02001296 /*
Saugata Das42659002011-12-21 13:09:17 +05301297 * Data tag is used only during writing meta data to speed
1298 * up write and any subsequent read of this meta data
1299 */
1300 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1301 (req->cmd_flags & REQ_META) &&
1302 (rq_data_dir(req) == WRITE) &&
1303 ((brq->data.blocks * brq->data.blksz) >=
1304 card->ext_csd.data_tag_unit_size);
1305
1306 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001307 * Pre-defined multi-block transfers are preferable to
1308 * open ended-ones (and necessary for reliable writes).
1309 * However, it is not sufficient to just send CMD23,
1310 * and avoid the final CMD12, as on an error condition
1311 * CMD12 (stop) needs to be sent anyway. This, coupled
1312 * with Auto-CMD23 enhancements provided by some
1313 * hosts, means that the complexity of dealing
1314 * with this is best left to the host. If CMD23 is
1315 * supported by card and host, we'll fill sbc in and let
1316 * the host deal with handling it correctly. This means
1317 * that for hosts that don't expose MMC_CAP_CMD23, no
1318 * change of behavior will be observed.
1319 *
1320 * N.B: Some MMC cards experience perf degradation.
1321 * We'll avoid using CMD23-bounded multiblock writes for
1322 * these, while retaining features like reliable writes.
1323 */
Saugata Das42659002011-12-21 13:09:17 +05301324 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1325 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1326 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001327 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1328 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301329 (do_rel_wr ? (1 << 31) : 0) |
1330 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001331 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1332 brq->mrq.sbc = &brq->sbc;
1333 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001334
Per Forlin54d49d772011-07-01 18:55:29 +02001335 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001336
Per Forlin54d49d772011-07-01 18:55:29 +02001337 brq->data.sg = mqrq->sg;
1338 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001339
Per Forlin54d49d772011-07-01 18:55:29 +02001340 /*
1341 * Adjust the sg list so it is the same size as the
1342 * request.
1343 */
1344 if (brq->data.blocks != blk_rq_sectors(req)) {
1345 int i, data_size = brq->data.blocks << 9;
1346 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001347
Per Forlin54d49d772011-07-01 18:55:29 +02001348 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1349 data_size -= sg->length;
1350 if (data_size <= 0) {
1351 sg->length += data_size;
1352 i++;
1353 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001354 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001355 }
Per Forlin54d49d772011-07-01 18:55:29 +02001356 brq->data.sg_len = i;
1357 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001358
Per Forlinee8a43a2011-07-01 18:55:33 +02001359 mqrq->mmc_active.mrq = &brq->mrq;
1360 mqrq->mmc_active.err_check = mmc_blk_err_check;
1361
Per Forlin54d49d772011-07-01 18:55:29 +02001362 mmc_queue_bounce_pre(mqrq);
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001365static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1366 struct request *req)
1367{
1368 struct mmc_host *host = mq->card->host;
1369 int data_dir;
1370
1371 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1372 return;
1373
1374 /*
1375 * In case the packing control is not supported by the host, it should
1376 * not have an effect on the write packing. Therefore we have to enable
1377 * the write packing
1378 */
1379 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1380 mq->wr_packing_enabled = true;
1381 return;
1382 }
1383
1384 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1385 if (mq->num_of_potential_packed_wr_reqs >
1386 mq->num_wr_reqs_to_start_packing)
1387 mq->wr_packing_enabled = true;
1388 return;
1389 }
1390
1391 data_dir = rq_data_dir(req);
1392
1393 if (data_dir == READ) {
1394 mq->num_of_potential_packed_wr_reqs = 0;
1395 mq->wr_packing_enabled = false;
1396 return;
1397 } else if (data_dir == WRITE) {
1398 mq->num_of_potential_packed_wr_reqs++;
1399 }
1400
1401 if (mq->num_of_potential_packed_wr_reqs >
1402 mq->num_wr_reqs_to_start_packing)
1403 mq->wr_packing_enabled = true;
1404
1405}
1406
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001407struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1408{
1409 if (!card)
1410 return NULL;
1411
1412 return &card->wr_pack_stats;
1413}
1414EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1415
1416void mmc_blk_init_packed_statistics(struct mmc_card *card)
1417{
1418 int max_num_of_packed_reqs = 0;
1419
1420 if (!card || !card->wr_pack_stats.packing_events)
1421 return;
1422
1423 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1424
1425 spin_lock(&card->wr_pack_stats.lock);
1426 memset(card->wr_pack_stats.packing_events, 0,
1427 (max_num_of_packed_reqs + 1) *
1428 sizeof(*card->wr_pack_stats.packing_events));
1429 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1430 sizeof(card->wr_pack_stats.pack_stop_reason));
1431 card->wr_pack_stats.enabled = true;
1432 spin_unlock(&card->wr_pack_stats.lock);
1433}
1434EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1435
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001436static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1437{
1438 struct request_queue *q = mq->queue;
1439 struct mmc_card *card = mq->card;
1440 struct request *cur = req, *next = NULL;
1441 struct mmc_blk_data *md = mq->data;
1442 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1443 unsigned int req_sectors = 0, phys_segments = 0;
1444 unsigned int max_blk_count, max_phys_segs;
1445 u8 put_back = 0;
1446 u8 max_packed_rw = 0;
1447 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001448 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001449
1450 mmc_blk_clear_packed(mq->mqrq_cur);
1451
1452 if (!(md->flags & MMC_BLK_CMD23) ||
1453 !card->ext_csd.packed_event_en)
1454 goto no_packed;
1455
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001456 if (!mq->wr_packing_enabled)
1457 goto no_packed;
1458
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001459 if ((rq_data_dir(cur) == WRITE) &&
1460 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1461 max_packed_rw = card->ext_csd.max_packed_writes;
1462
1463 if (max_packed_rw == 0)
1464 goto no_packed;
1465
1466 if (mmc_req_rel_wr(cur) &&
1467 (md->flags & MMC_BLK_REL_WR) &&
1468 !en_rel_wr)
1469 goto no_packed;
1470
1471 if (mmc_large_sec(card) &&
1472 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1473 goto no_packed;
1474
1475 max_blk_count = min(card->host->max_blk_count,
1476 card->host->max_req_size >> 9);
1477 if (unlikely(max_blk_count > 0xffff))
1478 max_blk_count = 0xffff;
1479
1480 max_phys_segs = queue_max_segments(q);
1481 req_sectors += blk_rq_sectors(cur);
1482 phys_segments += cur->nr_phys_segments;
1483
1484 if (rq_data_dir(cur) == WRITE) {
1485 req_sectors++;
1486 phys_segments++;
1487 }
1488
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001489 spin_lock(&stats->lock);
1490
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001491 while (reqs < max_packed_rw - 1) {
1492 spin_lock_irq(q->queue_lock);
1493 next = blk_fetch_request(q);
1494 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001495 if (!next) {
1496 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001497 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001498 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001499
1500 if (mmc_large_sec(card) &&
1501 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001502 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001503 put_back = 1;
1504 break;
1505 }
1506
1507 if (next->cmd_flags & REQ_DISCARD ||
1508 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001509 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001510 put_back = 1;
1511 break;
1512 }
1513
1514 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001515 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001516 put_back = 1;
1517 break;
1518 }
1519
1520 if (mmc_req_rel_wr(next) &&
1521 (md->flags & MMC_BLK_REL_WR) &&
1522 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001523 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001524 put_back = 1;
1525 break;
1526 }
1527
1528 req_sectors += blk_rq_sectors(next);
1529 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001530 if (stats->enabled)
1531 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001532 put_back = 1;
1533 break;
1534 }
1535
1536 phys_segments += next->nr_phys_segments;
1537 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001538 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001539 put_back = 1;
1540 break;
1541 }
1542
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001543 if (rq_data_dir(next) == WRITE)
1544 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001545 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1546 cur = next;
1547 reqs++;
1548 }
1549
1550 if (put_back) {
1551 spin_lock_irq(q->queue_lock);
1552 blk_requeue_request(q, next);
1553 spin_unlock_irq(q->queue_lock);
1554 }
1555
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001556 if (stats->enabled) {
1557 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1558 stats->packing_events[reqs + 1]++;
1559 if (reqs + 1 == max_packed_rw)
1560 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1561 }
1562
1563 spin_unlock(&stats->lock);
1564
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001565 if (reqs > 0) {
1566 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1567 mq->mqrq_cur->packed_num = ++reqs;
1568 mq->mqrq_cur->packed_retries = reqs;
1569 return reqs;
1570 }
1571
1572no_packed:
1573 mmc_blk_clear_packed(mq->mqrq_cur);
1574 return 0;
1575}
1576
1577static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1578 struct mmc_card *card,
1579 struct mmc_queue *mq)
1580{
1581 struct mmc_blk_request *brq = &mqrq->brq;
1582 struct request *req = mqrq->req;
1583 struct request *prq;
1584 struct mmc_blk_data *md = mq->data;
1585 bool do_rel_wr, do_data_tag;
1586 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1587 u8 i = 1;
1588
1589 mqrq->packed_cmd = MMC_PACKED_WRITE;
1590 mqrq->packed_blocks = 0;
1591 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1592
1593 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1594 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1595 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1596
1597 /*
1598 * Argument for each entry of packed group
1599 */
1600 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1601 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1602 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1603 (prq->cmd_flags & REQ_META) &&
1604 (rq_data_dir(prq) == WRITE) &&
1605 ((brq->data.blocks * brq->data.blksz) >=
1606 card->ext_csd.data_tag_unit_size);
1607 /* Argument of CMD23 */
1608 packed_cmd_hdr[(i * 2)] =
1609 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1610 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1611 blk_rq_sectors(prq);
1612 /* Argument of CMD18 or CMD25 */
1613 packed_cmd_hdr[((i * 2)) + 1] =
1614 mmc_card_blockaddr(card) ?
1615 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1616 mqrq->packed_blocks += blk_rq_sectors(prq);
1617 i++;
1618 }
1619
1620 memset(brq, 0, sizeof(struct mmc_blk_request));
1621 brq->mrq.cmd = &brq->cmd;
1622 brq->mrq.data = &brq->data;
1623 brq->mrq.sbc = &brq->sbc;
1624 brq->mrq.stop = &brq->stop;
1625
1626 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1627 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1628 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1629
1630 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1631 brq->cmd.arg = blk_rq_pos(req);
1632 if (!mmc_card_blockaddr(card))
1633 brq->cmd.arg <<= 9;
1634 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1635
1636 brq->data.blksz = 512;
1637 brq->data.blocks = mqrq->packed_blocks + 1;
1638 brq->data.flags |= MMC_DATA_WRITE;
1639
1640 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1641 brq->stop.arg = 0;
1642 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1643
1644 mmc_set_data_timeout(&brq->data, card);
1645
1646 brq->data.sg = mqrq->sg;
1647 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1648
1649 mqrq->mmc_active.mrq = &brq->mrq;
1650 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1651
1652 mmc_queue_bounce_pre(mqrq);
1653}
1654
Adrian Hunter67716322011-08-29 16:42:15 +03001655static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1656 struct mmc_blk_request *brq, struct request *req,
1657 int ret)
1658{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001659 struct mmc_queue_req *mq_rq;
1660 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1661
Adrian Hunter67716322011-08-29 16:42:15 +03001662 /*
1663 * If this is an SD card and we're writing, we can first
1664 * mark the known good sectors as ok.
1665 *
1666 * If the card is not SD, we can still ok written sectors
1667 * as reported by the controller (which might be less than
1668 * the real number of written sectors, but never more).
1669 */
1670 if (mmc_card_sd(card)) {
1671 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301672 if (!brq->data.fault_injected) {
1673 blocks = mmc_sd_num_wr_blocks(card);
1674 if (blocks != (u32)-1)
1675 ret = blk_end_request(req, 0, blocks << 9);
1676 } else
1677 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001678 } else {
1679 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1680 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1681 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001682 return ret;
1683}
1684
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001685static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1686{
1687 struct request *prq;
1688 int idx = mq_rq->packed_fail_idx, i = 0;
1689 int ret = 0;
1690
1691 while (!list_empty(&mq_rq->packed_list)) {
1692 prq = list_entry_rq(mq_rq->packed_list.next);
1693 if (idx == i) {
1694 /* retry from error index */
1695 mq_rq->packed_num -= idx;
1696 mq_rq->req = prq;
1697 ret = 1;
1698
1699 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1700 list_del_init(&prq->queuelist);
1701 mmc_blk_clear_packed(mq_rq);
1702 }
1703 return ret;
1704 }
1705 list_del_init(&prq->queuelist);
1706 blk_end_request(prq, 0, blk_rq_bytes(prq));
1707 i++;
1708 }
1709
1710 mmc_blk_clear_packed(mq_rq);
1711 return ret;
1712}
1713static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1714{
1715 struct request *prq;
1716
1717 while (!list_empty(&mq_rq->packed_list)) {
1718 prq = list_entry_rq(mq_rq->packed_list.next);
1719 list_del_init(&prq->queuelist);
1720 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1721 }
1722
1723 mmc_blk_clear_packed(mq_rq);
1724}
1725
1726static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1727 struct mmc_queue_req *mq_rq)
1728{
1729 struct request *prq;
1730 struct request_queue *q = mq->queue;
1731
1732 while (!list_empty(&mq_rq->packed_list)) {
1733 prq = list_entry_rq(mq_rq->packed_list.prev);
1734 if (prq->queuelist.prev != &mq_rq->packed_list) {
1735 list_del_init(&prq->queuelist);
1736 spin_lock_irq(q->queue_lock);
1737 blk_requeue_request(mq->queue, prq);
1738 spin_unlock_irq(q->queue_lock);
1739 } else {
1740 list_del_init(&prq->queuelist);
1741 }
1742 }
1743
1744 mmc_blk_clear_packed(mq_rq);
1745}
1746
Per Forlinee8a43a2011-07-01 18:55:33 +02001747static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001748{
1749 struct mmc_blk_data *md = mq->data;
1750 struct mmc_card *card = md->queue.card;
1751 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001752 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001753 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001754 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001755 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001756 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001757 const u8 packed_num = 2;
1758 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001759
1760 if (!rqc && !mq->mqrq_prev->req)
1761 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001762
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001763 if (rqc)
1764 reqs = mmc_blk_prep_packed_list(mq, rqc);
1765
Per Forlin54d49d772011-07-01 18:55:29 +02001766 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001767 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001768 if (reqs >= packed_num)
1769 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1770 card, mq);
1771 else
1772 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001773 areq = &mq->mqrq_cur->mmc_active;
1774 } else
1775 areq = NULL;
1776 areq = mmc_start_req(card->host, areq, (int *) &status);
1777 if (!areq)
1778 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001779
Per Forlinee8a43a2011-07-01 18:55:33 +02001780 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1781 brq = &mq_rq->brq;
1782 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001783 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001784 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001785
Per Forlind78d4a82011-07-01 18:55:30 +02001786 switch (status) {
1787 case MMC_BLK_SUCCESS:
1788 case MMC_BLK_PARTIAL:
1789 /*
1790 * A block was successfully transferred.
1791 */
Adrian Hunter67716322011-08-29 16:42:15 +03001792 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001793
1794 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1795 ret = mmc_blk_end_packed_req(mq_rq);
1796 break;
1797 } else {
1798 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001799 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001800 }
1801
Adrian Hunter67716322011-08-29 16:42:15 +03001802 /*
1803 * If the blk_end_request function returns non-zero even
1804 * though all data has been transferred and no errors
1805 * were returned by the host controller, it's a bug.
1806 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001807 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301808 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001809 __func__, blk_rq_bytes(req),
1810 brq->data.bytes_xfered);
1811 rqc = NULL;
1812 goto cmd_abort;
1813 }
Per Forlind78d4a82011-07-01 18:55:30 +02001814 break;
1815 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001816 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1817 if (!mmc_blk_reset(md, card->host, type))
1818 break;
1819 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001820 case MMC_BLK_RETRY:
1821 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001822 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001823 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001824 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001825 if (!mmc_blk_reset(md, card->host, type))
1826 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001827 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001828 case MMC_BLK_DATA_ERR: {
1829 int err;
1830
1831 err = mmc_blk_reset(md, card->host, type);
1832 if (!err)
1833 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001834 if (err == -ENODEV ||
1835 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001836 goto cmd_abort;
1837 /* Fall through */
1838 }
1839 case MMC_BLK_ECC_ERR:
1840 if (brq->data.blocks > 1) {
1841 /* Redo read one sector at a time */
1842 pr_warning("%s: retrying using single block read\n",
1843 req->rq_disk->disk_name);
1844 disable_multi = 1;
1845 break;
1846 }
Per Forlind78d4a82011-07-01 18:55:30 +02001847 /*
1848 * After an error, we redo I/O one sector at a
1849 * time, so we only reach here after trying to
1850 * read a single sector.
1851 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301852 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001853 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001854 if (!ret)
1855 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001856 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301857 case MMC_BLK_NOMEDIUM:
1858 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001859 }
1860
Per Forlinee8a43a2011-07-01 18:55:33 +02001861 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001862 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1863 /*
1864 * In case of a incomplete request
1865 * prepare it again and resend.
1866 */
1867 mmc_blk_rw_rq_prep(mq_rq, card,
1868 disable_multi, mq);
1869 mmc_start_req(card->host,
1870 &mq_rq->mmc_active, NULL);
1871 } else {
1872 if (!mq_rq->packed_retries)
1873 goto cmd_abort;
1874 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1875 mmc_start_req(card->host,
1876 &mq_rq->mmc_active, NULL);
1877 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 } while (ret);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 1;
1882
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001883 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001884 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1885 if (mmc_card_removed(card))
1886 req->cmd_flags |= REQ_QUIET;
1887 while (ret)
1888 ret = blk_end_request(req, -EIO,
1889 blk_rq_cur_bytes(req));
1890 } else {
1891 mmc_blk_abort_packed_req(mq_rq);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Per Forlinee8a43a2011-07-01 18:55:33 +02001894 start_new_req:
1895 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001896 /*
1897 * If current request is packed, it needs to put back.
1898 */
1899 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1900 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1901
Per Forlinee8a43a2011-07-01 18:55:33 +02001902 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1903 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1904 }
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return 0;
1907}
1908
Adrian Hunterbd788c92010-08-11 14:17:47 -07001909static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1910{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001911 int ret;
1912 struct mmc_blk_data *md = mq->data;
1913 struct mmc_card *card = md->queue.card;
1914
San Mehat148c57a2009-07-30 08:21:19 -07001915#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1916 if (mmc_bus_needs_resume(card->host)) {
1917 mmc_resume_bus(card->host);
1918 mmc_blk_set_blksize(md, card);
1919 }
1920#endif
1921
Per Forlinee8a43a2011-07-01 18:55:33 +02001922 if (req && !mq->mqrq_prev->req)
1923 /* claim host only for the first request */
1924 mmc_claim_host(card->host);
1925
Andrei Warkentin371a6892011-04-11 18:10:25 -05001926 ret = mmc_blk_part_switch(card, md);
1927 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001928 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301929 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001930 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001931 ret = 0;
1932 goto out;
1933 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001934
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001935 mmc_blk_write_packing_control(mq, req);
1936
Maya Erez463bb952012-05-24 23:46:29 +03001937 if (req && req->cmd_flags & REQ_SANITIZE) {
1938 /* complete ongoing async transfer before issuing sanitize */
1939 if (card->host && card->host->areq)
1940 mmc_blk_issue_rw_rq(mq, NULL);
1941 ret = mmc_blk_issue_sanitize_rq(mq, req);
1942 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001943 /* complete ongoing async transfer before issuing discard */
1944 if (card->host->areq)
1945 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001946 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001947 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001948 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001949 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001950 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001951 /* complete ongoing async transfer before issuing flush */
1952 if (card->host->areq)
1953 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001954 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001955 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001956 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001957 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001958
Andrei Warkentin371a6892011-04-11 18:10:25 -05001959out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001960 if (!req)
1961 /* release host only when there are no more requests */
1962 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001963 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001964}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Russell Kinga6f6c962006-01-03 22:38:44 +00001966static inline int mmc_blk_readonly(struct mmc_card *card)
1967{
1968 return mmc_card_readonly(card) ||
1969 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1970}
1971
Andrei Warkentin371a6892011-04-11 18:10:25 -05001972static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1973 struct device *parent,
1974 sector_t size,
1975 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001976 const char *subname,
1977 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct mmc_blk_data *md;
1980 int devidx, ret;
1981
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001982 devidx = find_first_zero_bit(dev_use, max_devices);
1983 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return ERR_PTR(-ENOSPC);
1985 __set_bit(devidx, dev_use);
1986
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001987 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001988 if (!md) {
1989 ret = -ENOMEM;
1990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001992
Russell Kinga6f6c962006-01-03 22:38:44 +00001993 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001994 * !subname implies we are creating main mmc_blk_data that will be
1995 * associated with mmc_card with mmc_set_drvdata. Due to device
1996 * partitions, devidx will not coincide with a per-physical card
1997 * index anymore so we keep track of a name index.
1998 */
1999 if (!subname) {
2000 md->name_idx = find_first_zero_bit(name_use, max_devices);
2001 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002002 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002003 md->name_idx = ((struct mmc_blk_data *)
2004 dev_to_disk(parent)->private_data)->name_idx;
2005
Johan Rudholmadd710e2011-12-02 08:51:06 +01002006 md->area_type = area_type;
2007
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002008 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002009 * Set the read-only status based on the supported commands
2010 * and the write protect switch.
2011 */
2012 md->read_only = mmc_blk_readonly(card);
2013
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002014 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002015 if (md->disk == NULL) {
2016 ret = -ENOMEM;
2017 goto err_kfree;
2018 }
2019
2020 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002021 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002022 md->usage = 1;
2023
Adrian Hunterd09408a2011-06-23 13:40:28 +03002024 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002025 if (ret)
2026 goto err_putdisk;
2027
Russell Kinga6f6c962006-01-03 22:38:44 +00002028 md->queue.issue_fn = mmc_blk_issue_rq;
2029 md->queue.data = md;
2030
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002031 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002032 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002033 md->disk->fops = &mmc_bdops;
2034 md->disk->private_data = md;
2035 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036 md->disk->driverfs_dev = parent;
2037 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002038 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002039
2040 /*
2041 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2042 *
2043 * - be set for removable media with permanent block devices
2044 * - be unset for removable block devices with permanent media
2045 *
2046 * Since MMC block devices clearly fall under the second
2047 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2048 * should use the block device creation/destruction hotplug
2049 * messages to tell when the card is present.
2050 */
2051
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002052 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2053 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002054
Martin K. Petersene1defc42009-05-22 17:17:49 -04002055 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002056 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002057
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002058 if (mmc_host_cmd23(card->host)) {
2059 if (mmc_card_mmc(card) ||
2060 (mmc_card_sd(card) &&
2061 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2062 md->flags |= MMC_BLK_CMD23;
2063 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002064
2065 if (mmc_card_mmc(card) &&
2066 md->flags & MMC_BLK_CMD23 &&
2067 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2068 card->ext_csd.rel_sectors)) {
2069 md->flags |= MMC_BLK_REL_WR;
2070 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2071 }
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002074
2075 err_putdisk:
2076 put_disk(md->disk);
2077 err_kfree:
2078 kfree(md);
2079 out:
2080 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Andrei Warkentin371a6892011-04-11 18:10:25 -05002083static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2084{
2085 sector_t size;
2086 struct mmc_blk_data *md;
2087
2088 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2089 /*
2090 * The EXT_CSD sector count is in number or 512 byte
2091 * sectors.
2092 */
2093 size = card->ext_csd.sectors;
2094 } else {
2095 /*
2096 * The CSD capacity field is in units of read_blkbits.
2097 * set_capacity takes units of 512 bytes.
2098 */
2099 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2100 }
2101
Johan Rudholmadd710e2011-12-02 08:51:06 +01002102 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2103 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002104 return md;
2105}
2106
2107static int mmc_blk_alloc_part(struct mmc_card *card,
2108 struct mmc_blk_data *md,
2109 unsigned int part_type,
2110 sector_t size,
2111 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002112 const char *subname,
2113 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002114{
2115 char cap_str[10];
2116 struct mmc_blk_data *part_md;
2117
2118 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002119 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002120 if (IS_ERR(part_md))
2121 return PTR_ERR(part_md);
2122 part_md->part_type = part_type;
2123 list_add(&part_md->part, &md->part);
2124
2125 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2126 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302127 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002128 part_md->disk->disk_name, mmc_card_id(card),
2129 mmc_card_name(card), part_md->part_type, cap_str);
2130 return 0;
2131}
2132
Namjae Jeone0c368d2011-10-06 23:41:38 +09002133/* MMC Physical partitions consist of two boot partitions and
2134 * up to four general purpose partitions.
2135 * For each partition enabled in EXT_CSD a block device will be allocatedi
2136 * to provide access to the partition.
2137 */
2138
Andrei Warkentin371a6892011-04-11 18:10:25 -05002139static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2140{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002141 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002142
2143 if (!mmc_card_mmc(card))
2144 return 0;
2145
Namjae Jeone0c368d2011-10-06 23:41:38 +09002146 for (idx = 0; idx < card->nr_parts; idx++) {
2147 if (card->part[idx].size) {
2148 ret = mmc_blk_alloc_part(card, md,
2149 card->part[idx].part_cfg,
2150 card->part[idx].size >> 9,
2151 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002152 card->part[idx].name,
2153 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002154 if (ret)
2155 return ret;
2156 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002157 }
2158
2159 return ret;
2160}
2161
Andrei Warkentin371a6892011-04-11 18:10:25 -05002162static void mmc_blk_remove_req(struct mmc_blk_data *md)
2163{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002164 struct mmc_card *card;
2165
Andrei Warkentin371a6892011-04-11 18:10:25 -05002166 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002167 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002168 device_remove_file(disk_to_dev(md->disk),
2169 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002170 if (md->disk->flags & GENHD_FL_UP) {
2171 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002172 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2173 card->ext_csd.boot_ro_lockable)
2174 device_remove_file(disk_to_dev(md->disk),
2175 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002176
2177 /* Stop new requests from getting into the queue */
2178 del_gendisk(md->disk);
2179 }
2180
2181 /* Then flush out any already in there */
2182 mmc_cleanup_queue(&md->queue);
2183 mmc_blk_put(md);
2184 }
2185}
2186
2187static void mmc_blk_remove_parts(struct mmc_card *card,
2188 struct mmc_blk_data *md)
2189{
2190 struct list_head *pos, *q;
2191 struct mmc_blk_data *part_md;
2192
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002193 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002194 list_for_each_safe(pos, q, &md->part) {
2195 part_md = list_entry(pos, struct mmc_blk_data, part);
2196 list_del(pos);
2197 mmc_blk_remove_req(part_md);
2198 }
2199}
2200
2201static int mmc_add_disk(struct mmc_blk_data *md)
2202{
2203 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002204 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002205
2206 add_disk(md->disk);
2207 md->force_ro.show = force_ro_show;
2208 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302209 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002210 md->force_ro.attr.name = "force_ro";
2211 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2212 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2213 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002214 goto force_ro_fail;
2215
2216 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2217 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002218 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002219
2220 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2221 mode = S_IRUGO;
2222 else
2223 mode = S_IRUGO | S_IWUSR;
2224
2225 md->power_ro_lock.show = power_ro_lock_show;
2226 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002227 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002228 md->power_ro_lock.attr.mode = mode;
2229 md->power_ro_lock.attr.name =
2230 "ro_lock_until_next_power_on";
2231 ret = device_create_file(disk_to_dev(md->disk),
2232 &md->power_ro_lock);
2233 if (ret)
2234 goto power_ro_lock_fail;
2235 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002236
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002237 md->num_wr_reqs_to_start_packing.show =
2238 num_wr_reqs_to_start_packing_show;
2239 md->num_wr_reqs_to_start_packing.store =
2240 num_wr_reqs_to_start_packing_store;
2241 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2242 md->num_wr_reqs_to_start_packing.attr.name =
2243 "num_wr_reqs_to_start_packing";
2244 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2245 ret = device_create_file(disk_to_dev(md->disk),
2246 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002247 if (ret)
2248 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002249
Johan Rudholmadd710e2011-12-02 08:51:06 +01002250 return ret;
2251
2252power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002253 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002254force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002255 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002256
2257 return ret;
2258}
2259
Chris Ballc59d4472011-11-11 22:01:43 -05002260#define CID_MANFID_SANDISK 0x2
2261#define CID_MANFID_TOSHIBA 0x11
2262#define CID_MANFID_MICRON 0x13
2263
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002264static const struct mmc_fixup blk_fixups[] =
2265{
Chris Ballc59d4472011-11-11 22:01:43 -05002266 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2267 MMC_QUIRK_INAND_CMD38),
2268 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2269 MMC_QUIRK_INAND_CMD38),
2270 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2271 MMC_QUIRK_INAND_CMD38),
2272 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2273 MMC_QUIRK_INAND_CMD38),
2274 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2275 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002276
2277 /*
2278 * Some MMC cards experience performance degradation with CMD23
2279 * instead of CMD12-bounded multiblock transfers. For now we'll
2280 * black list what's bad...
2281 * - Certain Toshiba cards.
2282 *
2283 * N.B. This doesn't affect SD cards.
2284 */
Chris Ballc59d4472011-11-11 22:01:43 -05002285 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002286 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002287 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002288 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002289 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002290 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002291
2292 /*
2293 * Some Micron MMC cards needs longer data read timeout than
2294 * indicated in CSD.
2295 */
Chris Ballc59d4472011-11-11 22:01:43 -05002296 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002297 MMC_QUIRK_LONG_READ_TIME),
2298
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302299 /* Some INAND MCP devices advertise incorrect timeout values */
2300 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2301 MMC_QUIRK_INAND_DATA_TIMEOUT),
2302
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002303 END_FIXUP
2304};
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static int mmc_blk_probe(struct mmc_card *card)
2307{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002308 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002309 char cap_str[10];
2310
Pierre Ossman912490d2005-05-21 10:27:02 +01002311 /*
2312 * Check that the card supports the command class(es) we need.
2313 */
2314 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -ENODEV;
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 md = mmc_blk_alloc(card);
2318 if (IS_ERR(md))
2319 return PTR_ERR(md);
2320
Yi Li444122f2009-02-05 15:31:57 +08002321 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002322 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302323 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002325 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Andrei Warkentin371a6892011-04-11 18:10:25 -05002327 if (mmc_blk_alloc_parts(card, md))
2328 goto out;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002331 mmc_fixup_device(card, blk_fixups);
2332
San Mehat148c57a2009-07-30 08:21:19 -07002333#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2334 mmc_set_bus_resume_policy(card->host, 1);
2335#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002336 if (mmc_add_disk(md))
2337 goto out;
2338
2339 list_for_each_entry(part_md, &md->part, part) {
2340 if (mmc_add_disk(part_md))
2341 goto out;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344
2345 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002346 mmc_blk_remove_parts(card, md);
2347 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351static void mmc_blk_remove(struct mmc_card *card)
2352{
2353 struct mmc_blk_data *md = mmc_get_drvdata(card);
2354
Andrei Warkentin371a6892011-04-11 18:10:25 -05002355 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002356 mmc_claim_host(card->host);
2357 mmc_blk_part_switch(card, md);
2358 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002359 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002361#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2362 mmc_set_bus_resume_policy(card->host, 0);
2363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002367static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002369 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct mmc_blk_data *md = mmc_get_drvdata(card);
2371
2372 if (md) {
2373 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002374 list_for_each_entry(part_md, &md->part, part) {
2375 mmc_queue_suspend(&part_md->queue);
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378 return 0;
2379}
2380
2381static int mmc_blk_resume(struct mmc_card *card)
2382{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002383 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct mmc_blk_data *md = mmc_get_drvdata(card);
2385
2386 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002387 /*
2388 * Resume involves the card going into idle state,
2389 * so current partition is always the main one.
2390 */
2391 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002393 list_for_each_entry(part_md, &md->part, part) {
2394 mmc_queue_resume(&part_md->queue);
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397 return 0;
2398}
2399#else
2400#define mmc_blk_suspend NULL
2401#define mmc_blk_resume NULL
2402#endif
2403
2404static struct mmc_driver mmc_driver = {
2405 .drv = {
2406 .name = "mmcblk",
2407 },
2408 .probe = mmc_blk_probe,
2409 .remove = mmc_blk_remove,
2410 .suspend = mmc_blk_suspend,
2411 .resume = mmc_blk_resume,
2412};
2413
2414static int __init mmc_blk_init(void)
2415{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002416 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002418 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2419 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2420
2421 max_devices = 256 / perdev_minors;
2422
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002423 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2424 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002427 res = mmc_register_driver(&mmc_driver);
2428 if (res)
2429 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002431 return 0;
2432 out2:
2433 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 out:
2435 return res;
2436}
2437
2438static void __exit mmc_blk_exit(void)
2439{
2440 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002441 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444module_init(mmc_blk_init);
2445module_exit(mmc_blk_exit);
2446
2447MODULE_LICENSE("GPL");
2448MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2449