blob: 21056b9ef0a0f97bf9f1e71de1e78a3253cd5284 [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 Ossman385e32272006-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
Trey Ramsay8fee4762012-11-16 09:31:41 -060060#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050061
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020062static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040063
64/*
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
67 */
68static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
69
70/*
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
73 */
74static int max_devices;
75
76/* 256 minors, so at most 256 separate devices */
77static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050078static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * There is one mmc_blk_data per slot.
82 */
83struct mmc_blk_data {
84 spinlock_t lock;
85 struct gendisk *disk;
86 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050087 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050089 unsigned int flags;
90#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
91#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000094 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050095 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -050096 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +030097 unsigned int reset_done;
98#define MMC_BLK_READ BIT(0)
99#define MMC_BLK_WRITE BIT(1)
100#define MMC_BLK_DISCARD BIT(2)
101#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102
103 /*
104 * Only set in main mmc_blk_data associated
105 * with mmc_card with mmc_set_drvdata, and keeps
106 * track of the current selected device partition.
107 */
108 unsigned int part_curr;
109 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100110 struct device_attribute power_ro_lock;
111 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Arjan van de Vena621aae2006-01-12 18:43:35 +0000114static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Per Forlind78d4a82011-07-01 18:55:30 +0200116enum mmc_blk_status {
117 MMC_BLK_SUCCESS = 0,
118 MMC_BLK_PARTIAL,
Per Forlind78d4a82011-07-01 18:55:30 +0200119 MMC_BLK_CMD_ERR,
Adrian Hunter67716322011-08-29 16:42:15 +0300120 MMC_BLK_RETRY,
Per Forlind78d4a82011-07-01 18:55:30 +0200121 MMC_BLK_ABORT,
Adrian Hunter67716322011-08-29 16:42:15 +0300122 MMC_BLK_DATA_ERR,
123 MMC_BLK_ECC_ERR,
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530124 MMC_BLK_NOMEDIUM,
Per Forlind78d4a82011-07-01 18:55:30 +0200125};
126
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400127module_param(perdev_minors, int, 0444);
128MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
129
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200130static inline int mmc_blk_part_switch(struct mmc_card *card,
131 struct mmc_blk_data *md);
132static int get_card_status(struct mmc_card *card, u32 *status, int retries);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
135{
136 struct mmc_blk_data *md;
137
Arjan van de Vena621aae2006-01-12 18:43:35 +0000138 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 md = disk->private_data;
140 if (md && md->usage == 0)
141 md = NULL;
142 if (md)
143 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000144 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return md;
147}
148
Andrei Warkentin371a6892011-04-11 18:10:25 -0500149static inline int mmc_get_devidx(struct gendisk *disk)
150{
151 int devmaj = MAJOR(disk_devt(disk));
152 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
153
154 if (!devmaj)
155 devidx = disk->first_minor / perdev_minors;
156 return devidx;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void mmc_blk_put(struct mmc_blk_data *md)
160{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 md->usage--;
163 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800165 blk_cleanup_queue(md->queue.queue);
166
David Woodhouse1dff3142007-11-21 18:45:12 +0100167 __clear_bit(devidx, dev_use);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kfree(md);
171 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000172 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Johan Rudholmadd710e2011-12-02 08:51:06 +0100175static ssize_t power_ro_lock_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
178 int ret;
179 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
180 struct mmc_card *card = md->queue.card;
181 int locked = 0;
182
183 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
184 locked = 2;
185 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
186 locked = 1;
187
188 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
189
190 return ret;
191}
192
193static ssize_t power_ro_lock_store(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t count)
195{
196 int ret;
197 struct mmc_blk_data *md, *part_md;
198 struct mmc_card *card;
199 unsigned long set;
200
201 if (kstrtoul(buf, 0, &set))
202 return -EINVAL;
203
204 if (set != 1)
205 return count;
206
207 md = mmc_blk_get(dev_to_disk(dev));
208 card = md->queue.card;
209
210 mmc_claim_host(card->host);
211
212 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
213 card->ext_csd.boot_ro_lock |
214 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
215 card->ext_csd.part_time);
216 if (ret)
217 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
218 else
219 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
220
221 mmc_release_host(card->host);
222
223 if (!ret) {
224 pr_info("%s: Locking boot partition ro until next power on\n",
225 md->disk->disk_name);
226 set_disk_ro(md->disk, 1);
227
228 list_for_each_entry(part_md, &md->part, part)
229 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
230 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
231 set_disk_ro(part_md->disk, 1);
232 }
233 }
234
235 mmc_blk_put(md);
236 return count;
237}
238
Andrei Warkentin371a6892011-04-11 18:10:25 -0500239static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 int ret;
243 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
244
245 ret = snprintf(buf, PAGE_SIZE, "%d",
246 get_disk_ro(dev_to_disk(dev)) ^
247 md->read_only);
248 mmc_blk_put(md);
249 return ret;
250}
251
252static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
253 const char *buf, size_t count)
254{
255 int ret;
256 char *end;
257 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
258 unsigned long set = simple_strtoul(buf, &end, 0);
259 if (end == buf) {
260 ret = -EINVAL;
261 goto out;
262 }
263
264 set_disk_ro(dev_to_disk(dev), set || md->read_only);
265 ret = count;
266out:
267 mmc_blk_put(md);
268 return ret;
269}
270
Al Viroa5a15612008-03-02 10:33:30 -0500271static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Al Viroa5a15612008-03-02 10:33:30 -0500273 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int ret = -ENXIO;
275
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200276 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (md) {
278 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500279 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700281
Al Viroa5a15612008-03-02 10:33:30 -0500282 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700283 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700284 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200287 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return ret;
290}
291
Al Viroa5a15612008-03-02 10:33:30 -0500292static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Al Viroa5a15612008-03-02 10:33:30 -0500294 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200296 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200298 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
301
302static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800303mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800305 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
306 geo->heads = 4;
307 geo->sectors = 16;
308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
John Calixtocb87ea22011-04-26 18:56:29 -0400311struct mmc_blk_ioc_data {
312 struct mmc_ioc_cmd ic;
313 unsigned char *buf;
314 u64 buf_bytes;
315};
316
317static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
318 struct mmc_ioc_cmd __user *user)
319{
320 struct mmc_blk_ioc_data *idata;
321 int err;
322
323 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
324 if (!idata) {
325 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400326 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400327 }
328
329 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
330 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400331 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400332 }
333
334 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
335 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
336 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400337 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400338 }
339
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100340 if (!idata->buf_bytes)
341 return idata;
342
John Calixtocb87ea22011-04-26 18:56:29 -0400343 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
344 if (!idata->buf) {
345 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400346 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400347 }
348
349 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
350 idata->ic.data_ptr, idata->buf_bytes)) {
351 err = -EFAULT;
352 goto copy_err;
353 }
354
355 return idata;
356
357copy_err:
358 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400359idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400360 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400361out:
John Calixtocb87ea22011-04-26 18:56:29 -0400362 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400363}
364
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200365static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
366 u32 retries_max)
367{
368 int err;
369 u32 retry_count = 0;
370
371 if (!status || !retries_max)
372 return -EINVAL;
373
374 do {
375 err = get_card_status(card, status, 5);
376 if (err)
377 break;
378
379 if (!R1_STATUS(*status) &&
380 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
381 break; /* RPMB programming operation complete */
382
383 /*
384 * Rechedule to give the MMC device a chance to continue
385 * processing the previous command without being polled too
386 * frequently.
387 */
388 usleep_range(1000, 5000);
389 } while (++retry_count < retries_max);
390
391 if (retry_count == retries_max)
392 err = -EPERM;
393
394 return err;
395}
396
John Calixtocb87ea22011-04-26 18:56:29 -0400397static int mmc_blk_ioctl_cmd(struct block_device *bdev,
398 struct mmc_ioc_cmd __user *ic_ptr)
399{
400 struct mmc_blk_ioc_data *idata;
401 struct mmc_blk_data *md;
402 struct mmc_card *card;
403 struct mmc_command cmd = {0};
404 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530405 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400406 struct scatterlist sg;
407 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200408 int is_rpmb = false;
409 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400410
411 /*
412 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
413 * whole block device, not on a partition. This prevents overspray
414 * between sibling partitions.
415 */
416 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
417 return -EPERM;
418
419 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
420 if (IS_ERR(idata))
421 return PTR_ERR(idata);
422
John Calixtocb87ea22011-04-26 18:56:29 -0400423 md = mmc_blk_get(bdev->bd_disk);
424 if (!md) {
425 err = -EINVAL;
Philippe De Swert1c02f002012-04-11 23:31:45 +0300426 goto cmd_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400427 }
428
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200429 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
430 is_rpmb = true;
431
John Calixtocb87ea22011-04-26 18:56:29 -0400432 card = md->queue.card;
433 if (IS_ERR(card)) {
434 err = PTR_ERR(card);
435 goto cmd_done;
436 }
437
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100438 cmd.opcode = idata->ic.opcode;
439 cmd.arg = idata->ic.arg;
440 cmd.flags = idata->ic.flags;
441
442 if (idata->buf_bytes) {
443 data.sg = &sg;
444 data.sg_len = 1;
445 data.blksz = idata->ic.blksz;
446 data.blocks = idata->ic.blocks;
447
448 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
449
450 if (idata->ic.write_flag)
451 data.flags = MMC_DATA_WRITE;
452 else
453 data.flags = MMC_DATA_READ;
454
455 /* data.flags must already be set before doing this. */
456 mmc_set_data_timeout(&data, card);
457
458 /* Allow overriding the timeout_ns for empirical tuning. */
459 if (idata->ic.data_timeout_ns)
460 data.timeout_ns = idata->ic.data_timeout_ns;
461
462 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
463 /*
464 * Pretend this is a data transfer and rely on the
465 * host driver to compute timeout. When all host
466 * drivers support cmd.cmd_timeout for R1B, this
467 * can be changed to:
468 *
469 * mrq.data = NULL;
470 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
471 */
472 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
473 }
474
475 mrq.data = &data;
476 }
477
478 mrq.cmd = &cmd;
479
John Calixtocb87ea22011-04-26 18:56:29 -0400480 mmc_claim_host(card->host);
481
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200482 err = mmc_blk_part_switch(card, md);
483 if (err)
484 goto cmd_rel_host;
485
John Calixtocb87ea22011-04-26 18:56:29 -0400486 if (idata->ic.is_acmd) {
487 err = mmc_app_cmd(card->host, card);
488 if (err)
489 goto cmd_rel_host;
490 }
491
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200492 if (is_rpmb) {
493 err = mmc_set_blockcount(card, data.blocks,
494 idata->ic.write_flag & (1 << 31));
495 if (err)
496 goto cmd_rel_host;
497 }
498
John Calixtocb87ea22011-04-26 18:56:29 -0400499 mmc_wait_for_req(card->host, &mrq);
500
501 if (cmd.error) {
502 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
503 __func__, cmd.error);
504 err = cmd.error;
505 goto cmd_rel_host;
506 }
507 if (data.error) {
508 dev_err(mmc_dev(card->host), "%s: data error %d\n",
509 __func__, data.error);
510 err = data.error;
511 goto cmd_rel_host;
512 }
513
514 /*
515 * According to the SD specs, some commands require a delay after
516 * issuing the command.
517 */
518 if (idata->ic.postsleep_min_us)
519 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
520
521 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
522 err = -EFAULT;
523 goto cmd_rel_host;
524 }
525
526 if (!idata->ic.write_flag) {
527 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
528 idata->buf, idata->buf_bytes)) {
529 err = -EFAULT;
530 goto cmd_rel_host;
531 }
532 }
533
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200534 if (is_rpmb) {
535 /*
536 * Ensure RPMB command has completed by polling CMD13
537 * "Send Status".
538 */
539 err = ioctl_rpmb_card_status_poll(card, &status, 5);
540 if (err)
541 dev_err(mmc_dev(card->host),
542 "%s: Card Status=0x%08X, error %d\n",
543 __func__, status, err);
544 }
545
John Calixtocb87ea22011-04-26 18:56:29 -0400546cmd_rel_host:
547 mmc_release_host(card->host);
548
549cmd_done:
550 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300551cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400552 kfree(idata->buf);
553 kfree(idata);
554 return err;
555}
556
557static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
558 unsigned int cmd, unsigned long arg)
559{
560 int ret = -EINVAL;
561 if (cmd == MMC_IOC_CMD)
562 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
563 return ret;
564}
565
566#ifdef CONFIG_COMPAT
567static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
568 unsigned int cmd, unsigned long arg)
569{
570 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
571}
572#endif
573
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700574static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500575 .open = mmc_blk_open,
576 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800577 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400579 .ioctl = mmc_blk_ioctl,
580#ifdef CONFIG_COMPAT
581 .compat_ioctl = mmc_blk_compat_ioctl,
582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583};
584
Andrei Warkentin371a6892011-04-11 18:10:25 -0500585static inline int mmc_blk_part_switch(struct mmc_card *card,
586 struct mmc_blk_data *md)
587{
588 int ret;
589 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300590
Andrei Warkentin371a6892011-04-11 18:10:25 -0500591 if (main_md->part_curr == md->part_type)
592 return 0;
593
594 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300595 u8 part_config = card->ext_csd.part_config;
596
597 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
598 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500599
600 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300601 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500602 card->ext_csd.part_time);
603 if (ret)
604 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300605
606 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300607 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500608
609 main_md->part_curr = md->part_type;
610 return 0;
611}
612
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700613static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
614{
615 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100616 u32 result;
617 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700618
Venkatraman Sad5fd972011-08-25 00:30:50 +0530619 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400620 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400621 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700622
623 struct scatterlist sg;
624
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700625 cmd.opcode = MMC_APP_CMD;
626 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700627 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700628
629 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700630 if (err)
631 return (u32)-1;
632 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700633 return (u32)-1;
634
635 memset(&cmd, 0, sizeof(struct mmc_command));
636
637 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
638 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700639 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700640
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700641 data.blksz = 4;
642 data.blocks = 1;
643 data.flags = MMC_DATA_READ;
644 data.sg = &sg;
645 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530646 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700647
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700648 mrq.cmd = &cmd;
649 mrq.data = &data;
650
Ben Dooks051913d2009-06-08 23:33:57 +0100651 blocks = kmalloc(4, GFP_KERNEL);
652 if (!blocks)
653 return (u32)-1;
654
655 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700656
657 mmc_wait_for_req(card->host, &mrq);
658
Ben Dooks051913d2009-06-08 23:33:57 +0100659 result = ntohl(*blocks);
660 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700661
Ben Dooks051913d2009-06-08 23:33:57 +0100662 if (cmd.error || data.error)
663 result = (u32)-1;
664
665 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700666}
667
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100668static int send_stop(struct mmc_card *card, u32 *status)
669{
670 struct mmc_command cmd = {0};
671 int err;
672
673 cmd.opcode = MMC_STOP_TRANSMISSION;
674 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
675 err = mmc_wait_for_cmd(card->host, &cmd, 5);
676 if (err == 0)
677 *status = cmd.resp[0];
678 return err;
679}
680
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100681static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300682{
Chris Ball1278dba2011-04-13 23:40:30 -0400683 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300684 int err;
685
Adrian Hunter504f1912008-10-16 12:55:25 +0300686 cmd.opcode = MMC_SEND_STATUS;
687 if (!mmc_host_is_spi(card->host))
688 cmd.arg = card->rca << 16;
689 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100690 err = mmc_wait_for_cmd(card->host, &cmd, retries);
691 if (err == 0)
692 *status = cmd.resp[0];
693 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300694}
695
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530696#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100697#define ERR_RETRY 2
698#define ERR_ABORT 1
699#define ERR_CONTINUE 0
700
701static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
702 bool status_valid, u32 status)
703{
704 switch (error) {
705 case -EILSEQ:
706 /* response crc error, retry the r/w cmd */
707 pr_err("%s: %s sending %s command, card status %#x\n",
708 req->rq_disk->disk_name, "response CRC error",
709 name, status);
710 return ERR_RETRY;
711
712 case -ETIMEDOUT:
713 pr_err("%s: %s sending %s command, card status %#x\n",
714 req->rq_disk->disk_name, "timed out", name, status);
715
716 /* If the status cmd initially failed, retry the r/w cmd */
717 if (!status_valid)
718 return ERR_RETRY;
719
720 /*
721 * If it was a r/w cmd crc error, or illegal command
722 * (eg, issued in wrong state) then retry - we should
723 * have corrected the state problem above.
724 */
725 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
726 return ERR_RETRY;
727
728 /* Otherwise abort the command */
729 return ERR_ABORT;
730
731 default:
732 /* We don't understand the error code the driver gave us */
733 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
734 req->rq_disk->disk_name, error, status);
735 return ERR_ABORT;
736 }
737}
738
739/*
740 * Initial r/w and stop cmd error recovery.
741 * We don't know whether the card received the r/w cmd or not, so try to
742 * restore things back to a sane state. Essentially, we do this as follows:
743 * - Obtain card status. If the first attempt to obtain card status fails,
744 * the status word will reflect the failed status cmd, not the failed
745 * r/w cmd. If we fail to obtain card status, it suggests we can no
746 * longer communicate with the card.
747 * - Check the card state. If the card received the cmd but there was a
748 * transient problem with the response, it might still be in a data transfer
749 * mode. Try to send it a stop command. If this fails, we can't recover.
750 * - If the r/w cmd failed due to a response CRC error, it was probably
751 * transient, so retry the cmd.
752 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
753 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
754 * illegal cmd, retry.
755 * Otherwise we don't understand what happened, so abort.
756 */
757static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300758 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100759{
760 bool prev_cmd_status_valid = true;
761 u32 status, stop_status = 0;
762 int err, retry;
763
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530764 if (mmc_card_removed(card))
765 return ERR_NOMEDIUM;
766
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100767 /*
768 * Try to get card status which indicates both the card state
769 * and why there was no response. If the first attempt fails,
770 * we can't be sure the returned status is for the r/w command.
771 */
772 for (retry = 2; retry >= 0; retry--) {
773 err = get_card_status(card, &status, 0);
774 if (!err)
775 break;
776
777 prev_cmd_status_valid = false;
778 pr_err("%s: error %d sending status command, %sing\n",
779 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
780 }
781
782 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530783 if (err) {
784 /* Check if the card is removed */
785 if (mmc_detect_card_removed(card->host))
786 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100787 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530788 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100789
Adrian Hunter67716322011-08-29 16:42:15 +0300790 /* Flag ECC errors */
791 if ((status & R1_CARD_ECC_FAILED) ||
792 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
793 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
794 *ecc_err = 1;
795
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100796 /*
797 * Check the current card state. If it is in some data transfer
798 * mode, tell it to stop (and hopefully transition back to TRAN.)
799 */
800 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
801 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
802 err = send_stop(card, &stop_status);
803 if (err)
804 pr_err("%s: error %d sending stop command\n",
805 req->rq_disk->disk_name, err);
806
807 /*
808 * If the stop cmd also timed out, the card is probably
809 * not present, so abort. Other errors are bad news too.
810 */
811 if (err)
812 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300813 if (stop_status & R1_CARD_ECC_FAILED)
814 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100815 }
816
817 /* Check for set block count errors */
818 if (brq->sbc.error)
819 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
820 prev_cmd_status_valid, status);
821
822 /* Check for r/w command errors */
823 if (brq->cmd.error)
824 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
825 prev_cmd_status_valid, status);
826
Adrian Hunter67716322011-08-29 16:42:15 +0300827 /* Data errors */
828 if (!brq->stop.error)
829 return ERR_CONTINUE;
830
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100831 /* Now for stop errors. These aren't fatal to the transfer. */
832 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
833 req->rq_disk->disk_name, brq->stop.error,
834 brq->cmd.resp[0], status);
835
836 /*
837 * Subsitute in our own stop status as this will give the error
838 * state which happened during the execution of the r/w command.
839 */
840 if (stop_status) {
841 brq->stop.resp[0] = stop_status;
842 brq->stop.error = 0;
843 }
844 return ERR_CONTINUE;
845}
846
Adrian Hunter67716322011-08-29 16:42:15 +0300847static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
848 int type)
849{
850 int err;
851
852 if (md->reset_done & type)
853 return -EEXIST;
854
855 md->reset_done |= type;
856 err = mmc_hw_reset(host);
857 /* Ensure we switch back to the correct partition */
858 if (err != -EOPNOTSUPP) {
859 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
860 int part_err;
861
862 main_md->part_curr = main_md->part_type;
863 part_err = mmc_blk_part_switch(host->card, md);
864 if (part_err) {
865 /*
866 * We have failed to get back into the correct
867 * partition, so we need to abort the whole request.
868 */
869 return -ENODEV;
870 }
871 }
872 return err;
873}
874
875static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
876{
877 md->reset_done &= ~type;
878}
879
Adrian Hunterbd788c92010-08-11 14:17:47 -0700880static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
881{
882 struct mmc_blk_data *md = mq->data;
883 struct mmc_card *card = md->queue.card;
884 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300885 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700886
Adrian Hunterbd788c92010-08-11 14:17:47 -0700887 if (!mmc_can_erase(card)) {
888 err = -EOPNOTSUPP;
889 goto out;
890 }
891
892 from = blk_rq_pos(req);
893 nr = blk_rq_sectors(req);
894
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900895 if (mmc_can_discard(card))
896 arg = MMC_DISCARD_ARG;
897 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700898 arg = MMC_TRIM_ARG;
899 else
900 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300901retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500902 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
903 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
904 INAND_CMD38_ARG_EXT_CSD,
905 arg == MMC_TRIM_ARG ?
906 INAND_CMD38_ARG_TRIM :
907 INAND_CMD38_ARG_ERASE,
908 0);
909 if (err)
910 goto out;
911 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700912 err = mmc_erase(card, from, nr, arg);
913out:
Adrian Hunter67716322011-08-29 16:42:15 +0300914 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
915 goto retry;
916 if (!err)
917 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +0530918 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700919
Adrian Hunterbd788c92010-08-11 14:17:47 -0700920 return err ? 0 : 1;
921}
922
Adrian Hunter49804542010-08-11 14:17:50 -0700923static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
924 struct request *req)
925{
926 struct mmc_blk_data *md = mq->data;
927 struct mmc_card *card = md->queue.card;
Adrian Hunter28302812012-04-05 14:45:48 +0300928 unsigned int from, nr, arg, trim_arg, erase_arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300929 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700930
Kyungmin Parkd9ddd622011-10-14 14:15:48 +0900931 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700932 err = -EOPNOTSUPP;
933 goto out;
934 }
935
936 from = blk_rq_pos(req);
937 nr = blk_rq_sectors(req);
938
Adrian Hunter28302812012-04-05 14:45:48 +0300939 /* The sanitize operation is supported at v4.5 only */
940 if (mmc_can_sanitize(card)) {
941 erase_arg = MMC_ERASE_ARG;
942 trim_arg = MMC_TRIM_ARG;
943 } else {
944 erase_arg = MMC_SECURE_ERASE_ARG;
945 trim_arg = MMC_SECURE_TRIM1_ARG;
946 }
947
948 if (mmc_erase_group_aligned(card, from, nr))
949 arg = erase_arg;
950 else if (mmc_can_trim(card))
951 arg = trim_arg;
952 else {
953 err = -EINVAL;
954 goto out;
955 }
Adrian Hunter67716322011-08-29 16:42:15 +0300956retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500957 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
958 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
959 INAND_CMD38_ARG_EXT_CSD,
960 arg == MMC_SECURE_TRIM1_ARG ?
961 INAND_CMD38_ARG_SECTRIM1 :
962 INAND_CMD38_ARG_SECERASE,
963 0);
964 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300965 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500966 }
Adrian Hunter28302812012-04-05 14:45:48 +0300967
Adrian Hunter49804542010-08-11 14:17:50 -0700968 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300969 if (err == -EIO)
970 goto out_retry;
971 if (err)
972 goto out;
973
974 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500975 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
976 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
977 INAND_CMD38_ARG_EXT_CSD,
978 INAND_CMD38_ARG_SECTRIM2,
979 0);
980 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300981 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500982 }
Adrian Hunter28302812012-04-05 14:45:48 +0300983
Adrian Hunter49804542010-08-11 14:17:50 -0700984 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300985 if (err == -EIO)
986 goto out_retry;
987 if (err)
988 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500989 }
Adrian Hunter28302812012-04-05 14:45:48 +0300990
991 if (mmc_can_sanitize(card))
992 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
993 EXT_CSD_SANITIZE_START, 1, 0);
994out_retry:
995 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300996 goto retry;
997 if (!err)
998 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300999out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301000 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001001
Adrian Hunter49804542010-08-11 14:17:50 -07001002 return err ? 0 : 1;
1003}
1004
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001005static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1006{
1007 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001008 struct mmc_card *card = md->queue.card;
1009 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001010
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001011 ret = mmc_flush_cache(card);
1012 if (ret)
1013 ret = -EIO;
1014
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301015 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001016
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001017 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001018}
1019
1020/*
1021 * Reformat current write as a reliable write, supporting
1022 * both legacy and the enhanced reliable write MMC cards.
1023 * In each transfer we'll handle only as much as a single
1024 * reliable write can handle, thus finish the request in
1025 * partial completions.
1026 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001027static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1028 struct mmc_card *card,
1029 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001030{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001031 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1032 /* Legacy mode imposes restrictions on transfers. */
1033 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1034 brq->data.blocks = 1;
1035
1036 if (brq->data.blocks > card->ext_csd.rel_sectors)
1037 brq->data.blocks = card->ext_csd.rel_sectors;
1038 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1039 brq->data.blocks = 1;
1040 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001041}
1042
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001043#define CMD_ERRORS \
1044 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1045 R1_ADDRESS_ERROR | /* Misaligned address */ \
1046 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1047 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1048 R1_CC_ERROR | /* Card controller error */ \
1049 R1_ERROR) /* General/unknown error */
1050
Per Forlinee8a43a2011-07-01 18:55:33 +02001051static int mmc_blk_err_check(struct mmc_card *card,
1052 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001053{
Per Forlinee8a43a2011-07-01 18:55:33 +02001054 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1055 mmc_active);
1056 struct mmc_blk_request *brq = &mq_mrq->brq;
1057 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001058 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001059
1060 /*
1061 * sbc.error indicates a problem with the set block count
1062 * command. No data will have been transferred.
1063 *
1064 * cmd.error indicates a problem with the r/w command. No
1065 * data will have been transferred.
1066 *
1067 * stop.error indicates a problem with the stop command. Data
1068 * may have been transferred, or may still be transferring.
1069 */
Adrian Hunter67716322011-08-29 16:42:15 +03001070 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1071 brq->data.error) {
1072 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001073 case ERR_RETRY:
1074 return MMC_BLK_RETRY;
1075 case ERR_ABORT:
1076 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301077 case ERR_NOMEDIUM:
1078 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001079 case ERR_CONTINUE:
1080 break;
1081 }
1082 }
1083
1084 /*
1085 * Check for errors relating to the execution of the
1086 * initial command - such as address errors. No data
1087 * has been transferred.
1088 */
1089 if (brq->cmd.resp[0] & CMD_ERRORS) {
1090 pr_err("%s: r/w command failed, status = %#x\n",
1091 req->rq_disk->disk_name, brq->cmd.resp[0]);
1092 return MMC_BLK_ABORT;
1093 }
1094
1095 /*
1096 * Everything else is either success, or a data error of some
1097 * kind. If it was a write, we may have transitioned to
1098 * program mode, which we have to wait for it to complete.
1099 */
1100 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1101 u32 status;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001102 unsigned long timeout;
1103
1104 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001105 do {
1106 int err = get_card_status(card, &status, 5);
1107 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301108 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001109 req->rq_disk->disk_name, err);
1110 return MMC_BLK_CMD_ERR;
1111 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06001112
1113 /* Timeout if the device never becomes ready for data
1114 * and never leaves the program state.
1115 */
1116 if (time_after(jiffies, timeout)) {
1117 pr_err("%s: Card stuck in programming state!"\
1118 " %s %s\n", mmc_hostname(card->host),
1119 req->rq_disk->disk_name, __func__);
1120
1121 return MMC_BLK_CMD_ERR;
1122 }
Per Forlind78d4a82011-07-01 18:55:30 +02001123 /*
1124 * Some cards mishandle the status bits,
1125 * so make sure to check both the busy
1126 * indication and the card state.
1127 */
1128 } while (!(status & R1_READY_FOR_DATA) ||
1129 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1130 }
1131
1132 if (brq->data.error) {
1133 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1134 req->rq_disk->disk_name, brq->data.error,
1135 (unsigned)blk_rq_pos(req),
1136 (unsigned)blk_rq_sectors(req),
1137 brq->cmd.resp[0], brq->stop.resp[0]);
1138
1139 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001140 if (ecc_err)
1141 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001142 return MMC_BLK_DATA_ERR;
1143 } else {
1144 return MMC_BLK_CMD_ERR;
1145 }
1146 }
1147
Adrian Hunter67716322011-08-29 16:42:15 +03001148 if (!brq->data.bytes_xfered)
1149 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001150
Adrian Hunter67716322011-08-29 16:42:15 +03001151 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1152 return MMC_BLK_PARTIAL;
1153
1154 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001155}
1156
Per Forlin54d49d72011-07-01 18:55:29 +02001157static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1158 struct mmc_card *card,
1159 int disable_multi,
1160 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Per Forlin54d49d72011-07-01 18:55:29 +02001162 u32 readcmd, writecmd;
1163 struct mmc_blk_request *brq = &mqrq->brq;
1164 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301166 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001168 /*
1169 * Reliable writes are used to implement Forced Unit Access and
1170 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001171 *
1172 * XXX: this really needs a good explanation of why REQ_META
1173 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001174 */
1175 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1176 (req->cmd_flags & REQ_META)) &&
1177 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001178 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001179
Per Forlin54d49d72011-07-01 18:55:29 +02001180 memset(brq, 0, sizeof(struct mmc_blk_request));
1181 brq->mrq.cmd = &brq->cmd;
1182 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Per Forlin54d49d72011-07-01 18:55:29 +02001184 brq->cmd.arg = blk_rq_pos(req);
1185 if (!mmc_card_blockaddr(card))
1186 brq->cmd.arg <<= 9;
1187 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1188 brq->data.blksz = 512;
1189 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1190 brq->stop.arg = 0;
1191 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1192 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Per Forlin54d49d72011-07-01 18:55:29 +02001194 /*
1195 * The block layer doesn't support all sector count
1196 * restrictions, so we need to be prepared for too big
1197 * requests.
1198 */
1199 if (brq->data.blocks > card->host->max_blk_count)
1200 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001202 if (brq->data.blocks > 1) {
1203 /*
1204 * After a read error, we redo the request one sector
1205 * at a time in order to accurately determine which
1206 * sectors can be read successfully.
1207 */
1208 if (disable_multi)
1209 brq->data.blocks = 1;
1210
1211 /* Some controllers can't do multiblock reads due to hw bugs */
1212 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1213 rq_data_dir(req) == READ)
1214 brq->data.blocks = 1;
1215 }
Per Forlin54d49d72011-07-01 18:55:29 +02001216
1217 if (brq->data.blocks > 1 || do_rel_wr) {
1218 /* SPI multiblock writes terminate using a special
1219 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001220 */
Per Forlin54d49d72011-07-01 18:55:29 +02001221 if (!mmc_host_is_spi(card->host) ||
1222 rq_data_dir(req) == READ)
1223 brq->mrq.stop = &brq->stop;
1224 readcmd = MMC_READ_MULTIPLE_BLOCK;
1225 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1226 } else {
1227 brq->mrq.stop = NULL;
1228 readcmd = MMC_READ_SINGLE_BLOCK;
1229 writecmd = MMC_WRITE_BLOCK;
1230 }
1231 if (rq_data_dir(req) == READ) {
1232 brq->cmd.opcode = readcmd;
1233 brq->data.flags |= MMC_DATA_READ;
1234 } else {
1235 brq->cmd.opcode = writecmd;
1236 brq->data.flags |= MMC_DATA_WRITE;
1237 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001238
Per Forlin54d49d72011-07-01 18:55:29 +02001239 if (do_rel_wr)
1240 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001241
Per Forlin54d49d72011-07-01 18:55:29 +02001242 /*
Saugata Das42659002011-12-21 13:09:17 +05301243 * Data tag is used only during writing meta data to speed
1244 * up write and any subsequent read of this meta data
1245 */
1246 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1247 (req->cmd_flags & REQ_META) &&
1248 (rq_data_dir(req) == WRITE) &&
1249 ((brq->data.blocks * brq->data.blksz) >=
1250 card->ext_csd.data_tag_unit_size);
1251
1252 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001253 * Pre-defined multi-block transfers are preferable to
1254 * open ended-ones (and necessary for reliable writes).
1255 * However, it is not sufficient to just send CMD23,
1256 * and avoid the final CMD12, as on an error condition
1257 * CMD12 (stop) needs to be sent anyway. This, coupled
1258 * with Auto-CMD23 enhancements provided by some
1259 * hosts, means that the complexity of dealing
1260 * with this is best left to the host. If CMD23 is
1261 * supported by card and host, we'll fill sbc in and let
1262 * the host deal with handling it correctly. This means
1263 * that for hosts that don't expose MMC_CAP_CMD23, no
1264 * change of behavior will be observed.
1265 *
1266 * N.B: Some MMC cards experience perf degradation.
1267 * We'll avoid using CMD23-bounded multiblock writes for
1268 * these, while retaining features like reliable writes.
1269 */
Saugata Das42659002011-12-21 13:09:17 +05301270 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1271 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1272 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001273 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1274 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301275 (do_rel_wr ? (1 << 31) : 0) |
1276 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001277 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1278 brq->mrq.sbc = &brq->sbc;
1279 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001280
Per Forlin54d49d72011-07-01 18:55:29 +02001281 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001282
Per Forlin54d49d72011-07-01 18:55:29 +02001283 brq->data.sg = mqrq->sg;
1284 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001285
Per Forlin54d49d72011-07-01 18:55:29 +02001286 /*
1287 * Adjust the sg list so it is the same size as the
1288 * request.
1289 */
1290 if (brq->data.blocks != blk_rq_sectors(req)) {
1291 int i, data_size = brq->data.blocks << 9;
1292 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001293
Per Forlin54d49d72011-07-01 18:55:29 +02001294 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1295 data_size -= sg->length;
1296 if (data_size <= 0) {
1297 sg->length += data_size;
1298 i++;
1299 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001300 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001301 }
Per Forlin54d49d72011-07-01 18:55:29 +02001302 brq->data.sg_len = i;
1303 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001304
Per Forlinee8a43a2011-07-01 18:55:33 +02001305 mqrq->mmc_active.mrq = &brq->mrq;
1306 mqrq->mmc_active.err_check = mmc_blk_err_check;
1307
Per Forlin54d49d72011-07-01 18:55:29 +02001308 mmc_queue_bounce_pre(mqrq);
1309}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Adrian Hunter67716322011-08-29 16:42:15 +03001311static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1312 struct mmc_blk_request *brq, struct request *req,
1313 int ret)
1314{
1315 /*
1316 * If this is an SD card and we're writing, we can first
1317 * mark the known good sectors as ok.
1318 *
1319 * If the card is not SD, we can still ok written sectors
1320 * as reported by the controller (which might be less than
1321 * the real number of written sectors, but never more).
1322 */
1323 if (mmc_card_sd(card)) {
1324 u32 blocks;
1325
1326 blocks = mmc_sd_num_wr_blocks(card);
1327 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301328 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001329 }
1330 } else {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301331 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001332 }
1333 return ret;
1334}
1335
Per Forlinee8a43a2011-07-01 18:55:33 +02001336static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001337{
1338 struct mmc_blk_data *md = mq->data;
1339 struct mmc_card *card = md->queue.card;
1340 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001341 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001342 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001343 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301344 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001345 struct mmc_async_req *areq;
1346
1347 if (!rqc && !mq->mqrq_prev->req)
1348 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001349
1350 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001351 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301352 /*
1353 * When 4KB native sector is enabled, only 8 blocks
1354 * multiple read or write is allowed
1355 */
1356 if ((brq->data.blocks & 0x07) &&
1357 (card->ext_csd.data_sector_size == 4096)) {
1358 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1359 req->rq_disk->disk_name);
1360 goto cmd_abort;
1361 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001362 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1363 areq = &mq->mqrq_cur->mmc_active;
1364 } else
1365 areq = NULL;
1366 areq = mmc_start_req(card->host, areq, (int *) &status);
1367 if (!areq)
1368 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001369
Per Forlinee8a43a2011-07-01 18:55:33 +02001370 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1371 brq = &mq_rq->brq;
1372 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001373 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001374 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001375
Per Forlind78d4a82011-07-01 18:55:30 +02001376 switch (status) {
1377 case MMC_BLK_SUCCESS:
1378 case MMC_BLK_PARTIAL:
1379 /*
1380 * A block was successfully transferred.
1381 */
Adrian Hunter67716322011-08-29 16:42:15 +03001382 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301383 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001384 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001385 /*
1386 * If the blk_end_request function returns non-zero even
1387 * though all data has been transferred and no errors
1388 * were returned by the host controller, it's a bug.
1389 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001390 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301391 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001392 __func__, blk_rq_bytes(req),
1393 brq->data.bytes_xfered);
1394 rqc = NULL;
1395 goto cmd_abort;
1396 }
Per Forlind78d4a82011-07-01 18:55:30 +02001397 break;
1398 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001399 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1400 if (!mmc_blk_reset(md, card->host, type))
1401 break;
1402 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001403 case MMC_BLK_RETRY:
1404 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001405 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001406 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001407 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001408 if (!mmc_blk_reset(md, card->host, type))
1409 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001410 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001411 case MMC_BLK_DATA_ERR: {
1412 int err;
1413
1414 err = mmc_blk_reset(md, card->host, type);
1415 if (!err)
1416 break;
1417 if (err == -ENODEV)
1418 goto cmd_abort;
1419 /* Fall through */
1420 }
1421 case MMC_BLK_ECC_ERR:
1422 if (brq->data.blocks > 1) {
1423 /* Redo read one sector at a time */
1424 pr_warning("%s: retrying using single block read\n",
1425 req->rq_disk->disk_name);
1426 disable_multi = 1;
1427 break;
1428 }
Per Forlind78d4a82011-07-01 18:55:30 +02001429 /*
1430 * After an error, we redo I/O one sector at a
1431 * time, so we only reach here after trying to
1432 * read a single sector.
1433 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301434 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001435 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001436 if (!ret)
1437 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001438 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301439 case MMC_BLK_NOMEDIUM:
1440 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001441 }
1442
Per Forlinee8a43a2011-07-01 18:55:33 +02001443 if (ret) {
1444 /*
Adrian Hunter67716322011-08-29 16:42:15 +03001445 * In case of a incomplete request
Per Forlinee8a43a2011-07-01 18:55:33 +02001446 * prepare it again and resend.
1447 */
1448 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1449 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 } while (ret);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return 1;
1454
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001455 cmd_abort:
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301456 if (mmc_card_removed(card))
1457 req->cmd_flags |= REQ_QUIET;
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001458 while (ret)
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301459 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Per Forlinee8a43a2011-07-01 18:55:33 +02001461 start_new_req:
1462 if (rqc) {
1463 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1464 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468}
1469
Adrian Hunterbd788c92010-08-11 14:17:47 -07001470static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1471{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001472 int ret;
1473 struct mmc_blk_data *md = mq->data;
1474 struct mmc_card *card = md->queue.card;
1475
Per Forlinee8a43a2011-07-01 18:55:33 +02001476 if (req && !mq->mqrq_prev->req)
1477 /* claim host only for the first request */
1478 mmc_claim_host(card->host);
1479
Andrei Warkentin371a6892011-04-11 18:10:25 -05001480 ret = mmc_blk_part_switch(card, md);
1481 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001482 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301483 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001484 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001485 ret = 0;
1486 goto out;
1487 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001488
Per Forlinee8a43a2011-07-01 18:55:33 +02001489 if (req && req->cmd_flags & REQ_DISCARD) {
1490 /* complete ongoing async transfer before issuing discard */
1491 if (card->host->areq)
1492 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chen3550ccd2012-08-29 15:05:36 +09001493 if (req->cmd_flags & REQ_SECURE &&
1494 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001495 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001496 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001497 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001498 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001499 /* complete ongoing async transfer before issuing flush */
1500 if (card->host->areq)
1501 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001502 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001503 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001504 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001505 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001506
Andrei Warkentin371a6892011-04-11 18:10:25 -05001507out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001508 if (!req)
1509 /* release host only when there are no more requests */
1510 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001511 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Russell Kinga6f6c962006-01-03 22:38:44 +00001514static inline int mmc_blk_readonly(struct mmc_card *card)
1515{
1516 return mmc_card_readonly(card) ||
1517 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1518}
1519
Andrei Warkentin371a6892011-04-11 18:10:25 -05001520static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1521 struct device *parent,
1522 sector_t size,
1523 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001524 const char *subname,
1525 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct mmc_blk_data *md;
1528 int devidx, ret;
1529
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001530 devidx = find_first_zero_bit(dev_use, max_devices);
1531 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return ERR_PTR(-ENOSPC);
1533 __set_bit(devidx, dev_use);
1534
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001535 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001536 if (!md) {
1537 ret = -ENOMEM;
1538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001540
Russell Kinga6f6c962006-01-03 22:38:44 +00001541 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001542 * !subname implies we are creating main mmc_blk_data that will be
1543 * associated with mmc_card with mmc_set_drvdata. Due to device
1544 * partitions, devidx will not coincide with a per-physical card
1545 * index anymore so we keep track of a name index.
1546 */
1547 if (!subname) {
1548 md->name_idx = find_first_zero_bit(name_use, max_devices);
1549 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001550 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001551 md->name_idx = ((struct mmc_blk_data *)
1552 dev_to_disk(parent)->private_data)->name_idx;
1553
Johan Rudholmadd710e2011-12-02 08:51:06 +01001554 md->area_type = area_type;
1555
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001556 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001557 * Set the read-only status based on the supported commands
1558 * and the write protect switch.
1559 */
1560 md->read_only = mmc_blk_readonly(card);
1561
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001562 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001563 if (md->disk == NULL) {
1564 ret = -ENOMEM;
1565 goto err_kfree;
1566 }
1567
1568 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001569 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001570 md->usage = 1;
1571
Adrian Hunterd09408a2011-06-23 13:40:28 +03001572 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001573 if (ret)
1574 goto err_putdisk;
1575
Russell Kinga6f6c962006-01-03 22:38:44 +00001576 md->queue.issue_fn = mmc_blk_issue_rq;
1577 md->queue.data = md;
1578
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001579 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001580 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001581 md->disk->fops = &mmc_bdops;
1582 md->disk->private_data = md;
1583 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001584 md->disk->driverfs_dev = parent;
1585 set_disk_ro(md->disk, md->read_only || default_ro);
Loic Pallardy53d8f972012-08-06 17:12:28 +02001586 if (area_type & MMC_BLK_DATA_AREA_RPMB)
1587 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001588
1589 /*
1590 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1591 *
1592 * - be set for removable media with permanent block devices
1593 * - be unset for removable block devices with permanent media
1594 *
1595 * Since MMC block devices clearly fall under the second
1596 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1597 * should use the block device creation/destruction hotplug
1598 * messages to tell when the card is present.
1599 */
1600
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001601 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1602 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001603
Saugata Dasa5075eb2012-05-17 16:32:21 +05301604 if (mmc_card_mmc(card))
1605 blk_queue_logical_block_size(md->queue.queue,
1606 card->ext_csd.data_sector_size);
1607 else
1608 blk_queue_logical_block_size(md->queue.queue, 512);
1609
Andrei Warkentin371a6892011-04-11 18:10:25 -05001610 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001611
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001612 if (mmc_host_cmd23(card->host)) {
1613 if (mmc_card_mmc(card) ||
1614 (mmc_card_sd(card) &&
1615 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1616 md->flags |= MMC_BLK_CMD23;
1617 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001618
1619 if (mmc_card_mmc(card) &&
1620 md->flags & MMC_BLK_CMD23 &&
1621 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1622 card->ext_csd.rel_sectors)) {
1623 md->flags |= MMC_BLK_REL_WR;
1624 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001628
1629 err_putdisk:
1630 put_disk(md->disk);
1631 err_kfree:
1632 kfree(md);
1633 out:
1634 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Andrei Warkentin371a6892011-04-11 18:10:25 -05001637static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1638{
1639 sector_t size;
1640 struct mmc_blk_data *md;
1641
1642 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1643 /*
1644 * The EXT_CSD sector count is in number or 512 byte
1645 * sectors.
1646 */
1647 size = card->ext_csd.sectors;
1648 } else {
1649 /*
1650 * The CSD capacity field is in units of read_blkbits.
1651 * set_capacity takes units of 512 bytes.
1652 */
1653 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1654 }
1655
Johan Rudholmadd710e2011-12-02 08:51:06 +01001656 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1657 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001658 return md;
1659}
1660
1661static int mmc_blk_alloc_part(struct mmc_card *card,
1662 struct mmc_blk_data *md,
1663 unsigned int part_type,
1664 sector_t size,
1665 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001666 const char *subname,
1667 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001668{
1669 char cap_str[10];
1670 struct mmc_blk_data *part_md;
1671
1672 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001673 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001674 if (IS_ERR(part_md))
1675 return PTR_ERR(part_md);
1676 part_md->part_type = part_type;
1677 list_add(&part_md->part, &md->part);
1678
1679 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1680 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301681 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001682 part_md->disk->disk_name, mmc_card_id(card),
1683 mmc_card_name(card), part_md->part_type, cap_str);
1684 return 0;
1685}
1686
Namjae Jeone0c368d2011-10-06 23:41:38 +09001687/* MMC Physical partitions consist of two boot partitions and
1688 * up to four general purpose partitions.
1689 * For each partition enabled in EXT_CSD a block device will be allocatedi
1690 * to provide access to the partition.
1691 */
1692
Andrei Warkentin371a6892011-04-11 18:10:25 -05001693static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1694{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001695 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001696
1697 if (!mmc_card_mmc(card))
1698 return 0;
1699
Namjae Jeone0c368d2011-10-06 23:41:38 +09001700 for (idx = 0; idx < card->nr_parts; idx++) {
1701 if (card->part[idx].size) {
1702 ret = mmc_blk_alloc_part(card, md,
1703 card->part[idx].part_cfg,
1704 card->part[idx].size >> 9,
1705 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001706 card->part[idx].name,
1707 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09001708 if (ret)
1709 return ret;
1710 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001711 }
1712
1713 return ret;
1714}
1715
Andrei Warkentin371a6892011-04-11 18:10:25 -05001716static void mmc_blk_remove_req(struct mmc_blk_data *md)
1717{
Johan Rudholmadd710e2011-12-02 08:51:06 +01001718 struct mmc_card *card;
1719
Andrei Warkentin371a6892011-04-11 18:10:25 -05001720 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01001721 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001722 if (md->disk->flags & GENHD_FL_UP) {
1723 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001724 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1725 card->ext_csd.boot_ro_lockable)
1726 device_remove_file(disk_to_dev(md->disk),
1727 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001728
1729 /* Stop new requests from getting into the queue */
1730 del_gendisk(md->disk);
1731 }
1732
1733 /* Then flush out any already in there */
1734 mmc_cleanup_queue(&md->queue);
1735 mmc_blk_put(md);
1736 }
1737}
1738
1739static void mmc_blk_remove_parts(struct mmc_card *card,
1740 struct mmc_blk_data *md)
1741{
1742 struct list_head *pos, *q;
1743 struct mmc_blk_data *part_md;
1744
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001745 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001746 list_for_each_safe(pos, q, &md->part) {
1747 part_md = list_entry(pos, struct mmc_blk_data, part);
1748 list_del(pos);
1749 mmc_blk_remove_req(part_md);
1750 }
1751}
1752
1753static int mmc_add_disk(struct mmc_blk_data *md)
1754{
1755 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001756 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001757
1758 add_disk(md->disk);
1759 md->force_ro.show = force_ro_show;
1760 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301761 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001762 md->force_ro.attr.name = "force_ro";
1763 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1764 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1765 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01001766 goto force_ro_fail;
1767
1768 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1769 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04001770 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001771
1772 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1773 mode = S_IRUGO;
1774 else
1775 mode = S_IRUGO | S_IWUSR;
1776
1777 md->power_ro_lock.show = power_ro_lock_show;
1778 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01001779 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001780 md->power_ro_lock.attr.mode = mode;
1781 md->power_ro_lock.attr.name =
1782 "ro_lock_until_next_power_on";
1783 ret = device_create_file(disk_to_dev(md->disk),
1784 &md->power_ro_lock);
1785 if (ret)
1786 goto power_ro_lock_fail;
1787 }
1788 return ret;
1789
1790power_ro_lock_fail:
1791 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1792force_ro_fail:
1793 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001794
1795 return ret;
1796}
1797
Chris Ballc59d4472011-11-11 22:01:43 -05001798#define CID_MANFID_SANDISK 0x2
1799#define CID_MANFID_TOSHIBA 0x11
1800#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09001801#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05001802
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001803static const struct mmc_fixup blk_fixups[] =
1804{
Chris Ballc59d4472011-11-11 22:01:43 -05001805 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1806 MMC_QUIRK_INAND_CMD38),
1807 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1808 MMC_QUIRK_INAND_CMD38),
1809 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1810 MMC_QUIRK_INAND_CMD38),
1811 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1812 MMC_QUIRK_INAND_CMD38),
1813 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1814 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001815
1816 /*
1817 * Some MMC cards experience performance degradation with CMD23
1818 * instead of CMD12-bounded multiblock transfers. For now we'll
1819 * black list what's bad...
1820 * - Certain Toshiba cards.
1821 *
1822 * N.B. This doesn't affect SD cards.
1823 */
Chris Ballc59d4472011-11-11 22:01:43 -05001824 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001825 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001826 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001827 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001828 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001829 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001830
1831 /*
1832 * Some Micron MMC cards needs longer data read timeout than
1833 * indicated in CSD.
1834 */
Chris Ballc59d4472011-11-11 22:01:43 -05001835 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001836 MMC_QUIRK_LONG_READ_TIME),
1837
Ian Chen3550ccd2012-08-29 15:05:36 +09001838 /*
1839 * On these Samsung MoviNAND parts, performing secure erase or
1840 * secure trim can result in unrecoverable corruption due to a
1841 * firmware bug.
1842 */
1843 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1844 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1845 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1846 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1847 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1848 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1849 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1850 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1851 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1852 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1853 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1854 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1855 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1856 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1857 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1858 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1859
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001860 END_FIXUP
1861};
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863static int mmc_blk_probe(struct mmc_card *card)
1864{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001865 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001866 char cap_str[10];
1867
Pierre Ossman912490d2005-05-21 10:27:02 +01001868 /*
1869 * Check that the card supports the command class(es) we need.
1870 */
1871 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return -ENODEV;
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 md = mmc_blk_alloc(card);
1875 if (IS_ERR(md))
1876 return PTR_ERR(md);
1877
Yi Li444122f2009-02-05 15:31:57 +08001878 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001879 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301880 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001882 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Andrei Warkentin371a6892011-04-11 18:10:25 -05001884 if (mmc_blk_alloc_parts(card, md))
1885 goto out;
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001888 mmc_fixup_device(card, blk_fixups);
1889
Andrei Warkentin371a6892011-04-11 18:10:25 -05001890 if (mmc_add_disk(md))
1891 goto out;
1892
1893 list_for_each_entry(part_md, &md->part, part) {
1894 if (mmc_add_disk(part_md))
1895 goto out;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 0;
1898
1899 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001900 mmc_blk_remove_parts(card, md);
1901 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905static void mmc_blk_remove(struct mmc_card *card)
1906{
1907 struct mmc_blk_data *md = mmc_get_drvdata(card);
1908
Andrei Warkentin371a6892011-04-11 18:10:25 -05001909 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001910 mmc_claim_host(card->host);
1911 mmc_blk_part_switch(card, md);
1912 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001913 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 mmc_set_drvdata(card, NULL);
1915}
1916
1917#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08001918static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001920 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct mmc_blk_data *md = mmc_get_drvdata(card);
1922
1923 if (md) {
1924 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001925 list_for_each_entry(part_md, &md->part, part) {
1926 mmc_queue_suspend(&part_md->queue);
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 return 0;
1930}
1931
1932static int mmc_blk_resume(struct mmc_card *card)
1933{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001934 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct mmc_blk_data *md = mmc_get_drvdata(card);
1936
1937 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05001938 /*
1939 * Resume involves the card going into idle state,
1940 * so current partition is always the main one.
1941 */
1942 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001944 list_for_each_entry(part_md, &md->part, part) {
1945 mmc_queue_resume(&part_md->queue);
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 return 0;
1949}
1950#else
1951#define mmc_blk_suspend NULL
1952#define mmc_blk_resume NULL
1953#endif
1954
1955static struct mmc_driver mmc_driver = {
1956 .drv = {
1957 .name = "mmcblk",
1958 },
1959 .probe = mmc_blk_probe,
1960 .remove = mmc_blk_remove,
1961 .suspend = mmc_blk_suspend,
1962 .resume = mmc_blk_resume,
1963};
1964
1965static int __init mmc_blk_init(void)
1966{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001967 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001969 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1970 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1971
1972 max_devices = 256 / perdev_minors;
1973
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001974 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1975 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001978 res = mmc_register_driver(&mmc_driver);
1979 if (res)
1980 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001982 return 0;
1983 out2:
1984 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 out:
1986 return res;
1987}
1988
1989static void __exit mmc_blk_exit(void)
1990{
1991 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001992 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995module_init(mmc_blk_init);
1996module_exit(mmc_blk_exit);
1997
1998MODULE_LICENSE("GPL");
1999MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2000