diff options
author | Jon Derrick <jonathan.derrick@intel.com> | 2017-02-21 13:59:14 -0500 |
---|---|---|
committer | Jens Axboe <axboe@fb.com> | 2017-02-22 13:54:49 -0500 |
commit | cccb92417d447172c4653876f6cd4b04c29d7905 (patch) | |
tree | 7947631e4dae50b13821477d359fa685bea4105b /block/sed-opal.c | |
parent | aedb6e2411baf242fd0faf95bd3ff0dc62ee1fa5 (diff) |
block/sed: Add helper to qualify response tokens
Add helper which verifies the response token is valid and matches the
expected value. Merges token_type and response_get_token.
Signed-off-by: Jon Derrick <jonathan.derrick@intel.com>
Reviewed-by: Scott Bauer <scott.bauer@intel.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@fb.com>
Diffstat (limited to 'block/sed-opal.c')
-rw-r--r-- | block/sed-opal.c | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/block/sed-opal.c b/block/sed-opal.c index 4675fd8eed7b..d3d6db2877b9 100644 --- a/block/sed-opal.c +++ b/block/sed-opal.c | |||
@@ -662,48 +662,25 @@ static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn) | |||
662 | return 0; | 662 | return 0; |
663 | } | 663 | } |
664 | 664 | ||
665 | static enum opal_response_token token_type(const struct parsed_resp *resp, | 665 | static const struct opal_resp_tok *response_get_token( |
666 | int n) | 666 | const struct parsed_resp *resp, |
667 | int n) | ||
667 | { | 668 | { |
668 | const struct opal_resp_tok *tok; | 669 | const struct opal_resp_tok *tok; |
669 | 670 | ||
670 | if (n >= resp->num) { | 671 | if (n >= resp->num) { |
671 | pr_err("Token number doesn't exist: %d, resp: %d\n", | 672 | pr_err("Token number doesn't exist: %d, resp: %d\n", |
672 | n, resp->num); | 673 | n, resp->num); |
673 | return OPAL_DTA_TOKENID_INVALID; | 674 | return ERR_PTR(-EINVAL); |
674 | } | 675 | } |
675 | 676 | ||
676 | tok = &resp->toks[n]; | 677 | tok = &resp->toks[n]; |
677 | if (tok->len == 0) { | 678 | if (tok->len == 0) { |
678 | pr_err("Token length must be non-zero\n"); | 679 | pr_err("Token length must be non-zero\n"); |
679 | return OPAL_DTA_TOKENID_INVALID; | 680 | return ERR_PTR(-EINVAL); |
680 | } | 681 | } |
681 | 682 | ||
682 | return tok->type; | 683 | return tok; |
683 | } | ||
684 | |||
685 | /* | ||
686 | * This function returns 0 in case of invalid token. One should call | ||
687 | * token_type() first to find out if the token is valid or not. | ||
688 | */ | ||
689 | static enum opal_token response_get_token(const struct parsed_resp *resp, | ||
690 | int n) | ||
691 | { | ||
692 | const struct opal_resp_tok *tok; | ||
693 | |||
694 | if (n >= resp->num) { | ||
695 | pr_err("Token number doesn't exist: %d, resp: %d\n", | ||
696 | n, resp->num); | ||
697 | return 0; | ||
698 | } | ||
699 | |||
700 | tok = &resp->toks[n]; | ||
701 | if (tok->len == 0) { | ||
702 | pr_err("Token length must be non-zero\n"); | ||
703 | return 0; | ||
704 | } | ||
705 | |||
706 | return tok->pos[0]; | ||
707 | } | 684 | } |
708 | 685 | ||
709 | static ssize_t response_parse_tiny(struct opal_resp_tok *tok, | 686 | static ssize_t response_parse_tiny(struct opal_resp_tok *tok, |
@@ -922,20 +899,32 @@ static u64 response_get_u64(const struct parsed_resp *resp, int n) | |||
922 | return resp->toks[n].stored.u; | 899 | return resp->toks[n].stored.u; |
923 | } | 900 | } |
924 | 901 | ||
902 | static bool response_token_matches(const struct opal_resp_tok *token, u8 match) | ||
903 | { | ||
904 | if (IS_ERR(token) || | ||
905 | token->type != OPAL_DTA_TOKENID_TOKEN || | ||
906 | token->pos[0] != match) | ||
907 | return false; | ||
908 | return true; | ||
909 | } | ||
910 | |||
925 | static u8 response_status(const struct parsed_resp *resp) | 911 | static u8 response_status(const struct parsed_resp *resp) |
926 | { | 912 | { |
927 | if (token_type(resp, 0) == OPAL_DTA_TOKENID_TOKEN && | 913 | const struct opal_resp_tok *tok; |
928 | response_get_token(resp, 0) == OPAL_ENDOFSESSION) { | 914 | |
915 | tok = response_get_token(resp, 0); | ||
916 | if (response_token_matches(tok, OPAL_ENDOFSESSION)) | ||
929 | return 0; | 917 | return 0; |
930 | } | ||
931 | 918 | ||
932 | if (resp->num < 5) | 919 | if (resp->num < 5) |
933 | return DTAERROR_NO_METHOD_STATUS; | 920 | return DTAERROR_NO_METHOD_STATUS; |
934 | 921 | ||
935 | if (token_type(resp, resp->num - 1) != OPAL_DTA_TOKENID_TOKEN || | 922 | tok = response_get_token(resp, resp->num - 5); |
936 | token_type(resp, resp->num - 5) != OPAL_DTA_TOKENID_TOKEN || | 923 | if (!response_token_matches(tok, OPAL_STARTLIST)) |
937 | response_get_token(resp, resp->num - 1) != OPAL_ENDLIST || | 924 | return DTAERROR_NO_METHOD_STATUS; |
938 | response_get_token(resp, resp->num - 5) != OPAL_STARTLIST) | 925 | |
926 | tok = response_get_token(resp, resp->num - 1); | ||
927 | if (!response_token_matches(tok, OPAL_ENDLIST)) | ||
939 | return DTAERROR_NO_METHOD_STATUS; | 928 | return DTAERROR_NO_METHOD_STATUS; |
940 | 929 | ||
941 | return response_get_u64(resp, resp->num - 4); | 930 | return response_get_u64(resp, resp->num - 4); |