blob: e515f51481b2adc4a83bcbc14954463add8beb76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020068#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
69 do { \
70 if (stats->enabled) \
71 stats->pack_stop_reason[reason]++; \
72 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020073
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020074static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040075
76/*
77 * The defaults come from config options but can be overriden by module
78 * or bootarg options.
79 */
80static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
81
82/*
83 * We've only got one major, so number of mmcblk devices is
84 * limited to 256 / number of minors per device.
85 */
86static int max_devices;
87
88/* 256 minors, so at most 256 separate devices */
89static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050090static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
97 struct gendisk *disk;
98 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050099 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500101 unsigned int flags;
102#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
103#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000106 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500107 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500108 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300109 unsigned int reset_done;
110#define MMC_BLK_READ BIT(0)
111#define MMC_BLK_WRITE BIT(1)
112#define MMC_BLK_DISCARD BIT(2)
113#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500114
115 /*
116 * Only set in main mmc_blk_data associated
117 * with mmc_card with mmc_set_drvdata, and keeps
118 * track of the current selected device partition.
119 */
120 unsigned int part_curr;
121 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100122 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200123 struct device_attribute num_wr_reqs_to_start_packing;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200124 struct device_attribute min_sectors_to_check_bkops_status;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200130enum {
131 MMC_PACKED_N_IDX = -1,
132 MMC_PACKED_N_ZERO,
133 MMC_PACKED_N_SINGLE,
134};
135
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400136module_param(perdev_minors, int, 0444);
137MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
138
Loic Pallardy961e37a2012-08-06 15:12:31 +0000139static inline int mmc_blk_part_switch(struct mmc_card *card,
140 struct mmc_blk_data *md);
141static int get_card_status(struct mmc_card *card, u32 *status, int retries);
142
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200143static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
144{
145 mqrq->packed_cmd = MMC_PACKED_NONE;
146 mqrq->packed_num = MMC_PACKED_N_ZERO;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
150{
151 struct mmc_blk_data *md;
152
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 md = disk->private_data;
155 if (md && md->usage == 0)
156 md = NULL;
157 if (md)
158 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000159 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return md;
162}
163
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164static inline int mmc_get_devidx(struct gendisk *disk)
165{
Colin Cross71b54d42010-09-03 12:41:21 -0700166 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500167 return devidx;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void mmc_blk_put(struct mmc_blk_data *md)
171{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000172 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 md->usage--;
174 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500175 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800176 blk_cleanup_queue(md->queue.queue);
177
David Woodhouse1dff3142007-11-21 18:45:12 +0100178 __clear_bit(devidx, dev_use);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 kfree(md);
182 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000183 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Johan Rudholmadd710e2011-12-02 08:51:06 +0100186static ssize_t power_ro_lock_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
189 int ret;
190 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
191 struct mmc_card *card = md->queue.card;
192 int locked = 0;
193
194 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
195 locked = 2;
196 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
197 locked = 1;
198
199 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
200
201 return ret;
202}
203
204static ssize_t power_ro_lock_store(struct device *dev,
205 struct device_attribute *attr, const char *buf, size_t count)
206{
207 int ret;
208 struct mmc_blk_data *md, *part_md;
209 struct mmc_card *card;
210 unsigned long set;
211
212 if (kstrtoul(buf, 0, &set))
213 return -EINVAL;
214
215 if (set != 1)
216 return count;
217
218 md = mmc_blk_get(dev_to_disk(dev));
219 card = md->queue.card;
220
221 mmc_claim_host(card->host);
222
223 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
224 card->ext_csd.boot_ro_lock |
225 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
226 card->ext_csd.part_time);
227 if (ret)
228 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
229 else
230 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
231
232 mmc_release_host(card->host);
233
234 if (!ret) {
235 pr_info("%s: Locking boot partition ro until next power on\n",
236 md->disk->disk_name);
237 set_disk_ro(md->disk, 1);
238
239 list_for_each_entry(part_md, &md->part, part)
240 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
241 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
242 set_disk_ro(part_md->disk, 1);
243 }
244 }
245
246 mmc_blk_put(md);
247 return count;
248}
249
Andrei Warkentin371a6892011-04-11 18:10:25 -0500250static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
251 char *buf)
252{
253 int ret;
254 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
255
256 ret = snprintf(buf, PAGE_SIZE, "%d",
257 get_disk_ro(dev_to_disk(dev)) ^
258 md->read_only);
259 mmc_blk_put(md);
260 return ret;
261}
262
263static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
264 const char *buf, size_t count)
265{
266 int ret;
267 char *end;
268 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
269 unsigned long set = simple_strtoul(buf, &end, 0);
270 if (end == buf) {
271 ret = -EINVAL;
272 goto out;
273 }
274
275 set_disk_ro(dev_to_disk(dev), set || md->read_only);
276 ret = count;
277out:
278 mmc_blk_put(md);
279 return ret;
280}
281
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200282static ssize_t
283num_wr_reqs_to_start_packing_show(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
286 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
287 int num_wr_reqs_to_start_packing;
288 int ret;
289
290 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
291
292 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
293
294 mmc_blk_put(md);
295 return ret;
296}
297
298static ssize_t
299num_wr_reqs_to_start_packing_store(struct device *dev,
300 struct device_attribute *attr,
301 const char *buf, size_t count)
302{
303 int value;
304 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200305 struct mmc_card *card = md->queue.card;
306 int ret = count;
307
308 if (!card) {
309 ret = -EINVAL;
310 goto exit;
311 }
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200312
313 sscanf(buf, "%d", &value);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200314
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200315 if (value >= 0) {
316 md->queue.num_wr_reqs_to_start_packing =
317 min_t(int, value, (int)card->ext_csd.max_packed_writes);
318
319 pr_debug("%s: trigger to pack: new value = %d",
320 mmc_hostname(card->host),
321 md->queue.num_wr_reqs_to_start_packing);
322 } else {
323 pr_err("%s: value %d is not valid. old value remains = %d",
324 mmc_hostname(card->host), value,
325 md->queue.num_wr_reqs_to_start_packing);
326 ret = -EINVAL;
327 }
328
329exit:
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200330 mmc_blk_put(md);
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200331 return ret;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200332}
333
Maya Erez2d33a192013-01-04 15:52:41 +0200334static ssize_t
335min_sectors_to_check_bkops_status_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
337{
338 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
339 unsigned int min_sectors_to_check_bkops_status;
340 struct mmc_card *card = md->queue.card;
341 int ret;
342
343 if (!card)
344 ret = -EINVAL;
345 else {
346 min_sectors_to_check_bkops_status =
347 card->bkops_info.min_sectors_to_queue_delayed_work;
348 ret = snprintf(buf, PAGE_SIZE, "%d\n",
349 min_sectors_to_check_bkops_status);
350 }
351
352 mmc_blk_put(md);
353 return ret;
354}
355
356static ssize_t
357min_sectors_to_check_bkops_status_store(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
360{
361 int value;
362 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
363 struct mmc_card *card = md->queue.card;
364
365 if (!card)
366 return -EINVAL;
367
368 sscanf(buf, "%d", &value);
369 if (value >= 0)
370 card->bkops_info.min_sectors_to_queue_delayed_work = value;
371
372 mmc_blk_put(md);
373 return count;
374}
375
Al Viroa5a15612008-03-02 10:33:30 -0500376static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Al Viroa5a15612008-03-02 10:33:30 -0500378 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int ret = -ENXIO;
380
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200381 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (md) {
383 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500384 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700386
Al Viroa5a15612008-03-02 10:33:30 -0500387 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700388 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700389 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200392 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return ret;
395}
396
Al Viroa5a15612008-03-02 10:33:30 -0500397static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Al Viroa5a15612008-03-02 10:33:30 -0500399 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200401 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200403 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
407static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800408mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800410 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
411 geo->heads = 4;
412 geo->sectors = 16;
413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
John Calixtocb87ea22011-04-26 18:56:29 -0400416struct mmc_blk_ioc_data {
417 struct mmc_ioc_cmd ic;
418 unsigned char *buf;
419 u64 buf_bytes;
420};
421
422static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
423 struct mmc_ioc_cmd __user *user)
424{
425 struct mmc_blk_ioc_data *idata;
426 int err;
427
428 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
429 if (!idata) {
430 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400431 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400432 }
433
434 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
435 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400436 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400437 }
438
439 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
440 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
441 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400442 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400443 }
444
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100445 if (!idata->buf_bytes)
446 return idata;
447
John Calixtocb87ea22011-04-26 18:56:29 -0400448 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
449 if (!idata->buf) {
450 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400451 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400452 }
453
454 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
455 idata->ic.data_ptr, idata->buf_bytes)) {
456 err = -EFAULT;
457 goto copy_err;
458 }
459
460 return idata;
461
462copy_err:
463 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400464idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400465 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400466out:
John Calixtocb87ea22011-04-26 18:56:29 -0400467 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400468}
469
Loic Pallardy961e37a2012-08-06 15:12:31 +0000470static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
471 u32 retries_max)
472{
473 int err;
474 u32 retry_count = 0;
475
476 if (!status || !retries_max)
477 return -EINVAL;
478
479 do {
480 err = get_card_status(card, status, 5);
481 if (err)
482 break;
483
484 if (!R1_STATUS(*status) &&
485 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
486 break; /* RPMB programming operation complete */
487
488 /*
489 * Rechedule to give the MMC device a chance to continue
490 * processing the previous command without being polled too
491 * frequently.
492 */
493 usleep_range(1000, 5000);
494 } while (++retry_count < retries_max);
495
496 if (retry_count == retries_max)
497 err = -EPERM;
498
499 return err;
500}
501
John Calixtocb87ea22011-04-26 18:56:29 -0400502static int mmc_blk_ioctl_cmd(struct block_device *bdev,
503 struct mmc_ioc_cmd __user *ic_ptr)
504{
505 struct mmc_blk_ioc_data *idata;
506 struct mmc_blk_data *md;
507 struct mmc_card *card;
508 struct mmc_command cmd = {0};
509 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530510 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400511 struct scatterlist sg;
512 int err;
Loic Pallardy961e37a2012-08-06 15:12:31 +0000513 int is_rpmb = false;
514 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400515
516 /*
517 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
518 * whole block device, not on a partition. This prevents overspray
519 * between sibling partitions.
520 */
521 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
522 return -EPERM;
523
524 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
525 if (IS_ERR(idata))
526 return PTR_ERR(idata);
527
John Calixtocb87ea22011-04-26 18:56:29 -0400528 md = mmc_blk_get(bdev->bd_disk);
529 if (!md) {
530 err = -EINVAL;
531 goto cmd_done;
532 }
533
Loic Pallardy961e37a2012-08-06 15:12:31 +0000534 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
535 is_rpmb = true;
536
John Calixtocb87ea22011-04-26 18:56:29 -0400537 card = md->queue.card;
538 if (IS_ERR(card)) {
539 err = PTR_ERR(card);
540 goto cmd_done;
541 }
542
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100543 cmd.opcode = idata->ic.opcode;
544 cmd.arg = idata->ic.arg;
545 cmd.flags = idata->ic.flags;
546
547 if (idata->buf_bytes) {
548 data.sg = &sg;
549 data.sg_len = 1;
550 data.blksz = idata->ic.blksz;
551 data.blocks = idata->ic.blocks;
552
553 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
554
555 if (idata->ic.write_flag)
556 data.flags = MMC_DATA_WRITE;
557 else
558 data.flags = MMC_DATA_READ;
559
560 /* data.flags must already be set before doing this. */
561 mmc_set_data_timeout(&data, card);
562
563 /* Allow overriding the timeout_ns for empirical tuning. */
564 if (idata->ic.data_timeout_ns)
565 data.timeout_ns = idata->ic.data_timeout_ns;
566
567 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
568 /*
569 * Pretend this is a data transfer and rely on the
570 * host driver to compute timeout. When all host
571 * drivers support cmd.cmd_timeout for R1B, this
572 * can be changed to:
573 *
574 * mrq.data = NULL;
575 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
576 */
577 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
578 }
579
580 mrq.data = &data;
581 }
582
583 mrq.cmd = &cmd;
584
John Calixtocb87ea22011-04-26 18:56:29 -0400585 mmc_claim_host(card->host);
586
Loic Pallardy961e37a2012-08-06 15:12:31 +0000587 err = mmc_blk_part_switch(card, md);
588 if (err)
589 goto cmd_rel_host;
590
John Calixtocb87ea22011-04-26 18:56:29 -0400591 if (idata->ic.is_acmd) {
592 err = mmc_app_cmd(card->host, card);
593 if (err)
594 goto cmd_rel_host;
595 }
596
Loic Pallardy961e37a2012-08-06 15:12:31 +0000597 if (is_rpmb) {
598 err = mmc_set_blockcount(card, data.blocks,
599 idata->ic.write_flag & (1 << 31));
600 if (err)
601 goto cmd_rel_host;
602 }
603
John Calixtocb87ea22011-04-26 18:56:29 -0400604 mmc_wait_for_req(card->host, &mrq);
605
606 if (cmd.error) {
607 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
608 __func__, cmd.error);
609 err = cmd.error;
610 goto cmd_rel_host;
611 }
612 if (data.error) {
613 dev_err(mmc_dev(card->host), "%s: data error %d\n",
614 __func__, data.error);
615 err = data.error;
616 goto cmd_rel_host;
617 }
618
619 /*
620 * According to the SD specs, some commands require a delay after
621 * issuing the command.
622 */
623 if (idata->ic.postsleep_min_us)
624 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
625
626 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
627 err = -EFAULT;
628 goto cmd_rel_host;
629 }
630
631 if (!idata->ic.write_flag) {
632 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
633 idata->buf, idata->buf_bytes)) {
634 err = -EFAULT;
635 goto cmd_rel_host;
636 }
637 }
638
Loic Pallardy961e37a2012-08-06 15:12:31 +0000639 if (is_rpmb) {
640 /*
641 * Ensure RPMB command has completed by polling CMD13
642 * "Send Status".
643 */
644 err = ioctl_rpmb_card_status_poll(card, &status, 5);
645 if (err)
646 dev_err(mmc_dev(card->host),
647 "%s: Card Status=0x%08X, error %d\n",
648 __func__, status, err);
649 }
650
John Calixtocb87ea22011-04-26 18:56:29 -0400651cmd_rel_host:
652 mmc_release_host(card->host);
653
654cmd_done:
655 mmc_blk_put(md);
656 kfree(idata->buf);
657 kfree(idata);
658 return err;
659}
660
661static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
662 unsigned int cmd, unsigned long arg)
663{
664 int ret = -EINVAL;
665 if (cmd == MMC_IOC_CMD)
666 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
667 return ret;
668}
669
670#ifdef CONFIG_COMPAT
671static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
672 unsigned int cmd, unsigned long arg)
673{
674 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
675}
676#endif
677
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700678static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500679 .open = mmc_blk_open,
680 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800681 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400683 .ioctl = mmc_blk_ioctl,
684#ifdef CONFIG_COMPAT
685 .compat_ioctl = mmc_blk_compat_ioctl,
686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687};
688
Andrei Warkentin371a6892011-04-11 18:10:25 -0500689static inline int mmc_blk_part_switch(struct mmc_card *card,
690 struct mmc_blk_data *md)
691{
692 int ret;
693 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300694
Andrei Warkentin371a6892011-04-11 18:10:25 -0500695 if (main_md->part_curr == md->part_type)
696 return 0;
697
698 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300699 u8 part_config = card->ext_csd.part_config;
700
701 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
702 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500703
704 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300705 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500706 card->ext_csd.part_time);
707 if (ret)
708 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300709
710 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300711 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500712
713 main_md->part_curr = md->part_type;
714 return 0;
715}
716
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700717static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
718{
719 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100720 u32 result;
721 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700722
Venkatraman Sad5fd972011-08-25 00:30:50 +0530723 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400724 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400725 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700726
727 struct scatterlist sg;
728
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700729 cmd.opcode = MMC_APP_CMD;
730 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700731 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700732
733 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700734 if (err)
735 return (u32)-1;
736 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700737 return (u32)-1;
738
739 memset(&cmd, 0, sizeof(struct mmc_command));
740
741 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
742 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700743 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700744
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700745 data.blksz = 4;
746 data.blocks = 1;
747 data.flags = MMC_DATA_READ;
748 data.sg = &sg;
749 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530750 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700751
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700752 mrq.cmd = &cmd;
753 mrq.data = &data;
754
Ben Dooks051913d2009-06-08 23:33:57 +0100755 blocks = kmalloc(4, GFP_KERNEL);
756 if (!blocks)
757 return (u32)-1;
758
759 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700760
761 mmc_wait_for_req(card->host, &mrq);
762
Ben Dooks051913d2009-06-08 23:33:57 +0100763 result = ntohl(*blocks);
764 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700765
Ben Dooks051913d2009-06-08 23:33:57 +0100766 if (cmd.error || data.error)
767 result = (u32)-1;
768
769 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700770}
771
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100772static int send_stop(struct mmc_card *card, u32 *status)
773{
774 struct mmc_command cmd = {0};
775 int err;
776
777 cmd.opcode = MMC_STOP_TRANSMISSION;
778 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
779 err = mmc_wait_for_cmd(card->host, &cmd, 5);
780 if (err == 0)
781 *status = cmd.resp[0];
782 return err;
783}
784
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100785static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300786{
Chris Ball1278dba2011-04-13 23:40:30 -0400787 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300788 int err;
789
Adrian Hunter504f1912008-10-16 12:55:25 +0300790 cmd.opcode = MMC_SEND_STATUS;
791 if (!mmc_host_is_spi(card->host))
792 cmd.arg = card->rca << 16;
793 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100794 err = mmc_wait_for_cmd(card->host, &cmd, retries);
795 if (err == 0)
796 *status = cmd.resp[0];
797 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300798}
799
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530800#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100801#define ERR_RETRY 2
802#define ERR_ABORT 1
803#define ERR_CONTINUE 0
804
805static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
806 bool status_valid, u32 status)
807{
808 switch (error) {
809 case -EILSEQ:
810 /* response crc error, retry the r/w cmd */
811 pr_err("%s: %s sending %s command, card status %#x\n",
812 req->rq_disk->disk_name, "response CRC error",
813 name, status);
814 return ERR_RETRY;
815
816 case -ETIMEDOUT:
817 pr_err("%s: %s sending %s command, card status %#x\n",
818 req->rq_disk->disk_name, "timed out", name, status);
819
820 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700821 if (!status_valid) {
822 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100823 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700824 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100825 /*
826 * If it was a r/w cmd crc error, or illegal command
827 * (eg, issued in wrong state) then retry - we should
828 * have corrected the state problem above.
829 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700830 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
831 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100832 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700833 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100834
835 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700836 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100837 return ERR_ABORT;
838
839 default:
840 /* We don't understand the error code the driver gave us */
841 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
842 req->rq_disk->disk_name, error, status);
843 return ERR_ABORT;
844 }
845}
846
847/*
848 * Initial r/w and stop cmd error recovery.
849 * We don't know whether the card received the r/w cmd or not, so try to
850 * restore things back to a sane state. Essentially, we do this as follows:
851 * - Obtain card status. If the first attempt to obtain card status fails,
852 * the status word will reflect the failed status cmd, not the failed
853 * r/w cmd. If we fail to obtain card status, it suggests we can no
854 * longer communicate with the card.
855 * - Check the card state. If the card received the cmd but there was a
856 * transient problem with the response, it might still be in a data transfer
857 * mode. Try to send it a stop command. If this fails, we can't recover.
858 * - If the r/w cmd failed due to a response CRC error, it was probably
859 * transient, so retry the cmd.
860 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
861 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
862 * illegal cmd, retry.
863 * Otherwise we don't understand what happened, so abort.
864 */
865static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300866 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100867{
868 bool prev_cmd_status_valid = true;
869 u32 status, stop_status = 0;
870 int err, retry;
871
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530872 if (mmc_card_removed(card))
873 return ERR_NOMEDIUM;
874
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100875 /*
876 * Try to get card status which indicates both the card state
877 * and why there was no response. If the first attempt fails,
878 * we can't be sure the returned status is for the r/w command.
879 */
880 for (retry = 2; retry >= 0; retry--) {
881 err = get_card_status(card, &status, 0);
882 if (!err)
883 break;
884
885 prev_cmd_status_valid = false;
886 pr_err("%s: error %d sending status command, %sing\n",
887 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
888 }
889
890 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530891 if (err) {
892 /* Check if the card is removed */
893 if (mmc_detect_card_removed(card->host))
894 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100895 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530896 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100897
Adrian Hunter67716322011-08-29 16:42:15 +0300898 /* Flag ECC errors */
899 if ((status & R1_CARD_ECC_FAILED) ||
900 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
901 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
902 *ecc_err = 1;
903
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100904 /*
905 * Check the current card state. If it is in some data transfer
906 * mode, tell it to stop (and hopefully transition back to TRAN.)
907 */
908 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
909 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
910 err = send_stop(card, &stop_status);
911 if (err)
912 pr_err("%s: error %d sending stop command\n",
913 req->rq_disk->disk_name, err);
914
915 /*
916 * If the stop cmd also timed out, the card is probably
917 * not present, so abort. Other errors are bad news too.
918 */
919 if (err)
920 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300921 if (stop_status & R1_CARD_ECC_FAILED)
922 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100923 }
924
925 /* Check for set block count errors */
926 if (brq->sbc.error)
927 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
928 prev_cmd_status_valid, status);
929
930 /* Check for r/w command errors */
931 if (brq->cmd.error)
932 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
933 prev_cmd_status_valid, status);
934
Adrian Hunter67716322011-08-29 16:42:15 +0300935 /* Data errors */
936 if (!brq->stop.error)
937 return ERR_CONTINUE;
938
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100939 /* Now for stop errors. These aren't fatal to the transfer. */
940 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
941 req->rq_disk->disk_name, brq->stop.error,
942 brq->cmd.resp[0], status);
943
944 /*
945 * Subsitute in our own stop status as this will give the error
946 * state which happened during the execution of the r/w command.
947 */
948 if (stop_status) {
949 brq->stop.resp[0] = stop_status;
950 brq->stop.error = 0;
951 }
952 return ERR_CONTINUE;
953}
954
Adrian Hunter67716322011-08-29 16:42:15 +0300955static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
956 int type)
957{
958 int err;
959
960 if (md->reset_done & type)
961 return -EEXIST;
962
963 md->reset_done |= type;
964 err = mmc_hw_reset(host);
965 /* Ensure we switch back to the correct partition */
966 if (err != -EOPNOTSUPP) {
967 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
968 int part_err;
969
970 main_md->part_curr = main_md->part_type;
971 part_err = mmc_blk_part_switch(host->card, md);
972 if (part_err) {
973 /*
974 * We have failed to get back into the correct
975 * partition, so we need to abort the whole request.
976 */
977 return -ENODEV;
978 }
979 }
980 return err;
981}
982
983static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
984{
985 md->reset_done &= ~type;
986}
987
Adrian Hunterbd788c92010-08-11 14:17:47 -0700988static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
989{
990 struct mmc_blk_data *md = mq->data;
991 struct mmc_card *card = md->queue.card;
992 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300993 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700994
Adrian Hunterbd788c92010-08-11 14:17:47 -0700995 if (!mmc_can_erase(card)) {
996 err = -EOPNOTSUPP;
997 goto out;
998 }
999
1000 from = blk_rq_pos(req);
1001 nr = blk_rq_sectors(req);
1002
Maya Erez2d33a192013-01-04 15:52:41 +02001003 if (card->ext_csd.bkops_en)
1004 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1005
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001006 if (mmc_can_discard(card))
1007 arg = MMC_DISCARD_ARG;
1008 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001009 arg = MMC_TRIM_ARG;
1010 else
1011 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001012retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001013 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1014 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1015 INAND_CMD38_ARG_EXT_CSD,
1016 arg == MMC_TRIM_ARG ?
1017 INAND_CMD38_ARG_TRIM :
1018 INAND_CMD38_ARG_ERASE,
1019 0);
1020 if (err)
1021 goto out;
1022 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001023 err = mmc_erase(card, from, nr, arg);
1024out:
Adrian Hunter67716322011-08-29 16:42:15 +03001025 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1026 goto retry;
1027 if (!err)
1028 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301029 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001030
Adrian Hunterbd788c92010-08-11 14:17:47 -07001031 return err ? 0 : 1;
1032}
1033
Adrian Hunter49804542010-08-11 14:17:50 -07001034static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1035 struct request *req)
1036{
1037 struct mmc_blk_data *md = mq->data;
1038 struct mmc_card *card = md->queue.card;
1039 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001040 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001041
Maya Erez463bb952012-05-24 23:46:29 +03001042 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001043 err = -EOPNOTSUPP;
1044 goto out;
1045 }
1046
1047 from = blk_rq_pos(req);
1048 nr = blk_rq_sectors(req);
1049
1050 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1051 arg = MMC_SECURE_TRIM1_ARG;
1052 else
1053 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001054retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001055 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1056 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1057 INAND_CMD38_ARG_EXT_CSD,
1058 arg == MMC_SECURE_TRIM1_ARG ?
1059 INAND_CMD38_ARG_SECTRIM1 :
1060 INAND_CMD38_ARG_SECERASE,
1061 0);
1062 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001063 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001064 }
Adrian Hunter28302812012-04-05 14:45:48 +03001065
Adrian Hunter49804542010-08-11 14:17:50 -07001066 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001067 if (err == -EIO)
1068 goto out_retry;
1069 if (err)
1070 goto out;
1071
1072 if (arg == MMC_SECURE_TRIM1_ARG) {
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 INAND_CMD38_ARG_SECTRIM2,
1077 0);
1078 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001079 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001080 }
Adrian Hunter28302812012-04-05 14:45:48 +03001081
Adrian Hunter49804542010-08-11 14:17:50 -07001082 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001083 if (err == -EIO)
1084 goto out_retry;
1085 if (err)
1086 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001087 }
Adrian Hunter28302812012-04-05 14:45:48 +03001088
Adrian Hunter28302812012-04-05 14:45:48 +03001089out_retry:
1090 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001091 goto retry;
1092 if (!err)
1093 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001094out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301095 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001096
Adrian Hunter49804542010-08-11 14:17:50 -07001097 return err ? 0 : 1;
1098}
1099
Maya Erez463bb952012-05-24 23:46:29 +03001100static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1101 struct request *req)
1102{
1103 struct mmc_blk_data *md = mq->data;
1104 struct mmc_card *card = md->queue.card;
1105 int err = 0;
1106
1107 BUG_ON(!card);
1108 BUG_ON(!card->host);
1109
1110 if (!(mmc_can_sanitize(card) &&
1111 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1112 pr_warning("%s: %s - SANITIZE is not supported\n",
1113 mmc_hostname(card->host), __func__);
1114 err = -EOPNOTSUPP;
1115 goto out;
1116 }
1117
1118 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1119 mmc_hostname(card->host), __func__);
1120
1121 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001122 EXT_CSD_SANITIZE_START, 1,
1123 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001124
1125 if (err)
1126 pr_err("%s: %s - mmc_switch() with "
1127 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1128 mmc_hostname(card->host), __func__, err);
1129
1130 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1131 __func__);
1132
1133out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301134 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001135
1136 return err ? 0 : 1;
1137}
1138
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001139static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1140{
1141 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001142 struct mmc_card *card = md->queue.card;
1143 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001144
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001145 ret = mmc_flush_cache(card);
1146 if (ret)
1147 ret = -EIO;
1148
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301149 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001150
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001151 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001152}
1153
1154/*
1155 * Reformat current write as a reliable write, supporting
1156 * both legacy and the enhanced reliable write MMC cards.
1157 * In each transfer we'll handle only as much as a single
1158 * reliable write can handle, thus finish the request in
1159 * partial completions.
1160 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001161static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1162 struct mmc_card *card,
1163 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001164{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001165 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1166 /* Legacy mode imposes restrictions on transfers. */
1167 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1168 brq->data.blocks = 1;
1169
1170 if (brq->data.blocks > card->ext_csd.rel_sectors)
1171 brq->data.blocks = card->ext_csd.rel_sectors;
1172 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1173 brq->data.blocks = 1;
1174 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001175}
1176
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001177#define CMD_ERRORS \
1178 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1179 R1_ADDRESS_ERROR | /* Misaligned address */ \
1180 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1181 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1182 R1_CC_ERROR | /* Card controller error */ \
1183 R1_ERROR) /* General/unknown error */
1184
Per Forlinee8a43a2011-07-01 18:55:33 +02001185static int mmc_blk_err_check(struct mmc_card *card,
1186 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001187{
Per Forlinee8a43a2011-07-01 18:55:33 +02001188 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1189 mmc_active);
1190 struct mmc_blk_request *brq = &mq_mrq->brq;
1191 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001192 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001193
1194 /*
1195 * sbc.error indicates a problem with the set block count
1196 * command. No data will have been transferred.
1197 *
1198 * cmd.error indicates a problem with the r/w command. No
1199 * data will have been transferred.
1200 *
1201 * stop.error indicates a problem with the stop command. Data
1202 * may have been transferred, or may still be transferring.
1203 */
Adrian Hunter67716322011-08-29 16:42:15 +03001204 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1205 brq->data.error) {
1206 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001207 case ERR_RETRY:
1208 return MMC_BLK_RETRY;
1209 case ERR_ABORT:
1210 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301211 case ERR_NOMEDIUM:
1212 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001213 case ERR_CONTINUE:
1214 break;
1215 }
1216 }
1217
1218 /*
1219 * Check for errors relating to the execution of the
1220 * initial command - such as address errors. No data
1221 * has been transferred.
1222 */
1223 if (brq->cmd.resp[0] & CMD_ERRORS) {
1224 pr_err("%s: r/w command failed, status = %#x\n",
1225 req->rq_disk->disk_name, brq->cmd.resp[0]);
1226 return MMC_BLK_ABORT;
1227 }
1228
1229 /*
1230 * Everything else is either success, or a data error of some
1231 * kind. If it was a write, we may have transitioned to
1232 * program mode, which we have to wait for it to complete.
1233 */
1234 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1235 u32 status;
1236 do {
1237 int err = get_card_status(card, &status, 5);
1238 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301239 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001240 req->rq_disk->disk_name, err);
1241 return MMC_BLK_CMD_ERR;
1242 }
1243 /*
1244 * Some cards mishandle the status bits,
1245 * so make sure to check both the busy
1246 * indication and the card state.
1247 */
1248 } while (!(status & R1_READY_FOR_DATA) ||
1249 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1250 }
1251
1252 if (brq->data.error) {
1253 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1254 req->rq_disk->disk_name, brq->data.error,
1255 (unsigned)blk_rq_pos(req),
1256 (unsigned)blk_rq_sectors(req),
1257 brq->cmd.resp[0], brq->stop.resp[0]);
1258
1259 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001260 if (ecc_err)
1261 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001262 return MMC_BLK_DATA_ERR;
1263 } else {
1264 return MMC_BLK_CMD_ERR;
1265 }
1266 }
1267
Adrian Hunter67716322011-08-29 16:42:15 +03001268 if (!brq->data.bytes_xfered)
1269 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001270
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001271 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1272 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1273 return MMC_BLK_PARTIAL;
1274 else
1275 return MMC_BLK_SUCCESS;
1276 }
1277
Adrian Hunter67716322011-08-29 16:42:15 +03001278 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1279 return MMC_BLK_PARTIAL;
1280
1281 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001282}
1283
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001284static int mmc_blk_packed_err_check(struct mmc_card *card,
1285 struct mmc_async_req *areq)
1286{
1287 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1288 mmc_active);
1289 struct request *req = mq_rq->req;
1290 int err, check, status;
1291 u8 ext_csd[512];
1292
1293 mq_rq->packed_retries--;
1294 check = mmc_blk_err_check(card, areq);
1295 err = get_card_status(card, &status, 0);
1296 if (err) {
1297 pr_err("%s: error %d sending status command\n",
1298 req->rq_disk->disk_name, err);
1299 return MMC_BLK_ABORT;
1300 }
1301
1302 if (status & R1_EXCEPTION_EVENT) {
1303 err = mmc_send_ext_csd(card, ext_csd);
1304 if (err) {
1305 pr_err("%s: error %d sending ext_csd\n",
1306 req->rq_disk->disk_name, err);
1307 return MMC_BLK_ABORT;
1308 }
1309
1310 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1311 EXT_CSD_PACKED_FAILURE) &&
1312 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1313 EXT_CSD_PACKED_GENERIC_ERROR)) {
1314 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1315 EXT_CSD_PACKED_INDEXED_ERROR) {
1316 mq_rq->packed_fail_idx =
1317 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1318 return MMC_BLK_PARTIAL;
1319 }
1320 }
1321 }
1322
1323 return check;
1324}
1325
Per Forlin54d49d772011-07-01 18:55:29 +02001326static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1327 struct mmc_card *card,
1328 int disable_multi,
1329 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Per Forlin54d49d772011-07-01 18:55:29 +02001331 u32 readcmd, writecmd;
1332 struct mmc_blk_request *brq = &mqrq->brq;
1333 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301335 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001337 /*
1338 * Reliable writes are used to implement Forced Unit Access and
1339 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001340 *
1341 * XXX: this really needs a good explanation of why REQ_META
1342 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001343 */
1344 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1345 (req->cmd_flags & REQ_META)) &&
1346 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001347 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001348
Per Forlin54d49d772011-07-01 18:55:29 +02001349 memset(brq, 0, sizeof(struct mmc_blk_request));
1350 brq->mrq.cmd = &brq->cmd;
1351 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Per Forlin54d49d772011-07-01 18:55:29 +02001353 brq->cmd.arg = blk_rq_pos(req);
1354 if (!mmc_card_blockaddr(card))
1355 brq->cmd.arg <<= 9;
1356 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1357 brq->data.blksz = 512;
1358 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1359 brq->stop.arg = 0;
1360 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1361 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Asutosh Das1a897142012-07-27 18:10:19 +05301363 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001364 /*
1365 * The block layer doesn't support all sector count
1366 * restrictions, so we need to be prepared for too big
1367 * requests.
1368 */
1369 if (brq->data.blocks > card->host->max_blk_count)
1370 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001372 if (brq->data.blocks > 1) {
1373 /*
1374 * After a read error, we redo the request one sector
1375 * at a time in order to accurately determine which
1376 * sectors can be read successfully.
1377 */
1378 if (disable_multi)
1379 brq->data.blocks = 1;
1380
1381 /* Some controllers can't do multiblock reads due to hw bugs */
1382 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1383 rq_data_dir(req) == READ)
1384 brq->data.blocks = 1;
1385 }
Per Forlin54d49d772011-07-01 18:55:29 +02001386
1387 if (brq->data.blocks > 1 || do_rel_wr) {
1388 /* SPI multiblock writes terminate using a special
1389 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001390 */
Per Forlin54d49d772011-07-01 18:55:29 +02001391 if (!mmc_host_is_spi(card->host) ||
1392 rq_data_dir(req) == READ)
1393 brq->mrq.stop = &brq->stop;
1394 readcmd = MMC_READ_MULTIPLE_BLOCK;
1395 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1396 } else {
1397 brq->mrq.stop = NULL;
1398 readcmd = MMC_READ_SINGLE_BLOCK;
1399 writecmd = MMC_WRITE_BLOCK;
1400 }
1401 if (rq_data_dir(req) == READ) {
1402 brq->cmd.opcode = readcmd;
1403 brq->data.flags |= MMC_DATA_READ;
1404 } else {
1405 brq->cmd.opcode = writecmd;
1406 brq->data.flags |= MMC_DATA_WRITE;
1407 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001408
Per Forlin54d49d772011-07-01 18:55:29 +02001409 if (do_rel_wr)
1410 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001411
Per Forlin54d49d772011-07-01 18:55:29 +02001412 /*
Saugata Das42659002011-12-21 13:09:17 +05301413 * Data tag is used only during writing meta data to speed
1414 * up write and any subsequent read of this meta data
1415 */
1416 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1417 (req->cmd_flags & REQ_META) &&
1418 (rq_data_dir(req) == WRITE) &&
1419 ((brq->data.blocks * brq->data.blksz) >=
1420 card->ext_csd.data_tag_unit_size);
1421
1422 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001423 * Pre-defined multi-block transfers are preferable to
1424 * open ended-ones (and necessary for reliable writes).
1425 * However, it is not sufficient to just send CMD23,
1426 * and avoid the final CMD12, as on an error condition
1427 * CMD12 (stop) needs to be sent anyway. This, coupled
1428 * with Auto-CMD23 enhancements provided by some
1429 * hosts, means that the complexity of dealing
1430 * with this is best left to the host. If CMD23 is
1431 * supported by card and host, we'll fill sbc in and let
1432 * the host deal with handling it correctly. This means
1433 * that for hosts that don't expose MMC_CAP_CMD23, no
1434 * change of behavior will be observed.
1435 *
1436 * N.B: Some MMC cards experience perf degradation.
1437 * We'll avoid using CMD23-bounded multiblock writes for
1438 * these, while retaining features like reliable writes.
1439 */
Saugata Das42659002011-12-21 13:09:17 +05301440 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1441 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1442 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001443 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1444 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301445 (do_rel_wr ? (1 << 31) : 0) |
1446 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001447 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1448 brq->mrq.sbc = &brq->sbc;
1449 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001450
Per Forlin54d49d772011-07-01 18:55:29 +02001451 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001452
Per Forlin54d49d772011-07-01 18:55:29 +02001453 brq->data.sg = mqrq->sg;
1454 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001455
Per Forlin54d49d772011-07-01 18:55:29 +02001456 /*
1457 * Adjust the sg list so it is the same size as the
1458 * request.
1459 */
1460 if (brq->data.blocks != blk_rq_sectors(req)) {
1461 int i, data_size = brq->data.blocks << 9;
1462 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001463
Per Forlin54d49d772011-07-01 18:55:29 +02001464 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1465 data_size -= sg->length;
1466 if (data_size <= 0) {
1467 sg->length += data_size;
1468 i++;
1469 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001470 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001471 }
Per Forlin54d49d772011-07-01 18:55:29 +02001472 brq->data.sg_len = i;
1473 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001474
Per Forlinee8a43a2011-07-01 18:55:33 +02001475 mqrq->mmc_active.mrq = &brq->mrq;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001476 if (mq->err_check_fn)
1477 mqrq->mmc_active.err_check = mq->err_check_fn;
1478 else
1479 mqrq->mmc_active.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001480
Per Forlin54d49d772011-07-01 18:55:29 +02001481 mmc_queue_bounce_pre(mqrq);
1482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001484static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1485 struct request *req)
1486{
1487 struct mmc_host *host = mq->card->host;
1488 int data_dir;
1489
1490 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1491 return;
1492
Maya Erez6f219eb2012-12-02 13:27:15 +02001493 /* Support for the write packing on eMMC 4.5 or later */
1494 if (mq->card->ext_csd.rev <= 5)
1495 return;
1496
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001497 /*
1498 * In case the packing control is not supported by the host, it should
1499 * not have an effect on the write packing. Therefore we have to enable
1500 * the write packing
1501 */
1502 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1503 mq->wr_packing_enabled = true;
1504 return;
1505 }
1506
1507 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1508 if (mq->num_of_potential_packed_wr_reqs >
1509 mq->num_wr_reqs_to_start_packing)
1510 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001511 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001512 return;
1513 }
1514
1515 data_dir = rq_data_dir(req);
1516
1517 if (data_dir == READ) {
1518 mq->num_of_potential_packed_wr_reqs = 0;
1519 mq->wr_packing_enabled = false;
1520 return;
1521 } else if (data_dir == WRITE) {
1522 mq->num_of_potential_packed_wr_reqs++;
1523 }
1524
1525 if (mq->num_of_potential_packed_wr_reqs >
1526 mq->num_wr_reqs_to_start_packing)
1527 mq->wr_packing_enabled = true;
1528
1529}
1530
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001531struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1532{
1533 if (!card)
1534 return NULL;
1535
1536 return &card->wr_pack_stats;
1537}
1538EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1539
1540void mmc_blk_init_packed_statistics(struct mmc_card *card)
1541{
1542 int max_num_of_packed_reqs = 0;
1543
1544 if (!card || !card->wr_pack_stats.packing_events)
1545 return;
1546
1547 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1548
1549 spin_lock(&card->wr_pack_stats.lock);
1550 memset(card->wr_pack_stats.packing_events, 0,
1551 (max_num_of_packed_reqs + 1) *
1552 sizeof(*card->wr_pack_stats.packing_events));
1553 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1554 sizeof(card->wr_pack_stats.pack_stop_reason));
1555 card->wr_pack_stats.enabled = true;
1556 spin_unlock(&card->wr_pack_stats.lock);
1557}
1558EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1559
Lee Susman66842b02012-12-19 14:28:03 +02001560/**
1561 * mmc_blk_init_async_event_statistics() - Init async event
1562 * statistics data
1563 * @card: The mmc_card in which the async_event_stats
1564 * struct is a member
1565 *
1566 * Initiate counters for the new request feature, and mark the
1567 * statistics as enabled.
1568 */
1569void mmc_blk_init_async_event_statistics(struct mmc_card *card)
1570{
1571 if (!card)
1572 return;
1573
1574 /* init async events tests stats */
1575 memset(&card->async_event_stats,
1576 sizeof(struct mmc_async_event_stats), 0);
1577 card->async_event_stats.null_fetched = 0;
1578 card->async_event_stats.wakeup_new = 0;
1579 card->async_event_stats.new_request_flag = 0;
1580 card->async_event_stats.q_no_waiting = 0;
1581 card->async_event_stats.enabled = true;
1582 card->async_event_stats.no_mmc_request_action = 0;
1583 card->async_event_stats.wakeup_mq_thread = 0;
1584 card->async_event_stats.fetch_due_to_new_req = 0;
1585 card->async_event_stats.returned_new_req = 0;
1586 card->async_event_stats.done_flag = 0;
1587 card->async_event_stats.cmd_retry = 0;
1588 card->async_event_stats.done_when_new_req_event_on = 0;
1589 card->async_event_stats.new_req_when_new_marked = 0;
1590}
1591EXPORT_SYMBOL(mmc_blk_init_async_event_statistics);
1592
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001593static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1594{
1595 struct request_queue *q = mq->queue;
1596 struct mmc_card *card = mq->card;
1597 struct request *cur = req, *next = NULL;
1598 struct mmc_blk_data *md = mq->data;
1599 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1600 unsigned int req_sectors = 0, phys_segments = 0;
1601 unsigned int max_blk_count, max_phys_segs;
1602 u8 put_back = 0;
1603 u8 max_packed_rw = 0;
1604 u8 reqs = 0;
Lee Susman66842b02012-12-19 14:28:03 +02001605 struct mmc_wr_pack_stats *stats;
1606
1607 if (!card)
1608 goto no_packed;
1609
1610 stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001611
1612 mmc_blk_clear_packed(mq->mqrq_cur);
1613
1614 if (!(md->flags & MMC_BLK_CMD23) ||
1615 !card->ext_csd.packed_event_en)
1616 goto no_packed;
1617
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001618 if (!mq->wr_packing_enabled)
1619 goto no_packed;
1620
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001621 if ((rq_data_dir(cur) == WRITE) &&
1622 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1623 max_packed_rw = card->ext_csd.max_packed_writes;
1624
1625 if (max_packed_rw == 0)
1626 goto no_packed;
1627
1628 if (mmc_req_rel_wr(cur) &&
1629 (md->flags & MMC_BLK_REL_WR) &&
1630 !en_rel_wr)
1631 goto no_packed;
1632
1633 if (mmc_large_sec(card) &&
1634 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1635 goto no_packed;
1636
1637 max_blk_count = min(card->host->max_blk_count,
1638 card->host->max_req_size >> 9);
1639 if (unlikely(max_blk_count > 0xffff))
1640 max_blk_count = 0xffff;
1641
1642 max_phys_segs = queue_max_segments(q);
1643 req_sectors += blk_rq_sectors(cur);
1644 phys_segments += cur->nr_phys_segments;
1645
1646 if (rq_data_dir(cur) == WRITE) {
1647 req_sectors++;
1648 phys_segments++;
1649 }
1650
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001651 spin_lock(&stats->lock);
1652
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001653 while (reqs < max_packed_rw - 1) {
1654 spin_lock_irq(q->queue_lock);
1655 next = blk_fetch_request(q);
1656 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001657 if (!next) {
1658 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001659 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001660 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001661
1662 if (mmc_large_sec(card) &&
1663 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001664 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001665 put_back = 1;
1666 break;
1667 }
1668
1669 if (next->cmd_flags & REQ_DISCARD ||
1670 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001671 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001672 put_back = 1;
1673 break;
1674 }
1675
1676 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001677 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001678 put_back = 1;
1679 break;
1680 }
1681
1682 if (mmc_req_rel_wr(next) &&
1683 (md->flags & MMC_BLK_REL_WR) &&
1684 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001685 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001686 put_back = 1;
1687 break;
1688 }
1689
1690 req_sectors += blk_rq_sectors(next);
1691 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001692 if (stats->enabled)
1693 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001694 put_back = 1;
1695 break;
1696 }
1697
1698 phys_segments += next->nr_phys_segments;
1699 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001700 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001701 put_back = 1;
1702 break;
1703 }
1704
Maya Erez2d33a192013-01-04 15:52:41 +02001705 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001706 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02001707 if (card->ext_csd.bkops_en)
1708 card->bkops_info.sectors_changed +=
1709 blk_rq_sectors(next);
1710 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001711 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1712 cur = next;
1713 reqs++;
1714 }
1715
1716 if (put_back) {
1717 spin_lock_irq(q->queue_lock);
1718 blk_requeue_request(q, next);
1719 spin_unlock_irq(q->queue_lock);
1720 }
1721
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001722 if (stats->enabled) {
1723 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1724 stats->packing_events[reqs + 1]++;
1725 if (reqs + 1 == max_packed_rw)
1726 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1727 }
1728
1729 spin_unlock(&stats->lock);
1730
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001731 if (reqs > 0) {
1732 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1733 mq->mqrq_cur->packed_num = ++reqs;
1734 mq->mqrq_cur->packed_retries = reqs;
1735 return reqs;
1736 }
1737
1738no_packed:
1739 mmc_blk_clear_packed(mq->mqrq_cur);
1740 return 0;
1741}
1742
1743static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1744 struct mmc_card *card,
1745 struct mmc_queue *mq)
1746{
1747 struct mmc_blk_request *brq = &mqrq->brq;
1748 struct request *req = mqrq->req;
1749 struct request *prq;
1750 struct mmc_blk_data *md = mq->data;
1751 bool do_rel_wr, do_data_tag;
1752 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1753 u8 i = 1;
1754
1755 mqrq->packed_cmd = MMC_PACKED_WRITE;
1756 mqrq->packed_blocks = 0;
1757 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1758
1759 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1760 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1761 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1762
1763 /*
1764 * Argument for each entry of packed group
1765 */
1766 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1767 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1768 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1769 (prq->cmd_flags & REQ_META) &&
1770 (rq_data_dir(prq) == WRITE) &&
1771 ((brq->data.blocks * brq->data.blksz) >=
1772 card->ext_csd.data_tag_unit_size);
1773 /* Argument of CMD23 */
1774 packed_cmd_hdr[(i * 2)] =
1775 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1776 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1777 blk_rq_sectors(prq);
1778 /* Argument of CMD18 or CMD25 */
1779 packed_cmd_hdr[((i * 2)) + 1] =
1780 mmc_card_blockaddr(card) ?
1781 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1782 mqrq->packed_blocks += blk_rq_sectors(prq);
1783 i++;
1784 }
1785
1786 memset(brq, 0, sizeof(struct mmc_blk_request));
1787 brq->mrq.cmd = &brq->cmd;
1788 brq->mrq.data = &brq->data;
1789 brq->mrq.sbc = &brq->sbc;
1790 brq->mrq.stop = &brq->stop;
1791
1792 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1793 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1794 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1795
1796 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1797 brq->cmd.arg = blk_rq_pos(req);
1798 if (!mmc_card_blockaddr(card))
1799 brq->cmd.arg <<= 9;
1800 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1801
1802 brq->data.blksz = 512;
1803 brq->data.blocks = mqrq->packed_blocks + 1;
1804 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02001805 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001806
1807 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1808 brq->stop.arg = 0;
1809 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1810
1811 mmc_set_data_timeout(&brq->data, card);
1812
1813 brq->data.sg = mqrq->sg;
1814 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1815
1816 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001817
1818 /*
1819 * This is intended for packed commands tests usage - in case these
1820 * functions are not in use the respective pointers are NULL
1821 */
1822 if (mq->err_check_fn)
1823 mqrq->mmc_active.err_check = mq->err_check_fn;
1824 else
1825 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1826
1827 if (mq->packed_test_fn)
1828 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001829
1830 mmc_queue_bounce_pre(mqrq);
1831}
1832
Adrian Hunter67716322011-08-29 16:42:15 +03001833static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1834 struct mmc_blk_request *brq, struct request *req,
1835 int ret)
1836{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001837 struct mmc_queue_req *mq_rq;
1838 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1839
Adrian Hunter67716322011-08-29 16:42:15 +03001840 /*
1841 * If this is an SD card and we're writing, we can first
1842 * mark the known good sectors as ok.
1843 *
1844 * If the card is not SD, we can still ok written sectors
1845 * as reported by the controller (which might be less than
1846 * the real number of written sectors, but never more).
1847 */
1848 if (mmc_card_sd(card)) {
1849 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301850 if (!brq->data.fault_injected) {
1851 blocks = mmc_sd_num_wr_blocks(card);
1852 if (blocks != (u32)-1)
1853 ret = blk_end_request(req, 0, blocks << 9);
1854 } else
1855 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001856 } else {
1857 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1858 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1859 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001860 return ret;
1861}
1862
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001863static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1864{
1865 struct request *prq;
1866 int idx = mq_rq->packed_fail_idx, i = 0;
1867 int ret = 0;
1868
1869 while (!list_empty(&mq_rq->packed_list)) {
1870 prq = list_entry_rq(mq_rq->packed_list.next);
1871 if (idx == i) {
1872 /* retry from error index */
1873 mq_rq->packed_num -= idx;
1874 mq_rq->req = prq;
1875 ret = 1;
1876
1877 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1878 list_del_init(&prq->queuelist);
1879 mmc_blk_clear_packed(mq_rq);
1880 }
1881 return ret;
1882 }
1883 list_del_init(&prq->queuelist);
1884 blk_end_request(prq, 0, blk_rq_bytes(prq));
1885 i++;
1886 }
1887
1888 mmc_blk_clear_packed(mq_rq);
1889 return ret;
1890}
1891static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1892{
1893 struct request *prq;
1894
1895 while (!list_empty(&mq_rq->packed_list)) {
1896 prq = list_entry_rq(mq_rq->packed_list.next);
1897 list_del_init(&prq->queuelist);
1898 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1899 }
1900
1901 mmc_blk_clear_packed(mq_rq);
1902}
1903
1904static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1905 struct mmc_queue_req *mq_rq)
1906{
1907 struct request *prq;
1908 struct request_queue *q = mq->queue;
1909
1910 while (!list_empty(&mq_rq->packed_list)) {
1911 prq = list_entry_rq(mq_rq->packed_list.prev);
1912 if (prq->queuelist.prev != &mq_rq->packed_list) {
1913 list_del_init(&prq->queuelist);
1914 spin_lock_irq(q->queue_lock);
1915 blk_requeue_request(mq->queue, prq);
1916 spin_unlock_irq(q->queue_lock);
1917 } else {
1918 list_del_init(&prq->queuelist);
1919 }
1920 }
1921
1922 mmc_blk_clear_packed(mq_rq);
1923}
1924
Per Forlinee8a43a2011-07-01 18:55:33 +02001925static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001926{
1927 struct mmc_blk_data *md = mq->data;
1928 struct mmc_card *card = md->queue.card;
1929 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001930 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001931 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001932 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001933 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001934 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001935 const u8 packed_num = 2;
1936 u8 reqs = 0;
Lee Susman66842b02012-12-19 14:28:03 +02001937 struct mmc_async_event_stats *stats = &card->async_event_stats;
Per Forlinee8a43a2011-07-01 18:55:33 +02001938
1939 if (!rqc && !mq->mqrq_prev->req)
1940 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001941
Maya Erez5f360692012-10-10 03:47:54 +02001942 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02001943 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1944 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001945 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001946 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001947
Per Forlin54d49d772011-07-01 18:55:29 +02001948 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001949 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001950 if (reqs >= packed_num)
1951 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1952 card, mq);
1953 else
1954 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001955 areq = &mq->mqrq_cur->mmc_active;
1956 } else
1957 areq = NULL;
1958 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001959 if (!areq) {
Lee Susman66842b02012-12-19 14:28:03 +02001960 if (status == MMC_BLK_NEW_REQUEST && stats) {
1961 if (stats->enabled)
1962 stats->returned_new_req++;
1963
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001964 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Lee Susman66842b02012-12-19 14:28:03 +02001965 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001966 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001967 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001968
Per Forlinee8a43a2011-07-01 18:55:33 +02001969 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1970 brq = &mq_rq->brq;
1971 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001972 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001973 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001974
Per Forlind78d4a82011-07-01 18:55:30 +02001975 switch (status) {
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001976 case MMC_BLK_NEW_REQUEST:
1977 BUG(); /* should never get here */
Per Forlind78d4a82011-07-01 18:55:30 +02001978 case MMC_BLK_SUCCESS:
1979 case MMC_BLK_PARTIAL:
1980 /*
1981 * A block was successfully transferred.
1982 */
Adrian Hunter67716322011-08-29 16:42:15 +03001983 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001984
1985 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1986 ret = mmc_blk_end_packed_req(mq_rq);
1987 break;
1988 } else {
1989 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001990 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001991 }
1992
Adrian Hunter67716322011-08-29 16:42:15 +03001993 /*
1994 * If the blk_end_request function returns non-zero even
1995 * though all data has been transferred and no errors
1996 * were returned by the host controller, it's a bug.
1997 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001998 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301999 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002000 __func__, blk_rq_bytes(req),
2001 brq->data.bytes_xfered);
2002 rqc = NULL;
2003 goto cmd_abort;
2004 }
Per Forlind78d4a82011-07-01 18:55:30 +02002005 break;
2006 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002007 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2008 if (!mmc_blk_reset(md, card->host, type))
2009 break;
2010 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002011 case MMC_BLK_RETRY:
2012 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002013 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002014 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002015 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002016 if (!mmc_blk_reset(md, card->host, type))
2017 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002018 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002019 case MMC_BLK_DATA_ERR: {
2020 int err;
2021
2022 err = mmc_blk_reset(md, card->host, type);
2023 if (!err)
2024 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002025 if (err == -ENODEV ||
2026 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002027 goto cmd_abort;
2028 /* Fall through */
2029 }
2030 case MMC_BLK_ECC_ERR:
2031 if (brq->data.blocks > 1) {
2032 /* Redo read one sector at a time */
2033 pr_warning("%s: retrying using single block read\n",
2034 req->rq_disk->disk_name);
2035 disable_multi = 1;
2036 break;
2037 }
Per Forlind78d4a82011-07-01 18:55:30 +02002038 /*
2039 * After an error, we redo I/O one sector at a
2040 * time, so we only reach here after trying to
2041 * read a single sector.
2042 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302043 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002044 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002045 if (!ret)
2046 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002047 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302048 case MMC_BLK_NOMEDIUM:
2049 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002050 }
2051
Per Forlinee8a43a2011-07-01 18:55:33 +02002052 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002053 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2054 /*
2055 * In case of a incomplete request
2056 * prepare it again and resend.
2057 */
2058 mmc_blk_rw_rq_prep(mq_rq, card,
2059 disable_multi, mq);
2060 mmc_start_req(card->host,
2061 &mq_rq->mmc_active, NULL);
2062 } else {
2063 if (!mq_rq->packed_retries)
2064 goto cmd_abort;
2065 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2066 mmc_start_req(card->host,
2067 &mq_rq->mmc_active, NULL);
2068 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 } while (ret);
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 1;
2073
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002074 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002075 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2076 if (mmc_card_removed(card))
2077 req->cmd_flags |= REQ_QUIET;
2078 while (ret)
2079 ret = blk_end_request(req, -EIO,
2080 blk_rq_cur_bytes(req));
2081 } else {
2082 mmc_blk_abort_packed_req(mq_rq);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Per Forlinee8a43a2011-07-01 18:55:33 +02002085 start_new_req:
2086 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002087 /*
2088 * If current request is packed, it needs to put back.
2089 */
2090 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2091 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2092
Per Forlinee8a43a2011-07-01 18:55:33 +02002093 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2094 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2095 }
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return 0;
2098}
2099
Adrian Hunterbd788c92010-08-11 14:17:47 -07002100static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2101{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002102 int ret;
2103 struct mmc_blk_data *md = mq->data;
2104 struct mmc_card *card = md->queue.card;
2105
San Mehat148c57a2009-07-30 08:21:19 -07002106#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2107 if (mmc_bus_needs_resume(card->host)) {
2108 mmc_resume_bus(card->host);
2109 mmc_blk_set_blksize(md, card);
2110 }
2111#endif
2112
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002113 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002114 /* claim host only for the first request */
2115 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002116 if (card->ext_csd.bkops_en)
2117 mmc_stop_bkops(card);
2118 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002119
Andrei Warkentin371a6892011-04-11 18:10:25 -05002120 ret = mmc_blk_part_switch(card, md);
2121 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002122 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302123 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002124 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002125 ret = 0;
2126 goto out;
2127 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002128
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002129 mmc_blk_write_packing_control(mq, req);
2130
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002131 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002132 if (req && req->cmd_flags & REQ_SANITIZE) {
2133 /* complete ongoing async transfer before issuing sanitize */
2134 if (card->host && card->host->areq)
2135 mmc_blk_issue_rw_rq(mq, NULL);
2136 ret = mmc_blk_issue_sanitize_rq(mq, req);
2137 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002138 /* complete ongoing async transfer before issuing discard */
2139 if (card->host->areq)
2140 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302141 if (req->cmd_flags & REQ_SECURE &&
2142 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002143 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002144 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002145 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002146 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002147 /* complete ongoing async transfer before issuing flush */
2148 if (card->host->areq)
2149 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002150 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002151 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002152 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002153 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002154
Andrei Warkentin371a6892011-04-11 18:10:25 -05002155out:
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002156 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) {
2157 if (mmc_card_need_bkops(card))
2158 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002159 /* release host only when there are no more requests */
2160 mmc_release_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002161 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002162 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Russell Kinga6f6c962006-01-03 22:38:44 +00002165static inline int mmc_blk_readonly(struct mmc_card *card)
2166{
2167 return mmc_card_readonly(card) ||
2168 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2169}
2170
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2172 struct device *parent,
2173 sector_t size,
2174 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002175 const char *subname,
2176 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct mmc_blk_data *md;
2179 int devidx, ret;
2180
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002181 devidx = find_first_zero_bit(dev_use, max_devices);
2182 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return ERR_PTR(-ENOSPC);
2184 __set_bit(devidx, dev_use);
2185
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002186 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002187 if (!md) {
2188 ret = -ENOMEM;
2189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002191
Russell Kinga6f6c962006-01-03 22:38:44 +00002192 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002193 * !subname implies we are creating main mmc_blk_data that will be
2194 * associated with mmc_card with mmc_set_drvdata. Due to device
2195 * partitions, devidx will not coincide with a per-physical card
2196 * index anymore so we keep track of a name index.
2197 */
2198 if (!subname) {
2199 md->name_idx = find_first_zero_bit(name_use, max_devices);
2200 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002201 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002202 md->name_idx = ((struct mmc_blk_data *)
2203 dev_to_disk(parent)->private_data)->name_idx;
2204
Johan Rudholmadd710e2011-12-02 08:51:06 +01002205 md->area_type = area_type;
2206
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002207 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002208 * Set the read-only status based on the supported commands
2209 * and the write protect switch.
2210 */
2211 md->read_only = mmc_blk_readonly(card);
2212
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002213 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002214 if (md->disk == NULL) {
2215 ret = -ENOMEM;
2216 goto err_kfree;
2217 }
2218
2219 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002221 md->usage = 1;
2222
Adrian Hunterd09408a2011-06-23 13:40:28 +03002223 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002224 if (ret)
2225 goto err_putdisk;
2226
Russell Kinga6f6c962006-01-03 22:38:44 +00002227 md->queue.issue_fn = mmc_blk_issue_rq;
2228 md->queue.data = md;
2229
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002230 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002231 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002232 md->disk->fops = &mmc_bdops;
2233 md->disk->private_data = md;
2234 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 md->disk->driverfs_dev = parent;
2236 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002237 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002238 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2239 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002240
2241 /*
2242 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2243 *
2244 * - be set for removable media with permanent block devices
2245 * - be unset for removable block devices with permanent media
2246 *
2247 * Since MMC block devices clearly fall under the second
2248 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2249 * should use the block device creation/destruction hotplug
2250 * messages to tell when the card is present.
2251 */
2252
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002253 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2254 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002255
Martin K. Petersene1defc42009-05-22 17:17:49 -04002256 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002257 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002258
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002259 if (mmc_host_cmd23(card->host)) {
2260 if (mmc_card_mmc(card) ||
2261 (mmc_card_sd(card) &&
2262 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2263 md->flags |= MMC_BLK_CMD23;
2264 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002265
2266 if (mmc_card_mmc(card) &&
2267 md->flags & MMC_BLK_CMD23 &&
2268 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2269 card->ext_csd.rel_sectors)) {
2270 md->flags |= MMC_BLK_REL_WR;
2271 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2272 }
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002275
2276 err_putdisk:
2277 put_disk(md->disk);
2278 err_kfree:
2279 kfree(md);
2280 out:
2281 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Andrei Warkentin371a6892011-04-11 18:10:25 -05002284static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2285{
2286 sector_t size;
2287 struct mmc_blk_data *md;
2288
2289 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2290 /*
2291 * The EXT_CSD sector count is in number or 512 byte
2292 * sectors.
2293 */
2294 size = card->ext_csd.sectors;
2295 } else {
2296 /*
2297 * The CSD capacity field is in units of read_blkbits.
2298 * set_capacity takes units of 512 bytes.
2299 */
2300 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2301 }
2302
Johan Rudholmadd710e2011-12-02 08:51:06 +01002303 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2304 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002305 return md;
2306}
2307
2308static int mmc_blk_alloc_part(struct mmc_card *card,
2309 struct mmc_blk_data *md,
2310 unsigned int part_type,
2311 sector_t size,
2312 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002313 const char *subname,
2314 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002315{
2316 char cap_str[10];
2317 struct mmc_blk_data *part_md;
2318
2319 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002320 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002321 if (IS_ERR(part_md))
2322 return PTR_ERR(part_md);
2323 part_md->part_type = part_type;
2324 list_add(&part_md->part, &md->part);
2325
2326 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2327 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302328 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002329 part_md->disk->disk_name, mmc_card_id(card),
2330 mmc_card_name(card), part_md->part_type, cap_str);
2331 return 0;
2332}
2333
Namjae Jeone0c368d2011-10-06 23:41:38 +09002334/* MMC Physical partitions consist of two boot partitions and
2335 * up to four general purpose partitions.
2336 * For each partition enabled in EXT_CSD a block device will be allocatedi
2337 * to provide access to the partition.
2338 */
2339
Andrei Warkentin371a6892011-04-11 18:10:25 -05002340static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2341{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002342 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002343
2344 if (!mmc_card_mmc(card))
2345 return 0;
2346
Namjae Jeone0c368d2011-10-06 23:41:38 +09002347 for (idx = 0; idx < card->nr_parts; idx++) {
2348 if (card->part[idx].size) {
2349 ret = mmc_blk_alloc_part(card, md,
2350 card->part[idx].part_cfg,
2351 card->part[idx].size >> 9,
2352 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002353 card->part[idx].name,
2354 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002355 if (ret)
2356 return ret;
2357 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002358 }
2359
2360 return ret;
2361}
2362
Andrei Warkentin371a6892011-04-11 18:10:25 -05002363static void mmc_blk_remove_req(struct mmc_blk_data *md)
2364{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002365 struct mmc_card *card;
2366
Andrei Warkentin371a6892011-04-11 18:10:25 -05002367 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002368 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002369 device_remove_file(disk_to_dev(md->disk),
2370 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002371 if (md->disk->flags & GENHD_FL_UP) {
2372 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002373 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2374 card->ext_csd.boot_ro_lockable)
2375 device_remove_file(disk_to_dev(md->disk),
2376 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002377
2378 /* Stop new requests from getting into the queue */
2379 del_gendisk(md->disk);
2380 }
2381
2382 /* Then flush out any already in there */
2383 mmc_cleanup_queue(&md->queue);
2384 mmc_blk_put(md);
2385 }
2386}
2387
2388static void mmc_blk_remove_parts(struct mmc_card *card,
2389 struct mmc_blk_data *md)
2390{
2391 struct list_head *pos, *q;
2392 struct mmc_blk_data *part_md;
2393
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002394 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002395 list_for_each_safe(pos, q, &md->part) {
2396 part_md = list_entry(pos, struct mmc_blk_data, part);
2397 list_del(pos);
2398 mmc_blk_remove_req(part_md);
2399 }
2400}
2401
2402static int mmc_add_disk(struct mmc_blk_data *md)
2403{
2404 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002405 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002406
2407 add_disk(md->disk);
2408 md->force_ro.show = force_ro_show;
2409 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302410 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002411 md->force_ro.attr.name = "force_ro";
2412 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2413 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2414 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002415 goto force_ro_fail;
2416
2417 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2418 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002419 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002420
2421 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2422 mode = S_IRUGO;
2423 else
2424 mode = S_IRUGO | S_IWUSR;
2425
2426 md->power_ro_lock.show = power_ro_lock_show;
2427 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002428 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002429 md->power_ro_lock.attr.mode = mode;
2430 md->power_ro_lock.attr.name =
2431 "ro_lock_until_next_power_on";
2432 ret = device_create_file(disk_to_dev(md->disk),
2433 &md->power_ro_lock);
2434 if (ret)
2435 goto power_ro_lock_fail;
2436 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002437
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002438 md->num_wr_reqs_to_start_packing.show =
2439 num_wr_reqs_to_start_packing_show;
2440 md->num_wr_reqs_to_start_packing.store =
2441 num_wr_reqs_to_start_packing_store;
2442 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2443 md->num_wr_reqs_to_start_packing.attr.name =
2444 "num_wr_reqs_to_start_packing";
2445 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2446 ret = device_create_file(disk_to_dev(md->disk),
2447 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002448 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002449 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002450
Maya Erez2d33a192013-01-04 15:52:41 +02002451 md->min_sectors_to_check_bkops_status.show =
2452 min_sectors_to_check_bkops_status_show;
2453 md->min_sectors_to_check_bkops_status.store =
2454 min_sectors_to_check_bkops_status_store;
2455 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2456 md->min_sectors_to_check_bkops_status.attr.name =
2457 "min_sectors_to_check_bkops_status";
2458 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2459 ret = device_create_file(disk_to_dev(md->disk),
2460 &md->min_sectors_to_check_bkops_status);
2461 if (ret)
2462 goto min_sectors_to_check_bkops_status_fails;
2463
Johan Rudholmadd710e2011-12-02 08:51:06 +01002464 return ret;
2465
Maya Erez2d33a192013-01-04 15:52:41 +02002466min_sectors_to_check_bkops_status_fails:
2467 device_remove_file(disk_to_dev(md->disk),
2468 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002469num_wr_reqs_to_start_packing_fail:
2470 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002471power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002472 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002473force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002474 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002475
2476 return ret;
2477}
2478
Chris Ballc59d4472011-11-11 22:01:43 -05002479#define CID_MANFID_SANDISK 0x2
2480#define CID_MANFID_TOSHIBA 0x11
2481#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302482#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002483
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002484static const struct mmc_fixup blk_fixups[] =
2485{
Chris Ballc59d4472011-11-11 22:01:43 -05002486 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2487 MMC_QUIRK_INAND_CMD38),
2488 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2489 MMC_QUIRK_INAND_CMD38),
2490 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2491 MMC_QUIRK_INAND_CMD38),
2492 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2493 MMC_QUIRK_INAND_CMD38),
2494 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2495 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002496
2497 /*
2498 * Some MMC cards experience performance degradation with CMD23
2499 * instead of CMD12-bounded multiblock transfers. For now we'll
2500 * black list what's bad...
2501 * - Certain Toshiba cards.
2502 *
2503 * N.B. This doesn't affect SD cards.
2504 */
Chris Ballc59d4472011-11-11 22:01:43 -05002505 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002506 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002507 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002508 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002509 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002510 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002511
2512 /*
2513 * Some Micron MMC cards needs longer data read timeout than
2514 * indicated in CSD.
2515 */
Chris Ballc59d4472011-11-11 22:01:43 -05002516 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002517 MMC_QUIRK_LONG_READ_TIME),
2518
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302519 /* Some INAND MCP devices advertise incorrect timeout values */
2520 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2521 MMC_QUIRK_INAND_DATA_TIMEOUT),
2522
Ian Chenc4856122012-11-01 13:26:21 +05302523 /*
2524 * On these Samsung MoviNAND parts, performing secure erase or
2525 * secure trim can result in unrecoverable corruption due to a
2526 * firmware bug.
2527 */
2528 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2530 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2531 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2532 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2533 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2534 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2535 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2536 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2537 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2538 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2539 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2540 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2541 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2542 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2543 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2544
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002545 END_FIXUP
2546};
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548static int mmc_blk_probe(struct mmc_card *card)
2549{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002550 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002551 char cap_str[10];
2552
Pierre Ossman912490d2005-05-21 10:27:02 +01002553 /*
2554 * Check that the card supports the command class(es) we need.
2555 */
2556 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return -ENODEV;
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 md = mmc_blk_alloc(card);
2560 if (IS_ERR(md))
2561 return PTR_ERR(md);
2562
Yi Li444122f2009-02-05 15:31:57 +08002563 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002564 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302565 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002567 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Andrei Warkentin371a6892011-04-11 18:10:25 -05002569 if (mmc_blk_alloc_parts(card, md))
2570 goto out;
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002573 mmc_fixup_device(card, blk_fixups);
2574
San Mehat148c57a2009-07-30 08:21:19 -07002575#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2576 mmc_set_bus_resume_policy(card->host, 1);
2577#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002578 if (mmc_add_disk(md))
2579 goto out;
2580
2581 list_for_each_entry(part_md, &md->part, part) {
2582 if (mmc_add_disk(part_md))
2583 goto out;
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return 0;
2586
2587 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002588 mmc_blk_remove_parts(card, md);
2589 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593static void mmc_blk_remove(struct mmc_card *card)
2594{
2595 struct mmc_blk_data *md = mmc_get_drvdata(card);
2596
Andrei Warkentin371a6892011-04-11 18:10:25 -05002597 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002598 mmc_claim_host(card->host);
2599 mmc_blk_part_switch(card, md);
2600 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002601 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002603#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2604 mmc_set_bus_resume_policy(card->host, 0);
2605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002609static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002611 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct mmc_blk_data *md = mmc_get_drvdata(card);
2613
2614 if (md) {
2615 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002616 list_for_each_entry(part_md, &md->part, part) {
2617 mmc_queue_suspend(&part_md->queue);
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620 return 0;
2621}
2622
2623static int mmc_blk_resume(struct mmc_card *card)
2624{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002625 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct mmc_blk_data *md = mmc_get_drvdata(card);
2627
2628 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002629 /*
2630 * Resume involves the card going into idle state,
2631 * so current partition is always the main one.
2632 */
2633 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002635 list_for_each_entry(part_md, &md->part, part) {
2636 mmc_queue_resume(&part_md->queue);
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639 return 0;
2640}
2641#else
2642#define mmc_blk_suspend NULL
2643#define mmc_blk_resume NULL
2644#endif
2645
2646static struct mmc_driver mmc_driver = {
2647 .drv = {
2648 .name = "mmcblk",
2649 },
2650 .probe = mmc_blk_probe,
2651 .remove = mmc_blk_remove,
2652 .suspend = mmc_blk_suspend,
2653 .resume = mmc_blk_resume,
2654};
2655
2656static int __init mmc_blk_init(void)
2657{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002658 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002660 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2661 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2662
2663 max_devices = 256 / perdev_minors;
2664
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002665 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2666 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002669 res = mmc_register_driver(&mmc_driver);
2670 if (res)
2671 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002673 return 0;
2674 out2:
2675 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 out:
2677 return res;
2678}
2679
2680static void __exit mmc_blk_exit(void)
2681{
2682 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002683 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686module_init(mmc_blk_init);
2687module_exit(mmc_blk_exit);
2688
2689MODULE_LICENSE("GPL");
2690MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2691