blob: 3eedc3267f427d3f6fb67c9a5cda0ae06d461c15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Asutosh Dasbbc84782013-02-11 15:31:35 +053037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
John Calixtocb87ea22011-04-26 18:56:29 -040039#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020041#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010042#include <linux/mmc/mmc.h>
43#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040050#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010054
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050055#define INAND_CMD38_ARG_EXT_CSD 113
56#define INAND_CMD38_ARG_ERASE 0x00
57#define INAND_CMD38_ARG_TRIM 0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani914fa652013-01-07 17:31:43 +053061#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050062
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030063#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
64
Seungwon Jeon53f8f572012-09-27 15:00:26 +020065#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68#define PACKED_CMD_VER 0x01
69#define PACKED_CMD_WR 0x02
Lee Susman2e3c0642013-04-23 17:59:26 +030070#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020071#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
72 do { \
73 if (stats->enabled) \
74 stats->pack_stop_reason[reason]++; \
75 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020076
Lee Susman2e3c0642013-04-23 17:59:26 +030077#define PCKD_TRGR_INIT_MEAN_POTEN 17
78#define PCKD_TRGR_POTEN_LOWER_BOUND 5
79#define PCKD_TRGR_URGENT_PENALTY 2
80#define PCKD_TRGR_LOWER_BOUND 5
81#define PCKD_TRGR_PRECISION_MULTIPLIER 100
82
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020083static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084
85/*
86 * The defaults come from config options but can be overriden by module
87 * or bootarg options.
88 */
89static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
90
91/*
92 * We've only got one major, so number of mmcblk devices is
93 * limited to 256 / number of minors per device.
94 */
95static int max_devices;
96
97/* 256 minors, so at most 256 separate devices */
98static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050099static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * There is one mmc_blk_data per slot.
103 */
104struct mmc_blk_data {
105 spinlock_t lock;
106 struct gendisk *disk;
107 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500110 unsigned int flags;
111#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000115 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500116 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500117 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300118 unsigned int reset_done;
119#define MMC_BLK_READ BIT(0)
120#define MMC_BLK_WRITE BIT(1)
121#define MMC_BLK_DISCARD BIT(2)
122#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500123
124 /*
125 * Only set in main mmc_blk_data associated
126 * with mmc_card with mmc_set_drvdata, and keeps
127 * track of the current selected device partition.
128 */
129 unsigned int part_curr;
130 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100131 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200132 struct device_attribute num_wr_reqs_to_start_packing;
Maya Ereze1eae982013-01-05 21:51:06 +0200133 struct device_attribute bkops_check_threshold;
Maya Erez64d12462013-01-06 10:19:38 +0200134 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100135 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Arjan van de Vena621aae2006-01-12 18:43:35 +0000138static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200140enum {
141 MMC_PACKED_N_IDX = -1,
142 MMC_PACKED_N_ZERO,
143 MMC_PACKED_N_SINGLE,
144};
145
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400146module_param(perdev_minors, int, 0444);
147MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
148
Loic Pallardy961e37a2012-08-06 15:12:31 +0000149static inline int mmc_blk_part_switch(struct mmc_card *card,
150 struct mmc_blk_data *md);
151static int get_card_status(struct mmc_card *card, u32 *status, int retries);
152
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200153static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
154{
155 mqrq->packed_cmd = MMC_PACKED_NONE;
156 mqrq->packed_num = MMC_PACKED_N_ZERO;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
160{
161 struct mmc_blk_data *md;
162
Arjan van de Vena621aae2006-01-12 18:43:35 +0000163 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 md = disk->private_data;
165 if (md && md->usage == 0)
166 md = NULL;
167 if (md)
168 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000169 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return md;
172}
173
Andrei Warkentin371a6892011-04-11 18:10:25 -0500174static inline int mmc_get_devidx(struct gendisk *disk)
175{
Colin Cross71b54d42010-09-03 12:41:21 -0700176 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500177 return devidx;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void mmc_blk_put(struct mmc_blk_data *md)
181{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000182 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 md->usage--;
184 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500185 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800186 blk_cleanup_queue(md->queue.queue);
187
David Woodhouse1dff3142007-11-21 18:45:12 +0100188 __clear_bit(devidx, dev_use);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(md);
192 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000193 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Johan Rudholmadd710e2011-12-02 08:51:06 +0100196static ssize_t power_ro_lock_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
199 int ret;
200 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
201 struct mmc_card *card = md->queue.card;
202 int locked = 0;
203
204 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
205 locked = 2;
206 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
207 locked = 1;
208
209 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
210
211 return ret;
212}
213
214static ssize_t power_ro_lock_store(struct device *dev,
215 struct device_attribute *attr, const char *buf, size_t count)
216{
217 int ret;
218 struct mmc_blk_data *md, *part_md;
219 struct mmc_card *card;
220 unsigned long set;
221
222 if (kstrtoul(buf, 0, &set))
223 return -EINVAL;
224
225 if (set != 1)
226 return count;
227
228 md = mmc_blk_get(dev_to_disk(dev));
229 card = md->queue.card;
230
Sahitya Tummala22459132013-05-01 11:14:51 +0530231 mmc_rpm_hold(card->host, &card->dev);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100232 mmc_claim_host(card->host);
233
234 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
235 card->ext_csd.boot_ro_lock |
236 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
237 card->ext_csd.part_time);
238 if (ret)
239 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
240 else
241 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
242
243 mmc_release_host(card->host);
Sahitya Tummala22459132013-05-01 11:14:51 +0530244 mmc_rpm_release(card->host, &card->dev);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100245
246 if (!ret) {
247 pr_info("%s: Locking boot partition ro until next power on\n",
248 md->disk->disk_name);
249 set_disk_ro(md->disk, 1);
250
251 list_for_each_entry(part_md, &md->part, part)
252 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
253 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
254 set_disk_ro(part_md->disk, 1);
255 }
256 }
257
258 mmc_blk_put(md);
259 return count;
260}
261
Andrei Warkentin371a6892011-04-11 18:10:25 -0500262static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
263 char *buf)
264{
265 int ret;
266 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
267
268 ret = snprintf(buf, PAGE_SIZE, "%d",
269 get_disk_ro(dev_to_disk(dev)) ^
270 md->read_only);
271 mmc_blk_put(md);
272 return ret;
273}
274
275static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
277{
278 int ret;
279 char *end;
280 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
281 unsigned long set = simple_strtoul(buf, &end, 0);
282 if (end == buf) {
283 ret = -EINVAL;
284 goto out;
285 }
286
287 set_disk_ro(dev_to_disk(dev), set || md->read_only);
288 ret = count;
289out:
290 mmc_blk_put(md);
291 return ret;
292}
293
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200294static ssize_t
295num_wr_reqs_to_start_packing_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
299 int num_wr_reqs_to_start_packing;
300 int ret;
301
302 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
303
304 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
305
306 mmc_blk_put(md);
307 return ret;
308}
309
310static ssize_t
311num_wr_reqs_to_start_packing_store(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf, size_t count)
314{
315 int value;
316 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200317 struct mmc_card *card = md->queue.card;
318 int ret = count;
319
320 if (!card) {
321 ret = -EINVAL;
322 goto exit;
323 }
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200324
325 sscanf(buf, "%d", &value);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200326
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200327 if (value >= 0) {
328 md->queue.num_wr_reqs_to_start_packing =
329 min_t(int, value, (int)card->ext_csd.max_packed_writes);
330
331 pr_debug("%s: trigger to pack: new value = %d",
332 mmc_hostname(card->host),
333 md->queue.num_wr_reqs_to_start_packing);
334 } else {
335 pr_err("%s: value %d is not valid. old value remains = %d",
336 mmc_hostname(card->host), value,
337 md->queue.num_wr_reqs_to_start_packing);
338 ret = -EINVAL;
339 }
340
341exit:
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200342 mmc_blk_put(md);
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200343 return ret;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200344}
345
Maya Erez2d33a192013-01-04 15:52:41 +0200346static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200347bkops_check_threshold_show(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200348 struct device_attribute *attr, char *buf)
349{
350 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Maya Erez2d33a192013-01-04 15:52:41 +0200351 struct mmc_card *card = md->queue.card;
352 int ret;
353
354 if (!card)
355 ret = -EINVAL;
Maya Ereze1eae982013-01-05 21:51:06 +0200356 else
Maya Erez2d33a192013-01-04 15:52:41 +0200357 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Maya Ereze1eae982013-01-05 21:51:06 +0200358 card->bkops_info.size_percentage_to_queue_delayed_work);
Maya Erez2d33a192013-01-04 15:52:41 +0200359
360 mmc_blk_put(md);
361 return ret;
362}
363
364static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200365bkops_check_threshold_store(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200366 struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 int value;
370 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
371 struct mmc_card *card = md->queue.card;
Maya Ereze1eae982013-01-05 21:51:06 +0200372 unsigned int card_size;
373 int ret = count;
Maya Erez2d33a192013-01-04 15:52:41 +0200374
Maya Ereze1eae982013-01-05 21:51:06 +0200375 if (!card) {
376 ret = -EINVAL;
377 goto exit;
378 }
Maya Erez2d33a192013-01-04 15:52:41 +0200379
380 sscanf(buf, "%d", &value);
Maya Ereze1eae982013-01-05 21:51:06 +0200381 if ((value <= 0) || (value >= 100)) {
382 ret = -EINVAL;
383 goto exit;
384 }
Maya Erez2d33a192013-01-04 15:52:41 +0200385
Maya Ereze1eae982013-01-05 21:51:06 +0200386 card_size = (unsigned int)get_capacity(md->disk);
387 if (card_size <= 0) {
388 ret = -EINVAL;
389 goto exit;
390 }
391 card->bkops_info.size_percentage_to_queue_delayed_work = value;
392 card->bkops_info.min_sectors_to_queue_delayed_work =
393 (card_size * value) / 100;
394
395 pr_debug("%s: size_percentage = %d, min_sectors = %d",
396 mmc_hostname(card->host),
397 card->bkops_info.size_percentage_to_queue_delayed_work,
398 card->bkops_info.min_sectors_to_queue_delayed_work);
399
400exit:
Maya Erez2d33a192013-01-04 15:52:41 +0200401 mmc_blk_put(md);
402 return count;
403}
404
Maya Erez64d12462013-01-06 10:19:38 +0200405static ssize_t
406no_pack_for_random_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
410 int ret;
411
412 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
413
414 mmc_blk_put(md);
415 return ret;
416}
417
418static ssize_t
419no_pack_for_random_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
422{
423 int value;
424 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
425 struct mmc_card *card = md->queue.card;
426 int ret = count;
427
428 if (!card) {
429 ret = -EINVAL;
430 goto exit;
431 }
432
433 sscanf(buf, "%d", &value);
434
435 if (value < 0) {
436 pr_err("%s: value %d is not valid. old value remains = %d",
437 mmc_hostname(card->host), value,
438 md->queue.no_pack_for_random);
439 ret = -EINVAL;
440 goto exit;
441 }
442
443 md->queue.no_pack_for_random = (value > 0) ? true : false;
444
445 pr_debug("%s: no_pack_for_random: new value = %d",
446 mmc_hostname(card->host),
447 md->queue.no_pack_for_random);
448
449exit:
450 mmc_blk_put(md);
451 return ret;
452}
453
Al Viroa5a15612008-03-02 10:33:30 -0500454static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Al Viroa5a15612008-03-02 10:33:30 -0500456 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int ret = -ENXIO;
458
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200459 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (md) {
461 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500462 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700464
Al Viroa5a15612008-03-02 10:33:30 -0500465 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700466 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700467 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200470 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return ret;
473}
474
Al Viroa5a15612008-03-02 10:33:30 -0500475static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Al Viroa5a15612008-03-02 10:33:30 -0500477 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200479 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200481 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
485static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800486mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800488 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
489 geo->heads = 4;
490 geo->sectors = 16;
491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
John Calixtocb87ea22011-04-26 18:56:29 -0400494struct mmc_blk_ioc_data {
495 struct mmc_ioc_cmd ic;
496 unsigned char *buf;
497 u64 buf_bytes;
498};
499
500static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
501 struct mmc_ioc_cmd __user *user)
502{
503 struct mmc_blk_ioc_data *idata;
504 int err;
505
506 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
507 if (!idata) {
508 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400509 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400510 }
511
512 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
513 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400514 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400515 }
516
517 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
518 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
519 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400520 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400521 }
522
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100523 if (!idata->buf_bytes)
524 return idata;
525
John Calixtocb87ea22011-04-26 18:56:29 -0400526 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
527 if (!idata->buf) {
528 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400529 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400530 }
531
532 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
533 idata->ic.data_ptr, idata->buf_bytes)) {
534 err = -EFAULT;
535 goto copy_err;
536 }
537
538 return idata;
539
540copy_err:
541 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400542idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400543 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400544out:
John Calixtocb87ea22011-04-26 18:56:29 -0400545 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400546}
547
Loic Pallardy961e37a2012-08-06 15:12:31 +0000548static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
549 u32 retries_max)
550{
551 int err;
552 u32 retry_count = 0;
553
554 if (!status || !retries_max)
555 return -EINVAL;
556
557 do {
558 err = get_card_status(card, status, 5);
559 if (err)
560 break;
561
562 if (!R1_STATUS(*status) &&
563 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
564 break; /* RPMB programming operation complete */
565
566 /*
567 * Rechedule to give the MMC device a chance to continue
568 * processing the previous command without being polled too
569 * frequently.
570 */
571 usleep_range(1000, 5000);
572 } while (++retry_count < retries_max);
573
574 if (retry_count == retries_max)
575 err = -EPERM;
576
577 return err;
578}
579
John Calixtocb87ea22011-04-26 18:56:29 -0400580static int mmc_blk_ioctl_cmd(struct block_device *bdev,
581 struct mmc_ioc_cmd __user *ic_ptr)
582{
583 struct mmc_blk_ioc_data *idata;
584 struct mmc_blk_data *md;
585 struct mmc_card *card;
586 struct mmc_command cmd = {0};
587 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530588 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400589 struct scatterlist sg;
590 int err;
591
592 /*
593 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
594 * whole block device, not on a partition. This prevents overspray
595 * between sibling partitions.
596 */
597 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
598 return -EPERM;
599
600 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
601 if (IS_ERR(idata))
602 return PTR_ERR(idata);
603
John Calixtocb87ea22011-04-26 18:56:29 -0400604 md = mmc_blk_get(bdev->bd_disk);
605 if (!md) {
606 err = -EINVAL;
Pratibhasagar V386c1b72013-04-26 18:59:55 +0530607 goto blk_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400608 }
609
610 card = md->queue.card;
611 if (IS_ERR(card)) {
612 err = PTR_ERR(card);
613 goto cmd_done;
614 }
615
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100616 cmd.opcode = idata->ic.opcode;
617 cmd.arg = idata->ic.arg;
618 cmd.flags = idata->ic.flags;
619
620 if (idata->buf_bytes) {
621 data.sg = &sg;
622 data.sg_len = 1;
623 data.blksz = idata->ic.blksz;
624 data.blocks = idata->ic.blocks;
625
626 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
627
628 if (idata->ic.write_flag)
629 data.flags = MMC_DATA_WRITE;
630 else
631 data.flags = MMC_DATA_READ;
632
633 /* data.flags must already be set before doing this. */
634 mmc_set_data_timeout(&data, card);
635
636 /* Allow overriding the timeout_ns for empirical tuning. */
637 if (idata->ic.data_timeout_ns)
638 data.timeout_ns = idata->ic.data_timeout_ns;
639
640 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
641 /*
642 * Pretend this is a data transfer and rely on the
643 * host driver to compute timeout. When all host
644 * drivers support cmd.cmd_timeout for R1B, this
645 * can be changed to:
646 *
647 * mrq.data = NULL;
648 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
649 */
650 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
651 }
652
653 mrq.data = &data;
654 }
655
656 mrq.cmd = &cmd;
657
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530658 mmc_rpm_hold(card->host, &card->dev);
John Calixtocb87ea22011-04-26 18:56:29 -0400659 mmc_claim_host(card->host);
660
Loic Pallardy961e37a2012-08-06 15:12:31 +0000661 err = mmc_blk_part_switch(card, md);
662 if (err)
663 goto cmd_rel_host;
664
John Calixtocb87ea22011-04-26 18:56:29 -0400665 if (idata->ic.is_acmd) {
666 err = mmc_app_cmd(card->host, card);
667 if (err)
668 goto cmd_rel_host;
669 }
670
John Calixtocb87ea22011-04-26 18:56:29 -0400671 mmc_wait_for_req(card->host, &mrq);
672
673 if (cmd.error) {
674 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
675 __func__, cmd.error);
676 err = cmd.error;
677 goto cmd_rel_host;
678 }
679 if (data.error) {
680 dev_err(mmc_dev(card->host), "%s: data error %d\n",
681 __func__, data.error);
682 err = data.error;
683 goto cmd_rel_host;
684 }
685
686 /*
687 * According to the SD specs, some commands require a delay after
688 * issuing the command.
689 */
690 if (idata->ic.postsleep_min_us)
691 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
692
693 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
694 err = -EFAULT;
695 goto cmd_rel_host;
696 }
697
698 if (!idata->ic.write_flag) {
699 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
700 idata->buf, idata->buf_bytes)) {
701 err = -EFAULT;
702 goto cmd_rel_host;
703 }
704 }
705
Krishna Kondabfa7df92012-11-20 20:26:05 -0800706cmd_rel_host:
707 mmc_release_host(card->host);
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530708 mmc_rpm_release(card->host, &card->dev);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800709
710cmd_done:
711 mmc_blk_put(md);
Pratibhasagar V386c1b72013-04-26 18:59:55 +0530712blk_err:
Krishna Kondabfa7df92012-11-20 20:26:05 -0800713 kfree(idata->buf);
714 kfree(idata);
715 return err;
716}
717
718struct mmc_blk_ioc_rpmb_data {
719 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
720};
721
722static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
723 struct mmc_ioc_rpmb __user *user)
724{
725 struct mmc_blk_ioc_rpmb_data *idata;
726 int err, i;
727
728 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
729 if (!idata) {
730 err = -ENOMEM;
731 goto out;
732 }
733
734 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
735 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
736 if (IS_ERR(idata->data[i])) {
737 err = PTR_ERR(idata->data[i]);
738 goto copy_err;
739 }
740 }
741
742 return idata;
743
744copy_err:
745 while (--i >= 0) {
746 kfree(idata->data[i]->buf);
747 kfree(idata->data[i]);
748 }
749 kfree(idata);
750out:
751 return ERR_PTR(err);
752}
753
754static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
755 struct mmc_ioc_rpmb __user *ic_ptr)
756{
757 struct mmc_blk_ioc_rpmb_data *idata;
758 struct mmc_blk_data *md;
759 struct mmc_card *card;
760 struct mmc_command cmd = {0};
761 struct mmc_data data = {0};
762 struct mmc_request mrq = {NULL};
763 struct scatterlist sg;
764 int err = 0, i = 0;
765 u32 status = 0;
766
767 /* The caller must have CAP_SYS_RAWIO */
768 if (!capable(CAP_SYS_RAWIO))
769 return -EPERM;
770
771 md = mmc_blk_get(bdev->bd_disk);
772 /* make sure this is a rpmb partition */
773 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
774 err = -EINVAL;
775 goto cmd_done;
776 }
777
778 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
779 if (IS_ERR(idata)) {
780 err = PTR_ERR(idata);
781 goto cmd_done;
782 }
783
784 card = md->queue.card;
785 if (IS_ERR(card)) {
786 err = PTR_ERR(card);
787 goto idata_free;
788 }
789
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530790 mmc_rpm_hold(card->host, &card->dev);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800791 mmc_claim_host(card->host);
792
793 err = mmc_blk_part_switch(card, md);
794 if (err)
795 goto cmd_rel_host;
796
797 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
798 struct mmc_blk_ioc_data *curr_data;
799 struct mmc_ioc_cmd *curr_cmd;
800
801 curr_data = idata->data[i];
802 curr_cmd = &curr_data->ic;
803 if (!curr_cmd->opcode)
804 break;
805
806 cmd.opcode = curr_cmd->opcode;
807 cmd.arg = curr_cmd->arg;
808 cmd.flags = curr_cmd->flags;
809
810 if (curr_data->buf_bytes) {
811 data.sg = &sg;
812 data.sg_len = 1;
813 data.blksz = curr_cmd->blksz;
814 data.blocks = curr_cmd->blocks;
815
816 sg_init_one(data.sg, curr_data->buf,
817 curr_data->buf_bytes);
818
819 if (curr_cmd->write_flag)
820 data.flags = MMC_DATA_WRITE;
821 else
822 data.flags = MMC_DATA_READ;
823
824 /* data.flags must already be set before doing this. */
825 mmc_set_data_timeout(&data, card);
826
827 /*
828 * Allow overriding the timeout_ns for empirical tuning.
829 */
830 if (curr_cmd->data_timeout_ns)
831 data.timeout_ns = curr_cmd->data_timeout_ns;
832
833 mrq.data = &data;
834 }
835
836 mrq.cmd = &cmd;
837
838 err = mmc_set_blockcount(card, data.blocks,
839 curr_cmd->write_flag & (1 << 31));
840 if (err)
841 goto cmd_rel_host;
842
843 mmc_wait_for_req(card->host, &mrq);
844
845 if (cmd.error) {
846 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
847 __func__, cmd.error);
848 err = cmd.error;
849 goto cmd_rel_host;
850 }
851 if (data.error) {
852 dev_err(mmc_dev(card->host), "%s: data error %d\n",
853 __func__, data.error);
854 err = data.error;
855 goto cmd_rel_host;
856 }
857
858 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
859 sizeof(cmd.resp))) {
860 err = -EFAULT;
861 goto cmd_rel_host;
862 }
863
864 if (!curr_cmd->write_flag) {
865 if (copy_to_user((void __user *)(unsigned long)
866 curr_cmd->data_ptr,
867 curr_data->buf,
868 curr_data->buf_bytes)) {
869 err = -EFAULT;
870 goto cmd_rel_host;
871 }
872 }
873
Loic Pallardy961e37a2012-08-06 15:12:31 +0000874 /*
875 * Ensure RPMB command has completed by polling CMD13
876 * "Send Status".
877 */
878 err = ioctl_rpmb_card_status_poll(card, &status, 5);
879 if (err)
880 dev_err(mmc_dev(card->host),
881 "%s: Card Status=0x%08X, error %d\n",
882 __func__, status, err);
883 }
884
John Calixtocb87ea22011-04-26 18:56:29 -0400885cmd_rel_host:
886 mmc_release_host(card->host);
Sahitya Tummala36692ab2013-04-10 10:15:09 +0530887 mmc_rpm_release(card->host, &card->dev);
John Calixtocb87ea22011-04-26 18:56:29 -0400888
Krishna Kondabfa7df92012-11-20 20:26:05 -0800889idata_free:
890 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
891 kfree(idata->data[i]->buf);
892 kfree(idata->data[i]);
893 }
894 kfree(idata);
895
John Calixtocb87ea22011-04-26 18:56:29 -0400896cmd_done:
897 mmc_blk_put(md);
John Calixtocb87ea22011-04-26 18:56:29 -0400898 return err;
899}
900
901static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
902 unsigned int cmd, unsigned long arg)
903{
904 int ret = -EINVAL;
905 if (cmd == MMC_IOC_CMD)
906 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800907 if (cmd == MMC_IOC_RPMB_CMD)
908 ret = mmc_blk_ioctl_rpmb_cmd(bdev,
909 (struct mmc_ioc_rpmb __user *)arg);
John Calixtocb87ea22011-04-26 18:56:29 -0400910 return ret;
911}
912
913#ifdef CONFIG_COMPAT
914static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
915 unsigned int cmd, unsigned long arg)
916{
917 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
918}
919#endif
920
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700921static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500922 .open = mmc_blk_open,
923 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800924 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400926 .ioctl = mmc_blk_ioctl,
927#ifdef CONFIG_COMPAT
928 .compat_ioctl = mmc_blk_compat_ioctl,
929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
Andrei Warkentin371a6892011-04-11 18:10:25 -0500932static inline int mmc_blk_part_switch(struct mmc_card *card,
933 struct mmc_blk_data *md)
934{
935 int ret;
936 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300937
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800938 if ((main_md->part_curr == md->part_type) &&
939 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -0500940 return 0;
941
942 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300943 u8 part_config = card->ext_csd.part_config;
944
945 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
946 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500947
948 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300949 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500950 card->ext_csd.part_time);
951 if (ret)
952 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300953
954 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800955 card->part_curr = md->part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300956 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500957
958 main_md->part_curr = md->part_type;
959 return 0;
960}
961
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700962static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
963{
964 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100965 u32 result;
966 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700967
Venkatraman Sad5fd972011-08-25 00:30:50 +0530968 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400969 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400970 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700971
972 struct scatterlist sg;
973
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700974 cmd.opcode = MMC_APP_CMD;
975 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700976 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700977
978 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700979 if (err)
980 return (u32)-1;
981 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700982 return (u32)-1;
983
984 memset(&cmd, 0, sizeof(struct mmc_command));
985
986 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
987 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700988 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700989
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700990 data.blksz = 4;
991 data.blocks = 1;
992 data.flags = MMC_DATA_READ;
993 data.sg = &sg;
994 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530995 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700996
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700997 mrq.cmd = &cmd;
998 mrq.data = &data;
999
Ben Dooks051913d2009-06-08 23:33:57 +01001000 blocks = kmalloc(4, GFP_KERNEL);
1001 if (!blocks)
1002 return (u32)-1;
1003
1004 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001005
1006 mmc_wait_for_req(card->host, &mrq);
1007
Ben Dooks051913d2009-06-08 23:33:57 +01001008 result = ntohl(*blocks);
1009 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001010
Ben Dooks051913d2009-06-08 23:33:57 +01001011 if (cmd.error || data.error)
1012 result = (u32)-1;
1013
1014 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001015}
1016
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001017static int send_stop(struct mmc_card *card, u32 *status)
1018{
1019 struct mmc_command cmd = {0};
1020 int err;
1021
1022 cmd.opcode = MMC_STOP_TRANSMISSION;
1023 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1024 err = mmc_wait_for_cmd(card->host, &cmd, 5);
1025 if (err == 0)
1026 *status = cmd.resp[0];
1027 return err;
1028}
1029
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001030static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001031{
Chris Ball1278dba2011-04-13 23:40:30 -04001032 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001033 int err;
1034
Adrian Hunter504f1912008-10-16 12:55:25 +03001035 cmd.opcode = MMC_SEND_STATUS;
1036 if (!mmc_host_is_spi(card->host))
1037 cmd.arg = card->rca << 16;
1038 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001039 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1040 if (err == 0)
1041 *status = cmd.resp[0];
1042 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001043}
1044
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301045#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001046#define ERR_RETRY 2
1047#define ERR_ABORT 1
1048#define ERR_CONTINUE 0
1049
1050static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1051 bool status_valid, u32 status)
1052{
1053 switch (error) {
1054 case -EILSEQ:
1055 /* response crc error, retry the r/w cmd */
1056 pr_err("%s: %s sending %s command, card status %#x\n",
1057 req->rq_disk->disk_name, "response CRC error",
1058 name, status);
1059 return ERR_RETRY;
1060
1061 case -ETIMEDOUT:
1062 pr_err("%s: %s sending %s command, card status %#x\n",
1063 req->rq_disk->disk_name, "timed out", name, status);
1064
1065 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001066 if (!status_valid) {
1067 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001068 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001069 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001070 /*
1071 * If it was a r/w cmd crc error, or illegal command
1072 * (eg, issued in wrong state) then retry - we should
1073 * have corrected the state problem above.
1074 */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001075 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1076 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001077 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001078 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001079
1080 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001081 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001082 return ERR_ABORT;
1083
1084 default:
1085 /* We don't understand the error code the driver gave us */
1086 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1087 req->rq_disk->disk_name, error, status);
1088 return ERR_ABORT;
1089 }
1090}
1091
1092/*
1093 * Initial r/w and stop cmd error recovery.
1094 * We don't know whether the card received the r/w cmd or not, so try to
1095 * restore things back to a sane state. Essentially, we do this as follows:
1096 * - Obtain card status. If the first attempt to obtain card status fails,
1097 * the status word will reflect the failed status cmd, not the failed
1098 * r/w cmd. If we fail to obtain card status, it suggests we can no
1099 * longer communicate with the card.
1100 * - Check the card state. If the card received the cmd but there was a
1101 * transient problem with the response, it might still be in a data transfer
1102 * mode. Try to send it a stop command. If this fails, we can't recover.
1103 * - If the r/w cmd failed due to a response CRC error, it was probably
1104 * transient, so retry the cmd.
1105 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1106 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1107 * illegal cmd, retry.
1108 * Otherwise we don't understand what happened, so abort.
1109 */
1110static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +03001111 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001112{
1113 bool prev_cmd_status_valid = true;
1114 u32 status, stop_status = 0;
1115 int err, retry;
1116
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301117 if (mmc_card_removed(card))
1118 return ERR_NOMEDIUM;
1119
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001120 /*
1121 * Try to get card status which indicates both the card state
1122 * and why there was no response. If the first attempt fails,
1123 * we can't be sure the returned status is for the r/w command.
1124 */
1125 for (retry = 2; retry >= 0; retry--) {
1126 err = get_card_status(card, &status, 0);
1127 if (!err)
1128 break;
1129
1130 prev_cmd_status_valid = false;
1131 pr_err("%s: error %d sending status command, %sing\n",
1132 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1133 }
1134
1135 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301136 if (err) {
1137 /* Check if the card is removed */
1138 if (mmc_detect_card_removed(card->host))
1139 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001140 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301141 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001142
Adrian Hunter67716322011-08-29 16:42:15 +03001143 /* Flag ECC errors */
1144 if ((status & R1_CARD_ECC_FAILED) ||
1145 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1146 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1147 *ecc_err = 1;
1148
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001149 /*
1150 * Check the current card state. If it is in some data transfer
1151 * mode, tell it to stop (and hopefully transition back to TRAN.)
1152 */
1153 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1154 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1155 err = send_stop(card, &stop_status);
1156 if (err)
1157 pr_err("%s: error %d sending stop command\n",
1158 req->rq_disk->disk_name, err);
1159
1160 /*
1161 * If the stop cmd also timed out, the card is probably
1162 * not present, so abort. Other errors are bad news too.
1163 */
1164 if (err)
1165 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +03001166 if (stop_status & R1_CARD_ECC_FAILED)
1167 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001168 }
1169
1170 /* Check for set block count errors */
1171 if (brq->sbc.error)
1172 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1173 prev_cmd_status_valid, status);
1174
1175 /* Check for r/w command errors */
1176 if (brq->cmd.error)
1177 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1178 prev_cmd_status_valid, status);
1179
Adrian Hunter67716322011-08-29 16:42:15 +03001180 /* Data errors */
1181 if (!brq->stop.error)
1182 return ERR_CONTINUE;
1183
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001184 /* Now for stop errors. These aren't fatal to the transfer. */
1185 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1186 req->rq_disk->disk_name, brq->stop.error,
1187 brq->cmd.resp[0], status);
1188
1189 /*
1190 * Subsitute in our own stop status as this will give the error
1191 * state which happened during the execution of the r/w command.
1192 */
1193 if (stop_status) {
1194 brq->stop.resp[0] = stop_status;
1195 brq->stop.error = 0;
1196 }
1197 return ERR_CONTINUE;
1198}
1199
Adrian Hunter67716322011-08-29 16:42:15 +03001200static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1201 int type)
1202{
1203 int err;
1204
1205 if (md->reset_done & type)
1206 return -EEXIST;
1207
1208 md->reset_done |= type;
1209 err = mmc_hw_reset(host);
1210 /* Ensure we switch back to the correct partition */
1211 if (err != -EOPNOTSUPP) {
1212 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
1213 int part_err;
1214
1215 main_md->part_curr = main_md->part_type;
1216 part_err = mmc_blk_part_switch(host->card, md);
1217 if (part_err) {
1218 /*
1219 * We have failed to get back into the correct
1220 * partition, so we need to abort the whole request.
1221 */
1222 return -ENODEV;
1223 }
1224 }
1225 return err;
1226}
1227
1228static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1229{
1230 md->reset_done &= ~type;
1231}
1232
Adrian Hunterbd788c92010-08-11 14:17:47 -07001233static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1234{
1235 struct mmc_blk_data *md = mq->data;
1236 struct mmc_card *card = md->queue.card;
1237 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001238 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001239
Adrian Hunterbd788c92010-08-11 14:17:47 -07001240 if (!mmc_can_erase(card)) {
1241 err = -EOPNOTSUPP;
1242 goto out;
1243 }
1244
1245 from = blk_rq_pos(req);
1246 nr = blk_rq_sectors(req);
1247
Maya Erez2d33a192013-01-04 15:52:41 +02001248 if (card->ext_csd.bkops_en)
1249 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1250
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001251 if (mmc_can_discard(card))
1252 arg = MMC_DISCARD_ARG;
1253 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001254 arg = MMC_TRIM_ARG;
1255 else
1256 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001257retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001258 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1259 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1260 INAND_CMD38_ARG_EXT_CSD,
1261 arg == MMC_TRIM_ARG ?
1262 INAND_CMD38_ARG_TRIM :
1263 INAND_CMD38_ARG_ERASE,
1264 0);
1265 if (err)
1266 goto out;
1267 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001268 err = mmc_erase(card, from, nr, arg);
1269out:
Adrian Hunter67716322011-08-29 16:42:15 +03001270 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1271 goto retry;
1272 if (!err)
1273 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301274 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001275
Adrian Hunterbd788c92010-08-11 14:17:47 -07001276 return err ? 0 : 1;
1277}
1278
Adrian Hunter49804542010-08-11 14:17:50 -07001279static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1280 struct request *req)
1281{
1282 struct mmc_blk_data *md = mq->data;
1283 struct mmc_card *card = md->queue.card;
1284 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001285 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001286
Maya Erez463bb952012-05-24 23:46:29 +03001287 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001288 err = -EOPNOTSUPP;
1289 goto out;
1290 }
1291
1292 from = blk_rq_pos(req);
1293 nr = blk_rq_sectors(req);
1294
1295 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1296 arg = MMC_SECURE_TRIM1_ARG;
1297 else
1298 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001299retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001300 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1301 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1302 INAND_CMD38_ARG_EXT_CSD,
1303 arg == MMC_SECURE_TRIM1_ARG ?
1304 INAND_CMD38_ARG_SECTRIM1 :
1305 INAND_CMD38_ARG_SECERASE,
1306 0);
1307 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001308 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001309 }
Adrian Hunter28302812012-04-05 14:45:48 +03001310
Adrian Hunter49804542010-08-11 14:17:50 -07001311 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001312 if (err == -EIO)
1313 goto out_retry;
1314 if (err)
1315 goto out;
1316
1317 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001318 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1319 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1320 INAND_CMD38_ARG_EXT_CSD,
1321 INAND_CMD38_ARG_SECTRIM2,
1322 0);
1323 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001324 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001325 }
Adrian Hunter28302812012-04-05 14:45:48 +03001326
Adrian Hunter49804542010-08-11 14:17:50 -07001327 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001328 if (err == -EIO)
1329 goto out_retry;
1330 if (err)
1331 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001332 }
Adrian Hunter28302812012-04-05 14:45:48 +03001333
Adrian Hunter28302812012-04-05 14:45:48 +03001334out_retry:
1335 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001336 goto retry;
1337 if (!err)
1338 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001339out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301340 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001341
Adrian Hunter49804542010-08-11 14:17:50 -07001342 return err ? 0 : 1;
1343}
1344
Maya Erez463bb952012-05-24 23:46:29 +03001345static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1346 struct request *req)
1347{
1348 struct mmc_blk_data *md = mq->data;
1349 struct mmc_card *card = md->queue.card;
1350 int err = 0;
1351
1352 BUG_ON(!card);
1353 BUG_ON(!card->host);
1354
1355 if (!(mmc_can_sanitize(card) &&
1356 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1357 pr_warning("%s: %s - SANITIZE is not supported\n",
1358 mmc_hostname(card->host), __func__);
1359 err = -EOPNOTSUPP;
1360 goto out;
1361 }
1362
1363 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1364 mmc_hostname(card->host), __func__);
1365
Subhash Jadavanid90d00c2013-06-25 13:13:24 +05301366 err = mmc_switch_ignore_timeout(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001367 EXT_CSD_SANITIZE_START, 1,
1368 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001369
1370 if (err)
1371 pr_err("%s: %s - mmc_switch() with "
1372 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1373 mmc_hostname(card->host), __func__, err);
1374
1375 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1376 __func__);
1377
1378out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301379 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001380
1381 return err ? 0 : 1;
1382}
1383
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001384static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1385{
1386 struct mmc_blk_data *md = mq->data;
Maya Ereza9a11612013-09-28 22:25:53 +03001387 struct request_queue *q = mq->queue;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001388 struct mmc_card *card = md->queue.card;
1389 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001390
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001391 ret = mmc_flush_cache(card);
Maya Ereza9a11612013-09-28 22:25:53 +03001392 if (ret == -ETIMEDOUT) {
1393 pr_info("%s: requeue flush request after timeout", __func__);
1394 spin_lock_irq(q->queue_lock);
1395 blk_requeue_request(q, req);
1396 spin_unlock_irq(q->queue_lock);
1397 ret = 0;
1398 goto exit;
1399 } else if (ret) {
1400 pr_err("%s: notify flush error to upper layers", __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001401 ret = -EIO;
Maya Erezd0daec42013-09-28 22:23:37 +03001402 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001403
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301404 blk_end_request_all(req, ret);
Maya Ereza9a11612013-09-28 22:25:53 +03001405exit:
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001406 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001407}
1408
1409/*
1410 * Reformat current write as a reliable write, supporting
1411 * both legacy and the enhanced reliable write MMC cards.
1412 * In each transfer we'll handle only as much as a single
1413 * reliable write can handle, thus finish the request in
1414 * partial completions.
1415 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001416static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1417 struct mmc_card *card,
1418 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001419{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001420 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1421 /* Legacy mode imposes restrictions on transfers. */
1422 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1423 brq->data.blocks = 1;
1424
1425 if (brq->data.blocks > card->ext_csd.rel_sectors)
1426 brq->data.blocks = card->ext_csd.rel_sectors;
1427 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1428 brq->data.blocks = 1;
1429 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001430}
1431
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001432#define CMD_ERRORS \
1433 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1434 R1_ADDRESS_ERROR | /* Misaligned address */ \
1435 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1436 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1437 R1_CC_ERROR | /* Card controller error */ \
1438 R1_ERROR) /* General/unknown error */
1439
Per Forlinee8a43a2011-07-01 18:55:33 +02001440static int mmc_blk_err_check(struct mmc_card *card,
1441 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001442{
Per Forlinee8a43a2011-07-01 18:55:33 +02001443 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1444 mmc_active);
1445 struct mmc_blk_request *brq = &mq_mrq->brq;
1446 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001447 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001448
1449 /*
1450 * sbc.error indicates a problem with the set block count
1451 * command. No data will have been transferred.
1452 *
1453 * cmd.error indicates a problem with the r/w command. No
1454 * data will have been transferred.
1455 *
1456 * stop.error indicates a problem with the stop command. Data
1457 * may have been transferred, or may still be transferring.
1458 */
Adrian Hunter67716322011-08-29 16:42:15 +03001459 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1460 brq->data.error) {
1461 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001462 case ERR_RETRY:
1463 return MMC_BLK_RETRY;
1464 case ERR_ABORT:
1465 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301466 case ERR_NOMEDIUM:
1467 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001468 case ERR_CONTINUE:
1469 break;
1470 }
1471 }
1472
1473 /*
1474 * Check for errors relating to the execution of the
1475 * initial command - such as address errors. No data
1476 * has been transferred.
1477 */
1478 if (brq->cmd.resp[0] & CMD_ERRORS) {
1479 pr_err("%s: r/w command failed, status = %#x\n",
1480 req->rq_disk->disk_name, brq->cmd.resp[0]);
1481 return MMC_BLK_ABORT;
1482 }
1483
1484 /*
1485 * Everything else is either success, or a data error of some
1486 * kind. If it was a write, we may have transitioned to
1487 * program mode, which we have to wait for it to complete.
1488 */
1489 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1490 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301491 unsigned long timeout;
1492
1493 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001494 do {
1495 int err = get_card_status(card, &status, 5);
1496 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301497 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001498 req->rq_disk->disk_name, err);
1499 return MMC_BLK_CMD_ERR;
1500 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301501
1502 /* Timeout if the device never becomes ready for data
1503 * and never leaves the program state.
1504 */
1505 if (time_after(jiffies, timeout)) {
1506 pr_err("%s: Card stuck in programming state!"\
1507 " %s %s\n", mmc_hostname(card->host),
1508 req->rq_disk->disk_name, __func__);
1509
1510 return MMC_BLK_CMD_ERR;
1511 }
Per Forlind78d4a82011-07-01 18:55:30 +02001512 /*
1513 * Some cards mishandle the status bits,
1514 * so make sure to check both the busy
1515 * indication and the card state.
1516 */
1517 } while (!(status & R1_READY_FOR_DATA) ||
1518 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1519 }
1520
1521 if (brq->data.error) {
1522 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1523 req->rq_disk->disk_name, brq->data.error,
1524 (unsigned)blk_rq_pos(req),
1525 (unsigned)blk_rq_sectors(req),
1526 brq->cmd.resp[0], brq->stop.resp[0]);
1527
1528 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001529 if (ecc_err)
1530 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001531 return MMC_BLK_DATA_ERR;
1532 } else {
1533 return MMC_BLK_CMD_ERR;
1534 }
1535 }
1536
Adrian Hunter67716322011-08-29 16:42:15 +03001537 if (!brq->data.bytes_xfered)
1538 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001539
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001540 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1541 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1542 return MMC_BLK_PARTIAL;
1543 else
1544 return MMC_BLK_SUCCESS;
1545 }
1546
Adrian Hunter67716322011-08-29 16:42:15 +03001547 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1548 return MMC_BLK_PARTIAL;
1549
1550 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001551}
1552
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001553/*
1554 * mmc_blk_reinsert_req() - re-insert request back to the scheduler
1555 * @areq: request to re-insert.
1556 *
1557 * Request may be packed or single. When fails to reinsert request, it will be
1558 * requeued to the the dispatch queue.
1559 */
1560static void mmc_blk_reinsert_req(struct mmc_async_req *areq)
1561{
1562 struct request *prq;
1563 int ret = 0;
1564 struct mmc_queue_req *mq_rq;
1565 struct request_queue *q;
1566
1567 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1568 q = mq_rq->req->q;
1569 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1570 while (!list_empty(&mq_rq->packed_list)) {
1571 /* return requests in reverse order */
1572 prq = list_entry_rq(mq_rq->packed_list.prev);
1573 list_del_init(&prq->queuelist);
1574 spin_lock_irq(q->queue_lock);
1575 ret = blk_reinsert_request(q, prq);
1576 if (ret) {
1577 blk_requeue_request(q, prq);
1578 spin_unlock_irq(q->queue_lock);
1579 goto reinsert_error;
1580 }
1581 spin_unlock_irq(q->queue_lock);
1582 }
1583 } else {
1584 spin_lock_irq(q->queue_lock);
1585 ret = blk_reinsert_request(q, mq_rq->req);
1586 if (ret)
1587 blk_requeue_request(q, mq_rq->req);
1588 spin_unlock_irq(q->queue_lock);
1589 }
1590 return;
1591
1592reinsert_error:
1593 pr_err("%s: blk_reinsert_request() failed (%d)",
1594 mq_rq->req->rq_disk->disk_name, ret);
1595 /*
1596 * -EIO will be reported for this request and rest of packed_list.
1597 * Urgent request will be proceeded anyway, while upper layer
1598 * responsibility to re-send failed requests
1599 */
1600 while (!list_empty(&mq_rq->packed_list)) {
1601 prq = list_entry_rq(mq_rq->packed_list.next);
1602 list_del_init(&prq->queuelist);
1603 spin_lock_irq(q->queue_lock);
1604 blk_requeue_request(q, prq);
1605 spin_unlock_irq(q->queue_lock);
1606 }
1607}
1608
1609/*
1610 * mmc_blk_update_interrupted_req() - update of the stopped request
1611 * @card: the MMC card associated with the request.
1612 * @areq: interrupted async request.
1613 *
1614 * Get stopped request state from card and update successfully done part of
1615 * the request by setting packed_fail_idx. The packed_fail_idx is index of
1616 * first uncompleted request in packed request list, for non-packed request
1617 * packed_fail_idx remains unchanged.
1618 *
1619 * Returns: MMC_BLK_SUCCESS for success, MMC_BLK_ABORT otherwise
1620 */
1621static int mmc_blk_update_interrupted_req(struct mmc_card *card,
1622 struct mmc_async_req *areq)
1623{
1624 int ret = MMC_BLK_SUCCESS;
1625 u8 *ext_csd;
1626 int correctly_done;
1627 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1628 mmc_active);
1629 struct request *prq;
1630 u8 req_index = 0;
1631
1632 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1633 return MMC_BLK_SUCCESS;
1634
1635 ext_csd = kmalloc(512, GFP_KERNEL);
1636 if (!ext_csd)
1637 return MMC_BLK_ABORT;
1638
1639 /* get correctly programmed sectors number from card */
1640 ret = mmc_send_ext_csd(card, ext_csd);
1641 if (ret) {
1642 pr_err("%s: error %d reading ext_csd\n",
1643 mmc_hostname(card->host), ret);
1644 ret = MMC_BLK_ABORT;
1645 goto exit;
1646 }
1647 correctly_done = card->ext_csd.data_sector_size *
1648 (ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 0] << 0 |
1649 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 1] << 8 |
1650 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 2] << 16 |
1651 ext_csd[EXT_CSD_CORRECTLY_PRG_SECTORS_NUM + 3] << 24);
1652
Konstantin Dorfmanf126d832013-07-31 18:13:37 +03001653 /*
1654 * skip packed command header (1 sector) included by the counter but not
1655 * actually written to the NAND
1656 */
1657 if (correctly_done >= card->ext_csd.data_sector_size)
1658 correctly_done -= card->ext_csd.data_sector_size;
1659
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001660 list_for_each_entry(prq, &mq_rq->packed_list, queuelist) {
1661 if ((correctly_done - (int)blk_rq_bytes(prq)) < 0) {
1662 /* prq is not successfull */
1663 mq_rq->packed_fail_idx = req_index;
1664 break;
1665 }
1666 correctly_done -= blk_rq_bytes(prq);
1667 req_index++;
1668 }
1669exit:
1670 kfree(ext_csd);
1671 return ret;
1672}
1673
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001674static int mmc_blk_packed_err_check(struct mmc_card *card,
1675 struct mmc_async_req *areq)
1676{
1677 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1678 mmc_active);
1679 struct request *req = mq_rq->req;
1680 int err, check, status;
1681 u8 ext_csd[512];
1682
1683 mq_rq->packed_retries--;
1684 check = mmc_blk_err_check(card, areq);
1685 err = get_card_status(card, &status, 0);
1686 if (err) {
1687 pr_err("%s: error %d sending status command\n",
1688 req->rq_disk->disk_name, err);
1689 return MMC_BLK_ABORT;
1690 }
1691
1692 if (status & R1_EXCEPTION_EVENT) {
1693 err = mmc_send_ext_csd(card, ext_csd);
1694 if (err) {
1695 pr_err("%s: error %d sending ext_csd\n",
1696 req->rq_disk->disk_name, err);
1697 return MMC_BLK_ABORT;
1698 }
1699
1700 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1701 EXT_CSD_PACKED_FAILURE) &&
1702 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1703 EXT_CSD_PACKED_GENERIC_ERROR)) {
1704 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1705 EXT_CSD_PACKED_INDEXED_ERROR) {
1706 mq_rq->packed_fail_idx =
1707 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1708 return MMC_BLK_PARTIAL;
1709 }
1710 }
1711 }
1712
1713 return check;
1714}
1715
Per Forlin54d49d772011-07-01 18:55:29 +02001716static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1717 struct mmc_card *card,
1718 int disable_multi,
1719 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Per Forlin54d49d772011-07-01 18:55:29 +02001721 u32 readcmd, writecmd;
1722 struct mmc_blk_request *brq = &mqrq->brq;
1723 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301725 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001727 /*
1728 * Reliable writes are used to implement Forced Unit Access and
1729 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001730 *
1731 * XXX: this really needs a good explanation of why REQ_META
1732 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001733 */
1734 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1735 (req->cmd_flags & REQ_META)) &&
1736 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001737 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001738
Per Forlin54d49d772011-07-01 18:55:29 +02001739 memset(brq, 0, sizeof(struct mmc_blk_request));
1740 brq->mrq.cmd = &brq->cmd;
1741 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Per Forlin54d49d772011-07-01 18:55:29 +02001743 brq->cmd.arg = blk_rq_pos(req);
1744 if (!mmc_card_blockaddr(card))
1745 brq->cmd.arg <<= 9;
1746 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1747 brq->data.blksz = 512;
1748 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1749 brq->stop.arg = 0;
1750 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1751 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Asutosh Das1a897142012-07-27 18:10:19 +05301753 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001754 /*
1755 * The block layer doesn't support all sector count
1756 * restrictions, so we need to be prepared for too big
1757 * requests.
1758 */
1759 if (brq->data.blocks > card->host->max_blk_count)
1760 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001762 if (brq->data.blocks > 1) {
1763 /*
1764 * After a read error, we redo the request one sector
1765 * at a time in order to accurately determine which
1766 * sectors can be read successfully.
1767 */
1768 if (disable_multi)
1769 brq->data.blocks = 1;
1770
1771 /* Some controllers can't do multiblock reads due to hw bugs */
1772 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1773 rq_data_dir(req) == READ)
1774 brq->data.blocks = 1;
1775 }
Per Forlin54d49d772011-07-01 18:55:29 +02001776
1777 if (brq->data.blocks > 1 || do_rel_wr) {
1778 /* SPI multiblock writes terminate using a special
1779 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001780 */
Per Forlin54d49d772011-07-01 18:55:29 +02001781 if (!mmc_host_is_spi(card->host) ||
1782 rq_data_dir(req) == READ)
1783 brq->mrq.stop = &brq->stop;
1784 readcmd = MMC_READ_MULTIPLE_BLOCK;
1785 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1786 } else {
1787 brq->mrq.stop = NULL;
1788 readcmd = MMC_READ_SINGLE_BLOCK;
1789 writecmd = MMC_WRITE_BLOCK;
1790 }
1791 if (rq_data_dir(req) == READ) {
1792 brq->cmd.opcode = readcmd;
1793 brq->data.flags |= MMC_DATA_READ;
1794 } else {
1795 brq->cmd.opcode = writecmd;
1796 brq->data.flags |= MMC_DATA_WRITE;
1797 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001798
Per Forlin54d49d772011-07-01 18:55:29 +02001799 if (do_rel_wr)
1800 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001801
Per Forlin54d49d772011-07-01 18:55:29 +02001802 /*
Saugata Das42659002011-12-21 13:09:17 +05301803 * Data tag is used only during writing meta data to speed
1804 * up write and any subsequent read of this meta data
1805 */
1806 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1807 (req->cmd_flags & REQ_META) &&
1808 (rq_data_dir(req) == WRITE) &&
1809 ((brq->data.blocks * brq->data.blksz) >=
1810 card->ext_csd.data_tag_unit_size);
1811
1812 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001813 * Pre-defined multi-block transfers are preferable to
1814 * open ended-ones (and necessary for reliable writes).
1815 * However, it is not sufficient to just send CMD23,
1816 * and avoid the final CMD12, as on an error condition
1817 * CMD12 (stop) needs to be sent anyway. This, coupled
1818 * with Auto-CMD23 enhancements provided by some
1819 * hosts, means that the complexity of dealing
1820 * with this is best left to the host. If CMD23 is
1821 * supported by card and host, we'll fill sbc in and let
1822 * the host deal with handling it correctly. This means
1823 * that for hosts that don't expose MMC_CAP_CMD23, no
1824 * change of behavior will be observed.
1825 *
1826 * N.B: Some MMC cards experience perf degradation.
1827 * We'll avoid using CMD23-bounded multiblock writes for
1828 * these, while retaining features like reliable writes.
1829 */
Saugata Das42659002011-12-21 13:09:17 +05301830 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1831 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1832 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001833 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1834 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301835 (do_rel_wr ? (1 << 31) : 0) |
1836 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001837 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1838 brq->mrq.sbc = &brq->sbc;
1839 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001840
Per Forlin54d49d772011-07-01 18:55:29 +02001841 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001842
Per Forlin54d49d772011-07-01 18:55:29 +02001843 brq->data.sg = mqrq->sg;
1844 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001845
Per Forlin54d49d772011-07-01 18:55:29 +02001846 /*
1847 * Adjust the sg list so it is the same size as the
1848 * request.
1849 */
1850 if (brq->data.blocks != blk_rq_sectors(req)) {
1851 int i, data_size = brq->data.blocks << 9;
1852 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001853
Per Forlin54d49d772011-07-01 18:55:29 +02001854 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1855 data_size -= sg->length;
1856 if (data_size <= 0) {
1857 sg->length += data_size;
1858 i++;
1859 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001860 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001861 }
Per Forlin54d49d772011-07-01 18:55:29 +02001862 brq->data.sg_len = i;
1863 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001864
Per Forlinee8a43a2011-07-01 18:55:33 +02001865 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02001866 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001867 if (mq->err_check_fn)
1868 mqrq->mmc_active.err_check = mq->err_check_fn;
1869 else
1870 mqrq->mmc_active.err_check = mmc_blk_err_check;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02001871 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
1872 mqrq->mmc_active.update_interrupted_req =
1873 mmc_blk_update_interrupted_req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001874
Per Forlin54d49d772011-07-01 18:55:29 +02001875 mmc_queue_bounce_pre(mqrq);
1876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001878/**
1879 * mmc_blk_disable_wr_packing() - disables packing mode
1880 * @mq: MMC queue.
1881 *
1882 */
1883void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
1884{
1885 if (mq) {
1886 mq->wr_packing_enabled = false;
1887 mq->num_of_potential_packed_wr_reqs = 0;
1888 }
1889}
1890EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
1891
Lee Susman2e3c0642013-04-23 17:59:26 +03001892static int get_packed_trigger(int potential, struct mmc_card *card,
1893 struct request *req, int curr_trigger)
1894{
1895 static int num_mean_elements = 1;
1896 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1897 unsigned int trigger = curr_trigger;
1898 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
1899
1900 /* scale down the upper bound to 75% */
1901 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
1902
1903 /*
1904 * since the most common calls for this function are with small
1905 * potential write values and since we don't want these calls to affect
1906 * the packed trigger, set a lower bound and ignore calls with
1907 * potential lower than that bound
1908 */
1909 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
1910 return trigger;
1911
1912 /*
1913 * this is to prevent integer overflow in the following calculation:
1914 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
1915 */
1916 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
1917 num_mean_elements = 1;
1918 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
1919 }
1920
1921 /*
1922 * get next mean value based on previous mean value and current
1923 * potential packed writes. Calculation is as follows:
1924 * mean_pot[i+1] =
1925 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
1926 */
1927 mean_potential *= num_mean_elements;
1928 /*
1929 * add num_mean_elements so that the division of two integers doesn't
1930 * lower mean_potential too much
1931 */
1932 if (potential > mean_potential)
1933 mean_potential += num_mean_elements;
1934 mean_potential += potential;
1935 /* this is for gaining more precision when dividing two integers */
1936 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
1937 /* this completes the mean calculation */
1938 mean_potential /= ++num_mean_elements;
1939 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
1940
1941 /*
1942 * if current potential packed writes is greater than the mean potential
1943 * then the heuristic is that the following workload will contain many
1944 * write requests, therefore we lower the packed trigger. In the
1945 * opposite case we want to increase the trigger in order to get less
1946 * packing events.
1947 */
1948 if (potential >= mean_potential)
1949 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
1950 PCKD_TRGR_LOWER_BOUND : trigger - 1;
1951 else
1952 trigger = (trigger >= pckd_trgr_upper_bound) ?
1953 pckd_trgr_upper_bound : trigger + 1;
1954
1955 /*
1956 * an urgent read request indicates a packed list being interrupted
1957 * by this read, therefore we aim for less packing, hence the trigger
1958 * gets increased
1959 */
1960 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
1961 trigger += PCKD_TRGR_URGENT_PENALTY;
1962
1963 return trigger;
1964}
1965
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001966static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1967 struct request *req)
1968{
1969 struct mmc_host *host = mq->card->host;
1970 int data_dir;
1971
1972 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1973 return;
1974
Maya Erez6f219eb2012-12-02 13:27:15 +02001975 /* Support for the write packing on eMMC 4.5 or later */
1976 if (mq->card->ext_csd.rev <= 5)
1977 return;
1978
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001979 /*
1980 * In case the packing control is not supported by the host, it should
1981 * not have an effect on the write packing. Therefore we have to enable
1982 * the write packing
1983 */
1984 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1985 mq->wr_packing_enabled = true;
1986 return;
1987 }
1988
1989 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1990 if (mq->num_of_potential_packed_wr_reqs >
1991 mq->num_wr_reqs_to_start_packing)
1992 mq->wr_packing_enabled = true;
Lee Susman2e3c0642013-04-23 17:59:26 +03001993 mq->num_wr_reqs_to_start_packing =
1994 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
1995 mq->card, req,
1996 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001997 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001998 return;
1999 }
2000
2001 data_dir = rq_data_dir(req);
2002
2003 if (data_dir == READ) {
Konstantin Dorfmand20df952013-02-05 15:45:53 +02002004 mmc_blk_disable_wr_packing(mq);
Lee Susman2e3c0642013-04-23 17:59:26 +03002005 mq->num_wr_reqs_to_start_packing =
2006 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2007 mq->card, req,
2008 mq->num_wr_reqs_to_start_packing);
2009 mq->num_of_potential_packed_wr_reqs = 0;
2010 mq->wr_packing_enabled = false;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002011 return;
2012 } else if (data_dir == WRITE) {
2013 mq->num_of_potential_packed_wr_reqs++;
2014 }
2015
2016 if (mq->num_of_potential_packed_wr_reqs >
2017 mq->num_wr_reqs_to_start_packing)
2018 mq->wr_packing_enabled = true;
2019
2020}
2021
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002022struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2023{
2024 if (!card)
2025 return NULL;
2026
2027 return &card->wr_pack_stats;
2028}
2029EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2030
2031void mmc_blk_init_packed_statistics(struct mmc_card *card)
2032{
2033 int max_num_of_packed_reqs = 0;
2034
2035 if (!card || !card->wr_pack_stats.packing_events)
2036 return;
2037
2038 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2039
2040 spin_lock(&card->wr_pack_stats.lock);
2041 memset(card->wr_pack_stats.packing_events, 0,
2042 (max_num_of_packed_reqs + 1) *
2043 sizeof(*card->wr_pack_stats.packing_events));
2044 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2045 sizeof(card->wr_pack_stats.pack_stop_reason));
2046 card->wr_pack_stats.enabled = true;
2047 spin_unlock(&card->wr_pack_stats.lock);
2048}
2049EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2050
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002051static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2052{
2053 struct request_queue *q = mq->queue;
2054 struct mmc_card *card = mq->card;
2055 struct request *cur = req, *next = NULL;
2056 struct mmc_blk_data *md = mq->data;
2057 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2058 unsigned int req_sectors = 0, phys_segments = 0;
2059 unsigned int max_blk_count, max_phys_segs;
2060 u8 put_back = 0;
2061 u8 max_packed_rw = 0;
2062 u8 reqs = 0;
Lee Susmand50afb52013-01-09 14:48:47 +02002063 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002064
2065 mmc_blk_clear_packed(mq->mqrq_cur);
2066
2067 if (!(md->flags & MMC_BLK_CMD23) ||
2068 !card->ext_csd.packed_event_en)
2069 goto no_packed;
2070
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002071 if (!mq->wr_packing_enabled)
2072 goto no_packed;
2073
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002074 if ((rq_data_dir(cur) == WRITE) &&
2075 (card->host->caps2 & MMC_CAP2_PACKED_WR))
2076 max_packed_rw = card->ext_csd.max_packed_writes;
2077
2078 if (max_packed_rw == 0)
2079 goto no_packed;
2080
2081 if (mmc_req_rel_wr(cur) &&
2082 (md->flags & MMC_BLK_REL_WR) &&
2083 !en_rel_wr)
2084 goto no_packed;
2085
2086 if (mmc_large_sec(card) &&
2087 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2088 goto no_packed;
2089
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002090 if (cur->cmd_flags & REQ_FUA)
2091 goto no_packed;
2092
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002093 max_blk_count = min(card->host->max_blk_count,
2094 card->host->max_req_size >> 9);
2095 if (unlikely(max_blk_count > 0xffff))
2096 max_blk_count = 0xffff;
2097
2098 max_phys_segs = queue_max_segments(q);
2099 req_sectors += blk_rq_sectors(cur);
2100 phys_segments += cur->nr_phys_segments;
2101
2102 if (rq_data_dir(cur) == WRITE) {
2103 req_sectors++;
2104 phys_segments++;
2105 }
2106
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002107 spin_lock(&stats->lock);
2108
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002109 while (reqs < max_packed_rw - 1) {
2110 spin_lock_irq(q->queue_lock);
2111 next = blk_fetch_request(q);
2112 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002113 if (!next) {
2114 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002115 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002116 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002117
2118 if (mmc_large_sec(card) &&
2119 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02002120 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002121 put_back = 1;
2122 break;
2123 }
2124
2125 if (next->cmd_flags & REQ_DISCARD ||
2126 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002127 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002128 put_back = 1;
2129 break;
2130 }
2131
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02002132 if (next->cmd_flags & REQ_FUA) {
2133 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2134 put_back = 1;
2135 break;
2136 }
2137
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002138 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002139 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002140 put_back = 1;
2141 break;
2142 }
2143
2144 if (mmc_req_rel_wr(next) &&
2145 (md->flags & MMC_BLK_REL_WR) &&
2146 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002147 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002148 put_back = 1;
2149 break;
2150 }
2151
2152 req_sectors += blk_rq_sectors(next);
2153 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002154 if (stats->enabled)
2155 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002156 put_back = 1;
2157 break;
2158 }
2159
2160 phys_segments += next->nr_phys_segments;
2161 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002162 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002163 put_back = 1;
2164 break;
2165 }
2166
Maya Erez64d12462013-01-06 10:19:38 +02002167 if (mq->no_pack_for_random) {
2168 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2169 blk_rq_pos(next)) {
2170 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2171 put_back = 1;
2172 break;
2173 }
2174 }
2175
Maya Erez2d33a192013-01-04 15:52:41 +02002176 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002177 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02002178 if (card->ext_csd.bkops_en)
2179 card->bkops_info.sectors_changed +=
2180 blk_rq_sectors(next);
2181 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002182 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
2183 cur = next;
2184 reqs++;
2185 }
2186
2187 if (put_back) {
2188 spin_lock_irq(q->queue_lock);
2189 blk_requeue_request(q, next);
2190 spin_unlock_irq(q->queue_lock);
2191 }
2192
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02002193 if (stats->enabled) {
2194 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2195 stats->packing_events[reqs + 1]++;
2196 if (reqs + 1 == max_packed_rw)
2197 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2198 }
2199
2200 spin_unlock(&stats->lock);
2201
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002202 if (reqs > 0) {
2203 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
2204 mq->mqrq_cur->packed_num = ++reqs;
2205 mq->mqrq_cur->packed_retries = reqs;
2206 return reqs;
2207 }
2208
2209no_packed:
2210 mmc_blk_clear_packed(mq->mqrq_cur);
2211 return 0;
2212}
2213
2214static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2215 struct mmc_card *card,
2216 struct mmc_queue *mq)
2217{
2218 struct mmc_blk_request *brq = &mqrq->brq;
2219 struct request *req = mqrq->req;
2220 struct request *prq;
2221 struct mmc_blk_data *md = mq->data;
2222 bool do_rel_wr, do_data_tag;
2223 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
2224 u8 i = 1;
2225
2226 mqrq->packed_cmd = MMC_PACKED_WRITE;
2227 mqrq->packed_blocks = 0;
2228 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
2229
2230 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
2231 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
2232 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
2233
2234 /*
2235 * Argument for each entry of packed group
2236 */
2237 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
2238 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2239 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2240 (prq->cmd_flags & REQ_META) &&
2241 (rq_data_dir(prq) == WRITE) &&
2242 ((brq->data.blocks * brq->data.blksz) >=
2243 card->ext_csd.data_tag_unit_size);
2244 /* Argument of CMD23 */
2245 packed_cmd_hdr[(i * 2)] =
2246 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2247 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
2248 blk_rq_sectors(prq);
2249 /* Argument of CMD18 or CMD25 */
2250 packed_cmd_hdr[((i * 2)) + 1] =
2251 mmc_card_blockaddr(card) ?
2252 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
2253 mqrq->packed_blocks += blk_rq_sectors(prq);
2254 i++;
2255 }
2256
2257 memset(brq, 0, sizeof(struct mmc_blk_request));
2258 brq->mrq.cmd = &brq->cmd;
2259 brq->mrq.data = &brq->data;
2260 brq->mrq.sbc = &brq->sbc;
2261 brq->mrq.stop = &brq->stop;
2262
2263 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2264 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
2265 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2266
2267 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2268 brq->cmd.arg = blk_rq_pos(req);
2269 if (!mmc_card_blockaddr(card))
2270 brq->cmd.arg <<= 9;
2271 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2272
2273 brq->data.blksz = 512;
2274 brq->data.blocks = mqrq->packed_blocks + 1;
2275 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02002276 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002277
2278 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2279 brq->stop.arg = 0;
2280 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2281
2282 mmc_set_data_timeout(&brq->data, card);
2283
2284 brq->data.sg = mqrq->sg;
2285 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2286
2287 mqrq->mmc_active.mrq = &brq->mrq;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002288 mqrq->mmc_active.cmd_flags = req->cmd_flags;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02002289
2290 /*
2291 * This is intended for packed commands tests usage - in case these
2292 * functions are not in use the respective pointers are NULL
2293 */
2294 if (mq->err_check_fn)
2295 mqrq->mmc_active.err_check = mq->err_check_fn;
2296 else
2297 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2298
2299 if (mq->packed_test_fn)
2300 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002301
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002302
2303 mqrq->mmc_active.reinsert_req = mmc_blk_reinsert_req;
2304 mqrq->mmc_active.update_interrupted_req =
2305 mmc_blk_update_interrupted_req;
2306
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002307 mmc_queue_bounce_pre(mqrq);
2308}
2309
Adrian Hunter67716322011-08-29 16:42:15 +03002310static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2311 struct mmc_blk_request *brq, struct request *req,
2312 int ret)
2313{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002314 struct mmc_queue_req *mq_rq;
2315 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2316
Adrian Hunter67716322011-08-29 16:42:15 +03002317 /*
2318 * If this is an SD card and we're writing, we can first
2319 * mark the known good sectors as ok.
2320 *
2321 * If the card is not SD, we can still ok written sectors
2322 * as reported by the controller (which might be less than
2323 * the real number of written sectors, but never more).
2324 */
2325 if (mmc_card_sd(card)) {
2326 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05302327 if (!brq->data.fault_injected) {
2328 blocks = mmc_sd_num_wr_blocks(card);
2329 if (blocks != (u32)-1)
2330 ret = blk_end_request(req, 0, blocks << 9);
2331 } else
2332 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002333 } else {
2334 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
2335 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
2336 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03002337 return ret;
2338}
2339
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002340static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2341{
2342 struct request *prq;
2343 int idx = mq_rq->packed_fail_idx, i = 0;
2344 int ret = 0;
2345
2346 while (!list_empty(&mq_rq->packed_list)) {
2347 prq = list_entry_rq(mq_rq->packed_list.next);
2348 if (idx == i) {
2349 /* retry from error index */
2350 mq_rq->packed_num -= idx;
2351 mq_rq->req = prq;
2352 ret = 1;
2353
2354 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
2355 list_del_init(&prq->queuelist);
2356 mmc_blk_clear_packed(mq_rq);
2357 }
2358 return ret;
2359 }
2360 list_del_init(&prq->queuelist);
2361 blk_end_request(prq, 0, blk_rq_bytes(prq));
2362 i++;
2363 }
2364
2365 mmc_blk_clear_packed(mq_rq);
2366 return ret;
2367}
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002368static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq,
2369 unsigned int cmd_flags)
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002370{
2371 struct request *prq;
2372
2373 while (!list_empty(&mq_rq->packed_list)) {
2374 prq = list_entry_rq(mq_rq->packed_list.next);
2375 list_del_init(&prq->queuelist);
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002376 prq->cmd_flags |= cmd_flags;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002377 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2378 }
2379
2380 mmc_blk_clear_packed(mq_rq);
2381}
2382
2383static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2384 struct mmc_queue_req *mq_rq)
2385{
2386 struct request *prq;
2387 struct request_queue *q = mq->queue;
2388
2389 while (!list_empty(&mq_rq->packed_list)) {
2390 prq = list_entry_rq(mq_rq->packed_list.prev);
2391 if (prq->queuelist.prev != &mq_rq->packed_list) {
2392 list_del_init(&prq->queuelist);
2393 spin_lock_irq(q->queue_lock);
2394 blk_requeue_request(mq->queue, prq);
2395 spin_unlock_irq(q->queue_lock);
2396 } else {
2397 list_del_init(&prq->queuelist);
2398 }
2399 }
2400
2401 mmc_blk_clear_packed(mq_rq);
2402}
2403
Per Forlinee8a43a2011-07-01 18:55:33 +02002404static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02002405{
2406 struct mmc_blk_data *md = mq->data;
2407 struct mmc_card *card = md->queue.card;
2408 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03002409 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02002410 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002411 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02002412 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002413 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002414 const u8 packed_num = 2;
2415 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02002416
2417 if (!rqc && !mq->mqrq_prev->req)
2418 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02002419
Maya Erez5f360692012-10-10 03:47:54 +02002420 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02002421 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
2422 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002423 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02002424 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002425
Per Forlin54d49d772011-07-01 18:55:29 +02002426 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002427 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002428 if (reqs >= packed_num)
2429 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2430 card, mq);
2431 else
2432 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002433 areq = &mq->mqrq_cur->mmc_active;
2434 } else
2435 areq = NULL;
2436 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002437 if (!areq) {
Lee Susmand50afb52013-01-09 14:48:47 +02002438 if (status == MMC_BLK_NEW_REQUEST)
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002439 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002440 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002441 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002442
Per Forlinee8a43a2011-07-01 18:55:33 +02002443 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2444 brq = &mq_rq->brq;
2445 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002446 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002447 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002448
Per Forlind78d4a82011-07-01 18:55:30 +02002449 switch (status) {
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002450 case MMC_BLK_URGENT:
2451 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2452 /* complete successfully transmitted part */
2453 if (mmc_blk_end_packed_req(mq_rq))
2454 /* process for not transmitted part */
2455 mmc_blk_reinsert_req(areq);
2456 } else {
2457 mmc_blk_reinsert_req(areq);
2458 }
2459
2460 mq->flags |= MMC_QUEUE_URGENT_REQUEST;
2461 ret = 0;
2462 break;
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002463 case MMC_BLK_URGENT_DONE:
Per Forlind78d4a82011-07-01 18:55:30 +02002464 case MMC_BLK_SUCCESS:
2465 case MMC_BLK_PARTIAL:
2466 /*
2467 * A block was successfully transferred.
2468 */
Adrian Hunter67716322011-08-29 16:42:15 +03002469 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002470
2471 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2472 ret = mmc_blk_end_packed_req(mq_rq);
2473 break;
2474 } else {
2475 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002476 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002477 }
2478
Adrian Hunter67716322011-08-29 16:42:15 +03002479 /*
2480 * If the blk_end_request function returns non-zero even
2481 * though all data has been transferred and no errors
2482 * were returned by the host controller, it's a bug.
2483 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002484 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302485 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002486 __func__, blk_rq_bytes(req),
2487 brq->data.bytes_xfered);
2488 rqc = NULL;
2489 goto cmd_abort;
2490 }
Per Forlind78d4a82011-07-01 18:55:30 +02002491 break;
2492 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002493 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2494 if (!mmc_blk_reset(md, card->host, type))
2495 break;
2496 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002497 case MMC_BLK_RETRY:
2498 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002499 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002500 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002501 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002502 if (!mmc_blk_reset(md, card->host, type))
2503 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002504 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002505 case MMC_BLK_DATA_ERR: {
2506 int err;
2507
2508 err = mmc_blk_reset(md, card->host, type);
2509 if (!err)
2510 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002511 if (err == -ENODEV ||
2512 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002513 goto cmd_abort;
2514 /* Fall through */
2515 }
2516 case MMC_BLK_ECC_ERR:
2517 if (brq->data.blocks > 1) {
2518 /* Redo read one sector at a time */
2519 pr_warning("%s: retrying using single block read\n",
2520 req->rq_disk->disk_name);
2521 disable_multi = 1;
2522 break;
2523 }
Per Forlind78d4a82011-07-01 18:55:30 +02002524 /*
2525 * After an error, we redo I/O one sector at a
2526 * time, so we only reach here after trying to
2527 * read a single sector.
2528 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302529 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002530 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002531 if (!ret)
2532 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002533 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302534 case MMC_BLK_NOMEDIUM:
2535 goto cmd_abort;
Konstantin Dorfman4744aae2013-01-07 12:12:17 +02002536 default:
2537 pr_err("%s:%s: Unhandled return value (%d)",
2538 req->rq_disk->disk_name,
2539 __func__, status);
2540 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002541 }
2542
Per Forlinee8a43a2011-07-01 18:55:33 +02002543 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002544 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2545 /*
2546 * In case of a incomplete request
2547 * prepare it again and resend.
2548 */
2549 mmc_blk_rw_rq_prep(mq_rq, card,
2550 disable_multi, mq);
2551 mmc_start_req(card->host,
2552 &mq_rq->mmc_active, NULL);
2553 } else {
2554 if (!mq_rq->packed_retries)
2555 goto cmd_abort;
2556 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2557 mmc_start_req(card->host,
2558 &mq_rq->mmc_active, NULL);
2559 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } while (ret);
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return 1;
2564
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002565 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002566 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2567 if (mmc_card_removed(card))
2568 req->cmd_flags |= REQ_QUIET;
2569 while (ret)
2570 ret = blk_end_request(req, -EIO,
2571 blk_rq_cur_bytes(req));
2572 } else {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002573 mmc_blk_abort_packed_req(mq_rq, 0);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Per Forlinee8a43a2011-07-01 18:55:33 +02002576 start_new_req:
2577 if (rqc) {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002578 if (mmc_card_removed(card)) {
2579 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2580 rqc->cmd_flags |= REQ_QUIET;
2581 blk_end_request_all(rqc, -EIO);
2582 } else {
2583 mmc_blk_abort_packed_req(mq_rq, REQ_QUIET);
2584 }
2585 } else {
2586 /* If current request is packed, it needs to put back */
2587 if (mq_rq->packed_cmd != MMC_PACKED_NONE)
2588 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002589
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002590 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2591 mmc_start_req(card->host,
2592 &mq->mqrq_cur->mmc_active,
2593 NULL);
2594 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002595 }
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
Adrian Hunterbd788c92010-08-11 14:17:47 -07002600static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2601{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002602 int ret;
2603 struct mmc_blk_data *md = mq->data;
2604 struct mmc_card *card = md->queue.card;
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002605 struct mmc_host *host = card->host;
2606 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002607
San Mehat148c57a2009-07-30 08:21:19 -07002608#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2609 if (mmc_bus_needs_resume(card->host)) {
2610 mmc_resume_bus(card->host);
2611 mmc_blk_set_blksize(md, card);
2612 }
2613#endif
2614
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002615 if (req && !mq->mqrq_prev->req) {
Asutosh Dasbbc84782013-02-11 15:31:35 +05302616 mmc_rpm_hold(host, &card->dev);
Per Forlinee8a43a2011-07-01 18:55:33 +02002617 /* claim host only for the first request */
2618 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002619 if (card->ext_csd.bkops_en)
2620 mmc_stop_bkops(card);
2621 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002622
Andrei Warkentin371a6892011-04-11 18:10:25 -05002623 ret = mmc_blk_part_switch(card, md);
2624 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002625 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302626 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002627 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002628 ret = 0;
2629 goto out;
2630 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002631
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002632 mmc_blk_write_packing_control(mq, req);
2633
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002634 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002635 mq->flags &= ~MMC_QUEUE_URGENT_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002636 if (req && req->cmd_flags & REQ_SANITIZE) {
2637 /* complete ongoing async transfer before issuing sanitize */
2638 if (card->host && card->host->areq)
2639 mmc_blk_issue_rw_rq(mq, NULL);
2640 ret = mmc_blk_issue_sanitize_rq(mq, req);
2641 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002642 /* complete ongoing async transfer before issuing discard */
2643 if (card->host->areq)
2644 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302645 if (req->cmd_flags & REQ_SECURE &&
2646 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002647 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002648 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002649 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002650 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002651 /* complete ongoing async transfer before issuing flush */
2652 if (card->host->areq)
2653 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002654 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002655 } else {
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002656 if (!req && host->areq) {
2657 spin_lock_irqsave(&host->context_info.lock, flags);
2658 host->context_info.is_waiting_last_req = true;
2659 spin_unlock_irqrestore(&host->context_info.lock, flags);
2660 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002661 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002662 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002663
Andrei Warkentin371a6892011-04-11 18:10:25 -05002664out:
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002665 /*
2666 * packet burst is over, when one of the following occurs:
2667 * - no more requests and new request notification is not in progress
2668 * - urgent notification in progress and current request is not urgent
2669 * (all existing requests completed or reinserted to the block layer)
2670 */
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +02002671 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Konstantin Dorfmane9382e62013-03-10 17:39:16 +02002672 ((mq->flags & MMC_QUEUE_URGENT_REQUEST) &&
Konstantin Dorfmanfac0bd62014-02-02 12:49:54 +02002673 !(mq->mqrq_cur->req->cmd_flags &
2674 MMC_REQ_NOREINSERT_MASK))) {
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002675 if (mmc_card_need_bkops(card))
2676 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002677 /* release host only when there are no more requests */
2678 mmc_release_host(card->host);
Asutosh Dasbbc84782013-02-11 15:31:35 +05302679 mmc_rpm_release(host, &card->dev);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002680 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002681 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Russell Kinga6f6c962006-01-03 22:38:44 +00002684static inline int mmc_blk_readonly(struct mmc_card *card)
2685{
2686 return mmc_card_readonly(card) ||
2687 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2688}
2689
Andrei Warkentin371a6892011-04-11 18:10:25 -05002690static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2691 struct device *parent,
2692 sector_t size,
2693 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002694 const char *subname,
2695 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 struct mmc_blk_data *md;
2698 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002699 unsigned int percentage =
2700 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002702 devidx = find_first_zero_bit(dev_use, max_devices);
2703 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return ERR_PTR(-ENOSPC);
2705 __set_bit(devidx, dev_use);
2706
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002707 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002708 if (!md) {
2709 ret = -ENOMEM;
2710 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002712
Russell Kinga6f6c962006-01-03 22:38:44 +00002713 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002714 * !subname implies we are creating main mmc_blk_data that will be
2715 * associated with mmc_card with mmc_set_drvdata. Due to device
2716 * partitions, devidx will not coincide with a per-physical card
2717 * index anymore so we keep track of a name index.
2718 */
2719 if (!subname) {
2720 md->name_idx = find_first_zero_bit(name_use, max_devices);
2721 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002722 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002723 md->name_idx = ((struct mmc_blk_data *)
2724 dev_to_disk(parent)->private_data)->name_idx;
2725
Johan Rudholmadd710e2011-12-02 08:51:06 +01002726 md->area_type = area_type;
2727
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002728 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002729 * Set the read-only status based on the supported commands
2730 * and the write protect switch.
2731 */
2732 md->read_only = mmc_blk_readonly(card);
2733
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002734 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002735 if (md->disk == NULL) {
2736 ret = -ENOMEM;
2737 goto err_kfree;
2738 }
2739
2740 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002741 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002742 md->usage = 1;
2743
Adrian Hunterd09408a2011-06-23 13:40:28 +03002744 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002745 if (ret)
2746 goto err_putdisk;
2747
Russell Kinga6f6c962006-01-03 22:38:44 +00002748 md->queue.issue_fn = mmc_blk_issue_rq;
2749 md->queue.data = md;
2750
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002751 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002752 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002753 md->disk->fops = &mmc_bdops;
2754 md->disk->private_data = md;
2755 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002756 md->disk->driverfs_dev = parent;
2757 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002758 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002759 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2760 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002761
2762 /*
2763 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2764 *
2765 * - be set for removable media with permanent block devices
2766 * - be unset for removable block devices with permanent media
2767 *
2768 * Since MMC block devices clearly fall under the second
2769 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2770 * should use the block device creation/destruction hotplug
2771 * messages to tell when the card is present.
2772 */
2773
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002774 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2775 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002776
Martin K. Petersene1defc42009-05-22 17:17:49 -04002777 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002778 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002779
Maya Ereze1eae982013-01-05 21:51:06 +02002780 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2781 card->bkops_info.min_sectors_to_queue_delayed_work =
2782 ((unsigned int)size * percentage) / 100;
2783
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002784 if (mmc_host_cmd23(card->host)) {
2785 if (mmc_card_mmc(card) ||
2786 (mmc_card_sd(card) &&
2787 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2788 md->flags |= MMC_BLK_CMD23;
2789 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002790
2791 if (mmc_card_mmc(card) &&
2792 md->flags & MMC_BLK_CMD23 &&
2793 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2794 card->ext_csd.rel_sectors)) {
2795 md->flags |= MMC_BLK_REL_WR;
2796 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2797 }
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002800
2801 err_putdisk:
2802 put_disk(md->disk);
2803 err_kfree:
Sahitya Tummala363eb142013-10-23 09:05:21 +05302804 if (!subname)
2805 __clear_bit(md->name_idx, name_use);
Russell Kinga6f6c962006-01-03 22:38:44 +00002806 kfree(md);
2807 out:
Sahitya Tummala363eb142013-10-23 09:05:21 +05302808 __clear_bit(devidx, dev_use);
Russell Kinga6f6c962006-01-03 22:38:44 +00002809 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
Andrei Warkentin371a6892011-04-11 18:10:25 -05002812static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2813{
2814 sector_t size;
2815 struct mmc_blk_data *md;
2816
2817 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2818 /*
2819 * The EXT_CSD sector count is in number or 512 byte
2820 * sectors.
2821 */
2822 size = card->ext_csd.sectors;
2823 } else {
2824 /*
2825 * The CSD capacity field is in units of read_blkbits.
2826 * set_capacity takes units of 512 bytes.
2827 */
2828 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2829 }
2830
Johan Rudholmadd710e2011-12-02 08:51:06 +01002831 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2832 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002833 return md;
2834}
2835
2836static int mmc_blk_alloc_part(struct mmc_card *card,
2837 struct mmc_blk_data *md,
2838 unsigned int part_type,
2839 sector_t size,
2840 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002841 const char *subname,
2842 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002843{
2844 char cap_str[10];
2845 struct mmc_blk_data *part_md;
2846
2847 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002848 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002849 if (IS_ERR(part_md))
2850 return PTR_ERR(part_md);
2851 part_md->part_type = part_type;
2852 list_add(&part_md->part, &md->part);
2853
2854 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2855 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302856 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002857 part_md->disk->disk_name, mmc_card_id(card),
2858 mmc_card_name(card), part_md->part_type, cap_str);
2859 return 0;
2860}
2861
Namjae Jeone0c368d2011-10-06 23:41:38 +09002862/* MMC Physical partitions consist of two boot partitions and
2863 * up to four general purpose partitions.
2864 * For each partition enabled in EXT_CSD a block device will be allocatedi
2865 * to provide access to the partition.
2866 */
2867
Andrei Warkentin371a6892011-04-11 18:10:25 -05002868static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2869{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002870 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002871
2872 if (!mmc_card_mmc(card))
2873 return 0;
2874
Namjae Jeone0c368d2011-10-06 23:41:38 +09002875 for (idx = 0; idx < card->nr_parts; idx++) {
2876 if (card->part[idx].size) {
2877 ret = mmc_blk_alloc_part(card, md,
2878 card->part[idx].part_cfg,
2879 card->part[idx].size >> 9,
2880 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002881 card->part[idx].name,
2882 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002883 if (ret)
2884 return ret;
2885 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002886 }
2887
2888 return ret;
2889}
2890
Andrei Warkentin371a6892011-04-11 18:10:25 -05002891static void mmc_blk_remove_req(struct mmc_blk_data *md)
2892{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002893 struct mmc_card *card;
2894
Andrei Warkentin371a6892011-04-11 18:10:25 -05002895 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002896 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002897 device_remove_file(disk_to_dev(md->disk),
2898 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002899 if (md->disk->flags & GENHD_FL_UP) {
2900 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002901 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2902 card->ext_csd.boot_ro_lockable)
2903 device_remove_file(disk_to_dev(md->disk),
2904 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002905
2906 /* Stop new requests from getting into the queue */
2907 del_gendisk(md->disk);
2908 }
2909
2910 /* Then flush out any already in there */
2911 mmc_cleanup_queue(&md->queue);
2912 mmc_blk_put(md);
2913 }
2914}
2915
2916static void mmc_blk_remove_parts(struct mmc_card *card,
2917 struct mmc_blk_data *md)
2918{
2919 struct list_head *pos, *q;
2920 struct mmc_blk_data *part_md;
2921
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002922 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002923 list_for_each_safe(pos, q, &md->part) {
2924 part_md = list_entry(pos, struct mmc_blk_data, part);
2925 list_del(pos);
2926 mmc_blk_remove_req(part_md);
2927 }
2928}
2929
2930static int mmc_add_disk(struct mmc_blk_data *md)
2931{
2932 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002933 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002934
2935 add_disk(md->disk);
2936 md->force_ro.show = force_ro_show;
2937 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302938 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002939 md->force_ro.attr.name = "force_ro";
2940 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2941 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2942 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002943 goto force_ro_fail;
2944
2945 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2946 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002947 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002948
2949 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2950 mode = S_IRUGO;
2951 else
2952 mode = S_IRUGO | S_IWUSR;
2953
2954 md->power_ro_lock.show = power_ro_lock_show;
2955 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002956 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002957 md->power_ro_lock.attr.mode = mode;
2958 md->power_ro_lock.attr.name =
2959 "ro_lock_until_next_power_on";
2960 ret = device_create_file(disk_to_dev(md->disk),
2961 &md->power_ro_lock);
2962 if (ret)
2963 goto power_ro_lock_fail;
2964 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002965
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002966 md->num_wr_reqs_to_start_packing.show =
2967 num_wr_reqs_to_start_packing_show;
2968 md->num_wr_reqs_to_start_packing.store =
2969 num_wr_reqs_to_start_packing_store;
2970 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2971 md->num_wr_reqs_to_start_packing.attr.name =
2972 "num_wr_reqs_to_start_packing";
2973 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2974 ret = device_create_file(disk_to_dev(md->disk),
2975 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002976 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002977 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002978
Maya Ereze1eae982013-01-05 21:51:06 +02002979 md->bkops_check_threshold.show = bkops_check_threshold_show;
2980 md->bkops_check_threshold.store = bkops_check_threshold_store;
2981 sysfs_attr_init(&md->bkops_check_threshold.attr);
2982 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2983 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002984 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002985 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002986 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002987 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002988
Maya Erez64d12462013-01-06 10:19:38 +02002989 md->no_pack_for_random.show = no_pack_for_random_show;
2990 md->no_pack_for_random.store = no_pack_for_random_store;
2991 sysfs_attr_init(&md->no_pack_for_random.attr);
2992 md->no_pack_for_random.attr.name = "no_pack_for_random";
2993 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
2994 ret = device_create_file(disk_to_dev(md->disk),
2995 &md->no_pack_for_random);
2996 if (ret)
2997 goto no_pack_for_random_fails;
2998
Johan Rudholmadd710e2011-12-02 08:51:06 +01002999 return ret;
3000
Maya Erez64d12462013-01-06 10:19:38 +02003001no_pack_for_random_fails:
3002 device_remove_file(disk_to_dev(md->disk),
3003 &md->bkops_check_threshold);
Maya Ereze1eae982013-01-05 21:51:06 +02003004bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02003005 device_remove_file(disk_to_dev(md->disk),
3006 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02003007num_wr_reqs_to_start_packing_fail:
3008 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003009power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02003010 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003011force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02003012 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003013
3014 return ret;
3015}
3016
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003017static const struct mmc_fixup blk_fixups[] =
3018{
Chris Ballc59d4472011-11-11 22:01:43 -05003019 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3020 MMC_QUIRK_INAND_CMD38),
3021 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3022 MMC_QUIRK_INAND_CMD38),
3023 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3024 MMC_QUIRK_INAND_CMD38),
3025 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3026 MMC_QUIRK_INAND_CMD38),
3027 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3028 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003029
3030 /*
3031 * Some MMC cards experience performance degradation with CMD23
3032 * instead of CMD12-bounded multiblock transfers. For now we'll
3033 * black list what's bad...
3034 * - Certain Toshiba cards.
3035 *
3036 * N.B. This doesn't affect SD cards.
3037 */
Chris Ballc59d4472011-11-11 22:01:43 -05003038 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003039 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003040 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003041 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003042 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003043 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003044
3045 /*
3046 * Some Micron MMC cards needs longer data read timeout than
3047 * indicated in CSD.
3048 */
Chris Ballc59d4472011-11-11 22:01:43 -05003049 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003050 MMC_QUIRK_LONG_READ_TIME),
3051
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05303052 /* Some INAND MCP devices advertise incorrect timeout values */
3053 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3054 MMC_QUIRK_INAND_DATA_TIMEOUT),
3055
Ian Chenc4856122012-11-01 13:26:21 +05303056 /*
3057 * On these Samsung MoviNAND parts, performing secure erase or
3058 * secure trim can result in unrecoverable corruption due to a
3059 * firmware bug.
3060 */
3061 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3062 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3063 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3064 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3065 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3066 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3067 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3068 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3069 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3070 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3071 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3072 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3073 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3074 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3075 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3076 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
Asutosh Das967c55b2013-12-11 15:39:40 +05303077 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
3078 MMC_QUIRK_BROKEN_DATA_TIMEOUT),
Ian Chenc4856122012-11-01 13:26:21 +05303079
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003080 END_FIXUP
3081};
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083static int mmc_blk_probe(struct mmc_card *card)
3084{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003085 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003086 char cap_str[10];
3087
Pierre Ossman912490d2005-05-21 10:27:02 +01003088 /*
3089 * Check that the card supports the command class(es) we need.
3090 */
3091 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return -ENODEV;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 md = mmc_blk_alloc(card);
3095 if (IS_ERR(md))
3096 return PTR_ERR(md);
3097
Yi Li444122f2009-02-05 15:31:57 +08003098 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003099 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303100 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003102 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Andrei Warkentin371a6892011-04-11 18:10:25 -05003104 if (mmc_blk_alloc_parts(card, md))
3105 goto out;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003108 mmc_fixup_device(card, blk_fixups);
3109
San Mehat148c57a2009-07-30 08:21:19 -07003110#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3111 mmc_set_bus_resume_policy(card->host, 1);
3112#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003113 if (mmc_add_disk(md))
3114 goto out;
3115
3116 list_for_each_entry(part_md, &md->part, part) {
3117 if (mmc_add_disk(part_md))
3118 goto out;
3119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return 0;
3121
3122 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003123 mmc_blk_remove_parts(card, md);
3124 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128static void mmc_blk_remove(struct mmc_card *card)
3129{
3130 struct mmc_blk_data *md = mmc_get_drvdata(card);
3131
Andrei Warkentin371a6892011-04-11 18:10:25 -05003132 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003133 mmc_claim_host(card->host);
3134 mmc_blk_part_switch(card, md);
3135 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003136 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07003138#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3139 mmc_set_bus_resume_policy(card->host, 0);
3140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003143static void mmc_blk_shutdown(struct mmc_card *card)
3144{
3145 struct mmc_blk_data *part_md;
3146 struct mmc_blk_data *md = mmc_get_drvdata(card);
3147 int rc;
3148
3149 /* Silent the block layer */
3150 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303151 rc = mmc_queue_suspend(&md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003152 if (rc)
3153 goto suspend_error;
3154 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303155 rc = mmc_queue_suspend(&part_md->queue, 1);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003156 if (rc)
3157 goto suspend_error;
3158 }
3159 }
3160
3161 /* send power off notification */
3162 if (mmc_card_mmc(card)) {
3163 mmc_rpm_hold(card->host, &card->dev);
Asutosh Das600068d2014-01-08 17:54:31 +05303164 mmc_claim_host(card->host);
3165 mmc_stop_bkops(card);
3166 mmc_release_host(card->host);
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003167 mmc_send_long_pon(card);
3168 mmc_rpm_release(card->host, &card->dev);
3169 }
3170 return;
3171
3172suspend_error:
3173 pr_err("%s: mmc_queue_suspend returned error = %d",
3174 mmc_hostname(card->host), rc);
3175}
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08003178static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003180 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 struct mmc_blk_data *md = mmc_get_drvdata(card);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303182 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 if (md) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303185 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303186 if (rc)
3187 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003188 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani7729aee2013-06-20 18:15:50 +05303189 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303190 if (rc)
3191 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Subhash Jadavanid1c87f12013-02-26 17:32:58 +05303194 goto out;
3195
3196 out_resume:
3197 mmc_queue_resume(&md->queue);
3198 list_for_each_entry(part_md, &md->part, part) {
3199 mmc_queue_resume(&part_md->queue);
3200 }
3201 out:
3202 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
3205static int mmc_blk_resume(struct mmc_card *card)
3206{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003207 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 struct mmc_blk_data *md = mmc_get_drvdata(card);
3209
3210 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003211 /*
3212 * Resume involves the card going into idle state,
3213 * so current partition is always the main one.
3214 */
3215 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003217 list_for_each_entry(part_md, &md->part, part) {
3218 mmc_queue_resume(&part_md->queue);
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
3221 return 0;
3222}
3223#else
3224#define mmc_blk_suspend NULL
3225#define mmc_blk_resume NULL
3226#endif
3227
3228static struct mmc_driver mmc_driver = {
3229 .drv = {
3230 .name = "mmcblk",
3231 },
3232 .probe = mmc_blk_probe,
3233 .remove = mmc_blk_remove,
3234 .suspend = mmc_blk_suspend,
3235 .resume = mmc_blk_resume,
Tatyana Brokhmand8127382013-05-28 09:45:17 +03003236 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237};
3238
3239static int __init mmc_blk_init(void)
3240{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003241 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003243 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3244 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3245
3246 max_devices = 256 / perdev_minors;
3247
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003248 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3249 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003252 res = mmc_register_driver(&mmc_driver);
3253 if (res)
3254 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003256 return 0;
3257 out2:
3258 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 out:
3260 return res;
3261}
3262
3263static void __exit mmc_blk_exit(void)
3264{
3265 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003266 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
3269module_init(mmc_blk_init);
3270module_exit(mmc_blk_exit);
3271
3272MODULE_LICENSE("GPL");
3273MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");