blob: a262b3a55030ed76eedf5a44758dafa00868d6ea [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
Andrei Warkentin371a6892011-04-11 18:10:25 -0500426static inline int mmc_blk_part_switch(struct mmc_card *card,
427 struct mmc_blk_data *md)
428{
429 int ret;
430 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
431 if (main_md->part_curr == md->part_type)
432 return 0;
433
434 if (mmc_card_mmc(card)) {
435 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
436 card->ext_csd.part_config |= md->part_type;
437
438 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
439 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
440 card->ext_csd.part_time);
441 if (ret)
442 return ret;
443}
444
445 main_md->part_curr = md->part_type;
446 return 0;
447}
448
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700449static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
450{
451 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100452 u32 result;
453 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700454
Chris Ball24f5b532011-04-13 23:49:45 -0400455 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400456 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400457 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700458 unsigned int timeout_us;
459
460 struct scatterlist sg;
461
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700462 cmd.opcode = MMC_APP_CMD;
463 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700464 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700465
466 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700467 if (err)
468 return (u32)-1;
469 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700470 return (u32)-1;
471
472 memset(&cmd, 0, sizeof(struct mmc_command));
473
474 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
475 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700476 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700477
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700478 data.timeout_ns = card->csd.tacc_ns * 100;
479 data.timeout_clks = card->csd.tacc_clks * 100;
480
481 timeout_us = data.timeout_ns / 1000;
482 timeout_us += data.timeout_clks * 1000 /
483 (card->host->ios.clock / 1000);
484
485 if (timeout_us > 100000) {
486 data.timeout_ns = 100000000;
487 data.timeout_clks = 0;
488 }
489
490 data.blksz = 4;
491 data.blocks = 1;
492 data.flags = MMC_DATA_READ;
493 data.sg = &sg;
494 data.sg_len = 1;
495
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700496 mrq.cmd = &cmd;
497 mrq.data = &data;
498
Ben Dooks051913d2009-06-08 23:33:57 +0100499 blocks = kmalloc(4, GFP_KERNEL);
500 if (!blocks)
501 return (u32)-1;
502
503 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700504
505 mmc_wait_for_req(card->host, &mrq);
506
Ben Dooks051913d2009-06-08 23:33:57 +0100507 result = ntohl(*blocks);
508 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700509
Ben Dooks051913d2009-06-08 23:33:57 +0100510 if (cmd.error || data.error)
511 result = (u32)-1;
512
513 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700514}
515
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100516static int send_stop(struct mmc_card *card, u32 *status)
517{
518 struct mmc_command cmd = {0};
519 int err;
520
521 cmd.opcode = MMC_STOP_TRANSMISSION;
522 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
523 err = mmc_wait_for_cmd(card->host, &cmd, 5);
524 if (err == 0)
525 *status = cmd.resp[0];
526 return err;
527}
528
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100529static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300530{
Chris Ball1278dba2011-04-13 23:40:30 -0400531 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300532 int err;
533
Adrian Hunter504f1912008-10-16 12:55:25 +0300534 cmd.opcode = MMC_SEND_STATUS;
535 if (!mmc_host_is_spi(card->host))
536 cmd.arg = card->rca << 16;
537 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100538 err = mmc_wait_for_cmd(card->host, &cmd, retries);
539 if (err == 0)
540 *status = cmd.resp[0];
541 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300542}
543
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530544#define ERR_NOMEDIUM 3
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100545#define ERR_RETRY 2
546#define ERR_ABORT 1
547#define ERR_CONTINUE 0
548
549static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
550 bool status_valid, u32 status)
551{
552 switch (error) {
553 case -EILSEQ:
554 /* response crc error, retry the r/w cmd */
555 pr_err("%s: %s sending %s command, card status %#x\n",
556 req->rq_disk->disk_name, "response CRC error",
557 name, status);
558 return ERR_RETRY;
559
560 case -ETIMEDOUT:
561 pr_err("%s: %s sending %s command, card status %#x\n",
562 req->rq_disk->disk_name, "timed out", name, status);
563
564 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallf4104732011-10-25 18:16:58 -0700565 if (!status_valid) {
566 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100567 return ERR_RETRY;
Ken Sumrallf4104732011-10-25 18:16:58 -0700568 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100569 /*
570 * If it was a r/w cmd crc error, or illegal command
571 * (eg, issued in wrong state) then retry - we should
572 * have corrected the state problem above.
573 */
Ken Sumrallf4104732011-10-25 18:16:58 -0700574 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
575 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100576 return ERR_RETRY;
Ken Sumrallf4104732011-10-25 18:16:58 -0700577 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100578
579 /* Otherwise abort the command */
Ken Sumrallf4104732011-10-25 18:16:58 -0700580 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100581 return ERR_ABORT;
582
583 default:
584 /* We don't understand the error code the driver gave us */
585 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
586 req->rq_disk->disk_name, error, status);
587 return ERR_ABORT;
588 }
589}
590
591/*
592 * Initial r/w and stop cmd error recovery.
593 * We don't know whether the card received the r/w cmd or not, so try to
594 * restore things back to a sane state. Essentially, we do this as follows:
595 * - Obtain card status. If the first attempt to obtain card status fails,
596 * the status word will reflect the failed status cmd, not the failed
597 * r/w cmd. If we fail to obtain card status, it suggests we can no
598 * longer communicate with the card.
599 * - Check the card state. If the card received the cmd but there was a
600 * transient problem with the response, it might still be in a data transfer
601 * mode. Try to send it a stop command. If this fails, we can't recover.
602 * - If the r/w cmd failed due to a response CRC error, it was probably
603 * transient, so retry the cmd.
604 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
605 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
606 * illegal cmd, retry.
607 * Otherwise we don't understand what happened, so abort.
608 */
609static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
610 struct mmc_blk_request *brq)
611{
612 bool prev_cmd_status_valid = true;
613 u32 status, stop_status = 0;
614 int err, retry;
615
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530616 if (mmc_card_removed(card))
617 return ERR_NOMEDIUM;
618
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100619 /*
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. */
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530635 if (err) {
636 /* Check if the card is removed */
637 if (mmc_detect_card_removed(card->host))
638 return ERR_NOMEDIUM;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100639 return ERR_ABORT;
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530640 }
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100641
642 /*
643 * Check the current card state. If it is in some data transfer
644 * mode, tell it to stop (and hopefully transition back to TRAN.)
645 */
646 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
647 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
648 err = send_stop(card, &stop_status);
649 if (err)
650 pr_err("%s: error %d sending stop command\n",
651 req->rq_disk->disk_name, err);
652
653 /*
654 * If the stop cmd also timed out, the card is probably
655 * not present, so abort. Other errors are bad news too.
656 */
657 if (err)
658 return ERR_ABORT;
659 }
660
661 /* Check for set block count errors */
662 if (brq->sbc.error)
663 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
664 prev_cmd_status_valid, status);
665
666 /* Check for r/w command errors */
667 if (brq->cmd.error)
668 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
669 prev_cmd_status_valid, status);
670
671 /* Now for stop errors. These aren't fatal to the transfer. */
672 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
673 req->rq_disk->disk_name, brq->stop.error,
674 brq->cmd.resp[0], status);
675
676 /*
677 * Subsitute in our own stop status as this will give the error
678 * state which happened during the execution of the r/w command.
679 */
680 if (stop_status) {
681 brq->stop.resp[0] = stop_status;
682 brq->stop.error = 0;
683 }
684 return ERR_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Adrian Hunterbd788c92010-08-11 14:17:47 -0700687static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
688{
689 struct mmc_blk_data *md = mq->data;
690 struct mmc_card *card = md->queue.card;
691 unsigned int from, nr, arg;
692 int err = 0;
693
Adrian Hunterbd788c92010-08-11 14:17:47 -0700694 if (!mmc_can_erase(card)) {
695 err = -EOPNOTSUPP;
696 goto out;
697 }
698
699 from = blk_rq_pos(req);
700 nr = blk_rq_sectors(req);
701
702 if (mmc_can_trim(card))
703 arg = MMC_TRIM_ARG;
704 else
705 arg = MMC_ERASE_ARG;
706
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500707 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
708 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
709 INAND_CMD38_ARG_EXT_CSD,
710 arg == MMC_TRIM_ARG ?
711 INAND_CMD38_ARG_TRIM :
712 INAND_CMD38_ARG_ERASE,
713 0);
714 if (err)
715 goto out;
716 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700717 err = mmc_erase(card, from, nr, arg);
718out:
719 spin_lock_irq(&md->lock);
720 __blk_end_request(req, err, blk_rq_bytes(req));
721 spin_unlock_irq(&md->lock);
722
Adrian Hunterbd788c92010-08-11 14:17:47 -0700723 return err ? 0 : 1;
724}
725
Adrian Hunter49804542010-08-11 14:17:50 -0700726static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
727 struct request *req)
728{
729 struct mmc_blk_data *md = mq->data;
730 struct mmc_card *card = md->queue.card;
731 unsigned int from, nr, arg;
732 int err = 0;
733
Adrian Hunter49804542010-08-11 14:17:50 -0700734 if (!mmc_can_secure_erase_trim(card)) {
735 err = -EOPNOTSUPP;
736 goto out;
737 }
738
739 from = blk_rq_pos(req);
740 nr = blk_rq_sectors(req);
741
742 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
743 arg = MMC_SECURE_TRIM1_ARG;
744 else
745 arg = MMC_SECURE_ERASE_ARG;
746
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500747 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
748 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
749 INAND_CMD38_ARG_EXT_CSD,
750 arg == MMC_SECURE_TRIM1_ARG ?
751 INAND_CMD38_ARG_SECTRIM1 :
752 INAND_CMD38_ARG_SECERASE,
753 0);
754 if (err)
755 goto out;
756 }
Adrian Hunter49804542010-08-11 14:17:50 -0700757 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500758 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
759 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
760 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
761 INAND_CMD38_ARG_EXT_CSD,
762 INAND_CMD38_ARG_SECTRIM2,
763 0);
764 if (err)
765 goto out;
766 }
Adrian Hunter49804542010-08-11 14:17:50 -0700767 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500768 }
Adrian Hunter49804542010-08-11 14:17:50 -0700769out:
770 spin_lock_irq(&md->lock);
771 __blk_end_request(req, err, blk_rq_bytes(req));
772 spin_unlock_irq(&md->lock);
773
Adrian Hunter49804542010-08-11 14:17:50 -0700774 return err ? 0 : 1;
775}
776
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500777static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
778{
779 struct mmc_blk_data *md = mq->data;
780
781 /*
782 * No-op, only service this because we need REQ_FUA for reliable
783 * writes.
784 */
785 spin_lock_irq(&md->lock);
786 __blk_end_request_all(req, 0);
787 spin_unlock_irq(&md->lock);
788
789 return 1;
790}
791
792/*
793 * Reformat current write as a reliable write, supporting
794 * both legacy and the enhanced reliable write MMC cards.
795 * In each transfer we'll handle only as much as a single
796 * reliable write can handle, thus finish the request in
797 * partial completions.
798 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500799static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
800 struct mmc_card *card,
801 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500802{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500803 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
804 /* Legacy mode imposes restrictions on transfers. */
805 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
806 brq->data.blocks = 1;
807
808 if (brq->data.blocks > card->ext_csd.rel_sectors)
809 brq->data.blocks = card->ext_csd.rel_sectors;
810 else if (brq->data.blocks < card->ext_csd.rel_sectors)
811 brq->data.blocks = 1;
812 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500813}
814
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100815#define CMD_ERRORS \
816 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
817 R1_ADDRESS_ERROR | /* Misaligned address */ \
818 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
819 R1_WP_VIOLATION | /* Tried to write to protected block */ \
820 R1_CC_ERROR | /* Card controller error */ \
821 R1_ERROR) /* General/unknown error */
822
Adrian Hunterbd788c92010-08-11 14:17:47 -0700823static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct mmc_blk_data *md = mq->data;
826 struct mmc_card *card = md->queue.card;
Per Forlincb86e7b2011-07-09 17:12:36 -0400827 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100828 int ret = 1, disable_multi = 0, retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500830 /*
831 * Reliable writes are used to implement Forced Unit Access and
832 * REQ_META accesses, and are supported only on MMCs.
833 */
834 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
835 (req->cmd_flags & REQ_META)) &&
836 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500837 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 do {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100840 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Per Forlincb86e7b2011-07-09 17:12:36 -0400842 memset(brq, 0, sizeof(struct mmc_blk_request));
843 brq->mrq.cmd = &brq->cmd;
844 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Per Forlincb86e7b2011-07-09 17:12:36 -0400846 brq->cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800847 if (!mmc_card_blockaddr(card))
Per Forlincb86e7b2011-07-09 17:12:36 -0400848 brq->cmd.arg <<= 9;
849 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
850 brq->data.blksz = 512;
851 brq->stop.opcode = MMC_STOP_TRANSMISSION;
852 brq->stop.arg = 0;
853 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
854 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Adrian Hunter6a79e392008-12-31 18:21:17 +0100856 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200857 * The block layer doesn't support all sector count
858 * restrictions, so we need to be prepared for too big
859 * requests.
860 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400861 if (brq->data.blocks > card->host->max_blk_count)
862 brq->data.blocks = card->host->max_blk_count;
Pierre Ossman548d2de2009-04-10 17:52:57 +0200863
864 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100865 * After a read error, we redo the request one sector at a time
866 * in order to accurately determine which sectors can be read
867 * successfully.
868 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400869 if (disable_multi && brq->data.blocks > 1)
870 brq->data.blocks = 1;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100871
Per Forlincb86e7b2011-07-09 17:12:36 -0400872 if (brq->data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700873 /* SPI multiblock writes terminate using a special
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500874 * token, not a STOP_TRANSMISSION request.
David Brownell7213d172007-08-08 09:10:23 -0700875 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500876 if (!mmc_host_is_spi(card->host) ||
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500877 rq_data_dir(req) == READ)
Per Forlincb86e7b2011-07-09 17:12:36 -0400878 brq->mrq.stop = &brq->stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100879 readcmd = MMC_READ_MULTIPLE_BLOCK;
880 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000881 } else {
Per Forlincb86e7b2011-07-09 17:12:36 -0400882 brq->mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100883 readcmd = MMC_READ_SINGLE_BLOCK;
884 writecmd = MMC_WRITE_BLOCK;
885 }
Russell Kingdb53f282006-08-30 15:14:56 +0100886 if (rq_data_dir(req) == READ) {
Per Forlincb86e7b2011-07-09 17:12:36 -0400887 brq->cmd.opcode = readcmd;
888 brq->data.flags |= MMC_DATA_READ;
Russell Kingdb53f282006-08-30 15:14:56 +0100889 } else {
Per Forlincb86e7b2011-07-09 17:12:36 -0400890 brq->cmd.opcode = writecmd;
891 brq->data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500894 if (do_rel_wr)
Per Forlincb86e7b2011-07-09 17:12:36 -0400895 mmc_apply_rel_rw(brq, card, req);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500896
897 /*
898 * Pre-defined multi-block transfers are preferable to
899 * open ended-ones (and necessary for reliable writes).
900 * However, it is not sufficient to just send CMD23,
901 * and avoid the final CMD12, as on an error condition
902 * CMD12 (stop) needs to be sent anyway. This, coupled
903 * with Auto-CMD23 enhancements provided by some
904 * hosts, means that the complexity of dealing
905 * with this is best left to the host. If CMD23 is
906 * supported by card and host, we'll fill sbc in and let
907 * the host deal with handling it correctly. This means
908 * that for hosts that don't expose MMC_CAP_CMD23, no
909 * change of behavior will be observed.
910 *
911 * N.B: Some MMC cards experience perf degradation.
912 * We'll avoid using CMD23-bounded multiblock writes for
913 * these, while retaining features like reliable writes.
914 */
915
916 if ((md->flags & MMC_BLK_CMD23) &&
Per Forlincb86e7b2011-07-09 17:12:36 -0400917 mmc_op_multi(brq->cmd.opcode) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500918 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
Per Forlincb86e7b2011-07-09 17:12:36 -0400919 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
920 brq->sbc.arg = brq->data.blocks |
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500921 (do_rel_wr ? (1 << 31) : 0);
Per Forlincb86e7b2011-07-09 17:12:36 -0400922 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
923 brq->mrq.sbc = &brq->sbc;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500924 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500925
Per Forlincb86e7b2011-07-09 17:12:36 -0400926 mmc_set_data_timeout(&brq->data, card);
Pierre Ossmanb146d262007-07-24 19:16:54 +0200927
Per Forlincb86e7b2011-07-09 17:12:36 -0400928 brq->data.sg = mq->mqrq_cur->sg;
929 brq->data.sg_len = mmc_queue_map_sg(mq, mq->mqrq_cur);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200930
Adrian Hunter6a79e392008-12-31 18:21:17 +0100931 /*
932 * Adjust the sg list so it is the same size as the
933 * request.
934 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400935 if (brq->data.blocks != blk_rq_sectors(req)) {
936 int i, data_size = brq->data.blocks << 9;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100937 struct scatterlist *sg;
938
Per Forlincb86e7b2011-07-09 17:12:36 -0400939 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100940 data_size -= sg->length;
941 if (data_size <= 0) {
942 sg->length += data_size;
943 i++;
944 break;
945 }
946 }
Per Forlincb86e7b2011-07-09 17:12:36 -0400947 brq->data.sg_len = i;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100948 }
949
Per Forlincb86e7b2011-07-09 17:12:36 -0400950 mmc_queue_bounce_pre(mq->mqrq_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Per Forlincb86e7b2011-07-09 17:12:36 -0400952 mmc_wait_for_req(card->host, &brq->mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200953
Per Forlincb86e7b2011-07-09 17:12:36 -0400954 mmc_queue_bounce_post(mq->mqrq_cur);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200955
Pierre Ossman979ce722008-06-29 12:19:47 +0200956 /*
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100957 * sbc.error indicates a problem with the set block count
958 * command. No data will have been transferred.
959 *
960 * cmd.error indicates a problem with the r/w command. No
961 * data will have been transferred.
962 *
963 * stop.error indicates a problem with the stop command. Data
964 * may have been transferred, or may still be transferring.
Pierre Ossman979ce722008-06-29 12:19:47 +0200965 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400966 if (brq->sbc.error || brq->cmd.error || brq->stop.error) {
967 switch (mmc_blk_cmd_recovery(card, req, brq)) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100968 case ERR_RETRY:
969 if (retry++ < 5)
970 continue;
971 case ERR_ABORT:
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +0530972 case ERR_NOMEDIUM:
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100973 goto cmd_abort;
974 case ERR_CONTINUE:
975 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100976 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100977 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300978
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100979 /*
980 * Check for errors relating to the execution of the
981 * initial command - such as address errors. No data
982 * has been transferred.
983 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400984 if (brq->cmd.resp[0] & CMD_ERRORS) {
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100985 pr_err("%s: r/w command failed, status = %#x\n",
Per Forlincb86e7b2011-07-09 17:12:36 -0400986 req->rq_disk->disk_name, brq->cmd.resp[0]);
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100987 goto cmd_abort;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500988 }
989
Russell King - ARM Linux20803902011-06-20 20:10:49 +0100990 /*
991 * Everything else is either success, or a data error of some
992 * kind. If it was a write, we may have transitioned to
993 * program mode, which we have to wait for it to complete.
994 */
David Brownell7213d172007-08-08 09:10:23 -0700995 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +0100996 u32 status;
Russell King2ed6d222006-09-24 10:46:43 +0100997 do {
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +0100998 int err = get_card_status(card, &status, 5);
Russell King2ed6d222006-09-24 10:46:43 +0100999 if (err) {
1000 printk(KERN_ERR "%s: error %d requesting status\n",
1001 req->rq_disk->disk_name, err);
1002 goto cmd_err;
1003 }
Pierre Ossmand198f102007-11-02 18:21:13 +01001004 /*
1005 * Some cards mishandle the status bits,
1006 * so make sure to check both the busy
1007 * indication and the card state.
1008 */
Russell King - ARM Linux6be918e2011-06-20 20:10:08 +01001009 } while (!(status & R1_READY_FOR_DATA) ||
1010 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
Russell King2ed6d222006-09-24 10:46:43 +01001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Per Forlincb86e7b2011-07-09 17:12:36 -04001013 if (brq->data.error) {
Russell King - ARM Linux20803902011-06-20 20:10:49 +01001014 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
Per Forlincb86e7b2011-07-09 17:12:36 -04001015 req->rq_disk->disk_name, brq->data.error,
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001016 (unsigned)blk_rq_pos(req),
1017 (unsigned)blk_rq_sectors(req),
Per Forlincb86e7b2011-07-09 17:12:36 -04001018 brq->cmd.resp[0], brq->stop.resp[0]);
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001019
Adrian Hunter6a79e392008-12-31 18:21:17 +01001020 if (rq_data_dir(req) == READ) {
Per Forlincb86e7b2011-07-09 17:12:36 -04001021 if (brq->data.blocks > 1) {
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001022 /* Redo read one sector at a time */
1023 pr_warning("%s: retrying using single block read\n",
1024 req->rq_disk->disk_name);
1025 disable_multi = 1;
1026 continue;
1027 }
1028
Adrian Hunter6a79e392008-12-31 18:21:17 +01001029 /*
1030 * After an error, we redo I/O one sector at a
1031 * time, so we only reach here after trying to
1032 * read a single sector.
1033 */
1034 spin_lock_irq(&md->lock);
Per Forlincb86e7b2011-07-09 17:12:36 -04001035 ret = __blk_end_request(req, -EIO,
1036 brq->data.blksz);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001037 spin_unlock_irq(&md->lock);
1038 continue;
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001039 } else {
1040 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001041 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001042 }
Pierre Ossman979ce722008-06-29 12:19:47 +02001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * A block was successfully transferred.
1046 */
1047 spin_lock_irq(&md->lock);
Per Forlincb86e7b2011-07-09 17:12:36 -04001048 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_unlock_irq(&md->lock);
1050 } while (ret);
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 1;
1053
1054 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001055 /*
1056 * If this is an SD card and we're writing, we can first
1057 * mark the known good sectors as ok.
1058 *
1059 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +02001060 * as reported by the controller (which might be less than
1061 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Adrian Hunter6a79e392008-12-31 18:21:17 +01001063 if (mmc_card_sd(card)) {
1064 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001065
Adrian Hunter6a79e392008-12-31 18:21:17 +01001066 blocks = mmc_sd_num_wr_blocks(card);
1067 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001068 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001069 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001070 spin_unlock_irq(&md->lock);
1071 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001072 } else {
1073 spin_lock_irq(&md->lock);
Per Forlincb86e7b2011-07-09 17:12:36 -04001074 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001075 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -07001076 }
1077
Russell King - ARM Linux91d56b52011-06-20 20:10:28 +01001078 cmd_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_lock_irq(&md->lock);
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +05301080 if (mmc_card_removed(card))
1081 req->cmd_flags |= REQ_QUIET;
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001082 while (ret)
1083 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_unlock_irq(&md->lock);
1085
1086 return 0;
1087}
1088
San Mehatc87f8d42009-07-30 08:21:19 -07001089static int
1090mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card);
1091
Adrian Hunterbd788c92010-08-11 14:17:47 -07001092static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1093{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001094 int ret;
1095 struct mmc_blk_data *md = mq->data;
1096 struct mmc_card *card = md->queue.card;
1097
San Mehatc87f8d42009-07-30 08:21:19 -07001098#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1099 if (mmc_bus_needs_resume(card->host)) {
1100 mmc_resume_bus(card->host);
1101 mmc_blk_set_blksize(md, card);
1102 }
1103#endif
1104
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001105 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001106 ret = mmc_blk_part_switch(card, md);
1107 if (ret) {
1108 ret = 0;
1109 goto out;
1110 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001111
Adrian Hunter49804542010-08-11 14:17:50 -07001112 if (req->cmd_flags & REQ_DISCARD) {
1113 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001114 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001115 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001116 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001117 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001118 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001119 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001120 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001121 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001122
Andrei Warkentin371a6892011-04-11 18:10:25 -05001123out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001124 mmc_release_host(card->host);
1125 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Russell Kinga6f6c962006-01-03 22:38:44 +00001128static inline int mmc_blk_readonly(struct mmc_card *card)
1129{
1130 return mmc_card_readonly(card) ||
1131 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1132}
1133
Andrei Warkentin371a6892011-04-11 18:10:25 -05001134static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1135 struct device *parent,
1136 sector_t size,
1137 bool default_ro,
1138 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct mmc_blk_data *md;
1141 int devidx, ret;
1142
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001143 devidx = find_first_zero_bit(dev_use, max_devices);
1144 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return ERR_PTR(-ENOSPC);
1146 __set_bit(devidx, dev_use);
1147
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001148 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001149 if (!md) {
1150 ret = -ENOMEM;
1151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001153
Russell Kinga6f6c962006-01-03 22:38:44 +00001154 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001155 * !subname implies we are creating main mmc_blk_data that will be
1156 * associated with mmc_card with mmc_set_drvdata. Due to device
1157 * partitions, devidx will not coincide with a per-physical card
1158 * index anymore so we keep track of a name index.
1159 */
1160 if (!subname) {
1161 md->name_idx = find_first_zero_bit(name_use, max_devices);
1162 __set_bit(md->name_idx, name_use);
1163 }
1164 else
1165 md->name_idx = ((struct mmc_blk_data *)
1166 dev_to_disk(parent)->private_data)->name_idx;
1167
1168 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001169 * Set the read-only status based on the supported commands
1170 * and the write protect switch.
1171 */
1172 md->read_only = mmc_blk_readonly(card);
1173
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001174 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001175 if (md->disk == NULL) {
1176 ret = -ENOMEM;
1177 goto err_kfree;
1178 }
1179
1180 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001181 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001182 md->usage = 1;
1183
Adrian Hunterd09408a2011-06-23 13:40:28 +03001184 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001185 if (ret)
1186 goto err_putdisk;
1187
Russell Kinga6f6c962006-01-03 22:38:44 +00001188 md->queue.issue_fn = mmc_blk_issue_rq;
1189 md->queue.data = md;
1190
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001191 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001192 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001193 md->disk->fops = &mmc_bdops;
1194 md->disk->private_data = md;
1195 md->disk->queue = md->queue.queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 md->disk->driverfs_dev = &card->dev;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001197 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Crossfa746fa2010-09-03 12:41:21 -07001198 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001199
1200 /*
1201 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1202 *
1203 * - be set for removable media with permanent block devices
1204 * - be unset for removable block devices with permanent media
1205 *
1206 * Since MMC block devices clearly fall under the second
1207 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1208 * should use the block device creation/destruction hotplug
1209 * messages to tell when the card is present.
1210 */
1211
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001212 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1213 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001214
Martin K. Petersene1defc42009-05-22 17:17:49 -04001215 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001216 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001217
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001218 if (mmc_host_cmd23(card->host)) {
1219 if (mmc_card_mmc(card) ||
1220 (mmc_card_sd(card) &&
1221 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1222 md->flags |= MMC_BLK_CMD23;
1223 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001224
1225 if (mmc_card_mmc(card) &&
1226 md->flags & MMC_BLK_CMD23 &&
1227 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1228 card->ext_csd.rel_sectors)) {
1229 md->flags |= MMC_BLK_REL_WR;
1230 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001234
1235 err_putdisk:
1236 put_disk(md->disk);
1237 err_kfree:
1238 kfree(md);
1239 out:
1240 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Andrei Warkentin371a6892011-04-11 18:10:25 -05001243static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1244{
1245 sector_t size;
1246 struct mmc_blk_data *md;
1247
1248 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1249 /*
1250 * The EXT_CSD sector count is in number or 512 byte
1251 * sectors.
1252 */
1253 size = card->ext_csd.sectors;
1254 } else {
1255 /*
1256 * The CSD capacity field is in units of read_blkbits.
1257 * set_capacity takes units of 512 bytes.
1258 */
1259 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1260 }
1261
1262 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1263 return md;
1264}
1265
1266static int mmc_blk_alloc_part(struct mmc_card *card,
1267 struct mmc_blk_data *md,
1268 unsigned int part_type,
1269 sector_t size,
1270 bool default_ro,
1271 const char *subname)
1272{
1273 char cap_str[10];
1274 struct mmc_blk_data *part_md;
1275
1276 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1277 subname);
1278 if (IS_ERR(part_md))
1279 return PTR_ERR(part_md);
1280 part_md->part_type = part_type;
1281 list_add(&part_md->part, &md->part);
1282
1283 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1284 cap_str, sizeof(cap_str));
1285 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1286 part_md->disk->disk_name, mmc_card_id(card),
1287 mmc_card_name(card), part_md->part_type, cap_str);
1288 return 0;
1289}
1290
1291static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1292{
1293 int ret = 0;
1294
1295 if (!mmc_card_mmc(card))
1296 return 0;
1297
1298 if (card->ext_csd.boot_size) {
1299 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1300 card->ext_csd.boot_size >> 9,
1301 true,
1302 "boot0");
1303 if (ret)
1304 return ret;
1305 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1306 card->ext_csd.boot_size >> 9,
1307 true,
1308 "boot1");
1309 if (ret)
1310 return ret;
1311 }
1312
1313 return ret;
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316static int
1317mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320
Pierre Ossmanb8558852007-01-03 19:47:29 +01001321 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001322 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001323 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001326 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1327 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EINVAL;
1329 }
1330
1331 return 0;
1332}
1333
Andrei Warkentin371a6892011-04-11 18:10:25 -05001334static void mmc_blk_remove_req(struct mmc_blk_data *md)
1335{
1336 if (md) {
1337 if (md->disk->flags & GENHD_FL_UP) {
1338 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1339
1340 /* Stop new requests from getting into the queue */
1341 del_gendisk(md->disk);
1342 }
1343
1344 /* Then flush out any already in there */
1345 mmc_cleanup_queue(&md->queue);
1346 mmc_blk_put(md);
1347 }
1348}
1349
1350static void mmc_blk_remove_parts(struct mmc_card *card,
1351 struct mmc_blk_data *md)
1352{
1353 struct list_head *pos, *q;
1354 struct mmc_blk_data *part_md;
1355
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001356 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001357 list_for_each_safe(pos, q, &md->part) {
1358 part_md = list_entry(pos, struct mmc_blk_data, part);
1359 list_del(pos);
1360 mmc_blk_remove_req(part_md);
1361 }
1362}
1363
1364static int mmc_add_disk(struct mmc_blk_data *md)
1365{
1366 int ret;
1367
1368 add_disk(md->disk);
1369 md->force_ro.show = force_ro_show;
1370 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301371 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001372 md->force_ro.attr.name = "force_ro";
1373 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1374 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1375 if (ret)
1376 del_gendisk(md->disk);
1377
1378 return ret;
1379}
1380
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001381static const struct mmc_fixup blk_fixups[] =
1382{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001383 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1384 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1385 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1386 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1387 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001388
1389 /*
1390 * Some MMC cards experience performance degradation with CMD23
1391 * instead of CMD12-bounded multiblock transfers. For now we'll
1392 * black list what's bad...
1393 * - Certain Toshiba cards.
1394 *
1395 * N.B. This doesn't affect SD cards.
1396 */
1397 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1398 MMC_QUIRK_BLK_NO_CMD23),
1399 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1400 MMC_QUIRK_BLK_NO_CMD23),
1401 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1402 MMC_QUIRK_BLK_NO_CMD23),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001403 END_FIXUP
1404};
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406static int mmc_blk_probe(struct mmc_card *card)
1407{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001408 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001410 char cap_str[10];
1411
Pierre Ossman912490d2005-05-21 10:27:02 +01001412 /*
1413 * Check that the card supports the command class(es) we need.
1414 */
1415 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -ENODEV;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 md = mmc_blk_alloc(card);
1419 if (IS_ERR(md))
1420 return PTR_ERR(md);
1421
1422 err = mmc_blk_set_blksize(md, card);
1423 if (err)
1424 goto out;
1425
Yi Li444122f2009-02-05 15:31:57 +08001426 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001427 cap_str, sizeof(cap_str));
1428 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001430 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Andrei Warkentin371a6892011-04-11 18:10:25 -05001432 if (mmc_blk_alloc_parts(card, md))
1433 goto out;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001436 mmc_fixup_device(card, blk_fixups);
1437
San Mehatc87f8d42009-07-30 08:21:19 -07001438#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1439 mmc_set_bus_resume_policy(card->host, 1);
1440#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001441 if (mmc_add_disk(md))
1442 goto out;
1443
1444 list_for_each_entry(part_md, &md->part, part) {
1445 if (mmc_add_disk(part_md))
1446 goto out;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 0;
1449
1450 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001451 mmc_blk_remove_parts(card, md);
1452 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return err;
1454}
1455
1456static void mmc_blk_remove(struct mmc_card *card)
1457{
1458 struct mmc_blk_data *md = mmc_get_drvdata(card);
1459
Andrei Warkentin371a6892011-04-11 18:10:25 -05001460 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001461 mmc_claim_host(card->host);
1462 mmc_blk_part_switch(card, md);
1463 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001464 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 mmc_set_drvdata(card, NULL);
San Mehatc87f8d42009-07-30 08:21:19 -07001466#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1467 mmc_set_bus_resume_policy(card->host, 0);
1468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471#ifdef CONFIG_PM
Chuanxiao Dong72407e92011-08-24 14:00:41 +05301472static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001474 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct mmc_blk_data *md = mmc_get_drvdata(card);
1476
1477 if (md) {
1478 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001479 list_for_each_entry(part_md, &md->part, part) {
1480 mmc_queue_suspend(&part_md->queue);
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483 return 0;
1484}
1485
1486static int mmc_blk_resume(struct mmc_card *card)
1487{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001488 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct mmc_blk_data *md = mmc_get_drvdata(card);
1490
1491 if (md) {
San Mehatc87f8d42009-07-30 08:21:19 -07001492#ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 mmc_blk_set_blksize(md, card);
San Mehatc87f8d42009-07-30 08:21:19 -07001494#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001495
1496 /*
1497 * Resume involves the card going into idle state,
1498 * so current partition is always the main one.
1499 */
1500 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001502 list_for_each_entry(part_md, &md->part, part) {
1503 mmc_queue_resume(&part_md->queue);
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 return 0;
1507}
1508#else
1509#define mmc_blk_suspend NULL
1510#define mmc_blk_resume NULL
1511#endif
1512
1513static struct mmc_driver mmc_driver = {
1514 .drv = {
1515 .name = "mmcblk",
1516 },
1517 .probe = mmc_blk_probe,
1518 .remove = mmc_blk_remove,
1519 .suspend = mmc_blk_suspend,
1520 .resume = mmc_blk_resume,
1521};
1522
1523static int __init mmc_blk_init(void)
1524{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001525 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001527 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1528 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1529
1530 max_devices = 256 / perdev_minors;
1531
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001532 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1533 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001536 res = mmc_register_driver(&mmc_driver);
1537 if (res)
1538 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001540 return 0;
1541 out2:
1542 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 out:
1544 return res;
1545}
1546
1547static void __exit mmc_blk_exit(void)
1548{
1549 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001550 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553module_init(mmc_blk_init);
1554module_exit(mmc_blk_exit);
1555
1556MODULE_LICENSE("GPL");
1557MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1558