blob: 73c47cd1a8bed3bec79dd208404dfbaf2bc7d2ee [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
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +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)
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200344 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 }
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +0200351
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 Erez5f360692012-10-10 03:47:54 +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
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001560static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1561{
1562 struct request_queue *q = mq->queue;
1563 struct mmc_card *card = mq->card;
1564 struct request *cur = req, *next = NULL;
1565 struct mmc_blk_data *md = mq->data;
1566 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1567 unsigned int req_sectors = 0, phys_segments = 0;
1568 unsigned int max_blk_count, max_phys_segs;
1569 u8 put_back = 0;
1570 u8 max_packed_rw = 0;
1571 u8 reqs = 0;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001572 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001573
1574 mmc_blk_clear_packed(mq->mqrq_cur);
1575
1576 if (!(md->flags & MMC_BLK_CMD23) ||
1577 !card->ext_csd.packed_event_en)
1578 goto no_packed;
1579
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001580 if (!mq->wr_packing_enabled)
1581 goto no_packed;
1582
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001583 if ((rq_data_dir(cur) == WRITE) &&
1584 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1585 max_packed_rw = card->ext_csd.max_packed_writes;
1586
1587 if (max_packed_rw == 0)
1588 goto no_packed;
1589
1590 if (mmc_req_rel_wr(cur) &&
1591 (md->flags & MMC_BLK_REL_WR) &&
1592 !en_rel_wr)
1593 goto no_packed;
1594
1595 if (mmc_large_sec(card) &&
1596 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1597 goto no_packed;
1598
1599 max_blk_count = min(card->host->max_blk_count,
1600 card->host->max_req_size >> 9);
1601 if (unlikely(max_blk_count > 0xffff))
1602 max_blk_count = 0xffff;
1603
1604 max_phys_segs = queue_max_segments(q);
1605 req_sectors += blk_rq_sectors(cur);
1606 phys_segments += cur->nr_phys_segments;
1607
1608 if (rq_data_dir(cur) == WRITE) {
1609 req_sectors++;
1610 phys_segments++;
1611 }
1612
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001613 spin_lock(&stats->lock);
1614
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001615 while (reqs < max_packed_rw - 1) {
1616 spin_lock_irq(q->queue_lock);
1617 next = blk_fetch_request(q);
1618 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001619 if (!next) {
1620 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001621 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001622 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001623
1624 if (mmc_large_sec(card) &&
1625 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001626 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001627 put_back = 1;
1628 break;
1629 }
1630
1631 if (next->cmd_flags & REQ_DISCARD ||
1632 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001633 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001634 put_back = 1;
1635 break;
1636 }
1637
1638 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001639 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001640 put_back = 1;
1641 break;
1642 }
1643
1644 if (mmc_req_rel_wr(next) &&
1645 (md->flags & MMC_BLK_REL_WR) &&
1646 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001647 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001648 put_back = 1;
1649 break;
1650 }
1651
1652 req_sectors += blk_rq_sectors(next);
1653 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001654 if (stats->enabled)
1655 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001656 put_back = 1;
1657 break;
1658 }
1659
1660 phys_segments += next->nr_phys_segments;
1661 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001662 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001663 put_back = 1;
1664 break;
1665 }
1666
Maya Erez5f360692012-10-10 03:47:54 +02001667 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001668 mq->num_of_potential_packed_wr_reqs++;
Maya Erez5f360692012-10-10 03:47:54 +02001669 if (card->ext_csd.bkops_en)
1670 card->bkops_info.sectors_changed +=
1671 blk_rq_sectors(next);
1672 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001673 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1674 cur = next;
1675 reqs++;
1676 }
1677
1678 if (put_back) {
1679 spin_lock_irq(q->queue_lock);
1680 blk_requeue_request(q, next);
1681 spin_unlock_irq(q->queue_lock);
1682 }
1683
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001684 if (stats->enabled) {
1685 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1686 stats->packing_events[reqs + 1]++;
1687 if (reqs + 1 == max_packed_rw)
1688 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1689 }
1690
1691 spin_unlock(&stats->lock);
1692
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001693 if (reqs > 0) {
1694 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1695 mq->mqrq_cur->packed_num = ++reqs;
1696 mq->mqrq_cur->packed_retries = reqs;
1697 return reqs;
1698 }
1699
1700no_packed:
1701 mmc_blk_clear_packed(mq->mqrq_cur);
1702 return 0;
1703}
1704
1705static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1706 struct mmc_card *card,
1707 struct mmc_queue *mq)
1708{
1709 struct mmc_blk_request *brq = &mqrq->brq;
1710 struct request *req = mqrq->req;
1711 struct request *prq;
1712 struct mmc_blk_data *md = mq->data;
1713 bool do_rel_wr, do_data_tag;
1714 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1715 u8 i = 1;
1716
1717 mqrq->packed_cmd = MMC_PACKED_WRITE;
1718 mqrq->packed_blocks = 0;
1719 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1720
1721 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1722 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1723 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1724
1725 /*
1726 * Argument for each entry of packed group
1727 */
1728 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1729 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1730 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1731 (prq->cmd_flags & REQ_META) &&
1732 (rq_data_dir(prq) == WRITE) &&
1733 ((brq->data.blocks * brq->data.blksz) >=
1734 card->ext_csd.data_tag_unit_size);
1735 /* Argument of CMD23 */
1736 packed_cmd_hdr[(i * 2)] =
1737 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1738 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1739 blk_rq_sectors(prq);
1740 /* Argument of CMD18 or CMD25 */
1741 packed_cmd_hdr[((i * 2)) + 1] =
1742 mmc_card_blockaddr(card) ?
1743 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1744 mqrq->packed_blocks += blk_rq_sectors(prq);
1745 i++;
1746 }
1747
1748 memset(brq, 0, sizeof(struct mmc_blk_request));
1749 brq->mrq.cmd = &brq->cmd;
1750 brq->mrq.data = &brq->data;
1751 brq->mrq.sbc = &brq->sbc;
1752 brq->mrq.stop = &brq->stop;
1753
1754 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1755 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1756 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1757
1758 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1759 brq->cmd.arg = blk_rq_pos(req);
1760 if (!mmc_card_blockaddr(card))
1761 brq->cmd.arg <<= 9;
1762 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1763
1764 brq->data.blksz = 512;
1765 brq->data.blocks = mqrq->packed_blocks + 1;
1766 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02001767 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001768
1769 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1770 brq->stop.arg = 0;
1771 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1772
1773 mmc_set_data_timeout(&brq->data, card);
1774
1775 brq->data.sg = mqrq->sg;
1776 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1777
1778 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02001779
1780 /*
1781 * This is intended for packed commands tests usage - in case these
1782 * functions are not in use the respective pointers are NULL
1783 */
1784 if (mq->err_check_fn)
1785 mqrq->mmc_active.err_check = mq->err_check_fn;
1786 else
1787 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1788
1789 if (mq->packed_test_fn)
1790 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001791
1792 mmc_queue_bounce_pre(mqrq);
1793}
1794
Adrian Hunter67716322011-08-29 16:42:15 +03001795static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1796 struct mmc_blk_request *brq, struct request *req,
1797 int ret)
1798{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001799 struct mmc_queue_req *mq_rq;
1800 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1801
Adrian Hunter67716322011-08-29 16:42:15 +03001802 /*
1803 * If this is an SD card and we're writing, we can first
1804 * mark the known good sectors as ok.
1805 *
1806 * If the card is not SD, we can still ok written sectors
1807 * as reported by the controller (which might be less than
1808 * the real number of written sectors, but never more).
1809 */
1810 if (mmc_card_sd(card)) {
1811 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301812 if (!brq->data.fault_injected) {
1813 blocks = mmc_sd_num_wr_blocks(card);
1814 if (blocks != (u32)-1)
1815 ret = blk_end_request(req, 0, blocks << 9);
1816 } else
1817 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001818 } else {
1819 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1820 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1821 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001822 return ret;
1823}
1824
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001825static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1826{
1827 struct request *prq;
1828 int idx = mq_rq->packed_fail_idx, i = 0;
1829 int ret = 0;
1830
1831 while (!list_empty(&mq_rq->packed_list)) {
1832 prq = list_entry_rq(mq_rq->packed_list.next);
1833 if (idx == i) {
1834 /* retry from error index */
1835 mq_rq->packed_num -= idx;
1836 mq_rq->req = prq;
1837 ret = 1;
1838
1839 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1840 list_del_init(&prq->queuelist);
1841 mmc_blk_clear_packed(mq_rq);
1842 }
1843 return ret;
1844 }
1845 list_del_init(&prq->queuelist);
1846 blk_end_request(prq, 0, blk_rq_bytes(prq));
1847 i++;
1848 }
1849
1850 mmc_blk_clear_packed(mq_rq);
1851 return ret;
1852}
1853static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1854{
1855 struct request *prq;
1856
1857 while (!list_empty(&mq_rq->packed_list)) {
1858 prq = list_entry_rq(mq_rq->packed_list.next);
1859 list_del_init(&prq->queuelist);
1860 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1861 }
1862
1863 mmc_blk_clear_packed(mq_rq);
1864}
1865
1866static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1867 struct mmc_queue_req *mq_rq)
1868{
1869 struct request *prq;
1870 struct request_queue *q = mq->queue;
1871
1872 while (!list_empty(&mq_rq->packed_list)) {
1873 prq = list_entry_rq(mq_rq->packed_list.prev);
1874 if (prq->queuelist.prev != &mq_rq->packed_list) {
1875 list_del_init(&prq->queuelist);
1876 spin_lock_irq(q->queue_lock);
1877 blk_requeue_request(mq->queue, prq);
1878 spin_unlock_irq(q->queue_lock);
1879 } else {
1880 list_del_init(&prq->queuelist);
1881 }
1882 }
1883
1884 mmc_blk_clear_packed(mq_rq);
1885}
1886
Per Forlinee8a43a2011-07-01 18:55:33 +02001887static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001888{
1889 struct mmc_blk_data *md = mq->data;
1890 struct mmc_card *card = md->queue.card;
1891 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001892 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001893 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001894 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001895 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001896 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001897 const u8 packed_num = 2;
1898 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001899
1900 if (!rqc && !mq->mqrq_prev->req)
1901 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001902
Maya Erez5f360692012-10-10 03:47:54 +02001903 if (rqc) {
1904 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
1905 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001906 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02001907 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001908
Per Forlin54d49d772011-07-01 18:55:29 +02001909 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001910 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001911 if (reqs >= packed_num)
1912 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1913 card, mq);
1914 else
1915 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001916 areq = &mq->mqrq_cur->mmc_active;
1917 } else
1918 areq = NULL;
1919 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001920 if (!areq) {
1921 if (status == MMC_BLK_NEW_REQUEST)
1922 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001923 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001924 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001925
Per Forlinee8a43a2011-07-01 18:55:33 +02001926 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1927 brq = &mq_rq->brq;
1928 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001929 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001930 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001931
Per Forlind78d4a82011-07-01 18:55:30 +02001932 switch (status) {
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02001933 case MMC_BLK_NEW_REQUEST:
1934 BUG(); /* should never get here */
Per Forlind78d4a82011-07-01 18:55:30 +02001935 case MMC_BLK_SUCCESS:
1936 case MMC_BLK_PARTIAL:
1937 /*
1938 * A block was successfully transferred.
1939 */
Adrian Hunter67716322011-08-29 16:42:15 +03001940 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001941
1942 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1943 ret = mmc_blk_end_packed_req(mq_rq);
1944 break;
1945 } else {
1946 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001947 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001948 }
1949
Adrian Hunter67716322011-08-29 16:42:15 +03001950 /*
1951 * If the blk_end_request function returns non-zero even
1952 * though all data has been transferred and no errors
1953 * were returned by the host controller, it's a bug.
1954 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001955 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301956 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001957 __func__, blk_rq_bytes(req),
1958 brq->data.bytes_xfered);
1959 rqc = NULL;
1960 goto cmd_abort;
1961 }
Per Forlind78d4a82011-07-01 18:55:30 +02001962 break;
1963 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001964 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1965 if (!mmc_blk_reset(md, card->host, type))
1966 break;
1967 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001968 case MMC_BLK_RETRY:
1969 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001970 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001971 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001972 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001973 if (!mmc_blk_reset(md, card->host, type))
1974 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001975 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001976 case MMC_BLK_DATA_ERR: {
1977 int err;
1978
1979 err = mmc_blk_reset(md, card->host, type);
1980 if (!err)
1981 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001982 if (err == -ENODEV ||
1983 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001984 goto cmd_abort;
1985 /* Fall through */
1986 }
1987 case MMC_BLK_ECC_ERR:
1988 if (brq->data.blocks > 1) {
1989 /* Redo read one sector at a time */
1990 pr_warning("%s: retrying using single block read\n",
1991 req->rq_disk->disk_name);
1992 disable_multi = 1;
1993 break;
1994 }
Per Forlind78d4a82011-07-01 18:55:30 +02001995 /*
1996 * After an error, we redo I/O one sector at a
1997 * time, so we only reach here after trying to
1998 * read a single sector.
1999 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302000 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002001 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002002 if (!ret)
2003 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002004 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302005 case MMC_BLK_NOMEDIUM:
2006 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002007 }
2008
Per Forlinee8a43a2011-07-01 18:55:33 +02002009 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002010 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2011 /*
2012 * In case of a incomplete request
2013 * prepare it again and resend.
2014 */
2015 mmc_blk_rw_rq_prep(mq_rq, card,
2016 disable_multi, mq);
2017 mmc_start_req(card->host,
2018 &mq_rq->mmc_active, NULL);
2019 } else {
2020 if (!mq_rq->packed_retries)
2021 goto cmd_abort;
2022 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2023 mmc_start_req(card->host,
2024 &mq_rq->mmc_active, NULL);
2025 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 } while (ret);
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 1;
2030
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002031 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002032 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2033 if (mmc_card_removed(card))
2034 req->cmd_flags |= REQ_QUIET;
2035 while (ret)
2036 ret = blk_end_request(req, -EIO,
2037 blk_rq_cur_bytes(req));
2038 } else {
2039 mmc_blk_abort_packed_req(mq_rq);
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Per Forlinee8a43a2011-07-01 18:55:33 +02002042 start_new_req:
2043 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002044 /*
2045 * If current request is packed, it needs to put back.
2046 */
2047 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2048 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2049
Per Forlinee8a43a2011-07-01 18:55:33 +02002050 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2051 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2052 }
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
2055}
2056
Adrian Hunterbd788c92010-08-11 14:17:47 -07002057static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2058{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002059 int ret;
2060 struct mmc_blk_data *md = mq->data;
2061 struct mmc_card *card = md->queue.card;
2062
San Mehat148c57a2009-07-30 08:21:19 -07002063#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2064 if (mmc_bus_needs_resume(card->host)) {
2065 mmc_resume_bus(card->host);
2066 mmc_blk_set_blksize(md, card);
2067 }
2068#endif
2069
Per Forlinee8a43a2011-07-01 18:55:33 +02002070 if (req && !mq->mqrq_prev->req)
2071 /* claim host only for the first request */
2072 mmc_claim_host(card->host);
2073
Andrei Warkentin371a6892011-04-11 18:10:25 -05002074 ret = mmc_blk_part_switch(card, md);
2075 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002076 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302077 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002078 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002079 ret = 0;
2080 goto out;
2081 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002082
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002083 mmc_blk_write_packing_control(mq, req);
2084
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002085 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002086 if (req && req->cmd_flags & REQ_SANITIZE) {
2087 /* complete ongoing async transfer before issuing sanitize */
2088 if (card->host && card->host->areq)
2089 mmc_blk_issue_rw_rq(mq, NULL);
2090 ret = mmc_blk_issue_sanitize_rq(mq, req);
2091 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002092 /* complete ongoing async transfer before issuing discard */
2093 if (card->host->areq)
2094 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302095 if (req->cmd_flags & REQ_SECURE &&
2096 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002097 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002098 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002099 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002100 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002101 /* complete ongoing async transfer before issuing flush */
2102 if (card->host->areq)
2103 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002104 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002105 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002106 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002107 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002108
Andrei Warkentin371a6892011-04-11 18:10:25 -05002109out:
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002110 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST))
Per Forlinee8a43a2011-07-01 18:55:33 +02002111 /* release host only when there are no more requests */
2112 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002113 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Russell Kinga6f6c962006-01-03 22:38:44 +00002116static inline int mmc_blk_readonly(struct mmc_card *card)
2117{
2118 return mmc_card_readonly(card) ||
2119 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2120}
2121
Andrei Warkentin371a6892011-04-11 18:10:25 -05002122static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2123 struct device *parent,
2124 sector_t size,
2125 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002126 const char *subname,
2127 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 struct mmc_blk_data *md;
2130 int devidx, ret;
2131
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002132 devidx = find_first_zero_bit(dev_use, max_devices);
2133 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return ERR_PTR(-ENOSPC);
2135 __set_bit(devidx, dev_use);
2136
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002137 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002138 if (!md) {
2139 ret = -ENOMEM;
2140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002142
Russell Kinga6f6c962006-01-03 22:38:44 +00002143 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002144 * !subname implies we are creating main mmc_blk_data that will be
2145 * associated with mmc_card with mmc_set_drvdata. Due to device
2146 * partitions, devidx will not coincide with a per-physical card
2147 * index anymore so we keep track of a name index.
2148 */
2149 if (!subname) {
2150 md->name_idx = find_first_zero_bit(name_use, max_devices);
2151 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002152 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002153 md->name_idx = ((struct mmc_blk_data *)
2154 dev_to_disk(parent)->private_data)->name_idx;
2155
Johan Rudholmadd710e2011-12-02 08:51:06 +01002156 md->area_type = area_type;
2157
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002158 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002159 * Set the read-only status based on the supported commands
2160 * and the write protect switch.
2161 */
2162 md->read_only = mmc_blk_readonly(card);
2163
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002164 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002165 if (md->disk == NULL) {
2166 ret = -ENOMEM;
2167 goto err_kfree;
2168 }
2169
2170 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002171 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002172 md->usage = 1;
2173
Adrian Hunterd09408a2011-06-23 13:40:28 +03002174 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002175 if (ret)
2176 goto err_putdisk;
2177
Russell Kinga6f6c962006-01-03 22:38:44 +00002178 md->queue.issue_fn = mmc_blk_issue_rq;
2179 md->queue.data = md;
2180
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002181 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002182 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002183 md->disk->fops = &mmc_bdops;
2184 md->disk->private_data = md;
2185 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002186 md->disk->driverfs_dev = parent;
2187 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002188 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002189 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2190 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002191
2192 /*
2193 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2194 *
2195 * - be set for removable media with permanent block devices
2196 * - be unset for removable block devices with permanent media
2197 *
2198 * Since MMC block devices clearly fall under the second
2199 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2200 * should use the block device creation/destruction hotplug
2201 * messages to tell when the card is present.
2202 */
2203
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002204 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2205 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002206
Martin K. Petersene1defc42009-05-22 17:17:49 -04002207 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002208 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002209
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002210 if (mmc_host_cmd23(card->host)) {
2211 if (mmc_card_mmc(card) ||
2212 (mmc_card_sd(card) &&
2213 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2214 md->flags |= MMC_BLK_CMD23;
2215 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002216
2217 if (mmc_card_mmc(card) &&
2218 md->flags & MMC_BLK_CMD23 &&
2219 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2220 card->ext_csd.rel_sectors)) {
2221 md->flags |= MMC_BLK_REL_WR;
2222 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2223 }
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002226
2227 err_putdisk:
2228 put_disk(md->disk);
2229 err_kfree:
2230 kfree(md);
2231 out:
2232 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2236{
2237 sector_t size;
2238 struct mmc_blk_data *md;
2239
2240 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2241 /*
2242 * The EXT_CSD sector count is in number or 512 byte
2243 * sectors.
2244 */
2245 size = card->ext_csd.sectors;
2246 } else {
2247 /*
2248 * The CSD capacity field is in units of read_blkbits.
2249 * set_capacity takes units of 512 bytes.
2250 */
2251 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2252 }
2253
Johan Rudholmadd710e2011-12-02 08:51:06 +01002254 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2255 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002256 return md;
2257}
2258
2259static int mmc_blk_alloc_part(struct mmc_card *card,
2260 struct mmc_blk_data *md,
2261 unsigned int part_type,
2262 sector_t size,
2263 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002264 const char *subname,
2265 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002266{
2267 char cap_str[10];
2268 struct mmc_blk_data *part_md;
2269
2270 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002271 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002272 if (IS_ERR(part_md))
2273 return PTR_ERR(part_md);
2274 part_md->part_type = part_type;
2275 list_add(&part_md->part, &md->part);
2276
2277 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2278 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302279 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002280 part_md->disk->disk_name, mmc_card_id(card),
2281 mmc_card_name(card), part_md->part_type, cap_str);
2282 return 0;
2283}
2284
Namjae Jeone0c368d2011-10-06 23:41:38 +09002285/* MMC Physical partitions consist of two boot partitions and
2286 * up to four general purpose partitions.
2287 * For each partition enabled in EXT_CSD a block device will be allocatedi
2288 * to provide access to the partition.
2289 */
2290
Andrei Warkentin371a6892011-04-11 18:10:25 -05002291static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2292{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002293 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002294
2295 if (!mmc_card_mmc(card))
2296 return 0;
2297
Namjae Jeone0c368d2011-10-06 23:41:38 +09002298 for (idx = 0; idx < card->nr_parts; idx++) {
2299 if (card->part[idx].size) {
2300 ret = mmc_blk_alloc_part(card, md,
2301 card->part[idx].part_cfg,
2302 card->part[idx].size >> 9,
2303 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002304 card->part[idx].name,
2305 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002306 if (ret)
2307 return ret;
2308 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309 }
2310
2311 return ret;
2312}
2313
Andrei Warkentin371a6892011-04-11 18:10:25 -05002314static void mmc_blk_remove_req(struct mmc_blk_data *md)
2315{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002316 struct mmc_card *card;
2317
Andrei Warkentin371a6892011-04-11 18:10:25 -05002318 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002319 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002320 device_remove_file(disk_to_dev(md->disk),
2321 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002322 if (md->disk->flags & GENHD_FL_UP) {
2323 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002324 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2325 card->ext_csd.boot_ro_lockable)
2326 device_remove_file(disk_to_dev(md->disk),
2327 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002328
2329 /* Stop new requests from getting into the queue */
2330 del_gendisk(md->disk);
2331 }
2332
2333 /* Then flush out any already in there */
2334 mmc_cleanup_queue(&md->queue);
2335 mmc_blk_put(md);
2336 }
2337}
2338
2339static void mmc_blk_remove_parts(struct mmc_card *card,
2340 struct mmc_blk_data *md)
2341{
2342 struct list_head *pos, *q;
2343 struct mmc_blk_data *part_md;
2344
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002345 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002346 list_for_each_safe(pos, q, &md->part) {
2347 part_md = list_entry(pos, struct mmc_blk_data, part);
2348 list_del(pos);
2349 mmc_blk_remove_req(part_md);
2350 }
2351}
2352
2353static int mmc_add_disk(struct mmc_blk_data *md)
2354{
2355 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002356 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002357
2358 add_disk(md->disk);
2359 md->force_ro.show = force_ro_show;
2360 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302361 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002362 md->force_ro.attr.name = "force_ro";
2363 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2364 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2365 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002366 goto force_ro_fail;
2367
2368 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2369 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002370 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002371
2372 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2373 mode = S_IRUGO;
2374 else
2375 mode = S_IRUGO | S_IWUSR;
2376
2377 md->power_ro_lock.show = power_ro_lock_show;
2378 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002379 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002380 md->power_ro_lock.attr.mode = mode;
2381 md->power_ro_lock.attr.name =
2382 "ro_lock_until_next_power_on";
2383 ret = device_create_file(disk_to_dev(md->disk),
2384 &md->power_ro_lock);
2385 if (ret)
2386 goto power_ro_lock_fail;
2387 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002389 md->num_wr_reqs_to_start_packing.show =
2390 num_wr_reqs_to_start_packing_show;
2391 md->num_wr_reqs_to_start_packing.store =
2392 num_wr_reqs_to_start_packing_store;
2393 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2394 md->num_wr_reqs_to_start_packing.attr.name =
2395 "num_wr_reqs_to_start_packing";
2396 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2397 ret = device_create_file(disk_to_dev(md->disk),
2398 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002399 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002400 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002401
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002402 md->min_sectors_to_check_bkops_status.show =
2403 min_sectors_to_check_bkops_status_show;
2404 md->min_sectors_to_check_bkops_status.store =
2405 min_sectors_to_check_bkops_status_store;
2406 sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr);
2407 md->min_sectors_to_check_bkops_status.attr.name =
2408 "min_sectors_to_check_bkops_status";
2409 md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR;
2410 ret = device_create_file(disk_to_dev(md->disk),
2411 &md->min_sectors_to_check_bkops_status);
2412 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002413 goto min_sectors_to_check_bkops_status_fails;
Yaniv Gardie6c6b4b2012-10-11 11:36:24 +02002414
Johan Rudholmadd710e2011-12-02 08:51:06 +01002415 return ret;
2416
Maya Erez8d006d32012-10-30 09:02:39 +02002417min_sectors_to_check_bkops_status_fails:
2418 device_remove_file(disk_to_dev(md->disk),
2419 &md->num_wr_reqs_to_start_packing);
2420num_wr_reqs_to_start_packing_fail:
2421 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002422power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002423 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002424force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002425 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002426
2427 return ret;
2428}
2429
Chris Ballc59d4472011-11-11 22:01:43 -05002430#define CID_MANFID_SANDISK 0x2
2431#define CID_MANFID_TOSHIBA 0x11
2432#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302433#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002434
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002435static const struct mmc_fixup blk_fixups[] =
2436{
Chris Ballc59d4472011-11-11 22:01:43 -05002437 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2438 MMC_QUIRK_INAND_CMD38),
2439 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2440 MMC_QUIRK_INAND_CMD38),
2441 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2442 MMC_QUIRK_INAND_CMD38),
2443 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2444 MMC_QUIRK_INAND_CMD38),
2445 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2446 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002447
2448 /*
2449 * Some MMC cards experience performance degradation with CMD23
2450 * instead of CMD12-bounded multiblock transfers. For now we'll
2451 * black list what's bad...
2452 * - Certain Toshiba cards.
2453 *
2454 * N.B. This doesn't affect SD cards.
2455 */
Chris Ballc59d4472011-11-11 22:01:43 -05002456 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002457 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002458 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002459 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002460 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002461 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002462
2463 /*
2464 * Some Micron MMC cards needs longer data read timeout than
2465 * indicated in CSD.
2466 */
Chris Ballc59d4472011-11-11 22:01:43 -05002467 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002468 MMC_QUIRK_LONG_READ_TIME),
2469
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302470 /* Some INAND MCP devices advertise incorrect timeout values */
2471 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2472 MMC_QUIRK_INAND_DATA_TIMEOUT),
2473
Ian Chenc4856122012-11-01 13:26:21 +05302474 /*
2475 * On these Samsung MoviNAND parts, performing secure erase or
2476 * secure trim can result in unrecoverable corruption due to a
2477 * firmware bug.
2478 */
2479 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2480 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2481 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2482 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2483 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2484 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2485 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2486 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2487 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2488 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2489 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2490 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2491 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2492 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2493 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2494 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2495
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002496 END_FIXUP
2497};
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499static int mmc_blk_probe(struct mmc_card *card)
2500{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002501 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002502 char cap_str[10];
2503
Pierre Ossman912490d2005-05-21 10:27:02 +01002504 /*
2505 * Check that the card supports the command class(es) we need.
2506 */
2507 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return -ENODEV;
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 md = mmc_blk_alloc(card);
2511 if (IS_ERR(md))
2512 return PTR_ERR(md);
2513
Yi Li444122f2009-02-05 15:31:57 +08002514 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002515 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302516 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002518 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Andrei Warkentin371a6892011-04-11 18:10:25 -05002520 if (mmc_blk_alloc_parts(card, md))
2521 goto out;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002524 mmc_fixup_device(card, blk_fixups);
2525
San Mehat148c57a2009-07-30 08:21:19 -07002526#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2527 mmc_set_bus_resume_policy(card->host, 1);
2528#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002529 if (mmc_add_disk(md))
2530 goto out;
2531
2532 list_for_each_entry(part_md, &md->part, part) {
2533 if (mmc_add_disk(part_md))
2534 goto out;
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return 0;
2537
2538 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002539 mmc_blk_remove_parts(card, md);
2540 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
2544static void mmc_blk_remove(struct mmc_card *card)
2545{
2546 struct mmc_blk_data *md = mmc_get_drvdata(card);
2547
Andrei Warkentin371a6892011-04-11 18:10:25 -05002548 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002549 mmc_claim_host(card->host);
2550 mmc_blk_part_switch(card, md);
2551 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002552 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002554#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2555 mmc_set_bus_resume_policy(card->host, 0);
2556#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002560static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002562 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct mmc_blk_data *md = mmc_get_drvdata(card);
2564
2565 if (md) {
2566 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002567 list_for_each_entry(part_md, &md->part, part) {
2568 mmc_queue_suspend(&part_md->queue);
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 return 0;
2572}
2573
2574static int mmc_blk_resume(struct mmc_card *card)
2575{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002576 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct mmc_blk_data *md = mmc_get_drvdata(card);
2578
2579 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002580 /*
2581 * Resume involves the card going into idle state,
2582 * so current partition is always the main one.
2583 */
2584 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002586 list_for_each_entry(part_md, &md->part, part) {
2587 mmc_queue_resume(&part_md->queue);
2588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 return 0;
2591}
2592#else
2593#define mmc_blk_suspend NULL
2594#define mmc_blk_resume NULL
2595#endif
2596
2597static struct mmc_driver mmc_driver = {
2598 .drv = {
2599 .name = "mmcblk",
2600 },
2601 .probe = mmc_blk_probe,
2602 .remove = mmc_blk_remove,
2603 .suspend = mmc_blk_suspend,
2604 .resume = mmc_blk_resume,
2605};
2606
2607static int __init mmc_blk_init(void)
2608{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002609 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002611 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2612 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2613
2614 max_devices = 256 / perdev_minors;
2615
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002616 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2617 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002620 res = mmc_register_driver(&mmc_driver);
2621 if (res)
2622 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002624 return 0;
2625 out2:
2626 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 out:
2628 return res;
2629}
2630
2631static void __exit mmc_blk_exit(void)
2632{
2633 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002634 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
2637module_init(mmc_blk_init);
2638module_exit(mmc_blk_exit);
2639
2640MODULE_LICENSE("GPL");
2641MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2642