From nobody Tue Feb 10 05:17:20 2026 Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A903F14D6E7 for ; Tue, 7 May 2024 09:16:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715073413; cv=none; b=Tl1ghRy93qTC1YzeLd5SVWwWYhTceTE2RchMilG9Cgcw0VXyX9GbAKt+5t6V6dOdVsz4cBUWVlPrh6akjRWKnxxeMqq933CWg2imNlHoKVmZpryqQXvkKSlLwrNpo3dOdCmj1VaWz/oH4PzDk4VNrW37k/T76j/A9tay3VnYXa4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715073413; c=relaxed/simple; bh=+jjmPCWL7r8Fyhz8AVURstJ73YjgSbV09gAbN+yt3g8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WFrc23OZnGtXY8pxzFoGSjuWUMADUUWh4vjsJJM6Reg2+tFCcIXlruStxK2mqPOc8z3Lg9sz4xV9zbkmVCe3tnG3PTp+1TQm4W0N0pqAuX9WDsHWlkAAIuadYR8/nRgl5hAAbA2ZXu0nAHbBzyrRQn/dgSAPXEykdSMjXgzOv0E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=FuBma0c5; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="FuBma0c5" Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2e3f6166e4aso6735251fa.1 for ; Tue, 07 May 2024 02:16:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715073409; x=1715678209; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SbRspnpSma1b5MC8A+dHKQyEgWUfH4y7j2WEQBaodgE=; b=FuBma0c566nz6SMZOkLjEsY68YbUySCOULAGZh0I7PQhqKrG7LGqKDy+VOxb3/Nqzv W4OT+FPVRB2FJTAvP6WkQ/zVrrgGfwbUQFV+zRJFTcnyF9glatz8TqsHa/36vIVamC7l 9Gc3Gf7DF2Hr46eTYDs5YTnCDPP/GiLIjsXo9mETSkbMeu4AJkIiqa4op9+lEWsvTyli PUAG4ZA1LGWuaKqay87CJWd/8vB/D22fLERus0eT4aCjYe9M9I6OJtE9bWGjBgEmxDMC Bl8CCGKGe+N13DPbfcMt8Y3VHTYjZ4MQTZ5ArTHBmTsY+GUWhcOmEwsZL+aOV5B95nzd pe6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715073409; x=1715678209; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SbRspnpSma1b5MC8A+dHKQyEgWUfH4y7j2WEQBaodgE=; b=kVLKs1K03grj1o5J6f43dD8NxrJzCAIvoJnyMBBr/PAWCeaaRhqx8sk9sM8I37dSG5 LhY9nxPLNttUJTMFtImuVdrKXSljKm+aMMRjvNyEpbhothi5vz5usoUCYFdHcpjnzpWq yWPvuW23HPGhTQZO5BkMXYJqe+IJStcA2S34mRvzZPH0PwVdN1h2QitEq4A4gfRRrZ0R GeNCfqpANooWB0lLPkedX+ARcMUjZASW2elQ7nfbesjxOl6qWFqYB//3yYS8/tKhrt6S c4xh7LkK35sa3NU3ALJDFlNFSznG9oRDMlYYplNooS6KTzMYPEJrcBsD/LpfHgk7gsqi 1iIw== X-Gm-Message-State: AOJu0Yzzb1CmhHppHKZs5mwCmGgA2xlgRA9Tqw0uOqvOo68Tix0DEdsU /F+NVZOBa0izLJvYTkCOceig6Rv/FYxiSvxv3NI3Syn38A+Q9xY6MSM/NdjXPzssLSSbe5Y3WuH I X-Google-Smtp-Source: AGHT+IFYwdXwqyDPJz0/hsNF/GVMaHPMWunbBWyfBA9jckpQ/DAl2rhonyiHr8a/X9Dq3Gxy7hvEZQ== X-Received: by 2002:a05:6512:457:b0:51f:b781:7293 with SMTP id y23-20020a056512045700b0051fb7817293mr8206662lfk.11.1715073408824; Tue, 07 May 2024 02:16:48 -0700 (PDT) Received: from rayden.urgonet (h-217-31-164-171.A175.priv.bahnhof.se. [217.31.164.171]) by smtp.gmail.com with ESMTPSA id o7-20020a1709061d4700b00a59a93a3ef4sm3976134ejh.149.2024.05.07.02.16.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 02:16:47 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Shyam Saini , Ulf Hansson , Linus Walleij , Jerome Forissier , Sumit Garg , Ilias Apalodimas , Bart Van Assche , Randy Dunlap , Ard Biesheuvel , Arnd Bergmann , Greg Kroah-Hartman , Manuel Traut , Jens Wiklander , Tomas Winkler , Alexander Usyskin Subject: [PATCH v6 2/3] mmc: block: register RPMB partition with the RPMB subsystem Date: Tue, 7 May 2024 11:16:18 +0200 Message-Id: <20240507091619.2208810-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240507091619.2208810-1-jens.wiklander@linaro.org> References: <20240507091619.2208810-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Alexander Usyskin Signed-off-by: Jens Wiklander Tested-by: Manuel Traut --- drivers/mmc/core/block.c | 241 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 239 insertions(+), 2 deletions(-) diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index 32d49100dff5..a7f126fbc605 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ #include #include #include +#include =20 #include #include @@ -76,6 +78,48 @@ MODULE_ALIAS("mmc:block"); #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8) =20 +/** + * struct rpmb_frame - rpmb frame as defined by eMMC 5.1 (JESD84-B51) + * + * @stuff : stuff bytes + * @key_mac : The authentication key or the message authentication + * code (MAC) depending on the request/response type. + * The MAC will be delivered in the last (or the only) + * block of data. + * @data : Data to be written or read by signed access. + * @nonce : Random number generated by the host for the requests + * and copied to the response by the RPMB engine. + * @write_counter: Counter value for the total amount of the successful + * authenticated data write requests made by the host. + * @addr : Address of the data to be programmed to or read + * from the RPMB. Address is the serial number of + * the accessed block (half sector 256B). + * @block_count : Number of blocks (half sectors, 256B) requested to be + * read/programmed. + * @result : Includes information about the status of the write coun= ter + * (valid, expired) and result of the access made to the R= PMB. + * @req_resp : Defines the type of request and response to/from the me= mory. + * + * The stuff bytes and big-endian properties are modeled to fit to the spe= c. + */ +struct rpmb_frame { + u8 stuff[196]; + u8 key_mac[32]; + u8 data[256]; + u8 nonce[16]; + __be32 write_counter; + __be16 addr; + __be16 block_count; + __be16 result; + __be16 req_resp; +} __packed; + +#define RPMB_PROGRAM_KEY 0x1 /* Program RPMB Authentication Key */ +#define RPMB_GET_WRITE_COUNTER 0x2 /* Read RPMB write counter */ +#define RPMB_WRITE_DATA 0x3 /* Write data to RPMB partition */ +#define RPMB_READ_DATA 0x4 /* Read data from RPMB partition */ +#define RPMB_RESULT_READ 0x5 /* Read result request (Internal) */ + static DEFINE_MUTEX(block_mutex); =20 /* @@ -163,6 +207,7 @@ struct mmc_rpmb_data { int id; unsigned int part_index; struct mmc_blk_data *md; + struct rpmb_dev *rdev; struct list_head node; }; =20 @@ -2672,7 +2717,6 @@ static int mmc_rpmb_chrdev_open(struct inode *inode, = struct file *filp) =20 get_device(&rpmb->dev); filp->private_data =3D rpmb; - mmc_blk_get(rpmb->md->disk); =20 return nonseekable_open(inode, filp); } @@ -2682,7 +2726,6 @@ static int mmc_rpmb_chrdev_release(struct inode *inod= e, struct file *filp) struct mmc_rpmb_data *rpmb =3D container_of(inode->i_cdev, struct mmc_rpmb_data, chrdev); =20 - mmc_blk_put(rpmb->md); put_device(&rpmb->dev); =20 return 0; @@ -2703,10 +2746,165 @@ static void mmc_blk_rpmb_device_release(struct dev= ice *dev) { struct mmc_rpmb_data *rpmb =3D dev_get_drvdata(dev); =20 + rpmb_dev_unregister(rpmb->rdev); + mmc_blk_put(rpmb->md); ida_simple_remove(&mmc_rpmb_ida, rpmb->id); kfree(rpmb); } =20 +static void free_idata(struct mmc_blk_ioc_data **idata, unsigned int cmd_c= ount) +{ + unsigned int n; + + for (n =3D 0; n < cmd_count; n++) + kfree(idata[n]); + kfree(idata); +} + +static struct mmc_blk_ioc_data **alloc_idata(struct mmc_rpmb_data *rpmb, + unsigned int cmd_count) +{ + struct mmc_blk_ioc_data **idata; + unsigned int n; + + idata =3D kcalloc(cmd_count, sizeof(*idata), GFP_KERNEL); + if (!idata) + return NULL; + + for (n =3D 0; n < cmd_count; n++) { + idata[n] =3D kcalloc(1, sizeof(**idata), GFP_KERNEL); + if (!idata[n]) { + free_idata(idata, n); + return NULL; + } + idata[n]->rpmb =3D rpmb; + } + + return idata; +} + +static void set_idata(struct mmc_blk_ioc_data *idata, u32 opcode, + int write_flag, u8 *buf, unsigned int buf_bytes) +{ + /* + * The size of an RPMB frame must match what's expected by the + * hardware. + */ + BUILD_BUG_ON(sizeof(struct rpmb_frame) !=3D 512); + + idata->ic.opcode =3D opcode; + idata->ic.flags =3D MMC_RSP_R1 | MMC_CMD_ADTC; + idata->ic.write_flag =3D write_flag; + idata->ic.blksz =3D sizeof(struct rpmb_frame); + idata->ic.blocks =3D buf_bytes / idata->ic.blksz; + idata->buf =3D buf; + idata->buf_bytes =3D buf_bytes; +} + +static int mmc_route_rpmb_frames(struct device *dev, u8 *req, + unsigned int req_len, u8 *resp, + unsigned int resp_len) +{ + struct rpmb_frame *frm =3D (struct rpmb_frame *)req; + struct mmc_rpmb_data *rpmb =3D dev_get_drvdata(dev); + struct mmc_blk_data *md =3D rpmb->md; + struct mmc_blk_ioc_data **idata; + struct mmc_queue_req *mq_rq; + unsigned int cmd_count; + struct request *rq; + u16 req_type; + bool write; + int ret; + + if (IS_ERR(md->queue.card)) + return PTR_ERR(md->queue.card); + + if (req_len < sizeof(*frm)) + return -EINVAL; + + req_type =3D be16_to_cpu(frm->req_resp); + switch (req_type) { + case RPMB_PROGRAM_KEY: + if (req_len !=3D sizeof(struct rpmb_frame) || + resp_len !=3D sizeof(struct rpmb_frame)) + return -EINVAL; + write =3D true; + break; + case RPMB_GET_WRITE_COUNTER: + if (req_len !=3D sizeof(struct rpmb_frame) || + resp_len !=3D sizeof(struct rpmb_frame)) + return -EINVAL; + write =3D false; + break; + case RPMB_WRITE_DATA: + if (req_len % sizeof(struct rpmb_frame) || + resp_len !=3D sizeof(struct rpmb_frame)) + return -EINVAL; + write =3D true; + break; + case RPMB_READ_DATA: + if (req_len !=3D sizeof(struct rpmb_frame) || + resp_len % sizeof(struct rpmb_frame)) + return -EINVAL; + write =3D false; + break; + default: + return -EINVAL; + } + + if (write) + cmd_count =3D 3; + else + cmd_count =3D 2; + + idata =3D alloc_idata(rpmb, cmd_count); + if (!idata) + return -ENOMEM; + + if (write) { + struct rpmb_frame *frm =3D (struct rpmb_frame *)resp; + + /* Send write request frame(s) */ + set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, + 1 | MMC_CMD23_ARG_REL_WR, req, req_len); + + /* Send result request frame */ + memset(frm, 0, sizeof(*frm)); + frm->req_resp =3D cpu_to_be16(RPMB_RESULT_READ); + set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp, + resp_len); + + /* Read response frame */ + set_idata(idata[2], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); + } else { + /* Send write request frame(s) */ + set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK, 1, req, req_len); + + /* Read response frame */ + set_idata(idata[1], MMC_READ_MULTIPLE_BLOCK, 0, resp, resp_len); + } + + rq =3D blk_mq_alloc_request(md->queue.queue, REQ_OP_DRV_OUT, 0); + if (IS_ERR(rq)) { + ret =3D PTR_ERR(rq); + goto out; + } + + mq_rq =3D req_to_mmc_queue_req(rq); + mq_rq->drv_op =3D MMC_DRV_OP_IOCTL_RPMB; + mq_rq->drv_op_result =3D -EIO; + mq_rq->drv_op_data =3D idata; + mq_rq->ioc_count =3D cmd_count; + blk_execute_rq(rq, false); + ret =3D req_to_mmc_queue_req(rq)->drv_op_result; + + blk_mq_free_request(rq); + +out: + free_idata(idata, cmd_count); + return ret; +} + static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, struct mmc_blk_data *md, unsigned int part_index, @@ -2741,6 +2939,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *c= ard, rpmb->dev.release =3D mmc_blk_rpmb_device_release; device_initialize(&rpmb->dev); dev_set_drvdata(&rpmb->dev, rpmb); + mmc_blk_get(md->disk); rpmb->md =3D md; =20 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); @@ -3002,6 +3201,42 @@ static void mmc_blk_remove_debugfs(struct mmc_card *= card, =20 #endif /* CONFIG_DEBUG_FS */ =20 +static void mmc_blk_rpmb_add(struct mmc_card *card) +{ + struct mmc_blk_data *md =3D dev_get_drvdata(&card->dev); + struct mmc_rpmb_data *rpmb; + struct rpmb_dev *rdev; + unsigned int n; + u32 cid[4]; + struct rpmb_descr descr =3D { + .type =3D RPMB_TYPE_EMMC, + .route_frames =3D mmc_route_rpmb_frames, + .reliable_wr_count =3D card->ext_csd.enhanced_rpmb_supported ? + 2 : 32, + .capacity =3D card->ext_csd.raw_rpmb_size_mult, + .dev_id =3D (void *)cid, + .dev_id_len =3D sizeof(cid), + }; + + /* + * Provice CID as an octet array. The CID needs to be interpreted + * when used as input to derive the RPMB key since some fields + * will change due to firmware updates. + */ + for (n =3D 0; n < 4; n++) + cid[n] =3D be32_to_cpu(card->raw_cid[n]); + + list_for_each_entry(rpmb, &md->rpmbs, node) { + rdev =3D rpmb_dev_register(&rpmb->dev, &descr); + if (IS_ERR(rdev)) { + pr_warn("%s: could not register RPMB device\n", + dev_name(&rpmb->dev)); + continue; + } + rpmb->rdev =3D rdev; + } +} + static int mmc_blk_probe(struct mmc_card *card) { struct mmc_blk_data *md; @@ -3047,6 +3282,8 @@ static int mmc_blk_probe(struct mmc_card *card) pm_runtime_enable(&card->dev); } =20 + mmc_blk_rpmb_add(card); + return 0; =20 out: --=20 2.34.1