blob: d97554375a8978b58b791e9f59c937a55990f915 [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
1366 err = mmc_switch(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);
1391 if (ret)
1392 ret = -EIO;
1393
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301394 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001395
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001396 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001397}
1398
1399/*
1400 * Reformat current write as a reliable write, supporting
1401 * both legacy and the enhanced reliable write MMC cards.
1402 * In each transfer we'll handle only as much as a single
1403 * reliable write can handle, thus finish the request in
1404 * partial completions.
1405 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001406static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1407 struct mmc_card *card,
1408 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001409{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001410 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1411 /* Legacy mode imposes restrictions on transfers. */
1412 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1413 brq->data.blocks = 1;
1414
1415 if (brq->data.blocks > card->ext_csd.rel_sectors)
1416 brq->data.blocks = card->ext_csd.rel_sectors;
1417 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1418 brq->data.blocks = 1;
1419 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420}
1421
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001422#define CMD_ERRORS \
1423 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1424 R1_ADDRESS_ERROR | /* Misaligned address */ \
1425 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1426 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1427 R1_CC_ERROR | /* Card controller error */ \
1428 R1_ERROR) /* General/unknown error */
1429
Per Forlinee8a43a2011-07-01 18:55:33 +02001430static int mmc_blk_err_check(struct mmc_card *card,
1431 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001432{
Per Forlinee8a43a2011-07-01 18:55:33 +02001433 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1434 mmc_active);
1435 struct mmc_blk_request *brq = &mq_mrq->brq;
1436 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001437 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001438
1439 /*
1440 * sbc.error indicates a problem with the set block count
1441 * command. No data will have been transferred.
1442 *
1443 * cmd.error indicates a problem with the r/w command. No
1444 * data will have been transferred.
1445 *
1446 * stop.error indicates a problem with the stop command. Data
1447 * may have been transferred, or may still be transferring.
1448 */
Adrian Hunter67716322011-08-29 16:42:15 +03001449 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1450 brq->data.error) {
1451 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001452 case ERR_RETRY:
1453 return MMC_BLK_RETRY;
1454 case ERR_ABORT:
1455 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301456 case ERR_NOMEDIUM:
1457 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001458 case ERR_CONTINUE:
1459 break;
1460 }
1461 }
1462
1463 /*
1464 * Check for errors relating to the execution of the
1465 * initial command - such as address errors. No data
1466 * has been transferred.
1467 */
1468 if (brq->cmd.resp[0] & CMD_ERRORS) {
1469 pr_err("%s: r/w command failed, status = %#x\n",
1470 req->rq_disk->disk_name, brq->cmd.resp[0]);
1471 return MMC_BLK_ABORT;
1472 }
1473
1474 /*
1475 * Everything else is either success, or a data error of some
1476 * kind. If it was a write, we may have transitioned to
1477 * program mode, which we have to wait for it to complete.
1478 */
1479 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1480 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301481 unsigned long timeout;
1482
1483 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001484 do {
1485 int err = get_card_status(card, &status, 5);
1486 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301487 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001488 req->rq_disk->disk_name, err);
1489 return MMC_BLK_CMD_ERR;
1490 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301491
1492 /* Timeout if the device never becomes ready for data
1493 * and never leaves the program state.
1494 */
1495 if (time_after(jiffies, timeout)) {
1496 pr_err("%s: Card stuck in programming state!"\
1497 " %s %s\n", mmc_hostname(card->host),
1498 req->rq_disk->disk_name, __func__);
1499
1500 return MMC_BLK_CMD_ERR;
1501 }
Per Forlind78d4a82011-07-01 18:55:30 +02001502 /*
1503 * Some cards mishandle the status bits,
1504 * so make sure to check both the busy
1505 * indication and the card state.
1506 */
1507 } while (!(status & R1_READY_FOR_DATA) ||
1508 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1509 }
1510
1511 if (brq->data.error) {
1512 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1513 req->rq_disk->disk_name, brq->data.error,
1514 (unsigned)blk_rq_pos(req),
1515 (unsigned)blk_rq_sectors(req),
1516 brq->cmd.resp[0], brq->stop.resp[0]);
1517
1518 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001519 if (ecc_err)
1520 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001521 return MMC_BLK_DATA_ERR;
1522 } else {
1523 return MMC_BLK_CMD_ERR;
1524 }
1525 }
1526
Adrian Hunter67716322011-08-29 16:42:15 +03001527 if (!brq->data.bytes_xfered)
1528 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001529
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001530 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1531 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1532 return MMC_BLK_PARTIAL;
1533 else
1534 return MMC_BLK_SUCCESS;
1535 }
1536
Adrian Hunter67716322011-08-29 16:42:15 +03001537 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1538 return MMC_BLK_PARTIAL;
1539
1540 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001541}
1542
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001543/*
1544 * mmc_blk_reinsert_req() - re-insert request back to the scheduler
1545 * @areq: request to re-insert.
1546 *
1547 * Request may be packed or single. When fails to reinsert request, it will be
1548 * requeued to the the dispatch queue.
1549 */
1550static void mmc_blk_reinsert_req(struct mmc_async_req *areq)
1551{
1552 struct request *prq;
1553 int ret = 0;
1554 struct mmc_queue_req *mq_rq;
1555 struct request_queue *q;
1556
1557 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1558 q = mq_rq->req->q;
1559 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1560 while (!list_empty(&mq_rq->packed_list)) {
1561 /* return requests in reverse order */
1562 prq = list_entry_rq(mq_rq->packed_list.prev);
1563 list_del_init(&prq->queuelist);
1564 spin_lock_irq(q->queue_lock);
1565 ret = blk_reinsert_request(q, prq);
1566 if (ret) {
1567 blk_requeue_request(q, prq);
1568 spin_unlock_irq(q->queue_lock);
1569 goto reinsert_error;
1570 }
1571 spin_unlock_irq(q->queue_lock);
1572 }
1573 } else {
1574 spin_lock_irq(q->queue_lock);
1575 ret = blk_reinsert_request(q, mq_rq->req);
1576 if (ret)
1577 blk_requeue_request(q, mq_rq->req);
1578 spin_unlock_irq(q->queue_lock);
1579 }
1580 return;
1581
1582reinsert_error:
1583 pr_err("%s: blk_reinsert_request() failed (%d)",
1584 mq_rq->req->rq_disk->disk_name, ret);
1585 /*
1586 * -EIO will be reported for this request and rest of packed_list.
1587 * Urgent request will be proceeded anyway, while upper layer
1588 * responsibility to re-send failed requests
1589 */
1590 while (!list_empty(&mq_rq->packed_list)) {
1591 prq = list_entry_rq(mq_rq->packed_list.next);
1592 list_del_init(&prq->queuelist);
1593 spin_lock_irq(q->queue_lock);
1594 blk_requeue_request(q, prq);
1595 spin_unlock_irq(q->queue_lock);
1596 }
1597}
1598
1599/*
1600 * mmc_blk_update_interrupted_req() - update of the stopped request
1601 * @card: the MMC card associated with the request.
1602 * @areq: interrupted async request.
1603 *
1604 * Get stopped request state from card and update successfully done part of
1605 * the request by setting packed_fail_idx. The packed_fail_idx is index of
1606 * first uncompleted request in packed request list, for non-packed request
1607 * packed_fail_idx remains unchanged.
1608 *
1609 * Returns: MMC_BLK_SUCCESS for success, MMC_BLK_ABORT otherwise
1610 */
1611static int mmc_blk_update_interrupted_req(struct mmc_card *card,
1612 struct mmc_async_req *areq)
1613{
1614 int ret = MMC_BLK_SUCCESS;
1615 u8 *ext_csd;
1616 int correctly_done;
1617 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1618 mmc_active);
1619 struct request *prq;
1620 u8 req_index = 0;
1621
1622 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1623 return MMC_BLK_SUCCESS;
1624
1625 ext_csd = kmalloc(512, GFP_KERNEL);
1626 if (!ext_csd)
1627 return MMC_BLK_ABORT;
1628
1629 /* get correctly programmed sectors number from card */
1630 ret = mmc_send_ext_csd(card, ext_csd);
1631 if (ret) {
1632 pr_err("%s: error %d reading ext_csd\n",
1633 mmc_hostname(card->host), ret);
1634 ret = MMC_BLK_ABORT;
1635 goto exit;
1636 }
1637 correctly_done = card->ext_csd.data_sector_size *
1638 (ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 0] << 0 |
1639 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 1] << 8 |
1640 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 2] << 16 |
1641 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 3] << 24);
1642
1643 list_for_each_entry(prq, &mq_rq->packed_list, queuelist) {
1644 if ((correctly_done - (int)blk_rq_bytes(prq)) < 0) {
1645 /* prq is not successfull */
1646 mq_rq->packed_fail_idx = req_index;
1647 break;
1648 }
1649 correctly_done -= blk_rq_bytes(prq);
1650 req_index++;
1651 }
1652exit:
1653 kfree(ext_csd);
1654 return ret;
1655}
1656
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001657static int mmc_blk_packed_err_check(struct mmc_card *card,
1658 struct mmc_async_req *areq)
1659{
1660 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1661 mmc_active);
1662 struct request *req = mq_rq->req;
1663 int err, check, status;
1664 u8 ext_csd[512];
1665
1666 mq_rq->packed_retries--;
1667 check = mmc_blk_err_check(card, areq);
1668 err = get_card_status(card, &status, 0);
1669 if (err) {
1670 pr_err("%s: error %d sending status command\n",
1671 req->rq_disk->disk_name, err);
1672 return MMC_BLK_ABORT;
1673 }
1674
1675 if (status & R1_EXCEPTION_EVENT) {
1676 err = mmc_send_ext_csd(card, ext_csd);
1677 if (err) {
1678 pr_err("%s: error %d sending ext_csd\n",
1679 req->rq_disk->disk_name, err);
1680 return MMC_BLK_ABORT;
1681 }
1682
1683 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1684 EXT_CSD_PACKED_FAILURE) &&
1685 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1686 EXT_CSD_PACKED_GENERIC_ERROR)) {
1687 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1688 EXT_CSD_PACKED_INDEXED_ERROR) {
1689 mq_rq->packed_fail_idx =
1690 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1691 return MMC_BLK_PARTIAL;
1692 }
1693 }
1694 }
1695
1696 return check;
1697}
1698
Per Forlin54d49d772011-07-01 18:55:29 +02001699static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1700 struct mmc_card *card,
1701 int disable_multi,
1702 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Per Forlin54d49d772011-07-01 18:55:29 +02001704 u32 readcmd, writecmd;
1705 struct mmc_blk_request *brq = &mqrq->brq;
1706 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301708 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001710 /*
1711 * Reliable writes are used to implement Forced Unit Access and
1712 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001713 *
1714 * XXX: this really needs a good explanation of why REQ_META
1715 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001716 */
1717 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1718 (req->cmd_flags & REQ_META)) &&
1719 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001720 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001721
Per Forlin54d49d772011-07-01 18:55:29 +02001722 memset(brq, 0, sizeof(struct mmc_blk_request));
1723 brq->mrq.cmd = &brq->cmd;
1724 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Per Forlin54d49d772011-07-01 18:55:29 +02001726 brq->cmd.arg = blk_rq_pos(req);
1727 if (!mmc_card_blockaddr(card))
1728 brq->cmd.arg <<= 9;
1729 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1730 brq->data.blksz = 512;
1731 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1732 brq->stop.arg = 0;
1733 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1734 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Asutosh Das1a897142012-07-27 18:10:19 +05301736 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001737 /*
1738 * The block layer doesn't support all sector count
1739 * restrictions, so we need to be prepared for too big
1740 * requests.
1741 */
1742 if (brq->data.blocks > card->host->max_blk_count)
1743 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001745 if (brq->data.blocks > 1) {
1746 /*
1747 * After a read error, we redo the request one sector
1748 * at a time in order to accurately determine which
1749 * sectors can be read successfully.
1750 */
1751 if (disable_multi)
1752 brq->data.blocks = 1;
1753
1754 /* Some controllers can't do multiblock reads due to hw bugs */
1755 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1756 rq_data_dir(req) == READ)
1757 brq->data.blocks = 1;
1758 }
Per Forlin54d49d772011-07-01 18:55:29 +02001759
1760 if (brq->data.blocks > 1 || do_rel_wr) {
1761 /* SPI multiblock writes terminate using a special
1762 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001763 */
Per Forlin54d49d772011-07-01 18:55:29 +02001764 if (!mmc_host_is_spi(card->host) ||
1765 rq_data_dir(req) == READ)
1766 brq->mrq.stop = &brq->stop;
1767 readcmd = MMC_READ_MULTIPLE_BLOCK;
1768 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1769 } else {
1770 brq->mrq.stop = NULL;
1771 readcmd = MMC_READ_SINGLE_BLOCK;
1772 writecmd = MMC_WRITE_BLOCK;
1773 }
1774 if (rq_data_dir(req) == READ) {
1775 brq->cmd.opcode = readcmd;
1776 brq->data.flags |= MMC_DATA_READ;
1777 } else {
1778 brq->cmd.opcode = writecmd;
1779 brq->data.flags |= MMC_DATA_WRITE;
1780 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001781
Per Forlin54d49d772011-07-01 18:55:29 +02001782 if (do_rel_wr)
1783 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001784
Per Forlin54d49d772011-07-01 18:55:29 +02001785 /*
Saugata Das42659002011-12-21 13:09:17 +05301786 * Data tag is used only during writing meta data to speed
1787 * up write and any subsequent read of this meta data
1788 */
1789 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1790 (req->cmd_flags & REQ_META) &&
1791 (rq_data_dir(req) == WRITE) &&
1792 ((brq->data.blocks * brq->data.blksz) >=
1793 card->ext_csd.data_tag_unit_size);
1794
1795 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001796 * Pre-defined multi-block transfers are preferable to
1797 * open ended-ones (and necessary for reliable writes).
1798 * However, it is not sufficient to just send CMD23,
1799 * and avoid the final CMD12, as on an error condition
1800 * CMD12 (stop) needs to be sent anyway. This, coupled
1801 * with Auto-CMD23 enhancements provided by some
1802 * hosts, means that the complexity of dealing
1803 * with this is best left to the host. If CMD23 is
1804 * supported by card and host, we'll fill sbc in and let
1805 * the host deal with handling it correctly. This means
1806 * that for hosts that don't expose MMC_CAP_CMD23, no
1807 * change of behavior will be observed.
1808 *
1809 * N.B: Some MMC cards experience perf degradation.
1810 * We'll avoid using CMD23-bounded multiblock writes for
1811 * these, while retaining features like reliable writes.
1812 */
Saugata Das42659002011-12-21 13:09:17 +05301813 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1814 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1815 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001816 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1817 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301818 (do_rel_wr ? (1 << 31) : 0) |
1819 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001820 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1821 brq->mrq.sbc = &brq->sbc;
1822 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001823
Per Forlin54d49d772011-07-01 18:55:29 +02001824 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001825
Per Forlin54d49d772011-07-01 18:55:29 +02001826 brq->data.sg = mqrq->sg;
1827 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001828
Per Forlin54d49d772011-07-01 18:55:29 +02001829 /*
1830 * Adjust the sg list so it is the same size as the
1831 * request.
1832 */
1833 if (brq->data.blocks != blk_rq_sectors(req)) {
1834 int i, data_size = brq->data.blocks << 9;
1835 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001836
Per Forlin54d49d772011-07-01 18:55:29 +02001837 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1838 data_size -= sg->length;
1839 if (data_size <= 0) {
1840 sg->length += data_size;
1841 i++;
1842 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001843 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001844 }
Per Forlin54d49d772011-07-01 18:55:29 +02001845 brq->data.sg_len = i;
1846 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001847
Per Forlinee8a43a2011-07-01 18:55:33 +02001848 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02001849 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001850 if (mq->err_check_fn)
1851 mqrq->mmc_active.err_check = mq->err_check_fn;
1852 else
1853 mqrq->mmc_active.err_check = mmc_blk_err_check;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001854 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
1855 mqrq->mmc_active.update_interrupted_req =
1856 mmc_blk_update_interrupted_req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001857
Per Forlin54d49d772011-07-01 18:55:29 +02001858 mmc_queue_bounce_pre(mqrq);
1859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001861/**
1862 * mmc_blk_disable_wr_packing() - disables packing mode
1863 * @mq: MMC queue.
1864 *
1865 */
1866void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
1867{
1868 if (mq) {
1869 mq->wr_packing_enabled = false;
1870 mq->num_of_potential_packed_wr_reqs = 0;
1871 }
1872}
1873EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
1874
Lee Susman2e3c0642013-04-23 17:59:26 +03001875static int get_packed_trigger(int potential, struct mmc_card *card,
1876 struct request *req, int curr_trigger)
1877{
1878 static int num_mean_elements = 1;
1879 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1880 unsigned int trigger = curr_trigger;
1881 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
1882
1883 /* scale down the upper bound to 75% */
1884 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
1885
1886 /*
1887 * since the most common calls for this function are with small
1888 * potential write values and since we don't want these calls to affect
1889 * the packed trigger, set a lower bound and ignore calls with
1890 * potential lower than that bound
1891 */
1892 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
1893 return trigger;
1894
1895 /*
1896 * this is to prevent integer overflow in the following calculation:
1897 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
1898 */
1899 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
1900 num_mean_elements = 1;
1901 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1902 }
1903
1904 /*
1905 * get next mean value based on previous mean value and current
1906 * potential packed writes. Calculation is as follows:
1907 * mean_pot[i+1] =
1908 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
1909 */
1910 mean_potential *= num_mean_elements;
1911 /*
1912 * add num_mean_elements so that the division of two integers doesn't
1913 * lower mean_potential too much
1914 */
1915 if (potential > mean_potential)
1916 mean_potential += num_mean_elements;
1917 mean_potential += potential;
1918 /* this is for gaining more precision when dividing two integers */
1919 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
1920 /* this completes the mean calculation */
1921 mean_potential /= ++num_mean_elements;
1922 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
1923
1924 /*
1925 * if current potential packed writes is greater than the mean potential
1926 * then the heuristic is that the following workload will contain many
1927 * write requests, therefore we lower the packed trigger. In the
1928 * opposite case we want to increase the trigger in order to get less
1929 * packing events.
1930 */
1931 if (potential >= mean_potential)
1932 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
1933 PCKD_TRGR_LOWER_BOUND : trigger - 1;
1934 else
1935 trigger = (trigger >= pckd_trgr_upper_bound) ?
1936 pckd_trgr_upper_bound : trigger + 1;
1937
1938 /*
1939 * an urgent read request indicates a packed list being interrupted
1940 * by this read, therefore we aim for less packing, hence the trigger
1941 * gets increased
1942 */
1943 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
1944 trigger += PCKD_TRGR_URGENT_PENALTY;
1945
1946 return trigger;
1947}
1948
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001949static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1950 struct request *req)
1951{
1952 struct mmc_host *host = mq->card->host;
1953 int data_dir;
1954
1955 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1956 return;
1957
Maya Erez6f219eb2012-12-02 13:27:15 +02001958 /* Support for the write packing on eMMC 4.5 or later */
1959 if (mq->card->ext_csd.rev <= 5)
1960 return;
1961
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001962 /*
1963 * In case the packing control is not supported by the host, it should
1964 * not have an effect on the write packing. Therefore we have to enable
1965 * the write packing
1966 */
1967 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1968 mq->wr_packing_enabled = true;
1969 return;
1970 }
1971
1972 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1973 if (mq->num_of_potential_packed_wr_reqs >
1974 mq->num_wr_reqs_to_start_packing)
1975 mq->wr_packing_enabled = true;
Lee Susman2e3c0642013-04-23 17:59:26 +03001976 mq->num_wr_reqs_to_start_packing =
1977 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
1978 mq->card, req,
1979 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001980 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001981 return;
1982 }
1983
1984 data_dir = rq_data_dir(req);
1985
1986 if (data_dir == READ) {
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001987 mmc_blk_disable_wr_packing(mq);
Lee Susman2e3c0642013-04-23 17:59:26 +03001988 mq->num_wr_reqs_to_start_packing =
1989 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
1990 mq->card, req,
1991 mq->num_wr_reqs_to_start_packing);
1992 mq->num_of_potential_packed_wr_reqs = 0;
1993 mq->wr_packing_enabled = false;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001994 return;
1995 } else if (data_dir == WRITE) {
1996 mq->num_of_potential_packed_wr_reqs++;
1997 }
1998
1999 if (mq->num_of_potential_packed_wr_reqs >
2000 mq->num_wr_reqs_to_start_packing)
2001 mq->wr_packing_enabled = true;
2002
2003}
2004
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002005struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2006{
2007 if (!card)
2008 return NULL;
2009
2010 return &card->wr_pack_stats;
2011}
2012EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2013
2014void mmc_blk_init_packed_statistics(struct mmc_card *card)
2015{
2016 int max_num_of_packed_reqs = 0;
2017
2018 if (!card || !card->wr_pack_stats.packing_events)
2019 return;
2020
2021 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2022
2023 spin_lock(&card->wr_pack_stats.lock);
2024 memset(card->wr_pack_stats.packing_events, 0,
2025 (max_num_of_packed_reqs + 1) *
2026 sizeof(*card->wr_pack_stats.packing_events));
2027 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2028 sizeof(card->wr_pack_stats.pack_stop_reason));
2029 card->wr_pack_stats.enabled = true;
2030 spin_unlock(&card->wr_pack_stats.lock);
2031}
2032EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2033
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002034static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2035{
2036 struct request_queue *q = mq->queue;
2037 struct mmc_card *card = mq->card;
2038 struct request *cur = req, *next = NULL;
2039 struct mmc_blk_data *md = mq->data;
2040 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2041 unsigned int req_sectors = 0, phys_segments = 0;
2042 unsigned int max_blk_count, max_phys_segs;
2043 u8 put_back = 0;
2044 u8 max_packed_rw = 0;
2045 u8 reqs = 0;
Lee Susmand50afb52013-01-09 14:48:47 +02002046 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002047
2048 mmc_blk_clear_packed(mq->mqrq_cur);
2049
2050 if (!(md->flags & MMC_BLK_CMD23) ||
2051 !card->ext_csd.packed_event_en)
2052 goto no_packed;
2053
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002054 if (!mq->wr_packing_enabled)
2055 goto no_packed;
2056
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002057 if ((rq_data_dir(cur) == WRITE) &&
2058 (card->host->caps2 & MMC_CAP2_PACKED_WR))
2059 max_packed_rw = card->ext_csd.max_packed_writes;
2060
2061 if (max_packed_rw == 0)
2062 goto no_packed;
2063
2064 if (mmc_req_rel_wr(cur) &&
2065 (md->flags & MMC_BLK_REL_WR) &&
2066 !en_rel_wr)
2067 goto no_packed;
2068
2069 if (mmc_large_sec(card) &&
2070 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2071 goto no_packed;
2072
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002073 if (cur->cmd_flags & REQ_FUA)
2074 goto no_packed;
2075
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002076 max_blk_count = min(card->host->max_blk_count,
2077 card->host->max_req_size >> 9);
2078 if (unlikely(max_blk_count > 0xffff))
2079 max_blk_count = 0xffff;
2080
2081 max_phys_segs = queue_max_segments(q);
2082 req_sectors += blk_rq_sectors(cur);
2083 phys_segments += cur->nr_phys_segments;
2084
2085 if (rq_data_dir(cur) == WRITE) {
2086 req_sectors++;
2087 phys_segments++;
2088 }
2089
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002090 spin_lock(&stats->lock);
2091
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002092 while (reqs < max_packed_rw - 1) {
2093 spin_lock_irq(q->queue_lock);
2094 next = blk_fetch_request(q);
2095 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002096 if (!next) {
2097 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002098 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002099 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002100
2101 if (mmc_large_sec(card) &&
2102 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02002103 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002104 put_back = 1;
2105 break;
2106 }
2107
2108 if (next->cmd_flags & REQ_DISCARD ||
2109 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002110 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002111 put_back = 1;
2112 break;
2113 }
2114
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002115 if (next->cmd_flags & REQ_FUA) {
2116 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2117 put_back = 1;
2118 break;
2119 }
2120
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002121 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002122 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002123 put_back = 1;
2124 break;
2125 }
2126
2127 if (mmc_req_rel_wr(next) &&
2128 (md->flags & MMC_BLK_REL_WR) &&
2129 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002130 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002131 put_back = 1;
2132 break;
2133 }
2134
2135 req_sectors += blk_rq_sectors(next);
2136 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002137 if (stats->enabled)
2138 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002139 put_back = 1;
2140 break;
2141 }
2142
2143 phys_segments += next->nr_phys_segments;
2144 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002145 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002146 put_back = 1;
2147 break;
2148 }
2149
Maya Erez64d12462013-01-06 10:19:38 +02002150 if (mq->no_pack_for_random) {
2151 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2152 blk_rq_pos(next)) {
2153 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2154 put_back = 1;
2155 break;
2156 }
2157 }
2158
Maya Erez2d33a192013-01-04 15:52:41 +02002159 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002160 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02002161 if (card->ext_csd.bkops_en)
2162 card->bkops_info.sectors_changed +=
2163 blk_rq_sectors(next);
2164 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002165 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
2166 cur = next;
2167 reqs++;
2168 }
2169
2170 if (put_back) {
2171 spin_lock_irq(q->queue_lock);
2172 blk_requeue_request(q, next);
2173 spin_unlock_irq(q->queue_lock);
2174 }
2175
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002176 if (stats->enabled) {
2177 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2178 stats->packing_events[reqs + 1]++;
2179 if (reqs + 1 == max_packed_rw)
2180 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2181 }
2182
2183 spin_unlock(&stats->lock);
2184
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002185 if (reqs > 0) {
2186 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
2187 mq->mqrq_cur->packed_num = ++reqs;
2188 mq->mqrq_cur->packed_retries = reqs;
2189 return reqs;
2190 }
2191
2192no_packed:
2193 mmc_blk_clear_packed(mq->mqrq_cur);
2194 return 0;
2195}
2196
2197static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2198 struct mmc_card *card,
2199 struct mmc_queue *mq)
2200{
2201 struct mmc_blk_request *brq = &mqrq->brq;
2202 struct request *req = mqrq->req;
2203 struct request *prq;
2204 struct mmc_blk_data *md = mq->data;
2205 bool do_rel_wr, do_data_tag;
2206 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
2207 u8 i = 1;
2208
2209 mqrq->packed_cmd = MMC_PACKED_WRITE;
2210 mqrq->packed_blocks = 0;
2211 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
2212
2213 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
2214 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
2215 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
2216
2217 /*
2218 * Argument for each entry of packed group
2219 */
2220 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
2221 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2222 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2223 (prq->cmd_flags & REQ_META) &&
2224 (rq_data_dir(prq) == WRITE) &&
2225 ((brq->data.blocks * brq->data.blksz) >=
2226 card->ext_csd.data_tag_unit_size);
2227 /* Argument of CMD23 */
2228 packed_cmd_hdr[(i * 2)] =
2229 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2230 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
2231 blk_rq_sectors(prq);
2232 /* Argument of CMD18 or CMD25 */
2233 packed_cmd_hdr[((i * 2)) + 1] =
2234 mmc_card_blockaddr(card) ?
2235 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
2236 mqrq->packed_blocks += blk_rq_sectors(prq);
2237 i++;
2238 }
2239
2240 memset(brq, 0, sizeof(struct mmc_blk_request));
2241 brq->mrq.cmd = &brq->cmd;
2242 brq->mrq.data = &brq->data;
2243 brq->mrq.sbc = &brq->sbc;
2244 brq->mrq.stop = &brq->stop;
2245
2246 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2247 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
2248 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2249
2250 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2251 brq->cmd.arg = blk_rq_pos(req);
2252 if (!mmc_card_blockaddr(card))
2253 brq->cmd.arg <<= 9;
2254 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2255
2256 brq->data.blksz = 512;
2257 brq->data.blocks = mqrq->packed_blocks + 1;
2258 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02002259 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002260
2261 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2262 brq->stop.arg = 0;
2263 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2264
2265 mmc_set_data_timeout(&brq->data, card);
2266
2267 brq->data.sg = mqrq->sg;
2268 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2269
2270 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002271 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02002272
2273 /*
2274 * This is intended for packed commands tests usage - in case these
2275 * functions are not in use the respective pointers are NULL
2276 */
2277 if (mq->err_check_fn)
2278 mqrq->mmc_active.err_check = mq->err_check_fn;
2279 else
2280 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2281
2282 if (mq->packed_test_fn)
2283 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002284
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002285
2286 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
2287 mqrq->mmc_active.update_interrupted_req =
2288 mmc_blk_update_interrupted_req;
2289
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002290 mmc_queue_bounce_pre(mqrq);
2291}
2292
Adrian Hunter67716322011-08-29 16:42:15 +03002293static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2294 struct mmc_blk_request *brq, struct request *req,
2295 int ret)
2296{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002297 struct mmc_queue_req *mq_rq;
2298 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2299
Adrian Hunter67716322011-08-29 16:42:15 +03002300 /*
2301 * If this is an SD card and we're writing, we can first
2302 * mark the known good sectors as ok.
2303 *
2304 * If the card is not SD, we can still ok written sectors
2305 * as reported by the controller (which might be less than
2306 * the real number of written sectors, but never more).
2307 */
2308 if (mmc_card_sd(card)) {
2309 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05302310 if (!brq->data.fault_injected) {
2311 blocks = mmc_sd_num_wr_blocks(card);
2312 if (blocks != (u32)-1)
2313 ret = blk_end_request(req, 0, blocks << 9);
2314 } else
2315 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002316 } else {
2317 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
2318 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
2319 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03002320 return ret;
2321}
2322
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002323static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2324{
2325 struct request *prq;
2326 int idx = mq_rq->packed_fail_idx, i = 0;
2327 int ret = 0;
2328
2329 while (!list_empty(&mq_rq->packed_list)) {
2330 prq = list_entry_rq(mq_rq->packed_list.next);
2331 if (idx == i) {
2332 /* retry from error index */
2333 mq_rq->packed_num -= idx;
2334 mq_rq->req = prq;
2335 ret = 1;
2336
2337 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
2338 list_del_init(&prq->queuelist);
2339 mmc_blk_clear_packed(mq_rq);
2340 }
2341 return ret;
2342 }
2343 list_del_init(&prq->queuelist);
2344 blk_end_request(prq, 0, blk_rq_bytes(prq));
2345 i++;
2346 }
2347
2348 mmc_blk_clear_packed(mq_rq);
2349 return ret;
2350}
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002351static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq,
2352 unsigned int cmd_flags)
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002353{
2354 struct request *prq;
2355
2356 while (!list_empty(&mq_rq->packed_list)) {
2357 prq = list_entry_rq(mq_rq->packed_list.next);
2358 list_del_init(&prq->queuelist);
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002359 prq->cmd_flags |= cmd_flags;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002360 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2361 }
2362
2363 mmc_blk_clear_packed(mq_rq);
2364}
2365
2366static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2367 struct mmc_queue_req *mq_rq)
2368{
2369 struct request *prq;
2370 struct request_queue *q = mq->queue;
2371
2372 while (!list_empty(&mq_rq->packed_list)) {
2373 prq = list_entry_rq(mq_rq->packed_list.prev);
2374 if (prq->queuelist.prev != &mq_rq->packed_list) {
2375 list_del_init(&prq->queuelist);
2376 spin_lock_irq(q->queue_lock);
2377 blk_requeue_request(mq->queue, prq);
2378 spin_unlock_irq(q->queue_lock);
2379 } else {
2380 list_del_init(&prq->queuelist);
2381 }
2382 }
2383
2384 mmc_blk_clear_packed(mq_rq);
2385}
2386
Per Forlinee8a43a2011-07-01 18:55:33 +02002387static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02002388{
2389 struct mmc_blk_data *md = mq->data;
2390 struct mmc_card *card = md->queue.card;
2391 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03002392 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02002393 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002394 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02002395 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002396 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002397 const u8 packed_num = 2;
2398 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02002399
2400 if (!rqc && !mq->mqrq_prev->req)
2401 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02002402
Maya Erez5f360692012-10-10 03:47:54 +02002403 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02002404 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
2405 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002406 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02002407 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002408
Per Forlin54d49d772011-07-01 18:55:29 +02002409 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002410 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002411 if (reqs >= packed_num)
2412 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2413 card, mq);
2414 else
2415 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002416 areq = &mq->mqrq_cur->mmc_active;
2417 } else
2418 areq = NULL;
2419 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002420 if (!areq) {
Lee Susmand50afb52013-01-09 14:48:47 +02002421 if (status == MMC_BLK_NEW_REQUEST)
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002422 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002423 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002424 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002425
Per Forlinee8a43a2011-07-01 18:55:33 +02002426 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2427 brq = &mq_rq->brq;
2428 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002429 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002430 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002431
Per Forlind78d4a82011-07-01 18:55:30 +02002432 switch (status) {
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002433 case MMC_BLK_URGENT:
2434 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2435 /* complete successfully transmitted part */
2436 if (mmc_blk_end_packed_req(mq_rq))
2437 /* process for not transmitted part */
2438 mmc_blk_reinsert_req(areq);
2439 } else {
2440 mmc_blk_reinsert_req(areq);
2441 }
2442
2443 mq->flags |= MMC_QUEUE_URGENT_REQUEST;
2444 ret = 0;
2445 break;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002446 case MMC_BLK_URGENT_DONE:
Per Forlind78d4a82011-07-01 18:55:30 +02002447 case MMC_BLK_SUCCESS:
2448 case MMC_BLK_PARTIAL:
2449 /*
2450 * A block was successfully transferred.
2451 */
Adrian Hunter67716322011-08-29 16:42:15 +03002452 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002453
2454 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2455 ret = mmc_blk_end_packed_req(mq_rq);
2456 break;
2457 } else {
2458 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002459 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002460 }
2461
Adrian Hunter67716322011-08-29 16:42:15 +03002462 /*
2463 * If the blk_end_request function returns non-zero even
2464 * though all data has been transferred and no errors
2465 * were returned by the host controller, it's a bug.
2466 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002467 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302468 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002469 __func__, blk_rq_bytes(req),
2470 brq->data.bytes_xfered);
2471 rqc = NULL;
2472 goto cmd_abort;
2473 }
Per Forlind78d4a82011-07-01 18:55:30 +02002474 break;
2475 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002476 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2477 if (!mmc_blk_reset(md, card->host, type))
2478 break;
2479 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002480 case MMC_BLK_RETRY:
2481 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002482 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002483 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002484 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002485 if (!mmc_blk_reset(md, card->host, type))
2486 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002487 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002488 case MMC_BLK_DATA_ERR: {
2489 int err;
2490
2491 err = mmc_blk_reset(md, card->host, type);
2492 if (!err)
2493 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002494 if (err == -ENODEV ||
2495 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002496 goto cmd_abort;
2497 /* Fall through */
2498 }
2499 case MMC_BLK_ECC_ERR:
2500 if (brq->data.blocks > 1) {
2501 /* Redo read one sector at a time */
2502 pr_warning("%s: retrying using single block read\n",
2503 req->rq_disk->disk_name);
2504 disable_multi = 1;
2505 break;
2506 }
Per Forlind78d4a82011-07-01 18:55:30 +02002507 /*
2508 * After an error, we redo I/O one sector at a
2509 * time, so we only reach here after trying to
2510 * read a single sector.
2511 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302512 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002513 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002514 if (!ret)
2515 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002516 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302517 case MMC_BLK_NOMEDIUM:
2518 goto cmd_abort;
Konstantin Dorfman4744aae2013-01-07 12:12:17 +02002519 default:
2520 pr_err("%s:%s: Unhandled return value (%d)",
2521 req->rq_disk->disk_name,
2522 __func__, status);
2523 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002524 }
2525
Per Forlinee8a43a2011-07-01 18:55:33 +02002526 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002527 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2528 /*
2529 * In case of a incomplete request
2530 * prepare it again and resend.
2531 */
2532 mmc_blk_rw_rq_prep(mq_rq, card,
2533 disable_multi, mq);
2534 mmc_start_req(card->host,
2535 &mq_rq->mmc_active, NULL);
2536 } else {
2537 if (!mq_rq->packed_retries)
2538 goto cmd_abort;
2539 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2540 mmc_start_req(card->host,
2541 &mq_rq->mmc_active, NULL);
2542 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 } while (ret);
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return 1;
2547
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002548 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002549 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2550 if (mmc_card_removed(card))
2551 req->cmd_flags |= REQ_QUIET;
2552 while (ret)
2553 ret = blk_end_request(req, -EIO,
2554 blk_rq_cur_bytes(req));
2555 } else {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002556 mmc_blk_abort_packed_req(mq_rq, 0);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Per Forlinee8a43a2011-07-01 18:55:33 +02002559 start_new_req:
2560 if (rqc) {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002561 if (mmc_card_removed(card)) {
2562 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2563 rqc->cmd_flags |= REQ_QUIET;
2564 blk_end_request_all(rqc, -EIO);
2565 } else {
2566 mmc_blk_abort_packed_req(mq_rq, REQ_QUIET);
2567 }
2568 } else {
2569 /* If current request is packed, it needs to put back */
2570 if (mq_rq->packed_cmd != MMC_PACKED_NONE)
2571 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002572
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002573 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2574 mmc_start_req(card->host,
2575 &mq->mqrq_cur->mmc_active,
2576 NULL);
2577 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002578 }
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return 0;
2581}
2582
Adrian Hunterbd788c92010-08-11 14:17:47 -07002583static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2584{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002585 int ret;
2586 struct mmc_blk_data *md = mq->data;
2587 struct mmc_card *card = md->queue.card;
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002588 struct mmc_host *host = card->host;
2589 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002590
San Mehat148c57a2009-07-30 08:21:19 -07002591#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2592 if (mmc_bus_needs_resume(card->host)) {
2593 mmc_resume_bus(card->host);
2594 mmc_blk_set_blksize(md, card);
2595 }
2596#endif
2597
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002598 if (req && !mq->mqrq_prev->req) {
Asutosh Dasbbc84782013-02-11 15:31:35 +05302599 mmc_rpm_hold(host, &card->dev);
Per Forlinee8a43a2011-07-01 18:55:33 +02002600 /* claim host only for the first request */
2601 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002602 if (card->ext_csd.bkops_en)
2603 mmc_stop_bkops(card);
2604 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002605
Andrei Warkentin371a6892011-04-11 18:10:25 -05002606 ret = mmc_blk_part_switch(card, md);
2607 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002608 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302609 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002610 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002611 ret = 0;
2612 goto out;
2613 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002614
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002615 mmc_blk_write_packing_control(mq, req);
2616
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002617 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002618 mq->flags &= ~MMC_QUEUE_URGENT_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002619 if (req && req->cmd_flags & REQ_SANITIZE) {
2620 /* complete ongoing async transfer before issuing sanitize */
2621 if (card->host && card->host->areq)
2622 mmc_blk_issue_rw_rq(mq, NULL);
2623 ret = mmc_blk_issue_sanitize_rq(mq, req);
2624 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002625 /* complete ongoing async transfer before issuing discard */
2626 if (card->host->areq)
2627 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302628 if (req->cmd_flags & REQ_SECURE &&
2629 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002630 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002631 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002632 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002633 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002634 /* complete ongoing async transfer before issuing flush */
2635 if (card->host->areq)
2636 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002637 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002638 } else {
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002639 if (!req && host->areq) {
2640 spin_lock_irqsave(&host->context_info.lock, flags);
2641 host->context_info.is_waiting_last_req = true;
2642 spin_unlock_irqrestore(&host->context_info.lock, flags);
2643 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002644 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002645 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002646
Andrei Warkentin371a6892011-04-11 18:10:25 -05002647out:
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002648 /*
2649 * packet burst is over, when one of the following occurs:
2650 * - no more requests and new request notification is not in progress
2651 * - urgent notification in progress and current request is not urgent
2652 * (all existing requests completed or reinserted to the block layer)
2653 */
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002654 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002655 ((mq->flags & MMC_QUEUE_URGENT_REQUEST) &&
2656 !(mq->mqrq_cur->req->cmd_flags & REQ_URGENT))) {
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002657 if (mmc_card_need_bkops(card))
2658 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002659 /* release host only when there are no more requests */
2660 mmc_release_host(card->host);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302661 mmc_rpm_release(host, &card->dev);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002662 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002663 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Russell Kinga6f6c962006-01-03 22:38:44 +00002666static inline int mmc_blk_readonly(struct mmc_card *card)
2667{
2668 return mmc_card_readonly(card) ||
2669 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2670}
2671
Andrei Warkentin371a6892011-04-11 18:10:25 -05002672static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2673 struct device *parent,
2674 sector_t size,
2675 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002676 const char *subname,
2677 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
2679 struct mmc_blk_data *md;
2680 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002681 unsigned int percentage =
2682 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002684 devidx = find_first_zero_bit(dev_use, max_devices);
2685 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return ERR_PTR(-ENOSPC);
2687 __set_bit(devidx, dev_use);
2688
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002689 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002690 if (!md) {
2691 ret = -ENOMEM;
2692 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002694
Russell Kinga6f6c962006-01-03 22:38:44 +00002695 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002696 * !subname implies we are creating main mmc_blk_data that will be
2697 * associated with mmc_card with mmc_set_drvdata. Due to device
2698 * partitions, devidx will not coincide with a per-physical card
2699 * index anymore so we keep track of a name index.
2700 */
2701 if (!subname) {
2702 md->name_idx = find_first_zero_bit(name_use, max_devices);
2703 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002704 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002705 md->name_idx = ((struct mmc_blk_data *)
2706 dev_to_disk(parent)->private_data)->name_idx;
2707
Johan Rudholmadd710e2011-12-02 08:51:06 +01002708 md->area_type = area_type;
2709
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002710 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002711 * Set the read-only status based on the supported commands
2712 * and the write protect switch.
2713 */
2714 md->read_only = mmc_blk_readonly(card);
2715
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002716 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002717 if (md->disk == NULL) {
2718 ret = -ENOMEM;
2719 goto err_kfree;
2720 }
2721
2722 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002723 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002724 md->usage = 1;
2725
Adrian Hunterd09408a2011-06-23 13:40:28 +03002726 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002727 if (ret)
2728 goto err_putdisk;
2729
Russell Kinga6f6c962006-01-03 22:38:44 +00002730 md->queue.issue_fn = mmc_blk_issue_rq;
2731 md->queue.data = md;
2732
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002733 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002734 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002735 md->disk->fops = &mmc_bdops;
2736 md->disk->private_data = md;
2737 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002738 md->disk->driverfs_dev = parent;
2739 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002740 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002741 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2742 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002743
2744 /*
2745 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2746 *
2747 * - be set for removable media with permanent block devices
2748 * - be unset for removable block devices with permanent media
2749 *
2750 * Since MMC block devices clearly fall under the second
2751 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2752 * should use the block device creation/destruction hotplug
2753 * messages to tell when the card is present.
2754 */
2755
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002756 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2757 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002758
Martin K. Petersene1defc42009-05-22 17:17:49 -04002759 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002760 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002761
Maya Ereze1eae982013-01-05 21:51:06 +02002762 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2763 card->bkops_info.min_sectors_to_queue_delayed_work =
2764 ((unsigned int)size * percentage) / 100;
2765
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002766 if (mmc_host_cmd23(card->host)) {
2767 if (mmc_card_mmc(card) ||
2768 (mmc_card_sd(card) &&
2769 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2770 md->flags |= MMC_BLK_CMD23;
2771 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002772
2773 if (mmc_card_mmc(card) &&
2774 md->flags & MMC_BLK_CMD23 &&
2775 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2776 card->ext_csd.rel_sectors)) {
2777 md->flags |= MMC_BLK_REL_WR;
2778 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2779 }
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002782
2783 err_putdisk:
2784 put_disk(md->disk);
2785 err_kfree:
2786 kfree(md);
2787 out:
2788 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Andrei Warkentin371a6892011-04-11 18:10:25 -05002791static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2792{
2793 sector_t size;
2794 struct mmc_blk_data *md;
2795
2796 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2797 /*
2798 * The EXT_CSD sector count is in number or 512 byte
2799 * sectors.
2800 */
2801 size = card->ext_csd.sectors;
2802 } else {
2803 /*
2804 * The CSD capacity field is in units of read_blkbits.
2805 * set_capacity takes units of 512 bytes.
2806 */
2807 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2808 }
2809
Johan Rudholmadd710e2011-12-02 08:51:06 +01002810 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2811 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002812 return md;
2813}
2814
2815static int mmc_blk_alloc_part(struct mmc_card *card,
2816 struct mmc_blk_data *md,
2817 unsigned int part_type,
2818 sector_t size,
2819 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002820 const char *subname,
2821 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002822{
2823 char cap_str[10];
2824 struct mmc_blk_data *part_md;
2825
2826 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002827 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002828 if (IS_ERR(part_md))
2829 return PTR_ERR(part_md);
2830 part_md->part_type = part_type;
2831 list_add(&part_md->part, &md->part);
2832
2833 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2834 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302835 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002836 part_md->disk->disk_name, mmc_card_id(card),
2837 mmc_card_name(card), part_md->part_type, cap_str);
2838 return 0;
2839}
2840
Namjae Jeone0c368d2011-10-06 23:41:38 +09002841/* MMC Physical partitions consist of two boot partitions and
2842 * up to four general purpose partitions.
2843 * For each partition enabled in EXT_CSD a block device will be allocatedi
2844 * to provide access to the partition.
2845 */
2846
Andrei Warkentin371a6892011-04-11 18:10:25 -05002847static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2848{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002849 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002850
2851 if (!mmc_card_mmc(card))
2852 return 0;
2853
Namjae Jeone0c368d2011-10-06 23:41:38 +09002854 for (idx = 0; idx < card->nr_parts; idx++) {
2855 if (card->part[idx].size) {
2856 ret = mmc_blk_alloc_part(card, md,
2857 card->part[idx].part_cfg,
2858 card->part[idx].size >> 9,
2859 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002860 card->part[idx].name,
2861 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002862 if (ret)
2863 return ret;
2864 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002865 }
2866
2867 return ret;
2868}
2869
Andrei Warkentin371a6892011-04-11 18:10:25 -05002870static void mmc_blk_remove_req(struct mmc_blk_data *md)
2871{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002872 struct mmc_card *card;
2873
Andrei Warkentin371a6892011-04-11 18:10:25 -05002874 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002875 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002876 device_remove_file(disk_to_dev(md->disk),
2877 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002878 if (md->disk->flags & GENHD_FL_UP) {
2879 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002880 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2881 card->ext_csd.boot_ro_lockable)
2882 device_remove_file(disk_to_dev(md->disk),
2883 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002884
2885 /* Stop new requests from getting into the queue */
2886 del_gendisk(md->disk);
2887 }
2888
2889 /* Then flush out any already in there */
2890 mmc_cleanup_queue(&md->queue);
2891 mmc_blk_put(md);
2892 }
2893}
2894
2895static void mmc_blk_remove_parts(struct mmc_card *card,
2896 struct mmc_blk_data *md)
2897{
2898 struct list_head *pos, *q;
2899 struct mmc_blk_data *part_md;
2900
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002901 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002902 list_for_each_safe(pos, q, &md->part) {
2903 part_md = list_entry(pos, struct mmc_blk_data, part);
2904 list_del(pos);
2905 mmc_blk_remove_req(part_md);
2906 }
2907}
2908
2909static int mmc_add_disk(struct mmc_blk_data *md)
2910{
2911 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002912 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002913
2914 add_disk(md->disk);
2915 md->force_ro.show = force_ro_show;
2916 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302917 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002918 md->force_ro.attr.name = "force_ro";
2919 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2920 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2921 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002922 goto force_ro_fail;
2923
2924 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2925 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002926 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002927
2928 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2929 mode = S_IRUGO;
2930 else
2931 mode = S_IRUGO | S_IWUSR;
2932
2933 md->power_ro_lock.show = power_ro_lock_show;
2934 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002935 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002936 md->power_ro_lock.attr.mode = mode;
2937 md->power_ro_lock.attr.name =
2938 "ro_lock_until_next_power_on";
2939 ret = device_create_file(disk_to_dev(md->disk),
2940 &md->power_ro_lock);
2941 if (ret)
2942 goto power_ro_lock_fail;
2943 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002944
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002945 md->num_wr_reqs_to_start_packing.show =
2946 num_wr_reqs_to_start_packing_show;
2947 md->num_wr_reqs_to_start_packing.store =
2948 num_wr_reqs_to_start_packing_store;
2949 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2950 md->num_wr_reqs_to_start_packing.attr.name =
2951 "num_wr_reqs_to_start_packing";
2952 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2953 ret = device_create_file(disk_to_dev(md->disk),
2954 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002955 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002956 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002957
Maya Ereze1eae982013-01-05 21:51:06 +02002958 md->bkops_check_threshold.show = bkops_check_threshold_show;
2959 md->bkops_check_threshold.store = bkops_check_threshold_store;
2960 sysfs_attr_init(&md->bkops_check_threshold.attr);
2961 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2962 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002963 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002964 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002965 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002966 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002967
Maya Erez64d12462013-01-06 10:19:38 +02002968 md->no_pack_for_random.show = no_pack_for_random_show;
2969 md->no_pack_for_random.store = no_pack_for_random_store;
2970 sysfs_attr_init(&md->no_pack_for_random.attr);
2971 md->no_pack_for_random.attr.name = "no_pack_for_random";
2972 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
2973 ret = device_create_file(disk_to_dev(md->disk),
2974 &md->no_pack_for_random);
2975 if (ret)
2976 goto no_pack_for_random_fails;
2977
Johan Rudholmadd710e2011-12-02 08:51:06 +01002978 return ret;
2979
Maya Erez64d12462013-01-06 10:19:38 +02002980no_pack_for_random_fails:
2981 device_remove_file(disk_to_dev(md->disk),
2982 &md->bkops_check_threshold);
Maya Ereze1eae982013-01-05 21:51:06 +02002983bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02002984 device_remove_file(disk_to_dev(md->disk),
2985 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002986num_wr_reqs_to_start_packing_fail:
2987 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002988power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002989 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002990force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002991 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002992
2993 return ret;
2994}
2995
Chris Ballc59d4472011-11-11 22:01:43 -05002996#define CID_MANFID_SANDISK 0x2
2997#define CID_MANFID_TOSHIBA 0x11
2998#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302999#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05003000
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003001static const struct mmc_fixup blk_fixups[] =
3002{
Chris Ballc59d4472011-11-11 22:01:43 -05003003 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3004 MMC_QUIRK_INAND_CMD38),
3005 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3006 MMC_QUIRK_INAND_CMD38),
3007 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3008 MMC_QUIRK_INAND_CMD38),
3009 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3010 MMC_QUIRK_INAND_CMD38),
3011 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3012 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003013
3014 /*
3015 * Some MMC cards experience performance degradation with CMD23
3016 * instead of CMD12-bounded multiblock transfers. For now we'll
3017 * black list what's bad...
3018 * - Certain Toshiba cards.
3019 *
3020 * N.B. This doesn't affect SD cards.
3021 */
Chris Ballc59d4472011-11-11 22:01:43 -05003022 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003023 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003024 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003025 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003026 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003027 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003028
3029 /*
3030 * Some Micron MMC cards needs longer data read timeout than
3031 * indicated in CSD.
3032 */
Chris Ballc59d4472011-11-11 22:01:43 -05003033 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003034 MMC_QUIRK_LONG_READ_TIME),
3035
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05303036 /* Some INAND MCP devices advertise incorrect timeout values */
3037 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3038 MMC_QUIRK_INAND_DATA_TIMEOUT),
3039
Ian Chenc4856122012-11-01 13:26:21 +05303040 /*
3041 * On these Samsung MoviNAND parts, performing secure erase or
3042 * secure trim can result in unrecoverable corruption due to a
3043 * firmware bug.
3044 */
3045 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3046 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3047 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3048 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3049 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3050 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3051 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3052 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3053 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3054 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3055 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3056 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3057 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3058 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3059 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3060 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3061
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003062 END_FIXUP
3063};
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065static int mmc_blk_probe(struct mmc_card *card)
3066{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003067 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003068 char cap_str[10];
3069
Pierre Ossman912490d2005-05-21 10:27:02 +01003070 /*
3071 * Check that the card supports the command class(es) we need.
3072 */
3073 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return -ENODEV;
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 md = mmc_blk_alloc(card);
3077 if (IS_ERR(md))
3078 return PTR_ERR(md);
3079
Yi Li444122f2009-02-05 15:31:57 +08003080 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003081 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303082 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003084 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Andrei Warkentin371a6892011-04-11 18:10:25 -05003086 if (mmc_blk_alloc_parts(card, md))
3087 goto out;
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003090 mmc_fixup_device(card, blk_fixups);
3091
San Mehat148c57a2009-07-30 08:21:19 -07003092#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3093 mmc_set_bus_resume_policy(card->host, 1);
3094#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003095 if (mmc_add_disk(md))
3096 goto out;
3097
3098 list_for_each_entry(part_md, &md->part, part) {
3099 if (mmc_add_disk(part_md))
3100 goto out;
3101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return 0;
3103
3104 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003105 mmc_blk_remove_parts(card, md);
3106 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
3110static void mmc_blk_remove(struct mmc_card *card)
3111{
3112 struct mmc_blk_data *md = mmc_get_drvdata(card);
3113
Andrei Warkentin371a6892011-04-11 18:10:25 -05003114 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003115 mmc_claim_host(card->host);
3116 mmc_blk_part_switch(card, md);
3117 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003118 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07003120#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3121 mmc_set_bus_resume_policy(card->host, 0);
3122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003125static void mmc_blk_shutdown(struct mmc_card *card)
3126{
3127 struct mmc_blk_data *part_md;
3128 struct mmc_blk_data *md = mmc_get_drvdata(card);
3129 int rc;
3130
3131 /* Silent the block layer */
3132 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303133 rc = mmc_queue_suspend(&md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003134 if (rc)
3135 goto suspend_error;
3136 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303137 rc = mmc_queue_suspend(&part_md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003138 if (rc)
3139 goto suspend_error;
3140 }
3141 }
3142
3143 /* send power off notification */
3144 if (mmc_card_mmc(card)) {
3145 mmc_rpm_hold(card->host, &card->dev);
3146 mmc_send_long_pon(card);
3147 mmc_rpm_release(card->host, &card->dev);
3148 }
3149 return;
3150
3151suspend_error:
3152 pr_err("%s: mmc_queue_suspend returned error = %d",
3153 mmc_hostname(card->host), rc);
3154}
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08003157static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003159 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 struct mmc_blk_data *md = mmc_get_drvdata(card);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303161 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303164 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303165 if (rc)
3166 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003167 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303168 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303169 if (rc)
3170 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303173 goto out;
3174
3175 out_resume:
3176 mmc_queue_resume(&md->queue);
3177 list_for_each_entry(part_md, &md->part, part) {
3178 mmc_queue_resume(&part_md->queue);
3179 }
3180 out:
3181 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
3184static int mmc_blk_resume(struct mmc_card *card)
3185{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003186 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 struct mmc_blk_data *md = mmc_get_drvdata(card);
3188
3189 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003190 /*
3191 * Resume involves the card going into idle state,
3192 * so current partition is always the main one.
3193 */
3194 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003196 list_for_each_entry(part_md, &md->part, part) {
3197 mmc_queue_resume(&part_md->queue);
3198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 return 0;
3201}
3202#else
3203#define mmc_blk_suspend NULL
3204#define mmc_blk_resume NULL
3205#endif
3206
3207static struct mmc_driver mmc_driver = {
3208 .drv = {
3209 .name = "mmcblk",
3210 },
3211 .probe = mmc_blk_probe,
3212 .remove = mmc_blk_remove,
3213 .suspend = mmc_blk_suspend,
3214 .resume = mmc_blk_resume,
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003215 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216};
3217
3218static int __init mmc_blk_init(void)
3219{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003220 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003222 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3223 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3224
3225 max_devices = 256 / perdev_minors;
3226
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003227 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3228 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003231 res = mmc_register_driver(&mmc_driver);
3232 if (res)
3233 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003235 return 0;
3236 out2:
3237 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 out:
3239 return res;
3240}
3241
3242static void __exit mmc_blk_exit(void)
3243{
3244 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003245 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
3248module_init(mmc_blk_init);
3249module_exit(mmc_blk_exit);
3250
3251MODULE_LICENSE("GPL");
3252MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3253