GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / crypto / ccp / ccp-pci.c
1 /*
2  * AMD Cryptographic Coprocessor (CCP) driver
3  *
4  * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
5  *
6  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7  * Author: Gary R Hook <gary.hook@amd.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/pci.h>
18 #include <linux/pci_ids.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/kthread.h>
21 #include <linux/sched.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/ccp.h>
26
27 #include "ccp-dev.h"
28
29 #define MSIX_VECTORS                    2
30
31 struct ccp_msix {
32         u32 vector;
33         char name[16];
34 };
35
36 struct ccp_pci {
37         int msix_count;
38         struct ccp_msix msix[MSIX_VECTORS];
39 };
40
41 static int ccp_get_msix_irqs(struct ccp_device *ccp)
42 {
43         struct ccp_pci *ccp_pci = ccp->dev_specific;
44         struct device *dev = ccp->dev;
45         struct pci_dev *pdev = to_pci_dev(dev);
46         struct msix_entry msix_entry[MSIX_VECTORS];
47         unsigned int name_len = sizeof(ccp_pci->msix[0].name) - 1;
48         int v, ret;
49
50         for (v = 0; v < ARRAY_SIZE(msix_entry); v++)
51                 msix_entry[v].entry = v;
52
53         ret = pci_enable_msix_range(pdev, msix_entry, 1, v);
54         if (ret < 0)
55                 return ret;
56
57         ccp_pci->msix_count = ret;
58         for (v = 0; v < ccp_pci->msix_count; v++) {
59                 /* Set the interrupt names and request the irqs */
60                 snprintf(ccp_pci->msix[v].name, name_len, "%s-%u",
61                          ccp->name, v);
62                 ccp_pci->msix[v].vector = msix_entry[v].vector;
63                 ret = request_irq(ccp_pci->msix[v].vector,
64                                   ccp->vdata->perform->irqhandler,
65                                   0, ccp_pci->msix[v].name, dev);
66                 if (ret) {
67                         dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n",
68                                    ret);
69                         goto e_irq;
70                 }
71         }
72         ccp->use_tasklet = true;
73
74         return 0;
75
76 e_irq:
77         while (v--)
78                 free_irq(ccp_pci->msix[v].vector, dev);
79
80         pci_disable_msix(pdev);
81
82         ccp_pci->msix_count = 0;
83
84         return ret;
85 }
86
87 static int ccp_get_msi_irq(struct ccp_device *ccp)
88 {
89         struct device *dev = ccp->dev;
90         struct pci_dev *pdev = to_pci_dev(dev);
91         int ret;
92
93         ret = pci_enable_msi(pdev);
94         if (ret)
95                 return ret;
96
97         ccp->irq = pdev->irq;
98         ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0,
99                           ccp->name, dev);
100         if (ret) {
101                 dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret);
102                 goto e_msi;
103         }
104         ccp->use_tasklet = true;
105
106         return 0;
107
108 e_msi:
109         pci_disable_msi(pdev);
110
111         return ret;
112 }
113
114 static int ccp_get_irqs(struct ccp_device *ccp)
115 {
116         struct device *dev = ccp->dev;
117         int ret;
118
119         ret = ccp_get_msix_irqs(ccp);
120         if (!ret)
121                 return 0;
122
123         /* Couldn't get MSI-X vectors, try MSI */
124         dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
125         ret = ccp_get_msi_irq(ccp);
126         if (!ret)
127                 return 0;
128
129         /* Couldn't get MSI interrupt */
130         dev_notice(dev, "could not enable MSI (%d)\n", ret);
131
132         return ret;
133 }
134
135 static void ccp_free_irqs(struct ccp_device *ccp)
136 {
137         struct ccp_pci *ccp_pci = ccp->dev_specific;
138         struct device *dev = ccp->dev;
139         struct pci_dev *pdev = to_pci_dev(dev);
140
141         if (ccp_pci->msix_count) {
142                 while (ccp_pci->msix_count--)
143                         free_irq(ccp_pci->msix[ccp_pci->msix_count].vector,
144                                  dev);
145                 pci_disable_msix(pdev);
146         } else if (ccp->irq) {
147                 free_irq(ccp->irq, dev);
148                 pci_disable_msi(pdev);
149         }
150         ccp->irq = 0;
151 }
152
153 static int ccp_find_mmio_area(struct ccp_device *ccp)
154 {
155         struct device *dev = ccp->dev;
156         struct pci_dev *pdev = to_pci_dev(dev);
157         resource_size_t io_len;
158         unsigned long io_flags;
159
160         io_flags = pci_resource_flags(pdev, ccp->vdata->bar);
161         io_len = pci_resource_len(pdev, ccp->vdata->bar);
162         if ((io_flags & IORESOURCE_MEM) &&
163             (io_len >= (ccp->vdata->offset + 0x800)))
164                 return ccp->vdata->bar;
165
166         return -EIO;
167 }
168
169 static int ccp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
170 {
171         struct ccp_device *ccp;
172         struct ccp_pci *ccp_pci;
173         struct device *dev = &pdev->dev;
174         unsigned int bar;
175         int ret;
176
177         ret = -ENOMEM;
178         ccp = ccp_alloc_struct(dev);
179         if (!ccp)
180                 goto e_err;
181
182         ccp_pci = devm_kzalloc(dev, sizeof(*ccp_pci), GFP_KERNEL);
183         if (!ccp_pci)
184                 goto e_err;
185
186         ccp->dev_specific = ccp_pci;
187         ccp->vdata = (struct ccp_vdata *)id->driver_data;
188         if (!ccp->vdata || !ccp->vdata->version) {
189                 ret = -ENODEV;
190                 dev_err(dev, "missing driver data\n");
191                 goto e_err;
192         }
193         ccp->get_irq = ccp_get_irqs;
194         ccp->free_irq = ccp_free_irqs;
195
196         ret = pci_request_regions(pdev, "ccp");
197         if (ret) {
198                 dev_err(dev, "pci_request_regions failed (%d)\n", ret);
199                 goto e_err;
200         }
201
202         ret = pci_enable_device(pdev);
203         if (ret) {
204                 dev_err(dev, "pci_enable_device failed (%d)\n", ret);
205                 goto e_regions;
206         }
207
208         pci_set_master(pdev);
209
210         ret = ccp_find_mmio_area(ccp);
211         if (ret < 0)
212                 goto e_device;
213         bar = ret;
214
215         ret = -EIO;
216         ccp->io_map = pci_iomap(pdev, bar, 0);
217         if (!ccp->io_map) {
218                 dev_err(dev, "pci_iomap failed\n");
219                 goto e_device;
220         }
221         ccp->io_regs = ccp->io_map + ccp->vdata->offset;
222
223         ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
224         if (ret) {
225                 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
226                 if (ret) {
227                         dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
228                                 ret);
229                         goto e_iomap;
230                 }
231         }
232
233         dev_set_drvdata(dev, ccp);
234
235         if (ccp->vdata->setup)
236                 ccp->vdata->setup(ccp);
237
238         ret = ccp->vdata->perform->init(ccp);
239         if (ret)
240                 goto e_iomap;
241
242         dev_notice(dev, "enabled\n");
243
244         return 0;
245
246 e_iomap:
247         pci_iounmap(pdev, ccp->io_map);
248
249 e_device:
250         pci_disable_device(pdev);
251
252 e_regions:
253         pci_release_regions(pdev);
254
255 e_err:
256         dev_notice(dev, "initialization failed\n");
257         return ret;
258 }
259
260 static void ccp_pci_remove(struct pci_dev *pdev)
261 {
262         struct device *dev = &pdev->dev;
263         struct ccp_device *ccp = dev_get_drvdata(dev);
264
265         if (!ccp)
266                 return;
267
268         ccp->vdata->perform->destroy(ccp);
269
270         pci_iounmap(pdev, ccp->io_map);
271
272         pci_disable_device(pdev);
273
274         pci_release_regions(pdev);
275
276         dev_notice(dev, "disabled\n");
277 }
278
279 #ifdef CONFIG_PM
280 static int ccp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
281 {
282         struct device *dev = &pdev->dev;
283         struct ccp_device *ccp = dev_get_drvdata(dev);
284         unsigned long flags;
285         unsigned int i;
286
287         spin_lock_irqsave(&ccp->cmd_lock, flags);
288
289         ccp->suspending = 1;
290
291         /* Wake all the queue kthreads to prepare for suspend */
292         for (i = 0; i < ccp->cmd_q_count; i++)
293                 wake_up_process(ccp->cmd_q[i].kthread);
294
295         spin_unlock_irqrestore(&ccp->cmd_lock, flags);
296
297         /* Wait for all queue kthreads to say they're done */
298         while (!ccp_queues_suspended(ccp))
299                 wait_event_interruptible(ccp->suspend_queue,
300                                          ccp_queues_suspended(ccp));
301
302         return 0;
303 }
304
305 static int ccp_pci_resume(struct pci_dev *pdev)
306 {
307         struct device *dev = &pdev->dev;
308         struct ccp_device *ccp = dev_get_drvdata(dev);
309         unsigned long flags;
310         unsigned int i;
311
312         spin_lock_irqsave(&ccp->cmd_lock, flags);
313
314         ccp->suspending = 0;
315
316         /* Wake up all the kthreads */
317         for (i = 0; i < ccp->cmd_q_count; i++) {
318                 ccp->cmd_q[i].suspended = 0;
319                 wake_up_process(ccp->cmd_q[i].kthread);
320         }
321
322         spin_unlock_irqrestore(&ccp->cmd_lock, flags);
323
324         return 0;
325 }
326 #endif
327
328 static const struct pci_device_id ccp_pci_table[] = {
329         { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&ccpv3 },
330         { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&ccpv5a },
331         { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&ccpv5b },
332         /* Last entry must be zero */
333         { 0, }
334 };
335 MODULE_DEVICE_TABLE(pci, ccp_pci_table);
336
337 static struct pci_driver ccp_pci_driver = {
338         .name = "ccp",
339         .id_table = ccp_pci_table,
340         .probe = ccp_pci_probe,
341         .remove = ccp_pci_remove,
342 #ifdef CONFIG_PM
343         .suspend = ccp_pci_suspend,
344         .resume = ccp_pci_resume,
345 #endif
346 };
347
348 int ccp_pci_init(void)
349 {
350         return pci_register_driver(&ccp_pci_driver);
351 }
352
353 void ccp_pci_exit(void)
354 {
355         pci_unregister_driver(&ccp_pci_driver);
356 }