blob: c32a9093159a3dd89daf1721d5f3a8b4f3c1e7b2 [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
60
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020061static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040062
63/*
64 * The defaults come from config options but can be overriden by module
65 * or bootarg options.
66 */
67static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
68
69/*
70 * We've only got one major, so number of mmcblk devices is
71 * limited to 256 / number of minors per device.
72 */
73static int max_devices;
74
75/* 256 minors, so at most 256 separate devices */
76static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050077static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * There is one mmc_blk_data per slot.
81 */
82struct mmc_blk_data {
83 spinlock_t lock;
84 struct gendisk *disk;
85 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050086 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050088 unsigned int flags;
89#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
90#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000093 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050094 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -050095 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +030096 unsigned int reset_done;
97#define MMC_BLK_READ BIT(0)
98#define MMC_BLK_WRITE BIT(1)
99#define MMC_BLK_DISCARD BIT(2)
100#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500101
102 /*
103 * Only set in main mmc_blk_data associated
104 * with mmc_card with mmc_set_drvdata, and keeps
105 * track of the current selected device partition.
106 */
107 unsigned int part_curr;
108 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100109 struct device_attribute power_ro_lock;
110 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Arjan van de Vena621aae2006-01-12 18:43:35 +0000113static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Per Forlind78d4a82011-07-01 18:55:30 +0200115enum mmc_blk_status {
116 MMC_BLK_SUCCESS = 0,
117 MMC_BLK_PARTIAL,
Per Forlind78d4a82011-07-01 18:55:30 +0200118 MMC_BLK_CMD_ERR,
Adrian Hunter67716322011-08-29 16:42:15 +0300119 MMC_BLK_RETRY,
Per Forlind78d4a82011-07-01 18:55:30 +0200120 MMC_BLK_ABORT,
Adrian Hunter67716322011-08-29 16:42:15 +0300121 MMC_BLK_DATA_ERR,
122 MMC_BLK_ECC_ERR,
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530123 MMC_BLK_NOMEDIUM,
Per Forlind78d4a82011-07-01 18:55:30 +0200124};
125
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400126module_param(perdev_minors, int, 0444);
127MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
130{
131 struct mmc_blk_data *md;
132
Arjan van de Vena621aae2006-01-12 18:43:35 +0000133 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 md = disk->private_data;
135 if (md && md->usage == 0)
136 md = NULL;
137 if (md)
138 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000139 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return md;
142}
143
Andrei Warkentin371a6892011-04-11 18:10:25 -0500144static inline int mmc_get_devidx(struct gendisk *disk)
145{
146 int devmaj = MAJOR(disk_devt(disk));
147 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
148
149 if (!devmaj)
150 devidx = disk->first_minor / perdev_minors;
151 return devidx;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static void mmc_blk_put(struct mmc_blk_data *md)
155{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000156 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 md->usage--;
158 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500159 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800160 blk_cleanup_queue(md->queue.queue);
161
David Woodhouse1dff3142007-11-21 18:45:12 +0100162 __clear_bit(devidx, dev_use);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 kfree(md);
166 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000167 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Johan Rudholmadd710e2011-12-02 08:51:06 +0100170static ssize_t power_ro_lock_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
173 int ret;
174 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
175 struct mmc_card *card = md->queue.card;
176 int locked = 0;
177
178 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
179 locked = 2;
180 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
181 locked = 1;
182
183 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
184
185 return ret;
186}
187
188static ssize_t power_ro_lock_store(struct device *dev,
189 struct device_attribute *attr, const char *buf, size_t count)
190{
191 int ret;
192 struct mmc_blk_data *md, *part_md;
193 struct mmc_card *card;
194 unsigned long set;
195
196 if (kstrtoul(buf, 0, &set))
197 return -EINVAL;
198
199 if (set != 1)
200 return count;
201
202 md = mmc_blk_get(dev_to_disk(dev));
203 card = md->queue.card;
204
205 mmc_claim_host(card->host);
206
207 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
208 card->ext_csd.boot_ro_lock |
209 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
210 card->ext_csd.part_time);
211 if (ret)
212 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
213 else
214 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
215
216 mmc_release_host(card->host);
217
218 if (!ret) {
219 pr_info("%s: Locking boot partition ro until next power on\n",
220 md->disk->disk_name);
221 set_disk_ro(md->disk, 1);
222
223 list_for_each_entry(part_md, &md->part, part)
224 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
225 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
226 set_disk_ro(part_md->disk, 1);
227 }
228 }
229
230 mmc_blk_put(md);
231 return count;
232}
233
Andrei Warkentin371a6892011-04-11 18:10:25 -0500234static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
235 char *buf)
236{
237 int ret;
238 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
239
240 ret = snprintf(buf, PAGE_SIZE, "%d",
241 get_disk_ro(dev_to_disk(dev)) ^
242 md->read_only);
243 mmc_blk_put(md);
244 return ret;
245}
246
247static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t count)
249{
250 int ret;
251 char *end;
252 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
253 unsigned long set = simple_strtoul(buf, &end, 0);
254 if (end == buf) {
255 ret = -EINVAL;
256 goto out;
257 }
258
259 set_disk_ro(dev_to_disk(dev), set || md->read_only);
260 ret = count;
261out:
262 mmc_blk_put(md);
263 return ret;
264}
265
Al Viroa5a15612008-03-02 10:33:30 -0500266static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Al Viroa5a15612008-03-02 10:33:30 -0500268 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int ret = -ENXIO;
270
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200271 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (md) {
273 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500274 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700276
Al Viroa5a15612008-03-02 10:33:30 -0500277 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700278 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700279 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200282 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 return ret;
285}
286
Al Viroa5a15612008-03-02 10:33:30 -0500287static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Al Viroa5a15612008-03-02 10:33:30 -0500289 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200291 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200293 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
296
297static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800298mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800300 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
301 geo->heads = 4;
302 geo->sectors = 16;
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
John Calixtocb87ea22011-04-26 18:56:29 -0400306struct mmc_blk_ioc_data {
307 struct mmc_ioc_cmd ic;
308 unsigned char *buf;
309 u64 buf_bytes;
310};
311
312static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
313 struct mmc_ioc_cmd __user *user)
314{
315 struct mmc_blk_ioc_data *idata;
316 int err;
317
318 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
319 if (!idata) {
320 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400321 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400322 }
323
324 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
325 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400326 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400327 }
328
329 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
330 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
331 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400332 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400333 }
334
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100335 if (!idata->buf_bytes)
336 return idata;
337
John Calixtocb87ea22011-04-26 18:56:29 -0400338 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
339 if (!idata->buf) {
340 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400341 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400342 }
343
344 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
345 idata->ic.data_ptr, idata->buf_bytes)) {
346 err = -EFAULT;
347 goto copy_err;
348 }
349
350 return idata;
351
352copy_err:
353 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400355 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400356out:
John Calixtocb87ea22011-04-26 18:56:29 -0400357 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400358}
359
360static int mmc_blk_ioctl_cmd(struct block_device *bdev,
361 struct mmc_ioc_cmd __user *ic_ptr)
362{
363 struct mmc_blk_ioc_data *idata;
364 struct mmc_blk_data *md;
365 struct mmc_card *card;
366 struct mmc_command cmd = {0};
367 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530368 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400369 struct scatterlist sg;
370 int err;
371
372 /*
373 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
374 * whole block device, not on a partition. This prevents overspray
375 * between sibling partitions.
376 */
377 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
378 return -EPERM;
379
380 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
381 if (IS_ERR(idata))
382 return PTR_ERR(idata);
383
John Calixtocb87ea22011-04-26 18:56:29 -0400384 md = mmc_blk_get(bdev->bd_disk);
385 if (!md) {
386 err = -EINVAL;
387 goto cmd_done;
388 }
389
390 card = md->queue.card;
391 if (IS_ERR(card)) {
392 err = PTR_ERR(card);
393 goto cmd_done;
394 }
395
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100396 cmd.opcode = idata->ic.opcode;
397 cmd.arg = idata->ic.arg;
398 cmd.flags = idata->ic.flags;
399
400 if (idata->buf_bytes) {
401 data.sg = &sg;
402 data.sg_len = 1;
403 data.blksz = idata->ic.blksz;
404 data.blocks = idata->ic.blocks;
405
406 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
407
408 if (idata->ic.write_flag)
409 data.flags = MMC_DATA_WRITE;
410 else
411 data.flags = MMC_DATA_READ;
412
413 /* data.flags must already be set before doing this. */
414 mmc_set_data_timeout(&data, card);
415
416 /* Allow overriding the timeout_ns for empirical tuning. */
417 if (idata->ic.data_timeout_ns)
418 data.timeout_ns = idata->ic.data_timeout_ns;
419
420 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
421 /*
422 * Pretend this is a data transfer and rely on the
423 * host driver to compute timeout. When all host
424 * drivers support cmd.cmd_timeout for R1B, this
425 * can be changed to:
426 *
427 * mrq.data = NULL;
428 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
429 */
430 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
431 }
432
433 mrq.data = &data;
434 }
435
436 mrq.cmd = &cmd;
437
John Calixtocb87ea22011-04-26 18:56:29 -0400438 mmc_claim_host(card->host);
439
440 if (idata->ic.is_acmd) {
441 err = mmc_app_cmd(card->host, card);
442 if (err)
443 goto cmd_rel_host;
444 }
445
John Calixtocb87ea22011-04-26 18:56:29 -0400446 mmc_wait_for_req(card->host, &mrq);
447
448 if (cmd.error) {
449 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
450 __func__, cmd.error);
451 err = cmd.error;
452 goto cmd_rel_host;
453 }
454 if (data.error) {
455 dev_err(mmc_dev(card->host), "%s: data error %d\n",
456 __func__, data.error);
457 err = data.error;
458 goto cmd_rel_host;
459 }
460
461 /*
462 * According to the SD specs, some commands require a delay after
463 * issuing the command.
464 */
465 if (idata->ic.postsleep_min_us)
466 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
467
468 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
469 err = -EFAULT;
470 goto cmd_rel_host;
471 }
472
473 if (!idata->ic.write_flag) {
474 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
475 idata->buf, idata->buf_bytes)) {
476 err = -EFAULT;
477 goto cmd_rel_host;
478 }
479 }
480
481cmd_rel_host:
482 mmc_release_host(card->host);
483
484cmd_done:
485 mmc_blk_put(md);
486 kfree(idata->buf);
487 kfree(idata);
488 return err;
489}
490
491static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
492 unsigned int cmd, unsigned long arg)
493{
494 int ret = -EINVAL;
495 if (cmd == MMC_IOC_CMD)
496 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
497 return ret;
498}
499
500#ifdef CONFIG_COMPAT
501static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
502 unsigned int cmd, unsigned long arg)
503{
504 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
505}
506#endif
507
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700508static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500509 .open = mmc_blk_open,
510 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800511 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400513 .ioctl = mmc_blk_ioctl,
514#ifdef CONFIG_COMPAT
515 .compat_ioctl = mmc_blk_compat_ioctl,
516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517};
518
Andrei Warkentin371a6892011-04-11 18:10:25 -0500519static inline int mmc_blk_part_switch(struct mmc_card *card,
520 struct mmc_blk_data *md)
521{
522 int ret;
523 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300524
Andrei Warkentin371a6892011-04-11 18:10:25 -0500525 if (main_md->part_curr == md->part_type)
526 return 0;
527
528 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300529 u8 part_config = card->ext_csd.part_config;
530
531 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
532 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500533
534 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300535 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500536 card->ext_csd.part_time);
537 if (ret)
538 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300539
540 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300541 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500542
543 main_md->part_curr = md->part_type;
544 return 0;
545}
546
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700547static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
548{
549 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100550 u32 result;
551 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700552
Venkatraman Sad5fd972011-08-25 00:30:50 +0530553 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400554 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400555 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700556 unsigned int timeout_us;
557
558 struct scatterlist sg;
559
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700560 cmd.opcode = MMC_APP_CMD;
561 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700562 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700563
564 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700565 if (err)
566 return (u32)-1;
567 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700568 return (u32)-1;
569
570 memset(&cmd, 0, sizeof(struct mmc_command));
571
572 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
573 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700574 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700575
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700576 data.timeout_ns = card->csd.tacc_ns * 100;
577 data.timeout_clks = card->csd.tacc_clks * 100;
578
579 timeout_us = data.timeout_ns / 1000;
580 timeout_us += data.timeout_clks * 1000 /
581 (card->host->ios.clock / 1000);
582
583 if (timeout_us > 100000) {
584 data.timeout_ns = 100000000;
585 data.timeout_clks = 0;
586 }
587
588 data.blksz = 4;
589 data.blocks = 1;
590 data.flags = MMC_DATA_READ;
591 data.sg = &sg;
592 data.sg_len = 1;
593
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700594 mrq.cmd = &cmd;
595 mrq.data = &data;
596
Ben Dooks051913d2009-06-08 23:33:57 +0100597 blocks = kmalloc(4, GFP_KERNEL);
598 if (!blocks)
599 return (u32)-1;
600
601 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700602
603 mmc_wait_for_req(card->host, &mrq);
604
Ben Dooks051913d2009-06-08 23:33:57 +0100605 result = ntohl(*blocks);
606 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700607
Ben Dooks051913d2009-06-08 23:33:57 +0100608 if (cmd.error || data.error)
609 result = (u32)-1;
610
611 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700612}
613
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100614static int send_stop(struct mmc_card *card, u32 *status)
615{
616 struct mmc_command cmd = {0};
617 int err;
618
619 cmd.opcode = MMC_STOP_TRANSMISSION;
620 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
621 err = mmc_wait_for_cmd(card->host, &cmd, 5);
622 if (err == 0)
623 *status = cmd.resp[0];
624 return err;
625}
626
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100627static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300628{
Chris Ball1278dba2011-04-13 23:40:30 -0400629 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300630 int err;
631
Adrian Hunter504f1912008-10-16 12:55:25 +0300632 cmd.opcode = MMC_SEND_STATUS;
633 if (!mmc_host_is_spi(card->host))
634 cmd.arg = card->rca << 16;
635 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100636 err = mmc_wait_for_cmd(card->host, &cmd, retries);
637 if (err == 0)
638 *status = cmd.resp[0];
639 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300640}
641
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530642#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100643#define ERR_RETRY 2
644#define ERR_ABORT 1
645#define ERR_CONTINUE 0
646
647static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
648 bool status_valid, u32 status)
649{
650 switch (error) {
651 case -EILSEQ:
652 /* response crc error, retry the r/w cmd */
653 pr_err("%s: %s sending %s command, card status %#x\n",
654 req->rq_disk->disk_name, "response CRC error",
655 name, status);
656 return ERR_RETRY;
657
658 case -ETIMEDOUT:
659 pr_err("%s: %s sending %s command, card status %#x\n",
660 req->rq_disk->disk_name, "timed out", name, status);
661
662 /* If the status cmd initially failed, retry the r/w cmd */
663 if (!status_valid)
664 return ERR_RETRY;
665
666 /*
667 * If it was a r/w cmd crc error, or illegal command
668 * (eg, issued in wrong state) then retry - we should
669 * have corrected the state problem above.
670 */
671 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
672 return ERR_RETRY;
673
674 /* Otherwise abort the command */
675 return ERR_ABORT;
676
677 default:
678 /* We don't understand the error code the driver gave us */
679 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
680 req->rq_disk->disk_name, error, status);
681 return ERR_ABORT;
682 }
683}
684
685/*
686 * Initial r/w and stop cmd error recovery.
687 * We don't know whether the card received the r/w cmd or not, so try to
688 * restore things back to a sane state. Essentially, we do this as follows:
689 * - Obtain card status. If the first attempt to obtain card status fails,
690 * the status word will reflect the failed status cmd, not the failed
691 * r/w cmd. If we fail to obtain card status, it suggests we can no
692 * longer communicate with the card.
693 * - Check the card state. If the card received the cmd but there was a
694 * transient problem with the response, it might still be in a data transfer
695 * mode. Try to send it a stop command. If this fails, we can't recover.
696 * - If the r/w cmd failed due to a response CRC error, it was probably
697 * transient, so retry the cmd.
698 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
699 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
700 * illegal cmd, retry.
701 * Otherwise we don't understand what happened, so abort.
702 */
703static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +0900704 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100705{
706 bool prev_cmd_status_valid = true;
707 u32 status, stop_status = 0;
708 int err, retry;
709
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530710 if (mmc_card_removed(card))
711 return ERR_NOMEDIUM;
712
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100713 /*
714 * Try to get card status which indicates both the card state
715 * and why there was no response. If the first attempt fails,
716 * we can't be sure the returned status is for the r/w command.
717 */
718 for (retry = 2; retry >= 0; retry--) {
719 err = get_card_status(card, &status, 0);
720 if (!err)
721 break;
722
723 prev_cmd_status_valid = false;
724 pr_err("%s: error %d sending status command, %sing\n",
725 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
726 }
727
728 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530729 if (err) {
730 /* Check if the card is removed */
731 if (mmc_detect_card_removed(card->host))
732 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100733 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530734 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100735
Adrian Hunter67716322011-08-29 16:42:15 +0300736 /* Flag ECC errors */
737 if ((status & R1_CARD_ECC_FAILED) ||
738 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
739 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
740 *ecc_err = 1;
741
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +0900742 /* Flag General errors */
743 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
744 if ((status & R1_ERROR) ||
745 (brq->stop.resp[0] & R1_ERROR)) {
746 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
747 req->rq_disk->disk_name, __func__,
748 brq->stop.resp[0], status);
749 *gen_err = 1;
750 }
751
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100752 /*
753 * Check the current card state. If it is in some data transfer
754 * mode, tell it to stop (and hopefully transition back to TRAN.)
755 */
756 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
757 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
758 err = send_stop(card, &stop_status);
759 if (err)
760 pr_err("%s: error %d sending stop command\n",
761 req->rq_disk->disk_name, err);
762
763 /*
764 * If the stop cmd also timed out, the card is probably
765 * not present, so abort. Other errors are bad news too.
766 */
767 if (err)
768 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300769 if (stop_status & R1_CARD_ECC_FAILED)
770 *ecc_err = 1;
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +0900771 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
772 if (stop_status & R1_ERROR) {
773 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
774 req->rq_disk->disk_name, __func__,
775 stop_status);
776 *gen_err = 1;
777 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100778 }
779
780 /* Check for set block count errors */
781 if (brq->sbc.error)
782 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
783 prev_cmd_status_valid, status);
784
785 /* Check for r/w command errors */
786 if (brq->cmd.error)
787 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
788 prev_cmd_status_valid, status);
789
Adrian Hunter67716322011-08-29 16:42:15 +0300790 /* Data errors */
791 if (!brq->stop.error)
792 return ERR_CONTINUE;
793
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100794 /* Now for stop errors. These aren't fatal to the transfer. */
795 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
796 req->rq_disk->disk_name, brq->stop.error,
797 brq->cmd.resp[0], status);
798
799 /*
800 * Subsitute in our own stop status as this will give the error
801 * state which happened during the execution of the r/w command.
802 */
803 if (stop_status) {
804 brq->stop.resp[0] = stop_status;
805 brq->stop.error = 0;
806 }
807 return ERR_CONTINUE;
808}
809
Adrian Hunter67716322011-08-29 16:42:15 +0300810static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
811 int type)
812{
813 int err;
814
815 if (md->reset_done & type)
816 return -EEXIST;
817
818 md->reset_done |= type;
819 err = mmc_hw_reset(host);
820 /* Ensure we switch back to the correct partition */
821 if (err != -EOPNOTSUPP) {
822 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
823 int part_err;
824
825 main_md->part_curr = main_md->part_type;
826 part_err = mmc_blk_part_switch(host->card, md);
827 if (part_err) {
828 /*
829 * We have failed to get back into the correct
830 * partition, so we need to abort the whole request.
831 */
832 return -ENODEV;
833 }
834 }
835 return err;
836}
837
838static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
839{
840 md->reset_done &= ~type;
841}
842
Adrian Hunterbd788c92010-08-11 14:17:47 -0700843static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
844{
845 struct mmc_blk_data *md = mq->data;
846 struct mmc_card *card = md->queue.card;
847 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300848 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700849
Adrian Hunterbd788c92010-08-11 14:17:47 -0700850 if (!mmc_can_erase(card)) {
851 err = -EOPNOTSUPP;
852 goto out;
853 }
854
855 from = blk_rq_pos(req);
856 nr = blk_rq_sectors(req);
857
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900858 if (mmc_can_discard(card))
859 arg = MMC_DISCARD_ARG;
860 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700861 arg = MMC_TRIM_ARG;
862 else
863 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300864retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500865 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
866 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
867 INAND_CMD38_ARG_EXT_CSD,
868 arg == MMC_TRIM_ARG ?
869 INAND_CMD38_ARG_TRIM :
870 INAND_CMD38_ARG_ERASE,
871 0);
872 if (err)
873 goto out;
874 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700875 err = mmc_erase(card, from, nr, arg);
876out:
Adrian Hunter67716322011-08-29 16:42:15 +0300877 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
878 goto retry;
879 if (!err)
880 mmc_blk_reset_success(md, type);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700881 spin_lock_irq(&md->lock);
882 __blk_end_request(req, err, blk_rq_bytes(req));
883 spin_unlock_irq(&md->lock);
884
Adrian Hunterbd788c92010-08-11 14:17:47 -0700885 return err ? 0 : 1;
886}
887
Adrian Hunter49804542010-08-11 14:17:50 -0700888static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
889 struct request *req)
890{
891 struct mmc_blk_data *md = mq->data;
892 struct mmc_card *card = md->queue.card;
Adrian Hunter28302812012-04-05 14:45:48 +0300893 unsigned int from, nr, arg, trim_arg, erase_arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300894 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700895
Kyungmin Parkd9ddd622011-10-14 14:15:48 +0900896 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700897 err = -EOPNOTSUPP;
898 goto out;
899 }
900
901 from = blk_rq_pos(req);
902 nr = blk_rq_sectors(req);
903
Adrian Hunter28302812012-04-05 14:45:48 +0300904 /* The sanitize operation is supported at v4.5 only */
905 if (mmc_can_sanitize(card)) {
906 erase_arg = MMC_ERASE_ARG;
907 trim_arg = MMC_TRIM_ARG;
908 } else {
909 erase_arg = MMC_SECURE_ERASE_ARG;
910 trim_arg = MMC_SECURE_TRIM1_ARG;
911 }
912
913 if (mmc_erase_group_aligned(card, from, nr))
914 arg = erase_arg;
915 else if (mmc_can_trim(card))
916 arg = trim_arg;
917 else {
918 err = -EINVAL;
919 goto out;
920 }
Adrian Hunter67716322011-08-29 16:42:15 +0300921retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500922 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
923 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
924 INAND_CMD38_ARG_EXT_CSD,
925 arg == MMC_SECURE_TRIM1_ARG ?
926 INAND_CMD38_ARG_SECTRIM1 :
927 INAND_CMD38_ARG_SECERASE,
928 0);
929 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300930 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500931 }
Adrian Hunter28302812012-04-05 14:45:48 +0300932
Adrian Hunter49804542010-08-11 14:17:50 -0700933 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300934 if (err == -EIO)
935 goto out_retry;
936 if (err)
937 goto out;
938
939 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500940 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
941 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
942 INAND_CMD38_ARG_EXT_CSD,
943 INAND_CMD38_ARG_SECTRIM2,
944 0);
945 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300946 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500947 }
Adrian Hunter28302812012-04-05 14:45:48 +0300948
Adrian Hunter49804542010-08-11 14:17:50 -0700949 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300950 if (err == -EIO)
951 goto out_retry;
952 if (err)
953 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500954 }
Adrian Hunter28302812012-04-05 14:45:48 +0300955
956 if (mmc_can_sanitize(card))
957 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
958 EXT_CSD_SANITIZE_START, 1, 0);
959out_retry:
960 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300961 goto retry;
962 if (!err)
963 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300964out:
Adrian Hunter49804542010-08-11 14:17:50 -0700965 spin_lock_irq(&md->lock);
966 __blk_end_request(req, err, blk_rq_bytes(req));
967 spin_unlock_irq(&md->lock);
968
Adrian Hunter49804542010-08-11 14:17:50 -0700969 return err ? 0 : 1;
970}
971
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500972static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
973{
974 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900975 struct mmc_card *card = md->queue.card;
976 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500977
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900978 ret = mmc_flush_cache(card);
979 if (ret)
980 ret = -EIO;
981
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500982 spin_lock_irq(&md->lock);
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900983 __blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500984 spin_unlock_irq(&md->lock);
985
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900986 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500987}
988
989/*
990 * Reformat current write as a reliable write, supporting
991 * both legacy and the enhanced reliable write MMC cards.
992 * In each transfer we'll handle only as much as a single
993 * reliable write can handle, thus finish the request in
994 * partial completions.
995 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500996static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
997 struct mmc_card *card,
998 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500999{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001000 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1001 /* Legacy mode imposes restrictions on transfers. */
1002 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1003 brq->data.blocks = 1;
1004
1005 if (brq->data.blocks > card->ext_csd.rel_sectors)
1006 brq->data.blocks = card->ext_csd.rel_sectors;
1007 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1008 brq->data.blocks = 1;
1009 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001010}
1011
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001012#define CMD_ERRORS \
1013 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1014 R1_ADDRESS_ERROR | /* Misaligned address */ \
1015 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1016 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1017 R1_CC_ERROR | /* Card controller error */ \
1018 R1_ERROR) /* General/unknown error */
1019
Per Forlinee8a43a2011-07-01 18:55:33 +02001020static int mmc_blk_err_check(struct mmc_card *card,
1021 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001022{
Per Forlinee8a43a2011-07-01 18:55:33 +02001023 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1024 mmc_active);
1025 struct mmc_blk_request *brq = &mq_mrq->brq;
1026 struct request *req = mq_mrq->req;
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +09001027 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001028
1029 /*
1030 * sbc.error indicates a problem with the set block count
1031 * command. No data will have been transferred.
1032 *
1033 * cmd.error indicates a problem with the r/w command. No
1034 * data will have been transferred.
1035 *
1036 * stop.error indicates a problem with the stop command. Data
1037 * may have been transferred, or may still be transferring.
1038 */
Adrian Hunter67716322011-08-29 16:42:15 +03001039 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1040 brq->data.error) {
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +09001041 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001042 case ERR_RETRY:
1043 return MMC_BLK_RETRY;
1044 case ERR_ABORT:
1045 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301046 case ERR_NOMEDIUM:
1047 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001048 case ERR_CONTINUE:
1049 break;
1050 }
1051 }
1052
1053 /*
1054 * Check for errors relating to the execution of the
1055 * initial command - such as address errors. No data
1056 * has been transferred.
1057 */
1058 if (brq->cmd.resp[0] & CMD_ERRORS) {
1059 pr_err("%s: r/w command failed, status = %#x\n",
1060 req->rq_disk->disk_name, brq->cmd.resp[0]);
1061 return MMC_BLK_ABORT;
1062 }
1063
1064 /*
1065 * Everything else is either success, or a data error of some
1066 * kind. If it was a write, we may have transitioned to
1067 * program mode, which we have to wait for it to complete.
1068 */
1069 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1070 u32 status;
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +09001071
1072 /* Check stop command response */
1073 if (brq->stop.resp[0] & R1_ERROR) {
1074 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1075 req->rq_disk->disk_name, __func__,
1076 brq->stop.resp[0]);
1077 gen_err = 1;
1078 }
1079
Per Forlind78d4a82011-07-01 18:55:30 +02001080 do {
1081 int err = get_card_status(card, &status, 5);
1082 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301083 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001084 req->rq_disk->disk_name, err);
1085 return MMC_BLK_CMD_ERR;
1086 }
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +09001087
1088 if (status & R1_ERROR) {
1089 pr_err("%s: %s: general error sending status command, card status %#x\n",
1090 req->rq_disk->disk_name, __func__,
1091 status);
1092 gen_err = 1;
1093 }
1094
Per Forlind78d4a82011-07-01 18:55:30 +02001095 /*
1096 * Some cards mishandle the status bits,
1097 * so make sure to check both the busy
1098 * indication and the card state.
1099 */
1100 } while (!(status & R1_READY_FOR_DATA) ||
1101 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1102 }
1103
KOBAYASHI Yoshitake98048d92013-07-07 07:35:45 +09001104 /* if general error occurs, retry the write operation. */
1105 if (gen_err) {
1106 pr_warning("%s: retrying write for general error\n",
1107 req->rq_disk->disk_name);
1108 return MMC_BLK_RETRY;
1109 }
1110
Per Forlind78d4a82011-07-01 18:55:30 +02001111 if (brq->data.error) {
1112 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1113 req->rq_disk->disk_name, brq->data.error,
1114 (unsigned)blk_rq_pos(req),
1115 (unsigned)blk_rq_sectors(req),
1116 brq->cmd.resp[0], brq->stop.resp[0]);
1117
1118 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001119 if (ecc_err)
1120 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001121 return MMC_BLK_DATA_ERR;
1122 } else {
1123 return MMC_BLK_CMD_ERR;
1124 }
1125 }
1126
Adrian Hunter67716322011-08-29 16:42:15 +03001127 if (!brq->data.bytes_xfered)
1128 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001129
Adrian Hunter67716322011-08-29 16:42:15 +03001130 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1131 return MMC_BLK_PARTIAL;
1132
1133 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001134}
1135
Per Forlin54d49d772011-07-01 18:55:29 +02001136static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1137 struct mmc_card *card,
1138 int disable_multi,
1139 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Per Forlin54d49d772011-07-01 18:55:29 +02001141 u32 readcmd, writecmd;
1142 struct mmc_blk_request *brq = &mqrq->brq;
1143 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301145 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001147 /*
1148 * Reliable writes are used to implement Forced Unit Access and
1149 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001150 *
1151 * XXX: this really needs a good explanation of why REQ_META
1152 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001153 */
1154 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1155 (req->cmd_flags & REQ_META)) &&
1156 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001157 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001158
Per Forlin54d49d772011-07-01 18:55:29 +02001159 memset(brq, 0, sizeof(struct mmc_blk_request));
1160 brq->mrq.cmd = &brq->cmd;
1161 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Per Forlin54d49d772011-07-01 18:55:29 +02001163 brq->cmd.arg = blk_rq_pos(req);
1164 if (!mmc_card_blockaddr(card))
1165 brq->cmd.arg <<= 9;
1166 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1167 brq->data.blksz = 512;
1168 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1169 brq->stop.arg = 0;
1170 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1171 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Per Forlin54d49d772011-07-01 18:55:29 +02001173 /*
1174 * The block layer doesn't support all sector count
1175 * restrictions, so we need to be prepared for too big
1176 * requests.
1177 */
1178 if (brq->data.blocks > card->host->max_blk_count)
1179 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001181 if (brq->data.blocks > 1) {
1182 /*
1183 * After a read error, we redo the request one sector
1184 * at a time in order to accurately determine which
1185 * sectors can be read successfully.
1186 */
1187 if (disable_multi)
1188 brq->data.blocks = 1;
1189
1190 /* Some controllers can't do multiblock reads due to hw bugs */
1191 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1192 rq_data_dir(req) == READ)
1193 brq->data.blocks = 1;
1194 }
Per Forlin54d49d772011-07-01 18:55:29 +02001195
1196 if (brq->data.blocks > 1 || do_rel_wr) {
1197 /* SPI multiblock writes terminate using a special
1198 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001199 */
Per Forlin54d49d772011-07-01 18:55:29 +02001200 if (!mmc_host_is_spi(card->host) ||
1201 rq_data_dir(req) == READ)
1202 brq->mrq.stop = &brq->stop;
1203 readcmd = MMC_READ_MULTIPLE_BLOCK;
1204 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1205 } else {
1206 brq->mrq.stop = NULL;
1207 readcmd = MMC_READ_SINGLE_BLOCK;
1208 writecmd = MMC_WRITE_BLOCK;
1209 }
1210 if (rq_data_dir(req) == READ) {
1211 brq->cmd.opcode = readcmd;
1212 brq->data.flags |= MMC_DATA_READ;
1213 } else {
1214 brq->cmd.opcode = writecmd;
1215 brq->data.flags |= MMC_DATA_WRITE;
1216 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001217
Per Forlin54d49d772011-07-01 18:55:29 +02001218 if (do_rel_wr)
1219 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001220
Per Forlin54d49d772011-07-01 18:55:29 +02001221 /*
Saugata Das42659002011-12-21 13:09:17 +05301222 * Data tag is used only during writing meta data to speed
1223 * up write and any subsequent read of this meta data
1224 */
1225 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1226 (req->cmd_flags & REQ_META) &&
1227 (rq_data_dir(req) == WRITE) &&
1228 ((brq->data.blocks * brq->data.blksz) >=
1229 card->ext_csd.data_tag_unit_size);
1230
1231 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001232 * Pre-defined multi-block transfers are preferable to
1233 * open ended-ones (and necessary for reliable writes).
1234 * However, it is not sufficient to just send CMD23,
1235 * and avoid the final CMD12, as on an error condition
1236 * CMD12 (stop) needs to be sent anyway. This, coupled
1237 * with Auto-CMD23 enhancements provided by some
1238 * hosts, means that the complexity of dealing
1239 * with this is best left to the host. If CMD23 is
1240 * supported by card and host, we'll fill sbc in and let
1241 * the host deal with handling it correctly. This means
1242 * that for hosts that don't expose MMC_CAP_CMD23, no
1243 * change of behavior will be observed.
1244 *
1245 * N.B: Some MMC cards experience perf degradation.
1246 * We'll avoid using CMD23-bounded multiblock writes for
1247 * these, while retaining features like reliable writes.
1248 */
Saugata Das42659002011-12-21 13:09:17 +05301249 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1250 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1251 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001252 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1253 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301254 (do_rel_wr ? (1 << 31) : 0) |
1255 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001256 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1257 brq->mrq.sbc = &brq->sbc;
1258 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001259
Per Forlin54d49d772011-07-01 18:55:29 +02001260 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001261
Per Forlin54d49d772011-07-01 18:55:29 +02001262 brq->data.sg = mqrq->sg;
1263 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001264
Per Forlin54d49d772011-07-01 18:55:29 +02001265 /*
1266 * Adjust the sg list so it is the same size as the
1267 * request.
1268 */
1269 if (brq->data.blocks != blk_rq_sectors(req)) {
1270 int i, data_size = brq->data.blocks << 9;
1271 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001272
Per Forlin54d49d772011-07-01 18:55:29 +02001273 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1274 data_size -= sg->length;
1275 if (data_size <= 0) {
1276 sg->length += data_size;
1277 i++;
1278 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001279 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001280 }
Per Forlin54d49d772011-07-01 18:55:29 +02001281 brq->data.sg_len = i;
1282 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001283
Per Forlinee8a43a2011-07-01 18:55:33 +02001284 mqrq->mmc_active.mrq = &brq->mrq;
1285 mqrq->mmc_active.err_check = mmc_blk_err_check;
1286
Per Forlin54d49d772011-07-01 18:55:29 +02001287 mmc_queue_bounce_pre(mqrq);
1288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Adrian Hunter67716322011-08-29 16:42:15 +03001290static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1291 struct mmc_blk_request *brq, struct request *req,
1292 int ret)
1293{
1294 /*
1295 * If this is an SD card and we're writing, we can first
1296 * mark the known good sectors as ok.
1297 *
1298 * If the card is not SD, we can still ok written sectors
1299 * as reported by the controller (which might be less than
1300 * the real number of written sectors, but never more).
1301 */
1302 if (mmc_card_sd(card)) {
1303 u32 blocks;
1304
1305 blocks = mmc_sd_num_wr_blocks(card);
1306 if (blocks != (u32)-1) {
1307 spin_lock_irq(&md->lock);
1308 ret = __blk_end_request(req, 0, blocks << 9);
1309 spin_unlock_irq(&md->lock);
1310 }
1311 } else {
1312 spin_lock_irq(&md->lock);
1313 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
1314 spin_unlock_irq(&md->lock);
1315 }
1316 return ret;
1317}
1318
Per Forlinee8a43a2011-07-01 18:55:33 +02001319static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001320{
1321 struct mmc_blk_data *md = mq->data;
1322 struct mmc_card *card = md->queue.card;
1323 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001324 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001325 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001326 struct mmc_queue_req *mq_rq;
1327 struct request *req;
1328 struct mmc_async_req *areq;
1329
1330 if (!rqc && !mq->mqrq_prev->req)
1331 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001332
1333 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001334 if (rqc) {
1335 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1336 areq = &mq->mqrq_cur->mmc_active;
1337 } else
1338 areq = NULL;
1339 areq = mmc_start_req(card->host, areq, (int *) &status);
1340 if (!areq)
1341 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001342
Per Forlinee8a43a2011-07-01 18:55:33 +02001343 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1344 brq = &mq_rq->brq;
1345 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001346 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001347 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001348
Per Forlind78d4a82011-07-01 18:55:30 +02001349 switch (status) {
1350 case MMC_BLK_SUCCESS:
1351 case MMC_BLK_PARTIAL:
1352 /*
1353 * A block was successfully transferred.
1354 */
Adrian Hunter67716322011-08-29 16:42:15 +03001355 mmc_blk_reset_success(md, type);
Per Forlind78d4a82011-07-01 18:55:30 +02001356 spin_lock_irq(&md->lock);
1357 ret = __blk_end_request(req, 0,
1358 brq->data.bytes_xfered);
1359 spin_unlock_irq(&md->lock);
Adrian Hunter67716322011-08-29 16:42:15 +03001360 /*
1361 * If the blk_end_request function returns non-zero even
1362 * though all data has been transferred and no errors
1363 * were returned by the host controller, it's a bug.
1364 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001365 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301366 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001367 __func__, blk_rq_bytes(req),
1368 brq->data.bytes_xfered);
1369 rqc = NULL;
1370 goto cmd_abort;
1371 }
Per Forlind78d4a82011-07-01 18:55:30 +02001372 break;
1373 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001374 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1375 if (!mmc_blk_reset(md, card->host, type))
1376 break;
1377 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001378 case MMC_BLK_RETRY:
1379 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001380 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001381 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001382 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001383 if (!mmc_blk_reset(md, card->host, type))
1384 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001385 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001386 case MMC_BLK_DATA_ERR: {
1387 int err;
1388
1389 err = mmc_blk_reset(md, card->host, type);
1390 if (!err)
1391 break;
1392 if (err == -ENODEV)
1393 goto cmd_abort;
1394 /* Fall through */
1395 }
1396 case MMC_BLK_ECC_ERR:
1397 if (brq->data.blocks > 1) {
1398 /* Redo read one sector at a time */
1399 pr_warning("%s: retrying using single block read\n",
1400 req->rq_disk->disk_name);
1401 disable_multi = 1;
1402 break;
1403 }
Per Forlind78d4a82011-07-01 18:55:30 +02001404 /*
1405 * After an error, we redo I/O one sector at a
1406 * time, so we only reach here after trying to
1407 * read a single sector.
1408 */
1409 spin_lock_irq(&md->lock);
1410 ret = __blk_end_request(req, -EIO,
1411 brq->data.blksz);
1412 spin_unlock_irq(&md->lock);
Per Forlinee8a43a2011-07-01 18:55:33 +02001413 if (!ret)
1414 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001415 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301416 case MMC_BLK_NOMEDIUM:
1417 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001418 }
1419
Per Forlinee8a43a2011-07-01 18:55:33 +02001420 if (ret) {
1421 /*
Adrian Hunter67716322011-08-29 16:42:15 +03001422 * In case of a incomplete request
Per Forlinee8a43a2011-07-01 18:55:33 +02001423 * prepare it again and resend.
1424 */
1425 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1426 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 } while (ret);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 1;
1431
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001432 cmd_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 spin_lock_irq(&md->lock);
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301434 if (mmc_card_removed(card))
1435 req->cmd_flags |= REQ_QUIET;
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001436 while (ret)
1437 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 spin_unlock_irq(&md->lock);
1439
Per Forlinee8a43a2011-07-01 18:55:33 +02001440 start_new_req:
1441 if (rqc) {
1442 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1443 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447}
1448
Adrian Hunterbd788c92010-08-11 14:17:47 -07001449static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1450{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001451 int ret;
1452 struct mmc_blk_data *md = mq->data;
1453 struct mmc_card *card = md->queue.card;
1454
Per Forlinee8a43a2011-07-01 18:55:33 +02001455 if (req && !mq->mqrq_prev->req)
1456 /* claim host only for the first request */
1457 mmc_claim_host(card->host);
1458
Andrei Warkentin371a6892011-04-11 18:10:25 -05001459 ret = mmc_blk_part_switch(card, md);
1460 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001461 if (req) {
1462 spin_lock_irq(&md->lock);
1463 __blk_end_request_all(req, -EIO);
1464 spin_unlock_irq(&md->lock);
1465 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001466 ret = 0;
1467 goto out;
1468 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001469
Per Forlinee8a43a2011-07-01 18:55:33 +02001470 if (req && req->cmd_flags & REQ_DISCARD) {
1471 /* complete ongoing async transfer before issuing discard */
1472 if (card->host->areq)
1473 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chend4093542012-08-29 15:05:36 +09001474 if (req->cmd_flags & REQ_SECURE &&
1475 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001476 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001477 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001478 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001479 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001480 /* complete ongoing async transfer before issuing flush */
1481 if (card->host->areq)
1482 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001483 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001484 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001485 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001486 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001487
Andrei Warkentin371a6892011-04-11 18:10:25 -05001488out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001489 if (!req)
1490 /* release host only when there are no more requests */
1491 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001492 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Russell Kinga6f6c962006-01-03 22:38:44 +00001495static inline int mmc_blk_readonly(struct mmc_card *card)
1496{
1497 return mmc_card_readonly(card) ||
1498 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1499}
1500
Andrei Warkentin371a6892011-04-11 18:10:25 -05001501static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1502 struct device *parent,
1503 sector_t size,
1504 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001505 const char *subname,
1506 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 struct mmc_blk_data *md;
1509 int devidx, ret;
1510
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001511 devidx = find_first_zero_bit(dev_use, max_devices);
1512 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return ERR_PTR(-ENOSPC);
1514 __set_bit(devidx, dev_use);
1515
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001516 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001517 if (!md) {
1518 ret = -ENOMEM;
1519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001521
Russell Kinga6f6c962006-01-03 22:38:44 +00001522 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001523 * !subname implies we are creating main mmc_blk_data that will be
1524 * associated with mmc_card with mmc_set_drvdata. Due to device
1525 * partitions, devidx will not coincide with a per-physical card
1526 * index anymore so we keep track of a name index.
1527 */
1528 if (!subname) {
1529 md->name_idx = find_first_zero_bit(name_use, max_devices);
1530 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001531 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001532 md->name_idx = ((struct mmc_blk_data *)
1533 dev_to_disk(parent)->private_data)->name_idx;
1534
Johan Rudholmadd710e2011-12-02 08:51:06 +01001535 md->area_type = area_type;
1536
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001537 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001538 * Set the read-only status based on the supported commands
1539 * and the write protect switch.
1540 */
1541 md->read_only = mmc_blk_readonly(card);
1542
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001543 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001544 if (md->disk == NULL) {
1545 ret = -ENOMEM;
1546 goto err_kfree;
1547 }
1548
1549 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001550 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001551 md->usage = 1;
1552
Adrian Hunterd09408a2011-06-23 13:40:28 +03001553 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001554 if (ret)
1555 goto err_putdisk;
1556
Russell Kinga6f6c962006-01-03 22:38:44 +00001557 md->queue.issue_fn = mmc_blk_issue_rq;
1558 md->queue.data = md;
1559
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001560 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001561 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001562 md->disk->fops = &mmc_bdops;
1563 md->disk->private_data = md;
1564 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001565 md->disk->driverfs_dev = parent;
1566 set_disk_ro(md->disk, md->read_only || default_ro);
Russell Kinga6f6c962006-01-03 22:38:44 +00001567
1568 /*
1569 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1570 *
1571 * - be set for removable media with permanent block devices
1572 * - be unset for removable block devices with permanent media
1573 *
1574 * Since MMC block devices clearly fall under the second
1575 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1576 * should use the block device creation/destruction hotplug
1577 * messages to tell when the card is present.
1578 */
1579
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001580 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1581 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001582
Martin K. Petersene1defc42009-05-22 17:17:49 -04001583 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001584 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001585
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001586 if (mmc_host_cmd23(card->host)) {
1587 if (mmc_card_mmc(card) ||
1588 (mmc_card_sd(card) &&
1589 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1590 md->flags |= MMC_BLK_CMD23;
1591 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001592
1593 if (mmc_card_mmc(card) &&
1594 md->flags & MMC_BLK_CMD23 &&
1595 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1596 card->ext_csd.rel_sectors)) {
1597 md->flags |= MMC_BLK_REL_WR;
1598 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1599 }
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001602
1603 err_putdisk:
1604 put_disk(md->disk);
1605 err_kfree:
1606 kfree(md);
1607 out:
1608 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Andrei Warkentin371a6892011-04-11 18:10:25 -05001611static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1612{
1613 sector_t size;
1614 struct mmc_blk_data *md;
1615
1616 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1617 /*
1618 * The EXT_CSD sector count is in number or 512 byte
1619 * sectors.
1620 */
1621 size = card->ext_csd.sectors;
1622 } else {
1623 /*
1624 * The CSD capacity field is in units of read_blkbits.
1625 * set_capacity takes units of 512 bytes.
1626 */
1627 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1628 }
1629
Johan Rudholmadd710e2011-12-02 08:51:06 +01001630 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1631 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001632 return md;
1633}
1634
1635static int mmc_blk_alloc_part(struct mmc_card *card,
1636 struct mmc_blk_data *md,
1637 unsigned int part_type,
1638 sector_t size,
1639 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001640 const char *subname,
1641 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001642{
1643 char cap_str[10];
1644 struct mmc_blk_data *part_md;
1645
1646 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001647 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001648 if (IS_ERR(part_md))
1649 return PTR_ERR(part_md);
1650 part_md->part_type = part_type;
1651 list_add(&part_md->part, &md->part);
1652
1653 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1654 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301655 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001656 part_md->disk->disk_name, mmc_card_id(card),
1657 mmc_card_name(card), part_md->part_type, cap_str);
1658 return 0;
1659}
1660
Namjae Jeone0c368d2011-10-06 23:41:38 +09001661/* MMC Physical partitions consist of two boot partitions and
1662 * up to four general purpose partitions.
1663 * For each partition enabled in EXT_CSD a block device will be allocatedi
1664 * to provide access to the partition.
1665 */
1666
Andrei Warkentin371a6892011-04-11 18:10:25 -05001667static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1668{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001669 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001670
1671 if (!mmc_card_mmc(card))
1672 return 0;
1673
Namjae Jeone0c368d2011-10-06 23:41:38 +09001674 for (idx = 0; idx < card->nr_parts; idx++) {
1675 if (card->part[idx].size) {
1676 ret = mmc_blk_alloc_part(card, md,
1677 card->part[idx].part_cfg,
1678 card->part[idx].size >> 9,
1679 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001680 card->part[idx].name,
1681 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09001682 if (ret)
1683 return ret;
1684 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001685 }
1686
1687 return ret;
1688}
1689
Andrei Warkentin371a6892011-04-11 18:10:25 -05001690static void mmc_blk_remove_req(struct mmc_blk_data *md)
1691{
Johan Rudholmadd710e2011-12-02 08:51:06 +01001692 struct mmc_card *card;
1693
Andrei Warkentin371a6892011-04-11 18:10:25 -05001694 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01001695 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001696 if (md->disk->flags & GENHD_FL_UP) {
1697 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001698 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1699 card->ext_csd.boot_ro_lockable)
1700 device_remove_file(disk_to_dev(md->disk),
1701 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001702
1703 /* Stop new requests from getting into the queue */
1704 del_gendisk(md->disk);
1705 }
1706
1707 /* Then flush out any already in there */
1708 mmc_cleanup_queue(&md->queue);
1709 mmc_blk_put(md);
1710 }
1711}
1712
1713static void mmc_blk_remove_parts(struct mmc_card *card,
1714 struct mmc_blk_data *md)
1715{
1716 struct list_head *pos, *q;
1717 struct mmc_blk_data *part_md;
1718
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001719 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001720 list_for_each_safe(pos, q, &md->part) {
1721 part_md = list_entry(pos, struct mmc_blk_data, part);
1722 list_del(pos);
1723 mmc_blk_remove_req(part_md);
1724 }
1725}
1726
1727static int mmc_add_disk(struct mmc_blk_data *md)
1728{
1729 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001730 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001731
1732 add_disk(md->disk);
1733 md->force_ro.show = force_ro_show;
1734 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301735 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001736 md->force_ro.attr.name = "force_ro";
1737 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1738 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1739 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01001740 goto force_ro_fail;
1741
1742 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1743 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04001744 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001745
1746 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1747 mode = S_IRUGO;
1748 else
1749 mode = S_IRUGO | S_IWUSR;
1750
1751 md->power_ro_lock.show = power_ro_lock_show;
1752 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01001753 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001754 md->power_ro_lock.attr.mode = mode;
1755 md->power_ro_lock.attr.name =
1756 "ro_lock_until_next_power_on";
1757 ret = device_create_file(disk_to_dev(md->disk),
1758 &md->power_ro_lock);
1759 if (ret)
1760 goto power_ro_lock_fail;
1761 }
1762 return ret;
1763
1764power_ro_lock_fail:
1765 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1766force_ro_fail:
1767 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001768
1769 return ret;
1770}
1771
Chris Ballc59d4472011-11-11 22:01:43 -05001772#define CID_MANFID_SANDISK 0x2
1773#define CID_MANFID_TOSHIBA 0x11
1774#define CID_MANFID_MICRON 0x13
Ian Chend4093542012-08-29 15:05:36 +09001775#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05001776
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001777static const struct mmc_fixup blk_fixups[] =
1778{
Chris Ballc59d4472011-11-11 22:01:43 -05001779 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1780 MMC_QUIRK_INAND_CMD38),
1781 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1782 MMC_QUIRK_INAND_CMD38),
1783 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1784 MMC_QUIRK_INAND_CMD38),
1785 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1786 MMC_QUIRK_INAND_CMD38),
1787 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1788 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001789
1790 /*
1791 * Some MMC cards experience performance degradation with CMD23
1792 * instead of CMD12-bounded multiblock transfers. For now we'll
1793 * black list what's bad...
1794 * - Certain Toshiba cards.
1795 *
1796 * N.B. This doesn't affect SD cards.
1797 */
Chris Ballc59d4472011-11-11 22:01:43 -05001798 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001799 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001800 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001801 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001802 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001803 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001804
1805 /*
1806 * Some Micron MMC cards needs longer data read timeout than
1807 * indicated in CSD.
1808 */
Chris Ballc59d4472011-11-11 22:01:43 -05001809 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001810 MMC_QUIRK_LONG_READ_TIME),
1811
Ian Chend4093542012-08-29 15:05:36 +09001812 /*
1813 * On these Samsung MoviNAND parts, performing secure erase or
1814 * secure trim can result in unrecoverable corruption due to a
1815 * firmware bug.
1816 */
1817 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1818 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1819 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1820 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1821 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1822 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1823 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1824 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1825 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1826 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1827 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1828 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1829 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1830 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1831 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1832 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1833
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001834 END_FIXUP
1835};
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837static int mmc_blk_probe(struct mmc_card *card)
1838{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001839 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001840 char cap_str[10];
1841
Pierre Ossman912490d2005-05-21 10:27:02 +01001842 /*
1843 * Check that the card supports the command class(es) we need.
1844 */
1845 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -ENODEV;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 md = mmc_blk_alloc(card);
1849 if (IS_ERR(md))
1850 return PTR_ERR(md);
1851
Yi Li444122f2009-02-05 15:31:57 +08001852 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001853 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301854 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001856 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Andrei Warkentin371a6892011-04-11 18:10:25 -05001858 if (mmc_blk_alloc_parts(card, md))
1859 goto out;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001862 mmc_fixup_device(card, blk_fixups);
1863
Andrei Warkentin371a6892011-04-11 18:10:25 -05001864 if (mmc_add_disk(md))
1865 goto out;
1866
1867 list_for_each_entry(part_md, &md->part, part) {
1868 if (mmc_add_disk(part_md))
1869 goto out;
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 0;
1872
1873 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001874 mmc_blk_remove_parts(card, md);
1875 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01001876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879static void mmc_blk_remove(struct mmc_card *card)
1880{
1881 struct mmc_blk_data *md = mmc_get_drvdata(card);
1882
Andrei Warkentin371a6892011-04-11 18:10:25 -05001883 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001884 mmc_claim_host(card->host);
1885 mmc_blk_part_switch(card, md);
1886 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001887 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 mmc_set_drvdata(card, NULL);
1889}
1890
1891#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08001892static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001894 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct mmc_blk_data *md = mmc_get_drvdata(card);
1896
1897 if (md) {
1898 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001899 list_for_each_entry(part_md, &md->part, part) {
1900 mmc_queue_suspend(&part_md->queue);
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 return 0;
1904}
1905
1906static int mmc_blk_resume(struct mmc_card *card)
1907{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001908 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct mmc_blk_data *md = mmc_get_drvdata(card);
1910
1911 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05001912 /*
1913 * Resume involves the card going into idle state,
1914 * so current partition is always the main one.
1915 */
1916 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001918 list_for_each_entry(part_md, &md->part, part) {
1919 mmc_queue_resume(&part_md->queue);
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 return 0;
1923}
1924#else
1925#define mmc_blk_suspend NULL
1926#define mmc_blk_resume NULL
1927#endif
1928
1929static struct mmc_driver mmc_driver = {
1930 .drv = {
1931 .name = "mmcblk",
1932 },
1933 .probe = mmc_blk_probe,
1934 .remove = mmc_blk_remove,
1935 .suspend = mmc_blk_suspend,
1936 .resume = mmc_blk_resume,
1937};
1938
1939static int __init mmc_blk_init(void)
1940{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001941 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001943 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1944 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1945
1946 max_devices = 256 / perdev_minors;
1947
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001948 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1949 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001952 res = mmc_register_driver(&mmc_driver);
1953 if (res)
1954 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001956 return 0;
1957 out2:
1958 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 out:
1960 return res;
1961}
1962
1963static void __exit mmc_blk_exit(void)
1964{
1965 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001966 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969module_init(mmc_blk_init);
1970module_exit(mmc_blk_exit);
1971
1972MODULE_LICENSE("GPL");
1973MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1974