blob: 126c7f41c5a33a9702185b0b947f1dadf1fbef7e [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
Andrei Warkentinf4c55222011-03-31 18:40:00 -050062#define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \
63 (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \
64 ((card)->ext_csd.rel_sectors)))
65
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020066static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040067
68/*
69 * The defaults come from config options but can be overriden by module
70 * or bootarg options.
71 */
72static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
73
74/*
75 * We've only got one major, so number of mmcblk devices is
76 * limited to 256 / number of minors per device.
77 */
78static int max_devices;
79
80/* 256 minors, so at most 256 separate devices */
81static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050082static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * There is one mmc_blk_data per slot.
86 */
87struct mmc_blk_data {
88 spinlock_t lock;
89 struct gendisk *disk;
90 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050091 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 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;
432 struct mmc_command cmd;
433 struct mmc_command stop;
434 struct mmc_data data;
435};
436
Andrei Warkentin371a6892011-04-11 18:10:25 -0500437static inline int mmc_blk_part_switch(struct mmc_card *card,
438 struct mmc_blk_data *md)
439{
440 int ret;
441 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
442 if (main_md->part_curr == md->part_type)
443 return 0;
444
445 if (mmc_card_mmc(card)) {
446 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
447 card->ext_csd.part_config |= md->part_type;
448
449 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
451 card->ext_csd.part_time);
452 if (ret)
453 return ret;
454}
455
456 main_md->part_curr = md->part_type;
457 return 0;
458}
459
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700460static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
461{
462 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100463 u32 result;
464 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700465
Chris Ball24f5b532011-04-13 23:49:45 -0400466 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400467 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400468 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700469 unsigned int timeout_us;
470
471 struct scatterlist sg;
472
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700473 cmd.opcode = MMC_APP_CMD;
474 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700475 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700476
477 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700478 if (err)
479 return (u32)-1;
480 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700481 return (u32)-1;
482
483 memset(&cmd, 0, sizeof(struct mmc_command));
484
485 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
486 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700487 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700488
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700489 data.timeout_ns = card->csd.tacc_ns * 100;
490 data.timeout_clks = card->csd.tacc_clks * 100;
491
492 timeout_us = data.timeout_ns / 1000;
493 timeout_us += data.timeout_clks * 1000 /
494 (card->host->ios.clock / 1000);
495
496 if (timeout_us > 100000) {
497 data.timeout_ns = 100000000;
498 data.timeout_clks = 0;
499 }
500
501 data.blksz = 4;
502 data.blocks = 1;
503 data.flags = MMC_DATA_READ;
504 data.sg = &sg;
505 data.sg_len = 1;
506
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700507 mrq.cmd = &cmd;
508 mrq.data = &data;
509
Ben Dooks051913d2009-06-08 23:33:57 +0100510 blocks = kmalloc(4, GFP_KERNEL);
511 if (!blocks)
512 return (u32)-1;
513
514 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700515
516 mmc_wait_for_req(card->host, &mrq);
517
Ben Dooks051913d2009-06-08 23:33:57 +0100518 result = ntohl(*blocks);
519 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700520
Ben Dooks051913d2009-06-08 23:33:57 +0100521 if (cmd.error || data.error)
522 result = (u32)-1;
523
524 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700525}
526
Adrian Hunter504f1912008-10-16 12:55:25 +0300527static u32 get_card_status(struct mmc_card *card, struct request *req)
528{
Chris Ball1278dba2011-04-13 23:40:30 -0400529 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300530 int err;
531
Adrian Hunter504f1912008-10-16 12:55:25 +0300532 cmd.opcode = MMC_SEND_STATUS;
533 if (!mmc_host_is_spi(card->host))
534 cmd.arg = card->rca << 16;
535 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
536 err = mmc_wait_for_cmd(card->host, &cmd, 0);
537 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400538 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300539 req->rq_disk->disk_name, err);
540 return cmd.resp[0];
541}
542
Adrian Hunterbd788c92010-08-11 14:17:47 -0700543static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
544{
545 struct mmc_blk_data *md = mq->data;
546 struct mmc_card *card = md->queue.card;
547 unsigned int from, nr, arg;
548 int err = 0;
549
Adrian Hunterbd788c92010-08-11 14:17:47 -0700550 if (!mmc_can_erase(card)) {
551 err = -EOPNOTSUPP;
552 goto out;
553 }
554
555 from = blk_rq_pos(req);
556 nr = blk_rq_sectors(req);
557
558 if (mmc_can_trim(card))
559 arg = MMC_TRIM_ARG;
560 else
561 arg = MMC_ERASE_ARG;
562
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500563 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
564 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
565 INAND_CMD38_ARG_EXT_CSD,
566 arg == MMC_TRIM_ARG ?
567 INAND_CMD38_ARG_TRIM :
568 INAND_CMD38_ARG_ERASE,
569 0);
570 if (err)
571 goto out;
572 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700573 err = mmc_erase(card, from, nr, arg);
574out:
575 spin_lock_irq(&md->lock);
576 __blk_end_request(req, err, blk_rq_bytes(req));
577 spin_unlock_irq(&md->lock);
578
Adrian Hunterbd788c92010-08-11 14:17:47 -0700579 return err ? 0 : 1;
580}
581
Adrian Hunter49804542010-08-11 14:17:50 -0700582static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
583 struct request *req)
584{
585 struct mmc_blk_data *md = mq->data;
586 struct mmc_card *card = md->queue.card;
587 unsigned int from, nr, arg;
588 int err = 0;
589
Adrian Hunter49804542010-08-11 14:17:50 -0700590 if (!mmc_can_secure_erase_trim(card)) {
591 err = -EOPNOTSUPP;
592 goto out;
593 }
594
595 from = blk_rq_pos(req);
596 nr = blk_rq_sectors(req);
597
598 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
599 arg = MMC_SECURE_TRIM1_ARG;
600 else
601 arg = MMC_SECURE_ERASE_ARG;
602
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500603 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
604 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
605 INAND_CMD38_ARG_EXT_CSD,
606 arg == MMC_SECURE_TRIM1_ARG ?
607 INAND_CMD38_ARG_SECTRIM1 :
608 INAND_CMD38_ARG_SECERASE,
609 0);
610 if (err)
611 goto out;
612 }
Adrian Hunter49804542010-08-11 14:17:50 -0700613 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500614 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
615 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
616 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
617 INAND_CMD38_ARG_EXT_CSD,
618 INAND_CMD38_ARG_SECTRIM2,
619 0);
620 if (err)
621 goto out;
622 }
Adrian Hunter49804542010-08-11 14:17:50 -0700623 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500624 }
Adrian Hunter49804542010-08-11 14:17:50 -0700625out:
626 spin_lock_irq(&md->lock);
627 __blk_end_request(req, err, blk_rq_bytes(req));
628 spin_unlock_irq(&md->lock);
629
Adrian Hunter49804542010-08-11 14:17:50 -0700630 return err ? 0 : 1;
631}
632
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500633static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
634{
635 struct mmc_blk_data *md = mq->data;
636
637 /*
638 * No-op, only service this because we need REQ_FUA for reliable
639 * writes.
640 */
641 spin_lock_irq(&md->lock);
642 __blk_end_request_all(req, 0);
643 spin_unlock_irq(&md->lock);
644
645 return 1;
646}
647
648/*
649 * Reformat current write as a reliable write, supporting
650 * both legacy and the enhanced reliable write MMC cards.
651 * In each transfer we'll handle only as much as a single
652 * reliable write can handle, thus finish the request in
653 * partial completions.
654 */
655static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
656 struct mmc_card *card,
657 struct request *req)
658{
659 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400660 struct mmc_command set_count = {0};
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500661
662 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
663 /* Legacy mode imposes restrictions on transfers. */
664 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
665 brq->data.blocks = 1;
666
667 if (brq->data.blocks > card->ext_csd.rel_sectors)
668 brq->data.blocks = card->ext_csd.rel_sectors;
669 else if (brq->data.blocks < card->ext_csd.rel_sectors)
670 brq->data.blocks = 1;
671 }
672
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500673 set_count.opcode = MMC_SET_BLOCK_COUNT;
674 set_count.arg = brq->data.blocks | (1 << 31);
675 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
676 err = mmc_wait_for_cmd(card->host, &set_count, 0);
677 if (err)
678 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
679 req->rq_disk->disk_name, err);
680 return err;
681}
682
Adrian Hunterbd788c92010-08-11 14:17:47 -0700683static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct mmc_blk_data *md = mq->data;
686 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700687 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100688 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500690 /*
691 * Reliable writes are used to implement Forced Unit Access and
692 * REQ_META accesses, and are supported only on MMCs.
693 */
694 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
695 (req->cmd_flags & REQ_META)) &&
696 (rq_data_dir(req) == WRITE) &&
697 REL_WRITES_SUPPORTED(card);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400700 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300701 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 memset(&brq, 0, sizeof(struct mmc_blk_request));
704 brq.mrq.cmd = &brq.cmd;
705 brq.mrq.data = &brq.data;
706
Tejun Heo83096eb2009-05-07 22:24:39 +0900707 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800708 if (!mmc_card_blockaddr(card))
709 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700710 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200711 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 brq.stop.opcode = MMC_STOP_TRANSMISSION;
713 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700714 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900715 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Adrian Hunter6a79e392008-12-31 18:21:17 +0100717 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200718 * The block layer doesn't support all sector count
719 * restrictions, so we need to be prepared for too big
720 * requests.
721 */
722 if (brq.data.blocks > card->host->max_blk_count)
723 brq.data.blocks = card->host->max_blk_count;
724
725 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100726 * After a read error, we redo the request one sector at a time
727 * in order to accurately determine which sectors can be read
728 * successfully.
729 */
730 if (disable_multi && brq.data.blocks > 1)
731 brq.data.blocks = 1;
732
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500733 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700734 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500735 * token, not a STOP_TRANSMISSION request. Reliable
736 * writes use SET_BLOCK_COUNT and do not use a
737 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700738 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500739 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
740 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700741 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100742 readcmd = MMC_READ_MULTIPLE_BLOCK;
743 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000744 } else {
745 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100746 readcmd = MMC_READ_SINGLE_BLOCK;
747 writecmd = MMC_WRITE_BLOCK;
748 }
Russell Kingdb53f282006-08-30 15:14:56 +0100749 if (rq_data_dir(req) == READ) {
750 brq.cmd.opcode = readcmd;
751 brq.data.flags |= MMC_DATA_READ;
752 } else {
753 brq.cmd.opcode = writecmd;
754 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500757 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
758 goto cmd_err;
759
Pierre Ossmanb146d262007-07-24 19:16:54 +0200760 mmc_set_data_timeout(&brq.data, card);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200763 brq.data.sg_len = mmc_queue_map_sg(mq);
764
Adrian Hunter6a79e392008-12-31 18:21:17 +0100765 /*
766 * Adjust the sg list so it is the same size as the
767 * request.
768 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900769 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100770 int i, data_size = brq.data.blocks << 9;
771 struct scatterlist *sg;
772
773 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
774 data_size -= sg->length;
775 if (data_size <= 0) {
776 sg->length += data_size;
777 i++;
778 break;
779 }
780 }
781 brq.data.sg_len = i;
782 }
783
Pierre Ossman98ccf142007-05-12 00:26:16 +0200784 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200787
788 mmc_queue_bounce_post(mq);
789
Pierre Ossman979ce722008-06-29 12:19:47 +0200790 /*
791 * Check for errors here, but don't jump to cmd_err
792 * until later as we need to wait for the card to leave
793 * programming mode even when things go wrong.
794 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100795 if (brq.cmd.error || brq.data.error || brq.stop.error) {
796 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
797 /* Redo read one sector at a time */
798 printk(KERN_WARNING "%s: retrying using single "
799 "block read\n", req->rq_disk->disk_name);
800 disable_multi = 1;
801 continue;
802 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300803 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100804 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300807 printk(KERN_ERR "%s: error %d sending read/write "
808 "command, response %#x, card status %#x\n",
809 req->rq_disk->disk_name, brq.cmd.error,
810 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300814 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
815 /* 'Stop' response contains card status */
816 status = brq.mrq.stop->resp[0];
817 printk(KERN_ERR "%s: error %d transferring data,"
818 " sector %u, nr %u, card status %#x\n",
819 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900820 (unsigned)blk_rq_pos(req),
821 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300825 printk(KERN_ERR "%s: error %d sending stop command, "
826 "response %#x, card status %#x\n",
827 req->rq_disk->disk_name, brq.stop.error,
828 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
David Brownell7213d172007-08-08 09:10:23 -0700831 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100832 do {
833 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Russell King2ed6d222006-09-24 10:46:43 +0100835 cmd.opcode = MMC_SEND_STATUS;
836 cmd.arg = card->rca << 16;
837 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
838 err = mmc_wait_for_cmd(card->host, &cmd, 5);
839 if (err) {
840 printk(KERN_ERR "%s: error %d requesting status\n",
841 req->rq_disk->disk_name, err);
842 goto cmd_err;
843 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100844 /*
845 * Some cards mishandle the status bits,
846 * so make sure to check both the busy
847 * indication and the card state.
848 */
849 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
850 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100853 if (cmd.resp[0] & ~0x00000900)
854 printk(KERN_ERR "%s: status = %08x\n",
855 req->rq_disk->disk_name, cmd.resp[0]);
856 if (mmc_decode_status(cmd.resp))
857 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#endif
Russell King2ed6d222006-09-24 10:46:43 +0100859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Adrian Hunter6a79e392008-12-31 18:21:17 +0100861 if (brq.cmd.error || brq.stop.error || brq.data.error) {
862 if (rq_data_dir(req) == READ) {
863 /*
864 * After an error, we redo I/O one sector at a
865 * time, so we only reach here after trying to
866 * read a single sector.
867 */
868 spin_lock_irq(&md->lock);
869 ret = __blk_end_request(req, -EIO, brq.data.blksz);
870 spin_unlock_irq(&md->lock);
871 continue;
872 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200873 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100874 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /*
877 * A block was successfully transferred.
878 */
879 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500880 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 spin_unlock_irq(&md->lock);
882 } while (ret);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 1;
885
886 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700887 /*
888 * If this is an SD card and we're writing, we can first
889 * mark the known good sectors as ok.
890 *
891 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200892 * as reported by the controller (which might be less than
893 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100895 if (mmc_card_sd(card)) {
896 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700897
Adrian Hunter6a79e392008-12-31 18:21:17 +0100898 blocks = mmc_sd_num_wr_blocks(card);
899 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700900 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100901 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700902 spin_unlock_irq(&md->lock);
903 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100904 } else {
905 spin_lock_irq(&md->lock);
906 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
907 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700908 }
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500911 while (ret)
912 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 spin_unlock_irq(&md->lock);
914
915 return 0;
916}
917
Adrian Hunterbd788c92010-08-11 14:17:47 -0700918static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
919{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500920 int ret;
921 struct mmc_blk_data *md = mq->data;
922 struct mmc_card *card = md->queue.card;
923
924 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500925 ret = mmc_blk_part_switch(card, md);
926 if (ret) {
927 ret = 0;
928 goto out;
929 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500930
Adrian Hunter49804542010-08-11 14:17:50 -0700931 if (req->cmd_flags & REQ_DISCARD) {
932 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500933 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700934 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500935 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500936 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500937 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700938 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500939 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700940 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500941
Andrei Warkentin371a6892011-04-11 18:10:25 -0500942out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500943 mmc_release_host(card->host);
944 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Russell Kinga6f6c962006-01-03 22:38:44 +0000947static inline int mmc_blk_readonly(struct mmc_card *card)
948{
949 return mmc_card_readonly(card) ||
950 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
951}
952
Andrei Warkentin371a6892011-04-11 18:10:25 -0500953static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
954 struct device *parent,
955 sector_t size,
956 bool default_ro,
957 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct mmc_blk_data *md;
960 int devidx, ret;
961
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400962 devidx = find_first_zero_bit(dev_use, max_devices);
963 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return ERR_PTR(-ENOSPC);
965 __set_bit(devidx, dev_use);
966
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700967 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000968 if (!md) {
969 ret = -ENOMEM;
970 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000972
Russell Kinga6f6c962006-01-03 22:38:44 +0000973 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500974 * !subname implies we are creating main mmc_blk_data that will be
975 * associated with mmc_card with mmc_set_drvdata. Due to device
976 * partitions, devidx will not coincide with a per-physical card
977 * index anymore so we keep track of a name index.
978 */
979 if (!subname) {
980 md->name_idx = find_first_zero_bit(name_use, max_devices);
981 __set_bit(md->name_idx, name_use);
982 }
983 else
984 md->name_idx = ((struct mmc_blk_data *)
985 dev_to_disk(parent)->private_data)->name_idx;
986
987 /*
Russell Kinga6f6c962006-01-03 22:38:44 +0000988 * Set the read-only status based on the supported commands
989 * and the write protect switch.
990 */
991 md->read_only = mmc_blk_readonly(card);
992
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400993 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000994 if (md->disk == NULL) {
995 ret = -ENOMEM;
996 goto err_kfree;
997 }
998
999 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001000 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001001 md->usage = 1;
1002
1003 ret = mmc_init_queue(&md->queue, card, &md->lock);
1004 if (ret)
1005 goto err_putdisk;
1006
Russell Kinga6f6c962006-01-03 22:38:44 +00001007 md->queue.issue_fn = mmc_blk_issue_rq;
1008 md->queue.data = md;
1009
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001010 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001011 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001012 md->disk->fops = &mmc_bdops;
1013 md->disk->private_data = md;
1014 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001015 md->disk->driverfs_dev = parent;
1016 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001017 if (REL_WRITES_SUPPORTED(card))
1018 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +00001019
1020 /*
1021 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1022 *
1023 * - be set for removable media with permanent block devices
1024 * - be unset for removable block devices with permanent media
1025 *
1026 * Since MMC block devices clearly fall under the second
1027 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1028 * should use the block device creation/destruction hotplug
1029 * messages to tell when the card is present.
1030 */
1031
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001032 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1033 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001034
Martin K. Petersene1defc42009-05-22 17:17:49 -04001035 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001036 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001038
1039 err_putdisk:
1040 put_disk(md->disk);
1041 err_kfree:
1042 kfree(md);
1043 out:
1044 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Andrei Warkentin371a6892011-04-11 18:10:25 -05001047static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1048{
1049 sector_t size;
1050 struct mmc_blk_data *md;
1051
1052 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1053 /*
1054 * The EXT_CSD sector count is in number or 512 byte
1055 * sectors.
1056 */
1057 size = card->ext_csd.sectors;
1058 } else {
1059 /*
1060 * The CSD capacity field is in units of read_blkbits.
1061 * set_capacity takes units of 512 bytes.
1062 */
1063 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1064 }
1065
1066 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1067 return md;
1068}
1069
1070static int mmc_blk_alloc_part(struct mmc_card *card,
1071 struct mmc_blk_data *md,
1072 unsigned int part_type,
1073 sector_t size,
1074 bool default_ro,
1075 const char *subname)
1076{
1077 char cap_str[10];
1078 struct mmc_blk_data *part_md;
1079
1080 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1081 subname);
1082 if (IS_ERR(part_md))
1083 return PTR_ERR(part_md);
1084 part_md->part_type = part_type;
1085 list_add(&part_md->part, &md->part);
1086
1087 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1088 cap_str, sizeof(cap_str));
1089 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1090 part_md->disk->disk_name, mmc_card_id(card),
1091 mmc_card_name(card), part_md->part_type, cap_str);
1092 return 0;
1093}
1094
1095static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1096{
1097 int ret = 0;
1098
1099 if (!mmc_card_mmc(card))
1100 return 0;
1101
1102 if (card->ext_csd.boot_size) {
1103 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1104 card->ext_csd.boot_size >> 9,
1105 true,
1106 "boot0");
1107 if (ret)
1108 return ret;
1109 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1110 card->ext_csd.boot_size >> 9,
1111 true,
1112 "boot1");
1113 if (ret)
1114 return ret;
1115 }
1116
1117 return ret;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static int
1121mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int err;
1124
Pierre Ossmanb8558852007-01-03 19:47:29 +01001125 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001126 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001127 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001130 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1131 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -EINVAL;
1133 }
1134
1135 return 0;
1136}
1137
Andrei Warkentin371a6892011-04-11 18:10:25 -05001138static void mmc_blk_remove_req(struct mmc_blk_data *md)
1139{
1140 if (md) {
1141 if (md->disk->flags & GENHD_FL_UP) {
1142 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1143
1144 /* Stop new requests from getting into the queue */
1145 del_gendisk(md->disk);
1146 }
1147
1148 /* Then flush out any already in there */
1149 mmc_cleanup_queue(&md->queue);
1150 mmc_blk_put(md);
1151 }
1152}
1153
1154static void mmc_blk_remove_parts(struct mmc_card *card,
1155 struct mmc_blk_data *md)
1156{
1157 struct list_head *pos, *q;
1158 struct mmc_blk_data *part_md;
1159
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001160 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001161 list_for_each_safe(pos, q, &md->part) {
1162 part_md = list_entry(pos, struct mmc_blk_data, part);
1163 list_del(pos);
1164 mmc_blk_remove_req(part_md);
1165 }
1166}
1167
1168static int mmc_add_disk(struct mmc_blk_data *md)
1169{
1170 int ret;
1171
1172 add_disk(md->disk);
1173 md->force_ro.show = force_ro_show;
1174 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301175 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001176 md->force_ro.attr.name = "force_ro";
1177 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1178 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1179 if (ret)
1180 del_gendisk(md->disk);
1181
1182 return ret;
1183}
1184
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001185static const struct mmc_fixup blk_fixups[] =
1186{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001187 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1188 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1189 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1190 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1191 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001192 END_FIXUP
1193};
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195static int mmc_blk_probe(struct mmc_card *card)
1196{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001197 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001199 char cap_str[10];
1200
Pierre Ossman912490d2005-05-21 10:27:02 +01001201 /*
1202 * Check that the card supports the command class(es) we need.
1203 */
1204 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return -ENODEV;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 md = mmc_blk_alloc(card);
1208 if (IS_ERR(md))
1209 return PTR_ERR(md);
1210
1211 err = mmc_blk_set_blksize(md, card);
1212 if (err)
1213 goto out;
1214
Yi Li444122f2009-02-05 15:31:57 +08001215 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001216 cap_str, sizeof(cap_str));
1217 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001219 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Andrei Warkentin371a6892011-04-11 18:10:25 -05001221 if (mmc_blk_alloc_parts(card, md))
1222 goto out;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001225 mmc_fixup_device(card, blk_fixups);
1226
Andrei Warkentin371a6892011-04-11 18:10:25 -05001227 if (mmc_add_disk(md))
1228 goto out;
1229
1230 list_for_each_entry(part_md, &md->part, part) {
1231 if (mmc_add_disk(part_md))
1232 goto out;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return 0;
1235
1236 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001237 mmc_blk_remove_parts(card, md);
1238 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return err;
1240}
1241
1242static void mmc_blk_remove(struct mmc_card *card)
1243{
1244 struct mmc_blk_data *md = mmc_get_drvdata(card);
1245
Andrei Warkentin371a6892011-04-11 18:10:25 -05001246 mmc_blk_remove_parts(card, md);
1247 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 mmc_set_drvdata(card, NULL);
1249}
1250
1251#ifdef CONFIG_PM
1252static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1253{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001254 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct mmc_blk_data *md = mmc_get_drvdata(card);
1256
1257 if (md) {
1258 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001259 list_for_each_entry(part_md, &md->part, part) {
1260 mmc_queue_suspend(&part_md->queue);
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 return 0;
1264}
1265
1266static int mmc_blk_resume(struct mmc_card *card)
1267{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001268 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct mmc_blk_data *md = mmc_get_drvdata(card);
1270
1271 if (md) {
1272 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001273
1274 /*
1275 * Resume involves the card going into idle state,
1276 * so current partition is always the main one.
1277 */
1278 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001280 list_for_each_entry(part_md, &md->part, part) {
1281 mmc_queue_resume(&part_md->queue);
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 return 0;
1285}
1286#else
1287#define mmc_blk_suspend NULL
1288#define mmc_blk_resume NULL
1289#endif
1290
1291static struct mmc_driver mmc_driver = {
1292 .drv = {
1293 .name = "mmcblk",
1294 },
1295 .probe = mmc_blk_probe,
1296 .remove = mmc_blk_remove,
1297 .suspend = mmc_blk_suspend,
1298 .resume = mmc_blk_resume,
1299};
1300
1301static int __init mmc_blk_init(void)
1302{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001303 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001305 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1306 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1307
1308 max_devices = 256 / perdev_minors;
1309
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001310 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1311 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001314 res = mmc_register_driver(&mmc_driver);
1315 if (res)
1316 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001318 return 0;
1319 out2:
1320 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 out:
1322 return res;
1323}
1324
1325static void __exit mmc_blk_exit(void)
1326{
1327 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001328 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331module_init(mmc_blk_init);
1332module_exit(mmc_blk_exit);
1333
1334MODULE_LICENSE("GPL");
1335MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1336