blob: a9970504cabbad33f7830bfeebcf1120bb2138a1 [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");
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * max 8 partitions per card
49 */
50#define MMC_SHIFT 3
David Woodhouse1dff3142007-11-21 18:45:12 +010051#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
52
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020053static DEFINE_MUTEX(block_mutex);
Ben Collins203c8012008-06-05 19:10:21 -040054static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * There is one mmc_blk_data per slot.
58 */
59struct mmc_blk_data {
60 spinlock_t lock;
61 struct gendisk *disk;
62 struct mmc_queue queue;
63
64 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000065 unsigned int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Arjan van de Vena621aae2006-01-12 18:43:35 +000068static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
71{
72 struct mmc_blk_data *md;
73
Arjan van de Vena621aae2006-01-12 18:43:35 +000074 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 md = disk->private_data;
76 if (md && md->usage == 0)
77 md = NULL;
78 if (md)
79 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +000080 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 return md;
83}
84
85static void mmc_blk_put(struct mmc_blk_data *md)
86{
Arjan van de Vena621aae2006-01-12 18:43:35 +000087 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 md->usage--;
89 if (md->usage == 0) {
Anna Lemehova7d92df62010-01-08 14:42:58 -080090 int devmaj = MAJOR(disk_devt(md->disk));
Tejun Heof331c022008-09-03 09:01:48 +020091 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
Anna Lemehova7d92df62010-01-08 14:42:58 -080092
93 if (!devmaj)
94 devidx = md->disk->first_minor >> MMC_SHIFT;
95
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080096 blk_cleanup_queue(md->queue.queue);
97
David Woodhouse1dff3142007-11-21 18:45:12 +010098 __clear_bit(devidx, dev_use);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(md);
102 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000103 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Al Viroa5a15612008-03-02 10:33:30 -0500106static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Al Viroa5a15612008-03-02 10:33:30 -0500108 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int ret = -ENXIO;
110
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200111 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (md) {
113 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500114 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700116
Al Viroa5a15612008-03-02 10:33:30 -0500117 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700118 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700119 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200122 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return ret;
125}
126
Al Viroa5a15612008-03-02 10:33:30 -0500127static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Al Viroa5a15612008-03-02 10:33:30 -0500129 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200131 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200133 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135}
136
137static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800138mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800140 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
141 geo->heads = 4;
142 geo->sectors = 16;
143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700146static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500147 .open = mmc_blk_open,
148 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800149 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .owner = THIS_MODULE,
151};
152
153struct mmc_blk_request {
154 struct mmc_request mrq;
155 struct mmc_command cmd;
156 struct mmc_command stop;
157 struct mmc_data data;
158};
159
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700160static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
161{
162 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100163 u32 result;
164 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700165
166 struct mmc_request mrq;
167 struct mmc_command cmd;
168 struct mmc_data data;
169 unsigned int timeout_us;
170
171 struct scatterlist sg;
172
173 memset(&cmd, 0, sizeof(struct mmc_command));
174
175 cmd.opcode = MMC_APP_CMD;
176 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700177 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700178
179 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700180 if (err)
181 return (u32)-1;
182 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700183 return (u32)-1;
184
185 memset(&cmd, 0, sizeof(struct mmc_command));
186
187 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
188 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700189 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700190
191 memset(&data, 0, sizeof(struct mmc_data));
192
193 data.timeout_ns = card->csd.tacc_ns * 100;
194 data.timeout_clks = card->csd.tacc_clks * 100;
195
196 timeout_us = data.timeout_ns / 1000;
197 timeout_us += data.timeout_clks * 1000 /
198 (card->host->ios.clock / 1000);
199
200 if (timeout_us > 100000) {
201 data.timeout_ns = 100000000;
202 data.timeout_clks = 0;
203 }
204
205 data.blksz = 4;
206 data.blocks = 1;
207 data.flags = MMC_DATA_READ;
208 data.sg = &sg;
209 data.sg_len = 1;
210
211 memset(&mrq, 0, sizeof(struct mmc_request));
212
213 mrq.cmd = &cmd;
214 mrq.data = &data;
215
Ben Dooks051913d2009-06-08 23:33:57 +0100216 blocks = kmalloc(4, GFP_KERNEL);
217 if (!blocks)
218 return (u32)-1;
219
220 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700221
222 mmc_wait_for_req(card->host, &mrq);
223
Ben Dooks051913d2009-06-08 23:33:57 +0100224 result = ntohl(*blocks);
225 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700226
Ben Dooks051913d2009-06-08 23:33:57 +0100227 if (cmd.error || data.error)
228 result = (u32)-1;
229
230 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700231}
232
Adrian Hunter504f1912008-10-16 12:55:25 +0300233static u32 get_card_status(struct mmc_card *card, struct request *req)
234{
235 struct mmc_command cmd;
236 int err;
237
238 memset(&cmd, 0, sizeof(struct mmc_command));
239 cmd.opcode = MMC_SEND_STATUS;
240 if (!mmc_host_is_spi(card->host))
241 cmd.arg = card->rca << 16;
242 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
243 err = mmc_wait_for_cmd(card->host, &cmd, 0);
244 if (err)
245 printk(KERN_ERR "%s: error %d sending status comand",
246 req->rq_disk->disk_name, err);
247 return cmd.resp[0];
248}
249
Adrian Hunterbd788c92010-08-11 14:17:47 -0700250static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
251{
252 struct mmc_blk_data *md = mq->data;
253 struct mmc_card *card = md->queue.card;
254 unsigned int from, nr, arg;
255 int err = 0;
256
257 mmc_claim_host(card->host);
258
259 if (!mmc_can_erase(card)) {
260 err = -EOPNOTSUPP;
261 goto out;
262 }
263
264 from = blk_rq_pos(req);
265 nr = blk_rq_sectors(req);
266
267 if (mmc_can_trim(card))
268 arg = MMC_TRIM_ARG;
269 else
270 arg = MMC_ERASE_ARG;
271
272 err = mmc_erase(card, from, nr, arg);
273out:
274 spin_lock_irq(&md->lock);
275 __blk_end_request(req, err, blk_rq_bytes(req));
276 spin_unlock_irq(&md->lock);
277
278 mmc_release_host(card->host);
279
280 return err ? 0 : 1;
281}
282
Adrian Hunter49804542010-08-11 14:17:50 -0700283static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
284 struct request *req)
285{
286 struct mmc_blk_data *md = mq->data;
287 struct mmc_card *card = md->queue.card;
288 unsigned int from, nr, arg;
289 int err = 0;
290
291 mmc_claim_host(card->host);
292
293 if (!mmc_can_secure_erase_trim(card)) {
294 err = -EOPNOTSUPP;
295 goto out;
296 }
297
298 from = blk_rq_pos(req);
299 nr = blk_rq_sectors(req);
300
301 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
302 arg = MMC_SECURE_TRIM1_ARG;
303 else
304 arg = MMC_SECURE_ERASE_ARG;
305
306 err = mmc_erase(card, from, nr, arg);
307 if (!err && arg == MMC_SECURE_TRIM1_ARG)
308 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
309out:
310 spin_lock_irq(&md->lock);
311 __blk_end_request(req, err, blk_rq_bytes(req));
312 spin_unlock_irq(&md->lock);
313
314 mmc_release_host(card->host);
315
316 return err ? 0 : 1;
317}
318
Adrian Hunterbd788c92010-08-11 14:17:47 -0700319static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct mmc_blk_data *md = mq->data;
322 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700323 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100324 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Pierre Ossmanb8558852007-01-03 19:47:29 +0100326 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct mmc_command cmd;
Adrian Hunter504f1912008-10-16 12:55:25 +0300330 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 memset(&brq, 0, sizeof(struct mmc_blk_request));
333 brq.mrq.cmd = &brq.cmd;
334 brq.mrq.data = &brq.data;
335
Tejun Heo83096eb2009-05-07 22:24:39 +0900336 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800337 if (!mmc_card_blockaddr(card))
338 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700339 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200340 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 brq.stop.opcode = MMC_STOP_TRANSMISSION;
342 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700343 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900344 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Adrian Hunter6a79e392008-12-31 18:21:17 +0100346 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200347 * The block layer doesn't support all sector count
348 * restrictions, so we need to be prepared for too big
349 * requests.
350 */
351 if (brq.data.blocks > card->host->max_blk_count)
352 brq.data.blocks = card->host->max_blk_count;
353
354 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100355 * After a read error, we redo the request one sector at a time
356 * in order to accurately determine which sectors can be read
357 * successfully.
358 */
359 if (disable_multi && brq.data.blocks > 1)
360 brq.data.blocks = 1;
361
Russell King788ee7b2006-01-09 21:12:17 +0000362 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700363 /* SPI multiblock writes terminate using a special
364 * token, not a STOP_TRANSMISSION request.
365 */
366 if (!mmc_host_is_spi(card->host)
367 || rq_data_dir(req) == READ)
368 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100369 readcmd = MMC_READ_MULTIPLE_BLOCK;
370 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000371 } else {
372 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100373 readcmd = MMC_READ_SINGLE_BLOCK;
374 writecmd = MMC_WRITE_BLOCK;
375 }
Russell Kingdb53f282006-08-30 15:14:56 +0100376 if (rq_data_dir(req) == READ) {
377 brq.cmd.opcode = readcmd;
378 brq.data.flags |= MMC_DATA_READ;
379 } else {
380 brq.cmd.opcode = writecmd;
381 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Pierre Ossmanb146d262007-07-24 19:16:54 +0200384 mmc_set_data_timeout(&brq.data, card);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200387 brq.data.sg_len = mmc_queue_map_sg(mq);
388
Adrian Hunter6a79e392008-12-31 18:21:17 +0100389 /*
390 * Adjust the sg list so it is the same size as the
391 * request.
392 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900393 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100394 int i, data_size = brq.data.blocks << 9;
395 struct scatterlist *sg;
396
397 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
398 data_size -= sg->length;
399 if (data_size <= 0) {
400 sg->length += data_size;
401 i++;
402 break;
403 }
404 }
405 brq.data.sg_len = i;
406 }
407
Pierre Ossman98ccf142007-05-12 00:26:16 +0200408 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200411
412 mmc_queue_bounce_post(mq);
413
Pierre Ossman979ce722008-06-29 12:19:47 +0200414 /*
415 * Check for errors here, but don't jump to cmd_err
416 * until later as we need to wait for the card to leave
417 * programming mode even when things go wrong.
418 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100419 if (brq.cmd.error || brq.data.error || brq.stop.error) {
420 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
421 /* Redo read one sector at a time */
422 printk(KERN_WARNING "%s: retrying using single "
423 "block read\n", req->rq_disk->disk_name);
424 disable_multi = 1;
425 continue;
426 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300427 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100428 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300431 printk(KERN_ERR "%s: error %d sending read/write "
432 "command, response %#x, card status %#x\n",
433 req->rq_disk->disk_name, brq.cmd.error,
434 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300438 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
439 /* 'Stop' response contains card status */
440 status = brq.mrq.stop->resp[0];
441 printk(KERN_ERR "%s: error %d transferring data,"
442 " sector %u, nr %u, card status %#x\n",
443 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900444 (unsigned)blk_rq_pos(req),
445 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300449 printk(KERN_ERR "%s: error %d sending stop command, "
450 "response %#x, card status %#x\n",
451 req->rq_disk->disk_name, brq.stop.error,
452 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
David Brownell7213d172007-08-08 09:10:23 -0700455 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100456 do {
457 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Russell King2ed6d222006-09-24 10:46:43 +0100459 cmd.opcode = MMC_SEND_STATUS;
460 cmd.arg = card->rca << 16;
461 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
462 err = mmc_wait_for_cmd(card->host, &cmd, 5);
463 if (err) {
464 printk(KERN_ERR "%s: error %d requesting status\n",
465 req->rq_disk->disk_name, err);
466 goto cmd_err;
467 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100468 /*
469 * Some cards mishandle the status bits,
470 * so make sure to check both the busy
471 * indication and the card state.
472 */
473 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
474 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100477 if (cmd.resp[0] & ~0x00000900)
478 printk(KERN_ERR "%s: status = %08x\n",
479 req->rq_disk->disk_name, cmd.resp[0]);
480 if (mmc_decode_status(cmd.resp))
481 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#endif
Russell King2ed6d222006-09-24 10:46:43 +0100483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Adrian Hunter6a79e392008-12-31 18:21:17 +0100485 if (brq.cmd.error || brq.stop.error || brq.data.error) {
486 if (rq_data_dir(req) == READ) {
487 /*
488 * After an error, we redo I/O one sector at a
489 * time, so we only reach here after trying to
490 * read a single sector.
491 */
492 spin_lock_irq(&md->lock);
493 ret = __blk_end_request(req, -EIO, brq.data.blksz);
494 spin_unlock_irq(&md->lock);
495 continue;
496 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200497 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100498 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * A block was successfully transferred.
502 */
503 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500504 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_unlock_irq(&md->lock);
506 } while (ret);
507
Pierre Ossmanb8558852007-01-03 19:47:29 +0100508 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 return 1;
511
512 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700513 /*
514 * If this is an SD card and we're writing, we can first
515 * mark the known good sectors as ok.
516 *
517 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200518 * as reported by the controller (which might be less than
519 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100521 if (mmc_card_sd(card)) {
522 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700523
Adrian Hunter6a79e392008-12-31 18:21:17 +0100524 blocks = mmc_sd_num_wr_blocks(card);
525 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700526 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100527 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700528 spin_unlock_irq(&md->lock);
529 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100530 } else {
531 spin_lock_irq(&md->lock);
532 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
533 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700534 }
535
Pierre Ossmanb8558852007-01-03 19:47:29 +0100536 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500539 while (ret)
540 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 spin_unlock_irq(&md->lock);
542
543 return 0;
544}
545
Adrian Hunterbd788c92010-08-11 14:17:47 -0700546static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
547{
Adrian Hunter49804542010-08-11 14:17:50 -0700548 if (req->cmd_flags & REQ_DISCARD) {
549 if (req->cmd_flags & REQ_SECURE)
550 return mmc_blk_issue_secdiscard_rq(mq, req);
551 else
552 return mmc_blk_issue_discard_rq(mq, req);
553 } else {
Adrian Hunterbd788c92010-08-11 14:17:47 -0700554 return mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -0700555 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Russell Kinga6f6c962006-01-03 22:38:44 +0000558static inline int mmc_blk_readonly(struct mmc_card *card)
559{
560 return mmc_card_readonly(card) ||
561 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
565{
566 struct mmc_blk_data *md;
567 int devidx, ret;
568
569 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
570 if (devidx >= MMC_NUM_MINORS)
571 return ERR_PTR(-ENOSPC);
572 __set_bit(devidx, dev_use);
573
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700574 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000575 if (!md) {
576 ret = -ENOMEM;
577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000579
Russell Kinga6f6c962006-01-03 22:38:44 +0000580
581 /*
582 * Set the read-only status based on the supported commands
583 * and the write protect switch.
584 */
585 md->read_only = mmc_blk_readonly(card);
586
Russell Kinga6f6c962006-01-03 22:38:44 +0000587 md->disk = alloc_disk(1 << MMC_SHIFT);
588 if (md->disk == NULL) {
589 ret = -ENOMEM;
590 goto err_kfree;
591 }
592
593 spin_lock_init(&md->lock);
594 md->usage = 1;
595
596 ret = mmc_init_queue(&md->queue, card, &md->lock);
597 if (ret)
598 goto err_putdisk;
599
Russell Kinga6f6c962006-01-03 22:38:44 +0000600 md->queue.issue_fn = mmc_blk_issue_rq;
601 md->queue.data = md;
602
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200603 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000604 md->disk->first_minor = devidx << MMC_SHIFT;
605 md->disk->fops = &mmc_bdops;
606 md->disk->private_data = md;
607 md->disk->queue = md->queue.queue;
608 md->disk->driverfs_dev = &card->dev;
609
610 /*
611 * As discussed on lkml, GENHD_FL_REMOVABLE should:
612 *
613 * - be set for removable media with permanent block devices
614 * - be unset for removable block devices with permanent media
615 *
616 * Since MMC block devices clearly fall under the second
617 * case, we do not set GENHD_FL_REMOVABLE. Userspace
618 * should use the block device creation/destruction hotplug
619 * messages to tell when the card is present.
620 */
621
JiebingLi12578f62010-06-17 15:58:46 +0100622 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
623 "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000624
Martin K. Petersene1defc42009-05-22 17:17:49 -0400625 blk_queue_logical_block_size(md->queue.queue, 512);
Russell Kinga6f6c962006-01-03 22:38:44 +0000626
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100627 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
628 /*
629 * The EXT_CSD sector count is in number or 512 byte
630 * sectors.
631 */
632 set_capacity(md->disk, card->ext_csd.sectors);
633 } else {
634 /*
635 * The CSD capacity field is in units of read_blkbits.
636 * set_capacity takes units of 512 bytes.
637 */
638 set_capacity(md->disk,
639 card->csd.capacity << (card->csd.read_blkbits - 9));
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000642
643 err_putdisk:
644 put_disk(md->disk);
645 err_kfree:
646 kfree(md);
647 out:
648 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static int
652mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int err;
655
Pierre Ossmanb8558852007-01-03 19:47:29 +0100656 mmc_claim_host(card->host);
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300657 err = mmc_set_blocklen(card, 512);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100658 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (err) {
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300661 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
662 md->disk->disk_name, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EINVAL;
664 }
665
666 return 0;
667}
668
669static int mmc_blk_probe(struct mmc_card *card)
670{
671 struct mmc_blk_data *md;
672 int err;
673
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200674 char cap_str[10];
675
Pierre Ossman912490d2005-05-21 10:27:02 +0100676 /*
677 * Check that the card supports the command class(es) we need.
678 */
679 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -ENODEV;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 md = mmc_blk_alloc(card);
683 if (IS_ERR(md))
684 return PTR_ERR(md);
685
686 err = mmc_blk_set_blksize(md, card);
687 if (err)
688 goto out;
689
Yi Li444122f2009-02-05 15:31:57 +0800690 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200691 cap_str, sizeof(cap_str));
692 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200694 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 mmc_set_drvdata(card, md);
697 add_disk(md->disk);
698 return 0;
699
700 out:
Jarkko Lavinen0a74ff22010-01-08 14:42:59 -0800701 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 mmc_blk_put(md);
703
704 return err;
705}
706
707static void mmc_blk_remove(struct mmc_card *card)
708{
709 struct mmc_blk_data *md = mmc_get_drvdata(card);
710
711 if (md) {
Pierre Ossman89b4e132006-11-14 22:08:16 +0100712 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 del_gendisk(md->disk);
714
Pierre Ossman89b4e132006-11-14 22:08:16 +0100715 /* Then flush out any already in there */
716 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 mmc_blk_put(md);
719 }
720 mmc_set_drvdata(card, NULL);
721}
722
723#ifdef CONFIG_PM
724static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
725{
726 struct mmc_blk_data *md = mmc_get_drvdata(card);
727
728 if (md) {
729 mmc_queue_suspend(&md->queue);
730 }
731 return 0;
732}
733
734static int mmc_blk_resume(struct mmc_card *card)
735{
736 struct mmc_blk_data *md = mmc_get_drvdata(card);
737
738 if (md) {
739 mmc_blk_set_blksize(md, card);
740 mmc_queue_resume(&md->queue);
741 }
742 return 0;
743}
744#else
745#define mmc_blk_suspend NULL
746#define mmc_blk_resume NULL
747#endif
748
749static struct mmc_driver mmc_driver = {
750 .drv = {
751 .name = "mmcblk",
752 },
753 .probe = mmc_blk_probe,
754 .remove = mmc_blk_remove,
755 .suspend = mmc_blk_suspend,
756 .resume = mmc_blk_resume,
757};
758
759static int __init mmc_blk_init(void)
760{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900761 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200763 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
764 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900767 res = mmc_register_driver(&mmc_driver);
768 if (res)
769 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900771 return 0;
772 out2:
773 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 out:
775 return res;
776}
777
778static void __exit mmc_blk_exit(void)
779{
780 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200781 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784module_init(mmc_blk_init);
785module_exit(mmc_blk_exit);
786
787MODULE_LICENSE("GPL");
788MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
789