1 From foo@baz Tue Oct 2 04:59:29 PDT 2018
2 From: Srikanth Jampala <Jampala.Srikanth@cavium.com>
3 Date: Wed, 22 Aug 2018 12:40:52 +0530
4 Subject: crypto: cavium/nitrox - fix for command corruption in queue full case with backlog submissions.
6 From: Srikanth Jampala <Jampala.Srikanth@cavium.com>
8 [ Upstream commit 3d7c82060d1fe65bde4023aac41a0b1bd7718e07 ]
10 Earlier used to post the current command without checking queue full
11 after backlog submissions. So, post the current command only after
12 confirming the space in queue after backlog submissions.
14 Maintain host write index instead of reading device registers
15 to get the next free slot to post the command.
17 Return -ENOSPC in queue full case.
19 Signed-off-by: Srikanth Jampala <Jampala.Srikanth@cavium.com>
20 Reviewed-by: Gadam Sreerama <sgadam@cavium.com>
21 Tested-by: Jha, Chandan <Chandan.Jha@cavium.com>
22 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
23 Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
24 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
26 drivers/crypto/cavium/nitrox/nitrox_dev.h | 3 -
27 drivers/crypto/cavium/nitrox/nitrox_lib.c | 1
28 drivers/crypto/cavium/nitrox/nitrox_reqmgr.c | 57 +++++++++++++++------------
29 3 files changed, 35 insertions(+), 26 deletions(-)
31 --- a/drivers/crypto/cavium/nitrox/nitrox_dev.h
32 +++ b/drivers/crypto/cavium/nitrox/nitrox_dev.h
33 @@ -35,6 +35,7 @@ struct nitrox_cmdq {
34 /* requests in backlog queues */
35 atomic_t backlog_count;
38 /* command size 32B/64B */
41 @@ -87,7 +88,7 @@ struct nitrox_bh {
45 -/* NITROX-5 driver state */
46 +/* NITROX-V driver state */
47 #define NITROX_UCODE_LOADED 0
48 #define NITROX_READY 1
50 --- a/drivers/crypto/cavium/nitrox/nitrox_lib.c
51 +++ b/drivers/crypto/cavium/nitrox/nitrox_lib.c
52 @@ -36,6 +36,7 @@ static int cmdq_common_init(struct nitro
53 cmdq->head = PTR_ALIGN(cmdq->head_unaligned, PKT_IN_ALIGN);
54 cmdq->dma = PTR_ALIGN(cmdq->dma_unaligned, PKT_IN_ALIGN);
55 cmdq->qsize = (qsize + PKT_IN_ALIGN);
56 + cmdq->write_idx = 0;
58 spin_lock_init(&cmdq->response_lock);
59 spin_lock_init(&cmdq->cmdq_lock);
60 --- a/drivers/crypto/cavium/nitrox/nitrox_reqmgr.c
61 +++ b/drivers/crypto/cavium/nitrox/nitrox_reqmgr.c
63 * Invalid flag options in AES-CCM IV.
66 +static inline int incr_index(int index, int count, int max)
68 + if ((index + count) >= max)
69 + index = index + count - max;
77 * dma_free_sglist - unmap and free the sg lists.
79 @@ -426,30 +436,29 @@ static void post_se_instr(struct nitrox_
80 struct nitrox_cmdq *cmdq)
82 struct nitrox_device *ndev = sr->ndev;
83 - union nps_pkt_in_instr_baoff_dbell pkt_in_baoff_dbell;
88 spin_lock_bh(&cmdq->cmdq_lock);
90 - /* get the next write offset */
91 - offset = NPS_PKT_IN_INSTR_BAOFF_DBELLX(cmdq->qno);
92 - pkt_in_baoff_dbell.value = nitrox_read_csr(ndev, offset);
93 + idx = cmdq->write_idx;
94 /* copy the instruction */
95 - ent = cmdq->head + pkt_in_baoff_dbell.s.aoff;
96 + ent = cmdq->head + (idx * cmdq->instr_size);
97 memcpy(ent, &sr->instr, cmdq->instr_size);
98 - /* flush the command queue updates */
101 - sr->tstamp = jiffies;
102 atomic_set(&sr->status, REQ_POSTED);
103 response_list_add(sr, cmdq);
104 + sr->tstamp = jiffies;
105 + /* flush the command queue updates */
108 /* Ring doorbell with count 1 */
109 writeq(1, cmdq->dbell_csr_addr);
110 /* orders the doorbell rings */
113 + cmdq->write_idx = incr_index(idx, 1, ndev->qlen);
115 spin_unlock_bh(&cmdq->cmdq_lock);
118 @@ -459,6 +468,9 @@ static int post_backlog_cmds(struct nitr
119 struct nitrox_softreq *sr, *tmp;
122 + if (!atomic_read(&cmdq->backlog_count))
125 spin_lock_bh(&cmdq->backlog_lock);
127 list_for_each_entry_safe(sr, tmp, &cmdq->backlog_head, backlog) {
128 @@ -466,7 +478,7 @@ static int post_backlog_cmds(struct nitr
130 /* submit until space available */
131 if (unlikely(cmdq_full(cmdq, ndev->qlen))) {
136 /* delete from backlog list */
137 @@ -491,23 +503,20 @@ static int nitrox_enqueue_request(struct
139 struct nitrox_cmdq *cmdq = sr->cmdq;
140 struct nitrox_device *ndev = sr->ndev;
143 + /* try to post backlog requests */
144 + post_backlog_cmds(cmdq);
146 if (unlikely(cmdq_full(cmdq, ndev->qlen))) {
147 if (!(sr->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
151 + /* add to backlog list */
152 backlog_list_add(sr, cmdq);
154 - ret = post_backlog_cmds(cmdq);
156 - backlog_list_add(sr, cmdq);
159 - post_se_instr(sr, cmdq);
160 - ret = -EINPROGRESS;
164 + post_se_instr(sr, cmdq);
166 + return -EINPROGRESS;
170 @@ -624,11 +633,9 @@ int nitrox_process_se_request(struct nit
172 sr->instr.fdata[0] = *((u64 *)&req->gph);
173 sr->instr.fdata[1] = 0;
174 - /* flush the soft_req changes before posting the cmd */
177 ret = nitrox_enqueue_request(sr);
178 - if (ret == -EAGAIN)
179 + if (ret == -ENOSPC)