From patchwork Mon Sep 3 14:46:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 145802 Delivered-To: patches@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp2575457ljw; Mon, 3 Sep 2018 07:47:26 -0700 (PDT) X-Received: by 2002:a2e:5bd5:: with SMTP id m82-v6mr18908197lje.115.1535986046022; Mon, 03 Sep 2018 07:47:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535986046; cv=none; d=google.com; s=arc-20160816; b=coMNNqGKBu9/EnHb279NPfO4z8Q/XxfYKJUG3pCUhipHtfNANL0Ffyk83ZKu7WGD6S Y0eKPJ9SYBVbf9V8SyswakTWIeb0EiKXsh9UNtV9u5hR3ArsPpUZA8YWUPQA7+Vu3YQ6 Yrr5Y5ZNA8ovJRRkhQdsYG6ykaPUGc+U2v1t4kueQUXldXIOp1D+eFX4ILiSm4+JYpes JjPsC6ZgTx5jlXuNwRWmCopLiF72Os9aw+B9CdIJjmsGRL0Au7RCw+Hpk0I36z70TwYC RWvaMbWs0takmwIK7rtKy2Y5M/fk0nC+BVh83CYpVvV3ND/BQn8ft3BDU697cJkIdUyg MdyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=u/0UF/7NtCAZYvPhEOE+mzk3+7xY9MuJlAagyF0alyw=; b=Em58ebE9k0S1j2jQ9JoG/j5vRtl1DZ7y2S9EfBNYed1cW/D7q9QR96jM+4XI9YJYy+ FojbxO/Nt26xEHWNPC3zrvlTUkyOzGj9Turn2YesKY4irgAf8xhoIFP/wYUC8qJj7QBA ZVzwxmJdITEi8RWPrE6LXZK9MeJQ2jzYIQvIvJeNkrvaR9NAIo2eCEIE8nfFrvTc13JT 0FQPLbVAP6JfWqRglNtwgVLbyceRatT7d9T7NGB5S7jsufBRKuECd+kFn8xRmOI+ZeYI TcpFxIeRnDWWP7Vpg3QxT+GVcG3dua8yECja3faj5dsdHKyL560xukgueHecX4xrvgsb xT0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TemU7UBD; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id m23-v6sor7899662ljj.33.2018.09.03.07.47.25 for (Google Transport Security); Mon, 03 Sep 2018 07:47:26 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TemU7UBD; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=u/0UF/7NtCAZYvPhEOE+mzk3+7xY9MuJlAagyF0alyw=; b=TemU7UBDxXtLNW+uQNXowqsMXNhjZMyo0a0PAAqDjb8BBuii3ZZpfpJYk+4gonX66G tuuSSTTvVc5IoPXggp5agRnDXi2JT5alvRTD1f3aagR6zy4RUl5AwN5sQXaLObTUigVn 6YVjdK0MFGdN3zMrUJ8I5rKDt9sK9spWXxfO4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=u/0UF/7NtCAZYvPhEOE+mzk3+7xY9MuJlAagyF0alyw=; b=Ryb1U+RVQgDlwdkjgw7w9iu5iZsJ6Fut8gdblZwjUO2pAmJUJ/RuGGHX/jsVOa9rDc fdntD5IbOVcpsLPLfhrSn0HcOztkc8Clhwu8OlTGFlsPYBk6Ei1pj0PHPNsJvwjEGzIJ n2+926S+Op2KUpURinSAD1JpOpoEQ4hyNBo/GcLXqnTkRGhMWO8dqeddDw8m1uPjmfq6 TiXK+bjJRke6iXpG5XcUXiBX5buzS0UAkS5gjDLMNuUbYld+p1X9/7/WVwfgZh+fGFu0 T28ty/1Qhmpc+UwB5WyYvvLhbRMi6+/5SBK7/ZC9tEOx+LNd5N7mGDfn/g6S6tIoRj+/ lL2A== X-Gm-Message-State: APzg51BO+yLOOEbOys6dEjl86NnqgNFxeYLkopANsNzqjG6kPRJL7FBE E70+H2y+abfFxljmCqwSsTTqyHPq X-Google-Smtp-Source: ANB0VdakHXkWiMfAQC+31JnfmrhuMv8sw6oujCAE8CNnnDCYPqHpui6sAy35pa+jTKfLLjs4Zx8xGw== X-Received: by 2002:a2e:89c5:: with SMTP id c5-v6mr12333058ljk.19.1535986045782; Mon, 03 Sep 2018 07:47:25 -0700 (PDT) Return-Path: Received: from jax.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id w18-v6sm3343431ljd.73.2018.09.03.07.47.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Sep 2018 07:47:25 -0700 (PDT) From: Jens Wiklander To: u-boot@lists.denx.de Cc: Simon Glass , Igor Opaniuk , Tom Rini , Jaehoon Chung , Pierre Aubert , Albert Aribaud , Peter Griffin , Michal Simek , Jens Wiklander Subject: [PATCH v3 05/20] mmc: rpmb: add mmc_rpmb_route_frames() Date: Mon, 3 Sep 2018 16:46:56 +0200 Message-Id: <20180903144711.31585-6-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180903144711.31585-1-jens.wiklander@linaro.org> References: <20180903144711.31585-1-jens.wiklander@linaro.org> Adds mmc_rpmb_route_frames() to route RPMB data frames from/to an external entity. Tested-by: Igor Opaniuk Signed-off-by: Jens Wiklander --- drivers/mmc/rpmb.c | 160 +++++++++++++++++++++++++++++++++++++++++++++ include/mmc.h | 2 + 2 files changed, 162 insertions(+) -- 2.17.1 diff --git a/drivers/mmc/rpmb.c b/drivers/mmc/rpmb.c index dfbdb0deb107..908f19208955 100644 --- a/drivers/mmc/rpmb.c +++ b/drivers/mmc/rpmb.c @@ -321,3 +321,163 @@ int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk, } return i; } + +static int send_write_mult_block(struct mmc *mmc, const struct s_rpmb *frm, + unsigned short cnt) +{ + struct mmc_cmd cmd = { + .cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK, + .resp_type = MMC_RSP_R1b, + }; + struct mmc_data data = { + .src = (const void *)frm, + .blocks = cnt, + .blocksize = sizeof(*frm), + .flags = MMC_DATA_WRITE, + }; + + return mmc_send_cmd(mmc, &cmd, &data); +} + +static int send_read_mult_block(struct mmc *mmc, struct s_rpmb *frm, + unsigned short cnt) +{ + struct mmc_cmd cmd = { + .cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK, + .resp_type = MMC_RSP_R1, + }; + struct mmc_data data = { + .dest = (void *)frm, + .blocks = cnt, + .blocksize = sizeof(*frm), + .flags = MMC_DATA_READ, + }; + + return mmc_send_cmd(mmc, &cmd, &data); +} + +static int rpmb_route_write_req(struct mmc *mmc, struct s_rpmb *req, + unsigned short req_cnt, struct s_rpmb *rsp, + unsigned short rsp_cnt) +{ + int ret; + + /* + * Send the write request. + */ + ret = mmc_set_blockcount(mmc, req_cnt, true); + if (ret) + return ret; + + ret = send_write_mult_block(mmc, req, req_cnt); + if (ret) + return ret; + + /* + * Read the result of the request. + */ + ret = mmc_set_blockcount(mmc, 1, false); + if (ret) + return ret; + + memset(rsp, 0, sizeof(*rsp)); + rsp->request = cpu_to_be16(RPMB_REQ_STATUS); + ret = send_write_mult_block(mmc, rsp, 1); + if (ret) + return ret; + + ret = mmc_set_blockcount(mmc, 1, false); + if (ret) + return ret; + + return send_read_mult_block(mmc, rsp, 1); +} + +static int rpmb_route_read_req(struct mmc *mmc, struct s_rpmb *req, + unsigned short req_cnt, struct s_rpmb *rsp, + unsigned short rsp_cnt) +{ + int ret; + + /* + * Send the read request. + */ + ret = mmc_set_blockcount(mmc, 1, false); + if (ret) + return ret; + + ret = send_write_mult_block(mmc, req, 1); + if (ret) + return ret; + + /* + * Read the result of the request. + */ + + ret = mmc_set_blockcount(mmc, rsp_cnt, false); + if (ret) + return ret; + + return send_read_mult_block(mmc, rsp, rsp_cnt); +} + +static int rpmb_route_frames(struct mmc *mmc, struct s_rpmb *req, + unsigned short req_cnt, struct s_rpmb *rsp, + unsigned short rsp_cnt) +{ + unsigned short n; + + /* + * If multiple request frames are provided, make sure that all are + * of the same type. + */ + for (n = 1; n < req_cnt; n++) + if (req[n].request != req->request) + return -EINVAL; + + switch (be16_to_cpu(req->request)) { + case RPMB_REQ_KEY: + if (req_cnt != 1 || rsp_cnt != 1) + return -EINVAL; + return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt); + + case RPMB_REQ_WRITE_DATA: + if (!req_cnt || rsp_cnt != 1) + return -EINVAL; + return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt); + + case RPMB_REQ_WCOUNTER: + if (req_cnt != 1 || rsp_cnt != 1) + return -EINVAL; + return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt); + + case RPMB_REQ_READ_DATA: + if (req_cnt != 1 || !req_cnt) + return -EINVAL; + return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt); + + default: + debug("Unsupported message type: %d\n", + be16_to_cpu(req->request)); + return -EINVAL; + } +} + +int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen, + void *rsp, unsigned long rsplen) +{ + /* + * Whoever crafted the data supplied to this function knows how to + * format the PRMB frames and which response is expected. If + * there's some unexpected mismatch it's more helpful to report an + * error immediately than trying to guess what was the intention + * and possibly just delay an eventual error which will be harder + * to track down. + */ + + if (reqlen % sizeof(struct s_rpmb) || rsplen % sizeof(struct s_rpmb)) + return -EINVAL; + + return rpmb_route_frames(mmc, req, reqlen / sizeof(struct s_rpmb), + rsp, rsplen / sizeof(struct s_rpmb)); +} diff --git a/include/mmc.h b/include/mmc.h index df4255b828a7..d6e02af4edea 100644 --- a/include/mmc.h +++ b/include/mmc.h @@ -748,6 +748,8 @@ int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk, unsigned short cnt, unsigned char *key); int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk, unsigned short cnt, unsigned char *key); +int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen, + void *rsp, unsigned long rsplen); #ifdef CONFIG_CMD_BKOPS_ENABLE int mmc_set_bkops_enable(struct mmc *mmc); #endif