mmc: Reliable write support.
[linux-2.6.git] / drivers / mmc / card / block.c
1 /*
2  * Block driver for media (i.e., flash cards)
3  *
4  * Copyright 2002 Hewlett-Packard Company
5  * Copyright 2005-2008 Pierre Ossman
6  *
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
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34
35 #include <linux/mmc/card.h>
36 #include <linux/mmc/host.h>
37 #include <linux/mmc/mmc.h>
38 #include <linux/mmc/sd.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42
43 #include "queue.h"
44
45 MODULE_ALIAS("mmc:block");
46 #ifdef MODULE_PARAM_PREFIX
47 #undef MODULE_PARAM_PREFIX
48 #endif
49 #define MODULE_PARAM_PREFIX "mmcblk."
50
51 #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) &&     \
52     (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||   \
53      ((card)->ext_csd.rel_sectors)))
54
55 static DEFINE_MUTEX(block_mutex);
56
57 /*
58  * The defaults come from config options but can be overriden by module
59  * or bootarg options.
60  */
61 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
62
63 /*
64  * We've only got one major, so number of mmcblk devices is
65  * limited to 256 / number of minors per device.
66  */
67 static int max_devices;
68
69 /* 256 minors, so at most 256 separate devices */
70 static DECLARE_BITMAP(dev_use, 256);
71
72 /*
73  * There is one mmc_blk_data per slot.
74  */
75 struct mmc_blk_data {
76         spinlock_t      lock;
77         struct gendisk  *disk;
78         struct mmc_queue queue;
79
80         unsigned int    usage;
81         unsigned int    read_only;
82 };
83
84 static DEFINE_MUTEX(open_lock);
85
86 module_param(perdev_minors, int, 0444);
87 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
88
89 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
90 {
91         struct mmc_blk_data *md;
92
93         mutex_lock(&open_lock);
94         md = disk->private_data;
95         if (md && md->usage == 0)
96                 md = NULL;
97         if (md)
98                 md->usage++;
99         mutex_unlock(&open_lock);
100
101         return md;
102 }
103
104 static void mmc_blk_put(struct mmc_blk_data *md)
105 {
106         mutex_lock(&open_lock);
107         md->usage--;
108         if (md->usage == 0) {
109                 int devmaj = MAJOR(disk_devt(md->disk));
110                 int devidx = MINOR(disk_devt(md->disk)) / perdev_minors;
111
112                 if (!devmaj)
113                         devidx = md->disk->first_minor / perdev_minors;
114
115                 blk_cleanup_queue(md->queue.queue);
116
117                 __clear_bit(devidx, dev_use);
118
119                 put_disk(md->disk);
120                 kfree(md);
121         }
122         mutex_unlock(&open_lock);
123 }
124
125 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
126 {
127         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
128         int ret = -ENXIO;
129
130         mutex_lock(&block_mutex);
131         if (md) {
132                 if (md->usage == 2)
133                         check_disk_change(bdev);
134                 ret = 0;
135
136                 if ((mode & FMODE_WRITE) && md->read_only) {
137                         mmc_blk_put(md);
138                         ret = -EROFS;
139                 }
140         }
141         mutex_unlock(&block_mutex);
142
143         return ret;
144 }
145
146 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
147 {
148         struct mmc_blk_data *md = disk->private_data;
149
150         mutex_lock(&block_mutex);
151         mmc_blk_put(md);
152         mutex_unlock(&block_mutex);
153         return 0;
154 }
155
156 static int
157 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
158 {
159         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
160         geo->heads = 4;
161         geo->sectors = 16;
162         return 0;
163 }
164
165 static const struct block_device_operations mmc_bdops = {
166         .open                   = mmc_blk_open,
167         .release                = mmc_blk_release,
168         .getgeo                 = mmc_blk_getgeo,
169         .owner                  = THIS_MODULE,
170 };
171
172 struct mmc_blk_request {
173         struct mmc_request      mrq;
174         struct mmc_command      cmd;
175         struct mmc_command      stop;
176         struct mmc_data         data;
177 };
178
179 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
180 {
181         int err;
182         u32 result;
183         __be32 *blocks;
184
185         struct mmc_request mrq;
186         struct mmc_command cmd;
187         struct mmc_data data;
188         unsigned int timeout_us;
189
190         struct scatterlist sg;
191
192         memset(&cmd, 0, sizeof(struct mmc_command));
193
194         cmd.opcode = MMC_APP_CMD;
195         cmd.arg = card->rca << 16;
196         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
197
198         err = mmc_wait_for_cmd(card->host, &cmd, 0);
199         if (err)
200                 return (u32)-1;
201         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
202                 return (u32)-1;
203
204         memset(&cmd, 0, sizeof(struct mmc_command));
205
206         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
207         cmd.arg = 0;
208         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
209
210         memset(&data, 0, sizeof(struct mmc_data));
211
212         data.timeout_ns = card->csd.tacc_ns * 100;
213         data.timeout_clks = card->csd.tacc_clks * 100;
214
215         timeout_us = data.timeout_ns / 1000;
216         timeout_us += data.timeout_clks * 1000 /
217                 (card->host->ios.clock / 1000);
218
219         if (timeout_us > 100000) {
220                 data.timeout_ns = 100000000;
221                 data.timeout_clks = 0;
222         }
223
224         data.blksz = 4;
225         data.blocks = 1;
226         data.flags = MMC_DATA_READ;
227         data.sg = &sg;
228         data.sg_len = 1;
229
230         memset(&mrq, 0, sizeof(struct mmc_request));
231
232         mrq.cmd = &cmd;
233         mrq.data = &data;
234
235         blocks = kmalloc(4, GFP_KERNEL);
236         if (!blocks)
237                 return (u32)-1;
238
239         sg_init_one(&sg, blocks, 4);
240
241         mmc_wait_for_req(card->host, &mrq);
242
243         result = ntohl(*blocks);
244         kfree(blocks);
245
246         if (cmd.error || data.error)
247                 result = (u32)-1;
248
249         return result;
250 }
251
252 static u32 get_card_status(struct mmc_card *card, struct request *req)
253 {
254         struct mmc_command cmd;
255         int err;
256
257         memset(&cmd, 0, sizeof(struct mmc_command));
258         cmd.opcode = MMC_SEND_STATUS;
259         if (!mmc_host_is_spi(card->host))
260                 cmd.arg = card->rca << 16;
261         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
262         err = mmc_wait_for_cmd(card->host, &cmd, 0);
263         if (err)
264                 printk(KERN_ERR "%s: error %d sending status command",
265                        req->rq_disk->disk_name, err);
266         return cmd.resp[0];
267 }
268
269 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
270 {
271         struct mmc_blk_data *md = mq->data;
272         struct mmc_card *card = md->queue.card;
273         unsigned int from, nr, arg;
274         int err = 0;
275
276         mmc_claim_host(card->host);
277
278         if (!mmc_can_erase(card)) {
279                 err = -EOPNOTSUPP;
280                 goto out;
281         }
282
283         from = blk_rq_pos(req);
284         nr = blk_rq_sectors(req);
285
286         if (mmc_can_trim(card))
287                 arg = MMC_TRIM_ARG;
288         else
289                 arg = MMC_ERASE_ARG;
290
291         err = mmc_erase(card, from, nr, arg);
292 out:
293         spin_lock_irq(&md->lock);
294         __blk_end_request(req, err, blk_rq_bytes(req));
295         spin_unlock_irq(&md->lock);
296
297         mmc_release_host(card->host);
298
299         return err ? 0 : 1;
300 }
301
302 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
303                                        struct request *req)
304 {
305         struct mmc_blk_data *md = mq->data;
306         struct mmc_card *card = md->queue.card;
307         unsigned int from, nr, arg;
308         int err = 0;
309
310         mmc_claim_host(card->host);
311
312         if (!mmc_can_secure_erase_trim(card)) {
313                 err = -EOPNOTSUPP;
314                 goto out;
315         }
316
317         from = blk_rq_pos(req);
318         nr = blk_rq_sectors(req);
319
320         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
321                 arg = MMC_SECURE_TRIM1_ARG;
322         else
323                 arg = MMC_SECURE_ERASE_ARG;
324
325         err = mmc_erase(card, from, nr, arg);
326         if (!err && arg == MMC_SECURE_TRIM1_ARG)
327                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
328 out:
329         spin_lock_irq(&md->lock);
330         __blk_end_request(req, err, blk_rq_bytes(req));
331         spin_unlock_irq(&md->lock);
332
333         mmc_release_host(card->host);
334
335         return err ? 0 : 1;
336 }
337
338 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
339 {
340         struct mmc_blk_data *md = mq->data;
341
342         /*
343          * No-op, only service this because we need REQ_FUA for reliable
344          * writes.
345          */
346         spin_lock_irq(&md->lock);
347         __blk_end_request_all(req, 0);
348         spin_unlock_irq(&md->lock);
349
350         return 1;
351 }
352
353 /*
354  * Reformat current write as a reliable write, supporting
355  * both legacy and the enhanced reliable write MMC cards.
356  * In each transfer we'll handle only as much as a single
357  * reliable write can handle, thus finish the request in
358  * partial completions.
359  */
360 static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
361                                    struct mmc_card *card,
362                                    struct request *req)
363 {
364         int err;
365         struct mmc_command set_count;
366
367         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
368                 /* Legacy mode imposes restrictions on transfers. */
369                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
370                         brq->data.blocks = 1;
371
372                 if (brq->data.blocks > card->ext_csd.rel_sectors)
373                         brq->data.blocks = card->ext_csd.rel_sectors;
374                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
375                         brq->data.blocks = 1;
376         }
377
378         memset(&set_count, 0, sizeof(struct mmc_command));
379         set_count.opcode = MMC_SET_BLOCK_COUNT;
380         set_count.arg = brq->data.blocks | (1 << 31);
381         set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
382         err = mmc_wait_for_cmd(card->host, &set_count, 0);
383         if (err)
384                 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
385                        req->rq_disk->disk_name, err);
386         return err;
387 }
388
389 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
390 {
391         struct mmc_blk_data *md = mq->data;
392         struct mmc_card *card = md->queue.card;
393         struct mmc_blk_request brq;
394         int ret = 1, disable_multi = 0;
395
396         /*
397          * Reliable writes are used to implement Forced Unit Access and
398          * REQ_META accesses, and are supported only on MMCs.
399          */
400         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
401                           (req->cmd_flags & REQ_META)) &&
402                 (rq_data_dir(req) == WRITE) &&
403                 REL_WRITES_SUPPORTED(card);
404
405         mmc_claim_host(card->host);
406
407         do {
408                 struct mmc_command cmd;
409                 u32 readcmd, writecmd, status = 0;
410
411                 memset(&brq, 0, sizeof(struct mmc_blk_request));
412                 brq.mrq.cmd = &brq.cmd;
413                 brq.mrq.data = &brq.data;
414
415                 brq.cmd.arg = blk_rq_pos(req);
416                 if (!mmc_card_blockaddr(card))
417                         brq.cmd.arg <<= 9;
418                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
419                 brq.data.blksz = 512;
420                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
421                 brq.stop.arg = 0;
422                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
423                 brq.data.blocks = blk_rq_sectors(req);
424
425                 /*
426                  * The block layer doesn't support all sector count
427                  * restrictions, so we need to be prepared for too big
428                  * requests.
429                  */
430                 if (brq.data.blocks > card->host->max_blk_count)
431                         brq.data.blocks = card->host->max_blk_count;
432
433                 /*
434                  * After a read error, we redo the request one sector at a time
435                  * in order to accurately determine which sectors can be read
436                  * successfully.
437                  */
438                 if (disable_multi && brq.data.blocks > 1)
439                         brq.data.blocks = 1;
440
441                 if (brq.data.blocks > 1 || do_rel_wr) {
442                         /* SPI multiblock writes terminate using a special
443                          * token, not a STOP_TRANSMISSION request. Reliable
444                          * writes use SET_BLOCK_COUNT and do not use a
445                          * STOP_TRANSMISSION request either.
446                          */
447                         if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
448                             rq_data_dir(req) == READ)
449                                 brq.mrq.stop = &brq.stop;
450                         readcmd = MMC_READ_MULTIPLE_BLOCK;
451                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
452                 } else {
453                         brq.mrq.stop = NULL;
454                         readcmd = MMC_READ_SINGLE_BLOCK;
455                         writecmd = MMC_WRITE_BLOCK;
456                 }
457                 if (rq_data_dir(req) == READ) {
458                         brq.cmd.opcode = readcmd;
459                         brq.data.flags |= MMC_DATA_READ;
460                 } else {
461                         brq.cmd.opcode = writecmd;
462                         brq.data.flags |= MMC_DATA_WRITE;
463                 }
464
465                 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
466                         goto cmd_err;
467
468                 mmc_set_data_timeout(&brq.data, card);
469
470                 brq.data.sg = mq->sg;
471                 brq.data.sg_len = mmc_queue_map_sg(mq);
472
473                 /*
474                  * Adjust the sg list so it is the same size as the
475                  * request.
476                  */
477                 if (brq.data.blocks != blk_rq_sectors(req)) {
478                         int i, data_size = brq.data.blocks << 9;
479                         struct scatterlist *sg;
480
481                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
482                                 data_size -= sg->length;
483                                 if (data_size <= 0) {
484                                         sg->length += data_size;
485                                         i++;
486                                         break;
487                                 }
488                         }
489                         brq.data.sg_len = i;
490                 }
491
492                 mmc_queue_bounce_pre(mq);
493
494                 mmc_wait_for_req(card->host, &brq.mrq);
495
496                 mmc_queue_bounce_post(mq);
497
498                 /*
499                  * Check for errors here, but don't jump to cmd_err
500                  * until later as we need to wait for the card to leave
501                  * programming mode even when things go wrong.
502                  */
503                 if (brq.cmd.error || brq.data.error || brq.stop.error) {
504                         if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
505                                 /* Redo read one sector at a time */
506                                 printk(KERN_WARNING "%s: retrying using single "
507                                        "block read\n", req->rq_disk->disk_name);
508                                 disable_multi = 1;
509                                 continue;
510                         }
511                         status = get_card_status(card, req);
512                 }
513
514                 if (brq.cmd.error) {
515                         printk(KERN_ERR "%s: error %d sending read/write "
516                                "command, response %#x, card status %#x\n",
517                                req->rq_disk->disk_name, brq.cmd.error,
518                                brq.cmd.resp[0], status);
519                 }
520
521                 if (brq.data.error) {
522                         if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
523                                 /* 'Stop' response contains card status */
524                                 status = brq.mrq.stop->resp[0];
525                         printk(KERN_ERR "%s: error %d transferring data,"
526                                " sector %u, nr %u, card status %#x\n",
527                                req->rq_disk->disk_name, brq.data.error,
528                                (unsigned)blk_rq_pos(req),
529                                (unsigned)blk_rq_sectors(req), status);
530                 }
531
532                 if (brq.stop.error) {
533                         printk(KERN_ERR "%s: error %d sending stop command, "
534                                "response %#x, card status %#x\n",
535                                req->rq_disk->disk_name, brq.stop.error,
536                                brq.stop.resp[0], status);
537                 }
538
539                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
540                         do {
541                                 int err;
542
543                                 cmd.opcode = MMC_SEND_STATUS;
544                                 cmd.arg = card->rca << 16;
545                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
546                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
547                                 if (err) {
548                                         printk(KERN_ERR "%s: error %d requesting status\n",
549                                                req->rq_disk->disk_name, err);
550                                         goto cmd_err;
551                                 }
552                                 /*
553                                  * Some cards mishandle the status bits,
554                                  * so make sure to check both the busy
555                                  * indication and the card state.
556                                  */
557                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
558                                 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
559
560 #if 0
561                         if (cmd.resp[0] & ~0x00000900)
562                                 printk(KERN_ERR "%s: status = %08x\n",
563                                        req->rq_disk->disk_name, cmd.resp[0]);
564                         if (mmc_decode_status(cmd.resp))
565                                 goto cmd_err;
566 #endif
567                 }
568
569                 if (brq.cmd.error || brq.stop.error || brq.data.error) {
570                         if (rq_data_dir(req) == READ) {
571                                 /*
572                                  * After an error, we redo I/O one sector at a
573                                  * time, so we only reach here after trying to
574                                  * read a single sector.
575                                  */
576                                 spin_lock_irq(&md->lock);
577                                 ret = __blk_end_request(req, -EIO, brq.data.blksz);
578                                 spin_unlock_irq(&md->lock);
579                                 continue;
580                         }
581                         goto cmd_err;
582                 }
583
584                 /*
585                  * A block was successfully transferred.
586                  */
587                 spin_lock_irq(&md->lock);
588                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
589                 spin_unlock_irq(&md->lock);
590         } while (ret);
591
592         mmc_release_host(card->host);
593
594         return 1;
595
596  cmd_err:
597         /*
598          * If this is an SD card and we're writing, we can first
599          * mark the known good sectors as ok.
600          *
601          * If the card is not SD, we can still ok written sectors
602          * as reported by the controller (which might be less than
603          * the real number of written sectors, but never more).
604          */
605         if (mmc_card_sd(card)) {
606                 u32 blocks;
607
608                 blocks = mmc_sd_num_wr_blocks(card);
609                 if (blocks != (u32)-1) {
610                         spin_lock_irq(&md->lock);
611                         ret = __blk_end_request(req, 0, blocks << 9);
612                         spin_unlock_irq(&md->lock);
613                 }
614         } else {
615                 spin_lock_irq(&md->lock);
616                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
617                 spin_unlock_irq(&md->lock);
618         }
619
620         mmc_release_host(card->host);
621
622         spin_lock_irq(&md->lock);
623         while (ret)
624                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
625         spin_unlock_irq(&md->lock);
626
627         return 0;
628 }
629
630 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
631 {
632         if (req->cmd_flags & REQ_DISCARD) {
633                 if (req->cmd_flags & REQ_SECURE)
634                         return mmc_blk_issue_secdiscard_rq(mq, req);
635                 else
636                         return mmc_blk_issue_discard_rq(mq, req);
637         } else if (req->cmd_flags & REQ_FLUSH) {
638                 return mmc_blk_issue_flush(mq, req);
639         } else {
640                 return mmc_blk_issue_rw_rq(mq, req);
641         }
642 }
643
644 static inline int mmc_blk_readonly(struct mmc_card *card)
645 {
646         return mmc_card_readonly(card) ||
647                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
648 }
649
650 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
651 {
652         struct mmc_blk_data *md;
653         int devidx, ret;
654
655         devidx = find_first_zero_bit(dev_use, max_devices);
656         if (devidx >= max_devices)
657                 return ERR_PTR(-ENOSPC);
658         __set_bit(devidx, dev_use);
659
660         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
661         if (!md) {
662                 ret = -ENOMEM;
663                 goto out;
664         }
665
666
667         /*
668          * Set the read-only status based on the supported commands
669          * and the write protect switch.
670          */
671         md->read_only = mmc_blk_readonly(card);
672
673         md->disk = alloc_disk(perdev_minors);
674         if (md->disk == NULL) {
675                 ret = -ENOMEM;
676                 goto err_kfree;
677         }
678
679         spin_lock_init(&md->lock);
680         md->usage = 1;
681
682         ret = mmc_init_queue(&md->queue, card, &md->lock);
683         if (ret)
684                 goto err_putdisk;
685
686         md->queue.issue_fn = mmc_blk_issue_rq;
687         md->queue.data = md;
688
689         md->disk->major = MMC_BLOCK_MAJOR;
690         md->disk->first_minor = devidx * perdev_minors;
691         md->disk->fops = &mmc_bdops;
692         md->disk->private_data = md;
693         md->disk->queue = md->queue.queue;
694         md->disk->driverfs_dev = &card->dev;
695         set_disk_ro(md->disk, md->read_only);
696         if (REL_WRITES_SUPPORTED(card))
697                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
698
699         /*
700          * As discussed on lkml, GENHD_FL_REMOVABLE should:
701          *
702          * - be set for removable media with permanent block devices
703          * - be unset for removable block devices with permanent media
704          *
705          * Since MMC block devices clearly fall under the second
706          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
707          * should use the block device creation/destruction hotplug
708          * messages to tell when the card is present.
709          */
710
711         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
712                 "mmcblk%d", devidx);
713
714         blk_queue_logical_block_size(md->queue.queue, 512);
715
716         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
717                 /*
718                  * The EXT_CSD sector count is in number or 512 byte
719                  * sectors.
720                  */
721                 set_capacity(md->disk, card->ext_csd.sectors);
722         } else {
723                 /*
724                  * The CSD capacity field is in units of read_blkbits.
725                  * set_capacity takes units of 512 bytes.
726                  */
727                 set_capacity(md->disk,
728                         card->csd.capacity << (card->csd.read_blkbits - 9));
729         }
730         return md;
731
732  err_putdisk:
733         put_disk(md->disk);
734  err_kfree:
735         kfree(md);
736  out:
737         return ERR_PTR(ret);
738 }
739
740 static int
741 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
742 {
743         int err;
744
745         mmc_claim_host(card->host);
746         err = mmc_set_blocklen(card, 512);
747         mmc_release_host(card->host);
748
749         if (err) {
750                 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
751                         md->disk->disk_name, err);
752                 return -EINVAL;
753         }
754
755         return 0;
756 }
757
758 static int mmc_blk_probe(struct mmc_card *card)
759 {
760         struct mmc_blk_data *md;
761         int err;
762         char cap_str[10];
763
764         /*
765          * Check that the card supports the command class(es) we need.
766          */
767         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
768                 return -ENODEV;
769
770         md = mmc_blk_alloc(card);
771         if (IS_ERR(md))
772                 return PTR_ERR(md);
773
774         err = mmc_blk_set_blksize(md, card);
775         if (err)
776                 goto out;
777
778         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
779                         cap_str, sizeof(cap_str));
780         printk(KERN_INFO "%s: %s %s %s %s\n",
781                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
782                 cap_str, md->read_only ? "(ro)" : "");
783
784         mmc_set_drvdata(card, md);
785         add_disk(md->disk);
786         return 0;
787
788  out:
789         mmc_cleanup_queue(&md->queue);
790         mmc_blk_put(md);
791
792         return err;
793 }
794
795 static void mmc_blk_remove(struct mmc_card *card)
796 {
797         struct mmc_blk_data *md = mmc_get_drvdata(card);
798
799         if (md) {
800                 /* Stop new requests from getting into the queue */
801                 del_gendisk(md->disk);
802
803                 /* Then flush out any already in there */
804                 mmc_cleanup_queue(&md->queue);
805
806                 mmc_blk_put(md);
807         }
808         mmc_set_drvdata(card, NULL);
809 }
810
811 #ifdef CONFIG_PM
812 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
813 {
814         struct mmc_blk_data *md = mmc_get_drvdata(card);
815
816         if (md) {
817                 mmc_queue_suspend(&md->queue);
818         }
819         return 0;
820 }
821
822 static int mmc_blk_resume(struct mmc_card *card)
823 {
824         struct mmc_blk_data *md = mmc_get_drvdata(card);
825
826         if (md) {
827                 mmc_blk_set_blksize(md, card);
828                 mmc_queue_resume(&md->queue);
829         }
830         return 0;
831 }
832 #else
833 #define mmc_blk_suspend NULL
834 #define mmc_blk_resume  NULL
835 #endif
836
837 static struct mmc_driver mmc_driver = {
838         .drv            = {
839                 .name   = "mmcblk",
840         },
841         .probe          = mmc_blk_probe,
842         .remove         = mmc_blk_remove,
843         .suspend        = mmc_blk_suspend,
844         .resume         = mmc_blk_resume,
845 };
846
847 static int __init mmc_blk_init(void)
848 {
849         int res;
850
851         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
852                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
853
854         max_devices = 256 / perdev_minors;
855
856         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
857         if (res)
858                 goto out;
859
860         res = mmc_register_driver(&mmc_driver);
861         if (res)
862                 goto out2;
863
864         return 0;
865  out2:
866         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
867  out:
868         return res;
869 }
870
871 static void __exit mmc_blk_exit(void)
872 {
873         mmc_unregister_driver(&mmc_driver);
874         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
875 }
876
877 module_init(mmc_blk_init);
878 module_exit(mmc_blk_exit);
879
880 MODULE_LICENSE("GPL");
881 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
882