Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / crypto / cavium / cpt / cptvf_mbox.c
blobd5ec3b8a9e615f49c94c498b92cc62ad41370866
1 /*
2 * Copyright (C) 2016 Cavium, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 */
9 #include "cptvf.h"
11 static void cptvf_send_msg_to_pf(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
13 /* Writing mbox(1) causes interrupt */
14 cpt_write_csr64(cptvf->reg_base, CPTX_VFX_PF_MBOXX(0, 0, 0),
15 mbx->msg);
16 cpt_write_csr64(cptvf->reg_base, CPTX_VFX_PF_MBOXX(0, 0, 1),
17 mbx->data);
20 /* ACKs PF's mailbox message
22 void cptvf_mbox_send_ack(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
24 mbx->msg = CPT_MBOX_MSG_TYPE_ACK;
25 cptvf_send_msg_to_pf(cptvf, mbx);
28 /* NACKs PF's mailbox message that VF is not able to
29 * complete the action
31 void cptvf_mbox_send_nack(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
33 mbx->msg = CPT_MBOX_MSG_TYPE_NACK;
34 cptvf_send_msg_to_pf(cptvf, mbx);
37 /* Interrupt handler to handle mailbox messages from VFs */
38 void cptvf_handle_mbox_intr(struct cpt_vf *cptvf)
40 struct cpt_mbox mbx = {};
43 * MBOX[0] contains msg
44 * MBOX[1] contains data
46 mbx.msg = cpt_read_csr64(cptvf->reg_base, CPTX_VFX_PF_MBOXX(0, 0, 0));
47 mbx.data = cpt_read_csr64(cptvf->reg_base, CPTX_VFX_PF_MBOXX(0, 0, 1));
48 dev_dbg(&cptvf->pdev->dev, "%s: Mailbox msg 0x%llx from PF\n",
49 __func__, mbx.msg);
50 switch (mbx.msg) {
51 case CPT_MSG_READY:
53 cptvf->pf_acked = true;
54 cptvf->vfid = mbx.data;
55 dev_dbg(&cptvf->pdev->dev, "Received VFID %d\n", cptvf->vfid);
56 break;
58 case CPT_MSG_QBIND_GRP:
59 cptvf->pf_acked = true;
60 cptvf->vftype = mbx.data;
61 dev_dbg(&cptvf->pdev->dev, "VF %d type %s group %d\n",
62 cptvf->vfid, ((mbx.data == SE_TYPES) ? "SE" : "AE"),
63 cptvf->vfgrp);
64 break;
65 case CPT_MBOX_MSG_TYPE_ACK:
66 cptvf->pf_acked = true;
67 break;
68 case CPT_MBOX_MSG_TYPE_NACK:
69 cptvf->pf_nacked = true;
70 break;
71 default:
72 dev_err(&cptvf->pdev->dev, "Invalid msg from PF, msg 0x%llx\n",
73 mbx.msg);
74 break;
78 static int cptvf_send_msg_to_pf_timeout(struct cpt_vf *cptvf,
79 struct cpt_mbox *mbx)
81 int timeout = CPT_MBOX_MSG_TIMEOUT;
82 int sleep = 10;
84 cptvf->pf_acked = false;
85 cptvf->pf_nacked = false;
86 cptvf_send_msg_to_pf(cptvf, mbx);
87 /* Wait for previous message to be acked, timeout 2sec */
88 while (!cptvf->pf_acked) {
89 if (cptvf->pf_nacked)
90 return -EINVAL;
91 msleep(sleep);
92 if (cptvf->pf_acked)
93 break;
94 timeout -= sleep;
95 if (!timeout) {
96 dev_err(&cptvf->pdev->dev, "PF didn't ack to mbox msg %llx from VF%u\n",
97 (mbx->msg & 0xFF), cptvf->vfid);
98 return -EBUSY;
102 return 0;
106 * Checks if VF is able to comminicate with PF
107 * and also gets the CPT number this VF is associated to.
109 int cptvf_check_pf_ready(struct cpt_vf *cptvf)
111 struct pci_dev *pdev = cptvf->pdev;
112 struct cpt_mbox mbx = {};
114 mbx.msg = CPT_MSG_READY;
115 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
116 dev_err(&pdev->dev, "PF didn't respond to READY msg\n");
117 return -EBUSY;
120 return 0;
124 * Communicate VQs size to PF to program CPT(0)_PF_Q(0-15)_CTL of the VF.
125 * Must be ACKed.
127 int cptvf_send_vq_size_msg(struct cpt_vf *cptvf)
129 struct pci_dev *pdev = cptvf->pdev;
130 struct cpt_mbox mbx = {};
132 mbx.msg = CPT_MSG_QLEN;
133 mbx.data = cptvf->qsize;
134 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
135 dev_err(&pdev->dev, "PF didn't respond to vq_size msg\n");
136 return -EBUSY;
139 return 0;
143 * Communicate VF group required to PF and get the VQ binded to that group
145 int cptvf_send_vf_to_grp_msg(struct cpt_vf *cptvf)
147 struct pci_dev *pdev = cptvf->pdev;
148 struct cpt_mbox mbx = {};
150 mbx.msg = CPT_MSG_QBIND_GRP;
151 /* Convey group of the VF */
152 mbx.data = cptvf->vfgrp;
153 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
154 dev_err(&pdev->dev, "PF didn't respond to vf_type msg\n");
155 return -EBUSY;
158 return 0;
162 * Communicate VF group required to PF and get the VQ binded to that group
164 int cptvf_send_vf_priority_msg(struct cpt_vf *cptvf)
166 struct pci_dev *pdev = cptvf->pdev;
167 struct cpt_mbox mbx = {};
169 mbx.msg = CPT_MSG_VQ_PRIORITY;
170 /* Convey group of the VF */
171 mbx.data = cptvf->priority;
172 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
173 dev_err(&pdev->dev, "PF didn't respond to vf_type msg\n");
174 return -EBUSY;
176 return 0;
180 * Communicate to PF that VF is UP and running
182 int cptvf_send_vf_up(struct cpt_vf *cptvf)
184 struct pci_dev *pdev = cptvf->pdev;
185 struct cpt_mbox mbx = {};
187 mbx.msg = CPT_MSG_VF_UP;
188 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
189 dev_err(&pdev->dev, "PF didn't respond to UP msg\n");
190 return -EBUSY;
193 return 0;
197 * Communicate to PF that VF is DOWN and running
199 int cptvf_send_vf_down(struct cpt_vf *cptvf)
201 struct pci_dev *pdev = cptvf->pdev;
202 struct cpt_mbox mbx = {};
204 mbx.msg = CPT_MSG_VF_DOWN;
205 if (cptvf_send_msg_to_pf_timeout(cptvf, &mbx)) {
206 dev_err(&pdev->dev, "PF didn't respond to DOWN msg\n");
207 return -EBUSY;
210 return 0;