blob: 407836d557126dc6c13a26a875bbd50128124f59 [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;
240 goto copy_err;
241 }
242
243 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
244 err = -EFAULT;
245 goto copy_err;
246 }
247
248 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
250 err = -EOVERFLOW;
251 goto copy_err;
252 }
253
254 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
255 if (!idata->buf) {
256 err = -ENOMEM;
257 goto copy_err;
258 }
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);
270 kfree(idata);
271 return ERR_PTR(err);
272
273}
274
275static int mmc_blk_ioctl_cmd(struct block_device *bdev,
276 struct mmc_ioc_cmd __user *ic_ptr)
277{
278 struct mmc_blk_ioc_data *idata;
279 struct mmc_blk_data *md;
280 struct mmc_card *card;
281 struct mmc_command cmd = {0};
282 struct mmc_data data = {0};
283 struct mmc_request mrq = {0};
284 struct scatterlist sg;
285 int err;
286
287 /*
288 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
289 * whole block device, not on a partition. This prevents overspray
290 * between sibling partitions.
291 */
292 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
293 return -EPERM;
294
295 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
296 if (IS_ERR(idata))
297 return PTR_ERR(idata);
298
299 cmd.opcode = idata->ic.opcode;
300 cmd.arg = idata->ic.arg;
301 cmd.flags = idata->ic.flags;
302
303 data.sg = &sg;
304 data.sg_len = 1;
305 data.blksz = idata->ic.blksz;
306 data.blocks = idata->ic.blocks;
307
308 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
309
310 if (idata->ic.write_flag)
311 data.flags = MMC_DATA_WRITE;
312 else
313 data.flags = MMC_DATA_READ;
314
315 mrq.cmd = &cmd;
316 mrq.data = &data;
317
318 md = mmc_blk_get(bdev->bd_disk);
319 if (!md) {
320 err = -EINVAL;
321 goto cmd_done;
322 }
323
324 card = md->queue.card;
325 if (IS_ERR(card)) {
326 err = PTR_ERR(card);
327 goto cmd_done;
328 }
329
330 mmc_claim_host(card->host);
331
332 if (idata->ic.is_acmd) {
333 err = mmc_app_cmd(card->host, card);
334 if (err)
335 goto cmd_rel_host;
336 }
337
338 /* data.flags must already be set before doing this. */
339 mmc_set_data_timeout(&data, card);
340 /* Allow overriding the timeout_ns for empirical tuning. */
341 if (idata->ic.data_timeout_ns)
342 data.timeout_ns = idata->ic.data_timeout_ns;
343
344 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
345 /*
346 * Pretend this is a data transfer and rely on the host driver
347 * to compute timeout. When all host drivers support
348 * cmd.cmd_timeout for R1B, this can be changed to:
349 *
350 * mrq.data = NULL;
351 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
352 */
353 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
354 }
355
356 mmc_wait_for_req(card->host, &mrq);
357
358 if (cmd.error) {
359 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
360 __func__, cmd.error);
361 err = cmd.error;
362 goto cmd_rel_host;
363 }
364 if (data.error) {
365 dev_err(mmc_dev(card->host), "%s: data error %d\n",
366 __func__, data.error);
367 err = data.error;
368 goto cmd_rel_host;
369 }
370
371 /*
372 * According to the SD specs, some commands require a delay after
373 * issuing the command.
374 */
375 if (idata->ic.postsleep_min_us)
376 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
377
378 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
379 err = -EFAULT;
380 goto cmd_rel_host;
381 }
382
383 if (!idata->ic.write_flag) {
384 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
385 idata->buf, idata->buf_bytes)) {
386 err = -EFAULT;
387 goto cmd_rel_host;
388 }
389 }
390
391cmd_rel_host:
392 mmc_release_host(card->host);
393
394cmd_done:
395 mmc_blk_put(md);
396 kfree(idata->buf);
397 kfree(idata);
398 return err;
399}
400
401static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
402 unsigned int cmd, unsigned long arg)
403{
404 int ret = -EINVAL;
405 if (cmd == MMC_IOC_CMD)
406 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
407 return ret;
408}
409
410#ifdef CONFIG_COMPAT
411static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
412 unsigned int cmd, unsigned long arg)
413{
414 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
415}
416#endif
417
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700418static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500419 .open = mmc_blk_open,
420 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800421 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400423 .ioctl = mmc_blk_ioctl,
424#ifdef CONFIG_COMPAT
425 .compat_ioctl = mmc_blk_compat_ioctl,
426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
429struct mmc_blk_request {
430 struct mmc_request mrq;
431 struct mmc_command cmd;
432 struct mmc_command stop;
433 struct mmc_data data;
434};
435
Andrei Warkentin371a6892011-04-11 18:10:25 -0500436static inline int mmc_blk_part_switch(struct mmc_card *card,
437 struct mmc_blk_data *md)
438{
439 int ret;
440 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
441 if (main_md->part_curr == md->part_type)
442 return 0;
443
444 if (mmc_card_mmc(card)) {
445 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
446 card->ext_csd.part_config |= md->part_type;
447
448 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
449 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
450 card->ext_csd.part_time);
451 if (ret)
452 return ret;
453}
454
455 main_md->part_curr = md->part_type;
456 return 0;
457}
458
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700459static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
460{
461 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100462 u32 result;
463 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700464
Chris Ball24f5b532011-04-13 23:49:45 -0400465 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400466 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400467 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700468 unsigned int timeout_us;
469
470 struct scatterlist sg;
471
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700472 cmd.opcode = MMC_APP_CMD;
473 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700474 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700475
476 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700477 if (err)
478 return (u32)-1;
479 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700480 return (u32)-1;
481
482 memset(&cmd, 0, sizeof(struct mmc_command));
483
484 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
485 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700486 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700487
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700488 data.timeout_ns = card->csd.tacc_ns * 100;
489 data.timeout_clks = card->csd.tacc_clks * 100;
490
491 timeout_us = data.timeout_ns / 1000;
492 timeout_us += data.timeout_clks * 1000 /
493 (card->host->ios.clock / 1000);
494
495 if (timeout_us > 100000) {
496 data.timeout_ns = 100000000;
497 data.timeout_clks = 0;
498 }
499
500 data.blksz = 4;
501 data.blocks = 1;
502 data.flags = MMC_DATA_READ;
503 data.sg = &sg;
504 data.sg_len = 1;
505
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700506 mrq.cmd = &cmd;
507 mrq.data = &data;
508
Ben Dooks051913d2009-06-08 23:33:57 +0100509 blocks = kmalloc(4, GFP_KERNEL);
510 if (!blocks)
511 return (u32)-1;
512
513 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700514
515 mmc_wait_for_req(card->host, &mrq);
516
Ben Dooks051913d2009-06-08 23:33:57 +0100517 result = ntohl(*blocks);
518 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700519
Ben Dooks051913d2009-06-08 23:33:57 +0100520 if (cmd.error || data.error)
521 result = (u32)-1;
522
523 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700524}
525
Adrian Hunter504f1912008-10-16 12:55:25 +0300526static u32 get_card_status(struct mmc_card *card, struct request *req)
527{
Chris Ball1278dba2011-04-13 23:40:30 -0400528 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300529 int err;
530
Adrian Hunter504f1912008-10-16 12:55:25 +0300531 cmd.opcode = MMC_SEND_STATUS;
532 if (!mmc_host_is_spi(card->host))
533 cmd.arg = card->rca << 16;
534 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
535 err = mmc_wait_for_cmd(card->host, &cmd, 0);
536 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400537 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300538 req->rq_disk->disk_name, err);
539 return cmd.resp[0];
540}
541
Adrian Hunterbd788c92010-08-11 14:17:47 -0700542static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
543{
544 struct mmc_blk_data *md = mq->data;
545 struct mmc_card *card = md->queue.card;
546 unsigned int from, nr, arg;
547 int err = 0;
548
Adrian Hunterbd788c92010-08-11 14:17:47 -0700549 if (!mmc_can_erase(card)) {
550 err = -EOPNOTSUPP;
551 goto out;
552 }
553
554 from = blk_rq_pos(req);
555 nr = blk_rq_sectors(req);
556
557 if (mmc_can_trim(card))
558 arg = MMC_TRIM_ARG;
559 else
560 arg = MMC_ERASE_ARG;
561
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500562 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
563 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
564 INAND_CMD38_ARG_EXT_CSD,
565 arg == MMC_TRIM_ARG ?
566 INAND_CMD38_ARG_TRIM :
567 INAND_CMD38_ARG_ERASE,
568 0);
569 if (err)
570 goto out;
571 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700572 err = mmc_erase(card, from, nr, arg);
573out:
574 spin_lock_irq(&md->lock);
575 __blk_end_request(req, err, blk_rq_bytes(req));
576 spin_unlock_irq(&md->lock);
577
Adrian Hunterbd788c92010-08-11 14:17:47 -0700578 return err ? 0 : 1;
579}
580
Adrian Hunter49804542010-08-11 14:17:50 -0700581static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
582 struct request *req)
583{
584 struct mmc_blk_data *md = mq->data;
585 struct mmc_card *card = md->queue.card;
586 unsigned int from, nr, arg;
587 int err = 0;
588
Adrian Hunter49804542010-08-11 14:17:50 -0700589 if (!mmc_can_secure_erase_trim(card)) {
590 err = -EOPNOTSUPP;
591 goto out;
592 }
593
594 from = blk_rq_pos(req);
595 nr = blk_rq_sectors(req);
596
597 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
598 arg = MMC_SECURE_TRIM1_ARG;
599 else
600 arg = MMC_SECURE_ERASE_ARG;
601
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500602 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
603 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
604 INAND_CMD38_ARG_EXT_CSD,
605 arg == MMC_SECURE_TRIM1_ARG ?
606 INAND_CMD38_ARG_SECTRIM1 :
607 INAND_CMD38_ARG_SECERASE,
608 0);
609 if (err)
610 goto out;
611 }
Adrian Hunter49804542010-08-11 14:17:50 -0700612 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500613 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
614 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
615 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
616 INAND_CMD38_ARG_EXT_CSD,
617 INAND_CMD38_ARG_SECTRIM2,
618 0);
619 if (err)
620 goto out;
621 }
Adrian Hunter49804542010-08-11 14:17:50 -0700622 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500623 }
Adrian Hunter49804542010-08-11 14:17:50 -0700624out:
625 spin_lock_irq(&md->lock);
626 __blk_end_request(req, err, blk_rq_bytes(req));
627 spin_unlock_irq(&md->lock);
628
Adrian Hunter49804542010-08-11 14:17:50 -0700629 return err ? 0 : 1;
630}
631
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500632static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
633{
634 struct mmc_blk_data *md = mq->data;
635
636 /*
637 * No-op, only service this because we need REQ_FUA for reliable
638 * writes.
639 */
640 spin_lock_irq(&md->lock);
641 __blk_end_request_all(req, 0);
642 spin_unlock_irq(&md->lock);
643
644 return 1;
645}
646
647/*
648 * Reformat current write as a reliable write, supporting
649 * both legacy and the enhanced reliable write MMC cards.
650 * In each transfer we'll handle only as much as a single
651 * reliable write can handle, thus finish the request in
652 * partial completions.
653 */
654static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
655 struct mmc_card *card,
656 struct request *req)
657{
658 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400659 struct mmc_command set_count = {0};
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500660
661 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
662 /* Legacy mode imposes restrictions on transfers. */
663 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
664 brq->data.blocks = 1;
665
666 if (brq->data.blocks > card->ext_csd.rel_sectors)
667 brq->data.blocks = card->ext_csd.rel_sectors;
668 else if (brq->data.blocks < card->ext_csd.rel_sectors)
669 brq->data.blocks = 1;
670 }
671
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500672 set_count.opcode = MMC_SET_BLOCK_COUNT;
673 set_count.arg = brq->data.blocks | (1 << 31);
674 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
675 err = mmc_wait_for_cmd(card->host, &set_count, 0);
676 if (err)
677 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
678 req->rq_disk->disk_name, err);
679 return err;
680}
681
Adrian Hunterbd788c92010-08-11 14:17:47 -0700682static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct mmc_blk_data *md = mq->data;
685 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700686 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100687 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500689 /*
690 * Reliable writes are used to implement Forced Unit Access and
691 * REQ_META accesses, and are supported only on MMCs.
692 */
693 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
694 (req->cmd_flags & REQ_META)) &&
695 (rq_data_dir(req) == WRITE) &&
696 REL_WRITES_SUPPORTED(card);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400699 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300700 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 memset(&brq, 0, sizeof(struct mmc_blk_request));
703 brq.mrq.cmd = &brq.cmd;
704 brq.mrq.data = &brq.data;
705
Tejun Heo83096eb2009-05-07 22:24:39 +0900706 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800707 if (!mmc_card_blockaddr(card))
708 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700709 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200710 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 brq.stop.opcode = MMC_STOP_TRANSMISSION;
712 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700713 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900714 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Adrian Hunter6a79e392008-12-31 18:21:17 +0100716 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200717 * The block layer doesn't support all sector count
718 * restrictions, so we need to be prepared for too big
719 * requests.
720 */
721 if (brq.data.blocks > card->host->max_blk_count)
722 brq.data.blocks = card->host->max_blk_count;
723
724 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100725 * After a read error, we redo the request one sector at a time
726 * in order to accurately determine which sectors can be read
727 * successfully.
728 */
729 if (disable_multi && brq.data.blocks > 1)
730 brq.data.blocks = 1;
731
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500732 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700733 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500734 * token, not a STOP_TRANSMISSION request. Reliable
735 * writes use SET_BLOCK_COUNT and do not use a
736 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700737 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500738 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
739 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700740 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100741 readcmd = MMC_READ_MULTIPLE_BLOCK;
742 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000743 } else {
744 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100745 readcmd = MMC_READ_SINGLE_BLOCK;
746 writecmd = MMC_WRITE_BLOCK;
747 }
Russell Kingdb53f282006-08-30 15:14:56 +0100748 if (rq_data_dir(req) == READ) {
749 brq.cmd.opcode = readcmd;
750 brq.data.flags |= MMC_DATA_READ;
751 } else {
752 brq.cmd.opcode = writecmd;
753 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500756 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
757 goto cmd_err;
758
Pierre Ossmanb146d262007-07-24 19:16:54 +0200759 mmc_set_data_timeout(&brq.data, card);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200762 brq.data.sg_len = mmc_queue_map_sg(mq);
763
Adrian Hunter6a79e392008-12-31 18:21:17 +0100764 /*
765 * Adjust the sg list so it is the same size as the
766 * request.
767 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900768 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100769 int i, data_size = brq.data.blocks << 9;
770 struct scatterlist *sg;
771
772 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
773 data_size -= sg->length;
774 if (data_size <= 0) {
775 sg->length += data_size;
776 i++;
777 break;
778 }
779 }
780 brq.data.sg_len = i;
781 }
782
Pierre Ossman98ccf142007-05-12 00:26:16 +0200783 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200786
787 mmc_queue_bounce_post(mq);
788
Pierre Ossman979ce722008-06-29 12:19:47 +0200789 /*
790 * Check for errors here, but don't jump to cmd_err
791 * until later as we need to wait for the card to leave
792 * programming mode even when things go wrong.
793 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100794 if (brq.cmd.error || brq.data.error || brq.stop.error) {
795 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
796 /* Redo read one sector at a time */
797 printk(KERN_WARNING "%s: retrying using single "
798 "block read\n", req->rq_disk->disk_name);
799 disable_multi = 1;
800 continue;
801 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300802 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100803 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300806 printk(KERN_ERR "%s: error %d sending read/write "
807 "command, response %#x, card status %#x\n",
808 req->rq_disk->disk_name, brq.cmd.error,
809 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300813 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
814 /* 'Stop' response contains card status */
815 status = brq.mrq.stop->resp[0];
816 printk(KERN_ERR "%s: error %d transferring data,"
817 " sector %u, nr %u, card status %#x\n",
818 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900819 (unsigned)blk_rq_pos(req),
820 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300824 printk(KERN_ERR "%s: error %d sending stop command, "
825 "response %#x, card status %#x\n",
826 req->rq_disk->disk_name, brq.stop.error,
827 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
David Brownell7213d172007-08-08 09:10:23 -0700830 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100831 do {
832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Russell King2ed6d222006-09-24 10:46:43 +0100834 cmd.opcode = MMC_SEND_STATUS;
835 cmd.arg = card->rca << 16;
836 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
837 err = mmc_wait_for_cmd(card->host, &cmd, 5);
838 if (err) {
839 printk(KERN_ERR "%s: error %d requesting status\n",
840 req->rq_disk->disk_name, err);
841 goto cmd_err;
842 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100843 /*
844 * Some cards mishandle the status bits,
845 * so make sure to check both the busy
846 * indication and the card state.
847 */
848 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
849 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100852 if (cmd.resp[0] & ~0x00000900)
853 printk(KERN_ERR "%s: status = %08x\n",
854 req->rq_disk->disk_name, cmd.resp[0]);
855 if (mmc_decode_status(cmd.resp))
856 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#endif
Russell King2ed6d222006-09-24 10:46:43 +0100858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Adrian Hunter6a79e392008-12-31 18:21:17 +0100860 if (brq.cmd.error || brq.stop.error || brq.data.error) {
861 if (rq_data_dir(req) == READ) {
862 /*
863 * After an error, we redo I/O one sector at a
864 * time, so we only reach here after trying to
865 * read a single sector.
866 */
867 spin_lock_irq(&md->lock);
868 ret = __blk_end_request(req, -EIO, brq.data.blksz);
869 spin_unlock_irq(&md->lock);
870 continue;
871 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200872 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100873 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
876 * A block was successfully transferred.
877 */
878 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500879 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 spin_unlock_irq(&md->lock);
881 } while (ret);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 1;
884
885 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700886 /*
887 * If this is an SD card and we're writing, we can first
888 * mark the known good sectors as ok.
889 *
890 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200891 * as reported by the controller (which might be less than
892 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100894 if (mmc_card_sd(card)) {
895 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700896
Adrian Hunter6a79e392008-12-31 18:21:17 +0100897 blocks = mmc_sd_num_wr_blocks(card);
898 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700899 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100900 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700901 spin_unlock_irq(&md->lock);
902 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100903 } else {
904 spin_lock_irq(&md->lock);
905 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
906 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500910 while (ret)
911 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_unlock_irq(&md->lock);
913
914 return 0;
915}
916
Adrian Hunterbd788c92010-08-11 14:17:47 -0700917static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
918{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500919 int ret;
920 struct mmc_blk_data *md = mq->data;
921 struct mmc_card *card = md->queue.card;
922
923 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500924 ret = mmc_blk_part_switch(card, md);
925 if (ret) {
926 ret = 0;
927 goto out;
928 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500929
Adrian Hunter49804542010-08-11 14:17:50 -0700930 if (req->cmd_flags & REQ_DISCARD) {
931 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500932 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700933 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500934 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500935 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500936 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700937 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500938 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700939 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500940
Andrei Warkentin371a6892011-04-11 18:10:25 -0500941out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500942 mmc_release_host(card->host);
943 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700944}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Russell Kinga6f6c962006-01-03 22:38:44 +0000946static inline int mmc_blk_readonly(struct mmc_card *card)
947{
948 return mmc_card_readonly(card) ||
949 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
950}
951
Andrei Warkentin371a6892011-04-11 18:10:25 -0500952static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
953 struct device *parent,
954 sector_t size,
955 bool default_ro,
956 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct mmc_blk_data *md;
959 int devidx, ret;
960
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400961 devidx = find_first_zero_bit(dev_use, max_devices);
962 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return ERR_PTR(-ENOSPC);
964 __set_bit(devidx, dev_use);
965
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700966 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000967 if (!md) {
968 ret = -ENOMEM;
969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000971
Russell Kinga6f6c962006-01-03 22:38:44 +0000972 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500973 * !subname implies we are creating main mmc_blk_data that will be
974 * associated with mmc_card with mmc_set_drvdata. Due to device
975 * partitions, devidx will not coincide with a per-physical card
976 * index anymore so we keep track of a name index.
977 */
978 if (!subname) {
979 md->name_idx = find_first_zero_bit(name_use, max_devices);
980 __set_bit(md->name_idx, name_use);
981 }
982 else
983 md->name_idx = ((struct mmc_blk_data *)
984 dev_to_disk(parent)->private_data)->name_idx;
985
986 /*
Russell Kinga6f6c962006-01-03 22:38:44 +0000987 * Set the read-only status based on the supported commands
988 * and the write protect switch.
989 */
990 md->read_only = mmc_blk_readonly(card);
991
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400992 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000993 if (md->disk == NULL) {
994 ret = -ENOMEM;
995 goto err_kfree;
996 }
997
998 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500999 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001000 md->usage = 1;
1001
1002 ret = mmc_init_queue(&md->queue, card, &md->lock);
1003 if (ret)
1004 goto err_putdisk;
1005
Russell Kinga6f6c962006-01-03 22:38:44 +00001006 md->queue.issue_fn = mmc_blk_issue_rq;
1007 md->queue.data = md;
1008
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001009 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001010 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001011 md->disk->fops = &mmc_bdops;
1012 md->disk->private_data = md;
1013 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001014 md->disk->driverfs_dev = parent;
1015 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001016 if (REL_WRITES_SUPPORTED(card))
1017 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +00001018
1019 /*
1020 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1021 *
1022 * - be set for removable media with permanent block devices
1023 * - be unset for removable block devices with permanent media
1024 *
1025 * Since MMC block devices clearly fall under the second
1026 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1027 * should use the block device creation/destruction hotplug
1028 * messages to tell when the card is present.
1029 */
1030
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001031 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1032 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001033
Martin K. Petersene1defc42009-05-22 17:17:49 -04001034 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001035 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001037
1038 err_putdisk:
1039 put_disk(md->disk);
1040 err_kfree:
1041 kfree(md);
1042 out:
1043 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Andrei Warkentin371a6892011-04-11 18:10:25 -05001046static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1047{
1048 sector_t size;
1049 struct mmc_blk_data *md;
1050
1051 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1052 /*
1053 * The EXT_CSD sector count is in number or 512 byte
1054 * sectors.
1055 */
1056 size = card->ext_csd.sectors;
1057 } else {
1058 /*
1059 * The CSD capacity field is in units of read_blkbits.
1060 * set_capacity takes units of 512 bytes.
1061 */
1062 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1063 }
1064
1065 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1066 return md;
1067}
1068
1069static int mmc_blk_alloc_part(struct mmc_card *card,
1070 struct mmc_blk_data *md,
1071 unsigned int part_type,
1072 sector_t size,
1073 bool default_ro,
1074 const char *subname)
1075{
1076 char cap_str[10];
1077 struct mmc_blk_data *part_md;
1078
1079 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1080 subname);
1081 if (IS_ERR(part_md))
1082 return PTR_ERR(part_md);
1083 part_md->part_type = part_type;
1084 list_add(&part_md->part, &md->part);
1085
1086 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1087 cap_str, sizeof(cap_str));
1088 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1089 part_md->disk->disk_name, mmc_card_id(card),
1090 mmc_card_name(card), part_md->part_type, cap_str);
1091 return 0;
1092}
1093
1094static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1095{
1096 int ret = 0;
1097
1098 if (!mmc_card_mmc(card))
1099 return 0;
1100
1101 if (card->ext_csd.boot_size) {
1102 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1103 card->ext_csd.boot_size >> 9,
1104 true,
1105 "boot0");
1106 if (ret)
1107 return ret;
1108 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1109 card->ext_csd.boot_size >> 9,
1110 true,
1111 "boot1");
1112 if (ret)
1113 return ret;
1114 }
1115
1116 return ret;
1117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static int
1120mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int err;
1123
Pierre Ossmanb8558852007-01-03 19:47:29 +01001124 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001125 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001126 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001129 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1130 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EINVAL;
1132 }
1133
1134 return 0;
1135}
1136
Andrei Warkentin371a6892011-04-11 18:10:25 -05001137static void mmc_blk_remove_req(struct mmc_blk_data *md)
1138{
1139 if (md) {
1140 if (md->disk->flags & GENHD_FL_UP) {
1141 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1142
1143 /* Stop new requests from getting into the queue */
1144 del_gendisk(md->disk);
1145 }
1146
1147 /* Then flush out any already in there */
1148 mmc_cleanup_queue(&md->queue);
1149 mmc_blk_put(md);
1150 }
1151}
1152
1153static void mmc_blk_remove_parts(struct mmc_card *card,
1154 struct mmc_blk_data *md)
1155{
1156 struct list_head *pos, *q;
1157 struct mmc_blk_data *part_md;
1158
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001159 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001160 list_for_each_safe(pos, q, &md->part) {
1161 part_md = list_entry(pos, struct mmc_blk_data, part);
1162 list_del(pos);
1163 mmc_blk_remove_req(part_md);
1164 }
1165}
1166
1167static int mmc_add_disk(struct mmc_blk_data *md)
1168{
1169 int ret;
1170
1171 add_disk(md->disk);
1172 md->force_ro.show = force_ro_show;
1173 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301174 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001175 md->force_ro.attr.name = "force_ro";
1176 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1177 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1178 if (ret)
1179 del_gendisk(md->disk);
1180
1181 return ret;
1182}
1183
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001184static const struct mmc_fixup blk_fixups[] =
1185{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001186 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1187 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1188 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1189 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1190 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001191 END_FIXUP
1192};
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194static int mmc_blk_probe(struct mmc_card *card)
1195{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001196 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001198 char cap_str[10];
1199
Pierre Ossman912490d2005-05-21 10:27:02 +01001200 /*
1201 * Check that the card supports the command class(es) we need.
1202 */
1203 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return -ENODEV;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 md = mmc_blk_alloc(card);
1207 if (IS_ERR(md))
1208 return PTR_ERR(md);
1209
1210 err = mmc_blk_set_blksize(md, card);
1211 if (err)
1212 goto out;
1213
Yi Li444122f2009-02-05 15:31:57 +08001214 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001215 cap_str, sizeof(cap_str));
1216 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001218 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Andrei Warkentin371a6892011-04-11 18:10:25 -05001220 if (mmc_blk_alloc_parts(card, md))
1221 goto out;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001224 mmc_fixup_device(card, blk_fixups);
1225
Andrei Warkentin371a6892011-04-11 18:10:25 -05001226 if (mmc_add_disk(md))
1227 goto out;
1228
1229 list_for_each_entry(part_md, &md->part, part) {
1230 if (mmc_add_disk(part_md))
1231 goto out;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234
1235 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001236 mmc_blk_remove_parts(card, md);
1237 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return err;
1239}
1240
1241static void mmc_blk_remove(struct mmc_card *card)
1242{
1243 struct mmc_blk_data *md = mmc_get_drvdata(card);
1244
Andrei Warkentin371a6892011-04-11 18:10:25 -05001245 mmc_blk_remove_parts(card, md);
1246 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 mmc_set_drvdata(card, NULL);
1248}
1249
1250#ifdef CONFIG_PM
1251static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1252{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001253 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct mmc_blk_data *md = mmc_get_drvdata(card);
1255
1256 if (md) {
1257 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001258 list_for_each_entry(part_md, &md->part, part) {
1259 mmc_queue_suspend(&part_md->queue);
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262 return 0;
1263}
1264
1265static int mmc_blk_resume(struct mmc_card *card)
1266{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001267 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct mmc_blk_data *md = mmc_get_drvdata(card);
1269
1270 if (md) {
1271 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001272
1273 /*
1274 * Resume involves the card going into idle state,
1275 * so current partition is always the main one.
1276 */
1277 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001279 list_for_each_entry(part_md, &md->part, part) {
1280 mmc_queue_resume(&part_md->queue);
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 return 0;
1284}
1285#else
1286#define mmc_blk_suspend NULL
1287#define mmc_blk_resume NULL
1288#endif
1289
1290static struct mmc_driver mmc_driver = {
1291 .drv = {
1292 .name = "mmcblk",
1293 },
1294 .probe = mmc_blk_probe,
1295 .remove = mmc_blk_remove,
1296 .suspend = mmc_blk_suspend,
1297 .resume = mmc_blk_resume,
1298};
1299
1300static int __init mmc_blk_init(void)
1301{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001302 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001304 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1305 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1306
1307 max_devices = 256 / perdev_minors;
1308
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001309 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1310 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001313 res = mmc_register_driver(&mmc_driver);
1314 if (res)
1315 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001317 return 0;
1318 out2:
1319 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 out:
1321 return res;
1322}
1323
1324static void __exit mmc_blk_exit(void)
1325{
1326 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001327 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330module_init(mmc_blk_init);
1331module_exit(mmc_blk_exit);
1332
1333MODULE_LICENSE("GPL");
1334MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1335