]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mmc/mmc_block.c
[MMC] MMC_CAP_BYTEBLOCK flag for non-log2 block sizes capable hosts
[mirror_ubuntu-artful-kernel.git] / drivers / mmc / mmc_block.c
CommitLineData
1da177e4
LT
1/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
5 *
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
9 *
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
13 *
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
15 *
16 * Author: Andrew Christian
17 * 28 May 2002
18 */
19#include <linux/moduleparam.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
23#include <linux/sched.h>
24#include <linux/kernel.h>
25#include <linux/fs.h>
26#include <linux/errno.h>
27#include <linux/hdreg.h>
28#include <linux/kdev_t.h>
29#include <linux/blkdev.h>
a621aaed 30#include <linux/mutex.h>
1da177e4
LT
31
32#include <linux/mmc/card.h>
385e3227 33#include <linux/mmc/host.h>
1da177e4 34#include <linux/mmc/protocol.h>
db53f28b 35#include <linux/mmc/host.h>
1da177e4
LT
36
37#include <asm/system.h>
38#include <asm/uaccess.h>
39
40#include "mmc_queue.h"
41
42/*
43 * max 8 partitions per card
44 */
45#define MMC_SHIFT 3
46
47static int major;
48
49/*
50 * There is one mmc_blk_data per slot.
51 */
52struct mmc_blk_data {
53 spinlock_t lock;
54 struct gendisk *disk;
55 struct mmc_queue queue;
56
57 unsigned int usage;
58 unsigned int block_bits;
a6f6c96b 59 unsigned int read_only;
1da177e4
LT
60};
61
a621aaed 62static DEFINE_MUTEX(open_lock);
1da177e4
LT
63
64static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
65{
66 struct mmc_blk_data *md;
67
a621aaed 68 mutex_lock(&open_lock);
1da177e4
LT
69 md = disk->private_data;
70 if (md && md->usage == 0)
71 md = NULL;
72 if (md)
73 md->usage++;
a621aaed 74 mutex_unlock(&open_lock);
1da177e4
LT
75
76 return md;
77}
78
79static void mmc_blk_put(struct mmc_blk_data *md)
80{
a621aaed 81 mutex_lock(&open_lock);
1da177e4
LT
82 md->usage--;
83 if (md->usage == 0) {
84 put_disk(md->disk);
85 mmc_cleanup_queue(&md->queue);
86 kfree(md);
87 }
a621aaed 88 mutex_unlock(&open_lock);
1da177e4
LT
89}
90
91static int mmc_blk_open(struct inode *inode, struct file *filp)
92{
93 struct mmc_blk_data *md;
94 int ret = -ENXIO;
95
96 md = mmc_blk_get(inode->i_bdev->bd_disk);
97 if (md) {
98 if (md->usage == 2)
99 check_disk_change(inode->i_bdev);
100 ret = 0;
a00fc090 101
a6f6c96b 102 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
a00fc090 103 ret = -EROFS;
1da177e4
LT
104 }
105
106 return ret;
107}
108
109static int mmc_blk_release(struct inode *inode, struct file *filp)
110{
111 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
112
113 mmc_blk_put(md);
114 return 0;
115}
116
117static int
a885c8c4 118mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4 119{
a885c8c4
CH
120 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
121 geo->heads = 4;
122 geo->sectors = 16;
123 return 0;
1da177e4
LT
124}
125
126static struct block_device_operations mmc_bdops = {
127 .open = mmc_blk_open,
128 .release = mmc_blk_release,
a885c8c4 129 .getgeo = mmc_blk_getgeo,
1da177e4
LT
130 .owner = THIS_MODULE,
131};
132
133struct mmc_blk_request {
134 struct mmc_request mrq;
135 struct mmc_command cmd;
136 struct mmc_command stop;
137 struct mmc_data data;
138};
139
140static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
141{
142 struct mmc_blk_data *md = mq->data;
143 int stat = BLKPREP_OK;
144
145 /*
146 * If we have no device, we haven't finished initialising.
147 */
148 if (!md || !mq->card) {
149 printk(KERN_ERR "%s: killing request - no device/host\n",
150 req->rq_disk->disk_name);
151 stat = BLKPREP_KILL;
152 }
153
154 return stat;
155}
156
157static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
158{
159 struct mmc_blk_data *md = mq->data;
160 struct mmc_card *card = md->queue.card;
161 int ret;
162
163 if (mmc_card_claim_host(card))
164 goto cmd_err;
165
166 do {
167 struct mmc_blk_request brq;
168 struct mmc_command cmd;
db53f28b 169 u32 readcmd, writecmd;
1da177e4
LT
170
171 memset(&brq, 0, sizeof(struct mmc_blk_request));
172 brq.mrq.cmd = &brq.cmd;
173 brq.mrq.data = &brq.data;
174
175 brq.cmd.arg = req->sector << 9;
e9225176 176 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2c171bf1 177 brq.data.blksz = 1 << md->block_bits;
1da177e4
LT
178 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
179 brq.stop.opcode = MMC_STOP_TRANSMISSION;
180 brq.stop.arg = 0;
e9225176 181 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1da177e4 182
d773d725 183 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
385e3227 184
db53f28b
RK
185 /*
186 * If the host doesn't support multiple block writes, force
187 * block writes to single block.
188 */
189 if (rq_data_dir(req) != READ &&
190 !(card->host->caps & MMC_CAP_MULTIWRITE))
1da177e4 191 brq.data.blocks = 1;
788ee7b0
RK
192
193 if (brq.data.blocks > 1) {
194 brq.data.flags |= MMC_DATA_MULTI;
195 brq.mrq.stop = &brq.stop;
db53f28b
RK
196 readcmd = MMC_READ_MULTIPLE_BLOCK;
197 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
788ee7b0
RK
198 } else {
199 brq.mrq.stop = NULL;
db53f28b
RK
200 readcmd = MMC_READ_SINGLE_BLOCK;
201 writecmd = MMC_WRITE_BLOCK;
202 }
203
204 if (rq_data_dir(req) == READ) {
205 brq.cmd.opcode = readcmd;
206 brq.data.flags |= MMC_DATA_READ;
207 } else {
208 brq.cmd.opcode = writecmd;
209 brq.data.flags |= MMC_DATA_WRITE;
788ee7b0 210 }
1da177e4
LT
211
212 brq.data.sg = mq->sg;
213 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
214
215 mmc_wait_for_req(card->host, &brq.mrq);
216 if (brq.cmd.error) {
217 printk(KERN_ERR "%s: error %d sending read/write command\n",
218 req->rq_disk->disk_name, brq.cmd.error);
219 goto cmd_err;
220 }
221
222 if (brq.data.error) {
223 printk(KERN_ERR "%s: error %d transferring data\n",
224 req->rq_disk->disk_name, brq.data.error);
225 goto cmd_err;
226 }
227
228 if (brq.stop.error) {
229 printk(KERN_ERR "%s: error %d sending stop command\n",
230 req->rq_disk->disk_name, brq.stop.error);
231 goto cmd_err;
232 }
233
234 do {
235 int err;
236
237 cmd.opcode = MMC_SEND_STATUS;
238 cmd.arg = card->rca << 16;
e9225176 239 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1da177e4
LT
240 err = mmc_wait_for_cmd(card->host, &cmd, 5);
241 if (err) {
242 printk(KERN_ERR "%s: error %d requesting status\n",
243 req->rq_disk->disk_name, err);
244 goto cmd_err;
245 }
246 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
247
248#if 0
249 if (cmd.resp[0] & ~0x00000900)
250 printk(KERN_ERR "%s: status = %08x\n",
251 req->rq_disk->disk_name, cmd.resp[0]);
252 if (mmc_decode_status(cmd.resp))
253 goto cmd_err;
254#endif
255
256 /*
257 * A block was successfully transferred.
258 */
259 spin_lock_irq(&md->lock);
260 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
261 if (!ret) {
262 /*
263 * The whole request completed successfully.
264 */
265 add_disk_randomness(req->rq_disk);
266 blkdev_dequeue_request(req);
8ffdc655 267 end_that_request_last(req, 1);
1da177e4
LT
268 }
269 spin_unlock_irq(&md->lock);
270 } while (ret);
271
272 mmc_card_release_host(card);
273
274 return 1;
275
276 cmd_err:
277 mmc_card_release_host(card);
278
279 /*
280 * This is a little draconian, but until we get proper
281 * error handling sorted out here, its the best we can
282 * do - especially as some hosts have no idea how much
283 * data was transferred before the error occurred.
284 */
285 spin_lock_irq(&md->lock);
286 do {
287 ret = end_that_request_chunk(req, 0,
288 req->current_nr_sectors << 9);
289 } while (ret);
290
291 add_disk_randomness(req->rq_disk);
292 blkdev_dequeue_request(req);
8ffdc655 293 end_that_request_last(req, 0);
1da177e4
LT
294 spin_unlock_irq(&md->lock);
295
296 return 0;
297}
298
299#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
300
301static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
302
a6f6c96b
RK
303static inline int mmc_blk_readonly(struct mmc_card *card)
304{
305 return mmc_card_readonly(card) ||
306 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
307}
308
1da177e4
LT
309static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
310{
311 struct mmc_blk_data *md;
312 int devidx, ret;
313
314 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
315 if (devidx >= MMC_NUM_MINORS)
316 return ERR_PTR(-ENOSPC);
317 __set_bit(devidx, dev_use);
318
319 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
a6f6c96b
RK
320 if (!md) {
321 ret = -ENOMEM;
322 goto out;
323 }
1da177e4 324
a6f6c96b 325 memset(md, 0, sizeof(struct mmc_blk_data));
1da177e4 326
a6f6c96b
RK
327 /*
328 * Set the read-only status based on the supported commands
329 * and the write protect switch.
330 */
331 md->read_only = mmc_blk_readonly(card);
1da177e4 332
a6f6c96b 333 /*
6fe9febb
PO
334 * Both SD and MMC specifications state (although a bit
335 * unclearly in the MMC case) that a block size of 512
336 * bytes must always be supported by the card.
a6f6c96b 337 */
6fe9febb 338 md->block_bits = 9;
1da177e4 339
a6f6c96b
RK
340 md->disk = alloc_disk(1 << MMC_SHIFT);
341 if (md->disk == NULL) {
342 ret = -ENOMEM;
343 goto err_kfree;
344 }
1da177e4 345
a6f6c96b
RK
346 spin_lock_init(&md->lock);
347 md->usage = 1;
1da177e4 348
a6f6c96b
RK
349 ret = mmc_init_queue(&md->queue, card, &md->lock);
350 if (ret)
351 goto err_putdisk;
1da177e4 352
a6f6c96b
RK
353 md->queue.prep_fn = mmc_blk_prep_rq;
354 md->queue.issue_fn = mmc_blk_issue_rq;
355 md->queue.data = md;
d2b18394 356
a6f6c96b
RK
357 md->disk->major = major;
358 md->disk->first_minor = devidx << MMC_SHIFT;
359 md->disk->fops = &mmc_bdops;
360 md->disk->private_data = md;
361 md->disk->queue = md->queue.queue;
362 md->disk->driverfs_dev = &card->dev;
363
364 /*
365 * As discussed on lkml, GENHD_FL_REMOVABLE should:
366 *
367 * - be set for removable media with permanent block devices
368 * - be unset for removable block devices with permanent media
369 *
370 * Since MMC block devices clearly fall under the second
371 * case, we do not set GENHD_FL_REMOVABLE. Userspace
372 * should use the block device creation/destruction hotplug
373 * messages to tell when the card is present.
374 */
375
376 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
a6f6c96b
RK
377
378 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
379
380 /*
381 * The CSD capacity field is in units of read_blkbits.
382 * set_capacity takes units of 512 bytes.
383 */
384 set_capacity(md->disk, card->csd.capacity << (card->csd.read_blkbits - 9));
1da177e4 385 return md;
a6f6c96b
RK
386
387 err_putdisk:
388 put_disk(md->disk);
389 err_kfree:
390 kfree(md);
391 out:
392 return ERR_PTR(ret);
1da177e4
LT
393}
394
395static int
396mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
397{
398 struct mmc_command cmd;
399 int err;
400
401 mmc_card_claim_host(card);
402 cmd.opcode = MMC_SET_BLOCKLEN;
d2b18394 403 cmd.arg = 1 << md->block_bits;
e9225176 404 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1da177e4
LT
405 err = mmc_wait_for_cmd(card->host, &cmd, 5);
406 mmc_card_release_host(card);
407
408 if (err) {
409 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
410 md->disk->disk_name, cmd.arg, err);
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
417static int mmc_blk_probe(struct mmc_card *card)
418{
419 struct mmc_blk_data *md;
420 int err;
421
912490db
PO
422 /*
423 * Check that the card supports the command class(es) we need.
424 */
425 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1da177e4
LT
426 return -ENODEV;
427
1da177e4
LT
428 md = mmc_blk_alloc(card);
429 if (IS_ERR(md))
430 return PTR_ERR(md);
431
432 err = mmc_blk_set_blksize(md, card);
433 if (err)
434 goto out;
435
51828abc 436 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
1da177e4 437 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
51828abc
AM
438 (unsigned long long)(get_capacity(md->disk) >> 1),
439 md->read_only ? "(ro)" : "");
1da177e4
LT
440
441 mmc_set_drvdata(card, md);
442 add_disk(md->disk);
443 return 0;
444
445 out:
446 mmc_blk_put(md);
447
448 return err;
449}
450
451static void mmc_blk_remove(struct mmc_card *card)
452{
453 struct mmc_blk_data *md = mmc_get_drvdata(card);
454
455 if (md) {
456 int devidx;
457
458 del_gendisk(md->disk);
459
460 /*
461 * I think this is needed.
462 */
463 md->disk->queue = NULL;
464
465 devidx = md->disk->first_minor >> MMC_SHIFT;
466 __clear_bit(devidx, dev_use);
467
468 mmc_blk_put(md);
469 }
470 mmc_set_drvdata(card, NULL);
471}
472
473#ifdef CONFIG_PM
474static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
475{
476 struct mmc_blk_data *md = mmc_get_drvdata(card);
477
478 if (md) {
479 mmc_queue_suspend(&md->queue);
480 }
481 return 0;
482}
483
484static int mmc_blk_resume(struct mmc_card *card)
485{
486 struct mmc_blk_data *md = mmc_get_drvdata(card);
487
488 if (md) {
489 mmc_blk_set_blksize(md, card);
490 mmc_queue_resume(&md->queue);
491 }
492 return 0;
493}
494#else
495#define mmc_blk_suspend NULL
496#define mmc_blk_resume NULL
497#endif
498
499static struct mmc_driver mmc_driver = {
500 .drv = {
501 .name = "mmcblk",
502 },
503 .probe = mmc_blk_probe,
504 .remove = mmc_blk_remove,
505 .suspend = mmc_blk_suspend,
506 .resume = mmc_blk_resume,
507};
508
509static int __init mmc_blk_init(void)
510{
511 int res = -ENOMEM;
512
513 res = register_blkdev(major, "mmc");
514 if (res < 0) {
515 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
516 major, res);
517 goto out;
518 }
519 if (major == 0)
520 major = res;
521
1da177e4
LT
522 return mmc_register_driver(&mmc_driver);
523
524 out:
525 return res;
526}
527
528static void __exit mmc_blk_exit(void)
529{
530 mmc_unregister_driver(&mmc_driver);
1da177e4
LT
531 unregister_blkdev(major, "mmc");
532}
533
534module_init(mmc_blk_init);
535module_exit(mmc_blk_exit);
536
537MODULE_LICENSE("GPL");
538MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
539
540module_param(major, int, 0444);
541MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");