blob: 6fbcfbafa0faade9f98cbeb81bd0743af2d70483 [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>
Asutosh Dasbbc84782013-02-11 15:31:35 +053037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
John Calixtocb87ea22011-04-26 18:56:29 -040039#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020041#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010042#include <linux/mmc/mmc.h>
43#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040050#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010054
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050055#define INAND_CMD38_ARG_EXT_CSD 113
56#define INAND_CMD38_ARG_ERASE 0x00
57#define INAND_CMD38_ARG_TRIM 0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani914fa652013-01-07 17:31:43 +053061#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050062
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030063#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
64
Seungwon Jeon53f8f572012-09-27 15:00:26 +020065#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68#define PACKED_CMD_VER 0x01
69#define PACKED_CMD_WR 0x02
Lee Susman2e3c0642013-04-23 17:59:26 +030070#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020071#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
72 do { \
73 if (stats->enabled) \
74 stats->pack_stop_reason[reason]++; \
75 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020076
Lee Susman2e3c0642013-04-23 17:59:26 +030077#define PCKD_TRGR_INIT_MEAN_POTEN 17
78#define PCKD_TRGR_POTEN_LOWER_BOUND 5
79#define PCKD_TRGR_URGENT_PENALTY 2
80#define PCKD_TRGR_LOWER_BOUND 5
81#define PCKD_TRGR_PRECISION_MULTIPLIER 100
82
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020083static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084
85/*
86 * The defaults come from config options but can be overriden by module
87 * or bootarg options.
88 */
89static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
90
91/*
92 * We've only got one major, so number of mmcblk devices is
93 * limited to 256 / number of minors per device.
94 */
95static int max_devices;
96
97/* 256 minors, so at most 256 separate devices */
98static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050099static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * There is one mmc_blk_data per slot.
103 */
104struct mmc_blk_data {
105 spinlock_t lock;
106 struct gendisk *disk;
107 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500110 unsigned int flags;
111#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000115 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500117 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300118 unsigned int reset_done;
119#define MMC_BLK_READ BIT(0)
120#define MMC_BLK_WRITE BIT(1)
121#define MMC_BLK_DISCARD BIT(2)
122#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500123
124 /*
125 * Only set in main mmc_blk_data associated
126 * with mmc_card with mmc_set_drvdata, and keeps
127 * track of the current selected device partition.
128 */
129 unsigned int part_curr;
130 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100131 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200132 struct device_attribute num_wr_reqs_to_start_packing;
Maya Ereze1eae982013-01-05 21:51:06 +0200133 struct device_attribute bkops_check_threshold;
Maya Erez64d12462013-01-06 10:19:38 +0200134 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100135 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Arjan van de Vena621aae2006-01-12 18:43:35 +0000138static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200140enum {
141 MMC_PACKED_N_IDX = -1,
142 MMC_PACKED_N_ZERO,
143 MMC_PACKED_N_SINGLE,
144};
145
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400146module_param(perdev_minors, int, 0444);
147MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
148
Loic Pallardy961e37a2012-08-06 15:12:31 +0000149static inline int mmc_blk_part_switch(struct mmc_card *card,
150 struct mmc_blk_data *md);
151static int get_card_status(struct mmc_card *card, u32 *status, int retries);
152
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200153static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
154{
155 mqrq->packed_cmd = MMC_PACKED_NONE;
156 mqrq->packed_num = MMC_PACKED_N_ZERO;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
160{
161 struct mmc_blk_data *md;
162
Arjan van de Vena621aae2006-01-12 18:43:35 +0000163 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 md = disk->private_data;
165 if (md && md->usage == 0)
166 md = NULL;
167 if (md)
168 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000169 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return md;
172}
173
Andrei Warkentin371a6892011-04-11 18:10:25 -0500174static inline int mmc_get_devidx(struct gendisk *disk)
175{
Colin Cross71b54d42010-09-03 12:41:21 -0700176 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500177 return devidx;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void mmc_blk_put(struct mmc_blk_data *md)
181{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000182 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 md->usage--;
184 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500185 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800186 blk_cleanup_queue(md->queue.queue);
187
David Woodhouse1dff3142007-11-21 18:45:12 +0100188 __clear_bit(devidx, dev_use);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(md);
192 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000193 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Johan Rudholmadd710e2011-12-02 08:51:06 +0100196static ssize_t power_ro_lock_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
199 int ret;
200 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
201 struct mmc_card *card = md->queue.card;
202 int locked = 0;
203
204 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
205 locked = 2;
206 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
207 locked = 1;
208
209 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
210
211 return ret;
212}
213
214static ssize_t power_ro_lock_store(struct device *dev,
215 struct device_attribute *attr, const char *buf, size_t count)
216{
217 int ret;
218 struct mmc_blk_data *md, *part_md;
219 struct mmc_card *card;
220 unsigned long set;
221
222 if (kstrtoul(buf, 0, &set))
223 return -EINVAL;
224
225 if (set != 1)
226 return count;
227
228 md = mmc_blk_get(dev_to_disk(dev));
229 card = md->queue.card;
230
Sahitya Tummala22459132013-05-01 11:14:51 +0530231 mmc_rpm_hold(card->host, &card->dev);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100232 mmc_claim_host(card->host);
233
234 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
235 card->ext_csd.boot_ro_lock |
236 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
237 card->ext_csd.part_time);
238 if (ret)
239 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
240 else
241 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
242
243 mmc_release_host(card->host);
Sahitya Tummala22459132013-05-01 11:14:51 +0530244 mmc_rpm_release(card->host, &card->dev);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100245
246 if (!ret) {
247 pr_info("%s: Locking boot partition ro until next power on\n",
248 md->disk->disk_name);
249 set_disk_ro(md->disk, 1);
250
251 list_for_each_entry(part_md, &md->part, part)
252 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
253 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
254 set_disk_ro(part_md->disk, 1);
255 }
256 }
257
258 mmc_blk_put(md);
259 return count;
260}
261
Andrei Warkentin371a6892011-04-11 18:10:25 -0500262static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
263 char *buf)
264{
265 int ret;
266 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
267
268 ret = snprintf(buf, PAGE_SIZE, "%d",
269 get_disk_ro(dev_to_disk(dev)) ^
270 md->read_only);
271 mmc_blk_put(md);
272 return ret;
273}
274
275static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
277{
278 int ret;
279 char *end;
280 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
281 unsigned long set = simple_strtoul(buf, &end, 0);
282 if (end == buf) {
283 ret = -EINVAL;
284 goto out;
285 }
286
287 set_disk_ro(dev_to_disk(dev), set || md->read_only);
288 ret = count;
289out:
290 mmc_blk_put(md);
291 return ret;
292}
293
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200294static ssize_t
295num_wr_reqs_to_start_packing_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
299 int num_wr_reqs_to_start_packing;
300 int ret;
301
302 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
303
304 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
305
306 mmc_blk_put(md);
307 return ret;
308}
309
310static ssize_t
311num_wr_reqs_to_start_packing_store(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf, size_t count)
314{
315 int value;
316 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200317 struct mmc_card *card = md->queue.card;
318 int ret = count;
319
320 if (!card) {
321 ret = -EINVAL;
322 goto exit;
323 }
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200324
325 sscanf(buf, "%d", &value);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200326
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200327 if (value >= 0) {
328 md->queue.num_wr_reqs_to_start_packing =
329 min_t(int, value, (int)card->ext_csd.max_packed_writes);
330
331 pr_debug("%s: trigger to pack: new value = %d",
332 mmc_hostname(card->host),
333 md->queue.num_wr_reqs_to_start_packing);
334 } else {
335 pr_err("%s: value %d is not valid. old value remains = %d",
336 mmc_hostname(card->host), value,
337 md->queue.num_wr_reqs_to_start_packing);
338 ret = -EINVAL;
339 }
340
341exit:
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200342 mmc_blk_put(md);
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200343 return ret;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200344}
345
Maya Erez2d33a192013-01-04 15:52:41 +0200346static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200347bkops_check_threshold_show(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200348 struct device_attribute *attr, char *buf)
349{
350 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Maya Erez2d33a192013-01-04 15:52:41 +0200351 struct mmc_card *card = md->queue.card;
352 int ret;
353
354 if (!card)
355 ret = -EINVAL;
Maya Ereze1eae982013-01-05 21:51:06 +0200356 else
Maya Erez2d33a192013-01-04 15:52:41 +0200357 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Maya Ereze1eae982013-01-05 21:51:06 +0200358 card->bkops_info.size_percentage_to_queue_delayed_work);
Maya Erez2d33a192013-01-04 15:52:41 +0200359
360 mmc_blk_put(md);
361 return ret;
362}
363
364static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200365bkops_check_threshold_store(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200366 struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 int value;
370 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
371 struct mmc_card *card = md->queue.card;
Maya Ereze1eae982013-01-05 21:51:06 +0200372 unsigned int card_size;
373 int ret = count;
Maya Erez2d33a192013-01-04 15:52:41 +0200374
Maya Ereze1eae982013-01-05 21:51:06 +0200375 if (!card) {
376 ret = -EINVAL;
377 goto exit;
378 }
Maya Erez2d33a192013-01-04 15:52:41 +0200379
380 sscanf(buf, "%d", &value);
Maya Ereze1eae982013-01-05 21:51:06 +0200381 if ((value <= 0) || (value >= 100)) {
382 ret = -EINVAL;
383 goto exit;
384 }
Maya Erez2d33a192013-01-04 15:52:41 +0200385
Maya Ereze1eae982013-01-05 21:51:06 +0200386 card_size = (unsigned int)get_capacity(md->disk);
387 if (card_size <= 0) {
388 ret = -EINVAL;
389 goto exit;
390 }
391 card->bkops_info.size_percentage_to_queue_delayed_work = value;
392 card->bkops_info.min_sectors_to_queue_delayed_work =
393 (card_size * value) / 100;
394
395 pr_debug("%s: size_percentage = %d, min_sectors = %d",
396 mmc_hostname(card->host),
397 card->bkops_info.size_percentage_to_queue_delayed_work,
398 card->bkops_info.min_sectors_to_queue_delayed_work);
399
400exit:
Maya Erez2d33a192013-01-04 15:52:41 +0200401 mmc_blk_put(md);
402 return count;
403}
404
Maya Erez64d12462013-01-06 10:19:38 +0200405static ssize_t
406no_pack_for_random_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
410 int ret;
411
412 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
413
414 mmc_blk_put(md);
415 return ret;
416}
417
418static ssize_t
419no_pack_for_random_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
422{
423 int value;
424 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
425 struct mmc_card *card = md->queue.card;
426 int ret = count;
427
428 if (!card) {
429 ret = -EINVAL;
430 goto exit;
431 }
432
433 sscanf(buf, "%d", &value);
434
435 if (value < 0) {
436 pr_err("%s: value %d is not valid. old value remains = %d",
437 mmc_hostname(card->host), value,
438 md->queue.no_pack_for_random);
439 ret = -EINVAL;
440 goto exit;
441 }
442
443 md->queue.no_pack_for_random = (value > 0) ? true : false;
444
445 pr_debug("%s: no_pack_for_random: new value = %d",
446 mmc_hostname(card->host),
447 md->queue.no_pack_for_random);
448
449exit:
450 mmc_blk_put(md);
451 return ret;
452}
453
Al Viroa5a15612008-03-02 10:33:30 -0500454static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Al Viroa5a15612008-03-02 10:33:30 -0500456 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int ret = -ENXIO;
458
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200459 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (md) {
461 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500462 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700464
Al Viroa5a15612008-03-02 10:33:30 -0500465 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700466 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700467 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200470 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return ret;
473}
474
Al Viroa5a15612008-03-02 10:33:30 -0500475static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Al Viroa5a15612008-03-02 10:33:30 -0500477 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200479 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200481 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
485static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800486mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800488 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
489 geo->heads = 4;
490 geo->sectors = 16;
491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
John Calixtocb87ea22011-04-26 18:56:29 -0400494struct mmc_blk_ioc_data {
495 struct mmc_ioc_cmd ic;
496 unsigned char *buf;
497 u64 buf_bytes;
498};
499
500static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
501 struct mmc_ioc_cmd __user *user)
502{
503 struct mmc_blk_ioc_data *idata;
504 int err;
505
506 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
507 if (!idata) {
508 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400509 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400510 }
511
512 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
513 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400514 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400515 }
516
517 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
518 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
519 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400520 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400521 }
522
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100523 if (!idata->buf_bytes)
524 return idata;
525
John Calixtocb87ea22011-04-26 18:56:29 -0400526 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
527 if (!idata->buf) {
528 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400529 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400530 }
531
532 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
533 idata->ic.data_ptr, idata->buf_bytes)) {
534 err = -EFAULT;
535 goto copy_err;
536 }
537
538 return idata;
539
540copy_err:
541 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400542idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400543 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400544out:
John Calixtocb87ea22011-04-26 18:56:29 -0400545 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400546}
547
Loic Pallardy961e37a2012-08-06 15:12:31 +0000548static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
549 u32 retries_max)
550{
551 int err;
552 u32 retry_count = 0;
553
554 if (!status || !retries_max)
555 return -EINVAL;
556
557 do {
558 err = get_card_status(card, status, 5);
559 if (err)
560 break;
561
562 if (!R1_STATUS(*status) &&
563 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
564 break; /* RPMB programming operation complete */
565
566 /*
567 * Rechedule to give the MMC device a chance to continue
568 * processing the previous command without being polled too
569 * frequently.
570 */
571 usleep_range(1000, 5000);
572 } while (++retry_count < retries_max);
573
574 if (retry_count == retries_max)
575 err = -EPERM;
576
577 return err;
578}
579
John Calixtocb87ea22011-04-26 18:56:29 -0400580static int mmc_blk_ioctl_cmd(struct block_device *bdev,
581 struct mmc_ioc_cmd __user *ic_ptr)
582{
583 struct mmc_blk_ioc_data *idata;
584 struct mmc_blk_data *md;
585 struct mmc_card *card;
586 struct mmc_command cmd = {0};
587 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530588 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400589 struct scatterlist sg;
590 int err;
591
592 /*
593 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
594 * whole block device, not on a partition. This prevents overspray
595 * between sibling partitions.
596 */
597 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
598 return -EPERM;
599
600 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
601 if (IS_ERR(idata))
602 return PTR_ERR(idata);
603
John Calixtocb87ea22011-04-26 18:56:29 -0400604 md = mmc_blk_get(bdev->bd_disk);
605 if (!md) {
606 err = -EINVAL;
Pratibhasagar V386c1b72013-04-26 18:59:55 +0530607 goto blk_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400608 }
609
610 card = md->queue.card;
611 if (IS_ERR(card)) {
612 err = PTR_ERR(card);
613 goto cmd_done;
614 }
615
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100616 cmd.opcode = idata->ic.opcode;
617 cmd.arg = idata->ic.arg;
618 cmd.flags = idata->ic.flags;
619
620 if (idata->buf_bytes) {
621 data.sg = &sg;
622 data.sg_len = 1;
623 data.blksz = idata->ic.blksz;
624 data.blocks = idata->ic.blocks;
625
626 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
627
628 if (idata->ic.write_flag)
629 data.flags = MMC_DATA_WRITE;
630 else
631 data.flags = MMC_DATA_READ;
632
633 /* data.flags must already be set before doing this. */
634 mmc_set_data_timeout(&data, card);
635
636 /* Allow overriding the timeout_ns for empirical tuning. */
637 if (idata->ic.data_timeout_ns)
638 data.timeout_ns = idata->ic.data_timeout_ns;
639
640 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
641 /*
642 * Pretend this is a data transfer and rely on the
643 * host driver to compute timeout. When all host
644 * drivers support cmd.cmd_timeout for R1B, this
645 * can be changed to:
646 *
647 * mrq.data = NULL;
648 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
649 */
650 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
651 }
652
653 mrq.data = &data;
654 }
655
656 mrq.cmd = &cmd;
657
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530658 mmc_rpm_hold(card->host, &card->dev);
John Calixtocb87ea22011-04-26 18:56:29 -0400659 mmc_claim_host(card->host);
660
Loic Pallardy961e37a2012-08-06 15:12:31 +0000661 err = mmc_blk_part_switch(card, md);
662 if (err)
663 goto cmd_rel_host;
664
John Calixtocb87ea22011-04-26 18:56:29 -0400665 if (idata->ic.is_acmd) {
666 err = mmc_app_cmd(card->host, card);
667 if (err)
668 goto cmd_rel_host;
669 }
670
John Calixtocb87ea22011-04-26 18:56:29 -0400671 mmc_wait_for_req(card->host, &mrq);
672
673 if (cmd.error) {
674 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
675 __func__, cmd.error);
676 err = cmd.error;
677 goto cmd_rel_host;
678 }
679 if (data.error) {
680 dev_err(mmc_dev(card->host), "%s: data error %d\n",
681 __func__, data.error);
682 err = data.error;
683 goto cmd_rel_host;
684 }
685
686 /*
687 * According to the SD specs, some commands require a delay after
688 * issuing the command.
689 */
690 if (idata->ic.postsleep_min_us)
691 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
692
693 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
694 err = -EFAULT;
695 goto cmd_rel_host;
696 }
697
698 if (!idata->ic.write_flag) {
699 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
700 idata->buf, idata->buf_bytes)) {
701 err = -EFAULT;
702 goto cmd_rel_host;
703 }
704 }
705
Krishna Kondabfa7df92012-11-20 20:26:05 -0800706cmd_rel_host:
707 mmc_release_host(card->host);
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530708 mmc_rpm_release(card->host, &card->dev);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800709
710cmd_done:
711 mmc_blk_put(md);
Pratibhasagar V386c1b72013-04-26 18:59:55 +0530712blk_err:
Krishna Kondabfa7df92012-11-20 20:26:05 -0800713 kfree(idata->buf);
714 kfree(idata);
715 return err;
716}
717
718struct mmc_blk_ioc_rpmb_data {
719 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
720};
721
722static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
723 struct mmc_ioc_rpmb __user *user)
724{
725 struct mmc_blk_ioc_rpmb_data *idata;
726 int err, i;
727
728 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
729 if (!idata) {
730 err = -ENOMEM;
731 goto out;
732 }
733
734 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
735 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
736 if (IS_ERR(idata->data[i])) {
737 err = PTR_ERR(idata->data[i]);
738 goto copy_err;
739 }
740 }
741
742 return idata;
743
744copy_err:
745 while (--i >= 0) {
746 kfree(idata->data[i]->buf);
747 kfree(idata->data[i]);
748 }
749 kfree(idata);
750out:
751 return ERR_PTR(err);
752}
753
754static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
755 struct mmc_ioc_rpmb __user *ic_ptr)
756{
757 struct mmc_blk_ioc_rpmb_data *idata;
758 struct mmc_blk_data *md;
759 struct mmc_card *card;
760 struct mmc_command cmd = {0};
761 struct mmc_data data = {0};
762 struct mmc_request mrq = {NULL};
763 struct scatterlist sg;
764 int err = 0, i = 0;
765 u32 status = 0;
766
767 /* The caller must have CAP_SYS_RAWIO */
768 if (!capable(CAP_SYS_RAWIO))
769 return -EPERM;
770
771 md = mmc_blk_get(bdev->bd_disk);
772 /* make sure this is a rpmb partition */
773 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
774 err = -EINVAL;
775 goto cmd_done;
776 }
777
778 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
779 if (IS_ERR(idata)) {
780 err = PTR_ERR(idata);
781 goto cmd_done;
782 }
783
784 card = md->queue.card;
785 if (IS_ERR(card)) {
786 err = PTR_ERR(card);
787 goto idata_free;
788 }
789
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530790 mmc_rpm_hold(card->host, &card->dev);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800791 mmc_claim_host(card->host);
792
793 err = mmc_blk_part_switch(card, md);
794 if (err)
795 goto cmd_rel_host;
796
797 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
798 struct mmc_blk_ioc_data *curr_data;
799 struct mmc_ioc_cmd *curr_cmd;
800
801 curr_data = idata->data[i];
802 curr_cmd = &curr_data->ic;
803 if (!curr_cmd->opcode)
804 break;
805
806 cmd.opcode = curr_cmd->opcode;
807 cmd.arg = curr_cmd->arg;
808 cmd.flags = curr_cmd->flags;
809
810 if (curr_data->buf_bytes) {
811 data.sg = &sg;
812 data.sg_len = 1;
813 data.blksz = curr_cmd->blksz;
814 data.blocks = curr_cmd->blocks;
815
816 sg_init_one(data.sg, curr_data->buf,
817 curr_data->buf_bytes);
818
819 if (curr_cmd->write_flag)
820 data.flags = MMC_DATA_WRITE;
821 else
822 data.flags = MMC_DATA_READ;
823
824 /* data.flags must already be set before doing this. */
825 mmc_set_data_timeout(&data, card);
826
827 /*
828 * Allow overriding the timeout_ns for empirical tuning.
829 */
830 if (curr_cmd->data_timeout_ns)
831 data.timeout_ns = curr_cmd->data_timeout_ns;
832
833 mrq.data = &data;
834 }
835
836 mrq.cmd = &cmd;
837
838 err = mmc_set_blockcount(card, data.blocks,
839 curr_cmd->write_flag & (1 << 31));
840 if (err)
841 goto cmd_rel_host;
842
843 mmc_wait_for_req(card->host, &mrq);
844
845 if (cmd.error) {
846 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
847 __func__, cmd.error);
848 err = cmd.error;
849 goto cmd_rel_host;
850 }
851 if (data.error) {
852 dev_err(mmc_dev(card->host), "%s: data error %d\n",
853 __func__, data.error);
854 err = data.error;
855 goto cmd_rel_host;
856 }
857
858 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
859 sizeof(cmd.resp))) {
860 err = -EFAULT;
861 goto cmd_rel_host;
862 }
863
864 if (!curr_cmd->write_flag) {
865 if (copy_to_user((void __user *)(unsigned long)
866 curr_cmd->data_ptr,
867 curr_data->buf,
868 curr_data->buf_bytes)) {
869 err = -EFAULT;
870 goto cmd_rel_host;
871 }
872 }
873
Loic Pallardy961e37a2012-08-06 15:12:31 +0000874 /*
875 * Ensure RPMB command has completed by polling CMD13
876 * "Send Status".
877 */
878 err = ioctl_rpmb_card_status_poll(card, &status, 5);
879 if (err)
880 dev_err(mmc_dev(card->host),
881 "%s: Card Status=0x%08X, error %d\n",
882 __func__, status, err);
883 }
884
John Calixtocb87ea22011-04-26 18:56:29 -0400885cmd_rel_host:
886 mmc_release_host(card->host);
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530887 mmc_rpm_release(card->host, &card->dev);
John Calixtocb87ea22011-04-26 18:56:29 -0400888
Krishna Kondabfa7df92012-11-20 20:26:05 -0800889idata_free:
890 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
891 kfree(idata->data[i]->buf);
892 kfree(idata->data[i]);
893 }
894 kfree(idata);
895
John Calixtocb87ea22011-04-26 18:56:29 -0400896cmd_done:
897 mmc_blk_put(md);
John Calixtocb87ea22011-04-26 18:56:29 -0400898 return err;
899}
900
901static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
902 unsigned int cmd, unsigned long arg)
903{
904 int ret = -EINVAL;
905 if (cmd == MMC_IOC_CMD)
906 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800907 if (cmd == MMC_IOC_RPMB_CMD)
908 ret = mmc_blk_ioctl_rpmb_cmd(bdev,
909 (struct mmc_ioc_rpmb __user *)arg);
John Calixtocb87ea22011-04-26 18:56:29 -0400910 return ret;
911}
912
913#ifdef CONFIG_COMPAT
914static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
915 unsigned int cmd, unsigned long arg)
916{
917 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
918}
919#endif
920
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700921static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500922 .open = mmc_blk_open,
923 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800924 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400926 .ioctl = mmc_blk_ioctl,
927#ifdef CONFIG_COMPAT
928 .compat_ioctl = mmc_blk_compat_ioctl,
929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
Andrei Warkentin371a6892011-04-11 18:10:25 -0500932static inline int mmc_blk_part_switch(struct mmc_card *card,
933 struct mmc_blk_data *md)
934{
935 int ret;
936 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300937
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800938 if ((main_md->part_curr == md->part_type) &&
939 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -0500940 return 0;
941
942 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300943 u8 part_config = card->ext_csd.part_config;
944
945 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
946 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500947
948 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300949 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500950 card->ext_csd.part_time);
951 if (ret)
952 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300953
954 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800955 card->part_curr = md->part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300956 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500957
958 main_md->part_curr = md->part_type;
959 return 0;
960}
961
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700962static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
963{
964 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100965 u32 result;
966 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700967
Venkatraman Sad5fd972011-08-25 00:30:50 +0530968 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400969 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400970 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700971
972 struct scatterlist sg;
973
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700974 cmd.opcode = MMC_APP_CMD;
975 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700976 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700977
978 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700979 if (err)
980 return (u32)-1;
981 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700982 return (u32)-1;
983
984 memset(&cmd, 0, sizeof(struct mmc_command));
985
986 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
987 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700988 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700989
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700990 data.blksz = 4;
991 data.blocks = 1;
992 data.flags = MMC_DATA_READ;
993 data.sg = &sg;
994 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530995 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700996
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700997 mrq.cmd = &cmd;
998 mrq.data = &data;
999
Ben Dooks051913d2009-06-08 23:33:57 +01001000 blocks = kmalloc(4, GFP_KERNEL);
1001 if (!blocks)
1002 return (u32)-1;
1003
1004 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001005
1006 mmc_wait_for_req(card->host, &mrq);
1007
Ben Dooks051913d2009-06-08 23:33:57 +01001008 result = ntohl(*blocks);
1009 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001010
Ben Dooks051913d2009-06-08 23:33:57 +01001011 if (cmd.error || data.error)
1012 result = (u32)-1;
1013
1014 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001015}
1016
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001017static int send_stop(struct mmc_card *card, u32 *status)
1018{
1019 struct mmc_command cmd = {0};
1020 int err;
1021
1022 cmd.opcode = MMC_STOP_TRANSMISSION;
1023 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1024 err = mmc_wait_for_cmd(card->host, &cmd, 5);
1025 if (err == 0)
1026 *status = cmd.resp[0];
1027 return err;
1028}
1029
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001030static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001031{
Chris Ball1278dba2011-04-13 23:40:30 -04001032 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001033 int err;
1034
Adrian Hunter504f1912008-10-16 12:55:25 +03001035 cmd.opcode = MMC_SEND_STATUS;
1036 if (!mmc_host_is_spi(card->host))
1037 cmd.arg = card->rca << 16;
1038 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001039 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1040 if (err == 0)
1041 *status = cmd.resp[0];
1042 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001043}
1044
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301045#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001046#define ERR_RETRY 2
1047#define ERR_ABORT 1
1048#define ERR_CONTINUE 0
1049
1050static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1051 bool status_valid, u32 status)
1052{
1053 switch (error) {
1054 case -EILSEQ:
1055 /* response crc error, retry the r/w cmd */
1056 pr_err("%s: %s sending %s command, card status %#x\n",
1057 req->rq_disk->disk_name, "response CRC error",
1058 name, status);
1059 return ERR_RETRY;
1060
1061 case -ETIMEDOUT:
1062 pr_err("%s: %s sending %s command, card status %#x\n",
1063 req->rq_disk->disk_name, "timed out", name, status);
1064
1065 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001066 if (!status_valid) {
1067 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001068 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001069 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001070 /*
1071 * If it was a r/w cmd crc error, or illegal command
1072 * (eg, issued in wrong state) then retry - we should
1073 * have corrected the state problem above.
1074 */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001075 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1076 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001077 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001078 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001079
1080 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001081 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001082 return ERR_ABORT;
1083
1084 default:
1085 /* We don't understand the error code the driver gave us */
1086 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1087 req->rq_disk->disk_name, error, status);
1088 return ERR_ABORT;
1089 }
1090}
1091
1092/*
1093 * Initial r/w and stop cmd error recovery.
1094 * We don't know whether the card received the r/w cmd or not, so try to
1095 * restore things back to a sane state. Essentially, we do this as follows:
1096 * - Obtain card status. If the first attempt to obtain card status fails,
1097 * the status word will reflect the failed status cmd, not the failed
1098 * r/w cmd. If we fail to obtain card status, it suggests we can no
1099 * longer communicate with the card.
1100 * - Check the card state. If the card received the cmd but there was a
1101 * transient problem with the response, it might still be in a data transfer
1102 * mode. Try to send it a stop command. If this fails, we can't recover.
1103 * - If the r/w cmd failed due to a response CRC error, it was probably
1104 * transient, so retry the cmd.
1105 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1106 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1107 * illegal cmd, retry.
1108 * Otherwise we don't understand what happened, so abort.
1109 */
1110static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +03001111 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001112{
1113 bool prev_cmd_status_valid = true;
1114 u32 status, stop_status = 0;
1115 int err, retry;
1116
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301117 if (mmc_card_removed(card))
1118 return ERR_NOMEDIUM;
1119
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001120 /*
1121 * Try to get card status which indicates both the card state
1122 * and why there was no response. If the first attempt fails,
1123 * we can't be sure the returned status is for the r/w command.
1124 */
1125 for (retry = 2; retry >= 0; retry--) {
1126 err = get_card_status(card, &status, 0);
1127 if (!err)
1128 break;
1129
1130 prev_cmd_status_valid = false;
1131 pr_err("%s: error %d sending status command, %sing\n",
1132 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1133 }
1134
1135 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301136 if (err) {
1137 /* Check if the card is removed */
1138 if (mmc_detect_card_removed(card->host))
1139 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001140 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301141 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001142
Adrian Hunter67716322011-08-29 16:42:15 +03001143 /* Flag ECC errors */
1144 if ((status & R1_CARD_ECC_FAILED) ||
1145 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1146 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1147 *ecc_err = 1;
1148
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001149 /*
1150 * Check the current card state. If it is in some data transfer
1151 * mode, tell it to stop (and hopefully transition back to TRAN.)
1152 */
1153 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1154 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1155 err = send_stop(card, &stop_status);
1156 if (err)
1157 pr_err("%s: error %d sending stop command\n",
1158 req->rq_disk->disk_name, err);
1159
1160 /*
1161 * If the stop cmd also timed out, the card is probably
1162 * not present, so abort. Other errors are bad news too.
1163 */
1164 if (err)
1165 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +03001166 if (stop_status & R1_CARD_ECC_FAILED)
1167 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001168 }
1169
1170 /* Check for set block count errors */
1171 if (brq->sbc.error)
1172 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1173 prev_cmd_status_valid, status);
1174
1175 /* Check for r/w command errors */
1176 if (brq->cmd.error)
1177 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1178 prev_cmd_status_valid, status);
1179
Adrian Hunter67716322011-08-29 16:42:15 +03001180 /* Data errors */
1181 if (!brq->stop.error)
1182 return ERR_CONTINUE;
1183
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001184 /* Now for stop errors. These aren't fatal to the transfer. */
1185 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1186 req->rq_disk->disk_name, brq->stop.error,
1187 brq->cmd.resp[0], status);
1188
1189 /*
1190 * Subsitute in our own stop status as this will give the error
1191 * state which happened during the execution of the r/w command.
1192 */
1193 if (stop_status) {
1194 brq->stop.resp[0] = stop_status;
1195 brq->stop.error = 0;
1196 }
1197 return ERR_CONTINUE;
1198}
1199
Adrian Hunter67716322011-08-29 16:42:15 +03001200static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1201 int type)
1202{
1203 int err;
1204
1205 if (md->reset_done & type)
1206 return -EEXIST;
1207
1208 md->reset_done |= type;
1209 err = mmc_hw_reset(host);
1210 /* Ensure we switch back to the correct partition */
1211 if (err != -EOPNOTSUPP) {
1212 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
1213 int part_err;
1214
1215 main_md->part_curr = main_md->part_type;
1216 part_err = mmc_blk_part_switch(host->card, md);
1217 if (part_err) {
1218 /*
1219 * We have failed to get back into the correct
1220 * partition, so we need to abort the whole request.
1221 */
1222 return -ENODEV;
1223 }
1224 }
1225 return err;
1226}
1227
1228static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1229{
1230 md->reset_done &= ~type;
1231}
1232
Adrian Hunterbd788c92010-08-11 14:17:47 -07001233static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1234{
1235 struct mmc_blk_data *md = mq->data;
1236 struct mmc_card *card = md->queue.card;
1237 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001238 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001239
Adrian Hunterbd788c92010-08-11 14:17:47 -07001240 if (!mmc_can_erase(card)) {
1241 err = -EOPNOTSUPP;
1242 goto out;
1243 }
1244
1245 from = blk_rq_pos(req);
1246 nr = blk_rq_sectors(req);
1247
Maya Erez2d33a192013-01-04 15:52:41 +02001248 if (card->ext_csd.bkops_en)
1249 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1250
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001251 if (mmc_can_discard(card))
1252 arg = MMC_DISCARD_ARG;
1253 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001254 arg = MMC_TRIM_ARG;
1255 else
1256 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001257retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001258 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1259 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1260 INAND_CMD38_ARG_EXT_CSD,
1261 arg == MMC_TRIM_ARG ?
1262 INAND_CMD38_ARG_TRIM :
1263 INAND_CMD38_ARG_ERASE,
1264 0);
1265 if (err)
1266 goto out;
1267 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001268 err = mmc_erase(card, from, nr, arg);
1269out:
Adrian Hunter67716322011-08-29 16:42:15 +03001270 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1271 goto retry;
1272 if (!err)
1273 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301274 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001275
Adrian Hunterbd788c92010-08-11 14:17:47 -07001276 return err ? 0 : 1;
1277}
1278
Adrian Hunter49804542010-08-11 14:17:50 -07001279static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1280 struct request *req)
1281{
1282 struct mmc_blk_data *md = mq->data;
1283 struct mmc_card *card = md->queue.card;
1284 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001285 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001286
Maya Erez463bb952012-05-24 23:46:29 +03001287 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001288 err = -EOPNOTSUPP;
1289 goto out;
1290 }
1291
1292 from = blk_rq_pos(req);
1293 nr = blk_rq_sectors(req);
1294
1295 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1296 arg = MMC_SECURE_TRIM1_ARG;
1297 else
1298 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001299retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001300 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1301 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1302 INAND_CMD38_ARG_EXT_CSD,
1303 arg == MMC_SECURE_TRIM1_ARG ?
1304 INAND_CMD38_ARG_SECTRIM1 :
1305 INAND_CMD38_ARG_SECERASE,
1306 0);
1307 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001308 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001309 }
Adrian Hunter28302812012-04-05 14:45:48 +03001310
Adrian Hunter49804542010-08-11 14:17:50 -07001311 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001312 if (err == -EIO)
1313 goto out_retry;
1314 if (err)
1315 goto out;
1316
1317 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001318 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1319 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1320 INAND_CMD38_ARG_EXT_CSD,
1321 INAND_CMD38_ARG_SECTRIM2,
1322 0);
1323 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001324 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001325 }
Adrian Hunter28302812012-04-05 14:45:48 +03001326
Adrian Hunter49804542010-08-11 14:17:50 -07001327 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001328 if (err == -EIO)
1329 goto out_retry;
1330 if (err)
1331 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001332 }
Adrian Hunter28302812012-04-05 14:45:48 +03001333
Adrian Hunter28302812012-04-05 14:45:48 +03001334out_retry:
1335 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001336 goto retry;
1337 if (!err)
1338 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001339out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301340 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001341
Adrian Hunter49804542010-08-11 14:17:50 -07001342 return err ? 0 : 1;
1343}
1344
Maya Erez463bb952012-05-24 23:46:29 +03001345static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1346 struct request *req)
1347{
1348 struct mmc_blk_data *md = mq->data;
1349 struct mmc_card *card = md->queue.card;
1350 int err = 0;
1351
1352 BUG_ON(!card);
1353 BUG_ON(!card->host);
1354
1355 if (!(mmc_can_sanitize(card) &&
1356 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1357 pr_warning("%s: %s - SANITIZE is not supported\n",
1358 mmc_hostname(card->host), __func__);
1359 err = -EOPNOTSUPP;
1360 goto out;
1361 }
1362
1363 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1364 mmc_hostname(card->host), __func__);
1365
Subhash Jadavanid90d00c2013-06-25 13:13:24 +05301366 err = mmc_switch_ignore_timeout(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001367 EXT_CSD_SANITIZE_START, 1,
1368 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001369
1370 if (err)
1371 pr_err("%s: %s - mmc_switch() with "
1372 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1373 mmc_hostname(card->host), __func__, err);
1374
1375 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1376 __func__);
1377
1378out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301379 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001380
1381 return err ? 0 : 1;
1382}
1383
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001384static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1385{
1386 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001387 struct mmc_card *card = md->queue.card;
1388 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001389
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001390 ret = mmc_flush_cache(card);
Maya Erezd0daec42013-09-28 22:23:37 +03001391 if (ret) {
1392 pr_err("%s: %s: notify flush error to upper layers",
1393 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001394 ret = -EIO;
Maya Erezd0daec42013-09-28 22:23:37 +03001395 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001396
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301397 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001398
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001399 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001400}
1401
1402/*
1403 * Reformat current write as a reliable write, supporting
1404 * both legacy and the enhanced reliable write MMC cards.
1405 * In each transfer we'll handle only as much as a single
1406 * reliable write can handle, thus finish the request in
1407 * partial completions.
1408 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001409static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1410 struct mmc_card *card,
1411 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001412{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001413 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1414 /* Legacy mode imposes restrictions on transfers. */
1415 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1416 brq->data.blocks = 1;
1417
1418 if (brq->data.blocks > card->ext_csd.rel_sectors)
1419 brq->data.blocks = card->ext_csd.rel_sectors;
1420 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1421 brq->data.blocks = 1;
1422 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001423}
1424
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001425#define CMD_ERRORS \
1426 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1427 R1_ADDRESS_ERROR | /* Misaligned address */ \
1428 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1429 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1430 R1_CC_ERROR | /* Card controller error */ \
1431 R1_ERROR) /* General/unknown error */
1432
Per Forlinee8a43a2011-07-01 18:55:33 +02001433static int mmc_blk_err_check(struct mmc_card *card,
1434 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001435{
Per Forlinee8a43a2011-07-01 18:55:33 +02001436 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1437 mmc_active);
1438 struct mmc_blk_request *brq = &mq_mrq->brq;
1439 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001440 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001441
1442 /*
1443 * sbc.error indicates a problem with the set block count
1444 * command. No data will have been transferred.
1445 *
1446 * cmd.error indicates a problem with the r/w command. No
1447 * data will have been transferred.
1448 *
1449 * stop.error indicates a problem with the stop command. Data
1450 * may have been transferred, or may still be transferring.
1451 */
Adrian Hunter67716322011-08-29 16:42:15 +03001452 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1453 brq->data.error) {
1454 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001455 case ERR_RETRY:
1456 return MMC_BLK_RETRY;
1457 case ERR_ABORT:
1458 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301459 case ERR_NOMEDIUM:
1460 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001461 case ERR_CONTINUE:
1462 break;
1463 }
1464 }
1465
1466 /*
1467 * Check for errors relating to the execution of the
1468 * initial command - such as address errors. No data
1469 * has been transferred.
1470 */
1471 if (brq->cmd.resp[0] & CMD_ERRORS) {
1472 pr_err("%s: r/w command failed, status = %#x\n",
1473 req->rq_disk->disk_name, brq->cmd.resp[0]);
1474 return MMC_BLK_ABORT;
1475 }
1476
1477 /*
1478 * Everything else is either success, or a data error of some
1479 * kind. If it was a write, we may have transitioned to
1480 * program mode, which we have to wait for it to complete.
1481 */
1482 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1483 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301484 unsigned long timeout;
1485
1486 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001487 do {
1488 int err = get_card_status(card, &status, 5);
1489 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301490 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001491 req->rq_disk->disk_name, err);
1492 return MMC_BLK_CMD_ERR;
1493 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301494
1495 /* Timeout if the device never becomes ready for data
1496 * and never leaves the program state.
1497 */
1498 if (time_after(jiffies, timeout)) {
1499 pr_err("%s: Card stuck in programming state!"\
1500 " %s %s\n", mmc_hostname(card->host),
1501 req->rq_disk->disk_name, __func__);
1502
1503 return MMC_BLK_CMD_ERR;
1504 }
Per Forlind78d4a82011-07-01 18:55:30 +02001505 /*
1506 * Some cards mishandle the status bits,
1507 * so make sure to check both the busy
1508 * indication and the card state.
1509 */
1510 } while (!(status & R1_READY_FOR_DATA) ||
1511 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1512 }
1513
1514 if (brq->data.error) {
1515 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1516 req->rq_disk->disk_name, brq->data.error,
1517 (unsigned)blk_rq_pos(req),
1518 (unsigned)blk_rq_sectors(req),
1519 brq->cmd.resp[0], brq->stop.resp[0]);
1520
1521 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001522 if (ecc_err)
1523 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001524 return MMC_BLK_DATA_ERR;
1525 } else {
1526 return MMC_BLK_CMD_ERR;
1527 }
1528 }
1529
Adrian Hunter67716322011-08-29 16:42:15 +03001530 if (!brq->data.bytes_xfered)
1531 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001532
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001533 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1534 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1535 return MMC_BLK_PARTIAL;
1536 else
1537 return MMC_BLK_SUCCESS;
1538 }
1539
Adrian Hunter67716322011-08-29 16:42:15 +03001540 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1541 return MMC_BLK_PARTIAL;
1542
1543 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001544}
1545
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001546/*
1547 * mmc_blk_reinsert_req() - re-insert request back to the scheduler
1548 * @areq: request to re-insert.
1549 *
1550 * Request may be packed or single. When fails to reinsert request, it will be
1551 * requeued to the the dispatch queue.
1552 */
1553static void mmc_blk_reinsert_req(struct mmc_async_req *areq)
1554{
1555 struct request *prq;
1556 int ret = 0;
1557 struct mmc_queue_req *mq_rq;
1558 struct request_queue *q;
1559
1560 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1561 q = mq_rq->req->q;
1562 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1563 while (!list_empty(&mq_rq->packed_list)) {
1564 /* return requests in reverse order */
1565 prq = list_entry_rq(mq_rq->packed_list.prev);
1566 list_del_init(&prq->queuelist);
1567 spin_lock_irq(q->queue_lock);
1568 ret = blk_reinsert_request(q, prq);
1569 if (ret) {
1570 blk_requeue_request(q, prq);
1571 spin_unlock_irq(q->queue_lock);
1572 goto reinsert_error;
1573 }
1574 spin_unlock_irq(q->queue_lock);
1575 }
1576 } else {
1577 spin_lock_irq(q->queue_lock);
1578 ret = blk_reinsert_request(q, mq_rq->req);
1579 if (ret)
1580 blk_requeue_request(q, mq_rq->req);
1581 spin_unlock_irq(q->queue_lock);
1582 }
1583 return;
1584
1585reinsert_error:
1586 pr_err("%s: blk_reinsert_request() failed (%d)",
1587 mq_rq->req->rq_disk->disk_name, ret);
1588 /*
1589 * -EIO will be reported for this request and rest of packed_list.
1590 * Urgent request will be proceeded anyway, while upper layer
1591 * responsibility to re-send failed requests
1592 */
1593 while (!list_empty(&mq_rq->packed_list)) {
1594 prq = list_entry_rq(mq_rq->packed_list.next);
1595 list_del_init(&prq->queuelist);
1596 spin_lock_irq(q->queue_lock);
1597 blk_requeue_request(q, prq);
1598 spin_unlock_irq(q->queue_lock);
1599 }
1600}
1601
1602/*
1603 * mmc_blk_update_interrupted_req() - update of the stopped request
1604 * @card: the MMC card associated with the request.
1605 * @areq: interrupted async request.
1606 *
1607 * Get stopped request state from card and update successfully done part of
1608 * the request by setting packed_fail_idx. The packed_fail_idx is index of
1609 * first uncompleted request in packed request list, for non-packed request
1610 * packed_fail_idx remains unchanged.
1611 *
1612 * Returns: MMC_BLK_SUCCESS for success, MMC_BLK_ABORT otherwise
1613 */
1614static int mmc_blk_update_interrupted_req(struct mmc_card *card,
1615 struct mmc_async_req *areq)
1616{
1617 int ret = MMC_BLK_SUCCESS;
1618 u8 *ext_csd;
1619 int correctly_done;
1620 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1621 mmc_active);
1622 struct request *prq;
1623 u8 req_index = 0;
1624
1625 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1626 return MMC_BLK_SUCCESS;
1627
1628 ext_csd = kmalloc(512, GFP_KERNEL);
1629 if (!ext_csd)
1630 return MMC_BLK_ABORT;
1631
1632 /* get correctly programmed sectors number from card */
1633 ret = mmc_send_ext_csd(card, ext_csd);
1634 if (ret) {
1635 pr_err("%s: error %d reading ext_csd\n",
1636 mmc_hostname(card->host), ret);
1637 ret = MMC_BLK_ABORT;
1638 goto exit;
1639 }
1640 correctly_done = card->ext_csd.data_sector_size *
1641 (ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 0] << 0 |
1642 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 1] << 8 |
1643 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 2] << 16 |
1644 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 3] << 24);
1645
Konstantin Dorfmanf126d832013-07-31 18:13:37 +03001646 /*
1647 * skip packed command header (1 sector) included by the counter but not
1648 * actually written to the NAND
1649 */
1650 if (correctly_done >= card->ext_csd.data_sector_size)
1651 correctly_done -= card->ext_csd.data_sector_size;
1652
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001653 list_for_each_entry(prq, &mq_rq->packed_list, queuelist) {
1654 if ((correctly_done - (int)blk_rq_bytes(prq)) < 0) {
1655 /* prq is not successfull */
1656 mq_rq->packed_fail_idx = req_index;
1657 break;
1658 }
1659 correctly_done -= blk_rq_bytes(prq);
1660 req_index++;
1661 }
1662exit:
1663 kfree(ext_csd);
1664 return ret;
1665}
1666
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001667static int mmc_blk_packed_err_check(struct mmc_card *card,
1668 struct mmc_async_req *areq)
1669{
1670 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1671 mmc_active);
1672 struct request *req = mq_rq->req;
1673 int err, check, status;
1674 u8 ext_csd[512];
1675
1676 mq_rq->packed_retries--;
1677 check = mmc_blk_err_check(card, areq);
1678 err = get_card_status(card, &status, 0);
1679 if (err) {
1680 pr_err("%s: error %d sending status command\n",
1681 req->rq_disk->disk_name, err);
1682 return MMC_BLK_ABORT;
1683 }
1684
1685 if (status & R1_EXCEPTION_EVENT) {
1686 err = mmc_send_ext_csd(card, ext_csd);
1687 if (err) {
1688 pr_err("%s: error %d sending ext_csd\n",
1689 req->rq_disk->disk_name, err);
1690 return MMC_BLK_ABORT;
1691 }
1692
1693 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1694 EXT_CSD_PACKED_FAILURE) &&
1695 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1696 EXT_CSD_PACKED_GENERIC_ERROR)) {
1697 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1698 EXT_CSD_PACKED_INDEXED_ERROR) {
1699 mq_rq->packed_fail_idx =
1700 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1701 return MMC_BLK_PARTIAL;
1702 }
1703 }
1704 }
1705
1706 return check;
1707}
1708
Per Forlin54d49d772011-07-01 18:55:29 +02001709static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1710 struct mmc_card *card,
1711 int disable_multi,
1712 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Per Forlin54d49d772011-07-01 18:55:29 +02001714 u32 readcmd, writecmd;
1715 struct mmc_blk_request *brq = &mqrq->brq;
1716 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301718 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001720 /*
1721 * Reliable writes are used to implement Forced Unit Access and
1722 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001723 *
1724 * XXX: this really needs a good explanation of why REQ_META
1725 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001726 */
1727 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1728 (req->cmd_flags & REQ_META)) &&
1729 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001730 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001731
Per Forlin54d49d772011-07-01 18:55:29 +02001732 memset(brq, 0, sizeof(struct mmc_blk_request));
1733 brq->mrq.cmd = &brq->cmd;
1734 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Per Forlin54d49d772011-07-01 18:55:29 +02001736 brq->cmd.arg = blk_rq_pos(req);
1737 if (!mmc_card_blockaddr(card))
1738 brq->cmd.arg <<= 9;
1739 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1740 brq->data.blksz = 512;
1741 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1742 brq->stop.arg = 0;
1743 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1744 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Asutosh Das1a897142012-07-27 18:10:19 +05301746 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001747 /*
1748 * The block layer doesn't support all sector count
1749 * restrictions, so we need to be prepared for too big
1750 * requests.
1751 */
1752 if (brq->data.blocks > card->host->max_blk_count)
1753 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001755 if (brq->data.blocks > 1) {
1756 /*
1757 * After a read error, we redo the request one sector
1758 * at a time in order to accurately determine which
1759 * sectors can be read successfully.
1760 */
1761 if (disable_multi)
1762 brq->data.blocks = 1;
1763
1764 /* Some controllers can't do multiblock reads due to hw bugs */
1765 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1766 rq_data_dir(req) == READ)
1767 brq->data.blocks = 1;
1768 }
Per Forlin54d49d772011-07-01 18:55:29 +02001769
1770 if (brq->data.blocks > 1 || do_rel_wr) {
1771 /* SPI multiblock writes terminate using a special
1772 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001773 */
Per Forlin54d49d772011-07-01 18:55:29 +02001774 if (!mmc_host_is_spi(card->host) ||
1775 rq_data_dir(req) == READ)
1776 brq->mrq.stop = &brq->stop;
1777 readcmd = MMC_READ_MULTIPLE_BLOCK;
1778 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1779 } else {
1780 brq->mrq.stop = NULL;
1781 readcmd = MMC_READ_SINGLE_BLOCK;
1782 writecmd = MMC_WRITE_BLOCK;
1783 }
1784 if (rq_data_dir(req) == READ) {
1785 brq->cmd.opcode = readcmd;
1786 brq->data.flags |= MMC_DATA_READ;
1787 } else {
1788 brq->cmd.opcode = writecmd;
1789 brq->data.flags |= MMC_DATA_WRITE;
1790 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001791
Per Forlin54d49d772011-07-01 18:55:29 +02001792 if (do_rel_wr)
1793 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001794
Per Forlin54d49d772011-07-01 18:55:29 +02001795 /*
Saugata Das42659002011-12-21 13:09:17 +05301796 * Data tag is used only during writing meta data to speed
1797 * up write and any subsequent read of this meta data
1798 */
1799 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1800 (req->cmd_flags & REQ_META) &&
1801 (rq_data_dir(req) == WRITE) &&
1802 ((brq->data.blocks * brq->data.blksz) >=
1803 card->ext_csd.data_tag_unit_size);
1804
1805 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001806 * Pre-defined multi-block transfers are preferable to
1807 * open ended-ones (and necessary for reliable writes).
1808 * However, it is not sufficient to just send CMD23,
1809 * and avoid the final CMD12, as on an error condition
1810 * CMD12 (stop) needs to be sent anyway. This, coupled
1811 * with Auto-CMD23 enhancements provided by some
1812 * hosts, means that the complexity of dealing
1813 * with this is best left to the host. If CMD23 is
1814 * supported by card and host, we'll fill sbc in and let
1815 * the host deal with handling it correctly. This means
1816 * that for hosts that don't expose MMC_CAP_CMD23, no
1817 * change of behavior will be observed.
1818 *
1819 * N.B: Some MMC cards experience perf degradation.
1820 * We'll avoid using CMD23-bounded multiblock writes for
1821 * these, while retaining features like reliable writes.
1822 */
Saugata Das42659002011-12-21 13:09:17 +05301823 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1824 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1825 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001826 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1827 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301828 (do_rel_wr ? (1 << 31) : 0) |
1829 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001830 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1831 brq->mrq.sbc = &brq->sbc;
1832 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001833
Per Forlin54d49d772011-07-01 18:55:29 +02001834 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001835
Per Forlin54d49d772011-07-01 18:55:29 +02001836 brq->data.sg = mqrq->sg;
1837 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001838
Per Forlin54d49d772011-07-01 18:55:29 +02001839 /*
1840 * Adjust the sg list so it is the same size as the
1841 * request.
1842 */
1843 if (brq->data.blocks != blk_rq_sectors(req)) {
1844 int i, data_size = brq->data.blocks << 9;
1845 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001846
Per Forlin54d49d772011-07-01 18:55:29 +02001847 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1848 data_size -= sg->length;
1849 if (data_size <= 0) {
1850 sg->length += data_size;
1851 i++;
1852 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001853 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001854 }
Per Forlin54d49d772011-07-01 18:55:29 +02001855 brq->data.sg_len = i;
1856 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001857
Per Forlinee8a43a2011-07-01 18:55:33 +02001858 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02001859 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001860 if (mq->err_check_fn)
1861 mqrq->mmc_active.err_check = mq->err_check_fn;
1862 else
1863 mqrq->mmc_active.err_check = mmc_blk_err_check;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001864 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
1865 mqrq->mmc_active.update_interrupted_req =
1866 mmc_blk_update_interrupted_req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001867
Per Forlin54d49d772011-07-01 18:55:29 +02001868 mmc_queue_bounce_pre(mqrq);
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001871/**
1872 * mmc_blk_disable_wr_packing() - disables packing mode
1873 * @mq: MMC queue.
1874 *
1875 */
1876void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
1877{
1878 if (mq) {
1879 mq->wr_packing_enabled = false;
1880 mq->num_of_potential_packed_wr_reqs = 0;
1881 }
1882}
1883EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
1884
Lee Susman2e3c0642013-04-23 17:59:26 +03001885static int get_packed_trigger(int potential, struct mmc_card *card,
1886 struct request *req, int curr_trigger)
1887{
1888 static int num_mean_elements = 1;
1889 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1890 unsigned int trigger = curr_trigger;
1891 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
1892
1893 /* scale down the upper bound to 75% */
1894 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
1895
1896 /*
1897 * since the most common calls for this function are with small
1898 * potential write values and since we don't want these calls to affect
1899 * the packed trigger, set a lower bound and ignore calls with
1900 * potential lower than that bound
1901 */
1902 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
1903 return trigger;
1904
1905 /*
1906 * this is to prevent integer overflow in the following calculation:
1907 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
1908 */
1909 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
1910 num_mean_elements = 1;
1911 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1912 }
1913
1914 /*
1915 * get next mean value based on previous mean value and current
1916 * potential packed writes. Calculation is as follows:
1917 * mean_pot[i+1] =
1918 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
1919 */
1920 mean_potential *= num_mean_elements;
1921 /*
1922 * add num_mean_elements so that the division of two integers doesn't
1923 * lower mean_potential too much
1924 */
1925 if (potential > mean_potential)
1926 mean_potential += num_mean_elements;
1927 mean_potential += potential;
1928 /* this is for gaining more precision when dividing two integers */
1929 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
1930 /* this completes the mean calculation */
1931 mean_potential /= ++num_mean_elements;
1932 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
1933
1934 /*
1935 * if current potential packed writes is greater than the mean potential
1936 * then the heuristic is that the following workload will contain many
1937 * write requests, therefore we lower the packed trigger. In the
1938 * opposite case we want to increase the trigger in order to get less
1939 * packing events.
1940 */
1941 if (potential >= mean_potential)
1942 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
1943 PCKD_TRGR_LOWER_BOUND : trigger - 1;
1944 else
1945 trigger = (trigger >= pckd_trgr_upper_bound) ?
1946 pckd_trgr_upper_bound : trigger + 1;
1947
1948 /*
1949 * an urgent read request indicates a packed list being interrupted
1950 * by this read, therefore we aim for less packing, hence the trigger
1951 * gets increased
1952 */
1953 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
1954 trigger += PCKD_TRGR_URGENT_PENALTY;
1955
1956 return trigger;
1957}
1958
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001959static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1960 struct request *req)
1961{
1962 struct mmc_host *host = mq->card->host;
1963 int data_dir;
1964
1965 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1966 return;
1967
Maya Erez6f219eb2012-12-02 13:27:15 +02001968 /* Support for the write packing on eMMC 4.5 or later */
1969 if (mq->card->ext_csd.rev <= 5)
1970 return;
1971
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001972 /*
1973 * In case the packing control is not supported by the host, it should
1974 * not have an effect on the write packing. Therefore we have to enable
1975 * the write packing
1976 */
1977 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1978 mq->wr_packing_enabled = true;
1979 return;
1980 }
1981
1982 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1983 if (mq->num_of_potential_packed_wr_reqs >
1984 mq->num_wr_reqs_to_start_packing)
1985 mq->wr_packing_enabled = true;
Lee Susman2e3c0642013-04-23 17:59:26 +03001986 mq->num_wr_reqs_to_start_packing =
1987 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
1988 mq->card, req,
1989 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001990 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001991 return;
1992 }
1993
1994 data_dir = rq_data_dir(req);
1995
1996 if (data_dir == READ) {
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001997 mmc_blk_disable_wr_packing(mq);
Lee Susman2e3c0642013-04-23 17:59:26 +03001998 mq->num_wr_reqs_to_start_packing =
1999 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2000 mq->card, req,
2001 mq->num_wr_reqs_to_start_packing);
2002 mq->num_of_potential_packed_wr_reqs = 0;
2003 mq->wr_packing_enabled = false;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002004 return;
2005 } else if (data_dir == WRITE) {
2006 mq->num_of_potential_packed_wr_reqs++;
2007 }
2008
2009 if (mq->num_of_potential_packed_wr_reqs >
2010 mq->num_wr_reqs_to_start_packing)
2011 mq->wr_packing_enabled = true;
2012
2013}
2014
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002015struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2016{
2017 if (!card)
2018 return NULL;
2019
2020 return &card->wr_pack_stats;
2021}
2022EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2023
2024void mmc_blk_init_packed_statistics(struct mmc_card *card)
2025{
2026 int max_num_of_packed_reqs = 0;
2027
2028 if (!card || !card->wr_pack_stats.packing_events)
2029 return;
2030
2031 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2032
2033 spin_lock(&card->wr_pack_stats.lock);
2034 memset(card->wr_pack_stats.packing_events, 0,
2035 (max_num_of_packed_reqs + 1) *
2036 sizeof(*card->wr_pack_stats.packing_events));
2037 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2038 sizeof(card->wr_pack_stats.pack_stop_reason));
2039 card->wr_pack_stats.enabled = true;
2040 spin_unlock(&card->wr_pack_stats.lock);
2041}
2042EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2043
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002044static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2045{
2046 struct request_queue *q = mq->queue;
2047 struct mmc_card *card = mq->card;
2048 struct request *cur = req, *next = NULL;
2049 struct mmc_blk_data *md = mq->data;
2050 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2051 unsigned int req_sectors = 0, phys_segments = 0;
2052 unsigned int max_blk_count, max_phys_segs;
2053 u8 put_back = 0;
2054 u8 max_packed_rw = 0;
2055 u8 reqs = 0;
Lee Susmand50afb52013-01-09 14:48:47 +02002056 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002057
2058 mmc_blk_clear_packed(mq->mqrq_cur);
2059
2060 if (!(md->flags & MMC_BLK_CMD23) ||
2061 !card->ext_csd.packed_event_en)
2062 goto no_packed;
2063
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002064 if (!mq->wr_packing_enabled)
2065 goto no_packed;
2066
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002067 if ((rq_data_dir(cur) == WRITE) &&
2068 (card->host->caps2 & MMC_CAP2_PACKED_WR))
2069 max_packed_rw = card->ext_csd.max_packed_writes;
2070
2071 if (max_packed_rw == 0)
2072 goto no_packed;
2073
2074 if (mmc_req_rel_wr(cur) &&
2075 (md->flags & MMC_BLK_REL_WR) &&
2076 !en_rel_wr)
2077 goto no_packed;
2078
2079 if (mmc_large_sec(card) &&
2080 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2081 goto no_packed;
2082
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002083 if (cur->cmd_flags & REQ_FUA)
2084 goto no_packed;
2085
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002086 max_blk_count = min(card->host->max_blk_count,
2087 card->host->max_req_size >> 9);
2088 if (unlikely(max_blk_count > 0xffff))
2089 max_blk_count = 0xffff;
2090
2091 max_phys_segs = queue_max_segments(q);
2092 req_sectors += blk_rq_sectors(cur);
2093 phys_segments += cur->nr_phys_segments;
2094
2095 if (rq_data_dir(cur) == WRITE) {
2096 req_sectors++;
2097 phys_segments++;
2098 }
2099
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002100 spin_lock(&stats->lock);
2101
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002102 while (reqs < max_packed_rw - 1) {
2103 spin_lock_irq(q->queue_lock);
2104 next = blk_fetch_request(q);
2105 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002106 if (!next) {
2107 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002108 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002109 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002110
2111 if (mmc_large_sec(card) &&
2112 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02002113 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002114 put_back = 1;
2115 break;
2116 }
2117
2118 if (next->cmd_flags & REQ_DISCARD ||
2119 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002120 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002121 put_back = 1;
2122 break;
2123 }
2124
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002125 if (next->cmd_flags & REQ_FUA) {
2126 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2127 put_back = 1;
2128 break;
2129 }
2130
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002131 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002132 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002133 put_back = 1;
2134 break;
2135 }
2136
2137 if (mmc_req_rel_wr(next) &&
2138 (md->flags & MMC_BLK_REL_WR) &&
2139 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002140 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002141 put_back = 1;
2142 break;
2143 }
2144
2145 req_sectors += blk_rq_sectors(next);
2146 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002147 if (stats->enabled)
2148 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002149 put_back = 1;
2150 break;
2151 }
2152
2153 phys_segments += next->nr_phys_segments;
2154 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002155 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002156 put_back = 1;
2157 break;
2158 }
2159
Maya Erez64d12462013-01-06 10:19:38 +02002160 if (mq->no_pack_for_random) {
2161 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2162 blk_rq_pos(next)) {
2163 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2164 put_back = 1;
2165 break;
2166 }
2167 }
2168
Maya Erez2d33a192013-01-04 15:52:41 +02002169 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002170 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02002171 if (card->ext_csd.bkops_en)
2172 card->bkops_info.sectors_changed +=
2173 blk_rq_sectors(next);
2174 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002175 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
2176 cur = next;
2177 reqs++;
2178 }
2179
2180 if (put_back) {
2181 spin_lock_irq(q->queue_lock);
2182 blk_requeue_request(q, next);
2183 spin_unlock_irq(q->queue_lock);
2184 }
2185
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002186 if (stats->enabled) {
2187 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2188 stats->packing_events[reqs + 1]++;
2189 if (reqs + 1 == max_packed_rw)
2190 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2191 }
2192
2193 spin_unlock(&stats->lock);
2194
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002195 if (reqs > 0) {
2196 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
2197 mq->mqrq_cur->packed_num = ++reqs;
2198 mq->mqrq_cur->packed_retries = reqs;
2199 return reqs;
2200 }
2201
2202no_packed:
2203 mmc_blk_clear_packed(mq->mqrq_cur);
2204 return 0;
2205}
2206
2207static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2208 struct mmc_card *card,
2209 struct mmc_queue *mq)
2210{
2211 struct mmc_blk_request *brq = &mqrq->brq;
2212 struct request *req = mqrq->req;
2213 struct request *prq;
2214 struct mmc_blk_data *md = mq->data;
2215 bool do_rel_wr, do_data_tag;
2216 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
2217 u8 i = 1;
2218
2219 mqrq->packed_cmd = MMC_PACKED_WRITE;
2220 mqrq->packed_blocks = 0;
2221 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
2222
2223 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
2224 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
2225 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
2226
2227 /*
2228 * Argument for each entry of packed group
2229 */
2230 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
2231 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2232 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2233 (prq->cmd_flags & REQ_META) &&
2234 (rq_data_dir(prq) == WRITE) &&
2235 ((brq->data.blocks * brq->data.blksz) >=
2236 card->ext_csd.data_tag_unit_size);
2237 /* Argument of CMD23 */
2238 packed_cmd_hdr[(i * 2)] =
2239 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2240 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
2241 blk_rq_sectors(prq);
2242 /* Argument of CMD18 or CMD25 */
2243 packed_cmd_hdr[((i * 2)) + 1] =
2244 mmc_card_blockaddr(card) ?
2245 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
2246 mqrq->packed_blocks += blk_rq_sectors(prq);
2247 i++;
2248 }
2249
2250 memset(brq, 0, sizeof(struct mmc_blk_request));
2251 brq->mrq.cmd = &brq->cmd;
2252 brq->mrq.data = &brq->data;
2253 brq->mrq.sbc = &brq->sbc;
2254 brq->mrq.stop = &brq->stop;
2255
2256 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2257 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
2258 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2259
2260 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2261 brq->cmd.arg = blk_rq_pos(req);
2262 if (!mmc_card_blockaddr(card))
2263 brq->cmd.arg <<= 9;
2264 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2265
2266 brq->data.blksz = 512;
2267 brq->data.blocks = mqrq->packed_blocks + 1;
2268 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02002269 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002270
2271 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2272 brq->stop.arg = 0;
2273 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2274
2275 mmc_set_data_timeout(&brq->data, card);
2276
2277 brq->data.sg = mqrq->sg;
2278 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2279
2280 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002281 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02002282
2283 /*
2284 * This is intended for packed commands tests usage - in case these
2285 * functions are not in use the respective pointers are NULL
2286 */
2287 if (mq->err_check_fn)
2288 mqrq->mmc_active.err_check = mq->err_check_fn;
2289 else
2290 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2291
2292 if (mq->packed_test_fn)
2293 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002294
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002295
2296 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
2297 mqrq->mmc_active.update_interrupted_req =
2298 mmc_blk_update_interrupted_req;
2299
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002300 mmc_queue_bounce_pre(mqrq);
2301}
2302
Adrian Hunter67716322011-08-29 16:42:15 +03002303static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2304 struct mmc_blk_request *brq, struct request *req,
2305 int ret)
2306{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002307 struct mmc_queue_req *mq_rq;
2308 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2309
Adrian Hunter67716322011-08-29 16:42:15 +03002310 /*
2311 * If this is an SD card and we're writing, we can first
2312 * mark the known good sectors as ok.
2313 *
2314 * If the card is not SD, we can still ok written sectors
2315 * as reported by the controller (which might be less than
2316 * the real number of written sectors, but never more).
2317 */
2318 if (mmc_card_sd(card)) {
2319 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05302320 if (!brq->data.fault_injected) {
2321 blocks = mmc_sd_num_wr_blocks(card);
2322 if (blocks != (u32)-1)
2323 ret = blk_end_request(req, 0, blocks << 9);
2324 } else
2325 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002326 } else {
2327 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
2328 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
2329 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03002330 return ret;
2331}
2332
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002333static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2334{
2335 struct request *prq;
2336 int idx = mq_rq->packed_fail_idx, i = 0;
2337 int ret = 0;
2338
2339 while (!list_empty(&mq_rq->packed_list)) {
2340 prq = list_entry_rq(mq_rq->packed_list.next);
2341 if (idx == i) {
2342 /* retry from error index */
2343 mq_rq->packed_num -= idx;
2344 mq_rq->req = prq;
2345 ret = 1;
2346
2347 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
2348 list_del_init(&prq->queuelist);
2349 mmc_blk_clear_packed(mq_rq);
2350 }
2351 return ret;
2352 }
2353 list_del_init(&prq->queuelist);
2354 blk_end_request(prq, 0, blk_rq_bytes(prq));
2355 i++;
2356 }
2357
2358 mmc_blk_clear_packed(mq_rq);
2359 return ret;
2360}
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002361static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq,
2362 unsigned int cmd_flags)
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002363{
2364 struct request *prq;
2365
2366 while (!list_empty(&mq_rq->packed_list)) {
2367 prq = list_entry_rq(mq_rq->packed_list.next);
2368 list_del_init(&prq->queuelist);
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002369 prq->cmd_flags |= cmd_flags;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002370 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2371 }
2372
2373 mmc_blk_clear_packed(mq_rq);
2374}
2375
2376static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2377 struct mmc_queue_req *mq_rq)
2378{
2379 struct request *prq;
2380 struct request_queue *q = mq->queue;
2381
2382 while (!list_empty(&mq_rq->packed_list)) {
2383 prq = list_entry_rq(mq_rq->packed_list.prev);
2384 if (prq->queuelist.prev != &mq_rq->packed_list) {
2385 list_del_init(&prq->queuelist);
2386 spin_lock_irq(q->queue_lock);
2387 blk_requeue_request(mq->queue, prq);
2388 spin_unlock_irq(q->queue_lock);
2389 } else {
2390 list_del_init(&prq->queuelist);
2391 }
2392 }
2393
2394 mmc_blk_clear_packed(mq_rq);
2395}
2396
Per Forlinee8a43a2011-07-01 18:55:33 +02002397static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02002398{
2399 struct mmc_blk_data *md = mq->data;
2400 struct mmc_card *card = md->queue.card;
2401 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03002402 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02002403 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002404 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02002405 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002406 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002407 const u8 packed_num = 2;
2408 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02002409
2410 if (!rqc && !mq->mqrq_prev->req)
2411 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02002412
Maya Erez5f360692012-10-10 03:47:54 +02002413 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02002414 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
2415 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002416 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02002417 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002418
Per Forlin54d49d772011-07-01 18:55:29 +02002419 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002420 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002421 if (reqs >= packed_num)
2422 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2423 card, mq);
2424 else
2425 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002426 areq = &mq->mqrq_cur->mmc_active;
2427 } else
2428 areq = NULL;
2429 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002430 if (!areq) {
Lee Susmand50afb52013-01-09 14:48:47 +02002431 if (status == MMC_BLK_NEW_REQUEST)
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002432 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002433 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002434 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002435
Per Forlinee8a43a2011-07-01 18:55:33 +02002436 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2437 brq = &mq_rq->brq;
2438 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002439 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002440 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002441
Per Forlind78d4a82011-07-01 18:55:30 +02002442 switch (status) {
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002443 case MMC_BLK_URGENT:
2444 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2445 /* complete successfully transmitted part */
2446 if (mmc_blk_end_packed_req(mq_rq))
2447 /* process for not transmitted part */
2448 mmc_blk_reinsert_req(areq);
2449 } else {
2450 mmc_blk_reinsert_req(areq);
2451 }
2452
2453 mq->flags |= MMC_QUEUE_URGENT_REQUEST;
2454 ret = 0;
2455 break;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002456 case MMC_BLK_URGENT_DONE:
Per Forlind78d4a82011-07-01 18:55:30 +02002457 case MMC_BLK_SUCCESS:
2458 case MMC_BLK_PARTIAL:
2459 /*
2460 * A block was successfully transferred.
2461 */
Adrian Hunter67716322011-08-29 16:42:15 +03002462 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002463
2464 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2465 ret = mmc_blk_end_packed_req(mq_rq);
2466 break;
2467 } else {
2468 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002469 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002470 }
2471
Adrian Hunter67716322011-08-29 16:42:15 +03002472 /*
2473 * If the blk_end_request function returns non-zero even
2474 * though all data has been transferred and no errors
2475 * were returned by the host controller, it's a bug.
2476 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002477 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302478 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002479 __func__, blk_rq_bytes(req),
2480 brq->data.bytes_xfered);
2481 rqc = NULL;
2482 goto cmd_abort;
2483 }
Per Forlind78d4a82011-07-01 18:55:30 +02002484 break;
2485 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002486 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2487 if (!mmc_blk_reset(md, card->host, type))
2488 break;
2489 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002490 case MMC_BLK_RETRY:
2491 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002492 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002493 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002494 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002495 if (!mmc_blk_reset(md, card->host, type))
2496 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002497 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002498 case MMC_BLK_DATA_ERR: {
2499 int err;
2500
2501 err = mmc_blk_reset(md, card->host, type);
2502 if (!err)
2503 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002504 if (err == -ENODEV ||
2505 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002506 goto cmd_abort;
2507 /* Fall through */
2508 }
2509 case MMC_BLK_ECC_ERR:
2510 if (brq->data.blocks > 1) {
2511 /* Redo read one sector at a time */
2512 pr_warning("%s: retrying using single block read\n",
2513 req->rq_disk->disk_name);
2514 disable_multi = 1;
2515 break;
2516 }
Per Forlind78d4a82011-07-01 18:55:30 +02002517 /*
2518 * After an error, we redo I/O one sector at a
2519 * time, so we only reach here after trying to
2520 * read a single sector.
2521 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302522 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002523 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002524 if (!ret)
2525 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002526 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302527 case MMC_BLK_NOMEDIUM:
2528 goto cmd_abort;
Konstantin Dorfman4744aae2013-01-07 12:12:17 +02002529 default:
2530 pr_err("%s:%s: Unhandled return value (%d)",
2531 req->rq_disk->disk_name,
2532 __func__, status);
2533 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002534 }
2535
Per Forlinee8a43a2011-07-01 18:55:33 +02002536 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002537 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2538 /*
2539 * In case of a incomplete request
2540 * prepare it again and resend.
2541 */
2542 mmc_blk_rw_rq_prep(mq_rq, card,
2543 disable_multi, mq);
2544 mmc_start_req(card->host,
2545 &mq_rq->mmc_active, NULL);
2546 } else {
2547 if (!mq_rq->packed_retries)
2548 goto cmd_abort;
2549 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2550 mmc_start_req(card->host,
2551 &mq_rq->mmc_active, NULL);
2552 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 } while (ret);
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return 1;
2557
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002558 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002559 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2560 if (mmc_card_removed(card))
2561 req->cmd_flags |= REQ_QUIET;
2562 while (ret)
2563 ret = blk_end_request(req, -EIO,
2564 blk_rq_cur_bytes(req));
2565 } else {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002566 mmc_blk_abort_packed_req(mq_rq, 0);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Per Forlinee8a43a2011-07-01 18:55:33 +02002569 start_new_req:
2570 if (rqc) {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002571 if (mmc_card_removed(card)) {
2572 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2573 rqc->cmd_flags |= REQ_QUIET;
2574 blk_end_request_all(rqc, -EIO);
2575 } else {
2576 mmc_blk_abort_packed_req(mq_rq, REQ_QUIET);
2577 }
2578 } else {
2579 /* If current request is packed, it needs to put back */
2580 if (mq_rq->packed_cmd != MMC_PACKED_NONE)
2581 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002582
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002583 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2584 mmc_start_req(card->host,
2585 &mq->mqrq_cur->mmc_active,
2586 NULL);
2587 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002588 }
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return 0;
2591}
2592
Adrian Hunterbd788c92010-08-11 14:17:47 -07002593static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2594{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002595 int ret;
2596 struct mmc_blk_data *md = mq->data;
2597 struct mmc_card *card = md->queue.card;
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002598 struct mmc_host *host = card->host;
2599 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002600
San Mehat148c57a2009-07-30 08:21:19 -07002601#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2602 if (mmc_bus_needs_resume(card->host)) {
2603 mmc_resume_bus(card->host);
2604 mmc_blk_set_blksize(md, card);
2605 }
2606#endif
2607
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002608 if (req && !mq->mqrq_prev->req) {
Asutosh Dasbbc84782013-02-11 15:31:35 +05302609 mmc_rpm_hold(host, &card->dev);
Per Forlinee8a43a2011-07-01 18:55:33 +02002610 /* claim host only for the first request */
2611 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002612 if (card->ext_csd.bkops_en)
2613 mmc_stop_bkops(card);
2614 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002615
Andrei Warkentin371a6892011-04-11 18:10:25 -05002616 ret = mmc_blk_part_switch(card, md);
2617 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002618 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302619 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002620 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002621 ret = 0;
2622 goto out;
2623 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002624
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002625 mmc_blk_write_packing_control(mq, req);
2626
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002627 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002628 mq->flags &= ~MMC_QUEUE_URGENT_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002629 if (req && req->cmd_flags & REQ_SANITIZE) {
2630 /* complete ongoing async transfer before issuing sanitize */
2631 if (card->host && card->host->areq)
2632 mmc_blk_issue_rw_rq(mq, NULL);
2633 ret = mmc_blk_issue_sanitize_rq(mq, req);
2634 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002635 /* complete ongoing async transfer before issuing discard */
2636 if (card->host->areq)
2637 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302638 if (req->cmd_flags & REQ_SECURE &&
2639 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002640 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002641 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002642 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002643 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002644 /* complete ongoing async transfer before issuing flush */
2645 if (card->host->areq)
2646 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002647 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002648 } else {
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002649 if (!req && host->areq) {
2650 spin_lock_irqsave(&host->context_info.lock, flags);
2651 host->context_info.is_waiting_last_req = true;
2652 spin_unlock_irqrestore(&host->context_info.lock, flags);
2653 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002654 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002655 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002656
Andrei Warkentin371a6892011-04-11 18:10:25 -05002657out:
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002658 /*
2659 * packet burst is over, when one of the following occurs:
2660 * - no more requests and new request notification is not in progress
2661 * - urgent notification in progress and current request is not urgent
2662 * (all existing requests completed or reinserted to the block layer)
2663 */
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002664 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002665 ((mq->flags & MMC_QUEUE_URGENT_REQUEST) &&
2666 !(mq->mqrq_cur->req->cmd_flags & REQ_URGENT))) {
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002667 if (mmc_card_need_bkops(card))
2668 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002669 /* release host only when there are no more requests */
2670 mmc_release_host(card->host);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302671 mmc_rpm_release(host, &card->dev);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002672 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002673 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Russell Kinga6f6c962006-01-03 22:38:44 +00002676static inline int mmc_blk_readonly(struct mmc_card *card)
2677{
2678 return mmc_card_readonly(card) ||
2679 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2680}
2681
Andrei Warkentin371a6892011-04-11 18:10:25 -05002682static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2683 struct device *parent,
2684 sector_t size,
2685 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002686 const char *subname,
2687 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 struct mmc_blk_data *md;
2690 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002691 unsigned int percentage =
2692 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002694 devidx = find_first_zero_bit(dev_use, max_devices);
2695 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return ERR_PTR(-ENOSPC);
2697 __set_bit(devidx, dev_use);
2698
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002699 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002700 if (!md) {
2701 ret = -ENOMEM;
2702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002704
Russell Kinga6f6c962006-01-03 22:38:44 +00002705 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002706 * !subname implies we are creating main mmc_blk_data that will be
2707 * associated with mmc_card with mmc_set_drvdata. Due to device
2708 * partitions, devidx will not coincide with a per-physical card
2709 * index anymore so we keep track of a name index.
2710 */
2711 if (!subname) {
2712 md->name_idx = find_first_zero_bit(name_use, max_devices);
2713 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002714 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002715 md->name_idx = ((struct mmc_blk_data *)
2716 dev_to_disk(parent)->private_data)->name_idx;
2717
Johan Rudholmadd710e2011-12-02 08:51:06 +01002718 md->area_type = area_type;
2719
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002720 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002721 * Set the read-only status based on the supported commands
2722 * and the write protect switch.
2723 */
2724 md->read_only = mmc_blk_readonly(card);
2725
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002726 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002727 if (md->disk == NULL) {
2728 ret = -ENOMEM;
2729 goto err_kfree;
2730 }
2731
2732 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002733 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002734 md->usage = 1;
2735
Adrian Hunterd09408a2011-06-23 13:40:28 +03002736 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002737 if (ret)
2738 goto err_putdisk;
2739
Russell Kinga6f6c962006-01-03 22:38:44 +00002740 md->queue.issue_fn = mmc_blk_issue_rq;
2741 md->queue.data = md;
2742
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002743 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002744 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002745 md->disk->fops = &mmc_bdops;
2746 md->disk->private_data = md;
2747 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002748 md->disk->driverfs_dev = parent;
2749 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002750 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002751 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2752 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002753
2754 /*
2755 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2756 *
2757 * - be set for removable media with permanent block devices
2758 * - be unset for removable block devices with permanent media
2759 *
2760 * Since MMC block devices clearly fall under the second
2761 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2762 * should use the block device creation/destruction hotplug
2763 * messages to tell when the card is present.
2764 */
2765
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002766 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2767 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002768
Martin K. Petersene1defc42009-05-22 17:17:49 -04002769 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002770 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002771
Maya Ereze1eae982013-01-05 21:51:06 +02002772 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2773 card->bkops_info.min_sectors_to_queue_delayed_work =
2774 ((unsigned int)size * percentage) / 100;
2775
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002776 if (mmc_host_cmd23(card->host)) {
2777 if (mmc_card_mmc(card) ||
2778 (mmc_card_sd(card) &&
2779 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2780 md->flags |= MMC_BLK_CMD23;
2781 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002782
2783 if (mmc_card_mmc(card) &&
2784 md->flags & MMC_BLK_CMD23 &&
2785 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2786 card->ext_csd.rel_sectors)) {
2787 md->flags |= MMC_BLK_REL_WR;
2788 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2789 }
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002792
2793 err_putdisk:
2794 put_disk(md->disk);
2795 err_kfree:
2796 kfree(md);
2797 out:
2798 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
Andrei Warkentin371a6892011-04-11 18:10:25 -05002801static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2802{
2803 sector_t size;
2804 struct mmc_blk_data *md;
2805
2806 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2807 /*
2808 * The EXT_CSD sector count is in number or 512 byte
2809 * sectors.
2810 */
2811 size = card->ext_csd.sectors;
2812 } else {
2813 /*
2814 * The CSD capacity field is in units of read_blkbits.
2815 * set_capacity takes units of 512 bytes.
2816 */
2817 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2818 }
2819
Johan Rudholmadd710e2011-12-02 08:51:06 +01002820 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2821 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002822 return md;
2823}
2824
2825static int mmc_blk_alloc_part(struct mmc_card *card,
2826 struct mmc_blk_data *md,
2827 unsigned int part_type,
2828 sector_t size,
2829 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002830 const char *subname,
2831 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002832{
2833 char cap_str[10];
2834 struct mmc_blk_data *part_md;
2835
2836 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002837 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002838 if (IS_ERR(part_md))
2839 return PTR_ERR(part_md);
2840 part_md->part_type = part_type;
2841 list_add(&part_md->part, &md->part);
2842
2843 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2844 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302845 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002846 part_md->disk->disk_name, mmc_card_id(card),
2847 mmc_card_name(card), part_md->part_type, cap_str);
2848 return 0;
2849}
2850
Namjae Jeone0c368d2011-10-06 23:41:38 +09002851/* MMC Physical partitions consist of two boot partitions and
2852 * up to four general purpose partitions.
2853 * For each partition enabled in EXT_CSD a block device will be allocatedi
2854 * to provide access to the partition.
2855 */
2856
Andrei Warkentin371a6892011-04-11 18:10:25 -05002857static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2858{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002859 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002860
2861 if (!mmc_card_mmc(card))
2862 return 0;
2863
Namjae Jeone0c368d2011-10-06 23:41:38 +09002864 for (idx = 0; idx < card->nr_parts; idx++) {
2865 if (card->part[idx].size) {
2866 ret = mmc_blk_alloc_part(card, md,
2867 card->part[idx].part_cfg,
2868 card->part[idx].size >> 9,
2869 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002870 card->part[idx].name,
2871 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002872 if (ret)
2873 return ret;
2874 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002875 }
2876
2877 return ret;
2878}
2879
Andrei Warkentin371a6892011-04-11 18:10:25 -05002880static void mmc_blk_remove_req(struct mmc_blk_data *md)
2881{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002882 struct mmc_card *card;
2883
Andrei Warkentin371a6892011-04-11 18:10:25 -05002884 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002885 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002886 device_remove_file(disk_to_dev(md->disk),
2887 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002888 if (md->disk->flags & GENHD_FL_UP) {
2889 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002890 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2891 card->ext_csd.boot_ro_lockable)
2892 device_remove_file(disk_to_dev(md->disk),
2893 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002894
2895 /* Stop new requests from getting into the queue */
2896 del_gendisk(md->disk);
2897 }
2898
2899 /* Then flush out any already in there */
2900 mmc_cleanup_queue(&md->queue);
2901 mmc_blk_put(md);
2902 }
2903}
2904
2905static void mmc_blk_remove_parts(struct mmc_card *card,
2906 struct mmc_blk_data *md)
2907{
2908 struct list_head *pos, *q;
2909 struct mmc_blk_data *part_md;
2910
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002911 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002912 list_for_each_safe(pos, q, &md->part) {
2913 part_md = list_entry(pos, struct mmc_blk_data, part);
2914 list_del(pos);
2915 mmc_blk_remove_req(part_md);
2916 }
2917}
2918
2919static int mmc_add_disk(struct mmc_blk_data *md)
2920{
2921 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002922 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002923
2924 add_disk(md->disk);
2925 md->force_ro.show = force_ro_show;
2926 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302927 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002928 md->force_ro.attr.name = "force_ro";
2929 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2930 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2931 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002932 goto force_ro_fail;
2933
2934 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2935 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002936 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002937
2938 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2939 mode = S_IRUGO;
2940 else
2941 mode = S_IRUGO | S_IWUSR;
2942
2943 md->power_ro_lock.show = power_ro_lock_show;
2944 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002945 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002946 md->power_ro_lock.attr.mode = mode;
2947 md->power_ro_lock.attr.name =
2948 "ro_lock_until_next_power_on";
2949 ret = device_create_file(disk_to_dev(md->disk),
2950 &md->power_ro_lock);
2951 if (ret)
2952 goto power_ro_lock_fail;
2953 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002954
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002955 md->num_wr_reqs_to_start_packing.show =
2956 num_wr_reqs_to_start_packing_show;
2957 md->num_wr_reqs_to_start_packing.store =
2958 num_wr_reqs_to_start_packing_store;
2959 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2960 md->num_wr_reqs_to_start_packing.attr.name =
2961 "num_wr_reqs_to_start_packing";
2962 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2963 ret = device_create_file(disk_to_dev(md->disk),
2964 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002965 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002966 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002967
Maya Ereze1eae982013-01-05 21:51:06 +02002968 md->bkops_check_threshold.show = bkops_check_threshold_show;
2969 md->bkops_check_threshold.store = bkops_check_threshold_store;
2970 sysfs_attr_init(&md->bkops_check_threshold.attr);
2971 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2972 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002973 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002974 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002975 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002976 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002977
Maya Erez64d12462013-01-06 10:19:38 +02002978 md->no_pack_for_random.show = no_pack_for_random_show;
2979 md->no_pack_for_random.store = no_pack_for_random_store;
2980 sysfs_attr_init(&md->no_pack_for_random.attr);
2981 md->no_pack_for_random.attr.name = "no_pack_for_random";
2982 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
2983 ret = device_create_file(disk_to_dev(md->disk),
2984 &md->no_pack_for_random);
2985 if (ret)
2986 goto no_pack_for_random_fails;
2987
Johan Rudholmadd710e2011-12-02 08:51:06 +01002988 return ret;
2989
Maya Erez64d12462013-01-06 10:19:38 +02002990no_pack_for_random_fails:
2991 device_remove_file(disk_to_dev(md->disk),
2992 &md->bkops_check_threshold);
Maya Ereze1eae982013-01-05 21:51:06 +02002993bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02002994 device_remove_file(disk_to_dev(md->disk),
2995 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002996num_wr_reqs_to_start_packing_fail:
2997 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002998power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002999 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003000force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02003001 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003002
3003 return ret;
3004}
3005
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003006static const struct mmc_fixup blk_fixups[] =
3007{
Chris Ballc59d4472011-11-11 22:01:43 -05003008 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3009 MMC_QUIRK_INAND_CMD38),
3010 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3011 MMC_QUIRK_INAND_CMD38),
3012 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3013 MMC_QUIRK_INAND_CMD38),
3014 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3015 MMC_QUIRK_INAND_CMD38),
3016 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3017 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003018
3019 /*
3020 * Some MMC cards experience performance degradation with CMD23
3021 * instead of CMD12-bounded multiblock transfers. For now we'll
3022 * black list what's bad...
3023 * - Certain Toshiba cards.
3024 *
3025 * N.B. This doesn't affect SD cards.
3026 */
Chris Ballc59d4472011-11-11 22:01:43 -05003027 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003028 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003029 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003030 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003031 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003032 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003033
3034 /*
3035 * Some Micron MMC cards needs longer data read timeout than
3036 * indicated in CSD.
3037 */
Chris Ballc59d4472011-11-11 22:01:43 -05003038 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003039 MMC_QUIRK_LONG_READ_TIME),
3040
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05303041 /* Some INAND MCP devices advertise incorrect timeout values */
3042 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3043 MMC_QUIRK_INAND_DATA_TIMEOUT),
3044
Ian Chenc4856122012-11-01 13:26:21 +05303045 /*
3046 * On these Samsung MoviNAND parts, performing secure erase or
3047 * secure trim can result in unrecoverable corruption due to a
3048 * firmware bug.
3049 */
3050 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3051 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3052 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3053 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3054 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3055 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3056 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3057 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3058 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3059 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3060 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3061 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3062 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3063 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3064 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3065 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3066
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003067 END_FIXUP
3068};
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070static int mmc_blk_probe(struct mmc_card *card)
3071{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003072 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003073 char cap_str[10];
3074
Pierre Ossman912490d2005-05-21 10:27:02 +01003075 /*
3076 * Check that the card supports the command class(es) we need.
3077 */
3078 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return -ENODEV;
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 md = mmc_blk_alloc(card);
3082 if (IS_ERR(md))
3083 return PTR_ERR(md);
3084
Yi Li444122f2009-02-05 15:31:57 +08003085 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003086 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303087 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003089 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Andrei Warkentin371a6892011-04-11 18:10:25 -05003091 if (mmc_blk_alloc_parts(card, md))
3092 goto out;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003095 mmc_fixup_device(card, blk_fixups);
3096
San Mehat148c57a2009-07-30 08:21:19 -07003097#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3098 mmc_set_bus_resume_policy(card->host, 1);
3099#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003100 if (mmc_add_disk(md))
3101 goto out;
3102
3103 list_for_each_entry(part_md, &md->part, part) {
3104 if (mmc_add_disk(part_md))
3105 goto out;
3106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 0;
3108
3109 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003110 mmc_blk_remove_parts(card, md);
3111 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
3115static void mmc_blk_remove(struct mmc_card *card)
3116{
3117 struct mmc_blk_data *md = mmc_get_drvdata(card);
3118
Andrei Warkentin371a6892011-04-11 18:10:25 -05003119 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003120 mmc_claim_host(card->host);
3121 mmc_blk_part_switch(card, md);
3122 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003123 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07003125#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3126 mmc_set_bus_resume_policy(card->host, 0);
3127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003130static void mmc_blk_shutdown(struct mmc_card *card)
3131{
3132 struct mmc_blk_data *part_md;
3133 struct mmc_blk_data *md = mmc_get_drvdata(card);
3134 int rc;
3135
3136 /* Silent the block layer */
3137 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303138 rc = mmc_queue_suspend(&md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003139 if (rc)
3140 goto suspend_error;
3141 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303142 rc = mmc_queue_suspend(&part_md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003143 if (rc)
3144 goto suspend_error;
3145 }
3146 }
3147
3148 /* send power off notification */
3149 if (mmc_card_mmc(card)) {
3150 mmc_rpm_hold(card->host, &card->dev);
3151 mmc_send_long_pon(card);
3152 mmc_rpm_release(card->host, &card->dev);
3153 }
3154 return;
3155
3156suspend_error:
3157 pr_err("%s: mmc_queue_suspend returned error = %d",
3158 mmc_hostname(card->host), rc);
3159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08003162static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003164 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct mmc_blk_data *md = mmc_get_drvdata(card);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303166 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303169 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303170 if (rc)
3171 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003172 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303173 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303174 if (rc)
3175 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303178 goto out;
3179
3180 out_resume:
3181 mmc_queue_resume(&md->queue);
3182 list_for_each_entry(part_md, &md->part, part) {
3183 mmc_queue_resume(&part_md->queue);
3184 }
3185 out:
3186 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
3189static int mmc_blk_resume(struct mmc_card *card)
3190{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003191 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct mmc_blk_data *md = mmc_get_drvdata(card);
3193
3194 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003195 /*
3196 * Resume involves the card going into idle state,
3197 * so current partition is always the main one.
3198 */
3199 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003201 list_for_each_entry(part_md, &md->part, part) {
3202 mmc_queue_resume(&part_md->queue);
3203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205 return 0;
3206}
3207#else
3208#define mmc_blk_suspend NULL
3209#define mmc_blk_resume NULL
3210#endif
3211
3212static struct mmc_driver mmc_driver = {
3213 .drv = {
3214 .name = "mmcblk",
3215 },
3216 .probe = mmc_blk_probe,
3217 .remove = mmc_blk_remove,
3218 .suspend = mmc_blk_suspend,
3219 .resume = mmc_blk_resume,
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003220 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221};
3222
3223static int __init mmc_blk_init(void)
3224{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003225 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003227 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3228 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3229
3230 max_devices = 256 / perdev_minors;
3231
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003232 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3233 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003236 res = mmc_register_driver(&mmc_driver);
3237 if (res)
3238 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003240 return 0;
3241 out2:
3242 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 out:
3244 return res;
3245}
3246
3247static void __exit mmc_blk_exit(void)
3248{
3249 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003250 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
3253module_init(mmc_blk_init);
3254module_exit(mmc_blk_exit);
3255
3256MODULE_LICENSE("GPL");
3257MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3258