]>
Commit | Line | Data |
---|---|---|
7afa232e TS |
1 | /* |
2 | This file is provided under a dual BSD/GPLv2 license. When using or | |
3 | redistributing this file, you may do so under either license. | |
4 | ||
5 | GPL LICENSE SUMMARY | |
6 | Copyright(c) 2014 Intel Corporation. | |
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of version 2 of the GNU General Public License as | |
9 | published by the Free Software Foundation. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | Contact Information: | |
17 | qat-linux@intel.com | |
18 | ||
19 | BSD LICENSE | |
20 | Copyright(c) 2014 Intel Corporation. | |
21 | Redistribution and use in source and binary forms, with or without | |
22 | modification, are permitted provided that the following conditions | |
23 | are met: | |
24 | ||
25 | * Redistributions of source code must retain the above copyright | |
26 | notice, this list of conditions and the following disclaimer. | |
27 | * Redistributions in binary form must reproduce the above copyright | |
28 | notice, this list of conditions and the following disclaimer in | |
29 | the documentation and/or other materials provided with the | |
30 | distribution. | |
31 | * Neither the name of Intel Corporation nor the names of its | |
32 | contributors may be used to endorse or promote products derived | |
33 | from this software without specific prior written permission. | |
34 | ||
35 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
36 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
37 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
38 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
39 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
40 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
41 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
42 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
43 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
44 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
45 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
46 | */ | |
47 | #include <linux/kernel.h> | |
48 | #include <linux/module.h> | |
49 | #include <linux/pci.h> | |
50 | #include <linux/init.h> | |
51 | #include <linux/types.h> | |
52 | #include <linux/fs.h> | |
53 | #include <linux/slab.h> | |
54 | #include <linux/errno.h> | |
55 | #include <linux/device.h> | |
56 | #include <linux/dma-mapping.h> | |
57 | #include <linux/platform_device.h> | |
58 | #include <linux/workqueue.h> | |
59 | #include <linux/io.h> | |
60 | #include <adf_accel_devices.h> | |
61 | #include <adf_common_drv.h> | |
62 | #include <adf_cfg.h> | |
7afa232e | 63 | #include "adf_dh895xcc_hw_data.h" |
7afa232e TS |
64 | |
65 | #define ADF_SYSTEM_DEVICE(device_id) \ | |
66 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)} | |
67 | ||
68 | static const struct pci_device_id adf_pci_tbl[] = { | |
69 | ADF_SYSTEM_DEVICE(ADF_DH895XCC_PCI_DEVICE_ID), | |
70 | {0,} | |
71 | }; | |
72 | MODULE_DEVICE_TABLE(pci, adf_pci_tbl); | |
73 | ||
74 | static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent); | |
75 | static void adf_remove(struct pci_dev *dev); | |
76 | ||
77 | static struct pci_driver adf_driver = { | |
78 | .id_table = adf_pci_tbl, | |
1a72d3a6 | 79 | .name = ADF_DH895XCC_DEVICE_NAME, |
7afa232e | 80 | .probe = adf_probe, |
ed8ccaef TS |
81 | .remove = adf_remove, |
82 | .sriov_configure = adf_sriov_configure, | |
7afa232e TS |
83 | }; |
84 | ||
ed8ccaef TS |
85 | static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev) |
86 | { | |
87 | pci_release_regions(accel_dev->accel_pci_dev.pci_dev); | |
88 | pci_disable_device(accel_dev->accel_pci_dev.pci_dev); | |
89 | } | |
90 | ||
7afa232e TS |
91 | static void adf_cleanup_accel(struct adf_accel_dev *accel_dev) |
92 | { | |
93 | struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev; | |
94 | int i; | |
95 | ||
7afa232e TS |
96 | for (i = 0; i < ADF_PCI_MAX_BARS; i++) { |
97 | struct adf_bar *bar = &accel_pci_dev->pci_bars[i]; | |
98 | ||
99 | if (bar->virt_addr) | |
100 | pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr); | |
101 | } | |
102 | ||
103 | if (accel_dev->hw_device) { | |
5995752e | 104 | switch (accel_pci_dev->pci_dev->device) { |
7afa232e TS |
105 | case ADF_DH895XCC_PCI_DEVICE_ID: |
106 | adf_clean_hw_data_dh895xcc(accel_dev->hw_device); | |
107 | break; | |
108 | default: | |
109 | break; | |
110 | } | |
111 | kfree(accel_dev->hw_device); | |
ed8ccaef | 112 | accel_dev->hw_device = NULL; |
7afa232e TS |
113 | } |
114 | adf_cfg_dev_remove(accel_dev); | |
115 | debugfs_remove(accel_dev->debugfs_dir); | |
ed8ccaef | 116 | adf_devmgr_rm_dev(accel_dev, NULL); |
7afa232e TS |
117 | } |
118 | ||
7afa232e TS |
119 | static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
120 | { | |
121 | struct adf_accel_dev *accel_dev; | |
122 | struct adf_accel_pci *accel_pci_dev; | |
123 | struct adf_hw_device_data *hw_data; | |
7afa232e TS |
124 | char name[ADF_DEVICE_NAME_LENGTH]; |
125 | unsigned int i, bar_nr; | |
ed8ccaef | 126 | int ret, bar_mask; |
7afa232e TS |
127 | |
128 | switch (ent->device) { | |
129 | case ADF_DH895XCC_PCI_DEVICE_ID: | |
130 | break; | |
131 | default: | |
132 | dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device); | |
133 | return -ENODEV; | |
134 | } | |
135 | ||
09adc878 TS |
136 | if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) { |
137 | /* If the accelerator is connected to a node with no memory | |
138 | * there is no point in using the accelerator since the remote | |
139 | * memory transaction will be very slow. */ | |
140 | dev_err(&pdev->dev, "Invalid NUMA configuration.\n"); | |
141 | return -EINVAL; | |
142 | } | |
143 | ||
144 | accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL, | |
64a31be3 | 145 | dev_to_node(&pdev->dev)); |
7afa232e TS |
146 | if (!accel_dev) |
147 | return -ENOMEM; | |
148 | ||
7afa232e | 149 | INIT_LIST_HEAD(&accel_dev->crypto_list); |
ed8ccaef TS |
150 | accel_pci_dev = &accel_dev->accel_pci_dev; |
151 | accel_pci_dev->pci_dev = pdev; | |
7afa232e TS |
152 | |
153 | /* Add accel device to accel table. | |
154 | * This should be called before adf_cleanup_accel is called */ | |
ed8ccaef | 155 | if (adf_devmgr_add_dev(accel_dev, NULL)) { |
7afa232e TS |
156 | dev_err(&pdev->dev, "Failed to add new accelerator device.\n"); |
157 | kfree(accel_dev); | |
158 | return -EFAULT; | |
159 | } | |
160 | ||
161 | accel_dev->owner = THIS_MODULE; | |
162 | /* Allocate and configure device configuration structure */ | |
09adc878 TS |
163 | hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL, |
164 | dev_to_node(&pdev->dev)); | |
7afa232e TS |
165 | if (!hw_data) { |
166 | ret = -ENOMEM; | |
167 | goto out_err; | |
168 | } | |
169 | ||
170 | accel_dev->hw_device = hw_data; | |
c52b6733 | 171 | adf_init_hw_data_dh895xcc(accel_dev->hw_device); |
7afa232e | 172 | pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid); |
1a72d3a6 | 173 | pci_read_config_dword(pdev, ADF_DEVICE_FUSECTL_OFFSET, |
7afa232e TS |
174 | &hw_data->fuses); |
175 | ||
176 | /* Get Accelerators and Accelerators Engines masks */ | |
177 | hw_data->accel_mask = hw_data->get_accel_mask(hw_data->fuses); | |
178 | hw_data->ae_mask = hw_data->get_ae_mask(hw_data->fuses); | |
179 | accel_pci_dev->sku = hw_data->get_sku(hw_data); | |
7afa232e TS |
180 | /* If the device has no acceleration engines then ignore it. */ |
181 | if (!hw_data->accel_mask || !hw_data->ae_mask || | |
182 | ((~hw_data->ae_mask) & 0x01)) { | |
183 | dev_err(&pdev->dev, "No acceleration units found"); | |
184 | ret = -EFAULT; | |
185 | goto out_err; | |
186 | } | |
187 | ||
188 | /* Create dev top level debugfs entry */ | |
ed8ccaef TS |
189 | snprintf(name, sizeof(name), "%s%s_%02x:%02d.%02d", |
190 | ADF_DEVICE_NAME_PREFIX, hw_data->dev_class->name, | |
191 | pdev->bus->number, PCI_SLOT(pdev->devfn), | |
192 | PCI_FUNC(pdev->devfn)); | |
193 | ||
7afa232e TS |
194 | accel_dev->debugfs_dir = debugfs_create_dir(name, NULL); |
195 | if (!accel_dev->debugfs_dir) { | |
ed8ccaef | 196 | dev_err(&pdev->dev, "Could not create debugfs dir %s\n", name); |
7afa232e TS |
197 | ret = -EINVAL; |
198 | goto out_err; | |
199 | } | |
200 | ||
201 | /* Create device configuration table */ | |
202 | ret = adf_cfg_dev_add(accel_dev); | |
203 | if (ret) | |
204 | goto out_err; | |
205 | ||
e4fa1460 TS |
206 | pcie_set_readrq(pdev, 1024); |
207 | ||
7afa232e TS |
208 | /* enable PCI device */ |
209 | if (pci_enable_device(pdev)) { | |
210 | ret = -EFAULT; | |
211 | goto out_err; | |
212 | } | |
213 | ||
214 | /* set dma identifier */ | |
215 | if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) { | |
216 | if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) { | |
217 | dev_err(&pdev->dev, "No usable DMA configuration\n"); | |
218 | ret = -EFAULT; | |
ed8ccaef | 219 | goto out_err_disable; |
7afa232e TS |
220 | } else { |
221 | pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); | |
222 | } | |
223 | ||
224 | } else { | |
225 | pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); | |
226 | } | |
227 | ||
1a72d3a6 | 228 | if (pci_request_regions(pdev, ADF_DH895XCC_DEVICE_NAME)) { |
7afa232e | 229 | ret = -EFAULT; |
ed8ccaef | 230 | goto out_err_disable; |
7afa232e TS |
231 | } |
232 | ||
233 | /* Read accelerator capabilities mask */ | |
1a72d3a6 | 234 | pci_read_config_dword(pdev, ADF_DEVICE_LEGFUSE_OFFSET, |
7afa232e TS |
235 | &hw_data->accel_capabilities_mask); |
236 | ||
237 | /* Find and map all the device's BARS */ | |
ed8ccaef TS |
238 | i = 0; |
239 | bar_mask = pci_select_bars(pdev, IORESOURCE_MEM); | |
240 | for_each_set_bit(bar_nr, (const unsigned long *)&bar_mask, | |
241 | ADF_PCI_MAX_BARS * 2) { | |
242 | struct adf_bar *bar = &accel_pci_dev->pci_bars[i++]; | |
7afa232e | 243 | |
7afa232e TS |
244 | bar->base_addr = pci_resource_start(pdev, bar_nr); |
245 | if (!bar->base_addr) | |
246 | break; | |
247 | bar->size = pci_resource_len(pdev, bar_nr); | |
248 | bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0); | |
249 | if (!bar->virt_addr) { | |
ed8ccaef | 250 | dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr); |
7afa232e | 251 | ret = -EFAULT; |
ed8ccaef | 252 | goto out_err_free_reg; |
7afa232e | 253 | } |
7afa232e TS |
254 | } |
255 | pci_set_master(pdev); | |
256 | ||
257 | if (adf_enable_aer(accel_dev, &adf_driver)) { | |
258 | dev_err(&pdev->dev, "Failed to enable aer\n"); | |
259 | ret = -EFAULT; | |
ed8ccaef | 260 | goto out_err_free_reg; |
7afa232e TS |
261 | } |
262 | ||
7afa232e TS |
263 | if (pci_save_state(pdev)) { |
264 | dev_err(&pdev->dev, "Failed to save pci state\n"); | |
265 | ret = -ENOMEM; | |
ed8ccaef | 266 | goto out_err_free_reg; |
7afa232e TS |
267 | } |
268 | ||
1a72d3a6 | 269 | ret = qat_crypto_dev_config(accel_dev); |
22e4dda0 | 270 | if (ret) |
ed8ccaef | 271 | goto out_err_free_reg; |
22e4dda0 AB |
272 | |
273 | ret = adf_dev_init(accel_dev); | |
274 | if (ret) | |
ed8ccaef | 275 | goto out_err_dev_shutdown; |
7afa232e | 276 | |
22e4dda0 | 277 | ret = adf_dev_start(accel_dev); |
ed8ccaef TS |
278 | if (ret) |
279 | goto out_err_dev_stop; | |
7afa232e | 280 | |
ed8ccaef TS |
281 | return ret; |
282 | ||
283 | out_err_dev_stop: | |
284 | adf_dev_stop(accel_dev); | |
285 | out_err_dev_shutdown: | |
286 | adf_dev_shutdown(accel_dev); | |
287 | out_err_free_reg: | |
288 | pci_release_regions(accel_pci_dev->pci_dev); | |
289 | out_err_disable: | |
290 | pci_disable_device(accel_pci_dev->pci_dev); | |
7afa232e TS |
291 | out_err: |
292 | adf_cleanup_accel(accel_dev); | |
ed8ccaef | 293 | kfree(accel_dev); |
7afa232e TS |
294 | return ret; |
295 | } | |
296 | ||
83ce01d2 | 297 | static void adf_remove(struct pci_dev *pdev) |
7afa232e TS |
298 | { |
299 | struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev); | |
300 | ||
301 | if (!accel_dev) { | |
302 | pr_err("QAT: Driver removal failed\n"); | |
303 | return; | |
304 | } | |
305 | if (adf_dev_stop(accel_dev)) | |
306 | dev_err(&GET_DEV(accel_dev), "Failed to stop QAT accel dev\n"); | |
ed8ccaef TS |
307 | |
308 | adf_dev_shutdown(accel_dev); | |
7afa232e TS |
309 | adf_disable_aer(accel_dev); |
310 | adf_cleanup_accel(accel_dev); | |
ed8ccaef TS |
311 | adf_cleanup_pci_dev(accel_dev); |
312 | kfree(accel_dev); | |
7afa232e TS |
313 | } |
314 | ||
315 | static int __init adfdrv_init(void) | |
316 | { | |
317 | request_module("intel_qat"); | |
7afa232e TS |
318 | |
319 | if (pci_register_driver(&adf_driver)) { | |
320 | pr_err("QAT: Driver initialization failed\n"); | |
321 | return -EFAULT; | |
322 | } | |
323 | return 0; | |
324 | } | |
325 | ||
326 | static void __exit adfdrv_release(void) | |
327 | { | |
328 | pci_unregister_driver(&adf_driver); | |
7afa232e TS |
329 | } |
330 | ||
331 | module_init(adfdrv_init); | |
332 | module_exit(adfdrv_release); | |
333 | ||
334 | MODULE_LICENSE("Dual BSD/GPL"); | |
335 | MODULE_AUTHOR("Intel"); | |
dcd93e83 | 336 | MODULE_FIRMWARE(ADF_DH895XCC_FW); |
7afa232e | 337 | MODULE_DESCRIPTION("Intel(R) QuickAssist Technology"); |
551d7ed2 | 338 | MODULE_VERSION(ADF_DRV_VERSION); |