blob: 2558c960afdf55cd7db5a83b650103a4b73505f6 [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 */
Seungwon Jeon968c7742012-05-31 11:54:47 +030062#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
63 (req->cmd_flags & REQ_META)) && \
64 (rq_data_dir(req) == WRITE))
65#define PACKED_CMD_VER 0x01
66#define PACKED_CMD_WR 0x02
Maya Ereze544d7002012-06-04 06:09:47 +030067#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
68 do { \
69 if (stats->enabled) \
70 stats->pack_stop_reason[reason]++; \
71 } while (0)
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030072
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020073static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040074
75/*
76 * The defaults come from config options but can be overriden by module
77 * or bootarg options.
78 */
79static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80
81/*
82 * We've only got one major, so number of mmcblk devices is
83 * limited to 256 / number of minors per device.
84 */
85static int max_devices;
86
87/* 256 minors, so at most 256 separate devices */
88static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050089static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * There is one mmc_blk_data per slot.
93 */
94struct mmc_blk_data {
95 spinlock_t lock;
96 struct gendisk *disk;
97 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050098 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500100 unsigned int flags;
101#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
102#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000105 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500106 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500107 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300108 unsigned int reset_done;
109#define MMC_BLK_READ BIT(0)
110#define MMC_BLK_WRITE BIT(1)
111#define MMC_BLK_DISCARD BIT(2)
112#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500113
114 /*
115 * Only set in main mmc_blk_data associated
116 * with mmc_card with mmc_set_drvdata, and keeps
117 * track of the current selected device partition.
118 */
119 unsigned int part_curr;
120 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100121 struct device_attribute power_ro_lock;
122 int area_type;
Maya Erez63c61d62012-05-31 21:00:18 +0300123 struct device_attribute num_wr_reqs_to_start_packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Arjan van de Vena621aae2006-01-12 18:43:35 +0000126static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Per Forlind78d4a82011-07-01 18:55:30 +0200128enum mmc_blk_status {
129 MMC_BLK_SUCCESS = 0,
130 MMC_BLK_PARTIAL,
Per Forlind78d4a82011-07-01 18:55:30 +0200131 MMC_BLK_CMD_ERR,
Adrian Hunter67716322011-08-29 16:42:15 +0300132 MMC_BLK_RETRY,
Per Forlind78d4a82011-07-01 18:55:30 +0200133 MMC_BLK_ABORT,
Adrian Hunter67716322011-08-29 16:42:15 +0300134 MMC_BLK_DATA_ERR,
135 MMC_BLK_ECC_ERR,
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530136 MMC_BLK_NOMEDIUM,
Per Forlind78d4a82011-07-01 18:55:30 +0200137};
138
Seungwon Jeon968c7742012-05-31 11:54:47 +0300139enum {
140 MMC_PACKED_N_IDX = -1,
141 MMC_PACKED_N_ZERO,
142 MMC_PACKED_N_SINGLE,
143};
144
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400145module_param(perdev_minors, int, 0444);
146MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
147
Seungwon Jeon968c7742012-05-31 11:54:47 +0300148static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
149{
150 mqrq->packed_cmd = MMC_PACKED_NONE;
151 mqrq->packed_num = MMC_PACKED_N_ZERO;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
155{
156 struct mmc_blk_data *md;
157
Arjan van de Vena621aae2006-01-12 18:43:35 +0000158 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 md = disk->private_data;
160 if (md && md->usage == 0)
161 md = NULL;
162 if (md)
163 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000164 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return md;
167}
168
Andrei Warkentin371a6892011-04-11 18:10:25 -0500169static inline int mmc_get_devidx(struct gendisk *disk)
170{
Colin Cross71b54d42010-09-03 12:41:21 -0700171 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500172 return devidx;
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static void mmc_blk_put(struct mmc_blk_data *md)
176{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000177 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 md->usage--;
179 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500180 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800181 blk_cleanup_queue(md->queue.queue);
182
David Woodhouse1dff3142007-11-21 18:45:12 +0100183 __clear_bit(devidx, dev_use);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(md);
187 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000188 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Johan Rudholmadd710e2011-12-02 08:51:06 +0100191static ssize_t power_ro_lock_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 int ret;
195 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
196 struct mmc_card *card = md->queue.card;
197 int locked = 0;
198
199 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
200 locked = 2;
201 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
202 locked = 1;
203
204 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
205
206 return ret;
207}
208
209static ssize_t power_ro_lock_store(struct device *dev,
210 struct device_attribute *attr, const char *buf, size_t count)
211{
212 int ret;
213 struct mmc_blk_data *md, *part_md;
214 struct mmc_card *card;
215 unsigned long set;
216
217 if (kstrtoul(buf, 0, &set))
218 return -EINVAL;
219
220 if (set != 1)
221 return count;
222
223 md = mmc_blk_get(dev_to_disk(dev));
224 card = md->queue.card;
225
226 mmc_claim_host(card->host);
227
228 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
229 card->ext_csd.boot_ro_lock |
230 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
231 card->ext_csd.part_time);
232 if (ret)
233 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
234 else
235 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
236
237 mmc_release_host(card->host);
238
239 if (!ret) {
240 pr_info("%s: Locking boot partition ro until next power on\n",
241 md->disk->disk_name);
242 set_disk_ro(md->disk, 1);
243
244 list_for_each_entry(part_md, &md->part, part)
245 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
246 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
247 set_disk_ro(part_md->disk, 1);
248 }
249 }
250
251 mmc_blk_put(md);
252 return count;
253}
254
Andrei Warkentin371a6892011-04-11 18:10:25 -0500255static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
256 char *buf)
257{
258 int ret;
259 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
260
261 ret = snprintf(buf, PAGE_SIZE, "%d",
262 get_disk_ro(dev_to_disk(dev)) ^
263 md->read_only);
264 mmc_blk_put(md);
265 return ret;
266}
267
268static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
269 const char *buf, size_t count)
270{
271 int ret;
272 char *end;
273 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
274 unsigned long set = simple_strtoul(buf, &end, 0);
275 if (end == buf) {
276 ret = -EINVAL;
277 goto out;
278 }
279
280 set_disk_ro(dev_to_disk(dev), set || md->read_only);
281 ret = count;
282out:
283 mmc_blk_put(md);
284 return ret;
285}
286
Maya Erez63c61d62012-05-31 21:00:18 +0300287static ssize_t
288num_wr_reqs_to_start_packing_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
291 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
292 int num_wr_reqs_to_start_packing;
293 int ret;
294
295 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
296
297 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
298
299 mmc_blk_put(md);
300 return ret;
301}
302
303static ssize_t
304num_wr_reqs_to_start_packing_store(struct device *dev,
305 struct device_attribute *attr,
306 const char *buf, size_t count)
307{
308 int value;
309 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
310
311 sscanf(buf, "%d", &value);
312 if (value >= 0)
313 md->queue.num_wr_reqs_to_start_packing = value;
314
315 mmc_blk_put(md);
316 return count;
317}
318
Al Viroa5a15612008-03-02 10:33:30 -0500319static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Al Viroa5a15612008-03-02 10:33:30 -0500321 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int ret = -ENXIO;
323
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200324 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (md) {
326 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500327 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700329
Al Viroa5a15612008-03-02 10:33:30 -0500330 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700331 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700332 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200335 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return ret;
338}
339
Al Viroa5a15612008-03-02 10:33:30 -0500340static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Al Viroa5a15612008-03-02 10:33:30 -0500342 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200344 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200346 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
350static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800351mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800353 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
354 geo->heads = 4;
355 geo->sectors = 16;
356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
John Calixtocb87ea22011-04-26 18:56:29 -0400359struct mmc_blk_ioc_data {
360 struct mmc_ioc_cmd ic;
361 unsigned char *buf;
362 u64 buf_bytes;
363};
364
365static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
366 struct mmc_ioc_cmd __user *user)
367{
368 struct mmc_blk_ioc_data *idata;
369 int err;
370
371 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
372 if (!idata) {
373 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400374 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400375 }
376
377 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
378 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400379 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400380 }
381
382 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
383 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
384 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400385 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400386 }
387
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100388 if (!idata->buf_bytes)
389 return idata;
390
John Calixtocb87ea22011-04-26 18:56:29 -0400391 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
392 if (!idata->buf) {
393 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400394 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400395 }
396
397 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
398 idata->ic.data_ptr, idata->buf_bytes)) {
399 err = -EFAULT;
400 goto copy_err;
401 }
402
403 return idata;
404
405copy_err:
406 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400407idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400408 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400409out:
John Calixtocb87ea22011-04-26 18:56:29 -0400410 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400411}
412
413static int mmc_blk_ioctl_cmd(struct block_device *bdev,
414 struct mmc_ioc_cmd __user *ic_ptr)
415{
416 struct mmc_blk_ioc_data *idata;
417 struct mmc_blk_data *md;
418 struct mmc_card *card;
419 struct mmc_command cmd = {0};
420 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530421 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400422 struct scatterlist sg;
423 int err;
424
425 /*
426 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
427 * whole block device, not on a partition. This prevents overspray
428 * between sibling partitions.
429 */
430 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
431 return -EPERM;
432
433 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
434 if (IS_ERR(idata))
435 return PTR_ERR(idata);
436
John Calixtocb87ea22011-04-26 18:56:29 -0400437 md = mmc_blk_get(bdev->bd_disk);
438 if (!md) {
439 err = -EINVAL;
440 goto cmd_done;
441 }
442
443 card = md->queue.card;
444 if (IS_ERR(card)) {
445 err = PTR_ERR(card);
446 goto cmd_done;
447 }
448
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100449 cmd.opcode = idata->ic.opcode;
450 cmd.arg = idata->ic.arg;
451 cmd.flags = idata->ic.flags;
452
453 if (idata->buf_bytes) {
454 data.sg = &sg;
455 data.sg_len = 1;
456 data.blksz = idata->ic.blksz;
457 data.blocks = idata->ic.blocks;
458
459 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
460
461 if (idata->ic.write_flag)
462 data.flags = MMC_DATA_WRITE;
463 else
464 data.flags = MMC_DATA_READ;
465
466 /* data.flags must already be set before doing this. */
467 mmc_set_data_timeout(&data, card);
468
469 /* Allow overriding the timeout_ns for empirical tuning. */
470 if (idata->ic.data_timeout_ns)
471 data.timeout_ns = idata->ic.data_timeout_ns;
472
473 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
474 /*
475 * Pretend this is a data transfer and rely on the
476 * host driver to compute timeout. When all host
477 * drivers support cmd.cmd_timeout for R1B, this
478 * can be changed to:
479 *
480 * mrq.data = NULL;
481 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
482 */
483 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
484 }
485
486 mrq.data = &data;
487 }
488
489 mrq.cmd = &cmd;
490
John Calixtocb87ea22011-04-26 18:56:29 -0400491 mmc_claim_host(card->host);
492
493 if (idata->ic.is_acmd) {
494 err = mmc_app_cmd(card->host, card);
495 if (err)
496 goto cmd_rel_host;
497 }
498
John Calixtocb87ea22011-04-26 18:56:29 -0400499 mmc_wait_for_req(card->host, &mrq);
500
501 if (cmd.error) {
502 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
503 __func__, cmd.error);
504 err = cmd.error;
505 goto cmd_rel_host;
506 }
507 if (data.error) {
508 dev_err(mmc_dev(card->host), "%s: data error %d\n",
509 __func__, data.error);
510 err = data.error;
511 goto cmd_rel_host;
512 }
513
514 /*
515 * According to the SD specs, some commands require a delay after
516 * issuing the command.
517 */
518 if (idata->ic.postsleep_min_us)
519 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
520
521 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
522 err = -EFAULT;
523 goto cmd_rel_host;
524 }
525
526 if (!idata->ic.write_flag) {
527 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
528 idata->buf, idata->buf_bytes)) {
529 err = -EFAULT;
530 goto cmd_rel_host;
531 }
532 }
533
534cmd_rel_host:
535 mmc_release_host(card->host);
536
537cmd_done:
538 mmc_blk_put(md);
539 kfree(idata->buf);
540 kfree(idata);
541 return err;
542}
543
544static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
545 unsigned int cmd, unsigned long arg)
546{
547 int ret = -EINVAL;
548 if (cmd == MMC_IOC_CMD)
549 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
550 return ret;
551}
552
553#ifdef CONFIG_COMPAT
554static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
555 unsigned int cmd, unsigned long arg)
556{
557 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
558}
559#endif
560
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700561static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500562 .open = mmc_blk_open,
563 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800564 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400566 .ioctl = mmc_blk_ioctl,
567#ifdef CONFIG_COMPAT
568 .compat_ioctl = mmc_blk_compat_ioctl,
569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570};
571
Andrei Warkentin371a6892011-04-11 18:10:25 -0500572static inline int mmc_blk_part_switch(struct mmc_card *card,
573 struct mmc_blk_data *md)
574{
575 int ret;
576 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300577
Andrei Warkentin371a6892011-04-11 18:10:25 -0500578 if (main_md->part_curr == md->part_type)
579 return 0;
580
581 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300582 u8 part_config = card->ext_csd.part_config;
583
584 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
585 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500586
587 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300588 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500589 card->ext_csd.part_time);
590 if (ret)
591 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300592
593 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300594 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500595
596 main_md->part_curr = md->part_type;
597 return 0;
598}
599
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700600static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
601{
602 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100603 u32 result;
604 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700605
Venkatraman Sad5fd972011-08-25 00:30:50 +0530606 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400607 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400608 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700609
610 struct scatterlist sg;
611
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700612 cmd.opcode = MMC_APP_CMD;
613 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700614 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700615
616 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700617 if (err)
618 return (u32)-1;
619 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700620 return (u32)-1;
621
622 memset(&cmd, 0, sizeof(struct mmc_command));
623
624 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
625 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700626 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700627
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700628 data.blksz = 4;
629 data.blocks = 1;
630 data.flags = MMC_DATA_READ;
631 data.sg = &sg;
632 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530633 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700634
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700635 mrq.cmd = &cmd;
636 mrq.data = &data;
637
Ben Dooks051913d2009-06-08 23:33:57 +0100638 blocks = kmalloc(4, GFP_KERNEL);
639 if (!blocks)
640 return (u32)-1;
641
642 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700643
644 mmc_wait_for_req(card->host, &mrq);
645
Ben Dooks051913d2009-06-08 23:33:57 +0100646 result = ntohl(*blocks);
647 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700648
Ben Dooks051913d2009-06-08 23:33:57 +0100649 if (cmd.error || data.error)
650 result = (u32)-1;
651
652 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700653}
654
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100655static int send_stop(struct mmc_card *card, u32 *status)
656{
657 struct mmc_command cmd = {0};
658 int err;
659
660 cmd.opcode = MMC_STOP_TRANSMISSION;
661 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
662 err = mmc_wait_for_cmd(card->host, &cmd, 5);
663 if (err == 0)
664 *status = cmd.resp[0];
665 return err;
666}
667
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100668static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300669{
Chris Ball1278dba2011-04-13 23:40:30 -0400670 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300671 int err;
672
Adrian Hunter504f1912008-10-16 12:55:25 +0300673 cmd.opcode = MMC_SEND_STATUS;
674 if (!mmc_host_is_spi(card->host))
675 cmd.arg = card->rca << 16;
676 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100677 err = mmc_wait_for_cmd(card->host, &cmd, retries);
678 if (err == 0)
679 *status = cmd.resp[0];
680 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300681}
682
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530683#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100684#define ERR_RETRY 2
685#define ERR_ABORT 1
686#define ERR_CONTINUE 0
687
688static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
689 bool status_valid, u32 status)
690{
691 switch (error) {
692 case -EILSEQ:
693 /* response crc error, retry the r/w cmd */
694 pr_err("%s: %s sending %s command, card status %#x\n",
695 req->rq_disk->disk_name, "response CRC error",
696 name, status);
697 return ERR_RETRY;
698
699 case -ETIMEDOUT:
700 pr_err("%s: %s sending %s command, card status %#x\n",
701 req->rq_disk->disk_name, "timed out", name, status);
702
703 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700704 if (!status_valid) {
705 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100706 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700707 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100708 /*
709 * If it was a r/w cmd crc error, or illegal command
710 * (eg, issued in wrong state) then retry - we should
711 * have corrected the state problem above.
712 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700713 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
714 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100715 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700716 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100717
718 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700719 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100720 return ERR_ABORT;
721
722 default:
723 /* We don't understand the error code the driver gave us */
724 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
725 req->rq_disk->disk_name, error, status);
726 return ERR_ABORT;
727 }
728}
729
730/*
731 * Initial r/w and stop cmd error recovery.
732 * We don't know whether the card received the r/w cmd or not, so try to
733 * restore things back to a sane state. Essentially, we do this as follows:
734 * - Obtain card status. If the first attempt to obtain card status fails,
735 * the status word will reflect the failed status cmd, not the failed
736 * r/w cmd. If we fail to obtain card status, it suggests we can no
737 * longer communicate with the card.
738 * - Check the card state. If the card received the cmd but there was a
739 * transient problem with the response, it might still be in a data transfer
740 * mode. Try to send it a stop command. If this fails, we can't recover.
741 * - If the r/w cmd failed due to a response CRC error, it was probably
742 * transient, so retry the cmd.
743 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
744 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
745 * illegal cmd, retry.
746 * Otherwise we don't understand what happened, so abort.
747 */
748static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300749 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100750{
751 bool prev_cmd_status_valid = true;
752 u32 status, stop_status = 0;
753 int err, retry;
754
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530755 if (mmc_card_removed(card))
756 return ERR_NOMEDIUM;
757
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100758 /*
759 * Try to get card status which indicates both the card state
760 * and why there was no response. If the first attempt fails,
761 * we can't be sure the returned status is for the r/w command.
762 */
763 for (retry = 2; retry >= 0; retry--) {
764 err = get_card_status(card, &status, 0);
765 if (!err)
766 break;
767
768 prev_cmd_status_valid = false;
769 pr_err("%s: error %d sending status command, %sing\n",
770 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
771 }
772
773 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530774 if (err) {
775 /* Check if the card is removed */
776 if (mmc_detect_card_removed(card->host))
777 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100778 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530779 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100780
Adrian Hunter67716322011-08-29 16:42:15 +0300781 /* Flag ECC errors */
782 if ((status & R1_CARD_ECC_FAILED) ||
783 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
784 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
785 *ecc_err = 1;
786
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100787 /*
788 * Check the current card state. If it is in some data transfer
789 * mode, tell it to stop (and hopefully transition back to TRAN.)
790 */
791 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
792 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
793 err = send_stop(card, &stop_status);
794 if (err)
795 pr_err("%s: error %d sending stop command\n",
796 req->rq_disk->disk_name, err);
797
798 /*
799 * If the stop cmd also timed out, the card is probably
800 * not present, so abort. Other errors are bad news too.
801 */
802 if (err)
803 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300804 if (stop_status & R1_CARD_ECC_FAILED)
805 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100806 }
807
808 /* Check for set block count errors */
809 if (brq->sbc.error)
810 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
811 prev_cmd_status_valid, status);
812
813 /* Check for r/w command errors */
814 if (brq->cmd.error)
815 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
816 prev_cmd_status_valid, status);
817
Adrian Hunter67716322011-08-29 16:42:15 +0300818 /* Data errors */
819 if (!brq->stop.error)
820 return ERR_CONTINUE;
821
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100822 /* Now for stop errors. These aren't fatal to the transfer. */
823 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
824 req->rq_disk->disk_name, brq->stop.error,
825 brq->cmd.resp[0], status);
826
827 /*
828 * Subsitute in our own stop status as this will give the error
829 * state which happened during the execution of the r/w command.
830 */
831 if (stop_status) {
832 brq->stop.resp[0] = stop_status;
833 brq->stop.error = 0;
834 }
835 return ERR_CONTINUE;
836}
837
Adrian Hunter67716322011-08-29 16:42:15 +0300838static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
839 int type)
840{
841 int err;
842
843 if (md->reset_done & type)
844 return -EEXIST;
845
846 md->reset_done |= type;
847 err = mmc_hw_reset(host);
848 /* Ensure we switch back to the correct partition */
849 if (err != -EOPNOTSUPP) {
850 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
851 int part_err;
852
853 main_md->part_curr = main_md->part_type;
854 part_err = mmc_blk_part_switch(host->card, md);
855 if (part_err) {
856 /*
857 * We have failed to get back into the correct
858 * partition, so we need to abort the whole request.
859 */
860 return -ENODEV;
861 }
862 }
863 return err;
864}
865
866static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
867{
868 md->reset_done &= ~type;
869}
870
Adrian Hunterbd788c92010-08-11 14:17:47 -0700871static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
872{
873 struct mmc_blk_data *md = mq->data;
874 struct mmc_card *card = md->queue.card;
875 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300876 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700877
Adrian Hunterbd788c92010-08-11 14:17:47 -0700878 if (!mmc_can_erase(card)) {
879 err = -EOPNOTSUPP;
880 goto out;
881 }
882
883 from = blk_rq_pos(req);
884 nr = blk_rq_sectors(req);
885
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900886 if (mmc_can_discard(card))
887 arg = MMC_DISCARD_ARG;
888 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700889 arg = MMC_TRIM_ARG;
890 else
891 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300892retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500893 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
894 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
895 INAND_CMD38_ARG_EXT_CSD,
896 arg == MMC_TRIM_ARG ?
897 INAND_CMD38_ARG_TRIM :
898 INAND_CMD38_ARG_ERASE,
899 0);
900 if (err)
901 goto out;
902 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700903 err = mmc_erase(card, from, nr, arg);
904out:
Adrian Hunter67716322011-08-29 16:42:15 +0300905 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
906 goto retry;
907 if (!err)
908 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530909 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700910
Adrian Hunterbd788c92010-08-11 14:17:47 -0700911 return err ? 0 : 1;
912}
913
Adrian Hunter49804542010-08-11 14:17:50 -0700914static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
915 struct request *req)
916{
917 struct mmc_blk_data *md = mq->data;
918 struct mmc_card *card = md->queue.card;
919 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300920 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700921
Maya Erez463bb952012-05-24 23:46:29 +0300922 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700923 err = -EOPNOTSUPP;
924 goto out;
925 }
926
927 from = blk_rq_pos(req);
928 nr = blk_rq_sectors(req);
929
930 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
931 arg = MMC_SECURE_TRIM1_ARG;
932 else
933 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300934retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500935 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
936 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
937 INAND_CMD38_ARG_EXT_CSD,
938 arg == MMC_SECURE_TRIM1_ARG ?
939 INAND_CMD38_ARG_SECTRIM1 :
940 INAND_CMD38_ARG_SECERASE,
941 0);
942 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300943 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500944 }
Adrian Hunter28302812012-04-05 14:45:48 +0300945
Adrian Hunter49804542010-08-11 14:17:50 -0700946 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300947 if (err == -EIO)
948 goto out_retry;
949 if (err)
950 goto out;
951
952 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500953 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
954 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
955 INAND_CMD38_ARG_EXT_CSD,
956 INAND_CMD38_ARG_SECTRIM2,
957 0);
958 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300959 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500960 }
Adrian Hunter28302812012-04-05 14:45:48 +0300961
Adrian Hunter49804542010-08-11 14:17:50 -0700962 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300963 if (err == -EIO)
964 goto out_retry;
965 if (err)
966 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500967 }
Adrian Hunter28302812012-04-05 14:45:48 +0300968
969 if (mmc_can_sanitize(card))
970 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
971 EXT_CSD_SANITIZE_START, 1, 0);
972out_retry:
973 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300974 goto retry;
975 if (!err)
976 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300977out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530978 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700979
Adrian Hunter49804542010-08-11 14:17:50 -0700980 return err ? 0 : 1;
981}
982
Maya Erez463bb952012-05-24 23:46:29 +0300983static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
984 struct request *req)
985{
986 struct mmc_blk_data *md = mq->data;
987 struct mmc_card *card = md->queue.card;
988 int err = 0;
989
990 BUG_ON(!card);
991 BUG_ON(!card->host);
992
993 if (!(mmc_can_sanitize(card) &&
994 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
995 pr_warning("%s: %s - SANITIZE is not supported\n",
996 mmc_hostname(card->host), __func__);
997 err = -EOPNOTSUPP;
998 goto out;
999 }
1000
1001 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1002 mmc_hostname(card->host), __func__);
1003
1004 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001005 EXT_CSD_SANITIZE_START, 1,
1006 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001007
1008 if (err)
1009 pr_err("%s: %s - mmc_switch() with "
1010 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1011 mmc_hostname(card->host), __func__, err);
1012
1013 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1014 __func__);
1015
1016out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301017 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001018
1019 return err ? 0 : 1;
1020}
1021
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001022static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1023{
1024 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001025 struct mmc_card *card = md->queue.card;
1026 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001027
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001028 ret = mmc_flush_cache(card);
1029 if (ret)
1030 ret = -EIO;
1031
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301032 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001033
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001034 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001035}
1036
1037/*
1038 * Reformat current write as a reliable write, supporting
1039 * both legacy and the enhanced reliable write MMC cards.
1040 * In each transfer we'll handle only as much as a single
1041 * reliable write can handle, thus finish the request in
1042 * partial completions.
1043 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001044static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1045 struct mmc_card *card,
1046 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001047{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001048 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1049 /* Legacy mode imposes restrictions on transfers. */
1050 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1051 brq->data.blocks = 1;
1052
1053 if (brq->data.blocks > card->ext_csd.rel_sectors)
1054 brq->data.blocks = card->ext_csd.rel_sectors;
1055 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1056 brq->data.blocks = 1;
1057 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001058}
1059
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001060#define CMD_ERRORS \
1061 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1062 R1_ADDRESS_ERROR | /* Misaligned address */ \
1063 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1064 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1065 R1_CC_ERROR | /* Card controller error */ \
1066 R1_ERROR) /* General/unknown error */
1067
Per Forlinee8a43a2011-07-01 18:55:33 +02001068static int mmc_blk_err_check(struct mmc_card *card,
1069 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001070{
Per Forlinee8a43a2011-07-01 18:55:33 +02001071 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1072 mmc_active);
1073 struct mmc_blk_request *brq = &mq_mrq->brq;
1074 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001075 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001076
1077 /*
1078 * sbc.error indicates a problem with the set block count
1079 * command. No data will have been transferred.
1080 *
1081 * cmd.error indicates a problem with the r/w command. No
1082 * data will have been transferred.
1083 *
1084 * stop.error indicates a problem with the stop command. Data
1085 * may have been transferred, or may still be transferring.
1086 */
Adrian Hunter67716322011-08-29 16:42:15 +03001087 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1088 brq->data.error) {
1089 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001090 case ERR_RETRY:
1091 return MMC_BLK_RETRY;
1092 case ERR_ABORT:
1093 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301094 case ERR_NOMEDIUM:
1095 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001096 case ERR_CONTINUE:
1097 break;
1098 }
1099 }
1100
1101 /*
1102 * Check for errors relating to the execution of the
1103 * initial command - such as address errors. No data
1104 * has been transferred.
1105 */
1106 if (brq->cmd.resp[0] & CMD_ERRORS) {
1107 pr_err("%s: r/w command failed, status = %#x\n",
1108 req->rq_disk->disk_name, brq->cmd.resp[0]);
1109 return MMC_BLK_ABORT;
1110 }
1111
1112 /*
1113 * Everything else is either success, or a data error of some
1114 * kind. If it was a write, we may have transitioned to
1115 * program mode, which we have to wait for it to complete.
1116 */
1117 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1118 u32 status;
1119 do {
1120 int err = get_card_status(card, &status, 5);
1121 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301122 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001123 req->rq_disk->disk_name, err);
1124 return MMC_BLK_CMD_ERR;
1125 }
1126 /*
1127 * Some cards mishandle the status bits,
1128 * so make sure to check both the busy
1129 * indication and the card state.
1130 */
1131 } while (!(status & R1_READY_FOR_DATA) ||
1132 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1133 }
1134
1135 if (brq->data.error) {
1136 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1137 req->rq_disk->disk_name, brq->data.error,
1138 (unsigned)blk_rq_pos(req),
1139 (unsigned)blk_rq_sectors(req),
1140 brq->cmd.resp[0], brq->stop.resp[0]);
1141
1142 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001143 if (ecc_err)
1144 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001145 return MMC_BLK_DATA_ERR;
1146 } else {
1147 return MMC_BLK_CMD_ERR;
1148 }
1149 }
1150
Adrian Hunter67716322011-08-29 16:42:15 +03001151 if (!brq->data.bytes_xfered)
1152 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001153
Seungwon Jeon968c7742012-05-31 11:54:47 +03001154 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1155 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1156 return MMC_BLK_PARTIAL;
1157 else
1158 return MMC_BLK_SUCCESS;
1159 }
1160
Adrian Hunter67716322011-08-29 16:42:15 +03001161 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1162 return MMC_BLK_PARTIAL;
1163
1164 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001165}
1166
Seungwon Jeon968c7742012-05-31 11:54:47 +03001167static int mmc_blk_packed_err_check(struct mmc_card *card,
1168 struct mmc_async_req *areq)
1169{
1170 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1171 mmc_active);
1172 struct request *req = mq_rq->req;
1173 int err, check, status;
1174 u8 ext_csd[512];
1175
1176 check = mmc_blk_err_check(card, areq);
1177 err = get_card_status(card, &status, 0);
1178 if (err) {
1179 pr_err("%s: error %d sending status command\n",
1180 req->rq_disk->disk_name, err);
1181 return MMC_BLK_ABORT;
1182 }
1183
1184 if (status & R1_EXP_EVENT) {
1185 err = mmc_send_ext_csd(card, ext_csd);
1186 if (err) {
1187 pr_err("%s: error %d sending ext_csd\n",
1188 req->rq_disk->disk_name, err);
1189 return MMC_BLK_ABORT;
1190 }
1191
1192 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1193 EXT_CSD_PACKED_FAILURE) &&
1194 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1195 EXT_CSD_PACKED_GENERIC_ERROR)) {
1196 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1197 EXT_CSD_PACKED_INDEXED_ERROR) {
1198 mq_rq->packed_fail_idx =
1199 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1200 return MMC_BLK_PARTIAL;
1201 }
1202 }
1203 }
1204
1205 return check;
1206}
1207
Per Forlin54d49d772011-07-01 18:55:29 +02001208static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1209 struct mmc_card *card,
1210 int disable_multi,
1211 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Per Forlin54d49d772011-07-01 18:55:29 +02001213 u32 readcmd, writecmd;
1214 struct mmc_blk_request *brq = &mqrq->brq;
1215 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301217 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001219 /*
1220 * Reliable writes are used to implement Forced Unit Access and
1221 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001222 *
1223 * XXX: this really needs a good explanation of why REQ_META
1224 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001225 */
1226 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1227 (req->cmd_flags & REQ_META)) &&
1228 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001229 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001230
Per Forlin54d49d772011-07-01 18:55:29 +02001231 memset(brq, 0, sizeof(struct mmc_blk_request));
1232 brq->mrq.cmd = &brq->cmd;
1233 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Per Forlin54d49d772011-07-01 18:55:29 +02001235 brq->cmd.arg = blk_rq_pos(req);
1236 if (!mmc_card_blockaddr(card))
1237 brq->cmd.arg <<= 9;
1238 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1239 brq->data.blksz = 512;
1240 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1241 brq->stop.arg = 0;
1242 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1243 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Per Forlin54d49d772011-07-01 18:55:29 +02001245 /*
1246 * The block layer doesn't support all sector count
1247 * restrictions, so we need to be prepared for too big
1248 * requests.
1249 */
1250 if (brq->data.blocks > card->host->max_blk_count)
1251 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001253 if (brq->data.blocks > 1) {
1254 /*
1255 * After a read error, we redo the request one sector
1256 * at a time in order to accurately determine which
1257 * sectors can be read successfully.
1258 */
1259 if (disable_multi)
1260 brq->data.blocks = 1;
1261
1262 /* Some controllers can't do multiblock reads due to hw bugs */
1263 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1264 rq_data_dir(req) == READ)
1265 brq->data.blocks = 1;
1266 }
Per Forlin54d49d772011-07-01 18:55:29 +02001267
1268 if (brq->data.blocks > 1 || do_rel_wr) {
1269 /* SPI multiblock writes terminate using a special
1270 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001271 */
Per Forlin54d49d772011-07-01 18:55:29 +02001272 if (!mmc_host_is_spi(card->host) ||
1273 rq_data_dir(req) == READ)
1274 brq->mrq.stop = &brq->stop;
1275 readcmd = MMC_READ_MULTIPLE_BLOCK;
1276 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1277 } else {
1278 brq->mrq.stop = NULL;
1279 readcmd = MMC_READ_SINGLE_BLOCK;
1280 writecmd = MMC_WRITE_BLOCK;
1281 }
1282 if (rq_data_dir(req) == READ) {
1283 brq->cmd.opcode = readcmd;
1284 brq->data.flags |= MMC_DATA_READ;
1285 } else {
1286 brq->cmd.opcode = writecmd;
1287 brq->data.flags |= MMC_DATA_WRITE;
1288 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001289
Per Forlin54d49d772011-07-01 18:55:29 +02001290 if (do_rel_wr)
1291 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001292
Per Forlin54d49d772011-07-01 18:55:29 +02001293 /*
Saugata Das42659002011-12-21 13:09:17 +05301294 * Data tag is used only during writing meta data to speed
1295 * up write and any subsequent read of this meta data
1296 */
1297 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1298 (req->cmd_flags & REQ_META) &&
1299 (rq_data_dir(req) == WRITE) &&
1300 ((brq->data.blocks * brq->data.blksz) >=
1301 card->ext_csd.data_tag_unit_size);
1302
1303 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001304 * Pre-defined multi-block transfers are preferable to
1305 * open ended-ones (and necessary for reliable writes).
1306 * However, it is not sufficient to just send CMD23,
1307 * and avoid the final CMD12, as on an error condition
1308 * CMD12 (stop) needs to be sent anyway. This, coupled
1309 * with Auto-CMD23 enhancements provided by some
1310 * hosts, means that the complexity of dealing
1311 * with this is best left to the host. If CMD23 is
1312 * supported by card and host, we'll fill sbc in and let
1313 * the host deal with handling it correctly. This means
1314 * that for hosts that don't expose MMC_CAP_CMD23, no
1315 * change of behavior will be observed.
1316 *
1317 * N.B: Some MMC cards experience perf degradation.
1318 * We'll avoid using CMD23-bounded multiblock writes for
1319 * these, while retaining features like reliable writes.
1320 */
Saugata Das42659002011-12-21 13:09:17 +05301321 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1322 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1323 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001324 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1325 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301326 (do_rel_wr ? (1 << 31) : 0) |
1327 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001328 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1329 brq->mrq.sbc = &brq->sbc;
1330 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001331
Per Forlin54d49d772011-07-01 18:55:29 +02001332 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001333
Per Forlin54d49d772011-07-01 18:55:29 +02001334 brq->data.sg = mqrq->sg;
1335 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001336
Per Forlin54d49d772011-07-01 18:55:29 +02001337 /*
1338 * Adjust the sg list so it is the same size as the
1339 * request.
1340 */
1341 if (brq->data.blocks != blk_rq_sectors(req)) {
1342 int i, data_size = brq->data.blocks << 9;
1343 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001344
Per Forlin54d49d772011-07-01 18:55:29 +02001345 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1346 data_size -= sg->length;
1347 if (data_size <= 0) {
1348 sg->length += data_size;
1349 i++;
1350 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001351 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001352 }
Per Forlin54d49d772011-07-01 18:55:29 +02001353 brq->data.sg_len = i;
1354 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001355
Per Forlinee8a43a2011-07-01 18:55:33 +02001356 mqrq->mmc_active.mrq = &brq->mrq;
1357 mqrq->mmc_active.err_check = mmc_blk_err_check;
1358
Per Forlin54d49d772011-07-01 18:55:29 +02001359 mmc_queue_bounce_pre(mqrq);
1360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Maya Erez63c61d62012-05-31 21:00:18 +03001362static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1363 struct request *req)
1364{
1365 struct mmc_host *host = mq->card->host;
1366 int data_dir;
1367
1368 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1369 return;
1370
1371 /*
1372 * In case the packing control is not supported by the host, it should
1373 * not have an effect on the write packing. Therefore we have to enable
1374 * the write packing
1375 */
1376 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1377 mq->wr_packing_enabled = true;
1378 return;
1379 }
1380
1381 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1382 if (mq->num_of_potential_packed_wr_reqs >
1383 mq->num_wr_reqs_to_start_packing)
1384 mq->wr_packing_enabled = true;
1385 return;
1386 }
1387
1388 data_dir = rq_data_dir(req);
1389
1390 if (data_dir == READ) {
1391 mq->num_of_potential_packed_wr_reqs = 0;
1392 mq->wr_packing_enabled = false;
1393 return;
1394 } else if (data_dir == WRITE) {
1395 mq->num_of_potential_packed_wr_reqs++;
1396 }
1397
1398 if (mq->num_of_potential_packed_wr_reqs >
1399 mq->num_wr_reqs_to_start_packing)
1400 mq->wr_packing_enabled = true;
1401
1402}
1403
Maya Ereze544d7002012-06-04 06:09:47 +03001404struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1405{
1406 if (!card)
1407 return NULL;
1408
1409 return &card->wr_pack_stats;
1410}
1411EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1412
1413void mmc_blk_init_packed_statistics(struct mmc_card *card)
1414{
1415 int max_num_of_packed_reqs = 0;
1416
1417 if (!card || !card->wr_pack_stats.packing_events)
1418 return;
1419
1420 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1421
1422 spin_lock(&card->wr_pack_stats.lock);
1423 memset(card->wr_pack_stats.packing_events, 0,
1424 (max_num_of_packed_reqs + 1) *
1425 sizeof(*card->wr_pack_stats.packing_events));
1426 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1427 sizeof(card->wr_pack_stats.pack_stop_reason));
1428 card->wr_pack_stats.enabled = true;
1429 spin_unlock(&card->wr_pack_stats.lock);
1430}
1431EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1432
Seungwon Jeon968c7742012-05-31 11:54:47 +03001433static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1434{
1435 struct request_queue *q = mq->queue;
1436 struct mmc_card *card = mq->card;
1437 struct request *cur = req, *next = NULL;
1438 struct mmc_blk_data *md = mq->data;
1439 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1440 unsigned int req_sectors = 0, phys_segments = 0;
1441 unsigned int max_blk_count, max_phys_segs;
1442 u8 put_back = 0;
1443 u8 max_packed_rw = 0;
1444 u8 reqs = 0;
Maya Ereze544d7002012-06-04 06:09:47 +03001445 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001446
1447 mmc_blk_clear_packed(mq->mqrq_cur);
1448
1449 if (!(md->flags & MMC_BLK_CMD23) ||
1450 !card->ext_csd.packed_event_en)
1451 goto no_packed;
1452
Maya Erez63c61d62012-05-31 21:00:18 +03001453 if (!mq->wr_packing_enabled)
1454 goto no_packed;
1455
Seungwon Jeon968c7742012-05-31 11:54:47 +03001456 if ((rq_data_dir(cur) == WRITE) &&
1457 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1458 max_packed_rw = card->ext_csd.max_packed_writes;
1459
1460 if (max_packed_rw == 0)
1461 goto no_packed;
1462
1463 if (mmc_req_rel_wr(cur) &&
1464 (md->flags & MMC_BLK_REL_WR) &&
1465 !en_rel_wr) {
1466 goto no_packed;
1467 }
1468
1469 max_blk_count = min(card->host->max_blk_count,
1470 card->host->max_req_size >> 9);
1471 if (unlikely(max_blk_count > 0xffff))
1472 max_blk_count = 0xffff;
1473
1474 max_phys_segs = queue_max_segments(q);
1475 req_sectors += blk_rq_sectors(cur);
1476 phys_segments += cur->nr_phys_segments;
1477
1478 if (rq_data_dir(cur) == WRITE) {
1479 req_sectors++;
1480 phys_segments++;
1481 }
1482
Maya Ereze544d7002012-06-04 06:09:47 +03001483 spin_lock(&stats->lock);
1484
Seungwon Jeon968c7742012-05-31 11:54:47 +03001485 while (reqs < max_packed_rw - 1) {
1486 spin_lock_irq(q->queue_lock);
1487 next = blk_fetch_request(q);
1488 spin_unlock_irq(q->queue_lock);
Maya Ereze544d7002012-06-04 06:09:47 +03001489 if (!next) {
1490 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001491 break;
Maya Ereze544d7002012-06-04 06:09:47 +03001492 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001493
1494 if (next->cmd_flags & REQ_DISCARD ||
1495 next->cmd_flags & REQ_FLUSH) {
Maya Ereze544d7002012-06-04 06:09:47 +03001496 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001497 put_back = 1;
1498 break;
1499 }
1500
1501 if (rq_data_dir(cur) != rq_data_dir(next)) {
Maya Ereze544d7002012-06-04 06:09:47 +03001502 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001503 put_back = 1;
1504 break;
1505 }
1506
1507 if (mmc_req_rel_wr(next) &&
1508 (md->flags & MMC_BLK_REL_WR) &&
1509 !en_rel_wr) {
Maya Ereze544d7002012-06-04 06:09:47 +03001510 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001511 put_back = 1;
1512 break;
1513 }
1514
1515 req_sectors += blk_rq_sectors(next);
1516 if (req_sectors > max_blk_count) {
Maya Ereze544d7002012-06-04 06:09:47 +03001517 if (stats->enabled)
1518 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001519 put_back = 1;
1520 break;
1521 }
1522
1523 phys_segments += next->nr_phys_segments;
1524 if (phys_segments > max_phys_segs) {
Maya Ereze544d7002012-06-04 06:09:47 +03001525 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001526 put_back = 1;
1527 break;
1528 }
1529
Maya Erez63c61d62012-05-31 21:00:18 +03001530 if (rq_data_dir(next) == WRITE)
1531 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001532 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1533 cur = next;
1534 reqs++;
1535 }
1536
1537 if (put_back) {
1538 spin_lock_irq(q->queue_lock);
1539 blk_requeue_request(q, next);
1540 spin_unlock_irq(q->queue_lock);
1541 }
1542
Maya Ereze544d7002012-06-04 06:09:47 +03001543 if (stats->enabled) {
1544 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1545 stats->packing_events[reqs + 1]++;
1546 if (reqs + 1 == max_packed_rw)
1547 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1548 }
1549
1550 spin_unlock(&stats->lock);
1551
Seungwon Jeon968c7742012-05-31 11:54:47 +03001552 if (reqs > 0) {
1553 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1554 mq->mqrq_cur->packed_num = ++reqs;
1555 return reqs;
1556 }
1557
1558no_packed:
1559 mmc_blk_clear_packed(mq->mqrq_cur);
1560 return 0;
1561}
1562
1563static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1564 struct mmc_card *card,
1565 struct mmc_queue *mq)
1566{
1567 struct mmc_blk_request *brq = &mqrq->brq;
1568 struct request *req = mqrq->req;
1569 struct request *prq;
1570 struct mmc_blk_data *md = mq->data;
1571 bool do_rel_wr;
1572 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1573 u8 i = 1;
1574
1575 mqrq->packed_cmd = MMC_PACKED_WRITE;
1576 mqrq->packed_blocks = 0;
1577 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1578
1579 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1580 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1581 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1582
1583 /*
1584 * Argument for each entry of packed group
1585 */
1586 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1587 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1588 /* Argument of CMD23*/
1589 packed_cmd_hdr[(i * 2)] =
1590 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1591 blk_rq_sectors(prq);
1592 /* Argument of CMD18 or CMD25 */
1593 packed_cmd_hdr[((i * 2)) + 1] =
1594 mmc_card_blockaddr(card) ?
1595 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1596 mqrq->packed_blocks += blk_rq_sectors(prq);
1597 i++;
1598 }
1599
1600 memset(brq, 0, sizeof(struct mmc_blk_request));
1601 brq->mrq.cmd = &brq->cmd;
1602 brq->mrq.data = &brq->data;
1603 brq->mrq.sbc = &brq->sbc;
1604 brq->mrq.stop = &brq->stop;
1605
1606 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1607 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1608 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1609
1610 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1611 brq->cmd.arg = blk_rq_pos(req);
1612 if (!mmc_card_blockaddr(card))
1613 brq->cmd.arg <<= 9;
1614 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1615
1616 brq->data.blksz = 512;
1617 brq->data.blocks = mqrq->packed_blocks + 1;
1618 brq->data.flags |= MMC_DATA_WRITE;
1619
1620 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1621 brq->stop.arg = 0;
1622 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1623
1624 mmc_set_data_timeout(&brq->data, card);
1625
1626 brq->data.sg = mqrq->sg;
1627 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1628
1629 mqrq->mmc_active.mrq = &brq->mrq;
1630 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1631
1632 mmc_queue_bounce_pre(mqrq);
1633}
1634
Adrian Hunter67716322011-08-29 16:42:15 +03001635static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1636 struct mmc_blk_request *brq, struct request *req,
1637 int ret)
1638{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001639 struct mmc_queue_req *mq_rq;
1640 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1641
Adrian Hunter67716322011-08-29 16:42:15 +03001642 /*
1643 * If this is an SD card and we're writing, we can first
1644 * mark the known good sectors as ok.
1645 *
1646 * If the card is not SD, we can still ok written sectors
1647 * as reported by the controller (which might be less than
1648 * the real number of written sectors, but never more).
1649 */
1650 if (mmc_card_sd(card)) {
1651 u32 blocks;
1652
1653 blocks = mmc_sd_num_wr_blocks(card);
1654 if (blocks != (u32)-1) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301655 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001656 }
1657 } else {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001658 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301659 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001660 }
Adrian Hunter67716322011-08-29 16:42:15 +03001661 }
1662 return ret;
1663}
1664
Seungwon Jeon968c7742012-05-31 11:54:47 +03001665static int mmc_blk_end_packed_req(struct mmc_queue *mq,
1666 struct mmc_queue_req *mq_rq)
1667{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001668 struct request *prq;
1669 int idx = mq_rq->packed_fail_idx, i = 0;
1670 int ret = 0;
1671
1672 while (!list_empty(&mq_rq->packed_list)) {
1673 prq = list_entry_rq(mq_rq->packed_list.next);
1674 if (idx == i) {
1675 /* retry from error index */
1676 mq_rq->packed_num -= idx;
1677 mq_rq->req = prq;
1678 ret = 1;
1679
1680 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1681 list_del_init(&prq->queuelist);
1682 mmc_blk_clear_packed(mq_rq);
1683 }
1684 return ret;
1685 }
1686 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301687 blk_end_request(prq, 0, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001688 i++;
1689 }
1690
1691 mmc_blk_clear_packed(mq_rq);
1692 return ret;
1693}
1694
Per Forlinee8a43a2011-07-01 18:55:33 +02001695static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001696{
1697 struct mmc_blk_data *md = mq->data;
1698 struct mmc_card *card = md->queue.card;
1699 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001700 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001701 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001702 struct mmc_queue_req *mq_rq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001703 struct request *req, *prq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001704 struct mmc_async_req *areq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001705 const u8 packed_num = 2;
1706 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001707
1708 if (!rqc && !mq->mqrq_prev->req)
1709 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001710
Seungwon Jeon968c7742012-05-31 11:54:47 +03001711 if (rqc)
1712 reqs = mmc_blk_prep_packed_list(mq, rqc);
1713
Per Forlin54d49d772011-07-01 18:55:29 +02001714 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001715 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001716 if (reqs >= packed_num)
1717 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1718 card, mq);
1719 else
1720 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001721 areq = &mq->mqrq_cur->mmc_active;
1722 } else
1723 areq = NULL;
1724 areq = mmc_start_req(card->host, areq, (int *) &status);
1725 if (!areq)
1726 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001727
Per Forlinee8a43a2011-07-01 18:55:33 +02001728 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1729 brq = &mq_rq->brq;
1730 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001731 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001732 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001733
Jaehoon Chungf886c802012-05-28 10:33:35 +03001734 /*
1735 * Check BKOPS urgency from each R1 response
1736 */
1737 if (mmc_card_mmc(card) &&
1738 (brq->cmd.resp[0] & R1_EXCEPTION_EVENT))
1739 mmc_card_set_check_bkops(card);
1740
Per Forlind78d4a82011-07-01 18:55:30 +02001741 switch (status) {
1742 case MMC_BLK_SUCCESS:
1743 case MMC_BLK_PARTIAL:
1744 /*
1745 * A block was successfully transferred.
1746 */
Adrian Hunter67716322011-08-29 16:42:15 +03001747 mmc_blk_reset_success(md, type);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001748
1749 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1750 ret = mmc_blk_end_packed_req(mq, mq_rq);
1751 break;
1752 } else {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301753 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001754 brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001755 }
1756
Adrian Hunter67716322011-08-29 16:42:15 +03001757 /*
1758 * If the blk_end_request function returns non-zero even
1759 * though all data has been transferred and no errors
1760 * were returned by the host controller, it's a bug.
1761 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001762 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301763 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001764 __func__, blk_rq_bytes(req),
1765 brq->data.bytes_xfered);
1766 rqc = NULL;
1767 goto cmd_abort;
1768 }
Per Forlind78d4a82011-07-01 18:55:30 +02001769 break;
1770 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001771 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1772 if (!mmc_blk_reset(md, card->host, type))
1773 break;
1774 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001775 case MMC_BLK_RETRY:
1776 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001777 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001778 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001779 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001780 if (!mmc_blk_reset(md, card->host, type))
1781 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001782 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001783 case MMC_BLK_DATA_ERR: {
1784 int err;
1785
1786 err = mmc_blk_reset(md, card->host, type);
1787 if (!err)
1788 break;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001789 if (err == -ENODEV ||
1790 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001791 goto cmd_abort;
1792 /* Fall through */
1793 }
1794 case MMC_BLK_ECC_ERR:
1795 if (brq->data.blocks > 1) {
1796 /* Redo read one sector at a time */
1797 pr_warning("%s: retrying using single block read\n",
1798 req->rq_disk->disk_name);
1799 disable_multi = 1;
1800 break;
1801 }
Per Forlind78d4a82011-07-01 18:55:30 +02001802 /*
1803 * After an error, we redo I/O one sector at a
1804 * time, so we only reach here after trying to
1805 * read a single sector.
1806 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301807 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001808 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001809 if (!ret)
1810 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001811 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301812 case MMC_BLK_NOMEDIUM:
1813 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001814 }
1815
Per Forlinee8a43a2011-07-01 18:55:33 +02001816 if (ret) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001817 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1818 /*
1819 * In case of a incomplete request
1820 * prepare it again and resend.
1821 */
1822 mmc_blk_rw_rq_prep(mq_rq, card,
1823 disable_multi, mq);
1824 mmc_start_req(card->host,
1825 &mq_rq->mmc_active, NULL);
1826 } else {
1827 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1828 mmc_start_req(card->host,
1829 &mq_rq->mmc_active, NULL);
1830 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 } while (ret);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 1;
1835
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001836 cmd_abort:
Seungwon Jeon968c7742012-05-31 11:54:47 +03001837 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001838 if (mmc_card_removed(card))
1839 req->cmd_flags |= REQ_QUIET;
1840 while (ret)
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301841 ret = blk_end_request(req, -EIO,
Seungwon Jeon968c7742012-05-31 11:54:47 +03001842 blk_rq_cur_bytes(req));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001843 } else {
1844 while (!list_empty(&mq_rq->packed_list)) {
1845 prq = list_entry_rq(mq_rq->packed_list.next);
1846 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301847 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001848 }
1849 mmc_blk_clear_packed(mq_rq);
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Per Forlinee8a43a2011-07-01 18:55:33 +02001852 start_new_req:
1853 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001854 /*
1855 * If current request is packed, it needs to put back.
1856 */
1857 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE) {
1858 while (!list_empty(&mq->mqrq_cur->packed_list)) {
1859 prq = list_entry_rq(
1860 mq->mqrq_cur->packed_list.prev);
1861 if (prq->queuelist.prev !=
1862 &mq->mqrq_cur->packed_list) {
1863 list_del_init(&prq->queuelist);
1864 spin_lock_irq(mq->queue->queue_lock);
1865 blk_requeue_request(mq->queue, prq);
1866 spin_unlock_irq(mq->queue->queue_lock);
1867 } else {
1868 list_del_init(&prq->queuelist);
1869 }
1870 }
1871 mmc_blk_clear_packed(mq->mqrq_cur);
1872 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001873 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1874 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
1878}
1879
Adrian Hunterbd788c92010-08-11 14:17:47 -07001880static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1881{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001882 int ret;
1883 struct mmc_blk_data *md = mq->data;
1884 struct mmc_card *card = md->queue.card;
1885
San Mehat148c57a2009-07-30 08:21:19 -07001886#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1887 if (mmc_bus_needs_resume(card->host)) {
1888 mmc_resume_bus(card->host);
1889 mmc_blk_set_blksize(md, card);
1890 }
1891#endif
1892
Per Forlinee8a43a2011-07-01 18:55:33 +02001893 if (req && !mq->mqrq_prev->req)
1894 /* claim host only for the first request */
1895 mmc_claim_host(card->host);
1896
Andrei Warkentin371a6892011-04-11 18:10:25 -05001897 ret = mmc_blk_part_switch(card, md);
1898 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001899 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301900 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001901 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001902 ret = 0;
1903 goto out;
1904 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001905
Maya Erez63c61d62012-05-31 21:00:18 +03001906 mmc_blk_write_packing_control(mq, req);
1907
Maya Erez463bb952012-05-24 23:46:29 +03001908 if (req && req->cmd_flags & REQ_SANITIZE) {
1909 /* complete ongoing async transfer before issuing sanitize */
1910 if (card->host && card->host->areq)
1911 mmc_blk_issue_rw_rq(mq, NULL);
1912 ret = mmc_blk_issue_sanitize_rq(mq, req);
1913 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001914 /* complete ongoing async transfer before issuing discard */
1915 if (card->host->areq)
1916 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001917 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001918 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001919 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001920 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001921 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001922 /* complete ongoing async transfer before issuing flush */
1923 if (card->host->areq)
1924 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001925 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001926 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001927 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001928 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001929
Andrei Warkentin371a6892011-04-11 18:10:25 -05001930out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001931 if (!req)
1932 /* release host only when there are no more requests */
1933 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001934 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001935}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Russell Kinga6f6c962006-01-03 22:38:44 +00001937static inline int mmc_blk_readonly(struct mmc_card *card)
1938{
1939 return mmc_card_readonly(card) ||
1940 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1941}
1942
Andrei Warkentin371a6892011-04-11 18:10:25 -05001943static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1944 struct device *parent,
1945 sector_t size,
1946 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001947 const char *subname,
1948 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct mmc_blk_data *md;
1951 int devidx, ret;
1952
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001953 devidx = find_first_zero_bit(dev_use, max_devices);
1954 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return ERR_PTR(-ENOSPC);
1956 __set_bit(devidx, dev_use);
1957
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001958 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001959 if (!md) {
1960 ret = -ENOMEM;
1961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001963
Russell Kinga6f6c962006-01-03 22:38:44 +00001964 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001965 * !subname implies we are creating main mmc_blk_data that will be
1966 * associated with mmc_card with mmc_set_drvdata. Due to device
1967 * partitions, devidx will not coincide with a per-physical card
1968 * index anymore so we keep track of a name index.
1969 */
1970 if (!subname) {
1971 md->name_idx = find_first_zero_bit(name_use, max_devices);
1972 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001973 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001974 md->name_idx = ((struct mmc_blk_data *)
1975 dev_to_disk(parent)->private_data)->name_idx;
1976
Johan Rudholmadd710e2011-12-02 08:51:06 +01001977 md->area_type = area_type;
1978
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001979 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001980 * Set the read-only status based on the supported commands
1981 * and the write protect switch.
1982 */
1983 md->read_only = mmc_blk_readonly(card);
1984
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001985 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001986 if (md->disk == NULL) {
1987 ret = -ENOMEM;
1988 goto err_kfree;
1989 }
1990
1991 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001992 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001993 md->usage = 1;
1994
Adrian Hunterd09408a2011-06-23 13:40:28 +03001995 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001996 if (ret)
1997 goto err_putdisk;
1998
Russell Kinga6f6c962006-01-03 22:38:44 +00001999 md->queue.issue_fn = mmc_blk_issue_rq;
2000 md->queue.data = md;
2001
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002002 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002003 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002004 md->disk->fops = &mmc_bdops;
2005 md->disk->private_data = md;
2006 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002007 md->disk->driverfs_dev = parent;
2008 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002009 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002010
2011 /*
2012 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2013 *
2014 * - be set for removable media with permanent block devices
2015 * - be unset for removable block devices with permanent media
2016 *
2017 * Since MMC block devices clearly fall under the second
2018 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2019 * should use the block device creation/destruction hotplug
2020 * messages to tell when the card is present.
2021 */
2022
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002023 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2024 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002025
Martin K. Petersene1defc42009-05-22 17:17:49 -04002026 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002027 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002028
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002029 if (mmc_host_cmd23(card->host)) {
2030 if (mmc_card_mmc(card) ||
2031 (mmc_card_sd(card) &&
2032 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2033 md->flags |= MMC_BLK_CMD23;
2034 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002035
2036 if (mmc_card_mmc(card) &&
2037 md->flags & MMC_BLK_CMD23 &&
2038 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2039 card->ext_csd.rel_sectors)) {
2040 md->flags |= MMC_BLK_REL_WR;
2041 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2042 }
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002045
2046 err_putdisk:
2047 put_disk(md->disk);
2048 err_kfree:
2049 kfree(md);
2050 out:
2051 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Andrei Warkentin371a6892011-04-11 18:10:25 -05002054static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2055{
2056 sector_t size;
2057 struct mmc_blk_data *md;
2058
2059 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2060 /*
2061 * The EXT_CSD sector count is in number or 512 byte
2062 * sectors.
2063 */
2064 size = card->ext_csd.sectors;
2065 } else {
2066 /*
2067 * The CSD capacity field is in units of read_blkbits.
2068 * set_capacity takes units of 512 bytes.
2069 */
2070 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2071 }
2072
Johan Rudholmadd710e2011-12-02 08:51:06 +01002073 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2074 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002075 return md;
2076}
2077
2078static int mmc_blk_alloc_part(struct mmc_card *card,
2079 struct mmc_blk_data *md,
2080 unsigned int part_type,
2081 sector_t size,
2082 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002083 const char *subname,
2084 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002085{
2086 char cap_str[10];
2087 struct mmc_blk_data *part_md;
2088
2089 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002090 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002091 if (IS_ERR(part_md))
2092 return PTR_ERR(part_md);
2093 part_md->part_type = part_type;
2094 list_add(&part_md->part, &md->part);
2095
2096 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2097 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302098 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002099 part_md->disk->disk_name, mmc_card_id(card),
2100 mmc_card_name(card), part_md->part_type, cap_str);
2101 return 0;
2102}
2103
Namjae Jeone0c368d2011-10-06 23:41:38 +09002104/* MMC Physical partitions consist of two boot partitions and
2105 * up to four general purpose partitions.
2106 * For each partition enabled in EXT_CSD a block device will be allocatedi
2107 * to provide access to the partition.
2108 */
2109
Andrei Warkentin371a6892011-04-11 18:10:25 -05002110static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2111{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002112 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002113
2114 if (!mmc_card_mmc(card))
2115 return 0;
2116
Namjae Jeone0c368d2011-10-06 23:41:38 +09002117 for (idx = 0; idx < card->nr_parts; idx++) {
2118 if (card->part[idx].size) {
2119 ret = mmc_blk_alloc_part(card, md,
2120 card->part[idx].part_cfg,
2121 card->part[idx].size >> 9,
2122 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002123 card->part[idx].name,
2124 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002125 if (ret)
2126 return ret;
2127 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002128 }
2129
2130 return ret;
2131}
2132
Andrei Warkentin371a6892011-04-11 18:10:25 -05002133static void mmc_blk_remove_req(struct mmc_blk_data *md)
2134{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002135 struct mmc_card *card;
2136
Andrei Warkentin371a6892011-04-11 18:10:25 -05002137 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002138 card = md->queue.card;
Maya Erez63c61d62012-05-31 21:00:18 +03002139 device_remove_file(disk_to_dev(md->disk),
2140 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002141 if (md->disk->flags & GENHD_FL_UP) {
2142 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002143 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2144 card->ext_csd.boot_ro_lockable)
2145 device_remove_file(disk_to_dev(md->disk),
2146 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002147
2148 /* Stop new requests from getting into the queue */
2149 del_gendisk(md->disk);
2150 }
2151
2152 /* Then flush out any already in there */
2153 mmc_cleanup_queue(&md->queue);
2154 mmc_blk_put(md);
2155 }
2156}
2157
2158static void mmc_blk_remove_parts(struct mmc_card *card,
2159 struct mmc_blk_data *md)
2160{
2161 struct list_head *pos, *q;
2162 struct mmc_blk_data *part_md;
2163
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002164 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165 list_for_each_safe(pos, q, &md->part) {
2166 part_md = list_entry(pos, struct mmc_blk_data, part);
2167 list_del(pos);
2168 mmc_blk_remove_req(part_md);
2169 }
2170}
2171
2172static int mmc_add_disk(struct mmc_blk_data *md)
2173{
2174 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002175 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002176
2177 add_disk(md->disk);
2178 md->force_ro.show = force_ro_show;
2179 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302180 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002181 md->force_ro.attr.name = "force_ro";
2182 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2183 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2184 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002185 goto force_ro_fail;
2186
2187 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2188 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002189 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002190
2191 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2192 mode = S_IRUGO;
2193 else
2194 mode = S_IRUGO | S_IWUSR;
2195
2196 md->power_ro_lock.show = power_ro_lock_show;
2197 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002198 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002199 md->power_ro_lock.attr.mode = mode;
2200 md->power_ro_lock.attr.name =
2201 "ro_lock_until_next_power_on";
2202 ret = device_create_file(disk_to_dev(md->disk),
2203 &md->power_ro_lock);
2204 if (ret)
2205 goto power_ro_lock_fail;
2206 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002207
Maya Erez63c61d62012-05-31 21:00:18 +03002208 md->num_wr_reqs_to_start_packing.show =
2209 num_wr_reqs_to_start_packing_show;
2210 md->num_wr_reqs_to_start_packing.store =
2211 num_wr_reqs_to_start_packing_store;
2212 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2213 md->num_wr_reqs_to_start_packing.attr.name =
2214 "num_wr_reqs_to_start_packing";
2215 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2216 ret = device_create_file(disk_to_dev(md->disk),
2217 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002218 if (ret)
2219 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002220
Johan Rudholmadd710e2011-12-02 08:51:06 +01002221 return ret;
2222
2223power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002224 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002225force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002226 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002227
2228 return ret;
2229}
2230
Chris Ballc59d4472011-11-11 22:01:43 -05002231#define CID_MANFID_SANDISK 0x2
2232#define CID_MANFID_TOSHIBA 0x11
2233#define CID_MANFID_MICRON 0x13
2234
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002235static const struct mmc_fixup blk_fixups[] =
2236{
Chris Ballc59d4472011-11-11 22:01:43 -05002237 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2238 MMC_QUIRK_INAND_CMD38),
2239 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2240 MMC_QUIRK_INAND_CMD38),
2241 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2242 MMC_QUIRK_INAND_CMD38),
2243 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2244 MMC_QUIRK_INAND_CMD38),
2245 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2246 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002247
2248 /*
2249 * Some MMC cards experience performance degradation with CMD23
2250 * instead of CMD12-bounded multiblock transfers. For now we'll
2251 * black list what's bad...
2252 * - Certain Toshiba cards.
2253 *
2254 * N.B. This doesn't affect SD cards.
2255 */
Chris Ballc59d4472011-11-11 22:01:43 -05002256 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002257 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002258 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002259 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002260 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002261 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002262
2263 /*
2264 * Some Micron MMC cards needs longer data read timeout than
2265 * indicated in CSD.
2266 */
Chris Ballc59d4472011-11-11 22:01:43 -05002267 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002268 MMC_QUIRK_LONG_READ_TIME),
2269
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302270 /* Some INAND MCP devices advertise incorrect timeout values */
2271 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2272 MMC_QUIRK_INAND_DATA_TIMEOUT),
2273
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002274 END_FIXUP
2275};
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277static int mmc_blk_probe(struct mmc_card *card)
2278{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002279 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002280 char cap_str[10];
2281
Pierre Ossman912490d2005-05-21 10:27:02 +01002282 /*
2283 * Check that the card supports the command class(es) we need.
2284 */
2285 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return -ENODEV;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 md = mmc_blk_alloc(card);
2289 if (IS_ERR(md))
2290 return PTR_ERR(md);
2291
Yi Li444122f2009-02-05 15:31:57 +08002292 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002293 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302294 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002296 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Andrei Warkentin371a6892011-04-11 18:10:25 -05002298 if (mmc_blk_alloc_parts(card, md))
2299 goto out;
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002302 mmc_fixup_device(card, blk_fixups);
2303
San Mehat148c57a2009-07-30 08:21:19 -07002304#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2305 mmc_set_bus_resume_policy(card->host, 1);
2306#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002307 if (mmc_add_disk(md))
2308 goto out;
2309
2310 list_for_each_entry(part_md, &md->part, part) {
2311 if (mmc_add_disk(part_md))
2312 goto out;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315
2316 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002317 mmc_blk_remove_parts(card, md);
2318 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
2322static void mmc_blk_remove(struct mmc_card *card)
2323{
2324 struct mmc_blk_data *md = mmc_get_drvdata(card);
2325
Andrei Warkentin371a6892011-04-11 18:10:25 -05002326 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002327 mmc_claim_host(card->host);
2328 mmc_blk_part_switch(card, md);
2329 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002330 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002332#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2333 mmc_set_bus_resume_policy(card->host, 0);
2334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
2337#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002338static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002340 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct mmc_blk_data *md = mmc_get_drvdata(card);
2342
2343 if (md) {
2344 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002345 list_for_each_entry(part_md, &md->part, part) {
2346 mmc_queue_suspend(&part_md->queue);
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 return 0;
2350}
2351
2352static int mmc_blk_resume(struct mmc_card *card)
2353{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002354 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct mmc_blk_data *md = mmc_get_drvdata(card);
2356
2357 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002358 /*
2359 * Resume involves the card going into idle state,
2360 * so current partition is always the main one.
2361 */
2362 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002364 list_for_each_entry(part_md, &md->part, part) {
2365 mmc_queue_resume(&part_md->queue);
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 return 0;
2369}
2370#else
2371#define mmc_blk_suspend NULL
2372#define mmc_blk_resume NULL
2373#endif
2374
2375static struct mmc_driver mmc_driver = {
2376 .drv = {
2377 .name = "mmcblk",
2378 },
2379 .probe = mmc_blk_probe,
2380 .remove = mmc_blk_remove,
2381 .suspend = mmc_blk_suspend,
2382 .resume = mmc_blk_resume,
2383};
2384
2385static int __init mmc_blk_init(void)
2386{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002387 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002389 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2390 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2391
2392 max_devices = 256 / perdev_minors;
2393
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002394 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2395 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002398 res = mmc_register_driver(&mmc_driver);
2399 if (res)
2400 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002402 return 0;
2403 out2:
2404 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 out:
2406 return res;
2407}
2408
2409static void __exit mmc_blk_exit(void)
2410{
2411 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002412 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415module_init(mmc_blk_init);
2416module_exit(mmc_blk_exit);
2417
2418MODULE_LICENSE("GPL");
2419MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2420