blob: 9b4c82ab4c49a2da1978753f2fd809d7a98a3b29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon121b8e82012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman10217bc2012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon121b8e82012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Maya Erezb52c7182012-11-28 23:46:50 +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 Jeon121b8e82012-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 Jeon121b8e82012-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 Brokhman98e67812012-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 Jeon121b8e82012-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 Jeon121b8e82012-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 Brokhman98e67812012-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;
Tatyana Brokhmanc9989122012-10-07 10:26:27 +02001388 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001389 return;
1390 }
1391
1392 data_dir = rq_data_dir(req);
1393
1394 if (data_dir == READ) {
1395 mq->num_of_potential_packed_wr_reqs = 0;
1396 mq->wr_packing_enabled = false;
1397 return;
1398 } else if (data_dir == WRITE) {
1399 mq->num_of_potential_packed_wr_reqs++;
1400 }
1401
1402 if (mq->num_of_potential_packed_wr_reqs >
1403 mq->num_wr_reqs_to_start_packing)
1404 mq->wr_packing_enabled = true;
1405
1406}
1407
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001408struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1409{
1410 if (!card)
1411 return NULL;
1412
1413 return &card->wr_pack_stats;
1414}
1415EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1416
1417void mmc_blk_init_packed_statistics(struct mmc_card *card)
1418{
1419 int max_num_of_packed_reqs = 0;
1420
1421 if (!card || !card->wr_pack_stats.packing_events)
1422 return;
1423
1424 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1425
1426 spin_lock(&card->wr_pack_stats.lock);
1427 memset(card->wr_pack_stats.packing_events, 0,
1428 (max_num_of_packed_reqs + 1) *
1429 sizeof(*card->wr_pack_stats.packing_events));
1430 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1431 sizeof(card->wr_pack_stats.pack_stop_reason));
1432 card->wr_pack_stats.enabled = true;
1433 spin_unlock(&card->wr_pack_stats.lock);
1434}
1435EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1436
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001437static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1438{
1439 struct request_queue *q = mq->queue;
1440 struct mmc_card *card = mq->card;
1441 struct request *cur = req, *next = NULL;
1442 struct mmc_blk_data *md = mq->data;
1443 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1444 unsigned int req_sectors = 0, phys_segments = 0;
1445 unsigned int max_blk_count, max_phys_segs;
1446 u8 put_back = 0;
1447 u8 max_packed_rw = 0;
1448 u8 reqs = 0;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001449 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001450
1451 mmc_blk_clear_packed(mq->mqrq_cur);
1452
1453 if (!(md->flags & MMC_BLK_CMD23) ||
1454 !card->ext_csd.packed_event_en)
1455 goto no_packed;
1456
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001457 if (!mq->wr_packing_enabled)
1458 goto no_packed;
1459
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001460 if ((rq_data_dir(cur) == WRITE) &&
1461 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1462 max_packed_rw = card->ext_csd.max_packed_writes;
1463
1464 if (max_packed_rw == 0)
1465 goto no_packed;
1466
1467 if (mmc_req_rel_wr(cur) &&
1468 (md->flags & MMC_BLK_REL_WR) &&
1469 !en_rel_wr)
1470 goto no_packed;
1471
1472 if (mmc_large_sec(card) &&
1473 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1474 goto no_packed;
1475
1476 max_blk_count = min(card->host->max_blk_count,
1477 card->host->max_req_size >> 9);
1478 if (unlikely(max_blk_count > 0xffff))
1479 max_blk_count = 0xffff;
1480
1481 max_phys_segs = queue_max_segments(q);
1482 req_sectors += blk_rq_sectors(cur);
1483 phys_segments += cur->nr_phys_segments;
1484
1485 if (rq_data_dir(cur) == WRITE) {
1486 req_sectors++;
1487 phys_segments++;
1488 }
1489
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001490 spin_lock(&stats->lock);
1491
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001492 while (reqs < max_packed_rw - 1) {
1493 spin_lock_irq(q->queue_lock);
1494 next = blk_fetch_request(q);
1495 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001496 if (!next) {
1497 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001498 break;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001499 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001500
1501 if (mmc_large_sec(card) &&
1502 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhmana07f7712012-10-04 11:11:08 +02001503 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001504 put_back = 1;
1505 break;
1506 }
1507
1508 if (next->cmd_flags & REQ_DISCARD ||
1509 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001510 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001511 put_back = 1;
1512 break;
1513 }
1514
1515 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001516 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001517 put_back = 1;
1518 break;
1519 }
1520
1521 if (mmc_req_rel_wr(next) &&
1522 (md->flags & MMC_BLK_REL_WR) &&
1523 !en_rel_wr) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001524 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001525 put_back = 1;
1526 break;
1527 }
1528
1529 req_sectors += blk_rq_sectors(next);
1530 if (req_sectors > max_blk_count) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001531 if (stats->enabled)
1532 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001533 put_back = 1;
1534 break;
1535 }
1536
1537 phys_segments += next->nr_phys_segments;
1538 if (phys_segments > max_phys_segs) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001539 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001540 put_back = 1;
1541 break;
1542 }
1543
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001544 if (rq_data_dir(next) == WRITE)
1545 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001546 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1547 cur = next;
1548 reqs++;
1549 }
1550
1551 if (put_back) {
1552 spin_lock_irq(q->queue_lock);
1553 blk_requeue_request(q, next);
1554 spin_unlock_irq(q->queue_lock);
1555 }
1556
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001557 if (stats->enabled) {
1558 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1559 stats->packing_events[reqs + 1]++;
1560 if (reqs + 1 == max_packed_rw)
1561 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1562 }
1563
1564 spin_unlock(&stats->lock);
1565
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001566 if (reqs > 0) {
1567 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1568 mq->mqrq_cur->packed_num = ++reqs;
1569 mq->mqrq_cur->packed_retries = reqs;
1570 return reqs;
1571 }
1572
1573no_packed:
1574 mmc_blk_clear_packed(mq->mqrq_cur);
1575 return 0;
1576}
1577
1578static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1579 struct mmc_card *card,
1580 struct mmc_queue *mq)
1581{
1582 struct mmc_blk_request *brq = &mqrq->brq;
1583 struct request *req = mqrq->req;
1584 struct request *prq;
1585 struct mmc_blk_data *md = mq->data;
1586 bool do_rel_wr, do_data_tag;
1587 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1588 u8 i = 1;
1589
1590 mqrq->packed_cmd = MMC_PACKED_WRITE;
1591 mqrq->packed_blocks = 0;
1592 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1593
1594 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1595 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1596 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1597
1598 /*
1599 * Argument for each entry of packed group
1600 */
1601 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1602 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1603 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1604 (prq->cmd_flags & REQ_META) &&
1605 (rq_data_dir(prq) == WRITE) &&
1606 ((brq->data.blocks * brq->data.blksz) >=
1607 card->ext_csd.data_tag_unit_size);
1608 /* Argument of CMD23 */
1609 packed_cmd_hdr[(i * 2)] =
1610 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1611 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1612 blk_rq_sectors(prq);
1613 /* Argument of CMD18 or CMD25 */
1614 packed_cmd_hdr[((i * 2)) + 1] =
1615 mmc_card_blockaddr(card) ?
1616 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1617 mqrq->packed_blocks += blk_rq_sectors(prq);
1618 i++;
1619 }
1620
1621 memset(brq, 0, sizeof(struct mmc_blk_request));
1622 brq->mrq.cmd = &brq->cmd;
1623 brq->mrq.data = &brq->data;
1624 brq->mrq.sbc = &brq->sbc;
1625 brq->mrq.stop = &brq->stop;
1626
1627 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1628 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1629 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1630
1631 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1632 brq->cmd.arg = blk_rq_pos(req);
1633 if (!mmc_card_blockaddr(card))
1634 brq->cmd.arg <<= 9;
1635 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1636
1637 brq->data.blksz = 512;
1638 brq->data.blocks = mqrq->packed_blocks + 1;
1639 brq->data.flags |= MMC_DATA_WRITE;
1640
1641 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1642 brq->stop.arg = 0;
1643 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1644
1645 mmc_set_data_timeout(&brq->data, card);
1646
1647 brq->data.sg = mqrq->sg;
1648 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1649
1650 mqrq->mmc_active.mrq = &brq->mrq;
1651 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1652
1653 mmc_queue_bounce_pre(mqrq);
1654}
1655
Adrian Hunter67716322011-08-29 16:42:15 +03001656static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1657 struct mmc_blk_request *brq, struct request *req,
1658 int ret)
1659{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001660 struct mmc_queue_req *mq_rq;
1661 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1662
Adrian Hunter67716322011-08-29 16:42:15 +03001663 /*
1664 * If this is an SD card and we're writing, we can first
1665 * mark the known good sectors as ok.
1666 *
1667 * If the card is not SD, we can still ok written sectors
1668 * as reported by the controller (which might be less than
1669 * the real number of written sectors, but never more).
1670 */
1671 if (mmc_card_sd(card)) {
1672 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301673 if (!brq->data.fault_injected) {
1674 blocks = mmc_sd_num_wr_blocks(card);
1675 if (blocks != (u32)-1)
1676 ret = blk_end_request(req, 0, blocks << 9);
1677 } else
1678 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001679 } else {
1680 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1681 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1682 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001683 return ret;
1684}
1685
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001686static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1687{
1688 struct request *prq;
1689 int idx = mq_rq->packed_fail_idx, i = 0;
1690 int ret = 0;
1691
1692 while (!list_empty(&mq_rq->packed_list)) {
1693 prq = list_entry_rq(mq_rq->packed_list.next);
1694 if (idx == i) {
1695 /* retry from error index */
1696 mq_rq->packed_num -= idx;
1697 mq_rq->req = prq;
1698 ret = 1;
1699
1700 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1701 list_del_init(&prq->queuelist);
1702 mmc_blk_clear_packed(mq_rq);
1703 }
1704 return ret;
1705 }
1706 list_del_init(&prq->queuelist);
1707 blk_end_request(prq, 0, blk_rq_bytes(prq));
1708 i++;
1709 }
1710
1711 mmc_blk_clear_packed(mq_rq);
1712 return ret;
1713}
1714static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1715{
1716 struct request *prq;
1717
1718 while (!list_empty(&mq_rq->packed_list)) {
1719 prq = list_entry_rq(mq_rq->packed_list.next);
1720 list_del_init(&prq->queuelist);
1721 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1722 }
1723
1724 mmc_blk_clear_packed(mq_rq);
1725}
1726
1727static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1728 struct mmc_queue_req *mq_rq)
1729{
1730 struct request *prq;
1731 struct request_queue *q = mq->queue;
1732
1733 while (!list_empty(&mq_rq->packed_list)) {
1734 prq = list_entry_rq(mq_rq->packed_list.prev);
1735 if (prq->queuelist.prev != &mq_rq->packed_list) {
1736 list_del_init(&prq->queuelist);
1737 spin_lock_irq(q->queue_lock);
1738 blk_requeue_request(mq->queue, prq);
1739 spin_unlock_irq(q->queue_lock);
1740 } else {
1741 list_del_init(&prq->queuelist);
1742 }
1743 }
1744
1745 mmc_blk_clear_packed(mq_rq);
1746}
1747
Per Forlinee8a43a2011-07-01 18:55:33 +02001748static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001749{
1750 struct mmc_blk_data *md = mq->data;
1751 struct mmc_card *card = md->queue.card;
1752 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001753 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001754 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001755 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001756 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001757 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001758 const u8 packed_num = 2;
1759 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001760
1761 if (!rqc && !mq->mqrq_prev->req)
1762 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001763
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001764 if (rqc)
1765 reqs = mmc_blk_prep_packed_list(mq, rqc);
1766
Per Forlin54d49d772011-07-01 18:55:29 +02001767 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001768 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001769 if (reqs >= packed_num)
1770 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1771 card, mq);
1772 else
1773 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001774 areq = &mq->mqrq_cur->mmc_active;
1775 } else
1776 areq = NULL;
1777 areq = mmc_start_req(card->host, areq, (int *) &status);
1778 if (!areq)
1779 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001780
Per Forlinee8a43a2011-07-01 18:55:33 +02001781 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1782 brq = &mq_rq->brq;
1783 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001784 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001785 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001786
Per Forlind78d4a82011-07-01 18:55:30 +02001787 switch (status) {
1788 case MMC_BLK_SUCCESS:
1789 case MMC_BLK_PARTIAL:
1790 /*
1791 * A block was successfully transferred.
1792 */
Adrian Hunter67716322011-08-29 16:42:15 +03001793 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001794
1795 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1796 ret = mmc_blk_end_packed_req(mq_rq);
1797 break;
1798 } else {
1799 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001800 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001801 }
1802
Adrian Hunter67716322011-08-29 16:42:15 +03001803 /*
1804 * If the blk_end_request function returns non-zero even
1805 * though all data has been transferred and no errors
1806 * were returned by the host controller, it's a bug.
1807 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001808 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301809 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001810 __func__, blk_rq_bytes(req),
1811 brq->data.bytes_xfered);
1812 rqc = NULL;
1813 goto cmd_abort;
1814 }
Per Forlind78d4a82011-07-01 18:55:30 +02001815 break;
1816 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001817 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1818 if (!mmc_blk_reset(md, card->host, type))
1819 break;
1820 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001821 case MMC_BLK_RETRY:
1822 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001823 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001824 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001825 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001826 if (!mmc_blk_reset(md, card->host, type))
1827 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001828 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001829 case MMC_BLK_DATA_ERR: {
1830 int err;
1831
1832 err = mmc_blk_reset(md, card->host, type);
1833 if (!err)
1834 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001835 if (err == -ENODEV ||
1836 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001837 goto cmd_abort;
1838 /* Fall through */
1839 }
1840 case MMC_BLK_ECC_ERR:
1841 if (brq->data.blocks > 1) {
1842 /* Redo read one sector at a time */
1843 pr_warning("%s: retrying using single block read\n",
1844 req->rq_disk->disk_name);
1845 disable_multi = 1;
1846 break;
1847 }
Per Forlind78d4a82011-07-01 18:55:30 +02001848 /*
1849 * After an error, we redo I/O one sector at a
1850 * time, so we only reach here after trying to
1851 * read a single sector.
1852 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301853 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001854 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001855 if (!ret)
1856 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001857 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301858 case MMC_BLK_NOMEDIUM:
1859 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001860 }
1861
Per Forlinee8a43a2011-07-01 18:55:33 +02001862 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001863 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1864 /*
1865 * In case of a incomplete request
1866 * prepare it again and resend.
1867 */
1868 mmc_blk_rw_rq_prep(mq_rq, card,
1869 disable_multi, mq);
1870 mmc_start_req(card->host,
1871 &mq_rq->mmc_active, NULL);
1872 } else {
1873 if (!mq_rq->packed_retries)
1874 goto cmd_abort;
1875 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1876 mmc_start_req(card->host,
1877 &mq_rq->mmc_active, NULL);
1878 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 } while (ret);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 1;
1883
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001884 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001885 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1886 if (mmc_card_removed(card))
1887 req->cmd_flags |= REQ_QUIET;
1888 while (ret)
1889 ret = blk_end_request(req, -EIO,
1890 blk_rq_cur_bytes(req));
1891 } else {
1892 mmc_blk_abort_packed_req(mq_rq);
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Per Forlinee8a43a2011-07-01 18:55:33 +02001895 start_new_req:
1896 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001897 /*
1898 * If current request is packed, it needs to put back.
1899 */
1900 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1901 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1902
Per Forlinee8a43a2011-07-01 18:55:33 +02001903 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1904 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1905 }
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
1908}
1909
Adrian Hunterbd788c92010-08-11 14:17:47 -07001910static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1911{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001912 int ret;
1913 struct mmc_blk_data *md = mq->data;
1914 struct mmc_card *card = md->queue.card;
1915
San Mehat148c57a2009-07-30 08:21:19 -07001916#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1917 if (mmc_bus_needs_resume(card->host)) {
1918 mmc_resume_bus(card->host);
1919 mmc_blk_set_blksize(md, card);
1920 }
1921#endif
1922
Per Forlinee8a43a2011-07-01 18:55:33 +02001923 if (req && !mq->mqrq_prev->req)
1924 /* claim host only for the first request */
1925 mmc_claim_host(card->host);
1926
Andrei Warkentin371a6892011-04-11 18:10:25 -05001927 ret = mmc_blk_part_switch(card, md);
1928 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001929 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301930 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001931 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001932 ret = 0;
1933 goto out;
1934 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001935
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001936 mmc_blk_write_packing_control(mq, req);
1937
Maya Erez463bb952012-05-24 23:46:29 +03001938 if (req && req->cmd_flags & REQ_SANITIZE) {
1939 /* complete ongoing async transfer before issuing sanitize */
1940 if (card->host && card->host->areq)
1941 mmc_blk_issue_rw_rq(mq, NULL);
1942 ret = mmc_blk_issue_sanitize_rq(mq, req);
1943 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001944 /* complete ongoing async transfer before issuing discard */
1945 if (card->host->areq)
1946 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001947 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001948 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001949 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001950 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001951 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001952 /* complete ongoing async transfer before issuing flush */
1953 if (card->host->areq)
1954 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001955 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001956 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001957 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001958 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001959
Andrei Warkentin371a6892011-04-11 18:10:25 -05001960out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001961 if (!req)
1962 /* release host only when there are no more requests */
1963 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001964 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Russell Kinga6f6c962006-01-03 22:38:44 +00001967static inline int mmc_blk_readonly(struct mmc_card *card)
1968{
1969 return mmc_card_readonly(card) ||
1970 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1971}
1972
Andrei Warkentin371a6892011-04-11 18:10:25 -05001973static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1974 struct device *parent,
1975 sector_t size,
1976 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001977 const char *subname,
1978 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct mmc_blk_data *md;
1981 int devidx, ret;
1982
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001983 devidx = find_first_zero_bit(dev_use, max_devices);
1984 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return ERR_PTR(-ENOSPC);
1986 __set_bit(devidx, dev_use);
1987
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001988 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001989 if (!md) {
1990 ret = -ENOMEM;
1991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001993
Russell Kinga6f6c962006-01-03 22:38:44 +00001994 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001995 * !subname implies we are creating main mmc_blk_data that will be
1996 * associated with mmc_card with mmc_set_drvdata. Due to device
1997 * partitions, devidx will not coincide with a per-physical card
1998 * index anymore so we keep track of a name index.
1999 */
2000 if (!subname) {
2001 md->name_idx = find_first_zero_bit(name_use, max_devices);
2002 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002003 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002004 md->name_idx = ((struct mmc_blk_data *)
2005 dev_to_disk(parent)->private_data)->name_idx;
2006
Johan Rudholmadd710e2011-12-02 08:51:06 +01002007 md->area_type = area_type;
2008
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002009 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002010 * Set the read-only status based on the supported commands
2011 * and the write protect switch.
2012 */
2013 md->read_only = mmc_blk_readonly(card);
2014
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002015 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002016 if (md->disk == NULL) {
2017 ret = -ENOMEM;
2018 goto err_kfree;
2019 }
2020
2021 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002022 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002023 md->usage = 1;
2024
Adrian Hunterd09408a2011-06-23 13:40:28 +03002025 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002026 if (ret)
2027 goto err_putdisk;
2028
Russell Kinga6f6c962006-01-03 22:38:44 +00002029 md->queue.issue_fn = mmc_blk_issue_rq;
2030 md->queue.data = md;
2031
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002032 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002033 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002034 md->disk->fops = &mmc_bdops;
2035 md->disk->private_data = md;
2036 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002037 md->disk->driverfs_dev = parent;
2038 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002039 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002040
2041 /*
2042 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2043 *
2044 * - be set for removable media with permanent block devices
2045 * - be unset for removable block devices with permanent media
2046 *
2047 * Since MMC block devices clearly fall under the second
2048 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2049 * should use the block device creation/destruction hotplug
2050 * messages to tell when the card is present.
2051 */
2052
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002053 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2054 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002055
Martin K. Petersene1defc42009-05-22 17:17:49 -04002056 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002057 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002058
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002059 if (mmc_host_cmd23(card->host)) {
2060 if (mmc_card_mmc(card) ||
2061 (mmc_card_sd(card) &&
2062 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2063 md->flags |= MMC_BLK_CMD23;
2064 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002065
2066 if (mmc_card_mmc(card) &&
2067 md->flags & MMC_BLK_CMD23 &&
2068 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2069 card->ext_csd.rel_sectors)) {
2070 md->flags |= MMC_BLK_REL_WR;
2071 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2072 }
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002075
2076 err_putdisk:
2077 put_disk(md->disk);
2078 err_kfree:
2079 kfree(md);
2080 out:
2081 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
Andrei Warkentin371a6892011-04-11 18:10:25 -05002084static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2085{
2086 sector_t size;
2087 struct mmc_blk_data *md;
2088
2089 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2090 /*
2091 * The EXT_CSD sector count is in number or 512 byte
2092 * sectors.
2093 */
2094 size = card->ext_csd.sectors;
2095 } else {
2096 /*
2097 * The CSD capacity field is in units of read_blkbits.
2098 * set_capacity takes units of 512 bytes.
2099 */
2100 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2101 }
2102
Johan Rudholmadd710e2011-12-02 08:51:06 +01002103 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2104 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002105 return md;
2106}
2107
2108static int mmc_blk_alloc_part(struct mmc_card *card,
2109 struct mmc_blk_data *md,
2110 unsigned int part_type,
2111 sector_t size,
2112 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002113 const char *subname,
2114 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002115{
2116 char cap_str[10];
2117 struct mmc_blk_data *part_md;
2118
2119 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002120 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002121 if (IS_ERR(part_md))
2122 return PTR_ERR(part_md);
2123 part_md->part_type = part_type;
2124 list_add(&part_md->part, &md->part);
2125
2126 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2127 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302128 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002129 part_md->disk->disk_name, mmc_card_id(card),
2130 mmc_card_name(card), part_md->part_type, cap_str);
2131 return 0;
2132}
2133
Namjae Jeone0c368d2011-10-06 23:41:38 +09002134/* MMC Physical partitions consist of two boot partitions and
2135 * up to four general purpose partitions.
2136 * For each partition enabled in EXT_CSD a block device will be allocatedi
2137 * to provide access to the partition.
2138 */
2139
Andrei Warkentin371a6892011-04-11 18:10:25 -05002140static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2141{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002142 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002143
2144 if (!mmc_card_mmc(card))
2145 return 0;
2146
Namjae Jeone0c368d2011-10-06 23:41:38 +09002147 for (idx = 0; idx < card->nr_parts; idx++) {
2148 if (card->part[idx].size) {
2149 ret = mmc_blk_alloc_part(card, md,
2150 card->part[idx].part_cfg,
2151 card->part[idx].size >> 9,
2152 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002153 card->part[idx].name,
2154 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002155 if (ret)
2156 return ret;
2157 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002158 }
2159
2160 return ret;
2161}
2162
Andrei Warkentin371a6892011-04-11 18:10:25 -05002163static void mmc_blk_remove_req(struct mmc_blk_data *md)
2164{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002165 struct mmc_card *card;
2166
Andrei Warkentin371a6892011-04-11 18:10:25 -05002167 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002168 card = md->queue.card;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002169 device_remove_file(disk_to_dev(md->disk),
2170 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171 if (md->disk->flags & GENHD_FL_UP) {
2172 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002173 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2174 card->ext_csd.boot_ro_lockable)
2175 device_remove_file(disk_to_dev(md->disk),
2176 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177
2178 /* Stop new requests from getting into the queue */
2179 del_gendisk(md->disk);
2180 }
2181
2182 /* Then flush out any already in there */
2183 mmc_cleanup_queue(&md->queue);
2184 mmc_blk_put(md);
2185 }
2186}
2187
2188static void mmc_blk_remove_parts(struct mmc_card *card,
2189 struct mmc_blk_data *md)
2190{
2191 struct list_head *pos, *q;
2192 struct mmc_blk_data *part_md;
2193
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002194 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002195 list_for_each_safe(pos, q, &md->part) {
2196 part_md = list_entry(pos, struct mmc_blk_data, part);
2197 list_del(pos);
2198 mmc_blk_remove_req(part_md);
2199 }
2200}
2201
2202static int mmc_add_disk(struct mmc_blk_data *md)
2203{
2204 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002205 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206
2207 add_disk(md->disk);
2208 md->force_ro.show = force_ro_show;
2209 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302210 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002211 md->force_ro.attr.name = "force_ro";
2212 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2213 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2214 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002215 goto force_ro_fail;
2216
2217 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2218 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002219 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002220
2221 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2222 mode = S_IRUGO;
2223 else
2224 mode = S_IRUGO | S_IWUSR;
2225
2226 md->power_ro_lock.show = power_ro_lock_show;
2227 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002228 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002229 md->power_ro_lock.attr.mode = mode;
2230 md->power_ro_lock.attr.name =
2231 "ro_lock_until_next_power_on";
2232 ret = device_create_file(disk_to_dev(md->disk),
2233 &md->power_ro_lock);
2234 if (ret)
2235 goto power_ro_lock_fail;
2236 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002237
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002238 md->num_wr_reqs_to_start_packing.show =
2239 num_wr_reqs_to_start_packing_show;
2240 md->num_wr_reqs_to_start_packing.store =
2241 num_wr_reqs_to_start_packing_store;
2242 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2243 md->num_wr_reqs_to_start_packing.attr.name =
2244 "num_wr_reqs_to_start_packing";
2245 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2246 ret = device_create_file(disk_to_dev(md->disk),
2247 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002248 if (ret)
2249 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002250
Johan Rudholmadd710e2011-12-02 08:51:06 +01002251 return ret;
2252
2253power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002254 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002255force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002256 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002257
2258 return ret;
2259}
2260
Chris Ballc59d4472011-11-11 22:01:43 -05002261#define CID_MANFID_SANDISK 0x2
2262#define CID_MANFID_TOSHIBA 0x11
2263#define CID_MANFID_MICRON 0x13
2264
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002265static const struct mmc_fixup blk_fixups[] =
2266{
Chris Ballc59d4472011-11-11 22:01:43 -05002267 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2268 MMC_QUIRK_INAND_CMD38),
2269 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2270 MMC_QUIRK_INAND_CMD38),
2271 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2272 MMC_QUIRK_INAND_CMD38),
2273 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2274 MMC_QUIRK_INAND_CMD38),
2275 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2276 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002277
2278 /*
2279 * Some MMC cards experience performance degradation with CMD23
2280 * instead of CMD12-bounded multiblock transfers. For now we'll
2281 * black list what's bad...
2282 * - Certain Toshiba cards.
2283 *
2284 * N.B. This doesn't affect SD cards.
2285 */
Chris Ballc59d4472011-11-11 22:01:43 -05002286 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002287 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002288 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002289 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002290 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002291 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002292
2293 /*
2294 * Some Micron MMC cards needs longer data read timeout than
2295 * indicated in CSD.
2296 */
Chris Ballc59d4472011-11-11 22:01:43 -05002297 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002298 MMC_QUIRK_LONG_READ_TIME),
2299
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302300 /* Some INAND MCP devices advertise incorrect timeout values */
2301 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2302 MMC_QUIRK_INAND_DATA_TIMEOUT),
2303
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002304 END_FIXUP
2305};
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307static int mmc_blk_probe(struct mmc_card *card)
2308{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002310 char cap_str[10];
2311
Pierre Ossman912490d2005-05-21 10:27:02 +01002312 /*
2313 * Check that the card supports the command class(es) we need.
2314 */
2315 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return -ENODEV;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 md = mmc_blk_alloc(card);
2319 if (IS_ERR(md))
2320 return PTR_ERR(md);
2321
Yi Li444122f2009-02-05 15:31:57 +08002322 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002323 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302324 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002326 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Andrei Warkentin371a6892011-04-11 18:10:25 -05002328 if (mmc_blk_alloc_parts(card, md))
2329 goto out;
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002332 mmc_fixup_device(card, blk_fixups);
2333
San Mehat148c57a2009-07-30 08:21:19 -07002334#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2335 mmc_set_bus_resume_policy(card->host, 1);
2336#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002337 if (mmc_add_disk(md))
2338 goto out;
2339
2340 list_for_each_entry(part_md, &md->part, part) {
2341 if (mmc_add_disk(part_md))
2342 goto out;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345
2346 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002347 mmc_blk_remove_parts(card, md);
2348 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
2352static void mmc_blk_remove(struct mmc_card *card)
2353{
2354 struct mmc_blk_data *md = mmc_get_drvdata(card);
2355
Andrei Warkentin371a6892011-04-11 18:10:25 -05002356 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002357 mmc_claim_host(card->host);
2358 mmc_blk_part_switch(card, md);
2359 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002360 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002362#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2363 mmc_set_bus_resume_policy(card->host, 0);
2364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002368static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002370 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct mmc_blk_data *md = mmc_get_drvdata(card);
2372
2373 if (md) {
2374 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002375 list_for_each_entry(part_md, &md->part, part) {
2376 mmc_queue_suspend(&part_md->queue);
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 return 0;
2380}
2381
2382static int mmc_blk_resume(struct mmc_card *card)
2383{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002384 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct mmc_blk_data *md = mmc_get_drvdata(card);
2386
2387 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388 /*
2389 * Resume involves the card going into idle state,
2390 * so current partition is always the main one.
2391 */
2392 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002394 list_for_each_entry(part_md, &md->part, part) {
2395 mmc_queue_resume(&part_md->queue);
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 return 0;
2399}
2400#else
2401#define mmc_blk_suspend NULL
2402#define mmc_blk_resume NULL
2403#endif
2404
2405static struct mmc_driver mmc_driver = {
2406 .drv = {
2407 .name = "mmcblk",
2408 },
2409 .probe = mmc_blk_probe,
2410 .remove = mmc_blk_remove,
2411 .suspend = mmc_blk_suspend,
2412 .resume = mmc_blk_resume,
2413};
2414
2415static int __init mmc_blk_init(void)
2416{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002417 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002419 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2420 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2421
2422 max_devices = 256 / perdev_minors;
2423
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002424 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2425 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002428 res = mmc_register_driver(&mmc_driver);
2429 if (res)
2430 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002432 return 0;
2433 out2:
2434 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 out:
2436 return res;
2437}
2438
2439static void __exit mmc_blk_exit(void)
2440{
2441 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002442 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445module_init(mmc_blk_init);
2446module_exit(mmc_blk_exit);
2447
2448MODULE_LICENSE("GPL");
2449MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2450