blob: 88bcc4e0be2191433d21a59a897fdabd30b661b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
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{
129 int devmaj = MAJOR(disk_devt(disk));
130 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
131
132 if (!devmaj)
133 devidx = disk->first_minor / perdev_minors;
134 return devidx;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static void mmc_blk_put(struct mmc_blk_data *md)
138{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000139 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 md->usage--;
141 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800143 blk_cleanup_queue(md->queue.queue);
144
David Woodhouse1dff3142007-11-21 18:45:12 +0100145 __clear_bit(devidx, dev_use);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 kfree(md);
149 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000150 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Andrei Warkentin371a6892011-04-11 18:10:25 -0500153static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
154 char *buf)
155{
156 int ret;
157 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
158
159 ret = snprintf(buf, PAGE_SIZE, "%d",
160 get_disk_ro(dev_to_disk(dev)) ^
161 md->read_only);
162 mmc_blk_put(md);
163 return ret;
164}
165
166static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
168{
169 int ret;
170 char *end;
171 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 unsigned long set = simple_strtoul(buf, &end, 0);
173 if (end == buf) {
174 ret = -EINVAL;
175 goto out;
176 }
177
178 set_disk_ro(dev_to_disk(dev), set || md->read_only);
179 ret = count;
180out:
181 mmc_blk_put(md);
182 return ret;
183}
184
Al Viroa5a15612008-03-02 10:33:30 -0500185static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Al Viroa5a15612008-03-02 10:33:30 -0500187 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int ret = -ENXIO;
189
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200190 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (md) {
192 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500193 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700195
Al Viroa5a15612008-03-02 10:33:30 -0500196 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700197 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700198 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200201 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return ret;
204}
205
Al Viroa5a15612008-03-02 10:33:30 -0500206static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Al Viroa5a15612008-03-02 10:33:30 -0500208 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200210 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200212 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214}
215
216static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800217mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800219 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
220 geo->heads = 4;
221 geo->sectors = 16;
222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
John Calixtocb87ea22011-04-26 18:56:29 -0400225struct mmc_blk_ioc_data {
226 struct mmc_ioc_cmd ic;
227 unsigned char *buf;
228 u64 buf_bytes;
229};
230
231static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232 struct mmc_ioc_cmd __user *user)
233{
234 struct mmc_blk_ioc_data *idata;
235 int err;
236
237 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
238 if (!idata) {
239 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400240 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400241 }
242
243 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
244 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400245 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400246 }
247
248 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
250 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400251 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400252 }
253
254 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
255 if (!idata->buf) {
256 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400257 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400258 }
259
260 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
261 idata->ic.data_ptr, idata->buf_bytes)) {
262 err = -EFAULT;
263 goto copy_err;
264 }
265
266 return idata;
267
268copy_err:
269 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400270idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400271 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400272out:
John Calixtocb87ea22011-04-26 18:56:29 -0400273 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400274}
275
276static int mmc_blk_ioctl_cmd(struct block_device *bdev,
277 struct mmc_ioc_cmd __user *ic_ptr)
278{
279 struct mmc_blk_ioc_data *idata;
280 struct mmc_blk_data *md;
281 struct mmc_card *card;
282 struct mmc_command cmd = {0};
283 struct mmc_data data = {0};
284 struct mmc_request mrq = {0};
285 struct scatterlist sg;
286 int err;
287
288 /*
289 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
290 * whole block device, not on a partition. This prevents overspray
291 * between sibling partitions.
292 */
293 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
294 return -EPERM;
295
296 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
297 if (IS_ERR(idata))
298 return PTR_ERR(idata);
299
300 cmd.opcode = idata->ic.opcode;
301 cmd.arg = idata->ic.arg;
302 cmd.flags = idata->ic.flags;
303
304 data.sg = &sg;
305 data.sg_len = 1;
306 data.blksz = idata->ic.blksz;
307 data.blocks = idata->ic.blocks;
308
309 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
310
311 if (idata->ic.write_flag)
312 data.flags = MMC_DATA_WRITE;
313 else
314 data.flags = MMC_DATA_READ;
315
316 mrq.cmd = &cmd;
317 mrq.data = &data;
318
319 md = mmc_blk_get(bdev->bd_disk);
320 if (!md) {
321 err = -EINVAL;
322 goto cmd_done;
323 }
324
325 card = md->queue.card;
326 if (IS_ERR(card)) {
327 err = PTR_ERR(card);
328 goto cmd_done;
329 }
330
331 mmc_claim_host(card->host);
332
333 if (idata->ic.is_acmd) {
334 err = mmc_app_cmd(card->host, card);
335 if (err)
336 goto cmd_rel_host;
337 }
338
339 /* data.flags must already be set before doing this. */
340 mmc_set_data_timeout(&data, card);
341 /* Allow overriding the timeout_ns for empirical tuning. */
342 if (idata->ic.data_timeout_ns)
343 data.timeout_ns = idata->ic.data_timeout_ns;
344
345 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
346 /*
347 * Pretend this is a data transfer and rely on the host driver
348 * to compute timeout. When all host drivers support
349 * cmd.cmd_timeout for R1B, this can be changed to:
350 *
351 * mrq.data = NULL;
352 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
353 */
354 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
355 }
356
357 mmc_wait_for_req(card->host, &mrq);
358
359 if (cmd.error) {
360 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
361 __func__, cmd.error);
362 err = cmd.error;
363 goto cmd_rel_host;
364 }
365 if (data.error) {
366 dev_err(mmc_dev(card->host), "%s: data error %d\n",
367 __func__, data.error);
368 err = data.error;
369 goto cmd_rel_host;
370 }
371
372 /*
373 * According to the SD specs, some commands require a delay after
374 * issuing the command.
375 */
376 if (idata->ic.postsleep_min_us)
377 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
378
379 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
380 err = -EFAULT;
381 goto cmd_rel_host;
382 }
383
384 if (!idata->ic.write_flag) {
385 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
386 idata->buf, idata->buf_bytes)) {
387 err = -EFAULT;
388 goto cmd_rel_host;
389 }
390 }
391
392cmd_rel_host:
393 mmc_release_host(card->host);
394
395cmd_done:
396 mmc_blk_put(md);
397 kfree(idata->buf);
398 kfree(idata);
399 return err;
400}
401
402static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
403 unsigned int cmd, unsigned long arg)
404{
405 int ret = -EINVAL;
406 if (cmd == MMC_IOC_CMD)
407 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
408 return ret;
409}
410
411#ifdef CONFIG_COMPAT
412static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
413 unsigned int cmd, unsigned long arg)
414{
415 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
416}
417#endif
418
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700419static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500420 .open = mmc_blk_open,
421 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800422 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400424 .ioctl = mmc_blk_ioctl,
425#ifdef CONFIG_COMPAT
426 .compat_ioctl = mmc_blk_compat_ioctl,
427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
Andrei Warkentin371a6892011-04-11 18:10:25 -0500430static inline int mmc_blk_part_switch(struct mmc_card *card,
431 struct mmc_blk_data *md)
432{
433 int ret;
434 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
435 if (main_md->part_curr == md->part_type)
436 return 0;
437
438 if (mmc_card_mmc(card)) {
439 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
440 card->ext_csd.part_config |= md->part_type;
441
442 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
443 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
444 card->ext_csd.part_time);
445 if (ret)
446 return ret;
447}
448
449 main_md->part_curr = md->part_type;
450 return 0;
451}
452
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700453static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
454{
455 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100456 u32 result;
457 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700458
Chris Ball24f5b532011-04-13 23:49:45 -0400459 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400460 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400461 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700462 unsigned int timeout_us;
463
464 struct scatterlist sg;
465
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700466 cmd.opcode = MMC_APP_CMD;
467 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700468 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700469
470 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700471 if (err)
472 return (u32)-1;
473 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700474 return (u32)-1;
475
476 memset(&cmd, 0, sizeof(struct mmc_command));
477
478 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
479 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700480 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700481
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700482 data.timeout_ns = card->csd.tacc_ns * 100;
483 data.timeout_clks = card->csd.tacc_clks * 100;
484
485 timeout_us = data.timeout_ns / 1000;
486 timeout_us += data.timeout_clks * 1000 /
487 (card->host->ios.clock / 1000);
488
489 if (timeout_us > 100000) {
490 data.timeout_ns = 100000000;
491 data.timeout_clks = 0;
492 }
493
494 data.blksz = 4;
495 data.blocks = 1;
496 data.flags = MMC_DATA_READ;
497 data.sg = &sg;
498 data.sg_len = 1;
499
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700500 mrq.cmd = &cmd;
501 mrq.data = &data;
502
Ben Dooks051913d2009-06-08 23:33:57 +0100503 blocks = kmalloc(4, GFP_KERNEL);
504 if (!blocks)
505 return (u32)-1;
506
507 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700508
509 mmc_wait_for_req(card->host, &mrq);
510
Ben Dooks051913d2009-06-08 23:33:57 +0100511 result = ntohl(*blocks);
512 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700513
Ben Dooks051913d2009-06-08 23:33:57 +0100514 if (cmd.error || data.error)
515 result = (u32)-1;
516
517 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700518}
519
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100520static int send_stop(struct mmc_card *card, u32 *status)
521{
522 struct mmc_command cmd = {0};
523 int err;
524
525 cmd.opcode = MMC_STOP_TRANSMISSION;
526 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
527 err = mmc_wait_for_cmd(card->host, &cmd, 5);
528 if (err == 0)
529 *status = cmd.resp[0];
530 return err;
531}
532
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100533static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300534{
Chris Ball1278dba2011-04-13 23:40:30 -0400535 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300536 int err;
537
Adrian Hunter504f1912008-10-16 12:55:25 +0300538 cmd.opcode = MMC_SEND_STATUS;
539 if (!mmc_host_is_spi(card->host))
540 cmd.arg = card->rca << 16;
541 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100542 err = mmc_wait_for_cmd(card->host, &cmd, retries);
543 if (err == 0)
544 *status = cmd.resp[0];
545 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300546}
547
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100548#define ERR_RETRY 2
549#define ERR_ABORT 1
550#define ERR_CONTINUE 0
551
552static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
553 bool status_valid, u32 status)
554{
555 switch (error) {
556 case -EILSEQ:
557 /* response crc error, retry the r/w cmd */
558 pr_err("%s: %s sending %s command, card status %#x\n",
559 req->rq_disk->disk_name, "response CRC error",
560 name, status);
561 return ERR_RETRY;
562
563 case -ETIMEDOUT:
564 pr_err("%s: %s sending %s command, card status %#x\n",
565 req->rq_disk->disk_name, "timed out", name, status);
566
567 /* If the status cmd initially failed, retry the r/w cmd */
568 if (!status_valid)
569 return ERR_RETRY;
570
571 /*
572 * If it was a r/w cmd crc error, or illegal command
573 * (eg, issued in wrong state) then retry - we should
574 * have corrected the state problem above.
575 */
576 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
577 return ERR_RETRY;
578
579 /* Otherwise abort the command */
580 return ERR_ABORT;
581
582 default:
583 /* We don't understand the error code the driver gave us */
584 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
585 req->rq_disk->disk_name, error, status);
586 return ERR_ABORT;
587 }
588}
589
590/*
591 * Initial r/w and stop cmd error recovery.
592 * We don't know whether the card received the r/w cmd or not, so try to
593 * restore things back to a sane state. Essentially, we do this as follows:
594 * - Obtain card status. If the first attempt to obtain card status fails,
595 * the status word will reflect the failed status cmd, not the failed
596 * r/w cmd. If we fail to obtain card status, it suggests we can no
597 * longer communicate with the card.
598 * - Check the card state. If the card received the cmd but there was a
599 * transient problem with the response, it might still be in a data transfer
600 * mode. Try to send it a stop command. If this fails, we can't recover.
601 * - If the r/w cmd failed due to a response CRC error, it was probably
602 * transient, so retry the cmd.
603 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
604 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
605 * illegal cmd, retry.
606 * Otherwise we don't understand what happened, so abort.
607 */
608static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
609 struct mmc_blk_request *brq)
610{
611 bool prev_cmd_status_valid = true;
612 u32 status, stop_status = 0;
613 int err, retry;
614
615 /*
616 * Try to get card status which indicates both the card state
617 * and why there was no response. If the first attempt fails,
618 * we can't be sure the returned status is for the r/w command.
619 */
620 for (retry = 2; retry >= 0; retry--) {
621 err = get_card_status(card, &status, 0);
622 if (!err)
623 break;
624
625 prev_cmd_status_valid = false;
626 pr_err("%s: error %d sending status command, %sing\n",
627 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
628 }
629
630 /* We couldn't get a response from the card. Give up. */
631 if (err)
632 return ERR_ABORT;
633
634 /*
635 * Check the current card state. If it is in some data transfer
636 * mode, tell it to stop (and hopefully transition back to TRAN.)
637 */
638 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
639 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
640 err = send_stop(card, &stop_status);
641 if (err)
642 pr_err("%s: error %d sending stop command\n",
643 req->rq_disk->disk_name, err);
644
645 /*
646 * If the stop cmd also timed out, the card is probably
647 * not present, so abort. Other errors are bad news too.
648 */
649 if (err)
650 return ERR_ABORT;
651 }
652
653 /* Check for set block count errors */
654 if (brq->sbc.error)
655 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
656 prev_cmd_status_valid, status);
657
658 /* Check for r/w command errors */
659 if (brq->cmd.error)
660 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
661 prev_cmd_status_valid, status);
662
663 /* Now for stop errors. These aren't fatal to the transfer. */
664 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
665 req->rq_disk->disk_name, brq->stop.error,
666 brq->cmd.resp[0], status);
667
668 /*
669 * Subsitute in our own stop status as this will give the error
670 * state which happened during the execution of the r/w command.
671 */
672 if (stop_status) {
673 brq->stop.resp[0] = stop_status;
674 brq->stop.error = 0;
675 }
676 return ERR_CONTINUE;
677}
678
Adrian Hunterbd788c92010-08-11 14:17:47 -0700679static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
680{
681 struct mmc_blk_data *md = mq->data;
682 struct mmc_card *card = md->queue.card;
683 unsigned int from, nr, arg;
684 int err = 0;
685
Adrian Hunterbd788c92010-08-11 14:17:47 -0700686 if (!mmc_can_erase(card)) {
687 err = -EOPNOTSUPP;
688 goto out;
689 }
690
691 from = blk_rq_pos(req);
692 nr = blk_rq_sectors(req);
693
694 if (mmc_can_trim(card))
695 arg = MMC_TRIM_ARG;
696 else
697 arg = MMC_ERASE_ARG;
698
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500699 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
700 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
701 INAND_CMD38_ARG_EXT_CSD,
702 arg == MMC_TRIM_ARG ?
703 INAND_CMD38_ARG_TRIM :
704 INAND_CMD38_ARG_ERASE,
705 0);
706 if (err)
707 goto out;
708 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700709 err = mmc_erase(card, from, nr, arg);
710out:
711 spin_lock_irq(&md->lock);
712 __blk_end_request(req, err, blk_rq_bytes(req));
713 spin_unlock_irq(&md->lock);
714
Adrian Hunterbd788c92010-08-11 14:17:47 -0700715 return err ? 0 : 1;
716}
717
Adrian Hunter49804542010-08-11 14:17:50 -0700718static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
719 struct request *req)
720{
721 struct mmc_blk_data *md = mq->data;
722 struct mmc_card *card = md->queue.card;
723 unsigned int from, nr, arg;
724 int err = 0;
725
Adrian Hunter49804542010-08-11 14:17:50 -0700726 if (!mmc_can_secure_erase_trim(card)) {
727 err = -EOPNOTSUPP;
728 goto out;
729 }
730
731 from = blk_rq_pos(req);
732 nr = blk_rq_sectors(req);
733
734 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
735 arg = MMC_SECURE_TRIM1_ARG;
736 else
737 arg = MMC_SECURE_ERASE_ARG;
738
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500739 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
740 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
741 INAND_CMD38_ARG_EXT_CSD,
742 arg == MMC_SECURE_TRIM1_ARG ?
743 INAND_CMD38_ARG_SECTRIM1 :
744 INAND_CMD38_ARG_SECERASE,
745 0);
746 if (err)
747 goto out;
748 }
Adrian Hunter49804542010-08-11 14:17:50 -0700749 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500750 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
751 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
752 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
753 INAND_CMD38_ARG_EXT_CSD,
754 INAND_CMD38_ARG_SECTRIM2,
755 0);
756 if (err)
757 goto out;
758 }
Adrian Hunter49804542010-08-11 14:17:50 -0700759 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500760 }
Adrian Hunter49804542010-08-11 14:17:50 -0700761out:
762 spin_lock_irq(&md->lock);
763 __blk_end_request(req, err, blk_rq_bytes(req));
764 spin_unlock_irq(&md->lock);
765
Adrian Hunter49804542010-08-11 14:17:50 -0700766 return err ? 0 : 1;
767}
768
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500769static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
770{
771 struct mmc_blk_data *md = mq->data;
772
773 /*
774 * No-op, only service this because we need REQ_FUA for reliable
775 * writes.
776 */
777 spin_lock_irq(&md->lock);
778 __blk_end_request_all(req, 0);
779 spin_unlock_irq(&md->lock);
780
781 return 1;
782}
783
784/*
785 * Reformat current write as a reliable write, supporting
786 * both legacy and the enhanced reliable write MMC cards.
787 * In each transfer we'll handle only as much as a single
788 * reliable write can handle, thus finish the request in
789 * partial completions.
790 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500791static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
792 struct mmc_card *card,
793 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500794{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500795 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
796 /* Legacy mode imposes restrictions on transfers. */
797 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
798 brq->data.blocks = 1;
799
800 if (brq->data.blocks > card->ext_csd.rel_sectors)
801 brq->data.blocks = card->ext_csd.rel_sectors;
802 else if (brq->data.blocks < card->ext_csd.rel_sectors)
803 brq->data.blocks = 1;
804 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500805}
806
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +0100807#define CMD_ERRORS \
808 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
809 R1_ADDRESS_ERROR | /* Misaligned address */ \
810 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
811 R1_WP_VIOLATION | /* Tried to write to protected block */ \
812 R1_CC_ERROR | /* Card controller error */ \
813 R1_ERROR) /* General/unknown error */
814
Adrian Hunterbd788c92010-08-11 14:17:47 -0700815static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct mmc_blk_data *md = mq->data;
818 struct mmc_card *card = md->queue.card;
Per Forlin97868a22011-07-09 17:12:36 -0400819 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100820 int ret = 1, disable_multi = 0, retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500822 /*
823 * Reliable writes are used to implement Forced Unit Access and
824 * REQ_META accesses, and are supported only on MMCs.
825 */
826 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
827 (req->cmd_flags & REQ_META)) &&
828 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500829 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 do {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100832 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Per Forlin97868a22011-07-09 17:12:36 -0400834 memset(brq, 0, sizeof(struct mmc_blk_request));
835 brq->mrq.cmd = &brq->cmd;
836 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Per Forlin97868a22011-07-09 17:12:36 -0400838 brq->cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800839 if (!mmc_card_blockaddr(card))
Per Forlin97868a22011-07-09 17:12:36 -0400840 brq->cmd.arg <<= 9;
841 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
842 brq->data.blksz = 512;
843 brq->stop.opcode = MMC_STOP_TRANSMISSION;
844 brq->stop.arg = 0;
845 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
846 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Adrian Hunter6a79e392008-12-31 18:21:17 +0100848 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200849 * The block layer doesn't support all sector count
850 * restrictions, so we need to be prepared for too big
851 * requests.
852 */
Per Forlin97868a22011-07-09 17:12:36 -0400853 if (brq->data.blocks > card->host->max_blk_count)
854 brq->data.blocks = card->host->max_blk_count;
Pierre Ossman548d2de2009-04-10 17:52:57 +0200855
856 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100857 * After a read error, we redo the request one sector at a time
858 * in order to accurately determine which sectors can be read
859 * successfully.
860 */
Per Forlin97868a22011-07-09 17:12:36 -0400861 if (disable_multi && brq->data.blocks > 1)
862 brq->data.blocks = 1;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100863
Per Forlin97868a22011-07-09 17:12:36 -0400864 if (brq->data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700865 /* SPI multiblock writes terminate using a special
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500866 * token, not a STOP_TRANSMISSION request.
David Brownell7213d172007-08-08 09:10:23 -0700867 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500868 if (!mmc_host_is_spi(card->host) ||
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500869 rq_data_dir(req) == READ)
Per Forlin97868a22011-07-09 17:12:36 -0400870 brq->mrq.stop = &brq->stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100871 readcmd = MMC_READ_MULTIPLE_BLOCK;
872 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000873 } else {
Per Forlin97868a22011-07-09 17:12:36 -0400874 brq->mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100875 readcmd = MMC_READ_SINGLE_BLOCK;
876 writecmd = MMC_WRITE_BLOCK;
877 }
Russell Kingdb53f282006-08-30 15:14:56 +0100878 if (rq_data_dir(req) == READ) {
Per Forlin97868a22011-07-09 17:12:36 -0400879 brq->cmd.opcode = readcmd;
880 brq->data.flags |= MMC_DATA_READ;
Russell Kingdb53f282006-08-30 15:14:56 +0100881 } else {
Per Forlin97868a22011-07-09 17:12:36 -0400882 brq->cmd.opcode = writecmd;
883 brq->data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500886 if (do_rel_wr)
Per Forlin97868a22011-07-09 17:12:36 -0400887 mmc_apply_rel_rw(brq, card, req);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500888
889 /*
890 * Pre-defined multi-block transfers are preferable to
891 * open ended-ones (and necessary for reliable writes).
892 * However, it is not sufficient to just send CMD23,
893 * and avoid the final CMD12, as on an error condition
894 * CMD12 (stop) needs to be sent anyway. This, coupled
895 * with Auto-CMD23 enhancements provided by some
896 * hosts, means that the complexity of dealing
897 * with this is best left to the host. If CMD23 is
898 * supported by card and host, we'll fill sbc in and let
899 * the host deal with handling it correctly. This means
900 * that for hosts that don't expose MMC_CAP_CMD23, no
901 * change of behavior will be observed.
902 *
903 * N.B: Some MMC cards experience perf degradation.
904 * We'll avoid using CMD23-bounded multiblock writes for
905 * these, while retaining features like reliable writes.
906 */
907
908 if ((md->flags & MMC_BLK_CMD23) &&
Per Forlin97868a22011-07-09 17:12:36 -0400909 mmc_op_multi(brq->cmd.opcode) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500910 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
Per Forlin97868a22011-07-09 17:12:36 -0400911 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
912 brq->sbc.arg = brq->data.blocks |
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500913 (do_rel_wr ? (1 << 31) : 0);
Per Forlin97868a22011-07-09 17:12:36 -0400914 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
915 brq->mrq.sbc = &brq->sbc;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500916 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500917
Per Forlin97868a22011-07-09 17:12:36 -0400918 mmc_set_data_timeout(&brq->data, card);
Pierre Ossmanb146d262007-07-24 19:16:54 +0200919
Per Forlin97868a22011-07-09 17:12:36 -0400920 brq->data.sg = mq->mqrq_cur->sg;
921 brq->data.sg_len = mmc_queue_map_sg(mq, mq->mqrq_cur);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200922
Adrian Hunter6a79e392008-12-31 18:21:17 +0100923 /*
924 * Adjust the sg list so it is the same size as the
925 * request.
926 */
Per Forlin97868a22011-07-09 17:12:36 -0400927 if (brq->data.blocks != blk_rq_sectors(req)) {
928 int i, data_size = brq->data.blocks << 9;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100929 struct scatterlist *sg;
930
Per Forlin97868a22011-07-09 17:12:36 -0400931 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100932 data_size -= sg->length;
933 if (data_size <= 0) {
934 sg->length += data_size;
935 i++;
936 break;
937 }
938 }
Per Forlin97868a22011-07-09 17:12:36 -0400939 brq->data.sg_len = i;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100940 }
941
Per Forlin97868a22011-07-09 17:12:36 -0400942 mmc_queue_bounce_pre(mq->mqrq_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Per Forlin97868a22011-07-09 17:12:36 -0400944 mmc_wait_for_req(card->host, &brq->mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200945
Per Forlin97868a22011-07-09 17:12:36 -0400946 mmc_queue_bounce_post(mq->mqrq_cur);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200947
Pierre Ossman979ce722008-06-29 12:19:47 +0200948 /*
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100949 * sbc.error indicates a problem with the set block count
950 * command. No data will have been transferred.
951 *
952 * cmd.error indicates a problem with the r/w command. No
953 * data will have been transferred.
954 *
955 * stop.error indicates a problem with the stop command. Data
956 * may have been transferred, or may still be transferring.
Pierre Ossman979ce722008-06-29 12:19:47 +0200957 */
Per Forlin97868a22011-07-09 17:12:36 -0400958 if (brq->sbc.error || brq->cmd.error || brq->stop.error) {
959 switch (mmc_blk_cmd_recovery(card, req, brq)) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100960 case ERR_RETRY:
961 if (retry++ < 5)
962 continue;
963 case ERR_ABORT:
964 goto cmd_abort;
965 case ERR_CONTINUE:
966 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +0100970 /*
971 * Check for errors relating to the execution of the
972 * initial command - such as address errors. No data
973 * has been transferred.
974 */
Per Forlin97868a22011-07-09 17:12:36 -0400975 if (brq->cmd.resp[0] & CMD_ERRORS) {
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +0100976 pr_err("%s: r/w command failed, status = %#x\n",
Per Forlin97868a22011-07-09 17:12:36 -0400977 req->rq_disk->disk_name, brq->cmd.resp[0]);
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +0100978 goto cmd_abort;
979 }
980
981 /*
982 * Everything else is either success, or a data error of some
983 * kind. If it was a write, we may have transitioned to
984 * program mode, which we have to wait for it to complete.
985 */
David Brownell7213d172007-08-08 09:10:23 -0700986 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100987 u32 status;
Russell King2ed6d222006-09-24 10:46:43 +0100988 do {
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100989 int err = get_card_status(card, &status, 5);
Russell King2ed6d222006-09-24 10:46:43 +0100990 if (err) {
991 printk(KERN_ERR "%s: error %d requesting status\n",
992 req->rq_disk->disk_name, err);
993 goto cmd_err;
994 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100995 /*
996 * Some cards mishandle the status bits,
997 * so make sure to check both the busy
998 * indication and the card state.
999 */
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001000 } while (!(status & R1_READY_FOR_DATA) ||
1001 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
Russell King2ed6d222006-09-24 10:46:43 +01001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Per Forlin97868a22011-07-09 17:12:36 -04001004 if (brq->data.error) {
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001005 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
Per Forlin97868a22011-07-09 17:12:36 -04001006 req->rq_disk->disk_name, brq->data.error,
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001007 (unsigned)blk_rq_pos(req),
1008 (unsigned)blk_rq_sectors(req),
Per Forlin97868a22011-07-09 17:12:36 -04001009 brq->cmd.resp[0], brq->stop.resp[0]);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001010
Adrian Hunter6a79e392008-12-31 18:21:17 +01001011 if (rq_data_dir(req) == READ) {
Per Forlin97868a22011-07-09 17:12:36 -04001012 if (brq->data.blocks > 1) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001013 /* Redo read one sector at a time */
1014 pr_warning("%s: retrying using single block read\n",
1015 req->rq_disk->disk_name);
1016 disable_multi = 1;
1017 continue;
1018 }
1019
Adrian Hunter6a79e392008-12-31 18:21:17 +01001020 /*
1021 * After an error, we redo I/O one sector at a
1022 * time, so we only reach here after trying to
1023 * read a single sector.
1024 */
1025 spin_lock_irq(&md->lock);
Per Forlin97868a22011-07-09 17:12:36 -04001026 ret = __blk_end_request(req, -EIO,
1027 brq->data.blksz);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001028 spin_unlock_irq(&md->lock);
1029 continue;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001030 } else {
1031 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001032 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001033 }
Pierre Ossman979ce722008-06-29 12:19:47 +02001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
1036 * A block was successfully transferred.
1037 */
1038 spin_lock_irq(&md->lock);
Per Forlin97868a22011-07-09 17:12:36 -04001039 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 spin_unlock_irq(&md->lock);
1041 } while (ret);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 1;
1044
1045 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001046 /*
1047 * If this is an SD card and we're writing, we can first
1048 * mark the known good sectors as ok.
1049 *
1050 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +02001051 * as reported by the controller (which might be less than
1052 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Adrian Hunter6a79e392008-12-31 18:21:17 +01001054 if (mmc_card_sd(card)) {
1055 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001056
Adrian Hunter6a79e392008-12-31 18:21:17 +01001057 blocks = mmc_sd_num_wr_blocks(card);
1058 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001059 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001060 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001061 spin_unlock_irq(&md->lock);
1062 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001063 } else {
1064 spin_lock_irq(&md->lock);
Per Forlin97868a22011-07-09 17:12:36 -04001065 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001066 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -07001067 }
1068
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001069 cmd_abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001071 while (ret)
1072 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 spin_unlock_irq(&md->lock);
1074
1075 return 0;
1076}
1077
Adrian Hunterbd788c92010-08-11 14:17:47 -07001078static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1079{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001080 int ret;
1081 struct mmc_blk_data *md = mq->data;
1082 struct mmc_card *card = md->queue.card;
1083
1084 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001085 ret = mmc_blk_part_switch(card, md);
1086 if (ret) {
1087 ret = 0;
1088 goto out;
1089 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001090
Adrian Hunter49804542010-08-11 14:17:50 -07001091 if (req->cmd_flags & REQ_DISCARD) {
1092 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001093 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001094 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001095 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001096 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001097 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001098 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001099 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001100 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001101
Andrei Warkentin371a6892011-04-11 18:10:25 -05001102out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001103 mmc_release_host(card->host);
1104 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Russell Kinga6f6c962006-01-03 22:38:44 +00001107static inline int mmc_blk_readonly(struct mmc_card *card)
1108{
1109 return mmc_card_readonly(card) ||
1110 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1111}
1112
Andrei Warkentin371a6892011-04-11 18:10:25 -05001113static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1114 struct device *parent,
1115 sector_t size,
1116 bool default_ro,
1117 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct mmc_blk_data *md;
1120 int devidx, ret;
1121
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001122 devidx = find_first_zero_bit(dev_use, max_devices);
1123 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return ERR_PTR(-ENOSPC);
1125 __set_bit(devidx, dev_use);
1126
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001127 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001128 if (!md) {
1129 ret = -ENOMEM;
1130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001132
Russell Kinga6f6c962006-01-03 22:38:44 +00001133 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001134 * !subname implies we are creating main mmc_blk_data that will be
1135 * associated with mmc_card with mmc_set_drvdata. Due to device
1136 * partitions, devidx will not coincide with a per-physical card
1137 * index anymore so we keep track of a name index.
1138 */
1139 if (!subname) {
1140 md->name_idx = find_first_zero_bit(name_use, max_devices);
1141 __set_bit(md->name_idx, name_use);
1142 }
1143 else
1144 md->name_idx = ((struct mmc_blk_data *)
1145 dev_to_disk(parent)->private_data)->name_idx;
1146
1147 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001148 * Set the read-only status based on the supported commands
1149 * and the write protect switch.
1150 */
1151 md->read_only = mmc_blk_readonly(card);
1152
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001153 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001154 if (md->disk == NULL) {
1155 ret = -ENOMEM;
1156 goto err_kfree;
1157 }
1158
1159 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001160 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001161 md->usage = 1;
1162
Adrian Hunterd09408a2011-06-23 13:40:28 +03001163 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001164 if (ret)
1165 goto err_putdisk;
1166
Russell Kinga6f6c962006-01-03 22:38:44 +00001167 md->queue.issue_fn = mmc_blk_issue_rq;
1168 md->queue.data = md;
1169
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001170 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001171 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001172 md->disk->fops = &mmc_bdops;
1173 md->disk->private_data = md;
1174 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001175 md->disk->driverfs_dev = parent;
1176 set_disk_ro(md->disk, md->read_only || default_ro);
Russell Kinga6f6c962006-01-03 22:38:44 +00001177
1178 /*
1179 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1180 *
1181 * - be set for removable media with permanent block devices
1182 * - be unset for removable block devices with permanent media
1183 *
1184 * Since MMC block devices clearly fall under the second
1185 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1186 * should use the block device creation/destruction hotplug
1187 * messages to tell when the card is present.
1188 */
1189
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001190 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1191 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001192
Martin K. Petersene1defc42009-05-22 17:17:49 -04001193 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001194 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001195
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001196 if (mmc_host_cmd23(card->host)) {
1197 if (mmc_card_mmc(card) ||
1198 (mmc_card_sd(card) &&
1199 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1200 md->flags |= MMC_BLK_CMD23;
1201 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001202
1203 if (mmc_card_mmc(card) &&
1204 md->flags & MMC_BLK_CMD23 &&
1205 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1206 card->ext_csd.rel_sectors)) {
1207 md->flags |= MMC_BLK_REL_WR;
1208 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001212
1213 err_putdisk:
1214 put_disk(md->disk);
1215 err_kfree:
1216 kfree(md);
1217 out:
1218 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Andrei Warkentin371a6892011-04-11 18:10:25 -05001221static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1222{
1223 sector_t size;
1224 struct mmc_blk_data *md;
1225
1226 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1227 /*
1228 * The EXT_CSD sector count is in number or 512 byte
1229 * sectors.
1230 */
1231 size = card->ext_csd.sectors;
1232 } else {
1233 /*
1234 * The CSD capacity field is in units of read_blkbits.
1235 * set_capacity takes units of 512 bytes.
1236 */
1237 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1238 }
1239
1240 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1241 return md;
1242}
1243
1244static int mmc_blk_alloc_part(struct mmc_card *card,
1245 struct mmc_blk_data *md,
1246 unsigned int part_type,
1247 sector_t size,
1248 bool default_ro,
1249 const char *subname)
1250{
1251 char cap_str[10];
1252 struct mmc_blk_data *part_md;
1253
1254 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1255 subname);
1256 if (IS_ERR(part_md))
1257 return PTR_ERR(part_md);
1258 part_md->part_type = part_type;
1259 list_add(&part_md->part, &md->part);
1260
1261 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1262 cap_str, sizeof(cap_str));
1263 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1264 part_md->disk->disk_name, mmc_card_id(card),
1265 mmc_card_name(card), part_md->part_type, cap_str);
1266 return 0;
1267}
1268
1269static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1270{
1271 int ret = 0;
1272
1273 if (!mmc_card_mmc(card))
1274 return 0;
1275
1276 if (card->ext_csd.boot_size) {
1277 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1278 card->ext_csd.boot_size >> 9,
1279 true,
1280 "boot0");
1281 if (ret)
1282 return ret;
1283 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1284 card->ext_csd.boot_size >> 9,
1285 true,
1286 "boot1");
1287 if (ret)
1288 return ret;
1289 }
1290
1291 return ret;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static int
1295mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int err;
1298
Pierre Ossmanb8558852007-01-03 19:47:29 +01001299 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001300 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001301 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001304 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1305 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EINVAL;
1307 }
1308
1309 return 0;
1310}
1311
Andrei Warkentin371a6892011-04-11 18:10:25 -05001312static void mmc_blk_remove_req(struct mmc_blk_data *md)
1313{
1314 if (md) {
1315 if (md->disk->flags & GENHD_FL_UP) {
1316 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1317
1318 /* Stop new requests from getting into the queue */
1319 del_gendisk(md->disk);
1320 }
1321
1322 /* Then flush out any already in there */
1323 mmc_cleanup_queue(&md->queue);
1324 mmc_blk_put(md);
1325 }
1326}
1327
1328static void mmc_blk_remove_parts(struct mmc_card *card,
1329 struct mmc_blk_data *md)
1330{
1331 struct list_head *pos, *q;
1332 struct mmc_blk_data *part_md;
1333
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001334 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001335 list_for_each_safe(pos, q, &md->part) {
1336 part_md = list_entry(pos, struct mmc_blk_data, part);
1337 list_del(pos);
1338 mmc_blk_remove_req(part_md);
1339 }
1340}
1341
1342static int mmc_add_disk(struct mmc_blk_data *md)
1343{
1344 int ret;
1345
1346 add_disk(md->disk);
1347 md->force_ro.show = force_ro_show;
1348 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301349 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001350 md->force_ro.attr.name = "force_ro";
1351 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1352 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1353 if (ret)
1354 del_gendisk(md->disk);
1355
1356 return ret;
1357}
1358
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001359static const struct mmc_fixup blk_fixups[] =
1360{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001361 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1362 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1363 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1364 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1365 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001366
1367 /*
1368 * Some MMC cards experience performance degradation with CMD23
1369 * instead of CMD12-bounded multiblock transfers. For now we'll
1370 * black list what's bad...
1371 * - Certain Toshiba cards.
1372 *
1373 * N.B. This doesn't affect SD cards.
1374 */
1375 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1376 MMC_QUIRK_BLK_NO_CMD23),
1377 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1378 MMC_QUIRK_BLK_NO_CMD23),
1379 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1380 MMC_QUIRK_BLK_NO_CMD23),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001381 END_FIXUP
1382};
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static int mmc_blk_probe(struct mmc_card *card)
1385{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001386 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001388 char cap_str[10];
1389
Pierre Ossman912490d2005-05-21 10:27:02 +01001390 /*
1391 * Check that the card supports the command class(es) we need.
1392 */
1393 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -ENODEV;
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 md = mmc_blk_alloc(card);
1397 if (IS_ERR(md))
1398 return PTR_ERR(md);
1399
1400 err = mmc_blk_set_blksize(md, card);
1401 if (err)
1402 goto out;
1403
Yi Li444122f2009-02-05 15:31:57 +08001404 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001405 cap_str, sizeof(cap_str));
1406 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001408 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Andrei Warkentin371a6892011-04-11 18:10:25 -05001410 if (mmc_blk_alloc_parts(card, md))
1411 goto out;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001414 mmc_fixup_device(card, blk_fixups);
1415
Andrei Warkentin371a6892011-04-11 18:10:25 -05001416 if (mmc_add_disk(md))
1417 goto out;
1418
1419 list_for_each_entry(part_md, &md->part, part) {
1420 if (mmc_add_disk(part_md))
1421 goto out;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return 0;
1424
1425 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001426 mmc_blk_remove_parts(card, md);
1427 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return err;
1429}
1430
1431static void mmc_blk_remove(struct mmc_card *card)
1432{
1433 struct mmc_blk_data *md = mmc_get_drvdata(card);
1434
Andrei Warkentin371a6892011-04-11 18:10:25 -05001435 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001436 mmc_claim_host(card->host);
1437 mmc_blk_part_switch(card, md);
1438 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001439 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 mmc_set_drvdata(card, NULL);
1441}
1442
1443#ifdef CONFIG_PM
1444static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1445{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001446 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 struct mmc_blk_data *md = mmc_get_drvdata(card);
1448
1449 if (md) {
1450 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001451 list_for_each_entry(part_md, &md->part, part) {
1452 mmc_queue_suspend(&part_md->queue);
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 return 0;
1456}
1457
1458static int mmc_blk_resume(struct mmc_card *card)
1459{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001460 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct mmc_blk_data *md = mmc_get_drvdata(card);
1462
1463 if (md) {
1464 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001465
1466 /*
1467 * Resume involves the card going into idle state,
1468 * so current partition is always the main one.
1469 */
1470 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001472 list_for_each_entry(part_md, &md->part, part) {
1473 mmc_queue_resume(&part_md->queue);
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 return 0;
1477}
1478#else
1479#define mmc_blk_suspend NULL
1480#define mmc_blk_resume NULL
1481#endif
1482
1483static struct mmc_driver mmc_driver = {
1484 .drv = {
1485 .name = "mmcblk",
1486 },
1487 .probe = mmc_blk_probe,
1488 .remove = mmc_blk_remove,
1489 .suspend = mmc_blk_suspend,
1490 .resume = mmc_blk_resume,
1491};
1492
1493static int __init mmc_blk_init(void)
1494{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001495 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001497 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1498 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1499
1500 max_devices = 256 / perdev_minors;
1501
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001502 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1503 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001506 res = mmc_register_driver(&mmc_driver);
1507 if (res)
1508 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001510 return 0;
1511 out2:
1512 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 out:
1514 return res;
1515}
1516
1517static void __exit mmc_blk_exit(void)
1518{
1519 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001520 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523module_init(mmc_blk_init);
1524module_exit(mmc_blk_exit);
1525
1526MODULE_LICENSE("GPL");
1527MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1528