blob: 12c9e175c41b9d11c379a2544ab494ac1306ece4 [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
Subhash Jadavani914fa652013-01-07 17:31:43 +053060#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050061
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030062#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
63
Seungwon Jeon53f8f572012-09-27 15:00:26 +020064#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
65 (req->cmd_flags & REQ_META)) && \
66 (rq_data_dir(req) == WRITE))
67#define PACKED_CMD_VER 0x01
68#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020069#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
70 do { \
71 if (stats->enabled) \
72 stats->pack_stop_reason[reason]++; \
73 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020074
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020075static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040076
77/*
78 * The defaults come from config options but can be overriden by module
79 * or bootarg options.
80 */
81static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82
83/*
84 * We've only got one major, so number of mmcblk devices is
85 * limited to 256 / number of minors per device.
86 */
87static int max_devices;
88
89/* 256 minors, so at most 256 separate devices */
90static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050091static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * There is one mmc_blk_data per slot.
95 */
96struct mmc_blk_data {
97 spinlock_t lock;
98 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000107 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500109 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
118 * with mmc_card with mmc_set_drvdata, and keeps
119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200124 struct device_attribute num_wr_reqs_to_start_packing;
Maya Ereze1eae982013-01-05 21:51:06 +0200125 struct device_attribute bkops_check_threshold;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100126 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Arjan van de Vena621aae2006-01-12 18:43:35 +0000129static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200131enum {
132 MMC_PACKED_N_IDX = -1,
133 MMC_PACKED_N_ZERO,
134 MMC_PACKED_N_SINGLE,
135};
136
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400137module_param(perdev_minors, int, 0444);
138MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
139
Loic Pallardy961e37a2012-08-06 15:12:31 +0000140static inline int mmc_blk_part_switch(struct mmc_card *card,
141 struct mmc_blk_data *md);
142static int get_card_status(struct mmc_card *card, u32 *status, int retries);
143
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200144static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
145{
146 mqrq->packed_cmd = MMC_PACKED_NONE;
147 mqrq->packed_num = MMC_PACKED_N_ZERO;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
151{
152 struct mmc_blk_data *md;
153
Arjan van de Vena621aae2006-01-12 18:43:35 +0000154 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 md = disk->private_data;
156 if (md && md->usage == 0)
157 md = NULL;
158 if (md)
159 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000160 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return md;
163}
164
Andrei Warkentin371a6892011-04-11 18:10:25 -0500165static inline int mmc_get_devidx(struct gendisk *disk)
166{
Colin Cross71b54d42010-09-03 12:41:21 -0700167 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500168 return devidx;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void mmc_blk_put(struct mmc_blk_data *md)
172{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000173 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 md->usage--;
175 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500176 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800177 blk_cleanup_queue(md->queue.queue);
178
David Woodhouse1dff3142007-11-21 18:45:12 +0100179 __clear_bit(devidx, dev_use);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kfree(md);
183 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Johan Rudholmadd710e2011-12-02 08:51:06 +0100187static ssize_t power_ro_lock_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 int ret;
191 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
192 struct mmc_card *card = md->queue.card;
193 int locked = 0;
194
195 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
196 locked = 2;
197 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
198 locked = 1;
199
200 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
201
202 return ret;
203}
204
205static ssize_t power_ro_lock_store(struct device *dev,
206 struct device_attribute *attr, const char *buf, size_t count)
207{
208 int ret;
209 struct mmc_blk_data *md, *part_md;
210 struct mmc_card *card;
211 unsigned long set;
212
213 if (kstrtoul(buf, 0, &set))
214 return -EINVAL;
215
216 if (set != 1)
217 return count;
218
219 md = mmc_blk_get(dev_to_disk(dev));
220 card = md->queue.card;
221
222 mmc_claim_host(card->host);
223
224 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
225 card->ext_csd.boot_ro_lock |
226 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
227 card->ext_csd.part_time);
228 if (ret)
229 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
230 else
231 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
232
233 mmc_release_host(card->host);
234
235 if (!ret) {
236 pr_info("%s: Locking boot partition ro until next power on\n",
237 md->disk->disk_name);
238 set_disk_ro(md->disk, 1);
239
240 list_for_each_entry(part_md, &md->part, part)
241 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
242 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
243 set_disk_ro(part_md->disk, 1);
244 }
245 }
246
247 mmc_blk_put(md);
248 return count;
249}
250
Andrei Warkentin371a6892011-04-11 18:10:25 -0500251static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
252 char *buf)
253{
254 int ret;
255 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
256
257 ret = snprintf(buf, PAGE_SIZE, "%d",
258 get_disk_ro(dev_to_disk(dev)) ^
259 md->read_only);
260 mmc_blk_put(md);
261 return ret;
262}
263
264static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
265 const char *buf, size_t count)
266{
267 int ret;
268 char *end;
269 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
270 unsigned long set = simple_strtoul(buf, &end, 0);
271 if (end == buf) {
272 ret = -EINVAL;
273 goto out;
274 }
275
276 set_disk_ro(dev_to_disk(dev), set || md->read_only);
277 ret = count;
278out:
279 mmc_blk_put(md);
280 return ret;
281}
282
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200283static ssize_t
284num_wr_reqs_to_start_packing_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
288 int num_wr_reqs_to_start_packing;
289 int ret;
290
291 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
292
293 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
294
295 mmc_blk_put(md);
296 return ret;
297}
298
299static ssize_t
300num_wr_reqs_to_start_packing_store(struct device *dev,
301 struct device_attribute *attr,
302 const char *buf, size_t count)
303{
304 int value;
305 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200306 struct mmc_card *card = md->queue.card;
307 int ret = count;
308
309 if (!card) {
310 ret = -EINVAL;
311 goto exit;
312 }
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200313
314 sscanf(buf, "%d", &value);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200315
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200316 if (value >= 0) {
317 md->queue.num_wr_reqs_to_start_packing =
318 min_t(int, value, (int)card->ext_csd.max_packed_writes);
319
320 pr_debug("%s: trigger to pack: new value = %d",
321 mmc_hostname(card->host),
322 md->queue.num_wr_reqs_to_start_packing);
323 } else {
324 pr_err("%s: value %d is not valid. old value remains = %d",
325 mmc_hostname(card->host), value,
326 md->queue.num_wr_reqs_to_start_packing);
327 ret = -EINVAL;
328 }
329
330exit:
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200331 mmc_blk_put(md);
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200332 return ret;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200333}
334
Maya Erez2d33a192013-01-04 15:52:41 +0200335static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200336bkops_check_threshold_show(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200337 struct device_attribute *attr, char *buf)
338{
339 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Maya Erez2d33a192013-01-04 15:52:41 +0200340 struct mmc_card *card = md->queue.card;
341 int ret;
342
343 if (!card)
344 ret = -EINVAL;
Maya Ereze1eae982013-01-05 21:51:06 +0200345 else
Maya Erez2d33a192013-01-04 15:52:41 +0200346 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Maya Ereze1eae982013-01-05 21:51:06 +0200347 card->bkops_info.size_percentage_to_queue_delayed_work);
Maya Erez2d33a192013-01-04 15:52:41 +0200348
349 mmc_blk_put(md);
350 return ret;
351}
352
353static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200354bkops_check_threshold_store(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200355 struct device_attribute *attr,
356 const char *buf, size_t count)
357{
358 int value;
359 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
360 struct mmc_card *card = md->queue.card;
Maya Ereze1eae982013-01-05 21:51:06 +0200361 unsigned int card_size;
362 int ret = count;
Maya Erez2d33a192013-01-04 15:52:41 +0200363
Maya Ereze1eae982013-01-05 21:51:06 +0200364 if (!card) {
365 ret = -EINVAL;
366 goto exit;
367 }
Maya Erez2d33a192013-01-04 15:52:41 +0200368
369 sscanf(buf, "%d", &value);
Maya Ereze1eae982013-01-05 21:51:06 +0200370 if ((value <= 0) || (value >= 100)) {
371 ret = -EINVAL;
372 goto exit;
373 }
Maya Erez2d33a192013-01-04 15:52:41 +0200374
Maya Ereze1eae982013-01-05 21:51:06 +0200375 card_size = (unsigned int)get_capacity(md->disk);
376 if (card_size <= 0) {
377 ret = -EINVAL;
378 goto exit;
379 }
380 card->bkops_info.size_percentage_to_queue_delayed_work = value;
381 card->bkops_info.min_sectors_to_queue_delayed_work =
382 (card_size * value) / 100;
383
384 pr_debug("%s: size_percentage = %d, min_sectors = %d",
385 mmc_hostname(card->host),
386 card->bkops_info.size_percentage_to_queue_delayed_work,
387 card->bkops_info.min_sectors_to_queue_delayed_work);
388
389exit:
Maya Erez2d33a192013-01-04 15:52:41 +0200390 mmc_blk_put(md);
391 return count;
392}
393
Al Viroa5a15612008-03-02 10:33:30 -0500394static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Al Viroa5a15612008-03-02 10:33:30 -0500396 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int ret = -ENXIO;
398
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200399 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (md) {
401 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500402 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700404
Al Viroa5a15612008-03-02 10:33:30 -0500405 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700406 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700407 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200410 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return ret;
413}
414
Al Viroa5a15612008-03-02 10:33:30 -0500415static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Al Viroa5a15612008-03-02 10:33:30 -0500417 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200419 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200421 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424
425static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800426mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800428 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
429 geo->heads = 4;
430 geo->sectors = 16;
431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
John Calixtocb87ea22011-04-26 18:56:29 -0400434struct mmc_blk_ioc_data {
435 struct mmc_ioc_cmd ic;
436 unsigned char *buf;
437 u64 buf_bytes;
438};
439
440static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
441 struct mmc_ioc_cmd __user *user)
442{
443 struct mmc_blk_ioc_data *idata;
444 int err;
445
446 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
447 if (!idata) {
448 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400449 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400450 }
451
452 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
453 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400454 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400455 }
456
457 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
458 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
459 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400460 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400461 }
462
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100463 if (!idata->buf_bytes)
464 return idata;
465
John Calixtocb87ea22011-04-26 18:56:29 -0400466 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
467 if (!idata->buf) {
468 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400469 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400470 }
471
472 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
473 idata->ic.data_ptr, idata->buf_bytes)) {
474 err = -EFAULT;
475 goto copy_err;
476 }
477
478 return idata;
479
480copy_err:
481 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400482idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400483 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400484out:
John Calixtocb87ea22011-04-26 18:56:29 -0400485 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400486}
487
Loic Pallardy961e37a2012-08-06 15:12:31 +0000488static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
489 u32 retries_max)
490{
491 int err;
492 u32 retry_count = 0;
493
494 if (!status || !retries_max)
495 return -EINVAL;
496
497 do {
498 err = get_card_status(card, status, 5);
499 if (err)
500 break;
501
502 if (!R1_STATUS(*status) &&
503 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
504 break; /* RPMB programming operation complete */
505
506 /*
507 * Rechedule to give the MMC device a chance to continue
508 * processing the previous command without being polled too
509 * frequently.
510 */
511 usleep_range(1000, 5000);
512 } while (++retry_count < retries_max);
513
514 if (retry_count == retries_max)
515 err = -EPERM;
516
517 return err;
518}
519
John Calixtocb87ea22011-04-26 18:56:29 -0400520static int mmc_blk_ioctl_cmd(struct block_device *bdev,
521 struct mmc_ioc_cmd __user *ic_ptr)
522{
523 struct mmc_blk_ioc_data *idata;
524 struct mmc_blk_data *md;
525 struct mmc_card *card;
526 struct mmc_command cmd = {0};
527 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530528 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400529 struct scatterlist sg;
530 int err;
Loic Pallardy961e37a2012-08-06 15:12:31 +0000531 int is_rpmb = false;
532 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400533
534 /*
535 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
536 * whole block device, not on a partition. This prevents overspray
537 * between sibling partitions.
538 */
539 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
540 return -EPERM;
541
542 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
543 if (IS_ERR(idata))
544 return PTR_ERR(idata);
545
John Calixtocb87ea22011-04-26 18:56:29 -0400546 md = mmc_blk_get(bdev->bd_disk);
547 if (!md) {
548 err = -EINVAL;
549 goto cmd_done;
550 }
551
Loic Pallardy961e37a2012-08-06 15:12:31 +0000552 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
553 is_rpmb = true;
554
John Calixtocb87ea22011-04-26 18:56:29 -0400555 card = md->queue.card;
556 if (IS_ERR(card)) {
557 err = PTR_ERR(card);
558 goto cmd_done;
559 }
560
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100561 cmd.opcode = idata->ic.opcode;
562 cmd.arg = idata->ic.arg;
563 cmd.flags = idata->ic.flags;
564
565 if (idata->buf_bytes) {
566 data.sg = &sg;
567 data.sg_len = 1;
568 data.blksz = idata->ic.blksz;
569 data.blocks = idata->ic.blocks;
570
571 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
572
573 if (idata->ic.write_flag)
574 data.flags = MMC_DATA_WRITE;
575 else
576 data.flags = MMC_DATA_READ;
577
578 /* data.flags must already be set before doing this. */
579 mmc_set_data_timeout(&data, card);
580
581 /* Allow overriding the timeout_ns for empirical tuning. */
582 if (idata->ic.data_timeout_ns)
583 data.timeout_ns = idata->ic.data_timeout_ns;
584
585 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
586 /*
587 * Pretend this is a data transfer and rely on the
588 * host driver to compute timeout. When all host
589 * drivers support cmd.cmd_timeout for R1B, this
590 * can be changed to:
591 *
592 * mrq.data = NULL;
593 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
594 */
595 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
596 }
597
598 mrq.data = &data;
599 }
600
601 mrq.cmd = &cmd;
602
John Calixtocb87ea22011-04-26 18:56:29 -0400603 mmc_claim_host(card->host);
604
Loic Pallardy961e37a2012-08-06 15:12:31 +0000605 err = mmc_blk_part_switch(card, md);
606 if (err)
607 goto cmd_rel_host;
608
John Calixtocb87ea22011-04-26 18:56:29 -0400609 if (idata->ic.is_acmd) {
610 err = mmc_app_cmd(card->host, card);
611 if (err)
612 goto cmd_rel_host;
613 }
614
Loic Pallardy961e37a2012-08-06 15:12:31 +0000615 if (is_rpmb) {
616 err = mmc_set_blockcount(card, data.blocks,
617 idata->ic.write_flag & (1 << 31));
618 if (err)
619 goto cmd_rel_host;
620 }
621
John Calixtocb87ea22011-04-26 18:56:29 -0400622 mmc_wait_for_req(card->host, &mrq);
623
624 if (cmd.error) {
625 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
626 __func__, cmd.error);
627 err = cmd.error;
628 goto cmd_rel_host;
629 }
630 if (data.error) {
631 dev_err(mmc_dev(card->host), "%s: data error %d\n",
632 __func__, data.error);
633 err = data.error;
634 goto cmd_rel_host;
635 }
636
637 /*
638 * According to the SD specs, some commands require a delay after
639 * issuing the command.
640 */
641 if (idata->ic.postsleep_min_us)
642 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
643
644 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
645 err = -EFAULT;
646 goto cmd_rel_host;
647 }
648
649 if (!idata->ic.write_flag) {
650 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
651 idata->buf, idata->buf_bytes)) {
652 err = -EFAULT;
653 goto cmd_rel_host;
654 }
655 }
656
Loic Pallardy961e37a2012-08-06 15:12:31 +0000657 if (is_rpmb) {
658 /*
659 * Ensure RPMB command has completed by polling CMD13
660 * "Send Status".
661 */
662 err = ioctl_rpmb_card_status_poll(card, &status, 5);
663 if (err)
664 dev_err(mmc_dev(card->host),
665 "%s: Card Status=0x%08X, error %d\n",
666 __func__, status, err);
667 }
668
John Calixtocb87ea22011-04-26 18:56:29 -0400669cmd_rel_host:
670 mmc_release_host(card->host);
671
672cmd_done:
673 mmc_blk_put(md);
674 kfree(idata->buf);
675 kfree(idata);
676 return err;
677}
678
679static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
680 unsigned int cmd, unsigned long arg)
681{
682 int ret = -EINVAL;
683 if (cmd == MMC_IOC_CMD)
684 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
685 return ret;
686}
687
688#ifdef CONFIG_COMPAT
689static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
690 unsigned int cmd, unsigned long arg)
691{
692 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
693}
694#endif
695
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700696static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500697 .open = mmc_blk_open,
698 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800699 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400701 .ioctl = mmc_blk_ioctl,
702#ifdef CONFIG_COMPAT
703 .compat_ioctl = mmc_blk_compat_ioctl,
704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
Andrei Warkentin371a6892011-04-11 18:10:25 -0500707static inline int mmc_blk_part_switch(struct mmc_card *card,
708 struct mmc_blk_data *md)
709{
710 int ret;
711 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300712
Andrei Warkentin371a6892011-04-11 18:10:25 -0500713 if (main_md->part_curr == md->part_type)
714 return 0;
715
716 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300717 u8 part_config = card->ext_csd.part_config;
718
719 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
720 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500721
722 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300723 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500724 card->ext_csd.part_time);
725 if (ret)
726 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300727
728 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300729 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500730
731 main_md->part_curr = md->part_type;
732 return 0;
733}
734
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700735static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
736{
737 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100738 u32 result;
739 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700740
Venkatraman Sad5fd972011-08-25 00:30:50 +0530741 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400742 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400743 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700744
745 struct scatterlist sg;
746
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700747 cmd.opcode = MMC_APP_CMD;
748 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700749 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700750
751 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700752 if (err)
753 return (u32)-1;
754 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700755 return (u32)-1;
756
757 memset(&cmd, 0, sizeof(struct mmc_command));
758
759 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
760 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700761 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700762
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700763 data.blksz = 4;
764 data.blocks = 1;
765 data.flags = MMC_DATA_READ;
766 data.sg = &sg;
767 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530768 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700769
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700770 mrq.cmd = &cmd;
771 mrq.data = &data;
772
Ben Dooks051913d2009-06-08 23:33:57 +0100773 blocks = kmalloc(4, GFP_KERNEL);
774 if (!blocks)
775 return (u32)-1;
776
777 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700778
779 mmc_wait_for_req(card->host, &mrq);
780
Ben Dooks051913d2009-06-08 23:33:57 +0100781 result = ntohl(*blocks);
782 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783
Ben Dooks051913d2009-06-08 23:33:57 +0100784 if (cmd.error || data.error)
785 result = (u32)-1;
786
787 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700788}
789
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100790static int send_stop(struct mmc_card *card, u32 *status)
791{
792 struct mmc_command cmd = {0};
793 int err;
794
795 cmd.opcode = MMC_STOP_TRANSMISSION;
796 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
797 err = mmc_wait_for_cmd(card->host, &cmd, 5);
798 if (err == 0)
799 *status = cmd.resp[0];
800 return err;
801}
802
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100803static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300804{
Chris Ball1278dba2011-04-13 23:40:30 -0400805 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300806 int err;
807
Adrian Hunter504f1912008-10-16 12:55:25 +0300808 cmd.opcode = MMC_SEND_STATUS;
809 if (!mmc_host_is_spi(card->host))
810 cmd.arg = card->rca << 16;
811 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100812 err = mmc_wait_for_cmd(card->host, &cmd, retries);
813 if (err == 0)
814 *status = cmd.resp[0];
815 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300816}
817
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530818#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100819#define ERR_RETRY 2
820#define ERR_ABORT 1
821#define ERR_CONTINUE 0
822
823static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
824 bool status_valid, u32 status)
825{
826 switch (error) {
827 case -EILSEQ:
828 /* response crc error, retry the r/w cmd */
829 pr_err("%s: %s sending %s command, card status %#x\n",
830 req->rq_disk->disk_name, "response CRC error",
831 name, status);
832 return ERR_RETRY;
833
834 case -ETIMEDOUT:
835 pr_err("%s: %s sending %s command, card status %#x\n",
836 req->rq_disk->disk_name, "timed out", name, status);
837
838 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700839 if (!status_valid) {
840 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100841 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700842 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100843 /*
844 * If it was a r/w cmd crc error, or illegal command
845 * (eg, issued in wrong state) then retry - we should
846 * have corrected the state problem above.
847 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700848 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
849 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100850 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700851 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100852
853 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700854 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100855 return ERR_ABORT;
856
857 default:
858 /* We don't understand the error code the driver gave us */
859 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
860 req->rq_disk->disk_name, error, status);
861 return ERR_ABORT;
862 }
863}
864
865/*
866 * Initial r/w and stop cmd error recovery.
867 * We don't know whether the card received the r/w cmd or not, so try to
868 * restore things back to a sane state. Essentially, we do this as follows:
869 * - Obtain card status. If the first attempt to obtain card status fails,
870 * the status word will reflect the failed status cmd, not the failed
871 * r/w cmd. If we fail to obtain card status, it suggests we can no
872 * longer communicate with the card.
873 * - Check the card state. If the card received the cmd but there was a
874 * transient problem with the response, it might still be in a data transfer
875 * mode. Try to send it a stop command. If this fails, we can't recover.
876 * - If the r/w cmd failed due to a response CRC error, it was probably
877 * transient, so retry the cmd.
878 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
879 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
880 * illegal cmd, retry.
881 * Otherwise we don't understand what happened, so abort.
882 */
883static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300884 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100885{
886 bool prev_cmd_status_valid = true;
887 u32 status, stop_status = 0;
888 int err, retry;
889
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530890 if (mmc_card_removed(card))
891 return ERR_NOMEDIUM;
892
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100893 /*
894 * Try to get card status which indicates both the card state
895 * and why there was no response. If the first attempt fails,
896 * we can't be sure the returned status is for the r/w command.
897 */
898 for (retry = 2; retry >= 0; retry--) {
899 err = get_card_status(card, &status, 0);
900 if (!err)
901 break;
902
903 prev_cmd_status_valid = false;
904 pr_err("%s: error %d sending status command, %sing\n",
905 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
906 }
907
908 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530909 if (err) {
910 /* Check if the card is removed */
911 if (mmc_detect_card_removed(card->host))
912 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100913 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530914 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100915
Adrian Hunter67716322011-08-29 16:42:15 +0300916 /* Flag ECC errors */
917 if ((status & R1_CARD_ECC_FAILED) ||
918 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
919 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
920 *ecc_err = 1;
921
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100922 /*
923 * Check the current card state. If it is in some data transfer
924 * mode, tell it to stop (and hopefully transition back to TRAN.)
925 */
926 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
927 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
928 err = send_stop(card, &stop_status);
929 if (err)
930 pr_err("%s: error %d sending stop command\n",
931 req->rq_disk->disk_name, err);
932
933 /*
934 * If the stop cmd also timed out, the card is probably
935 * not present, so abort. Other errors are bad news too.
936 */
937 if (err)
938 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300939 if (stop_status & R1_CARD_ECC_FAILED)
940 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100941 }
942
943 /* Check for set block count errors */
944 if (brq->sbc.error)
945 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
946 prev_cmd_status_valid, status);
947
948 /* Check for r/w command errors */
949 if (brq->cmd.error)
950 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
951 prev_cmd_status_valid, status);
952
Adrian Hunter67716322011-08-29 16:42:15 +0300953 /* Data errors */
954 if (!brq->stop.error)
955 return ERR_CONTINUE;
956
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100957 /* Now for stop errors. These aren't fatal to the transfer. */
958 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
959 req->rq_disk->disk_name, brq->stop.error,
960 brq->cmd.resp[0], status);
961
962 /*
963 * Subsitute in our own stop status as this will give the error
964 * state which happened during the execution of the r/w command.
965 */
966 if (stop_status) {
967 brq->stop.resp[0] = stop_status;
968 brq->stop.error = 0;
969 }
970 return ERR_CONTINUE;
971}
972
Adrian Hunter67716322011-08-29 16:42:15 +0300973static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
974 int type)
975{
976 int err;
977
978 if (md->reset_done & type)
979 return -EEXIST;
980
981 md->reset_done |= type;
982 err = mmc_hw_reset(host);
983 /* Ensure we switch back to the correct partition */
984 if (err != -EOPNOTSUPP) {
985 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
986 int part_err;
987
988 main_md->part_curr = main_md->part_type;
989 part_err = mmc_blk_part_switch(host->card, md);
990 if (part_err) {
991 /*
992 * We have failed to get back into the correct
993 * partition, so we need to abort the whole request.
994 */
995 return -ENODEV;
996 }
997 }
998 return err;
999}
1000
1001static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1002{
1003 md->reset_done &= ~type;
1004}
1005
Adrian Hunterbd788c92010-08-11 14:17:47 -07001006static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1007{
1008 struct mmc_blk_data *md = mq->data;
1009 struct mmc_card *card = md->queue.card;
1010 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001011 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001012
Adrian Hunterbd788c92010-08-11 14:17:47 -07001013 if (!mmc_can_erase(card)) {
1014 err = -EOPNOTSUPP;
1015 goto out;
1016 }
1017
1018 from = blk_rq_pos(req);
1019 nr = blk_rq_sectors(req);
1020
Maya Erez2d33a192013-01-04 15:52:41 +02001021 if (card->ext_csd.bkops_en)
1022 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1023
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001024 if (mmc_can_discard(card))
1025 arg = MMC_DISCARD_ARG;
1026 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001027 arg = MMC_TRIM_ARG;
1028 else
1029 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001030retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001031 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1032 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1033 INAND_CMD38_ARG_EXT_CSD,
1034 arg == MMC_TRIM_ARG ?
1035 INAND_CMD38_ARG_TRIM :
1036 INAND_CMD38_ARG_ERASE,
1037 0);
1038 if (err)
1039 goto out;
1040 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001041 err = mmc_erase(card, from, nr, arg);
1042out:
Adrian Hunter67716322011-08-29 16:42:15 +03001043 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1044 goto retry;
1045 if (!err)
1046 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301047 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001048
Adrian Hunterbd788c92010-08-11 14:17:47 -07001049 return err ? 0 : 1;
1050}
1051
Adrian Hunter49804542010-08-11 14:17:50 -07001052static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1053 struct request *req)
1054{
1055 struct mmc_blk_data *md = mq->data;
1056 struct mmc_card *card = md->queue.card;
1057 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001058 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001059
Maya Erez463bb952012-05-24 23:46:29 +03001060 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001061 err = -EOPNOTSUPP;
1062 goto out;
1063 }
1064
1065 from = blk_rq_pos(req);
1066 nr = blk_rq_sectors(req);
1067
1068 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1069 arg = MMC_SECURE_TRIM1_ARG;
1070 else
1071 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001072retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001073 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1074 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1075 INAND_CMD38_ARG_EXT_CSD,
1076 arg == MMC_SECURE_TRIM1_ARG ?
1077 INAND_CMD38_ARG_SECTRIM1 :
1078 INAND_CMD38_ARG_SECERASE,
1079 0);
1080 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001081 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001082 }
Adrian Hunter28302812012-04-05 14:45:48 +03001083
Adrian Hunter49804542010-08-11 14:17:50 -07001084 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001085 if (err == -EIO)
1086 goto out_retry;
1087 if (err)
1088 goto out;
1089
1090 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001091 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1092 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1093 INAND_CMD38_ARG_EXT_CSD,
1094 INAND_CMD38_ARG_SECTRIM2,
1095 0);
1096 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001097 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001098 }
Adrian Hunter28302812012-04-05 14:45:48 +03001099
Adrian Hunter49804542010-08-11 14:17:50 -07001100 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001101 if (err == -EIO)
1102 goto out_retry;
1103 if (err)
1104 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001105 }
Adrian Hunter28302812012-04-05 14:45:48 +03001106
Adrian Hunter28302812012-04-05 14:45:48 +03001107out_retry:
1108 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001109 goto retry;
1110 if (!err)
1111 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001112out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301113 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001114
Adrian Hunter49804542010-08-11 14:17:50 -07001115 return err ? 0 : 1;
1116}
1117
Maya Erez463bb952012-05-24 23:46:29 +03001118static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1119 struct request *req)
1120{
1121 struct mmc_blk_data *md = mq->data;
1122 struct mmc_card *card = md->queue.card;
1123 int err = 0;
1124
1125 BUG_ON(!card);
1126 BUG_ON(!card->host);
1127
1128 if (!(mmc_can_sanitize(card) &&
1129 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1130 pr_warning("%s: %s - SANITIZE is not supported\n",
1131 mmc_hostname(card->host), __func__);
1132 err = -EOPNOTSUPP;
1133 goto out;
1134 }
1135
1136 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1137 mmc_hostname(card->host), __func__);
1138
1139 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001140 EXT_CSD_SANITIZE_START, 1,
1141 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001142
1143 if (err)
1144 pr_err("%s: %s - mmc_switch() with "
1145 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1146 mmc_hostname(card->host), __func__, err);
1147
1148 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1149 __func__);
1150
1151out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301152 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001153
1154 return err ? 0 : 1;
1155}
1156
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001157static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1158{
1159 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001160 struct mmc_card *card = md->queue.card;
1161 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001162
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001163 ret = mmc_flush_cache(card);
1164 if (ret)
1165 ret = -EIO;
1166
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301167 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001168
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001169 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001170}
1171
1172/*
1173 * Reformat current write as a reliable write, supporting
1174 * both legacy and the enhanced reliable write MMC cards.
1175 * In each transfer we'll handle only as much as a single
1176 * reliable write can handle, thus finish the request in
1177 * partial completions.
1178 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001179static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1180 struct mmc_card *card,
1181 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001182{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001183 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1184 /* Legacy mode imposes restrictions on transfers. */
1185 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1186 brq->data.blocks = 1;
1187
1188 if (brq->data.blocks > card->ext_csd.rel_sectors)
1189 brq->data.blocks = card->ext_csd.rel_sectors;
1190 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1191 brq->data.blocks = 1;
1192 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001193}
1194
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001195#define CMD_ERRORS \
1196 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1197 R1_ADDRESS_ERROR | /* Misaligned address */ \
1198 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1199 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1200 R1_CC_ERROR | /* Card controller error */ \
1201 R1_ERROR) /* General/unknown error */
1202
Per Forlinee8a43a2011-07-01 18:55:33 +02001203static int mmc_blk_err_check(struct mmc_card *card,
1204 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001205{
Per Forlinee8a43a2011-07-01 18:55:33 +02001206 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1207 mmc_active);
1208 struct mmc_blk_request *brq = &mq_mrq->brq;
1209 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001210 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001211
1212 /*
1213 * sbc.error indicates a problem with the set block count
1214 * command. No data will have been transferred.
1215 *
1216 * cmd.error indicates a problem with the r/w command. No
1217 * data will have been transferred.
1218 *
1219 * stop.error indicates a problem with the stop command. Data
1220 * may have been transferred, or may still be transferring.
1221 */
Adrian Hunter67716322011-08-29 16:42:15 +03001222 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1223 brq->data.error) {
1224 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001225 case ERR_RETRY:
1226 return MMC_BLK_RETRY;
1227 case ERR_ABORT:
1228 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301229 case ERR_NOMEDIUM:
1230 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001231 case ERR_CONTINUE:
1232 break;
1233 }
1234 }
1235
1236 /*
1237 * Check for errors relating to the execution of the
1238 * initial command - such as address errors. No data
1239 * has been transferred.
1240 */
1241 if (brq->cmd.resp[0] & CMD_ERRORS) {
1242 pr_err("%s: r/w command failed, status = %#x\n",
1243 req->rq_disk->disk_name, brq->cmd.resp[0]);
1244 return MMC_BLK_ABORT;
1245 }
1246
1247 /*
1248 * Everything else is either success, or a data error of some
1249 * kind. If it was a write, we may have transitioned to
1250 * program mode, which we have to wait for it to complete.
1251 */
1252 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1253 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301254 unsigned long timeout;
1255
1256 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001257 do {
1258 int err = get_card_status(card, &status, 5);
1259 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301260 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001261 req->rq_disk->disk_name, err);
1262 return MMC_BLK_CMD_ERR;
1263 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301264
1265 /* Timeout if the device never becomes ready for data
1266 * and never leaves the program state.
1267 */
1268 if (time_after(jiffies, timeout)) {
1269 pr_err("%s: Card stuck in programming state!"\
1270 " %s %s\n", mmc_hostname(card->host),
1271 req->rq_disk->disk_name, __func__);
1272
1273 return MMC_BLK_CMD_ERR;
1274 }
Per Forlind78d4a82011-07-01 18:55:30 +02001275 /*
1276 * Some cards mishandle the status bits,
1277 * so make sure to check both the busy
1278 * indication and the card state.
1279 */
1280 } while (!(status & R1_READY_FOR_DATA) ||
1281 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1282 }
1283
1284 if (brq->data.error) {
1285 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1286 req->rq_disk->disk_name, brq->data.error,
1287 (unsigned)blk_rq_pos(req),
1288 (unsigned)blk_rq_sectors(req),
1289 brq->cmd.resp[0], brq->stop.resp[0]);
1290
1291 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001292 if (ecc_err)
1293 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001294 return MMC_BLK_DATA_ERR;
1295 } else {
1296 return MMC_BLK_CMD_ERR;
1297 }
1298 }
1299
Adrian Hunter67716322011-08-29 16:42:15 +03001300 if (!brq->data.bytes_xfered)
1301 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001302
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001303 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1304 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1305 return MMC_BLK_PARTIAL;
1306 else
1307 return MMC_BLK_SUCCESS;
1308 }
1309
Adrian Hunter67716322011-08-29 16:42:15 +03001310 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1311 return MMC_BLK_PARTIAL;
1312
1313 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001314}
1315
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001316static int mmc_blk_packed_err_check(struct mmc_card *card,
1317 struct mmc_async_req *areq)
1318{
1319 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1320 mmc_active);
1321 struct request *req = mq_rq->req;
1322 int err, check, status;
1323 u8 ext_csd[512];
1324
1325 mq_rq->packed_retries--;
1326 check = mmc_blk_err_check(card, areq);
1327 err = get_card_status(card, &status, 0);
1328 if (err) {
1329 pr_err("%s: error %d sending status command\n",
1330 req->rq_disk->disk_name, err);
1331 return MMC_BLK_ABORT;
1332 }
1333
1334 if (status & R1_EXCEPTION_EVENT) {
1335 err = mmc_send_ext_csd(card, ext_csd);
1336 if (err) {
1337 pr_err("%s: error %d sending ext_csd\n",
1338 req->rq_disk->disk_name, err);
1339 return MMC_BLK_ABORT;
1340 }
1341
1342 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1343 EXT_CSD_PACKED_FAILURE) &&
1344 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1345 EXT_CSD_PACKED_GENERIC_ERROR)) {
1346 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1347 EXT_CSD_PACKED_INDEXED_ERROR) {
1348 mq_rq->packed_fail_idx =
1349 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1350 return MMC_BLK_PARTIAL;
1351 }
1352 }
1353 }
1354
1355 return check;
1356}
1357
Per Forlin54d49d772011-07-01 18:55:29 +02001358static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1359 struct mmc_card *card,
1360 int disable_multi,
1361 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Per Forlin54d49d772011-07-01 18:55:29 +02001363 u32 readcmd, writecmd;
1364 struct mmc_blk_request *brq = &mqrq->brq;
1365 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301367 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001369 /*
1370 * Reliable writes are used to implement Forced Unit Access and
1371 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001372 *
1373 * XXX: this really needs a good explanation of why REQ_META
1374 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001375 */
1376 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1377 (req->cmd_flags & REQ_META)) &&
1378 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001379 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001380
Per Forlin54d49d772011-07-01 18:55:29 +02001381 memset(brq, 0, sizeof(struct mmc_blk_request));
1382 brq->mrq.cmd = &brq->cmd;
1383 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Per Forlin54d49d772011-07-01 18:55:29 +02001385 brq->cmd.arg = blk_rq_pos(req);
1386 if (!mmc_card_blockaddr(card))
1387 brq->cmd.arg <<= 9;
1388 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1389 brq->data.blksz = 512;
1390 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1391 brq->stop.arg = 0;
1392 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1393 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Asutosh Das1a897142012-07-27 18:10:19 +05301395 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001396 /*
1397 * The block layer doesn't support all sector count
1398 * restrictions, so we need to be prepared for too big
1399 * requests.
1400 */
1401 if (brq->data.blocks > card->host->max_blk_count)
1402 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001404 if (brq->data.blocks > 1) {
1405 /*
1406 * After a read error, we redo the request one sector
1407 * at a time in order to accurately determine which
1408 * sectors can be read successfully.
1409 */
1410 if (disable_multi)
1411 brq->data.blocks = 1;
1412
1413 /* Some controllers can't do multiblock reads due to hw bugs */
1414 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1415 rq_data_dir(req) == READ)
1416 brq->data.blocks = 1;
1417 }
Per Forlin54d49d772011-07-01 18:55:29 +02001418
1419 if (brq->data.blocks > 1 || do_rel_wr) {
1420 /* SPI multiblock writes terminate using a special
1421 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001422 */
Per Forlin54d49d772011-07-01 18:55:29 +02001423 if (!mmc_host_is_spi(card->host) ||
1424 rq_data_dir(req) == READ)
1425 brq->mrq.stop = &brq->stop;
1426 readcmd = MMC_READ_MULTIPLE_BLOCK;
1427 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1428 } else {
1429 brq->mrq.stop = NULL;
1430 readcmd = MMC_READ_SINGLE_BLOCK;
1431 writecmd = MMC_WRITE_BLOCK;
1432 }
1433 if (rq_data_dir(req) == READ) {
1434 brq->cmd.opcode = readcmd;
1435 brq->data.flags |= MMC_DATA_READ;
1436 } else {
1437 brq->cmd.opcode = writecmd;
1438 brq->data.flags |= MMC_DATA_WRITE;
1439 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001440
Per Forlin54d49d772011-07-01 18:55:29 +02001441 if (do_rel_wr)
1442 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001443
Per Forlin54d49d772011-07-01 18:55:29 +02001444 /*
Saugata Das42659002011-12-21 13:09:17 +05301445 * Data tag is used only during writing meta data to speed
1446 * up write and any subsequent read of this meta data
1447 */
1448 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1449 (req->cmd_flags & REQ_META) &&
1450 (rq_data_dir(req) == WRITE) &&
1451 ((brq->data.blocks * brq->data.blksz) >=
1452 card->ext_csd.data_tag_unit_size);
1453
1454 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001455 * Pre-defined multi-block transfers are preferable to
1456 * open ended-ones (and necessary for reliable writes).
1457 * However, it is not sufficient to just send CMD23,
1458 * and avoid the final CMD12, as on an error condition
1459 * CMD12 (stop) needs to be sent anyway. This, coupled
1460 * with Auto-CMD23 enhancements provided by some
1461 * hosts, means that the complexity of dealing
1462 * with this is best left to the host. If CMD23 is
1463 * supported by card and host, we'll fill sbc in and let
1464 * the host deal with handling it correctly. This means
1465 * that for hosts that don't expose MMC_CAP_CMD23, no
1466 * change of behavior will be observed.
1467 *
1468 * N.B: Some MMC cards experience perf degradation.
1469 * We'll avoid using CMD23-bounded multiblock writes for
1470 * these, while retaining features like reliable writes.
1471 */
Saugata Das42659002011-12-21 13:09:17 +05301472 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1473 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1474 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001475 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1476 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301477 (do_rel_wr ? (1 << 31) : 0) |
1478 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001479 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1480 brq->mrq.sbc = &brq->sbc;
1481 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001482
Per Forlin54d49d772011-07-01 18:55:29 +02001483 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001484
Per Forlin54d49d772011-07-01 18:55:29 +02001485 brq->data.sg = mqrq->sg;
1486 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001487
Per Forlin54d49d772011-07-01 18:55:29 +02001488 /*
1489 * Adjust the sg list so it is the same size as the
1490 * request.
1491 */
1492 if (brq->data.blocks != blk_rq_sectors(req)) {
1493 int i, data_size = brq->data.blocks << 9;
1494 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001495
Per Forlin54d49d772011-07-01 18:55:29 +02001496 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1497 data_size -= sg->length;
1498 if (data_size <= 0) {
1499 sg->length += data_size;
1500 i++;
1501 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001502 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001503 }
Per Forlin54d49d772011-07-01 18:55:29 +02001504 brq->data.sg_len = i;
1505 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001506
Per Forlinee8a43a2011-07-01 18:55:33 +02001507 mqrq->mmc_active.mrq = &brq->mrq;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001508 if (mq->err_check_fn)
1509 mqrq->mmc_active.err_check = mq->err_check_fn;
1510 else
1511 mqrq->mmc_active.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001512
Per Forlin54d49d772011-07-01 18:55:29 +02001513 mmc_queue_bounce_pre(mqrq);
1514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001516static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1517 struct request *req)
1518{
1519 struct mmc_host *host = mq->card->host;
1520 int data_dir;
1521
1522 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1523 return;
1524
Maya Erez6f219eb2012-12-02 13:27:15 +02001525 /* Support for the write packing on eMMC 4.5 or later */
1526 if (mq->card->ext_csd.rev <= 5)
1527 return;
1528
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001529 /*
1530 * In case the packing control is not supported by the host, it should
1531 * not have an effect on the write packing. Therefore we have to enable
1532 * the write packing
1533 */
1534 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1535 mq->wr_packing_enabled = true;
1536 return;
1537 }
1538
1539 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1540 if (mq->num_of_potential_packed_wr_reqs >
1541 mq->num_wr_reqs_to_start_packing)
1542 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001543 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001544 return;
1545 }
1546
1547 data_dir = rq_data_dir(req);
1548
1549 if (data_dir == READ) {
1550 mq->num_of_potential_packed_wr_reqs = 0;
1551 mq->wr_packing_enabled = false;
1552 return;
1553 } else if (data_dir == WRITE) {
1554 mq->num_of_potential_packed_wr_reqs++;
1555 }
1556
1557 if (mq->num_of_potential_packed_wr_reqs >
1558 mq->num_wr_reqs_to_start_packing)
1559 mq->wr_packing_enabled = true;
1560
1561}
1562
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001563struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1564{
1565 if (!card)
1566 return NULL;
1567
1568 return &card->wr_pack_stats;
1569}
1570EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1571
1572void mmc_blk_init_packed_statistics(struct mmc_card *card)
1573{
1574 int max_num_of_packed_reqs = 0;
1575
1576 if (!card || !card->wr_pack_stats.packing_events)
1577 return;
1578
1579 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1580
1581 spin_lock(&card->wr_pack_stats.lock);
1582 memset(card->wr_pack_stats.packing_events, 0,
1583 (max_num_of_packed_reqs + 1) *
1584 sizeof(*card->wr_pack_stats.packing_events));
1585 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1586 sizeof(card->wr_pack_stats.pack_stop_reason));
1587 card->wr_pack_stats.enabled = true;
1588 spin_unlock(&card->wr_pack_stats.lock);
1589}
1590EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1591
Lee Susman66842b02012-12-19 14:28:03 +02001592/**
1593 * mmc_blk_init_async_event_statistics() - Init async event
1594 * statistics data
1595 * @card: The mmc_card in which the async_event_stats
1596 * struct is a member
1597 *
1598 * Initiate counters for the new request feature, and mark the
1599 * statistics as enabled.
1600 */
1601void mmc_blk_init_async_event_statistics(struct mmc_card *card)
1602{
1603 if (!card)
1604 return;
1605
1606 /* init async events tests stats */
1607 memset(&card->async_event_stats,
1608 sizeof(struct mmc_async_event_stats), 0);
1609 card->async_event_stats.null_fetched = 0;
1610 card->async_event_stats.wakeup_new = 0;
1611 card->async_event_stats.new_request_flag = 0;
1612 card->async_event_stats.q_no_waiting = 0;
1613 card->async_event_stats.enabled = true;
1614 card->async_event_stats.no_mmc_request_action = 0;
1615 card->async_event_stats.wakeup_mq_thread = 0;
1616 card->async_event_stats.fetch_due_to_new_req = 0;
1617 card->async_event_stats.returned_new_req = 0;
1618 card->async_event_stats.done_flag = 0;
1619 card->async_event_stats.cmd_retry = 0;
1620 card->async_event_stats.done_when_new_req_event_on = 0;
1621 card->async_event_stats.new_req_when_new_marked = 0;
1622}
1623EXPORT_SYMBOL(mmc_blk_init_async_event_statistics);
1624
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001625static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1626{
1627 struct request_queue *q = mq->queue;
1628 struct mmc_card *card = mq->card;
1629 struct request *cur = req, *next = NULL;
1630 struct mmc_blk_data *md = mq->data;
1631 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1632 unsigned int req_sectors = 0, phys_segments = 0;
1633 unsigned int max_blk_count, max_phys_segs;
1634 u8 put_back = 0;
1635 u8 max_packed_rw = 0;
1636 u8 reqs = 0;
Lee Susman66842b02012-12-19 14:28:03 +02001637 struct mmc_wr_pack_stats *stats;
1638
1639 if (!card)
1640 goto no_packed;
1641
1642 stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001643
1644 mmc_blk_clear_packed(mq->mqrq_cur);
1645
1646 if (!(md->flags & MMC_BLK_CMD23) ||
1647 !card->ext_csd.packed_event_en)
1648 goto no_packed;
1649
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001650 if (!mq->wr_packing_enabled)
1651 goto no_packed;
1652
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001653 if ((rq_data_dir(cur) == WRITE) &&
1654 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1655 max_packed_rw = card->ext_csd.max_packed_writes;
1656
1657 if (max_packed_rw == 0)
1658 goto no_packed;
1659
1660 if (mmc_req_rel_wr(cur) &&
1661 (md->flags & MMC_BLK_REL_WR) &&
1662 !en_rel_wr)
1663 goto no_packed;
1664
1665 if (mmc_large_sec(card) &&
1666 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1667 goto no_packed;
1668
1669 max_blk_count = min(card->host->max_blk_count,
1670 card->host->max_req_size >> 9);
1671 if (unlikely(max_blk_count > 0xffff))
1672 max_blk_count = 0xffff;
1673
1674 max_phys_segs = queue_max_segments(q);
1675 req_sectors += blk_rq_sectors(cur);
1676 phys_segments += cur->nr_phys_segments;
1677
1678 if (rq_data_dir(cur) == WRITE) {
1679 req_sectors++;
1680 phys_segments++;
1681 }
1682
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001683 spin_lock(&stats->lock);
1684
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001685 while (reqs < max_packed_rw - 1) {
1686 spin_lock_irq(q->queue_lock);
1687 next = blk_fetch_request(q);
1688 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001689 if (!next) {
1690 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001691 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001692 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001693
1694 if (mmc_large_sec(card) &&
1695 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001696 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001697 put_back = 1;
1698 break;
1699 }
1700
1701 if (next->cmd_flags & REQ_DISCARD ||
1702 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001703 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001704 put_back = 1;
1705 break;
1706 }
1707
1708 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001709 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001710 put_back = 1;
1711 break;
1712 }
1713
1714 if (mmc_req_rel_wr(next) &&
1715 (md->flags & MMC_BLK_REL_WR) &&
1716 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001717 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001718 put_back = 1;
1719 break;
1720 }
1721
1722 req_sectors += blk_rq_sectors(next);
1723 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001724 if (stats->enabled)
1725 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001726 put_back = 1;
1727 break;
1728 }
1729
1730 phys_segments += next->nr_phys_segments;
1731 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001732 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001733 put_back = 1;
1734 break;
1735 }
1736
Maya Erez2d33a192013-01-04 15:52:41 +02001737 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001738 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02001739 if (card->ext_csd.bkops_en)
1740 card->bkops_info.sectors_changed +=
1741 blk_rq_sectors(next);
1742 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001743 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1744 cur = next;
1745 reqs++;
1746 }
1747
1748 if (put_back) {
1749 spin_lock_irq(q->queue_lock);
1750 blk_requeue_request(q, next);
1751 spin_unlock_irq(q->queue_lock);
1752 }
1753
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001754 if (stats->enabled) {
1755 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1756 stats->packing_events[reqs + 1]++;
1757 if (reqs + 1 == max_packed_rw)
1758 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1759 }
1760
1761 spin_unlock(&stats->lock);
1762
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001763 if (reqs > 0) {
1764 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1765 mq->mqrq_cur->packed_num = ++reqs;
1766 mq->mqrq_cur->packed_retries = reqs;
1767 return reqs;
1768 }
1769
1770no_packed:
1771 mmc_blk_clear_packed(mq->mqrq_cur);
1772 return 0;
1773}
1774
1775static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1776 struct mmc_card *card,
1777 struct mmc_queue *mq)
1778{
1779 struct mmc_blk_request *brq = &mqrq->brq;
1780 struct request *req = mqrq->req;
1781 struct request *prq;
1782 struct mmc_blk_data *md = mq->data;
1783 bool do_rel_wr, do_data_tag;
1784 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1785 u8 i = 1;
1786
1787 mqrq->packed_cmd = MMC_PACKED_WRITE;
1788 mqrq->packed_blocks = 0;
1789 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1790
1791 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1792 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1793 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1794
1795 /*
1796 * Argument for each entry of packed group
1797 */
1798 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1799 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1800 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1801 (prq->cmd_flags & REQ_META) &&
1802 (rq_data_dir(prq) == WRITE) &&
1803 ((brq->data.blocks * brq->data.blksz) >=
1804 card->ext_csd.data_tag_unit_size);
1805 /* Argument of CMD23 */
1806 packed_cmd_hdr[(i * 2)] =
1807 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1808 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1809 blk_rq_sectors(prq);
1810 /* Argument of CMD18 or CMD25 */
1811 packed_cmd_hdr[((i * 2)) + 1] =
1812 mmc_card_blockaddr(card) ?
1813 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1814 mqrq->packed_blocks += blk_rq_sectors(prq);
1815 i++;
1816 }
1817
1818 memset(brq, 0, sizeof(struct mmc_blk_request));
1819 brq->mrq.cmd = &brq->cmd;
1820 brq->mrq.data = &brq->data;
1821 brq->mrq.sbc = &brq->sbc;
1822 brq->mrq.stop = &brq->stop;
1823
1824 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1825 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1826 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1827
1828 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1829 brq->cmd.arg = blk_rq_pos(req);
1830 if (!mmc_card_blockaddr(card))
1831 brq->cmd.arg <<= 9;
1832 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1833
1834 brq->data.blksz = 512;
1835 brq->data.blocks = mqrq->packed_blocks + 1;
1836 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02001837 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001838
1839 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1840 brq->stop.arg = 0;
1841 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1842
1843 mmc_set_data_timeout(&brq->data, card);
1844
1845 brq->data.sg = mqrq->sg;
1846 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1847
1848 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001849
1850 /*
1851 * This is intended for packed commands tests usage - in case these
1852 * functions are not in use the respective pointers are NULL
1853 */
1854 if (mq->err_check_fn)
1855 mqrq->mmc_active.err_check = mq->err_check_fn;
1856 else
1857 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1858
1859 if (mq->packed_test_fn)
1860 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001861
1862 mmc_queue_bounce_pre(mqrq);
1863}
1864
Adrian Hunter67716322011-08-29 16:42:15 +03001865static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1866 struct mmc_blk_request *brq, struct request *req,
1867 int ret)
1868{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001869 struct mmc_queue_req *mq_rq;
1870 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1871
Adrian Hunter67716322011-08-29 16:42:15 +03001872 /*
1873 * If this is an SD card and we're writing, we can first
1874 * mark the known good sectors as ok.
1875 *
1876 * If the card is not SD, we can still ok written sectors
1877 * as reported by the controller (which might be less than
1878 * the real number of written sectors, but never more).
1879 */
1880 if (mmc_card_sd(card)) {
1881 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301882 if (!brq->data.fault_injected) {
1883 blocks = mmc_sd_num_wr_blocks(card);
1884 if (blocks != (u32)-1)
1885 ret = blk_end_request(req, 0, blocks << 9);
1886 } else
1887 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001888 } else {
1889 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1890 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1891 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001892 return ret;
1893}
1894
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001895static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1896{
1897 struct request *prq;
1898 int idx = mq_rq->packed_fail_idx, i = 0;
1899 int ret = 0;
1900
1901 while (!list_empty(&mq_rq->packed_list)) {
1902 prq = list_entry_rq(mq_rq->packed_list.next);
1903 if (idx == i) {
1904 /* retry from error index */
1905 mq_rq->packed_num -= idx;
1906 mq_rq->req = prq;
1907 ret = 1;
1908
1909 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1910 list_del_init(&prq->queuelist);
1911 mmc_blk_clear_packed(mq_rq);
1912 }
1913 return ret;
1914 }
1915 list_del_init(&prq->queuelist);
1916 blk_end_request(prq, 0, blk_rq_bytes(prq));
1917 i++;
1918 }
1919
1920 mmc_blk_clear_packed(mq_rq);
1921 return ret;
1922}
1923static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1924{
1925 struct request *prq;
1926
1927 while (!list_empty(&mq_rq->packed_list)) {
1928 prq = list_entry_rq(mq_rq->packed_list.next);
1929 list_del_init(&prq->queuelist);
1930 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1931 }
1932
1933 mmc_blk_clear_packed(mq_rq);
1934}
1935
1936static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1937 struct mmc_queue_req *mq_rq)
1938{
1939 struct request *prq;
1940 struct request_queue *q = mq->queue;
1941
1942 while (!list_empty(&mq_rq->packed_list)) {
1943 prq = list_entry_rq(mq_rq->packed_list.prev);
1944 if (prq->queuelist.prev != &mq_rq->packed_list) {
1945 list_del_init(&prq->queuelist);
1946 spin_lock_irq(q->queue_lock);
1947 blk_requeue_request(mq->queue, prq);
1948 spin_unlock_irq(q->queue_lock);
1949 } else {
1950 list_del_init(&prq->queuelist);
1951 }
1952 }
1953
1954 mmc_blk_clear_packed(mq_rq);
1955}
1956
Per Forlinee8a43a2011-07-01 18:55:33 +02001957static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001958{
1959 struct mmc_blk_data *md = mq->data;
1960 struct mmc_card *card = md->queue.card;
1961 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001962 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001963 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001964 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001965 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001966 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001967 const u8 packed_num = 2;
1968 u8 reqs = 0;
Lee Susman66842b02012-12-19 14:28:03 +02001969 struct mmc_async_event_stats *stats = &card->async_event_stats;
Per Forlinee8a43a2011-07-01 18:55:33 +02001970
1971 if (!rqc && !mq->mqrq_prev->req)
1972 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001973
Maya Erez5f360692012-10-10 03:47:54 +02001974 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02001975 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1976 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001977 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001978 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001979
Per Forlin54d49d772011-07-01 18:55:29 +02001980 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001981 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001982 if (reqs >= packed_num)
1983 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1984 card, mq);
1985 else
1986 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001987 areq = &mq->mqrq_cur->mmc_active;
1988 } else
1989 areq = NULL;
1990 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001991 if (!areq) {
Lee Susman66842b02012-12-19 14:28:03 +02001992 if (status == MMC_BLK_NEW_REQUEST && stats) {
1993 if (stats->enabled)
1994 stats->returned_new_req++;
1995
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001996 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Lee Susman66842b02012-12-19 14:28:03 +02001997 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001998 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001999 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002000
Per Forlinee8a43a2011-07-01 18:55:33 +02002001 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2002 brq = &mq_rq->brq;
2003 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002004 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002005 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002006
Per Forlind78d4a82011-07-01 18:55:30 +02002007 switch (status) {
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002008 case MMC_BLK_NEW_REQUEST:
2009 BUG(); /* should never get here */
Per Forlind78d4a82011-07-01 18:55:30 +02002010 case MMC_BLK_SUCCESS:
2011 case MMC_BLK_PARTIAL:
2012 /*
2013 * A block was successfully transferred.
2014 */
Adrian Hunter67716322011-08-29 16:42:15 +03002015 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002016
2017 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2018 ret = mmc_blk_end_packed_req(mq_rq);
2019 break;
2020 } else {
2021 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002022 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002023 }
2024
Adrian Hunter67716322011-08-29 16:42:15 +03002025 /*
2026 * If the blk_end_request function returns non-zero even
2027 * though all data has been transferred and no errors
2028 * were returned by the host controller, it's a bug.
2029 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002030 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302031 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002032 __func__, blk_rq_bytes(req),
2033 brq->data.bytes_xfered);
2034 rqc = NULL;
2035 goto cmd_abort;
2036 }
Per Forlind78d4a82011-07-01 18:55:30 +02002037 break;
2038 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002039 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2040 if (!mmc_blk_reset(md, card->host, type))
2041 break;
2042 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002043 case MMC_BLK_RETRY:
2044 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002045 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002046 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002047 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002048 if (!mmc_blk_reset(md, card->host, type))
2049 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002050 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002051 case MMC_BLK_DATA_ERR: {
2052 int err;
2053
2054 err = mmc_blk_reset(md, card->host, type);
2055 if (!err)
2056 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002057 if (err == -ENODEV ||
2058 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002059 goto cmd_abort;
2060 /* Fall through */
2061 }
2062 case MMC_BLK_ECC_ERR:
2063 if (brq->data.blocks > 1) {
2064 /* Redo read one sector at a time */
2065 pr_warning("%s: retrying using single block read\n",
2066 req->rq_disk->disk_name);
2067 disable_multi = 1;
2068 break;
2069 }
Per Forlind78d4a82011-07-01 18:55:30 +02002070 /*
2071 * After an error, we redo I/O one sector at a
2072 * time, so we only reach here after trying to
2073 * read a single sector.
2074 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302075 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002076 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002077 if (!ret)
2078 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002079 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302080 case MMC_BLK_NOMEDIUM:
2081 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002082 }
2083
Per Forlinee8a43a2011-07-01 18:55:33 +02002084 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002085 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2086 /*
2087 * In case of a incomplete request
2088 * prepare it again and resend.
2089 */
2090 mmc_blk_rw_rq_prep(mq_rq, card,
2091 disable_multi, mq);
2092 mmc_start_req(card->host,
2093 &mq_rq->mmc_active, NULL);
2094 } else {
2095 if (!mq_rq->packed_retries)
2096 goto cmd_abort;
2097 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2098 mmc_start_req(card->host,
2099 &mq_rq->mmc_active, NULL);
2100 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } while (ret);
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 1;
2105
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002106 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002107 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2108 if (mmc_card_removed(card))
2109 req->cmd_flags |= REQ_QUIET;
2110 while (ret)
2111 ret = blk_end_request(req, -EIO,
2112 blk_rq_cur_bytes(req));
2113 } else {
2114 mmc_blk_abort_packed_req(mq_rq);
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Per Forlinee8a43a2011-07-01 18:55:33 +02002117 start_new_req:
2118 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002119 /*
2120 * If current request is packed, it needs to put back.
2121 */
2122 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2123 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2124
Per Forlinee8a43a2011-07-01 18:55:33 +02002125 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2126 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2127 }
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return 0;
2130}
2131
Adrian Hunterbd788c92010-08-11 14:17:47 -07002132static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2133{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002134 int ret;
2135 struct mmc_blk_data *md = mq->data;
2136 struct mmc_card *card = md->queue.card;
2137
San Mehat148c57a2009-07-30 08:21:19 -07002138#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2139 if (mmc_bus_needs_resume(card->host)) {
2140 mmc_resume_bus(card->host);
2141 mmc_blk_set_blksize(md, card);
2142 }
2143#endif
2144
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002145 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002146 /* claim host only for the first request */
2147 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002148 if (card->ext_csd.bkops_en)
2149 mmc_stop_bkops(card);
2150 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002151
Andrei Warkentin371a6892011-04-11 18:10:25 -05002152 ret = mmc_blk_part_switch(card, md);
2153 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002154 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302155 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002156 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002157 ret = 0;
2158 goto out;
2159 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002160
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002161 mmc_blk_write_packing_control(mq, req);
2162
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002163 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002164 if (req && req->cmd_flags & REQ_SANITIZE) {
2165 /* complete ongoing async transfer before issuing sanitize */
2166 if (card->host && card->host->areq)
2167 mmc_blk_issue_rw_rq(mq, NULL);
2168 ret = mmc_blk_issue_sanitize_rq(mq, req);
2169 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002170 /* complete ongoing async transfer before issuing discard */
2171 if (card->host->areq)
2172 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302173 if (req->cmd_flags & REQ_SECURE &&
2174 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002175 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002176 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002177 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002178 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002179 /* complete ongoing async transfer before issuing flush */
2180 if (card->host->areq)
2181 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002182 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002183 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002184 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002185 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002186
Andrei Warkentin371a6892011-04-11 18:10:25 -05002187out:
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002188 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) {
2189 if (mmc_card_need_bkops(card))
2190 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002191 /* release host only when there are no more requests */
2192 mmc_release_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002193 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002194 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Russell Kinga6f6c962006-01-03 22:38:44 +00002197static inline int mmc_blk_readonly(struct mmc_card *card)
2198{
2199 return mmc_card_readonly(card) ||
2200 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2201}
2202
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2204 struct device *parent,
2205 sector_t size,
2206 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002207 const char *subname,
2208 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 struct mmc_blk_data *md;
2211 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002212 unsigned int percentage =
2213 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002215 devidx = find_first_zero_bit(dev_use, max_devices);
2216 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return ERR_PTR(-ENOSPC);
2218 __set_bit(devidx, dev_use);
2219
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002220 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002221 if (!md) {
2222 ret = -ENOMEM;
2223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002225
Russell Kinga6f6c962006-01-03 22:38:44 +00002226 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002227 * !subname implies we are creating main mmc_blk_data that will be
2228 * associated with mmc_card with mmc_set_drvdata. Due to device
2229 * partitions, devidx will not coincide with a per-physical card
2230 * index anymore so we keep track of a name index.
2231 */
2232 if (!subname) {
2233 md->name_idx = find_first_zero_bit(name_use, max_devices);
2234 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002235 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002236 md->name_idx = ((struct mmc_blk_data *)
2237 dev_to_disk(parent)->private_data)->name_idx;
2238
Johan Rudholmadd710e2011-12-02 08:51:06 +01002239 md->area_type = area_type;
2240
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002241 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002242 * Set the read-only status based on the supported commands
2243 * and the write protect switch.
2244 */
2245 md->read_only = mmc_blk_readonly(card);
2246
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002247 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002248 if (md->disk == NULL) {
2249 ret = -ENOMEM;
2250 goto err_kfree;
2251 }
2252
2253 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002254 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002255 md->usage = 1;
2256
Adrian Hunterd09408a2011-06-23 13:40:28 +03002257 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002258 if (ret)
2259 goto err_putdisk;
2260
Russell Kinga6f6c962006-01-03 22:38:44 +00002261 md->queue.issue_fn = mmc_blk_issue_rq;
2262 md->queue.data = md;
2263
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002264 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002265 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002266 md->disk->fops = &mmc_bdops;
2267 md->disk->private_data = md;
2268 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002269 md->disk->driverfs_dev = parent;
2270 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002271 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002272 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2273 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002274
2275 /*
2276 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2277 *
2278 * - be set for removable media with permanent block devices
2279 * - be unset for removable block devices with permanent media
2280 *
2281 * Since MMC block devices clearly fall under the second
2282 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2283 * should use the block device creation/destruction hotplug
2284 * messages to tell when the card is present.
2285 */
2286
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002287 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2288 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002289
Martin K. Petersene1defc42009-05-22 17:17:49 -04002290 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002291 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002292
Maya Ereze1eae982013-01-05 21:51:06 +02002293 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2294 card->bkops_info.min_sectors_to_queue_delayed_work =
2295 ((unsigned int)size * percentage) / 100;
2296
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002297 if (mmc_host_cmd23(card->host)) {
2298 if (mmc_card_mmc(card) ||
2299 (mmc_card_sd(card) &&
2300 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2301 md->flags |= MMC_BLK_CMD23;
2302 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002303
2304 if (mmc_card_mmc(card) &&
2305 md->flags & MMC_BLK_CMD23 &&
2306 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2307 card->ext_csd.rel_sectors)) {
2308 md->flags |= MMC_BLK_REL_WR;
2309 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2310 }
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002313
2314 err_putdisk:
2315 put_disk(md->disk);
2316 err_kfree:
2317 kfree(md);
2318 out:
2319 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Andrei Warkentin371a6892011-04-11 18:10:25 -05002322static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2323{
2324 sector_t size;
2325 struct mmc_blk_data *md;
2326
2327 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2328 /*
2329 * The EXT_CSD sector count is in number or 512 byte
2330 * sectors.
2331 */
2332 size = card->ext_csd.sectors;
2333 } else {
2334 /*
2335 * The CSD capacity field is in units of read_blkbits.
2336 * set_capacity takes units of 512 bytes.
2337 */
2338 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2339 }
2340
Johan Rudholmadd710e2011-12-02 08:51:06 +01002341 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2342 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002343 return md;
2344}
2345
2346static int mmc_blk_alloc_part(struct mmc_card *card,
2347 struct mmc_blk_data *md,
2348 unsigned int part_type,
2349 sector_t size,
2350 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002351 const char *subname,
2352 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002353{
2354 char cap_str[10];
2355 struct mmc_blk_data *part_md;
2356
2357 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002358 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002359 if (IS_ERR(part_md))
2360 return PTR_ERR(part_md);
2361 part_md->part_type = part_type;
2362 list_add(&part_md->part, &md->part);
2363
2364 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2365 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302366 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002367 part_md->disk->disk_name, mmc_card_id(card),
2368 mmc_card_name(card), part_md->part_type, cap_str);
2369 return 0;
2370}
2371
Namjae Jeone0c368d2011-10-06 23:41:38 +09002372/* MMC Physical partitions consist of two boot partitions and
2373 * up to four general purpose partitions.
2374 * For each partition enabled in EXT_CSD a block device will be allocatedi
2375 * to provide access to the partition.
2376 */
2377
Andrei Warkentin371a6892011-04-11 18:10:25 -05002378static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2379{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002380 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002381
2382 if (!mmc_card_mmc(card))
2383 return 0;
2384
Namjae Jeone0c368d2011-10-06 23:41:38 +09002385 for (idx = 0; idx < card->nr_parts; idx++) {
2386 if (card->part[idx].size) {
2387 ret = mmc_blk_alloc_part(card, md,
2388 card->part[idx].part_cfg,
2389 card->part[idx].size >> 9,
2390 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002391 card->part[idx].name,
2392 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002393 if (ret)
2394 return ret;
2395 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002396 }
2397
2398 return ret;
2399}
2400
Andrei Warkentin371a6892011-04-11 18:10:25 -05002401static void mmc_blk_remove_req(struct mmc_blk_data *md)
2402{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002403 struct mmc_card *card;
2404
Andrei Warkentin371a6892011-04-11 18:10:25 -05002405 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002406 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002407 device_remove_file(disk_to_dev(md->disk),
2408 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002409 if (md->disk->flags & GENHD_FL_UP) {
2410 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002411 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2412 card->ext_csd.boot_ro_lockable)
2413 device_remove_file(disk_to_dev(md->disk),
2414 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002415
2416 /* Stop new requests from getting into the queue */
2417 del_gendisk(md->disk);
2418 }
2419
2420 /* Then flush out any already in there */
2421 mmc_cleanup_queue(&md->queue);
2422 mmc_blk_put(md);
2423 }
2424}
2425
2426static void mmc_blk_remove_parts(struct mmc_card *card,
2427 struct mmc_blk_data *md)
2428{
2429 struct list_head *pos, *q;
2430 struct mmc_blk_data *part_md;
2431
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002432 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002433 list_for_each_safe(pos, q, &md->part) {
2434 part_md = list_entry(pos, struct mmc_blk_data, part);
2435 list_del(pos);
2436 mmc_blk_remove_req(part_md);
2437 }
2438}
2439
2440static int mmc_add_disk(struct mmc_blk_data *md)
2441{
2442 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002443 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002444
2445 add_disk(md->disk);
2446 md->force_ro.show = force_ro_show;
2447 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302448 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002449 md->force_ro.attr.name = "force_ro";
2450 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2451 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2452 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002453 goto force_ro_fail;
2454
2455 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2456 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002457 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002458
2459 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2460 mode = S_IRUGO;
2461 else
2462 mode = S_IRUGO | S_IWUSR;
2463
2464 md->power_ro_lock.show = power_ro_lock_show;
2465 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002466 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002467 md->power_ro_lock.attr.mode = mode;
2468 md->power_ro_lock.attr.name =
2469 "ro_lock_until_next_power_on";
2470 ret = device_create_file(disk_to_dev(md->disk),
2471 &md->power_ro_lock);
2472 if (ret)
2473 goto power_ro_lock_fail;
2474 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002475
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002476 md->num_wr_reqs_to_start_packing.show =
2477 num_wr_reqs_to_start_packing_show;
2478 md->num_wr_reqs_to_start_packing.store =
2479 num_wr_reqs_to_start_packing_store;
2480 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2481 md->num_wr_reqs_to_start_packing.attr.name =
2482 "num_wr_reqs_to_start_packing";
2483 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2484 ret = device_create_file(disk_to_dev(md->disk),
2485 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002486 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002487 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002488
Maya Ereze1eae982013-01-05 21:51:06 +02002489 md->bkops_check_threshold.show = bkops_check_threshold_show;
2490 md->bkops_check_threshold.store = bkops_check_threshold_store;
2491 sysfs_attr_init(&md->bkops_check_threshold.attr);
2492 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2493 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002494 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002495 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002496 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002497 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002498
Johan Rudholmadd710e2011-12-02 08:51:06 +01002499 return ret;
2500
Maya Ereze1eae982013-01-05 21:51:06 +02002501bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02002502 device_remove_file(disk_to_dev(md->disk),
2503 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002504num_wr_reqs_to_start_packing_fail:
2505 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002506power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002507 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002508force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002509 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002510
2511 return ret;
2512}
2513
Chris Ballc59d4472011-11-11 22:01:43 -05002514#define CID_MANFID_SANDISK 0x2
2515#define CID_MANFID_TOSHIBA 0x11
2516#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302517#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002518
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002519static const struct mmc_fixup blk_fixups[] =
2520{
Chris Ballc59d4472011-11-11 22:01:43 -05002521 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2522 MMC_QUIRK_INAND_CMD38),
2523 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2524 MMC_QUIRK_INAND_CMD38),
2525 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2526 MMC_QUIRK_INAND_CMD38),
2527 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2528 MMC_QUIRK_INAND_CMD38),
2529 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2530 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002531
2532 /*
2533 * Some MMC cards experience performance degradation with CMD23
2534 * instead of CMD12-bounded multiblock transfers. For now we'll
2535 * black list what's bad...
2536 * - Certain Toshiba cards.
2537 *
2538 * N.B. This doesn't affect SD cards.
2539 */
Chris Ballc59d4472011-11-11 22:01:43 -05002540 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002541 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002542 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002543 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002544 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002545 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002546
2547 /*
2548 * Some Micron MMC cards needs longer data read timeout than
2549 * indicated in CSD.
2550 */
Chris Ballc59d4472011-11-11 22:01:43 -05002551 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002552 MMC_QUIRK_LONG_READ_TIME),
2553
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302554 /* Some INAND MCP devices advertise incorrect timeout values */
2555 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2556 MMC_QUIRK_INAND_DATA_TIMEOUT),
2557
Ian Chenc4856122012-11-01 13:26:21 +05302558 /*
2559 * On these Samsung MoviNAND parts, performing secure erase or
2560 * secure trim can result in unrecoverable corruption due to a
2561 * firmware bug.
2562 */
2563 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2564 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2565 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2566 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2567 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2568 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2569 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2570 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2571 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2572 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2573 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2574 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2575 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2576 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2577 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2578 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2579
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002580 END_FIXUP
2581};
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583static int mmc_blk_probe(struct mmc_card *card)
2584{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002585 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002586 char cap_str[10];
2587
Pierre Ossman912490d2005-05-21 10:27:02 +01002588 /*
2589 * Check that the card supports the command class(es) we need.
2590 */
2591 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return -ENODEV;
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 md = mmc_blk_alloc(card);
2595 if (IS_ERR(md))
2596 return PTR_ERR(md);
2597
Yi Li444122f2009-02-05 15:31:57 +08002598 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002599 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302600 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002602 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Andrei Warkentin371a6892011-04-11 18:10:25 -05002604 if (mmc_blk_alloc_parts(card, md))
2605 goto out;
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002608 mmc_fixup_device(card, blk_fixups);
2609
San Mehat148c57a2009-07-30 08:21:19 -07002610#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2611 mmc_set_bus_resume_policy(card->host, 1);
2612#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002613 if (mmc_add_disk(md))
2614 goto out;
2615
2616 list_for_each_entry(part_md, &md->part, part) {
2617 if (mmc_add_disk(part_md))
2618 goto out;
2619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return 0;
2621
2622 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002623 mmc_blk_remove_parts(card, md);
2624 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
2628static void mmc_blk_remove(struct mmc_card *card)
2629{
2630 struct mmc_blk_data *md = mmc_get_drvdata(card);
2631
Andrei Warkentin371a6892011-04-11 18:10:25 -05002632 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002633 mmc_claim_host(card->host);
2634 mmc_blk_part_switch(card, md);
2635 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002636 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002638#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2639 mmc_set_bus_resume_policy(card->host, 0);
2640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002644static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002646 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct mmc_blk_data *md = mmc_get_drvdata(card);
2648
2649 if (md) {
2650 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002651 list_for_each_entry(part_md, &md->part, part) {
2652 mmc_queue_suspend(&part_md->queue);
2653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655 return 0;
2656}
2657
2658static int mmc_blk_resume(struct mmc_card *card)
2659{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002660 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct mmc_blk_data *md = mmc_get_drvdata(card);
2662
2663 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002664 /*
2665 * Resume involves the card going into idle state,
2666 * so current partition is always the main one.
2667 */
2668 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002670 list_for_each_entry(part_md, &md->part, part) {
2671 mmc_queue_resume(&part_md->queue);
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674 return 0;
2675}
2676#else
2677#define mmc_blk_suspend NULL
2678#define mmc_blk_resume NULL
2679#endif
2680
2681static struct mmc_driver mmc_driver = {
2682 .drv = {
2683 .name = "mmcblk",
2684 },
2685 .probe = mmc_blk_probe,
2686 .remove = mmc_blk_remove,
2687 .suspend = mmc_blk_suspend,
2688 .resume = mmc_blk_resume,
2689};
2690
2691static int __init mmc_blk_init(void)
2692{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002693 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002695 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2696 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2697
2698 max_devices = 256 / perdev_minors;
2699
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002700 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2701 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002704 res = mmc_register_driver(&mmc_driver);
2705 if (res)
2706 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002708 return 0;
2709 out2:
2710 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 out:
2712 return res;
2713}
2714
2715static void __exit mmc_blk_exit(void)
2716{
2717 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002718 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
2721module_init(mmc_blk_init);
2722module_exit(mmc_blk_exit);
2723
2724MODULE_LICENSE("GPL");
2725MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2726