blob: 990faebf2e41dcb3399710bf175f143fbaa476e0 [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 unsigned int timeout_us;
610
611 struct scatterlist sg;
612
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700613 cmd.opcode = MMC_APP_CMD;
614 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700615 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700616
617 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700618 if (err)
619 return (u32)-1;
620 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700621 return (u32)-1;
622
623 memset(&cmd, 0, sizeof(struct mmc_command));
624
625 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
626 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700627 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700628
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700629 data.timeout_ns = card->csd.tacc_ns * 100;
630 data.timeout_clks = card->csd.tacc_clks * 100;
631
632 timeout_us = data.timeout_ns / 1000;
633 timeout_us += data.timeout_clks * 1000 /
634 (card->host->ios.clock / 1000);
635
636 if (timeout_us > 100000) {
637 data.timeout_ns = 100000000;
638 data.timeout_clks = 0;
639 }
640
641 data.blksz = 4;
642 data.blocks = 1;
643 data.flags = MMC_DATA_READ;
644 data.sg = &sg;
645 data.sg_len = 1;
646
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700647 mrq.cmd = &cmd;
648 mrq.data = &data;
649
Ben Dooks051913d2009-06-08 23:33:57 +0100650 blocks = kmalloc(4, GFP_KERNEL);
651 if (!blocks)
652 return (u32)-1;
653
654 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700655
656 mmc_wait_for_req(card->host, &mrq);
657
Ben Dooks051913d2009-06-08 23:33:57 +0100658 result = ntohl(*blocks);
659 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700660
Ben Dooks051913d2009-06-08 23:33:57 +0100661 if (cmd.error || data.error)
662 result = (u32)-1;
663
664 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700665}
666
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100667static int send_stop(struct mmc_card *card, u32 *status)
668{
669 struct mmc_command cmd = {0};
670 int err;
671
672 cmd.opcode = MMC_STOP_TRANSMISSION;
673 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
674 err = mmc_wait_for_cmd(card->host, &cmd, 5);
675 if (err == 0)
676 *status = cmd.resp[0];
677 return err;
678}
679
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100680static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300681{
Chris Ball1278dba2011-04-13 23:40:30 -0400682 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300683 int err;
684
Adrian Hunter504f1912008-10-16 12:55:25 +0300685 cmd.opcode = MMC_SEND_STATUS;
686 if (!mmc_host_is_spi(card->host))
687 cmd.arg = card->rca << 16;
688 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100689 err = mmc_wait_for_cmd(card->host, &cmd, retries);
690 if (err == 0)
691 *status = cmd.resp[0];
692 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300693}
694
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530695#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100696#define ERR_RETRY 2
697#define ERR_ABORT 1
698#define ERR_CONTINUE 0
699
700static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
701 bool status_valid, u32 status)
702{
703 switch (error) {
704 case -EILSEQ:
705 /* response crc error, retry the r/w cmd */
706 pr_err("%s: %s sending %s command, card status %#x\n",
707 req->rq_disk->disk_name, "response CRC error",
708 name, status);
709 return ERR_RETRY;
710
711 case -ETIMEDOUT:
712 pr_err("%s: %s sending %s command, card status %#x\n",
713 req->rq_disk->disk_name, "timed out", name, status);
714
715 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700716 if (!status_valid) {
717 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100718 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700719 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100720 /*
721 * If it was a r/w cmd crc error, or illegal command
722 * (eg, issued in wrong state) then retry - we should
723 * have corrected the state problem above.
724 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700725 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
726 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100727 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700728 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100729
730 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700731 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100732 return ERR_ABORT;
733
734 default:
735 /* We don't understand the error code the driver gave us */
736 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
737 req->rq_disk->disk_name, error, status);
738 return ERR_ABORT;
739 }
740}
741
742/*
743 * Initial r/w and stop cmd error recovery.
744 * We don't know whether the card received the r/w cmd or not, so try to
745 * restore things back to a sane state. Essentially, we do this as follows:
746 * - Obtain card status. If the first attempt to obtain card status fails,
747 * the status word will reflect the failed status cmd, not the failed
748 * r/w cmd. If we fail to obtain card status, it suggests we can no
749 * longer communicate with the card.
750 * - Check the card state. If the card received the cmd but there was a
751 * transient problem with the response, it might still be in a data transfer
752 * mode. Try to send it a stop command. If this fails, we can't recover.
753 * - If the r/w cmd failed due to a response CRC error, it was probably
754 * transient, so retry the cmd.
755 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
756 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
757 * illegal cmd, retry.
758 * Otherwise we don't understand what happened, so abort.
759 */
760static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300761 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100762{
763 bool prev_cmd_status_valid = true;
764 u32 status, stop_status = 0;
765 int err, retry;
766
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530767 if (mmc_card_removed(card))
768 return ERR_NOMEDIUM;
769
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100770 /*
771 * Try to get card status which indicates both the card state
772 * and why there was no response. If the first attempt fails,
773 * we can't be sure the returned status is for the r/w command.
774 */
775 for (retry = 2; retry >= 0; retry--) {
776 err = get_card_status(card, &status, 0);
777 if (!err)
778 break;
779
780 prev_cmd_status_valid = false;
781 pr_err("%s: error %d sending status command, %sing\n",
782 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
783 }
784
785 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530786 if (err) {
787 /* Check if the card is removed */
788 if (mmc_detect_card_removed(card->host))
789 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100790 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530791 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100792
Adrian Hunter67716322011-08-29 16:42:15 +0300793 /* Flag ECC errors */
794 if ((status & R1_CARD_ECC_FAILED) ||
795 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
796 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
797 *ecc_err = 1;
798
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100799 /*
800 * Check the current card state. If it is in some data transfer
801 * mode, tell it to stop (and hopefully transition back to TRAN.)
802 */
803 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
804 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
805 err = send_stop(card, &stop_status);
806 if (err)
807 pr_err("%s: error %d sending stop command\n",
808 req->rq_disk->disk_name, err);
809
810 /*
811 * If the stop cmd also timed out, the card is probably
812 * not present, so abort. Other errors are bad news too.
813 */
814 if (err)
815 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300816 if (stop_status & R1_CARD_ECC_FAILED)
817 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100818 }
819
820 /* Check for set block count errors */
821 if (brq->sbc.error)
822 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
823 prev_cmd_status_valid, status);
824
825 /* Check for r/w command errors */
826 if (brq->cmd.error)
827 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
828 prev_cmd_status_valid, status);
829
Adrian Hunter67716322011-08-29 16:42:15 +0300830 /* Data errors */
831 if (!brq->stop.error)
832 return ERR_CONTINUE;
833
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100834 /* Now for stop errors. These aren't fatal to the transfer. */
835 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
836 req->rq_disk->disk_name, brq->stop.error,
837 brq->cmd.resp[0], status);
838
839 /*
840 * Subsitute in our own stop status as this will give the error
841 * state which happened during the execution of the r/w command.
842 */
843 if (stop_status) {
844 brq->stop.resp[0] = stop_status;
845 brq->stop.error = 0;
846 }
847 return ERR_CONTINUE;
848}
849
Adrian Hunter67716322011-08-29 16:42:15 +0300850static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
851 int type)
852{
853 int err;
854
855 if (md->reset_done & type)
856 return -EEXIST;
857
858 md->reset_done |= type;
859 err = mmc_hw_reset(host);
860 /* Ensure we switch back to the correct partition */
861 if (err != -EOPNOTSUPP) {
862 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
863 int part_err;
864
865 main_md->part_curr = main_md->part_type;
866 part_err = mmc_blk_part_switch(host->card, md);
867 if (part_err) {
868 /*
869 * We have failed to get back into the correct
870 * partition, so we need to abort the whole request.
871 */
872 return -ENODEV;
873 }
874 }
875 return err;
876}
877
878static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
879{
880 md->reset_done &= ~type;
881}
882
Adrian Hunterbd788c92010-08-11 14:17:47 -0700883static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
884{
885 struct mmc_blk_data *md = mq->data;
886 struct mmc_card *card = md->queue.card;
887 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300888 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700889
Adrian Hunterbd788c92010-08-11 14:17:47 -0700890 if (!mmc_can_erase(card)) {
891 err = -EOPNOTSUPP;
892 goto out;
893 }
894
895 from = blk_rq_pos(req);
896 nr = blk_rq_sectors(req);
897
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900898 if (mmc_can_discard(card))
899 arg = MMC_DISCARD_ARG;
900 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700901 arg = MMC_TRIM_ARG;
902 else
903 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300904retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500905 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
906 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
907 INAND_CMD38_ARG_EXT_CSD,
908 arg == MMC_TRIM_ARG ?
909 INAND_CMD38_ARG_TRIM :
910 INAND_CMD38_ARG_ERASE,
911 0);
912 if (err)
913 goto out;
914 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700915 err = mmc_erase(card, from, nr, arg);
916out:
Adrian Hunter67716322011-08-29 16:42:15 +0300917 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
918 goto retry;
919 if (!err)
920 mmc_blk_reset_success(md, type);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700921 spin_lock_irq(&md->lock);
922 __blk_end_request(req, err, blk_rq_bytes(req));
923 spin_unlock_irq(&md->lock);
924
Adrian Hunterbd788c92010-08-11 14:17:47 -0700925 return err ? 0 : 1;
926}
927
Adrian Hunter49804542010-08-11 14:17:50 -0700928static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
929 struct request *req)
930{
931 struct mmc_blk_data *md = mq->data;
932 struct mmc_card *card = md->queue.card;
933 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300934 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700935
Maya Erez463bb952012-05-24 23:46:29 +0300936 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700937 err = -EOPNOTSUPP;
938 goto out;
939 }
940
941 from = blk_rq_pos(req);
942 nr = blk_rq_sectors(req);
943
944 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
945 arg = MMC_SECURE_TRIM1_ARG;
946 else
947 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300948retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500949 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
950 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
951 INAND_CMD38_ARG_EXT_CSD,
952 arg == MMC_SECURE_TRIM1_ARG ?
953 INAND_CMD38_ARG_SECTRIM1 :
954 INAND_CMD38_ARG_SECERASE,
955 0);
956 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300957 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500958 }
Adrian Hunter28302812012-04-05 14:45:48 +0300959
Adrian Hunter49804542010-08-11 14:17:50 -0700960 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300961 if (err == -EIO)
962 goto out_retry;
963 if (err)
964 goto out;
965
966 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500967 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
968 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
969 INAND_CMD38_ARG_EXT_CSD,
970 INAND_CMD38_ARG_SECTRIM2,
971 0);
972 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300973 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500974 }
Adrian Hunter28302812012-04-05 14:45:48 +0300975
Adrian Hunter49804542010-08-11 14:17:50 -0700976 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300977 if (err == -EIO)
978 goto out_retry;
979 if (err)
980 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500981 }
Adrian Hunter28302812012-04-05 14:45:48 +0300982
983 if (mmc_can_sanitize(card))
984 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
985 EXT_CSD_SANITIZE_START, 1, 0);
986out_retry:
987 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300988 goto retry;
989 if (!err)
990 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300991out:
Adrian Hunter49804542010-08-11 14:17:50 -0700992 spin_lock_irq(&md->lock);
993 __blk_end_request(req, err, blk_rq_bytes(req));
994 spin_unlock_irq(&md->lock);
995
Adrian Hunter49804542010-08-11 14:17:50 -0700996 return err ? 0 : 1;
997}
998
Maya Erez463bb952012-05-24 23:46:29 +0300999static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1000 struct request *req)
1001{
1002 struct mmc_blk_data *md = mq->data;
1003 struct mmc_card *card = md->queue.card;
1004 int err = 0;
1005
1006 BUG_ON(!card);
1007 BUG_ON(!card->host);
1008
1009 if (!(mmc_can_sanitize(card) &&
1010 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1011 pr_warning("%s: %s - SANITIZE is not supported\n",
1012 mmc_hostname(card->host), __func__);
1013 err = -EOPNOTSUPP;
1014 goto out;
1015 }
1016
1017 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1018 mmc_hostname(card->host), __func__);
1019
1020 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001021 EXT_CSD_SANITIZE_START, 1,
1022 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001023
1024 if (err)
1025 pr_err("%s: %s - mmc_switch() with "
1026 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1027 mmc_hostname(card->host), __func__, err);
1028
1029 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1030 __func__);
1031
1032out:
1033 spin_lock_irq(&md->lock);
1034 __blk_end_request(req, err, blk_rq_bytes(req));
1035 spin_unlock_irq(&md->lock);
1036
1037 return err ? 0 : 1;
1038}
1039
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001040static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1041{
1042 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001043 struct mmc_card *card = md->queue.card;
1044 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001045
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001046 ret = mmc_flush_cache(card);
1047 if (ret)
1048 ret = -EIO;
1049
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001050 spin_lock_irq(&md->lock);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001051 __blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001052 spin_unlock_irq(&md->lock);
1053
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001054 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001055}
1056
1057/*
1058 * Reformat current write as a reliable write, supporting
1059 * both legacy and the enhanced reliable write MMC cards.
1060 * In each transfer we'll handle only as much as a single
1061 * reliable write can handle, thus finish the request in
1062 * partial completions.
1063 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001064static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1065 struct mmc_card *card,
1066 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001067{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001068 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1069 /* Legacy mode imposes restrictions on transfers. */
1070 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1071 brq->data.blocks = 1;
1072
1073 if (brq->data.blocks > card->ext_csd.rel_sectors)
1074 brq->data.blocks = card->ext_csd.rel_sectors;
1075 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1076 brq->data.blocks = 1;
1077 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001078}
1079
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001080#define CMD_ERRORS \
1081 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1082 R1_ADDRESS_ERROR | /* Misaligned address */ \
1083 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1084 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1085 R1_CC_ERROR | /* Card controller error */ \
1086 R1_ERROR) /* General/unknown error */
1087
Per Forlinee8a43a2011-07-01 18:55:33 +02001088static int mmc_blk_err_check(struct mmc_card *card,
1089 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001090{
Per Forlinee8a43a2011-07-01 18:55:33 +02001091 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1092 mmc_active);
1093 struct mmc_blk_request *brq = &mq_mrq->brq;
1094 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001095 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001096
1097 /*
1098 * sbc.error indicates a problem with the set block count
1099 * command. No data will have been transferred.
1100 *
1101 * cmd.error indicates a problem with the r/w command. No
1102 * data will have been transferred.
1103 *
1104 * stop.error indicates a problem with the stop command. Data
1105 * may have been transferred, or may still be transferring.
1106 */
Adrian Hunter67716322011-08-29 16:42:15 +03001107 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1108 brq->data.error) {
1109 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001110 case ERR_RETRY:
1111 return MMC_BLK_RETRY;
1112 case ERR_ABORT:
1113 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301114 case ERR_NOMEDIUM:
1115 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001116 case ERR_CONTINUE:
1117 break;
1118 }
1119 }
1120
1121 /*
1122 * Check for errors relating to the execution of the
1123 * initial command - such as address errors. No data
1124 * has been transferred.
1125 */
1126 if (brq->cmd.resp[0] & CMD_ERRORS) {
1127 pr_err("%s: r/w command failed, status = %#x\n",
1128 req->rq_disk->disk_name, brq->cmd.resp[0]);
1129 return MMC_BLK_ABORT;
1130 }
1131
1132 /*
1133 * Everything else is either success, or a data error of some
1134 * kind. If it was a write, we may have transitioned to
1135 * program mode, which we have to wait for it to complete.
1136 */
1137 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1138 u32 status;
1139 do {
1140 int err = get_card_status(card, &status, 5);
1141 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301142 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001143 req->rq_disk->disk_name, err);
1144 return MMC_BLK_CMD_ERR;
1145 }
1146 /*
1147 * Some cards mishandle the status bits,
1148 * so make sure to check both the busy
1149 * indication and the card state.
1150 */
1151 } while (!(status & R1_READY_FOR_DATA) ||
1152 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1153 }
1154
1155 if (brq->data.error) {
1156 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1157 req->rq_disk->disk_name, brq->data.error,
1158 (unsigned)blk_rq_pos(req),
1159 (unsigned)blk_rq_sectors(req),
1160 brq->cmd.resp[0], brq->stop.resp[0]);
1161
1162 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001163 if (ecc_err)
1164 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001165 return MMC_BLK_DATA_ERR;
1166 } else {
1167 return MMC_BLK_CMD_ERR;
1168 }
1169 }
1170
Adrian Hunter67716322011-08-29 16:42:15 +03001171 if (!brq->data.bytes_xfered)
1172 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001173
Seungwon Jeon968c7742012-05-31 11:54:47 +03001174 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1175 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1176 return MMC_BLK_PARTIAL;
1177 else
1178 return MMC_BLK_SUCCESS;
1179 }
1180
Adrian Hunter67716322011-08-29 16:42:15 +03001181 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1182 return MMC_BLK_PARTIAL;
1183
1184 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001185}
1186
Seungwon Jeon968c7742012-05-31 11:54:47 +03001187static int mmc_blk_packed_err_check(struct mmc_card *card,
1188 struct mmc_async_req *areq)
1189{
1190 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1191 mmc_active);
1192 struct request *req = mq_rq->req;
1193 int err, check, status;
1194 u8 ext_csd[512];
1195
1196 check = mmc_blk_err_check(card, areq);
1197 err = get_card_status(card, &status, 0);
1198 if (err) {
1199 pr_err("%s: error %d sending status command\n",
1200 req->rq_disk->disk_name, err);
1201 return MMC_BLK_ABORT;
1202 }
1203
1204 if (status & R1_EXP_EVENT) {
1205 err = mmc_send_ext_csd(card, ext_csd);
1206 if (err) {
1207 pr_err("%s: error %d sending ext_csd\n",
1208 req->rq_disk->disk_name, err);
1209 return MMC_BLK_ABORT;
1210 }
1211
1212 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1213 EXT_CSD_PACKED_FAILURE) &&
1214 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1215 EXT_CSD_PACKED_GENERIC_ERROR)) {
1216 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1217 EXT_CSD_PACKED_INDEXED_ERROR) {
1218 mq_rq->packed_fail_idx =
1219 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1220 return MMC_BLK_PARTIAL;
1221 }
1222 }
1223 }
1224
1225 return check;
1226}
1227
Per Forlin54d49d772011-07-01 18:55:29 +02001228static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1229 struct mmc_card *card,
1230 int disable_multi,
1231 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Per Forlin54d49d772011-07-01 18:55:29 +02001233 u32 readcmd, writecmd;
1234 struct mmc_blk_request *brq = &mqrq->brq;
1235 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301237 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001239 /*
1240 * Reliable writes are used to implement Forced Unit Access and
1241 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001242 *
1243 * XXX: this really needs a good explanation of why REQ_META
1244 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001245 */
1246 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1247 (req->cmd_flags & REQ_META)) &&
1248 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001249 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001250
Per Forlin54d49d772011-07-01 18:55:29 +02001251 memset(brq, 0, sizeof(struct mmc_blk_request));
1252 brq->mrq.cmd = &brq->cmd;
1253 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Per Forlin54d49d772011-07-01 18:55:29 +02001255 brq->cmd.arg = blk_rq_pos(req);
1256 if (!mmc_card_blockaddr(card))
1257 brq->cmd.arg <<= 9;
1258 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1259 brq->data.blksz = 512;
1260 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1261 brq->stop.arg = 0;
1262 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1263 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Per Forlin54d49d772011-07-01 18:55:29 +02001265 /*
1266 * The block layer doesn't support all sector count
1267 * restrictions, so we need to be prepared for too big
1268 * requests.
1269 */
1270 if (brq->data.blocks > card->host->max_blk_count)
1271 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001273 if (brq->data.blocks > 1) {
1274 /*
1275 * After a read error, we redo the request one sector
1276 * at a time in order to accurately determine which
1277 * sectors can be read successfully.
1278 */
1279 if (disable_multi)
1280 brq->data.blocks = 1;
1281
1282 /* Some controllers can't do multiblock reads due to hw bugs */
1283 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1284 rq_data_dir(req) == READ)
1285 brq->data.blocks = 1;
1286 }
Per Forlin54d49d772011-07-01 18:55:29 +02001287
1288 if (brq->data.blocks > 1 || do_rel_wr) {
1289 /* SPI multiblock writes terminate using a special
1290 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001291 */
Per Forlin54d49d772011-07-01 18:55:29 +02001292 if (!mmc_host_is_spi(card->host) ||
1293 rq_data_dir(req) == READ)
1294 brq->mrq.stop = &brq->stop;
1295 readcmd = MMC_READ_MULTIPLE_BLOCK;
1296 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1297 } else {
1298 brq->mrq.stop = NULL;
1299 readcmd = MMC_READ_SINGLE_BLOCK;
1300 writecmd = MMC_WRITE_BLOCK;
1301 }
1302 if (rq_data_dir(req) == READ) {
1303 brq->cmd.opcode = readcmd;
1304 brq->data.flags |= MMC_DATA_READ;
1305 } else {
1306 brq->cmd.opcode = writecmd;
1307 brq->data.flags |= MMC_DATA_WRITE;
1308 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001309
Per Forlin54d49d772011-07-01 18:55:29 +02001310 if (do_rel_wr)
1311 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001312
Per Forlin54d49d772011-07-01 18:55:29 +02001313 /*
Saugata Das42659002011-12-21 13:09:17 +05301314 * Data tag is used only during writing meta data to speed
1315 * up write and any subsequent read of this meta data
1316 */
1317 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1318 (req->cmd_flags & REQ_META) &&
1319 (rq_data_dir(req) == WRITE) &&
1320 ((brq->data.blocks * brq->data.blksz) >=
1321 card->ext_csd.data_tag_unit_size);
1322
1323 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001324 * Pre-defined multi-block transfers are preferable to
1325 * open ended-ones (and necessary for reliable writes).
1326 * However, it is not sufficient to just send CMD23,
1327 * and avoid the final CMD12, as on an error condition
1328 * CMD12 (stop) needs to be sent anyway. This, coupled
1329 * with Auto-CMD23 enhancements provided by some
1330 * hosts, means that the complexity of dealing
1331 * with this is best left to the host. If CMD23 is
1332 * supported by card and host, we'll fill sbc in and let
1333 * the host deal with handling it correctly. This means
1334 * that for hosts that don't expose MMC_CAP_CMD23, no
1335 * change of behavior will be observed.
1336 *
1337 * N.B: Some MMC cards experience perf degradation.
1338 * We'll avoid using CMD23-bounded multiblock writes for
1339 * these, while retaining features like reliable writes.
1340 */
Saugata Das42659002011-12-21 13:09:17 +05301341 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1342 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1343 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001344 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1345 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301346 (do_rel_wr ? (1 << 31) : 0) |
1347 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001348 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1349 brq->mrq.sbc = &brq->sbc;
1350 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001351
Per Forlin54d49d772011-07-01 18:55:29 +02001352 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001353
Per Forlin54d49d772011-07-01 18:55:29 +02001354 brq->data.sg = mqrq->sg;
1355 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001356
Per Forlin54d49d772011-07-01 18:55:29 +02001357 /*
1358 * Adjust the sg list so it is the same size as the
1359 * request.
1360 */
1361 if (brq->data.blocks != blk_rq_sectors(req)) {
1362 int i, data_size = brq->data.blocks << 9;
1363 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001364
Per Forlin54d49d772011-07-01 18:55:29 +02001365 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1366 data_size -= sg->length;
1367 if (data_size <= 0) {
1368 sg->length += data_size;
1369 i++;
1370 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001371 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001372 }
Per Forlin54d49d772011-07-01 18:55:29 +02001373 brq->data.sg_len = i;
1374 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001375
Per Forlinee8a43a2011-07-01 18:55:33 +02001376 mqrq->mmc_active.mrq = &brq->mrq;
1377 mqrq->mmc_active.err_check = mmc_blk_err_check;
1378
Per Forlin54d49d772011-07-01 18:55:29 +02001379 mmc_queue_bounce_pre(mqrq);
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Maya Erez63c61d62012-05-31 21:00:18 +03001382static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1383 struct request *req)
1384{
1385 struct mmc_host *host = mq->card->host;
1386 int data_dir;
1387
1388 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1389 return;
1390
1391 /*
1392 * In case the packing control is not supported by the host, it should
1393 * not have an effect on the write packing. Therefore we have to enable
1394 * the write packing
1395 */
1396 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1397 mq->wr_packing_enabled = true;
1398 return;
1399 }
1400
1401 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1402 if (mq->num_of_potential_packed_wr_reqs >
1403 mq->num_wr_reqs_to_start_packing)
1404 mq->wr_packing_enabled = true;
1405 return;
1406 }
1407
1408 data_dir = rq_data_dir(req);
1409
1410 if (data_dir == READ) {
1411 mq->num_of_potential_packed_wr_reqs = 0;
1412 mq->wr_packing_enabled = false;
1413 return;
1414 } else if (data_dir == WRITE) {
1415 mq->num_of_potential_packed_wr_reqs++;
1416 }
1417
1418 if (mq->num_of_potential_packed_wr_reqs >
1419 mq->num_wr_reqs_to_start_packing)
1420 mq->wr_packing_enabled = true;
1421
1422}
1423
Maya Ereze544d7002012-06-04 06:09:47 +03001424struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1425{
1426 if (!card)
1427 return NULL;
1428
1429 return &card->wr_pack_stats;
1430}
1431EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1432
1433void mmc_blk_init_packed_statistics(struct mmc_card *card)
1434{
1435 int max_num_of_packed_reqs = 0;
1436
1437 if (!card || !card->wr_pack_stats.packing_events)
1438 return;
1439
1440 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1441
1442 spin_lock(&card->wr_pack_stats.lock);
1443 memset(card->wr_pack_stats.packing_events, 0,
1444 (max_num_of_packed_reqs + 1) *
1445 sizeof(*card->wr_pack_stats.packing_events));
1446 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1447 sizeof(card->wr_pack_stats.pack_stop_reason));
1448 card->wr_pack_stats.enabled = true;
1449 spin_unlock(&card->wr_pack_stats.lock);
1450}
1451EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1452
Seungwon Jeon968c7742012-05-31 11:54:47 +03001453static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1454{
1455 struct request_queue *q = mq->queue;
1456 struct mmc_card *card = mq->card;
1457 struct request *cur = req, *next = NULL;
1458 struct mmc_blk_data *md = mq->data;
1459 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1460 unsigned int req_sectors = 0, phys_segments = 0;
1461 unsigned int max_blk_count, max_phys_segs;
1462 u8 put_back = 0;
1463 u8 max_packed_rw = 0;
1464 u8 reqs = 0;
Maya Ereze544d7002012-06-04 06:09:47 +03001465 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001466
1467 mmc_blk_clear_packed(mq->mqrq_cur);
1468
1469 if (!(md->flags & MMC_BLK_CMD23) ||
1470 !card->ext_csd.packed_event_en)
1471 goto no_packed;
1472
Maya Erez63c61d62012-05-31 21:00:18 +03001473 if (!mq->wr_packing_enabled)
1474 goto no_packed;
1475
Seungwon Jeon968c7742012-05-31 11:54:47 +03001476 if ((rq_data_dir(cur) == WRITE) &&
1477 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1478 max_packed_rw = card->ext_csd.max_packed_writes;
1479
1480 if (max_packed_rw == 0)
1481 goto no_packed;
1482
1483 if (mmc_req_rel_wr(cur) &&
1484 (md->flags & MMC_BLK_REL_WR) &&
1485 !en_rel_wr) {
1486 goto no_packed;
1487 }
1488
1489 max_blk_count = min(card->host->max_blk_count,
1490 card->host->max_req_size >> 9);
1491 if (unlikely(max_blk_count > 0xffff))
1492 max_blk_count = 0xffff;
1493
1494 max_phys_segs = queue_max_segments(q);
1495 req_sectors += blk_rq_sectors(cur);
1496 phys_segments += cur->nr_phys_segments;
1497
1498 if (rq_data_dir(cur) == WRITE) {
1499 req_sectors++;
1500 phys_segments++;
1501 }
1502
Maya Ereze544d7002012-06-04 06:09:47 +03001503 spin_lock(&stats->lock);
1504
Seungwon Jeon968c7742012-05-31 11:54:47 +03001505 while (reqs < max_packed_rw - 1) {
1506 spin_lock_irq(q->queue_lock);
1507 next = blk_fetch_request(q);
1508 spin_unlock_irq(q->queue_lock);
Maya Ereze544d7002012-06-04 06:09:47 +03001509 if (!next) {
1510 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001511 break;
Maya Ereze544d7002012-06-04 06:09:47 +03001512 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001513
1514 if (next->cmd_flags & REQ_DISCARD ||
1515 next->cmd_flags & REQ_FLUSH) {
Maya Ereze544d7002012-06-04 06:09:47 +03001516 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001517 put_back = 1;
1518 break;
1519 }
1520
1521 if (rq_data_dir(cur) != rq_data_dir(next)) {
Maya Ereze544d7002012-06-04 06:09:47 +03001522 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001523 put_back = 1;
1524 break;
1525 }
1526
1527 if (mmc_req_rel_wr(next) &&
1528 (md->flags & MMC_BLK_REL_WR) &&
1529 !en_rel_wr) {
Maya Ereze544d7002012-06-04 06:09:47 +03001530 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001531 put_back = 1;
1532 break;
1533 }
1534
1535 req_sectors += blk_rq_sectors(next);
1536 if (req_sectors > max_blk_count) {
Maya Ereze544d7002012-06-04 06:09:47 +03001537 if (stats->enabled)
1538 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001539 put_back = 1;
1540 break;
1541 }
1542
1543 phys_segments += next->nr_phys_segments;
1544 if (phys_segments > max_phys_segs) {
Maya Ereze544d7002012-06-04 06:09:47 +03001545 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001546 put_back = 1;
1547 break;
1548 }
1549
Maya Erez63c61d62012-05-31 21:00:18 +03001550 if (rq_data_dir(next) == WRITE)
1551 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001552 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1553 cur = next;
1554 reqs++;
1555 }
1556
1557 if (put_back) {
1558 spin_lock_irq(q->queue_lock);
1559 blk_requeue_request(q, next);
1560 spin_unlock_irq(q->queue_lock);
1561 }
1562
Maya Ereze544d7002012-06-04 06:09:47 +03001563 if (stats->enabled) {
1564 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1565 stats->packing_events[reqs + 1]++;
1566 if (reqs + 1 == max_packed_rw)
1567 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1568 }
1569
1570 spin_unlock(&stats->lock);
1571
Seungwon Jeon968c7742012-05-31 11:54:47 +03001572 if (reqs > 0) {
1573 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1574 mq->mqrq_cur->packed_num = ++reqs;
1575 return reqs;
1576 }
1577
1578no_packed:
1579 mmc_blk_clear_packed(mq->mqrq_cur);
1580 return 0;
1581}
1582
1583static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1584 struct mmc_card *card,
1585 struct mmc_queue *mq)
1586{
1587 struct mmc_blk_request *brq = &mqrq->brq;
1588 struct request *req = mqrq->req;
1589 struct request *prq;
1590 struct mmc_blk_data *md = mq->data;
1591 bool do_rel_wr;
1592 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1593 u8 i = 1;
1594
1595 mqrq->packed_cmd = MMC_PACKED_WRITE;
1596 mqrq->packed_blocks = 0;
1597 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1598
1599 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1600 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1601 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1602
1603 /*
1604 * Argument for each entry of packed group
1605 */
1606 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1607 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1608 /* Argument of CMD23*/
1609 packed_cmd_hdr[(i * 2)] =
1610 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1611 blk_rq_sectors(prq);
1612 /* Argument of CMD18 or CMD25 */
1613 packed_cmd_hdr[((i * 2)) + 1] =
1614 mmc_card_blockaddr(card) ?
1615 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1616 mqrq->packed_blocks += blk_rq_sectors(prq);
1617 i++;
1618 }
1619
1620 memset(brq, 0, sizeof(struct mmc_blk_request));
1621 brq->mrq.cmd = &brq->cmd;
1622 brq->mrq.data = &brq->data;
1623 brq->mrq.sbc = &brq->sbc;
1624 brq->mrq.stop = &brq->stop;
1625
1626 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1627 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1628 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1629
1630 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1631 brq->cmd.arg = blk_rq_pos(req);
1632 if (!mmc_card_blockaddr(card))
1633 brq->cmd.arg <<= 9;
1634 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1635
1636 brq->data.blksz = 512;
1637 brq->data.blocks = mqrq->packed_blocks + 1;
1638 brq->data.flags |= MMC_DATA_WRITE;
1639
1640 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1641 brq->stop.arg = 0;
1642 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1643
1644 mmc_set_data_timeout(&brq->data, card);
1645
1646 brq->data.sg = mqrq->sg;
1647 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1648
1649 mqrq->mmc_active.mrq = &brq->mrq;
1650 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1651
1652 mmc_queue_bounce_pre(mqrq);
1653}
1654
Adrian Hunter67716322011-08-29 16:42:15 +03001655static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1656 struct mmc_blk_request *brq, struct request *req,
1657 int ret)
1658{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001659 struct mmc_queue_req *mq_rq;
1660 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1661
Adrian Hunter67716322011-08-29 16:42:15 +03001662 /*
1663 * If this is an SD card and we're writing, we can first
1664 * mark the known good sectors as ok.
1665 *
1666 * If the card is not SD, we can still ok written sectors
1667 * as reported by the controller (which might be less than
1668 * the real number of written sectors, but never more).
1669 */
1670 if (mmc_card_sd(card)) {
1671 u32 blocks;
1672
1673 blocks = mmc_sd_num_wr_blocks(card);
1674 if (blocks != (u32)-1) {
1675 spin_lock_irq(&md->lock);
1676 ret = __blk_end_request(req, 0, blocks << 9);
1677 spin_unlock_irq(&md->lock);
1678 }
1679 } else {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001680 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1681 spin_lock_irq(&md->lock);
1682 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
1683 spin_unlock_irq(&md->lock);
1684 }
Adrian Hunter67716322011-08-29 16:42:15 +03001685 }
1686 return ret;
1687}
1688
Seungwon Jeon968c7742012-05-31 11:54:47 +03001689static int mmc_blk_end_packed_req(struct mmc_queue *mq,
1690 struct mmc_queue_req *mq_rq)
1691{
1692 struct mmc_blk_data *md = mq->data;
1693 struct request *prq;
1694 int idx = mq_rq->packed_fail_idx, i = 0;
1695 int ret = 0;
1696
1697 while (!list_empty(&mq_rq->packed_list)) {
1698 prq = list_entry_rq(mq_rq->packed_list.next);
1699 if (idx == i) {
1700 /* retry from error index */
1701 mq_rq->packed_num -= idx;
1702 mq_rq->req = prq;
1703 ret = 1;
1704
1705 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1706 list_del_init(&prq->queuelist);
1707 mmc_blk_clear_packed(mq_rq);
1708 }
1709 return ret;
1710 }
1711 list_del_init(&prq->queuelist);
1712 spin_lock_irq(&md->lock);
1713 __blk_end_request(prq, 0, blk_rq_bytes(prq));
1714 spin_unlock_irq(&md->lock);
1715 i++;
1716 }
1717
1718 mmc_blk_clear_packed(mq_rq);
1719 return ret;
1720}
1721
Per Forlinee8a43a2011-07-01 18:55:33 +02001722static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001723{
1724 struct mmc_blk_data *md = mq->data;
1725 struct mmc_card *card = md->queue.card;
1726 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001727 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001728 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001729 struct mmc_queue_req *mq_rq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001730 struct request *req, *prq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001731 struct mmc_async_req *areq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001732 const u8 packed_num = 2;
1733 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001734
1735 if (!rqc && !mq->mqrq_prev->req)
1736 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001737
Seungwon Jeon968c7742012-05-31 11:54:47 +03001738 if (rqc)
1739 reqs = mmc_blk_prep_packed_list(mq, rqc);
1740
Per Forlin54d49d772011-07-01 18:55:29 +02001741 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001742 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001743 if (reqs >= packed_num)
1744 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1745 card, mq);
1746 else
1747 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001748 areq = &mq->mqrq_cur->mmc_active;
1749 } else
1750 areq = NULL;
1751 areq = mmc_start_req(card->host, areq, (int *) &status);
1752 if (!areq)
1753 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001754
Per Forlinee8a43a2011-07-01 18:55:33 +02001755 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1756 brq = &mq_rq->brq;
1757 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001758 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001759 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001760
Jaehoon Chungf886c802012-05-28 10:33:35 +03001761 /*
1762 * Check BKOPS urgency from each R1 response
1763 */
1764 if (mmc_card_mmc(card) &&
1765 (brq->cmd.resp[0] & R1_EXCEPTION_EVENT))
1766 mmc_card_set_check_bkops(card);
1767
Per Forlind78d4a82011-07-01 18:55:30 +02001768 switch (status) {
1769 case MMC_BLK_SUCCESS:
1770 case MMC_BLK_PARTIAL:
1771 /*
1772 * A block was successfully transferred.
1773 */
Adrian Hunter67716322011-08-29 16:42:15 +03001774 mmc_blk_reset_success(md, type);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001775
1776 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1777 ret = mmc_blk_end_packed_req(mq, mq_rq);
1778 break;
1779 } else {
1780 spin_lock_irq(&md->lock);
1781 ret = __blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001782 brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001783 spin_unlock_irq(&md->lock);
1784 }
1785
Adrian Hunter67716322011-08-29 16:42:15 +03001786 /*
1787 * If the blk_end_request function returns non-zero even
1788 * though all data has been transferred and no errors
1789 * were returned by the host controller, it's a bug.
1790 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001791 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301792 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001793 __func__, blk_rq_bytes(req),
1794 brq->data.bytes_xfered);
1795 rqc = NULL;
1796 goto cmd_abort;
1797 }
Per Forlind78d4a82011-07-01 18:55:30 +02001798 break;
1799 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001800 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1801 if (!mmc_blk_reset(md, card->host, type))
1802 break;
1803 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001804 case MMC_BLK_RETRY:
1805 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001806 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001807 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001808 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001809 if (!mmc_blk_reset(md, card->host, type))
1810 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001811 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001812 case MMC_BLK_DATA_ERR: {
1813 int err;
1814
1815 err = mmc_blk_reset(md, card->host, type);
1816 if (!err)
1817 break;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001818 if (err == -ENODEV ||
1819 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001820 goto cmd_abort;
1821 /* Fall through */
1822 }
1823 case MMC_BLK_ECC_ERR:
1824 if (brq->data.blocks > 1) {
1825 /* Redo read one sector at a time */
1826 pr_warning("%s: retrying using single block read\n",
1827 req->rq_disk->disk_name);
1828 disable_multi = 1;
1829 break;
1830 }
Per Forlind78d4a82011-07-01 18:55:30 +02001831 /*
1832 * After an error, we redo I/O one sector at a
1833 * time, so we only reach here after trying to
1834 * read a single sector.
1835 */
1836 spin_lock_irq(&md->lock);
1837 ret = __blk_end_request(req, -EIO,
1838 brq->data.blksz);
1839 spin_unlock_irq(&md->lock);
Per Forlinee8a43a2011-07-01 18:55:33 +02001840 if (!ret)
1841 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001842 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301843 case MMC_BLK_NOMEDIUM:
1844 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001845 }
1846
Per Forlinee8a43a2011-07-01 18:55:33 +02001847 if (ret) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001848 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1849 /*
1850 * In case of a incomplete request
1851 * prepare it again and resend.
1852 */
1853 mmc_blk_rw_rq_prep(mq_rq, card,
1854 disable_multi, mq);
1855 mmc_start_req(card->host,
1856 &mq_rq->mmc_active, NULL);
1857 } else {
1858 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1859 mmc_start_req(card->host,
1860 &mq_rq->mmc_active, NULL);
1861 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 } while (ret);
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return 1;
1866
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001867 cmd_abort:
Seungwon Jeon968c7742012-05-31 11:54:47 +03001868 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1869 spin_lock_irq(&md->lock);
1870 if (mmc_card_removed(card))
1871 req->cmd_flags |= REQ_QUIET;
1872 while (ret)
1873 ret = __blk_end_request(req, -EIO,
1874 blk_rq_cur_bytes(req));
1875 spin_unlock_irq(&md->lock);
1876 } else {
1877 while (!list_empty(&mq_rq->packed_list)) {
1878 prq = list_entry_rq(mq_rq->packed_list.next);
1879 list_del_init(&prq->queuelist);
1880 spin_lock_irq(&md->lock);
1881 __blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1882 spin_unlock_irq(&md->lock);
1883 }
1884 mmc_blk_clear_packed(mq_rq);
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Per Forlinee8a43a2011-07-01 18:55:33 +02001887 start_new_req:
1888 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001889 /*
1890 * If current request is packed, it needs to put back.
1891 */
1892 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE) {
1893 while (!list_empty(&mq->mqrq_cur->packed_list)) {
1894 prq = list_entry_rq(
1895 mq->mqrq_cur->packed_list.prev);
1896 if (prq->queuelist.prev !=
1897 &mq->mqrq_cur->packed_list) {
1898 list_del_init(&prq->queuelist);
1899 spin_lock_irq(mq->queue->queue_lock);
1900 blk_requeue_request(mq->queue, prq);
1901 spin_unlock_irq(mq->queue->queue_lock);
1902 } else {
1903 list_del_init(&prq->queuelist);
1904 }
1905 }
1906 mmc_blk_clear_packed(mq->mqrq_cur);
1907 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001908 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1909 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1910 }
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
1913}
1914
Adrian Hunterbd788c92010-08-11 14:17:47 -07001915static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1916{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001917 int ret;
1918 struct mmc_blk_data *md = mq->data;
1919 struct mmc_card *card = md->queue.card;
1920
San Mehat148c57a2009-07-30 08:21:19 -07001921#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1922 if (mmc_bus_needs_resume(card->host)) {
1923 mmc_resume_bus(card->host);
1924 mmc_blk_set_blksize(md, card);
1925 }
1926#endif
1927
Per Forlinee8a43a2011-07-01 18:55:33 +02001928 if (req && !mq->mqrq_prev->req)
1929 /* claim host only for the first request */
1930 mmc_claim_host(card->host);
1931
Andrei Warkentin371a6892011-04-11 18:10:25 -05001932 ret = mmc_blk_part_switch(card, md);
1933 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001934 if (req) {
1935 spin_lock_irq(&md->lock);
1936 __blk_end_request_all(req, -EIO);
1937 spin_unlock_irq(&md->lock);
1938 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001939 ret = 0;
1940 goto out;
1941 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001942
Maya Erez63c61d62012-05-31 21:00:18 +03001943 mmc_blk_write_packing_control(mq, req);
1944
Maya Erez463bb952012-05-24 23:46:29 +03001945 if (req && req->cmd_flags & REQ_SANITIZE) {
1946 /* complete ongoing async transfer before issuing sanitize */
1947 if (card->host && card->host->areq)
1948 mmc_blk_issue_rw_rq(mq, NULL);
1949 ret = mmc_blk_issue_sanitize_rq(mq, req);
1950 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001951 /* complete ongoing async transfer before issuing discard */
1952 if (card->host->areq)
1953 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001954 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001955 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001956 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001957 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001958 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001959 /* complete ongoing async transfer before issuing flush */
1960 if (card->host->areq)
1961 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001962 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001963 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001964 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001965 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001966
Andrei Warkentin371a6892011-04-11 18:10:25 -05001967out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001968 if (!req)
1969 /* release host only when there are no more requests */
1970 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001971 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001972}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Russell Kinga6f6c962006-01-03 22:38:44 +00001974static inline int mmc_blk_readonly(struct mmc_card *card)
1975{
1976 return mmc_card_readonly(card) ||
1977 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1978}
1979
Andrei Warkentin371a6892011-04-11 18:10:25 -05001980static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1981 struct device *parent,
1982 sector_t size,
1983 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001984 const char *subname,
1985 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 struct mmc_blk_data *md;
1988 int devidx, ret;
1989
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001990 devidx = find_first_zero_bit(dev_use, max_devices);
1991 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return ERR_PTR(-ENOSPC);
1993 __set_bit(devidx, dev_use);
1994
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001995 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001996 if (!md) {
1997 ret = -ENOMEM;
1998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002000
Russell Kinga6f6c962006-01-03 22:38:44 +00002001 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002002 * !subname implies we are creating main mmc_blk_data that will be
2003 * associated with mmc_card with mmc_set_drvdata. Due to device
2004 * partitions, devidx will not coincide with a per-physical card
2005 * index anymore so we keep track of a name index.
2006 */
2007 if (!subname) {
2008 md->name_idx = find_first_zero_bit(name_use, max_devices);
2009 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002010 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002011 md->name_idx = ((struct mmc_blk_data *)
2012 dev_to_disk(parent)->private_data)->name_idx;
2013
Johan Rudholmadd710e2011-12-02 08:51:06 +01002014 md->area_type = area_type;
2015
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002016 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002017 * Set the read-only status based on the supported commands
2018 * and the write protect switch.
2019 */
2020 md->read_only = mmc_blk_readonly(card);
2021
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002022 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002023 if (md->disk == NULL) {
2024 ret = -ENOMEM;
2025 goto err_kfree;
2026 }
2027
2028 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002029 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002030 md->usage = 1;
2031
Adrian Hunterd09408a2011-06-23 13:40:28 +03002032 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002033 if (ret)
2034 goto err_putdisk;
2035
Russell Kinga6f6c962006-01-03 22:38:44 +00002036 md->queue.issue_fn = mmc_blk_issue_rq;
2037 md->queue.data = md;
2038
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002039 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002040 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002041 md->disk->fops = &mmc_bdops;
2042 md->disk->private_data = md;
2043 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002044 md->disk->driverfs_dev = parent;
2045 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002046 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002047
2048 /*
2049 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2050 *
2051 * - be set for removable media with permanent block devices
2052 * - be unset for removable block devices with permanent media
2053 *
2054 * Since MMC block devices clearly fall under the second
2055 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2056 * should use the block device creation/destruction hotplug
2057 * messages to tell when the card is present.
2058 */
2059
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002060 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2061 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002062
Martin K. Petersene1defc42009-05-22 17:17:49 -04002063 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002064 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002065
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002066 if (mmc_host_cmd23(card->host)) {
2067 if (mmc_card_mmc(card) ||
2068 (mmc_card_sd(card) &&
2069 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2070 md->flags |= MMC_BLK_CMD23;
2071 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002072
2073 if (mmc_card_mmc(card) &&
2074 md->flags & MMC_BLK_CMD23 &&
2075 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2076 card->ext_csd.rel_sectors)) {
2077 md->flags |= MMC_BLK_REL_WR;
2078 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2079 }
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002082
2083 err_putdisk:
2084 put_disk(md->disk);
2085 err_kfree:
2086 kfree(md);
2087 out:
2088 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Andrei Warkentin371a6892011-04-11 18:10:25 -05002091static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2092{
2093 sector_t size;
2094 struct mmc_blk_data *md;
2095
2096 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2097 /*
2098 * The EXT_CSD sector count is in number or 512 byte
2099 * sectors.
2100 */
2101 size = card->ext_csd.sectors;
2102 } else {
2103 /*
2104 * The CSD capacity field is in units of read_blkbits.
2105 * set_capacity takes units of 512 bytes.
2106 */
2107 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2108 }
2109
Johan Rudholmadd710e2011-12-02 08:51:06 +01002110 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2111 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002112 return md;
2113}
2114
2115static int mmc_blk_alloc_part(struct mmc_card *card,
2116 struct mmc_blk_data *md,
2117 unsigned int part_type,
2118 sector_t size,
2119 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002120 const char *subname,
2121 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002122{
2123 char cap_str[10];
2124 struct mmc_blk_data *part_md;
2125
2126 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002127 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002128 if (IS_ERR(part_md))
2129 return PTR_ERR(part_md);
2130 part_md->part_type = part_type;
2131 list_add(&part_md->part, &md->part);
2132
2133 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2134 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302135 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002136 part_md->disk->disk_name, mmc_card_id(card),
2137 mmc_card_name(card), part_md->part_type, cap_str);
2138 return 0;
2139}
2140
Namjae Jeone0c368d2011-10-06 23:41:38 +09002141/* MMC Physical partitions consist of two boot partitions and
2142 * up to four general purpose partitions.
2143 * For each partition enabled in EXT_CSD a block device will be allocatedi
2144 * to provide access to the partition.
2145 */
2146
Andrei Warkentin371a6892011-04-11 18:10:25 -05002147static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2148{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002149 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002150
2151 if (!mmc_card_mmc(card))
2152 return 0;
2153
Namjae Jeone0c368d2011-10-06 23:41:38 +09002154 for (idx = 0; idx < card->nr_parts; idx++) {
2155 if (card->part[idx].size) {
2156 ret = mmc_blk_alloc_part(card, md,
2157 card->part[idx].part_cfg,
2158 card->part[idx].size >> 9,
2159 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002160 card->part[idx].name,
2161 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002162 if (ret)
2163 return ret;
2164 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002165 }
2166
2167 return ret;
2168}
2169
Andrei Warkentin371a6892011-04-11 18:10:25 -05002170static void mmc_blk_remove_req(struct mmc_blk_data *md)
2171{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002172 struct mmc_card *card;
2173
Andrei Warkentin371a6892011-04-11 18:10:25 -05002174 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002175 card = md->queue.card;
Maya Erez63c61d62012-05-31 21:00:18 +03002176 device_remove_file(disk_to_dev(md->disk),
2177 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002178 if (md->disk->flags & GENHD_FL_UP) {
2179 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002180 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2181 card->ext_csd.boot_ro_lockable)
2182 device_remove_file(disk_to_dev(md->disk),
2183 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002184
2185 /* Stop new requests from getting into the queue */
2186 del_gendisk(md->disk);
2187 }
2188
2189 /* Then flush out any already in there */
2190 mmc_cleanup_queue(&md->queue);
2191 mmc_blk_put(md);
2192 }
2193}
2194
2195static void mmc_blk_remove_parts(struct mmc_card *card,
2196 struct mmc_blk_data *md)
2197{
2198 struct list_head *pos, *q;
2199 struct mmc_blk_data *part_md;
2200
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002201 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002202 list_for_each_safe(pos, q, &md->part) {
2203 part_md = list_entry(pos, struct mmc_blk_data, part);
2204 list_del(pos);
2205 mmc_blk_remove_req(part_md);
2206 }
2207}
2208
2209static int mmc_add_disk(struct mmc_blk_data *md)
2210{
2211 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002212 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002213
2214 add_disk(md->disk);
2215 md->force_ro.show = force_ro_show;
2216 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302217 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002218 md->force_ro.attr.name = "force_ro";
2219 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2220 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2221 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002222 goto force_ro_fail;
2223
2224 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2225 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002226 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002227
2228 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2229 mode = S_IRUGO;
2230 else
2231 mode = S_IRUGO | S_IWUSR;
2232
2233 md->power_ro_lock.show = power_ro_lock_show;
2234 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002235 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002236 md->power_ro_lock.attr.mode = mode;
2237 md->power_ro_lock.attr.name =
2238 "ro_lock_until_next_power_on";
2239 ret = device_create_file(disk_to_dev(md->disk),
2240 &md->power_ro_lock);
2241 if (ret)
2242 goto power_ro_lock_fail;
2243 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244
Maya Erez63c61d62012-05-31 21:00:18 +03002245 md->num_wr_reqs_to_start_packing.show =
2246 num_wr_reqs_to_start_packing_show;
2247 md->num_wr_reqs_to_start_packing.store =
2248 num_wr_reqs_to_start_packing_store;
2249 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2250 md->num_wr_reqs_to_start_packing.attr.name =
2251 "num_wr_reqs_to_start_packing";
2252 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2253 ret = device_create_file(disk_to_dev(md->disk),
2254 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002255 if (ret)
2256 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002257
Johan Rudholmadd710e2011-12-02 08:51:06 +01002258 return ret;
2259
2260power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002261 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002262force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002263 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002264
2265 return ret;
2266}
2267
Chris Ballc59d4472011-11-11 22:01:43 -05002268#define CID_MANFID_SANDISK 0x2
2269#define CID_MANFID_TOSHIBA 0x11
2270#define CID_MANFID_MICRON 0x13
2271
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002272static const struct mmc_fixup blk_fixups[] =
2273{
Chris Ballc59d4472011-11-11 22:01:43 -05002274 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2275 MMC_QUIRK_INAND_CMD38),
2276 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2277 MMC_QUIRK_INAND_CMD38),
2278 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2279 MMC_QUIRK_INAND_CMD38),
2280 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2281 MMC_QUIRK_INAND_CMD38),
2282 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2283 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002284
2285 /*
2286 * Some MMC cards experience performance degradation with CMD23
2287 * instead of CMD12-bounded multiblock transfers. For now we'll
2288 * black list what's bad...
2289 * - Certain Toshiba cards.
2290 *
2291 * N.B. This doesn't affect SD cards.
2292 */
Chris Ballc59d4472011-11-11 22:01:43 -05002293 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002294 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002295 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002296 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002297 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002298 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002299
2300 /*
2301 * Some Micron MMC cards needs longer data read timeout than
2302 * indicated in CSD.
2303 */
Chris Ballc59d4472011-11-11 22:01:43 -05002304 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002305 MMC_QUIRK_LONG_READ_TIME),
2306
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302307 /* Some INAND MCP devices advertise incorrect timeout values */
2308 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2309 MMC_QUIRK_INAND_DATA_TIMEOUT),
2310
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002311 END_FIXUP
2312};
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314static int mmc_blk_probe(struct mmc_card *card)
2315{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002316 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002317 char cap_str[10];
2318
Pierre Ossman912490d2005-05-21 10:27:02 +01002319 /*
2320 * Check that the card supports the command class(es) we need.
2321 */
2322 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return -ENODEV;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 md = mmc_blk_alloc(card);
2326 if (IS_ERR(md))
2327 return PTR_ERR(md);
2328
Yi Li444122f2009-02-05 15:31:57 +08002329 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002330 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302331 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002333 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Andrei Warkentin371a6892011-04-11 18:10:25 -05002335 if (mmc_blk_alloc_parts(card, md))
2336 goto out;
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002339 mmc_fixup_device(card, blk_fixups);
2340
San Mehat148c57a2009-07-30 08:21:19 -07002341#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2342 mmc_set_bus_resume_policy(card->host, 1);
2343#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002344 if (mmc_add_disk(md))
2345 goto out;
2346
2347 list_for_each_entry(part_md, &md->part, part) {
2348 if (mmc_add_disk(part_md))
2349 goto out;
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 0;
2352
2353 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002354 mmc_blk_remove_parts(card, md);
2355 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359static void mmc_blk_remove(struct mmc_card *card)
2360{
2361 struct mmc_blk_data *md = mmc_get_drvdata(card);
2362
Andrei Warkentin371a6892011-04-11 18:10:25 -05002363 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002364 mmc_claim_host(card->host);
2365 mmc_blk_part_switch(card, md);
2366 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002367 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002369#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2370 mmc_set_bus_resume_policy(card->host, 0);
2371#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
2374#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002375static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002377 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct mmc_blk_data *md = mmc_get_drvdata(card);
2379
2380 if (md) {
2381 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002382 list_for_each_entry(part_md, &md->part, part) {
2383 mmc_queue_suspend(&part_md->queue);
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386 return 0;
2387}
2388
2389static int mmc_blk_resume(struct mmc_card *card)
2390{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002391 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct mmc_blk_data *md = mmc_get_drvdata(card);
2393
2394 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002395 /*
2396 * Resume involves the card going into idle state,
2397 * so current partition is always the main one.
2398 */
2399 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002401 list_for_each_entry(part_md, &md->part, part) {
2402 mmc_queue_resume(&part_md->queue);
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405 return 0;
2406}
2407#else
2408#define mmc_blk_suspend NULL
2409#define mmc_blk_resume NULL
2410#endif
2411
2412static struct mmc_driver mmc_driver = {
2413 .drv = {
2414 .name = "mmcblk",
2415 },
2416 .probe = mmc_blk_probe,
2417 .remove = mmc_blk_remove,
2418 .suspend = mmc_blk_suspend,
2419 .resume = mmc_blk_resume,
2420};
2421
2422static int __init mmc_blk_init(void)
2423{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002424 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002426 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2427 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2428
2429 max_devices = 256 / perdev_minors;
2430
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002431 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2432 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002435 res = mmc_register_driver(&mmc_driver);
2436 if (res)
2437 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002439 return 0;
2440 out2:
2441 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 out:
2443 return res;
2444}
2445
2446static void __exit mmc_blk_exit(void)
2447{
2448 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002449 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452module_init(mmc_blk_init);
2453module_exit(mmc_blk_exit);
2454
2455MODULE_LICENSE("GPL");
2456MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2457