blob: 9ee19ab69d624ec460d153da5b0b036098c6021f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e3222006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani914fa652013-01-07 17:31:43 +053060#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050061
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030062#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
63
Seungwon Jeon53f8f572012-09-27 15:00:26 +020064#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
65 (req->cmd_flags & REQ_META)) && \
66 (rq_data_dir(req) == WRITE))
67#define PACKED_CMD_VER 0x01
68#define PACKED_CMD_WR 0x02
Tatyana Brokhman464fbe12012-10-07 10:33:13 +020069#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
70 do { \
71 if (stats->enabled) \
72 stats->pack_stop_reason[reason]++; \
73 } while (0)
Seungwon Jeon53f8f572012-09-27 15:00:26 +020074
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020075static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040076
77/*
78 * The defaults come from config options but can be overriden by module
79 * or bootarg options.
80 */
81static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82
83/*
84 * We've only got one major, so number of mmcblk devices is
85 * limited to 256 / number of minors per device.
86 */
87static int max_devices;
88
89/* 256 minors, so at most 256 separate devices */
90static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050091static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * There is one mmc_blk_data per slot.
95 */
96struct mmc_blk_data {
97 spinlock_t lock;
98 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000107 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500108 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500109 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
118 * with mmc_card with mmc_set_drvdata, and keeps
119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200124 struct device_attribute num_wr_reqs_to_start_packing;
Maya Ereze1eae982013-01-05 21:51:06 +0200125 struct device_attribute bkops_check_threshold;
Maya Erez64d12462013-01-06 10:19:38 +0200126 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100127 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Arjan van de Vena621aae2006-01-12 18:43:35 +0000130static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200132enum {
133 MMC_PACKED_N_IDX = -1,
134 MMC_PACKED_N_ZERO,
135 MMC_PACKED_N_SINGLE,
136};
137
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400138module_param(perdev_minors, int, 0444);
139MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
140
Loic Pallardy961e37a2012-08-06 15:12:31 +0000141static inline int mmc_blk_part_switch(struct mmc_card *card,
142 struct mmc_blk_data *md);
143static int get_card_status(struct mmc_card *card, u32 *status, int retries);
144
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200145static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
146{
147 mqrq->packed_cmd = MMC_PACKED_NONE;
148 mqrq->packed_num = MMC_PACKED_N_ZERO;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
152{
153 struct mmc_blk_data *md;
154
Arjan van de Vena621aae2006-01-12 18:43:35 +0000155 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 md = disk->private_data;
157 if (md && md->usage == 0)
158 md = NULL;
159 if (md)
160 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return md;
164}
165
Andrei Warkentin371a6892011-04-11 18:10:25 -0500166static inline int mmc_get_devidx(struct gendisk *disk)
167{
Colin Cross71b54d42010-09-03 12:41:21 -0700168 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500169 return devidx;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static void mmc_blk_put(struct mmc_blk_data *md)
173{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000174 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 md->usage--;
176 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500177 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800178 blk_cleanup_queue(md->queue.queue);
179
David Woodhouse1dff3142007-11-21 18:45:12 +0100180 __clear_bit(devidx, dev_use);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(md);
184 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000185 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Johan Rudholmadd710e2011-12-02 08:51:06 +0100188static ssize_t power_ro_lock_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 int ret;
192 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
193 struct mmc_card *card = md->queue.card;
194 int locked = 0;
195
196 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
197 locked = 2;
198 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
199 locked = 1;
200
201 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
202
203 return ret;
204}
205
206static ssize_t power_ro_lock_store(struct device *dev,
207 struct device_attribute *attr, const char *buf, size_t count)
208{
209 int ret;
210 struct mmc_blk_data *md, *part_md;
211 struct mmc_card *card;
212 unsigned long set;
213
214 if (kstrtoul(buf, 0, &set))
215 return -EINVAL;
216
217 if (set != 1)
218 return count;
219
220 md = mmc_blk_get(dev_to_disk(dev));
221 card = md->queue.card;
222
223 mmc_claim_host(card->host);
224
225 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
226 card->ext_csd.boot_ro_lock |
227 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
228 card->ext_csd.part_time);
229 if (ret)
230 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
231 else
232 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
233
234 mmc_release_host(card->host);
235
236 if (!ret) {
237 pr_info("%s: Locking boot partition ro until next power on\n",
238 md->disk->disk_name);
239 set_disk_ro(md->disk, 1);
240
241 list_for_each_entry(part_md, &md->part, part)
242 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
243 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
244 set_disk_ro(part_md->disk, 1);
245 }
246 }
247
248 mmc_blk_put(md);
249 return count;
250}
251
Andrei Warkentin371a6892011-04-11 18:10:25 -0500252static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
253 char *buf)
254{
255 int ret;
256 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
257
258 ret = snprintf(buf, PAGE_SIZE, "%d",
259 get_disk_ro(dev_to_disk(dev)) ^
260 md->read_only);
261 mmc_blk_put(md);
262 return ret;
263}
264
265static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
266 const char *buf, size_t count)
267{
268 int ret;
269 char *end;
270 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
271 unsigned long set = simple_strtoul(buf, &end, 0);
272 if (end == buf) {
273 ret = -EINVAL;
274 goto out;
275 }
276
277 set_disk_ro(dev_to_disk(dev), set || md->read_only);
278 ret = count;
279out:
280 mmc_blk_put(md);
281 return ret;
282}
283
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200284static ssize_t
285num_wr_reqs_to_start_packing_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
289 int num_wr_reqs_to_start_packing;
290 int ret;
291
292 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
293
294 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
295
296 mmc_blk_put(md);
297 return ret;
298}
299
300static ssize_t
301num_wr_reqs_to_start_packing_store(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 int value;
306 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200307 struct mmc_card *card = md->queue.card;
308 int ret = count;
309
310 if (!card) {
311 ret = -EINVAL;
312 goto exit;
313 }
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200314
315 sscanf(buf, "%d", &value);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200316
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200317 if (value >= 0) {
318 md->queue.num_wr_reqs_to_start_packing =
319 min_t(int, value, (int)card->ext_csd.max_packed_writes);
320
321 pr_debug("%s: trigger to pack: new value = %d",
322 mmc_hostname(card->host),
323 md->queue.num_wr_reqs_to_start_packing);
324 } else {
325 pr_err("%s: value %d is not valid. old value remains = %d",
326 mmc_hostname(card->host), value,
327 md->queue.num_wr_reqs_to_start_packing);
328 ret = -EINVAL;
329 }
330
331exit:
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200332 mmc_blk_put(md);
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200333 return ret;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200334}
335
Maya Erez2d33a192013-01-04 15:52:41 +0200336static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200337bkops_check_threshold_show(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200338 struct device_attribute *attr, char *buf)
339{
340 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Maya Erez2d33a192013-01-04 15:52:41 +0200341 struct mmc_card *card = md->queue.card;
342 int ret;
343
344 if (!card)
345 ret = -EINVAL;
Maya Ereze1eae982013-01-05 21:51:06 +0200346 else
Maya Erez2d33a192013-01-04 15:52:41 +0200347 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Maya Ereze1eae982013-01-05 21:51:06 +0200348 card->bkops_info.size_percentage_to_queue_delayed_work);
Maya Erez2d33a192013-01-04 15:52:41 +0200349
350 mmc_blk_put(md);
351 return ret;
352}
353
354static ssize_t
Maya Ereze1eae982013-01-05 21:51:06 +0200355bkops_check_threshold_store(struct device *dev,
Maya Erez2d33a192013-01-04 15:52:41 +0200356 struct device_attribute *attr,
357 const char *buf, size_t count)
358{
359 int value;
360 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
361 struct mmc_card *card = md->queue.card;
Maya Ereze1eae982013-01-05 21:51:06 +0200362 unsigned int card_size;
363 int ret = count;
Maya Erez2d33a192013-01-04 15:52:41 +0200364
Maya Ereze1eae982013-01-05 21:51:06 +0200365 if (!card) {
366 ret = -EINVAL;
367 goto exit;
368 }
Maya Erez2d33a192013-01-04 15:52:41 +0200369
370 sscanf(buf, "%d", &value);
Maya Ereze1eae982013-01-05 21:51:06 +0200371 if ((value <= 0) || (value >= 100)) {
372 ret = -EINVAL;
373 goto exit;
374 }
Maya Erez2d33a192013-01-04 15:52:41 +0200375
Maya Ereze1eae982013-01-05 21:51:06 +0200376 card_size = (unsigned int)get_capacity(md->disk);
377 if (card_size <= 0) {
378 ret = -EINVAL;
379 goto exit;
380 }
381 card->bkops_info.size_percentage_to_queue_delayed_work = value;
382 card->bkops_info.min_sectors_to_queue_delayed_work =
383 (card_size * value) / 100;
384
385 pr_debug("%s: size_percentage = %d, min_sectors = %d",
386 mmc_hostname(card->host),
387 card->bkops_info.size_percentage_to_queue_delayed_work,
388 card->bkops_info.min_sectors_to_queue_delayed_work);
389
390exit:
Maya Erez2d33a192013-01-04 15:52:41 +0200391 mmc_blk_put(md);
392 return count;
393}
394
Maya Erez64d12462013-01-06 10:19:38 +0200395static ssize_t
396no_pack_for_random_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
398{
399 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
400 int ret;
401
402 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
403
404 mmc_blk_put(md);
405 return ret;
406}
407
408static ssize_t
409no_pack_for_random_store(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
412{
413 int value;
414 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
415 struct mmc_card *card = md->queue.card;
416 int ret = count;
417
418 if (!card) {
419 ret = -EINVAL;
420 goto exit;
421 }
422
423 sscanf(buf, "%d", &value);
424
425 if (value < 0) {
426 pr_err("%s: value %d is not valid. old value remains = %d",
427 mmc_hostname(card->host), value,
428 md->queue.no_pack_for_random);
429 ret = -EINVAL;
430 goto exit;
431 }
432
433 md->queue.no_pack_for_random = (value > 0) ? true : false;
434
435 pr_debug("%s: no_pack_for_random: new value = %d",
436 mmc_hostname(card->host),
437 md->queue.no_pack_for_random);
438
439exit:
440 mmc_blk_put(md);
441 return ret;
442}
443
Al Viroa5a15612008-03-02 10:33:30 -0500444static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Al Viroa5a15612008-03-02 10:33:30 -0500446 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int ret = -ENXIO;
448
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200449 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (md) {
451 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500452 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700454
Al Viroa5a15612008-03-02 10:33:30 -0500455 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700456 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700457 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200460 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return ret;
463}
464
Al Viroa5a15612008-03-02 10:33:30 -0500465static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Al Viroa5a15612008-03-02 10:33:30 -0500467 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200469 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200471 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473}
474
475static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800476mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800478 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
479 geo->heads = 4;
480 geo->sectors = 16;
481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
John Calixtocb87ea22011-04-26 18:56:29 -0400484struct mmc_blk_ioc_data {
485 struct mmc_ioc_cmd ic;
486 unsigned char *buf;
487 u64 buf_bytes;
488};
489
490static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
491 struct mmc_ioc_cmd __user *user)
492{
493 struct mmc_blk_ioc_data *idata;
494 int err;
495
496 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
497 if (!idata) {
498 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400499 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400500 }
501
502 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
503 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400504 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400505 }
506
507 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
508 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
509 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400510 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400511 }
512
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100513 if (!idata->buf_bytes)
514 return idata;
515
John Calixtocb87ea22011-04-26 18:56:29 -0400516 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
517 if (!idata->buf) {
518 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400519 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400520 }
521
522 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
523 idata->ic.data_ptr, idata->buf_bytes)) {
524 err = -EFAULT;
525 goto copy_err;
526 }
527
528 return idata;
529
530copy_err:
531 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400532idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400533 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400534out:
John Calixtocb87ea22011-04-26 18:56:29 -0400535 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400536}
537
Loic Pallardy961e37a2012-08-06 15:12:31 +0000538static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
539 u32 retries_max)
540{
541 int err;
542 u32 retry_count = 0;
543
544 if (!status || !retries_max)
545 return -EINVAL;
546
547 do {
548 err = get_card_status(card, status, 5);
549 if (err)
550 break;
551
552 if (!R1_STATUS(*status) &&
553 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
554 break; /* RPMB programming operation complete */
555
556 /*
557 * Rechedule to give the MMC device a chance to continue
558 * processing the previous command without being polled too
559 * frequently.
560 */
561 usleep_range(1000, 5000);
562 } while (++retry_count < retries_max);
563
564 if (retry_count == retries_max)
565 err = -EPERM;
566
567 return err;
568}
569
John Calixtocb87ea22011-04-26 18:56:29 -0400570static int mmc_blk_ioctl_cmd(struct block_device *bdev,
571 struct mmc_ioc_cmd __user *ic_ptr)
572{
573 struct mmc_blk_ioc_data *idata;
574 struct mmc_blk_data *md;
575 struct mmc_card *card;
576 struct mmc_command cmd = {0};
577 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530578 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400579 struct scatterlist sg;
580 int err;
581
582 /*
583 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
584 * whole block device, not on a partition. This prevents overspray
585 * between sibling partitions.
586 */
587 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
588 return -EPERM;
589
590 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
591 if (IS_ERR(idata))
592 return PTR_ERR(idata);
593
John Calixtocb87ea22011-04-26 18:56:29 -0400594 md = mmc_blk_get(bdev->bd_disk);
595 if (!md) {
596 err = -EINVAL;
597 goto cmd_done;
598 }
599
600 card = md->queue.card;
601 if (IS_ERR(card)) {
602 err = PTR_ERR(card);
603 goto cmd_done;
604 }
605
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100606 cmd.opcode = idata->ic.opcode;
607 cmd.arg = idata->ic.arg;
608 cmd.flags = idata->ic.flags;
609
610 if (idata->buf_bytes) {
611 data.sg = &sg;
612 data.sg_len = 1;
613 data.blksz = idata->ic.blksz;
614 data.blocks = idata->ic.blocks;
615
616 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
617
618 if (idata->ic.write_flag)
619 data.flags = MMC_DATA_WRITE;
620 else
621 data.flags = MMC_DATA_READ;
622
623 /* data.flags must already be set before doing this. */
624 mmc_set_data_timeout(&data, card);
625
626 /* Allow overriding the timeout_ns for empirical tuning. */
627 if (idata->ic.data_timeout_ns)
628 data.timeout_ns = idata->ic.data_timeout_ns;
629
630 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
631 /*
632 * Pretend this is a data transfer and rely on the
633 * host driver to compute timeout. When all host
634 * drivers support cmd.cmd_timeout for R1B, this
635 * can be changed to:
636 *
637 * mrq.data = NULL;
638 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
639 */
640 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
641 }
642
643 mrq.data = &data;
644 }
645
646 mrq.cmd = &cmd;
647
John Calixtocb87ea22011-04-26 18:56:29 -0400648 mmc_claim_host(card->host);
649
Loic Pallardy961e37a2012-08-06 15:12:31 +0000650 err = mmc_blk_part_switch(card, md);
651 if (err)
652 goto cmd_rel_host;
653
John Calixtocb87ea22011-04-26 18:56:29 -0400654 if (idata->ic.is_acmd) {
655 err = mmc_app_cmd(card->host, card);
656 if (err)
657 goto cmd_rel_host;
658 }
659
John Calixtocb87ea22011-04-26 18:56:29 -0400660 mmc_wait_for_req(card->host, &mrq);
661
662 if (cmd.error) {
663 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
664 __func__, cmd.error);
665 err = cmd.error;
666 goto cmd_rel_host;
667 }
668 if (data.error) {
669 dev_err(mmc_dev(card->host), "%s: data error %d\n",
670 __func__, data.error);
671 err = data.error;
672 goto cmd_rel_host;
673 }
674
675 /*
676 * According to the SD specs, some commands require a delay after
677 * issuing the command.
678 */
679 if (idata->ic.postsleep_min_us)
680 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
681
682 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
683 err = -EFAULT;
684 goto cmd_rel_host;
685 }
686
687 if (!idata->ic.write_flag) {
688 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
689 idata->buf, idata->buf_bytes)) {
690 err = -EFAULT;
691 goto cmd_rel_host;
692 }
693 }
694
Krishna Kondabfa7df92012-11-20 20:26:05 -0800695cmd_rel_host:
696 mmc_release_host(card->host);
697
698cmd_done:
699 mmc_blk_put(md);
700 kfree(idata->buf);
701 kfree(idata);
702 return err;
703}
704
705struct mmc_blk_ioc_rpmb_data {
706 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
707};
708
709static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
710 struct mmc_ioc_rpmb __user *user)
711{
712 struct mmc_blk_ioc_rpmb_data *idata;
713 int err, i;
714
715 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
716 if (!idata) {
717 err = -ENOMEM;
718 goto out;
719 }
720
721 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
722 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
723 if (IS_ERR(idata->data[i])) {
724 err = PTR_ERR(idata->data[i]);
725 goto copy_err;
726 }
727 }
728
729 return idata;
730
731copy_err:
732 while (--i >= 0) {
733 kfree(idata->data[i]->buf);
734 kfree(idata->data[i]);
735 }
736 kfree(idata);
737out:
738 return ERR_PTR(err);
739}
740
741static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
742 struct mmc_ioc_rpmb __user *ic_ptr)
743{
744 struct mmc_blk_ioc_rpmb_data *idata;
745 struct mmc_blk_data *md;
746 struct mmc_card *card;
747 struct mmc_command cmd = {0};
748 struct mmc_data data = {0};
749 struct mmc_request mrq = {NULL};
750 struct scatterlist sg;
751 int err = 0, i = 0;
752 u32 status = 0;
753
754 /* The caller must have CAP_SYS_RAWIO */
755 if (!capable(CAP_SYS_RAWIO))
756 return -EPERM;
757
758 md = mmc_blk_get(bdev->bd_disk);
759 /* make sure this is a rpmb partition */
760 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
761 err = -EINVAL;
762 goto cmd_done;
763 }
764
765 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
766 if (IS_ERR(idata)) {
767 err = PTR_ERR(idata);
768 goto cmd_done;
769 }
770
771 card = md->queue.card;
772 if (IS_ERR(card)) {
773 err = PTR_ERR(card);
774 goto idata_free;
775 }
776
777 mmc_claim_host(card->host);
778
779 err = mmc_blk_part_switch(card, md);
780 if (err)
781 goto cmd_rel_host;
782
783 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
784 struct mmc_blk_ioc_data *curr_data;
785 struct mmc_ioc_cmd *curr_cmd;
786
787 curr_data = idata->data[i];
788 curr_cmd = &curr_data->ic;
789 if (!curr_cmd->opcode)
790 break;
791
792 cmd.opcode = curr_cmd->opcode;
793 cmd.arg = curr_cmd->arg;
794 cmd.flags = curr_cmd->flags;
795
796 if (curr_data->buf_bytes) {
797 data.sg = &sg;
798 data.sg_len = 1;
799 data.blksz = curr_cmd->blksz;
800 data.blocks = curr_cmd->blocks;
801
802 sg_init_one(data.sg, curr_data->buf,
803 curr_data->buf_bytes);
804
805 if (curr_cmd->write_flag)
806 data.flags = MMC_DATA_WRITE;
807 else
808 data.flags = MMC_DATA_READ;
809
810 /* data.flags must already be set before doing this. */
811 mmc_set_data_timeout(&data, card);
812
813 /*
814 * Allow overriding the timeout_ns for empirical tuning.
815 */
816 if (curr_cmd->data_timeout_ns)
817 data.timeout_ns = curr_cmd->data_timeout_ns;
818
819 mrq.data = &data;
820 }
821
822 mrq.cmd = &cmd;
823
824 err = mmc_set_blockcount(card, data.blocks,
825 curr_cmd->write_flag & (1 << 31));
826 if (err)
827 goto cmd_rel_host;
828
829 mmc_wait_for_req(card->host, &mrq);
830
831 if (cmd.error) {
832 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
833 __func__, cmd.error);
834 err = cmd.error;
835 goto cmd_rel_host;
836 }
837 if (data.error) {
838 dev_err(mmc_dev(card->host), "%s: data error %d\n",
839 __func__, data.error);
840 err = data.error;
841 goto cmd_rel_host;
842 }
843
844 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
845 sizeof(cmd.resp))) {
846 err = -EFAULT;
847 goto cmd_rel_host;
848 }
849
850 if (!curr_cmd->write_flag) {
851 if (copy_to_user((void __user *)(unsigned long)
852 curr_cmd->data_ptr,
853 curr_data->buf,
854 curr_data->buf_bytes)) {
855 err = -EFAULT;
856 goto cmd_rel_host;
857 }
858 }
859
Loic Pallardy961e37a2012-08-06 15:12:31 +0000860 /*
861 * Ensure RPMB command has completed by polling CMD13
862 * "Send Status".
863 */
864 err = ioctl_rpmb_card_status_poll(card, &status, 5);
865 if (err)
866 dev_err(mmc_dev(card->host),
867 "%s: Card Status=0x%08X, error %d\n",
868 __func__, status, err);
869 }
870
John Calixtocb87ea22011-04-26 18:56:29 -0400871cmd_rel_host:
872 mmc_release_host(card->host);
873
Krishna Kondabfa7df92012-11-20 20:26:05 -0800874idata_free:
875 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
876 kfree(idata->data[i]->buf);
877 kfree(idata->data[i]);
878 }
879 kfree(idata);
880
John Calixtocb87ea22011-04-26 18:56:29 -0400881cmd_done:
882 mmc_blk_put(md);
John Calixtocb87ea22011-04-26 18:56:29 -0400883 return err;
884}
885
886static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
887 unsigned int cmd, unsigned long arg)
888{
889 int ret = -EINVAL;
890 if (cmd == MMC_IOC_CMD)
891 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
Krishna Kondabfa7df92012-11-20 20:26:05 -0800892 if (cmd == MMC_IOC_RPMB_CMD)
893 ret = mmc_blk_ioctl_rpmb_cmd(bdev,
894 (struct mmc_ioc_rpmb __user *)arg);
John Calixtocb87ea22011-04-26 18:56:29 -0400895 return ret;
896}
897
898#ifdef CONFIG_COMPAT
899static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
900 unsigned int cmd, unsigned long arg)
901{
902 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
903}
904#endif
905
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700906static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500907 .open = mmc_blk_open,
908 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800909 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400911 .ioctl = mmc_blk_ioctl,
912#ifdef CONFIG_COMPAT
913 .compat_ioctl = mmc_blk_compat_ioctl,
914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915};
916
Andrei Warkentin371a6892011-04-11 18:10:25 -0500917static inline int mmc_blk_part_switch(struct mmc_card *card,
918 struct mmc_blk_data *md)
919{
920 int ret;
921 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300922
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800923 if ((main_md->part_curr == md->part_type) &&
924 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -0500925 return 0;
926
927 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300928 u8 part_config = card->ext_csd.part_config;
929
930 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
931 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500932
933 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300934 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500935 card->ext_csd.part_time);
936 if (ret)
937 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300938
939 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemi151b4772013-01-03 11:32:53 -0800940 card->part_curr = md->part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300941 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500942
943 main_md->part_curr = md->part_type;
944 return 0;
945}
946
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700947static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
948{
949 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100950 u32 result;
951 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700952
Venkatraman Sad5fd972011-08-25 00:30:50 +0530953 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400954 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400955 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700956
957 struct scatterlist sg;
958
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700959 cmd.opcode = MMC_APP_CMD;
960 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700961 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700962
963 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700964 if (err)
965 return (u32)-1;
966 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700967 return (u32)-1;
968
969 memset(&cmd, 0, sizeof(struct mmc_command));
970
971 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
972 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700973 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700974
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700975 data.blksz = 4;
976 data.blocks = 1;
977 data.flags = MMC_DATA_READ;
978 data.sg = &sg;
979 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530980 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700981
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700982 mrq.cmd = &cmd;
983 mrq.data = &data;
984
Ben Dooks051913d2009-06-08 23:33:57 +0100985 blocks = kmalloc(4, GFP_KERNEL);
986 if (!blocks)
987 return (u32)-1;
988
989 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700990
991 mmc_wait_for_req(card->host, &mrq);
992
Ben Dooks051913d2009-06-08 23:33:57 +0100993 result = ntohl(*blocks);
994 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700995
Ben Dooks051913d2009-06-08 23:33:57 +0100996 if (cmd.error || data.error)
997 result = (u32)-1;
998
999 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001000}
1001
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001002static int send_stop(struct mmc_card *card, u32 *status)
1003{
1004 struct mmc_command cmd = {0};
1005 int err;
1006
1007 cmd.opcode = MMC_STOP_TRANSMISSION;
1008 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1009 err = mmc_wait_for_cmd(card->host, &cmd, 5);
1010 if (err == 0)
1011 *status = cmd.resp[0];
1012 return err;
1013}
1014
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001015static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001016{
Chris Ball1278dba2011-04-13 23:40:30 -04001017 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001018 int err;
1019
Adrian Hunter504f1912008-10-16 12:55:25 +03001020 cmd.opcode = MMC_SEND_STATUS;
1021 if (!mmc_host_is_spi(card->host))
1022 cmd.arg = card->rca << 16;
1023 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001024 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1025 if (err == 0)
1026 *status = cmd.resp[0];
1027 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001028}
1029
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301030#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001031#define ERR_RETRY 2
1032#define ERR_ABORT 1
1033#define ERR_CONTINUE 0
1034
1035static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1036 bool status_valid, u32 status)
1037{
1038 switch (error) {
1039 case -EILSEQ:
1040 /* response crc error, retry the r/w cmd */
1041 pr_err("%s: %s sending %s command, card status %#x\n",
1042 req->rq_disk->disk_name, "response CRC error",
1043 name, status);
1044 return ERR_RETRY;
1045
1046 case -ETIMEDOUT:
1047 pr_err("%s: %s sending %s command, card status %#x\n",
1048 req->rq_disk->disk_name, "timed out", name, status);
1049
1050 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001051 if (!status_valid) {
1052 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001054 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001055 /*
1056 * If it was a r/w cmd crc error, or illegal command
1057 * (eg, issued in wrong state) then retry - we should
1058 * have corrected the state problem above.
1059 */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001060 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1061 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001062 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001063 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001064
1065 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001066 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001067 return ERR_ABORT;
1068
1069 default:
1070 /* We don't understand the error code the driver gave us */
1071 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1072 req->rq_disk->disk_name, error, status);
1073 return ERR_ABORT;
1074 }
1075}
1076
1077/*
1078 * Initial r/w and stop cmd error recovery.
1079 * We don't know whether the card received the r/w cmd or not, so try to
1080 * restore things back to a sane state. Essentially, we do this as follows:
1081 * - Obtain card status. If the first attempt to obtain card status fails,
1082 * the status word will reflect the failed status cmd, not the failed
1083 * r/w cmd. If we fail to obtain card status, it suggests we can no
1084 * longer communicate with the card.
1085 * - Check the card state. If the card received the cmd but there was a
1086 * transient problem with the response, it might still be in a data transfer
1087 * mode. Try to send it a stop command. If this fails, we can't recover.
1088 * - If the r/w cmd failed due to a response CRC error, it was probably
1089 * transient, so retry the cmd.
1090 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1091 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1092 * illegal cmd, retry.
1093 * Otherwise we don't understand what happened, so abort.
1094 */
1095static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +03001096 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001097{
1098 bool prev_cmd_status_valid = true;
1099 u32 status, stop_status = 0;
1100 int err, retry;
1101
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301102 if (mmc_card_removed(card))
1103 return ERR_NOMEDIUM;
1104
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001105 /*
1106 * Try to get card status which indicates both the card state
1107 * and why there was no response. If the first attempt fails,
1108 * we can't be sure the returned status is for the r/w command.
1109 */
1110 for (retry = 2; retry >= 0; retry--) {
1111 err = get_card_status(card, &status, 0);
1112 if (!err)
1113 break;
1114
1115 prev_cmd_status_valid = false;
1116 pr_err("%s: error %d sending status command, %sing\n",
1117 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1118 }
1119
1120 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301121 if (err) {
1122 /* Check if the card is removed */
1123 if (mmc_detect_card_removed(card->host))
1124 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001125 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301126 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001127
Adrian Hunter67716322011-08-29 16:42:15 +03001128 /* Flag ECC errors */
1129 if ((status & R1_CARD_ECC_FAILED) ||
1130 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1131 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1132 *ecc_err = 1;
1133
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001134 /*
1135 * Check the current card state. If it is in some data transfer
1136 * mode, tell it to stop (and hopefully transition back to TRAN.)
1137 */
1138 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1139 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1140 err = send_stop(card, &stop_status);
1141 if (err)
1142 pr_err("%s: error %d sending stop command\n",
1143 req->rq_disk->disk_name, err);
1144
1145 /*
1146 * If the stop cmd also timed out, the card is probably
1147 * not present, so abort. Other errors are bad news too.
1148 */
1149 if (err)
1150 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +03001151 if (stop_status & R1_CARD_ECC_FAILED)
1152 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001153 }
1154
1155 /* Check for set block count errors */
1156 if (brq->sbc.error)
1157 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1158 prev_cmd_status_valid, status);
1159
1160 /* Check for r/w command errors */
1161 if (brq->cmd.error)
1162 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1163 prev_cmd_status_valid, status);
1164
Adrian Hunter67716322011-08-29 16:42:15 +03001165 /* Data errors */
1166 if (!brq->stop.error)
1167 return ERR_CONTINUE;
1168
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001169 /* Now for stop errors. These aren't fatal to the transfer. */
1170 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1171 req->rq_disk->disk_name, brq->stop.error,
1172 brq->cmd.resp[0], status);
1173
1174 /*
1175 * Subsitute in our own stop status as this will give the error
1176 * state which happened during the execution of the r/w command.
1177 */
1178 if (stop_status) {
1179 brq->stop.resp[0] = stop_status;
1180 brq->stop.error = 0;
1181 }
1182 return ERR_CONTINUE;
1183}
1184
Adrian Hunter67716322011-08-29 16:42:15 +03001185static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1186 int type)
1187{
1188 int err;
1189
1190 if (md->reset_done & type)
1191 return -EEXIST;
1192
1193 md->reset_done |= type;
1194 err = mmc_hw_reset(host);
1195 /* Ensure we switch back to the correct partition */
1196 if (err != -EOPNOTSUPP) {
1197 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
1198 int part_err;
1199
1200 main_md->part_curr = main_md->part_type;
1201 part_err = mmc_blk_part_switch(host->card, md);
1202 if (part_err) {
1203 /*
1204 * We have failed to get back into the correct
1205 * partition, so we need to abort the whole request.
1206 */
1207 return -ENODEV;
1208 }
1209 }
1210 return err;
1211}
1212
1213static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1214{
1215 md->reset_done &= ~type;
1216}
1217
Adrian Hunterbd788c92010-08-11 14:17:47 -07001218static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1219{
1220 struct mmc_blk_data *md = mq->data;
1221 struct mmc_card *card = md->queue.card;
1222 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001223 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001224
Adrian Hunterbd788c92010-08-11 14:17:47 -07001225 if (!mmc_can_erase(card)) {
1226 err = -EOPNOTSUPP;
1227 goto out;
1228 }
1229
1230 from = blk_rq_pos(req);
1231 nr = blk_rq_sectors(req);
1232
Maya Erez2d33a192013-01-04 15:52:41 +02001233 if (card->ext_csd.bkops_en)
1234 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1235
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001236 if (mmc_can_discard(card))
1237 arg = MMC_DISCARD_ARG;
1238 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001239 arg = MMC_TRIM_ARG;
1240 else
1241 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001242retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001243 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245 INAND_CMD38_ARG_EXT_CSD,
1246 arg == MMC_TRIM_ARG ?
1247 INAND_CMD38_ARG_TRIM :
1248 INAND_CMD38_ARG_ERASE,
1249 0);
1250 if (err)
1251 goto out;
1252 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001253 err = mmc_erase(card, from, nr, arg);
1254out:
Adrian Hunter67716322011-08-29 16:42:15 +03001255 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1256 goto retry;
1257 if (!err)
1258 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301259 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001260
Adrian Hunterbd788c92010-08-11 14:17:47 -07001261 return err ? 0 : 1;
1262}
1263
Adrian Hunter49804542010-08-11 14:17:50 -07001264static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1265 struct request *req)
1266{
1267 struct mmc_blk_data *md = mq->data;
1268 struct mmc_card *card = md->queue.card;
1269 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001270 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001271
Maya Erez463bb952012-05-24 23:46:29 +03001272 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001273 err = -EOPNOTSUPP;
1274 goto out;
1275 }
1276
1277 from = blk_rq_pos(req);
1278 nr = blk_rq_sectors(req);
1279
1280 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1281 arg = MMC_SECURE_TRIM1_ARG;
1282 else
1283 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001284retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001285 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1286 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1287 INAND_CMD38_ARG_EXT_CSD,
1288 arg == MMC_SECURE_TRIM1_ARG ?
1289 INAND_CMD38_ARG_SECTRIM1 :
1290 INAND_CMD38_ARG_SECERASE,
1291 0);
1292 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001293 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001294 }
Adrian Hunter28302812012-04-05 14:45:48 +03001295
Adrian Hunter49804542010-08-11 14:17:50 -07001296 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001297 if (err == -EIO)
1298 goto out_retry;
1299 if (err)
1300 goto out;
1301
1302 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001303 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1304 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1305 INAND_CMD38_ARG_EXT_CSD,
1306 INAND_CMD38_ARG_SECTRIM2,
1307 0);
1308 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001309 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001310 }
Adrian Hunter28302812012-04-05 14:45:48 +03001311
Adrian Hunter49804542010-08-11 14:17:50 -07001312 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001313 if (err == -EIO)
1314 goto out_retry;
1315 if (err)
1316 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001317 }
Adrian Hunter28302812012-04-05 14:45:48 +03001318
Adrian Hunter28302812012-04-05 14:45:48 +03001319out_retry:
1320 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001321 goto retry;
1322 if (!err)
1323 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001324out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301325 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001326
Adrian Hunter49804542010-08-11 14:17:50 -07001327 return err ? 0 : 1;
1328}
1329
Maya Erez463bb952012-05-24 23:46:29 +03001330static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1331 struct request *req)
1332{
1333 struct mmc_blk_data *md = mq->data;
1334 struct mmc_card *card = md->queue.card;
1335 int err = 0;
1336
1337 BUG_ON(!card);
1338 BUG_ON(!card->host);
1339
1340 if (!(mmc_can_sanitize(card) &&
1341 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1342 pr_warning("%s: %s - SANITIZE is not supported\n",
1343 mmc_hostname(card->host), __func__);
1344 err = -EOPNOTSUPP;
1345 goto out;
1346 }
1347
1348 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1349 mmc_hostname(card->host), __func__);
1350
1351 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001352 EXT_CSD_SANITIZE_START, 1,
1353 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001354
1355 if (err)
1356 pr_err("%s: %s - mmc_switch() with "
1357 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1358 mmc_hostname(card->host), __func__, err);
1359
1360 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1361 __func__);
1362
1363out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301364 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001365
1366 return err ? 0 : 1;
1367}
1368
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001369static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1370{
1371 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001372 struct mmc_card *card = md->queue.card;
1373 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001374
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001375 ret = mmc_flush_cache(card);
1376 if (ret)
1377 ret = -EIO;
1378
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301379 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001380
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001381 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001382}
1383
1384/*
1385 * Reformat current write as a reliable write, supporting
1386 * both legacy and the enhanced reliable write MMC cards.
1387 * In each transfer we'll handle only as much as a single
1388 * reliable write can handle, thus finish the request in
1389 * partial completions.
1390 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001391static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1392 struct mmc_card *card,
1393 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001394{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001395 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1396 /* Legacy mode imposes restrictions on transfers. */
1397 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1398 brq->data.blocks = 1;
1399
1400 if (brq->data.blocks > card->ext_csd.rel_sectors)
1401 brq->data.blocks = card->ext_csd.rel_sectors;
1402 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1403 brq->data.blocks = 1;
1404 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001405}
1406
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001407#define CMD_ERRORS \
1408 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1409 R1_ADDRESS_ERROR | /* Misaligned address */ \
1410 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1411 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1412 R1_CC_ERROR | /* Card controller error */ \
1413 R1_ERROR) /* General/unknown error */
1414
Per Forlinee8a43a2011-07-01 18:55:33 +02001415static int mmc_blk_err_check(struct mmc_card *card,
1416 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001417{
Per Forlinee8a43a2011-07-01 18:55:33 +02001418 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1419 mmc_active);
1420 struct mmc_blk_request *brq = &mq_mrq->brq;
1421 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001422 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001423
1424 /*
1425 * sbc.error indicates a problem with the set block count
1426 * command. No data will have been transferred.
1427 *
1428 * cmd.error indicates a problem with the r/w command. No
1429 * data will have been transferred.
1430 *
1431 * stop.error indicates a problem with the stop command. Data
1432 * may have been transferred, or may still be transferring.
1433 */
Adrian Hunter67716322011-08-29 16:42:15 +03001434 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1435 brq->data.error) {
1436 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001437 case ERR_RETRY:
1438 return MMC_BLK_RETRY;
1439 case ERR_ABORT:
1440 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301441 case ERR_NOMEDIUM:
1442 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001443 case ERR_CONTINUE:
1444 break;
1445 }
1446 }
1447
1448 /*
1449 * Check for errors relating to the execution of the
1450 * initial command - such as address errors. No data
1451 * has been transferred.
1452 */
1453 if (brq->cmd.resp[0] & CMD_ERRORS) {
1454 pr_err("%s: r/w command failed, status = %#x\n",
1455 req->rq_disk->disk_name, brq->cmd.resp[0]);
1456 return MMC_BLK_ABORT;
1457 }
1458
1459 /*
1460 * Everything else is either success, or a data error of some
1461 * kind. If it was a write, we may have transitioned to
1462 * program mode, which we have to wait for it to complete.
1463 */
1464 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1465 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301466 unsigned long timeout;
1467
1468 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001469 do {
1470 int err = get_card_status(card, &status, 5);
1471 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301472 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001473 req->rq_disk->disk_name, err);
1474 return MMC_BLK_CMD_ERR;
1475 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301476
1477 /* Timeout if the device never becomes ready for data
1478 * and never leaves the program state.
1479 */
1480 if (time_after(jiffies, timeout)) {
1481 pr_err("%s: Card stuck in programming state!"\
1482 " %s %s\n", mmc_hostname(card->host),
1483 req->rq_disk->disk_name, __func__);
1484
1485 return MMC_BLK_CMD_ERR;
1486 }
Per Forlind78d4a82011-07-01 18:55:30 +02001487 /*
1488 * Some cards mishandle the status bits,
1489 * so make sure to check both the busy
1490 * indication and the card state.
1491 */
1492 } while (!(status & R1_READY_FOR_DATA) ||
1493 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1494 }
1495
1496 if (brq->data.error) {
1497 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1498 req->rq_disk->disk_name, brq->data.error,
1499 (unsigned)blk_rq_pos(req),
1500 (unsigned)blk_rq_sectors(req),
1501 brq->cmd.resp[0], brq->stop.resp[0]);
1502
1503 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001504 if (ecc_err)
1505 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001506 return MMC_BLK_DATA_ERR;
1507 } else {
1508 return MMC_BLK_CMD_ERR;
1509 }
1510 }
1511
Adrian Hunter67716322011-08-29 16:42:15 +03001512 if (!brq->data.bytes_xfered)
1513 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001514
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001515 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1516 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1517 return MMC_BLK_PARTIAL;
1518 else
1519 return MMC_BLK_SUCCESS;
1520 }
1521
Adrian Hunter67716322011-08-29 16:42:15 +03001522 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1523 return MMC_BLK_PARTIAL;
1524
1525 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001526}
1527
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001528static int mmc_blk_packed_err_check(struct mmc_card *card,
1529 struct mmc_async_req *areq)
1530{
1531 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1532 mmc_active);
1533 struct request *req = mq_rq->req;
1534 int err, check, status;
1535 u8 ext_csd[512];
1536
1537 mq_rq->packed_retries--;
1538 check = mmc_blk_err_check(card, areq);
1539 err = get_card_status(card, &status, 0);
1540 if (err) {
1541 pr_err("%s: error %d sending status command\n",
1542 req->rq_disk->disk_name, err);
1543 return MMC_BLK_ABORT;
1544 }
1545
1546 if (status & R1_EXCEPTION_EVENT) {
1547 err = mmc_send_ext_csd(card, ext_csd);
1548 if (err) {
1549 pr_err("%s: error %d sending ext_csd\n",
1550 req->rq_disk->disk_name, err);
1551 return MMC_BLK_ABORT;
1552 }
1553
1554 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1555 EXT_CSD_PACKED_FAILURE) &&
1556 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1557 EXT_CSD_PACKED_GENERIC_ERROR)) {
1558 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1559 EXT_CSD_PACKED_INDEXED_ERROR) {
1560 mq_rq->packed_fail_idx =
1561 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1562 return MMC_BLK_PARTIAL;
1563 }
1564 }
1565 }
1566
1567 return check;
1568}
1569
Per Forlin54d49d772011-07-01 18:55:29 +02001570static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1571 struct mmc_card *card,
1572 int disable_multi,
1573 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Per Forlin54d49d772011-07-01 18:55:29 +02001575 u32 readcmd, writecmd;
1576 struct mmc_blk_request *brq = &mqrq->brq;
1577 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301579 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001581 /*
1582 * Reliable writes are used to implement Forced Unit Access and
1583 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001584 *
1585 * XXX: this really needs a good explanation of why REQ_META
1586 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001587 */
1588 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1589 (req->cmd_flags & REQ_META)) &&
1590 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001591 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001592
Per Forlin54d49d772011-07-01 18:55:29 +02001593 memset(brq, 0, sizeof(struct mmc_blk_request));
1594 brq->mrq.cmd = &brq->cmd;
1595 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Per Forlin54d49d772011-07-01 18:55:29 +02001597 brq->cmd.arg = blk_rq_pos(req);
1598 if (!mmc_card_blockaddr(card))
1599 brq->cmd.arg <<= 9;
1600 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1601 brq->data.blksz = 512;
1602 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1603 brq->stop.arg = 0;
1604 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1605 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Asutosh Das1a897142012-07-27 18:10:19 +05301607 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001608 /*
1609 * The block layer doesn't support all sector count
1610 * restrictions, so we need to be prepared for too big
1611 * requests.
1612 */
1613 if (brq->data.blocks > card->host->max_blk_count)
1614 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001616 if (brq->data.blocks > 1) {
1617 /*
1618 * After a read error, we redo the request one sector
1619 * at a time in order to accurately determine which
1620 * sectors can be read successfully.
1621 */
1622 if (disable_multi)
1623 brq->data.blocks = 1;
1624
1625 /* Some controllers can't do multiblock reads due to hw bugs */
1626 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1627 rq_data_dir(req) == READ)
1628 brq->data.blocks = 1;
1629 }
Per Forlin54d49d772011-07-01 18:55:29 +02001630
1631 if (brq->data.blocks > 1 || do_rel_wr) {
1632 /* SPI multiblock writes terminate using a special
1633 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001634 */
Per Forlin54d49d772011-07-01 18:55:29 +02001635 if (!mmc_host_is_spi(card->host) ||
1636 rq_data_dir(req) == READ)
1637 brq->mrq.stop = &brq->stop;
1638 readcmd = MMC_READ_MULTIPLE_BLOCK;
1639 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1640 } else {
1641 brq->mrq.stop = NULL;
1642 readcmd = MMC_READ_SINGLE_BLOCK;
1643 writecmd = MMC_WRITE_BLOCK;
1644 }
1645 if (rq_data_dir(req) == READ) {
1646 brq->cmd.opcode = readcmd;
1647 brq->data.flags |= MMC_DATA_READ;
1648 } else {
1649 brq->cmd.opcode = writecmd;
1650 brq->data.flags |= MMC_DATA_WRITE;
1651 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001652
Per Forlin54d49d772011-07-01 18:55:29 +02001653 if (do_rel_wr)
1654 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001655
Per Forlin54d49d772011-07-01 18:55:29 +02001656 /*
Saugata Das42659002011-12-21 13:09:17 +05301657 * Data tag is used only during writing meta data to speed
1658 * up write and any subsequent read of this meta data
1659 */
1660 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1661 (req->cmd_flags & REQ_META) &&
1662 (rq_data_dir(req) == WRITE) &&
1663 ((brq->data.blocks * brq->data.blksz) >=
1664 card->ext_csd.data_tag_unit_size);
1665
1666 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001667 * Pre-defined multi-block transfers are preferable to
1668 * open ended-ones (and necessary for reliable writes).
1669 * However, it is not sufficient to just send CMD23,
1670 * and avoid the final CMD12, as on an error condition
1671 * CMD12 (stop) needs to be sent anyway. This, coupled
1672 * with Auto-CMD23 enhancements provided by some
1673 * hosts, means that the complexity of dealing
1674 * with this is best left to the host. If CMD23 is
1675 * supported by card and host, we'll fill sbc in and let
1676 * the host deal with handling it correctly. This means
1677 * that for hosts that don't expose MMC_CAP_CMD23, no
1678 * change of behavior will be observed.
1679 *
1680 * N.B: Some MMC cards experience perf degradation.
1681 * We'll avoid using CMD23-bounded multiblock writes for
1682 * these, while retaining features like reliable writes.
1683 */
Saugata Das42659002011-12-21 13:09:17 +05301684 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1685 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1686 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001687 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1688 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301689 (do_rel_wr ? (1 << 31) : 0) |
1690 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001691 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1692 brq->mrq.sbc = &brq->sbc;
1693 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001694
Per Forlin54d49d772011-07-01 18:55:29 +02001695 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001696
Per Forlin54d49d772011-07-01 18:55:29 +02001697 brq->data.sg = mqrq->sg;
1698 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001699
Per Forlin54d49d772011-07-01 18:55:29 +02001700 /*
1701 * Adjust the sg list so it is the same size as the
1702 * request.
1703 */
1704 if (brq->data.blocks != blk_rq_sectors(req)) {
1705 int i, data_size = brq->data.blocks << 9;
1706 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001707
Per Forlin54d49d772011-07-01 18:55:29 +02001708 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1709 data_size -= sg->length;
1710 if (data_size <= 0) {
1711 sg->length += data_size;
1712 i++;
1713 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001714 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001715 }
Per Forlin54d49d772011-07-01 18:55:29 +02001716 brq->data.sg_len = i;
1717 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001718
Per Forlinee8a43a2011-07-01 18:55:33 +02001719 mqrq->mmc_active.mrq = &brq->mrq;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001720 if (mq->err_check_fn)
1721 mqrq->mmc_active.err_check = mq->err_check_fn;
1722 else
1723 mqrq->mmc_active.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001724
Per Forlin54d49d772011-07-01 18:55:29 +02001725 mmc_queue_bounce_pre(mqrq);
1726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001728/**
1729 * mmc_blk_disable_wr_packing() - disables packing mode
1730 * @mq: MMC queue.
1731 *
1732 */
1733void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
1734{
1735 if (mq) {
1736 mq->wr_packing_enabled = false;
1737 mq->num_of_potential_packed_wr_reqs = 0;
1738 }
1739}
1740EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
1741
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001742static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1743 struct request *req)
1744{
1745 struct mmc_host *host = mq->card->host;
1746 int data_dir;
1747
1748 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1749 return;
1750
Maya Erez6f219eb2012-12-02 13:27:15 +02001751 /* Support for the write packing on eMMC 4.5 or later */
1752 if (mq->card->ext_csd.rev <= 5)
1753 return;
1754
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001755 /*
1756 * In case the packing control is not supported by the host, it should
1757 * not have an effect on the write packing. Therefore we have to enable
1758 * the write packing
1759 */
1760 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1761 mq->wr_packing_enabled = true;
1762 return;
1763 }
1764
1765 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1766 if (mq->num_of_potential_packed_wr_reqs >
1767 mq->num_wr_reqs_to_start_packing)
1768 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001769 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001770 return;
1771 }
1772
1773 data_dir = rq_data_dir(req);
1774
1775 if (data_dir == READ) {
Konstantin Dorfmand20df952013-02-05 15:45:53 +02001776 mmc_blk_disable_wr_packing(mq);
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001777 return;
1778 } else if (data_dir == WRITE) {
1779 mq->num_of_potential_packed_wr_reqs++;
1780 }
1781
1782 if (mq->num_of_potential_packed_wr_reqs >
1783 mq->num_wr_reqs_to_start_packing)
1784 mq->wr_packing_enabled = true;
1785
1786}
1787
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001788struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1789{
1790 if (!card)
1791 return NULL;
1792
1793 return &card->wr_pack_stats;
1794}
1795EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1796
1797void mmc_blk_init_packed_statistics(struct mmc_card *card)
1798{
1799 int max_num_of_packed_reqs = 0;
1800
1801 if (!card || !card->wr_pack_stats.packing_events)
1802 return;
1803
1804 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1805
1806 spin_lock(&card->wr_pack_stats.lock);
1807 memset(card->wr_pack_stats.packing_events, 0,
1808 (max_num_of_packed_reqs + 1) *
1809 sizeof(*card->wr_pack_stats.packing_events));
1810 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1811 sizeof(card->wr_pack_stats.pack_stop_reason));
1812 card->wr_pack_stats.enabled = true;
1813 spin_unlock(&card->wr_pack_stats.lock);
1814}
1815EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1816
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001817static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1818{
1819 struct request_queue *q = mq->queue;
1820 struct mmc_card *card = mq->card;
1821 struct request *cur = req, *next = NULL;
1822 struct mmc_blk_data *md = mq->data;
1823 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1824 unsigned int req_sectors = 0, phys_segments = 0;
1825 unsigned int max_blk_count, max_phys_segs;
1826 u8 put_back = 0;
1827 u8 max_packed_rw = 0;
1828 u8 reqs = 0;
Lee Susmand50afb52013-01-09 14:48:47 +02001829 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001830
1831 mmc_blk_clear_packed(mq->mqrq_cur);
1832
1833 if (!(md->flags & MMC_BLK_CMD23) ||
1834 !card->ext_csd.packed_event_en)
1835 goto no_packed;
1836
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001837 if (!mq->wr_packing_enabled)
1838 goto no_packed;
1839
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001840 if ((rq_data_dir(cur) == WRITE) &&
1841 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1842 max_packed_rw = card->ext_csd.max_packed_writes;
1843
1844 if (max_packed_rw == 0)
1845 goto no_packed;
1846
1847 if (mmc_req_rel_wr(cur) &&
1848 (md->flags & MMC_BLK_REL_WR) &&
1849 !en_rel_wr)
1850 goto no_packed;
1851
1852 if (mmc_large_sec(card) &&
1853 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1854 goto no_packed;
1855
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02001856 if (cur->cmd_flags & REQ_FUA)
1857 goto no_packed;
1858
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001859 max_blk_count = min(card->host->max_blk_count,
1860 card->host->max_req_size >> 9);
1861 if (unlikely(max_blk_count > 0xffff))
1862 max_blk_count = 0xffff;
1863
1864 max_phys_segs = queue_max_segments(q);
1865 req_sectors += blk_rq_sectors(cur);
1866 phys_segments += cur->nr_phys_segments;
1867
1868 if (rq_data_dir(cur) == WRITE) {
1869 req_sectors++;
1870 phys_segments++;
1871 }
1872
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001873 spin_lock(&stats->lock);
1874
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001875 while (reqs < max_packed_rw - 1) {
1876 spin_lock_irq(q->queue_lock);
1877 next = blk_fetch_request(q);
1878 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001879 if (!next) {
1880 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001881 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001882 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001883
1884 if (mmc_large_sec(card) &&
1885 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001886 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001887 put_back = 1;
1888 break;
1889 }
1890
1891 if (next->cmd_flags & REQ_DISCARD ||
1892 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001893 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001894 put_back = 1;
1895 break;
1896 }
1897
Konstantin Dorfmanb0832972013-02-05 16:26:19 +02001898 if (next->cmd_flags & REQ_FUA) {
1899 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
1900 put_back = 1;
1901 break;
1902 }
1903
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001904 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001905 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001906 put_back = 1;
1907 break;
1908 }
1909
1910 if (mmc_req_rel_wr(next) &&
1911 (md->flags & MMC_BLK_REL_WR) &&
1912 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001913 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001914 put_back = 1;
1915 break;
1916 }
1917
1918 req_sectors += blk_rq_sectors(next);
1919 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001920 if (stats->enabled)
1921 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001922 put_back = 1;
1923 break;
1924 }
1925
1926 phys_segments += next->nr_phys_segments;
1927 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001928 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001929 put_back = 1;
1930 break;
1931 }
1932
Maya Erez64d12462013-01-06 10:19:38 +02001933 if (mq->no_pack_for_random) {
1934 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
1935 blk_rq_pos(next)) {
1936 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
1937 put_back = 1;
1938 break;
1939 }
1940 }
1941
Maya Erez2d33a192013-01-04 15:52:41 +02001942 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001943 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02001944 if (card->ext_csd.bkops_en)
1945 card->bkops_info.sectors_changed +=
1946 blk_rq_sectors(next);
1947 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001948 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1949 cur = next;
1950 reqs++;
1951 }
1952
1953 if (put_back) {
1954 spin_lock_irq(q->queue_lock);
1955 blk_requeue_request(q, next);
1956 spin_unlock_irq(q->queue_lock);
1957 }
1958
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001959 if (stats->enabled) {
1960 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1961 stats->packing_events[reqs + 1]++;
1962 if (reqs + 1 == max_packed_rw)
1963 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1964 }
1965
1966 spin_unlock(&stats->lock);
1967
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001968 if (reqs > 0) {
1969 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1970 mq->mqrq_cur->packed_num = ++reqs;
1971 mq->mqrq_cur->packed_retries = reqs;
1972 return reqs;
1973 }
1974
1975no_packed:
1976 mmc_blk_clear_packed(mq->mqrq_cur);
1977 return 0;
1978}
1979
1980static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1981 struct mmc_card *card,
1982 struct mmc_queue *mq)
1983{
1984 struct mmc_blk_request *brq = &mqrq->brq;
1985 struct request *req = mqrq->req;
1986 struct request *prq;
1987 struct mmc_blk_data *md = mq->data;
1988 bool do_rel_wr, do_data_tag;
1989 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1990 u8 i = 1;
1991
1992 mqrq->packed_cmd = MMC_PACKED_WRITE;
1993 mqrq->packed_blocks = 0;
1994 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1995
1996 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1997 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1998 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1999
2000 /*
2001 * Argument for each entry of packed group
2002 */
2003 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
2004 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2005 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2006 (prq->cmd_flags & REQ_META) &&
2007 (rq_data_dir(prq) == WRITE) &&
2008 ((brq->data.blocks * brq->data.blksz) >=
2009 card->ext_csd.data_tag_unit_size);
2010 /* Argument of CMD23 */
2011 packed_cmd_hdr[(i * 2)] =
2012 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2013 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
2014 blk_rq_sectors(prq);
2015 /* Argument of CMD18 or CMD25 */
2016 packed_cmd_hdr[((i * 2)) + 1] =
2017 mmc_card_blockaddr(card) ?
2018 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
2019 mqrq->packed_blocks += blk_rq_sectors(prq);
2020 i++;
2021 }
2022
2023 memset(brq, 0, sizeof(struct mmc_blk_request));
2024 brq->mrq.cmd = &brq->cmd;
2025 brq->mrq.data = &brq->data;
2026 brq->mrq.sbc = &brq->sbc;
2027 brq->mrq.stop = &brq->stop;
2028
2029 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2030 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
2031 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2032
2033 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2034 brq->cmd.arg = blk_rq_pos(req);
2035 if (!mmc_card_blockaddr(card))
2036 brq->cmd.arg <<= 9;
2037 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2038
2039 brq->data.blksz = 512;
2040 brq->data.blocks = mqrq->packed_blocks + 1;
2041 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02002042 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002043
2044 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2045 brq->stop.arg = 0;
2046 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2047
2048 mmc_set_data_timeout(&brq->data, card);
2049
2050 brq->data.sg = mqrq->sg;
2051 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2052
2053 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02002054
2055 /*
2056 * This is intended for packed commands tests usage - in case these
2057 * functions are not in use the respective pointers are NULL
2058 */
2059 if (mq->err_check_fn)
2060 mqrq->mmc_active.err_check = mq->err_check_fn;
2061 else
2062 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2063
2064 if (mq->packed_test_fn)
2065 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002066
2067 mmc_queue_bounce_pre(mqrq);
2068}
2069
Adrian Hunter67716322011-08-29 16:42:15 +03002070static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2071 struct mmc_blk_request *brq, struct request *req,
2072 int ret)
2073{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002074 struct mmc_queue_req *mq_rq;
2075 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2076
Adrian Hunter67716322011-08-29 16:42:15 +03002077 /*
2078 * If this is an SD card and we're writing, we can first
2079 * mark the known good sectors as ok.
2080 *
2081 * If the card is not SD, we can still ok written sectors
2082 * as reported by the controller (which might be less than
2083 * the real number of written sectors, but never more).
2084 */
2085 if (mmc_card_sd(card)) {
2086 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05302087 if (!brq->data.fault_injected) {
2088 blocks = mmc_sd_num_wr_blocks(card);
2089 if (blocks != (u32)-1)
2090 ret = blk_end_request(req, 0, blocks << 9);
2091 } else
2092 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002093 } else {
2094 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
2095 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
2096 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03002097 return ret;
2098}
2099
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002100static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2101{
2102 struct request *prq;
2103 int idx = mq_rq->packed_fail_idx, i = 0;
2104 int ret = 0;
2105
2106 while (!list_empty(&mq_rq->packed_list)) {
2107 prq = list_entry_rq(mq_rq->packed_list.next);
2108 if (idx == i) {
2109 /* retry from error index */
2110 mq_rq->packed_num -= idx;
2111 mq_rq->req = prq;
2112 ret = 1;
2113
2114 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
2115 list_del_init(&prq->queuelist);
2116 mmc_blk_clear_packed(mq_rq);
2117 }
2118 return ret;
2119 }
2120 list_del_init(&prq->queuelist);
2121 blk_end_request(prq, 0, blk_rq_bytes(prq));
2122 i++;
2123 }
2124
2125 mmc_blk_clear_packed(mq_rq);
2126 return ret;
2127}
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002128static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq,
2129 unsigned int cmd_flags)
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002130{
2131 struct request *prq;
2132
2133 while (!list_empty(&mq_rq->packed_list)) {
2134 prq = list_entry_rq(mq_rq->packed_list.next);
2135 list_del_init(&prq->queuelist);
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002136 prq->cmd_flags |= cmd_flags;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002137 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2138 }
2139
2140 mmc_blk_clear_packed(mq_rq);
2141}
2142
2143static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2144 struct mmc_queue_req *mq_rq)
2145{
2146 struct request *prq;
2147 struct request_queue *q = mq->queue;
2148
2149 while (!list_empty(&mq_rq->packed_list)) {
2150 prq = list_entry_rq(mq_rq->packed_list.prev);
2151 if (prq->queuelist.prev != &mq_rq->packed_list) {
2152 list_del_init(&prq->queuelist);
2153 spin_lock_irq(q->queue_lock);
2154 blk_requeue_request(mq->queue, prq);
2155 spin_unlock_irq(q->queue_lock);
2156 } else {
2157 list_del_init(&prq->queuelist);
2158 }
2159 }
2160
2161 mmc_blk_clear_packed(mq_rq);
2162}
2163
Per Forlinee8a43a2011-07-01 18:55:33 +02002164static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02002165{
2166 struct mmc_blk_data *md = mq->data;
2167 struct mmc_card *card = md->queue.card;
2168 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03002169 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02002170 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002171 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02002172 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002173 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002174 const u8 packed_num = 2;
2175 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02002176
2177 if (!rqc && !mq->mqrq_prev->req)
2178 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02002179
Maya Erez5f360692012-10-10 03:47:54 +02002180 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02002181 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
2182 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002183 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02002184 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002185
Per Forlin54d49d772011-07-01 18:55:29 +02002186 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002187 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002188 if (reqs >= packed_num)
2189 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2190 card, mq);
2191 else
2192 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002193 areq = &mq->mqrq_cur->mmc_active;
2194 } else
2195 areq = NULL;
2196 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002197 if (!areq) {
Lee Susmand50afb52013-01-09 14:48:47 +02002198 if (status == MMC_BLK_NEW_REQUEST)
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002199 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002200 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002201 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002202
Per Forlinee8a43a2011-07-01 18:55:33 +02002203 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2204 brq = &mq_rq->brq;
2205 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002206 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002207 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002208
Per Forlind78d4a82011-07-01 18:55:30 +02002209 switch (status) {
2210 case MMC_BLK_SUCCESS:
2211 case MMC_BLK_PARTIAL:
2212 /*
2213 * A block was successfully transferred.
2214 */
Adrian Hunter67716322011-08-29 16:42:15 +03002215 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002216
2217 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2218 ret = mmc_blk_end_packed_req(mq_rq);
2219 break;
2220 } else {
2221 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002222 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002223 }
2224
Adrian Hunter67716322011-08-29 16:42:15 +03002225 /*
2226 * If the blk_end_request function returns non-zero even
2227 * though all data has been transferred and no errors
2228 * were returned by the host controller, it's a bug.
2229 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002230 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302231 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002232 __func__, blk_rq_bytes(req),
2233 brq->data.bytes_xfered);
2234 rqc = NULL;
2235 goto cmd_abort;
2236 }
Per Forlind78d4a82011-07-01 18:55:30 +02002237 break;
2238 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002239 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2240 if (!mmc_blk_reset(md, card->host, type))
2241 break;
2242 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002243 case MMC_BLK_RETRY:
2244 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002245 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002246 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002247 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002248 if (!mmc_blk_reset(md, card->host, type))
2249 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002250 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002251 case MMC_BLK_DATA_ERR: {
2252 int err;
2253
2254 err = mmc_blk_reset(md, card->host, type);
2255 if (!err)
2256 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002257 if (err == -ENODEV ||
2258 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002259 goto cmd_abort;
2260 /* Fall through */
2261 }
2262 case MMC_BLK_ECC_ERR:
2263 if (brq->data.blocks > 1) {
2264 /* Redo read one sector at a time */
2265 pr_warning("%s: retrying using single block read\n",
2266 req->rq_disk->disk_name);
2267 disable_multi = 1;
2268 break;
2269 }
Per Forlind78d4a82011-07-01 18:55:30 +02002270 /*
2271 * After an error, we redo I/O one sector at a
2272 * time, so we only reach here after trying to
2273 * read a single sector.
2274 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302275 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002276 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002277 if (!ret)
2278 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002279 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302280 case MMC_BLK_NOMEDIUM:
2281 goto cmd_abort;
Konstantin Dorfman4744aae2013-01-07 12:12:17 +02002282 default:
2283 pr_err("%s:%s: Unhandled return value (%d)",
2284 req->rq_disk->disk_name,
2285 __func__, status);
2286 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002287 }
2288
Per Forlinee8a43a2011-07-01 18:55:33 +02002289 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002290 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2291 /*
2292 * In case of a incomplete request
2293 * prepare it again and resend.
2294 */
2295 mmc_blk_rw_rq_prep(mq_rq, card,
2296 disable_multi, mq);
2297 mmc_start_req(card->host,
2298 &mq_rq->mmc_active, NULL);
2299 } else {
2300 if (!mq_rq->packed_retries)
2301 goto cmd_abort;
2302 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2303 mmc_start_req(card->host,
2304 &mq_rq->mmc_active, NULL);
2305 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 } while (ret);
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 1;
2310
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002311 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002312 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2313 if (mmc_card_removed(card))
2314 req->cmd_flags |= REQ_QUIET;
2315 while (ret)
2316 ret = blk_end_request(req, -EIO,
2317 blk_rq_cur_bytes(req));
2318 } else {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002319 mmc_blk_abort_packed_req(mq_rq, 0);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Per Forlinee8a43a2011-07-01 18:55:33 +02002322 start_new_req:
2323 if (rqc) {
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002324 if (mmc_card_removed(card)) {
2325 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2326 rqc->cmd_flags |= REQ_QUIET;
2327 blk_end_request_all(rqc, -EIO);
2328 } else {
2329 mmc_blk_abort_packed_req(mq_rq, REQ_QUIET);
2330 }
2331 } else {
2332 /* If current request is packed, it needs to put back */
2333 if (mq_rq->packed_cmd != MMC_PACKED_NONE)
2334 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002335
Seungwon Jeonfc1a5a22013-02-04 22:06:47 +02002336 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2337 mmc_start_req(card->host,
2338 &mq->mqrq_cur->mmc_active,
2339 NULL);
2340 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002341 }
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344}
2345
Adrian Hunterbd788c92010-08-11 14:17:47 -07002346static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2347{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002348 int ret;
2349 struct mmc_blk_data *md = mq->data;
2350 struct mmc_card *card = md->queue.card;
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002351 struct mmc_host *host = card->host;
2352 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002353
San Mehat148c57a2009-07-30 08:21:19 -07002354#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2355 if (mmc_bus_needs_resume(card->host)) {
2356 mmc_resume_bus(card->host);
2357 mmc_blk_set_blksize(md, card);
2358 }
2359#endif
2360
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002361 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002362 /* claim host only for the first request */
2363 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002364 if (card->ext_csd.bkops_en)
2365 mmc_stop_bkops(card);
2366 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002367
Andrei Warkentin371a6892011-04-11 18:10:25 -05002368 ret = mmc_blk_part_switch(card, md);
2369 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002370 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302371 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002372 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002373 ret = 0;
2374 goto out;
2375 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002376
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002377 mmc_blk_write_packing_control(mq, req);
2378
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002379 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002380 if (req && req->cmd_flags & REQ_SANITIZE) {
2381 /* complete ongoing async transfer before issuing sanitize */
2382 if (card->host && card->host->areq)
2383 mmc_blk_issue_rw_rq(mq, NULL);
2384 ret = mmc_blk_issue_sanitize_rq(mq, req);
2385 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002386 /* complete ongoing async transfer before issuing discard */
2387 if (card->host->areq)
2388 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302389 if (req->cmd_flags & REQ_SECURE &&
2390 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002391 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002392 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002393 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002394 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002395 /* complete ongoing async transfer before issuing flush */
2396 if (card->host->areq)
2397 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002398 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002399 } else {
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002400 if (!req && host->areq) {
2401 spin_lock_irqsave(&host->context_info.lock, flags);
2402 host->context_info.is_waiting_last_req = true;
2403 spin_unlock_irqrestore(&host->context_info.lock, flags);
2404 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002405 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002406 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002407
Andrei Warkentin371a6892011-04-11 18:10:25 -05002408out:
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002409 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) {
2410 if (mmc_card_need_bkops(card))
2411 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002412 /* release host only when there are no more requests */
2413 mmc_release_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002414 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002415 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Russell Kinga6f6c962006-01-03 22:38:44 +00002418static inline int mmc_blk_readonly(struct mmc_card *card)
2419{
2420 return mmc_card_readonly(card) ||
2421 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2422}
2423
Andrei Warkentin371a6892011-04-11 18:10:25 -05002424static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2425 struct device *parent,
2426 sector_t size,
2427 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002428 const char *subname,
2429 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct mmc_blk_data *md;
2432 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002433 unsigned int percentage =
2434 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002436 devidx = find_first_zero_bit(dev_use, max_devices);
2437 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return ERR_PTR(-ENOSPC);
2439 __set_bit(devidx, dev_use);
2440
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002441 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002442 if (!md) {
2443 ret = -ENOMEM;
2444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002446
Russell Kinga6f6c962006-01-03 22:38:44 +00002447 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002448 * !subname implies we are creating main mmc_blk_data that will be
2449 * associated with mmc_card with mmc_set_drvdata. Due to device
2450 * partitions, devidx will not coincide with a per-physical card
2451 * index anymore so we keep track of a name index.
2452 */
2453 if (!subname) {
2454 md->name_idx = find_first_zero_bit(name_use, max_devices);
2455 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002456 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002457 md->name_idx = ((struct mmc_blk_data *)
2458 dev_to_disk(parent)->private_data)->name_idx;
2459
Johan Rudholmadd710e2011-12-02 08:51:06 +01002460 md->area_type = area_type;
2461
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002462 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002463 * Set the read-only status based on the supported commands
2464 * and the write protect switch.
2465 */
2466 md->read_only = mmc_blk_readonly(card);
2467
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002468 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002469 if (md->disk == NULL) {
2470 ret = -ENOMEM;
2471 goto err_kfree;
2472 }
2473
2474 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002475 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002476 md->usage = 1;
2477
Adrian Hunterd09408a2011-06-23 13:40:28 +03002478 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002479 if (ret)
2480 goto err_putdisk;
2481
Russell Kinga6f6c962006-01-03 22:38:44 +00002482 md->queue.issue_fn = mmc_blk_issue_rq;
2483 md->queue.data = md;
2484
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002485 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002486 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002487 md->disk->fops = &mmc_bdops;
2488 md->disk->private_data = md;
2489 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002490 md->disk->driverfs_dev = parent;
2491 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002492 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002493 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2494 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002495
2496 /*
2497 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2498 *
2499 * - be set for removable media with permanent block devices
2500 * - be unset for removable block devices with permanent media
2501 *
2502 * Since MMC block devices clearly fall under the second
2503 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2504 * should use the block device creation/destruction hotplug
2505 * messages to tell when the card is present.
2506 */
2507
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002508 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2509 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002510
Martin K. Petersene1defc42009-05-22 17:17:49 -04002511 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002512 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002513
Maya Ereze1eae982013-01-05 21:51:06 +02002514 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2515 card->bkops_info.min_sectors_to_queue_delayed_work =
2516 ((unsigned int)size * percentage) / 100;
2517
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002518 if (mmc_host_cmd23(card->host)) {
2519 if (mmc_card_mmc(card) ||
2520 (mmc_card_sd(card) &&
2521 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2522 md->flags |= MMC_BLK_CMD23;
2523 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002524
2525 if (mmc_card_mmc(card) &&
2526 md->flags & MMC_BLK_CMD23 &&
2527 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2528 card->ext_csd.rel_sectors)) {
2529 md->flags |= MMC_BLK_REL_WR;
2530 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2531 }
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002534
2535 err_putdisk:
2536 put_disk(md->disk);
2537 err_kfree:
2538 kfree(md);
2539 out:
2540 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
Andrei Warkentin371a6892011-04-11 18:10:25 -05002543static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2544{
2545 sector_t size;
2546 struct mmc_blk_data *md;
2547
2548 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2549 /*
2550 * The EXT_CSD sector count is in number or 512 byte
2551 * sectors.
2552 */
2553 size = card->ext_csd.sectors;
2554 } else {
2555 /*
2556 * The CSD capacity field is in units of read_blkbits.
2557 * set_capacity takes units of 512 bytes.
2558 */
2559 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2560 }
2561
Johan Rudholmadd710e2011-12-02 08:51:06 +01002562 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2563 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002564 return md;
2565}
2566
2567static int mmc_blk_alloc_part(struct mmc_card *card,
2568 struct mmc_blk_data *md,
2569 unsigned int part_type,
2570 sector_t size,
2571 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002572 const char *subname,
2573 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002574{
2575 char cap_str[10];
2576 struct mmc_blk_data *part_md;
2577
2578 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002579 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002580 if (IS_ERR(part_md))
2581 return PTR_ERR(part_md);
2582 part_md->part_type = part_type;
2583 list_add(&part_md->part, &md->part);
2584
2585 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2586 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302587 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002588 part_md->disk->disk_name, mmc_card_id(card),
2589 mmc_card_name(card), part_md->part_type, cap_str);
2590 return 0;
2591}
2592
Namjae Jeone0c368d2011-10-06 23:41:38 +09002593/* MMC Physical partitions consist of two boot partitions and
2594 * up to four general purpose partitions.
2595 * For each partition enabled in EXT_CSD a block device will be allocatedi
2596 * to provide access to the partition.
2597 */
2598
Andrei Warkentin371a6892011-04-11 18:10:25 -05002599static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2600{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002601 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002602
2603 if (!mmc_card_mmc(card))
2604 return 0;
2605
Namjae Jeone0c368d2011-10-06 23:41:38 +09002606 for (idx = 0; idx < card->nr_parts; idx++) {
2607 if (card->part[idx].size) {
2608 ret = mmc_blk_alloc_part(card, md,
2609 card->part[idx].part_cfg,
2610 card->part[idx].size >> 9,
2611 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002612 card->part[idx].name,
2613 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002614 if (ret)
2615 return ret;
2616 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002617 }
2618
2619 return ret;
2620}
2621
Andrei Warkentin371a6892011-04-11 18:10:25 -05002622static void mmc_blk_remove_req(struct mmc_blk_data *md)
2623{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002624 struct mmc_card *card;
2625
Andrei Warkentin371a6892011-04-11 18:10:25 -05002626 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002627 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002628 device_remove_file(disk_to_dev(md->disk),
2629 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002630 if (md->disk->flags & GENHD_FL_UP) {
2631 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002632 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2633 card->ext_csd.boot_ro_lockable)
2634 device_remove_file(disk_to_dev(md->disk),
2635 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002636
2637 /* Stop new requests from getting into the queue */
2638 del_gendisk(md->disk);
2639 }
2640
2641 /* Then flush out any already in there */
2642 mmc_cleanup_queue(&md->queue);
2643 mmc_blk_put(md);
2644 }
2645}
2646
2647static void mmc_blk_remove_parts(struct mmc_card *card,
2648 struct mmc_blk_data *md)
2649{
2650 struct list_head *pos, *q;
2651 struct mmc_blk_data *part_md;
2652
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002653 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002654 list_for_each_safe(pos, q, &md->part) {
2655 part_md = list_entry(pos, struct mmc_blk_data, part);
2656 list_del(pos);
2657 mmc_blk_remove_req(part_md);
2658 }
2659}
2660
2661static int mmc_add_disk(struct mmc_blk_data *md)
2662{
2663 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002664 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002665
2666 add_disk(md->disk);
2667 md->force_ro.show = force_ro_show;
2668 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302669 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002670 md->force_ro.attr.name = "force_ro";
2671 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2672 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2673 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002674 goto force_ro_fail;
2675
2676 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2677 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002678 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002679
2680 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2681 mode = S_IRUGO;
2682 else
2683 mode = S_IRUGO | S_IWUSR;
2684
2685 md->power_ro_lock.show = power_ro_lock_show;
2686 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002687 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002688 md->power_ro_lock.attr.mode = mode;
2689 md->power_ro_lock.attr.name =
2690 "ro_lock_until_next_power_on";
2691 ret = device_create_file(disk_to_dev(md->disk),
2692 &md->power_ro_lock);
2693 if (ret)
2694 goto power_ro_lock_fail;
2695 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002696
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002697 md->num_wr_reqs_to_start_packing.show =
2698 num_wr_reqs_to_start_packing_show;
2699 md->num_wr_reqs_to_start_packing.store =
2700 num_wr_reqs_to_start_packing_store;
2701 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2702 md->num_wr_reqs_to_start_packing.attr.name =
2703 "num_wr_reqs_to_start_packing";
2704 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2705 ret = device_create_file(disk_to_dev(md->disk),
2706 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002707 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002708 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002709
Maya Ereze1eae982013-01-05 21:51:06 +02002710 md->bkops_check_threshold.show = bkops_check_threshold_show;
2711 md->bkops_check_threshold.store = bkops_check_threshold_store;
2712 sysfs_attr_init(&md->bkops_check_threshold.attr);
2713 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2714 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002715 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002716 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002717 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002718 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002719
Maya Erez64d12462013-01-06 10:19:38 +02002720 md->no_pack_for_random.show = no_pack_for_random_show;
2721 md->no_pack_for_random.store = no_pack_for_random_store;
2722 sysfs_attr_init(&md->no_pack_for_random.attr);
2723 md->no_pack_for_random.attr.name = "no_pack_for_random";
2724 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
2725 ret = device_create_file(disk_to_dev(md->disk),
2726 &md->no_pack_for_random);
2727 if (ret)
2728 goto no_pack_for_random_fails;
2729
Johan Rudholmadd710e2011-12-02 08:51:06 +01002730 return ret;
2731
Maya Erez64d12462013-01-06 10:19:38 +02002732no_pack_for_random_fails:
2733 device_remove_file(disk_to_dev(md->disk),
2734 &md->bkops_check_threshold);
Maya Ereze1eae982013-01-05 21:51:06 +02002735bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02002736 device_remove_file(disk_to_dev(md->disk),
2737 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002738num_wr_reqs_to_start_packing_fail:
2739 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002740power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002741 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002742force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002743 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002744
2745 return ret;
2746}
2747
Chris Ballc59d4472011-11-11 22:01:43 -05002748#define CID_MANFID_SANDISK 0x2
2749#define CID_MANFID_TOSHIBA 0x11
2750#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302751#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002752
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002753static const struct mmc_fixup blk_fixups[] =
2754{
Chris Ballc59d4472011-11-11 22:01:43 -05002755 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2756 MMC_QUIRK_INAND_CMD38),
2757 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2758 MMC_QUIRK_INAND_CMD38),
2759 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2760 MMC_QUIRK_INAND_CMD38),
2761 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2762 MMC_QUIRK_INAND_CMD38),
2763 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2764 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002765
2766 /*
2767 * Some MMC cards experience performance degradation with CMD23
2768 * instead of CMD12-bounded multiblock transfers. For now we'll
2769 * black list what's bad...
2770 * - Certain Toshiba cards.
2771 *
2772 * N.B. This doesn't affect SD cards.
2773 */
Chris Ballc59d4472011-11-11 22:01:43 -05002774 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002775 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002776 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002777 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002778 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002779 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002780
2781 /*
2782 * Some Micron MMC cards needs longer data read timeout than
2783 * indicated in CSD.
2784 */
Chris Ballc59d4472011-11-11 22:01:43 -05002785 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002786 MMC_QUIRK_LONG_READ_TIME),
2787
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302788 /* Some INAND MCP devices advertise incorrect timeout values */
2789 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2790 MMC_QUIRK_INAND_DATA_TIMEOUT),
2791
Ian Chenc4856122012-11-01 13:26:21 +05302792 /*
2793 * On these Samsung MoviNAND parts, performing secure erase or
2794 * secure trim can result in unrecoverable corruption due to a
2795 * firmware bug.
2796 */
2797 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2798 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2799 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2800 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2801 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2802 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2803 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2804 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2805 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2806 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2807 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2808 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2809 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2810 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2811 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2812 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2813
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002814 END_FIXUP
2815};
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817static int mmc_blk_probe(struct mmc_card *card)
2818{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002819 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002820 char cap_str[10];
2821
Pierre Ossman912490d2005-05-21 10:27:02 +01002822 /*
2823 * Check that the card supports the command class(es) we need.
2824 */
2825 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return -ENODEV;
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 md = mmc_blk_alloc(card);
2829 if (IS_ERR(md))
2830 return PTR_ERR(md);
2831
Yi Li444122f2009-02-05 15:31:57 +08002832 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002833 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302834 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002836 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Andrei Warkentin371a6892011-04-11 18:10:25 -05002838 if (mmc_blk_alloc_parts(card, md))
2839 goto out;
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002842 mmc_fixup_device(card, blk_fixups);
2843
San Mehat148c57a2009-07-30 08:21:19 -07002844#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2845 mmc_set_bus_resume_policy(card->host, 1);
2846#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002847 if (mmc_add_disk(md))
2848 goto out;
2849
2850 list_for_each_entry(part_md, &md->part, part) {
2851 if (mmc_add_disk(part_md))
2852 goto out;
2853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return 0;
2855
2856 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002857 mmc_blk_remove_parts(card, md);
2858 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
2862static void mmc_blk_remove(struct mmc_card *card)
2863{
2864 struct mmc_blk_data *md = mmc_get_drvdata(card);
2865
Andrei Warkentin371a6892011-04-11 18:10:25 -05002866 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002867 mmc_claim_host(card->host);
2868 mmc_blk_part_switch(card, md);
2869 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002870 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002872#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2873 mmc_set_bus_resume_policy(card->host, 0);
2874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002878static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002880 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct mmc_blk_data *md = mmc_get_drvdata(card);
2882
2883 if (md) {
2884 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002885 list_for_each_entry(part_md, &md->part, part) {
2886 mmc_queue_suspend(&part_md->queue);
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
2889 return 0;
2890}
2891
2892static int mmc_blk_resume(struct mmc_card *card)
2893{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002894 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 struct mmc_blk_data *md = mmc_get_drvdata(card);
2896
2897 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002898 /*
2899 * Resume involves the card going into idle state,
2900 * so current partition is always the main one.
2901 */
2902 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002904 list_for_each_entry(part_md, &md->part, part) {
2905 mmc_queue_resume(&part_md->queue);
2906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908 return 0;
2909}
2910#else
2911#define mmc_blk_suspend NULL
2912#define mmc_blk_resume NULL
2913#endif
2914
2915static struct mmc_driver mmc_driver = {
2916 .drv = {
2917 .name = "mmcblk",
2918 },
2919 .probe = mmc_blk_probe,
2920 .remove = mmc_blk_remove,
2921 .suspend = mmc_blk_suspend,
2922 .resume = mmc_blk_resume,
2923};
2924
2925static int __init mmc_blk_init(void)
2926{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002927 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002929 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2930 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2931
2932 max_devices = 256 / perdev_minors;
2933
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002934 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2935 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002938 res = mmc_register_driver(&mmc_driver);
2939 if (res)
2940 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002942 return 0;
2943 out2:
2944 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 out:
2946 return res;
2947}
2948
2949static void __exit mmc_blk_exit(void)
2950{
2951 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002952 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
2955module_init(mmc_blk_init);
2956module_exit(mmc_blk_exit);
2957
2958MODULE_LICENSE("GPL");
2959MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2960