blob: 5e0d669e33d29293250fb67025ac59b80eef20b5 [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);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530921 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700922
Adrian Hunterbd788c92010-08-11 14:17:47 -0700923 return err ? 0 : 1;
924}
925
Adrian Hunter49804542010-08-11 14:17:50 -0700926static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
927 struct request *req)
928{
929 struct mmc_blk_data *md = mq->data;
930 struct mmc_card *card = md->queue.card;
931 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300932 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700933
Maya Erez463bb952012-05-24 23:46:29 +0300934 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700935 err = -EOPNOTSUPP;
936 goto out;
937 }
938
939 from = blk_rq_pos(req);
940 nr = blk_rq_sectors(req);
941
942 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
943 arg = MMC_SECURE_TRIM1_ARG;
944 else
945 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300946retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500947 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
948 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
949 INAND_CMD38_ARG_EXT_CSD,
950 arg == MMC_SECURE_TRIM1_ARG ?
951 INAND_CMD38_ARG_SECTRIM1 :
952 INAND_CMD38_ARG_SECERASE,
953 0);
954 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300955 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500956 }
Adrian Hunter28302812012-04-05 14:45:48 +0300957
Adrian Hunter49804542010-08-11 14:17:50 -0700958 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300959 if (err == -EIO)
960 goto out_retry;
961 if (err)
962 goto out;
963
964 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500965 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
966 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
967 INAND_CMD38_ARG_EXT_CSD,
968 INAND_CMD38_ARG_SECTRIM2,
969 0);
970 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300971 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500972 }
Adrian Hunter28302812012-04-05 14:45:48 +0300973
Adrian Hunter49804542010-08-11 14:17:50 -0700974 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300975 if (err == -EIO)
976 goto out_retry;
977 if (err)
978 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500979 }
Adrian Hunter28302812012-04-05 14:45:48 +0300980
981 if (mmc_can_sanitize(card))
982 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
983 EXT_CSD_SANITIZE_START, 1, 0);
984out_retry:
985 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300986 goto retry;
987 if (!err)
988 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300989out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530990 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700991
Adrian Hunter49804542010-08-11 14:17:50 -0700992 return err ? 0 : 1;
993}
994
Maya Erez463bb952012-05-24 23:46:29 +0300995static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
996 struct request *req)
997{
998 struct mmc_blk_data *md = mq->data;
999 struct mmc_card *card = md->queue.card;
1000 int err = 0;
1001
1002 BUG_ON(!card);
1003 BUG_ON(!card->host);
1004
1005 if (!(mmc_can_sanitize(card) &&
1006 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1007 pr_warning("%s: %s - SANITIZE is not supported\n",
1008 mmc_hostname(card->host), __func__);
1009 err = -EOPNOTSUPP;
1010 goto out;
1011 }
1012
1013 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1014 mmc_hostname(card->host), __func__);
1015
1016 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001017 EXT_CSD_SANITIZE_START, 1,
1018 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001019
1020 if (err)
1021 pr_err("%s: %s - mmc_switch() with "
1022 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1023 mmc_hostname(card->host), __func__, err);
1024
1025 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1026 __func__);
1027
1028out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301029 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001030
1031 return err ? 0 : 1;
1032}
1033
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001034static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1035{
1036 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001037 struct mmc_card *card = md->queue.card;
1038 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001039
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001040 ret = mmc_flush_cache(card);
1041 if (ret)
1042 ret = -EIO;
1043
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301044 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001045
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001046 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001047}
1048
1049/*
1050 * Reformat current write as a reliable write, supporting
1051 * both legacy and the enhanced reliable write MMC cards.
1052 * In each transfer we'll handle only as much as a single
1053 * reliable write can handle, thus finish the request in
1054 * partial completions.
1055 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001056static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1057 struct mmc_card *card,
1058 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001059{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001060 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1061 /* Legacy mode imposes restrictions on transfers. */
1062 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1063 brq->data.blocks = 1;
1064
1065 if (brq->data.blocks > card->ext_csd.rel_sectors)
1066 brq->data.blocks = card->ext_csd.rel_sectors;
1067 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1068 brq->data.blocks = 1;
1069 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001070}
1071
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001072#define CMD_ERRORS \
1073 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1074 R1_ADDRESS_ERROR | /* Misaligned address */ \
1075 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1076 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1077 R1_CC_ERROR | /* Card controller error */ \
1078 R1_ERROR) /* General/unknown error */
1079
Per Forlinee8a43a2011-07-01 18:55:33 +02001080static int mmc_blk_err_check(struct mmc_card *card,
1081 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001082{
Per Forlinee8a43a2011-07-01 18:55:33 +02001083 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1084 mmc_active);
1085 struct mmc_blk_request *brq = &mq_mrq->brq;
1086 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001087 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001088
1089 /*
1090 * sbc.error indicates a problem with the set block count
1091 * command. No data will have been transferred.
1092 *
1093 * cmd.error indicates a problem with the r/w command. No
1094 * data will have been transferred.
1095 *
1096 * stop.error indicates a problem with the stop command. Data
1097 * may have been transferred, or may still be transferring.
1098 */
Adrian Hunter67716322011-08-29 16:42:15 +03001099 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1100 brq->data.error) {
1101 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001102 case ERR_RETRY:
1103 return MMC_BLK_RETRY;
1104 case ERR_ABORT:
1105 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301106 case ERR_NOMEDIUM:
1107 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001108 case ERR_CONTINUE:
1109 break;
1110 }
1111 }
1112
1113 /*
1114 * Check for errors relating to the execution of the
1115 * initial command - such as address errors. No data
1116 * has been transferred.
1117 */
1118 if (brq->cmd.resp[0] & CMD_ERRORS) {
1119 pr_err("%s: r/w command failed, status = %#x\n",
1120 req->rq_disk->disk_name, brq->cmd.resp[0]);
1121 return MMC_BLK_ABORT;
1122 }
1123
1124 /*
1125 * Everything else is either success, or a data error of some
1126 * kind. If it was a write, we may have transitioned to
1127 * program mode, which we have to wait for it to complete.
1128 */
1129 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1130 u32 status;
1131 do {
1132 int err = get_card_status(card, &status, 5);
1133 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301134 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001135 req->rq_disk->disk_name, err);
1136 return MMC_BLK_CMD_ERR;
1137 }
1138 /*
1139 * Some cards mishandle the status bits,
1140 * so make sure to check both the busy
1141 * indication and the card state.
1142 */
1143 } while (!(status & R1_READY_FOR_DATA) ||
1144 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1145 }
1146
1147 if (brq->data.error) {
1148 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1149 req->rq_disk->disk_name, brq->data.error,
1150 (unsigned)blk_rq_pos(req),
1151 (unsigned)blk_rq_sectors(req),
1152 brq->cmd.resp[0], brq->stop.resp[0]);
1153
1154 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001155 if (ecc_err)
1156 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001157 return MMC_BLK_DATA_ERR;
1158 } else {
1159 return MMC_BLK_CMD_ERR;
1160 }
1161 }
1162
Adrian Hunter67716322011-08-29 16:42:15 +03001163 if (!brq->data.bytes_xfered)
1164 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001165
Seungwon Jeon968c7742012-05-31 11:54:47 +03001166 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1167 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1168 return MMC_BLK_PARTIAL;
1169 else
1170 return MMC_BLK_SUCCESS;
1171 }
1172
Adrian Hunter67716322011-08-29 16:42:15 +03001173 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1174 return MMC_BLK_PARTIAL;
1175
1176 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001177}
1178
Seungwon Jeon968c7742012-05-31 11:54:47 +03001179static int mmc_blk_packed_err_check(struct mmc_card *card,
1180 struct mmc_async_req *areq)
1181{
1182 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1183 mmc_active);
1184 struct request *req = mq_rq->req;
1185 int err, check, status;
1186 u8 ext_csd[512];
1187
1188 check = mmc_blk_err_check(card, areq);
1189 err = get_card_status(card, &status, 0);
1190 if (err) {
1191 pr_err("%s: error %d sending status command\n",
1192 req->rq_disk->disk_name, err);
1193 return MMC_BLK_ABORT;
1194 }
1195
1196 if (status & R1_EXP_EVENT) {
1197 err = mmc_send_ext_csd(card, ext_csd);
1198 if (err) {
1199 pr_err("%s: error %d sending ext_csd\n",
1200 req->rq_disk->disk_name, err);
1201 return MMC_BLK_ABORT;
1202 }
1203
1204 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1205 EXT_CSD_PACKED_FAILURE) &&
1206 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1207 EXT_CSD_PACKED_GENERIC_ERROR)) {
1208 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1209 EXT_CSD_PACKED_INDEXED_ERROR) {
1210 mq_rq->packed_fail_idx =
1211 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1212 return MMC_BLK_PARTIAL;
1213 }
1214 }
1215 }
1216
1217 return check;
1218}
1219
Per Forlin54d49d772011-07-01 18:55:29 +02001220static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1221 struct mmc_card *card,
1222 int disable_multi,
1223 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Per Forlin54d49d772011-07-01 18:55:29 +02001225 u32 readcmd, writecmd;
1226 struct mmc_blk_request *brq = &mqrq->brq;
1227 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301229 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001231 /*
1232 * Reliable writes are used to implement Forced Unit Access and
1233 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001234 *
1235 * XXX: this really needs a good explanation of why REQ_META
1236 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001237 */
1238 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1239 (req->cmd_flags & REQ_META)) &&
1240 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001241 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001242
Per Forlin54d49d772011-07-01 18:55:29 +02001243 memset(brq, 0, sizeof(struct mmc_blk_request));
1244 brq->mrq.cmd = &brq->cmd;
1245 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Per Forlin54d49d772011-07-01 18:55:29 +02001247 brq->cmd.arg = blk_rq_pos(req);
1248 if (!mmc_card_blockaddr(card))
1249 brq->cmd.arg <<= 9;
1250 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1251 brq->data.blksz = 512;
1252 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1253 brq->stop.arg = 0;
1254 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1255 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Per Forlin54d49d772011-07-01 18:55:29 +02001257 /*
1258 * The block layer doesn't support all sector count
1259 * restrictions, so we need to be prepared for too big
1260 * requests.
1261 */
1262 if (brq->data.blocks > card->host->max_blk_count)
1263 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001265 if (brq->data.blocks > 1) {
1266 /*
1267 * After a read error, we redo the request one sector
1268 * at a time in order to accurately determine which
1269 * sectors can be read successfully.
1270 */
1271 if (disable_multi)
1272 brq->data.blocks = 1;
1273
1274 /* Some controllers can't do multiblock reads due to hw bugs */
1275 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1276 rq_data_dir(req) == READ)
1277 brq->data.blocks = 1;
1278 }
Per Forlin54d49d772011-07-01 18:55:29 +02001279
1280 if (brq->data.blocks > 1 || do_rel_wr) {
1281 /* SPI multiblock writes terminate using a special
1282 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001283 */
Per Forlin54d49d772011-07-01 18:55:29 +02001284 if (!mmc_host_is_spi(card->host) ||
1285 rq_data_dir(req) == READ)
1286 brq->mrq.stop = &brq->stop;
1287 readcmd = MMC_READ_MULTIPLE_BLOCK;
1288 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1289 } else {
1290 brq->mrq.stop = NULL;
1291 readcmd = MMC_READ_SINGLE_BLOCK;
1292 writecmd = MMC_WRITE_BLOCK;
1293 }
1294 if (rq_data_dir(req) == READ) {
1295 brq->cmd.opcode = readcmd;
1296 brq->data.flags |= MMC_DATA_READ;
1297 } else {
1298 brq->cmd.opcode = writecmd;
1299 brq->data.flags |= MMC_DATA_WRITE;
1300 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001301
Per Forlin54d49d772011-07-01 18:55:29 +02001302 if (do_rel_wr)
1303 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001304
Per Forlin54d49d772011-07-01 18:55:29 +02001305 /*
Saugata Das42659002011-12-21 13:09:17 +05301306 * Data tag is used only during writing meta data to speed
1307 * up write and any subsequent read of this meta data
1308 */
1309 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1310 (req->cmd_flags & REQ_META) &&
1311 (rq_data_dir(req) == WRITE) &&
1312 ((brq->data.blocks * brq->data.blksz) >=
1313 card->ext_csd.data_tag_unit_size);
1314
1315 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001316 * Pre-defined multi-block transfers are preferable to
1317 * open ended-ones (and necessary for reliable writes).
1318 * However, it is not sufficient to just send CMD23,
1319 * and avoid the final CMD12, as on an error condition
1320 * CMD12 (stop) needs to be sent anyway. This, coupled
1321 * with Auto-CMD23 enhancements provided by some
1322 * hosts, means that the complexity of dealing
1323 * with this is best left to the host. If CMD23 is
1324 * supported by card and host, we'll fill sbc in and let
1325 * the host deal with handling it correctly. This means
1326 * that for hosts that don't expose MMC_CAP_CMD23, no
1327 * change of behavior will be observed.
1328 *
1329 * N.B: Some MMC cards experience perf degradation.
1330 * We'll avoid using CMD23-bounded multiblock writes for
1331 * these, while retaining features like reliable writes.
1332 */
Saugata Das42659002011-12-21 13:09:17 +05301333 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1334 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1335 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001336 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1337 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301338 (do_rel_wr ? (1 << 31) : 0) |
1339 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001340 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1341 brq->mrq.sbc = &brq->sbc;
1342 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001343
Per Forlin54d49d772011-07-01 18:55:29 +02001344 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001345
Per Forlin54d49d772011-07-01 18:55:29 +02001346 brq->data.sg = mqrq->sg;
1347 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001348
Per Forlin54d49d772011-07-01 18:55:29 +02001349 /*
1350 * Adjust the sg list so it is the same size as the
1351 * request.
1352 */
1353 if (brq->data.blocks != blk_rq_sectors(req)) {
1354 int i, data_size = brq->data.blocks << 9;
1355 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001356
Per Forlin54d49d772011-07-01 18:55:29 +02001357 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1358 data_size -= sg->length;
1359 if (data_size <= 0) {
1360 sg->length += data_size;
1361 i++;
1362 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001363 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001364 }
Per Forlin54d49d772011-07-01 18:55:29 +02001365 brq->data.sg_len = i;
1366 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001367
Per Forlinee8a43a2011-07-01 18:55:33 +02001368 mqrq->mmc_active.mrq = &brq->mrq;
1369 mqrq->mmc_active.err_check = mmc_blk_err_check;
1370
Per Forlin54d49d772011-07-01 18:55:29 +02001371 mmc_queue_bounce_pre(mqrq);
1372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Maya Erez63c61d62012-05-31 21:00:18 +03001374static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1375 struct request *req)
1376{
1377 struct mmc_host *host = mq->card->host;
1378 int data_dir;
1379
1380 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1381 return;
1382
1383 /*
1384 * In case the packing control is not supported by the host, it should
1385 * not have an effect on the write packing. Therefore we have to enable
1386 * the write packing
1387 */
1388 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1389 mq->wr_packing_enabled = true;
1390 return;
1391 }
1392
1393 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1394 if (mq->num_of_potential_packed_wr_reqs >
1395 mq->num_wr_reqs_to_start_packing)
1396 mq->wr_packing_enabled = true;
1397 return;
1398 }
1399
1400 data_dir = rq_data_dir(req);
1401
1402 if (data_dir == READ) {
1403 mq->num_of_potential_packed_wr_reqs = 0;
1404 mq->wr_packing_enabled = false;
1405 return;
1406 } else if (data_dir == WRITE) {
1407 mq->num_of_potential_packed_wr_reqs++;
1408 }
1409
1410 if (mq->num_of_potential_packed_wr_reqs >
1411 mq->num_wr_reqs_to_start_packing)
1412 mq->wr_packing_enabled = true;
1413
1414}
1415
Maya Ereze544d7002012-06-04 06:09:47 +03001416struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1417{
1418 if (!card)
1419 return NULL;
1420
1421 return &card->wr_pack_stats;
1422}
1423EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1424
1425void mmc_blk_init_packed_statistics(struct mmc_card *card)
1426{
1427 int max_num_of_packed_reqs = 0;
1428
1429 if (!card || !card->wr_pack_stats.packing_events)
1430 return;
1431
1432 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1433
1434 spin_lock(&card->wr_pack_stats.lock);
1435 memset(card->wr_pack_stats.packing_events, 0,
1436 (max_num_of_packed_reqs + 1) *
1437 sizeof(*card->wr_pack_stats.packing_events));
1438 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1439 sizeof(card->wr_pack_stats.pack_stop_reason));
1440 card->wr_pack_stats.enabled = true;
1441 spin_unlock(&card->wr_pack_stats.lock);
1442}
1443EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1444
Seungwon Jeon968c7742012-05-31 11:54:47 +03001445static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1446{
1447 struct request_queue *q = mq->queue;
1448 struct mmc_card *card = mq->card;
1449 struct request *cur = req, *next = NULL;
1450 struct mmc_blk_data *md = mq->data;
1451 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1452 unsigned int req_sectors = 0, phys_segments = 0;
1453 unsigned int max_blk_count, max_phys_segs;
1454 u8 put_back = 0;
1455 u8 max_packed_rw = 0;
1456 u8 reqs = 0;
Maya Ereze544d7002012-06-04 06:09:47 +03001457 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001458
1459 mmc_blk_clear_packed(mq->mqrq_cur);
1460
1461 if (!(md->flags & MMC_BLK_CMD23) ||
1462 !card->ext_csd.packed_event_en)
1463 goto no_packed;
1464
Maya Erez63c61d62012-05-31 21:00:18 +03001465 if (!mq->wr_packing_enabled)
1466 goto no_packed;
1467
Seungwon Jeon968c7742012-05-31 11:54:47 +03001468 if ((rq_data_dir(cur) == WRITE) &&
1469 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1470 max_packed_rw = card->ext_csd.max_packed_writes;
1471
1472 if (max_packed_rw == 0)
1473 goto no_packed;
1474
1475 if (mmc_req_rel_wr(cur) &&
1476 (md->flags & MMC_BLK_REL_WR) &&
1477 !en_rel_wr) {
1478 goto no_packed;
1479 }
1480
1481 max_blk_count = min(card->host->max_blk_count,
1482 card->host->max_req_size >> 9);
1483 if (unlikely(max_blk_count > 0xffff))
1484 max_blk_count = 0xffff;
1485
1486 max_phys_segs = queue_max_segments(q);
1487 req_sectors += blk_rq_sectors(cur);
1488 phys_segments += cur->nr_phys_segments;
1489
1490 if (rq_data_dir(cur) == WRITE) {
1491 req_sectors++;
1492 phys_segments++;
1493 }
1494
Maya Ereze544d7002012-06-04 06:09:47 +03001495 spin_lock(&stats->lock);
1496
Seungwon Jeon968c7742012-05-31 11:54:47 +03001497 while (reqs < max_packed_rw - 1) {
1498 spin_lock_irq(q->queue_lock);
1499 next = blk_fetch_request(q);
1500 spin_unlock_irq(q->queue_lock);
Maya Ereze544d7002012-06-04 06:09:47 +03001501 if (!next) {
1502 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001503 break;
Maya Ereze544d7002012-06-04 06:09:47 +03001504 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001505
1506 if (next->cmd_flags & REQ_DISCARD ||
1507 next->cmd_flags & REQ_FLUSH) {
Maya Ereze544d7002012-06-04 06:09:47 +03001508 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001509 put_back = 1;
1510 break;
1511 }
1512
1513 if (rq_data_dir(cur) != rq_data_dir(next)) {
Maya Ereze544d7002012-06-04 06:09:47 +03001514 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001515 put_back = 1;
1516 break;
1517 }
1518
1519 if (mmc_req_rel_wr(next) &&
1520 (md->flags & MMC_BLK_REL_WR) &&
1521 !en_rel_wr) {
Maya Ereze544d7002012-06-04 06:09:47 +03001522 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001523 put_back = 1;
1524 break;
1525 }
1526
1527 req_sectors += blk_rq_sectors(next);
1528 if (req_sectors > max_blk_count) {
Maya Ereze544d7002012-06-04 06:09:47 +03001529 if (stats->enabled)
1530 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001531 put_back = 1;
1532 break;
1533 }
1534
1535 phys_segments += next->nr_phys_segments;
1536 if (phys_segments > max_phys_segs) {
Maya Ereze544d7002012-06-04 06:09:47 +03001537 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001538 put_back = 1;
1539 break;
1540 }
1541
Maya Erez63c61d62012-05-31 21:00:18 +03001542 if (rq_data_dir(next) == WRITE)
1543 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001544 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1545 cur = next;
1546 reqs++;
1547 }
1548
1549 if (put_back) {
1550 spin_lock_irq(q->queue_lock);
1551 blk_requeue_request(q, next);
1552 spin_unlock_irq(q->queue_lock);
1553 }
1554
Maya Ereze544d7002012-06-04 06:09:47 +03001555 if (stats->enabled) {
1556 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1557 stats->packing_events[reqs + 1]++;
1558 if (reqs + 1 == max_packed_rw)
1559 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1560 }
1561
1562 spin_unlock(&stats->lock);
1563
Seungwon Jeon968c7742012-05-31 11:54:47 +03001564 if (reqs > 0) {
1565 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1566 mq->mqrq_cur->packed_num = ++reqs;
1567 return reqs;
1568 }
1569
1570no_packed:
1571 mmc_blk_clear_packed(mq->mqrq_cur);
1572 return 0;
1573}
1574
1575static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1576 struct mmc_card *card,
1577 struct mmc_queue *mq)
1578{
1579 struct mmc_blk_request *brq = &mqrq->brq;
1580 struct request *req = mqrq->req;
1581 struct request *prq;
1582 struct mmc_blk_data *md = mq->data;
1583 bool do_rel_wr;
1584 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1585 u8 i = 1;
1586
1587 mqrq->packed_cmd = MMC_PACKED_WRITE;
1588 mqrq->packed_blocks = 0;
1589 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1590
1591 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1592 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1593 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1594
1595 /*
1596 * Argument for each entry of packed group
1597 */
1598 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1599 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1600 /* Argument of CMD23*/
1601 packed_cmd_hdr[(i * 2)] =
1602 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1603 blk_rq_sectors(prq);
1604 /* Argument of CMD18 or CMD25 */
1605 packed_cmd_hdr[((i * 2)) + 1] =
1606 mmc_card_blockaddr(card) ?
1607 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1608 mqrq->packed_blocks += blk_rq_sectors(prq);
1609 i++;
1610 }
1611
1612 memset(brq, 0, sizeof(struct mmc_blk_request));
1613 brq->mrq.cmd = &brq->cmd;
1614 brq->mrq.data = &brq->data;
1615 brq->mrq.sbc = &brq->sbc;
1616 brq->mrq.stop = &brq->stop;
1617
1618 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1619 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1620 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1621
1622 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1623 brq->cmd.arg = blk_rq_pos(req);
1624 if (!mmc_card_blockaddr(card))
1625 brq->cmd.arg <<= 9;
1626 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1627
1628 brq->data.blksz = 512;
1629 brq->data.blocks = mqrq->packed_blocks + 1;
1630 brq->data.flags |= MMC_DATA_WRITE;
1631
1632 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1633 brq->stop.arg = 0;
1634 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1635
1636 mmc_set_data_timeout(&brq->data, card);
1637
1638 brq->data.sg = mqrq->sg;
1639 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1640
1641 mqrq->mmc_active.mrq = &brq->mrq;
1642 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1643
1644 mmc_queue_bounce_pre(mqrq);
1645}
1646
Adrian Hunter67716322011-08-29 16:42:15 +03001647static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1648 struct mmc_blk_request *brq, struct request *req,
1649 int ret)
1650{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001651 struct mmc_queue_req *mq_rq;
1652 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1653
Adrian Hunter67716322011-08-29 16:42:15 +03001654 /*
1655 * If this is an SD card and we're writing, we can first
1656 * mark the known good sectors as ok.
1657 *
1658 * If the card is not SD, we can still ok written sectors
1659 * as reported by the controller (which might be less than
1660 * the real number of written sectors, but never more).
1661 */
1662 if (mmc_card_sd(card)) {
1663 u32 blocks;
1664
1665 blocks = mmc_sd_num_wr_blocks(card);
1666 if (blocks != (u32)-1) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301667 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001668 }
1669 } else {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001670 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301671 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001672 }
Adrian Hunter67716322011-08-29 16:42:15 +03001673 }
1674 return ret;
1675}
1676
Seungwon Jeon968c7742012-05-31 11:54:47 +03001677static int mmc_blk_end_packed_req(struct mmc_queue *mq,
1678 struct mmc_queue_req *mq_rq)
1679{
Seungwon Jeon968c7742012-05-31 11:54:47 +03001680 struct request *prq;
1681 int idx = mq_rq->packed_fail_idx, i = 0;
1682 int ret = 0;
1683
1684 while (!list_empty(&mq_rq->packed_list)) {
1685 prq = list_entry_rq(mq_rq->packed_list.next);
1686 if (idx == i) {
1687 /* retry from error index */
1688 mq_rq->packed_num -= idx;
1689 mq_rq->req = prq;
1690 ret = 1;
1691
1692 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1693 list_del_init(&prq->queuelist);
1694 mmc_blk_clear_packed(mq_rq);
1695 }
1696 return ret;
1697 }
1698 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301699 blk_end_request(prq, 0, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001700 i++;
1701 }
1702
1703 mmc_blk_clear_packed(mq_rq);
1704 return ret;
1705}
1706
Per Forlinee8a43a2011-07-01 18:55:33 +02001707static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001708{
1709 struct mmc_blk_data *md = mq->data;
1710 struct mmc_card *card = md->queue.card;
1711 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001712 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001713 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001714 struct mmc_queue_req *mq_rq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001715 struct request *req, *prq;
Per Forlinee8a43a2011-07-01 18:55:33 +02001716 struct mmc_async_req *areq;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001717 const u8 packed_num = 2;
1718 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001719
1720 if (!rqc && !mq->mqrq_prev->req)
1721 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001722
Seungwon Jeon968c7742012-05-31 11:54:47 +03001723 if (rqc)
1724 reqs = mmc_blk_prep_packed_list(mq, rqc);
1725
Per Forlin54d49d772011-07-01 18:55:29 +02001726 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001727 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001728 if (reqs >= packed_num)
1729 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1730 card, mq);
1731 else
1732 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001733 areq = &mq->mqrq_cur->mmc_active;
1734 } else
1735 areq = NULL;
1736 areq = mmc_start_req(card->host, areq, (int *) &status);
1737 if (!areq)
1738 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001739
Per Forlinee8a43a2011-07-01 18:55:33 +02001740 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1741 brq = &mq_rq->brq;
1742 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001743 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001744 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001745
Jaehoon Chungf886c802012-05-28 10:33:35 +03001746 /*
1747 * Check BKOPS urgency from each R1 response
1748 */
1749 if (mmc_card_mmc(card) &&
1750 (brq->cmd.resp[0] & R1_EXCEPTION_EVENT))
1751 mmc_card_set_check_bkops(card);
1752
Per Forlind78d4a82011-07-01 18:55:30 +02001753 switch (status) {
1754 case MMC_BLK_SUCCESS:
1755 case MMC_BLK_PARTIAL:
1756 /*
1757 * A block was successfully transferred.
1758 */
Adrian Hunter67716322011-08-29 16:42:15 +03001759 mmc_blk_reset_success(md, type);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001760
1761 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1762 ret = mmc_blk_end_packed_req(mq, mq_rq);
1763 break;
1764 } else {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301765 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001766 brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001767 }
1768
Adrian Hunter67716322011-08-29 16:42:15 +03001769 /*
1770 * If the blk_end_request function returns non-zero even
1771 * though all data has been transferred and no errors
1772 * were returned by the host controller, it's a bug.
1773 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001774 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301775 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001776 __func__, blk_rq_bytes(req),
1777 brq->data.bytes_xfered);
1778 rqc = NULL;
1779 goto cmd_abort;
1780 }
Per Forlind78d4a82011-07-01 18:55:30 +02001781 break;
1782 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001783 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1784 if (!mmc_blk_reset(md, card->host, type))
1785 break;
1786 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001787 case MMC_BLK_RETRY:
1788 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001789 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001790 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001791 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001792 if (!mmc_blk_reset(md, card->host, type))
1793 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001794 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001795 case MMC_BLK_DATA_ERR: {
1796 int err;
1797
1798 err = mmc_blk_reset(md, card->host, type);
1799 if (!err)
1800 break;
Seungwon Jeon968c7742012-05-31 11:54:47 +03001801 if (err == -ENODEV ||
1802 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001803 goto cmd_abort;
1804 /* Fall through */
1805 }
1806 case MMC_BLK_ECC_ERR:
1807 if (brq->data.blocks > 1) {
1808 /* Redo read one sector at a time */
1809 pr_warning("%s: retrying using single block read\n",
1810 req->rq_disk->disk_name);
1811 disable_multi = 1;
1812 break;
1813 }
Per Forlind78d4a82011-07-01 18:55:30 +02001814 /*
1815 * After an error, we redo I/O one sector at a
1816 * time, so we only reach here after trying to
1817 * read a single sector.
1818 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301819 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001820 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001821 if (!ret)
1822 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001823 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301824 case MMC_BLK_NOMEDIUM:
1825 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001826 }
1827
Per Forlinee8a43a2011-07-01 18:55:33 +02001828 if (ret) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001829 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1830 /*
1831 * In case of a incomplete request
1832 * prepare it again and resend.
1833 */
1834 mmc_blk_rw_rq_prep(mq_rq, card,
1835 disable_multi, mq);
1836 mmc_start_req(card->host,
1837 &mq_rq->mmc_active, NULL);
1838 } else {
1839 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1840 mmc_start_req(card->host,
1841 &mq_rq->mmc_active, NULL);
1842 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 } while (ret);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 1;
1847
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001848 cmd_abort:
Seungwon Jeon968c7742012-05-31 11:54:47 +03001849 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001850 if (mmc_card_removed(card))
1851 req->cmd_flags |= REQ_QUIET;
1852 while (ret)
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301853 ret = blk_end_request(req, -EIO,
Seungwon Jeon968c7742012-05-31 11:54:47 +03001854 blk_rq_cur_bytes(req));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001855 } else {
1856 while (!list_empty(&mq_rq->packed_list)) {
1857 prq = list_entry_rq(mq_rq->packed_list.next);
1858 list_del_init(&prq->queuelist);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301859 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
Seungwon Jeon968c7742012-05-31 11:54:47 +03001860 }
1861 mmc_blk_clear_packed(mq_rq);
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Per Forlinee8a43a2011-07-01 18:55:33 +02001864 start_new_req:
1865 if (rqc) {
Seungwon Jeon968c7742012-05-31 11:54:47 +03001866 /*
1867 * If current request is packed, it needs to put back.
1868 */
1869 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE) {
1870 while (!list_empty(&mq->mqrq_cur->packed_list)) {
1871 prq = list_entry_rq(
1872 mq->mqrq_cur->packed_list.prev);
1873 if (prq->queuelist.prev !=
1874 &mq->mqrq_cur->packed_list) {
1875 list_del_init(&prq->queuelist);
1876 spin_lock_irq(mq->queue->queue_lock);
1877 blk_requeue_request(mq->queue, prq);
1878 spin_unlock_irq(mq->queue->queue_lock);
1879 } else {
1880 list_del_init(&prq->queuelist);
1881 }
1882 }
1883 mmc_blk_clear_packed(mq->mqrq_cur);
1884 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001885 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1886 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1887 }
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 0;
1890}
1891
Adrian Hunterbd788c92010-08-11 14:17:47 -07001892static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1893{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001894 int ret;
1895 struct mmc_blk_data *md = mq->data;
1896 struct mmc_card *card = md->queue.card;
1897
San Mehat148c57a2009-07-30 08:21:19 -07001898#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1899 if (mmc_bus_needs_resume(card->host)) {
1900 mmc_resume_bus(card->host);
1901 mmc_blk_set_blksize(md, card);
1902 }
1903#endif
1904
Per Forlinee8a43a2011-07-01 18:55:33 +02001905 if (req && !mq->mqrq_prev->req)
1906 /* claim host only for the first request */
1907 mmc_claim_host(card->host);
1908
Andrei Warkentin371a6892011-04-11 18:10:25 -05001909 ret = mmc_blk_part_switch(card, md);
1910 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001911 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301912 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001913 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001914 ret = 0;
1915 goto out;
1916 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001917
Maya Erez63c61d62012-05-31 21:00:18 +03001918 mmc_blk_write_packing_control(mq, req);
1919
Maya Erez463bb952012-05-24 23:46:29 +03001920 if (req && req->cmd_flags & REQ_SANITIZE) {
1921 /* complete ongoing async transfer before issuing sanitize */
1922 if (card->host && card->host->areq)
1923 mmc_blk_issue_rw_rq(mq, NULL);
1924 ret = mmc_blk_issue_sanitize_rq(mq, req);
1925 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001926 /* complete ongoing async transfer before issuing discard */
1927 if (card->host->areq)
1928 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001929 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001930 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001931 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001932 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001933 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001934 /* complete ongoing async transfer before issuing flush */
1935 if (card->host->areq)
1936 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001937 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001938 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001939 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001940 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001941
Andrei Warkentin371a6892011-04-11 18:10:25 -05001942out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001943 if (!req)
1944 /* release host only when there are no more requests */
1945 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001946 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Russell Kinga6f6c962006-01-03 22:38:44 +00001949static inline int mmc_blk_readonly(struct mmc_card *card)
1950{
1951 return mmc_card_readonly(card) ||
1952 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1953}
1954
Andrei Warkentin371a6892011-04-11 18:10:25 -05001955static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1956 struct device *parent,
1957 sector_t size,
1958 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001959 const char *subname,
1960 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 struct mmc_blk_data *md;
1963 int devidx, ret;
1964
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001965 devidx = find_first_zero_bit(dev_use, max_devices);
1966 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return ERR_PTR(-ENOSPC);
1968 __set_bit(devidx, dev_use);
1969
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001970 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001971 if (!md) {
1972 ret = -ENOMEM;
1973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001975
Russell Kinga6f6c962006-01-03 22:38:44 +00001976 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001977 * !subname implies we are creating main mmc_blk_data that will be
1978 * associated with mmc_card with mmc_set_drvdata. Due to device
1979 * partitions, devidx will not coincide with a per-physical card
1980 * index anymore so we keep track of a name index.
1981 */
1982 if (!subname) {
1983 md->name_idx = find_first_zero_bit(name_use, max_devices);
1984 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001985 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001986 md->name_idx = ((struct mmc_blk_data *)
1987 dev_to_disk(parent)->private_data)->name_idx;
1988
Johan Rudholmadd710e2011-12-02 08:51:06 +01001989 md->area_type = area_type;
1990
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001991 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001992 * Set the read-only status based on the supported commands
1993 * and the write protect switch.
1994 */
1995 md->read_only = mmc_blk_readonly(card);
1996
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001997 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001998 if (md->disk == NULL) {
1999 ret = -ENOMEM;
2000 goto err_kfree;
2001 }
2002
2003 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002004 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002005 md->usage = 1;
2006
Adrian Hunterd09408a2011-06-23 13:40:28 +03002007 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002008 if (ret)
2009 goto err_putdisk;
2010
Russell Kinga6f6c962006-01-03 22:38:44 +00002011 md->queue.issue_fn = mmc_blk_issue_rq;
2012 md->queue.data = md;
2013
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002014 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002015 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002016 md->disk->fops = &mmc_bdops;
2017 md->disk->private_data = md;
2018 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002019 md->disk->driverfs_dev = parent;
2020 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002021 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002022
2023 /*
2024 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2025 *
2026 * - be set for removable media with permanent block devices
2027 * - be unset for removable block devices with permanent media
2028 *
2029 * Since MMC block devices clearly fall under the second
2030 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2031 * should use the block device creation/destruction hotplug
2032 * messages to tell when the card is present.
2033 */
2034
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002035 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2036 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002037
Martin K. Petersene1defc42009-05-22 17:17:49 -04002038 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002039 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002040
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002041 if (mmc_host_cmd23(card->host)) {
2042 if (mmc_card_mmc(card) ||
2043 (mmc_card_sd(card) &&
2044 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2045 md->flags |= MMC_BLK_CMD23;
2046 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002047
2048 if (mmc_card_mmc(card) &&
2049 md->flags & MMC_BLK_CMD23 &&
2050 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2051 card->ext_csd.rel_sectors)) {
2052 md->flags |= MMC_BLK_REL_WR;
2053 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2054 }
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002057
2058 err_putdisk:
2059 put_disk(md->disk);
2060 err_kfree:
2061 kfree(md);
2062 out:
2063 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Andrei Warkentin371a6892011-04-11 18:10:25 -05002066static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2067{
2068 sector_t size;
2069 struct mmc_blk_data *md;
2070
2071 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2072 /*
2073 * The EXT_CSD sector count is in number or 512 byte
2074 * sectors.
2075 */
2076 size = card->ext_csd.sectors;
2077 } else {
2078 /*
2079 * The CSD capacity field is in units of read_blkbits.
2080 * set_capacity takes units of 512 bytes.
2081 */
2082 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2083 }
2084
Johan Rudholmadd710e2011-12-02 08:51:06 +01002085 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2086 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002087 return md;
2088}
2089
2090static int mmc_blk_alloc_part(struct mmc_card *card,
2091 struct mmc_blk_data *md,
2092 unsigned int part_type,
2093 sector_t size,
2094 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002095 const char *subname,
2096 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002097{
2098 char cap_str[10];
2099 struct mmc_blk_data *part_md;
2100
2101 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002102 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002103 if (IS_ERR(part_md))
2104 return PTR_ERR(part_md);
2105 part_md->part_type = part_type;
2106 list_add(&part_md->part, &md->part);
2107
2108 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2109 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302110 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002111 part_md->disk->disk_name, mmc_card_id(card),
2112 mmc_card_name(card), part_md->part_type, cap_str);
2113 return 0;
2114}
2115
Namjae Jeone0c368d2011-10-06 23:41:38 +09002116/* MMC Physical partitions consist of two boot partitions and
2117 * up to four general purpose partitions.
2118 * For each partition enabled in EXT_CSD a block device will be allocatedi
2119 * to provide access to the partition.
2120 */
2121
Andrei Warkentin371a6892011-04-11 18:10:25 -05002122static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2123{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002124 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002125
2126 if (!mmc_card_mmc(card))
2127 return 0;
2128
Namjae Jeone0c368d2011-10-06 23:41:38 +09002129 for (idx = 0; idx < card->nr_parts; idx++) {
2130 if (card->part[idx].size) {
2131 ret = mmc_blk_alloc_part(card, md,
2132 card->part[idx].part_cfg,
2133 card->part[idx].size >> 9,
2134 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002135 card->part[idx].name,
2136 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002137 if (ret)
2138 return ret;
2139 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002140 }
2141
2142 return ret;
2143}
2144
Andrei Warkentin371a6892011-04-11 18:10:25 -05002145static void mmc_blk_remove_req(struct mmc_blk_data *md)
2146{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002147 struct mmc_card *card;
2148
Andrei Warkentin371a6892011-04-11 18:10:25 -05002149 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002150 card = md->queue.card;
Maya Erez63c61d62012-05-31 21:00:18 +03002151 device_remove_file(disk_to_dev(md->disk),
2152 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002153 if (md->disk->flags & GENHD_FL_UP) {
2154 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002155 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2156 card->ext_csd.boot_ro_lockable)
2157 device_remove_file(disk_to_dev(md->disk),
2158 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002159
2160 /* Stop new requests from getting into the queue */
2161 del_gendisk(md->disk);
2162 }
2163
2164 /* Then flush out any already in there */
2165 mmc_cleanup_queue(&md->queue);
2166 mmc_blk_put(md);
2167 }
2168}
2169
2170static void mmc_blk_remove_parts(struct mmc_card *card,
2171 struct mmc_blk_data *md)
2172{
2173 struct list_head *pos, *q;
2174 struct mmc_blk_data *part_md;
2175
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002176 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002177 list_for_each_safe(pos, q, &md->part) {
2178 part_md = list_entry(pos, struct mmc_blk_data, part);
2179 list_del(pos);
2180 mmc_blk_remove_req(part_md);
2181 }
2182}
2183
2184static int mmc_add_disk(struct mmc_blk_data *md)
2185{
2186 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002187 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002188
2189 add_disk(md->disk);
2190 md->force_ro.show = force_ro_show;
2191 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302192 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002193 md->force_ro.attr.name = "force_ro";
2194 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2195 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2196 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002197 goto force_ro_fail;
2198
2199 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2200 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002201 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002202
2203 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2204 mode = S_IRUGO;
2205 else
2206 mode = S_IRUGO | S_IWUSR;
2207
2208 md->power_ro_lock.show = power_ro_lock_show;
2209 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002210 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002211 md->power_ro_lock.attr.mode = mode;
2212 md->power_ro_lock.attr.name =
2213 "ro_lock_until_next_power_on";
2214 ret = device_create_file(disk_to_dev(md->disk),
2215 &md->power_ro_lock);
2216 if (ret)
2217 goto power_ro_lock_fail;
2218 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002219
Maya Erez63c61d62012-05-31 21:00:18 +03002220 md->num_wr_reqs_to_start_packing.show =
2221 num_wr_reqs_to_start_packing_show;
2222 md->num_wr_reqs_to_start_packing.store =
2223 num_wr_reqs_to_start_packing_store;
2224 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2225 md->num_wr_reqs_to_start_packing.attr.name =
2226 "num_wr_reqs_to_start_packing";
2227 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2228 ret = device_create_file(disk_to_dev(md->disk),
2229 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002230 if (ret)
2231 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002232
Johan Rudholmadd710e2011-12-02 08:51:06 +01002233 return ret;
2234
2235power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002236 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002237force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002238 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002239
2240 return ret;
2241}
2242
Chris Ballc59d4472011-11-11 22:01:43 -05002243#define CID_MANFID_SANDISK 0x2
2244#define CID_MANFID_TOSHIBA 0x11
2245#define CID_MANFID_MICRON 0x13
2246
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002247static const struct mmc_fixup blk_fixups[] =
2248{
Chris Ballc59d4472011-11-11 22:01:43 -05002249 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2250 MMC_QUIRK_INAND_CMD38),
2251 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2252 MMC_QUIRK_INAND_CMD38),
2253 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2254 MMC_QUIRK_INAND_CMD38),
2255 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2256 MMC_QUIRK_INAND_CMD38),
2257 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2258 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002259
2260 /*
2261 * Some MMC cards experience performance degradation with CMD23
2262 * instead of CMD12-bounded multiblock transfers. For now we'll
2263 * black list what's bad...
2264 * - Certain Toshiba cards.
2265 *
2266 * N.B. This doesn't affect SD cards.
2267 */
Chris Ballc59d4472011-11-11 22:01:43 -05002268 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002269 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002270 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002271 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002272 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002273 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002274
2275 /*
2276 * Some Micron MMC cards needs longer data read timeout than
2277 * indicated in CSD.
2278 */
Chris Ballc59d4472011-11-11 22:01:43 -05002279 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002280 MMC_QUIRK_LONG_READ_TIME),
2281
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302282 /* Some INAND MCP devices advertise incorrect timeout values */
2283 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2284 MMC_QUIRK_INAND_DATA_TIMEOUT),
2285
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002286 END_FIXUP
2287};
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289static int mmc_blk_probe(struct mmc_card *card)
2290{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002291 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002292 char cap_str[10];
2293
Pierre Ossman912490d2005-05-21 10:27:02 +01002294 /*
2295 * Check that the card supports the command class(es) we need.
2296 */
2297 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return -ENODEV;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 md = mmc_blk_alloc(card);
2301 if (IS_ERR(md))
2302 return PTR_ERR(md);
2303
Yi Li444122f2009-02-05 15:31:57 +08002304 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002305 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302306 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002308 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Andrei Warkentin371a6892011-04-11 18:10:25 -05002310 if (mmc_blk_alloc_parts(card, md))
2311 goto out;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002314 mmc_fixup_device(card, blk_fixups);
2315
San Mehat148c57a2009-07-30 08:21:19 -07002316#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2317 mmc_set_bus_resume_policy(card->host, 1);
2318#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002319 if (mmc_add_disk(md))
2320 goto out;
2321
2322 list_for_each_entry(part_md, &md->part, part) {
2323 if (mmc_add_disk(part_md))
2324 goto out;
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
2327
2328 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002329 mmc_blk_remove_parts(card, md);
2330 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334static void mmc_blk_remove(struct mmc_card *card)
2335{
2336 struct mmc_blk_data *md = mmc_get_drvdata(card);
2337
Andrei Warkentin371a6892011-04-11 18:10:25 -05002338 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002339 mmc_claim_host(card->host);
2340 mmc_blk_part_switch(card, md);
2341 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002342 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002344#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2345 mmc_set_bus_resume_policy(card->host, 0);
2346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002350static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002352 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct mmc_blk_data *md = mmc_get_drvdata(card);
2354
2355 if (md) {
2356 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002357 list_for_each_entry(part_md, &md->part, part) {
2358 mmc_queue_suspend(&part_md->queue);
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 return 0;
2362}
2363
2364static int mmc_blk_resume(struct mmc_card *card)
2365{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002366 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct mmc_blk_data *md = mmc_get_drvdata(card);
2368
2369 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002370 /*
2371 * Resume involves the card going into idle state,
2372 * so current partition is always the main one.
2373 */
2374 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002376 list_for_each_entry(part_md, &md->part, part) {
2377 mmc_queue_resume(&part_md->queue);
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 return 0;
2381}
2382#else
2383#define mmc_blk_suspend NULL
2384#define mmc_blk_resume NULL
2385#endif
2386
2387static struct mmc_driver mmc_driver = {
2388 .drv = {
2389 .name = "mmcblk",
2390 },
2391 .probe = mmc_blk_probe,
2392 .remove = mmc_blk_remove,
2393 .suspend = mmc_blk_suspend,
2394 .resume = mmc_blk_resume,
2395};
2396
2397static int __init mmc_blk_init(void)
2398{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002399 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002401 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2402 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2403
2404 max_devices = 256 / perdev_minors;
2405
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002406 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2407 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002410 res = mmc_register_driver(&mmc_driver);
2411 if (res)
2412 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002414 return 0;
2415 out2:
2416 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 out:
2418 return res;
2419}
2420
2421static void __exit mmc_blk_exit(void)
2422{
2423 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002424 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
2427module_init(mmc_blk_init);
2428module_exit(mmc_blk_exit);
2429
2430MODULE_LICENSE("GPL");
2431MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2432