Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mmc: block: register RPMB partition with the RPMB subsystem

Register eMMC RPMB partition with the RPMB subsystem and provide
an implementation for the RPMB access operations abstracting
the actual multi step process.

Add a callback to extract the needed device information at registration
to avoid accessing the struct mmc_card at a later stage as we're not
holding a reference counter for this struct.

Taking the needed reference to md->disk in mmc_blk_alloc_rpmb_part()
instead of in mmc_rpmb_chrdev_open(). This is needed by the
route_frames() function pointer in struct rpmb_ops.

Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Tested-by: Manuel Traut <manut@mecka.net>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Reviewed-by: Ulf Hansson <ulf.hansson@linaro.org>
Link: https://lore.kernel.org/r/20240814153558.708365-3-jens.wiklander@linaro.org
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>

authored by

Jens Wiklander and committed by
Ulf Hansson
7852028a 1e9046e3

+240 -2
+240 -2
drivers/mmc/core/block.c
··· 33 33 #include <linux/cdev.h> 34 34 #include <linux/mutex.h> 35 35 #include <linux/scatterlist.h> 36 + #include <linux/string.h> 36 37 #include <linux/string_helpers.h> 37 38 #include <linux/delay.h> 38 39 #include <linux/capability.h> ··· 41 40 #include <linux/pm_runtime.h> 42 41 #include <linux/idr.h> 43 42 #include <linux/debugfs.h> 43 + #include <linux/rpmb.h> 44 44 45 45 #include <linux/mmc/ioctl.h> 46 46 #include <linux/mmc/card.h> ··· 77 75 #define MMC_BLK_TIMEOUT_MS (10 * 1000) 78 76 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) 79 77 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8) 78 + 79 + /** 80 + * struct rpmb_frame - rpmb frame as defined by eMMC 5.1 (JESD84-B51) 81 + * 82 + * @stuff : stuff bytes 83 + * @key_mac : The authentication key or the message authentication 84 + * code (MAC) depending on the request/response type. 85 + * The MAC will be delivered in the last (or the only) 86 + * block of data. 87 + * @data : Data to be written or read by signed access. 88 + * @nonce : Random number generated by the host for the requests 89 + * and copied to the response by the RPMB engine. 90 + * @write_counter: Counter value for the total amount of the successful 91 + * authenticated data write requests made by the host. 92 + * @addr : Address of the data to be programmed to or read 93 + * from the RPMB. Address is the serial number of 94 + * the accessed block (half sector 256B). 95 + * @block_count : Number of blocks (half sectors, 256B) requested to be 96 + * read/programmed. 97 + * @result : Includes information about the status of the write counter 98 + * (valid, expired) and result of the access made to the RPMB. 99 + * @req_resp : Defines the type of request and response to/from the memory. 100 + * 101 + * The stuff bytes and big-endian properties are modeled to fit to the spec. 102 + */ 103 + struct rpmb_frame { 104 + u8 stuff[196]; 105 + u8 key_mac[32]; 106 + u8 data[256]; 107 + u8 nonce[16]; 108 + __be32 write_counter; 109 + __be16 addr; 110 + __be16 block_count; 111 + __be16 result; 112 + __be16 req_resp; 113 + } __packed; 114 + 115 + #define RPMB_PROGRAM_KEY 0x1 /* Program RPMB Authentication Key */ 116 + #define RPMB_GET_WRITE_COUNTER 0x2 /* Read RPMB write counter */ 117 + #define RPMB_WRITE_DATA 0x3 /* Write data to RPMB partition */ 118 + #define RPMB_READ_DATA 0x4 /* Read data from RPMB partition */ 119 + #define RPMB_RESULT_READ 0x5 /* Read result request (Internal) */ 80 120 81 121 static DEFINE_MUTEX(block_mutex); 82 122 ··· 199 155 * @id: unique device ID number 200 156 * @part_index: partition index (0 on first) 201 157 * @md: parent MMC block device 158 + * @rdev: registered RPMB device 202 159 * @node: list item, so we can put this device on a list 203 160 */ 204 161 struct mmc_rpmb_data { ··· 208 163 int id; 209 164 unsigned int part_index; 210 165 struct mmc_blk_data *md; 166 + struct rpmb_dev *rdev; 211 167 struct list_head node; 212 168 }; 213 169 ··· 2716 2670 2717 2671 get_device(&rpmb->dev); 2718 2672 filp->private_data = rpmb; 2719 - mmc_blk_get(rpmb->md->disk); 2720 2673 2721 2674 return nonseekable_open(inode, filp); 2722 2675 } ··· 2725 2680 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, 2726 2681 struct mmc_rpmb_data, chrdev); 2727 2682 2728 - mmc_blk_put(rpmb->md); 2729 2683 put_device(&rpmb->dev); 2730 2684 2731 2685 return 0; ··· 2745 2701 { 2746 2702 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); 2747 2703 2704 + rpmb_dev_unregister(rpmb->rdev); 2705 + mmc_blk_put(rpmb->md); 2748 2706 ida_free(&mmc_rpmb_ida, rpmb->id); 2749 2707 kfree(rpmb); 2708 + } 2709 + 2710 + static void free_idata(struct mmc_blk_ioc_data **idata, unsigned int cmd_count) 2711 + { 2712 + unsigned int n; 2713 + 2714 + for (n = 0; n < cmd_count; n++) 2715 + kfree(idata[n]); 2716 + kfree(idata); 2717 + } 2718 + 2719 + static struct mmc_blk_ioc_data **alloc_idata(struct mmc_rpmb_data *rpmb, 2720 + unsigned int cmd_count) 2721 + { 2722 + struct mmc_blk_ioc_data **idata; 2723 + unsigned int n; 2724 + 2725 + idata = kcalloc(cmd_count, sizeof(*idata), GFP_KERNEL); 2726 + if (!idata) 2727 + return NULL; 2728 + 2729 + for (n = 0; n < cmd_count; n++) { 2730 + idata[n] = kcalloc(1, sizeof(**idata), GFP_KERNEL); 2731 + if (!idata[n]) { 2732 + free_idata(idata, n); 2733 + return NULL; 2734 + } 2735 + idata[n]->rpmb = rpmb; 2736 + } 2737 + 2738 + return idata; 2739 + } 2740 + 2741 + static void set_idata(struct mmc_blk_ioc_data *idata, u32 opcode, 2742 + int write_flag, u8 *buf, unsigned int buf_bytes) 2743 + { 2744 + /* 2745 + * The size of an RPMB frame must match what's expected by the 2746 + * hardware. 2747 + */ 2748 + BUILD_BUG_ON(sizeof(struct rpmb_frame) != 512); 2749 + 2750 + idata->ic.opcode = opcode; 2751 + idata->ic.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 2752 + idata->ic.write_flag = write_flag; 2753 + idata->ic.blksz = sizeof(struct rpmb_frame); 2754 + idata->ic.blocks = buf_bytes / idata->ic.blksz; 2755 + idata->buf = buf; 2756 + idata->buf_bytes = buf_bytes; 2757 + } 2758 + 2759 + static int mmc_route_rpmb_frames(struct device *dev, u8 *req, 2760 + unsigned int req_len, u8 *resp, 2761 + unsigned int resp_len) 2762 + { 2763 + struct rpmb_frame *frm = (struct rpmb_frame *)req; 2764 + struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); 2765 + struct mmc_blk_data *md = rpmb->md; 2766 + struct mmc_blk_ioc_data **idata; 2767 + struct mmc_queue_req *mq_rq; 2768 + unsigned int cmd_count; 2769 + struct request *rq; 2770 + u16 req_type; 2771 + bool write; 2772 + int ret; 2773 + 2774 + if (IS_ERR(md->queue.card)) 2775 + return PTR_ERR(md->queue.card); 2776 + 2777 + if (req_len < sizeof(*frm)) 2778 + return -EINVAL; 2779 + 2780 + req_type = be16_to_cpu(frm->req_resp); 2781 + switch (req_type) { 2782 + case RPMB_PROGRAM_KEY: 2783 + if (req_len != sizeof(struct rpmb_frame) || 2784 + resp_len != sizeof(struct rpmb_frame)) 2785 + return -EINVAL; 2786 + write = true; 2787 + break; 2788 + case RPMB_GET_WRITE_COUNTER: 2789 + if (req_len != sizeof(struct rpmb_frame) || 2790 + resp_len != sizeof(struct rpmb_frame)) 2791 + return -EINVAL; 2792 + write = false; 2793 + break; 2794 + case RPMB_WRITE_DATA: 2795 + if (req_len % sizeof(struct rpmb_frame) || 2796 + resp_len != sizeof(struct rpmb_frame)) 2797 + return -EINVAL; 2798 + write = true; 2799 + break; 2800 + case RPMB_READ_DATA: 2801 + if (req_len != sizeof(struct rpmb_frame) || 2802 + resp_len % sizeof(struct rpmb_frame)) 2803 + return -EINVAL; 2804 + write = false; 2805 + break; 2806 + default: 2807 + return -EINVAL; 2808 + } 2809 + 2810 + if (write) 2811 + cmd_count = 3; 2812 + else 2813 + cmd_count = 2; 2814 + 2815 + idata = alloc_idata(rpmb, cmd_count); 2816 + if (!idata) 2817 + return -ENOMEM; 2818 + 2819 + if (write) { 2820 + struct rpmb_frame *frm = (struct rpmb_frame *)resp; 2821 + 2822 + /* Send write request frame(s) */ 2823 + set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, 2824 + 1 | MMC_CMD23_ARG_REL_WR, req, req_len); 2825 + 2826 + /* Send result request frame */ 2827 + memset(frm, 0, sizeof(*frm)); 2828 + frm->req_resp = cpu_to_be16(RPMB_RESULT_READ); 2829 + set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp, 2830 + resp_len); 2831 + 2832 + /* Read response frame */ 2833 + set_idata(idata[2], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); 2834 + } else { 2835 + /* Send write request frame(s) */ 2836 + set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, 1, req, req_len); 2837 + 2838 + /* Read response frame */ 2839 + set_idata(idata[1], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); 2840 + } 2841 + 2842 + rq = blk_mq_alloc_request(md->queue.queue, REQ_OP_DRV_OUT, 0); 2843 + if (IS_ERR(rq)) { 2844 + ret = PTR_ERR(rq); 2845 + goto out; 2846 + } 2847 + 2848 + mq_rq = req_to_mmc_queue_req(rq); 2849 + mq_rq->drv_op = MMC_DRV_OP_IOCTL_RPMB; 2850 + mq_rq->drv_op_result = -EIO; 2851 + mq_rq->drv_op_data = idata; 2852 + mq_rq->ioc_count = cmd_count; 2853 + blk_execute_rq(rq, false); 2854 + ret = req_to_mmc_queue_req(rq)->drv_op_result; 2855 + 2856 + blk_mq_free_request(rq); 2857 + 2858 + out: 2859 + free_idata(idata, cmd_count); 2860 + return ret; 2750 2861 } 2751 2862 2752 2863 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, ··· 2938 2739 rpmb->dev.release = mmc_blk_rpmb_device_release; 2939 2740 device_initialize(&rpmb->dev); 2940 2741 dev_set_drvdata(&rpmb->dev, rpmb); 2742 + mmc_blk_get(md->disk); 2941 2743 rpmb->md = md; 2942 2744 2943 2745 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); ··· 3200 3000 3201 3001 #endif /* CONFIG_DEBUG_FS */ 3202 3002 3003 + static void mmc_blk_rpmb_add(struct mmc_card *card) 3004 + { 3005 + struct mmc_blk_data *md = dev_get_drvdata(&card->dev); 3006 + struct mmc_rpmb_data *rpmb; 3007 + struct rpmb_dev *rdev; 3008 + unsigned int n; 3009 + u32 cid[4]; 3010 + struct rpmb_descr descr = { 3011 + .type = RPMB_TYPE_EMMC, 3012 + .route_frames = mmc_route_rpmb_frames, 3013 + .reliable_wr_count = card->ext_csd.enhanced_rpmb_supported ? 3014 + 2 : 32, 3015 + .capacity = card->ext_csd.raw_rpmb_size_mult, 3016 + .dev_id = (void *)cid, 3017 + .dev_id_len = sizeof(cid), 3018 + }; 3019 + 3020 + /* 3021 + * Provice CID as an octet array. The CID needs to be interpreted 3022 + * when used as input to derive the RPMB key since some fields 3023 + * will change due to firmware updates. 3024 + */ 3025 + for (n = 0; n < 4; n++) 3026 + cid[n] = be32_to_cpu((__force __be32)card->raw_cid[n]); 3027 + 3028 + list_for_each_entry(rpmb, &md->rpmbs, node) { 3029 + rdev = rpmb_dev_register(&rpmb->dev, &descr); 3030 + if (IS_ERR(rdev)) { 3031 + pr_warn("%s: could not register RPMB device\n", 3032 + dev_name(&rpmb->dev)); 3033 + continue; 3034 + } 3035 + rpmb->rdev = rdev; 3036 + } 3037 + } 3038 + 3203 3039 static int mmc_blk_probe(struct mmc_card *card) 3204 3040 { 3205 3041 struct mmc_blk_data *md; ··· 3280 3044 pm_runtime_set_active(&card->dev); 3281 3045 pm_runtime_enable(&card->dev); 3282 3046 } 3047 + 3048 + mmc_blk_rpmb_add(card); 3283 3049 3284 3050 return 0; 3285 3051