1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved. 3 */ 4 5 #include <rte_string_fns.h> 6 #include <bus_pci_driver.h> 7 #include <bus_vdev_driver.h> 8 #include <rte_common.h> 9 #include <rte_cryptodev.h> 10 #include <cryptodev_pmd.h> 11 #include <rte_pci.h> 12 #include <dev_driver.h> 13 #include <rte_malloc.h> 14 15 #include "ccp_crypto.h" 16 #include "ccp_dev.h" 17 #include "ccp_pmd_private.h" 18 19 /** 20 * Global static parameter used to find if CCP device is already initialized. 21 */ 22 static unsigned int ccp_pmd_init_done; 23 uint8_t ccp_cryptodev_driver_id; 24 uint8_t cryptodev_cnt; 25 26 struct ccp_pmd_init_params { 27 struct rte_cryptodev_pmd_init_params def_p; 28 bool auth_opt; 29 }; 30 31 #define CCP_CRYPTODEV_PARAM_NAME ("name") 32 #define CCP_CRYPTODEV_PARAM_SOCKET_ID ("socket_id") 33 #define CCP_CRYPTODEV_PARAM_MAX_NB_QP ("max_nb_queue_pairs") 34 #define CCP_CRYPTODEV_PARAM_AUTH_OPT ("ccp_auth_opt") 35 36 const char *ccp_pmd_valid_params[] = { 37 CCP_CRYPTODEV_PARAM_NAME, 38 CCP_CRYPTODEV_PARAM_SOCKET_ID, 39 CCP_CRYPTODEV_PARAM_MAX_NB_QP, 40 CCP_CRYPTODEV_PARAM_AUTH_OPT, 41 }; 42 43 /** ccp pmd auth option */ 44 enum ccp_pmd_auth_opt { 45 CCP_PMD_AUTH_OPT_CCP = 0, 46 CCP_PMD_AUTH_OPT_CPU, 47 }; 48 49 static struct ccp_session * 50 get_ccp_session(struct ccp_qp *qp, struct rte_crypto_op *op) 51 { 52 struct ccp_session *sess = NULL; 53 54 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) { 55 if (unlikely(op->sym->session == NULL)) 56 return NULL; 57 58 sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session); 59 } else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) { 60 struct rte_cryptodev_sym_session *_sess; 61 struct ccp_private *internals; 62 63 if (rte_mempool_get(qp->sess_mp, (void **)&_sess)) 64 return NULL; 65 66 sess = (void *)_sess->driver_priv_data; 67 68 internals = (struct ccp_private *)qp->dev->data->dev_private; 69 if (unlikely(ccp_set_session_parameters(sess, op->sym->xform, 70 internals) != 0)) { 71 rte_mempool_put(qp->sess_mp, _sess); 72 sess = NULL; 73 } 74 op->sym->session = _sess; 75 } 76 77 return sess; 78 } 79 80 static uint16_t 81 ccp_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops, 82 uint16_t nb_ops) 83 { 84 struct ccp_session *sess = NULL; 85 struct ccp_qp *qp = queue_pair; 86 struct ccp_queue *cmd_q; 87 struct rte_cryptodev *dev = qp->dev; 88 uint16_t i, enq_cnt = 0, slots_req = 0; 89 uint16_t tmp_ops = nb_ops, b_idx, cur_ops = 0; 90 91 if (nb_ops == 0) 92 return 0; 93 94 if (unlikely(rte_ring_full(qp->processed_pkts) != 0)) 95 return 0; 96 if (tmp_ops >= cryptodev_cnt) 97 cur_ops = nb_ops / cryptodev_cnt + (nb_ops)%cryptodev_cnt; 98 else 99 cur_ops = tmp_ops; 100 while (tmp_ops) { 101 b_idx = nb_ops - tmp_ops; 102 slots_req = 0; 103 if (cur_ops <= tmp_ops) { 104 tmp_ops -= cur_ops; 105 } else { 106 cur_ops = tmp_ops; 107 tmp_ops = 0; 108 } 109 for (i = 0; i < cur_ops; i++) { 110 sess = get_ccp_session(qp, ops[i + b_idx]); 111 if (unlikely(sess == NULL) && (i == 0)) { 112 qp->qp_stats.enqueue_err_count++; 113 return 0; 114 } else if (sess == NULL) { 115 cur_ops = i; 116 break; 117 } 118 slots_req += ccp_compute_slot_count(sess); 119 } 120 121 cmd_q = ccp_allot_queue(dev, slots_req); 122 if (unlikely(cmd_q == NULL)) 123 return 0; 124 enq_cnt += process_ops_to_enqueue(qp, ops, cmd_q, cur_ops, 125 nb_ops, slots_req, b_idx); 126 i++; 127 } 128 129 qp->qp_stats.enqueued_count += enq_cnt; 130 return enq_cnt; 131 } 132 133 static uint16_t 134 ccp_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops, 135 uint16_t nb_ops) 136 { 137 struct ccp_qp *qp = queue_pair; 138 uint16_t nb_dequeued = 0, i, total_nb_ops; 139 140 nb_dequeued = process_ops_to_dequeue(qp, ops, nb_ops, &total_nb_ops); 141 142 if (total_nb_ops) { 143 while (nb_dequeued != total_nb_ops) { 144 nb_dequeued = process_ops_to_dequeue(qp, 145 ops, nb_ops, &total_nb_ops); 146 } 147 } 148 149 /* Free session if a session-less crypto op */ 150 for (i = 0; i < nb_dequeued; i++) 151 if (unlikely(ops[i]->sess_type == 152 RTE_CRYPTO_OP_SESSIONLESS)) { 153 struct ccp_session *sess = 154 CRYPTODEV_GET_SYM_SESS_PRIV(ops[i]->sym->session); 155 156 memset(sess, 0, sizeof(*sess)); 157 rte_mempool_put(qp->sess_mp, 158 ops[i]->sym->session); 159 ops[i]->sym->session = NULL; 160 } 161 qp->qp_stats.dequeued_count += nb_dequeued; 162 163 return nb_dequeued; 164 } 165 166 /* 167 * The set of PCI devices this driver supports 168 */ 169 static struct rte_pci_id ccp_pci_id[] = { 170 { 171 RTE_PCI_DEVICE(0x1022, 0x1456), /* AMD CCP-5a */ 172 }, 173 { 174 RTE_PCI_DEVICE(0x1022, 0x1468), /* AMD CCP-5b */ 175 }, 176 { 177 RTE_PCI_DEVICE(0x1022, 0x15df), /* AMD CCP RV */ 178 }, 179 {.device_id = 0}, 180 }; 181 182 /** Remove ccp pmd */ 183 static int 184 cryptodev_ccp_remove(struct rte_pci_device *pci_dev) 185 { 186 char name[RTE_CRYPTODEV_NAME_MAX_LEN]; 187 struct rte_cryptodev *dev; 188 189 if (pci_dev == NULL) 190 return -EINVAL; 191 192 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 193 194 if (name[0] == '\0') 195 return -EINVAL; 196 197 dev = rte_cryptodev_pmd_get_named_dev(name); 198 if (dev == NULL) 199 return -ENODEV; 200 201 ccp_pmd_init_done = 0; 202 203 RTE_LOG(INFO, PMD, "Closing ccp device %s on numa socket %u\n", 204 name, rte_socket_id()); 205 206 return rte_cryptodev_pmd_destroy(dev); 207 } 208 209 /** Create crypto device */ 210 static int 211 cryptodev_ccp_create(const char *name, 212 struct rte_pci_device *pci_dev, 213 struct ccp_pmd_init_params *init_params, 214 struct rte_pci_driver *pci_drv) 215 { 216 struct rte_cryptodev *dev; 217 struct ccp_private *internals; 218 219 if (init_params->def_p.name[0] == '\0') 220 strlcpy(init_params->def_p.name, name, 221 sizeof(init_params->def_p.name)); 222 223 dev = rte_cryptodev_pmd_create(init_params->def_p.name, 224 &pci_dev->device, 225 &init_params->def_p); 226 if (dev == NULL) { 227 CCP_LOG_ERR("failed to create cryptodev vdev"); 228 goto init_error; 229 } 230 231 cryptodev_cnt = ccp_probe_devices(pci_dev, ccp_pci_id); 232 233 if (cryptodev_cnt == 0) { 234 CCP_LOG_ERR("failed to detect CCP crypto device"); 235 goto init_error; 236 } 237 238 CCP_LOG_DBG("CCP : Crypto device count = %d\n", cryptodev_cnt); 239 dev->device = &pci_dev->device; 240 dev->device->driver = &pci_drv->driver; 241 dev->driver_id = ccp_cryptodev_driver_id; 242 243 /* register rx/tx burst functions for data path */ 244 dev->dev_ops = ccp_pmd_ops; 245 dev->enqueue_burst = ccp_pmd_enqueue_burst; 246 dev->dequeue_burst = ccp_pmd_dequeue_burst; 247 248 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 249 RTE_CRYPTODEV_FF_HW_ACCELERATED | 250 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING | 251 RTE_CRYPTODEV_FF_SYM_SESSIONLESS; 252 253 internals = dev->data->dev_private; 254 255 internals->max_nb_qpairs = init_params->def_p.max_nb_queue_pairs; 256 internals->auth_opt = init_params->auth_opt; 257 internals->crypto_num_dev = cryptodev_cnt; 258 259 rte_cryptodev_pmd_probing_finish(dev); 260 261 return 0; 262 263 init_error: 264 CCP_LOG_ERR("driver %s: %s() failed", 265 init_params->def_p.name, __func__); 266 cryptodev_ccp_remove(pci_dev); 267 268 return -EFAULT; 269 } 270 271 /** Probe ccp pmd */ 272 static int 273 cryptodev_ccp_probe(struct rte_pci_driver *pci_drv __rte_unused, 274 struct rte_pci_device *pci_dev) 275 { 276 int rc = 0; 277 char name[RTE_CRYPTODEV_NAME_MAX_LEN]; 278 struct ccp_pmd_init_params init_params = { 279 .def_p = { 280 "", 281 sizeof(struct ccp_private), 282 rte_socket_id(), 283 CCP_PMD_MAX_QUEUE_PAIRS 284 }, 285 .auth_opt = CCP_PMD_AUTH_OPT_CCP, 286 }; 287 288 if (ccp_pmd_init_done) { 289 RTE_LOG(INFO, PMD, "CCP PMD already initialized\n"); 290 return -EFAULT; 291 } 292 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 293 if (name[0] == '\0') 294 return -EINVAL; 295 296 init_params.def_p.max_nb_queue_pairs = CCP_PMD_MAX_QUEUE_PAIRS; 297 298 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name, 299 init_params.def_p.socket_id); 300 RTE_LOG(INFO, PMD, "Max number of queue pairs = %d\n", 301 init_params.def_p.max_nb_queue_pairs); 302 RTE_LOG(INFO, PMD, "Authentication offload to %s\n", 303 ((init_params.auth_opt == 0) ? "CCP" : "CPU")); 304 305 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 306 307 rc = cryptodev_ccp_create(name, pci_dev, &init_params, pci_drv); 308 if (rc) 309 return rc; 310 ccp_pmd_init_done = 1; 311 return 0; 312 } 313 314 static struct rte_pci_driver cryptodev_ccp_pmd_drv = { 315 .id_table = ccp_pci_id, 316 .drv_flags = RTE_PCI_DRV_NEED_MAPPING, 317 .probe = cryptodev_ccp_probe, 318 .remove = cryptodev_ccp_remove 319 }; 320 321 static struct cryptodev_driver ccp_crypto_drv; 322 323 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_CCP_PMD, cryptodev_ccp_pmd_drv); 324 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_CCP_PMD, "* igb_uio | uio_pci_generic | vfio-pci"); 325 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_CCP_PMD, 326 "max_nb_queue_pairs=<int> " 327 "socket_id=<int> " 328 "ccp_auth_opt=<int>"); 329 RTE_PMD_REGISTER_CRYPTO_DRIVER(ccp_crypto_drv, cryptodev_ccp_pmd_drv.driver, 330 ccp_cryptodev_driver_id); 331