Path: blob/master/drivers/crypto/marvell/octeontx/otx_cptvf_main.c
26285 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 <linux/interrupt.h>11#include <linux/module.h>12#include "otx_cptvf.h"13#include "otx_cptvf_algs.h"14#include "otx_cptvf_reqmgr.h"1516#define DRV_NAME "octeontx-cptvf"17#define DRV_VERSION "1.0"1819static void vq_work_handler(unsigned long data)20{21struct otx_cptvf_wqe_info *cwqe_info =22(struct otx_cptvf_wqe_info *) data;2324otx_cpt_post_process(&cwqe_info->vq_wqe[0]);25}2627static int init_worker_threads(struct otx_cptvf *cptvf)28{29struct pci_dev *pdev = cptvf->pdev;30struct otx_cptvf_wqe_info *cwqe_info;31int i;3233cwqe_info = kzalloc(sizeof(*cwqe_info), GFP_KERNEL);34if (!cwqe_info)35return -ENOMEM;3637if (cptvf->num_queues) {38dev_dbg(&pdev->dev, "Creating VQ worker threads (%d)\n",39cptvf->num_queues);40}4142for (i = 0; i < cptvf->num_queues; i++) {43tasklet_init(&cwqe_info->vq_wqe[i].twork, vq_work_handler,44(u64)cwqe_info);45cwqe_info->vq_wqe[i].cptvf = cptvf;46}47cptvf->wqe_info = cwqe_info;4849return 0;50}5152static void cleanup_worker_threads(struct otx_cptvf *cptvf)53{54struct pci_dev *pdev = cptvf->pdev;55struct otx_cptvf_wqe_info *cwqe_info;56int i;5758cwqe_info = (struct otx_cptvf_wqe_info *)cptvf->wqe_info;59if (!cwqe_info)60return;6162if (cptvf->num_queues) {63dev_dbg(&pdev->dev, "Cleaning VQ worker threads (%u)\n",64cptvf->num_queues);65}6667for (i = 0; i < cptvf->num_queues; i++)68tasklet_kill(&cwqe_info->vq_wqe[i].twork);6970kfree_sensitive(cwqe_info);71cptvf->wqe_info = NULL;72}7374static void free_pending_queues(struct otx_cpt_pending_qinfo *pqinfo)75{76struct otx_cpt_pending_queue *queue;77int i;7879for_each_pending_queue(pqinfo, queue, i) {80if (!queue->head)81continue;8283/* free single queue */84kfree_sensitive((queue->head));85queue->front = 0;86queue->rear = 0;87queue->qlen = 0;88}89pqinfo->num_queues = 0;90}9192static int alloc_pending_queues(struct otx_cpt_pending_qinfo *pqinfo, u32 qlen,93u32 num_queues)94{95struct otx_cpt_pending_queue *queue = NULL;96int ret;97u32 i;9899pqinfo->num_queues = num_queues;100101for_each_pending_queue(pqinfo, queue, i) {102queue->head = kcalloc(qlen, sizeof(*queue->head), GFP_KERNEL);103if (!queue->head) {104ret = -ENOMEM;105goto pending_qfail;106}107108queue->pending_count = 0;109queue->front = 0;110queue->rear = 0;111queue->qlen = qlen;112113/* init queue spin lock */114spin_lock_init(&queue->lock);115}116return 0;117118pending_qfail:119free_pending_queues(pqinfo);120121return ret;122}123124static int init_pending_queues(struct otx_cptvf *cptvf, u32 qlen,125u32 num_queues)126{127struct pci_dev *pdev = cptvf->pdev;128int ret;129130if (!num_queues)131return 0;132133ret = alloc_pending_queues(&cptvf->pqinfo, qlen, num_queues);134if (ret) {135dev_err(&pdev->dev, "Failed to setup pending queues (%u)\n",136num_queues);137return ret;138}139return 0;140}141142static void cleanup_pending_queues(struct otx_cptvf *cptvf)143{144struct pci_dev *pdev = cptvf->pdev;145146if (!cptvf->num_queues)147return;148149dev_dbg(&pdev->dev, "Cleaning VQ pending queue (%u)\n",150cptvf->num_queues);151free_pending_queues(&cptvf->pqinfo);152}153154static void free_command_queues(struct otx_cptvf *cptvf,155struct otx_cpt_cmd_qinfo *cqinfo)156{157struct otx_cpt_cmd_queue *queue = NULL;158struct otx_cpt_cmd_chunk *chunk = NULL;159struct pci_dev *pdev = cptvf->pdev;160int i;161162/* clean up for each queue */163for (i = 0; i < cptvf->num_queues; i++) {164queue = &cqinfo->queue[i];165166while (!list_empty(&cqinfo->queue[i].chead)) {167chunk = list_first_entry(&cqinfo->queue[i].chead,168struct otx_cpt_cmd_chunk, nextchunk);169170dma_free_coherent(&pdev->dev, chunk->size,171chunk->head,172chunk->dma_addr);173chunk->head = NULL;174chunk->dma_addr = 0;175list_del(&chunk->nextchunk);176kfree_sensitive(chunk);177}178queue->num_chunks = 0;179queue->idx = 0;180181}182}183184static int alloc_command_queues(struct otx_cptvf *cptvf,185struct otx_cpt_cmd_qinfo *cqinfo,186u32 qlen)187{188struct otx_cpt_cmd_chunk *curr, *first, *last;189struct otx_cpt_cmd_queue *queue = NULL;190struct pci_dev *pdev = cptvf->pdev;191size_t q_size, c_size, rem_q_size;192u32 qcsize_bytes;193int i;194195196/* Qsize in dwords, needed for SADDR config, 1-next chunk pointer */197cptvf->qsize = min(qlen, cqinfo->qchunksize) *198OTX_CPT_NEXT_CHUNK_PTR_SIZE + 1;199/* Qsize in bytes to create space for alignment */200q_size = qlen * OTX_CPT_INST_SIZE;201202qcsize_bytes = cqinfo->qchunksize * OTX_CPT_INST_SIZE;203204/* per queue initialization */205for (i = 0; i < cptvf->num_queues; i++) {206rem_q_size = q_size;207first = NULL;208last = NULL;209210queue = &cqinfo->queue[i];211INIT_LIST_HEAD(&queue->chead);212do {213curr = kzalloc(sizeof(*curr), GFP_KERNEL);214if (!curr)215goto cmd_qfail;216217c_size = (rem_q_size > qcsize_bytes) ? qcsize_bytes :218rem_q_size;219curr->head = dma_alloc_coherent(&pdev->dev,220c_size + OTX_CPT_NEXT_CHUNK_PTR_SIZE,221&curr->dma_addr, GFP_KERNEL);222if (!curr->head) {223dev_err(&pdev->dev,224"Command Q (%d) chunk (%d) allocation failed\n",225i, queue->num_chunks);226goto free_curr;227}228curr->size = c_size;229230if (queue->num_chunks == 0) {231first = curr;232queue->base = first;233}234list_add_tail(&curr->nextchunk,235&cqinfo->queue[i].chead);236237queue->num_chunks++;238rem_q_size -= c_size;239if (last)240*((u64 *)(&last->head[last->size])) =241(u64)curr->dma_addr;242243last = curr;244} while (rem_q_size);245246/*247* Make the queue circular, tie back last chunk entry to head248*/249curr = first;250*((u64 *)(&last->head[last->size])) = (u64)curr->dma_addr;251queue->qhead = curr;252}253return 0;254free_curr:255kfree(curr);256cmd_qfail:257free_command_queues(cptvf, cqinfo);258return -ENOMEM;259}260261static int init_command_queues(struct otx_cptvf *cptvf, u32 qlen)262{263struct pci_dev *pdev = cptvf->pdev;264int ret;265266/* setup command queues */267ret = alloc_command_queues(cptvf, &cptvf->cqinfo, qlen);268if (ret) {269dev_err(&pdev->dev, "Failed to allocate command queues (%u)\n",270cptvf->num_queues);271return ret;272}273return ret;274}275276static void cleanup_command_queues(struct otx_cptvf *cptvf)277{278struct pci_dev *pdev = cptvf->pdev;279280if (!cptvf->num_queues)281return;282283dev_dbg(&pdev->dev, "Cleaning VQ command queue (%u)\n",284cptvf->num_queues);285free_command_queues(cptvf, &cptvf->cqinfo);286}287288static void cptvf_sw_cleanup(struct otx_cptvf *cptvf)289{290cleanup_worker_threads(cptvf);291cleanup_pending_queues(cptvf);292cleanup_command_queues(cptvf);293}294295static int cptvf_sw_init(struct otx_cptvf *cptvf, u32 qlen, u32 num_queues)296{297struct pci_dev *pdev = cptvf->pdev;298u32 max_dev_queues = 0;299int ret;300301max_dev_queues = OTX_CPT_NUM_QS_PER_VF;302/* possible cpus */303num_queues = min_t(u32, num_queues, max_dev_queues);304cptvf->num_queues = num_queues;305306ret = init_command_queues(cptvf, qlen);307if (ret) {308dev_err(&pdev->dev, "Failed to setup command queues (%u)\n",309num_queues);310return ret;311}312313ret = init_pending_queues(cptvf, qlen, num_queues);314if (ret) {315dev_err(&pdev->dev, "Failed to setup pending queues (%u)\n",316num_queues);317goto setup_pqfail;318}319320/* Create worker threads for BH processing */321ret = init_worker_threads(cptvf);322if (ret) {323dev_err(&pdev->dev, "Failed to setup worker threads\n");324goto init_work_fail;325}326return 0;327328init_work_fail:329cleanup_worker_threads(cptvf);330cleanup_pending_queues(cptvf);331332setup_pqfail:333cleanup_command_queues(cptvf);334335return ret;336}337338static void cptvf_free_irq_affinity(struct otx_cptvf *cptvf, int vec)339{340irq_set_affinity_hint(pci_irq_vector(cptvf->pdev, vec), NULL);341free_cpumask_var(cptvf->affinity_mask[vec]);342}343344static void cptvf_write_vq_ctl(struct otx_cptvf *cptvf, bool val)345{346union otx_cptx_vqx_ctl vqx_ctl;347348vqx_ctl.u = readq(cptvf->reg_base + OTX_CPT_VQX_CTL(0));349vqx_ctl.s.ena = val;350writeq(vqx_ctl.u, cptvf->reg_base + OTX_CPT_VQX_CTL(0));351}352353void otx_cptvf_write_vq_doorbell(struct otx_cptvf *cptvf, u32 val)354{355union otx_cptx_vqx_doorbell vqx_dbell;356357vqx_dbell.u = readq(cptvf->reg_base + OTX_CPT_VQX_DOORBELL(0));358vqx_dbell.s.dbell_cnt = val * 8; /* Num of Instructions * 8 words */359writeq(vqx_dbell.u, cptvf->reg_base + OTX_CPT_VQX_DOORBELL(0));360}361362static void cptvf_write_vq_inprog(struct otx_cptvf *cptvf, u8 val)363{364union otx_cptx_vqx_inprog vqx_inprg;365366vqx_inprg.u = readq(cptvf->reg_base + OTX_CPT_VQX_INPROG(0));367vqx_inprg.s.inflight = val;368writeq(vqx_inprg.u, cptvf->reg_base + OTX_CPT_VQX_INPROG(0));369}370371static void cptvf_write_vq_done_numwait(struct otx_cptvf *cptvf, u32 val)372{373union otx_cptx_vqx_done_wait vqx_dwait;374375vqx_dwait.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));376vqx_dwait.s.num_wait = val;377writeq(vqx_dwait.u, cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));378}379380static u32 cptvf_read_vq_done_numwait(struct otx_cptvf *cptvf)381{382union otx_cptx_vqx_done_wait vqx_dwait;383384vqx_dwait.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));385return vqx_dwait.s.num_wait;386}387388static void cptvf_write_vq_done_timewait(struct otx_cptvf *cptvf, u16 time)389{390union otx_cptx_vqx_done_wait vqx_dwait;391392vqx_dwait.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));393vqx_dwait.s.time_wait = time;394writeq(vqx_dwait.u, cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));395}396397398static u16 cptvf_read_vq_done_timewait(struct otx_cptvf *cptvf)399{400union otx_cptx_vqx_done_wait vqx_dwait;401402vqx_dwait.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_WAIT(0));403return vqx_dwait.s.time_wait;404}405406static void cptvf_enable_swerr_interrupts(struct otx_cptvf *cptvf)407{408union otx_cptx_vqx_misc_ena_w1s vqx_misc_ena;409410vqx_misc_ena.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_ENA_W1S(0));411/* Enable SWERR interrupts for the requested VF */412vqx_misc_ena.s.swerr = 1;413writeq(vqx_misc_ena.u, cptvf->reg_base + OTX_CPT_VQX_MISC_ENA_W1S(0));414}415416static void cptvf_enable_mbox_interrupts(struct otx_cptvf *cptvf)417{418union otx_cptx_vqx_misc_ena_w1s vqx_misc_ena;419420vqx_misc_ena.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_ENA_W1S(0));421/* Enable MBOX interrupt for the requested VF */422vqx_misc_ena.s.mbox = 1;423writeq(vqx_misc_ena.u, cptvf->reg_base + OTX_CPT_VQX_MISC_ENA_W1S(0));424}425426static void cptvf_enable_done_interrupts(struct otx_cptvf *cptvf)427{428union otx_cptx_vqx_done_ena_w1s vqx_done_ena;429430vqx_done_ena.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_ENA_W1S(0));431/* Enable DONE interrupt for the requested VF */432vqx_done_ena.s.done = 1;433writeq(vqx_done_ena.u, cptvf->reg_base + OTX_CPT_VQX_DONE_ENA_W1S(0));434}435436static void cptvf_clear_dovf_intr(struct otx_cptvf *cptvf)437{438union otx_cptx_vqx_misc_int vqx_misc_int;439440vqx_misc_int.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));441/* W1C for the VF */442vqx_misc_int.s.dovf = 1;443writeq(vqx_misc_int.u, cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));444}445446static void cptvf_clear_irde_intr(struct otx_cptvf *cptvf)447{448union otx_cptx_vqx_misc_int vqx_misc_int;449450vqx_misc_int.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));451/* W1C for the VF */452vqx_misc_int.s.irde = 1;453writeq(vqx_misc_int.u, cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));454}455456static void cptvf_clear_nwrp_intr(struct otx_cptvf *cptvf)457{458union otx_cptx_vqx_misc_int vqx_misc_int;459460vqx_misc_int.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));461/* W1C for the VF */462vqx_misc_int.s.nwrp = 1;463writeq(vqx_misc_int.u, cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));464}465466static void cptvf_clear_mbox_intr(struct otx_cptvf *cptvf)467{468union otx_cptx_vqx_misc_int vqx_misc_int;469470vqx_misc_int.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));471/* W1C for the VF */472vqx_misc_int.s.mbox = 1;473writeq(vqx_misc_int.u, cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));474}475476static void cptvf_clear_swerr_intr(struct otx_cptvf *cptvf)477{478union otx_cptx_vqx_misc_int vqx_misc_int;479480vqx_misc_int.u = readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));481/* W1C for the VF */482vqx_misc_int.s.swerr = 1;483writeq(vqx_misc_int.u, cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));484}485486static u64 cptvf_read_vf_misc_intr_status(struct otx_cptvf *cptvf)487{488return readq(cptvf->reg_base + OTX_CPT_VQX_MISC_INT(0));489}490491static irqreturn_t cptvf_misc_intr_handler(int __always_unused irq,492void *arg)493{494struct otx_cptvf *cptvf = arg;495struct pci_dev *pdev = cptvf->pdev;496u64 intr;497498intr = cptvf_read_vf_misc_intr_status(cptvf);499/* Check for MISC interrupt types */500if (likely(intr & OTX_CPT_VF_INTR_MBOX_MASK)) {501dev_dbg(&pdev->dev, "Mailbox interrupt 0x%llx on CPT VF %d\n",502intr, cptvf->vfid);503otx_cptvf_handle_mbox_intr(cptvf);504cptvf_clear_mbox_intr(cptvf);505} else if (unlikely(intr & OTX_CPT_VF_INTR_DOVF_MASK)) {506cptvf_clear_dovf_intr(cptvf);507/* Clear doorbell count */508otx_cptvf_write_vq_doorbell(cptvf, 0);509dev_err(&pdev->dev,510"Doorbell overflow error interrupt 0x%llx on CPT VF %d\n",511intr, cptvf->vfid);512} else if (unlikely(intr & OTX_CPT_VF_INTR_IRDE_MASK)) {513cptvf_clear_irde_intr(cptvf);514dev_err(&pdev->dev,515"Instruction NCB read error interrupt 0x%llx on CPT VF %d\n",516intr, cptvf->vfid);517} else if (unlikely(intr & OTX_CPT_VF_INTR_NWRP_MASK)) {518cptvf_clear_nwrp_intr(cptvf);519dev_err(&pdev->dev,520"NCB response write error interrupt 0x%llx on CPT VF %d\n",521intr, cptvf->vfid);522} else if (unlikely(intr & OTX_CPT_VF_INTR_SERR_MASK)) {523cptvf_clear_swerr_intr(cptvf);524dev_err(&pdev->dev,525"Software error interrupt 0x%llx on CPT VF %d\n",526intr, cptvf->vfid);527} else {528dev_err(&pdev->dev, "Unhandled interrupt in OTX_CPT VF %d\n",529cptvf->vfid);530}531532return IRQ_HANDLED;533}534535static inline struct otx_cptvf_wqe *get_cptvf_vq_wqe(struct otx_cptvf *cptvf,536int qno)537{538struct otx_cptvf_wqe_info *nwqe_info;539540if (unlikely(qno >= cptvf->num_queues))541return NULL;542nwqe_info = (struct otx_cptvf_wqe_info *)cptvf->wqe_info;543544return &nwqe_info->vq_wqe[qno];545}546547static inline u32 cptvf_read_vq_done_count(struct otx_cptvf *cptvf)548{549union otx_cptx_vqx_done vqx_done;550551vqx_done.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE(0));552return vqx_done.s.done;553}554555static inline void cptvf_write_vq_done_ack(struct otx_cptvf *cptvf,556u32 ackcnt)557{558union otx_cptx_vqx_done_ack vqx_dack_cnt;559560vqx_dack_cnt.u = readq(cptvf->reg_base + OTX_CPT_VQX_DONE_ACK(0));561vqx_dack_cnt.s.done_ack = ackcnt;562writeq(vqx_dack_cnt.u, cptvf->reg_base + OTX_CPT_VQX_DONE_ACK(0));563}564565static irqreturn_t cptvf_done_intr_handler(int __always_unused irq,566void *cptvf_dev)567{568struct otx_cptvf *cptvf = (struct otx_cptvf *)cptvf_dev;569struct pci_dev *pdev = cptvf->pdev;570/* Read the number of completions */571u32 intr = cptvf_read_vq_done_count(cptvf);572573if (intr) {574struct otx_cptvf_wqe *wqe;575576/*577* Acknowledge the number of scheduled completions for578* processing579*/580cptvf_write_vq_done_ack(cptvf, intr);581wqe = get_cptvf_vq_wqe(cptvf, 0);582if (unlikely(!wqe)) {583dev_err(&pdev->dev, "No work to schedule for VF (%d)\n",584cptvf->vfid);585return IRQ_NONE;586}587tasklet_hi_schedule(&wqe->twork);588}589590return IRQ_HANDLED;591}592593static void cptvf_set_irq_affinity(struct otx_cptvf *cptvf, int vec)594{595struct pci_dev *pdev = cptvf->pdev;596int cpu;597598if (!zalloc_cpumask_var(&cptvf->affinity_mask[vec],599GFP_KERNEL)) {600dev_err(&pdev->dev,601"Allocation failed for affinity_mask for VF %d\n",602cptvf->vfid);603return;604}605606cpu = cptvf->vfid % num_online_cpus();607cpumask_set_cpu(cpumask_local_spread(cpu, cptvf->node),608cptvf->affinity_mask[vec]);609irq_set_affinity_hint(pci_irq_vector(pdev, vec),610cptvf->affinity_mask[vec]);611}612613static void cptvf_write_vq_saddr(struct otx_cptvf *cptvf, u64 val)614{615union otx_cptx_vqx_saddr vqx_saddr;616617vqx_saddr.u = val;618writeq(vqx_saddr.u, cptvf->reg_base + OTX_CPT_VQX_SADDR(0));619}620621static void cptvf_device_init(struct otx_cptvf *cptvf)622{623u64 base_addr = 0;624625/* Disable the VQ */626cptvf_write_vq_ctl(cptvf, 0);627/* Reset the doorbell */628otx_cptvf_write_vq_doorbell(cptvf, 0);629/* Clear inflight */630cptvf_write_vq_inprog(cptvf, 0);631/* Write VQ SADDR */632base_addr = (u64)(cptvf->cqinfo.queue[0].qhead->dma_addr);633cptvf_write_vq_saddr(cptvf, base_addr);634/* Configure timerhold / coalescence */635cptvf_write_vq_done_timewait(cptvf, OTX_CPT_TIMER_HOLD);636cptvf_write_vq_done_numwait(cptvf, OTX_CPT_COUNT_HOLD);637/* Enable the VQ */638cptvf_write_vq_ctl(cptvf, 1);639/* Flag the VF ready */640cptvf->flags |= OTX_CPT_FLAG_DEVICE_READY;641}642643static ssize_t vf_type_show(struct device *dev,644struct device_attribute *attr,645char *buf)646{647struct otx_cptvf *cptvf = dev_get_drvdata(dev);648char *msg;649650switch (cptvf->vftype) {651case OTX_CPT_AE_TYPES:652msg = "AE";653break;654655case OTX_CPT_SE_TYPES:656msg = "SE";657break;658659default:660msg = "Invalid";661}662663return sysfs_emit(buf, "%s\n", msg);664}665666static ssize_t vf_engine_group_show(struct device *dev,667struct device_attribute *attr,668char *buf)669{670struct otx_cptvf *cptvf = dev_get_drvdata(dev);671672return sysfs_emit(buf, "%d\n", cptvf->vfgrp);673}674675static ssize_t vf_engine_group_store(struct device *dev,676struct device_attribute *attr,677const char *buf, size_t count)678{679struct otx_cptvf *cptvf = dev_get_drvdata(dev);680int val, ret;681682ret = kstrtoint(buf, 10, &val);683if (ret)684return ret;685686if (val < 0)687return -EINVAL;688689if (val >= OTX_CPT_MAX_ENGINE_GROUPS) {690dev_err(dev, "Engine group >= than max available groups %d\n",691OTX_CPT_MAX_ENGINE_GROUPS);692return -EINVAL;693}694695ret = otx_cptvf_send_vf_to_grp_msg(cptvf, val);696if (ret)697return ret;698699return count;700}701702static ssize_t vf_coalesc_time_wait_show(struct device *dev,703struct device_attribute *attr,704char *buf)705{706struct otx_cptvf *cptvf = dev_get_drvdata(dev);707708return sysfs_emit(buf, "%d\n",709cptvf_read_vq_done_timewait(cptvf));710}711712static ssize_t vf_coalesc_num_wait_show(struct device *dev,713struct device_attribute *attr,714char *buf)715{716struct otx_cptvf *cptvf = dev_get_drvdata(dev);717718return sysfs_emit(buf, "%d\n",719cptvf_read_vq_done_numwait(cptvf));720}721722static ssize_t vf_coalesc_time_wait_store(struct device *dev,723struct device_attribute *attr,724const char *buf, size_t count)725{726struct otx_cptvf *cptvf = dev_get_drvdata(dev);727long val;728int ret;729730ret = kstrtol(buf, 10, &val);731if (ret != 0)732return ret;733734if (val < OTX_CPT_COALESC_MIN_TIME_WAIT ||735val > OTX_CPT_COALESC_MAX_TIME_WAIT)736return -EINVAL;737738cptvf_write_vq_done_timewait(cptvf, val);739return count;740}741742static ssize_t vf_coalesc_num_wait_store(struct device *dev,743struct device_attribute *attr,744const char *buf, size_t count)745{746struct otx_cptvf *cptvf = dev_get_drvdata(dev);747long val;748int ret;749750ret = kstrtol(buf, 10, &val);751if (ret != 0)752return ret;753754if (val < OTX_CPT_COALESC_MIN_NUM_WAIT ||755val > OTX_CPT_COALESC_MAX_NUM_WAIT)756return -EINVAL;757758cptvf_write_vq_done_numwait(cptvf, val);759return count;760}761762static DEVICE_ATTR_RO(vf_type);763static DEVICE_ATTR_RW(vf_engine_group);764static DEVICE_ATTR_RW(vf_coalesc_time_wait);765static DEVICE_ATTR_RW(vf_coalesc_num_wait);766767static struct attribute *otx_cptvf_attrs[] = {768&dev_attr_vf_type.attr,769&dev_attr_vf_engine_group.attr,770&dev_attr_vf_coalesc_time_wait.attr,771&dev_attr_vf_coalesc_num_wait.attr,772NULL773};774775static const struct attribute_group otx_cptvf_sysfs_group = {776.attrs = otx_cptvf_attrs,777};778779static int otx_cptvf_probe(struct pci_dev *pdev,780const struct pci_device_id *ent)781{782struct device *dev = &pdev->dev;783struct otx_cptvf *cptvf;784int err;785786cptvf = devm_kzalloc(dev, sizeof(*cptvf), GFP_KERNEL);787if (!cptvf)788return -ENOMEM;789790pci_set_drvdata(pdev, cptvf);791cptvf->pdev = pdev;792793err = pci_enable_device(pdev);794if (err) {795dev_err(dev, "Failed to enable PCI device\n");796goto clear_drvdata;797}798err = pci_request_regions(pdev, DRV_NAME);799if (err) {800dev_err(dev, "PCI request regions failed 0x%x\n", err);801goto disable_device;802}803err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));804if (err) {805dev_err(dev, "Unable to get usable 48-bit DMA configuration\n");806goto release_regions;807}808809/* MAP PF's configuration registers */810cptvf->reg_base = pci_iomap(pdev, OTX_CPT_VF_PCI_CFG_BAR, 0);811if (!cptvf->reg_base) {812dev_err(dev, "Cannot map config register space, aborting\n");813err = -ENOMEM;814goto release_regions;815}816817cptvf->node = dev_to_node(&pdev->dev);818err = pci_alloc_irq_vectors(pdev, OTX_CPT_VF_MSIX_VECTORS,819OTX_CPT_VF_MSIX_VECTORS, PCI_IRQ_MSIX);820if (err < 0) {821dev_err(dev, "Request for #%d msix vectors failed\n",822OTX_CPT_VF_MSIX_VECTORS);823goto unmap_region;824}825826err = request_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC),827cptvf_misc_intr_handler, 0, "CPT VF misc intr",828cptvf);829if (err) {830dev_err(dev, "Failed to request misc irq\n");831goto free_vectors;832}833834/* Enable mailbox interrupt */835cptvf_enable_mbox_interrupts(cptvf);836cptvf_enable_swerr_interrupts(cptvf);837838/* Check cpt pf status, gets chip ID / device Id from PF if ready */839err = otx_cptvf_check_pf_ready(cptvf);840if (err)841goto free_misc_irq;842843/* CPT VF software resources initialization */844cptvf->cqinfo.qchunksize = OTX_CPT_CMD_QCHUNK_SIZE;845err = cptvf_sw_init(cptvf, OTX_CPT_CMD_QLEN, OTX_CPT_NUM_QS_PER_VF);846if (err) {847dev_err(dev, "cptvf_sw_init() failed\n");848goto free_misc_irq;849}850/* Convey VQ LEN to PF */851err = otx_cptvf_send_vq_size_msg(cptvf);852if (err)853goto sw_cleanup;854855/* CPT VF device initialization */856cptvf_device_init(cptvf);857/* Send msg to PF to assign currnet Q to required group */858err = otx_cptvf_send_vf_to_grp_msg(cptvf, cptvf->vfgrp);859if (err)860goto sw_cleanup;861862cptvf->priority = 1;863err = otx_cptvf_send_vf_priority_msg(cptvf);864if (err)865goto sw_cleanup;866867err = request_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_DONE),868cptvf_done_intr_handler, 0, "CPT VF done intr",869cptvf);870if (err) {871dev_err(dev, "Failed to request done irq\n");872goto free_done_irq;873}874875/* Enable done interrupt */876cptvf_enable_done_interrupts(cptvf);877878/* Set irq affinity masks */879cptvf_set_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);880cptvf_set_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);881882err = otx_cptvf_send_vf_up(cptvf);883if (err)884goto free_irq_affinity;885886/* Initialize algorithms and set ops */887err = otx_cpt_crypto_init(pdev, THIS_MODULE,888cptvf->vftype == OTX_CPT_SE_TYPES ? OTX_CPT_SE : OTX_CPT_AE,889cptvf->vftype, 1, cptvf->num_vfs);890if (err) {891dev_err(dev, "Failed to register crypto algs\n");892goto free_irq_affinity;893}894895err = sysfs_create_group(&dev->kobj, &otx_cptvf_sysfs_group);896if (err) {897dev_err(dev, "Creating sysfs entries failed\n");898goto crypto_exit;899}900901return 0;902903crypto_exit:904otx_cpt_crypto_exit(pdev, THIS_MODULE, cptvf->vftype);905free_irq_affinity:906cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);907cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);908free_done_irq:909free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_DONE), cptvf);910sw_cleanup:911cptvf_sw_cleanup(cptvf);912free_misc_irq:913free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC), cptvf);914free_vectors:915pci_free_irq_vectors(cptvf->pdev);916unmap_region:917pci_iounmap(pdev, cptvf->reg_base);918release_regions:919pci_release_regions(pdev);920disable_device:921pci_disable_device(pdev);922clear_drvdata:923pci_set_drvdata(pdev, NULL);924925return err;926}927928static void otx_cptvf_remove(struct pci_dev *pdev)929{930struct otx_cptvf *cptvf = pci_get_drvdata(pdev);931932if (!cptvf) {933dev_err(&pdev->dev, "Invalid CPT-VF device\n");934return;935}936937/* Convey DOWN to PF */938if (otx_cptvf_send_vf_down(cptvf)) {939dev_err(&pdev->dev, "PF not responding to DOWN msg\n");940} else {941sysfs_remove_group(&pdev->dev.kobj, &otx_cptvf_sysfs_group);942otx_cpt_crypto_exit(pdev, THIS_MODULE, cptvf->vftype);943cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_DONE);944cptvf_free_irq_affinity(cptvf, CPT_VF_INT_VEC_E_MISC);945free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_DONE), cptvf);946free_irq(pci_irq_vector(pdev, CPT_VF_INT_VEC_E_MISC), cptvf);947cptvf_sw_cleanup(cptvf);948pci_free_irq_vectors(cptvf->pdev);949pci_iounmap(pdev, cptvf->reg_base);950pci_release_regions(pdev);951pci_disable_device(pdev);952pci_set_drvdata(pdev, NULL);953}954}955956/* Supported devices */957static const struct pci_device_id otx_cptvf_id_table[] = {958{PCI_VDEVICE(CAVIUM, OTX_CPT_PCI_VF_DEVICE_ID), 0},959{ 0, } /* end of table */960};961962static struct pci_driver otx_cptvf_pci_driver = {963.name = DRV_NAME,964.id_table = otx_cptvf_id_table,965.probe = otx_cptvf_probe,966.remove = otx_cptvf_remove,967};968969module_pci_driver(otx_cptvf_pci_driver);970971MODULE_AUTHOR("Marvell International Ltd.");972MODULE_DESCRIPTION("Marvell OcteonTX CPT Virtual Function Driver");973MODULE_LICENSE("GPL v2");974MODULE_VERSION(DRV_VERSION);975MODULE_DEVICE_TABLE(pci, otx_cptvf_id_table);976977978