blob: de28394eb0f6a80669e51e6b5c58ccf0db33f2e2 [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
Andrei Warkentin371a6892011-04-11 18:10:25 -0500923 if (main_md->part_curr == md->part_type)
924 return 0;
925
926 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300927 u8 part_config = card->ext_csd.part_config;
928
929 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
930 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500931
932 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300933 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500934 card->ext_csd.part_time);
935 if (ret)
936 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300937
938 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300939 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500940
941 main_md->part_curr = md->part_type;
942 return 0;
943}
944
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700945static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
946{
947 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100948 u32 result;
949 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700950
Venkatraman Sad5fd972011-08-25 00:30:50 +0530951 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400952 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400953 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700954
955 struct scatterlist sg;
956
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700957 cmd.opcode = MMC_APP_CMD;
958 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700959 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700960
961 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700962 if (err)
963 return (u32)-1;
964 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700965 return (u32)-1;
966
967 memset(&cmd, 0, sizeof(struct mmc_command));
968
969 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
970 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700971 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700972
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700973 data.blksz = 4;
974 data.blocks = 1;
975 data.flags = MMC_DATA_READ;
976 data.sg = &sg;
977 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530978 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700979
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700980 mrq.cmd = &cmd;
981 mrq.data = &data;
982
Ben Dooks051913d2009-06-08 23:33:57 +0100983 blocks = kmalloc(4, GFP_KERNEL);
984 if (!blocks)
985 return (u32)-1;
986
987 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700988
989 mmc_wait_for_req(card->host, &mrq);
990
Ben Dooks051913d2009-06-08 23:33:57 +0100991 result = ntohl(*blocks);
992 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700993
Ben Dooks051913d2009-06-08 23:33:57 +0100994 if (cmd.error || data.error)
995 result = (u32)-1;
996
997 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700998}
999
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001000static int send_stop(struct mmc_card *card, u32 *status)
1001{
1002 struct mmc_command cmd = {0};
1003 int err;
1004
1005 cmd.opcode = MMC_STOP_TRANSMISSION;
1006 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1007 err = mmc_wait_for_cmd(card->host, &cmd, 5);
1008 if (err == 0)
1009 *status = cmd.resp[0];
1010 return err;
1011}
1012
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001013static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001014{
Chris Ball1278dba2011-04-13 23:40:30 -04001015 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001016 int err;
1017
Adrian Hunter504f1912008-10-16 12:55:25 +03001018 cmd.opcode = MMC_SEND_STATUS;
1019 if (!mmc_host_is_spi(card->host))
1020 cmd.arg = card->rca << 16;
1021 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001022 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1023 if (err == 0)
1024 *status = cmd.resp[0];
1025 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001026}
1027
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301028#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001029#define ERR_RETRY 2
1030#define ERR_ABORT 1
1031#define ERR_CONTINUE 0
1032
1033static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1034 bool status_valid, u32 status)
1035{
1036 switch (error) {
1037 case -EILSEQ:
1038 /* response crc error, retry the r/w cmd */
1039 pr_err("%s: %s sending %s command, card status %#x\n",
1040 req->rq_disk->disk_name, "response CRC error",
1041 name, status);
1042 return ERR_RETRY;
1043
1044 case -ETIMEDOUT:
1045 pr_err("%s: %s sending %s command, card status %#x\n",
1046 req->rq_disk->disk_name, "timed out", name, status);
1047
1048 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001049 if (!status_valid) {
1050 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001051 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001052 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001053 /*
1054 * If it was a r/w cmd crc error, or illegal command
1055 * (eg, issued in wrong state) then retry - we should
1056 * have corrected the state problem above.
1057 */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001058 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1059 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001060 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -07001061 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001062
1063 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -07001064 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001065 return ERR_ABORT;
1066
1067 default:
1068 /* We don't understand the error code the driver gave us */
1069 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1070 req->rq_disk->disk_name, error, status);
1071 return ERR_ABORT;
1072 }
1073}
1074
1075/*
1076 * Initial r/w and stop cmd error recovery.
1077 * We don't know whether the card received the r/w cmd or not, so try to
1078 * restore things back to a sane state. Essentially, we do this as follows:
1079 * - Obtain card status. If the first attempt to obtain card status fails,
1080 * the status word will reflect the failed status cmd, not the failed
1081 * r/w cmd. If we fail to obtain card status, it suggests we can no
1082 * longer communicate with the card.
1083 * - Check the card state. If the card received the cmd but there was a
1084 * transient problem with the response, it might still be in a data transfer
1085 * mode. Try to send it a stop command. If this fails, we can't recover.
1086 * - If the r/w cmd failed due to a response CRC error, it was probably
1087 * transient, so retry the cmd.
1088 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1089 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1090 * illegal cmd, retry.
1091 * Otherwise we don't understand what happened, so abort.
1092 */
1093static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +03001094 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001095{
1096 bool prev_cmd_status_valid = true;
1097 u32 status, stop_status = 0;
1098 int err, retry;
1099
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301100 if (mmc_card_removed(card))
1101 return ERR_NOMEDIUM;
1102
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001103 /*
1104 * Try to get card status which indicates both the card state
1105 * and why there was no response. If the first attempt fails,
1106 * we can't be sure the returned status is for the r/w command.
1107 */
1108 for (retry = 2; retry >= 0; retry--) {
1109 err = get_card_status(card, &status, 0);
1110 if (!err)
1111 break;
1112
1113 prev_cmd_status_valid = false;
1114 pr_err("%s: error %d sending status command, %sing\n",
1115 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1116 }
1117
1118 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301119 if (err) {
1120 /* Check if the card is removed */
1121 if (mmc_detect_card_removed(card->host))
1122 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001123 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301124 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001125
Adrian Hunter67716322011-08-29 16:42:15 +03001126 /* Flag ECC errors */
1127 if ((status & R1_CARD_ECC_FAILED) ||
1128 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1129 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1130 *ecc_err = 1;
1131
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001132 /*
1133 * Check the current card state. If it is in some data transfer
1134 * mode, tell it to stop (and hopefully transition back to TRAN.)
1135 */
1136 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1137 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1138 err = send_stop(card, &stop_status);
1139 if (err)
1140 pr_err("%s: error %d sending stop command\n",
1141 req->rq_disk->disk_name, err);
1142
1143 /*
1144 * If the stop cmd also timed out, the card is probably
1145 * not present, so abort. Other errors are bad news too.
1146 */
1147 if (err)
1148 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +03001149 if (stop_status & R1_CARD_ECC_FAILED)
1150 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001151 }
1152
1153 /* Check for set block count errors */
1154 if (brq->sbc.error)
1155 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1156 prev_cmd_status_valid, status);
1157
1158 /* Check for r/w command errors */
1159 if (brq->cmd.error)
1160 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1161 prev_cmd_status_valid, status);
1162
Adrian Hunter67716322011-08-29 16:42:15 +03001163 /* Data errors */
1164 if (!brq->stop.error)
1165 return ERR_CONTINUE;
1166
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001167 /* Now for stop errors. These aren't fatal to the transfer. */
1168 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1169 req->rq_disk->disk_name, brq->stop.error,
1170 brq->cmd.resp[0], status);
1171
1172 /*
1173 * Subsitute in our own stop status as this will give the error
1174 * state which happened during the execution of the r/w command.
1175 */
1176 if (stop_status) {
1177 brq->stop.resp[0] = stop_status;
1178 brq->stop.error = 0;
1179 }
1180 return ERR_CONTINUE;
1181}
1182
Adrian Hunter67716322011-08-29 16:42:15 +03001183static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1184 int type)
1185{
1186 int err;
1187
1188 if (md->reset_done & type)
1189 return -EEXIST;
1190
1191 md->reset_done |= type;
1192 err = mmc_hw_reset(host);
1193 /* Ensure we switch back to the correct partition */
1194 if (err != -EOPNOTSUPP) {
1195 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
1196 int part_err;
1197
1198 main_md->part_curr = main_md->part_type;
1199 part_err = mmc_blk_part_switch(host->card, md);
1200 if (part_err) {
1201 /*
1202 * We have failed to get back into the correct
1203 * partition, so we need to abort the whole request.
1204 */
1205 return -ENODEV;
1206 }
1207 }
1208 return err;
1209}
1210
1211static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1212{
1213 md->reset_done &= ~type;
1214}
1215
Adrian Hunterbd788c92010-08-11 14:17:47 -07001216static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1217{
1218 struct mmc_blk_data *md = mq->data;
1219 struct mmc_card *card = md->queue.card;
1220 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001221 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001222
Adrian Hunterbd788c92010-08-11 14:17:47 -07001223 if (!mmc_can_erase(card)) {
1224 err = -EOPNOTSUPP;
1225 goto out;
1226 }
1227
1228 from = blk_rq_pos(req);
1229 nr = blk_rq_sectors(req);
1230
Maya Erez2d33a192013-01-04 15:52:41 +02001231 if (card->ext_csd.bkops_en)
1232 card->bkops_info.sectors_changed += blk_rq_sectors(req);
1233
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001234 if (mmc_can_discard(card))
1235 arg = MMC_DISCARD_ARG;
1236 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001237 arg = MMC_TRIM_ARG;
1238 else
1239 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001240retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001241 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1242 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1243 INAND_CMD38_ARG_EXT_CSD,
1244 arg == MMC_TRIM_ARG ?
1245 INAND_CMD38_ARG_TRIM :
1246 INAND_CMD38_ARG_ERASE,
1247 0);
1248 if (err)
1249 goto out;
1250 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001251 err = mmc_erase(card, from, nr, arg);
1252out:
Adrian Hunter67716322011-08-29 16:42:15 +03001253 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1254 goto retry;
1255 if (!err)
1256 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301257 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001258
Adrian Hunterbd788c92010-08-11 14:17:47 -07001259 return err ? 0 : 1;
1260}
1261
Adrian Hunter49804542010-08-11 14:17:50 -07001262static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1263 struct request *req)
1264{
1265 struct mmc_blk_data *md = mq->data;
1266 struct mmc_card *card = md->queue.card;
1267 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001268 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001269
Maya Erez463bb952012-05-24 23:46:29 +03001270 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001271 err = -EOPNOTSUPP;
1272 goto out;
1273 }
1274
1275 from = blk_rq_pos(req);
1276 nr = blk_rq_sectors(req);
1277
1278 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1279 arg = MMC_SECURE_TRIM1_ARG;
1280 else
1281 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001282retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001283 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1284 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1285 INAND_CMD38_ARG_EXT_CSD,
1286 arg == MMC_SECURE_TRIM1_ARG ?
1287 INAND_CMD38_ARG_SECTRIM1 :
1288 INAND_CMD38_ARG_SECERASE,
1289 0);
1290 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001291 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001292 }
Adrian Hunter28302812012-04-05 14:45:48 +03001293
Adrian Hunter49804542010-08-11 14:17:50 -07001294 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001295 if (err == -EIO)
1296 goto out_retry;
1297 if (err)
1298 goto out;
1299
1300 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001301 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1302 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1303 INAND_CMD38_ARG_EXT_CSD,
1304 INAND_CMD38_ARG_SECTRIM2,
1305 0);
1306 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001307 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001308 }
Adrian Hunter28302812012-04-05 14:45:48 +03001309
Adrian Hunter49804542010-08-11 14:17:50 -07001310 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001311 if (err == -EIO)
1312 goto out_retry;
1313 if (err)
1314 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001315 }
Adrian Hunter28302812012-04-05 14:45:48 +03001316
Adrian Hunter28302812012-04-05 14:45:48 +03001317out_retry:
1318 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001319 goto retry;
1320 if (!err)
1321 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001322out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301323 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001324
Adrian Hunter49804542010-08-11 14:17:50 -07001325 return err ? 0 : 1;
1326}
1327
Maya Erez463bb952012-05-24 23:46:29 +03001328static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
1329 struct request *req)
1330{
1331 struct mmc_blk_data *md = mq->data;
1332 struct mmc_card *card = md->queue.card;
1333 int err = 0;
1334
1335 BUG_ON(!card);
1336 BUG_ON(!card->host);
1337
1338 if (!(mmc_can_sanitize(card) &&
1339 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
1340 pr_warning("%s: %s - SANITIZE is not supported\n",
1341 mmc_hostname(card->host), __func__);
1342 err = -EOPNOTSUPP;
1343 goto out;
1344 }
1345
1346 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
1347 mmc_hostname(card->host), __func__);
1348
1349 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001350 EXT_CSD_SANITIZE_START, 1,
1351 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001352
1353 if (err)
1354 pr_err("%s: %s - mmc_switch() with "
1355 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1356 mmc_hostname(card->host), __func__, err);
1357
1358 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1359 __func__);
1360
1361out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301362 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001363
1364 return err ? 0 : 1;
1365}
1366
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001367static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1368{
1369 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001370 struct mmc_card *card = md->queue.card;
1371 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001372
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001373 ret = mmc_flush_cache(card);
1374 if (ret)
1375 ret = -EIO;
1376
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301377 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001378
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001379 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001380}
1381
1382/*
1383 * Reformat current write as a reliable write, supporting
1384 * both legacy and the enhanced reliable write MMC cards.
1385 * In each transfer we'll handle only as much as a single
1386 * reliable write can handle, thus finish the request in
1387 * partial completions.
1388 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001389static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1390 struct mmc_card *card,
1391 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001392{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001393 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1394 /* Legacy mode imposes restrictions on transfers. */
1395 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1396 brq->data.blocks = 1;
1397
1398 if (brq->data.blocks > card->ext_csd.rel_sectors)
1399 brq->data.blocks = card->ext_csd.rel_sectors;
1400 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1401 brq->data.blocks = 1;
1402 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001403}
1404
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001405#define CMD_ERRORS \
1406 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1407 R1_ADDRESS_ERROR | /* Misaligned address */ \
1408 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1409 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1410 R1_CC_ERROR | /* Card controller error */ \
1411 R1_ERROR) /* General/unknown error */
1412
Per Forlinee8a43a2011-07-01 18:55:33 +02001413static int mmc_blk_err_check(struct mmc_card *card,
1414 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001415{
Per Forlinee8a43a2011-07-01 18:55:33 +02001416 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1417 mmc_active);
1418 struct mmc_blk_request *brq = &mq_mrq->brq;
1419 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001420 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001421
1422 /*
1423 * sbc.error indicates a problem with the set block count
1424 * command. No data will have been transferred.
1425 *
1426 * cmd.error indicates a problem with the r/w command. No
1427 * data will have been transferred.
1428 *
1429 * stop.error indicates a problem with the stop command. Data
1430 * may have been transferred, or may still be transferring.
1431 */
Adrian Hunter67716322011-08-29 16:42:15 +03001432 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1433 brq->data.error) {
1434 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001435 case ERR_RETRY:
1436 return MMC_BLK_RETRY;
1437 case ERR_ABORT:
1438 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301439 case ERR_NOMEDIUM:
1440 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001441 case ERR_CONTINUE:
1442 break;
1443 }
1444 }
1445
1446 /*
1447 * Check for errors relating to the execution of the
1448 * initial command - such as address errors. No data
1449 * has been transferred.
1450 */
1451 if (brq->cmd.resp[0] & CMD_ERRORS) {
1452 pr_err("%s: r/w command failed, status = %#x\n",
1453 req->rq_disk->disk_name, brq->cmd.resp[0]);
1454 return MMC_BLK_ABORT;
1455 }
1456
1457 /*
1458 * Everything else is either success, or a data error of some
1459 * kind. If it was a write, we may have transitioned to
1460 * program mode, which we have to wait for it to complete.
1461 */
1462 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1463 u32 status;
Trey Ramsay29e158e2013-01-07 17:26:37 +05301464 unsigned long timeout;
1465
1466 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001467 do {
1468 int err = get_card_status(card, &status, 5);
1469 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301470 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001471 req->rq_disk->disk_name, err);
1472 return MMC_BLK_CMD_ERR;
1473 }
Trey Ramsay29e158e2013-01-07 17:26:37 +05301474
1475 /* Timeout if the device never becomes ready for data
1476 * and never leaves the program state.
1477 */
1478 if (time_after(jiffies, timeout)) {
1479 pr_err("%s: Card stuck in programming state!"\
1480 " %s %s\n", mmc_hostname(card->host),
1481 req->rq_disk->disk_name, __func__);
1482
1483 return MMC_BLK_CMD_ERR;
1484 }
Per Forlind78d4a82011-07-01 18:55:30 +02001485 /*
1486 * Some cards mishandle the status bits,
1487 * so make sure to check both the busy
1488 * indication and the card state.
1489 */
1490 } while (!(status & R1_READY_FOR_DATA) ||
1491 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1492 }
1493
1494 if (brq->data.error) {
1495 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1496 req->rq_disk->disk_name, brq->data.error,
1497 (unsigned)blk_rq_pos(req),
1498 (unsigned)blk_rq_sectors(req),
1499 brq->cmd.resp[0], brq->stop.resp[0]);
1500
1501 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001502 if (ecc_err)
1503 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001504 return MMC_BLK_DATA_ERR;
1505 } else {
1506 return MMC_BLK_CMD_ERR;
1507 }
1508 }
1509
Adrian Hunter67716322011-08-29 16:42:15 +03001510 if (!brq->data.bytes_xfered)
1511 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001512
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001513 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1514 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1515 return MMC_BLK_PARTIAL;
1516 else
1517 return MMC_BLK_SUCCESS;
1518 }
1519
Adrian Hunter67716322011-08-29 16:42:15 +03001520 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1521 return MMC_BLK_PARTIAL;
1522
1523 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001524}
1525
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001526static int mmc_blk_packed_err_check(struct mmc_card *card,
1527 struct mmc_async_req *areq)
1528{
1529 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1530 mmc_active);
1531 struct request *req = mq_rq->req;
1532 int err, check, status;
1533 u8 ext_csd[512];
1534
1535 mq_rq->packed_retries--;
1536 check = mmc_blk_err_check(card, areq);
1537 err = get_card_status(card, &status, 0);
1538 if (err) {
1539 pr_err("%s: error %d sending status command\n",
1540 req->rq_disk->disk_name, err);
1541 return MMC_BLK_ABORT;
1542 }
1543
1544 if (status & R1_EXCEPTION_EVENT) {
1545 err = mmc_send_ext_csd(card, ext_csd);
1546 if (err) {
1547 pr_err("%s: error %d sending ext_csd\n",
1548 req->rq_disk->disk_name, err);
1549 return MMC_BLK_ABORT;
1550 }
1551
1552 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1553 EXT_CSD_PACKED_FAILURE) &&
1554 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1555 EXT_CSD_PACKED_GENERIC_ERROR)) {
1556 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1557 EXT_CSD_PACKED_INDEXED_ERROR) {
1558 mq_rq->packed_fail_idx =
1559 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1560 return MMC_BLK_PARTIAL;
1561 }
1562 }
1563 }
1564
1565 return check;
1566}
1567
Per Forlin54d49d772011-07-01 18:55:29 +02001568static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1569 struct mmc_card *card,
1570 int disable_multi,
1571 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Per Forlin54d49d772011-07-01 18:55:29 +02001573 u32 readcmd, writecmd;
1574 struct mmc_blk_request *brq = &mqrq->brq;
1575 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301577 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001579 /*
1580 * Reliable writes are used to implement Forced Unit Access and
1581 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001582 *
1583 * XXX: this really needs a good explanation of why REQ_META
1584 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001585 */
1586 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1587 (req->cmd_flags & REQ_META)) &&
1588 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001589 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001590
Per Forlin54d49d772011-07-01 18:55:29 +02001591 memset(brq, 0, sizeof(struct mmc_blk_request));
1592 brq->mrq.cmd = &brq->cmd;
1593 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Per Forlin54d49d772011-07-01 18:55:29 +02001595 brq->cmd.arg = blk_rq_pos(req);
1596 if (!mmc_card_blockaddr(card))
1597 brq->cmd.arg <<= 9;
1598 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1599 brq->data.blksz = 512;
1600 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1601 brq->stop.arg = 0;
1602 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1603 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Asutosh Das1a897142012-07-27 18:10:19 +05301605 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001606 /*
1607 * The block layer doesn't support all sector count
1608 * restrictions, so we need to be prepared for too big
1609 * requests.
1610 */
1611 if (brq->data.blocks > card->host->max_blk_count)
1612 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001614 if (brq->data.blocks > 1) {
1615 /*
1616 * After a read error, we redo the request one sector
1617 * at a time in order to accurately determine which
1618 * sectors can be read successfully.
1619 */
1620 if (disable_multi)
1621 brq->data.blocks = 1;
1622
1623 /* Some controllers can't do multiblock reads due to hw bugs */
1624 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1625 rq_data_dir(req) == READ)
1626 brq->data.blocks = 1;
1627 }
Per Forlin54d49d772011-07-01 18:55:29 +02001628
1629 if (brq->data.blocks > 1 || do_rel_wr) {
1630 /* SPI multiblock writes terminate using a special
1631 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001632 */
Per Forlin54d49d772011-07-01 18:55:29 +02001633 if (!mmc_host_is_spi(card->host) ||
1634 rq_data_dir(req) == READ)
1635 brq->mrq.stop = &brq->stop;
1636 readcmd = MMC_READ_MULTIPLE_BLOCK;
1637 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1638 } else {
1639 brq->mrq.stop = NULL;
1640 readcmd = MMC_READ_SINGLE_BLOCK;
1641 writecmd = MMC_WRITE_BLOCK;
1642 }
1643 if (rq_data_dir(req) == READ) {
1644 brq->cmd.opcode = readcmd;
1645 brq->data.flags |= MMC_DATA_READ;
1646 } else {
1647 brq->cmd.opcode = writecmd;
1648 brq->data.flags |= MMC_DATA_WRITE;
1649 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001650
Per Forlin54d49d772011-07-01 18:55:29 +02001651 if (do_rel_wr)
1652 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001653
Per Forlin54d49d772011-07-01 18:55:29 +02001654 /*
Saugata Das42659002011-12-21 13:09:17 +05301655 * Data tag is used only during writing meta data to speed
1656 * up write and any subsequent read of this meta data
1657 */
1658 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1659 (req->cmd_flags & REQ_META) &&
1660 (rq_data_dir(req) == WRITE) &&
1661 ((brq->data.blocks * brq->data.blksz) >=
1662 card->ext_csd.data_tag_unit_size);
1663
1664 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001665 * Pre-defined multi-block transfers are preferable to
1666 * open ended-ones (and necessary for reliable writes).
1667 * However, it is not sufficient to just send CMD23,
1668 * and avoid the final CMD12, as on an error condition
1669 * CMD12 (stop) needs to be sent anyway. This, coupled
1670 * with Auto-CMD23 enhancements provided by some
1671 * hosts, means that the complexity of dealing
1672 * with this is best left to the host. If CMD23 is
1673 * supported by card and host, we'll fill sbc in and let
1674 * the host deal with handling it correctly. This means
1675 * that for hosts that don't expose MMC_CAP_CMD23, no
1676 * change of behavior will be observed.
1677 *
1678 * N.B: Some MMC cards experience perf degradation.
1679 * We'll avoid using CMD23-bounded multiblock writes for
1680 * these, while retaining features like reliable writes.
1681 */
Saugata Das42659002011-12-21 13:09:17 +05301682 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1683 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1684 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001685 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1686 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301687 (do_rel_wr ? (1 << 31) : 0) |
1688 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001689 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1690 brq->mrq.sbc = &brq->sbc;
1691 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001692
Per Forlin54d49d772011-07-01 18:55:29 +02001693 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001694
Per Forlin54d49d772011-07-01 18:55:29 +02001695 brq->data.sg = mqrq->sg;
1696 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001697
Per Forlin54d49d772011-07-01 18:55:29 +02001698 /*
1699 * Adjust the sg list so it is the same size as the
1700 * request.
1701 */
1702 if (brq->data.blocks != blk_rq_sectors(req)) {
1703 int i, data_size = brq->data.blocks << 9;
1704 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001705
Per Forlin54d49d772011-07-01 18:55:29 +02001706 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1707 data_size -= sg->length;
1708 if (data_size <= 0) {
1709 sg->length += data_size;
1710 i++;
1711 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001712 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001713 }
Per Forlin54d49d772011-07-01 18:55:29 +02001714 brq->data.sg_len = i;
1715 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001716
Per Forlinee8a43a2011-07-01 18:55:33 +02001717 mqrq->mmc_active.mrq = &brq->mrq;
Yaniv Gardie9214c82012-10-18 13:58:18 +02001718 if (mq->err_check_fn)
1719 mqrq->mmc_active.err_check = mq->err_check_fn;
1720 else
1721 mqrq->mmc_active.err_check = mmc_blk_err_check;
Per Forlinee8a43a2011-07-01 18:55:33 +02001722
Per Forlin54d49d772011-07-01 18:55:29 +02001723 mmc_queue_bounce_pre(mqrq);
1724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001726static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1727 struct request *req)
1728{
1729 struct mmc_host *host = mq->card->host;
1730 int data_dir;
1731
1732 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1733 return;
1734
Maya Erez6f219eb2012-12-02 13:27:15 +02001735 /* Support for the write packing on eMMC 4.5 or later */
1736 if (mq->card->ext_csd.rev <= 5)
1737 return;
1738
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001739 /*
1740 * In case the packing control is not supported by the host, it should
1741 * not have an effect on the write packing. Therefore we have to enable
1742 * the write packing
1743 */
1744 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1745 mq->wr_packing_enabled = true;
1746 return;
1747 }
1748
1749 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1750 if (mq->num_of_potential_packed_wr_reqs >
1751 mq->num_wr_reqs_to_start_packing)
1752 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001753 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001754 return;
1755 }
1756
1757 data_dir = rq_data_dir(req);
1758
1759 if (data_dir == READ) {
1760 mq->num_of_potential_packed_wr_reqs = 0;
1761 mq->wr_packing_enabled = false;
1762 return;
1763 } else if (data_dir == WRITE) {
1764 mq->num_of_potential_packed_wr_reqs++;
1765 }
1766
1767 if (mq->num_of_potential_packed_wr_reqs >
1768 mq->num_wr_reqs_to_start_packing)
1769 mq->wr_packing_enabled = true;
1770
1771}
1772
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001773struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
1774{
1775 if (!card)
1776 return NULL;
1777
1778 return &card->wr_pack_stats;
1779}
1780EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
1781
1782void mmc_blk_init_packed_statistics(struct mmc_card *card)
1783{
1784 int max_num_of_packed_reqs = 0;
1785
1786 if (!card || !card->wr_pack_stats.packing_events)
1787 return;
1788
1789 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
1790
1791 spin_lock(&card->wr_pack_stats.lock);
1792 memset(card->wr_pack_stats.packing_events, 0,
1793 (max_num_of_packed_reqs + 1) *
1794 sizeof(*card->wr_pack_stats.packing_events));
1795 memset(&card->wr_pack_stats.pack_stop_reason, 0,
1796 sizeof(card->wr_pack_stats.pack_stop_reason));
1797 card->wr_pack_stats.enabled = true;
1798 spin_unlock(&card->wr_pack_stats.lock);
1799}
1800EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
1801
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001802static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1803{
1804 struct request_queue *q = mq->queue;
1805 struct mmc_card *card = mq->card;
1806 struct request *cur = req, *next = NULL;
1807 struct mmc_blk_data *md = mq->data;
1808 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1809 unsigned int req_sectors = 0, phys_segments = 0;
1810 unsigned int max_blk_count, max_phys_segs;
1811 u8 put_back = 0;
1812 u8 max_packed_rw = 0;
1813 u8 reqs = 0;
Lee Susmand50afb52013-01-09 14:48:47 +02001814 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001815
1816 mmc_blk_clear_packed(mq->mqrq_cur);
1817
1818 if (!(md->flags & MMC_BLK_CMD23) ||
1819 !card->ext_csd.packed_event_en)
1820 goto no_packed;
1821
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001822 if (!mq->wr_packing_enabled)
1823 goto no_packed;
1824
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001825 if ((rq_data_dir(cur) == WRITE) &&
1826 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1827 max_packed_rw = card->ext_csd.max_packed_writes;
1828
1829 if (max_packed_rw == 0)
1830 goto no_packed;
1831
1832 if (mmc_req_rel_wr(cur) &&
1833 (md->flags & MMC_BLK_REL_WR) &&
1834 !en_rel_wr)
1835 goto no_packed;
1836
1837 if (mmc_large_sec(card) &&
1838 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1839 goto no_packed;
1840
1841 max_blk_count = min(card->host->max_blk_count,
1842 card->host->max_req_size >> 9);
1843 if (unlikely(max_blk_count > 0xffff))
1844 max_blk_count = 0xffff;
1845
1846 max_phys_segs = queue_max_segments(q);
1847 req_sectors += blk_rq_sectors(cur);
1848 phys_segments += cur->nr_phys_segments;
1849
1850 if (rq_data_dir(cur) == WRITE) {
1851 req_sectors++;
1852 phys_segments++;
1853 }
1854
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001855 spin_lock(&stats->lock);
1856
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001857 while (reqs < max_packed_rw - 1) {
1858 spin_lock_irq(q->queue_lock);
1859 next = blk_fetch_request(q);
1860 spin_unlock_irq(q->queue_lock);
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001861 if (!next) {
1862 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001863 break;
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001864 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001865
1866 if (mmc_large_sec(card) &&
1867 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
Tatyana Brokhman1718ef22012-10-04 11:11:08 +02001868 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001869 put_back = 1;
1870 break;
1871 }
1872
1873 if (next->cmd_flags & REQ_DISCARD ||
1874 next->cmd_flags & REQ_FLUSH) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001875 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001876 put_back = 1;
1877 break;
1878 }
1879
1880 if (rq_data_dir(cur) != rq_data_dir(next)) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001881 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001882 put_back = 1;
1883 break;
1884 }
1885
1886 if (mmc_req_rel_wr(next) &&
1887 (md->flags & MMC_BLK_REL_WR) &&
1888 !en_rel_wr) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001889 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001890 put_back = 1;
1891 break;
1892 }
1893
1894 req_sectors += blk_rq_sectors(next);
1895 if (req_sectors > max_blk_count) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001896 if (stats->enabled)
1897 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001898 put_back = 1;
1899 break;
1900 }
1901
1902 phys_segments += next->nr_phys_segments;
1903 if (phys_segments > max_phys_segs) {
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001904 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001905 put_back = 1;
1906 break;
1907 }
1908
Maya Erez64d12462013-01-06 10:19:38 +02001909 if (mq->no_pack_for_random) {
1910 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
1911 blk_rq_pos(next)) {
1912 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
1913 put_back = 1;
1914 break;
1915 }
1916 }
1917
Maya Erez2d33a192013-01-04 15:52:41 +02001918 if (rq_data_dir(next) == WRITE) {
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001919 mq->num_of_potential_packed_wr_reqs++;
Maya Erez2d33a192013-01-04 15:52:41 +02001920 if (card->ext_csd.bkops_en)
1921 card->bkops_info.sectors_changed +=
1922 blk_rq_sectors(next);
1923 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001924 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1925 cur = next;
1926 reqs++;
1927 }
1928
1929 if (put_back) {
1930 spin_lock_irq(q->queue_lock);
1931 blk_requeue_request(q, next);
1932 spin_unlock_irq(q->queue_lock);
1933 }
1934
Tatyana Brokhman464fbe12012-10-07 10:33:13 +02001935 if (stats->enabled) {
1936 if (reqs + 1 <= card->ext_csd.max_packed_writes)
1937 stats->packing_events[reqs + 1]++;
1938 if (reqs + 1 == max_packed_rw)
1939 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
1940 }
1941
1942 spin_unlock(&stats->lock);
1943
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001944 if (reqs > 0) {
1945 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1946 mq->mqrq_cur->packed_num = ++reqs;
1947 mq->mqrq_cur->packed_retries = reqs;
1948 return reqs;
1949 }
1950
1951no_packed:
1952 mmc_blk_clear_packed(mq->mqrq_cur);
1953 return 0;
1954}
1955
1956static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1957 struct mmc_card *card,
1958 struct mmc_queue *mq)
1959{
1960 struct mmc_blk_request *brq = &mqrq->brq;
1961 struct request *req = mqrq->req;
1962 struct request *prq;
1963 struct mmc_blk_data *md = mq->data;
1964 bool do_rel_wr, do_data_tag;
1965 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1966 u8 i = 1;
1967
1968 mqrq->packed_cmd = MMC_PACKED_WRITE;
1969 mqrq->packed_blocks = 0;
1970 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1971
1972 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1973 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1974 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1975
1976 /*
1977 * Argument for each entry of packed group
1978 */
1979 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1980 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1981 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1982 (prq->cmd_flags & REQ_META) &&
1983 (rq_data_dir(prq) == WRITE) &&
1984 ((brq->data.blocks * brq->data.blksz) >=
1985 card->ext_csd.data_tag_unit_size);
1986 /* Argument of CMD23 */
1987 packed_cmd_hdr[(i * 2)] =
1988 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1989 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1990 blk_rq_sectors(prq);
1991 /* Argument of CMD18 or CMD25 */
1992 packed_cmd_hdr[((i * 2)) + 1] =
1993 mmc_card_blockaddr(card) ?
1994 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1995 mqrq->packed_blocks += blk_rq_sectors(prq);
1996 i++;
1997 }
1998
1999 memset(brq, 0, sizeof(struct mmc_blk_request));
2000 brq->mrq.cmd = &brq->cmd;
2001 brq->mrq.data = &brq->data;
2002 brq->mrq.sbc = &brq->sbc;
2003 brq->mrq.stop = &brq->stop;
2004
2005 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2006 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
2007 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2008
2009 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2010 brq->cmd.arg = blk_rq_pos(req);
2011 if (!mmc_card_blockaddr(card))
2012 brq->cmd.arg <<= 9;
2013 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2014
2015 brq->data.blksz = 512;
2016 brq->data.blocks = mqrq->packed_blocks + 1;
2017 brq->data.flags |= MMC_DATA_WRITE;
Maya Erez47b37922012-10-29 20:19:01 +02002018 brq->data.fault_injected = false;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002019
2020 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2021 brq->stop.arg = 0;
2022 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2023
2024 mmc_set_data_timeout(&brq->data, card);
2025
2026 brq->data.sg = mqrq->sg;
2027 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2028
2029 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman09b010d2012-10-09 13:50:56 +02002030
2031 /*
2032 * This is intended for packed commands tests usage - in case these
2033 * functions are not in use the respective pointers are NULL
2034 */
2035 if (mq->err_check_fn)
2036 mqrq->mmc_active.err_check = mq->err_check_fn;
2037 else
2038 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2039
2040 if (mq->packed_test_fn)
2041 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002042
2043 mmc_queue_bounce_pre(mqrq);
2044}
2045
Adrian Hunter67716322011-08-29 16:42:15 +03002046static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2047 struct mmc_blk_request *brq, struct request *req,
2048 int ret)
2049{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002050 struct mmc_queue_req *mq_rq;
2051 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2052
Adrian Hunter67716322011-08-29 16:42:15 +03002053 /*
2054 * If this is an SD card and we're writing, we can first
2055 * mark the known good sectors as ok.
2056 *
2057 * If the card is not SD, we can still ok written sectors
2058 * as reported by the controller (which might be less than
2059 * the real number of written sectors, but never more).
2060 */
2061 if (mmc_card_sd(card)) {
2062 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05302063 if (!brq->data.fault_injected) {
2064 blocks = mmc_sd_num_wr_blocks(card);
2065 if (blocks != (u32)-1)
2066 ret = blk_end_request(req, 0, blocks << 9);
2067 } else
2068 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002069 } else {
2070 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
2071 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
2072 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03002073 return ret;
2074}
2075
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002076static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2077{
2078 struct request *prq;
2079 int idx = mq_rq->packed_fail_idx, i = 0;
2080 int ret = 0;
2081
2082 while (!list_empty(&mq_rq->packed_list)) {
2083 prq = list_entry_rq(mq_rq->packed_list.next);
2084 if (idx == i) {
2085 /* retry from error index */
2086 mq_rq->packed_num -= idx;
2087 mq_rq->req = prq;
2088 ret = 1;
2089
2090 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
2091 list_del_init(&prq->queuelist);
2092 mmc_blk_clear_packed(mq_rq);
2093 }
2094 return ret;
2095 }
2096 list_del_init(&prq->queuelist);
2097 blk_end_request(prq, 0, blk_rq_bytes(prq));
2098 i++;
2099 }
2100
2101 mmc_blk_clear_packed(mq_rq);
2102 return ret;
2103}
2104static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2105{
2106 struct request *prq;
2107
2108 while (!list_empty(&mq_rq->packed_list)) {
2109 prq = list_entry_rq(mq_rq->packed_list.next);
2110 list_del_init(&prq->queuelist);
2111 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2112 }
2113
2114 mmc_blk_clear_packed(mq_rq);
2115}
2116
2117static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2118 struct mmc_queue_req *mq_rq)
2119{
2120 struct request *prq;
2121 struct request_queue *q = mq->queue;
2122
2123 while (!list_empty(&mq_rq->packed_list)) {
2124 prq = list_entry_rq(mq_rq->packed_list.prev);
2125 if (prq->queuelist.prev != &mq_rq->packed_list) {
2126 list_del_init(&prq->queuelist);
2127 spin_lock_irq(q->queue_lock);
2128 blk_requeue_request(mq->queue, prq);
2129 spin_unlock_irq(q->queue_lock);
2130 } else {
2131 list_del_init(&prq->queuelist);
2132 }
2133 }
2134
2135 mmc_blk_clear_packed(mq_rq);
2136}
2137
Per Forlinee8a43a2011-07-01 18:55:33 +02002138static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02002139{
2140 struct mmc_blk_data *md = mq->data;
2141 struct mmc_card *card = md->queue.card;
2142 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03002143 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02002144 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002145 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02002146 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002147 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002148 const u8 packed_num = 2;
2149 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02002150
2151 if (!rqc && !mq->mqrq_prev->req)
2152 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02002153
Maya Erez5f360692012-10-10 03:47:54 +02002154 if (rqc) {
Maya Erez2d33a192013-01-04 15:52:41 +02002155 if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE))
2156 card->bkops_info.sectors_changed += blk_rq_sectors(rqc);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002157 reqs = mmc_blk_prep_packed_list(mq, rqc);
Maya Erez5f360692012-10-10 03:47:54 +02002158 }
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002159
Per Forlin54d49d772011-07-01 18:55:29 +02002160 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002161 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002162 if (reqs >= packed_num)
2163 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2164 card, mq);
2165 else
2166 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002167 areq = &mq->mqrq_cur->mmc_active;
2168 } else
2169 areq = NULL;
2170 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002171 if (!areq) {
Lee Susmand50afb52013-01-09 14:48:47 +02002172 if (status == MMC_BLK_NEW_REQUEST)
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002173 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002174 return 0;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002175 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002176
Per Forlinee8a43a2011-07-01 18:55:33 +02002177 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2178 brq = &mq_rq->brq;
2179 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002180 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002181 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002182
Per Forlind78d4a82011-07-01 18:55:30 +02002183 switch (status) {
2184 case MMC_BLK_SUCCESS:
2185 case MMC_BLK_PARTIAL:
2186 /*
2187 * A block was successfully transferred.
2188 */
Adrian Hunter67716322011-08-29 16:42:15 +03002189 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002190
2191 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
2192 ret = mmc_blk_end_packed_req(mq_rq);
2193 break;
2194 } else {
2195 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002196 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002197 }
2198
Adrian Hunter67716322011-08-29 16:42:15 +03002199 /*
2200 * If the blk_end_request function returns non-zero even
2201 * though all data has been transferred and no errors
2202 * were returned by the host controller, it's a bug.
2203 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002204 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302205 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002206 __func__, blk_rq_bytes(req),
2207 brq->data.bytes_xfered);
2208 rqc = NULL;
2209 goto cmd_abort;
2210 }
Per Forlind78d4a82011-07-01 18:55:30 +02002211 break;
2212 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002213 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2214 if (!mmc_blk_reset(md, card->host, type))
2215 break;
2216 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02002217 case MMC_BLK_RETRY:
2218 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002219 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002220 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002221 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002222 if (!mmc_blk_reset(md, card->host, type))
2223 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002224 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002225 case MMC_BLK_DATA_ERR: {
2226 int err;
2227
2228 err = mmc_blk_reset(md, card->host, type);
2229 if (!err)
2230 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002231 if (err == -ENODEV ||
2232 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03002233 goto cmd_abort;
2234 /* Fall through */
2235 }
2236 case MMC_BLK_ECC_ERR:
2237 if (brq->data.blocks > 1) {
2238 /* Redo read one sector at a time */
2239 pr_warning("%s: retrying using single block read\n",
2240 req->rq_disk->disk_name);
2241 disable_multi = 1;
2242 break;
2243 }
Per Forlind78d4a82011-07-01 18:55:30 +02002244 /*
2245 * After an error, we redo I/O one sector at a
2246 * time, so we only reach here after trying to
2247 * read a single sector.
2248 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302249 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002250 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002251 if (!ret)
2252 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002253 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05302254 case MMC_BLK_NOMEDIUM:
2255 goto cmd_abort;
Konstantin Dorfman4744aae2013-01-07 12:12:17 +02002256 default:
2257 pr_err("%s:%s: Unhandled return value (%d)",
2258 req->rq_disk->disk_name,
2259 __func__, status);
2260 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002261 }
2262
Per Forlinee8a43a2011-07-01 18:55:33 +02002263 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002264 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2265 /*
2266 * In case of a incomplete request
2267 * prepare it again and resend.
2268 */
2269 mmc_blk_rw_rq_prep(mq_rq, card,
2270 disable_multi, mq);
2271 mmc_start_req(card->host,
2272 &mq_rq->mmc_active, NULL);
2273 } else {
2274 if (!mq_rq->packed_retries)
2275 goto cmd_abort;
2276 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2277 mmc_start_req(card->host,
2278 &mq_rq->mmc_active, NULL);
2279 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 } while (ret);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return 1;
2284
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002285 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002286 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
2287 if (mmc_card_removed(card))
2288 req->cmd_flags |= REQ_QUIET;
2289 while (ret)
2290 ret = blk_end_request(req, -EIO,
2291 blk_rq_cur_bytes(req));
2292 } else {
2293 mmc_blk_abort_packed_req(mq_rq);
2294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Per Forlinee8a43a2011-07-01 18:55:33 +02002296 start_new_req:
2297 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02002298 /*
2299 * If current request is packed, it needs to put back.
2300 */
2301 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
2302 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2303
Per Forlinee8a43a2011-07-01 18:55:33 +02002304 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2305 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
2306 }
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309}
2310
Adrian Hunterbd788c92010-08-11 14:17:47 -07002311static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2312{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002313 int ret;
2314 struct mmc_blk_data *md = mq->data;
2315 struct mmc_card *card = md->queue.card;
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002316 struct mmc_host *host = card->host;
2317 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002318
San Mehat148c57a2009-07-30 08:21:19 -07002319#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2320 if (mmc_bus_needs_resume(card->host)) {
2321 mmc_resume_bus(card->host);
2322 mmc_blk_set_blksize(md, card);
2323 }
2324#endif
2325
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002326 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002327 /* claim host only for the first request */
2328 mmc_claim_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002329 if (card->ext_csd.bkops_en)
2330 mmc_stop_bkops(card);
2331 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002332
Andrei Warkentin371a6892011-04-11 18:10:25 -05002333 ret = mmc_blk_part_switch(card, md);
2334 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002335 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05302336 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002337 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002338 ret = 0;
2339 goto out;
2340 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002341
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002342 mmc_blk_write_packing_control(mq, req);
2343
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +02002344 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Maya Erez463bb952012-05-24 23:46:29 +03002345 if (req && req->cmd_flags & REQ_SANITIZE) {
2346 /* complete ongoing async transfer before issuing sanitize */
2347 if (card->host && card->host->areq)
2348 mmc_blk_issue_rw_rq(mq, NULL);
2349 ret = mmc_blk_issue_sanitize_rq(mq, req);
2350 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002351 /* complete ongoing async transfer before issuing discard */
2352 if (card->host->areq)
2353 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chenc4856122012-11-01 13:26:21 +05302354 if (req->cmd_flags & REQ_SECURE &&
2355 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002356 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002357 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002358 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02002359 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002360 /* complete ongoing async transfer before issuing flush */
2361 if (card->host->areq)
2362 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002363 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002364 } else {
Konstantin Dorfman4ab83432013-01-07 14:15:55 +02002365 if (!req && host->areq) {
2366 spin_lock_irqsave(&host->context_info.lock, flags);
2367 host->context_info.is_waiting_last_req = true;
2368 spin_unlock_irqrestore(&host->context_info.lock, flags);
2369 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002370 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002371 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002372
Andrei Warkentin371a6892011-04-11 18:10:25 -05002373out:
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002374 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) {
2375 if (mmc_card_need_bkops(card))
2376 mmc_start_bkops(card, false);
Per Forlinee8a43a2011-07-01 18:55:33 +02002377 /* release host only when there are no more requests */
2378 mmc_release_host(card->host);
Maya Erez5f8ac3b2013-01-04 10:55:00 +02002379 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002380 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Russell Kinga6f6c962006-01-03 22:38:44 +00002383static inline int mmc_blk_readonly(struct mmc_card *card)
2384{
2385 return mmc_card_readonly(card) ||
2386 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2387}
2388
Andrei Warkentin371a6892011-04-11 18:10:25 -05002389static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2390 struct device *parent,
2391 sector_t size,
2392 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002393 const char *subname,
2394 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 struct mmc_blk_data *md;
2397 int devidx, ret;
Maya Ereze1eae982013-01-05 21:51:06 +02002398 unsigned int percentage =
2399 BKOPS_SIZE_PERCENTAGE_TO_QUEUE_DELAYED_WORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002401 devidx = find_first_zero_bit(dev_use, max_devices);
2402 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return ERR_PTR(-ENOSPC);
2404 __set_bit(devidx, dev_use);
2405
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002406 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002407 if (!md) {
2408 ret = -ENOMEM;
2409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002411
Russell Kinga6f6c962006-01-03 22:38:44 +00002412 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002413 * !subname implies we are creating main mmc_blk_data that will be
2414 * associated with mmc_card with mmc_set_drvdata. Due to device
2415 * partitions, devidx will not coincide with a per-physical card
2416 * index anymore so we keep track of a name index.
2417 */
2418 if (!subname) {
2419 md->name_idx = find_first_zero_bit(name_use, max_devices);
2420 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002421 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002422 md->name_idx = ((struct mmc_blk_data *)
2423 dev_to_disk(parent)->private_data)->name_idx;
2424
Johan Rudholmadd710e2011-12-02 08:51:06 +01002425 md->area_type = area_type;
2426
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002427 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002428 * Set the read-only status based on the supported commands
2429 * and the write protect switch.
2430 */
2431 md->read_only = mmc_blk_readonly(card);
2432
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002433 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002434 if (md->disk == NULL) {
2435 ret = -ENOMEM;
2436 goto err_kfree;
2437 }
2438
2439 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002440 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002441 md->usage = 1;
2442
Adrian Hunterd09408a2011-06-23 13:40:28 +03002443 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002444 if (ret)
2445 goto err_putdisk;
2446
Russell Kinga6f6c962006-01-03 22:38:44 +00002447 md->queue.issue_fn = mmc_blk_issue_rq;
2448 md->queue.data = md;
2449
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002450 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002451 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002452 md->disk->fops = &mmc_bdops;
2453 md->disk->private_data = md;
2454 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002455 md->disk->driverfs_dev = parent;
2456 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07002457 md->disk->flags = GENHD_FL_EXT_DEVT;
Loic Pallardy2cc44c42012-08-06 15:12:28 +00002458 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2459 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002460
2461 /*
2462 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2463 *
2464 * - be set for removable media with permanent block devices
2465 * - be unset for removable block devices with permanent media
2466 *
2467 * Since MMC block devices clearly fall under the second
2468 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2469 * should use the block device creation/destruction hotplug
2470 * messages to tell when the card is present.
2471 */
2472
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002473 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2474 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002475
Martin K. Petersene1defc42009-05-22 17:17:49 -04002476 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002477 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002478
Maya Ereze1eae982013-01-05 21:51:06 +02002479 card->bkops_info.size_percentage_to_queue_delayed_work = percentage;
2480 card->bkops_info.min_sectors_to_queue_delayed_work =
2481 ((unsigned int)size * percentage) / 100;
2482
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002483 if (mmc_host_cmd23(card->host)) {
2484 if (mmc_card_mmc(card) ||
2485 (mmc_card_sd(card) &&
2486 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2487 md->flags |= MMC_BLK_CMD23;
2488 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002489
2490 if (mmc_card_mmc(card) &&
2491 md->flags & MMC_BLK_CMD23 &&
2492 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2493 card->ext_csd.rel_sectors)) {
2494 md->flags |= MMC_BLK_REL_WR;
2495 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2496 }
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002499
2500 err_putdisk:
2501 put_disk(md->disk);
2502 err_kfree:
2503 kfree(md);
2504 out:
2505 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Andrei Warkentin371a6892011-04-11 18:10:25 -05002508static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2509{
2510 sector_t size;
2511 struct mmc_blk_data *md;
2512
2513 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2514 /*
2515 * The EXT_CSD sector count is in number or 512 byte
2516 * sectors.
2517 */
2518 size = card->ext_csd.sectors;
2519 } else {
2520 /*
2521 * The CSD capacity field is in units of read_blkbits.
2522 * set_capacity takes units of 512 bytes.
2523 */
2524 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2525 }
2526
Johan Rudholmadd710e2011-12-02 08:51:06 +01002527 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2528 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002529 return md;
2530}
2531
2532static int mmc_blk_alloc_part(struct mmc_card *card,
2533 struct mmc_blk_data *md,
2534 unsigned int part_type,
2535 sector_t size,
2536 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002537 const char *subname,
2538 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002539{
2540 char cap_str[10];
2541 struct mmc_blk_data *part_md;
2542
2543 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002544 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002545 if (IS_ERR(part_md))
2546 return PTR_ERR(part_md);
2547 part_md->part_type = part_type;
2548 list_add(&part_md->part, &md->part);
2549
2550 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2551 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302552 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002553 part_md->disk->disk_name, mmc_card_id(card),
2554 mmc_card_name(card), part_md->part_type, cap_str);
2555 return 0;
2556}
2557
Namjae Jeone0c368d2011-10-06 23:41:38 +09002558/* MMC Physical partitions consist of two boot partitions and
2559 * up to four general purpose partitions.
2560 * For each partition enabled in EXT_CSD a block device will be allocatedi
2561 * to provide access to the partition.
2562 */
2563
Andrei Warkentin371a6892011-04-11 18:10:25 -05002564static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2565{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002566 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002567
2568 if (!mmc_card_mmc(card))
2569 return 0;
2570
Namjae Jeone0c368d2011-10-06 23:41:38 +09002571 for (idx = 0; idx < card->nr_parts; idx++) {
2572 if (card->part[idx].size) {
2573 ret = mmc_blk_alloc_part(card, md,
2574 card->part[idx].part_cfg,
2575 card->part[idx].size >> 9,
2576 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002577 card->part[idx].name,
2578 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002579 if (ret)
2580 return ret;
2581 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002582 }
2583
2584 return ret;
2585}
2586
Andrei Warkentin371a6892011-04-11 18:10:25 -05002587static void mmc_blk_remove_req(struct mmc_blk_data *md)
2588{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002589 struct mmc_card *card;
2590
Andrei Warkentin371a6892011-04-11 18:10:25 -05002591 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002592 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002593 device_remove_file(disk_to_dev(md->disk),
2594 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002595 if (md->disk->flags & GENHD_FL_UP) {
2596 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002597 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2598 card->ext_csd.boot_ro_lockable)
2599 device_remove_file(disk_to_dev(md->disk),
2600 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002601
2602 /* Stop new requests from getting into the queue */
2603 del_gendisk(md->disk);
2604 }
2605
2606 /* Then flush out any already in there */
2607 mmc_cleanup_queue(&md->queue);
2608 mmc_blk_put(md);
2609 }
2610}
2611
2612static void mmc_blk_remove_parts(struct mmc_card *card,
2613 struct mmc_blk_data *md)
2614{
2615 struct list_head *pos, *q;
2616 struct mmc_blk_data *part_md;
2617
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002618 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002619 list_for_each_safe(pos, q, &md->part) {
2620 part_md = list_entry(pos, struct mmc_blk_data, part);
2621 list_del(pos);
2622 mmc_blk_remove_req(part_md);
2623 }
2624}
2625
2626static int mmc_add_disk(struct mmc_blk_data *md)
2627{
2628 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002629 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002630
2631 add_disk(md->disk);
2632 md->force_ro.show = force_ro_show;
2633 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302634 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002635 md->force_ro.attr.name = "force_ro";
2636 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2637 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2638 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002639 goto force_ro_fail;
2640
2641 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2642 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002643 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002644
2645 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2646 mode = S_IRUGO;
2647 else
2648 mode = S_IRUGO | S_IWUSR;
2649
2650 md->power_ro_lock.show = power_ro_lock_show;
2651 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002652 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002653 md->power_ro_lock.attr.mode = mode;
2654 md->power_ro_lock.attr.name =
2655 "ro_lock_until_next_power_on";
2656 ret = device_create_file(disk_to_dev(md->disk),
2657 &md->power_ro_lock);
2658 if (ret)
2659 goto power_ro_lock_fail;
2660 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002661
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002662 md->num_wr_reqs_to_start_packing.show =
2663 num_wr_reqs_to_start_packing_show;
2664 md->num_wr_reqs_to_start_packing.store =
2665 num_wr_reqs_to_start_packing_store;
2666 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2667 md->num_wr_reqs_to_start_packing.attr.name =
2668 "num_wr_reqs_to_start_packing";
2669 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2670 ret = device_create_file(disk_to_dev(md->disk),
2671 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002672 if (ret)
Maya Erez8d006d32012-10-30 09:02:39 +02002673 goto num_wr_reqs_to_start_packing_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002674
Maya Ereze1eae982013-01-05 21:51:06 +02002675 md->bkops_check_threshold.show = bkops_check_threshold_show;
2676 md->bkops_check_threshold.store = bkops_check_threshold_store;
2677 sysfs_attr_init(&md->bkops_check_threshold.attr);
2678 md->bkops_check_threshold.attr.name = "bkops_check_threshold";
2679 md->bkops_check_threshold.attr.mode = S_IRUGO | S_IWUSR;
Maya Erez2d33a192013-01-04 15:52:41 +02002680 ret = device_create_file(disk_to_dev(md->disk),
Maya Ereze1eae982013-01-05 21:51:06 +02002681 &md->bkops_check_threshold);
Maya Erez2d33a192013-01-04 15:52:41 +02002682 if (ret)
Maya Ereze1eae982013-01-05 21:51:06 +02002683 goto bkops_check_threshold_fails;
Maya Erez2d33a192013-01-04 15:52:41 +02002684
Maya Erez64d12462013-01-06 10:19:38 +02002685 md->no_pack_for_random.show = no_pack_for_random_show;
2686 md->no_pack_for_random.store = no_pack_for_random_store;
2687 sysfs_attr_init(&md->no_pack_for_random.attr);
2688 md->no_pack_for_random.attr.name = "no_pack_for_random";
2689 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
2690 ret = device_create_file(disk_to_dev(md->disk),
2691 &md->no_pack_for_random);
2692 if (ret)
2693 goto no_pack_for_random_fails;
2694
Johan Rudholmadd710e2011-12-02 08:51:06 +01002695 return ret;
2696
Maya Erez64d12462013-01-06 10:19:38 +02002697no_pack_for_random_fails:
2698 device_remove_file(disk_to_dev(md->disk),
2699 &md->bkops_check_threshold);
Maya Ereze1eae982013-01-05 21:51:06 +02002700bkops_check_threshold_fails:
Maya Erez2d33a192013-01-04 15:52:41 +02002701 device_remove_file(disk_to_dev(md->disk),
2702 &md->num_wr_reqs_to_start_packing);
Maya Erez8d006d32012-10-30 09:02:39 +02002703num_wr_reqs_to_start_packing_fail:
2704 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002705power_ro_lock_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002706 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002707force_ro_fail:
Maya Erez8d006d32012-10-30 09:02:39 +02002708 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002709
2710 return ret;
2711}
2712
Chris Ballc59d4472011-11-11 22:01:43 -05002713#define CID_MANFID_SANDISK 0x2
2714#define CID_MANFID_TOSHIBA 0x11
2715#define CID_MANFID_MICRON 0x13
Ian Chenc4856122012-11-01 13:26:21 +05302716#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002717
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002718static const struct mmc_fixup blk_fixups[] =
2719{
Chris Ballc59d4472011-11-11 22:01:43 -05002720 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2721 MMC_QUIRK_INAND_CMD38),
2722 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2723 MMC_QUIRK_INAND_CMD38),
2724 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2725 MMC_QUIRK_INAND_CMD38),
2726 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2727 MMC_QUIRK_INAND_CMD38),
2728 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2729 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002730
2731 /*
2732 * Some MMC cards experience performance degradation with CMD23
2733 * instead of CMD12-bounded multiblock transfers. For now we'll
2734 * black list what's bad...
2735 * - Certain Toshiba cards.
2736 *
2737 * N.B. This doesn't affect SD cards.
2738 */
Chris Ballc59d4472011-11-11 22:01:43 -05002739 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002740 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002741 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002742 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002743 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002744 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002745
2746 /*
2747 * Some Micron MMC cards needs longer data read timeout than
2748 * indicated in CSD.
2749 */
Chris Ballc59d4472011-11-11 22:01:43 -05002750 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002751 MMC_QUIRK_LONG_READ_TIME),
2752
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302753 /* Some INAND MCP devices advertise incorrect timeout values */
2754 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2755 MMC_QUIRK_INAND_DATA_TIMEOUT),
2756
Ian Chenc4856122012-11-01 13:26:21 +05302757 /*
2758 * On these Samsung MoviNAND parts, performing secure erase or
2759 * secure trim can result in unrecoverable corruption due to a
2760 * firmware bug.
2761 */
2762 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2763 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2764 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2765 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2766 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2767 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2768 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2769 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2770 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2771 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2772 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2773 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2774 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2775 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2776 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2777 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2778
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002779 END_FIXUP
2780};
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782static int mmc_blk_probe(struct mmc_card *card)
2783{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002784 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002785 char cap_str[10];
2786
Pierre Ossman912490d2005-05-21 10:27:02 +01002787 /*
2788 * Check that the card supports the command class(es) we need.
2789 */
2790 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return -ENODEV;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 md = mmc_blk_alloc(card);
2794 if (IS_ERR(md))
2795 return PTR_ERR(md);
2796
Yi Li444122f2009-02-05 15:31:57 +08002797 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002798 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302799 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002801 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Andrei Warkentin371a6892011-04-11 18:10:25 -05002803 if (mmc_blk_alloc_parts(card, md))
2804 goto out;
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002807 mmc_fixup_device(card, blk_fixups);
2808
San Mehat148c57a2009-07-30 08:21:19 -07002809#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2810 mmc_set_bus_resume_policy(card->host, 1);
2811#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002812 if (mmc_add_disk(md))
2813 goto out;
2814
2815 list_for_each_entry(part_md, &md->part, part) {
2816 if (mmc_add_disk(part_md))
2817 goto out;
2818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return 0;
2820
2821 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002822 mmc_blk_remove_parts(card, md);
2823 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
2827static void mmc_blk_remove(struct mmc_card *card)
2828{
2829 struct mmc_blk_data *md = mmc_get_drvdata(card);
2830
Andrei Warkentin371a6892011-04-11 18:10:25 -05002831 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002832 mmc_claim_host(card->host);
2833 mmc_blk_part_switch(card, md);
2834 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002835 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002837#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2838 mmc_set_bus_resume_policy(card->host, 0);
2839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002843static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002845 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct mmc_blk_data *md = mmc_get_drvdata(card);
2847
2848 if (md) {
2849 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002850 list_for_each_entry(part_md, &md->part, part) {
2851 mmc_queue_suspend(&part_md->queue);
2852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854 return 0;
2855}
2856
2857static int mmc_blk_resume(struct mmc_card *card)
2858{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002859 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 struct mmc_blk_data *md = mmc_get_drvdata(card);
2861
2862 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002863 /*
2864 * Resume involves the card going into idle state,
2865 * so current partition is always the main one.
2866 */
2867 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002869 list_for_each_entry(part_md, &md->part, part) {
2870 mmc_queue_resume(&part_md->queue);
2871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873 return 0;
2874}
2875#else
2876#define mmc_blk_suspend NULL
2877#define mmc_blk_resume NULL
2878#endif
2879
2880static struct mmc_driver mmc_driver = {
2881 .drv = {
2882 .name = "mmcblk",
2883 },
2884 .probe = mmc_blk_probe,
2885 .remove = mmc_blk_remove,
2886 .suspend = mmc_blk_suspend,
2887 .resume = mmc_blk_resume,
2888};
2889
2890static int __init mmc_blk_init(void)
2891{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002892 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002894 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2895 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2896
2897 max_devices = 256 / perdev_minors;
2898
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002899 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2900 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002903 res = mmc_register_driver(&mmc_driver);
2904 if (res)
2905 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002907 return 0;
2908 out2:
2909 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 out:
2911 return res;
2912}
2913
2914static void __exit mmc_blk_exit(void)
2915{
2916 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002917 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
2920module_init(mmc_blk_init);
2921module_exit(mmc_blk_exit);
2922
2923MODULE_LICENSE("GPL");
2924MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2925