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