blob: 48f3f2d8d26fc039d3b02b89d75697b07ab62302 [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
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530552#define ERR_NOMEDIUM 3
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100553#define ERR_RETRY 2
554#define ERR_ABORT 1
555#define ERR_CONTINUE 0
556
557static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
558 bool status_valid, u32 status)
559{
560 switch (error) {
561 case -EILSEQ:
562 /* response crc error, retry the r/w cmd */
563 pr_err("%s: %s sending %s command, card status %#x\n",
564 req->rq_disk->disk_name, "response CRC error",
565 name, status);
566 return ERR_RETRY;
567
568 case -ETIMEDOUT:
569 pr_err("%s: %s sending %s command, card status %#x\n",
570 req->rq_disk->disk_name, "timed out", name, status);
571
572 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallf4104732011-10-25 18:16:58 -0700573 if (!status_valid) {
574 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100575 return ERR_RETRY;
Ken Sumrallf4104732011-10-25 18:16:58 -0700576 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100577 /*
578 * If it was a r/w cmd crc error, or illegal command
579 * (eg, issued in wrong state) then retry - we should
580 * have corrected the state problem above.
581 */
Ken Sumrallf4104732011-10-25 18:16:58 -0700582 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
583 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100584 return ERR_RETRY;
Ken Sumrallf4104732011-10-25 18:16:58 -0700585 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100586
587 /* Otherwise abort the command */
Ken Sumrallf4104732011-10-25 18:16:58 -0700588 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100589 return ERR_ABORT;
590
591 default:
592 /* We don't understand the error code the driver gave us */
593 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
594 req->rq_disk->disk_name, error, status);
595 return ERR_ABORT;
596 }
597}
598
599/*
600 * Initial r/w and stop cmd error recovery.
601 * We don't know whether the card received the r/w cmd or not, so try to
602 * restore things back to a sane state. Essentially, we do this as follows:
603 * - Obtain card status. If the first attempt to obtain card status fails,
604 * the status word will reflect the failed status cmd, not the failed
605 * r/w cmd. If we fail to obtain card status, it suggests we can no
606 * longer communicate with the card.
607 * - Check the card state. If the card received the cmd but there was a
608 * transient problem with the response, it might still be in a data transfer
609 * mode. Try to send it a stop command. If this fails, we can't recover.
610 * - If the r/w cmd failed due to a response CRC error, it was probably
611 * transient, so retry the cmd.
612 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
613 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
614 * illegal cmd, retry.
615 * Otherwise we don't understand what happened, so abort.
616 */
617static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
618 struct mmc_blk_request *brq)
619{
620 bool prev_cmd_status_valid = true;
621 u32 status, stop_status = 0;
622 int err, retry;
623
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530624 if (mmc_card_removed(card))
625 return ERR_NOMEDIUM;
626
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100627 /*
628 * Try to get card status which indicates both the card state
629 * and why there was no response. If the first attempt fails,
630 * we can't be sure the returned status is for the r/w command.
631 */
632 for (retry = 2; retry >= 0; retry--) {
633 err = get_card_status(card, &status, 0);
634 if (!err)
635 break;
636
637 prev_cmd_status_valid = false;
638 pr_err("%s: error %d sending status command, %sing\n",
639 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
640 }
641
642 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530643 if (err) {
644 /* Check if the card is removed */
645 if (mmc_detect_card_removed(card->host))
646 return ERR_NOMEDIUM;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100647 return ERR_ABORT;
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530648 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100649
650 /*
651 * Check the current card state. If it is in some data transfer
652 * mode, tell it to stop (and hopefully transition back to TRAN.)
653 */
654 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
655 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
656 err = send_stop(card, &stop_status);
657 if (err)
658 pr_err("%s: error %d sending stop command\n",
659 req->rq_disk->disk_name, err);
660
661 /*
662 * If the stop cmd also timed out, the card is probably
663 * not present, so abort. Other errors are bad news too.
664 */
665 if (err)
666 return ERR_ABORT;
667 }
668
669 /* Check for set block count errors */
670 if (brq->sbc.error)
671 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
672 prev_cmd_status_valid, status);
673
674 /* Check for r/w command errors */
675 if (brq->cmd.error)
676 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
677 prev_cmd_status_valid, status);
678
679 /* Now for stop errors. These aren't fatal to the transfer. */
680 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
681 req->rq_disk->disk_name, brq->stop.error,
682 brq->cmd.resp[0], status);
683
684 /*
685 * Subsitute in our own stop status as this will give the error
686 * state which happened during the execution of the r/w command.
687 */
688 if (stop_status) {
689 brq->stop.resp[0] = stop_status;
690 brq->stop.error = 0;
691 }
692 return ERR_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Adrian Hunterbd788c92010-08-11 14:17:47 -0700695static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
696{
697 struct mmc_blk_data *md = mq->data;
698 struct mmc_card *card = md->queue.card;
699 unsigned int from, nr, arg;
700 int err = 0;
701
Adrian Hunterbd788c92010-08-11 14:17:47 -0700702 if (!mmc_can_erase(card)) {
703 err = -EOPNOTSUPP;
704 goto out;
705 }
706
707 from = blk_rq_pos(req);
708 nr = blk_rq_sectors(req);
709
710 if (mmc_can_trim(card))
711 arg = MMC_TRIM_ARG;
712 else
713 arg = MMC_ERASE_ARG;
714
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500715 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
716 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
717 INAND_CMD38_ARG_EXT_CSD,
718 arg == MMC_TRIM_ARG ?
719 INAND_CMD38_ARG_TRIM :
720 INAND_CMD38_ARG_ERASE,
721 0);
722 if (err)
723 goto out;
724 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700725 err = mmc_erase(card, from, nr, arg);
726out:
727 spin_lock_irq(&md->lock);
728 __blk_end_request(req, err, blk_rq_bytes(req));
729 spin_unlock_irq(&md->lock);
730
Adrian Hunterbd788c92010-08-11 14:17:47 -0700731 return err ? 0 : 1;
732}
733
Adrian Hunter49804542010-08-11 14:17:50 -0700734static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
735 struct request *req)
736{
737 struct mmc_blk_data *md = mq->data;
738 struct mmc_card *card = md->queue.card;
739 unsigned int from, nr, arg;
740 int err = 0;
741
Adrian Hunter49804542010-08-11 14:17:50 -0700742 if (!mmc_can_secure_erase_trim(card)) {
743 err = -EOPNOTSUPP;
744 goto out;
745 }
746
747 from = blk_rq_pos(req);
748 nr = blk_rq_sectors(req);
749
750 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
751 arg = MMC_SECURE_TRIM1_ARG;
752 else
753 arg = MMC_SECURE_ERASE_ARG;
754
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500755 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
756 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
757 INAND_CMD38_ARG_EXT_CSD,
758 arg == MMC_SECURE_TRIM1_ARG ?
759 INAND_CMD38_ARG_SECTRIM1 :
760 INAND_CMD38_ARG_SECERASE,
761 0);
762 if (err)
763 goto out;
764 }
Adrian Hunter49804542010-08-11 14:17:50 -0700765 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500766 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
767 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
768 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
769 INAND_CMD38_ARG_EXT_CSD,
770 INAND_CMD38_ARG_SECTRIM2,
771 0);
772 if (err)
773 goto out;
774 }
Adrian Hunter49804542010-08-11 14:17:50 -0700775 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500776 }
Adrian Hunter49804542010-08-11 14:17:50 -0700777out:
778 spin_lock_irq(&md->lock);
779 __blk_end_request(req, err, blk_rq_bytes(req));
780 spin_unlock_irq(&md->lock);
781
Adrian Hunter49804542010-08-11 14:17:50 -0700782 return err ? 0 : 1;
783}
784
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500785static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
786{
787 struct mmc_blk_data *md = mq->data;
788
789 /*
790 * No-op, only service this because we need REQ_FUA for reliable
791 * writes.
792 */
793 spin_lock_irq(&md->lock);
794 __blk_end_request_all(req, 0);
795 spin_unlock_irq(&md->lock);
796
797 return 1;
798}
799
800/*
801 * Reformat current write as a reliable write, supporting
802 * both legacy and the enhanced reliable write MMC cards.
803 * In each transfer we'll handle only as much as a single
804 * reliable write can handle, thus finish the request in
805 * partial completions.
806 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500807static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
808 struct mmc_card *card,
809 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500810{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500811 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
812 /* Legacy mode imposes restrictions on transfers. */
813 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
814 brq->data.blocks = 1;
815
816 if (brq->data.blocks > card->ext_csd.rel_sectors)
817 brq->data.blocks = card->ext_csd.rel_sectors;
818 else if (brq->data.blocks < card->ext_csd.rel_sectors)
819 brq->data.blocks = 1;
820 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500821}
822
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100823#define CMD_ERRORS \
824 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
825 R1_ADDRESS_ERROR | /* Misaligned address */ \
826 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
827 R1_WP_VIOLATION | /* Tried to write to protected block */ \
828 R1_CC_ERROR | /* Card controller error */ \
829 R1_ERROR) /* General/unknown error */
830
Adrian Hunterbd788c92010-08-11 14:17:47 -0700831static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct mmc_blk_data *md = mq->data;
834 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700835 struct mmc_blk_request brq;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100836 int ret = 1, disable_multi = 0, retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500838 /*
839 * Reliable writes are used to implement Forced Unit Access and
840 * REQ_META accesses, and are supported only on MMCs.
841 */
842 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
843 (req->cmd_flags & REQ_META)) &&
844 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500845 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 do {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100848 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 memset(&brq, 0, sizeof(struct mmc_blk_request));
851 brq.mrq.cmd = &brq.cmd;
852 brq.mrq.data = &brq.data;
853
Tejun Heo83096eb2009-05-07 22:24:39 +0900854 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800855 if (!mmc_card_blockaddr(card))
856 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700857 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200858 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 brq.stop.opcode = MMC_STOP_TRANSMISSION;
860 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700861 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900862 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Adrian Hunter6a79e392008-12-31 18:21:17 +0100864 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200865 * The block layer doesn't support all sector count
866 * restrictions, so we need to be prepared for too big
867 * requests.
868 */
869 if (brq.data.blocks > card->host->max_blk_count)
870 brq.data.blocks = card->host->max_blk_count;
871
872 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100873 * After a read error, we redo the request one sector at a time
874 * in order to accurately determine which sectors can be read
875 * successfully.
876 */
877 if (disable_multi && brq.data.blocks > 1)
878 brq.data.blocks = 1;
879
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500880 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700881 /* SPI multiblock writes terminate using a special
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500882 * token, not a STOP_TRANSMISSION request.
David Brownell7213d172007-08-08 09:10:23 -0700883 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500884 if (!mmc_host_is_spi(card->host) ||
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500885 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700886 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100887 readcmd = MMC_READ_MULTIPLE_BLOCK;
888 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000889 } else {
890 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100891 readcmd = MMC_READ_SINGLE_BLOCK;
892 writecmd = MMC_WRITE_BLOCK;
893 }
Russell Kingdb53f282006-08-30 15:14:56 +0100894 if (rq_data_dir(req) == READ) {
895 brq.cmd.opcode = readcmd;
896 brq.data.flags |= MMC_DATA_READ;
897 } else {
898 brq.cmd.opcode = writecmd;
899 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500902 if (do_rel_wr)
903 mmc_apply_rel_rw(&brq, card, req);
904
905 /*
906 * Pre-defined multi-block transfers are preferable to
907 * open ended-ones (and necessary for reliable writes).
908 * However, it is not sufficient to just send CMD23,
909 * and avoid the final CMD12, as on an error condition
910 * CMD12 (stop) needs to be sent anyway. This, coupled
911 * with Auto-CMD23 enhancements provided by some
912 * hosts, means that the complexity of dealing
913 * with this is best left to the host. If CMD23 is
914 * supported by card and host, we'll fill sbc in and let
915 * the host deal with handling it correctly. This means
916 * that for hosts that don't expose MMC_CAP_CMD23, no
917 * change of behavior will be observed.
918 *
919 * N.B: Some MMC cards experience perf degradation.
920 * We'll avoid using CMD23-bounded multiblock writes for
921 * these, while retaining features like reliable writes.
922 */
923
924 if ((md->flags & MMC_BLK_CMD23) &&
925 mmc_op_multi(brq.cmd.opcode) &&
926 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
927 brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
928 brq.sbc.arg = brq.data.blocks |
929 (do_rel_wr ? (1 << 31) : 0);
930 brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
931 brq.mrq.sbc = &brq.sbc;
932 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500933
Pierre Ossmanb146d262007-07-24 19:16:54 +0200934 mmc_set_data_timeout(&brq.data, card);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200937 brq.data.sg_len = mmc_queue_map_sg(mq);
938
Adrian Hunter6a79e392008-12-31 18:21:17 +0100939 /*
940 * Adjust the sg list so it is the same size as the
941 * request.
942 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900943 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100944 int i, data_size = brq.data.blocks << 9;
945 struct scatterlist *sg;
946
947 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
948 data_size -= sg->length;
949 if (data_size <= 0) {
950 sg->length += data_size;
951 i++;
952 break;
953 }
954 }
955 brq.data.sg_len = i;
956 }
957
Pierre Ossman98ccf142007-05-12 00:26:16 +0200958 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200961
962 mmc_queue_bounce_post(mq);
963
Pierre Ossman979ce722008-06-29 12:19:47 +0200964 /*
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100965 * sbc.error indicates a problem with the set block count
966 * command. No data will have been transferred.
967 *
968 * cmd.error indicates a problem with the r/w command. No
969 * data will have been transferred.
970 *
971 * stop.error indicates a problem with the stop command. Data
972 * may have been transferred, or may still be transferring.
Pierre Ossman979ce722008-06-29 12:19:47 +0200973 */
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100974 if (brq.sbc.error || brq.cmd.error || brq.stop.error) {
975 switch (mmc_blk_cmd_recovery(card, req, &brq)) {
976 case ERR_RETRY:
977 if (retry++ < 5)
978 continue;
979 case ERR_ABORT:
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530980 case ERR_NOMEDIUM:
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100981 goto cmd_abort;
982 case ERR_CONTINUE:
983 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100984 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100985 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300986
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100987 /*
988 * Check for errors relating to the execution of the
989 * initial command - such as address errors. No data
990 * has been transferred.
991 */
992 if (brq.cmd.resp[0] & CMD_ERRORS) {
993 pr_err("%s: r/w command failed, status = %#x\n",
994 req->rq_disk->disk_name, brq.cmd.resp[0]);
995 goto cmd_abort;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500996 }
997
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100998 /*
999 * Everything else is either success, or a data error of some
1000 * kind. If it was a write, we may have transitioned to
1001 * program mode, which we have to wait for it to complete.
1002 */
David Brownell7213d172007-08-08 09:10:23 -07001003 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001004 u32 status;
Russell King2ed6d222006-09-24 10:46:43 +01001005 do {
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +01001006 int err = get_card_status(card, &status, 5);
Russell King2ed6d222006-09-24 10:46:43 +01001007 if (err) {
1008 printk(KERN_ERR "%s: error %d requesting status\n",
1009 req->rq_disk->disk_name, err);
1010 goto cmd_err;
1011 }
Pierre Ossmand198f102007-11-02 18:21:13 +01001012 /*
1013 * Some cards mishandle the status bits,
1014 * so make sure to check both the busy
1015 * indication and the card state.
1016 */
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +01001017 } while (!(status & R1_READY_FOR_DATA) ||
1018 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
Russell King2ed6d222006-09-24 10:46:43 +01001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001021 if (brq.data.error) {
Russell King - ARM Linux20803902011-06-20 20:10:49 +01001022 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001023 req->rq_disk->disk_name, brq.data.error,
1024 (unsigned)blk_rq_pos(req),
1025 (unsigned)blk_rq_sectors(req),
1026 brq.cmd.resp[0], brq.stop.resp[0]);
1027
Adrian Hunter6a79e392008-12-31 18:21:17 +01001028 if (rq_data_dir(req) == READ) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001029 if (brq.data.blocks > 1) {
1030 /* Redo read one sector at a time */
1031 pr_warning("%s: retrying using single block read\n",
1032 req->rq_disk->disk_name);
1033 disable_multi = 1;
1034 continue;
1035 }
1036
Adrian Hunter6a79e392008-12-31 18:21:17 +01001037 /*
1038 * After an error, we redo I/O one sector at a
1039 * time, so we only reach here after trying to
1040 * read a single sector.
1041 */
1042 spin_lock_irq(&md->lock);
1043 ret = __blk_end_request(req, -EIO, brq.data.blksz);
1044 spin_unlock_irq(&md->lock);
1045 continue;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001046 } else {
1047 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001048 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001049 }
Pierre Ossman979ce722008-06-29 12:19:47 +02001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * A block was successfully transferred.
1053 */
1054 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001055 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock_irq(&md->lock);
1057 } while (ret);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 1;
1060
1061 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001062 /*
1063 * If this is an SD card and we're writing, we can first
1064 * mark the known good sectors as ok.
1065 *
1066 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +02001067 * as reported by the controller (which might be less than
1068 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Adrian Hunter6a79e392008-12-31 18:21:17 +01001070 if (mmc_card_sd(card)) {
1071 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001072
Adrian Hunter6a79e392008-12-31 18:21:17 +01001073 blocks = mmc_sd_num_wr_blocks(card);
1074 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001075 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001076 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001077 spin_unlock_irq(&md->lock);
1078 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001079 } else {
1080 spin_lock_irq(&md->lock);
1081 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1082 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -07001083 }
1084
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001085 cmd_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_lock_irq(&md->lock);
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +05301087 if (mmc_card_removed(card))
1088 req->cmd_flags |= REQ_QUIET;
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001089 while (ret)
1090 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 spin_unlock_irq(&md->lock);
1092
1093 return 0;
1094}
1095
San Mehatc87f8d42009-07-30 08:21:19 -07001096static int
1097mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card);
1098
Adrian Hunterbd788c92010-08-11 14:17:47 -07001099static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1100{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001101 int ret;
1102 struct mmc_blk_data *md = mq->data;
1103 struct mmc_card *card = md->queue.card;
1104
San Mehatc87f8d42009-07-30 08:21:19 -07001105#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1106 if (mmc_bus_needs_resume(card->host)) {
1107 mmc_resume_bus(card->host);
1108 mmc_blk_set_blksize(md, card);
1109 }
1110#endif
1111
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001112 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001113 ret = mmc_blk_part_switch(card, md);
1114 if (ret) {
1115 ret = 0;
1116 goto out;
1117 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001118
Adrian Hunter49804542010-08-11 14:17:50 -07001119 if (req->cmd_flags & REQ_DISCARD) {
1120 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001121 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001122 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001123 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001124 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001125 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001126 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001127 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001128 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001129
Andrei Warkentin371a6892011-04-11 18:10:25 -05001130out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001131 mmc_release_host(card->host);
1132 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Russell Kinga6f6c962006-01-03 22:38:44 +00001135static inline int mmc_blk_readonly(struct mmc_card *card)
1136{
1137 return mmc_card_readonly(card) ||
1138 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1139}
1140
Andrei Warkentin371a6892011-04-11 18:10:25 -05001141static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1142 struct device *parent,
1143 sector_t size,
1144 bool default_ro,
1145 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct mmc_blk_data *md;
1148 int devidx, ret;
1149
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001150 devidx = find_first_zero_bit(dev_use, max_devices);
1151 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return ERR_PTR(-ENOSPC);
1153 __set_bit(devidx, dev_use);
1154
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001155 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001156 if (!md) {
1157 ret = -ENOMEM;
1158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001160
Russell Kinga6f6c962006-01-03 22:38:44 +00001161 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001162 * !subname implies we are creating main mmc_blk_data that will be
1163 * associated with mmc_card with mmc_set_drvdata. Due to device
1164 * partitions, devidx will not coincide with a per-physical card
1165 * index anymore so we keep track of a name index.
1166 */
1167 if (!subname) {
1168 md->name_idx = find_first_zero_bit(name_use, max_devices);
1169 __set_bit(md->name_idx, name_use);
1170 }
1171 else
1172 md->name_idx = ((struct mmc_blk_data *)
1173 dev_to_disk(parent)->private_data)->name_idx;
1174
1175 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001176 * Set the read-only status based on the supported commands
1177 * and the write protect switch.
1178 */
1179 md->read_only = mmc_blk_readonly(card);
1180
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001181 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001182 if (md->disk == NULL) {
1183 ret = -ENOMEM;
1184 goto err_kfree;
1185 }
1186
1187 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001188 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001189 md->usage = 1;
1190
Adrian Hunterd09408a2011-06-23 13:40:28 +03001191 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001192 if (ret)
1193 goto err_putdisk;
1194
Russell Kinga6f6c962006-01-03 22:38:44 +00001195 md->queue.issue_fn = mmc_blk_issue_rq;
1196 md->queue.data = md;
1197
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001198 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001199 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001200 md->disk->fops = &mmc_bdops;
1201 md->disk->private_data = md;
1202 md->disk->queue = md->queue.queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001203 md->disk->driverfs_dev = &card->dev;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001204 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Crossfa746fa2010-09-03 12:41:21 -07001205 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001206
1207 /*
1208 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1209 *
1210 * - be set for removable media with permanent block devices
1211 * - be unset for removable block devices with permanent media
1212 *
1213 * Since MMC block devices clearly fall under the second
1214 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1215 * should use the block device creation/destruction hotplug
1216 * messages to tell when the card is present.
1217 */
1218
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001219 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1220 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001221
Martin K. Petersene1defc42009-05-22 17:17:49 -04001222 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001223 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001224
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001225 if (mmc_host_cmd23(card->host)) {
1226 if (mmc_card_mmc(card) ||
1227 (mmc_card_sd(card) &&
1228 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1229 md->flags |= MMC_BLK_CMD23;
1230 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001231
1232 if (mmc_card_mmc(card) &&
1233 md->flags & MMC_BLK_CMD23 &&
1234 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1235 card->ext_csd.rel_sectors)) {
1236 md->flags |= MMC_BLK_REL_WR;
1237 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1238 }
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001241
1242 err_putdisk:
1243 put_disk(md->disk);
1244 err_kfree:
1245 kfree(md);
1246 out:
1247 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Andrei Warkentin371a6892011-04-11 18:10:25 -05001250static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1251{
1252 sector_t size;
1253 struct mmc_blk_data *md;
1254
1255 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1256 /*
1257 * The EXT_CSD sector count is in number or 512 byte
1258 * sectors.
1259 */
1260 size = card->ext_csd.sectors;
1261 } else {
1262 /*
1263 * The CSD capacity field is in units of read_blkbits.
1264 * set_capacity takes units of 512 bytes.
1265 */
1266 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1267 }
1268
1269 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1270 return md;
1271}
1272
1273static int mmc_blk_alloc_part(struct mmc_card *card,
1274 struct mmc_blk_data *md,
1275 unsigned int part_type,
1276 sector_t size,
1277 bool default_ro,
1278 const char *subname)
1279{
1280 char cap_str[10];
1281 struct mmc_blk_data *part_md;
1282
1283 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1284 subname);
1285 if (IS_ERR(part_md))
1286 return PTR_ERR(part_md);
1287 part_md->part_type = part_type;
1288 list_add(&part_md->part, &md->part);
1289
1290 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1291 cap_str, sizeof(cap_str));
1292 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1293 part_md->disk->disk_name, mmc_card_id(card),
1294 mmc_card_name(card), part_md->part_type, cap_str);
1295 return 0;
1296}
1297
1298static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1299{
1300 int ret = 0;
1301
1302 if (!mmc_card_mmc(card))
1303 return 0;
1304
1305 if (card->ext_csd.boot_size) {
1306 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1307 card->ext_csd.boot_size >> 9,
1308 true,
1309 "boot0");
1310 if (ret)
1311 return ret;
1312 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1313 card->ext_csd.boot_size >> 9,
1314 true,
1315 "boot1");
1316 if (ret)
1317 return ret;
1318 }
1319
1320 return ret;
1321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323static int
1324mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int err;
1327
Pierre Ossmanb8558852007-01-03 19:47:29 +01001328 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001329 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001330 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001333 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1334 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EINVAL;
1336 }
1337
1338 return 0;
1339}
1340
Andrei Warkentin371a6892011-04-11 18:10:25 -05001341static void mmc_blk_remove_req(struct mmc_blk_data *md)
1342{
1343 if (md) {
1344 if (md->disk->flags & GENHD_FL_UP) {
1345 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1346
1347 /* Stop new requests from getting into the queue */
1348 del_gendisk(md->disk);
1349 }
1350
1351 /* Then flush out any already in there */
1352 mmc_cleanup_queue(&md->queue);
1353 mmc_blk_put(md);
1354 }
1355}
1356
1357static void mmc_blk_remove_parts(struct mmc_card *card,
1358 struct mmc_blk_data *md)
1359{
1360 struct list_head *pos, *q;
1361 struct mmc_blk_data *part_md;
1362
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001363 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001364 list_for_each_safe(pos, q, &md->part) {
1365 part_md = list_entry(pos, struct mmc_blk_data, part);
1366 list_del(pos);
1367 mmc_blk_remove_req(part_md);
1368 }
1369}
1370
1371static int mmc_add_disk(struct mmc_blk_data *md)
1372{
1373 int ret;
1374
1375 add_disk(md->disk);
1376 md->force_ro.show = force_ro_show;
1377 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301378 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001379 md->force_ro.attr.name = "force_ro";
1380 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1381 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1382 if (ret)
1383 del_gendisk(md->disk);
1384
1385 return ret;
1386}
1387
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001388static const struct mmc_fixup blk_fixups[] =
1389{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001390 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1391 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1392 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1393 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1394 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001395
1396 /*
1397 * Some MMC cards experience performance degradation with CMD23
1398 * instead of CMD12-bounded multiblock transfers. For now we'll
1399 * black list what's bad...
1400 * - Certain Toshiba cards.
1401 *
1402 * N.B. This doesn't affect SD cards.
1403 */
1404 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1405 MMC_QUIRK_BLK_NO_CMD23),
1406 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1407 MMC_QUIRK_BLK_NO_CMD23),
1408 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1409 MMC_QUIRK_BLK_NO_CMD23),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001410 END_FIXUP
1411};
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static int mmc_blk_probe(struct mmc_card *card)
1414{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001415 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001417 char cap_str[10];
1418
Pierre Ossman912490d2005-05-21 10:27:02 +01001419 /*
1420 * Check that the card supports the command class(es) we need.
1421 */
1422 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -ENODEV;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 md = mmc_blk_alloc(card);
1426 if (IS_ERR(md))
1427 return PTR_ERR(md);
1428
1429 err = mmc_blk_set_blksize(md, card);
1430 if (err)
1431 goto out;
1432
Yi Li444122f2009-02-05 15:31:57 +08001433 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001434 cap_str, sizeof(cap_str));
1435 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001437 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Andrei Warkentin371a6892011-04-11 18:10:25 -05001439 if (mmc_blk_alloc_parts(card, md))
1440 goto out;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001443 mmc_fixup_device(card, blk_fixups);
1444
San Mehatc87f8d42009-07-30 08:21:19 -07001445#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1446 mmc_set_bus_resume_policy(card->host, 1);
1447#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001448 if (mmc_add_disk(md))
1449 goto out;
1450
1451 list_for_each_entry(part_md, &md->part, part) {
1452 if (mmc_add_disk(part_md))
1453 goto out;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456
1457 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001458 mmc_blk_remove_parts(card, md);
1459 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return err;
1461}
1462
1463static void mmc_blk_remove(struct mmc_card *card)
1464{
1465 struct mmc_blk_data *md = mmc_get_drvdata(card);
1466
Andrei Warkentin371a6892011-04-11 18:10:25 -05001467 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001468 mmc_claim_host(card->host);
1469 mmc_blk_part_switch(card, md);
1470 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001471 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 mmc_set_drvdata(card, NULL);
San Mehatc87f8d42009-07-30 08:21:19 -07001473#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1474 mmc_set_bus_resume_policy(card->host, 0);
1475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478#ifdef CONFIG_PM
Chuanxiao Dong72407e92011-08-24 14:00:41 +05301479static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001481 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct mmc_blk_data *md = mmc_get_drvdata(card);
1483
1484 if (md) {
1485 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001486 list_for_each_entry(part_md, &md->part, part) {
1487 mmc_queue_suspend(&part_md->queue);
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 return 0;
1491}
1492
1493static int mmc_blk_resume(struct mmc_card *card)
1494{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001495 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct mmc_blk_data *md = mmc_get_drvdata(card);
1497
1498 if (md) {
San Mehatc87f8d42009-07-30 08:21:19 -07001499#ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 mmc_blk_set_blksize(md, card);
San Mehatc87f8d42009-07-30 08:21:19 -07001501#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001502
1503 /*
1504 * Resume involves the card going into idle state,
1505 * so current partition is always the main one.
1506 */
1507 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001509 list_for_each_entry(part_md, &md->part, part) {
1510 mmc_queue_resume(&part_md->queue);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 return 0;
1514}
1515#else
1516#define mmc_blk_suspend NULL
1517#define mmc_blk_resume NULL
1518#endif
1519
1520static struct mmc_driver mmc_driver = {
1521 .drv = {
1522 .name = "mmcblk",
1523 },
1524 .probe = mmc_blk_probe,
1525 .remove = mmc_blk_remove,
1526 .suspend = mmc_blk_suspend,
1527 .resume = mmc_blk_resume,
1528};
1529
1530static int __init mmc_blk_init(void)
1531{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001532 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001534 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1535 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1536
1537 max_devices = 256 / perdev_minors;
1538
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001539 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1540 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001543 res = mmc_register_driver(&mmc_driver);
1544 if (res)
1545 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001547 return 0;
1548 out2:
1549 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 out:
1551 return res;
1552}
1553
1554static void __exit mmc_blk_exit(void)
1555{
1556 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001557 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560module_init(mmc_blk_init);
1561module_exit(mmc_blk_exit);
1562
1563MODULE_LICENSE("GPL");
1564MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1565