blob: 2eca7dac7748df907af43b5e245d62c35da05c1b [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
44#include <asm/system.h>
45#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040050#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010054
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050055#define INAND_CMD38_ARG_EXT_CSD 113
56#define INAND_CMD38_ARG_ERASE 0x00
57#define INAND_CMD38_ARG_TRIM 0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
61
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;
Andrei Warkentin371a6892011-04-11 18:10:25 -050097
98 /*
99 * Only set in main mmc_blk_data associated
100 * with mmc_card with mmc_set_drvdata, and keeps
101 * track of the current selected device partition.
102 */
103 unsigned int part_curr;
104 struct device_attribute force_ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Arjan van de Vena621aae2006-01-12 18:43:35 +0000107static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400109module_param(perdev_minors, int, 0444);
110MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
113{
114 struct mmc_blk_data *md;
115
Arjan van de Vena621aae2006-01-12 18:43:35 +0000116 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 md = disk->private_data;
118 if (md && md->usage == 0)
119 md = NULL;
120 if (md)
121 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000122 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return md;
125}
126
Andrei Warkentin371a6892011-04-11 18:10:25 -0500127static inline int mmc_get_devidx(struct gendisk *disk)
128{
Colin Crossfa746fa2010-09-03 12:41:21 -0700129 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500130 return devidx;
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void mmc_blk_put(struct mmc_blk_data *md)
134{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 md->usage--;
137 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800139 blk_cleanup_queue(md->queue.queue);
140
David Woodhouse1dff3142007-11-21 18:45:12 +0100141 __clear_bit(devidx, dev_use);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 kfree(md);
145 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000146 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Andrei Warkentin371a6892011-04-11 18:10:25 -0500149static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
150 char *buf)
151{
152 int ret;
153 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
154
155 ret = snprintf(buf, PAGE_SIZE, "%d",
156 get_disk_ro(dev_to_disk(dev)) ^
157 md->read_only);
158 mmc_blk_put(md);
159 return ret;
160}
161
162static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
163 const char *buf, size_t count)
164{
165 int ret;
166 char *end;
167 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
168 unsigned long set = simple_strtoul(buf, &end, 0);
169 if (end == buf) {
170 ret = -EINVAL;
171 goto out;
172 }
173
174 set_disk_ro(dev_to_disk(dev), set || md->read_only);
175 ret = count;
176out:
177 mmc_blk_put(md);
178 return ret;
179}
180
Al Viroa5a15612008-03-02 10:33:30 -0500181static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Viroa5a15612008-03-02 10:33:30 -0500183 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int ret = -ENXIO;
185
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200186 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (md) {
188 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500189 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700191
Al Viroa5a15612008-03-02 10:33:30 -0500192 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700193 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700194 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200197 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 return ret;
200}
201
Al Viroa5a15612008-03-02 10:33:30 -0500202static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Al Viroa5a15612008-03-02 10:33:30 -0500204 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200206 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200208 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210}
211
212static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800213mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800215 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
216 geo->heads = 4;
217 geo->sectors = 16;
218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
John Calixtocb87ea22011-04-26 18:56:29 -0400221struct mmc_blk_ioc_data {
222 struct mmc_ioc_cmd ic;
223 unsigned char *buf;
224 u64 buf_bytes;
225};
226
227static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
228 struct mmc_ioc_cmd __user *user)
229{
230 struct mmc_blk_ioc_data *idata;
231 int err;
232
233 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
234 if (!idata) {
235 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400236 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400237 }
238
239 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
240 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400241 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400242 }
243
244 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
245 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
246 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400247 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400248 }
249
250 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
251 if (!idata->buf) {
252 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400253 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400254 }
255
256 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
257 idata->ic.data_ptr, idata->buf_bytes)) {
258 err = -EFAULT;
259 goto copy_err;
260 }
261
262 return idata;
263
264copy_err:
265 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400266idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400267 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400268out:
John Calixtocb87ea22011-04-26 18:56:29 -0400269 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400270}
271
272static int mmc_blk_ioctl_cmd(struct block_device *bdev,
273 struct mmc_ioc_cmd __user *ic_ptr)
274{
275 struct mmc_blk_ioc_data *idata;
276 struct mmc_blk_data *md;
277 struct mmc_card *card;
278 struct mmc_command cmd = {0};
279 struct mmc_data data = {0};
280 struct mmc_request mrq = {0};
281 struct scatterlist sg;
282 int err;
283
284 /*
285 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
286 * whole block device, not on a partition. This prevents overspray
287 * between sibling partitions.
288 */
289 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
290 return -EPERM;
291
292 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
293 if (IS_ERR(idata))
294 return PTR_ERR(idata);
295
296 cmd.opcode = idata->ic.opcode;
297 cmd.arg = idata->ic.arg;
298 cmd.flags = idata->ic.flags;
299
300 data.sg = &sg;
301 data.sg_len = 1;
302 data.blksz = idata->ic.blksz;
303 data.blocks = idata->ic.blocks;
304
305 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
306
307 if (idata->ic.write_flag)
308 data.flags = MMC_DATA_WRITE;
309 else
310 data.flags = MMC_DATA_READ;
311
312 mrq.cmd = &cmd;
313 mrq.data = &data;
314
315 md = mmc_blk_get(bdev->bd_disk);
316 if (!md) {
317 err = -EINVAL;
318 goto cmd_done;
319 }
320
321 card = md->queue.card;
322 if (IS_ERR(card)) {
323 err = PTR_ERR(card);
324 goto cmd_done;
325 }
326
327 mmc_claim_host(card->host);
328
329 if (idata->ic.is_acmd) {
330 err = mmc_app_cmd(card->host, card);
331 if (err)
332 goto cmd_rel_host;
333 }
334
335 /* data.flags must already be set before doing this. */
336 mmc_set_data_timeout(&data, card);
337 /* Allow overriding the timeout_ns for empirical tuning. */
338 if (idata->ic.data_timeout_ns)
339 data.timeout_ns = idata->ic.data_timeout_ns;
340
341 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
342 /*
343 * Pretend this is a data transfer and rely on the host driver
344 * to compute timeout. When all host drivers support
345 * cmd.cmd_timeout for R1B, this can be changed to:
346 *
347 * mrq.data = NULL;
348 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
349 */
350 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
351 }
352
353 mmc_wait_for_req(card->host, &mrq);
354
355 if (cmd.error) {
356 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
357 __func__, cmd.error);
358 err = cmd.error;
359 goto cmd_rel_host;
360 }
361 if (data.error) {
362 dev_err(mmc_dev(card->host), "%s: data error %d\n",
363 __func__, data.error);
364 err = data.error;
365 goto cmd_rel_host;
366 }
367
368 /*
369 * According to the SD specs, some commands require a delay after
370 * issuing the command.
371 */
372 if (idata->ic.postsleep_min_us)
373 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
374
375 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
376 err = -EFAULT;
377 goto cmd_rel_host;
378 }
379
380 if (!idata->ic.write_flag) {
381 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
382 idata->buf, idata->buf_bytes)) {
383 err = -EFAULT;
384 goto cmd_rel_host;
385 }
386 }
387
388cmd_rel_host:
389 mmc_release_host(card->host);
390
391cmd_done:
392 mmc_blk_put(md);
393 kfree(idata->buf);
394 kfree(idata);
395 return err;
396}
397
398static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
399 unsigned int cmd, unsigned long arg)
400{
401 int ret = -EINVAL;
402 if (cmd == MMC_IOC_CMD)
403 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
404 return ret;
405}
406
407#ifdef CONFIG_COMPAT
408static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
409 unsigned int cmd, unsigned long arg)
410{
411 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
412}
413#endif
414
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700415static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500416 .open = mmc_blk_open,
417 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800418 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400420 .ioctl = mmc_blk_ioctl,
421#ifdef CONFIG_COMPAT
422 .compat_ioctl = mmc_blk_compat_ioctl,
423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424};
425
426struct mmc_blk_request {
427 struct mmc_request mrq;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500428 struct mmc_command sbc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct mmc_command cmd;
430 struct mmc_command stop;
431 struct mmc_data data;
432};
433
Andrei Warkentin371a6892011-04-11 18:10:25 -0500434static inline int mmc_blk_part_switch(struct mmc_card *card,
435 struct mmc_blk_data *md)
436{
437 int ret;
438 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
439 if (main_md->part_curr == md->part_type)
440 return 0;
441
442 if (mmc_card_mmc(card)) {
443 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
444 card->ext_csd.part_config |= md->part_type;
445
446 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
447 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
448 card->ext_csd.part_time);
449 if (ret)
450 return ret;
451}
452
453 main_md->part_curr = md->part_type;
454 return 0;
455}
456
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700457static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
458{
459 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100460 u32 result;
461 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700462
Chris Ball24f5b532011-04-13 23:49:45 -0400463 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400464 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400465 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700466 unsigned int timeout_us;
467
468 struct scatterlist sg;
469
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700470 cmd.opcode = MMC_APP_CMD;
471 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700472 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700473
474 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700475 if (err)
476 return (u32)-1;
477 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700478 return (u32)-1;
479
480 memset(&cmd, 0, sizeof(struct mmc_command));
481
482 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
483 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700484 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700485
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700486 data.timeout_ns = card->csd.tacc_ns * 100;
487 data.timeout_clks = card->csd.tacc_clks * 100;
488
489 timeout_us = data.timeout_ns / 1000;
490 timeout_us += data.timeout_clks * 1000 /
491 (card->host->ios.clock / 1000);
492
493 if (timeout_us > 100000) {
494 data.timeout_ns = 100000000;
495 data.timeout_clks = 0;
496 }
497
498 data.blksz = 4;
499 data.blocks = 1;
500 data.flags = MMC_DATA_READ;
501 data.sg = &sg;
502 data.sg_len = 1;
503
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700504 mrq.cmd = &cmd;
505 mrq.data = &data;
506
Ben Dooks051913d2009-06-08 23:33:57 +0100507 blocks = kmalloc(4, GFP_KERNEL);
508 if (!blocks)
509 return (u32)-1;
510
511 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700512
513 mmc_wait_for_req(card->host, &mrq);
514
Ben Dooks051913d2009-06-08 23:33:57 +0100515 result = ntohl(*blocks);
516 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700517
Ben Dooks051913d2009-06-08 23:33:57 +0100518 if (cmd.error || data.error)
519 result = (u32)-1;
520
521 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700522}
523
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100524static int send_stop(struct mmc_card *card, u32 *status)
525{
526 struct mmc_command cmd = {0};
527 int err;
528
529 cmd.opcode = MMC_STOP_TRANSMISSION;
530 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
531 err = mmc_wait_for_cmd(card->host, &cmd, 5);
532 if (err == 0)
533 *status = cmd.resp[0];
534 return err;
535}
536
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100537static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300538{
Chris Ball1278dba2011-04-13 23:40:30 -0400539 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300540 int err;
541
Adrian Hunter504f1912008-10-16 12:55:25 +0300542 cmd.opcode = MMC_SEND_STATUS;
543 if (!mmc_host_is_spi(card->host))
544 cmd.arg = card->rca << 16;
545 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100546 err = mmc_wait_for_cmd(card->host, &cmd, retries);
547 if (err == 0)
548 *status = cmd.resp[0];
549 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300550}
551
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100552#define ERR_RETRY 2
553#define ERR_ABORT 1
554#define ERR_CONTINUE 0
555
556static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
557 bool status_valid, u32 status)
558{
559 switch (error) {
560 case -EILSEQ:
561 /* response crc error, retry the r/w cmd */
562 pr_err("%s: %s sending %s command, card status %#x\n",
563 req->rq_disk->disk_name, "response CRC error",
564 name, status);
565 return ERR_RETRY;
566
567 case -ETIMEDOUT:
568 pr_err("%s: %s sending %s command, card status %#x\n",
569 req->rq_disk->disk_name, "timed out", name, status);
570
571 /* If the status cmd initially failed, retry the r/w cmd */
572 if (!status_valid)
573 return ERR_RETRY;
574
575 /*
576 * If it was a r/w cmd crc error, or illegal command
577 * (eg, issued in wrong state) then retry - we should
578 * have corrected the state problem above.
579 */
580 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
581 return ERR_RETRY;
582
583 /* Otherwise abort the command */
584 return ERR_ABORT;
585
586 default:
587 /* We don't understand the error code the driver gave us */
588 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
589 req->rq_disk->disk_name, error, status);
590 return ERR_ABORT;
591 }
592}
593
594/*
595 * Initial r/w and stop cmd error recovery.
596 * We don't know whether the card received the r/w cmd or not, so try to
597 * restore things back to a sane state. Essentially, we do this as follows:
598 * - Obtain card status. If the first attempt to obtain card status fails,
599 * the status word will reflect the failed status cmd, not the failed
600 * r/w cmd. If we fail to obtain card status, it suggests we can no
601 * longer communicate with the card.
602 * - Check the card state. If the card received the cmd but there was a
603 * transient problem with the response, it might still be in a data transfer
604 * mode. Try to send it a stop command. If this fails, we can't recover.
605 * - If the r/w cmd failed due to a response CRC error, it was probably
606 * transient, so retry the cmd.
607 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
608 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
609 * illegal cmd, retry.
610 * Otherwise we don't understand what happened, so abort.
611 */
612static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
613 struct mmc_blk_request *brq)
614{
615 bool prev_cmd_status_valid = true;
616 u32 status, stop_status = 0;
617 int err, retry;
618
619 /*
620 * Try to get card status which indicates both the card state
621 * and why there was no response. If the first attempt fails,
622 * we can't be sure the returned status is for the r/w command.
623 */
624 for (retry = 2; retry >= 0; retry--) {
625 err = get_card_status(card, &status, 0);
626 if (!err)
627 break;
628
629 prev_cmd_status_valid = false;
630 pr_err("%s: error %d sending status command, %sing\n",
631 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
632 }
633
634 /* We couldn't get a response from the card. Give up. */
635 if (err)
636 return ERR_ABORT;
637
638 /*
639 * Check the current card state. If it is in some data transfer
640 * mode, tell it to stop (and hopefully transition back to TRAN.)
641 */
642 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
643 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
644 err = send_stop(card, &stop_status);
645 if (err)
646 pr_err("%s: error %d sending stop command\n",
647 req->rq_disk->disk_name, err);
648
649 /*
650 * If the stop cmd also timed out, the card is probably
651 * not present, so abort. Other errors are bad news too.
652 */
653 if (err)
654 return ERR_ABORT;
655 }
656
657 /* Check for set block count errors */
658 if (brq->sbc.error)
659 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
660 prev_cmd_status_valid, status);
661
662 /* Check for r/w command errors */
663 if (brq->cmd.error)
664 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
665 prev_cmd_status_valid, status);
666
667 /* Now for stop errors. These aren't fatal to the transfer. */
668 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
669 req->rq_disk->disk_name, brq->stop.error,
670 brq->cmd.resp[0], status);
671
672 /*
673 * Subsitute in our own stop status as this will give the error
674 * state which happened during the execution of the r/w command.
675 */
676 if (stop_status) {
677 brq->stop.resp[0] = stop_status;
678 brq->stop.error = 0;
679 }
680 return ERR_CONTINUE;
681}
682
Adrian Hunterbd788c92010-08-11 14:17:47 -0700683static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
684{
685 struct mmc_blk_data *md = mq->data;
686 struct mmc_card *card = md->queue.card;
687 unsigned int from, nr, arg;
688 int err = 0;
689
Adrian Hunterbd788c92010-08-11 14:17:47 -0700690 if (!mmc_can_erase(card)) {
691 err = -EOPNOTSUPP;
692 goto out;
693 }
694
695 from = blk_rq_pos(req);
696 nr = blk_rq_sectors(req);
697
698 if (mmc_can_trim(card))
699 arg = MMC_TRIM_ARG;
700 else
701 arg = MMC_ERASE_ARG;
702
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500703 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
704 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
705 INAND_CMD38_ARG_EXT_CSD,
706 arg == MMC_TRIM_ARG ?
707 INAND_CMD38_ARG_TRIM :
708 INAND_CMD38_ARG_ERASE,
709 0);
710 if (err)
711 goto out;
712 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700713 err = mmc_erase(card, from, nr, arg);
714out:
715 spin_lock_irq(&md->lock);
716 __blk_end_request(req, err, blk_rq_bytes(req));
717 spin_unlock_irq(&md->lock);
718
Adrian Hunterbd788c92010-08-11 14:17:47 -0700719 return err ? 0 : 1;
720}
721
Adrian Hunter49804542010-08-11 14:17:50 -0700722static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
723 struct request *req)
724{
725 struct mmc_blk_data *md = mq->data;
726 struct mmc_card *card = md->queue.card;
727 unsigned int from, nr, arg;
728 int err = 0;
729
Adrian Hunter49804542010-08-11 14:17:50 -0700730 if (!mmc_can_secure_erase_trim(card)) {
731 err = -EOPNOTSUPP;
732 goto out;
733 }
734
735 from = blk_rq_pos(req);
736 nr = blk_rq_sectors(req);
737
738 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
739 arg = MMC_SECURE_TRIM1_ARG;
740 else
741 arg = MMC_SECURE_ERASE_ARG;
742
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500743 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
744 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
745 INAND_CMD38_ARG_EXT_CSD,
746 arg == MMC_SECURE_TRIM1_ARG ?
747 INAND_CMD38_ARG_SECTRIM1 :
748 INAND_CMD38_ARG_SECERASE,
749 0);
750 if (err)
751 goto out;
752 }
Adrian Hunter49804542010-08-11 14:17:50 -0700753 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500754 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
755 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
756 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
757 INAND_CMD38_ARG_EXT_CSD,
758 INAND_CMD38_ARG_SECTRIM2,
759 0);
760 if (err)
761 goto out;
762 }
Adrian Hunter49804542010-08-11 14:17:50 -0700763 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500764 }
Adrian Hunter49804542010-08-11 14:17:50 -0700765out:
766 spin_lock_irq(&md->lock);
767 __blk_end_request(req, err, blk_rq_bytes(req));
768 spin_unlock_irq(&md->lock);
769
Adrian Hunter49804542010-08-11 14:17:50 -0700770 return err ? 0 : 1;
771}
772
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500773static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
774{
775 struct mmc_blk_data *md = mq->data;
776
777 /*
778 * No-op, only service this because we need REQ_FUA for reliable
779 * writes.
780 */
781 spin_lock_irq(&md->lock);
782 __blk_end_request_all(req, 0);
783 spin_unlock_irq(&md->lock);
784
785 return 1;
786}
787
788/*
789 * Reformat current write as a reliable write, supporting
790 * both legacy and the enhanced reliable write MMC cards.
791 * In each transfer we'll handle only as much as a single
792 * reliable write can handle, thus finish the request in
793 * partial completions.
794 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500795static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
796 struct mmc_card *card,
797 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500798{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500799 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
800 /* Legacy mode imposes restrictions on transfers. */
801 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
802 brq->data.blocks = 1;
803
804 if (brq->data.blocks > card->ext_csd.rel_sectors)
805 brq->data.blocks = card->ext_csd.rel_sectors;
806 else if (brq->data.blocks < card->ext_csd.rel_sectors)
807 brq->data.blocks = 1;
808 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500809}
810
Adrian Hunterbd788c92010-08-11 14:17:47 -0700811static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct mmc_blk_data *md = mq->data;
814 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700815 struct mmc_blk_request brq;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100816 int ret = 1, disable_multi = 0, retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500818 /*
819 * Reliable writes are used to implement Forced Unit Access and
820 * REQ_META accesses, and are supported only on MMCs.
821 */
822 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
823 (req->cmd_flags & REQ_META)) &&
824 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500825 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 do {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100828 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 memset(&brq, 0, sizeof(struct mmc_blk_request));
831 brq.mrq.cmd = &brq.cmd;
832 brq.mrq.data = &brq.data;
833
Tejun Heo83096eb2009-05-07 22:24:39 +0900834 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800835 if (!mmc_card_blockaddr(card))
836 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700837 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200838 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 brq.stop.opcode = MMC_STOP_TRANSMISSION;
840 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700841 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900842 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Adrian Hunter6a79e392008-12-31 18:21:17 +0100844 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200845 * The block layer doesn't support all sector count
846 * restrictions, so we need to be prepared for too big
847 * requests.
848 */
849 if (brq.data.blocks > card->host->max_blk_count)
850 brq.data.blocks = card->host->max_blk_count;
851
852 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100853 * After a read error, we redo the request one sector at a time
854 * in order to accurately determine which sectors can be read
855 * successfully.
856 */
857 if (disable_multi && brq.data.blocks > 1)
858 brq.data.blocks = 1;
859
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500860 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700861 /* SPI multiblock writes terminate using a special
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500862 * token, not a STOP_TRANSMISSION request.
David Brownell7213d172007-08-08 09:10:23 -0700863 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500864 if (!mmc_host_is_spi(card->host) ||
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500865 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700866 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100867 readcmd = MMC_READ_MULTIPLE_BLOCK;
868 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000869 } else {
870 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100871 readcmd = MMC_READ_SINGLE_BLOCK;
872 writecmd = MMC_WRITE_BLOCK;
873 }
Russell Kingdb53f282006-08-30 15:14:56 +0100874 if (rq_data_dir(req) == READ) {
875 brq.cmd.opcode = readcmd;
876 brq.data.flags |= MMC_DATA_READ;
877 } else {
878 brq.cmd.opcode = writecmd;
879 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500882 if (do_rel_wr)
883 mmc_apply_rel_rw(&brq, card, req);
884
885 /*
886 * Pre-defined multi-block transfers are preferable to
887 * open ended-ones (and necessary for reliable writes).
888 * However, it is not sufficient to just send CMD23,
889 * and avoid the final CMD12, as on an error condition
890 * CMD12 (stop) needs to be sent anyway. This, coupled
891 * with Auto-CMD23 enhancements provided by some
892 * hosts, means that the complexity of dealing
893 * with this is best left to the host. If CMD23 is
894 * supported by card and host, we'll fill sbc in and let
895 * the host deal with handling it correctly. This means
896 * that for hosts that don't expose MMC_CAP_CMD23, no
897 * change of behavior will be observed.
898 *
899 * N.B: Some MMC cards experience perf degradation.
900 * We'll avoid using CMD23-bounded multiblock writes for
901 * these, while retaining features like reliable writes.
902 */
903
904 if ((md->flags & MMC_BLK_CMD23) &&
905 mmc_op_multi(brq.cmd.opcode) &&
906 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
907 brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
908 brq.sbc.arg = brq.data.blocks |
909 (do_rel_wr ? (1 << 31) : 0);
910 brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
911 brq.mrq.sbc = &brq.sbc;
912 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500913
Pierre Ossmanb146d262007-07-24 19:16:54 +0200914 mmc_set_data_timeout(&brq.data, card);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200917 brq.data.sg_len = mmc_queue_map_sg(mq);
918
Adrian Hunter6a79e392008-12-31 18:21:17 +0100919 /*
920 * Adjust the sg list so it is the same size as the
921 * request.
922 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900923 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100924 int i, data_size = brq.data.blocks << 9;
925 struct scatterlist *sg;
926
927 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
928 data_size -= sg->length;
929 if (data_size <= 0) {
930 sg->length += data_size;
931 i++;
932 break;
933 }
934 }
935 brq.data.sg_len = i;
936 }
937
Pierre Ossman98ccf142007-05-12 00:26:16 +0200938 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200941
942 mmc_queue_bounce_post(mq);
943
Pierre Ossman979ce722008-06-29 12:19:47 +0200944 /*
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100945 * sbc.error indicates a problem with the set block count
946 * command. No data will have been transferred.
947 *
948 * cmd.error indicates a problem with the r/w command. No
949 * data will have been transferred.
950 *
951 * stop.error indicates a problem with the stop command. Data
952 * may have been transferred, or may still be transferring.
Pierre Ossman979ce722008-06-29 12:19:47 +0200953 */
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100954 if (brq.sbc.error || brq.cmd.error || brq.stop.error) {
955 switch (mmc_blk_cmd_recovery(card, req, &brq)) {
956 case ERR_RETRY:
957 if (retry++ < 5)
958 continue;
959 case ERR_ABORT:
960 goto cmd_abort;
961 case ERR_CONTINUE:
962 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
David Brownell7213d172007-08-08 09:10:23 -0700966 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100967 u32 status;
Russell King2ed6d222006-09-24 10:46:43 +0100968 do {
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100969 int err = get_card_status(card, &status, 5);
Russell King2ed6d222006-09-24 10:46:43 +0100970 if (err) {
971 printk(KERN_ERR "%s: error %d requesting status\n",
972 req->rq_disk->disk_name, err);
973 goto cmd_err;
974 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100975 /*
976 * Some cards mishandle the status bits,
977 * so make sure to check both the busy
978 * indication and the card state.
979 */
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100980 } while (!(status & R1_READY_FOR_DATA) ||
981 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
Russell King2ed6d222006-09-24 10:46:43 +0100982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100984 if (brq.data.error) {
985 pr_err("%s: error %d transferring data, sector %u nr %u, cmd response %#x card status %#x\n",
986 req->rq_disk->disk_name, brq.data.error,
987 (unsigned)blk_rq_pos(req),
988 (unsigned)blk_rq_sectors(req),
989 brq.cmd.resp[0], brq.stop.resp[0]);
990
Adrian Hunter6a79e392008-12-31 18:21:17 +0100991 if (rq_data_dir(req) == READ) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100992 if (brq.data.blocks > 1) {
993 /* Redo read one sector at a time */
994 pr_warning("%s: retrying using single block read\n",
995 req->rq_disk->disk_name);
996 disable_multi = 1;
997 continue;
998 }
999
Adrian Hunter6a79e392008-12-31 18:21:17 +01001000 /*
1001 * After an error, we redo I/O one sector at a
1002 * time, so we only reach here after trying to
1003 * read a single sector.
1004 */
1005 spin_lock_irq(&md->lock);
1006 ret = __blk_end_request(req, -EIO, brq.data.blksz);
1007 spin_unlock_irq(&md->lock);
1008 continue;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001009 } else {
1010 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001011 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001012 }
Pierre Ossman979ce722008-06-29 12:19:47 +02001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /*
1015 * A block was successfully transferred.
1016 */
1017 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001018 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 spin_unlock_irq(&md->lock);
1020 } while (ret);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 1;
1023
1024 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001025 /*
1026 * If this is an SD card and we're writing, we can first
1027 * mark the known good sectors as ok.
1028 *
1029 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +02001030 * as reported by the controller (which might be less than
1031 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
Adrian Hunter6a79e392008-12-31 18:21:17 +01001033 if (mmc_card_sd(card)) {
1034 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001035
Adrian Hunter6a79e392008-12-31 18:21:17 +01001036 blocks = mmc_sd_num_wr_blocks(card);
1037 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001038 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001039 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001040 spin_unlock_irq(&md->lock);
1041 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001042 } else {
1043 spin_lock_irq(&md->lock);
1044 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1045 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -07001046 }
1047
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001048 cmd_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001050 while (ret)
1051 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_unlock_irq(&md->lock);
1053
1054 return 0;
1055}
1056
San Mehatc87f8d42009-07-30 08:21:19 -07001057static int
1058mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card);
1059
Adrian Hunterbd788c92010-08-11 14:17:47 -07001060static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1061{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001062 int ret;
1063 struct mmc_blk_data *md = mq->data;
1064 struct mmc_card *card = md->queue.card;
1065
San Mehatc87f8d42009-07-30 08:21:19 -07001066#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1067 if (mmc_bus_needs_resume(card->host)) {
1068 mmc_resume_bus(card->host);
1069 mmc_blk_set_blksize(md, card);
1070 }
1071#endif
1072
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001073 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001074 ret = mmc_blk_part_switch(card, md);
1075 if (ret) {
1076 ret = 0;
1077 goto out;
1078 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001079
Adrian Hunter49804542010-08-11 14:17:50 -07001080 if (req->cmd_flags & REQ_DISCARD) {
1081 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001082 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001083 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001084 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001085 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001086 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001087 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001088 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001089 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001090
Andrei Warkentin371a6892011-04-11 18:10:25 -05001091out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001092 mmc_release_host(card->host);
1093 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Russell Kinga6f6c962006-01-03 22:38:44 +00001096static inline int mmc_blk_readonly(struct mmc_card *card)
1097{
1098 return mmc_card_readonly(card) ||
1099 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1100}
1101
Andrei Warkentin371a6892011-04-11 18:10:25 -05001102static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1103 struct device *parent,
1104 sector_t size,
1105 bool default_ro,
1106 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct mmc_blk_data *md;
1109 int devidx, ret;
1110
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001111 devidx = find_first_zero_bit(dev_use, max_devices);
1112 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return ERR_PTR(-ENOSPC);
1114 __set_bit(devidx, dev_use);
1115
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001116 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001117 if (!md) {
1118 ret = -ENOMEM;
1119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001121
Russell Kinga6f6c962006-01-03 22:38:44 +00001122 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001123 * !subname implies we are creating main mmc_blk_data that will be
1124 * associated with mmc_card with mmc_set_drvdata. Due to device
1125 * partitions, devidx will not coincide with a per-physical card
1126 * index anymore so we keep track of a name index.
1127 */
1128 if (!subname) {
1129 md->name_idx = find_first_zero_bit(name_use, max_devices);
1130 __set_bit(md->name_idx, name_use);
1131 }
1132 else
1133 md->name_idx = ((struct mmc_blk_data *)
1134 dev_to_disk(parent)->private_data)->name_idx;
1135
1136 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001137 * Set the read-only status based on the supported commands
1138 * and the write protect switch.
1139 */
1140 md->read_only = mmc_blk_readonly(card);
1141
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001142 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001143 if (md->disk == NULL) {
1144 ret = -ENOMEM;
1145 goto err_kfree;
1146 }
1147
1148 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001149 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001150 md->usage = 1;
1151
Adrian Hunterd09408a2011-06-23 13:40:28 +03001152 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001153 if (ret)
1154 goto err_putdisk;
1155
Russell Kinga6f6c962006-01-03 22:38:44 +00001156 md->queue.issue_fn = mmc_blk_issue_rq;
1157 md->queue.data = md;
1158
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001159 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001160 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001161 md->disk->fops = &mmc_bdops;
1162 md->disk->private_data = md;
1163 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001164 md->disk->driverfs_dev = parent;
1165 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Crossfa746fa2010-09-03 12:41:21 -07001166 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001167
1168 /*
1169 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1170 *
1171 * - be set for removable media with permanent block devices
1172 * - be unset for removable block devices with permanent media
1173 *
1174 * Since MMC block devices clearly fall under the second
1175 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1176 * should use the block device creation/destruction hotplug
1177 * messages to tell when the card is present.
1178 */
1179
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001180 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1181 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001182
Martin K. Petersene1defc42009-05-22 17:17:49 -04001183 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001184 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001185
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001186 if (mmc_host_cmd23(card->host)) {
1187 if (mmc_card_mmc(card) ||
1188 (mmc_card_sd(card) &&
1189 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1190 md->flags |= MMC_BLK_CMD23;
1191 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001192
1193 if (mmc_card_mmc(card) &&
1194 md->flags & MMC_BLK_CMD23 &&
1195 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1196 card->ext_csd.rel_sectors)) {
1197 md->flags |= MMC_BLK_REL_WR;
1198 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001202
1203 err_putdisk:
1204 put_disk(md->disk);
1205 err_kfree:
1206 kfree(md);
1207 out:
1208 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Andrei Warkentin371a6892011-04-11 18:10:25 -05001211static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1212{
1213 sector_t size;
1214 struct mmc_blk_data *md;
1215
1216 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1217 /*
1218 * The EXT_CSD sector count is in number or 512 byte
1219 * sectors.
1220 */
1221 size = card->ext_csd.sectors;
1222 } else {
1223 /*
1224 * The CSD capacity field is in units of read_blkbits.
1225 * set_capacity takes units of 512 bytes.
1226 */
1227 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1228 }
1229
1230 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1231 return md;
1232}
1233
1234static int mmc_blk_alloc_part(struct mmc_card *card,
1235 struct mmc_blk_data *md,
1236 unsigned int part_type,
1237 sector_t size,
1238 bool default_ro,
1239 const char *subname)
1240{
1241 char cap_str[10];
1242 struct mmc_blk_data *part_md;
1243
1244 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1245 subname);
1246 if (IS_ERR(part_md))
1247 return PTR_ERR(part_md);
1248 part_md->part_type = part_type;
1249 list_add(&part_md->part, &md->part);
1250
1251 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1252 cap_str, sizeof(cap_str));
1253 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1254 part_md->disk->disk_name, mmc_card_id(card),
1255 mmc_card_name(card), part_md->part_type, cap_str);
1256 return 0;
1257}
1258
1259static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1260{
1261 int ret = 0;
1262
1263 if (!mmc_card_mmc(card))
1264 return 0;
1265
1266 if (card->ext_csd.boot_size) {
1267 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1268 card->ext_csd.boot_size >> 9,
1269 true,
1270 "boot0");
1271 if (ret)
1272 return ret;
1273 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1274 card->ext_csd.boot_size >> 9,
1275 true,
1276 "boot1");
1277 if (ret)
1278 return ret;
1279 }
1280
1281 return ret;
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static int
1285mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err;
1288
Pierre Ossmanb8558852007-01-03 19:47:29 +01001289 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001290 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001291 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001294 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1295 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EINVAL;
1297 }
1298
1299 return 0;
1300}
1301
Andrei Warkentin371a6892011-04-11 18:10:25 -05001302static void mmc_blk_remove_req(struct mmc_blk_data *md)
1303{
1304 if (md) {
1305 if (md->disk->flags & GENHD_FL_UP) {
1306 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1307
1308 /* Stop new requests from getting into the queue */
1309 del_gendisk(md->disk);
1310 }
1311
1312 /* Then flush out any already in there */
1313 mmc_cleanup_queue(&md->queue);
1314 mmc_blk_put(md);
1315 }
1316}
1317
1318static void mmc_blk_remove_parts(struct mmc_card *card,
1319 struct mmc_blk_data *md)
1320{
1321 struct list_head *pos, *q;
1322 struct mmc_blk_data *part_md;
1323
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001324 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001325 list_for_each_safe(pos, q, &md->part) {
1326 part_md = list_entry(pos, struct mmc_blk_data, part);
1327 list_del(pos);
1328 mmc_blk_remove_req(part_md);
1329 }
1330}
1331
1332static int mmc_add_disk(struct mmc_blk_data *md)
1333{
1334 int ret;
1335
1336 add_disk(md->disk);
1337 md->force_ro.show = force_ro_show;
1338 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301339 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001340 md->force_ro.attr.name = "force_ro";
1341 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1342 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1343 if (ret)
1344 del_gendisk(md->disk);
1345
1346 return ret;
1347}
1348
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001349static const struct mmc_fixup blk_fixups[] =
1350{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001351 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1352 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1353 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1354 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1355 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001356
1357 /*
1358 * Some MMC cards experience performance degradation with CMD23
1359 * instead of CMD12-bounded multiblock transfers. For now we'll
1360 * black list what's bad...
1361 * - Certain Toshiba cards.
1362 *
1363 * N.B. This doesn't affect SD cards.
1364 */
1365 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1366 MMC_QUIRK_BLK_NO_CMD23),
1367 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1368 MMC_QUIRK_BLK_NO_CMD23),
1369 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1370 MMC_QUIRK_BLK_NO_CMD23),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001371 END_FIXUP
1372};
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static int mmc_blk_probe(struct mmc_card *card)
1375{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001376 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001378 char cap_str[10];
1379
Pierre Ossman912490d2005-05-21 10:27:02 +01001380 /*
1381 * Check that the card supports the command class(es) we need.
1382 */
1383 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -ENODEV;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 md = mmc_blk_alloc(card);
1387 if (IS_ERR(md))
1388 return PTR_ERR(md);
1389
1390 err = mmc_blk_set_blksize(md, card);
1391 if (err)
1392 goto out;
1393
Yi Li444122f2009-02-05 15:31:57 +08001394 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001395 cap_str, sizeof(cap_str));
1396 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001398 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Andrei Warkentin371a6892011-04-11 18:10:25 -05001400 if (mmc_blk_alloc_parts(card, md))
1401 goto out;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001404 mmc_fixup_device(card, blk_fixups);
1405
San Mehatc87f8d42009-07-30 08:21:19 -07001406#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1407 mmc_set_bus_resume_policy(card->host, 1);
1408#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001409 if (mmc_add_disk(md))
1410 goto out;
1411
1412 list_for_each_entry(part_md, &md->part, part) {
1413 if (mmc_add_disk(part_md))
1414 goto out;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
1417
1418 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001419 mmc_blk_remove_parts(card, md);
1420 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return err;
1422}
1423
1424static void mmc_blk_remove(struct mmc_card *card)
1425{
1426 struct mmc_blk_data *md = mmc_get_drvdata(card);
1427
Andrei Warkentin371a6892011-04-11 18:10:25 -05001428 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001429 mmc_claim_host(card->host);
1430 mmc_blk_part_switch(card, md);
1431 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001432 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 mmc_set_drvdata(card, NULL);
San Mehatc87f8d42009-07-30 08:21:19 -07001434#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1435 mmc_set_bus_resume_policy(card->host, 0);
1436#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439#ifdef CONFIG_PM
1440static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1441{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001442 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct mmc_blk_data *md = mmc_get_drvdata(card);
1444
1445 if (md) {
1446 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001447 list_for_each_entry(part_md, &md->part, part) {
1448 mmc_queue_suspend(&part_md->queue);
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 return 0;
1452}
1453
1454static int mmc_blk_resume(struct mmc_card *card)
1455{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001456 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct mmc_blk_data *md = mmc_get_drvdata(card);
1458
1459 if (md) {
San Mehatc87f8d42009-07-30 08:21:19 -07001460#ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 mmc_blk_set_blksize(md, card);
San Mehatc87f8d42009-07-30 08:21:19 -07001462#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001463
1464 /*
1465 * Resume involves the card going into idle state,
1466 * so current partition is always the main one.
1467 */
1468 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001470 list_for_each_entry(part_md, &md->part, part) {
1471 mmc_queue_resume(&part_md->queue);
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 return 0;
1475}
1476#else
1477#define mmc_blk_suspend NULL
1478#define mmc_blk_resume NULL
1479#endif
1480
1481static struct mmc_driver mmc_driver = {
1482 .drv = {
1483 .name = "mmcblk",
1484 },
1485 .probe = mmc_blk_probe,
1486 .remove = mmc_blk_remove,
1487 .suspend = mmc_blk_suspend,
1488 .resume = mmc_blk_resume,
1489};
1490
1491static int __init mmc_blk_init(void)
1492{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001493 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001495 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1496 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1497
1498 max_devices = 256 / perdev_minors;
1499
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001500 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1501 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001504 res = mmc_register_driver(&mmc_driver);
1505 if (res)
1506 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001508 return 0;
1509 out2:
1510 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 out:
1512 return res;
1513}
1514
1515static void __exit mmc_blk_exit(void)
1516{
1517 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001518 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521module_init(mmc_blk_init);
1522module_exit(mmc_blk_exit);
1523
1524MODULE_LICENSE("GPL");
1525MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1526