blob: c20995323348c3c30999e49819a1cfe6d132dc49 [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;
263 struct mmc_command cmd;
264 struct mmc_data data;
265 unsigned int timeout_us;
266
267 struct scatterlist sg;
268
269 memset(&cmd, 0, sizeof(struct mmc_command));
270
271 cmd.opcode = MMC_APP_CMD;
272 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700273 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700274
275 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700276 if (err)
277 return (u32)-1;
278 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700279 return (u32)-1;
280
281 memset(&cmd, 0, sizeof(struct mmc_command));
282
283 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
284 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700285 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700286
287 memset(&data, 0, sizeof(struct mmc_data));
288
289 data.timeout_ns = card->csd.tacc_ns * 100;
290 data.timeout_clks = card->csd.tacc_clks * 100;
291
292 timeout_us = data.timeout_ns / 1000;
293 timeout_us += data.timeout_clks * 1000 /
294 (card->host->ios.clock / 1000);
295
296 if (timeout_us > 100000) {
297 data.timeout_ns = 100000000;
298 data.timeout_clks = 0;
299 }
300
301 data.blksz = 4;
302 data.blocks = 1;
303 data.flags = MMC_DATA_READ;
304 data.sg = &sg;
305 data.sg_len = 1;
306
307 memset(&mrq, 0, sizeof(struct mmc_request));
308
309 mrq.cmd = &cmd;
310 mrq.data = &data;
311
Ben Dooks051913d2009-06-08 23:33:57 +0100312 blocks = kmalloc(4, GFP_KERNEL);
313 if (!blocks)
314 return (u32)-1;
315
316 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700317
318 mmc_wait_for_req(card->host, &mrq);
319
Ben Dooks051913d2009-06-08 23:33:57 +0100320 result = ntohl(*blocks);
321 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700322
Ben Dooks051913d2009-06-08 23:33:57 +0100323 if (cmd.error || data.error)
324 result = (u32)-1;
325
326 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700327}
328
Adrian Hunter504f1912008-10-16 12:55:25 +0300329static u32 get_card_status(struct mmc_card *card, struct request *req)
330{
331 struct mmc_command cmd;
332 int err;
333
334 memset(&cmd, 0, sizeof(struct mmc_command));
335 cmd.opcode = MMC_SEND_STATUS;
336 if (!mmc_host_is_spi(card->host))
337 cmd.arg = card->rca << 16;
338 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
339 err = mmc_wait_for_cmd(card->host, &cmd, 0);
340 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400341 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300342 req->rq_disk->disk_name, err);
343 return cmd.resp[0];
344}
345
Adrian Hunterbd788c92010-08-11 14:17:47 -0700346static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
347{
348 struct mmc_blk_data *md = mq->data;
349 struct mmc_card *card = md->queue.card;
350 unsigned int from, nr, arg;
351 int err = 0;
352
Adrian Hunterbd788c92010-08-11 14:17:47 -0700353 if (!mmc_can_erase(card)) {
354 err = -EOPNOTSUPP;
355 goto out;
356 }
357
358 from = blk_rq_pos(req);
359 nr = blk_rq_sectors(req);
360
361 if (mmc_can_trim(card))
362 arg = MMC_TRIM_ARG;
363 else
364 arg = MMC_ERASE_ARG;
365
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500366 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
367 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
368 INAND_CMD38_ARG_EXT_CSD,
369 arg == MMC_TRIM_ARG ?
370 INAND_CMD38_ARG_TRIM :
371 INAND_CMD38_ARG_ERASE,
372 0);
373 if (err)
374 goto out;
375 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700376 err = mmc_erase(card, from, nr, arg);
377out:
378 spin_lock_irq(&md->lock);
379 __blk_end_request(req, err, blk_rq_bytes(req));
380 spin_unlock_irq(&md->lock);
381
Adrian Hunterbd788c92010-08-11 14:17:47 -0700382 return err ? 0 : 1;
383}
384
Adrian Hunter49804542010-08-11 14:17:50 -0700385static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
386 struct request *req)
387{
388 struct mmc_blk_data *md = mq->data;
389 struct mmc_card *card = md->queue.card;
390 unsigned int from, nr, arg;
391 int err = 0;
392
Adrian Hunter49804542010-08-11 14:17:50 -0700393 if (!mmc_can_secure_erase_trim(card)) {
394 err = -EOPNOTSUPP;
395 goto out;
396 }
397
398 from = blk_rq_pos(req);
399 nr = blk_rq_sectors(req);
400
401 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
402 arg = MMC_SECURE_TRIM1_ARG;
403 else
404 arg = MMC_SECURE_ERASE_ARG;
405
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500406 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
407 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
408 INAND_CMD38_ARG_EXT_CSD,
409 arg == MMC_SECURE_TRIM1_ARG ?
410 INAND_CMD38_ARG_SECTRIM1 :
411 INAND_CMD38_ARG_SECERASE,
412 0);
413 if (err)
414 goto out;
415 }
Adrian Hunter49804542010-08-11 14:17:50 -0700416 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500417 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
418 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
419 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
420 INAND_CMD38_ARG_EXT_CSD,
421 INAND_CMD38_ARG_SECTRIM2,
422 0);
423 if (err)
424 goto out;
425 }
Adrian Hunter49804542010-08-11 14:17:50 -0700426 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500427 }
Adrian Hunter49804542010-08-11 14:17:50 -0700428out:
429 spin_lock_irq(&md->lock);
430 __blk_end_request(req, err, blk_rq_bytes(req));
431 spin_unlock_irq(&md->lock);
432
Adrian Hunter49804542010-08-11 14:17:50 -0700433 return err ? 0 : 1;
434}
435
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500436static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
437{
438 struct mmc_blk_data *md = mq->data;
439
440 /*
441 * No-op, only service this because we need REQ_FUA for reliable
442 * writes.
443 */
444 spin_lock_irq(&md->lock);
445 __blk_end_request_all(req, 0);
446 spin_unlock_irq(&md->lock);
447
448 return 1;
449}
450
451/*
452 * Reformat current write as a reliable write, supporting
453 * both legacy and the enhanced reliable write MMC cards.
454 * In each transfer we'll handle only as much as a single
455 * reliable write can handle, thus finish the request in
456 * partial completions.
457 */
458static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
459 struct mmc_card *card,
460 struct request *req)
461{
462 int err;
463 struct mmc_command set_count;
464
465 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
466 /* Legacy mode imposes restrictions on transfers. */
467 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
468 brq->data.blocks = 1;
469
470 if (brq->data.blocks > card->ext_csd.rel_sectors)
471 brq->data.blocks = card->ext_csd.rel_sectors;
472 else if (brq->data.blocks < card->ext_csd.rel_sectors)
473 brq->data.blocks = 1;
474 }
475
476 memset(&set_count, 0, sizeof(struct mmc_command));
477 set_count.opcode = MMC_SET_BLOCK_COUNT;
478 set_count.arg = brq->data.blocks | (1 << 31);
479 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
480 err = mmc_wait_for_cmd(card->host, &set_count, 0);
481 if (err)
482 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
483 req->rq_disk->disk_name, err);
484 return err;
485}
486
Adrian Hunterbd788c92010-08-11 14:17:47 -0700487static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct mmc_blk_data *md = mq->data;
490 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700491 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100492 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500494 /*
495 * Reliable writes are used to implement Forced Unit Access and
496 * REQ_META accesses, and are supported only on MMCs.
497 */
498 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
499 (req->cmd_flags & REQ_META)) &&
500 (rq_data_dir(req) == WRITE) &&
501 REL_WRITES_SUPPORTED(card);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct mmc_command cmd;
Adrian Hunter504f1912008-10-16 12:55:25 +0300505 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 memset(&brq, 0, sizeof(struct mmc_blk_request));
508 brq.mrq.cmd = &brq.cmd;
509 brq.mrq.data = &brq.data;
510
Tejun Heo83096eb2009-05-07 22:24:39 +0900511 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800512 if (!mmc_card_blockaddr(card))
513 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700514 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200515 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 brq.stop.opcode = MMC_STOP_TRANSMISSION;
517 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700518 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900519 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Adrian Hunter6a79e392008-12-31 18:21:17 +0100521 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200522 * The block layer doesn't support all sector count
523 * restrictions, so we need to be prepared for too big
524 * requests.
525 */
526 if (brq.data.blocks > card->host->max_blk_count)
527 brq.data.blocks = card->host->max_blk_count;
528
529 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100530 * After a read error, we redo the request one sector at a time
531 * in order to accurately determine which sectors can be read
532 * successfully.
533 */
534 if (disable_multi && brq.data.blocks > 1)
535 brq.data.blocks = 1;
536
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500537 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700538 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500539 * token, not a STOP_TRANSMISSION request. Reliable
540 * writes use SET_BLOCK_COUNT and do not use a
541 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700542 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500543 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
544 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700545 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100546 readcmd = MMC_READ_MULTIPLE_BLOCK;
547 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000548 } else {
549 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100550 readcmd = MMC_READ_SINGLE_BLOCK;
551 writecmd = MMC_WRITE_BLOCK;
552 }
Russell Kingdb53f282006-08-30 15:14:56 +0100553 if (rq_data_dir(req) == READ) {
554 brq.cmd.opcode = readcmd;
555 brq.data.flags |= MMC_DATA_READ;
556 } else {
557 brq.cmd.opcode = writecmd;
558 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500561 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
562 goto cmd_err;
563
Pierre Ossmanb146d262007-07-24 19:16:54 +0200564 mmc_set_data_timeout(&brq.data, card);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200567 brq.data.sg_len = mmc_queue_map_sg(mq);
568
Adrian Hunter6a79e392008-12-31 18:21:17 +0100569 /*
570 * Adjust the sg list so it is the same size as the
571 * request.
572 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900573 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100574 int i, data_size = brq.data.blocks << 9;
575 struct scatterlist *sg;
576
577 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
578 data_size -= sg->length;
579 if (data_size <= 0) {
580 sg->length += data_size;
581 i++;
582 break;
583 }
584 }
585 brq.data.sg_len = i;
586 }
587
Pierre Ossman98ccf142007-05-12 00:26:16 +0200588 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200591
592 mmc_queue_bounce_post(mq);
593
Pierre Ossman979ce722008-06-29 12:19:47 +0200594 /*
595 * Check for errors here, but don't jump to cmd_err
596 * until later as we need to wait for the card to leave
597 * programming mode even when things go wrong.
598 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100599 if (brq.cmd.error || brq.data.error || brq.stop.error) {
600 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
601 /* Redo read one sector at a time */
602 printk(KERN_WARNING "%s: retrying using single "
603 "block read\n", req->rq_disk->disk_name);
604 disable_multi = 1;
605 continue;
606 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300607 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100608 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300611 printk(KERN_ERR "%s: error %d sending read/write "
612 "command, response %#x, card status %#x\n",
613 req->rq_disk->disk_name, brq.cmd.error,
614 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300618 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
619 /* 'Stop' response contains card status */
620 status = brq.mrq.stop->resp[0];
621 printk(KERN_ERR "%s: error %d transferring data,"
622 " sector %u, nr %u, card status %#x\n",
623 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900624 (unsigned)blk_rq_pos(req),
625 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300629 printk(KERN_ERR "%s: error %d sending stop command, "
630 "response %#x, card status %#x\n",
631 req->rq_disk->disk_name, brq.stop.error,
632 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
David Brownell7213d172007-08-08 09:10:23 -0700635 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100636 do {
637 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Russell King2ed6d222006-09-24 10:46:43 +0100639 cmd.opcode = MMC_SEND_STATUS;
640 cmd.arg = card->rca << 16;
641 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
642 err = mmc_wait_for_cmd(card->host, &cmd, 5);
643 if (err) {
644 printk(KERN_ERR "%s: error %d requesting status\n",
645 req->rq_disk->disk_name, err);
646 goto cmd_err;
647 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100648 /*
649 * Some cards mishandle the status bits,
650 * so make sure to check both the busy
651 * indication and the card state.
652 */
653 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
654 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100657 if (cmd.resp[0] & ~0x00000900)
658 printk(KERN_ERR "%s: status = %08x\n",
659 req->rq_disk->disk_name, cmd.resp[0]);
660 if (mmc_decode_status(cmd.resp))
661 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#endif
Russell King2ed6d222006-09-24 10:46:43 +0100663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Adrian Hunter6a79e392008-12-31 18:21:17 +0100665 if (brq.cmd.error || brq.stop.error || brq.data.error) {
666 if (rq_data_dir(req) == READ) {
667 /*
668 * After an error, we redo I/O one sector at a
669 * time, so we only reach here after trying to
670 * read a single sector.
671 */
672 spin_lock_irq(&md->lock);
673 ret = __blk_end_request(req, -EIO, brq.data.blksz);
674 spin_unlock_irq(&md->lock);
675 continue;
676 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200677 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100678 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * A block was successfully transferred.
682 */
683 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500684 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 spin_unlock_irq(&md->lock);
686 } while (ret);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 1;
689
690 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700691 /*
692 * If this is an SD card and we're writing, we can first
693 * mark the known good sectors as ok.
694 *
695 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200696 * as reported by the controller (which might be less than
697 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100699 if (mmc_card_sd(card)) {
700 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700701
Adrian Hunter6a79e392008-12-31 18:21:17 +0100702 blocks = mmc_sd_num_wr_blocks(card);
703 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700704 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100705 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700706 spin_unlock_irq(&md->lock);
707 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100708 } else {
709 spin_lock_irq(&md->lock);
710 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
711 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500715 while (ret)
716 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 spin_unlock_irq(&md->lock);
718
719 return 0;
720}
721
Adrian Hunterbd788c92010-08-11 14:17:47 -0700722static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
723{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500724 int ret;
725 struct mmc_blk_data *md = mq->data;
726 struct mmc_card *card = md->queue.card;
727
728 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500729 ret = mmc_blk_part_switch(card, md);
730 if (ret) {
731 ret = 0;
732 goto out;
733 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500734
Adrian Hunter49804542010-08-11 14:17:50 -0700735 if (req->cmd_flags & REQ_DISCARD) {
736 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500737 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700738 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500739 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500740 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500741 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700742 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500743 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700744 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500745
Andrei Warkentin371a6892011-04-11 18:10:25 -0500746out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500747 mmc_release_host(card->host);
748 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Russell Kinga6f6c962006-01-03 22:38:44 +0000751static inline int mmc_blk_readonly(struct mmc_card *card)
752{
753 return mmc_card_readonly(card) ||
754 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
755}
756
Andrei Warkentin371a6892011-04-11 18:10:25 -0500757static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
758 struct device *parent,
759 sector_t size,
760 bool default_ro,
761 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct mmc_blk_data *md;
764 int devidx, ret;
765
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400766 devidx = find_first_zero_bit(dev_use, max_devices);
767 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return ERR_PTR(-ENOSPC);
769 __set_bit(devidx, dev_use);
770
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700771 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000772 if (!md) {
773 ret = -ENOMEM;
774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000776
Russell Kinga6f6c962006-01-03 22:38:44 +0000777 /*
778 * Set the read-only status based on the supported commands
779 * and the write protect switch.
780 */
781 md->read_only = mmc_blk_readonly(card);
782
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400783 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000784 if (md->disk == NULL) {
785 ret = -ENOMEM;
786 goto err_kfree;
787 }
788
789 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500790 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +0000791 md->usage = 1;
792
793 ret = mmc_init_queue(&md->queue, card, &md->lock);
794 if (ret)
795 goto err_putdisk;
796
Russell Kinga6f6c962006-01-03 22:38:44 +0000797 md->queue.issue_fn = mmc_blk_issue_rq;
798 md->queue.data = md;
799
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200800 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400801 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +0000802 md->disk->fops = &mmc_bdops;
803 md->disk->private_data = md;
804 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500805 md->disk->driverfs_dev = parent;
806 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500807 if (REL_WRITES_SUPPORTED(card))
808 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +0000809
810 /*
811 * As discussed on lkml, GENHD_FL_REMOVABLE should:
812 *
813 * - be set for removable media with permanent block devices
814 * - be unset for removable block devices with permanent media
815 *
816 * Since MMC block devices clearly fall under the second
817 * case, we do not set GENHD_FL_REMOVABLE. Userspace
818 * should use the block device creation/destruction hotplug
819 * messages to tell when the card is present.
820 */
821
Andrei Warkentin371a6892011-04-11 18:10:25 -0500822 if (subname)
823 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
824 "mmcblk%d%s",
825 mmc_get_devidx(dev_to_disk(parent)), subname);
826 else
827 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
828 "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000829
Martin K. Petersene1defc42009-05-22 17:17:49 -0400830 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500831 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000833
834 err_putdisk:
835 put_disk(md->disk);
836 err_kfree:
837 kfree(md);
838 out:
839 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Andrei Warkentin371a6892011-04-11 18:10:25 -0500842static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
843{
844 sector_t size;
845 struct mmc_blk_data *md;
846
847 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
848 /*
849 * The EXT_CSD sector count is in number or 512 byte
850 * sectors.
851 */
852 size = card->ext_csd.sectors;
853 } else {
854 /*
855 * The CSD capacity field is in units of read_blkbits.
856 * set_capacity takes units of 512 bytes.
857 */
858 size = card->csd.capacity << (card->csd.read_blkbits - 9);
859 }
860
861 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
862 return md;
863}
864
865static int mmc_blk_alloc_part(struct mmc_card *card,
866 struct mmc_blk_data *md,
867 unsigned int part_type,
868 sector_t size,
869 bool default_ro,
870 const char *subname)
871{
872 char cap_str[10];
873 struct mmc_blk_data *part_md;
874
875 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
876 subname);
877 if (IS_ERR(part_md))
878 return PTR_ERR(part_md);
879 part_md->part_type = part_type;
880 list_add(&part_md->part, &md->part);
881
882 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
883 cap_str, sizeof(cap_str));
884 printk(KERN_INFO "%s: %s %s partition %u %s\n",
885 part_md->disk->disk_name, mmc_card_id(card),
886 mmc_card_name(card), part_md->part_type, cap_str);
887 return 0;
888}
889
890static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
891{
892 int ret = 0;
893
894 if (!mmc_card_mmc(card))
895 return 0;
896
897 if (card->ext_csd.boot_size) {
898 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
899 card->ext_csd.boot_size >> 9,
900 true,
901 "boot0");
902 if (ret)
903 return ret;
904 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
905 card->ext_csd.boot_size >> 9,
906 true,
907 "boot1");
908 if (ret)
909 return ret;
910 }
911
912 return ret;
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915static int
916mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int err;
919
Pierre Ossmanb8558852007-01-03 19:47:29 +0100920 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300921 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100922 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300925 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
926 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return -EINVAL;
928 }
929
930 return 0;
931}
932
Andrei Warkentin371a6892011-04-11 18:10:25 -0500933static void mmc_blk_remove_req(struct mmc_blk_data *md)
934{
935 if (md) {
936 if (md->disk->flags & GENHD_FL_UP) {
937 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
938
939 /* Stop new requests from getting into the queue */
940 del_gendisk(md->disk);
941 }
942
943 /* Then flush out any already in there */
944 mmc_cleanup_queue(&md->queue);
945 mmc_blk_put(md);
946 }
947}
948
949static void mmc_blk_remove_parts(struct mmc_card *card,
950 struct mmc_blk_data *md)
951{
952 struct list_head *pos, *q;
953 struct mmc_blk_data *part_md;
954
955 list_for_each_safe(pos, q, &md->part) {
956 part_md = list_entry(pos, struct mmc_blk_data, part);
957 list_del(pos);
958 mmc_blk_remove_req(part_md);
959 }
960}
961
962static int mmc_add_disk(struct mmc_blk_data *md)
963{
964 int ret;
965
966 add_disk(md->disk);
967 md->force_ro.show = force_ro_show;
968 md->force_ro.store = force_ro_store;
969 md->force_ro.attr.name = "force_ro";
970 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
971 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
972 if (ret)
973 del_gendisk(md->disk);
974
975 return ret;
976}
977
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400978static const struct mmc_fixup blk_fixups[] =
979{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500980 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
981 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
982 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
983 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
984 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400985 END_FIXUP
986};
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static int mmc_blk_probe(struct mmc_card *card)
989{
Andrei Warkentin371a6892011-04-11 18:10:25 -0500990 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200992 char cap_str[10];
993
Pierre Ossman912490d2005-05-21 10:27:02 +0100994 /*
995 * Check that the card supports the command class(es) we need.
996 */
997 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENODEV;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 md = mmc_blk_alloc(card);
1001 if (IS_ERR(md))
1002 return PTR_ERR(md);
1003
1004 err = mmc_blk_set_blksize(md, card);
1005 if (err)
1006 goto out;
1007
Yi Li444122f2009-02-05 15:31:57 +08001008 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001009 cap_str, sizeof(cap_str));
1010 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001012 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Andrei Warkentin371a6892011-04-11 18:10:25 -05001014 if (mmc_blk_alloc_parts(card, md))
1015 goto out;
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001018 mmc_fixup_device(card, blk_fixups);
1019
Andrei Warkentin371a6892011-04-11 18:10:25 -05001020 if (mmc_add_disk(md))
1021 goto out;
1022
1023 list_for_each_entry(part_md, &md->part, part) {
1024 if (mmc_add_disk(part_md))
1025 goto out;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028
1029 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001030 mmc_blk_remove_parts(card, md);
1031 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return err;
1033}
1034
1035static void mmc_blk_remove(struct mmc_card *card)
1036{
1037 struct mmc_blk_data *md = mmc_get_drvdata(card);
1038
Andrei Warkentin371a6892011-04-11 18:10:25 -05001039 mmc_blk_remove_parts(card, md);
1040 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mmc_set_drvdata(card, NULL);
1042}
1043
1044#ifdef CONFIG_PM
1045static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1046{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001047 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct mmc_blk_data *md = mmc_get_drvdata(card);
1049
1050 if (md) {
1051 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001052 list_for_each_entry(part_md, &md->part, part) {
1053 mmc_queue_suspend(&part_md->queue);
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 return 0;
1057}
1058
1059static int mmc_blk_resume(struct mmc_card *card)
1060{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001061 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct mmc_blk_data *md = mmc_get_drvdata(card);
1063
1064 if (md) {
1065 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001066
1067 /*
1068 * Resume involves the card going into idle state,
1069 * so current partition is always the main one.
1070 */
1071 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001073 list_for_each_entry(part_md, &md->part, part) {
1074 mmc_queue_resume(&part_md->queue);
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 return 0;
1078}
1079#else
1080#define mmc_blk_suspend NULL
1081#define mmc_blk_resume NULL
1082#endif
1083
1084static struct mmc_driver mmc_driver = {
1085 .drv = {
1086 .name = "mmcblk",
1087 },
1088 .probe = mmc_blk_probe,
1089 .remove = mmc_blk_remove,
1090 .suspend = mmc_blk_suspend,
1091 .resume = mmc_blk_resume,
1092};
1093
1094static int __init mmc_blk_init(void)
1095{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001096 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001098 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1099 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1100
1101 max_devices = 256 / perdev_minors;
1102
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001103 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1104 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001107 res = mmc_register_driver(&mmc_driver);
1108 if (res)
1109 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001111 return 0;
1112 out2:
1113 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 out:
1115 return res;
1116}
1117
1118static void __exit mmc_blk_exit(void)
1119{
1120 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001121 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124module_init(mmc_blk_init);
1125module_exit(mmc_blk_exit);
1126
1127MODULE_LICENSE("GPL");
1128MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1129