blob: 93a7efcbfde96aec97f23e748ced27164807b608 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020036#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010037#include <linux/mmc/mmc.h>
38#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/system.h>
41#include <asm/uaccess.h>
42
Pierre Ossman98ac2162006-12-23 20:03:02 +010043#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000045MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040046#ifdef MODULE_PARAM_PREFIX
47#undef MODULE_PARAM_PREFIX
48#endif
49#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010050
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050051#define INAND_CMD38_ARG_EXT_CSD 113
52#define INAND_CMD38_ARG_ERASE 0x00
53#define INAND_CMD38_ARG_TRIM 0x01
54#define INAND_CMD38_ARG_SECERASE 0x80
55#define INAND_CMD38_ARG_SECTRIM1 0x81
56#define INAND_CMD38_ARG_SECTRIM2 0x88
57
Andrei Warkentinf4c55222011-03-31 18:40:00 -050058#define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \
59 (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \
60 ((card)->ext_csd.rel_sectors)))
61
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020062static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040063
64/*
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
67 */
68static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
69
70/*
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
73 */
74static int max_devices;
75
76/* 256 minors, so at most 256 separate devices */
77static DECLARE_BITMAP(dev_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * There is one mmc_blk_data per slot.
81 */
82struct mmc_blk_data {
83 spinlock_t lock;
84 struct gendisk *disk;
85 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050086 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000089 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050090 unsigned int part_type;
91
92 /*
93 * Only set in main mmc_blk_data associated
94 * with mmc_card with mmc_set_drvdata, and keeps
95 * track of the current selected device partition.
96 */
97 unsigned int part_curr;
98 struct device_attribute force_ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Arjan van de Vena621aae2006-01-12 18:43:35 +0000101static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400103module_param(perdev_minors, int, 0444);
104MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
107{
108 struct mmc_blk_data *md;
109
Arjan van de Vena621aae2006-01-12 18:43:35 +0000110 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 md = disk->private_data;
112 if (md && md->usage == 0)
113 md = NULL;
114 if (md)
115 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000116 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return md;
119}
120
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121static inline int mmc_get_devidx(struct gendisk *disk)
122{
123 int devmaj = MAJOR(disk_devt(disk));
124 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
125
126 if (!devmaj)
127 devidx = disk->first_minor / perdev_minors;
128 return devidx;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void mmc_blk_put(struct mmc_blk_data *md)
132{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000133 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 md->usage--;
135 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500136 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800137 blk_cleanup_queue(md->queue.queue);
138
David Woodhouse1dff3142007-11-21 18:45:12 +0100139 __clear_bit(devidx, dev_use);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 kfree(md);
143 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000144 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Andrei Warkentin371a6892011-04-11 18:10:25 -0500147static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
148 char *buf)
149{
150 int ret;
151 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
152
153 ret = snprintf(buf, PAGE_SIZE, "%d",
154 get_disk_ro(dev_to_disk(dev)) ^
155 md->read_only);
156 mmc_blk_put(md);
157 return ret;
158}
159
160static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
161 const char *buf, size_t count)
162{
163 int ret;
164 char *end;
165 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
166 unsigned long set = simple_strtoul(buf, &end, 0);
167 if (end == buf) {
168 ret = -EINVAL;
169 goto out;
170 }
171
172 set_disk_ro(dev_to_disk(dev), set || md->read_only);
173 ret = count;
174out:
175 mmc_blk_put(md);
176 return ret;
177}
178
Al Viroa5a15612008-03-02 10:33:30 -0500179static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Al Viroa5a15612008-03-02 10:33:30 -0500181 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int ret = -ENXIO;
183
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200184 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (md) {
186 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500187 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700189
Al Viroa5a15612008-03-02 10:33:30 -0500190 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700191 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700192 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200195 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 return ret;
198}
199
Al Viroa5a15612008-03-02 10:33:30 -0500200static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Al Viroa5a15612008-03-02 10:33:30 -0500202 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200204 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200206 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
209
210static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800211mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800213 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
214 geo->heads = 4;
215 geo->sectors = 16;
216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700219static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500220 .open = mmc_blk_open,
221 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800222 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .owner = THIS_MODULE,
224};
225
226struct mmc_blk_request {
227 struct mmc_request mrq;
228 struct mmc_command cmd;
229 struct mmc_command stop;
230 struct mmc_data data;
231};
232
Andrei Warkentin371a6892011-04-11 18:10:25 -0500233static inline int mmc_blk_part_switch(struct mmc_card *card,
234 struct mmc_blk_data *md)
235{
236 int ret;
237 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
238 if (main_md->part_curr == md->part_type)
239 return 0;
240
241 if (mmc_card_mmc(card)) {
242 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
243 card->ext_csd.part_config |= md->part_type;
244
245 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
246 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
247 card->ext_csd.part_time);
248 if (ret)
249 return ret;
250}
251
252 main_md->part_curr = md->part_type;
253 return 0;
254}
255
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700256static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
257{
258 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100259 u32 result;
260 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700261
262 struct mmc_request mrq;
Chris Ball1278dba2011-04-13 23:40:30 -0400263 struct mmc_command cmd = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700264 struct mmc_data data;
265 unsigned int timeout_us;
266
267 struct scatterlist sg;
268
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700269 cmd.opcode = MMC_APP_CMD;
270 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700271 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700272
273 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700274 if (err)
275 return (u32)-1;
276 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700277 return (u32)-1;
278
279 memset(&cmd, 0, sizeof(struct mmc_command));
280
281 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
282 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700283 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700284
285 memset(&data, 0, sizeof(struct mmc_data));
286
287 data.timeout_ns = card->csd.tacc_ns * 100;
288 data.timeout_clks = card->csd.tacc_clks * 100;
289
290 timeout_us = data.timeout_ns / 1000;
291 timeout_us += data.timeout_clks * 1000 /
292 (card->host->ios.clock / 1000);
293
294 if (timeout_us > 100000) {
295 data.timeout_ns = 100000000;
296 data.timeout_clks = 0;
297 }
298
299 data.blksz = 4;
300 data.blocks = 1;
301 data.flags = MMC_DATA_READ;
302 data.sg = &sg;
303 data.sg_len = 1;
304
305 memset(&mrq, 0, sizeof(struct mmc_request));
306
307 mrq.cmd = &cmd;
308 mrq.data = &data;
309
Ben Dooks051913d2009-06-08 23:33:57 +0100310 blocks = kmalloc(4, GFP_KERNEL);
311 if (!blocks)
312 return (u32)-1;
313
314 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700315
316 mmc_wait_for_req(card->host, &mrq);
317
Ben Dooks051913d2009-06-08 23:33:57 +0100318 result = ntohl(*blocks);
319 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700320
Ben Dooks051913d2009-06-08 23:33:57 +0100321 if (cmd.error || data.error)
322 result = (u32)-1;
323
324 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700325}
326
Adrian Hunter504f1912008-10-16 12:55:25 +0300327static u32 get_card_status(struct mmc_card *card, struct request *req)
328{
Chris Ball1278dba2011-04-13 23:40:30 -0400329 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300330 int err;
331
Adrian Hunter504f1912008-10-16 12:55:25 +0300332 cmd.opcode = MMC_SEND_STATUS;
333 if (!mmc_host_is_spi(card->host))
334 cmd.arg = card->rca << 16;
335 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
336 err = mmc_wait_for_cmd(card->host, &cmd, 0);
337 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400338 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300339 req->rq_disk->disk_name, err);
340 return cmd.resp[0];
341}
342
Adrian Hunterbd788c92010-08-11 14:17:47 -0700343static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
344{
345 struct mmc_blk_data *md = mq->data;
346 struct mmc_card *card = md->queue.card;
347 unsigned int from, nr, arg;
348 int err = 0;
349
Adrian Hunterbd788c92010-08-11 14:17:47 -0700350 if (!mmc_can_erase(card)) {
351 err = -EOPNOTSUPP;
352 goto out;
353 }
354
355 from = blk_rq_pos(req);
356 nr = blk_rq_sectors(req);
357
358 if (mmc_can_trim(card))
359 arg = MMC_TRIM_ARG;
360 else
361 arg = MMC_ERASE_ARG;
362
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500363 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
364 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
365 INAND_CMD38_ARG_EXT_CSD,
366 arg == MMC_TRIM_ARG ?
367 INAND_CMD38_ARG_TRIM :
368 INAND_CMD38_ARG_ERASE,
369 0);
370 if (err)
371 goto out;
372 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700373 err = mmc_erase(card, from, nr, arg);
374out:
375 spin_lock_irq(&md->lock);
376 __blk_end_request(req, err, blk_rq_bytes(req));
377 spin_unlock_irq(&md->lock);
378
Adrian Hunterbd788c92010-08-11 14:17:47 -0700379 return err ? 0 : 1;
380}
381
Adrian Hunter49804542010-08-11 14:17:50 -0700382static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
383 struct request *req)
384{
385 struct mmc_blk_data *md = mq->data;
386 struct mmc_card *card = md->queue.card;
387 unsigned int from, nr, arg;
388 int err = 0;
389
Adrian Hunter49804542010-08-11 14:17:50 -0700390 if (!mmc_can_secure_erase_trim(card)) {
391 err = -EOPNOTSUPP;
392 goto out;
393 }
394
395 from = blk_rq_pos(req);
396 nr = blk_rq_sectors(req);
397
398 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
399 arg = MMC_SECURE_TRIM1_ARG;
400 else
401 arg = MMC_SECURE_ERASE_ARG;
402
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500403 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
404 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
405 INAND_CMD38_ARG_EXT_CSD,
406 arg == MMC_SECURE_TRIM1_ARG ?
407 INAND_CMD38_ARG_SECTRIM1 :
408 INAND_CMD38_ARG_SECERASE,
409 0);
410 if (err)
411 goto out;
412 }
Adrian Hunter49804542010-08-11 14:17:50 -0700413 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500414 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
415 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
416 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
417 INAND_CMD38_ARG_EXT_CSD,
418 INAND_CMD38_ARG_SECTRIM2,
419 0);
420 if (err)
421 goto out;
422 }
Adrian Hunter49804542010-08-11 14:17:50 -0700423 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500424 }
Adrian Hunter49804542010-08-11 14:17:50 -0700425out:
426 spin_lock_irq(&md->lock);
427 __blk_end_request(req, err, blk_rq_bytes(req));
428 spin_unlock_irq(&md->lock);
429
Adrian Hunter49804542010-08-11 14:17:50 -0700430 return err ? 0 : 1;
431}
432
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500433static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
434{
435 struct mmc_blk_data *md = mq->data;
436
437 /*
438 * No-op, only service this because we need REQ_FUA for reliable
439 * writes.
440 */
441 spin_lock_irq(&md->lock);
442 __blk_end_request_all(req, 0);
443 spin_unlock_irq(&md->lock);
444
445 return 1;
446}
447
448/*
449 * Reformat current write as a reliable write, supporting
450 * both legacy and the enhanced reliable write MMC cards.
451 * In each transfer we'll handle only as much as a single
452 * reliable write can handle, thus finish the request in
453 * partial completions.
454 */
455static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
456 struct mmc_card *card,
457 struct request *req)
458{
459 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400460 struct mmc_command set_count = {0};
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500461
462 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
463 /* Legacy mode imposes restrictions on transfers. */
464 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
465 brq->data.blocks = 1;
466
467 if (brq->data.blocks > card->ext_csd.rel_sectors)
468 brq->data.blocks = card->ext_csd.rel_sectors;
469 else if (brq->data.blocks < card->ext_csd.rel_sectors)
470 brq->data.blocks = 1;
471 }
472
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500473 set_count.opcode = MMC_SET_BLOCK_COUNT;
474 set_count.arg = brq->data.blocks | (1 << 31);
475 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
476 err = mmc_wait_for_cmd(card->host, &set_count, 0);
477 if (err)
478 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
479 req->rq_disk->disk_name, err);
480 return err;
481}
482
Adrian Hunterbd788c92010-08-11 14:17:47 -0700483static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct mmc_blk_data *md = mq->data;
486 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700487 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100488 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500490 /*
491 * Reliable writes are used to implement Forced Unit Access and
492 * REQ_META accesses, and are supported only on MMCs.
493 */
494 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
495 (req->cmd_flags & REQ_META)) &&
496 (rq_data_dir(req) == WRITE) &&
497 REL_WRITES_SUPPORTED(card);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400500 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300501 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 memset(&brq, 0, sizeof(struct mmc_blk_request));
504 brq.mrq.cmd = &brq.cmd;
505 brq.mrq.data = &brq.data;
506
Tejun Heo83096eb2009-05-07 22:24:39 +0900507 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800508 if (!mmc_card_blockaddr(card))
509 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700510 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200511 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 brq.stop.opcode = MMC_STOP_TRANSMISSION;
513 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700514 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900515 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Adrian Hunter6a79e392008-12-31 18:21:17 +0100517 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200518 * The block layer doesn't support all sector count
519 * restrictions, so we need to be prepared for too big
520 * requests.
521 */
522 if (brq.data.blocks > card->host->max_blk_count)
523 brq.data.blocks = card->host->max_blk_count;
524
525 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100526 * After a read error, we redo the request one sector at a time
527 * in order to accurately determine which sectors can be read
528 * successfully.
529 */
530 if (disable_multi && brq.data.blocks > 1)
531 brq.data.blocks = 1;
532
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500533 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700534 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500535 * token, not a STOP_TRANSMISSION request. Reliable
536 * writes use SET_BLOCK_COUNT and do not use a
537 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700538 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500539 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
540 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700541 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100542 readcmd = MMC_READ_MULTIPLE_BLOCK;
543 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000544 } else {
545 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100546 readcmd = MMC_READ_SINGLE_BLOCK;
547 writecmd = MMC_WRITE_BLOCK;
548 }
Russell Kingdb53f282006-08-30 15:14:56 +0100549 if (rq_data_dir(req) == READ) {
550 brq.cmd.opcode = readcmd;
551 brq.data.flags |= MMC_DATA_READ;
552 } else {
553 brq.cmd.opcode = writecmd;
554 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500557 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
558 goto cmd_err;
559
Pierre Ossmanb146d262007-07-24 19:16:54 +0200560 mmc_set_data_timeout(&brq.data, card);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200563 brq.data.sg_len = mmc_queue_map_sg(mq);
564
Adrian Hunter6a79e392008-12-31 18:21:17 +0100565 /*
566 * Adjust the sg list so it is the same size as the
567 * request.
568 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900569 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100570 int i, data_size = brq.data.blocks << 9;
571 struct scatterlist *sg;
572
573 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
574 data_size -= sg->length;
575 if (data_size <= 0) {
576 sg->length += data_size;
577 i++;
578 break;
579 }
580 }
581 brq.data.sg_len = i;
582 }
583
Pierre Ossman98ccf142007-05-12 00:26:16 +0200584 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200587
588 mmc_queue_bounce_post(mq);
589
Pierre Ossman979ce722008-06-29 12:19:47 +0200590 /*
591 * Check for errors here, but don't jump to cmd_err
592 * until later as we need to wait for the card to leave
593 * programming mode even when things go wrong.
594 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100595 if (brq.cmd.error || brq.data.error || brq.stop.error) {
596 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
597 /* Redo read one sector at a time */
598 printk(KERN_WARNING "%s: retrying using single "
599 "block read\n", req->rq_disk->disk_name);
600 disable_multi = 1;
601 continue;
602 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300603 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100604 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300607 printk(KERN_ERR "%s: error %d sending read/write "
608 "command, response %#x, card status %#x\n",
609 req->rq_disk->disk_name, brq.cmd.error,
610 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300614 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
615 /* 'Stop' response contains card status */
616 status = brq.mrq.stop->resp[0];
617 printk(KERN_ERR "%s: error %d transferring data,"
618 " sector %u, nr %u, card status %#x\n",
619 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900620 (unsigned)blk_rq_pos(req),
621 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300625 printk(KERN_ERR "%s: error %d sending stop command, "
626 "response %#x, card status %#x\n",
627 req->rq_disk->disk_name, brq.stop.error,
628 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
David Brownell7213d172007-08-08 09:10:23 -0700631 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100632 do {
633 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Russell King2ed6d222006-09-24 10:46:43 +0100635 cmd.opcode = MMC_SEND_STATUS;
636 cmd.arg = card->rca << 16;
637 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
638 err = mmc_wait_for_cmd(card->host, &cmd, 5);
639 if (err) {
640 printk(KERN_ERR "%s: error %d requesting status\n",
641 req->rq_disk->disk_name, err);
642 goto cmd_err;
643 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100644 /*
645 * Some cards mishandle the status bits,
646 * so make sure to check both the busy
647 * indication and the card state.
648 */
649 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
650 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100653 if (cmd.resp[0] & ~0x00000900)
654 printk(KERN_ERR "%s: status = %08x\n",
655 req->rq_disk->disk_name, cmd.resp[0]);
656 if (mmc_decode_status(cmd.resp))
657 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#endif
Russell King2ed6d222006-09-24 10:46:43 +0100659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Adrian Hunter6a79e392008-12-31 18:21:17 +0100661 if (brq.cmd.error || brq.stop.error || brq.data.error) {
662 if (rq_data_dir(req) == READ) {
663 /*
664 * After an error, we redo I/O one sector at a
665 * time, so we only reach here after trying to
666 * read a single sector.
667 */
668 spin_lock_irq(&md->lock);
669 ret = __blk_end_request(req, -EIO, brq.data.blksz);
670 spin_unlock_irq(&md->lock);
671 continue;
672 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200673 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100674 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * A block was successfully transferred.
678 */
679 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500680 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_unlock_irq(&md->lock);
682 } while (ret);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 1;
685
686 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700687 /*
688 * If this is an SD card and we're writing, we can first
689 * mark the known good sectors as ok.
690 *
691 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200692 * as reported by the controller (which might be less than
693 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100695 if (mmc_card_sd(card)) {
696 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700697
Adrian Hunter6a79e392008-12-31 18:21:17 +0100698 blocks = mmc_sd_num_wr_blocks(card);
699 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700700 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100701 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700702 spin_unlock_irq(&md->lock);
703 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100704 } else {
705 spin_lock_irq(&md->lock);
706 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
707 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500711 while (ret)
712 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_unlock_irq(&md->lock);
714
715 return 0;
716}
717
Adrian Hunterbd788c92010-08-11 14:17:47 -0700718static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
719{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500720 int ret;
721 struct mmc_blk_data *md = mq->data;
722 struct mmc_card *card = md->queue.card;
723
724 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500725 ret = mmc_blk_part_switch(card, md);
726 if (ret) {
727 ret = 0;
728 goto out;
729 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500730
Adrian Hunter49804542010-08-11 14:17:50 -0700731 if (req->cmd_flags & REQ_DISCARD) {
732 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500733 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700734 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500735 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500736 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500737 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700738 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500739 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700740 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500741
Andrei Warkentin371a6892011-04-11 18:10:25 -0500742out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500743 mmc_release_host(card->host);
744 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Russell Kinga6f6c962006-01-03 22:38:44 +0000747static inline int mmc_blk_readonly(struct mmc_card *card)
748{
749 return mmc_card_readonly(card) ||
750 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
751}
752
Andrei Warkentin371a6892011-04-11 18:10:25 -0500753static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
754 struct device *parent,
755 sector_t size,
756 bool default_ro,
757 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct mmc_blk_data *md;
760 int devidx, ret;
761
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400762 devidx = find_first_zero_bit(dev_use, max_devices);
763 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return ERR_PTR(-ENOSPC);
765 __set_bit(devidx, dev_use);
766
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700767 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000768 if (!md) {
769 ret = -ENOMEM;
770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000772
Russell Kinga6f6c962006-01-03 22:38:44 +0000773 /*
774 * Set the read-only status based on the supported commands
775 * and the write protect switch.
776 */
777 md->read_only = mmc_blk_readonly(card);
778
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400779 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000780 if (md->disk == NULL) {
781 ret = -ENOMEM;
782 goto err_kfree;
783 }
784
785 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500786 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +0000787 md->usage = 1;
788
789 ret = mmc_init_queue(&md->queue, card, &md->lock);
790 if (ret)
791 goto err_putdisk;
792
Russell Kinga6f6c962006-01-03 22:38:44 +0000793 md->queue.issue_fn = mmc_blk_issue_rq;
794 md->queue.data = md;
795
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200796 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400797 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +0000798 md->disk->fops = &mmc_bdops;
799 md->disk->private_data = md;
800 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500801 md->disk->driverfs_dev = parent;
802 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500803 if (REL_WRITES_SUPPORTED(card))
804 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +0000805
806 /*
807 * As discussed on lkml, GENHD_FL_REMOVABLE should:
808 *
809 * - be set for removable media with permanent block devices
810 * - be unset for removable block devices with permanent media
811 *
812 * Since MMC block devices clearly fall under the second
813 * case, we do not set GENHD_FL_REMOVABLE. Userspace
814 * should use the block device creation/destruction hotplug
815 * messages to tell when the card is present.
816 */
817
Andrei Warkentin371a6892011-04-11 18:10:25 -0500818 if (subname)
819 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
820 "mmcblk%d%s",
821 mmc_get_devidx(dev_to_disk(parent)), subname);
822 else
823 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
824 "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000825
Martin K. Petersene1defc42009-05-22 17:17:49 -0400826 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500827 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000829
830 err_putdisk:
831 put_disk(md->disk);
832 err_kfree:
833 kfree(md);
834 out:
835 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Andrei Warkentin371a6892011-04-11 18:10:25 -0500838static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
839{
840 sector_t size;
841 struct mmc_blk_data *md;
842
843 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
844 /*
845 * The EXT_CSD sector count is in number or 512 byte
846 * sectors.
847 */
848 size = card->ext_csd.sectors;
849 } else {
850 /*
851 * The CSD capacity field is in units of read_blkbits.
852 * set_capacity takes units of 512 bytes.
853 */
854 size = card->csd.capacity << (card->csd.read_blkbits - 9);
855 }
856
857 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
858 return md;
859}
860
861static int mmc_blk_alloc_part(struct mmc_card *card,
862 struct mmc_blk_data *md,
863 unsigned int part_type,
864 sector_t size,
865 bool default_ro,
866 const char *subname)
867{
868 char cap_str[10];
869 struct mmc_blk_data *part_md;
870
871 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
872 subname);
873 if (IS_ERR(part_md))
874 return PTR_ERR(part_md);
875 part_md->part_type = part_type;
876 list_add(&part_md->part, &md->part);
877
878 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
879 cap_str, sizeof(cap_str));
880 printk(KERN_INFO "%s: %s %s partition %u %s\n",
881 part_md->disk->disk_name, mmc_card_id(card),
882 mmc_card_name(card), part_md->part_type, cap_str);
883 return 0;
884}
885
886static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
887{
888 int ret = 0;
889
890 if (!mmc_card_mmc(card))
891 return 0;
892
893 if (card->ext_csd.boot_size) {
894 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
895 card->ext_csd.boot_size >> 9,
896 true,
897 "boot0");
898 if (ret)
899 return ret;
900 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
901 card->ext_csd.boot_size >> 9,
902 true,
903 "boot1");
904 if (ret)
905 return ret;
906 }
907
908 return ret;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int
912mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int err;
915
Pierre Ossmanb8558852007-01-03 19:47:29 +0100916 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300917 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100918 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300921 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
922 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EINVAL;
924 }
925
926 return 0;
927}
928
Andrei Warkentin371a6892011-04-11 18:10:25 -0500929static void mmc_blk_remove_req(struct mmc_blk_data *md)
930{
931 if (md) {
932 if (md->disk->flags & GENHD_FL_UP) {
933 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
934
935 /* Stop new requests from getting into the queue */
936 del_gendisk(md->disk);
937 }
938
939 /* Then flush out any already in there */
940 mmc_cleanup_queue(&md->queue);
941 mmc_blk_put(md);
942 }
943}
944
945static void mmc_blk_remove_parts(struct mmc_card *card,
946 struct mmc_blk_data *md)
947{
948 struct list_head *pos, *q;
949 struct mmc_blk_data *part_md;
950
951 list_for_each_safe(pos, q, &md->part) {
952 part_md = list_entry(pos, struct mmc_blk_data, part);
953 list_del(pos);
954 mmc_blk_remove_req(part_md);
955 }
956}
957
958static int mmc_add_disk(struct mmc_blk_data *md)
959{
960 int ret;
961
962 add_disk(md->disk);
963 md->force_ro.show = force_ro_show;
964 md->force_ro.store = force_ro_store;
965 md->force_ro.attr.name = "force_ro";
966 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
967 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
968 if (ret)
969 del_gendisk(md->disk);
970
971 return ret;
972}
973
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400974static const struct mmc_fixup blk_fixups[] =
975{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500976 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
977 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
978 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
979 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
980 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400981 END_FIXUP
982};
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int mmc_blk_probe(struct mmc_card *card)
985{
Andrei Warkentin371a6892011-04-11 18:10:25 -0500986 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200988 char cap_str[10];
989
Pierre Ossman912490d2005-05-21 10:27:02 +0100990 /*
991 * Check that the card supports the command class(es) we need.
992 */
993 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -ENODEV;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 md = mmc_blk_alloc(card);
997 if (IS_ERR(md))
998 return PTR_ERR(md);
999
1000 err = mmc_blk_set_blksize(md, card);
1001 if (err)
1002 goto out;
1003
Yi Li444122f2009-02-05 15:31:57 +08001004 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001005 cap_str, sizeof(cap_str));
1006 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001008 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Andrei Warkentin371a6892011-04-11 18:10:25 -05001010 if (mmc_blk_alloc_parts(card, md))
1011 goto out;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001014 mmc_fixup_device(card, blk_fixups);
1015
Andrei Warkentin371a6892011-04-11 18:10:25 -05001016 if (mmc_add_disk(md))
1017 goto out;
1018
1019 list_for_each_entry(part_md, &md->part, part) {
1020 if (mmc_add_disk(part_md))
1021 goto out;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024
1025 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001026 mmc_blk_remove_parts(card, md);
1027 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return err;
1029}
1030
1031static void mmc_blk_remove(struct mmc_card *card)
1032{
1033 struct mmc_blk_data *md = mmc_get_drvdata(card);
1034
Andrei Warkentin371a6892011-04-11 18:10:25 -05001035 mmc_blk_remove_parts(card, md);
1036 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 mmc_set_drvdata(card, NULL);
1038}
1039
1040#ifdef CONFIG_PM
1041static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1042{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001043 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct mmc_blk_data *md = mmc_get_drvdata(card);
1045
1046 if (md) {
1047 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001048 list_for_each_entry(part_md, &md->part, part) {
1049 mmc_queue_suspend(&part_md->queue);
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052 return 0;
1053}
1054
1055static int mmc_blk_resume(struct mmc_card *card)
1056{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001057 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct mmc_blk_data *md = mmc_get_drvdata(card);
1059
1060 if (md) {
1061 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001062
1063 /*
1064 * Resume involves the card going into idle state,
1065 * so current partition is always the main one.
1066 */
1067 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001069 list_for_each_entry(part_md, &md->part, part) {
1070 mmc_queue_resume(&part_md->queue);
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 return 0;
1074}
1075#else
1076#define mmc_blk_suspend NULL
1077#define mmc_blk_resume NULL
1078#endif
1079
1080static struct mmc_driver mmc_driver = {
1081 .drv = {
1082 .name = "mmcblk",
1083 },
1084 .probe = mmc_blk_probe,
1085 .remove = mmc_blk_remove,
1086 .suspend = mmc_blk_suspend,
1087 .resume = mmc_blk_resume,
1088};
1089
1090static int __init mmc_blk_init(void)
1091{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001092 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001094 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1095 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1096
1097 max_devices = 256 / perdev_minors;
1098
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001099 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1100 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001103 res = mmc_register_driver(&mmc_driver);
1104 if (res)
1105 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001107 return 0;
1108 out2:
1109 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 out:
1111 return res;
1112}
1113
1114static void __exit mmc_blk_exit(void)
1115{
1116 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001117 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120module_init(mmc_blk_init);
1121module_exit(mmc_blk_exit);
1122
1123MODULE_LICENSE("GPL");
1124MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1125