cryptodev: break dependency on PCI device bus
[dpdk.git] / drivers / crypto / qat / rte_qat_cryptodev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_common.h>
35 #include <rte_dev.h>
36 #include <rte_malloc.h>
37 #include <rte_pci.h>
38 #include <rte_cryptodev_pmd.h>
39
40 #include "qat_crypto.h"
41 #include "qat_logs.h"
42
43 uint8_t cryptodev_qat_driver_id;
44
45 static const struct rte_cryptodev_capabilities qat_gen1_capabilities[] = {
46         QAT_BASE_GEN1_SYM_CAPABILITIES,
47         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
48 };
49
50 static const struct rte_cryptodev_capabilities qat_gen2_capabilities[] = {
51         QAT_BASE_GEN1_SYM_CAPABILITIES,
52         QAT_EXTRA_GEN2_SYM_CAPABILITIES,
53         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
54 };
55
56 static struct rte_cryptodev_ops crypto_qat_ops = {
57
58                 /* Device related operations */
59                 .dev_configure          = qat_dev_config,
60                 .dev_start              = qat_dev_start,
61                 .dev_stop               = qat_dev_stop,
62                 .dev_close              = qat_dev_close,
63                 .dev_infos_get          = qat_dev_info_get,
64
65                 .stats_get              = qat_crypto_sym_stats_get,
66                 .stats_reset            = qat_crypto_sym_stats_reset,
67                 .queue_pair_setup       = qat_crypto_sym_qp_setup,
68                 .queue_pair_release     = qat_crypto_sym_qp_release,
69                 .queue_pair_start       = NULL,
70                 .queue_pair_stop        = NULL,
71                 .queue_pair_count       = NULL,
72
73                 /* Crypto related operations */
74                 .session_get_size       = qat_crypto_sym_get_session_private_size,
75                 .session_configure      = qat_crypto_sym_configure_session,
76                 .session_clear          = qat_crypto_sym_clear_session
77 };
78
79 /*
80  * The set of PCI devices this driver supports
81  */
82
83 static const struct rte_pci_id pci_id_qat_map[] = {
84                 {
85                         RTE_PCI_DEVICE(0x8086, 0x0443),
86                 },
87                 {
88                         RTE_PCI_DEVICE(0x8086, 0x37c9),
89                 },
90                 {
91                         RTE_PCI_DEVICE(0x8086, 0x19e3),
92                 },
93                 {
94                         RTE_PCI_DEVICE(0x8086, 0x6f55),
95                 },
96                 {.device_id = 0},
97 };
98
99 static int
100 crypto_qat_create(const char *name, struct rte_pci_device *pci_dev,
101                 struct rte_cryptodev_pmd_init_params *init_params)
102 {
103         struct rte_cryptodev *cryptodev;
104         struct qat_pmd_private *internals;
105
106         PMD_INIT_FUNC_TRACE();
107
108         cryptodev = rte_cryptodev_pmd_create(name, &pci_dev->device,
109                         init_params);
110         if (cryptodev == NULL)
111                 return -ENODEV;
112
113         cryptodev->driver_id = cryptodev_qat_driver_id;
114         cryptodev->dev_ops = &crypto_qat_ops;
115
116         cryptodev->enqueue_burst = qat_pmd_enqueue_op_burst;
117         cryptodev->dequeue_burst = qat_pmd_dequeue_op_burst;
118
119         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
120                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
121                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
122                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
123
124         internals = cryptodev->data->dev_private;
125         internals->max_nb_sessions = init_params->max_nb_sessions;
126         switch (pci_dev->id.device_id) {
127         case 0x0443:
128                 internals->qat_dev_gen = QAT_GEN1;
129                 internals->qat_dev_capabilities = qat_gen1_capabilities;
130                 break;
131         case 0x37c9:
132         case 0x19e3:
133         case 0x6f55:
134                 internals->qat_dev_gen = QAT_GEN2;
135                 internals->qat_dev_capabilities = qat_gen2_capabilities;
136                 break;
137         default:
138                 PMD_DRV_LOG(ERR,
139                         "Invalid dev_id, can't determine capabilities");
140                 break;
141         }
142
143         /*
144          * For secondary processes, we don't initialise any further as primary
145          * has already done this work. Only check we don't need a different
146          * RX function
147          */
148         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
149                 PMD_DRV_LOG(DEBUG, "Device already initialised by primary process");
150                 return 0;
151         }
152
153         return 0;
154 }
155
156 static int crypto_qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
157                 struct rte_pci_device *pci_dev)
158 {
159         struct rte_cryptodev_pmd_init_params init_params = {
160                 .name = "",
161                 .socket_id = rte_socket_id(),
162                 .private_data_size = sizeof(struct qat_pmd_private),
163                 .max_nb_sessions = RTE_QAT_PMD_MAX_NB_SESSIONS
164         };
165         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
166
167         PMD_DRV_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
168                         pci_dev->addr.bus,
169                         pci_dev->addr.devid,
170                         pci_dev->addr.function);
171
172         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
173
174         return crypto_qat_create(name, pci_dev, &init_params);
175 }
176
177 static int crypto_qat_pci_remove(struct rte_pci_device *pci_dev)
178 {
179         struct rte_cryptodev *cryptodev;
180         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
181
182         if (pci_dev == NULL)
183                 return -EINVAL;
184
185         rte_pci_device_name(&pci_dev->addr, cryptodev_name,
186                         sizeof(cryptodev_name));
187
188         cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
189         if (cryptodev == NULL)
190                 return -ENODEV;
191
192         /* free crypto device */
193         return rte_cryptodev_pmd_destroy(cryptodev);
194 }
195
196 static struct rte_pci_driver rte_qat_pmd = {
197         .id_table = pci_id_qat_map,
198         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
199         .probe = crypto_qat_pci_probe,
200         .remove = crypto_qat_pci_remove
201 };
202
203 static struct cryptodev_driver qat_crypto_drv;
204
205 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_QAT_SYM_PMD, rte_qat_pmd);
206 RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_QAT_SYM_PMD, pci_id_qat_map);
207 RTE_PMD_REGISTER_CRYPTO_DRIVER(qat_crypto_drv, rte_qat_pmd,
208                 cryptodev_qat_driver_id);