blob: 3348aef1e73e6eee733bb9b571a45f8c66d96e61 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon121b8e82012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman10217bc2012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon121b8e82012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez860cef92013-01-16 09:48:25 +0200124 struct device_attribute bkops_check_threshold;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200130enum {
131 MMC_PACKED_N_IDX = -1,
132 MMC_PACKED_N_ZERO,
133 MMC_PACKED_N_SINGLE,
134};
135
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400136module_param(perdev_minors, int, 0444);
137MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
138
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200139static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
140{
141 mqrq->packed_cmd = MMC_PACKED_NONE;
142 mqrq->packed_num = MMC_PACKED_N_ZERO;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
146{
147 struct mmc_blk_data *md;
148
Arjan van de Vena621aae2006-01-12 18:43:35 +0000149 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 md = disk->private_data;
151 if (md && md->usage == 0)
152 md = NULL;
153 if (md)
154 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return md;
158}
159
Andrei Warkentin371a6892011-04-11 18:10:25 -0500160static inline int mmc_get_devidx(struct gendisk *disk)
161{
Colin Cross71b54d42010-09-03 12:41:21 -0700162 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500163 return devidx;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void mmc_blk_put(struct mmc_blk_data *md)
167{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000168 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 md->usage--;
170 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500171 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800172 blk_cleanup_queue(md->queue.queue);
173
David Woodhouse1dff3142007-11-21 18:45:12 +0100174 __clear_bit(devidx, dev_use);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(md);
178 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000179 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Johan Rudholmadd710e2011-12-02 08:51:06 +0100182static ssize_t power_ro_lock_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
185 int ret;
186 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
187 struct mmc_card *card = md->queue.card;
188 int locked = 0;
189
190 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
191 locked = 2;
192 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
193 locked = 1;
194
195 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
196
197 return ret;
198}
199
200static ssize_t power_ro_lock_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count)
202{
203 int ret;
204 struct mmc_blk_data *md, *part_md;
205 struct mmc_card *card;
206 unsigned long set;
207
208 if (kstrtoul(buf, 0, &set))
209 return -EINVAL;
210
211 if (set != 1)
212 return count;
213
214 md = mmc_blk_get(dev_to_disk(dev));
215 card = md->queue.card;
216
217 mmc_claim_host(card->host);
218
219 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
220 card->ext_csd.boot_ro_lock |
221 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
222 card->ext_csd.part_time);
223 if (ret)
224 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
225 else
226 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
227
228 mmc_release_host(card->host);
229
230 if (!ret) {
231 pr_info("%s: Locking boot partition ro until next power on\n",
232 md->disk->disk_name);
233 set_disk_ro(md->disk, 1);
234
235 list_for_each_entry(part_md, &md->part, part)
236 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
237 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
238 set_disk_ro(part_md->disk, 1);
239 }
240 }
241
242 mmc_blk_put(md);
243 return count;
244}
245
Andrei Warkentin371a6892011-04-11 18:10:25 -0500246static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
247 char *buf)
248{
249 int ret;
250 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
251
252 ret = snprintf(buf, PAGE_SIZE, "%d",
253 get_disk_ro(dev_to_disk(dev)) ^
254 md->read_only);
255 mmc_blk_put(md);
256 return ret;
257}
258
259static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
260 const char *buf, size_t count)
261{
262 int ret;
263 char *end;
264 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
265 unsigned long set = simple_strtoul(buf, &end, 0);
266 if (end == buf) {
267 ret = -EINVAL;
268 goto out;
269 }
270
271 set_disk_ro(dev_to_disk(dev), set || md->read_only);
272 ret = count;
273out:
274 mmc_blk_put(md);
275 return ret;
276}
277
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200278static ssize_t
279num_wr_reqs_to_start_packing_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
283 int num_wr_reqs_to_start_packing;
284 int ret;
285
286 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
287
288 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
289
290 mmc_blk_put(md);
291 return ret;
292}
293
294static ssize_t
295num_wr_reqs_to_start_packing_store(struct device *dev,
296 struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 int value;
300 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
301
302 sscanf(buf, "%d", &value);
303 if (value >= 0)
304 md->queue.num_wr_reqs_to_start_packing = value;
305
306 mmc_blk_put(md);
307 return count;
308}
309
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200310static ssize_t
Maya Erez860cef92013-01-16 09:48:25 +0200311bkops_check_threshold_show(struct device *dev,
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200312 struct device_attribute *attr, char *buf)
313{
314 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200315 struct mmc_card *card = md->queue.card;
316 int ret;
317
318 if (!card)
Maya Erez860cef92013-01-16 09:48:25 +0200319 ret = -EINVAL;
320 else
321 ret = snprintf(buf, PAGE_SIZE, "%d\n",
322 card->bkops_info.size_percentage_to_queue_delayed_work);
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200323
324 mmc_blk_put(md);
325 return ret;
326}
327
328static ssize_t
Maya Erez860cef92013-01-16 09:48:25 +0200329bkops_check_threshold_store(struct device *dev,
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200330 struct device_attribute *attr,
331 const char *buf, size_t count)
332{
333 int value;
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 struct mmc_card *card = md->queue.card;
Maya Erez860cef92013-01-16 09:48:25 +0200336 unsigned int card_size;
337 int ret = count;
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200338
Maya Erez860cef92013-01-16 09:48:25 +0200339 if (!card) {
340 ret = -EINVAL;
341 goto exit;
342 }
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200343
344 sscanf(buf, "%d", &value);
Maya Erez860cef92013-01-16 09:48:25 +0200345 if ((value <= 0) || (value >= 100)) {
346 ret = -EINVAL;
347 goto exit;
348 }
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200349
Maya Erez860cef92013-01-16 09:48:25 +0200350 card_size = (unsigned int)get_capacity(md->disk);
351 if (card_size <= 0) {
352 ret = -EINVAL;
353 goto exit;
354 }
355 card->bkops_info.size_percentage_to_queue_delayed_work = value;
356 card->bkops_info.min_sectors_to_queue_delayed_work =
357 (card_size * value) / 100;
358
359 pr_debug("%s: size_percentage = %d, min_sectors = %d",
360 mmc_hostname(card->host),
361 card->bkops_info.size_percentage_to_queue_delayed_work,
362 card->bkops_info.min_sectors_to_queue_delayed_work);
363
364exit:
Yaniv Gardiab0b6b02012-10-11 11:36:24 +0200365 mmc_blk_put(md);
366 return count;
367}
368
Al Viroa5a15612008-03-02 10:33:30 -0500369static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Al Viroa5a15612008-03-02 10:33:30 -0500371 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int ret = -ENXIO;
373
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200374 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (md) {
376 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500377 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700379
Al Viroa5a15612008-03-02 10:33:30 -0500380 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700381 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700382 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200385 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return ret;
388}
389
Al Viroa5a15612008-03-02 10:33:30 -0500390static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Al Viroa5a15612008-03-02 10:33:30 -0500392 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200394 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200396 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398}
399
400static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800401mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800403 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
404 geo->heads = 4;
405 geo->sectors = 16;
406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
John Calixtocb87ea22011-04-26 18:56:29 -0400409struct mmc_blk_ioc_data {
410 struct mmc_ioc_cmd ic;
411 unsigned char *buf;
412 u64 buf_bytes;
413};
414
415static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
416 struct mmc_ioc_cmd __user *user)
417{
418 struct mmc_blk_ioc_data *idata;
419 int err;
420
421 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
422 if (!idata) {
423 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400424 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400425 }
426
427 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
428 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400429 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400430 }
431
432 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
433 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
434 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400435 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400436 }
437
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100438 if (!idata->buf_bytes)
439 return idata;
440
John Calixtocb87ea22011-04-26 18:56:29 -0400441 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
442 if (!idata->buf) {
443 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400444 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400445 }
446
447 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
448 idata->ic.data_ptr, idata->buf_bytes)) {
449 err = -EFAULT;
450 goto copy_err;
451 }
452
453 return idata;
454
455copy_err:
456 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400457idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400458 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400459out:
John Calixtocb87ea22011-04-26 18:56:29 -0400460 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400461}
462
463static int mmc_blk_ioctl_cmd(struct block_device *bdev,
464 struct mmc_ioc_cmd __user *ic_ptr)
465{
466 struct mmc_blk_ioc_data *idata;
467 struct mmc_blk_data *md;
468 struct mmc_card *card;
469 struct mmc_command cmd = {0};
470 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530471 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400472 struct scatterlist sg;
473 int err;
474
475 /*
476 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
477 * whole block device, not on a partition. This prevents overspray
478 * between sibling partitions.
479 */
480 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
481 return -EPERM;
482
483 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
484 if (IS_ERR(idata))
485 return PTR_ERR(idata);
486
John Calixtocb87ea22011-04-26 18:56:29 -0400487 md = mmc_blk_get(bdev->bd_disk);
488 if (!md) {
489 err = -EINVAL;
490 goto cmd_done;
491 }
492
493 card = md->queue.card;
494 if (IS_ERR(card)) {
495 err = PTR_ERR(card);
496 goto cmd_done;
497 }
498
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100499 cmd.opcode = idata->ic.opcode;
500 cmd.arg = idata->ic.arg;
501 cmd.flags = idata->ic.flags;
502
503 if (idata->buf_bytes) {
504 data.sg = &sg;
505 data.sg_len = 1;
506 data.blksz = idata->ic.blksz;
507 data.blocks = idata->ic.blocks;
508
509 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
510
511 if (idata->ic.write_flag)
512 data.flags = MMC_DATA_WRITE;
513 else
514 data.flags = MMC_DATA_READ;
515
516 /* data.flags must already be set before doing this. */
517 mmc_set_data_timeout(&data, card);
518
519 /* Allow overriding the timeout_ns for empirical tuning. */
520 if (idata->ic.data_timeout_ns)
521 data.timeout_ns = idata->ic.data_timeout_ns;
522
523 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
524 /*
525 * Pretend this is a data transfer and rely on the
526 * host driver to compute timeout. When all host
527 * drivers support cmd.cmd_timeout for R1B, this
528 * can be changed to:
529 *
530 * mrq.data = NULL;
531 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
532 */
533 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
534 }
535
536 mrq.data = &data;
537 }
538
539 mrq.cmd = &cmd;
540
John Calixtocb87ea22011-04-26 18:56:29 -0400541 mmc_claim_host(card->host);
542
543 if (idata->ic.is_acmd) {
544 err = mmc_app_cmd(card->host, card);
545 if (err)
546 goto cmd_rel_host;
547 }
548
John Calixtocb87ea22011-04-26 18:56:29 -0400549 mmc_wait_for_req(card->host, &mrq);
550
551 if (cmd.error) {
552 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
553 __func__, cmd.error);
554 err = cmd.error;
555 goto cmd_rel_host;
556 }
557 if (data.error) {
558 dev_err(mmc_dev(card->host), "%s: data error %d\n",
559 __func__, data.error);
560 err = data.error;
561 goto cmd_rel_host;
562 }
563
564 /*
565 * According to the SD specs, some commands require a delay after
566 * issuing the command.
567 */
568 if (idata->ic.postsleep_min_us)
569 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
570
571 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
572 err = -EFAULT;
573 goto cmd_rel_host;
574 }
575
576 if (!idata->ic.write_flag) {
577 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
578 idata->buf, idata->buf_bytes)) {
579 err = -EFAULT;
580 goto cmd_rel_host;
581 }
582 }
583
584cmd_rel_host:
585 mmc_release_host(card->host);
586
587cmd_done:
588 mmc_blk_put(md);
589 kfree(idata->buf);
590 kfree(idata);
591 return err;
592}
593
594static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
595 unsigned int cmd, unsigned long arg)
596{
597 int ret = -EINVAL;
598 if (cmd == MMC_IOC_CMD)
599 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
600 return ret;
601}
602
603#ifdef CONFIG_COMPAT
604static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
605 unsigned int cmd, unsigned long arg)
606{
607 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
608}
609#endif
610
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700611static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500612 .open = mmc_blk_open,
613 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800614 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400616 .ioctl = mmc_blk_ioctl,
617#ifdef CONFIG_COMPAT
618 .compat_ioctl = mmc_blk_compat_ioctl,
619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620};
621
Andrei Warkentin371a6892011-04-11 18:10:25 -0500622static inline int mmc_blk_part_switch(struct mmc_card *card,
623 struct mmc_blk_data *md)
624{
625 int ret;
626 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300627
Andrei Warkentin371a6892011-04-11 18:10:25 -0500628 if (main_md->part_curr == md->part_type)
629 return 0;
630
631 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300632 u8 part_config = card->ext_csd.part_config;
633
634 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
635 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500636
637 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300638 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500639 card->ext_csd.part_time);
640 if (ret)
641 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300642
643 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300644 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500645
646 main_md->part_curr = md->part_type;
647 return 0;
648}
649
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700650static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
651{
652 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100653 u32 result;
654 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700655
Venkatraman Sad5fd972011-08-25 00:30:50 +0530656 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400657 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400658 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700659
660 struct scatterlist sg;
661
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700662 cmd.opcode = MMC_APP_CMD;
663 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700664 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700665
666 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700667 if (err)
668 return (u32)-1;
669 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700670 return (u32)-1;
671
672 memset(&cmd, 0, sizeof(struct mmc_command));
673
674 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
675 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700676 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700677
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700678 data.blksz = 4;
679 data.blocks = 1;
680 data.flags = MMC_DATA_READ;
681 data.sg = &sg;
682 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530683 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700684
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700685 mrq.cmd = &cmd;
686 mrq.data = &data;
687
Ben Dooks051913d2009-06-08 23:33:57 +0100688 blocks = kmalloc(4, GFP_KERNEL);
689 if (!blocks)
690 return (u32)-1;
691
692 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700693
694 mmc_wait_for_req(card->host, &mrq);
695
Ben Dooks051913d2009-06-08 23:33:57 +0100696 result = ntohl(*blocks);
697 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700698
Ben Dooks051913d2009-06-08 23:33:57 +0100699 if (cmd.error || data.error)
700 result = (u32)-1;
701
702 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700703}
704
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100705static int send_stop(struct mmc_card *card, u32 *status)
706{
707 struct mmc_command cmd = {0};
708 int err;
709
710 cmd.opcode = MMC_STOP_TRANSMISSION;
711 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
712 err = mmc_wait_for_cmd(card->host, &cmd, 5);
713 if (err == 0)
714 *status = cmd.resp[0];
715 return err;
716}
717
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100718static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300719{
Chris Ball1278dba2011-04-13 23:40:30 -0400720 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300721 int err;
722
Adrian Hunter504f1912008-10-16 12:55:25 +0300723 cmd.opcode = MMC_SEND_STATUS;
724 if (!mmc_host_is_spi(card->host))
725 cmd.arg = card->rca << 16;
726 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100727 err = mmc_wait_for_cmd(card->host, &cmd, retries);
728 if (err == 0)
729 *status = cmd.resp[0];
730 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300731}
732
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530733#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100734#define ERR_RETRY 2
735#define ERR_ABORT 1
736#define ERR_CONTINUE 0
737
738static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
739 bool status_valid, u32 status)
740{
741 switch (error) {
742 case -EILSEQ:
743 /* response crc error, retry the r/w cmd */
744 pr_err("%s: %s sending %s command, card status %#x\n",
745 req->rq_disk->disk_name, "response CRC error",
746 name, status);
747 return ERR_RETRY;
748
749 case -ETIMEDOUT:
750 pr_err("%s: %s sending %s command, card status %#x\n",
751 req->rq_disk->disk_name, "timed out", name, status);
752
753 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700754 if (!status_valid) {
755 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100756 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700757 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100758 /*
759 * If it was a r/w cmd crc error, or illegal command
760 * (eg, issued in wrong state) then retry - we should
761 * have corrected the state problem above.
762 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700763 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
764 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100765 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700766 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100767
768 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700769 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100770 return ERR_ABORT;
771
772 default:
773 /* We don't understand the error code the driver gave us */
774 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
775 req->rq_disk->disk_name, error, status);
776 return ERR_ABORT;
777 }
778}
779
780/*
781 * Initial r/w and stop cmd error recovery.
782 * We don't know whether the card received the r/w cmd or not, so try to
783 * restore things back to a sane state. Essentially, we do this as follows:
784 * - Obtain card status. If the first attempt to obtain card status fails,
785 * the status word will reflect the failed status cmd, not the failed
786 * r/w cmd. If we fail to obtain card status, it suggests we can no
787 * longer communicate with the card.
788 * - Check the card state. If the card received the cmd but there was a
789 * transient problem with the response, it might still be in a data transfer
790 * mode. Try to send it a stop command. If this fails, we can't recover.
791 * - If the r/w cmd failed due to a response CRC error, it was probably
792 * transient, so retry the cmd.
793 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
794 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
795 * illegal cmd, retry.
796 * Otherwise we don't understand what happened, so abort.
797 */
798static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300799 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100800{
801 bool prev_cmd_status_valid = true;
802 u32 status, stop_status = 0;
803 int err, retry;
804
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530805 if (mmc_card_removed(card))
806 return ERR_NOMEDIUM;
807
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100808 /*
809 * Try to get card status which indicates both the card state
810 * and why there was no response. If the first attempt fails,
811 * we can't be sure the returned status is for the r/w command.
812 */
813 for (retry = 2; retry >= 0; retry--) {
814 err = get_card_status(card, &status, 0);
815 if (!err)
816 break;
817
818 prev_cmd_status_valid = false;
819 pr_err("%s: error %d sending status command, %sing\n",
820 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
821 }
822
823 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530824 if (err) {
825 /* Check if the card is removed */
826 if (mmc_detect_card_removed(card->host))
827 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100828 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530829 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100830
Adrian Hunter67716322011-08-29 16:42:15 +0300831 /* Flag ECC errors */
832 if ((status & R1_CARD_ECC_FAILED) ||
833 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
834 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
835 *ecc_err = 1;
836
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100837 /*
838 * Check the current card state. If it is in some data transfer
839 * mode, tell it to stop (and hopefully transition back to TRAN.)
840 */
841 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
842 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
843 err = send_stop(card, &stop_status);
844 if (err)
845 pr_err("%s: error %d sending stop command\n",
846 req->rq_disk->disk_name, err);
847
848 /*
849 * If the stop cmd also timed out, the card is probably
850 * not present, so abort. Other errors are bad news too.
851 */
852 if (err)
853 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300854 if (stop_status & R1_CARD_ECC_FAILED)
855 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100856 }
857
858 /* Check for set block count errors */
859 if (brq->sbc.error)
860 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
861 prev_cmd_status_valid, status);
862
863 /* Check for r/w command errors */
864 if (brq->cmd.error)
865 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
866 prev_cmd_status_valid, status);
867
Adrian Hunter67716322011-08-29 16:42:15 +0300868 /* Data errors */
869 if (!brq->stop.error)
870 return ERR_CONTINUE;
871
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100872 /* Now for stop errors. These aren't fatal to the transfer. */
873 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
874 req->rq_disk->disk_name, brq->stop.error,
875 brq->cmd.resp[0], status);
876
877 /*
878 * Subsitute in our own stop status as this will give the error
879 * state which happened during the execution of the r/w command.
880 */
881 if (stop_status) {
882 brq->stop.resp[0] = stop_status;
883 brq->stop.error = 0;
884 }
885 return ERR_CONTINUE;
886}
887
Adrian Hunter67716322011-08-29 16:42:15 +0300888static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
889 int type)
890{
891 int err;
892
893 if (md->reset_done & type)
894 return -EEXIST;
895
896 md->reset_done |= type;
897 err = mmc_hw_reset(host);
898 /* Ensure we switch back to the correct partition */
899 if (err != -EOPNOTSUPP) {
900 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
901 int part_err;
902
903 main_md->part_curr = main_md->part_type;
904 part_err = mmc_blk_part_switch(host->card, md);
905 if (part_err) {
906 /*
907 * We have failed to get back into the correct
908 * partition, so we need to abort the whole request.
909 */
910 return -ENODEV;
911 }
912 }
913 return err;
914}
915
916static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
917{
918 md->reset_done &= ~type;
919}
920
Adrian Hunterbd788c92010-08-11 14:17:47 -0700921static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
922{
923 struct mmc_blk_data *md = mq->data;
924 struct mmc_card *card = md->queue.card;
925 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300926 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700927
Adrian Hunterbd788c92010-08-11 14:17:47 -0700928 if (!mmc_can_erase(card)) {
929 err = -EOPNOTSUPP;
930 goto out;
931 }
932
933 from = blk_rq_pos(req);
934 nr = blk_rq_sectors(req);
935
Maya Erez5c46dad2012-10-10 03:47:54 +0200936 if (card->ext_csd.bkops_en)
937 card->bkops_info.sectors_changed += blk_rq_sectors(req);
938
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900939 if (mmc_can_discard(card))
940 arg = MMC_DISCARD_ARG;
941 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700942 arg = MMC_TRIM_ARG;
943 else
944 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300945retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
947 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
948 INAND_CMD38_ARG_EXT_CSD,
949 arg == MMC_TRIM_ARG ?
950 INAND_CMD38_ARG_TRIM :
951 INAND_CMD38_ARG_ERASE,
952 0);
953 if (err)
954 goto out;
955 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700956 err = mmc_erase(card, from, nr, arg);
957out:
Adrian Hunter67716322011-08-29 16:42:15 +0300958 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
959 goto retry;
960 if (!err)
961 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530962 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700963
Adrian Hunterbd788c92010-08-11 14:17:47 -0700964 return err ? 0 : 1;
965}
966
Adrian Hunter49804542010-08-11 14:17:50 -0700967static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
968 struct request *req)
969{
970 struct mmc_blk_data *md = mq->data;
971 struct mmc_card *card = md->queue.card;
972 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300973 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700974
Maya Erez463bb952012-05-24 23:46:29 +0300975 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700976 err = -EOPNOTSUPP;
977 goto out;
978 }
979
980 from = blk_rq_pos(req);
981 nr = blk_rq_sectors(req);
982
983 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
984 arg = MMC_SECURE_TRIM1_ARG;
985 else
986 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300987retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500988 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
989 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
990 INAND_CMD38_ARG_EXT_CSD,
991 arg == MMC_SECURE_TRIM1_ARG ?
992 INAND_CMD38_ARG_SECTRIM1 :
993 INAND_CMD38_ARG_SECERASE,
994 0);
995 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300996 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500997 }
Adrian Hunter28302812012-04-05 14:45:48 +0300998
Adrian Hunter49804542010-08-11 14:17:50 -0700999 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001000 if (err == -EIO)
1001 goto out_retry;
1002 if (err)
1003 goto out;
1004
1005 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001006 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1007 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1008 INAND_CMD38_ARG_EXT_CSD,
1009 INAND_CMD38_ARG_SECTRIM2,
1010 0);
1011 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001012 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001013 }
Adrian Hunter28302812012-04-05 14:45:48 +03001014
Adrian Hunter49804542010-08-11 14:17:50 -07001015 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001016 if (err == -EIO)
1017 goto out_retry;
1018 if (err)
1019 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001020 }
Adrian Hunter28302812012-04-05 14:45:48 +03001021
Adrian Hunter28302812012-04-05 14:45:48 +03001022out_retry:
1023 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001024 goto retry;
1025 if (!err)
1026 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001027out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301028 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001029
Adrian Hunter49804542010-08-11 14:17:50 -07001030 return err ? 0 : 1;
1031}
1032
Maya Erez463bb952012-05-24 23:46:29 +03001033static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1034 struct request *req)
1035{
1036 struct mmc_blk_data *md = mq->data;
1037 struct mmc_card *card = md->queue.card;
1038 int err = 0;
1039
1040 BUG_ON(!card);
1041 BUG_ON(!card->host);
1042
1043 if (!(mmc_can_sanitize(card) &&
1044 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1045 pr_warning("%s: %s - SANITIZE is not supported\n",
1046 mmc_hostname(card->host), __func__);
1047 err = -EOPNOTSUPP;
1048 goto out;
1049 }
1050
1051 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1052 mmc_hostname(card->host), __func__);
1053
1054 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001055 EXT_CSD_SANITIZE_START, 1,
1056 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001057
1058 if (err)
1059 pr_err("%s: %s - mmc_switch() with "
1060 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1061 mmc_hostname(card->host), __func__, err);
1062
1063 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1064 __func__);
1065
1066out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301067 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001068
1069 return err ? 0 : 1;
1070}
1071
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001072static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1073{
1074 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001075 struct mmc_card *card = md->queue.card;
1076 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001077
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001078 ret = mmc_flush_cache(card);
1079 if (ret)
1080 ret = -EIO;
1081
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301082 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001083
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001084 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001085}
1086
1087/*
1088 * Reformat current write as a reliable write, supporting
1089 * both legacy and the enhanced reliable write MMC cards.
1090 * In each transfer we'll handle only as much as a single
1091 * reliable write can handle, thus finish the request in
1092 * partial completions.
1093 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001094static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1095 struct mmc_card *card,
1096 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001097{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001098 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1099 /* Legacy mode imposes restrictions on transfers. */
1100 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1101 brq->data.blocks = 1;
1102
1103 if (brq->data.blocks > card->ext_csd.rel_sectors)
1104 brq->data.blocks = card->ext_csd.rel_sectors;
1105 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1106 brq->data.blocks = 1;
1107 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001108}
1109
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001110#define CMD_ERRORS \
1111 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1112 R1_ADDRESS_ERROR | /* Misaligned address */ \
1113 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1114 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1115 R1_CC_ERROR | /* Card controller error */ \
1116 R1_ERROR) /* General/unknown error */
1117
Per Forlinee8a43a2011-07-01 18:55:33 +02001118static int mmc_blk_err_check(struct mmc_card *card,
1119 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001120{
Per Forlinee8a43a2011-07-01 18:55:33 +02001121 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1122 mmc_active);
1123 struct mmc_blk_request *brq = &mq_mrq->brq;
1124 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001125 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001126
1127 /*
1128 * sbc.error indicates a problem with the set block count
1129 * command. No data will have been transferred.
1130 *
1131 * cmd.error indicates a problem with the r/w command. No
1132 * data will have been transferred.
1133 *
1134 * stop.error indicates a problem with the stop command. Data
1135 * may have been transferred, or may still be transferring.
1136 */
Adrian Hunter67716322011-08-29 16:42:15 +03001137 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1138 brq->data.error) {
1139 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001140 case ERR_RETRY:
1141 return MMC_BLK_RETRY;
1142 case ERR_ABORT:
1143 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301144 case ERR_NOMEDIUM:
1145 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001146 case ERR_CONTINUE:
1147 break;
1148 }
1149 }
1150
1151 /*
1152 * Check for errors relating to the execution of the
1153 * initial command - such as address errors. No data
1154 * has been transferred.
1155 */
1156 if (brq->cmd.resp[0] & CMD_ERRORS) {
1157 pr_err("%s: r/w command failed, status = %#x\n",
1158 req->rq_disk->disk_name, brq->cmd.resp[0]);
1159 return MMC_BLK_ABORT;
1160 }
1161
1162 /*
1163 * Everything else is either success, or a data error of some
1164 * kind. If it was a write, we may have transitioned to
1165 * program mode, which we have to wait for it to complete.
1166 */
1167 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1168 u32 status;
1169 do {
1170 int err = get_card_status(card, &status, 5);
1171 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301172 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001173 req->rq_disk->disk_name, err);
1174 return MMC_BLK_CMD_ERR;
1175 }
1176 /*
1177 * Some cards mishandle the status bits,
1178 * so make sure to check both the busy
1179 * indication and the card state.
1180 */
1181 } while (!(status & R1_READY_FOR_DATA) ||
1182 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1183 }
1184
1185 if (brq->data.error) {
1186 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1187 req->rq_disk->disk_name, brq->data.error,
1188 (unsigned)blk_rq_pos(req),
1189 (unsigned)blk_rq_sectors(req),
1190 brq->cmd.resp[0], brq->stop.resp[0]);
1191
1192 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001193 if (ecc_err)
1194 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001195 return MMC_BLK_DATA_ERR;
1196 } else {
1197 return MMC_BLK_CMD_ERR;
1198 }
1199 }
1200
Adrian Hunter67716322011-08-29 16:42:15 +03001201 if (!brq->data.bytes_xfered)
1202 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001203
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001204 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1205 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1206 return MMC_BLK_PARTIAL;
1207 else
1208 return MMC_BLK_SUCCESS;
1209 }
1210
Adrian Hunter67716322011-08-29 16:42:15 +03001211 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1212 return MMC_BLK_PARTIAL;
1213
1214 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001215}
1216
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001217static int mmc_blk_packed_err_check(struct mmc_card *card,
1218 struct mmc_async_req *areq)
1219{
1220 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1221 mmc_active);
1222 struct request *req = mq_rq->req;
1223 int err, check, status;
1224 u8 ext_csd[512];
1225
1226 mq_rq->packed_retries--;
1227 check = mmc_blk_err_check(card, areq);
1228 err = get_card_status(card, &status, 0);
1229 if (err) {
1230 pr_err("%s: error %d sending status command\n",
1231 req->rq_disk->disk_name, err);
1232 return MMC_BLK_ABORT;
1233 }
1234
1235 if (status & R1_EXCEPTION_EVENT) {
1236 err = mmc_send_ext_csd(card, ext_csd);
1237 if (err) {
1238 pr_err("%s: error %d sending ext_csd\n",
1239 req->rq_disk->disk_name, err);
1240 return MMC_BLK_ABORT;
1241 }
1242
1243 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1244 EXT_CSD_PACKED_FAILURE) &&
1245 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1246 EXT_CSD_PACKED_GENERIC_ERROR)) {
1247 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1248 EXT_CSD_PACKED_INDEXED_ERROR) {
1249 mq_rq->packed_fail_idx =
1250 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1251 return MMC_BLK_PARTIAL;
1252 }
1253 }
1254 }
1255
1256 return check;
1257}
1258
Per Forlin54d49d772011-07-01 18:55:29 +02001259static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1260 struct mmc_card *card,
1261 int disable_multi,
1262 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Per Forlin54d49d772011-07-01 18:55:29 +02001264 u32 readcmd, writecmd;
1265 struct mmc_blk_request *brq = &mqrq->brq;
1266 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301268 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001270 /*
1271 * Reliable writes are used to implement Forced Unit Access and
1272 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001273 *
1274 * XXX: this really needs a good explanation of why REQ_META
1275 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001276 */
1277 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1278 (req->cmd_flags & REQ_META)) &&
1279 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001280 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001281
Per Forlin54d49d772011-07-01 18:55:29 +02001282 memset(brq, 0, sizeof(struct mmc_blk_request));
1283 brq->mrq.cmd = &brq->cmd;
1284 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Per Forlin54d49d772011-07-01 18:55:29 +02001286 brq->cmd.arg = blk_rq_pos(req);
1287 if (!mmc_card_blockaddr(card))
1288 brq->cmd.arg <<= 9;
1289 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1290 brq->data.blksz = 512;
1291 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1292 brq->stop.arg = 0;
1293 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1294 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Asutosh Das1a897142012-07-27 18:10:19 +05301296 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001297 /*
1298 * The block layer doesn't support all sector count
1299 * restrictions, so we need to be prepared for too big
1300 * requests.
1301 */
1302 if (brq->data.blocks > card->host->max_blk_count)
1303 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001305 if (brq->data.blocks > 1) {
1306 /*
1307 * After a read error, we redo the request one sector
1308 * at a time in order to accurately determine which
1309 * sectors can be read successfully.
1310 */
1311 if (disable_multi)
1312 brq->data.blocks = 1;
1313
1314 /* Some controllers can't do multiblock reads due to hw bugs */
1315 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1316 rq_data_dir(req) == READ)
1317 brq->data.blocks = 1;
1318 }
Per Forlin54d49d772011-07-01 18:55:29 +02001319
1320 if (brq->data.blocks > 1 || do_rel_wr) {
1321 /* SPI multiblock writes terminate using a special
1322 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001323 */
Per Forlin54d49d772011-07-01 18:55:29 +02001324 if (!mmc_host_is_spi(card->host) ||
1325 rq_data_dir(req) == READ)
1326 brq->mrq.stop = &brq->stop;
1327 readcmd = MMC_READ_MULTIPLE_BLOCK;
1328 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1329 } else {
1330 brq->mrq.stop = NULL;
1331 readcmd = MMC_READ_SINGLE_BLOCK;
1332 writecmd = MMC_WRITE_BLOCK;
1333 }
1334 if (rq_data_dir(req) == READ) {
1335 brq->cmd.opcode = readcmd;
1336 brq->data.flags |= MMC_DATA_READ;
1337 } else {
1338 brq->cmd.opcode = writecmd;
1339 brq->data.flags |= MMC_DATA_WRITE;
1340 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001341
Per Forlin54d49d772011-07-01 18:55:29 +02001342 if (do_rel_wr)
1343 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001344
Per Forlin54d49d772011-07-01 18:55:29 +02001345 /*
Saugata Das42659002011-12-21 13:09:17 +05301346 * Data tag is used only during writing meta data to speed
1347 * up write and any subsequent read of this meta data
1348 */
1349 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1350 (req->cmd_flags & REQ_META) &&
1351 (rq_data_dir(req) == WRITE) &&
1352 ((brq->data.blocks * brq->data.blksz) >=
1353 card->ext_csd.data_tag_unit_size);
1354
1355 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001356 * Pre-defined multi-block transfers are preferable to
1357 * open ended-ones (and necessary for reliable writes).
1358 * However, it is not sufficient to just send CMD23,
1359 * and avoid the final CMD12, as on an error condition
1360 * CMD12 (stop) needs to be sent anyway. This, coupled
1361 * with Auto-CMD23 enhancements provided by some
1362 * hosts, means that the complexity of dealing
1363 * with this is best left to the host. If CMD23 is
1364 * supported by card and host, we'll fill sbc in and let
1365 * the host deal with handling it correctly. This means
1366 * that for hosts that don't expose MMC_CAP_CMD23, no
1367 * change of behavior will be observed.
1368 *
1369 * N.B: Some MMC cards experience perf degradation.
1370 * We'll avoid using CMD23-bounded multiblock writes for
1371 * these, while retaining features like reliable writes.
1372 */
Saugata Das42659002011-12-21 13:09:17 +05301373 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1374 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1375 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001376 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1377 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301378 (do_rel_wr ? (1 << 31) : 0) |
1379 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001380 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1381 brq->mrq.sbc = &brq->sbc;
1382 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001383
Per Forlin54d49d772011-07-01 18:55:29 +02001384 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001385
Per Forlin54d49d772011-07-01 18:55:29 +02001386 brq->data.sg = mqrq->sg;
1387 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001388
Per Forlin54d49d772011-07-01 18:55:29 +02001389 /*
1390 * Adjust the sg list so it is the same size as the
1391 * request.
1392 */
1393 if (brq->data.blocks != blk_rq_sectors(req)) {
1394 int i, data_size = brq->data.blocks << 9;
1395 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001396
Per Forlin54d49d772011-07-01 18:55:29 +02001397 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1398 data_size -= sg->length;
1399 if (data_size <= 0) {
1400 sg->length += data_size;
1401 i++;
1402 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001403 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001404 }
Per Forlin54d49d772011-07-01 18:55:29 +02001405 brq->data.sg_len = i;
1406 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001407
Per Forlinee8a43a2011-07-01 18:55:33 +02001408 mqrq->mmc_active.mrq = &brq->mrq;
1409 mqrq->mmc_active.err_check = mmc_blk_err_check;
1410
Per Forlin54d49d772011-07-01 18:55:29 +02001411 mmc_queue_bounce_pre(mqrq);
1412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001414static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1415 struct request *req)
1416{
1417 struct mmc_host *host = mq->card->host;
1418 int data_dir;
1419
1420 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1421 return;
1422
1423 /*
1424 * In case the packing control is not supported by the host, it should
1425 * not have an effect on the write packing. Therefore we have to enable
1426 * the write packing
1427 */
1428 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1429 mq->wr_packing_enabled = true;
1430 return;
1431 }
1432
1433 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1434 if (mq->num_of_potential_packed_wr_reqs >
1435 mq->num_wr_reqs_to_start_packing)
1436 mq->wr_packing_enabled = true;
Tatyana Brokhmanc9989122012-10-07 10:26:27 +02001437 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001438 return;
1439 }
1440
1441 data_dir = rq_data_dir(req);
1442
1443 if (data_dir == READ) {
1444 mq->num_of_potential_packed_wr_reqs = 0;
1445 mq->wr_packing_enabled = false;
1446 return;
1447 } else if (data_dir == WRITE) {
1448 mq->num_of_potential_packed_wr_reqs++;
1449 }
1450
1451 if (mq->num_of_potential_packed_wr_reqs >
1452 mq->num_wr_reqs_to_start_packing)
1453 mq->wr_packing_enabled = true;
1454
1455}
1456
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001457struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1458{
1459 if (!card)
1460 return NULL;
1461
1462 return &card->wr_pack_stats;
1463}
1464EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1465
1466void mmc_blk_init_packed_statistics(struct mmc_card *card)
1467{
1468 int max_num_of_packed_reqs = 0;
1469
1470 if (!card || !card->wr_pack_stats.packing_events)
1471 return;
1472
1473 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1474
1475 spin_lock(&card->wr_pack_stats.lock);
1476 memset(card->wr_pack_stats.packing_events, 0,
1477 (max_num_of_packed_reqs + 1) *
1478 sizeof(*card->wr_pack_stats.packing_events));
1479 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1480 sizeof(card->wr_pack_stats.pack_stop_reason));
1481 card->wr_pack_stats.enabled = true;
1482 spin_unlock(&card->wr_pack_stats.lock);
1483}
1484EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1485
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001486void print_mmc_packing_stats(struct mmc_card *card)
1487{
1488 int i;
1489 int max_num_of_packed_reqs = 0;
1490
1491 if ((!card) || (!card->wr_pack_stats.packing_events))
1492 return;
1493
1494 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1495
1496 spin_lock(&card->wr_pack_stats.lock);
1497
1498 pr_info("%s: write packing statistics:\n",
1499 mmc_hostname(card->host));
1500
1501 for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) {
1502 if (card->wr_pack_stats.packing_events[i] != 0)
1503 pr_info("%s: Packed %d reqs - %d times\n",
1504 mmc_hostname(card->host), i,
1505 card->wr_pack_stats.packing_events[i]);
1506 }
1507
1508 pr_info("%s: stopped packing due to the following reasons:\n",
1509 mmc_hostname(card->host));
1510
1511 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS])
1512 pr_info("%s: %d times: exceedmax num of segments\n",
1513 mmc_hostname(card->host),
1514 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]);
1515 if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS])
1516 pr_info("%s: %d times: exceeding the max num of sectors\n",
1517 mmc_hostname(card->host),
1518 card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]);
1519 if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR])
1520 pr_info("%s: %d times: wrong data direction\n",
1521 mmc_hostname(card->host),
1522 card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]);
1523 if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD])
1524 pr_info("%s: %d times: flush or discard\n",
1525 mmc_hostname(card->host),
1526 card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]);
1527 if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE])
1528 pr_info("%s: %d times: empty queue\n",
1529 mmc_hostname(card->host),
1530 card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]);
1531 if (card->wr_pack_stats.pack_stop_reason[REL_WRITE])
1532 pr_info("%s: %d times: rel write\n",
1533 mmc_hostname(card->host),
1534 card->wr_pack_stats.pack_stop_reason[REL_WRITE]);
1535 if (card->wr_pack_stats.pack_stop_reason[THRESHOLD])
1536 pr_info("%s: %d times: Threshold\n",
1537 mmc_hostname(card->host),
1538 card->wr_pack_stats.pack_stop_reason[THRESHOLD]);
1539
1540 spin_unlock(&card->wr_pack_stats.lock);
1541}
1542EXPORT_SYMBOL(print_mmc_packing_stats);
1543
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001544static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1545{
1546 struct request_queue *q = mq->queue;
1547 struct mmc_card *card = mq->card;
1548 struct request *cur = req, *next = NULL;
1549 struct mmc_blk_data *md = mq->data;
1550 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1551 unsigned int req_sectors = 0, phys_segments = 0;
1552 unsigned int max_blk_count, max_phys_segs;
1553 u8 put_back = 0;
1554 u8 max_packed_rw = 0;
1555 u8 reqs = 0;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001556 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001557
1558 mmc_blk_clear_packed(mq->mqrq_cur);
1559
1560 if (!(md->flags & MMC_BLK_CMD23) ||
1561 !card->ext_csd.packed_event_en)
1562 goto no_packed;
1563
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001564 if (!mq->wr_packing_enabled)
1565 goto no_packed;
1566
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001567 if ((rq_data_dir(cur) == WRITE) &&
1568 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1569 max_packed_rw = card->ext_csd.max_packed_writes;
1570
1571 if (max_packed_rw == 0)
1572 goto no_packed;
1573
1574 if (mmc_req_rel_wr(cur) &&
1575 (md->flags & MMC_BLK_REL_WR) &&
1576 !en_rel_wr)
1577 goto no_packed;
1578
1579 if (mmc_large_sec(card) &&
1580 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1581 goto no_packed;
1582
1583 max_blk_count = min(card->host->max_blk_count,
1584 card->host->max_req_size >> 9);
1585 if (unlikely(max_blk_count > 0xffff))
1586 max_blk_count = 0xffff;
1587
1588 max_phys_segs = queue_max_segments(q);
1589 req_sectors += blk_rq_sectors(cur);
1590 phys_segments += cur->nr_phys_segments;
1591
1592 if (rq_data_dir(cur) == WRITE) {
1593 req_sectors++;
1594 phys_segments++;
1595 }
1596
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001597 spin_lock(&stats->lock);
1598
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001599 while (reqs < max_packed_rw - 1) {
1600 spin_lock_irq(q->queue_lock);
1601 next = blk_fetch_request(q);
1602 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001603 if (!next) {
1604 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001605 break;
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001606 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001607
1608 if (mmc_large_sec(card) &&
1609 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhmana07f7712012-10-04 11:11:08 +02001610 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001611 put_back = 1;
1612 break;
1613 }
1614
1615 if (next->cmd_flags & REQ_DISCARD ||
1616 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001617 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001618 put_back = 1;
1619 break;
1620 }
1621
1622 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001623 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001624 put_back = 1;
1625 break;
1626 }
1627
1628 if (mmc_req_rel_wr(next) &&
1629 (md->flags & MMC_BLK_REL_WR) &&
1630 !en_rel_wr) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001631 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001632 put_back = 1;
1633 break;
1634 }
1635
1636 req_sectors += blk_rq_sectors(next);
1637 if (req_sectors > max_blk_count) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001638 if (stats->enabled)
1639 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001640 put_back = 1;
1641 break;
1642 }
1643
1644 phys_segments += next->nr_phys_segments;
1645 if (phys_segments > max_phys_segs) {
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001646 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001647 put_back = 1;
1648 break;
1649 }
1650
Maya Erez5c46dad2012-10-10 03:47:54 +02001651 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001652 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5c46dad2012-10-10 03:47:54 +02001653 if (card->ext_csd.bkops_en)
1654 card->bkops_info.sectors_changed +=
1655 blk_rq_sectors(next);
1656 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001657 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1658 cur = next;
1659 reqs++;
1660 }
1661
1662 if (put_back) {
1663 spin_lock_irq(q->queue_lock);
1664 blk_requeue_request(q, next);
1665 spin_unlock_irq(q->queue_lock);
1666 }
1667
Tatyana Brokhman10217bc2012-10-07 10:33:13 +02001668 if (stats->enabled) {
1669 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1670 stats->packing_events[reqs + 1]++;
1671 if (reqs + 1 == max_packed_rw)
1672 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1673 }
1674
1675 spin_unlock(&stats->lock);
1676
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001677 if (reqs > 0) {
1678 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1679 mq->mqrq_cur->packed_num = ++reqs;
1680 mq->mqrq_cur->packed_retries = reqs;
1681 return reqs;
1682 }
1683
1684no_packed:
1685 mmc_blk_clear_packed(mq->mqrq_cur);
1686 return 0;
1687}
1688
1689static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1690 struct mmc_card *card,
1691 struct mmc_queue *mq)
1692{
1693 struct mmc_blk_request *brq = &mqrq->brq;
1694 struct request *req = mqrq->req;
1695 struct request *prq;
1696 struct mmc_blk_data *md = mq->data;
1697 bool do_rel_wr, do_data_tag;
1698 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1699 u8 i = 1;
1700
1701 mqrq->packed_cmd = MMC_PACKED_WRITE;
1702 mqrq->packed_blocks = 0;
1703 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1704
1705 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1706 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1707 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1708
1709 /*
1710 * Argument for each entry of packed group
1711 */
1712 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1713 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1714 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1715 (prq->cmd_flags & REQ_META) &&
1716 (rq_data_dir(prq) == WRITE) &&
1717 ((brq->data.blocks * brq->data.blksz) >=
1718 card->ext_csd.data_tag_unit_size);
1719 /* Argument of CMD23 */
1720 packed_cmd_hdr[(i * 2)] =
1721 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1722 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1723 blk_rq_sectors(prq);
1724 /* Argument of CMD18 or CMD25 */
1725 packed_cmd_hdr[((i * 2)) + 1] =
1726 mmc_card_blockaddr(card) ?
1727 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1728 mqrq->packed_blocks += blk_rq_sectors(prq);
1729 i++;
1730 }
1731
1732 memset(brq, 0, sizeof(struct mmc_blk_request));
1733 brq->mrq.cmd = &brq->cmd;
1734 brq->mrq.data = &brq->data;
1735 brq->mrq.sbc = &brq->sbc;
1736 brq->mrq.stop = &brq->stop;
1737
1738 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1739 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1740 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1741
1742 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1743 brq->cmd.arg = blk_rq_pos(req);
1744 if (!mmc_card_blockaddr(card))
1745 brq->cmd.arg <<= 9;
1746 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1747
1748 brq->data.blksz = 512;
1749 brq->data.blocks = mqrq->packed_blocks + 1;
1750 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez867be742012-10-29 20:19:01 +02001751 brq->data.fault_injected = false;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001752
1753 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1754 brq->stop.arg = 0;
1755 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1756
1757 mmc_set_data_timeout(&brq->data, card);
1758
1759 brq->data.sg = mqrq->sg;
1760 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1761
1762 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman60aaa642012-10-09 13:50:56 +02001763
1764 /*
1765 * This is intended for packed commands tests usage - in case these
1766 * functions are not in use the respective pointers are NULL
1767 */
1768 if (mq->err_check_fn)
1769 mqrq->mmc_active.err_check = mq->err_check_fn;
1770 else
1771 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1772
1773 if (mq->packed_test_fn)
1774 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001775
1776 mmc_queue_bounce_pre(mqrq);
1777}
1778
Adrian Hunter67716322011-08-29 16:42:15 +03001779static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1780 struct mmc_blk_request *brq, struct request *req,
1781 int ret)
1782{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001783 struct mmc_queue_req *mq_rq;
1784 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1785
Adrian Hunter67716322011-08-29 16:42:15 +03001786 /*
1787 * If this is an SD card and we're writing, we can first
1788 * mark the known good sectors as ok.
1789 *
1790 * If the card is not SD, we can still ok written sectors
1791 * as reported by the controller (which might be less than
1792 * the real number of written sectors, but never more).
1793 */
1794 if (mmc_card_sd(card)) {
1795 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301796 if (!brq->data.fault_injected) {
1797 blocks = mmc_sd_num_wr_blocks(card);
1798 if (blocks != (u32)-1)
1799 ret = blk_end_request(req, 0, blocks << 9);
1800 } else
1801 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001802 } else {
1803 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1804 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1805 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001806 return ret;
1807}
1808
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001809static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1810{
1811 struct request *prq;
1812 int idx = mq_rq->packed_fail_idx, i = 0;
1813 int ret = 0;
1814
1815 while (!list_empty(&mq_rq->packed_list)) {
1816 prq = list_entry_rq(mq_rq->packed_list.next);
1817 if (idx == i) {
1818 /* retry from error index */
1819 mq_rq->packed_num -= idx;
1820 mq_rq->req = prq;
1821 ret = 1;
1822
1823 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1824 list_del_init(&prq->queuelist);
1825 mmc_blk_clear_packed(mq_rq);
1826 }
1827 return ret;
1828 }
1829 list_del_init(&prq->queuelist);
1830 blk_end_request(prq, 0, blk_rq_bytes(prq));
1831 i++;
1832 }
1833
1834 mmc_blk_clear_packed(mq_rq);
1835 return ret;
1836}
1837static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1838{
1839 struct request *prq;
1840
1841 while (!list_empty(&mq_rq->packed_list)) {
1842 prq = list_entry_rq(mq_rq->packed_list.next);
1843 list_del_init(&prq->queuelist);
1844 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1845 }
1846
1847 mmc_blk_clear_packed(mq_rq);
1848}
1849
1850static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1851 struct mmc_queue_req *mq_rq)
1852{
1853 struct request *prq;
1854 struct request_queue *q = mq->queue;
1855
1856 while (!list_empty(&mq_rq->packed_list)) {
1857 prq = list_entry_rq(mq_rq->packed_list.prev);
1858 if (prq->queuelist.prev != &mq_rq->packed_list) {
1859 list_del_init(&prq->queuelist);
1860 spin_lock_irq(q->queue_lock);
1861 blk_requeue_request(mq->queue, prq);
1862 spin_unlock_irq(q->queue_lock);
1863 } else {
1864 list_del_init(&prq->queuelist);
1865 }
1866 }
1867
1868 mmc_blk_clear_packed(mq_rq);
1869}
1870
Per Forlinee8a43a2011-07-01 18:55:33 +02001871static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001872{
1873 struct mmc_blk_data *md = mq->data;
1874 struct mmc_card *card = md->queue.card;
1875 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001876 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001877 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001878 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001879 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001880 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001881 const u8 packed_num = 2;
1882 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001883
1884 if (!rqc && !mq->mqrq_prev->req)
1885 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001886
Maya Erez5c46dad2012-10-10 03:47:54 +02001887 if (rqc) {
1888 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1889 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001890 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5c46dad2012-10-10 03:47:54 +02001891 }
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001892
Per Forlin54d49d772011-07-01 18:55:29 +02001893 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001894 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001895 if (reqs >= packed_num)
1896 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1897 card, mq);
1898 else
1899 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001900 areq = &mq->mqrq_cur->mmc_active;
1901 } else
1902 areq = NULL;
1903 areq = mmc_start_req(card->host, areq, (int *) &status);
1904 if (!areq)
1905 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001906
Per Forlinee8a43a2011-07-01 18:55:33 +02001907 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1908 brq = &mq_rq->brq;
1909 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001910 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001911 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001912
Per Forlind78d4a82011-07-01 18:55:30 +02001913 switch (status) {
1914 case MMC_BLK_SUCCESS:
1915 case MMC_BLK_PARTIAL:
1916 /*
1917 * A block was successfully transferred.
1918 */
Adrian Hunter67716322011-08-29 16:42:15 +03001919 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001920
1921 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1922 ret = mmc_blk_end_packed_req(mq_rq);
1923 break;
1924 } else {
1925 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001926 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001927 }
1928
Adrian Hunter67716322011-08-29 16:42:15 +03001929 /*
1930 * If the blk_end_request function returns non-zero even
1931 * though all data has been transferred and no errors
1932 * were returned by the host controller, it's a bug.
1933 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001934 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301935 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001936 __func__, blk_rq_bytes(req),
1937 brq->data.bytes_xfered);
1938 rqc = NULL;
1939 goto cmd_abort;
1940 }
Per Forlind78d4a82011-07-01 18:55:30 +02001941 break;
1942 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001943 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1944 if (!mmc_blk_reset(md, card->host, type))
1945 break;
1946 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001947 case MMC_BLK_RETRY:
1948 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001949 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001950 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001951 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001952 if (!mmc_blk_reset(md, card->host, type))
1953 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001954 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001955 case MMC_BLK_DATA_ERR: {
1956 int err;
1957
1958 err = mmc_blk_reset(md, card->host, type);
1959 if (!err)
1960 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001961 if (err == -ENODEV ||
1962 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001963 goto cmd_abort;
1964 /* Fall through */
1965 }
1966 case MMC_BLK_ECC_ERR:
1967 if (brq->data.blocks > 1) {
1968 /* Redo read one sector at a time */
1969 pr_warning("%s: retrying using single block read\n",
1970 req->rq_disk->disk_name);
1971 disable_multi = 1;
1972 break;
1973 }
Per Forlind78d4a82011-07-01 18:55:30 +02001974 /*
1975 * After an error, we redo I/O one sector at a
1976 * time, so we only reach here after trying to
1977 * read a single sector.
1978 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301979 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001980 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001981 if (!ret)
1982 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001983 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301984 case MMC_BLK_NOMEDIUM:
1985 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001986 }
1987
Per Forlinee8a43a2011-07-01 18:55:33 +02001988 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001989 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1990 /*
1991 * In case of a incomplete request
1992 * prepare it again and resend.
1993 */
1994 mmc_blk_rw_rq_prep(mq_rq, card,
1995 disable_multi, mq);
1996 mmc_start_req(card->host,
1997 &mq_rq->mmc_active, NULL);
1998 } else {
1999 if (!mq_rq->packed_retries)
2000 goto cmd_abort;
2001 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2002 mmc_start_req(card->host,
2003 &mq_rq->mmc_active, NULL);
2004 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } while (ret);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return 1;
2009
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002010 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02002011 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2012 if (mmc_card_removed(card))
2013 req->cmd_flags |= REQ_QUIET;
2014 while (ret)
2015 ret = blk_end_request(req, -EIO,
2016 blk_rq_cur_bytes(req));
2017 } else {
2018 mmc_blk_abort_packed_req(mq_rq);
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Per Forlinee8a43a2011-07-01 18:55:33 +02002021 start_new_req:
2022 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02002023 /*
2024 * If current request is packed, it needs to put back.
2025 */
2026 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2027 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2028
Per Forlinee8a43a2011-07-01 18:55:33 +02002029 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2030 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return 0;
2034}
2035
Adrian Hunterbd788c92010-08-11 14:17:47 -07002036static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2037{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002038 int ret;
2039 struct mmc_blk_data *md = mq->data;
2040 struct mmc_card *card = md->queue.card;
2041
San Mehat148c57a2009-07-30 08:21:19 -07002042#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2043 if (mmc_bus_needs_resume(card->host)) {
2044 mmc_resume_bus(card->host);
2045 mmc_blk_set_blksize(md, card);
2046 }
2047#endif
2048
Maya Erezec0bbad2013-01-16 09:43:45 +02002049 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002050 /* claim host only for the first request */
2051 mmc_claim_host(card->host);
Maya Erezec0bbad2013-01-16 09:43:45 +02002052 if (card->ext_csd.bkops_en)
2053 mmc_stop_bkops(card);
2054 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002055
Andrei Warkentin371a6892011-04-11 18:10:25 -05002056 ret = mmc_blk_part_switch(card, md);
2057 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002058 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302059 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002060 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002061 ret = 0;
2062 goto out;
2063 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002064
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002065 mmc_blk_write_packing_control(mq, req);
2066
Maya Erez463bb952012-05-24 23:46:29 +03002067 if (req && req->cmd_flags & REQ_SANITIZE) {
2068 /* complete ongoing async transfer before issuing sanitize */
2069 if (card->host && card->host->areq)
2070 mmc_blk_issue_rw_rq(mq, NULL);
2071 ret = mmc_blk_issue_sanitize_rq(mq, req);
2072 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002073 /* complete ongoing async transfer before issuing discard */
2074 if (card->host->areq)
2075 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07002076 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002077 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002078 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002079 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002080 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002081 /* complete ongoing async transfer before issuing flush */
2082 if (card->host->areq)
2083 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002084 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002085 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002086 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002087 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002088
Andrei Warkentin371a6892011-04-11 18:10:25 -05002089out:
Maya Erezec0bbad2013-01-16 09:43:45 +02002090 if (!req) {
2091 if (mmc_card_need_bkops(card))
2092 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002093 /* release host only when there are no more requests */
2094 mmc_release_host(card->host);
Maya Erezec0bbad2013-01-16 09:43:45 +02002095 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002096 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Russell Kinga6f6c962006-01-03 22:38:44 +00002099static inline int mmc_blk_readonly(struct mmc_card *card)
2100{
2101 return mmc_card_readonly(card) ||
2102 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2103}
2104
Andrei Warkentin371a6892011-04-11 18:10:25 -05002105static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2106 struct device *parent,
2107 sector_t size,
2108 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002109 const char *subname,
2110 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct mmc_blk_data *md;
2113 int devidx, ret;
Maya Erez860cef92013-01-16 09:48:25 +02002114 unsigned int percentage =
2115 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002117 devidx = find_first_zero_bit(dev_use, max_devices);
2118 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return ERR_PTR(-ENOSPC);
2120 __set_bit(devidx, dev_use);
2121
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002122 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002123 if (!md) {
2124 ret = -ENOMEM;
2125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002127
Russell Kinga6f6c962006-01-03 22:38:44 +00002128 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002129 * !subname implies we are creating main mmc_blk_data that will be
2130 * associated with mmc_card with mmc_set_drvdata. Due to device
2131 * partitions, devidx will not coincide with a per-physical card
2132 * index anymore so we keep track of a name index.
2133 */
2134 if (!subname) {
2135 md->name_idx = find_first_zero_bit(name_use, max_devices);
2136 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002137 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002138 md->name_idx = ((struct mmc_blk_data *)
2139 dev_to_disk(parent)->private_data)->name_idx;
2140
Johan Rudholmadd710e2011-12-02 08:51:06 +01002141 md->area_type = area_type;
2142
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002143 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002144 * Set the read-only status based on the supported commands
2145 * and the write protect switch.
2146 */
2147 md->read_only = mmc_blk_readonly(card);
2148
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002149 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002150 if (md->disk == NULL) {
2151 ret = -ENOMEM;
2152 goto err_kfree;
2153 }
2154
2155 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002156 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002157 md->usage = 1;
2158
Adrian Hunterd09408a2011-06-23 13:40:28 +03002159 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002160 if (ret)
2161 goto err_putdisk;
2162
Russell Kinga6f6c962006-01-03 22:38:44 +00002163 md->queue.issue_fn = mmc_blk_issue_rq;
2164 md->queue.data = md;
2165
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002166 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002167 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002168 md->disk->fops = &mmc_bdops;
2169 md->disk->private_data = md;
2170 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171 md->disk->driverfs_dev = parent;
2172 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002173 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00002174
2175 /*
2176 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2177 *
2178 * - be set for removable media with permanent block devices
2179 * - be unset for removable block devices with permanent media
2180 *
2181 * Since MMC block devices clearly fall under the second
2182 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2183 * should use the block device creation/destruction hotplug
2184 * messages to tell when the card is present.
2185 */
2186
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002187 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2188 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002189
Martin K. Petersene1defc42009-05-22 17:17:49 -04002190 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002191 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002192
Maya Erez860cef92013-01-16 09:48:25 +02002193 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2194 card->bkops_info.min_sectors_to_queue_delayed_work =
2195 ((unsigned int)size * percentage) / 100;
2196
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002197 if (mmc_host_cmd23(card->host)) {
2198 if (mmc_card_mmc(card) ||
2199 (mmc_card_sd(card) &&
2200 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2201 md->flags |= MMC_BLK_CMD23;
2202 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002203
2204 if (mmc_card_mmc(card) &&
2205 md->flags & MMC_BLK_CMD23 &&
2206 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2207 card->ext_csd.rel_sectors)) {
2208 md->flags |= MMC_BLK_REL_WR;
2209 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2210 }
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002213
2214 err_putdisk:
2215 put_disk(md->disk);
2216 err_kfree:
2217 kfree(md);
2218 out:
2219 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Andrei Warkentin371a6892011-04-11 18:10:25 -05002222static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2223{
2224 sector_t size;
2225 struct mmc_blk_data *md;
2226
2227 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2228 /*
2229 * The EXT_CSD sector count is in number or 512 byte
2230 * sectors.
2231 */
2232 size = card->ext_csd.sectors;
2233 } else {
2234 /*
2235 * The CSD capacity field is in units of read_blkbits.
2236 * set_capacity takes units of 512 bytes.
2237 */
2238 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2239 }
2240
Johan Rudholmadd710e2011-12-02 08:51:06 +01002241 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2242 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002243 return md;
2244}
2245
2246static int mmc_blk_alloc_part(struct mmc_card *card,
2247 struct mmc_blk_data *md,
2248 unsigned int part_type,
2249 sector_t size,
2250 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002251 const char *subname,
2252 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002253{
2254 char cap_str[10];
2255 struct mmc_blk_data *part_md;
2256
2257 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002258 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002259 if (IS_ERR(part_md))
2260 return PTR_ERR(part_md);
2261 part_md->part_type = part_type;
2262 list_add(&part_md->part, &md->part);
2263
2264 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2265 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302266 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002267 part_md->disk->disk_name, mmc_card_id(card),
2268 mmc_card_name(card), part_md->part_type, cap_str);
2269 return 0;
2270}
2271
Namjae Jeone0c368d2011-10-06 23:41:38 +09002272/* MMC Physical partitions consist of two boot partitions and
2273 * up to four general purpose partitions.
2274 * For each partition enabled in EXT_CSD a block device will be allocatedi
2275 * to provide access to the partition.
2276 */
2277
Andrei Warkentin371a6892011-04-11 18:10:25 -05002278static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2279{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002280 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002281
2282 if (!mmc_card_mmc(card))
2283 return 0;
2284
Namjae Jeone0c368d2011-10-06 23:41:38 +09002285 for (idx = 0; idx < card->nr_parts; idx++) {
2286 if (card->part[idx].size) {
2287 ret = mmc_blk_alloc_part(card, md,
2288 card->part[idx].part_cfg,
2289 card->part[idx].size >> 9,
2290 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002291 card->part[idx].name,
2292 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002293 if (ret)
2294 return ret;
2295 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002296 }
2297
2298 return ret;
2299}
2300
Andrei Warkentin371a6892011-04-11 18:10:25 -05002301static void mmc_blk_remove_req(struct mmc_blk_data *md)
2302{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002303 struct mmc_card *card;
2304
Andrei Warkentin371a6892011-04-11 18:10:25 -05002305 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002306 card = md->queue.card;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002307 device_remove_file(disk_to_dev(md->disk),
2308 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 if (md->disk->flags & GENHD_FL_UP) {
2310 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002311 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2312 card->ext_csd.boot_ro_lockable)
2313 device_remove_file(disk_to_dev(md->disk),
2314 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002315
2316 /* Stop new requests from getting into the queue */
2317 del_gendisk(md->disk);
2318 }
2319
2320 /* Then flush out any already in there */
2321 mmc_cleanup_queue(&md->queue);
2322 mmc_blk_put(md);
2323 }
2324}
2325
2326static void mmc_blk_remove_parts(struct mmc_card *card,
2327 struct mmc_blk_data *md)
2328{
2329 struct list_head *pos, *q;
2330 struct mmc_blk_data *part_md;
2331
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002332 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002333 list_for_each_safe(pos, q, &md->part) {
2334 part_md = list_entry(pos, struct mmc_blk_data, part);
2335 list_del(pos);
2336 mmc_blk_remove_req(part_md);
2337 }
2338}
2339
2340static int mmc_add_disk(struct mmc_blk_data *md)
2341{
2342 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002343 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002344
2345 add_disk(md->disk);
2346 md->force_ro.show = force_ro_show;
2347 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302348 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002349 md->force_ro.attr.name = "force_ro";
2350 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2351 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2352 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002353 goto force_ro_fail;
2354
2355 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2356 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002357 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002358
2359 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2360 mode = S_IRUGO;
2361 else
2362 mode = S_IRUGO | S_IWUSR;
2363
2364 md->power_ro_lock.show = power_ro_lock_show;
2365 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002366 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002367 md->power_ro_lock.attr.mode = mode;
2368 md->power_ro_lock.attr.name =
2369 "ro_lock_until_next_power_on";
2370 ret = device_create_file(disk_to_dev(md->disk),
2371 &md->power_ro_lock);
2372 if (ret)
2373 goto power_ro_lock_fail;
2374 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002375
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002376 md->num_wr_reqs_to_start_packing.show =
2377 num_wr_reqs_to_start_packing_show;
2378 md->num_wr_reqs_to_start_packing.store =
2379 num_wr_reqs_to_start_packing_store;
2380 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2381 md->num_wr_reqs_to_start_packing.attr.name =
2382 "num_wr_reqs_to_start_packing";
2383 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2384 ret = device_create_file(disk_to_dev(md->disk),
2385 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002386 if (ret)
Maya Ereza317a4c2012-10-30 09:02:39 +02002387 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002388
Maya Erez860cef92013-01-16 09:48:25 +02002389 md->bkops_check_threshold.show = bkops_check_threshold_show;
2390 md->bkops_check_threshold.store = bkops_check_threshold_store;
2391 sysfs_attr_init(&md->bkops_check_threshold.attr);
2392 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2393 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Yaniv Gardiab0b6b02012-10-11 11:36:24 +02002394 ret = device_create_file(disk_to_dev(md->disk),
Maya Erez860cef92013-01-16 09:48:25 +02002395 &md->bkops_check_threshold);
Yaniv Gardiab0b6b02012-10-11 11:36:24 +02002396 if (ret)
Maya Erez860cef92013-01-16 09:48:25 +02002397 goto bkops_check_threshold_fails;
Yaniv Gardiab0b6b02012-10-11 11:36:24 +02002398
Johan Rudholmadd710e2011-12-02 08:51:06 +01002399 return ret;
2400
Maya Erez860cef92013-01-16 09:48:25 +02002401bkops_check_threshold_fails:
Maya Ereza317a4c2012-10-30 09:02:39 +02002402 device_remove_file(disk_to_dev(md->disk),
2403 &md->num_wr_reqs_to_start_packing);
2404num_wr_reqs_to_start_packing_fail:
2405 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002406power_ro_lock_fail:
Maya Ereza317a4c2012-10-30 09:02:39 +02002407 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002408force_ro_fail:
Maya Ereza317a4c2012-10-30 09:02:39 +02002409 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002410
2411 return ret;
2412}
2413
Chris Ballc59d4472011-11-11 22:01:43 -05002414#define CID_MANFID_SANDISK 0x2
2415#define CID_MANFID_TOSHIBA 0x11
2416#define CID_MANFID_MICRON 0x13
2417
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002418static const struct mmc_fixup blk_fixups[] =
2419{
Chris Ballc59d4472011-11-11 22:01:43 -05002420 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2421 MMC_QUIRK_INAND_CMD38),
2422 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2423 MMC_QUIRK_INAND_CMD38),
2424 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2425 MMC_QUIRK_INAND_CMD38),
2426 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2427 MMC_QUIRK_INAND_CMD38),
2428 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2429 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002430
2431 /*
2432 * Some MMC cards experience performance degradation with CMD23
2433 * instead of CMD12-bounded multiblock transfers. For now we'll
2434 * black list what's bad...
2435 * - Certain Toshiba cards.
2436 *
2437 * N.B. This doesn't affect SD cards.
2438 */
Chris Ballc59d4472011-11-11 22:01:43 -05002439 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002440 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002441 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002442 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002443 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002444 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002445
2446 /*
2447 * Some Micron MMC cards needs longer data read timeout than
2448 * indicated in CSD.
2449 */
Chris Ballc59d4472011-11-11 22:01:43 -05002450 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002451 MMC_QUIRK_LONG_READ_TIME),
2452
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302453 /* Some INAND MCP devices advertise incorrect timeout values */
2454 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2455 MMC_QUIRK_INAND_DATA_TIMEOUT),
2456
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002457 END_FIXUP
2458};
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460static int mmc_blk_probe(struct mmc_card *card)
2461{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002462 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002463 char cap_str[10];
2464
Pierre Ossman912490d2005-05-21 10:27:02 +01002465 /*
2466 * Check that the card supports the command class(es) we need.
2467 */
2468 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return -ENODEV;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 md = mmc_blk_alloc(card);
2472 if (IS_ERR(md))
2473 return PTR_ERR(md);
2474
Yi Li444122f2009-02-05 15:31:57 +08002475 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002476 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302477 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002479 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Andrei Warkentin371a6892011-04-11 18:10:25 -05002481 if (mmc_blk_alloc_parts(card, md))
2482 goto out;
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002485 mmc_fixup_device(card, blk_fixups);
2486
San Mehat148c57a2009-07-30 08:21:19 -07002487#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2488 mmc_set_bus_resume_policy(card->host, 1);
2489#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002490 if (mmc_add_disk(md))
2491 goto out;
2492
2493 list_for_each_entry(part_md, &md->part, part) {
2494 if (mmc_add_disk(part_md))
2495 goto out;
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return 0;
2498
2499 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002500 mmc_blk_remove_parts(card, md);
2501 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505static void mmc_blk_remove(struct mmc_card *card)
2506{
2507 struct mmc_blk_data *md = mmc_get_drvdata(card);
2508
Andrei Warkentin371a6892011-04-11 18:10:25 -05002509 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002510 mmc_claim_host(card->host);
2511 mmc_blk_part_switch(card, md);
2512 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002513 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002515#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2516 mmc_set_bus_resume_policy(card->host, 0);
2517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
2520#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002521static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002523 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct mmc_blk_data *md = mmc_get_drvdata(card);
2525
2526 if (md) {
2527 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002528 list_for_each_entry(part_md, &md->part, part) {
2529 mmc_queue_suspend(&part_md->queue);
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532 return 0;
2533}
2534
2535static int mmc_blk_resume(struct mmc_card *card)
2536{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002537 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct mmc_blk_data *md = mmc_get_drvdata(card);
2539
2540 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002541 /*
2542 * Resume involves the card going into idle state,
2543 * so current partition is always the main one.
2544 */
2545 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002547 list_for_each_entry(part_md, &md->part, part) {
2548 mmc_queue_resume(&part_md->queue);
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551 return 0;
2552}
2553#else
2554#define mmc_blk_suspend NULL
2555#define mmc_blk_resume NULL
2556#endif
2557
2558static struct mmc_driver mmc_driver = {
2559 .drv = {
2560 .name = "mmcblk",
2561 },
2562 .probe = mmc_blk_probe,
2563 .remove = mmc_blk_remove,
2564 .suspend = mmc_blk_suspend,
2565 .resume = mmc_blk_resume,
2566};
2567
2568static int __init mmc_blk_init(void)
2569{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002570 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002572 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2573 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2574
2575 max_devices = 256 / perdev_minors;
2576
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002577 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2578 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002581 res = mmc_register_driver(&mmc_driver);
2582 if (res)
2583 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002585 return 0;
2586 out2:
2587 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 out:
2589 return res;
2590}
2591
2592static void __exit mmc_blk_exit(void)
2593{
2594 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002595 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598module_init(mmc_blk_init);
2599module_exit(mmc_blk_exit);
2600
2601MODULE_LICENSE("GPL");
2602MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2603