blob: adf0ed3f2c0875bc46654ba2bf307fb6edbc2c40 [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
Chris Ball24f5b532011-04-13 23:49:45 -0400262 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400263 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400264 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700265 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
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700285 data.timeout_ns = card->csd.tacc_ns * 100;
286 data.timeout_clks = card->csd.tacc_clks * 100;
287
288 timeout_us = data.timeout_ns / 1000;
289 timeout_us += data.timeout_clks * 1000 /
290 (card->host->ios.clock / 1000);
291
292 if (timeout_us > 100000) {
293 data.timeout_ns = 100000000;
294 data.timeout_clks = 0;
295 }
296
297 data.blksz = 4;
298 data.blocks = 1;
299 data.flags = MMC_DATA_READ;
300 data.sg = &sg;
301 data.sg_len = 1;
302
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700303 mrq.cmd = &cmd;
304 mrq.data = &data;
305
Ben Dooks051913d2009-06-08 23:33:57 +0100306 blocks = kmalloc(4, GFP_KERNEL);
307 if (!blocks)
308 return (u32)-1;
309
310 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700311
312 mmc_wait_for_req(card->host, &mrq);
313
Ben Dooks051913d2009-06-08 23:33:57 +0100314 result = ntohl(*blocks);
315 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700316
Ben Dooks051913d2009-06-08 23:33:57 +0100317 if (cmd.error || data.error)
318 result = (u32)-1;
319
320 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700321}
322
Adrian Hunter504f1912008-10-16 12:55:25 +0300323static u32 get_card_status(struct mmc_card *card, struct request *req)
324{
Chris Ball1278dba2011-04-13 23:40:30 -0400325 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300326 int err;
327
Adrian Hunter504f1912008-10-16 12:55:25 +0300328 cmd.opcode = MMC_SEND_STATUS;
329 if (!mmc_host_is_spi(card->host))
330 cmd.arg = card->rca << 16;
331 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
332 err = mmc_wait_for_cmd(card->host, &cmd, 0);
333 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400334 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300335 req->rq_disk->disk_name, err);
336 return cmd.resp[0];
337}
338
Adrian Hunterbd788c92010-08-11 14:17:47 -0700339static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
340{
341 struct mmc_blk_data *md = mq->data;
342 struct mmc_card *card = md->queue.card;
343 unsigned int from, nr, arg;
344 int err = 0;
345
Adrian Hunterbd788c92010-08-11 14:17:47 -0700346 if (!mmc_can_erase(card)) {
347 err = -EOPNOTSUPP;
348 goto out;
349 }
350
351 from = blk_rq_pos(req);
352 nr = blk_rq_sectors(req);
353
354 if (mmc_can_trim(card))
355 arg = MMC_TRIM_ARG;
356 else
357 arg = MMC_ERASE_ARG;
358
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500359 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
360 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
361 INAND_CMD38_ARG_EXT_CSD,
362 arg == MMC_TRIM_ARG ?
363 INAND_CMD38_ARG_TRIM :
364 INAND_CMD38_ARG_ERASE,
365 0);
366 if (err)
367 goto out;
368 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700369 err = mmc_erase(card, from, nr, arg);
370out:
371 spin_lock_irq(&md->lock);
372 __blk_end_request(req, err, blk_rq_bytes(req));
373 spin_unlock_irq(&md->lock);
374
Adrian Hunterbd788c92010-08-11 14:17:47 -0700375 return err ? 0 : 1;
376}
377
Adrian Hunter49804542010-08-11 14:17:50 -0700378static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
379 struct request *req)
380{
381 struct mmc_blk_data *md = mq->data;
382 struct mmc_card *card = md->queue.card;
383 unsigned int from, nr, arg;
384 int err = 0;
385
Adrian Hunter49804542010-08-11 14:17:50 -0700386 if (!mmc_can_secure_erase_trim(card)) {
387 err = -EOPNOTSUPP;
388 goto out;
389 }
390
391 from = blk_rq_pos(req);
392 nr = blk_rq_sectors(req);
393
394 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
395 arg = MMC_SECURE_TRIM1_ARG;
396 else
397 arg = MMC_SECURE_ERASE_ARG;
398
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500399 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
400 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
401 INAND_CMD38_ARG_EXT_CSD,
402 arg == MMC_SECURE_TRIM1_ARG ?
403 INAND_CMD38_ARG_SECTRIM1 :
404 INAND_CMD38_ARG_SECERASE,
405 0);
406 if (err)
407 goto out;
408 }
Adrian Hunter49804542010-08-11 14:17:50 -0700409 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500410 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
411 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
412 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
413 INAND_CMD38_ARG_EXT_CSD,
414 INAND_CMD38_ARG_SECTRIM2,
415 0);
416 if (err)
417 goto out;
418 }
Adrian Hunter49804542010-08-11 14:17:50 -0700419 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500420 }
Adrian Hunter49804542010-08-11 14:17:50 -0700421out:
422 spin_lock_irq(&md->lock);
423 __blk_end_request(req, err, blk_rq_bytes(req));
424 spin_unlock_irq(&md->lock);
425
Adrian Hunter49804542010-08-11 14:17:50 -0700426 return err ? 0 : 1;
427}
428
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500429static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
430{
431 struct mmc_blk_data *md = mq->data;
432
433 /*
434 * No-op, only service this because we need REQ_FUA for reliable
435 * writes.
436 */
437 spin_lock_irq(&md->lock);
438 __blk_end_request_all(req, 0);
439 spin_unlock_irq(&md->lock);
440
441 return 1;
442}
443
444/*
445 * Reformat current write as a reliable write, supporting
446 * both legacy and the enhanced reliable write MMC cards.
447 * In each transfer we'll handle only as much as a single
448 * reliable write can handle, thus finish the request in
449 * partial completions.
450 */
451static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
452 struct mmc_card *card,
453 struct request *req)
454{
455 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400456 struct mmc_command set_count = {0};
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500457
458 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
459 /* Legacy mode imposes restrictions on transfers. */
460 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
461 brq->data.blocks = 1;
462
463 if (brq->data.blocks > card->ext_csd.rel_sectors)
464 brq->data.blocks = card->ext_csd.rel_sectors;
465 else if (brq->data.blocks < card->ext_csd.rel_sectors)
466 brq->data.blocks = 1;
467 }
468
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500469 set_count.opcode = MMC_SET_BLOCK_COUNT;
470 set_count.arg = brq->data.blocks | (1 << 31);
471 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
472 err = mmc_wait_for_cmd(card->host, &set_count, 0);
473 if (err)
474 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
475 req->rq_disk->disk_name, err);
476 return err;
477}
478
Adrian Hunterbd788c92010-08-11 14:17:47 -0700479static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct mmc_blk_data *md = mq->data;
482 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700483 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100484 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500486 /*
487 * Reliable writes are used to implement Forced Unit Access and
488 * REQ_META accesses, and are supported only on MMCs.
489 */
490 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
491 (req->cmd_flags & REQ_META)) &&
492 (rq_data_dir(req) == WRITE) &&
493 REL_WRITES_SUPPORTED(card);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400496 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300497 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 memset(&brq, 0, sizeof(struct mmc_blk_request));
500 brq.mrq.cmd = &brq.cmd;
501 brq.mrq.data = &brq.data;
502
Tejun Heo83096eb2009-05-07 22:24:39 +0900503 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800504 if (!mmc_card_blockaddr(card))
505 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700506 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200507 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 brq.stop.opcode = MMC_STOP_TRANSMISSION;
509 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700510 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900511 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Adrian Hunter6a79e392008-12-31 18:21:17 +0100513 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200514 * The block layer doesn't support all sector count
515 * restrictions, so we need to be prepared for too big
516 * requests.
517 */
518 if (brq.data.blocks > card->host->max_blk_count)
519 brq.data.blocks = card->host->max_blk_count;
520
521 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100522 * After a read error, we redo the request one sector at a time
523 * in order to accurately determine which sectors can be read
524 * successfully.
525 */
526 if (disable_multi && brq.data.blocks > 1)
527 brq.data.blocks = 1;
528
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500529 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700530 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500531 * token, not a STOP_TRANSMISSION request. Reliable
532 * writes use SET_BLOCK_COUNT and do not use a
533 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700534 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500535 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
536 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700537 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100538 readcmd = MMC_READ_MULTIPLE_BLOCK;
539 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000540 } else {
541 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100542 readcmd = MMC_READ_SINGLE_BLOCK;
543 writecmd = MMC_WRITE_BLOCK;
544 }
Russell Kingdb53f282006-08-30 15:14:56 +0100545 if (rq_data_dir(req) == READ) {
546 brq.cmd.opcode = readcmd;
547 brq.data.flags |= MMC_DATA_READ;
548 } else {
549 brq.cmd.opcode = writecmd;
550 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500553 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
554 goto cmd_err;
555
Pierre Ossmanb146d262007-07-24 19:16:54 +0200556 mmc_set_data_timeout(&brq.data, card);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200559 brq.data.sg_len = mmc_queue_map_sg(mq);
560
Adrian Hunter6a79e392008-12-31 18:21:17 +0100561 /*
562 * Adjust the sg list so it is the same size as the
563 * request.
564 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900565 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100566 int i, data_size = brq.data.blocks << 9;
567 struct scatterlist *sg;
568
569 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
570 data_size -= sg->length;
571 if (data_size <= 0) {
572 sg->length += data_size;
573 i++;
574 break;
575 }
576 }
577 brq.data.sg_len = i;
578 }
579
Pierre Ossman98ccf142007-05-12 00:26:16 +0200580 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200583
584 mmc_queue_bounce_post(mq);
585
Pierre Ossman979ce722008-06-29 12:19:47 +0200586 /*
587 * Check for errors here, but don't jump to cmd_err
588 * until later as we need to wait for the card to leave
589 * programming mode even when things go wrong.
590 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100591 if (brq.cmd.error || brq.data.error || brq.stop.error) {
592 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
593 /* Redo read one sector at a time */
594 printk(KERN_WARNING "%s: retrying using single "
595 "block read\n", req->rq_disk->disk_name);
596 disable_multi = 1;
597 continue;
598 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300599 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100600 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300603 printk(KERN_ERR "%s: error %d sending read/write "
604 "command, response %#x, card status %#x\n",
605 req->rq_disk->disk_name, brq.cmd.error,
606 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300610 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
611 /* 'Stop' response contains card status */
612 status = brq.mrq.stop->resp[0];
613 printk(KERN_ERR "%s: error %d transferring data,"
614 " sector %u, nr %u, card status %#x\n",
615 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900616 (unsigned)blk_rq_pos(req),
617 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300621 printk(KERN_ERR "%s: error %d sending stop command, "
622 "response %#x, card status %#x\n",
623 req->rq_disk->disk_name, brq.stop.error,
624 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
David Brownell7213d172007-08-08 09:10:23 -0700627 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100628 do {
629 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Russell King2ed6d222006-09-24 10:46:43 +0100631 cmd.opcode = MMC_SEND_STATUS;
632 cmd.arg = card->rca << 16;
633 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
634 err = mmc_wait_for_cmd(card->host, &cmd, 5);
635 if (err) {
636 printk(KERN_ERR "%s: error %d requesting status\n",
637 req->rq_disk->disk_name, err);
638 goto cmd_err;
639 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100640 /*
641 * Some cards mishandle the status bits,
642 * so make sure to check both the busy
643 * indication and the card state.
644 */
645 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
646 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100649 if (cmd.resp[0] & ~0x00000900)
650 printk(KERN_ERR "%s: status = %08x\n",
651 req->rq_disk->disk_name, cmd.resp[0]);
652 if (mmc_decode_status(cmd.resp))
653 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
Russell King2ed6d222006-09-24 10:46:43 +0100655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Adrian Hunter6a79e392008-12-31 18:21:17 +0100657 if (brq.cmd.error || brq.stop.error || brq.data.error) {
658 if (rq_data_dir(req) == READ) {
659 /*
660 * After an error, we redo I/O one sector at a
661 * time, so we only reach here after trying to
662 * read a single sector.
663 */
664 spin_lock_irq(&md->lock);
665 ret = __blk_end_request(req, -EIO, brq.data.blksz);
666 spin_unlock_irq(&md->lock);
667 continue;
668 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200669 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100670 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /*
673 * A block was successfully transferred.
674 */
675 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500676 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 spin_unlock_irq(&md->lock);
678 } while (ret);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 1;
681
682 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700683 /*
684 * If this is an SD card and we're writing, we can first
685 * mark the known good sectors as ok.
686 *
687 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200688 * as reported by the controller (which might be less than
689 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100691 if (mmc_card_sd(card)) {
692 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700693
Adrian Hunter6a79e392008-12-31 18:21:17 +0100694 blocks = mmc_sd_num_wr_blocks(card);
695 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700696 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100697 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700698 spin_unlock_irq(&md->lock);
699 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100700 } else {
701 spin_lock_irq(&md->lock);
702 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
703 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700704 }
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500707 while (ret)
708 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock_irq(&md->lock);
710
711 return 0;
712}
713
Adrian Hunterbd788c92010-08-11 14:17:47 -0700714static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
715{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500716 int ret;
717 struct mmc_blk_data *md = mq->data;
718 struct mmc_card *card = md->queue.card;
719
720 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500721 ret = mmc_blk_part_switch(card, md);
722 if (ret) {
723 ret = 0;
724 goto out;
725 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500726
Adrian Hunter49804542010-08-11 14:17:50 -0700727 if (req->cmd_flags & REQ_DISCARD) {
728 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500729 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700730 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500731 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500732 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500733 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700734 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500735 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700736 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500737
Andrei Warkentin371a6892011-04-11 18:10:25 -0500738out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500739 mmc_release_host(card->host);
740 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700741}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Russell Kinga6f6c962006-01-03 22:38:44 +0000743static inline int mmc_blk_readonly(struct mmc_card *card)
744{
745 return mmc_card_readonly(card) ||
746 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
747}
748
Andrei Warkentin371a6892011-04-11 18:10:25 -0500749static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
750 struct device *parent,
751 sector_t size,
752 bool default_ro,
753 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 struct mmc_blk_data *md;
756 int devidx, ret;
757
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400758 devidx = find_first_zero_bit(dev_use, max_devices);
759 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ERR_PTR(-ENOSPC);
761 __set_bit(devidx, dev_use);
762
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700763 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000764 if (!md) {
765 ret = -ENOMEM;
766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000768
Russell Kinga6f6c962006-01-03 22:38:44 +0000769 /*
770 * Set the read-only status based on the supported commands
771 * and the write protect switch.
772 */
773 md->read_only = mmc_blk_readonly(card);
774
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400775 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000776 if (md->disk == NULL) {
777 ret = -ENOMEM;
778 goto err_kfree;
779 }
780
781 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500782 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +0000783 md->usage = 1;
784
785 ret = mmc_init_queue(&md->queue, card, &md->lock);
786 if (ret)
787 goto err_putdisk;
788
Russell Kinga6f6c962006-01-03 22:38:44 +0000789 md->queue.issue_fn = mmc_blk_issue_rq;
790 md->queue.data = md;
791
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200792 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400793 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +0000794 md->disk->fops = &mmc_bdops;
795 md->disk->private_data = md;
796 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500797 md->disk->driverfs_dev = parent;
798 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500799 if (REL_WRITES_SUPPORTED(card))
800 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +0000801
802 /*
803 * As discussed on lkml, GENHD_FL_REMOVABLE should:
804 *
805 * - be set for removable media with permanent block devices
806 * - be unset for removable block devices with permanent media
807 *
808 * Since MMC block devices clearly fall under the second
809 * case, we do not set GENHD_FL_REMOVABLE. Userspace
810 * should use the block device creation/destruction hotplug
811 * messages to tell when the card is present.
812 */
813
Andrei Warkentin371a6892011-04-11 18:10:25 -0500814 if (subname)
815 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
816 "mmcblk%d%s",
817 mmc_get_devidx(dev_to_disk(parent)), subname);
818 else
819 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
820 "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000821
Martin K. Petersene1defc42009-05-22 17:17:49 -0400822 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500823 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000825
826 err_putdisk:
827 put_disk(md->disk);
828 err_kfree:
829 kfree(md);
830 out:
831 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Andrei Warkentin371a6892011-04-11 18:10:25 -0500834static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
835{
836 sector_t size;
837 struct mmc_blk_data *md;
838
839 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
840 /*
841 * The EXT_CSD sector count is in number or 512 byte
842 * sectors.
843 */
844 size = card->ext_csd.sectors;
845 } else {
846 /*
847 * The CSD capacity field is in units of read_blkbits.
848 * set_capacity takes units of 512 bytes.
849 */
850 size = card->csd.capacity << (card->csd.read_blkbits - 9);
851 }
852
853 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
854 return md;
855}
856
857static int mmc_blk_alloc_part(struct mmc_card *card,
858 struct mmc_blk_data *md,
859 unsigned int part_type,
860 sector_t size,
861 bool default_ro,
862 const char *subname)
863{
864 char cap_str[10];
865 struct mmc_blk_data *part_md;
866
867 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
868 subname);
869 if (IS_ERR(part_md))
870 return PTR_ERR(part_md);
871 part_md->part_type = part_type;
872 list_add(&part_md->part, &md->part);
873
874 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
875 cap_str, sizeof(cap_str));
876 printk(KERN_INFO "%s: %s %s partition %u %s\n",
877 part_md->disk->disk_name, mmc_card_id(card),
878 mmc_card_name(card), part_md->part_type, cap_str);
879 return 0;
880}
881
882static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
883{
884 int ret = 0;
885
886 if (!mmc_card_mmc(card))
887 return 0;
888
889 if (card->ext_csd.boot_size) {
890 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
891 card->ext_csd.boot_size >> 9,
892 true,
893 "boot0");
894 if (ret)
895 return ret;
896 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
897 card->ext_csd.boot_size >> 9,
898 true,
899 "boot1");
900 if (ret)
901 return ret;
902 }
903
904 return ret;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int
908mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int err;
911
Pierre Ossmanb8558852007-01-03 19:47:29 +0100912 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300913 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100914 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300917 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
918 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EINVAL;
920 }
921
922 return 0;
923}
924
Andrei Warkentin371a6892011-04-11 18:10:25 -0500925static void mmc_blk_remove_req(struct mmc_blk_data *md)
926{
927 if (md) {
928 if (md->disk->flags & GENHD_FL_UP) {
929 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
930
931 /* Stop new requests from getting into the queue */
932 del_gendisk(md->disk);
933 }
934
935 /* Then flush out any already in there */
936 mmc_cleanup_queue(&md->queue);
937 mmc_blk_put(md);
938 }
939}
940
941static void mmc_blk_remove_parts(struct mmc_card *card,
942 struct mmc_blk_data *md)
943{
944 struct list_head *pos, *q;
945 struct mmc_blk_data *part_md;
946
947 list_for_each_safe(pos, q, &md->part) {
948 part_md = list_entry(pos, struct mmc_blk_data, part);
949 list_del(pos);
950 mmc_blk_remove_req(part_md);
951 }
952}
953
954static int mmc_add_disk(struct mmc_blk_data *md)
955{
956 int ret;
957
958 add_disk(md->disk);
959 md->force_ro.show = force_ro_show;
960 md->force_ro.store = force_ro_store;
961 md->force_ro.attr.name = "force_ro";
962 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
963 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
964 if (ret)
965 del_gendisk(md->disk);
966
967 return ret;
968}
969
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400970static const struct mmc_fixup blk_fixups[] =
971{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500972 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
973 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
974 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
975 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
976 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400977 END_FIXUP
978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int mmc_blk_probe(struct mmc_card *card)
981{
Andrei Warkentin371a6892011-04-11 18:10:25 -0500982 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200984 char cap_str[10];
985
Pierre Ossman912490d2005-05-21 10:27:02 +0100986 /*
987 * Check that the card supports the command class(es) we need.
988 */
989 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -ENODEV;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 md = mmc_blk_alloc(card);
993 if (IS_ERR(md))
994 return PTR_ERR(md);
995
996 err = mmc_blk_set_blksize(md, card);
997 if (err)
998 goto out;
999
Yi Li444122f2009-02-05 15:31:57 +08001000 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001001 cap_str, sizeof(cap_str));
1002 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001004 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Andrei Warkentin371a6892011-04-11 18:10:25 -05001006 if (mmc_blk_alloc_parts(card, md))
1007 goto out;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001010 mmc_fixup_device(card, blk_fixups);
1011
Andrei Warkentin371a6892011-04-11 18:10:25 -05001012 if (mmc_add_disk(md))
1013 goto out;
1014
1015 list_for_each_entry(part_md, &md->part, part) {
1016 if (mmc_add_disk(part_md))
1017 goto out;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020
1021 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001022 mmc_blk_remove_parts(card, md);
1023 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return err;
1025}
1026
1027static void mmc_blk_remove(struct mmc_card *card)
1028{
1029 struct mmc_blk_data *md = mmc_get_drvdata(card);
1030
Andrei Warkentin371a6892011-04-11 18:10:25 -05001031 mmc_blk_remove_parts(card, md);
1032 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 mmc_set_drvdata(card, NULL);
1034}
1035
1036#ifdef CONFIG_PM
1037static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1038{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001039 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct mmc_blk_data *md = mmc_get_drvdata(card);
1041
1042 if (md) {
1043 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001044 list_for_each_entry(part_md, &md->part, part) {
1045 mmc_queue_suspend(&part_md->queue);
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 return 0;
1049}
1050
1051static int mmc_blk_resume(struct mmc_card *card)
1052{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001053 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct mmc_blk_data *md = mmc_get_drvdata(card);
1055
1056 if (md) {
1057 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001058
1059 /*
1060 * Resume involves the card going into idle state,
1061 * so current partition is always the main one.
1062 */
1063 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001065 list_for_each_entry(part_md, &md->part, part) {
1066 mmc_queue_resume(&part_md->queue);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 return 0;
1070}
1071#else
1072#define mmc_blk_suspend NULL
1073#define mmc_blk_resume NULL
1074#endif
1075
1076static struct mmc_driver mmc_driver = {
1077 .drv = {
1078 .name = "mmcblk",
1079 },
1080 .probe = mmc_blk_probe,
1081 .remove = mmc_blk_remove,
1082 .suspend = mmc_blk_suspend,
1083 .resume = mmc_blk_resume,
1084};
1085
1086static int __init mmc_blk_init(void)
1087{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001088 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001090 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1091 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1092
1093 max_devices = 256 / perdev_minors;
1094
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001095 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1096 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001099 res = mmc_register_driver(&mmc_driver);
1100 if (res)
1101 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001103 return 0;
1104 out2:
1105 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 out:
1107 return res;
1108}
1109
1110static void __exit mmc_blk_exit(void)
1111{
1112 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001113 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116module_init(mmc_blk_init);
1117module_exit(mmc_blk_exit);
1118
1119MODULE_LICENSE("GPL");
1120MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1121