blob: e5bf2bfb5463a917f64cebede2860cb279eb2806 [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);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050078static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * There is one mmc_blk_data per slot.
82 */
83struct mmc_blk_data {
84 spinlock_t lock;
85 struct gendisk *disk;
86 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050087 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000090 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050091 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -050092 unsigned int name_idx;
Andrei Warkentin371a6892011-04-11 18:10:25 -050093
94 /*
95 * Only set in main mmc_blk_data associated
96 * with mmc_card with mmc_set_drvdata, and keeps
97 * track of the current selected device partition.
98 */
99 unsigned int part_curr;
100 struct device_attribute force_ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Arjan van de Vena621aae2006-01-12 18:43:35 +0000103static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105module_param(perdev_minors, int, 0444);
106MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
109{
110 struct mmc_blk_data *md;
111
Arjan van de Vena621aae2006-01-12 18:43:35 +0000112 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 md = disk->private_data;
114 if (md && md->usage == 0)
115 md = NULL;
116 if (md)
117 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000118 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return md;
121}
122
Andrei Warkentin371a6892011-04-11 18:10:25 -0500123static inline int mmc_get_devidx(struct gendisk *disk)
124{
125 int devmaj = MAJOR(disk_devt(disk));
126 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
127
128 if (!devmaj)
129 devidx = disk->first_minor / perdev_minors;
130 return devidx;
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void mmc_blk_put(struct mmc_blk_data *md)
134{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 md->usage--;
137 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800139 blk_cleanup_queue(md->queue.queue);
140
David Woodhouse1dff3142007-11-21 18:45:12 +0100141 __clear_bit(devidx, dev_use);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 kfree(md);
145 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000146 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Andrei Warkentin371a6892011-04-11 18:10:25 -0500149static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
150 char *buf)
151{
152 int ret;
153 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
154
155 ret = snprintf(buf, PAGE_SIZE, "%d",
156 get_disk_ro(dev_to_disk(dev)) ^
157 md->read_only);
158 mmc_blk_put(md);
159 return ret;
160}
161
162static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
163 const char *buf, size_t count)
164{
165 int ret;
166 char *end;
167 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
168 unsigned long set = simple_strtoul(buf, &end, 0);
169 if (end == buf) {
170 ret = -EINVAL;
171 goto out;
172 }
173
174 set_disk_ro(dev_to_disk(dev), set || md->read_only);
175 ret = count;
176out:
177 mmc_blk_put(md);
178 return ret;
179}
180
Al Viroa5a15612008-03-02 10:33:30 -0500181static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Viroa5a15612008-03-02 10:33:30 -0500183 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int ret = -ENXIO;
185
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200186 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (md) {
188 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500189 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700191
Al Viroa5a15612008-03-02 10:33:30 -0500192 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700193 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700194 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200197 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 return ret;
200}
201
Al Viroa5a15612008-03-02 10:33:30 -0500202static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Al Viroa5a15612008-03-02 10:33:30 -0500204 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200206 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200208 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210}
211
212static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800213mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800215 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
216 geo->heads = 4;
217 geo->sectors = 16;
218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700221static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500222 .open = mmc_blk_open,
223 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800224 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .owner = THIS_MODULE,
226};
227
228struct mmc_blk_request {
229 struct mmc_request mrq;
230 struct mmc_command cmd;
231 struct mmc_command stop;
232 struct mmc_data data;
233};
234
Andrei Warkentin371a6892011-04-11 18:10:25 -0500235static inline int mmc_blk_part_switch(struct mmc_card *card,
236 struct mmc_blk_data *md)
237{
238 int ret;
239 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
240 if (main_md->part_curr == md->part_type)
241 return 0;
242
243 if (mmc_card_mmc(card)) {
244 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
245 card->ext_csd.part_config |= md->part_type;
246
247 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
248 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
249 card->ext_csd.part_time);
250 if (ret)
251 return ret;
252}
253
254 main_md->part_curr = md->part_type;
255 return 0;
256}
257
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700258static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
259{
260 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100261 u32 result;
262 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700263
Chris Ball24f5b532011-04-13 23:49:45 -0400264 struct mmc_request mrq = {0};
Chris Ball1278dba2011-04-13 23:40:30 -0400265 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400266 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700267 unsigned int timeout_us;
268
269 struct scatterlist sg;
270
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700271 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
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700287 data.timeout_ns = card->csd.tacc_ns * 100;
288 data.timeout_clks = card->csd.tacc_clks * 100;
289
290 timeout_us = data.timeout_ns / 1000;
291 timeout_us += data.timeout_clks * 1000 /
292 (card->host->ios.clock / 1000);
293
294 if (timeout_us > 100000) {
295 data.timeout_ns = 100000000;
296 data.timeout_clks = 0;
297 }
298
299 data.blksz = 4;
300 data.blocks = 1;
301 data.flags = MMC_DATA_READ;
302 data.sg = &sg;
303 data.sg_len = 1;
304
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700305 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 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500772 * !subname implies we are creating main mmc_blk_data that will be
773 * associated with mmc_card with mmc_set_drvdata. Due to device
774 * partitions, devidx will not coincide with a per-physical card
775 * index anymore so we keep track of a name index.
776 */
777 if (!subname) {
778 md->name_idx = find_first_zero_bit(name_use, max_devices);
779 __set_bit(md->name_idx, name_use);
780 }
781 else
782 md->name_idx = ((struct mmc_blk_data *)
783 dev_to_disk(parent)->private_data)->name_idx;
784
785 /*
Russell Kinga6f6c962006-01-03 22:38:44 +0000786 * Set the read-only status based on the supported commands
787 * and the write protect switch.
788 */
789 md->read_only = mmc_blk_readonly(card);
790
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400791 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +0000792 if (md->disk == NULL) {
793 ret = -ENOMEM;
794 goto err_kfree;
795 }
796
797 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500798 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +0000799 md->usage = 1;
800
801 ret = mmc_init_queue(&md->queue, card, &md->lock);
802 if (ret)
803 goto err_putdisk;
804
Russell Kinga6f6c962006-01-03 22:38:44 +0000805 md->queue.issue_fn = mmc_blk_issue_rq;
806 md->queue.data = md;
807
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200808 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400809 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +0000810 md->disk->fops = &mmc_bdops;
811 md->disk->private_data = md;
812 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500813 md->disk->driverfs_dev = parent;
814 set_disk_ro(md->disk, md->read_only || default_ro);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500815 if (REL_WRITES_SUPPORTED(card))
816 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
Russell Kinga6f6c962006-01-03 22:38:44 +0000817
818 /*
819 * As discussed on lkml, GENHD_FL_REMOVABLE should:
820 *
821 * - be set for removable media with permanent block devices
822 * - be unset for removable block devices with permanent media
823 *
824 * Since MMC block devices clearly fall under the second
825 * case, we do not set GENHD_FL_REMOVABLE. Userspace
826 * should use the block device creation/destruction hotplug
827 * messages to tell when the card is present.
828 */
829
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500830 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
831 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +0000832
Martin K. Petersene1defc42009-05-22 17:17:49 -0400833 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500834 set_capacity(md->disk, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000836
837 err_putdisk:
838 put_disk(md->disk);
839 err_kfree:
840 kfree(md);
841 out:
842 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Andrei Warkentin371a6892011-04-11 18:10:25 -0500845static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
846{
847 sector_t size;
848 struct mmc_blk_data *md;
849
850 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
851 /*
852 * The EXT_CSD sector count is in number or 512 byte
853 * sectors.
854 */
855 size = card->ext_csd.sectors;
856 } else {
857 /*
858 * The CSD capacity field is in units of read_blkbits.
859 * set_capacity takes units of 512 bytes.
860 */
861 size = card->csd.capacity << (card->csd.read_blkbits - 9);
862 }
863
864 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
865 return md;
866}
867
868static int mmc_blk_alloc_part(struct mmc_card *card,
869 struct mmc_blk_data *md,
870 unsigned int part_type,
871 sector_t size,
872 bool default_ro,
873 const char *subname)
874{
875 char cap_str[10];
876 struct mmc_blk_data *part_md;
877
878 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
879 subname);
880 if (IS_ERR(part_md))
881 return PTR_ERR(part_md);
882 part_md->part_type = part_type;
883 list_add(&part_md->part, &md->part);
884
885 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
886 cap_str, sizeof(cap_str));
887 printk(KERN_INFO "%s: %s %s partition %u %s\n",
888 part_md->disk->disk_name, mmc_card_id(card),
889 mmc_card_name(card), part_md->part_type, cap_str);
890 return 0;
891}
892
893static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
894{
895 int ret = 0;
896
897 if (!mmc_card_mmc(card))
898 return 0;
899
900 if (card->ext_csd.boot_size) {
901 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
902 card->ext_csd.boot_size >> 9,
903 true,
904 "boot0");
905 if (ret)
906 return ret;
907 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
908 card->ext_csd.boot_size >> 9,
909 true,
910 "boot1");
911 if (ret)
912 return ret;
913 }
914
915 return ret;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static int
919mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int err;
922
Pierre Ossmanb8558852007-01-03 19:47:29 +0100923 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300924 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100925 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300928 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
929 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EINVAL;
931 }
932
933 return 0;
934}
935
Andrei Warkentin371a6892011-04-11 18:10:25 -0500936static void mmc_blk_remove_req(struct mmc_blk_data *md)
937{
938 if (md) {
939 if (md->disk->flags & GENHD_FL_UP) {
940 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
941
942 /* Stop new requests from getting into the queue */
943 del_gendisk(md->disk);
944 }
945
946 /* Then flush out any already in there */
947 mmc_cleanup_queue(&md->queue);
948 mmc_blk_put(md);
949 }
950}
951
952static void mmc_blk_remove_parts(struct mmc_card *card,
953 struct mmc_blk_data *md)
954{
955 struct list_head *pos, *q;
956 struct mmc_blk_data *part_md;
957
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500958 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -0500959 list_for_each_safe(pos, q, &md->part) {
960 part_md = list_entry(pos, struct mmc_blk_data, part);
961 list_del(pos);
962 mmc_blk_remove_req(part_md);
963 }
964}
965
966static int mmc_add_disk(struct mmc_blk_data *md)
967{
968 int ret;
969
970 add_disk(md->disk);
971 md->force_ro.show = force_ro_show;
972 md->force_ro.store = force_ro_store;
973 md->force_ro.attr.name = "force_ro";
974 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
975 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
976 if (ret)
977 del_gendisk(md->disk);
978
979 return ret;
980}
981
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400982static const struct mmc_fixup blk_fixups[] =
983{
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500984 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
985 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
986 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
987 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
988 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
Andrei Warkentin6f60c222011-04-11 19:11:04 -0400989 END_FIXUP
990};
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static int mmc_blk_probe(struct mmc_card *card)
993{
Andrei Warkentin371a6892011-04-11 18:10:25 -0500994 struct mmc_blk_data *md, *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int err;
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200996 char cap_str[10];
997
Pierre Ossman912490d2005-05-21 10:27:02 +0100998 /*
999 * Check that the card supports the command class(es) we need.
1000 */
1001 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return -ENODEV;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 md = mmc_blk_alloc(card);
1005 if (IS_ERR(md))
1006 return PTR_ERR(md);
1007
1008 err = mmc_blk_set_blksize(md, card);
1009 if (err)
1010 goto out;
1011
Yi Li444122f2009-02-05 15:31:57 +08001012 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001013 cap_str, sizeof(cap_str));
1014 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001016 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Andrei Warkentin371a6892011-04-11 18:10:25 -05001018 if (mmc_blk_alloc_parts(card, md))
1019 goto out;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001022 mmc_fixup_device(card, blk_fixups);
1023
Andrei Warkentin371a6892011-04-11 18:10:25 -05001024 if (mmc_add_disk(md))
1025 goto out;
1026
1027 list_for_each_entry(part_md, &md->part, part) {
1028 if (mmc_add_disk(part_md))
1029 goto out;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
1032
1033 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001034 mmc_blk_remove_parts(card, md);
1035 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return err;
1037}
1038
1039static void mmc_blk_remove(struct mmc_card *card)
1040{
1041 struct mmc_blk_data *md = mmc_get_drvdata(card);
1042
Andrei Warkentin371a6892011-04-11 18:10:25 -05001043 mmc_blk_remove_parts(card, md);
1044 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 mmc_set_drvdata(card, NULL);
1046}
1047
1048#ifdef CONFIG_PM
1049static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1050{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001051 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct mmc_blk_data *md = mmc_get_drvdata(card);
1053
1054 if (md) {
1055 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001056 list_for_each_entry(part_md, &md->part, part) {
1057 mmc_queue_suspend(&part_md->queue);
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 return 0;
1061}
1062
1063static int mmc_blk_resume(struct mmc_card *card)
1064{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001065 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct mmc_blk_data *md = mmc_get_drvdata(card);
1067
1068 if (md) {
1069 mmc_blk_set_blksize(md, card);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001070
1071 /*
1072 * Resume involves the card going into idle state,
1073 * so current partition is always the main one.
1074 */
1075 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001077 list_for_each_entry(part_md, &md->part, part) {
1078 mmc_queue_resume(&part_md->queue);
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 return 0;
1082}
1083#else
1084#define mmc_blk_suspend NULL
1085#define mmc_blk_resume NULL
1086#endif
1087
1088static struct mmc_driver mmc_driver = {
1089 .drv = {
1090 .name = "mmcblk",
1091 },
1092 .probe = mmc_blk_probe,
1093 .remove = mmc_blk_remove,
1094 .suspend = mmc_blk_suspend,
1095 .resume = mmc_blk_resume,
1096};
1097
1098static int __init mmc_blk_init(void)
1099{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001100 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001102 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1103 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1104
1105 max_devices = 256 / perdev_minors;
1106
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001107 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1108 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001111 res = mmc_register_driver(&mmc_driver);
1112 if (res)
1113 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001115 return 0;
1116 out2:
1117 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 out:
1119 return res;
1120}
1121
1122static void __exit mmc_blk_exit(void)
1123{
1124 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001125 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128module_init(mmc_blk_init);
1129module_exit(mmc_blk_exit);
1130
1131MODULE_LICENSE("GPL");
1132MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1133