]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/mmc/card/block.c
jfs: fix error path in ialloc
[mirror_ubuntu-eoan-kernel.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 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37 #include <linux/pm_runtime.h>
38
39 #include <linux/mmc/ioctl.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/mmc.h>
43 #include <linux/mmc/sd.h>
44
45 #include <asm/uaccess.h>
46
47 #include "queue.h"
48
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
52 #endif
53 #define MODULE_PARAM_PREFIX "mmcblk."
54
55 #define INAND_CMD38_ARG_EXT_CSD 113
56 #define INAND_CMD38_ARG_ERASE 0x00
57 #define INAND_CMD38_ARG_TRIM 0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
61 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 #define MMC_SANITIZE_REQ_TIMEOUT 240000
63 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
64
65 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68 #define PACKED_CMD_VER 0x01
69 #define PACKED_CMD_WR 0x02
70
71 static DEFINE_MUTEX(block_mutex);
72
73 /*
74 * The defaults come from config options but can be overriden by module
75 * or bootarg options.
76 */
77 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
78
79 /*
80 * We've only got one major, so number of mmcblk devices is
81 * limited to 256 / number of minors per device.
82 */
83 static int max_devices;
84
85 /* 256 minors, so at most 256 separate devices */
86 static DECLARE_BITMAP(dev_use, 256);
87 static DECLARE_BITMAP(name_use, 256);
88
89 /*
90 * There is one mmc_blk_data per slot.
91 */
92 struct mmc_blk_data {
93 spinlock_t lock;
94 struct gendisk *disk;
95 struct mmc_queue queue;
96 struct list_head part;
97
98 unsigned int flags;
99 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
100 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
101 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
102
103 unsigned int usage;
104 unsigned int read_only;
105 unsigned int part_type;
106 unsigned int name_idx;
107 unsigned int reset_done;
108 #define MMC_BLK_READ BIT(0)
109 #define MMC_BLK_WRITE BIT(1)
110 #define MMC_BLK_DISCARD BIT(2)
111 #define MMC_BLK_SECDISCARD BIT(3)
112
113 /*
114 * Only set in main mmc_blk_data associated
115 * with mmc_card with mmc_set_drvdata, and keeps
116 * track of the current selected device partition.
117 */
118 unsigned int part_curr;
119 struct device_attribute force_ro;
120 struct device_attribute power_ro_lock;
121 int area_type;
122 };
123
124 static DEFINE_MUTEX(open_lock);
125
126 enum {
127 MMC_PACKED_NR_IDX = -1,
128 MMC_PACKED_NR_ZERO,
129 MMC_PACKED_NR_SINGLE,
130 };
131
132 module_param(perdev_minors, int, 0444);
133 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
134
135 static inline int mmc_blk_part_switch(struct mmc_card *card,
136 struct mmc_blk_data *md);
137 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
138
139 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
140 {
141 struct mmc_packed *packed = mqrq->packed;
142
143 BUG_ON(!packed);
144
145 mqrq->cmd_type = MMC_PACKED_NONE;
146 packed->nr_entries = MMC_PACKED_NR_ZERO;
147 packed->idx_failure = MMC_PACKED_NR_IDX;
148 packed->retries = 0;
149 packed->blocks = 0;
150 }
151
152 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
153 {
154 struct mmc_blk_data *md;
155
156 mutex_lock(&open_lock);
157 md = disk->private_data;
158 if (md && md->usage == 0)
159 md = NULL;
160 if (md)
161 md->usage++;
162 mutex_unlock(&open_lock);
163
164 return md;
165 }
166
167 static inline int mmc_get_devidx(struct gendisk *disk)
168 {
169 int devmaj = MAJOR(disk_devt(disk));
170 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
171
172 if (!devmaj)
173 devidx = disk->first_minor / perdev_minors;
174 return devidx;
175 }
176
177 static void mmc_blk_put(struct mmc_blk_data *md)
178 {
179 mutex_lock(&open_lock);
180 md->usage--;
181 if (md->usage == 0) {
182 int devidx = mmc_get_devidx(md->disk);
183 blk_cleanup_queue(md->queue.queue);
184
185 __clear_bit(devidx, dev_use);
186
187 put_disk(md->disk);
188 kfree(md);
189 }
190 mutex_unlock(&open_lock);
191 }
192
193 static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195 {
196 int ret;
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
199 int locked = 0;
200
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 locked = 2;
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
204 locked = 1;
205
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
207
208 return ret;
209 }
210
211 static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213 {
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
218
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
221
222 if (set != 1)
223 return count;
224
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
227
228 mmc_get_card(card);
229
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
238
239 mmc_put_card(card);
240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255 }
256
257 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259 {
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
263 ret = snprintf(buf, PAGE_SIZE, "%d",
264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268 }
269
270 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272 {
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284 out:
285 mmc_blk_put(md);
286 return ret;
287 }
288
289 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
290 {
291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
292 int ret = -ENXIO;
293
294 mutex_lock(&block_mutex);
295 if (md) {
296 if (md->usage == 2)
297 check_disk_change(bdev);
298 ret = 0;
299
300 if ((mode & FMODE_WRITE) && md->read_only) {
301 mmc_blk_put(md);
302 ret = -EROFS;
303 }
304 }
305 mutex_unlock(&block_mutex);
306
307 return ret;
308 }
309
310 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
311 {
312 struct mmc_blk_data *md = disk->private_data;
313
314 mutex_lock(&block_mutex);
315 mmc_blk_put(md);
316 mutex_unlock(&block_mutex);
317 }
318
319 static int
320 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
321 {
322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
326 }
327
328 struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
332 };
333
334 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
336 {
337 struct mmc_blk_ioc_data *idata;
338 int err;
339
340 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
341 if (!idata) {
342 err = -ENOMEM;
343 goto out;
344 }
345
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
348 goto idata_err;
349 }
350
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
354 goto idata_err;
355 }
356
357 if (!idata->buf_bytes)
358 return idata;
359
360 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
361 if (!idata->buf) {
362 err = -ENOMEM;
363 goto idata_err;
364 }
365
366 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
367 idata->ic.data_ptr, idata->buf_bytes)) {
368 err = -EFAULT;
369 goto copy_err;
370 }
371
372 return idata;
373
374 copy_err:
375 kfree(idata->buf);
376 idata_err:
377 kfree(idata);
378 out:
379 return ERR_PTR(err);
380 }
381
382 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
383 u32 retries_max)
384 {
385 int err;
386 u32 retry_count = 0;
387
388 if (!status || !retries_max)
389 return -EINVAL;
390
391 do {
392 err = get_card_status(card, status, 5);
393 if (err)
394 break;
395
396 if (!R1_STATUS(*status) &&
397 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
398 break; /* RPMB programming operation complete */
399
400 /*
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
403 * frequently.
404 */
405 usleep_range(1000, 5000);
406 } while (++retry_count < retries_max);
407
408 if (retry_count == retries_max)
409 err = -EPERM;
410
411 return err;
412 }
413
414 static int ioctl_do_sanitize(struct mmc_card *card)
415 {
416 int err;
417
418 if (!(mmc_can_sanitize(card) &&
419 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
420 pr_warn("%s: %s - SANITIZE is not supported\n",
421 mmc_hostname(card->host), __func__);
422 err = -EOPNOTSUPP;
423 goto out;
424 }
425
426 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
427 mmc_hostname(card->host), __func__);
428
429 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
430 EXT_CSD_SANITIZE_START, 1,
431 MMC_SANITIZE_REQ_TIMEOUT);
432
433 if (err)
434 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
435 mmc_hostname(card->host), __func__, err);
436
437 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
438 __func__);
439 out:
440 return err;
441 }
442
443 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
444 struct mmc_ioc_cmd __user *ic_ptr)
445 {
446 struct mmc_blk_ioc_data *idata;
447 struct mmc_blk_data *md;
448 struct mmc_card *card;
449 struct mmc_command cmd = {0};
450 struct mmc_data data = {0};
451 struct mmc_request mrq = {NULL};
452 struct scatterlist sg;
453 int err;
454 int is_rpmb = false;
455 u32 status = 0;
456
457 /*
458 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
459 * whole block device, not on a partition. This prevents overspray
460 * between sibling partitions.
461 */
462 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
463 return -EPERM;
464
465 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
466 if (IS_ERR(idata))
467 return PTR_ERR(idata);
468
469 md = mmc_blk_get(bdev->bd_disk);
470 if (!md) {
471 err = -EINVAL;
472 goto cmd_err;
473 }
474
475 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
476 is_rpmb = true;
477
478 card = md->queue.card;
479 if (IS_ERR(card)) {
480 err = PTR_ERR(card);
481 goto cmd_done;
482 }
483
484 cmd.opcode = idata->ic.opcode;
485 cmd.arg = idata->ic.arg;
486 cmd.flags = idata->ic.flags;
487
488 if (idata->buf_bytes) {
489 data.sg = &sg;
490 data.sg_len = 1;
491 data.blksz = idata->ic.blksz;
492 data.blocks = idata->ic.blocks;
493
494 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
495
496 if (idata->ic.write_flag)
497 data.flags = MMC_DATA_WRITE;
498 else
499 data.flags = MMC_DATA_READ;
500
501 /* data.flags must already be set before doing this. */
502 mmc_set_data_timeout(&data, card);
503
504 /* Allow overriding the timeout_ns for empirical tuning. */
505 if (idata->ic.data_timeout_ns)
506 data.timeout_ns = idata->ic.data_timeout_ns;
507
508 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
509 /*
510 * Pretend this is a data transfer and rely on the
511 * host driver to compute timeout. When all host
512 * drivers support cmd.cmd_timeout for R1B, this
513 * can be changed to:
514 *
515 * mrq.data = NULL;
516 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
517 */
518 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
519 }
520
521 mrq.data = &data;
522 }
523
524 mrq.cmd = &cmd;
525
526 mmc_get_card(card);
527
528 err = mmc_blk_part_switch(card, md);
529 if (err)
530 goto cmd_rel_host;
531
532 if (idata->ic.is_acmd) {
533 err = mmc_app_cmd(card->host, card);
534 if (err)
535 goto cmd_rel_host;
536 }
537
538 if (is_rpmb) {
539 err = mmc_set_blockcount(card, data.blocks,
540 idata->ic.write_flag & (1 << 31));
541 if (err)
542 goto cmd_rel_host;
543 }
544
545 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
546 (cmd.opcode == MMC_SWITCH)) {
547 err = ioctl_do_sanitize(card);
548
549 if (err)
550 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
551 __func__, err);
552
553 goto cmd_rel_host;
554 }
555
556 mmc_wait_for_req(card->host, &mrq);
557
558 if (cmd.error) {
559 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
560 __func__, cmd.error);
561 err = cmd.error;
562 goto cmd_rel_host;
563 }
564 if (data.error) {
565 dev_err(mmc_dev(card->host), "%s: data error %d\n",
566 __func__, data.error);
567 err = data.error;
568 goto cmd_rel_host;
569 }
570
571 /*
572 * According to the SD specs, some commands require a delay after
573 * issuing the command.
574 */
575 if (idata->ic.postsleep_min_us)
576 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
577
578 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
579 err = -EFAULT;
580 goto cmd_rel_host;
581 }
582
583 if (!idata->ic.write_flag) {
584 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
585 idata->buf, idata->buf_bytes)) {
586 err = -EFAULT;
587 goto cmd_rel_host;
588 }
589 }
590
591 if (is_rpmb) {
592 /*
593 * Ensure RPMB command has completed by polling CMD13
594 * "Send Status".
595 */
596 err = ioctl_rpmb_card_status_poll(card, &status, 5);
597 if (err)
598 dev_err(mmc_dev(card->host),
599 "%s: Card Status=0x%08X, error %d\n",
600 __func__, status, err);
601 }
602
603 cmd_rel_host:
604 mmc_put_card(card);
605
606 cmd_done:
607 mmc_blk_put(md);
608 cmd_err:
609 kfree(idata->buf);
610 kfree(idata);
611 return err;
612 }
613
614 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
615 unsigned int cmd, unsigned long arg)
616 {
617 int ret = -EINVAL;
618 if (cmd == MMC_IOC_CMD)
619 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
620 return ret;
621 }
622
623 #ifdef CONFIG_COMPAT
624 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
625 unsigned int cmd, unsigned long arg)
626 {
627 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
628 }
629 #endif
630
631 static const struct block_device_operations mmc_bdops = {
632 .open = mmc_blk_open,
633 .release = mmc_blk_release,
634 .getgeo = mmc_blk_getgeo,
635 .owner = THIS_MODULE,
636 .ioctl = mmc_blk_ioctl,
637 #ifdef CONFIG_COMPAT
638 .compat_ioctl = mmc_blk_compat_ioctl,
639 #endif
640 };
641
642 static inline int mmc_blk_part_switch(struct mmc_card *card,
643 struct mmc_blk_data *md)
644 {
645 int ret;
646 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
647
648 if (main_md->part_curr == md->part_type)
649 return 0;
650
651 if (mmc_card_mmc(card)) {
652 u8 part_config = card->ext_csd.part_config;
653
654 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
655 part_config |= md->part_type;
656
657 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
658 EXT_CSD_PART_CONFIG, part_config,
659 card->ext_csd.part_time);
660 if (ret)
661 return ret;
662
663 card->ext_csd.part_config = part_config;
664 }
665
666 main_md->part_curr = md->part_type;
667 return 0;
668 }
669
670 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
671 {
672 int err;
673 u32 result;
674 __be32 *blocks;
675
676 struct mmc_request mrq = {NULL};
677 struct mmc_command cmd = {0};
678 struct mmc_data data = {0};
679
680 struct scatterlist sg;
681
682 cmd.opcode = MMC_APP_CMD;
683 cmd.arg = card->rca << 16;
684 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
685
686 err = mmc_wait_for_cmd(card->host, &cmd, 0);
687 if (err)
688 return (u32)-1;
689 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
690 return (u32)-1;
691
692 memset(&cmd, 0, sizeof(struct mmc_command));
693
694 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
695 cmd.arg = 0;
696 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
697
698 data.blksz = 4;
699 data.blocks = 1;
700 data.flags = MMC_DATA_READ;
701 data.sg = &sg;
702 data.sg_len = 1;
703 mmc_set_data_timeout(&data, card);
704
705 mrq.cmd = &cmd;
706 mrq.data = &data;
707
708 blocks = kmalloc(4, GFP_KERNEL);
709 if (!blocks)
710 return (u32)-1;
711
712 sg_init_one(&sg, blocks, 4);
713
714 mmc_wait_for_req(card->host, &mrq);
715
716 result = ntohl(*blocks);
717 kfree(blocks);
718
719 if (cmd.error || data.error)
720 result = (u32)-1;
721
722 return result;
723 }
724
725 static int send_stop(struct mmc_card *card, u32 *status)
726 {
727 struct mmc_command cmd = {0};
728 int err;
729
730 cmd.opcode = MMC_STOP_TRANSMISSION;
731 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
732 err = mmc_wait_for_cmd(card->host, &cmd, 5);
733 if (err == 0)
734 *status = cmd.resp[0];
735 return err;
736 }
737
738 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
739 {
740 struct mmc_command cmd = {0};
741 int err;
742
743 cmd.opcode = MMC_SEND_STATUS;
744 if (!mmc_host_is_spi(card->host))
745 cmd.arg = card->rca << 16;
746 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
747 err = mmc_wait_for_cmd(card->host, &cmd, retries);
748 if (err == 0)
749 *status = cmd.resp[0];
750 return err;
751 }
752
753 #define ERR_NOMEDIUM 3
754 #define ERR_RETRY 2
755 #define ERR_ABORT 1
756 #define ERR_CONTINUE 0
757
758 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
759 bool status_valid, u32 status)
760 {
761 switch (error) {
762 case -EILSEQ:
763 /* response crc error, retry the r/w cmd */
764 pr_err("%s: %s sending %s command, card status %#x\n",
765 req->rq_disk->disk_name, "response CRC error",
766 name, status);
767 return ERR_RETRY;
768
769 case -ETIMEDOUT:
770 pr_err("%s: %s sending %s command, card status %#x\n",
771 req->rq_disk->disk_name, "timed out", name, status);
772
773 /* If the status cmd initially failed, retry the r/w cmd */
774 if (!status_valid)
775 return ERR_RETRY;
776
777 /*
778 * If it was a r/w cmd crc error, or illegal command
779 * (eg, issued in wrong state) then retry - we should
780 * have corrected the state problem above.
781 */
782 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
783 return ERR_RETRY;
784
785 /* Otherwise abort the command */
786 return ERR_ABORT;
787
788 default:
789 /* We don't understand the error code the driver gave us */
790 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
791 req->rq_disk->disk_name, error, status);
792 return ERR_ABORT;
793 }
794 }
795
796 /*
797 * Initial r/w and stop cmd error recovery.
798 * We don't know whether the card received the r/w cmd or not, so try to
799 * restore things back to a sane state. Essentially, we do this as follows:
800 * - Obtain card status. If the first attempt to obtain card status fails,
801 * the status word will reflect the failed status cmd, not the failed
802 * r/w cmd. If we fail to obtain card status, it suggests we can no
803 * longer communicate with the card.
804 * - Check the card state. If the card received the cmd but there was a
805 * transient problem with the response, it might still be in a data transfer
806 * mode. Try to send it a stop command. If this fails, we can't recover.
807 * - If the r/w cmd failed due to a response CRC error, it was probably
808 * transient, so retry the cmd.
809 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
810 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
811 * illegal cmd, retry.
812 * Otherwise we don't understand what happened, so abort.
813 */
814 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
815 struct mmc_blk_request *brq, int *ecc_err)
816 {
817 bool prev_cmd_status_valid = true;
818 u32 status, stop_status = 0;
819 int err, retry;
820
821 if (mmc_card_removed(card))
822 return ERR_NOMEDIUM;
823
824 /*
825 * Try to get card status which indicates both the card state
826 * and why there was no response. If the first attempt fails,
827 * we can't be sure the returned status is for the r/w command.
828 */
829 for (retry = 2; retry >= 0; retry--) {
830 err = get_card_status(card, &status, 0);
831 if (!err)
832 break;
833
834 prev_cmd_status_valid = false;
835 pr_err("%s: error %d sending status command, %sing\n",
836 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
837 }
838
839 /* We couldn't get a response from the card. Give up. */
840 if (err) {
841 /* Check if the card is removed */
842 if (mmc_detect_card_removed(card->host))
843 return ERR_NOMEDIUM;
844 return ERR_ABORT;
845 }
846
847 /* Flag ECC errors */
848 if ((status & R1_CARD_ECC_FAILED) ||
849 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
850 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
851 *ecc_err = 1;
852
853 /*
854 * Check the current card state. If it is in some data transfer
855 * mode, tell it to stop (and hopefully transition back to TRAN.)
856 */
857 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
858 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
859 err = send_stop(card, &stop_status);
860 if (err)
861 pr_err("%s: error %d sending stop command\n",
862 req->rq_disk->disk_name, err);
863
864 /*
865 * If the stop cmd also timed out, the card is probably
866 * not present, so abort. Other errors are bad news too.
867 */
868 if (err)
869 return ERR_ABORT;
870 if (stop_status & R1_CARD_ECC_FAILED)
871 *ecc_err = 1;
872 }
873
874 /* Check for set block count errors */
875 if (brq->sbc.error)
876 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
877 prev_cmd_status_valid, status);
878
879 /* Check for r/w command errors */
880 if (brq->cmd.error)
881 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
882 prev_cmd_status_valid, status);
883
884 /* Data errors */
885 if (!brq->stop.error)
886 return ERR_CONTINUE;
887
888 /* Now for stop errors. These aren't fatal to the transfer. */
889 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
890 req->rq_disk->disk_name, brq->stop.error,
891 brq->cmd.resp[0], status);
892
893 /*
894 * Subsitute in our own stop status as this will give the error
895 * state which happened during the execution of the r/w command.
896 */
897 if (stop_status) {
898 brq->stop.resp[0] = stop_status;
899 brq->stop.error = 0;
900 }
901 return ERR_CONTINUE;
902 }
903
904 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
905 int type)
906 {
907 int err;
908
909 if (md->reset_done & type)
910 return -EEXIST;
911
912 md->reset_done |= type;
913 err = mmc_hw_reset(host);
914 /* Ensure we switch back to the correct partition */
915 if (err != -EOPNOTSUPP) {
916 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
917 int part_err;
918
919 main_md->part_curr = main_md->part_type;
920 part_err = mmc_blk_part_switch(host->card, md);
921 if (part_err) {
922 /*
923 * We have failed to get back into the correct
924 * partition, so we need to abort the whole request.
925 */
926 return -ENODEV;
927 }
928 }
929 return err;
930 }
931
932 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
933 {
934 md->reset_done &= ~type;
935 }
936
937 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
938 {
939 struct mmc_blk_data *md = mq->data;
940 struct mmc_card *card = md->queue.card;
941 unsigned int from, nr, arg;
942 int err = 0, type = MMC_BLK_DISCARD;
943
944 if (!mmc_can_erase(card)) {
945 err = -EOPNOTSUPP;
946 goto out;
947 }
948
949 from = blk_rq_pos(req);
950 nr = blk_rq_sectors(req);
951
952 if (mmc_can_discard(card))
953 arg = MMC_DISCARD_ARG;
954 else if (mmc_can_trim(card))
955 arg = MMC_TRIM_ARG;
956 else
957 arg = MMC_ERASE_ARG;
958 retry:
959 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
960 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
961 INAND_CMD38_ARG_EXT_CSD,
962 arg == MMC_TRIM_ARG ?
963 INAND_CMD38_ARG_TRIM :
964 INAND_CMD38_ARG_ERASE,
965 0);
966 if (err)
967 goto out;
968 }
969 err = mmc_erase(card, from, nr, arg);
970 out:
971 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
972 goto retry;
973 if (!err)
974 mmc_blk_reset_success(md, type);
975 blk_end_request(req, err, blk_rq_bytes(req));
976
977 return err ? 0 : 1;
978 }
979
980 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
981 struct request *req)
982 {
983 struct mmc_blk_data *md = mq->data;
984 struct mmc_card *card = md->queue.card;
985 unsigned int from, nr, arg;
986 int err = 0, type = MMC_BLK_SECDISCARD;
987
988 if (!(mmc_can_secure_erase_trim(card))) {
989 err = -EOPNOTSUPP;
990 goto out;
991 }
992
993 from = blk_rq_pos(req);
994 nr = blk_rq_sectors(req);
995
996 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
997 arg = MMC_SECURE_TRIM1_ARG;
998 else
999 arg = MMC_SECURE_ERASE_ARG;
1000
1001 retry:
1002 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1003 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1004 INAND_CMD38_ARG_EXT_CSD,
1005 arg == MMC_SECURE_TRIM1_ARG ?
1006 INAND_CMD38_ARG_SECTRIM1 :
1007 INAND_CMD38_ARG_SECERASE,
1008 0);
1009 if (err)
1010 goto out_retry;
1011 }
1012
1013 err = mmc_erase(card, from, nr, arg);
1014 if (err == -EIO)
1015 goto out_retry;
1016 if (err)
1017 goto out;
1018
1019 if (arg == MMC_SECURE_TRIM1_ARG) {
1020 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1021 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022 INAND_CMD38_ARG_EXT_CSD,
1023 INAND_CMD38_ARG_SECTRIM2,
1024 0);
1025 if (err)
1026 goto out_retry;
1027 }
1028
1029 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1030 if (err == -EIO)
1031 goto out_retry;
1032 if (err)
1033 goto out;
1034 }
1035
1036 out_retry:
1037 if (err && !mmc_blk_reset(md, card->host, type))
1038 goto retry;
1039 if (!err)
1040 mmc_blk_reset_success(md, type);
1041 out:
1042 blk_end_request(req, err, blk_rq_bytes(req));
1043
1044 return err ? 0 : 1;
1045 }
1046
1047 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1048 {
1049 struct mmc_blk_data *md = mq->data;
1050 struct mmc_card *card = md->queue.card;
1051 int ret = 0;
1052
1053 ret = mmc_flush_cache(card);
1054 if (ret)
1055 ret = -EIO;
1056
1057 blk_end_request_all(req, ret);
1058
1059 return ret ? 0 : 1;
1060 }
1061
1062 /*
1063 * Reformat current write as a reliable write, supporting
1064 * both legacy and the enhanced reliable write MMC cards.
1065 * In each transfer we'll handle only as much as a single
1066 * reliable write can handle, thus finish the request in
1067 * partial completions.
1068 */
1069 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1070 struct mmc_card *card,
1071 struct request *req)
1072 {
1073 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1074 /* Legacy mode imposes restrictions on transfers. */
1075 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1076 brq->data.blocks = 1;
1077
1078 if (brq->data.blocks > card->ext_csd.rel_sectors)
1079 brq->data.blocks = card->ext_csd.rel_sectors;
1080 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1081 brq->data.blocks = 1;
1082 }
1083 }
1084
1085 #define CMD_ERRORS \
1086 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1087 R1_ADDRESS_ERROR | /* Misaligned address */ \
1088 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1089 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1090 R1_CC_ERROR | /* Card controller error */ \
1091 R1_ERROR) /* General/unknown error */
1092
1093 static int mmc_blk_err_check(struct mmc_card *card,
1094 struct mmc_async_req *areq)
1095 {
1096 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1097 mmc_active);
1098 struct mmc_blk_request *brq = &mq_mrq->brq;
1099 struct request *req = mq_mrq->req;
1100 int ecc_err = 0;
1101
1102 /*
1103 * sbc.error indicates a problem with the set block count
1104 * command. No data will have been transferred.
1105 *
1106 * cmd.error indicates a problem with the r/w command. No
1107 * data will have been transferred.
1108 *
1109 * stop.error indicates a problem with the stop command. Data
1110 * may have been transferred, or may still be transferring.
1111 */
1112 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1113 brq->data.error) {
1114 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
1115 case ERR_RETRY:
1116 return MMC_BLK_RETRY;
1117 case ERR_ABORT:
1118 return MMC_BLK_ABORT;
1119 case ERR_NOMEDIUM:
1120 return MMC_BLK_NOMEDIUM;
1121 case ERR_CONTINUE:
1122 break;
1123 }
1124 }
1125
1126 /*
1127 * Check for errors relating to the execution of the
1128 * initial command - such as address errors. No data
1129 * has been transferred.
1130 */
1131 if (brq->cmd.resp[0] & CMD_ERRORS) {
1132 pr_err("%s: r/w command failed, status = %#x\n",
1133 req->rq_disk->disk_name, brq->cmd.resp[0]);
1134 return MMC_BLK_ABORT;
1135 }
1136
1137 /*
1138 * Everything else is either success, or a data error of some
1139 * kind. If it was a write, we may have transitioned to
1140 * program mode, which we have to wait for it to complete.
1141 */
1142 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1143 u32 status;
1144 unsigned long timeout;
1145
1146 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1147 do {
1148 int err = get_card_status(card, &status, 5);
1149 if (err) {
1150 pr_err("%s: error %d requesting status\n",
1151 req->rq_disk->disk_name, err);
1152 return MMC_BLK_CMD_ERR;
1153 }
1154
1155 /* Timeout if the device never becomes ready for data
1156 * and never leaves the program state.
1157 */
1158 if (time_after(jiffies, timeout)) {
1159 pr_err("%s: Card stuck in programming state!"\
1160 " %s %s\n", mmc_hostname(card->host),
1161 req->rq_disk->disk_name, __func__);
1162
1163 return MMC_BLK_CMD_ERR;
1164 }
1165 /*
1166 * Some cards mishandle the status bits,
1167 * so make sure to check both the busy
1168 * indication and the card state.
1169 */
1170 } while (!(status & R1_READY_FOR_DATA) ||
1171 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1172 }
1173
1174 if (brq->data.error) {
1175 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1176 req->rq_disk->disk_name, brq->data.error,
1177 (unsigned)blk_rq_pos(req),
1178 (unsigned)blk_rq_sectors(req),
1179 brq->cmd.resp[0], brq->stop.resp[0]);
1180
1181 if (rq_data_dir(req) == READ) {
1182 if (ecc_err)
1183 return MMC_BLK_ECC_ERR;
1184 return MMC_BLK_DATA_ERR;
1185 } else {
1186 return MMC_BLK_CMD_ERR;
1187 }
1188 }
1189
1190 if (!brq->data.bytes_xfered)
1191 return MMC_BLK_RETRY;
1192
1193 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1194 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1195 return MMC_BLK_PARTIAL;
1196 else
1197 return MMC_BLK_SUCCESS;
1198 }
1199
1200 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1201 return MMC_BLK_PARTIAL;
1202
1203 return MMC_BLK_SUCCESS;
1204 }
1205
1206 static int mmc_blk_packed_err_check(struct mmc_card *card,
1207 struct mmc_async_req *areq)
1208 {
1209 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1210 mmc_active);
1211 struct request *req = mq_rq->req;
1212 struct mmc_packed *packed = mq_rq->packed;
1213 int err, check, status;
1214 u8 *ext_csd;
1215
1216 BUG_ON(!packed);
1217
1218 packed->retries--;
1219 check = mmc_blk_err_check(card, areq);
1220 err = get_card_status(card, &status, 0);
1221 if (err) {
1222 pr_err("%s: error %d sending status command\n",
1223 req->rq_disk->disk_name, err);
1224 return MMC_BLK_ABORT;
1225 }
1226
1227 if (status & R1_EXCEPTION_EVENT) {
1228 ext_csd = kzalloc(512, GFP_KERNEL);
1229 if (!ext_csd) {
1230 pr_err("%s: unable to allocate buffer for ext_csd\n",
1231 req->rq_disk->disk_name);
1232 return -ENOMEM;
1233 }
1234
1235 err = mmc_send_ext_csd(card, ext_csd);
1236 if (err) {
1237 pr_err("%s: error %d sending ext_csd\n",
1238 req->rq_disk->disk_name, err);
1239 check = MMC_BLK_ABORT;
1240 goto free;
1241 }
1242
1243 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1244 EXT_CSD_PACKED_FAILURE) &&
1245 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1246 EXT_CSD_PACKED_GENERIC_ERROR)) {
1247 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1248 EXT_CSD_PACKED_INDEXED_ERROR) {
1249 packed->idx_failure =
1250 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1251 check = MMC_BLK_PARTIAL;
1252 }
1253 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1254 "failure index: %d\n",
1255 req->rq_disk->disk_name, packed->nr_entries,
1256 packed->blocks, packed->idx_failure);
1257 }
1258 free:
1259 kfree(ext_csd);
1260 }
1261
1262 return check;
1263 }
1264
1265 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1266 struct mmc_card *card,
1267 int disable_multi,
1268 struct mmc_queue *mq)
1269 {
1270 u32 readcmd, writecmd;
1271 struct mmc_blk_request *brq = &mqrq->brq;
1272 struct request *req = mqrq->req;
1273 struct mmc_blk_data *md = mq->data;
1274 bool do_data_tag;
1275
1276 /*
1277 * Reliable writes are used to implement Forced Unit Access and
1278 * REQ_META accesses, and are supported only on MMCs.
1279 *
1280 * XXX: this really needs a good explanation of why REQ_META
1281 * is treated special.
1282 */
1283 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1284 (req->cmd_flags & REQ_META)) &&
1285 (rq_data_dir(req) == WRITE) &&
1286 (md->flags & MMC_BLK_REL_WR);
1287
1288 memset(brq, 0, sizeof(struct mmc_blk_request));
1289 brq->mrq.cmd = &brq->cmd;
1290 brq->mrq.data = &brq->data;
1291
1292 brq->cmd.arg = blk_rq_pos(req);
1293 if (!mmc_card_blockaddr(card))
1294 brq->cmd.arg <<= 9;
1295 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1296 brq->data.blksz = 512;
1297 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1298 brq->stop.arg = 0;
1299 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1300 brq->data.blocks = blk_rq_sectors(req);
1301
1302 /*
1303 * The block layer doesn't support all sector count
1304 * restrictions, so we need to be prepared for too big
1305 * requests.
1306 */
1307 if (brq->data.blocks > card->host->max_blk_count)
1308 brq->data.blocks = card->host->max_blk_count;
1309
1310 if (brq->data.blocks > 1) {
1311 /*
1312 * After a read error, we redo the request one sector
1313 * at a time in order to accurately determine which
1314 * sectors can be read successfully.
1315 */
1316 if (disable_multi)
1317 brq->data.blocks = 1;
1318
1319 /* Some controllers can't do multiblock reads due to hw bugs */
1320 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1321 rq_data_dir(req) == READ)
1322 brq->data.blocks = 1;
1323 }
1324
1325 if (brq->data.blocks > 1 || do_rel_wr) {
1326 /* SPI multiblock writes terminate using a special
1327 * token, not a STOP_TRANSMISSION request.
1328 */
1329 if (!mmc_host_is_spi(card->host) ||
1330 rq_data_dir(req) == READ)
1331 brq->mrq.stop = &brq->stop;
1332 readcmd = MMC_READ_MULTIPLE_BLOCK;
1333 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1334 } else {
1335 brq->mrq.stop = NULL;
1336 readcmd = MMC_READ_SINGLE_BLOCK;
1337 writecmd = MMC_WRITE_BLOCK;
1338 }
1339 if (rq_data_dir(req) == READ) {
1340 brq->cmd.opcode = readcmd;
1341 brq->data.flags |= MMC_DATA_READ;
1342 } else {
1343 brq->cmd.opcode = writecmd;
1344 brq->data.flags |= MMC_DATA_WRITE;
1345 }
1346
1347 if (do_rel_wr)
1348 mmc_apply_rel_rw(brq, card, req);
1349
1350 /*
1351 * Data tag is used only during writing meta data to speed
1352 * up write and any subsequent read of this meta data
1353 */
1354 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1355 (req->cmd_flags & REQ_META) &&
1356 (rq_data_dir(req) == WRITE) &&
1357 ((brq->data.blocks * brq->data.blksz) >=
1358 card->ext_csd.data_tag_unit_size);
1359
1360 /*
1361 * Pre-defined multi-block transfers are preferable to
1362 * open ended-ones (and necessary for reliable writes).
1363 * However, it is not sufficient to just send CMD23,
1364 * and avoid the final CMD12, as on an error condition
1365 * CMD12 (stop) needs to be sent anyway. This, coupled
1366 * with Auto-CMD23 enhancements provided by some
1367 * hosts, means that the complexity of dealing
1368 * with this is best left to the host. If CMD23 is
1369 * supported by card and host, we'll fill sbc in and let
1370 * the host deal with handling it correctly. This means
1371 * that for hosts that don't expose MMC_CAP_CMD23, no
1372 * change of behavior will be observed.
1373 *
1374 * N.B: Some MMC cards experience perf degradation.
1375 * We'll avoid using CMD23-bounded multiblock writes for
1376 * these, while retaining features like reliable writes.
1377 */
1378 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1379 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1380 do_data_tag)) {
1381 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1382 brq->sbc.arg = brq->data.blocks |
1383 (do_rel_wr ? (1 << 31) : 0) |
1384 (do_data_tag ? (1 << 29) : 0);
1385 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1386 brq->mrq.sbc = &brq->sbc;
1387 }
1388
1389 mmc_set_data_timeout(&brq->data, card);
1390
1391 brq->data.sg = mqrq->sg;
1392 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1393
1394 /*
1395 * Adjust the sg list so it is the same size as the
1396 * request.
1397 */
1398 if (brq->data.blocks != blk_rq_sectors(req)) {
1399 int i, data_size = brq->data.blocks << 9;
1400 struct scatterlist *sg;
1401
1402 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1403 data_size -= sg->length;
1404 if (data_size <= 0) {
1405 sg->length += data_size;
1406 i++;
1407 break;
1408 }
1409 }
1410 brq->data.sg_len = i;
1411 }
1412
1413 mqrq->mmc_active.mrq = &brq->mrq;
1414 mqrq->mmc_active.err_check = mmc_blk_err_check;
1415
1416 mmc_queue_bounce_pre(mqrq);
1417 }
1418
1419 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1420 struct mmc_card *card)
1421 {
1422 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1423 unsigned int max_seg_sz = queue_max_segment_size(q);
1424 unsigned int len, nr_segs = 0;
1425
1426 do {
1427 len = min(hdr_sz, max_seg_sz);
1428 hdr_sz -= len;
1429 nr_segs++;
1430 } while (hdr_sz);
1431
1432 return nr_segs;
1433 }
1434
1435 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1436 {
1437 struct request_queue *q = mq->queue;
1438 struct mmc_card *card = mq->card;
1439 struct request *cur = req, *next = NULL;
1440 struct mmc_blk_data *md = mq->data;
1441 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1442 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1443 unsigned int req_sectors = 0, phys_segments = 0;
1444 unsigned int max_blk_count, max_phys_segs;
1445 bool put_back = true;
1446 u8 max_packed_rw = 0;
1447 u8 reqs = 0;
1448
1449 if (!(md->flags & MMC_BLK_PACKED_CMD))
1450 goto no_packed;
1451
1452 if ((rq_data_dir(cur) == WRITE) &&
1453 mmc_host_packed_wr(card->host))
1454 max_packed_rw = card->ext_csd.max_packed_writes;
1455
1456 if (max_packed_rw == 0)
1457 goto no_packed;
1458
1459 if (mmc_req_rel_wr(cur) &&
1460 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1461 goto no_packed;
1462
1463 if (mmc_large_sector(card) &&
1464 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1465 goto no_packed;
1466
1467 mmc_blk_clear_packed(mqrq);
1468
1469 max_blk_count = min(card->host->max_blk_count,
1470 card->host->max_req_size >> 9);
1471 if (unlikely(max_blk_count > 0xffff))
1472 max_blk_count = 0xffff;
1473
1474 max_phys_segs = queue_max_segments(q);
1475 req_sectors += blk_rq_sectors(cur);
1476 phys_segments += cur->nr_phys_segments;
1477
1478 if (rq_data_dir(cur) == WRITE) {
1479 req_sectors += mmc_large_sector(card) ? 8 : 1;
1480 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1481 }
1482
1483 do {
1484 if (reqs >= max_packed_rw - 1) {
1485 put_back = false;
1486 break;
1487 }
1488
1489 spin_lock_irq(q->queue_lock);
1490 next = blk_fetch_request(q);
1491 spin_unlock_irq(q->queue_lock);
1492 if (!next) {
1493 put_back = false;
1494 break;
1495 }
1496
1497 if (mmc_large_sector(card) &&
1498 !IS_ALIGNED(blk_rq_sectors(next), 8))
1499 break;
1500
1501 if (next->cmd_flags & REQ_DISCARD ||
1502 next->cmd_flags & REQ_FLUSH)
1503 break;
1504
1505 if (rq_data_dir(cur) != rq_data_dir(next))
1506 break;
1507
1508 if (mmc_req_rel_wr(next) &&
1509 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1510 break;
1511
1512 req_sectors += blk_rq_sectors(next);
1513 if (req_sectors > max_blk_count)
1514 break;
1515
1516 phys_segments += next->nr_phys_segments;
1517 if (phys_segments > max_phys_segs)
1518 break;
1519
1520 list_add_tail(&next->queuelist, &mqrq->packed->list);
1521 cur = next;
1522 reqs++;
1523 } while (1);
1524
1525 if (put_back) {
1526 spin_lock_irq(q->queue_lock);
1527 blk_requeue_request(q, next);
1528 spin_unlock_irq(q->queue_lock);
1529 }
1530
1531 if (reqs > 0) {
1532 list_add(&req->queuelist, &mqrq->packed->list);
1533 mqrq->packed->nr_entries = ++reqs;
1534 mqrq->packed->retries = reqs;
1535 return reqs;
1536 }
1537
1538 no_packed:
1539 mqrq->cmd_type = MMC_PACKED_NONE;
1540 return 0;
1541 }
1542
1543 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1544 struct mmc_card *card,
1545 struct mmc_queue *mq)
1546 {
1547 struct mmc_blk_request *brq = &mqrq->brq;
1548 struct request *req = mqrq->req;
1549 struct request *prq;
1550 struct mmc_blk_data *md = mq->data;
1551 struct mmc_packed *packed = mqrq->packed;
1552 bool do_rel_wr, do_data_tag;
1553 u32 *packed_cmd_hdr;
1554 u8 hdr_blocks;
1555 u8 i = 1;
1556
1557 BUG_ON(!packed);
1558
1559 mqrq->cmd_type = MMC_PACKED_WRITE;
1560 packed->blocks = 0;
1561 packed->idx_failure = MMC_PACKED_NR_IDX;
1562
1563 packed_cmd_hdr = packed->cmd_hdr;
1564 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1565 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1566 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1567 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1568
1569 /*
1570 * Argument for each entry of packed group
1571 */
1572 list_for_each_entry(prq, &packed->list, queuelist) {
1573 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1574 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1575 (prq->cmd_flags & REQ_META) &&
1576 (rq_data_dir(prq) == WRITE) &&
1577 ((brq->data.blocks * brq->data.blksz) >=
1578 card->ext_csd.data_tag_unit_size);
1579 /* Argument of CMD23 */
1580 packed_cmd_hdr[(i * 2)] =
1581 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1582 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1583 blk_rq_sectors(prq);
1584 /* Argument of CMD18 or CMD25 */
1585 packed_cmd_hdr[((i * 2)) + 1] =
1586 mmc_card_blockaddr(card) ?
1587 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1588 packed->blocks += blk_rq_sectors(prq);
1589 i++;
1590 }
1591
1592 memset(brq, 0, sizeof(struct mmc_blk_request));
1593 brq->mrq.cmd = &brq->cmd;
1594 brq->mrq.data = &brq->data;
1595 brq->mrq.sbc = &brq->sbc;
1596 brq->mrq.stop = &brq->stop;
1597
1598 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1599 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1600 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1601
1602 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1603 brq->cmd.arg = blk_rq_pos(req);
1604 if (!mmc_card_blockaddr(card))
1605 brq->cmd.arg <<= 9;
1606 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1607
1608 brq->data.blksz = 512;
1609 brq->data.blocks = packed->blocks + hdr_blocks;
1610 brq->data.flags |= MMC_DATA_WRITE;
1611
1612 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1613 brq->stop.arg = 0;
1614 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1615
1616 mmc_set_data_timeout(&brq->data, card);
1617
1618 brq->data.sg = mqrq->sg;
1619 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1620
1621 mqrq->mmc_active.mrq = &brq->mrq;
1622 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1623
1624 mmc_queue_bounce_pre(mqrq);
1625 }
1626
1627 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1628 struct mmc_blk_request *brq, struct request *req,
1629 int ret)
1630 {
1631 struct mmc_queue_req *mq_rq;
1632 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1633
1634 /*
1635 * If this is an SD card and we're writing, we can first
1636 * mark the known good sectors as ok.
1637 *
1638 * If the card is not SD, we can still ok written sectors
1639 * as reported by the controller (which might be less than
1640 * the real number of written sectors, but never more).
1641 */
1642 if (mmc_card_sd(card)) {
1643 u32 blocks;
1644
1645 blocks = mmc_sd_num_wr_blocks(card);
1646 if (blocks != (u32)-1) {
1647 ret = blk_end_request(req, 0, blocks << 9);
1648 }
1649 } else {
1650 if (!mmc_packed_cmd(mq_rq->cmd_type))
1651 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1652 }
1653 return ret;
1654 }
1655
1656 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1657 {
1658 struct request *prq;
1659 struct mmc_packed *packed = mq_rq->packed;
1660 int idx = packed->idx_failure, i = 0;
1661 int ret = 0;
1662
1663 BUG_ON(!packed);
1664
1665 while (!list_empty(&packed->list)) {
1666 prq = list_entry_rq(packed->list.next);
1667 if (idx == i) {
1668 /* retry from error index */
1669 packed->nr_entries -= idx;
1670 mq_rq->req = prq;
1671 ret = 1;
1672
1673 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1674 list_del_init(&prq->queuelist);
1675 mmc_blk_clear_packed(mq_rq);
1676 }
1677 return ret;
1678 }
1679 list_del_init(&prq->queuelist);
1680 blk_end_request(prq, 0, blk_rq_bytes(prq));
1681 i++;
1682 }
1683
1684 mmc_blk_clear_packed(mq_rq);
1685 return ret;
1686 }
1687
1688 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1689 {
1690 struct request *prq;
1691 struct mmc_packed *packed = mq_rq->packed;
1692
1693 BUG_ON(!packed);
1694
1695 while (!list_empty(&packed->list)) {
1696 prq = list_entry_rq(packed->list.next);
1697 list_del_init(&prq->queuelist);
1698 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1699 }
1700
1701 mmc_blk_clear_packed(mq_rq);
1702 }
1703
1704 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1705 struct mmc_queue_req *mq_rq)
1706 {
1707 struct request *prq;
1708 struct request_queue *q = mq->queue;
1709 struct mmc_packed *packed = mq_rq->packed;
1710
1711 BUG_ON(!packed);
1712
1713 while (!list_empty(&packed->list)) {
1714 prq = list_entry_rq(packed->list.prev);
1715 if (prq->queuelist.prev != &packed->list) {
1716 list_del_init(&prq->queuelist);
1717 spin_lock_irq(q->queue_lock);
1718 blk_requeue_request(mq->queue, prq);
1719 spin_unlock_irq(q->queue_lock);
1720 } else {
1721 list_del_init(&prq->queuelist);
1722 }
1723 }
1724
1725 mmc_blk_clear_packed(mq_rq);
1726 }
1727
1728 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1729 {
1730 struct mmc_blk_data *md = mq->data;
1731 struct mmc_card *card = md->queue.card;
1732 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1733 int ret = 1, disable_multi = 0, retry = 0, type;
1734 enum mmc_blk_status status;
1735 struct mmc_queue_req *mq_rq;
1736 struct request *req = rqc;
1737 struct mmc_async_req *areq;
1738 const u8 packed_nr = 2;
1739 u8 reqs = 0;
1740
1741 if (!rqc && !mq->mqrq_prev->req)
1742 return 0;
1743
1744 if (rqc)
1745 reqs = mmc_blk_prep_packed_list(mq, rqc);
1746
1747 do {
1748 if (rqc) {
1749 /*
1750 * When 4KB native sector is enabled, only 8 blocks
1751 * multiple read or write is allowed
1752 */
1753 if ((brq->data.blocks & 0x07) &&
1754 (card->ext_csd.data_sector_size == 4096)) {
1755 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1756 req->rq_disk->disk_name);
1757 mq_rq = mq->mqrq_cur;
1758 goto cmd_abort;
1759 }
1760
1761 if (reqs >= packed_nr)
1762 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1763 card, mq);
1764 else
1765 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1766 areq = &mq->mqrq_cur->mmc_active;
1767 } else
1768 areq = NULL;
1769 areq = mmc_start_req(card->host, areq, (int *) &status);
1770 if (!areq) {
1771 if (status == MMC_BLK_NEW_REQUEST)
1772 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1773 return 0;
1774 }
1775
1776 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1777 brq = &mq_rq->brq;
1778 req = mq_rq->req;
1779 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1780 mmc_queue_bounce_post(mq_rq);
1781
1782 switch (status) {
1783 case MMC_BLK_SUCCESS:
1784 case MMC_BLK_PARTIAL:
1785 /*
1786 * A block was successfully transferred.
1787 */
1788 mmc_blk_reset_success(md, type);
1789
1790 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1791 ret = mmc_blk_end_packed_req(mq_rq);
1792 break;
1793 } else {
1794 ret = blk_end_request(req, 0,
1795 brq->data.bytes_xfered);
1796 }
1797
1798 /*
1799 * If the blk_end_request function returns non-zero even
1800 * though all data has been transferred and no errors
1801 * were returned by the host controller, it's a bug.
1802 */
1803 if (status == MMC_BLK_SUCCESS && ret) {
1804 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1805 __func__, blk_rq_bytes(req),
1806 brq->data.bytes_xfered);
1807 rqc = NULL;
1808 goto cmd_abort;
1809 }
1810 break;
1811 case MMC_BLK_CMD_ERR:
1812 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1813 if (!mmc_blk_reset(md, card->host, type))
1814 break;
1815 goto cmd_abort;
1816 case MMC_BLK_RETRY:
1817 if (retry++ < 5)
1818 break;
1819 /* Fall through */
1820 case MMC_BLK_ABORT:
1821 if (!mmc_blk_reset(md, card->host, type))
1822 break;
1823 goto cmd_abort;
1824 case MMC_BLK_DATA_ERR: {
1825 int err;
1826
1827 err = mmc_blk_reset(md, card->host, type);
1828 if (!err)
1829 break;
1830 if (err == -ENODEV ||
1831 mmc_packed_cmd(mq_rq->cmd_type))
1832 goto cmd_abort;
1833 /* Fall through */
1834 }
1835 case MMC_BLK_ECC_ERR:
1836 if (brq->data.blocks > 1) {
1837 /* Redo read one sector at a time */
1838 pr_warning("%s: retrying using single block read\n",
1839 req->rq_disk->disk_name);
1840 disable_multi = 1;
1841 break;
1842 }
1843 /*
1844 * After an error, we redo I/O one sector at a
1845 * time, so we only reach here after trying to
1846 * read a single sector.
1847 */
1848 ret = blk_end_request(req, -EIO,
1849 brq->data.blksz);
1850 if (!ret)
1851 goto start_new_req;
1852 break;
1853 case MMC_BLK_NOMEDIUM:
1854 goto cmd_abort;
1855 default:
1856 pr_err("%s: Unhandled return value (%d)",
1857 req->rq_disk->disk_name, status);
1858 goto cmd_abort;
1859 }
1860
1861 if (ret) {
1862 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1863 if (!mq_rq->packed->retries)
1864 goto cmd_abort;
1865 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1866 mmc_start_req(card->host,
1867 &mq_rq->mmc_active, NULL);
1868 } else {
1869
1870 /*
1871 * In case of a incomplete request
1872 * prepare it again and resend.
1873 */
1874 mmc_blk_rw_rq_prep(mq_rq, card,
1875 disable_multi, mq);
1876 mmc_start_req(card->host,
1877 &mq_rq->mmc_active, NULL);
1878 }
1879 }
1880 } while (ret);
1881
1882 return 1;
1883
1884 cmd_abort:
1885 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1886 mmc_blk_abort_packed_req(mq_rq);
1887 } else {
1888 if (mmc_card_removed(card))
1889 req->cmd_flags |= REQ_QUIET;
1890 while (ret)
1891 ret = blk_end_request(req, -EIO,
1892 blk_rq_cur_bytes(req));
1893 }
1894
1895 start_new_req:
1896 if (rqc) {
1897 if (mmc_card_removed(card)) {
1898 rqc->cmd_flags |= REQ_QUIET;
1899 blk_end_request_all(rqc, -EIO);
1900 } else {
1901 /*
1902 * If current request is packed, it needs to put back.
1903 */
1904 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1905 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1906
1907 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1908 mmc_start_req(card->host,
1909 &mq->mqrq_cur->mmc_active, NULL);
1910 }
1911 }
1912
1913 return 0;
1914 }
1915
1916 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1917 {
1918 int ret;
1919 struct mmc_blk_data *md = mq->data;
1920 struct mmc_card *card = md->queue.card;
1921 struct mmc_host *host = card->host;
1922 unsigned long flags;
1923
1924 if (req && !mq->mqrq_prev->req)
1925 /* claim host only for the first request */
1926 mmc_get_card(card);
1927
1928 ret = mmc_blk_part_switch(card, md);
1929 if (ret) {
1930 if (req) {
1931 blk_end_request_all(req, -EIO);
1932 }
1933 ret = 0;
1934 goto out;
1935 }
1936
1937 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1938 if (req && req->cmd_flags & REQ_DISCARD) {
1939 /* complete ongoing async transfer before issuing discard */
1940 if (card->host->areq)
1941 mmc_blk_issue_rw_rq(mq, NULL);
1942 if (req->cmd_flags & REQ_SECURE &&
1943 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1944 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1945 else
1946 ret = mmc_blk_issue_discard_rq(mq, req);
1947 } else if (req && req->cmd_flags & REQ_FLUSH) {
1948 /* complete ongoing async transfer before issuing flush */
1949 if (card->host->areq)
1950 mmc_blk_issue_rw_rq(mq, NULL);
1951 ret = mmc_blk_issue_flush(mq, req);
1952 } else {
1953 if (!req && host->areq) {
1954 spin_lock_irqsave(&host->context_info.lock, flags);
1955 host->context_info.is_waiting_last_req = true;
1956 spin_unlock_irqrestore(&host->context_info.lock, flags);
1957 }
1958 ret = mmc_blk_issue_rw_rq(mq, req);
1959 }
1960
1961 out:
1962 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
1963 (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
1964 /*
1965 * Release host when there are no more requests
1966 * and after special request(discard, flush) is done.
1967 * In case sepecial request, there is no reentry to
1968 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1969 */
1970 mmc_put_card(card);
1971 return ret;
1972 }
1973
1974 static inline int mmc_blk_readonly(struct mmc_card *card)
1975 {
1976 return mmc_card_readonly(card) ||
1977 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1978 }
1979
1980 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1981 struct device *parent,
1982 sector_t size,
1983 bool default_ro,
1984 const char *subname,
1985 int area_type)
1986 {
1987 struct mmc_blk_data *md;
1988 int devidx, ret;
1989
1990 devidx = find_first_zero_bit(dev_use, max_devices);
1991 if (devidx >= max_devices)
1992 return ERR_PTR(-ENOSPC);
1993 __set_bit(devidx, dev_use);
1994
1995 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1996 if (!md) {
1997 ret = -ENOMEM;
1998 goto out;
1999 }
2000
2001 /*
2002 * !subname implies we are creating main mmc_blk_data that will be
2003 * associated with mmc_card with mmc_set_drvdata. Due to device
2004 * partitions, devidx will not coincide with a per-physical card
2005 * index anymore so we keep track of a name index.
2006 */
2007 if (!subname) {
2008 md->name_idx = find_first_zero_bit(name_use, max_devices);
2009 __set_bit(md->name_idx, name_use);
2010 } else
2011 md->name_idx = ((struct mmc_blk_data *)
2012 dev_to_disk(parent)->private_data)->name_idx;
2013
2014 md->area_type = area_type;
2015
2016 /*
2017 * Set the read-only status based on the supported commands
2018 * and the write protect switch.
2019 */
2020 md->read_only = mmc_blk_readonly(card);
2021
2022 md->disk = alloc_disk(perdev_minors);
2023 if (md->disk == NULL) {
2024 ret = -ENOMEM;
2025 goto err_kfree;
2026 }
2027
2028 spin_lock_init(&md->lock);
2029 INIT_LIST_HEAD(&md->part);
2030 md->usage = 1;
2031
2032 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2033 if (ret)
2034 goto err_putdisk;
2035
2036 md->queue.issue_fn = mmc_blk_issue_rq;
2037 md->queue.data = md;
2038
2039 md->disk->major = MMC_BLOCK_MAJOR;
2040 md->disk->first_minor = devidx * perdev_minors;
2041 md->disk->fops = &mmc_bdops;
2042 md->disk->private_data = md;
2043 md->disk->queue = md->queue.queue;
2044 md->disk->driverfs_dev = parent;
2045 set_disk_ro(md->disk, md->read_only || default_ro);
2046 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2047 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2048
2049 /*
2050 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2051 *
2052 * - be set for removable media with permanent block devices
2053 * - be unset for removable block devices with permanent media
2054 *
2055 * Since MMC block devices clearly fall under the second
2056 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2057 * should use the block device creation/destruction hotplug
2058 * messages to tell when the card is present.
2059 */
2060
2061 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2062 "mmcblk%d%s", md->name_idx, subname ? subname : "");
2063
2064 if (mmc_card_mmc(card))
2065 blk_queue_logical_block_size(md->queue.queue,
2066 card->ext_csd.data_sector_size);
2067 else
2068 blk_queue_logical_block_size(md->queue.queue, 512);
2069
2070 set_capacity(md->disk, size);
2071
2072 if (mmc_host_cmd23(card->host)) {
2073 if (mmc_card_mmc(card) ||
2074 (mmc_card_sd(card) &&
2075 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2076 md->flags |= MMC_BLK_CMD23;
2077 }
2078
2079 if (mmc_card_mmc(card) &&
2080 md->flags & MMC_BLK_CMD23 &&
2081 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2082 card->ext_csd.rel_sectors)) {
2083 md->flags |= MMC_BLK_REL_WR;
2084 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2085 }
2086
2087 if (mmc_card_mmc(card) &&
2088 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2089 (md->flags & MMC_BLK_CMD23) &&
2090 card->ext_csd.packed_event_en) {
2091 if (!mmc_packed_init(&md->queue, card))
2092 md->flags |= MMC_BLK_PACKED_CMD;
2093 }
2094
2095 return md;
2096
2097 err_putdisk:
2098 put_disk(md->disk);
2099 err_kfree:
2100 kfree(md);
2101 out:
2102 return ERR_PTR(ret);
2103 }
2104
2105 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2106 {
2107 sector_t size;
2108 struct mmc_blk_data *md;
2109
2110 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2111 /*
2112 * The EXT_CSD sector count is in number or 512 byte
2113 * sectors.
2114 */
2115 size = card->ext_csd.sectors;
2116 } else {
2117 /*
2118 * The CSD capacity field is in units of read_blkbits.
2119 * set_capacity takes units of 512 bytes.
2120 */
2121 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2122 }
2123
2124 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2125 MMC_BLK_DATA_AREA_MAIN);
2126 return md;
2127 }
2128
2129 static int mmc_blk_alloc_part(struct mmc_card *card,
2130 struct mmc_blk_data *md,
2131 unsigned int part_type,
2132 sector_t size,
2133 bool default_ro,
2134 const char *subname,
2135 int area_type)
2136 {
2137 char cap_str[10];
2138 struct mmc_blk_data *part_md;
2139
2140 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2141 subname, area_type);
2142 if (IS_ERR(part_md))
2143 return PTR_ERR(part_md);
2144 part_md->part_type = part_type;
2145 list_add(&part_md->part, &md->part);
2146
2147 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2148 cap_str, sizeof(cap_str));
2149 pr_info("%s: %s %s partition %u %s\n",
2150 part_md->disk->disk_name, mmc_card_id(card),
2151 mmc_card_name(card), part_md->part_type, cap_str);
2152 return 0;
2153 }
2154
2155 /* MMC Physical partitions consist of two boot partitions and
2156 * up to four general purpose partitions.
2157 * For each partition enabled in EXT_CSD a block device will be allocatedi
2158 * to provide access to the partition.
2159 */
2160
2161 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2162 {
2163 int idx, ret = 0;
2164
2165 if (!mmc_card_mmc(card))
2166 return 0;
2167
2168 for (idx = 0; idx < card->nr_parts; idx++) {
2169 if (card->part[idx].size) {
2170 ret = mmc_blk_alloc_part(card, md,
2171 card->part[idx].part_cfg,
2172 card->part[idx].size >> 9,
2173 card->part[idx].force_ro,
2174 card->part[idx].name,
2175 card->part[idx].area_type);
2176 if (ret)
2177 return ret;
2178 }
2179 }
2180
2181 return ret;
2182 }
2183
2184 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2185 {
2186 struct mmc_card *card;
2187
2188 if (md) {
2189 /*
2190 * Flush remaining requests and free queues. It
2191 * is freeing the queue that stops new requests
2192 * from being accepted.
2193 */
2194 mmc_cleanup_queue(&md->queue);
2195 if (md->flags & MMC_BLK_PACKED_CMD)
2196 mmc_packed_clean(&md->queue);
2197 card = md->queue.card;
2198 if (md->disk->flags & GENHD_FL_UP) {
2199 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2200 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2201 card->ext_csd.boot_ro_lockable)
2202 device_remove_file(disk_to_dev(md->disk),
2203 &md->power_ro_lock);
2204
2205 del_gendisk(md->disk);
2206 }
2207 mmc_blk_put(md);
2208 }
2209 }
2210
2211 static void mmc_blk_remove_parts(struct mmc_card *card,
2212 struct mmc_blk_data *md)
2213 {
2214 struct list_head *pos, *q;
2215 struct mmc_blk_data *part_md;
2216
2217 __clear_bit(md->name_idx, name_use);
2218 list_for_each_safe(pos, q, &md->part) {
2219 part_md = list_entry(pos, struct mmc_blk_data, part);
2220 list_del(pos);
2221 mmc_blk_remove_req(part_md);
2222 }
2223 }
2224
2225 static int mmc_add_disk(struct mmc_blk_data *md)
2226 {
2227 int ret;
2228 struct mmc_card *card = md->queue.card;
2229
2230 add_disk(md->disk);
2231 md->force_ro.show = force_ro_show;
2232 md->force_ro.store = force_ro_store;
2233 sysfs_attr_init(&md->force_ro.attr);
2234 md->force_ro.attr.name = "force_ro";
2235 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2236 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2237 if (ret)
2238 goto force_ro_fail;
2239
2240 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2241 card->ext_csd.boot_ro_lockable) {
2242 umode_t mode;
2243
2244 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2245 mode = S_IRUGO;
2246 else
2247 mode = S_IRUGO | S_IWUSR;
2248
2249 md->power_ro_lock.show = power_ro_lock_show;
2250 md->power_ro_lock.store = power_ro_lock_store;
2251 sysfs_attr_init(&md->power_ro_lock.attr);
2252 md->power_ro_lock.attr.mode = mode;
2253 md->power_ro_lock.attr.name =
2254 "ro_lock_until_next_power_on";
2255 ret = device_create_file(disk_to_dev(md->disk),
2256 &md->power_ro_lock);
2257 if (ret)
2258 goto power_ro_lock_fail;
2259 }
2260 return ret;
2261
2262 power_ro_lock_fail:
2263 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2264 force_ro_fail:
2265 del_gendisk(md->disk);
2266
2267 return ret;
2268 }
2269
2270 #define CID_MANFID_SANDISK 0x2
2271 #define CID_MANFID_TOSHIBA 0x11
2272 #define CID_MANFID_MICRON 0x13
2273 #define CID_MANFID_SAMSUNG 0x15
2274
2275 static const struct mmc_fixup blk_fixups[] =
2276 {
2277 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2278 MMC_QUIRK_INAND_CMD38),
2279 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2280 MMC_QUIRK_INAND_CMD38),
2281 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2282 MMC_QUIRK_INAND_CMD38),
2283 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2284 MMC_QUIRK_INAND_CMD38),
2285 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2286 MMC_QUIRK_INAND_CMD38),
2287
2288 /*
2289 * Some MMC cards experience performance degradation with CMD23
2290 * instead of CMD12-bounded multiblock transfers. For now we'll
2291 * black list what's bad...
2292 * - Certain Toshiba cards.
2293 *
2294 * N.B. This doesn't affect SD cards.
2295 */
2296 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2297 MMC_QUIRK_BLK_NO_CMD23),
2298 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2299 MMC_QUIRK_BLK_NO_CMD23),
2300 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2301 MMC_QUIRK_BLK_NO_CMD23),
2302
2303 /*
2304 * Some Micron MMC cards needs longer data read timeout than
2305 * indicated in CSD.
2306 */
2307 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2308 MMC_QUIRK_LONG_READ_TIME),
2309
2310 /*
2311 * On these Samsung MoviNAND parts, performing secure erase or
2312 * secure trim can result in unrecoverable corruption due to a
2313 * firmware bug.
2314 */
2315 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2317 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2319 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2321 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2323 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2325 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2326 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2327 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2328 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2329 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2330 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2331
2332 END_FIXUP
2333 };
2334
2335 static int mmc_blk_probe(struct mmc_card *card)
2336 {
2337 struct mmc_blk_data *md, *part_md;
2338 char cap_str[10];
2339
2340 /*
2341 * Check that the card supports the command class(es) we need.
2342 */
2343 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2344 return -ENODEV;
2345
2346 md = mmc_blk_alloc(card);
2347 if (IS_ERR(md))
2348 return PTR_ERR(md);
2349
2350 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2351 cap_str, sizeof(cap_str));
2352 pr_info("%s: %s %s %s %s\n",
2353 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2354 cap_str, md->read_only ? "(ro)" : "");
2355
2356 if (mmc_blk_alloc_parts(card, md))
2357 goto out;
2358
2359 mmc_set_drvdata(card, md);
2360 mmc_fixup_device(card, blk_fixups);
2361
2362 if (mmc_add_disk(md))
2363 goto out;
2364
2365 list_for_each_entry(part_md, &md->part, part) {
2366 if (mmc_add_disk(part_md))
2367 goto out;
2368 }
2369
2370 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2371 pm_runtime_use_autosuspend(&card->dev);
2372
2373 /*
2374 * Don't enable runtime PM for SD-combo cards here. Leave that
2375 * decision to be taken during the SDIO init sequence instead.
2376 */
2377 if (card->type != MMC_TYPE_SD_COMBO) {
2378 pm_runtime_set_active(&card->dev);
2379 pm_runtime_enable(&card->dev);
2380 }
2381
2382 return 0;
2383
2384 out:
2385 mmc_blk_remove_parts(card, md);
2386 mmc_blk_remove_req(md);
2387 return 0;
2388 }
2389
2390 static void mmc_blk_remove(struct mmc_card *card)
2391 {
2392 struct mmc_blk_data *md = mmc_get_drvdata(card);
2393
2394 mmc_blk_remove_parts(card, md);
2395 pm_runtime_get_sync(&card->dev);
2396 mmc_claim_host(card->host);
2397 mmc_blk_part_switch(card, md);
2398 mmc_release_host(card->host);
2399 if (card->type != MMC_TYPE_SD_COMBO)
2400 pm_runtime_disable(&card->dev);
2401 pm_runtime_put_noidle(&card->dev);
2402 mmc_blk_remove_req(md);
2403 mmc_set_drvdata(card, NULL);
2404 }
2405
2406 static int _mmc_blk_suspend(struct mmc_card *card)
2407 {
2408 struct mmc_blk_data *part_md;
2409 struct mmc_blk_data *md = mmc_get_drvdata(card);
2410
2411 if (md) {
2412 pm_runtime_get_sync(&card->dev);
2413 mmc_queue_suspend(&md->queue);
2414 list_for_each_entry(part_md, &md->part, part) {
2415 mmc_queue_suspend(&part_md->queue);
2416 }
2417 }
2418 return 0;
2419 }
2420
2421 static void mmc_blk_shutdown(struct mmc_card *card)
2422 {
2423 _mmc_blk_suspend(card);
2424 }
2425
2426 #ifdef CONFIG_PM
2427 static int mmc_blk_suspend(struct mmc_card *card)
2428 {
2429 return _mmc_blk_suspend(card);
2430 }
2431
2432 static int mmc_blk_resume(struct mmc_card *card)
2433 {
2434 struct mmc_blk_data *part_md;
2435 struct mmc_blk_data *md = mmc_get_drvdata(card);
2436
2437 if (md) {
2438 /*
2439 * Resume involves the card going into idle state,
2440 * so current partition is always the main one.
2441 */
2442 md->part_curr = md->part_type;
2443 mmc_queue_resume(&md->queue);
2444 list_for_each_entry(part_md, &md->part, part) {
2445 mmc_queue_resume(&part_md->queue);
2446 }
2447 pm_runtime_put(&card->dev);
2448 }
2449 return 0;
2450 }
2451 #else
2452 #define mmc_blk_suspend NULL
2453 #define mmc_blk_resume NULL
2454 #endif
2455
2456 static struct mmc_driver mmc_driver = {
2457 .drv = {
2458 .name = "mmcblk",
2459 },
2460 .probe = mmc_blk_probe,
2461 .remove = mmc_blk_remove,
2462 .suspend = mmc_blk_suspend,
2463 .resume = mmc_blk_resume,
2464 .shutdown = mmc_blk_shutdown,
2465 };
2466
2467 static int __init mmc_blk_init(void)
2468 {
2469 int res;
2470
2471 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2472 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2473
2474 max_devices = 256 / perdev_minors;
2475
2476 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2477 if (res)
2478 goto out;
2479
2480 res = mmc_register_driver(&mmc_driver);
2481 if (res)
2482 goto out2;
2483
2484 return 0;
2485 out2:
2486 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2487 out:
2488 return res;
2489 }
2490
2491 static void __exit mmc_blk_exit(void)
2492 {
2493 mmc_unregister_driver(&mmc_driver);
2494 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2495 }
2496
2497 module_init(mmc_blk_init);
2498 module_exit(mmc_blk_exit);
2499
2500 MODULE_LICENSE("GPL");
2501 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2502