blob: 71da5641e258e041cb9b72439d3d18eac9d9baa9 [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
430struct mmc_blk_request {
431 struct mmc_request mrq;
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500432 struct mmc_command sbc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct mmc_command cmd;
434 struct mmc_command stop;
435 struct mmc_data data;
436};
437
Andrei Warkentin371a6892011-04-11 18:10:25 -0500438static inline int mmc_blk_part_switch(struct mmc_card *card,
439 struct mmc_blk_data *md)
440{
441 int ret;
442 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
443 if (main_md->part_curr == md->part_type)
444 return 0;
445
446 if (mmc_card_mmc(card)) {
447 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
448 card->ext_csd.part_config |= md->part_type;
449
450 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
451 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
452 card->ext_csd.part_time);
453 if (ret)
454 return ret;
455}
456
457 main_md->part_curr = md->part_type;
458 return 0;
459}
460
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700461static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
462{
463 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100464 u32 result;
465 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700466
Chris Ball24f5b532011-04-13 23:49:45 -0400467 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400468 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400469 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700470 unsigned int timeout_us;
471
472 struct scatterlist sg;
473
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700474 cmd.opcode = MMC_APP_CMD;
475 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700476 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700477
478 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700479 if (err)
480 return (u32)-1;
481 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700482 return (u32)-1;
483
484 memset(&cmd, 0, sizeof(struct mmc_command));
485
486 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
487 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700488 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700489
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700490 data.timeout_ns = card->csd.tacc_ns * 100;
491 data.timeout_clks = card->csd.tacc_clks * 100;
492
493 timeout_us = data.timeout_ns / 1000;
494 timeout_us += data.timeout_clks * 1000 /
495 (card->host->ios.clock / 1000);
496
497 if (timeout_us > 100000) {
498 data.timeout_ns = 100000000;
499 data.timeout_clks = 0;
500 }
501
502 data.blksz = 4;
503 data.blocks = 1;
504 data.flags = MMC_DATA_READ;
505 data.sg = &sg;
506 data.sg_len = 1;
507
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700508 mrq.cmd = &cmd;
509 mrq.data = &data;
510
Ben Dooks051913d2009-06-08 23:33:57 +0100511 blocks = kmalloc(4, GFP_KERNEL);
512 if (!blocks)
513 return (u32)-1;
514
515 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700516
517 mmc_wait_for_req(card->host, &mrq);
518
Ben Dooks051913d2009-06-08 23:33:57 +0100519 result = ntohl(*blocks);
520 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700521
Ben Dooks051913d2009-06-08 23:33:57 +0100522 if (cmd.error || data.error)
523 result = (u32)-1;
524
525 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700526}
527
Adrian Hunter504f1912008-10-16 12:55:25 +0300528static u32 get_card_status(struct mmc_card *card, struct request *req)
529{
Chris Ball1278dba2011-04-13 23:40:30 -0400530 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300531 int err;
532
Adrian Hunter504f1912008-10-16 12:55:25 +0300533 cmd.opcode = MMC_SEND_STATUS;
534 if (!mmc_host_is_spi(card->host))
535 cmd.arg = card->rca << 16;
536 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
537 err = mmc_wait_for_cmd(card->host, &cmd, 0);
538 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400539 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300540 req->rq_disk->disk_name, err);
541 return cmd.resp[0];
542}
543
Adrian Hunterbd788c92010-08-11 14:17:47 -0700544static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
545{
546 struct mmc_blk_data *md = mq->data;
547 struct mmc_card *card = md->queue.card;
548 unsigned int from, nr, arg;
549 int err = 0;
550
Adrian Hunterbd788c92010-08-11 14:17:47 -0700551 if (!mmc_can_erase(card)) {
552 err = -EOPNOTSUPP;
553 goto out;
554 }
555
556 from = blk_rq_pos(req);
557 nr = blk_rq_sectors(req);
558
559 if (mmc_can_trim(card))
560 arg = MMC_TRIM_ARG;
561 else
562 arg = MMC_ERASE_ARG;
563
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500564 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
565 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
566 INAND_CMD38_ARG_EXT_CSD,
567 arg == MMC_TRIM_ARG ?
568 INAND_CMD38_ARG_TRIM :
569 INAND_CMD38_ARG_ERASE,
570 0);
571 if (err)
572 goto out;
573 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700574 err = mmc_erase(card, from, nr, arg);
575out:
576 spin_lock_irq(&md->lock);
577 __blk_end_request(req, err, blk_rq_bytes(req));
578 spin_unlock_irq(&md->lock);
579
Adrian Hunterbd788c92010-08-11 14:17:47 -0700580 return err ? 0 : 1;
581}
582
Adrian Hunter49804542010-08-11 14:17:50 -0700583static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
584 struct request *req)
585{
586 struct mmc_blk_data *md = mq->data;
587 struct mmc_card *card = md->queue.card;
588 unsigned int from, nr, arg;
589 int err = 0;
590
Adrian Hunter49804542010-08-11 14:17:50 -0700591 if (!mmc_can_secure_erase_trim(card)) {
592 err = -EOPNOTSUPP;
593 goto out;
594 }
595
596 from = blk_rq_pos(req);
597 nr = blk_rq_sectors(req);
598
599 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
600 arg = MMC_SECURE_TRIM1_ARG;
601 else
602 arg = MMC_SECURE_ERASE_ARG;
603
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500604 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
605 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
606 INAND_CMD38_ARG_EXT_CSD,
607 arg == MMC_SECURE_TRIM1_ARG ?
608 INAND_CMD38_ARG_SECTRIM1 :
609 INAND_CMD38_ARG_SECERASE,
610 0);
611 if (err)
612 goto out;
613 }
Adrian Hunter49804542010-08-11 14:17:50 -0700614 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500615 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
616 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
617 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
618 INAND_CMD38_ARG_EXT_CSD,
619 INAND_CMD38_ARG_SECTRIM2,
620 0);
621 if (err)
622 goto out;
623 }
Adrian Hunter49804542010-08-11 14:17:50 -0700624 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500625 }
Adrian Hunter49804542010-08-11 14:17:50 -0700626out:
627 spin_lock_irq(&md->lock);
628 __blk_end_request(req, err, blk_rq_bytes(req));
629 spin_unlock_irq(&md->lock);
630
Adrian Hunter49804542010-08-11 14:17:50 -0700631 return err ? 0 : 1;
632}
633
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500634static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
635{
636 struct mmc_blk_data *md = mq->data;
637
638 /*
639 * No-op, only service this because we need REQ_FUA for reliable
640 * writes.
641 */
642 spin_lock_irq(&md->lock);
643 __blk_end_request_all(req, 0);
644 spin_unlock_irq(&md->lock);
645
646 return 1;
647}
648
649/*
650 * Reformat current write as a reliable write, supporting
651 * both legacy and the enhanced reliable write MMC cards.
652 * In each transfer we'll handle only as much as a single
653 * reliable write can handle, thus finish the request in
654 * partial completions.
655 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500656static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
657 struct mmc_card *card,
658 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500659{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500660 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
661 /* Legacy mode imposes restrictions on transfers. */
662 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
663 brq->data.blocks = 1;
664
665 if (brq->data.blocks > card->ext_csd.rel_sectors)
666 brq->data.blocks = card->ext_csd.rel_sectors;
667 else if (brq->data.blocks < card->ext_csd.rel_sectors)
668 brq->data.blocks = 1;
669 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500670}
671
Adrian Hunterbd788c92010-08-11 14:17:47 -0700672static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct mmc_blk_data *md = mq->data;
675 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700676 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100677 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500679 /*
680 * Reliable writes are used to implement Forced Unit Access and
681 * REQ_META accesses, and are supported only on MMCs.
682 */
683 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
684 (req->cmd_flags & REQ_META)) &&
685 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500686 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400689 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300690 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 memset(&brq, 0, sizeof(struct mmc_blk_request));
693 brq.mrq.cmd = &brq.cmd;
694 brq.mrq.data = &brq.data;
695
Tejun Heo83096eb2009-05-07 22:24:39 +0900696 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800697 if (!mmc_card_blockaddr(card))
698 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700699 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200700 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 brq.stop.opcode = MMC_STOP_TRANSMISSION;
702 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700703 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900704 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Adrian Hunter6a79e392008-12-31 18:21:17 +0100706 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200707 * The block layer doesn't support all sector count
708 * restrictions, so we need to be prepared for too big
709 * requests.
710 */
711 if (brq.data.blocks > card->host->max_blk_count)
712 brq.data.blocks = card->host->max_blk_count;
713
714 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100715 * After a read error, we redo the request one sector at a time
716 * in order to accurately determine which sectors can be read
717 * successfully.
718 */
719 if (disable_multi && brq.data.blocks > 1)
720 brq.data.blocks = 1;
721
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500722 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700723 /* SPI multiblock writes terminate using a special
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500724 * token, not a STOP_TRANSMISSION request.
David Brownell7213d172007-08-08 09:10:23 -0700725 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500726 if (!mmc_host_is_spi(card->host) ||
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500727 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700728 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100729 readcmd = MMC_READ_MULTIPLE_BLOCK;
730 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000731 } else {
732 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100733 readcmd = MMC_READ_SINGLE_BLOCK;
734 writecmd = MMC_WRITE_BLOCK;
735 }
Russell Kingdb53f282006-08-30 15:14:56 +0100736 if (rq_data_dir(req) == READ) {
737 brq.cmd.opcode = readcmd;
738 brq.data.flags |= MMC_DATA_READ;
739 } else {
740 brq.cmd.opcode = writecmd;
741 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500744 if (do_rel_wr)
745 mmc_apply_rel_rw(&brq, card, req);
746
747 /*
748 * Pre-defined multi-block transfers are preferable to
749 * open ended-ones (and necessary for reliable writes).
750 * However, it is not sufficient to just send CMD23,
751 * and avoid the final CMD12, as on an error condition
752 * CMD12 (stop) needs to be sent anyway. This, coupled
753 * with Auto-CMD23 enhancements provided by some
754 * hosts, means that the complexity of dealing
755 * with this is best left to the host. If CMD23 is
756 * supported by card and host, we'll fill sbc in and let
757 * the host deal with handling it correctly. This means
758 * that for hosts that don't expose MMC_CAP_CMD23, no
759 * change of behavior will be observed.
760 *
761 * N.B: Some MMC cards experience perf degradation.
762 * We'll avoid using CMD23-bounded multiblock writes for
763 * these, while retaining features like reliable writes.
764 */
765
766 if ((md->flags & MMC_BLK_CMD23) &&
767 mmc_op_multi(brq.cmd.opcode) &&
768 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
769 brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
770 brq.sbc.arg = brq.data.blocks |
771 (do_rel_wr ? (1 << 31) : 0);
772 brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
773 brq.mrq.sbc = &brq.sbc;
774 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500775
Pierre Ossmanb146d262007-07-24 19:16:54 +0200776 mmc_set_data_timeout(&brq.data, card);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200779 brq.data.sg_len = mmc_queue_map_sg(mq);
780
Adrian Hunter6a79e392008-12-31 18:21:17 +0100781 /*
782 * Adjust the sg list so it is the same size as the
783 * request.
784 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900785 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100786 int i, data_size = brq.data.blocks << 9;
787 struct scatterlist *sg;
788
789 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
790 data_size -= sg->length;
791 if (data_size <= 0) {
792 sg->length += data_size;
793 i++;
794 break;
795 }
796 }
797 brq.data.sg_len = i;
798 }
799
Pierre Ossman98ccf142007-05-12 00:26:16 +0200800 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200803
804 mmc_queue_bounce_post(mq);
805
Pierre Ossman979ce722008-06-29 12:19:47 +0200806 /*
807 * Check for errors here, but don't jump to cmd_err
808 * until later as we need to wait for the card to leave
809 * programming mode even when things go wrong.
810 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500811 if (brq.sbc.error || brq.cmd.error ||
812 brq.data.error || brq.stop.error) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100813 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
814 /* Redo read one sector at a time */
815 printk(KERN_WARNING "%s: retrying using single "
816 "block read\n", req->rq_disk->disk_name);
817 disable_multi = 1;
818 continue;
819 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300820 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100821 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300822
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500823 if (brq.sbc.error) {
824 printk(KERN_ERR "%s: error %d sending SET_BLOCK_COUNT "
825 "command, response %#x, card status %#x\n",
826 req->rq_disk->disk_name, brq.sbc.error,
827 brq.sbc.resp[0], status);
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300831 printk(KERN_ERR "%s: error %d sending read/write "
832 "command, response %#x, card status %#x\n",
833 req->rq_disk->disk_name, brq.cmd.error,
834 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300838 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
839 /* 'Stop' response contains card status */
840 status = brq.mrq.stop->resp[0];
841 printk(KERN_ERR "%s: error %d transferring data,"
842 " sector %u, nr %u, card status %#x\n",
843 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900844 (unsigned)blk_rq_pos(req),
845 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300849 printk(KERN_ERR "%s: error %d sending stop command, "
850 "response %#x, card status %#x\n",
851 req->rq_disk->disk_name, brq.stop.error,
852 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
David Brownell7213d172007-08-08 09:10:23 -0700855 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100856 do {
857 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Russell King2ed6d222006-09-24 10:46:43 +0100859 cmd.opcode = MMC_SEND_STATUS;
860 cmd.arg = card->rca << 16;
861 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
862 err = mmc_wait_for_cmd(card->host, &cmd, 5);
863 if (err) {
864 printk(KERN_ERR "%s: error %d requesting status\n",
865 req->rq_disk->disk_name, err);
866 goto cmd_err;
867 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100868 /*
869 * Some cards mishandle the status bits,
870 * so make sure to check both the busy
871 * indication and the card state.
872 */
873 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
874 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100877 if (cmd.resp[0] & ~0x00000900)
878 printk(KERN_ERR "%s: status = %08x\n",
879 req->rq_disk->disk_name, cmd.resp[0]);
880 if (mmc_decode_status(cmd.resp))
881 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
Russell King2ed6d222006-09-24 10:46:43 +0100883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Adrian Hunter6a79e392008-12-31 18:21:17 +0100885 if (brq.cmd.error || brq.stop.error || brq.data.error) {
886 if (rq_data_dir(req) == READ) {
887 /*
888 * After an error, we redo I/O one sector at a
889 * time, so we only reach here after trying to
890 * read a single sector.
891 */
892 spin_lock_irq(&md->lock);
893 ret = __blk_end_request(req, -EIO, brq.data.blksz);
894 spin_unlock_irq(&md->lock);
895 continue;
896 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200897 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100898 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * A block was successfully transferred.
902 */
903 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500904 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock_irq(&md->lock);
906 } while (ret);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 1;
909
910 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700911 /*
912 * If this is an SD card and we're writing, we can first
913 * mark the known good sectors as ok.
914 *
915 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200916 * as reported by the controller (which might be less than
917 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100919 if (mmc_card_sd(card)) {
920 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700921
Adrian Hunter6a79e392008-12-31 18:21:17 +0100922 blocks = mmc_sd_num_wr_blocks(card);
923 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700924 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100925 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700926 spin_unlock_irq(&md->lock);
927 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100928 } else {
929 spin_lock_irq(&md->lock);
930 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
931 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700932 }
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500935 while (ret)
936 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_unlock_irq(&md->lock);
938
939 return 0;
940}
941
Adrian Hunterbd788c92010-08-11 14:17:47 -0700942static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
943{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500944 int ret;
945 struct mmc_blk_data *md = mq->data;
946 struct mmc_card *card = md->queue.card;
947
948 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500949 ret = mmc_blk_part_switch(card, md);
950 if (ret) {
951 ret = 0;
952 goto out;
953 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500954
Adrian Hunter49804542010-08-11 14:17:50 -0700955 if (req->cmd_flags & REQ_DISCARD) {
956 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500957 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700958 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500959 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500960 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500961 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700962 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500963 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700964 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500965
Andrei Warkentin371a6892011-04-11 18:10:25 -0500966out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500967 mmc_release_host(card->host);
968 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Russell Kinga6f6c962006-01-03 22:38:44 +0000971static inline int mmc_blk_readonly(struct mmc_card *card)
972{
973 return mmc_card_readonly(card) ||
974 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
975}
976
Andrei Warkentin371a6892011-04-11 18:10:25 -0500977static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
978 struct device *parent,
979 sector_t size,
980 bool default_ro,
981 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct mmc_blk_data *md;
984 int devidx, ret;
985
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400986 devidx = find_first_zero_bit(dev_use, max_devices);
987 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return ERR_PTR(-ENOSPC);
989 __set_bit(devidx, dev_use);
990
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700991 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000992 if (!md) {
993 ret = -ENOMEM;
994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000996
Russell Kinga6f6c962006-01-03 22:38:44 +0000997 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500998 * !subname implies we are creating main mmc_blk_data that will be
999 * associated with mmc_card with mmc_set_drvdata. Due to device
1000 * partitions, devidx will not coincide with a per-physical card
1001 * index anymore so we keep track of a name index.
1002 */
1003 if (!subname) {
1004 md->name_idx = find_first_zero_bit(name_use, max_devices);
1005 __set_bit(md->name_idx, name_use);
1006 }
1007 else
1008 md->name_idx = ((struct mmc_blk_data *)
1009 dev_to_disk(parent)->private_data)->name_idx;
1010
1011 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001012 * Set the read-only status based on the supported commands
1013 * and the write protect switch.
1014 */
1015 md->read_only = mmc_blk_readonly(card);
1016
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001017 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001018 if (md->disk == NULL) {
1019 ret = -ENOMEM;
1020 goto err_kfree;
1021 }
1022
1023 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001024 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001025 md->usage = 1;
1026
1027 ret = mmc_init_queue(&md->queue, card, &md->lock);
1028 if (ret)
1029 goto err_putdisk;
1030
Russell Kinga6f6c962006-01-03 22:38:44 +00001031 md->queue.issue_fn = mmc_blk_issue_rq;
1032 md->queue.data = md;
1033
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001034 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001035 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001036 md->disk->fops = &mmc_bdops;
1037 md->disk->private_data = md;
1038 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001039 md->disk->driverfs_dev = parent;
1040 set_disk_ro(md->disk, md->read_only || default_ro);
Russell Kinga6f6c962006-01-03 22:38:44 +00001041
1042 /*
1043 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1044 *
1045 * - be set for removable media with permanent block devices
1046 * - be unset for removable block devices with permanent media
1047 *
1048 * Since MMC block devices clearly fall under the second
1049 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1050 * should use the block device creation/destruction hotplug
1051 * messages to tell when the card is present.
1052 */
1053
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001054 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1055 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001056
Martin K. Petersene1defc42009-05-22 17:17:49 -04001057 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001058 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001059
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001060 if (mmc_host_cmd23(card->host)) {
1061 if (mmc_card_mmc(card) ||
1062 (mmc_card_sd(card) &&
1063 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1064 md->flags |= MMC_BLK_CMD23;
1065 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001066
1067 if (mmc_card_mmc(card) &&
1068 md->flags & MMC_BLK_CMD23 &&
1069 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1070 card->ext_csd.rel_sectors)) {
1071 md->flags |= MMC_BLK_REL_WR;
1072 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001076
1077 err_putdisk:
1078 put_disk(md->disk);
1079 err_kfree:
1080 kfree(md);
1081 out:
1082 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Andrei Warkentin371a6892011-04-11 18:10:25 -05001085static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1086{
1087 sector_t size;
1088 struct mmc_blk_data *md;
1089
1090 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1091 /*
1092 * The EXT_CSD sector count is in number or 512 byte
1093 * sectors.
1094 */
1095 size = card->ext_csd.sectors;
1096 } else {
1097 /*
1098 * The CSD capacity field is in units of read_blkbits.
1099 * set_capacity takes units of 512 bytes.
1100 */
1101 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1102 }
1103
1104 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1105 return md;
1106}
1107
1108static int mmc_blk_alloc_part(struct mmc_card *card,
1109 struct mmc_blk_data *md,
1110 unsigned int part_type,
1111 sector_t size,
1112 bool default_ro,
1113 const char *subname)
1114{
1115 char cap_str[10];
1116 struct mmc_blk_data *part_md;
1117
1118 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1119 subname);
1120 if (IS_ERR(part_md))
1121 return PTR_ERR(part_md);
1122 part_md->part_type = part_type;
1123 list_add(&part_md->part, &md->part);
1124
1125 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1126 cap_str, sizeof(cap_str));
1127 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1128 part_md->disk->disk_name, mmc_card_id(card),
1129 mmc_card_name(card), part_md->part_type, cap_str);
1130 return 0;
1131}
1132
1133static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1134{
1135 int ret = 0;
1136
1137 if (!mmc_card_mmc(card))
1138 return 0;
1139
1140 if (card->ext_csd.boot_size) {
1141 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1142 card->ext_csd.boot_size >> 9,
1143 true,
1144 "boot0");
1145 if (ret)
1146 return ret;
1147 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1148 card->ext_csd.boot_size >> 9,
1149 true,
1150 "boot1");
1151 if (ret)
1152 return ret;
1153 }
1154
1155 return ret;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static int
1159mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int err;
1162
Pierre Ossmanb8558852007-01-03 19:47:29 +01001163 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001164 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001165 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001168 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1169 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return -EINVAL;
1171 }
1172
1173 return 0;
1174}
1175
Andrei Warkentin371a6892011-04-11 18:10:25 -05001176static void mmc_blk_remove_req(struct mmc_blk_data *md)
1177{
1178 if (md) {
1179 if (md->disk->flags & GENHD_FL_UP) {
1180 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1181
1182 /* Stop new requests from getting into the queue */
1183 del_gendisk(md->disk);
1184 }
1185
1186 /* Then flush out any already in there */
1187 mmc_cleanup_queue(&md->queue);
1188 mmc_blk_put(md);
1189 }
1190}
1191
1192static void mmc_blk_remove_parts(struct mmc_card *card,
1193 struct mmc_blk_data *md)
1194{
1195 struct list_head *pos, *q;
1196 struct mmc_blk_data *part_md;
1197
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001198 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001199 list_for_each_safe(pos, q, &md->part) {
1200 part_md = list_entry(pos, struct mmc_blk_data, part);
1201 list_del(pos);
1202 mmc_blk_remove_req(part_md);
1203 }
1204}
1205
1206static int mmc_add_disk(struct mmc_blk_data *md)
1207{
1208 int ret;
1209
1210 add_disk(md->disk);
1211 md->force_ro.show = force_ro_show;
1212 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301213 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001214 md->force_ro.attr.name = "force_ro";
1215 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1216 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1217 if (ret)
1218 del_gendisk(md->disk);
1219
1220 return ret;
1221}
1222
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001223static const struct mmc_fixup blk_fixups[] =
1224{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001225 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1226 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1227 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1228 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1229 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001230
1231 /*
1232 * Some MMC cards experience performance degradation with CMD23
1233 * instead of CMD12-bounded multiblock transfers. For now we'll
1234 * black list what's bad...
1235 * - Certain Toshiba cards.
1236 *
1237 * N.B. This doesn't affect SD cards.
1238 */
1239 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1240 MMC_QUIRK_BLK_NO_CMD23),
1241 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1242 MMC_QUIRK_BLK_NO_CMD23),
1243 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1244 MMC_QUIRK_BLK_NO_CMD23),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001245 END_FIXUP
1246};
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static int mmc_blk_probe(struct mmc_card *card)
1249{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001250 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001252 char cap_str[10];
1253
Pierre Ossman912490d2005-05-21 10:27:02 +01001254 /*
1255 * Check that the card supports the command class(es) we need.
1256 */
1257 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -ENODEV;
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 md = mmc_blk_alloc(card);
1261 if (IS_ERR(md))
1262 return PTR_ERR(md);
1263
1264 err = mmc_blk_set_blksize(md, card);
1265 if (err)
1266 goto out;
1267
Yi Li444122f2009-02-05 15:31:57 +08001268 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001269 cap_str, sizeof(cap_str));
1270 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001272 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Andrei Warkentin371a6892011-04-11 18:10:25 -05001274 if (mmc_blk_alloc_parts(card, md))
1275 goto out;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001278 mmc_fixup_device(card, blk_fixups);
1279
Andrei Warkentin371a6892011-04-11 18:10:25 -05001280 if (mmc_add_disk(md))
1281 goto out;
1282
1283 list_for_each_entry(part_md, &md->part, part) {
1284 if (mmc_add_disk(part_md))
1285 goto out;
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 0;
1288
1289 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001290 mmc_blk_remove_parts(card, md);
1291 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return err;
1293}
1294
1295static void mmc_blk_remove(struct mmc_card *card)
1296{
1297 struct mmc_blk_data *md = mmc_get_drvdata(card);
1298
Andrei Warkentin371a6892011-04-11 18:10:25 -05001299 mmc_blk_remove_parts(card, md);
1300 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 mmc_set_drvdata(card, NULL);
1302}
1303
1304#ifdef CONFIG_PM
1305static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1306{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001307 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct mmc_blk_data *md = mmc_get_drvdata(card);
1309
1310 if (md) {
1311 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001312 list_for_each_entry(part_md, &md->part, part) {
1313 mmc_queue_suspend(&part_md->queue);
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 return 0;
1317}
1318
1319static int mmc_blk_resume(struct mmc_card *card)
1320{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001321 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct mmc_blk_data *md = mmc_get_drvdata(card);
1323
1324 if (md) {
1325 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001326
1327 /*
1328 * Resume involves the card going into idle state,
1329 * so current partition is always the main one.
1330 */
1331 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001333 list_for_each_entry(part_md, &md->part, part) {
1334 mmc_queue_resume(&part_md->queue);
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 return 0;
1338}
1339#else
1340#define mmc_blk_suspend NULL
1341#define mmc_blk_resume NULL
1342#endif
1343
1344static struct mmc_driver mmc_driver = {
1345 .drv = {
1346 .name = "mmcblk",
1347 },
1348 .probe = mmc_blk_probe,
1349 .remove = mmc_blk_remove,
1350 .suspend = mmc_blk_suspend,
1351 .resume = mmc_blk_resume,
1352};
1353
1354static int __init mmc_blk_init(void)
1355{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001356 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001358 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1359 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1360
1361 max_devices = 256 / perdev_minors;
1362
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001363 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1364 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001367 res = mmc_register_driver(&mmc_driver);
1368 if (res)
1369 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001371 return 0;
1372 out2:
1373 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 out:
1375 return res;
1376}
1377
1378static void __exit mmc_blk_exit(void)
1379{
1380 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001381 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384module_init(mmc_blk_init);
1385module_exit(mmc_blk_exit);
1386
1387MODULE_LICENSE("GPL");
1388MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1389