blob: fb387c8032054fcaac7a7930e96fc2f456d75685 [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};
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
303 memset(&mrq, 0, sizeof(struct mmc_request));
304
305 mrq.cmd = &cmd;
306 mrq.data = &data;
307
Ben Dooks051913d2009-06-08 23:33:57 +0100308 blocks = kmalloc(4, GFP_KERNEL);
309 if (!blocks)
310 return (u32)-1;
311
312 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700313
314 mmc_wait_for_req(card->host, &mrq);
315
Ben Dooks051913d2009-06-08 23:33:57 +0100316 result = ntohl(*blocks);
317 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700318
Ben Dooks051913d2009-06-08 23:33:57 +0100319 if (cmd.error || data.error)
320 result = (u32)-1;
321
322 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700323}
324
Adrian Hunter504f1912008-10-16 12:55:25 +0300325static u32 get_card_status(struct mmc_card *card, struct request *req)
326{
Chris Ball1278dba2011-04-13 23:40:30 -0400327 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300328 int err;
329
Adrian Hunter504f1912008-10-16 12:55:25 +0300330 cmd.opcode = MMC_SEND_STATUS;
331 if (!mmc_host_is_spi(card->host))
332 cmd.arg = card->rca << 16;
333 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
334 err = mmc_wait_for_cmd(card->host, &cmd, 0);
335 if (err)
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400336 printk(KERN_ERR "%s: error %d sending status command",
Adrian Hunter504f1912008-10-16 12:55:25 +0300337 req->rq_disk->disk_name, err);
338 return cmd.resp[0];
339}
340
Adrian Hunterbd788c92010-08-11 14:17:47 -0700341static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
342{
343 struct mmc_blk_data *md = mq->data;
344 struct mmc_card *card = md->queue.card;
345 unsigned int from, nr, arg;
346 int err = 0;
347
Adrian Hunterbd788c92010-08-11 14:17:47 -0700348 if (!mmc_can_erase(card)) {
349 err = -EOPNOTSUPP;
350 goto out;
351 }
352
353 from = blk_rq_pos(req);
354 nr = blk_rq_sectors(req);
355
356 if (mmc_can_trim(card))
357 arg = MMC_TRIM_ARG;
358 else
359 arg = MMC_ERASE_ARG;
360
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500361 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
362 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
363 INAND_CMD38_ARG_EXT_CSD,
364 arg == MMC_TRIM_ARG ?
365 INAND_CMD38_ARG_TRIM :
366 INAND_CMD38_ARG_ERASE,
367 0);
368 if (err)
369 goto out;
370 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700371 err = mmc_erase(card, from, nr, arg);
372out:
373 spin_lock_irq(&md->lock);
374 __blk_end_request(req, err, blk_rq_bytes(req));
375 spin_unlock_irq(&md->lock);
376
Adrian Hunterbd788c92010-08-11 14:17:47 -0700377 return err ? 0 : 1;
378}
379
Adrian Hunter49804542010-08-11 14:17:50 -0700380static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
381 struct request *req)
382{
383 struct mmc_blk_data *md = mq->data;
384 struct mmc_card *card = md->queue.card;
385 unsigned int from, nr, arg;
386 int err = 0;
387
Adrian Hunter49804542010-08-11 14:17:50 -0700388 if (!mmc_can_secure_erase_trim(card)) {
389 err = -EOPNOTSUPP;
390 goto out;
391 }
392
393 from = blk_rq_pos(req);
394 nr = blk_rq_sectors(req);
395
396 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
397 arg = MMC_SECURE_TRIM1_ARG;
398 else
399 arg = MMC_SECURE_ERASE_ARG;
400
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500401 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
402 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
403 INAND_CMD38_ARG_EXT_CSD,
404 arg == MMC_SECURE_TRIM1_ARG ?
405 INAND_CMD38_ARG_SECTRIM1 :
406 INAND_CMD38_ARG_SECERASE,
407 0);
408 if (err)
409 goto out;
410 }
Adrian Hunter49804542010-08-11 14:17:50 -0700411 err = mmc_erase(card, from, nr, arg);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500412 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
413 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
414 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
415 INAND_CMD38_ARG_EXT_CSD,
416 INAND_CMD38_ARG_SECTRIM2,
417 0);
418 if (err)
419 goto out;
420 }
Adrian Hunter49804542010-08-11 14:17:50 -0700421 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500422 }
Adrian Hunter49804542010-08-11 14:17:50 -0700423out:
424 spin_lock_irq(&md->lock);
425 __blk_end_request(req, err, blk_rq_bytes(req));
426 spin_unlock_irq(&md->lock);
427
Adrian Hunter49804542010-08-11 14:17:50 -0700428 return err ? 0 : 1;
429}
430
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500431static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
432{
433 struct mmc_blk_data *md = mq->data;
434
435 /*
436 * No-op, only service this because we need REQ_FUA for reliable
437 * writes.
438 */
439 spin_lock_irq(&md->lock);
440 __blk_end_request_all(req, 0);
441 spin_unlock_irq(&md->lock);
442
443 return 1;
444}
445
446/*
447 * Reformat current write as a reliable write, supporting
448 * both legacy and the enhanced reliable write MMC cards.
449 * In each transfer we'll handle only as much as a single
450 * reliable write can handle, thus finish the request in
451 * partial completions.
452 */
453static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
454 struct mmc_card *card,
455 struct request *req)
456{
457 int err;
Chris Ball1278dba2011-04-13 23:40:30 -0400458 struct mmc_command set_count = {0};
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500459
460 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
461 /* Legacy mode imposes restrictions on transfers. */
462 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
463 brq->data.blocks = 1;
464
465 if (brq->data.blocks > card->ext_csd.rel_sectors)
466 brq->data.blocks = card->ext_csd.rel_sectors;
467 else if (brq->data.blocks < card->ext_csd.rel_sectors)
468 brq->data.blocks = 1;
469 }
470
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500471 set_count.opcode = MMC_SET_BLOCK_COUNT;
472 set_count.arg = brq->data.blocks | (1 << 31);
473 set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
474 err = mmc_wait_for_cmd(card->host, &set_count, 0);
475 if (err)
476 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
477 req->rq_disk->disk_name, err);
478 return err;
479}
480
Adrian Hunterbd788c92010-08-11 14:17:47 -0700481static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct mmc_blk_data *md = mq->data;
484 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700485 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100486 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500488 /*
489 * Reliable writes are used to implement Forced Unit Access and
490 * REQ_META accesses, and are supported only on MMCs.
491 */
492 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
493 (req->cmd_flags & REQ_META)) &&
494 (rq_data_dir(req) == WRITE) &&
495 REL_WRITES_SUPPORTED(card);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 do {
Chris Ball1278dba2011-04-13 23:40:30 -0400498 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300499 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 memset(&brq, 0, sizeof(struct mmc_blk_request));
502 brq.mrq.cmd = &brq.cmd;
503 brq.mrq.data = &brq.data;
504
Tejun Heo83096eb2009-05-07 22:24:39 +0900505 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800506 if (!mmc_card_blockaddr(card))
507 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700508 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200509 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 brq.stop.opcode = MMC_STOP_TRANSMISSION;
511 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700512 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900513 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Adrian Hunter6a79e392008-12-31 18:21:17 +0100515 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200516 * The block layer doesn't support all sector count
517 * restrictions, so we need to be prepared for too big
518 * requests.
519 */
520 if (brq.data.blocks > card->host->max_blk_count)
521 brq.data.blocks = card->host->max_blk_count;
522
523 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100524 * After a read error, we redo the request one sector at a time
525 * in order to accurately determine which sectors can be read
526 * successfully.
527 */
528 if (disable_multi && brq.data.blocks > 1)
529 brq.data.blocks = 1;
530
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500531 if (brq.data.blocks > 1 || do_rel_wr) {
David Brownell7213d172007-08-08 09:10:23 -0700532 /* SPI multiblock writes terminate using a special
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500533 * token, not a STOP_TRANSMISSION request. Reliable
534 * writes use SET_BLOCK_COUNT and do not use a
535 * STOP_TRANSMISSION request either.
David Brownell7213d172007-08-08 09:10:23 -0700536 */
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500537 if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
538 rq_data_dir(req) == READ)
David Brownell7213d172007-08-08 09:10:23 -0700539 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100540 readcmd = MMC_READ_MULTIPLE_BLOCK;
541 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000542 } else {
543 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100544 readcmd = MMC_READ_SINGLE_BLOCK;
545 writecmd = MMC_WRITE_BLOCK;
546 }
Russell Kingdb53f282006-08-30 15:14:56 +0100547 if (rq_data_dir(req) == READ) {
548 brq.cmd.opcode = readcmd;
549 brq.data.flags |= MMC_DATA_READ;
550 } else {
551 brq.cmd.opcode = writecmd;
552 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500555 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
556 goto cmd_err;
557
Pierre Ossmanb146d262007-07-24 19:16:54 +0200558 mmc_set_data_timeout(&brq.data, card);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200561 brq.data.sg_len = mmc_queue_map_sg(mq);
562
Adrian Hunter6a79e392008-12-31 18:21:17 +0100563 /*
564 * Adjust the sg list so it is the same size as the
565 * request.
566 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900567 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100568 int i, data_size = brq.data.blocks << 9;
569 struct scatterlist *sg;
570
571 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
572 data_size -= sg->length;
573 if (data_size <= 0) {
574 sg->length += data_size;
575 i++;
576 break;
577 }
578 }
579 brq.data.sg_len = i;
580 }
581
Pierre Ossman98ccf142007-05-12 00:26:16 +0200582 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200585
586 mmc_queue_bounce_post(mq);
587
Pierre Ossman979ce722008-06-29 12:19:47 +0200588 /*
589 * Check for errors here, but don't jump to cmd_err
590 * until later as we need to wait for the card to leave
591 * programming mode even when things go wrong.
592 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100593 if (brq.cmd.error || brq.data.error || brq.stop.error) {
594 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
595 /* Redo read one sector at a time */
596 printk(KERN_WARNING "%s: retrying using single "
597 "block read\n", req->rq_disk->disk_name);
598 disable_multi = 1;
599 continue;
600 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300601 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100602 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300605 printk(KERN_ERR "%s: error %d sending read/write "
606 "command, response %#x, card status %#x\n",
607 req->rq_disk->disk_name, brq.cmd.error,
608 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300612 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
613 /* 'Stop' response contains card status */
614 status = brq.mrq.stop->resp[0];
615 printk(KERN_ERR "%s: error %d transferring data,"
616 " sector %u, nr %u, card status %#x\n",
617 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900618 (unsigned)blk_rq_pos(req),
619 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300623 printk(KERN_ERR "%s: error %d sending stop command, "
624 "response %#x, card status %#x\n",
625 req->rq_disk->disk_name, brq.stop.error,
626 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
David Brownell7213d172007-08-08 09:10:23 -0700629 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100630 do {
631 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Russell King2ed6d222006-09-24 10:46:43 +0100633 cmd.opcode = MMC_SEND_STATUS;
634 cmd.arg = card->rca << 16;
635 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
636 err = mmc_wait_for_cmd(card->host, &cmd, 5);
637 if (err) {
638 printk(KERN_ERR "%s: error %d requesting status\n",
639 req->rq_disk->disk_name, err);
640 goto cmd_err;
641 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100642 /*
643 * Some cards mishandle the status bits,
644 * so make sure to check both the busy
645 * indication and the card state.
646 */
647 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
648 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100651 if (cmd.resp[0] & ~0x00000900)
652 printk(KERN_ERR "%s: status = %08x\n",
653 req->rq_disk->disk_name, cmd.resp[0]);
654 if (mmc_decode_status(cmd.resp))
655 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#endif
Russell King2ed6d222006-09-24 10:46:43 +0100657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Adrian Hunter6a79e392008-12-31 18:21:17 +0100659 if (brq.cmd.error || brq.stop.error || brq.data.error) {
660 if (rq_data_dir(req) == READ) {
661 /*
662 * After an error, we redo I/O one sector at a
663 * time, so we only reach here after trying to
664 * read a single sector.
665 */
666 spin_lock_irq(&md->lock);
667 ret = __blk_end_request(req, -EIO, brq.data.blksz);
668 spin_unlock_irq(&md->lock);
669 continue;
670 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200671 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100672 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * A block was successfully transferred.
676 */
677 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500678 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_unlock_irq(&md->lock);
680 } while (ret);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
683
684 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700685 /*
686 * If this is an SD card and we're writing, we can first
687 * mark the known good sectors as ok.
688 *
689 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200690 * as reported by the controller (which might be less than
691 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100693 if (mmc_card_sd(card)) {
694 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700695
Adrian Hunter6a79e392008-12-31 18:21:17 +0100696 blocks = mmc_sd_num_wr_blocks(card);
697 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700698 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100699 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700700 spin_unlock_irq(&md->lock);
701 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100702 } else {
703 spin_lock_irq(&md->lock);
704 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
705 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500709 while (ret)
710 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 spin_unlock_irq(&md->lock);
712
713 return 0;
714}
715
Adrian Hunterbd788c92010-08-11 14:17:47 -0700716static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
717{
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500718 int ret;
719 struct mmc_blk_data *md = mq->data;
720 struct mmc_card *card = md->queue.card;
721
722 mmc_claim_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500723 ret = mmc_blk_part_switch(card, md);
724 if (ret) {
725 ret = 0;
726 goto out;
727 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500728
Adrian Hunter49804542010-08-11 14:17:50 -0700729 if (req->cmd_flags & REQ_DISCARD) {
730 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500731 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700732 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500733 ret = mmc_blk_issue_discard_rq(mq, req);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500734 } else if (req->cmd_flags & REQ_FLUSH) {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500735 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700736 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500737 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700738 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500739
Andrei Warkentin371a6892011-04-11 18:10:25 -0500740out:
Andrei Warkentin1a258db2011-04-11 18:10:24 -0500741 mmc_release_host(card->host);
742 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Russell Kinga6f6c962006-01-03 22:38:44 +0000745static inline int mmc_blk_readonly(struct mmc_card *card)
746{
747 return mmc_card_readonly(card) ||
748 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
749}
750
Andrei Warkentin371a6892011-04-11 18:10:25 -0500751static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
752 struct device *parent,
753 sector_t size,
754 bool default_ro,
755 const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct mmc_blk_data *md;
758 int devidx, ret;
759
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400760 devidx = find_first_zero_bit(dev_use, max_devices);
761 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return ERR_PTR(-ENOSPC);
763 __set_bit(devidx, dev_use);
764
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700765 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000766 if (!md) {
767 ret = -ENOMEM;
768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000770
Russell Kinga6f6c962006-01-03 22:38:44 +0000771 /*
772 * Set the read-only status based on the supported commands
773 * and the write protect switch.
774 */
775 md->read_only = mmc_blk_readonly(card);
776
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400777 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000778 if (md->disk == NULL) {
779 ret = -ENOMEM;
780 goto err_kfree;
781 }
782
783 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500784 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +0000785 md->usage = 1;
786
787 ret = mmc_init_queue(&md->queue, card, &md->lock);
788 if (ret)
789 goto err_putdisk;
790
Russell Kinga6f6c962006-01-03 22:38:44 +0000791 md->queue.issue_fn = mmc_blk_issue_rq;
792 md->queue.data = md;
793
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200794 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400795 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +0000796 md->disk->fops = &mmc_bdops;
797 md->disk->private_data = md;
798 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500799 md->disk->driverfs_dev = parent;
800 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500801 if (REL_WRITES_SUPPORTED(card))
802 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +0000803
804 /*
805 * As discussed on lkml, GENHD_FL_REMOVABLE should:
806 *
807 * - be set for removable media with permanent block devices
808 * - be unset for removable block devices with permanent media
809 *
810 * Since MMC block devices clearly fall under the second
811 * case, we do not set GENHD_FL_REMOVABLE. Userspace
812 * should use the block device creation/destruction hotplug
813 * messages to tell when the card is present.
814 */
815
Andrei Warkentin371a6892011-04-11 18:10:25 -0500816 if (subname)
817 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
818 "mmcblk%d%s",
819 mmc_get_devidx(dev_to_disk(parent)), subname);
820 else
821 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
822 "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000823
Martin K. Petersene1defc42009-05-22 17:17:49 -0400824 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500825 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000827
828 err_putdisk:
829 put_disk(md->disk);
830 err_kfree:
831 kfree(md);
832 out:
833 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Andrei Warkentin371a6892011-04-11 18:10:25 -0500836static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
837{
838 sector_t size;
839 struct mmc_blk_data *md;
840
841 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
842 /*
843 * The EXT_CSD sector count is in number or 512 byte
844 * sectors.
845 */
846 size = card->ext_csd.sectors;
847 } else {
848 /*
849 * The CSD capacity field is in units of read_blkbits.
850 * set_capacity takes units of 512 bytes.
851 */
852 size = card->csd.capacity << (card->csd.read_blkbits - 9);
853 }
854
855 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
856 return md;
857}
858
859static int mmc_blk_alloc_part(struct mmc_card *card,
860 struct mmc_blk_data *md,
861 unsigned int part_type,
862 sector_t size,
863 bool default_ro,
864 const char *subname)
865{
866 char cap_str[10];
867 struct mmc_blk_data *part_md;
868
869 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
870 subname);
871 if (IS_ERR(part_md))
872 return PTR_ERR(part_md);
873 part_md->part_type = part_type;
874 list_add(&part_md->part, &md->part);
875
876 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
877 cap_str, sizeof(cap_str));
878 printk(KERN_INFO "%s: %s %s partition %u %s\n",
879 part_md->disk->disk_name, mmc_card_id(card),
880 mmc_card_name(card), part_md->part_type, cap_str);
881 return 0;
882}
883
884static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
885{
886 int ret = 0;
887
888 if (!mmc_card_mmc(card))
889 return 0;
890
891 if (card->ext_csd.boot_size) {
892 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
893 card->ext_csd.boot_size >> 9,
894 true,
895 "boot0");
896 if (ret)
897 return ret;
898 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
899 card->ext_csd.boot_size >> 9,
900 true,
901 "boot1");
902 if (ret)
903 return ret;
904 }
905
906 return ret;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int
910mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
911{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int err;
913
Pierre Ossmanb8558852007-01-03 19:47:29 +0100914 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300915 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100916 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300919 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
920 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -EINVAL;
922 }
923
924 return 0;
925}
926
Andrei Warkentin371a6892011-04-11 18:10:25 -0500927static void mmc_blk_remove_req(struct mmc_blk_data *md)
928{
929 if (md) {
930 if (md->disk->flags & GENHD_FL_UP) {
931 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
932
933 /* Stop new requests from getting into the queue */
934 del_gendisk(md->disk);
935 }
936
937 /* Then flush out any already in there */
938 mmc_cleanup_queue(&md->queue);
939 mmc_blk_put(md);
940 }
941}
942
943static void mmc_blk_remove_parts(struct mmc_card *card,
944 struct mmc_blk_data *md)
945{
946 struct list_head *pos, *q;
947 struct mmc_blk_data *part_md;
948
949 list_for_each_safe(pos, q, &md->part) {
950 part_md = list_entry(pos, struct mmc_blk_data, part);
951 list_del(pos);
952 mmc_blk_remove_req(part_md);
953 }
954}
955
956static int mmc_add_disk(struct mmc_blk_data *md)
957{
958 int ret;
959
960 add_disk(md->disk);
961 md->force_ro.show = force_ro_show;
962 md->force_ro.store = force_ro_store;
963 md->force_ro.attr.name = "force_ro";
964 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
965 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
966 if (ret)
967 del_gendisk(md->disk);
968
969 return ret;
970}
971
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400972static const struct mmc_fixup blk_fixups[] =
973{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500974 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
975 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
976 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
977 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
978 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400979 END_FIXUP
980};
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982static int mmc_blk_probe(struct mmc_card *card)
983{
Andrei Warkentin371a6892011-04-11 18:10:25 -0500984 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200986 char cap_str[10];
987
Pierre Ossman912490d2005-05-21 10:27:02 +0100988 /*
989 * Check that the card supports the command class(es) we need.
990 */
991 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENODEV;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 md = mmc_blk_alloc(card);
995 if (IS_ERR(md))
996 return PTR_ERR(md);
997
998 err = mmc_blk_set_blksize(md, card);
999 if (err)
1000 goto out;
1001
Yi Li444122f2009-02-05 15:31:57 +08001002 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001003 cap_str, sizeof(cap_str));
1004 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001006 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Andrei Warkentin371a6892011-04-11 18:10:25 -05001008 if (mmc_blk_alloc_parts(card, md))
1009 goto out;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001012 mmc_fixup_device(card, blk_fixups);
1013
Andrei Warkentin371a6892011-04-11 18:10:25 -05001014 if (mmc_add_disk(md))
1015 goto out;
1016
1017 list_for_each_entry(part_md, &md->part, part) {
1018 if (mmc_add_disk(part_md))
1019 goto out;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022
1023 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001024 mmc_blk_remove_parts(card, md);
1025 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return err;
1027}
1028
1029static void mmc_blk_remove(struct mmc_card *card)
1030{
1031 struct mmc_blk_data *md = mmc_get_drvdata(card);
1032
Andrei Warkentin371a6892011-04-11 18:10:25 -05001033 mmc_blk_remove_parts(card, md);
1034 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 mmc_set_drvdata(card, NULL);
1036}
1037
1038#ifdef CONFIG_PM
1039static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1040{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001041 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct mmc_blk_data *md = mmc_get_drvdata(card);
1043
1044 if (md) {
1045 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001046 list_for_each_entry(part_md, &md->part, part) {
1047 mmc_queue_suspend(&part_md->queue);
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 return 0;
1051}
1052
1053static int mmc_blk_resume(struct mmc_card *card)
1054{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001055 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct mmc_blk_data *md = mmc_get_drvdata(card);
1057
1058 if (md) {
1059 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001060
1061 /*
1062 * Resume involves the card going into idle state,
1063 * so current partition is always the main one.
1064 */
1065 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001067 list_for_each_entry(part_md, &md->part, part) {
1068 mmc_queue_resume(&part_md->queue);
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 return 0;
1072}
1073#else
1074#define mmc_blk_suspend NULL
1075#define mmc_blk_resume NULL
1076#endif
1077
1078static struct mmc_driver mmc_driver = {
1079 .drv = {
1080 .name = "mmcblk",
1081 },
1082 .probe = mmc_blk_probe,
1083 .remove = mmc_blk_remove,
1084 .suspend = mmc_blk_suspend,
1085 .resume = mmc_blk_resume,
1086};
1087
1088static int __init mmc_blk_init(void)
1089{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001090 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001092 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1093 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1094
1095 max_devices = 256 / perdev_minors;
1096
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001097 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1098 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001101 res = mmc_register_driver(&mmc_driver);
1102 if (res)
1103 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001105 return 0;
1106 out2:
1107 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 out:
1109 return res;
1110}
1111
1112static void __exit mmc_blk_exit(void)
1113{
1114 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001115 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118module_init(mmc_blk_init);
1119module_exit(mmc_blk_exit);
1120
1121MODULE_LICENSE("GPL");
1122MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1123