Path: blob/master/drivers/crypto/marvell/octeontx/otx_cptvf_reqmgr.c
26288 views
// SPDX-License-Identifier: GPL-2.01/* Marvell OcteonTX CPT driver2*3* Copyright (C) 2019 Marvell International Ltd.4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License version 2 as7* published by the Free Software Foundation.8*/910#include "otx_cptvf.h"11#include "otx_cptvf_algs.h"1213/* Completion code size and initial value */14#define COMPLETION_CODE_SIZE 815#define COMPLETION_CODE_INIT 01617/* SG list header size in bytes */18#define SG_LIST_HDR_SIZE 81920/* Default timeout when waiting for free pending entry in us */21#define CPT_PENTRY_TIMEOUT 100022#define CPT_PENTRY_STEP 502324/* Default threshold for stopping and resuming sender requests */25#define CPT_IQ_STOP_MARGIN 12826#define CPT_IQ_RESUME_MARGIN 5122728#define CPT_DMA_ALIGN 1282930void otx_cpt_dump_sg_list(struct pci_dev *pdev, struct otx_cpt_req_info *req)31{32int i;3334pr_debug("Gather list size %d\n", req->incnt);35for (i = 0; i < req->incnt; i++) {36pr_debug("Buffer %d size %d, vptr 0x%p, dmaptr 0x%p\n", i,37req->in[i].size, req->in[i].vptr,38(void *) req->in[i].dma_addr);39pr_debug("Buffer hexdump (%d bytes)\n",40req->in[i].size);41print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1,42req->in[i].vptr, req->in[i].size, false);43}4445pr_debug("Scatter list size %d\n", req->outcnt);46for (i = 0; i < req->outcnt; i++) {47pr_debug("Buffer %d size %d, vptr 0x%p, dmaptr 0x%p\n", i,48req->out[i].size, req->out[i].vptr,49(void *) req->out[i].dma_addr);50pr_debug("Buffer hexdump (%d bytes)\n", req->out[i].size);51print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1,52req->out[i].vptr, req->out[i].size, false);53}54}5556static inline struct otx_cpt_pending_entry *get_free_pending_entry(57struct otx_cpt_pending_queue *q,58int qlen)59{60struct otx_cpt_pending_entry *ent = NULL;6162ent = &q->head[q->rear];63if (unlikely(ent->busy))64return NULL;6566q->rear++;67if (unlikely(q->rear == qlen))68q->rear = 0;6970return ent;71}7273static inline u32 modulo_inc(u32 index, u32 length, u32 inc)74{75if (WARN_ON(inc > length))76inc = length;7778index += inc;79if (unlikely(index >= length))80index -= length;8182return index;83}8485static inline void free_pentry(struct otx_cpt_pending_entry *pentry)86{87pentry->completion_addr = NULL;88pentry->info = NULL;89pentry->callback = NULL;90pentry->areq = NULL;91pentry->resume_sender = false;92pentry->busy = false;93}9495static inline int setup_sgio_components(struct pci_dev *pdev,96struct otx_cpt_buf_ptr *list,97int buf_count, u8 *buffer)98{99struct otx_cpt_sglist_component *sg_ptr = NULL;100int ret = 0, i, j;101int components;102103if (unlikely(!list)) {104dev_err(&pdev->dev, "Input list pointer is NULL\n");105return -EFAULT;106}107108for (i = 0; i < buf_count; i++) {109if (likely(list[i].vptr)) {110list[i].dma_addr = dma_map_single(&pdev->dev,111list[i].vptr,112list[i].size,113DMA_BIDIRECTIONAL);114if (unlikely(dma_mapping_error(&pdev->dev,115list[i].dma_addr))) {116dev_err(&pdev->dev, "Dma mapping failed\n");117ret = -EIO;118goto sg_cleanup;119}120}121}122123components = buf_count / 4;124sg_ptr = (struct otx_cpt_sglist_component *)buffer;125for (i = 0; i < components; i++) {126sg_ptr->u.s.len0 = cpu_to_be16(list[i * 4 + 0].size);127sg_ptr->u.s.len1 = cpu_to_be16(list[i * 4 + 1].size);128sg_ptr->u.s.len2 = cpu_to_be16(list[i * 4 + 2].size);129sg_ptr->u.s.len3 = cpu_to_be16(list[i * 4 + 3].size);130sg_ptr->ptr0 = cpu_to_be64(list[i * 4 + 0].dma_addr);131sg_ptr->ptr1 = cpu_to_be64(list[i * 4 + 1].dma_addr);132sg_ptr->ptr2 = cpu_to_be64(list[i * 4 + 2].dma_addr);133sg_ptr->ptr3 = cpu_to_be64(list[i * 4 + 3].dma_addr);134sg_ptr++;135}136components = buf_count % 4;137138switch (components) {139case 3:140sg_ptr->u.s.len2 = cpu_to_be16(list[i * 4 + 2].size);141sg_ptr->ptr2 = cpu_to_be64(list[i * 4 + 2].dma_addr);142fallthrough;143case 2:144sg_ptr->u.s.len1 = cpu_to_be16(list[i * 4 + 1].size);145sg_ptr->ptr1 = cpu_to_be64(list[i * 4 + 1].dma_addr);146fallthrough;147case 1:148sg_ptr->u.s.len0 = cpu_to_be16(list[i * 4 + 0].size);149sg_ptr->ptr0 = cpu_to_be64(list[i * 4 + 0].dma_addr);150break;151default:152break;153}154return ret;155156sg_cleanup:157for (j = 0; j < i; j++) {158if (list[j].dma_addr) {159dma_unmap_single(&pdev->dev, list[i].dma_addr,160list[i].size, DMA_BIDIRECTIONAL);161}162163list[j].dma_addr = 0;164}165return ret;166}167168static inline int setup_sgio_list(struct pci_dev *pdev,169struct otx_cpt_info_buffer **pinfo,170struct otx_cpt_req_info *req, gfp_t gfp)171{172u32 dlen, align_dlen, info_len, rlen;173struct otx_cpt_info_buffer *info;174u16 g_sz_bytes, s_sz_bytes;175int align = CPT_DMA_ALIGN;176u32 total_mem_len;177178if (unlikely(req->incnt > OTX_CPT_MAX_SG_IN_CNT ||179req->outcnt > OTX_CPT_MAX_SG_OUT_CNT)) {180dev_err(&pdev->dev, "Error too many sg components\n");181return -EINVAL;182}183184g_sz_bytes = ((req->incnt + 3) / 4) *185sizeof(struct otx_cpt_sglist_component);186s_sz_bytes = ((req->outcnt + 3) / 4) *187sizeof(struct otx_cpt_sglist_component);188189dlen = g_sz_bytes + s_sz_bytes + SG_LIST_HDR_SIZE;190align_dlen = ALIGN(dlen, align);191info_len = ALIGN(sizeof(*info), align);192rlen = ALIGN(sizeof(union otx_cpt_res_s), align);193total_mem_len = align_dlen + info_len + rlen + COMPLETION_CODE_SIZE;194195info = kzalloc(total_mem_len, gfp);196if (unlikely(!info)) {197dev_err(&pdev->dev, "Memory allocation failed\n");198return -ENOMEM;199}200*pinfo = info;201info->dlen = dlen;202info->in_buffer = (u8 *)info + info_len;203204((__be16 *)info->in_buffer)[0] = cpu_to_be16(req->outcnt);205((__be16 *)info->in_buffer)[1] = cpu_to_be16(req->incnt);206((u16 *)info->in_buffer)[2] = 0;207((u16 *)info->in_buffer)[3] = 0;208209/* Setup gather (input) components */210if (setup_sgio_components(pdev, req->in, req->incnt,211&info->in_buffer[8])) {212dev_err(&pdev->dev, "Failed to setup gather list\n");213return -EFAULT;214}215216if (setup_sgio_components(pdev, req->out, req->outcnt,217&info->in_buffer[8 + g_sz_bytes])) {218dev_err(&pdev->dev, "Failed to setup scatter list\n");219return -EFAULT;220}221222info->dma_len = total_mem_len - info_len;223info->dptr_baddr = dma_map_single(&pdev->dev, (void *)info->in_buffer,224info->dma_len, DMA_BIDIRECTIONAL);225if (unlikely(dma_mapping_error(&pdev->dev, info->dptr_baddr))) {226dev_err(&pdev->dev, "DMA Mapping failed for cpt req\n");227return -EIO;228}229/*230* Get buffer for union otx_cpt_res_s response231* structure and its physical address232*/233info->completion_addr = (u64 *)(info->in_buffer + align_dlen);234info->comp_baddr = info->dptr_baddr + align_dlen;235236/* Create and initialize RPTR */237info->out_buffer = (u8 *)info->completion_addr + rlen;238info->rptr_baddr = info->comp_baddr + rlen;239240*((u64 *) info->out_buffer) = ~((u64) COMPLETION_CODE_INIT);241242return 0;243}244245246static void cpt_fill_inst(union otx_cpt_inst_s *inst,247struct otx_cpt_info_buffer *info,248struct otx_cpt_iq_cmd *cmd)249{250inst->u[0] = 0x0;251inst->s.doneint = true;252inst->s.res_addr = (u64)info->comp_baddr;253inst->u[2] = 0x0;254inst->s.wq_ptr = 0;255inst->s.ei0 = cmd->cmd.u64;256inst->s.ei1 = cmd->dptr;257inst->s.ei2 = cmd->rptr;258inst->s.ei3 = cmd->cptr.u64;259}260261/*262* On OcteonTX platform the parameter db_count is used as a count for ringing263* door bell. The valid values for db_count are:264* 0 - 1 CPT instruction will be enqueued however CPT will not be informed265* 1 - 1 CPT instruction will be enqueued and CPT will be informed266*/267static void cpt_send_cmd(union otx_cpt_inst_s *cptinst, struct otx_cptvf *cptvf)268{269struct otx_cpt_cmd_qinfo *qinfo = &cptvf->cqinfo;270struct otx_cpt_cmd_queue *queue;271struct otx_cpt_cmd_chunk *curr;272u8 *ent;273274queue = &qinfo->queue[0];275/*276* cpt_send_cmd is currently called only from critical section277* therefore no locking is required for accessing instruction queue278*/279ent = &queue->qhead->head[queue->idx * OTX_CPT_INST_SIZE];280memcpy(ent, (void *) cptinst, OTX_CPT_INST_SIZE);281282if (++queue->idx >= queue->qhead->size / 64) {283curr = queue->qhead;284285if (list_is_last(&curr->nextchunk, &queue->chead))286queue->qhead = queue->base;287else288queue->qhead = list_next_entry(queue->qhead, nextchunk);289queue->idx = 0;290}291/* make sure all memory stores are done before ringing doorbell */292smp_wmb();293otx_cptvf_write_vq_doorbell(cptvf, 1);294}295296static int process_request(struct pci_dev *pdev, struct otx_cpt_req_info *req,297struct otx_cpt_pending_queue *pqueue,298struct otx_cptvf *cptvf)299{300struct otx_cptvf_request *cpt_req = &req->req;301struct otx_cpt_pending_entry *pentry = NULL;302union otx_cpt_ctrl_info *ctrl = &req->ctrl;303struct otx_cpt_info_buffer *info = NULL;304union otx_cpt_res_s *result = NULL;305struct otx_cpt_iq_cmd iq_cmd;306union otx_cpt_inst_s cptinst;307int retry, ret = 0;308u8 resume_sender;309gfp_t gfp;310311gfp = (req->areq->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :312GFP_ATOMIC;313ret = setup_sgio_list(pdev, &info, req, gfp);314if (unlikely(ret)) {315dev_err(&pdev->dev, "Setting up SG list failed\n");316goto request_cleanup;317}318cpt_req->dlen = info->dlen;319320result = (union otx_cpt_res_s *) info->completion_addr;321result->s.compcode = COMPLETION_CODE_INIT;322323spin_lock_bh(&pqueue->lock);324pentry = get_free_pending_entry(pqueue, pqueue->qlen);325retry = CPT_PENTRY_TIMEOUT / CPT_PENTRY_STEP;326while (unlikely(!pentry) && retry--) {327spin_unlock_bh(&pqueue->lock);328udelay(CPT_PENTRY_STEP);329spin_lock_bh(&pqueue->lock);330pentry = get_free_pending_entry(pqueue, pqueue->qlen);331}332333if (unlikely(!pentry)) {334ret = -ENOSPC;335spin_unlock_bh(&pqueue->lock);336goto request_cleanup;337}338339/*340* Check if we are close to filling in entire pending queue,341* if so then tell the sender to stop/sleep by returning -EBUSY342* We do it only for context which can sleep (GFP_KERNEL)343*/344if (gfp == GFP_KERNEL &&345pqueue->pending_count > (pqueue->qlen - CPT_IQ_STOP_MARGIN)) {346pentry->resume_sender = true;347} else348pentry->resume_sender = false;349resume_sender = pentry->resume_sender;350pqueue->pending_count++;351352pentry->completion_addr = info->completion_addr;353pentry->info = info;354pentry->callback = req->callback;355pentry->areq = req->areq;356pentry->busy = true;357info->pentry = pentry;358info->time_in = jiffies;359info->req = req;360361/* Fill in the command */362iq_cmd.cmd.u64 = 0;363iq_cmd.cmd.s.opcode = cpu_to_be16(cpt_req->opcode.flags);364iq_cmd.cmd.s.param1 = cpu_to_be16(cpt_req->param1);365iq_cmd.cmd.s.param2 = cpu_to_be16(cpt_req->param2);366iq_cmd.cmd.s.dlen = cpu_to_be16(cpt_req->dlen);367368iq_cmd.dptr = info->dptr_baddr;369iq_cmd.rptr = info->rptr_baddr;370iq_cmd.cptr.u64 = 0;371iq_cmd.cptr.s.grp = ctrl->s.grp;372373/* Fill in the CPT_INST_S type command for HW interpretation */374cpt_fill_inst(&cptinst, info, &iq_cmd);375376/* Print debug info if enabled */377otx_cpt_dump_sg_list(pdev, req);378pr_debug("Cpt_inst_s hexdump (%d bytes)\n", OTX_CPT_INST_SIZE);379print_hex_dump_debug("", 0, 16, 1, &cptinst, OTX_CPT_INST_SIZE, false);380pr_debug("Dptr hexdump (%d bytes)\n", cpt_req->dlen);381print_hex_dump_debug("", 0, 16, 1, info->in_buffer,382cpt_req->dlen, false);383384/* Send CPT command */385cpt_send_cmd(&cptinst, cptvf);386387/*388* We allocate and prepare pending queue entry in critical section389* together with submitting CPT instruction to CPT instruction queue390* to make sure that order of CPT requests is the same in both391* pending and instruction queues392*/393spin_unlock_bh(&pqueue->lock);394395ret = resume_sender ? -EBUSY : -EINPROGRESS;396return ret;397398request_cleanup:399do_request_cleanup(pdev, info);400return ret;401}402403int otx_cpt_do_request(struct pci_dev *pdev, struct otx_cpt_req_info *req,404int cpu_num)405{406struct otx_cptvf *cptvf = pci_get_drvdata(pdev);407408if (!otx_cpt_device_ready(cptvf)) {409dev_err(&pdev->dev, "CPT Device is not ready\n");410return -ENODEV;411}412413if ((cptvf->vftype == OTX_CPT_SE_TYPES) && (!req->ctrl.s.se_req)) {414dev_err(&pdev->dev, "CPTVF-%d of SE TYPE got AE request\n",415cptvf->vfid);416return -EINVAL;417} else if ((cptvf->vftype == OTX_CPT_AE_TYPES) &&418(req->ctrl.s.se_req)) {419dev_err(&pdev->dev, "CPTVF-%d of AE TYPE got SE request\n",420cptvf->vfid);421return -EINVAL;422}423424return process_request(pdev, req, &cptvf->pqinfo.queue[0], cptvf);425}426427static int cpt_process_ccode(struct pci_dev *pdev,428union otx_cpt_res_s *cpt_status,429struct otx_cpt_info_buffer *cpt_info,430struct otx_cpt_req_info *req, u32 *res_code)431{432u8 ccode = cpt_status->s.compcode;433union otx_cpt_error_code ecode;434435ecode.u = be64_to_cpup((__be64 *)cpt_info->out_buffer);436switch (ccode) {437case CPT_COMP_E_FAULT:438dev_err(&pdev->dev,439"Request failed with DMA fault\n");440otx_cpt_dump_sg_list(pdev, req);441break;442443case CPT_COMP_E_SWERR:444dev_err(&pdev->dev,445"Request failed with software error code %d\n",446ecode.s.ccode);447otx_cpt_dump_sg_list(pdev, req);448break;449450case CPT_COMP_E_HWERR:451dev_err(&pdev->dev,452"Request failed with hardware error\n");453otx_cpt_dump_sg_list(pdev, req);454break;455456case COMPLETION_CODE_INIT:457/* check for timeout */458if (time_after_eq(jiffies, cpt_info->time_in +459OTX_CPT_COMMAND_TIMEOUT * HZ))460dev_warn(&pdev->dev, "Request timed out 0x%p\n", req);461else if (cpt_info->extra_time < OTX_CPT_TIME_IN_RESET_COUNT) {462cpt_info->time_in = jiffies;463cpt_info->extra_time++;464}465return 1;466467case CPT_COMP_E_GOOD:468/* Check microcode completion code */469if (ecode.s.ccode) {470/*471* If requested hmac is truncated and ucode returns472* s/g write length error then we report success473* because ucode writes as many bytes of calculated474* hmac as available in gather buffer and reports475* s/g write length error if number of bytes in gather476* buffer is less than full hmac size.477*/478if (req->is_trunc_hmac &&479ecode.s.ccode == ERR_SCATTER_GATHER_WRITE_LENGTH) {480*res_code = 0;481break;482}483484dev_err(&pdev->dev,485"Request failed with software error code 0x%x\n",486ecode.s.ccode);487otx_cpt_dump_sg_list(pdev, req);488break;489}490491/* Request has been processed with success */492*res_code = 0;493break;494495default:496dev_err(&pdev->dev, "Request returned invalid status\n");497break;498}499500return 0;501}502503static inline void process_pending_queue(struct pci_dev *pdev,504struct otx_cpt_pending_queue *pqueue)505{506void (*callback)(int status, void *arg1, void *arg2);507struct otx_cpt_pending_entry *resume_pentry = NULL;508struct otx_cpt_pending_entry *pentry = NULL;509struct otx_cpt_info_buffer *cpt_info = NULL;510union otx_cpt_res_s *cpt_status = NULL;511struct otx_cpt_req_info *req = NULL;512struct crypto_async_request *areq;513u32 res_code, resume_index;514515while (1) {516spin_lock_bh(&pqueue->lock);517pentry = &pqueue->head[pqueue->front];518519if (WARN_ON(!pentry)) {520spin_unlock_bh(&pqueue->lock);521break;522}523524res_code = -EINVAL;525if (unlikely(!pentry->busy)) {526spin_unlock_bh(&pqueue->lock);527break;528}529530if (unlikely(!pentry->callback)) {531dev_err(&pdev->dev, "Callback NULL\n");532goto process_pentry;533}534535cpt_info = pentry->info;536if (unlikely(!cpt_info)) {537dev_err(&pdev->dev, "Pending entry post arg NULL\n");538goto process_pentry;539}540541req = cpt_info->req;542if (unlikely(!req)) {543dev_err(&pdev->dev, "Request NULL\n");544goto process_pentry;545}546547cpt_status = (union otx_cpt_res_s *) pentry->completion_addr;548if (unlikely(!cpt_status)) {549dev_err(&pdev->dev, "Completion address NULL\n");550goto process_pentry;551}552553if (cpt_process_ccode(pdev, cpt_status, cpt_info, req,554&res_code)) {555spin_unlock_bh(&pqueue->lock);556return;557}558cpt_info->pdev = pdev;559560process_pentry:561/*562* Check if we should inform sending side to resume563* We do it CPT_IQ_RESUME_MARGIN elements in advance before564* pending queue becomes empty565*/566resume_index = modulo_inc(pqueue->front, pqueue->qlen,567CPT_IQ_RESUME_MARGIN);568resume_pentry = &pqueue->head[resume_index];569if (resume_pentry &&570resume_pentry->resume_sender) {571resume_pentry->resume_sender = false;572callback = resume_pentry->callback;573areq = resume_pentry->areq;574575if (callback) {576spin_unlock_bh(&pqueue->lock);577578/*579* EINPROGRESS is an indication for sending580* side that it can resume sending requests581*/582callback(-EINPROGRESS, areq, cpt_info);583spin_lock_bh(&pqueue->lock);584}585}586587callback = pentry->callback;588areq = pentry->areq;589free_pentry(pentry);590591pqueue->pending_count--;592pqueue->front = modulo_inc(pqueue->front, pqueue->qlen, 1);593spin_unlock_bh(&pqueue->lock);594595/*596* Call callback after current pending entry has been597* processed, we don't do it if the callback pointer is598* invalid.599*/600if (callback)601callback(res_code, areq, cpt_info);602}603}604605void otx_cpt_post_process(struct otx_cptvf_wqe *wqe)606{607process_pending_queue(wqe->cptvf->pdev, &wqe->cptvf->pqinfo.queue[0]);608}609610611