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/cdev.h>
32 #include <linux/mutex.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35 #include <linux/delay.h>
36 #include <linux/capability.h>
37 #include <linux/compat.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/idr.h>
40 #include <linux/debugfs.h>
41
42 #include <linux/mmc/ioctl.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/mmc.h>
46 #include <linux/mmc/sd.h>
47
48 #include <linux/uaccess.h>
49
50 #include "queue.h"
51 #include "block.h"
52 #include "core.h"
53 #include "card.h"
54 #include "host.h"
55 #include "bus.h"
56 #include "mmc_ops.h"
57 #include "quirks.h"
58 #include "sd_ops.h"
59
60 MODULE_ALIAS("mmc:block");
61 #ifdef MODULE_PARAM_PREFIX
62 #undef MODULE_PARAM_PREFIX
63 #endif
64 #define MODULE_PARAM_PREFIX "mmcblk."
65
66 /*
67 * Set a 10 second timeout for polling write request busy state. Note, mmc core
68 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
69 * second software timer to timeout the whole request, so 10 seconds should be
70 * ample.
71 */
72 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
73 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
74 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
75
76 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
77 (rq_data_dir(req) == WRITE))
78 static DEFINE_MUTEX(block_mutex);
79
80 /*
81 * The defaults come from config options but can be overriden by module
82 * or bootarg options.
83 */
84 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
85
86 /*
87 * We've only got one major, so number of mmcblk devices is
88 * limited to (1 << 20) / number of minors per device. It is also
89 * limited by the MAX_DEVICES below.
90 */
91 static int max_devices;
92
93 #define MAX_DEVICES 256
94
95 static DEFINE_IDA(mmc_blk_ida);
96 static DEFINE_IDA(mmc_rpmb_ida);
97
98 /*
99 * There is one mmc_blk_data per slot.
100 */
101 struct mmc_blk_data {
102 struct device *parent;
103 struct gendisk *disk;
104 struct mmc_queue queue;
105 struct list_head part;
106 struct list_head rpmbs;
107
108 unsigned int flags;
109 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
110 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
111
112 unsigned int usage;
113 unsigned int read_only;
114 unsigned int part_type;
115 unsigned int reset_done;
116 #define MMC_BLK_READ BIT(0)
117 #define MMC_BLK_WRITE BIT(1)
118 #define MMC_BLK_DISCARD BIT(2)
119 #define MMC_BLK_SECDISCARD BIT(3)
120 #define MMC_BLK_CQE_RECOVERY BIT(4)
121
122 /*
123 * Only set in main mmc_blk_data associated
124 * with mmc_card with dev_set_drvdata, and keeps
125 * track of the current selected device partition.
126 */
127 unsigned int part_curr;
128 struct device_attribute force_ro;
129 struct device_attribute power_ro_lock;
130 int area_type;
131
132 /* debugfs files (only in main mmc_blk_data) */
133 struct dentry *status_dentry;
134 struct dentry *ext_csd_dentry;
135 };
136
137 /* Device type for RPMB character devices */
138 static dev_t mmc_rpmb_devt;
139
140 /* Bus type for RPMB character devices */
141 static struct bus_type mmc_rpmb_bus_type = {
142 .name = "mmc_rpmb",
143 };
144
145 /**
146 * struct mmc_rpmb_data - special RPMB device type for these areas
147 * @dev: the device for the RPMB area
148 * @chrdev: character device for the RPMB area
149 * @id: unique device ID number
150 * @part_index: partition index (0 on first)
151 * @md: parent MMC block device
152 * @node: list item, so we can put this device on a list
153 */
154 struct mmc_rpmb_data {
155 struct device dev;
156 struct cdev chrdev;
157 int id;
158 unsigned int part_index;
159 struct mmc_blk_data *md;
160 struct list_head node;
161 };
162
163 static DEFINE_MUTEX(open_lock);
164
165 module_param(perdev_minors, int, 0444);
166 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
167
168 static inline int mmc_blk_part_switch(struct mmc_card *card,
169 unsigned int part_type);
170 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
171 struct mmc_card *card,
172 int recovery_mode,
173 struct mmc_queue *mq);
174 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
175
mmc_blk_get(struct gendisk *disk)176 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
177 {
178 struct mmc_blk_data *md;
179
180 mutex_lock(&open_lock);
181 md = disk->private_data;
182 if (md && md->usage == 0)
183 md = NULL;
184 if (md)
185 md->usage++;
186 mutex_unlock(&open_lock);
187
188 return md;
189 }
190
mmc_get_devidx(struct gendisk *disk)191 static inline int mmc_get_devidx(struct gendisk *disk)
192 {
193 int devidx = disk->first_minor / perdev_minors;
194 return devidx;
195 }
196
mmc_blk_put(struct mmc_blk_data *md)197 static void mmc_blk_put(struct mmc_blk_data *md)
198 {
199 mutex_lock(&open_lock);
200 md->usage--;
201 if (md->usage == 0) {
202 int devidx = mmc_get_devidx(md->disk);
203 blk_put_queue(md->queue.queue);
204 ida_simple_remove(&mmc_blk_ida, devidx);
205 put_disk(md->disk);
206 kfree(md);
207 }
208 mutex_unlock(&open_lock);
209 }
210
power_ro_lock_show(struct device *dev, struct device_attribute *attr, char *buf)211 static ssize_t power_ro_lock_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
213 {
214 int ret;
215 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
216 struct mmc_card *card = md->queue.card;
217 int locked = 0;
218
219 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
220 locked = 2;
221 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
222 locked = 1;
223
224 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
225
226 mmc_blk_put(md);
227
228 return ret;
229 }
230
power_ro_lock_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)231 static ssize_t power_ro_lock_store(struct device *dev,
232 struct device_attribute *attr, const char *buf, size_t count)
233 {
234 int ret;
235 struct mmc_blk_data *md, *part_md;
236 struct mmc_queue *mq;
237 struct request *req;
238 unsigned long set;
239
240 if (kstrtoul(buf, 0, &set))
241 return -EINVAL;
242
243 if (set != 1)
244 return count;
245
246 md = mmc_blk_get(dev_to_disk(dev));
247 mq = &md->queue;
248
249 /* Dispatch locking to the block layer */
250 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
251 if (IS_ERR(req)) {
252 count = PTR_ERR(req);
253 goto out_put;
254 }
255 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
256 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
257 blk_execute_rq(mq->queue, NULL, req, 0);
258 ret = req_to_mmc_queue_req(req)->drv_op_result;
259 blk_put_request(req);
260
261 if (!ret) {
262 pr_info("%s: Locking boot partition ro until next power on\n",
263 md->disk->disk_name);
264 set_disk_ro(md->disk, 1);
265
266 list_for_each_entry(part_md, &md->part, part)
267 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
268 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
269 set_disk_ro(part_md->disk, 1);
270 }
271 }
272 out_put:
273 mmc_blk_put(md);
274 return count;
275 }
276
force_ro_show(struct device *dev, struct device_attribute *attr, char *buf)277 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
278 char *buf)
279 {
280 int ret;
281 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
282
283 ret = snprintf(buf, PAGE_SIZE, "%d\n",
284 get_disk_ro(dev_to_disk(dev)) ^
285 md->read_only);
286 mmc_blk_put(md);
287 return ret;
288 }
289
force_ro_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)290 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
291 const char *buf, size_t count)
292 {
293 int ret;
294 char *end;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296 unsigned long set = simple_strtoul(buf, &end, 0);
297 if (end == buf) {
298 ret = -EINVAL;
299 goto out;
300 }
301
302 set_disk_ro(dev_to_disk(dev), set || md->read_only);
303 ret = count;
304 out:
305 mmc_blk_put(md);
306 return ret;
307 }
308
mmc_blk_open(struct block_device *bdev, fmode_t mode)309 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
310 {
311 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
312 int ret = -ENXIO;
313
314 mutex_lock(&block_mutex);
315 if (md) {
316 ret = 0;
317 if ((mode & FMODE_WRITE) && md->read_only) {
318 mmc_blk_put(md);
319 ret = -EROFS;
320 }
321 }
322 mutex_unlock(&block_mutex);
323
324 return ret;
325 }
326
mmc_blk_release(struct gendisk *disk, fmode_t mode)327 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
328 {
329 struct mmc_blk_data *md = disk->private_data;
330
331 mutex_lock(&block_mutex);
332 mmc_blk_put(md);
333 mutex_unlock(&block_mutex);
334 }
335
336 static int
mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)337 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
338 {
339 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
340 geo->heads = 4;
341 geo->sectors = 16;
342 return 0;
343 }
344
345 struct mmc_blk_ioc_data {
346 struct mmc_ioc_cmd ic;
347 unsigned char *buf;
348 u64 buf_bytes;
349 unsigned int flags;
350 #define MMC_BLK_IOC_DROP BIT(0) /* drop this mrq */
351 #define MMC_BLK_IOC_SBC BIT(1) /* use mrq.sbc */
352
353 struct mmc_rpmb_data *rpmb;
354 };
355
mmc_blk_ioctl_copy_from_user( struct mmc_ioc_cmd __user *user)356 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
357 struct mmc_ioc_cmd __user *user)
358 {
359 struct mmc_blk_ioc_data *idata;
360 int err;
361
362 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
363 if (!idata) {
364 err = -ENOMEM;
365 goto out;
366 }
367
368 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
369 err = -EFAULT;
370 goto idata_err;
371 }
372
373 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
374 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
375 err = -EOVERFLOW;
376 goto idata_err;
377 }
378
379 if (!idata->buf_bytes) {
380 idata->buf = NULL;
381 return idata;
382 }
383
384 idata->buf = memdup_user((void __user *)(unsigned long)
385 idata->ic.data_ptr, idata->buf_bytes);
386 if (IS_ERR(idata->buf)) {
387 err = PTR_ERR(idata->buf);
388 goto idata_err;
389 }
390
391 return idata;
392
393 idata_err:
394 kfree(idata);
395 out:
396 return ERR_PTR(err);
397 }
398
mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, struct mmc_blk_ioc_data *idata)399 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
400 struct mmc_blk_ioc_data *idata)
401 {
402 struct mmc_ioc_cmd *ic = &idata->ic;
403
404 if (copy_to_user(&(ic_ptr->response), ic->response,
405 sizeof(ic->response)))
406 return -EFAULT;
407
408 if (!idata->ic.write_flag) {
409 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
410 idata->buf, idata->buf_bytes))
411 return -EFAULT;
412 }
413
414 return 0;
415 }
416
card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, u32 *resp_errs)417 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
418 u32 *resp_errs)
419 {
420 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
421 int err = 0;
422 u32 status;
423
424 do {
425 bool done = time_after(jiffies, timeout);
426
427 err = __mmc_send_status(card, &status, 5);
428 if (err) {
429 dev_err(mmc_dev(card->host),
430 "error %d requesting status\n", err);
431 return err;
432 }
433
434 /* Accumulate any response error bits seen */
435 if (resp_errs)
436 *resp_errs |= status;
437
438 /*
439 * Timeout if the device never becomes ready for data and never
440 * leaves the program state.
441 */
442 if (done) {
443 dev_err(mmc_dev(card->host),
444 "Card stuck in wrong state! %s status: %#x\n",
445 __func__, status);
446 return -ETIMEDOUT;
447 }
448 } while (!mmc_ready_for_data(status));
449
450 return err;
451 }
452
__mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, struct mmc_blk_ioc_data **idatas, int i)453 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
454 struct mmc_blk_ioc_data **idatas, int i)
455 {
456 struct mmc_command cmd = {}, sbc = {};
457 struct mmc_data data = {};
458 struct mmc_request mrq = {};
459 struct scatterlist sg;
460 int err;
461 unsigned int target_part;
462 struct mmc_blk_ioc_data *idata = idatas[i];
463 struct mmc_blk_ioc_data *prev_idata = NULL;
464
465 if (!card || !md || !idata)
466 return -EINVAL;
467
468 if (idata->flags & MMC_BLK_IOC_DROP)
469 return 0;
470
471 if (idata->flags & MMC_BLK_IOC_SBC && i > 0)
472 prev_idata = idatas[i - 1];
473
474 /*
475 * The RPMB accesses comes in from the character device, so we
476 * need to target these explicitly. Else we just target the
477 * partition type for the block device the ioctl() was issued
478 * on.
479 */
480 if (idata->rpmb) {
481 /* Support multiple RPMB partitions */
482 target_part = idata->rpmb->part_index;
483 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
484 } else {
485 target_part = md->part_type;
486 }
487
488 cmd.opcode = idata->ic.opcode;
489 cmd.arg = idata->ic.arg;
490 cmd.flags = idata->ic.flags;
491
492 if (idata->buf_bytes) {
493 data.sg = &sg;
494 data.sg_len = 1;
495 data.blksz = idata->ic.blksz;
496 data.blocks = idata->ic.blocks;
497
498 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
499
500 if (idata->ic.write_flag)
501 data.flags = MMC_DATA_WRITE;
502 else
503 data.flags = MMC_DATA_READ;
504
505 /* data.flags must already be set before doing this. */
506 mmc_set_data_timeout(&data, card);
507
508 /* Allow overriding the timeout_ns for empirical tuning. */
509 if (idata->ic.data_timeout_ns)
510 data.timeout_ns = idata->ic.data_timeout_ns;
511
512 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
513 /*
514 * Pretend this is a data transfer and rely on the
515 * host driver to compute timeout. When all host
516 * drivers support cmd.cmd_timeout for R1B, this
517 * can be changed to:
518 *
519 * mrq.data = NULL;
520 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
521 */
522 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
523 }
524
525 mrq.data = &data;
526 }
527
528 mrq.cmd = &cmd;
529
530 err = mmc_blk_part_switch(card, target_part);
531 if (err)
532 return err;
533
534 if (idata->ic.is_acmd) {
535 err = mmc_app_cmd(card->host, card);
536 if (err)
537 return err;
538 }
539
540 if (idata->rpmb || prev_idata) {
541 sbc.opcode = MMC_SET_BLOCK_COUNT;
542 /*
543 * We don't do any blockcount validation because the max size
544 * may be increased by a future standard. We just copy the
545 * 'Reliable Write' bit here.
546 */
547 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
548 if (prev_idata)
549 sbc.arg = prev_idata->ic.arg;
550 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
551 mrq.sbc = &sbc;
552 }
553
554 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
555 (cmd.opcode == MMC_SWITCH))
556 return mmc_sanitize(card);
557
558 mmc_wait_for_req(card->host, &mrq);
559 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
560
561 if (prev_idata) {
562 memcpy(&prev_idata->ic.response, sbc.resp, sizeof(sbc.resp));
563 if (sbc.error) {
564 dev_err(mmc_dev(card->host), "%s: sbc error %d\n",
565 __func__, sbc.error);
566 return sbc.error;
567 }
568 }
569
570 if (cmd.error) {
571 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
572 __func__, cmd.error);
573 return cmd.error;
574 }
575 if (data.error) {
576 dev_err(mmc_dev(card->host), "%s: data error %d\n",
577 __func__, data.error);
578 return data.error;
579 }
580
581 /*
582 * Make sure the cache of the PARTITION_CONFIG register and
583 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
584 * changed it successfully.
585 */
586 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
587 (cmd.opcode == MMC_SWITCH)) {
588 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
589 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
590
591 /*
592 * Update cache so the next mmc_blk_part_switch call operates
593 * on up-to-date data.
594 */
595 card->ext_csd.part_config = value;
596 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
597 }
598
599 /*
600 * Make sure to update CACHE_CTRL in case it was changed. The cache
601 * will get turned back on if the card is re-initialized, e.g.
602 * suspend/resume or hw reset in recovery.
603 */
604 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
605 (cmd.opcode == MMC_SWITCH)) {
606 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
607
608 card->ext_csd.cache_ctrl = value;
609 }
610
611 /*
612 * According to the SD specs, some commands require a delay after
613 * issuing the command.
614 */
615 if (idata->ic.postsleep_min_us)
616 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
617
618 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
619 /*
620 * Ensure RPMB/R1B command has completed by polling CMD13
621 * "Send Status".
622 */
623 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
624 }
625
626 return err;
627 }
628
mmc_blk_ioctl_cmd(struct mmc_blk_data *md, struct mmc_ioc_cmd __user *ic_ptr, struct mmc_rpmb_data *rpmb)629 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
630 struct mmc_ioc_cmd __user *ic_ptr,
631 struct mmc_rpmb_data *rpmb)
632 {
633 struct mmc_blk_ioc_data *idata;
634 struct mmc_blk_ioc_data *idatas[1];
635 struct mmc_queue *mq;
636 struct mmc_card *card;
637 int err = 0, ioc_err = 0;
638 struct request *req;
639
640 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
641 if (IS_ERR(idata))
642 return PTR_ERR(idata);
643 /* This will be NULL on non-RPMB ioctl():s */
644 idata->rpmb = rpmb;
645
646 card = md->queue.card;
647 if (IS_ERR(card)) {
648 err = PTR_ERR(card);
649 goto cmd_done;
650 }
651
652 /*
653 * Dispatch the ioctl() into the block request queue.
654 */
655 mq = &md->queue;
656 req = blk_get_request(mq->queue,
657 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
658 if (IS_ERR(req)) {
659 err = PTR_ERR(req);
660 goto cmd_done;
661 }
662 idatas[0] = idata;
663 req_to_mmc_queue_req(req)->drv_op =
664 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
665 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
666 req_to_mmc_queue_req(req)->drv_op_data = idatas;
667 req_to_mmc_queue_req(req)->ioc_count = 1;
668 blk_execute_rq(mq->queue, NULL, req, 0);
669 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
670 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
671 blk_put_request(req);
672
673 cmd_done:
674 kfree(idata->buf);
675 kfree(idata);
676 return ioc_err ? ioc_err : err;
677 }
678
mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md, struct mmc_ioc_multi_cmd __user *user, struct mmc_rpmb_data *rpmb)679 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
680 struct mmc_ioc_multi_cmd __user *user,
681 struct mmc_rpmb_data *rpmb)
682 {
683 struct mmc_blk_ioc_data **idata = NULL;
684 struct mmc_ioc_cmd __user *cmds = user->cmds;
685 struct mmc_card *card;
686 struct mmc_queue *mq;
687 int i, err = 0, ioc_err = 0;
688 __u64 num_of_cmds;
689 struct request *req;
690
691 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
692 sizeof(num_of_cmds)))
693 return -EFAULT;
694
695 if (!num_of_cmds)
696 return 0;
697
698 if (num_of_cmds > MMC_IOC_MAX_CMDS)
699 return -EINVAL;
700
701 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
702 if (!idata)
703 return -ENOMEM;
704
705 for (i = 0; i < num_of_cmds; i++) {
706 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
707 if (IS_ERR(idata[i])) {
708 err = PTR_ERR(idata[i]);
709 num_of_cmds = i;
710 goto cmd_err;
711 }
712 /* This will be NULL on non-RPMB ioctl():s */
713 idata[i]->rpmb = rpmb;
714 }
715
716 card = md->queue.card;
717 if (IS_ERR(card)) {
718 err = PTR_ERR(card);
719 goto cmd_err;
720 }
721
722
723 /*
724 * Dispatch the ioctl()s into the block request queue.
725 */
726 mq = &md->queue;
727 req = blk_get_request(mq->queue,
728 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
729 if (IS_ERR(req)) {
730 err = PTR_ERR(req);
731 goto cmd_err;
732 }
733 req_to_mmc_queue_req(req)->drv_op =
734 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
735 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
736 req_to_mmc_queue_req(req)->drv_op_data = idata;
737 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
738 blk_execute_rq(mq->queue, NULL, req, 0);
739 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
740
741 /* copy to user if data and response */
742 for (i = 0; i < num_of_cmds && !err; i++)
743 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
744
745 blk_put_request(req);
746
747 cmd_err:
748 for (i = 0; i < num_of_cmds; i++) {
749 kfree(idata[i]->buf);
750 kfree(idata[i]);
751 }
752 kfree(idata);
753 return ioc_err ? ioc_err : err;
754 }
755
mmc_blk_check_blkdev(struct block_device *bdev)756 static int mmc_blk_check_blkdev(struct block_device *bdev)
757 {
758 /*
759 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
760 * whole block device, not on a partition. This prevents overspray
761 * between sibling partitions.
762 */
763 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
764 return -EPERM;
765 return 0;
766 }
767
mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)768 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
769 unsigned int cmd, unsigned long arg)
770 {
771 struct mmc_blk_data *md;
772 int ret;
773
774 switch (cmd) {
775 case MMC_IOC_CMD:
776 ret = mmc_blk_check_blkdev(bdev);
777 if (ret)
778 return ret;
779 md = mmc_blk_get(bdev->bd_disk);
780 if (!md)
781 return -EINVAL;
782 ret = mmc_blk_ioctl_cmd(md,
783 (struct mmc_ioc_cmd __user *)arg,
784 NULL);
785 mmc_blk_put(md);
786 return ret;
787 case MMC_IOC_MULTI_CMD:
788 ret = mmc_blk_check_blkdev(bdev);
789 if (ret)
790 return ret;
791 md = mmc_blk_get(bdev->bd_disk);
792 if (!md)
793 return -EINVAL;
794 ret = mmc_blk_ioctl_multi_cmd(md,
795 (struct mmc_ioc_multi_cmd __user *)arg,
796 NULL);
797 mmc_blk_put(md);
798 return ret;
799 default:
800 return -EINVAL;
801 }
802 }
803
804 #ifdef CONFIG_COMPAT
mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg)805 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
806 unsigned int cmd, unsigned long arg)
807 {
808 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
809 }
810 #endif
811
812 static const struct block_device_operations mmc_bdops = {
813 .open = mmc_blk_open,
814 .release = mmc_blk_release,
815 .getgeo = mmc_blk_getgeo,
816 .owner = THIS_MODULE,
817 .ioctl = mmc_blk_ioctl,
818 #ifdef CONFIG_COMPAT
819 .compat_ioctl = mmc_blk_compat_ioctl,
820 #endif
821 };
822
mmc_blk_part_switch_pre(struct mmc_card *card, unsigned int part_type)823 static int mmc_blk_part_switch_pre(struct mmc_card *card,
824 unsigned int part_type)
825 {
826 const unsigned int mask = EXT_CSD_PART_CONFIG_ACC_RPMB;
827 int ret = 0;
828
829 if ((part_type & mask) == mask) {
830 if (card->ext_csd.cmdq_en) {
831 ret = mmc_cmdq_disable(card);
832 if (ret)
833 return ret;
834 }
835 mmc_retune_pause(card->host);
836 }
837
838 return ret;
839 }
840
mmc_blk_part_switch_post(struct mmc_card *card, unsigned int part_type)841 static int mmc_blk_part_switch_post(struct mmc_card *card,
842 unsigned int part_type)
843 {
844 const unsigned int mask = EXT_CSD_PART_CONFIG_ACC_RPMB;
845 int ret = 0;
846
847 if ((part_type & mask) == mask) {
848 mmc_retune_unpause(card->host);
849 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
850 ret = mmc_cmdq_enable(card);
851 }
852
853 return ret;
854 }
855
mmc_blk_part_switch(struct mmc_card *card, unsigned int part_type)856 static inline int mmc_blk_part_switch(struct mmc_card *card,
857 unsigned int part_type)
858 {
859 int ret = 0;
860 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
861
862 if (main_md->part_curr == part_type)
863 return 0;
864
865 if (mmc_card_mmc(card)) {
866 u8 part_config = card->ext_csd.part_config;
867
868 ret = mmc_blk_part_switch_pre(card, part_type);
869 if (ret)
870 return ret;
871
872 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
873 part_config |= part_type;
874
875 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
876 EXT_CSD_PART_CONFIG, part_config,
877 card->ext_csd.part_time);
878 if (ret) {
879 mmc_blk_part_switch_post(card, part_type);
880 return ret;
881 }
882
883 card->ext_csd.part_config = part_config;
884
885 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
886 }
887
888 main_md->part_curr = part_type;
889 return ret;
890 }
891
mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)892 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
893 {
894 int err;
895 u32 result;
896 __be32 *blocks;
897
898 struct mmc_request mrq = {};
899 struct mmc_command cmd = {};
900 struct mmc_data data = {};
901
902 struct scatterlist sg;
903
904 cmd.opcode = MMC_APP_CMD;
905 cmd.arg = card->rca << 16;
906 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
907
908 err = mmc_wait_for_cmd(card->host, &cmd, 0);
909 if (err)
910 return err;
911 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
912 return -EIO;
913
914 memset(&cmd, 0, sizeof(struct mmc_command));
915
916 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
917 cmd.arg = 0;
918 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
919
920 data.blksz = 4;
921 data.blocks = 1;
922 data.flags = MMC_DATA_READ;
923 data.sg = &sg;
924 data.sg_len = 1;
925 mmc_set_data_timeout(&data, card);
926
927 mrq.cmd = &cmd;
928 mrq.data = &data;
929
930 blocks = kmalloc(4, GFP_KERNEL);
931 if (!blocks)
932 return -ENOMEM;
933
934 sg_init_one(&sg, blocks, 4);
935
936 mmc_wait_for_req(card->host, &mrq);
937
938 result = ntohl(*blocks);
939 kfree(blocks);
940
941 if (cmd.error || data.error)
942 return -EIO;
943
944 *written_blocks = result;
945
946 return 0;
947 }
948
mmc_blk_clock_khz(struct mmc_host *host)949 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
950 {
951 if (host->actual_clock)
952 return host->actual_clock / 1000;
953
954 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
955 if (host->ios.clock)
956 return host->ios.clock / 2000;
957
958 /* How can there be no clock */
959 WARN_ON_ONCE(1);
960 return 100; /* 100 kHz is minimum possible value */
961 }
962
mmc_blk_data_timeout_ms(struct mmc_host *host, struct mmc_data *data)963 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
964 struct mmc_data *data)
965 {
966 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
967 unsigned int khz;
968
969 if (data->timeout_clks) {
970 khz = mmc_blk_clock_khz(host);
971 ms += DIV_ROUND_UP(data->timeout_clks, khz);
972 }
973
974 return ms;
975 }
976
mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, int type)977 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
978 int type)
979 {
980 int err;
981
982 if (md->reset_done & type)
983 return -EEXIST;
984
985 md->reset_done |= type;
986 err = mmc_hw_reset(host);
987 /* Ensure we switch back to the correct partition */
988 if (err != -EOPNOTSUPP) {
989 struct mmc_blk_data *main_md =
990 dev_get_drvdata(&host->card->dev);
991 int part_err;
992
993 main_md->part_curr = main_md->part_type;
994 part_err = mmc_blk_part_switch(host->card, md->part_type);
995 if (part_err) {
996 /*
997 * We have failed to get back into the correct
998 * partition, so we need to abort the whole request.
999 */
1000 return -ENODEV;
1001 }
1002 }
1003 return err;
1004 }
1005
mmc_blk_reset_success(struct mmc_blk_data *md, int type)1006 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1007 {
1008 md->reset_done &= ~type;
1009 }
1010
mmc_blk_check_sbc(struct mmc_queue_req *mq_rq)1011 static void mmc_blk_check_sbc(struct mmc_queue_req *mq_rq)
1012 {
1013 struct mmc_blk_ioc_data **idata = mq_rq->drv_op_data;
1014 int i;
1015
1016 for (i = 1; i < mq_rq->ioc_count; i++) {
1017 if (idata[i - 1]->ic.opcode == MMC_SET_BLOCK_COUNT &&
1018 mmc_op_multi(idata[i]->ic.opcode)) {
1019 idata[i - 1]->flags |= MMC_BLK_IOC_DROP;
1020 idata[i]->flags |= MMC_BLK_IOC_SBC;
1021 }
1022 }
1023 }
1024
1025 /*
1026 * The non-block commands come back from the block layer after it queued it and
1027 * processed it with all other requests and then they get issued in this
1028 * function.
1029 */
mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)1030 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1031 {
1032 struct mmc_queue_req *mq_rq;
1033 struct mmc_card *card = mq->card;
1034 struct mmc_blk_data *md = mq->blkdata;
1035 struct mmc_blk_ioc_data **idata;
1036 bool rpmb_ioctl;
1037 u8 **ext_csd;
1038 u32 status;
1039 int ret;
1040 int i;
1041
1042 mq_rq = req_to_mmc_queue_req(req);
1043 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1044
1045 switch (mq_rq->drv_op) {
1046 case MMC_DRV_OP_IOCTL:
1047 if (card->ext_csd.cmdq_en) {
1048 ret = mmc_cmdq_disable(card);
1049 if (ret)
1050 break;
1051 }
1052
1053 mmc_blk_check_sbc(mq_rq);
1054
1055 fallthrough;
1056 case MMC_DRV_OP_IOCTL_RPMB:
1057 idata = mq_rq->drv_op_data;
1058 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1059 ret = __mmc_blk_ioctl_cmd(card, md, idata, i);
1060 if (ret)
1061 break;
1062 }
1063 /* Always switch back to main area after RPMB access */
1064 if (rpmb_ioctl)
1065 mmc_blk_part_switch(card, 0);
1066 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1067 mmc_cmdq_enable(card);
1068 break;
1069 case MMC_DRV_OP_BOOT_WP:
1070 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1071 card->ext_csd.boot_ro_lock |
1072 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1073 card->ext_csd.part_time);
1074 if (ret)
1075 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1076 md->disk->disk_name, ret);
1077 else
1078 card->ext_csd.boot_ro_lock |=
1079 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1080 break;
1081 case MMC_DRV_OP_GET_CARD_STATUS:
1082 ret = mmc_send_status(card, &status);
1083 if (!ret)
1084 ret = status;
1085 break;
1086 case MMC_DRV_OP_GET_EXT_CSD:
1087 ext_csd = mq_rq->drv_op_data;
1088 ret = mmc_get_ext_csd(card, ext_csd);
1089 break;
1090 default:
1091 pr_err("%s: unknown driver specific operation\n",
1092 md->disk->disk_name);
1093 ret = -EINVAL;
1094 break;
1095 }
1096 mq_rq->drv_op_result = ret;
1097 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1098 }
1099
mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)1100 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1101 {
1102 struct mmc_blk_data *md = mq->blkdata;
1103 struct mmc_card *card = md->queue.card;
1104 unsigned int from, nr;
1105 int err = 0, type = MMC_BLK_DISCARD;
1106 blk_status_t status = BLK_STS_OK;
1107
1108 if (!mmc_can_erase(card)) {
1109 status = BLK_STS_NOTSUPP;
1110 goto fail;
1111 }
1112
1113 from = blk_rq_pos(req);
1114 nr = blk_rq_sectors(req);
1115
1116 do {
1117 unsigned int erase_arg = card->erase_arg;
1118
1119 if (mmc_card_broken_sd_discard(card))
1120 erase_arg = SD_ERASE_ARG;
1121
1122 err = 0;
1123 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1124 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1125 INAND_CMD38_ARG_EXT_CSD,
1126 card->erase_arg == MMC_TRIM_ARG ?
1127 INAND_CMD38_ARG_TRIM :
1128 INAND_CMD38_ARG_ERASE,
1129 card->ext_csd.generic_cmd6_time);
1130 }
1131 if (!err)
1132 err = mmc_erase(card, from, nr, erase_arg);
1133 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1134 if (err)
1135 status = BLK_STS_IOERR;
1136 else
1137 mmc_blk_reset_success(md, type);
1138 fail:
1139 blk_mq_end_request(req, status);
1140 }
1141
mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, struct request *req)1142 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1143 struct request *req)
1144 {
1145 struct mmc_blk_data *md = mq->blkdata;
1146 struct mmc_card *card = md->queue.card;
1147 unsigned int from, nr, arg;
1148 int err = 0, type = MMC_BLK_SECDISCARD;
1149 blk_status_t status = BLK_STS_OK;
1150
1151 if (!(mmc_can_secure_erase_trim(card))) {
1152 status = BLK_STS_NOTSUPP;
1153 goto out;
1154 }
1155
1156 from = blk_rq_pos(req);
1157 nr = blk_rq_sectors(req);
1158
1159 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1160 arg = MMC_SECURE_TRIM1_ARG;
1161 else
1162 arg = MMC_SECURE_ERASE_ARG;
1163
1164 retry:
1165 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1166 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1167 INAND_CMD38_ARG_EXT_CSD,
1168 arg == MMC_SECURE_TRIM1_ARG ?
1169 INAND_CMD38_ARG_SECTRIM1 :
1170 INAND_CMD38_ARG_SECERASE,
1171 card->ext_csd.generic_cmd6_time);
1172 if (err)
1173 goto out_retry;
1174 }
1175
1176 err = mmc_erase(card, from, nr, arg);
1177 if (err == -EIO)
1178 goto out_retry;
1179 if (err) {
1180 status = BLK_STS_IOERR;
1181 goto out;
1182 }
1183
1184 if (arg == MMC_SECURE_TRIM1_ARG) {
1185 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1186 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1187 INAND_CMD38_ARG_EXT_CSD,
1188 INAND_CMD38_ARG_SECTRIM2,
1189 card->ext_csd.generic_cmd6_time);
1190 if (err)
1191 goto out_retry;
1192 }
1193
1194 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1195 if (err == -EIO)
1196 goto out_retry;
1197 if (err) {
1198 status = BLK_STS_IOERR;
1199 goto out;
1200 }
1201 }
1202
1203 out_retry:
1204 if (err && !mmc_blk_reset(md, card->host, type))
1205 goto retry;
1206 if (!err)
1207 mmc_blk_reset_success(md, type);
1208 out:
1209 blk_mq_end_request(req, status);
1210 }
1211
mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)1212 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1213 {
1214 struct mmc_blk_data *md = mq->blkdata;
1215 struct mmc_card *card = md->queue.card;
1216 int ret = 0;
1217
1218 ret = mmc_flush_cache(card);
1219 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1220 }
1221
1222 /*
1223 * Reformat current write as a reliable write, supporting
1224 * both legacy and the enhanced reliable write MMC cards.
1225 * In each transfer we'll handle only as much as a single
1226 * reliable write can handle, thus finish the request in
1227 * partial completions.
1228 */
mmc_apply_rel_rw(struct mmc_blk_request *brq, struct mmc_card *card, struct request *req)1229 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1230 struct mmc_card *card,
1231 struct request *req)
1232 {
1233 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1234 /* Legacy mode imposes restrictions on transfers. */
1235 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1236 brq->data.blocks = 1;
1237
1238 if (brq->data.blocks > card->ext_csd.rel_sectors)
1239 brq->data.blocks = card->ext_csd.rel_sectors;
1240 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1241 brq->data.blocks = 1;
1242 }
1243 }
1244
1245 #define CMD_ERRORS_EXCL_OOR \
1246 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1247 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1248 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1249 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1250 R1_CC_ERROR | /* Card controller error */ \
1251 R1_ERROR) /* General/unknown error */
1252
1253 #define CMD_ERRORS \
1254 (CMD_ERRORS_EXCL_OOR | \
1255 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1256
mmc_blk_eval_resp_error(struct mmc_blk_request *brq)1257 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1258 {
1259 u32 val;
1260
1261 /*
1262 * Per the SD specification(physical layer version 4.10)[1],
1263 * section 4.3.3, it explicitly states that "When the last
1264 * block of user area is read using CMD18, the host should
1265 * ignore OUT_OF_RANGE error that may occur even the sequence
1266 * is correct". And JESD84-B51 for eMMC also has a similar
1267 * statement on section 6.8.3.
1268 *
1269 * Multiple block read/write could be done by either predefined
1270 * method, namely CMD23, or open-ending mode. For open-ending mode,
1271 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1272 *
1273 * However the spec[1] doesn't tell us whether we should also
1274 * ignore that for predefined method. But per the spec[1], section
1275 * 4.15 Set Block Count Command, it says"If illegal block count
1276 * is set, out of range error will be indicated during read/write
1277 * operation (For example, data transfer is stopped at user area
1278 * boundary)." In another word, we could expect a out of range error
1279 * in the response for the following CMD18/25. And if argument of
1280 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1281 * we could also expect to get a -ETIMEDOUT or any error number from
1282 * the host drivers due to missing data response(for write)/data(for
1283 * read), as the cards will stop the data transfer by itself per the
1284 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1285 */
1286
1287 if (!brq->stop.error) {
1288 bool oor_with_open_end;
1289 /* If there is no error yet, check R1 response */
1290
1291 val = brq->stop.resp[0] & CMD_ERRORS;
1292 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1293
1294 if (val && !oor_with_open_end)
1295 brq->stop.error = -EIO;
1296 }
1297 }
1298
mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, int recovery_mode, bool *do_rel_wr_p, bool *do_data_tag_p)1299 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1300 int recovery_mode, bool *do_rel_wr_p,
1301 bool *do_data_tag_p)
1302 {
1303 struct mmc_blk_data *md = mq->blkdata;
1304 struct mmc_card *card = md->queue.card;
1305 struct mmc_blk_request *brq = &mqrq->brq;
1306 struct request *req = mmc_queue_req_to_req(mqrq);
1307 bool do_rel_wr, do_data_tag;
1308
1309 /*
1310 * Reliable writes are used to implement Forced Unit Access and
1311 * are supported only on MMCs.
1312 */
1313 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1314 rq_data_dir(req) == WRITE &&
1315 (md->flags & MMC_BLK_REL_WR);
1316
1317 memset(brq, 0, sizeof(struct mmc_blk_request));
1318
1319 brq->mrq.data = &brq->data;
1320 brq->mrq.tag = req->tag;
1321
1322 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1323 brq->stop.arg = 0;
1324
1325 if (rq_data_dir(req) == READ) {
1326 brq->data.flags = MMC_DATA_READ;
1327 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1328 } else {
1329 brq->data.flags = MMC_DATA_WRITE;
1330 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1331 }
1332
1333 brq->data.blksz = 512;
1334 brq->data.blocks = blk_rq_sectors(req);
1335 brq->data.blk_addr = blk_rq_pos(req);
1336
1337 /*
1338 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1339 * The eMMC will give "high" priority tasks priority over "simple"
1340 * priority tasks. Here we always set "simple" priority by not setting
1341 * MMC_DATA_PRIO.
1342 */
1343
1344 /*
1345 * The block layer doesn't support all sector count
1346 * restrictions, so we need to be prepared for too big
1347 * requests.
1348 */
1349 if (brq->data.blocks > card->host->max_blk_count)
1350 brq->data.blocks = card->host->max_blk_count;
1351
1352 if (brq->data.blocks > 1) {
1353 /*
1354 * Some SD cards in SPI mode return a CRC error or even lock up
1355 * completely when trying to read the last block using a
1356 * multiblock read command.
1357 */
1358 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1359 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1360 get_capacity(md->disk)))
1361 brq->data.blocks--;
1362
1363 /*
1364 * After a read error, we redo the request one (native) sector
1365 * at a time in order to accurately determine which
1366 * sectors can be read successfully.
1367 */
1368 if (recovery_mode)
1369 brq->data.blocks = queue_physical_block_size(mq->queue) >> 9;
1370
1371 /*
1372 * Some controllers have HW issues while operating
1373 * in multiple I/O mode
1374 */
1375 if (card->host->ops->multi_io_quirk)
1376 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1377 (rq_data_dir(req) == READ) ?
1378 MMC_DATA_READ : MMC_DATA_WRITE,
1379 brq->data.blocks);
1380 }
1381
1382 if (do_rel_wr) {
1383 mmc_apply_rel_rw(brq, card, req);
1384 brq->data.flags |= MMC_DATA_REL_WR;
1385 }
1386
1387 /*
1388 * Data tag is used only during writing meta data to speed
1389 * up write and any subsequent read of this meta data
1390 */
1391 do_data_tag = card->ext_csd.data_tag_unit_size &&
1392 (req->cmd_flags & REQ_META) &&
1393 (rq_data_dir(req) == WRITE) &&
1394 ((brq->data.blocks * brq->data.blksz) >=
1395 card->ext_csd.data_tag_unit_size);
1396
1397 if (do_data_tag)
1398 brq->data.flags |= MMC_DATA_DAT_TAG;
1399
1400 mmc_set_data_timeout(&brq->data, card);
1401
1402 brq->data.sg = mqrq->sg;
1403 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1404
1405 /*
1406 * Adjust the sg list so it is the same size as the
1407 * request.
1408 */
1409 if (brq->data.blocks != blk_rq_sectors(req)) {
1410 int i, data_size = brq->data.blocks << 9;
1411 struct scatterlist *sg;
1412
1413 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1414 data_size -= sg->length;
1415 if (data_size <= 0) {
1416 sg->length += data_size;
1417 i++;
1418 break;
1419 }
1420 }
1421 brq->data.sg_len = i;
1422 }
1423
1424 if (do_rel_wr_p)
1425 *do_rel_wr_p = do_rel_wr;
1426
1427 if (do_data_tag_p)
1428 *do_data_tag_p = do_data_tag;
1429 }
1430
1431 #define MMC_CQE_RETRIES 2
1432
mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)1433 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1434 {
1435 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1436 struct mmc_request *mrq = &mqrq->brq.mrq;
1437 struct request_queue *q = req->q;
1438 struct mmc_host *host = mq->card->host;
1439 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1440 unsigned long flags;
1441 bool put_card;
1442 int err;
1443
1444 mmc_cqe_post_req(host, mrq);
1445
1446 if (mrq->cmd && mrq->cmd->error)
1447 err = mrq->cmd->error;
1448 else if (mrq->data && mrq->data->error)
1449 err = mrq->data->error;
1450 else
1451 err = 0;
1452
1453 if (err) {
1454 if (mqrq->retries++ < MMC_CQE_RETRIES)
1455 blk_mq_requeue_request(req, true);
1456 else
1457 blk_mq_end_request(req, BLK_STS_IOERR);
1458 } else if (mrq->data) {
1459 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1460 blk_mq_requeue_request(req, true);
1461 else
1462 __blk_mq_end_request(req, BLK_STS_OK);
1463 } else if (mq->in_recovery) {
1464 blk_mq_requeue_request(req, true);
1465 } else {
1466 blk_mq_end_request(req, BLK_STS_OK);
1467 }
1468
1469 spin_lock_irqsave(&mq->lock, flags);
1470
1471 mq->in_flight[issue_type] -= 1;
1472
1473 put_card = (mmc_tot_in_flight(mq) == 0);
1474
1475 mmc_cqe_check_busy(mq);
1476
1477 spin_unlock_irqrestore(&mq->lock, flags);
1478
1479 if (!mq->cqe_busy)
1480 blk_mq_run_hw_queues(q, true);
1481
1482 if (put_card)
1483 mmc_put_card(mq->card, &mq->ctx);
1484 }
1485
mmc_blk_cqe_recovery(struct mmc_queue *mq)1486 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1487 {
1488 struct mmc_card *card = mq->card;
1489 struct mmc_host *host = card->host;
1490 int err;
1491
1492 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1493
1494 err = mmc_cqe_recovery(host);
1495 if (err)
1496 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1497 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1498
1499 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1500 }
1501
mmc_blk_cqe_req_done(struct mmc_request *mrq)1502 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1503 {
1504 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1505 brq.mrq);
1506 struct request *req = mmc_queue_req_to_req(mqrq);
1507 struct request_queue *q = req->q;
1508 struct mmc_queue *mq = q->queuedata;
1509
1510 /*
1511 * Block layer timeouts race with completions which means the normal
1512 * completion path cannot be used during recovery.
1513 */
1514 if (mq->in_recovery)
1515 mmc_blk_cqe_complete_rq(mq, req);
1516 else if (likely(!blk_should_fake_timeout(req->q)))
1517 blk_mq_complete_request(req);
1518 }
1519
mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)1520 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1521 {
1522 mrq->done = mmc_blk_cqe_req_done;
1523 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1524
1525 return mmc_cqe_start_req(host, mrq);
1526 }
1527
mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, struct request *req)1528 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1529 struct request *req)
1530 {
1531 struct mmc_blk_request *brq = &mqrq->brq;
1532
1533 memset(brq, 0, sizeof(*brq));
1534
1535 brq->mrq.cmd = &brq->cmd;
1536 brq->mrq.tag = req->tag;
1537
1538 return &brq->mrq;
1539 }
1540
mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)1541 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1542 {
1543 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1544 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1545
1546 mrq->cmd->opcode = MMC_SWITCH;
1547 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1548 (EXT_CSD_FLUSH_CACHE << 16) |
1549 (1 << 8) |
1550 EXT_CSD_CMD_SET_NORMAL;
1551 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1552
1553 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1554 }
1555
mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)1556 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1557 {
1558 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1559 struct mmc_host *host = mq->card->host;
1560 int err;
1561
1562 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1563 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1564 mmc_pre_req(host, &mqrq->brq.mrq);
1565
1566 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1567 if (err)
1568 mmc_post_req(host, &mqrq->brq.mrq, err);
1569
1570 return err;
1571 }
1572
mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)1573 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1574 {
1575 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1576 struct mmc_host *host = mq->card->host;
1577
1578 if (host->hsq_enabled)
1579 return mmc_blk_hsq_issue_rw_rq(mq, req);
1580
1581 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1582
1583 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1584 }
1585
mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, struct mmc_card *card, int recovery_mode, struct mmc_queue *mq)1586 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1587 struct mmc_card *card,
1588 int recovery_mode,
1589 struct mmc_queue *mq)
1590 {
1591 u32 readcmd, writecmd;
1592 struct mmc_blk_request *brq = &mqrq->brq;
1593 struct request *req = mmc_queue_req_to_req(mqrq);
1594 struct mmc_blk_data *md = mq->blkdata;
1595 bool do_rel_wr, do_data_tag;
1596
1597 mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag);
1598
1599 brq->mrq.cmd = &brq->cmd;
1600
1601 brq->cmd.arg = blk_rq_pos(req);
1602 if (!mmc_card_blockaddr(card))
1603 brq->cmd.arg <<= 9;
1604 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1605
1606 if (brq->data.blocks > 1 || do_rel_wr) {
1607 /* SPI multiblock writes terminate using a special
1608 * token, not a STOP_TRANSMISSION request.
1609 */
1610 if (!mmc_host_is_spi(card->host) ||
1611 rq_data_dir(req) == READ)
1612 brq->mrq.stop = &brq->stop;
1613 readcmd = MMC_READ_MULTIPLE_BLOCK;
1614 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1615 } else {
1616 brq->mrq.stop = NULL;
1617 readcmd = MMC_READ_SINGLE_BLOCK;
1618 writecmd = MMC_WRITE_BLOCK;
1619 }
1620 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1621
1622 /*
1623 * Pre-defined multi-block transfers are preferable to
1624 * open ended-ones (and necessary for reliable writes).
1625 * However, it is not sufficient to just send CMD23,
1626 * and avoid the final CMD12, as on an error condition
1627 * CMD12 (stop) needs to be sent anyway. This, coupled
1628 * with Auto-CMD23 enhancements provided by some
1629 * hosts, means that the complexity of dealing
1630 * with this is best left to the host. If CMD23 is
1631 * supported by card and host, we'll fill sbc in and let
1632 * the host deal with handling it correctly. This means
1633 * that for hosts that don't expose MMC_CAP_CMD23, no
1634 * change of behavior will be observed.
1635 *
1636 * N.B: Some MMC cards experience perf degradation.
1637 * We'll avoid using CMD23-bounded multiblock writes for
1638 * these, while retaining features like reliable writes.
1639 */
1640 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1641 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1642 do_data_tag)) {
1643 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1644 brq->sbc.arg = brq->data.blocks |
1645 (do_rel_wr ? (1 << 31) : 0) |
1646 (do_data_tag ? (1 << 29) : 0);
1647 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1648 brq->mrq.sbc = &brq->sbc;
1649 }
1650 }
1651
1652 #define MMC_MAX_RETRIES 5
1653 #define MMC_DATA_RETRIES 2
1654 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1655
mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)1656 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1657 {
1658 struct mmc_command cmd = {
1659 .opcode = MMC_STOP_TRANSMISSION,
1660 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1661 /* Some hosts wait for busy anyway, so provide a busy timeout */
1662 .busy_timeout = timeout,
1663 };
1664
1665 return mmc_wait_for_cmd(card->host, &cmd, 5);
1666 }
1667
mmc_blk_fix_state(struct mmc_card *card, struct request *req)1668 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1669 {
1670 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1671 struct mmc_blk_request *brq = &mqrq->brq;
1672 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1673 int err;
1674
1675 mmc_retune_hold_now(card->host);
1676
1677 mmc_blk_send_stop(card, timeout);
1678
1679 err = card_busy_detect(card, timeout, NULL);
1680
1681 mmc_retune_release(card->host);
1682
1683 return err;
1684 }
1685
1686 #define MMC_READ_SINGLE_RETRIES 2
1687
1688 /* Single (native) sector read during recovery */
mmc_blk_read_single(struct mmc_queue *mq, struct request *req)1689 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1690 {
1691 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1692 struct mmc_request *mrq = &mqrq->brq.mrq;
1693 struct mmc_card *card = mq->card;
1694 struct mmc_host *host = card->host;
1695 blk_status_t error = BLK_STS_OK;
1696 size_t bytes_per_read = queue_physical_block_size(mq->queue);
1697
1698 do {
1699 u32 status;
1700 int err;
1701 int retries = 0;
1702
1703 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1704 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1705
1706 mmc_wait_for_req(host, mrq);
1707
1708 err = mmc_send_status(card, &status);
1709 if (err)
1710 goto error_exit;
1711
1712 if (!mmc_host_is_spi(host) &&
1713 !mmc_ready_for_data(status)) {
1714 err = mmc_blk_fix_state(card, req);
1715 if (err)
1716 goto error_exit;
1717 }
1718
1719 if (!mrq->cmd->error)
1720 break;
1721 }
1722
1723 if (mrq->cmd->error ||
1724 mrq->data->error ||
1725 (!mmc_host_is_spi(host) &&
1726 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1727 error = BLK_STS_IOERR;
1728 else
1729 error = BLK_STS_OK;
1730
1731 } while (blk_update_request(req, error, bytes_per_read));
1732
1733 return;
1734
1735 error_exit:
1736 mrq->data->bytes_xfered = 0;
1737 blk_update_request(req, BLK_STS_IOERR, bytes_per_read);
1738 /* Let it try the remaining request again */
1739 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1740 mqrq->retries = MMC_MAX_RETRIES - 1;
1741 }
1742
mmc_blk_oor_valid(struct mmc_blk_request *brq)1743 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1744 {
1745 return !!brq->mrq.sbc;
1746 }
1747
mmc_blk_stop_err_bits(struct mmc_blk_request *brq)1748 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1749 {
1750 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1751 }
1752
1753 /*
1754 * Check for errors the host controller driver might not have seen such as
1755 * response mode errors or invalid card state.
1756 */
mmc_blk_status_error(struct request *req, u32 status)1757 static bool mmc_blk_status_error(struct request *req, u32 status)
1758 {
1759 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1760 struct mmc_blk_request *brq = &mqrq->brq;
1761 struct mmc_queue *mq = req->q->queuedata;
1762 u32 stop_err_bits;
1763
1764 if (mmc_host_is_spi(mq->card->host))
1765 return false;
1766
1767 stop_err_bits = mmc_blk_stop_err_bits(brq);
1768
1769 return brq->cmd.resp[0] & CMD_ERRORS ||
1770 brq->stop.resp[0] & stop_err_bits ||
1771 status & stop_err_bits ||
1772 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1773 }
1774
mmc_blk_cmd_started(struct mmc_blk_request *brq)1775 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1776 {
1777 return !brq->sbc.error && !brq->cmd.error &&
1778 !(brq->cmd.resp[0] & CMD_ERRORS);
1779 }
1780
1781 /*
1782 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1783 * policy:
1784 * 1. A request that has transferred at least some data is considered
1785 * successful and will be requeued if there is remaining data to
1786 * transfer.
1787 * 2. Otherwise the number of retries is incremented and the request
1788 * will be requeued if there are remaining retries.
1789 * 3. Otherwise the request will be errored out.
1790 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1791 * mqrq->retries. So there are only 4 possible actions here:
1792 * 1. do not accept the bytes_xfered value i.e. set it to zero
1793 * 2. change mqrq->retries to determine the number of retries
1794 * 3. try to reset the card
1795 * 4. read one sector at a time
1796 */
mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)1797 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1798 {
1799 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1800 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1801 struct mmc_blk_request *brq = &mqrq->brq;
1802 struct mmc_blk_data *md = mq->blkdata;
1803 struct mmc_card *card = mq->card;
1804 u32 status;
1805 u32 blocks;
1806 int err;
1807
1808 /*
1809 * Some errors the host driver might not have seen. Set the number of
1810 * bytes transferred to zero in that case.
1811 */
1812 err = __mmc_send_status(card, &status, 0);
1813 if (err || mmc_blk_status_error(req, status))
1814 brq->data.bytes_xfered = 0;
1815
1816 mmc_retune_release(card->host);
1817
1818 /*
1819 * Try again to get the status. This also provides an opportunity for
1820 * re-tuning.
1821 */
1822 if (err)
1823 err = __mmc_send_status(card, &status, 0);
1824
1825 /*
1826 * Nothing more to do after the number of bytes transferred has been
1827 * updated and there is no card.
1828 */
1829 if (err && mmc_detect_card_removed(card->host))
1830 return;
1831
1832 /* Try to get back to "tran" state */
1833 if (!mmc_host_is_spi(mq->card->host) &&
1834 (err || !mmc_ready_for_data(status)))
1835 err = mmc_blk_fix_state(mq->card, req);
1836
1837 /*
1838 * Special case for SD cards where the card might record the number of
1839 * blocks written.
1840 */
1841 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1842 rq_data_dir(req) == WRITE) {
1843 if (mmc_sd_num_wr_blocks(card, &blocks))
1844 brq->data.bytes_xfered = 0;
1845 else
1846 brq->data.bytes_xfered = blocks << 9;
1847 }
1848
1849 /* Reset if the card is in a bad state */
1850 if (!mmc_host_is_spi(mq->card->host) &&
1851 err && mmc_blk_reset(md, card->host, type)) {
1852 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1853 mqrq->retries = MMC_NO_RETRIES;
1854 return;
1855 }
1856
1857 /*
1858 * If anything was done, just return and if there is anything remaining
1859 * on the request it will get requeued.
1860 */
1861 if (brq->data.bytes_xfered)
1862 return;
1863
1864 /* Reset before last retry */
1865 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1866 mmc_blk_reset(md, card->host, type);
1867
1868 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1869 if (brq->sbc.error || brq->cmd.error)
1870 return;
1871
1872 /* Reduce the remaining retries for data errors */
1873 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1874 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1875 return;
1876 }
1877
1878 if (rq_data_dir(req) == READ && brq->data.blocks >
1879 queue_physical_block_size(mq->queue) >> 9) {
1880 /* Read one (native) sector at a time */
1881 mmc_blk_read_single(mq, req);
1882 return;
1883 }
1884 }
1885
mmc_blk_rq_error(struct mmc_blk_request *brq)1886 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1887 {
1888 mmc_blk_eval_resp_error(brq);
1889
1890 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1891 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1892 }
1893
mmc_blk_card_busy(struct mmc_card *card, struct request *req)1894 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1895 {
1896 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1897 u32 status = 0;
1898 int err;
1899
1900 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1901 return 0;
1902
1903 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status);
1904
1905 /*
1906 * Do not assume data transferred correctly if there are any error bits
1907 * set.
1908 */
1909 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1910 mqrq->brq.data.bytes_xfered = 0;
1911 err = err ? err : -EIO;
1912 }
1913
1914 /* Copy the exception bit so it will be seen later on */
1915 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1916 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1917
1918 return err;
1919 }
1920
mmc_blk_rw_reset_success(struct mmc_queue *mq, struct request *req)1921 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1922 struct request *req)
1923 {
1924 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1925
1926 mmc_blk_reset_success(mq->blkdata, type);
1927 }
1928
mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)1929 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1930 {
1931 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1932 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1933
1934 if (nr_bytes) {
1935 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1936 blk_mq_requeue_request(req, true);
1937 else
1938 __blk_mq_end_request(req, BLK_STS_OK);
1939 } else if (!blk_rq_bytes(req)) {
1940 __blk_mq_end_request(req, BLK_STS_IOERR);
1941 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1942 blk_mq_requeue_request(req, true);
1943 } else {
1944 if (mmc_card_removed(mq->card))
1945 req->rq_flags |= RQF_QUIET;
1946 blk_mq_end_request(req, BLK_STS_IOERR);
1947 }
1948 }
1949
mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, struct mmc_queue_req *mqrq)1950 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1951 struct mmc_queue_req *mqrq)
1952 {
1953 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1954 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1955 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1956 }
1957
mmc_blk_urgent_bkops(struct mmc_queue *mq, struct mmc_queue_req *mqrq)1958 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1959 struct mmc_queue_req *mqrq)
1960 {
1961 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1962 mmc_run_bkops(mq->card);
1963 }
1964
mmc_blk_hsq_req_done(struct mmc_request *mrq)1965 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
1966 {
1967 struct mmc_queue_req *mqrq =
1968 container_of(mrq, struct mmc_queue_req, brq.mrq);
1969 struct request *req = mmc_queue_req_to_req(mqrq);
1970 struct request_queue *q = req->q;
1971 struct mmc_queue *mq = q->queuedata;
1972 struct mmc_host *host = mq->card->host;
1973 unsigned long flags;
1974
1975 if (mmc_blk_rq_error(&mqrq->brq) ||
1976 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
1977 spin_lock_irqsave(&mq->lock, flags);
1978 mq->recovery_needed = true;
1979 mq->recovery_req = req;
1980 spin_unlock_irqrestore(&mq->lock, flags);
1981
1982 host->cqe_ops->cqe_recovery_start(host);
1983
1984 schedule_work(&mq->recovery_work);
1985 return;
1986 }
1987
1988 mmc_blk_rw_reset_success(mq, req);
1989
1990 /*
1991 * Block layer timeouts race with completions which means the normal
1992 * completion path cannot be used during recovery.
1993 */
1994 if (mq->in_recovery)
1995 mmc_blk_cqe_complete_rq(mq, req);
1996 else if (likely(!blk_should_fake_timeout(req->q)))
1997 blk_mq_complete_request(req);
1998 }
1999
mmc_blk_mq_complete(struct request *req)2000 void mmc_blk_mq_complete(struct request *req)
2001 {
2002 struct mmc_queue *mq = req->q->queuedata;
2003
2004 if (mq->use_cqe)
2005 mmc_blk_cqe_complete_rq(mq, req);
2006 else if (likely(!blk_should_fake_timeout(req->q)))
2007 mmc_blk_mq_complete_rq(mq, req);
2008 }
2009
mmc_blk_mq_poll_completion(struct mmc_queue *mq, struct request *req)2010 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2011 struct request *req)
2012 {
2013 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2014 struct mmc_host *host = mq->card->host;
2015
2016 if (mmc_blk_rq_error(&mqrq->brq) ||
2017 mmc_blk_card_busy(mq->card, req)) {
2018 mmc_blk_mq_rw_recovery(mq, req);
2019 } else {
2020 mmc_blk_rw_reset_success(mq, req);
2021 mmc_retune_release(host);
2022 }
2023
2024 mmc_blk_urgent_bkops(mq, mqrq);
2025 }
2026
mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, enum mmc_issue_type issue_type)2027 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, enum mmc_issue_type issue_type)
2028 {
2029 unsigned long flags;
2030 bool put_card;
2031
2032 spin_lock_irqsave(&mq->lock, flags);
2033
2034 mq->in_flight[issue_type] -= 1;
2035
2036 put_card = (mmc_tot_in_flight(mq) == 0);
2037
2038 spin_unlock_irqrestore(&mq->lock, flags);
2039
2040 if (put_card)
2041 mmc_put_card(mq->card, &mq->ctx);
2042 }
2043
mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)2044 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2045 {
2046 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
2047 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2048 struct mmc_request *mrq = &mqrq->brq.mrq;
2049 struct mmc_host *host = mq->card->host;
2050
2051 mmc_post_req(host, mrq, 0);
2052
2053 /*
2054 * Block layer timeouts race with completions which means the normal
2055 * completion path cannot be used during recovery.
2056 */
2057 if (mq->in_recovery)
2058 mmc_blk_mq_complete_rq(mq, req);
2059 else if (likely(!blk_should_fake_timeout(req->q)))
2060 blk_mq_complete_request(req);
2061
2062 mmc_blk_mq_dec_in_flight(mq, issue_type);
2063 }
2064
mmc_blk_mq_recovery(struct mmc_queue *mq)2065 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2066 {
2067 struct request *req = mq->recovery_req;
2068 struct mmc_host *host = mq->card->host;
2069 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2070
2071 mq->recovery_req = NULL;
2072 mq->rw_wait = false;
2073
2074 if (mmc_blk_rq_error(&mqrq->brq)) {
2075 mmc_retune_hold_now(host);
2076 mmc_blk_mq_rw_recovery(mq, req);
2077 }
2078
2079 mmc_blk_urgent_bkops(mq, mqrq);
2080
2081 mmc_blk_mq_post_req(mq, req);
2082 }
2083
mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, struct request **prev_req)2084 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2085 struct request **prev_req)
2086 {
2087 if (mmc_host_done_complete(mq->card->host))
2088 return;
2089
2090 mutex_lock(&mq->complete_lock);
2091
2092 if (!mq->complete_req)
2093 goto out_unlock;
2094
2095 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2096
2097 if (prev_req)
2098 *prev_req = mq->complete_req;
2099 else
2100 mmc_blk_mq_post_req(mq, mq->complete_req);
2101
2102 mq->complete_req = NULL;
2103
2104 out_unlock:
2105 mutex_unlock(&mq->complete_lock);
2106 }
2107
mmc_blk_mq_complete_work(struct work_struct *work)2108 void mmc_blk_mq_complete_work(struct work_struct *work)
2109 {
2110 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2111 complete_work);
2112
2113 mmc_blk_mq_complete_prev_req(mq, NULL);
2114 }
2115
mmc_blk_mq_req_done(struct mmc_request *mrq)2116 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2117 {
2118 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2119 brq.mrq);
2120 struct request *req = mmc_queue_req_to_req(mqrq);
2121 struct request_queue *q = req->q;
2122 struct mmc_queue *mq = q->queuedata;
2123 struct mmc_host *host = mq->card->host;
2124 unsigned long flags;
2125
2126 if (!mmc_host_done_complete(host)) {
2127 bool waiting;
2128
2129 /*
2130 * We cannot complete the request in this context, so record
2131 * that there is a request to complete, and that a following
2132 * request does not need to wait (although it does need to
2133 * complete complete_req first).
2134 */
2135 spin_lock_irqsave(&mq->lock, flags);
2136 mq->complete_req = req;
2137 mq->rw_wait = false;
2138 waiting = mq->waiting;
2139 spin_unlock_irqrestore(&mq->lock, flags);
2140
2141 /*
2142 * If 'waiting' then the waiting task will complete this
2143 * request, otherwise queue a work to do it. Note that
2144 * complete_work may still race with the dispatch of a following
2145 * request.
2146 */
2147 if (waiting)
2148 wake_up(&mq->wait);
2149 else
2150 queue_work(mq->card->complete_wq, &mq->complete_work);
2151
2152 return;
2153 }
2154
2155 /* Take the recovery path for errors or urgent background operations */
2156 if (mmc_blk_rq_error(&mqrq->brq) ||
2157 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2158 spin_lock_irqsave(&mq->lock, flags);
2159 mq->recovery_needed = true;
2160 mq->recovery_req = req;
2161 spin_unlock_irqrestore(&mq->lock, flags);
2162 wake_up(&mq->wait);
2163 schedule_work(&mq->recovery_work);
2164 return;
2165 }
2166
2167 mmc_blk_rw_reset_success(mq, req);
2168
2169 mq->rw_wait = false;
2170 wake_up(&mq->wait);
2171
2172 mmc_blk_mq_post_req(mq, req);
2173 }
2174
mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)2175 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2176 {
2177 unsigned long flags;
2178 bool done;
2179
2180 /*
2181 * Wait while there is another request in progress, but not if recovery
2182 * is needed. Also indicate whether there is a request waiting to start.
2183 */
2184 spin_lock_irqsave(&mq->lock, flags);
2185 if (mq->recovery_needed) {
2186 *err = -EBUSY;
2187 done = true;
2188 } else {
2189 done = !mq->rw_wait;
2190 }
2191 mq->waiting = !done;
2192 spin_unlock_irqrestore(&mq->lock, flags);
2193
2194 return done;
2195 }
2196
mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)2197 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2198 {
2199 int err = 0;
2200
2201 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2202
2203 /* Always complete the previous request if there is one */
2204 mmc_blk_mq_complete_prev_req(mq, prev_req);
2205
2206 return err;
2207 }
2208
mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, struct request *req)2209 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2210 struct request *req)
2211 {
2212 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2213 struct mmc_host *host = mq->card->host;
2214 struct request *prev_req = NULL;
2215 int err = 0;
2216
2217 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2218
2219 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2220
2221 mmc_pre_req(host, &mqrq->brq.mrq);
2222
2223 err = mmc_blk_rw_wait(mq, &prev_req);
2224 if (err)
2225 goto out_post_req;
2226
2227 mq->rw_wait = true;
2228
2229 err = mmc_start_request(host, &mqrq->brq.mrq);
2230
2231 if (prev_req)
2232 mmc_blk_mq_post_req(mq, prev_req);
2233
2234 if (err)
2235 mq->rw_wait = false;
2236
2237 /* Release re-tuning here where there is no synchronization required */
2238 if (err || mmc_host_done_complete(host))
2239 mmc_retune_release(host);
2240
2241 out_post_req:
2242 if (err)
2243 mmc_post_req(host, &mqrq->brq.mrq, err);
2244
2245 return err;
2246 }
2247
mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)2248 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2249 {
2250 if (mq->use_cqe)
2251 return host->cqe_ops->cqe_wait_for_idle(host);
2252
2253 return mmc_blk_rw_wait(mq, NULL);
2254 }
2255
mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)2256 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2257 {
2258 struct mmc_blk_data *md = mq->blkdata;
2259 struct mmc_card *card = md->queue.card;
2260 struct mmc_host *host = card->host;
2261 int ret;
2262
2263 ret = mmc_blk_part_switch(card, md->part_type);
2264 if (ret)
2265 return MMC_REQ_FAILED_TO_START;
2266
2267 switch (mmc_issue_type(mq, req)) {
2268 case MMC_ISSUE_SYNC:
2269 ret = mmc_blk_wait_for_idle(mq, host);
2270 if (ret)
2271 return MMC_REQ_BUSY;
2272 switch (req_op(req)) {
2273 case REQ_OP_DRV_IN:
2274 case REQ_OP_DRV_OUT:
2275 mmc_blk_issue_drv_op(mq, req);
2276 break;
2277 case REQ_OP_DISCARD:
2278 mmc_blk_issue_discard_rq(mq, req);
2279 break;
2280 case REQ_OP_SECURE_ERASE:
2281 mmc_blk_issue_secdiscard_rq(mq, req);
2282 break;
2283 case REQ_OP_FLUSH:
2284 mmc_blk_issue_flush(mq, req);
2285 break;
2286 default:
2287 WARN_ON_ONCE(1);
2288 return MMC_REQ_FAILED_TO_START;
2289 }
2290 return MMC_REQ_FINISHED;
2291 case MMC_ISSUE_DCMD:
2292 case MMC_ISSUE_ASYNC:
2293 switch (req_op(req)) {
2294 case REQ_OP_FLUSH:
2295 if (!mmc_cache_enabled(host)) {
2296 blk_mq_end_request(req, BLK_STS_OK);
2297 return MMC_REQ_FINISHED;
2298 }
2299 ret = mmc_blk_cqe_issue_flush(mq, req);
2300 break;
2301 case REQ_OP_READ:
2302 case REQ_OP_WRITE:
2303 if (mq->use_cqe)
2304 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2305 else
2306 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2307 break;
2308 default:
2309 WARN_ON_ONCE(1);
2310 ret = -EINVAL;
2311 }
2312 if (!ret)
2313 return MMC_REQ_STARTED;
2314 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2315 default:
2316 WARN_ON_ONCE(1);
2317 return MMC_REQ_FAILED_TO_START;
2318 }
2319 }
2320
mmc_blk_readonly(struct mmc_card *card)2321 static inline int mmc_blk_readonly(struct mmc_card *card)
2322 {
2323 return mmc_card_readonly(card) ||
2324 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2325 }
2326
mmc_blk_alloc_req(struct mmc_card *card, struct device *parent, sector_t size, bool default_ro, const char *subname, int area_type)2327 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2328 struct device *parent,
2329 sector_t size,
2330 bool default_ro,
2331 const char *subname,
2332 int area_type)
2333 {
2334 struct mmc_blk_data *md;
2335 int devidx, ret;
2336
2337 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2338 if (devidx < 0) {
2339 /*
2340 * We get -ENOSPC because there are no more any available
2341 * devidx. The reason may be that, either userspace haven't yet
2342 * unmounted the partitions, which postpones mmc_blk_release()
2343 * from being called, or the device has more partitions than
2344 * what we support.
2345 */
2346 if (devidx == -ENOSPC)
2347 dev_err(mmc_dev(card->host),
2348 "no more device IDs available\n");
2349
2350 return ERR_PTR(devidx);
2351 }
2352
2353 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2354 if (!md) {
2355 ret = -ENOMEM;
2356 goto out;
2357 }
2358
2359 md->area_type = area_type;
2360
2361 /*
2362 * Set the read-only status based on the supported commands
2363 * and the write protect switch.
2364 */
2365 md->read_only = mmc_blk_readonly(card);
2366
2367 md->disk = alloc_disk(perdev_minors);
2368 if (md->disk == NULL) {
2369 ret = -ENOMEM;
2370 goto err_kfree;
2371 }
2372
2373 INIT_LIST_HEAD(&md->part);
2374 INIT_LIST_HEAD(&md->rpmbs);
2375 md->usage = 1;
2376
2377 ret = mmc_init_queue(&md->queue, card);
2378 if (ret)
2379 goto err_putdisk;
2380
2381 md->queue.blkdata = md;
2382
2383 /*
2384 * Keep an extra reference to the queue so that we can shutdown the
2385 * queue (i.e. call blk_cleanup_queue()) while there are still
2386 * references to the 'md'. The corresponding blk_put_queue() is in
2387 * mmc_blk_put().
2388 */
2389 if (!blk_get_queue(md->queue.queue)) {
2390 mmc_cleanup_queue(&md->queue);
2391 ret = -ENODEV;
2392 goto err_putdisk;
2393 }
2394
2395 md->disk->major = MMC_BLOCK_MAJOR;
2396 md->disk->first_minor = devidx * perdev_minors;
2397 md->disk->fops = &mmc_bdops;
2398 md->disk->private_data = md;
2399 md->disk->queue = md->queue.queue;
2400 md->parent = parent;
2401 set_disk_ro(md->disk, md->read_only || default_ro);
2402 md->disk->flags = GENHD_FL_EXT_DEVT;
2403 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2404 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2405 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2406
2407 /*
2408 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2409 *
2410 * - be set for removable media with permanent block devices
2411 * - be unset for removable block devices with permanent media
2412 *
2413 * Since MMC block devices clearly fall under the second
2414 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2415 * should use the block device creation/destruction hotplug
2416 * messages to tell when the card is present.
2417 */
2418
2419 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2420 "mmcblk%u%s", card->host->index, subname ? subname : "");
2421
2422 set_capacity(md->disk, size);
2423
2424 if (mmc_host_cmd23(card->host)) {
2425 if ((mmc_card_mmc(card) &&
2426 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2427 (mmc_card_sd(card) &&
2428 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2429 md->flags |= MMC_BLK_CMD23;
2430 }
2431
2432 if (mmc_card_mmc(card) &&
2433 md->flags & MMC_BLK_CMD23 &&
2434 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2435 card->ext_csd.rel_sectors)) {
2436 md->flags |= MMC_BLK_REL_WR;
2437 blk_queue_write_cache(md->queue.queue, true, true);
2438 }
2439
2440 return md;
2441
2442 err_putdisk:
2443 put_disk(md->disk);
2444 err_kfree:
2445 kfree(md);
2446 out:
2447 ida_simple_remove(&mmc_blk_ida, devidx);
2448 return ERR_PTR(ret);
2449 }
2450
mmc_blk_alloc(struct mmc_card *card)2451 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2452 {
2453 sector_t size;
2454
2455 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2456 /*
2457 * The EXT_CSD sector count is in number or 512 byte
2458 * sectors.
2459 */
2460 size = card->ext_csd.sectors;
2461 } else {
2462 /*
2463 * The CSD capacity field is in units of read_blkbits.
2464 * set_capacity takes units of 512 bytes.
2465 */
2466 size = (typeof(sector_t))card->csd.capacity
2467 << (card->csd.read_blkbits - 9);
2468 }
2469
2470 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2471 MMC_BLK_DATA_AREA_MAIN);
2472 }
2473
mmc_blk_alloc_part(struct mmc_card *card, struct mmc_blk_data *md, unsigned int part_type, sector_t size, bool default_ro, const char *subname, int area_type)2474 static int mmc_blk_alloc_part(struct mmc_card *card,
2475 struct mmc_blk_data *md,
2476 unsigned int part_type,
2477 sector_t size,
2478 bool default_ro,
2479 const char *subname,
2480 int area_type)
2481 {
2482 char cap_str[10];
2483 struct mmc_blk_data *part_md;
2484
2485 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2486 subname, area_type);
2487 if (IS_ERR(part_md))
2488 return PTR_ERR(part_md);
2489 part_md->part_type = part_type;
2490 list_add(&part_md->part, &md->part);
2491
2492 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2493 cap_str, sizeof(cap_str));
2494 pr_info("%s: %s %s partition %u %s\n",
2495 part_md->disk->disk_name, mmc_card_id(card),
2496 mmc_card_name(card), part_md->part_type, cap_str);
2497 return 0;
2498 }
2499
2500 /**
2501 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2502 * @filp: the character device file
2503 * @cmd: the ioctl() command
2504 * @arg: the argument from userspace
2505 *
2506 * This will essentially just redirect the ioctl()s coming in over to
2507 * the main block device spawning the RPMB character device.
2508 */
mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)2509 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2510 unsigned long arg)
2511 {
2512 struct mmc_rpmb_data *rpmb = filp->private_data;
2513 int ret;
2514
2515 switch (cmd) {
2516 case MMC_IOC_CMD:
2517 ret = mmc_blk_ioctl_cmd(rpmb->md,
2518 (struct mmc_ioc_cmd __user *)arg,
2519 rpmb);
2520 break;
2521 case MMC_IOC_MULTI_CMD:
2522 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2523 (struct mmc_ioc_multi_cmd __user *)arg,
2524 rpmb);
2525 break;
2526 default:
2527 ret = -EINVAL;
2528 break;
2529 }
2530
2531 return ret;
2532 }
2533
2534 #ifdef CONFIG_COMPAT
mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, unsigned long arg)2535 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2536 unsigned long arg)
2537 {
2538 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2539 }
2540 #endif
2541
mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)2542 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2543 {
2544 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2545 struct mmc_rpmb_data, chrdev);
2546
2547 get_device(&rpmb->dev);
2548 filp->private_data = rpmb;
2549 mmc_blk_get(rpmb->md->disk);
2550
2551 return nonseekable_open(inode, filp);
2552 }
2553
mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)2554 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2555 {
2556 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2557 struct mmc_rpmb_data, chrdev);
2558
2559 mmc_blk_put(rpmb->md);
2560 put_device(&rpmb->dev);
2561
2562 return 0;
2563 }
2564
2565 static const struct file_operations mmc_rpmb_fileops = {
2566 .release = mmc_rpmb_chrdev_release,
2567 .open = mmc_rpmb_chrdev_open,
2568 .owner = THIS_MODULE,
2569 .llseek = no_llseek,
2570 .unlocked_ioctl = mmc_rpmb_ioctl,
2571 #ifdef CONFIG_COMPAT
2572 .compat_ioctl = mmc_rpmb_ioctl_compat,
2573 #endif
2574 };
2575
mmc_blk_rpmb_device_release(struct device *dev)2576 static void mmc_blk_rpmb_device_release(struct device *dev)
2577 {
2578 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2579
2580 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2581 kfree(rpmb);
2582 }
2583
mmc_blk_alloc_rpmb_part(struct mmc_card *card, struct mmc_blk_data *md, unsigned int part_index, sector_t size, const char *subname)2584 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2585 struct mmc_blk_data *md,
2586 unsigned int part_index,
2587 sector_t size,
2588 const char *subname)
2589 {
2590 int devidx, ret;
2591 char rpmb_name[DISK_NAME_LEN];
2592 char cap_str[10];
2593 struct mmc_rpmb_data *rpmb;
2594
2595 /* This creates the minor number for the RPMB char device */
2596 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2597 if (devidx < 0)
2598 return devidx;
2599
2600 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2601 if (!rpmb) {
2602 ida_simple_remove(&mmc_rpmb_ida, devidx);
2603 return -ENOMEM;
2604 }
2605
2606 snprintf(rpmb_name, sizeof(rpmb_name),
2607 "mmcblk%u%s", card->host->index, subname ? subname : "");
2608
2609 rpmb->id = devidx;
2610 rpmb->part_index = part_index;
2611 rpmb->dev.init_name = rpmb_name;
2612 rpmb->dev.bus = &mmc_rpmb_bus_type;
2613 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2614 rpmb->dev.parent = &card->dev;
2615 rpmb->dev.release = mmc_blk_rpmb_device_release;
2616 device_initialize(&rpmb->dev);
2617 dev_set_drvdata(&rpmb->dev, rpmb);
2618 rpmb->md = md;
2619
2620 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2621 rpmb->chrdev.owner = THIS_MODULE;
2622 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2623 if (ret) {
2624 pr_err("%s: could not add character device\n", rpmb_name);
2625 goto out_put_device;
2626 }
2627
2628 list_add(&rpmb->node, &md->rpmbs);
2629
2630 string_get_size((u64)size, 512, STRING_UNITS_2,
2631 cap_str, sizeof(cap_str));
2632
2633 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2634 rpmb_name, mmc_card_id(card),
2635 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2636 MAJOR(mmc_rpmb_devt), rpmb->id);
2637
2638 return 0;
2639
2640 out_put_device:
2641 put_device(&rpmb->dev);
2642 return ret;
2643 }
2644
mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)2645 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2646
2647 {
2648 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2649 put_device(&rpmb->dev);
2650 }
2651
2652 /* MMC Physical partitions consist of two boot partitions and
2653 * up to four general purpose partitions.
2654 * For each partition enabled in EXT_CSD a block device will be allocatedi
2655 * to provide access to the partition.
2656 */
2657
mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)2658 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2659 {
2660 int idx, ret;
2661
2662 if (!mmc_card_mmc(card))
2663 return 0;
2664
2665 for (idx = 0; idx < card->nr_parts; idx++) {
2666 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2667 /*
2668 * RPMB partitions does not provide block access, they
2669 * are only accessed using ioctl():s. Thus create
2670 * special RPMB block devices that do not have a
2671 * backing block queue for these.
2672 */
2673 ret = mmc_blk_alloc_rpmb_part(card, md,
2674 card->part[idx].part_cfg,
2675 card->part[idx].size >> 9,
2676 card->part[idx].name);
2677 if (ret)
2678 return ret;
2679 } else if (card->part[idx].size) {
2680 ret = mmc_blk_alloc_part(card, md,
2681 card->part[idx].part_cfg,
2682 card->part[idx].size >> 9,
2683 card->part[idx].force_ro,
2684 card->part[idx].name,
2685 card->part[idx].area_type);
2686 if (ret)
2687 return ret;
2688 }
2689 }
2690
2691 return 0;
2692 }
2693
mmc_blk_remove_req(struct mmc_blk_data *md)2694 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2695 {
2696 struct mmc_card *card;
2697
2698 if (md) {
2699 /*
2700 * Flush remaining requests and free queues. It
2701 * is freeing the queue that stops new requests
2702 * from being accepted.
2703 */
2704 card = md->queue.card;
2705 if (md->disk->flags & GENHD_FL_UP) {
2706 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2707 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2708 card->ext_csd.boot_ro_lockable)
2709 device_remove_file(disk_to_dev(md->disk),
2710 &md->power_ro_lock);
2711
2712 del_gendisk(md->disk);
2713 }
2714 mmc_cleanup_queue(&md->queue);
2715 mmc_blk_put(md);
2716 }
2717 }
2718
mmc_blk_remove_parts(struct mmc_card *card, struct mmc_blk_data *md)2719 static void mmc_blk_remove_parts(struct mmc_card *card,
2720 struct mmc_blk_data *md)
2721 {
2722 struct list_head *pos, *q;
2723 struct mmc_blk_data *part_md;
2724 struct mmc_rpmb_data *rpmb;
2725
2726 /* Remove RPMB partitions */
2727 list_for_each_safe(pos, q, &md->rpmbs) {
2728 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2729 list_del(pos);
2730 mmc_blk_remove_rpmb_part(rpmb);
2731 }
2732 /* Remove block partitions */
2733 list_for_each_safe(pos, q, &md->part) {
2734 part_md = list_entry(pos, struct mmc_blk_data, part);
2735 list_del(pos);
2736 mmc_blk_remove_req(part_md);
2737 }
2738 }
2739
mmc_add_disk(struct mmc_blk_data *md)2740 static int mmc_add_disk(struct mmc_blk_data *md)
2741 {
2742 int ret;
2743 struct mmc_card *card = md->queue.card;
2744
2745 device_add_disk(md->parent, md->disk, NULL);
2746 md->force_ro.show = force_ro_show;
2747 md->force_ro.store = force_ro_store;
2748 sysfs_attr_init(&md->force_ro.attr);
2749 md->force_ro.attr.name = "force_ro";
2750 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2751 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2752 if (ret)
2753 goto force_ro_fail;
2754
2755 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2756 card->ext_csd.boot_ro_lockable) {
2757 umode_t mode;
2758
2759 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2760 mode = S_IRUGO;
2761 else
2762 mode = S_IRUGO | S_IWUSR;
2763
2764 md->power_ro_lock.show = power_ro_lock_show;
2765 md->power_ro_lock.store = power_ro_lock_store;
2766 sysfs_attr_init(&md->power_ro_lock.attr);
2767 md->power_ro_lock.attr.mode = mode;
2768 md->power_ro_lock.attr.name =
2769 "ro_lock_until_next_power_on";
2770 ret = device_create_file(disk_to_dev(md->disk),
2771 &md->power_ro_lock);
2772 if (ret)
2773 goto power_ro_lock_fail;
2774 }
2775 return ret;
2776
2777 power_ro_lock_fail:
2778 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2779 force_ro_fail:
2780 del_gendisk(md->disk);
2781
2782 return ret;
2783 }
2784
2785 #ifdef CONFIG_DEBUG_FS
2786
mmc_dbg_card_status_get(void *data, u64 *val)2787 static int mmc_dbg_card_status_get(void *data, u64 *val)
2788 {
2789 struct mmc_card *card = data;
2790 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2791 struct mmc_queue *mq = &md->queue;
2792 struct request *req;
2793 int ret;
2794
2795 /* Ask the block layer about the card status */
2796 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2797 if (IS_ERR(req))
2798 return PTR_ERR(req);
2799 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2800 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2801 blk_execute_rq(mq->queue, NULL, req, 0);
2802 ret = req_to_mmc_queue_req(req)->drv_op_result;
2803 if (ret >= 0) {
2804 *val = ret;
2805 ret = 0;
2806 }
2807 blk_put_request(req);
2808
2809 return ret;
2810 }
2811 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2812 NULL, "%08llx\n");
2813
2814 /* That is two digits * 512 + 1 for newline */
2815 #define EXT_CSD_STR_LEN 1025
2816
mmc_ext_csd_open(struct inode *inode, struct file *filp)2817 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2818 {
2819 struct mmc_card *card = inode->i_private;
2820 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2821 struct mmc_queue *mq = &md->queue;
2822 struct request *req;
2823 char *buf;
2824 ssize_t n = 0;
2825 u8 *ext_csd;
2826 int err, i;
2827
2828 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2829 if (!buf)
2830 return -ENOMEM;
2831
2832 /* Ask the block layer for the EXT CSD */
2833 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2834 if (IS_ERR(req)) {
2835 err = PTR_ERR(req);
2836 goto out_free;
2837 }
2838 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2839 req_to_mmc_queue_req(req)->drv_op_result = -EIO;
2840 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2841 blk_execute_rq(mq->queue, NULL, req, 0);
2842 err = req_to_mmc_queue_req(req)->drv_op_result;
2843 blk_put_request(req);
2844 if (err) {
2845 pr_err("FAILED %d\n", err);
2846 goto out_free;
2847 }
2848
2849 for (i = 0; i < 512; i++)
2850 n += sprintf(buf + n, "%02x", ext_csd[i]);
2851 n += sprintf(buf + n, "\n");
2852
2853 if (n != EXT_CSD_STR_LEN) {
2854 err = -EINVAL;
2855 kfree(ext_csd);
2856 goto out_free;
2857 }
2858
2859 filp->private_data = buf;
2860 kfree(ext_csd);
2861 return 0;
2862
2863 out_free:
2864 kfree(buf);
2865 return err;
2866 }
2867
mmc_ext_csd_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)2868 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2869 size_t cnt, loff_t *ppos)
2870 {
2871 char *buf = filp->private_data;
2872
2873 return simple_read_from_buffer(ubuf, cnt, ppos,
2874 buf, EXT_CSD_STR_LEN);
2875 }
2876
mmc_ext_csd_release(struct inode *inode, struct file *file)2877 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2878 {
2879 kfree(file->private_data);
2880 return 0;
2881 }
2882
2883 static const struct file_operations mmc_dbg_ext_csd_fops = {
2884 .open = mmc_ext_csd_open,
2885 .read = mmc_ext_csd_read,
2886 .release = mmc_ext_csd_release,
2887 .llseek = default_llseek,
2888 };
2889
mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)2890 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2891 {
2892 struct dentry *root;
2893
2894 if (!card->debugfs_root)
2895 return 0;
2896
2897 root = card->debugfs_root;
2898
2899 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2900 md->status_dentry =
2901 debugfs_create_file_unsafe("status", 0400, root,
2902 card,
2903 &mmc_dbg_card_status_fops);
2904 if (!md->status_dentry)
2905 return -EIO;
2906 }
2907
2908 if (mmc_card_mmc(card)) {
2909 md->ext_csd_dentry =
2910 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2911 &mmc_dbg_ext_csd_fops);
2912 if (!md->ext_csd_dentry)
2913 return -EIO;
2914 }
2915
2916 return 0;
2917 }
2918
mmc_blk_remove_debugfs(struct mmc_card *card, struct mmc_blk_data *md)2919 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2920 struct mmc_blk_data *md)
2921 {
2922 if (!card->debugfs_root)
2923 return;
2924
2925 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2926 debugfs_remove(md->status_dentry);
2927 md->status_dentry = NULL;
2928 }
2929
2930 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2931 debugfs_remove(md->ext_csd_dentry);
2932 md->ext_csd_dentry = NULL;
2933 }
2934 }
2935
2936 #else
2937
mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)2938 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2939 {
2940 return 0;
2941 }
2942
mmc_blk_remove_debugfs(struct mmc_card *card, struct mmc_blk_data *md)2943 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2944 struct mmc_blk_data *md)
2945 {
2946 }
2947
2948 #endif /* CONFIG_DEBUG_FS */
2949
mmc_blk_probe(struct mmc_card *card)2950 static int mmc_blk_probe(struct mmc_card *card)
2951 {
2952 struct mmc_blk_data *md, *part_md;
2953 char cap_str[10];
2954
2955 /*
2956 * Check that the card supports the command class(es) we need.
2957 */
2958 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2959 return -ENODEV;
2960
2961 mmc_fixup_device(card, mmc_blk_fixups);
2962
2963 card->complete_wq = alloc_workqueue("mmc_complete",
2964 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2965 if (unlikely(!card->complete_wq)) {
2966 pr_err("Failed to create mmc completion workqueue");
2967 return -ENOMEM;
2968 }
2969
2970 md = mmc_blk_alloc(card);
2971 if (IS_ERR(md))
2972 return PTR_ERR(md);
2973
2974 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2975 cap_str, sizeof(cap_str));
2976 pr_info("%s: %s %s %s %s\n",
2977 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2978 cap_str, md->read_only ? "(ro)" : "");
2979
2980 if (mmc_blk_alloc_parts(card, md))
2981 goto out;
2982
2983 dev_set_drvdata(&card->dev, md);
2984
2985 if (mmc_add_disk(md))
2986 goto out;
2987
2988 list_for_each_entry(part_md, &md->part, part) {
2989 if (mmc_add_disk(part_md))
2990 goto out;
2991 }
2992
2993 /* Add two debugfs entries */
2994 mmc_blk_add_debugfs(card, md);
2995
2996 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2997 pm_runtime_use_autosuspend(&card->dev);
2998
2999 /*
3000 * Don't enable runtime PM for SD-combo cards here. Leave that
3001 * decision to be taken during the SDIO init sequence instead.
3002 */
3003 if (card->type != MMC_TYPE_SD_COMBO) {
3004 pm_runtime_set_active(&card->dev);
3005 pm_runtime_enable(&card->dev);
3006 }
3007
3008 return 0;
3009
3010 out:
3011 mmc_blk_remove_parts(card, md);
3012 mmc_blk_remove_req(md);
3013 return 0;
3014 }
3015
mmc_blk_remove(struct mmc_card *card)3016 static void mmc_blk_remove(struct mmc_card *card)
3017 {
3018 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3019
3020 mmc_blk_remove_debugfs(card, md);
3021 mmc_blk_remove_parts(card, md);
3022 pm_runtime_get_sync(&card->dev);
3023 if (md->part_curr != md->part_type) {
3024 mmc_claim_host(card->host);
3025 mmc_blk_part_switch(card, md->part_type);
3026 mmc_release_host(card->host);
3027 }
3028 if (card->type != MMC_TYPE_SD_COMBO)
3029 pm_runtime_disable(&card->dev);
3030 pm_runtime_put_noidle(&card->dev);
3031 mmc_blk_remove_req(md);
3032 dev_set_drvdata(&card->dev, NULL);
3033 destroy_workqueue(card->complete_wq);
3034 }
3035
_mmc_blk_suspend(struct mmc_card *card)3036 static int _mmc_blk_suspend(struct mmc_card *card)
3037 {
3038 struct mmc_blk_data *part_md;
3039 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3040
3041 if (md) {
3042 mmc_queue_suspend(&md->queue);
3043 list_for_each_entry(part_md, &md->part, part) {
3044 mmc_queue_suspend(&part_md->queue);
3045 }
3046 }
3047 return 0;
3048 }
3049
mmc_blk_shutdown(struct mmc_card *card)3050 static void mmc_blk_shutdown(struct mmc_card *card)
3051 {
3052 _mmc_blk_suspend(card);
3053 }
3054
3055 #ifdef CONFIG_PM_SLEEP
mmc_blk_suspend(struct device *dev)3056 static int mmc_blk_suspend(struct device *dev)
3057 {
3058 struct mmc_card *card = mmc_dev_to_card(dev);
3059
3060 return _mmc_blk_suspend(card);
3061 }
3062
mmc_blk_resume(struct device *dev)3063 static int mmc_blk_resume(struct device *dev)
3064 {
3065 struct mmc_blk_data *part_md;
3066 struct mmc_blk_data *md = dev_get_drvdata(dev);
3067
3068 if (md) {
3069 /*
3070 * Resume involves the card going into idle state,
3071 * so current partition is always the main one.
3072 */
3073 md->part_curr = md->part_type;
3074 mmc_queue_resume(&md->queue);
3075 list_for_each_entry(part_md, &md->part, part) {
3076 mmc_queue_resume(&part_md->queue);
3077 }
3078 }
3079 return 0;
3080 }
3081 #endif
3082
3083 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3084
3085 static struct mmc_driver mmc_driver = {
3086 .drv = {
3087 .name = "mmcblk",
3088 .pm = &mmc_blk_pm_ops,
3089 },
3090 .probe = mmc_blk_probe,
3091 .remove = mmc_blk_remove,
3092 .shutdown = mmc_blk_shutdown,
3093 };
3094
mmc_blk_init(void)3095 static int __init mmc_blk_init(void)
3096 {
3097 int res;
3098
3099 res = bus_register(&mmc_rpmb_bus_type);
3100 if (res < 0) {
3101 pr_err("mmcblk: could not register RPMB bus type\n");
3102 return res;
3103 }
3104 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3105 if (res < 0) {
3106 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3107 goto out_bus_unreg;
3108 }
3109
3110 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3111 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3112
3113 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3114
3115 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3116 if (res)
3117 goto out_chrdev_unreg;
3118
3119 res = mmc_register_driver(&mmc_driver);
3120 if (res)
3121 goto out_blkdev_unreg;
3122
3123 return 0;
3124
3125 out_blkdev_unreg:
3126 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3127 out_chrdev_unreg:
3128 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3129 out_bus_unreg:
3130 bus_unregister(&mmc_rpmb_bus_type);
3131 return res;
3132 }
3133
mmc_blk_exit(void)3134 static void __exit mmc_blk_exit(void)
3135 {
3136 mmc_unregister_driver(&mmc_driver);
3137 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3138 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3139 bus_unregister(&mmc_rpmb_bus_type);
3140 }
3141
3142 module_init(mmc_blk_init);
3143 module_exit(mmc_blk_exit);
3144
3145 MODULE_LICENSE("GPL");
3146 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3147