]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/crypto/qat/qat_common/adf_aer.c
regmap: rbtree: When adding a reg do a bsearch for target node
[karo-tx-linux.git] / drivers / crypto / qat / qat_common / adf_aer.c
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/pci.h>
49 #include <linux/aer.h>
50 #include <linux/completion.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include "adf_accel_devices.h"
54 #include "adf_common_drv.h"
55
56 static struct workqueue_struct *device_reset_wq;
57
58 static pci_ers_result_t adf_error_detected(struct pci_dev *pdev,
59                                            pci_channel_state_t state)
60 {
61         struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
62
63         dev_info(&pdev->dev, "Acceleration driver hardware error detected.\n");
64         if (!accel_dev) {
65                 dev_err(&pdev->dev, "Can't find acceleration device\n");
66                 return PCI_ERS_RESULT_DISCONNECT;
67         }
68
69         if (state == pci_channel_io_perm_failure) {
70                 dev_err(&pdev->dev, "Can't recover from device error\n");
71                 return PCI_ERS_RESULT_DISCONNECT;
72         }
73
74         return PCI_ERS_RESULT_NEED_RESET;
75 }
76
77 /* reset dev data */
78 struct adf_reset_dev_data {
79         int mode;
80         struct adf_accel_dev *accel_dev;
81         struct completion compl;
82         struct work_struct reset_work;
83 };
84
85 static void adf_dev_restore(struct adf_accel_dev *accel_dev)
86 {
87         struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
88         struct pci_dev *parent = pdev->bus->self;
89         uint16_t bridge_ctl = 0;
90
91         dev_info(&GET_DEV(accel_dev), "Resetting device qat_dev%d\n",
92                  accel_dev->accel_id);
93
94         if (!parent)
95                 parent = pdev;
96
97         if (!pci_wait_for_pending_transaction(pdev))
98                 dev_info(&GET_DEV(accel_dev),
99                          "Transaction still in progress. Proceeding\n");
100
101         pci_read_config_word(parent, PCI_BRIDGE_CONTROL, &bridge_ctl);
102         bridge_ctl |= PCI_BRIDGE_CTL_BUS_RESET;
103         pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
104         msleep(100);
105         bridge_ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
106         pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
107         msleep(100);
108         pci_restore_state(pdev);
109         pci_save_state(pdev);
110 }
111
112 static void adf_device_reset_worker(struct work_struct *work)
113 {
114         struct adf_reset_dev_data *reset_data =
115                   container_of(work, struct adf_reset_dev_data, reset_work);
116         struct adf_accel_dev *accel_dev = reset_data->accel_dev;
117
118         adf_dev_restarting_notify(accel_dev);
119         adf_dev_stop(accel_dev);
120         adf_dev_shutdown(accel_dev);
121         adf_dev_restore(accel_dev);
122         if (adf_dev_init(accel_dev) || adf_dev_start(accel_dev)) {
123                 /* The device hanged and we can't restart it so stop here */
124                 dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
125                 kfree(reset_data);
126                 WARN(1, "QAT: device restart failed. Device is unusable\n");
127                 return;
128         }
129         adf_dev_restarted_notify(accel_dev);
130         clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
131
132         /* The dev is back alive. Notify the caller if in sync mode */
133         if (reset_data->mode == ADF_DEV_RESET_SYNC)
134                 complete(&reset_data->compl);
135         else
136                 kfree(reset_data);
137 }
138
139 static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
140                                       enum adf_dev_reset_mode mode)
141 {
142         struct adf_reset_dev_data *reset_data;
143
144         if (!adf_dev_started(accel_dev) ||
145             test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
146                 return 0;
147
148         set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
149         reset_data = kzalloc(sizeof(*reset_data), GFP_ATOMIC);
150         if (!reset_data)
151                 return -ENOMEM;
152         reset_data->accel_dev = accel_dev;
153         init_completion(&reset_data->compl);
154         reset_data->mode = mode;
155         INIT_WORK(&reset_data->reset_work, adf_device_reset_worker);
156         queue_work(device_reset_wq, &reset_data->reset_work);
157
158         /* If in sync mode wait for the result */
159         if (mode == ADF_DEV_RESET_SYNC) {
160                 int ret = 0;
161                 /* Maximum device reset time is 10 seconds */
162                 unsigned long wait_jiffies = msecs_to_jiffies(10000);
163                 unsigned long timeout = wait_for_completion_timeout(
164                                    &reset_data->compl, wait_jiffies);
165                 if (!timeout) {
166                         dev_err(&GET_DEV(accel_dev),
167                                 "Reset device timeout expired\n");
168                         ret = -EFAULT;
169                 }
170                 kfree(reset_data);
171                 return ret;
172         }
173         return 0;
174 }
175
176 static pci_ers_result_t adf_slot_reset(struct pci_dev *pdev)
177 {
178         struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
179
180         if (!accel_dev) {
181                 pr_err("QAT: Can't find acceleration device\n");
182                 return PCI_ERS_RESULT_DISCONNECT;
183         }
184         pci_cleanup_aer_uncorrect_error_status(pdev);
185         if (adf_dev_aer_schedule_reset(accel_dev, ADF_DEV_RESET_SYNC))
186                 return PCI_ERS_RESULT_DISCONNECT;
187
188         return PCI_ERS_RESULT_RECOVERED;
189 }
190
191 static void adf_resume(struct pci_dev *pdev)
192 {
193         dev_info(&pdev->dev, "Acceleration driver reset completed\n");
194         dev_info(&pdev->dev, "Device is up and runnig\n");
195 }
196
197 static struct pci_error_handlers adf_err_handler = {
198         .error_detected = adf_error_detected,
199         .slot_reset = adf_slot_reset,
200         .resume = adf_resume,
201 };
202
203 /**
204  * adf_enable_aer() - Enable Advance Error Reporting for acceleration device
205  * @accel_dev:  Pointer to acceleration device.
206  * @adf:        PCI device driver owning the given acceleration device.
207  *
208  * Function enables PCI Advance Error Reporting for the
209  * QAT acceleration device accel_dev.
210  * To be used by QAT device specific drivers.
211  *
212  * Return: 0 on success, error code otherwise.
213  */
214 int adf_enable_aer(struct adf_accel_dev *accel_dev, struct pci_driver *adf)
215 {
216         struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
217
218         adf->err_handler = &adf_err_handler;
219         pci_enable_pcie_error_reporting(pdev);
220         return 0;
221 }
222 EXPORT_SYMBOL_GPL(adf_enable_aer);
223
224 /**
225  * adf_disable_aer() - Enable Advance Error Reporting for acceleration device
226  * @accel_dev:  Pointer to acceleration device.
227  *
228  * Function disables PCI Advance Error Reporting for the
229  * QAT acceleration device accel_dev.
230  * To be used by QAT device specific drivers.
231  *
232  * Return: void
233  */
234 void adf_disable_aer(struct adf_accel_dev *accel_dev)
235 {
236         struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
237
238         pci_disable_pcie_error_reporting(pdev);
239 }
240 EXPORT_SYMBOL_GPL(adf_disable_aer);
241
242 int adf_init_aer(void)
243 {
244         device_reset_wq = create_workqueue("qat_device_reset_wq");
245         return !device_reset_wq ? -EFAULT : 0;
246 }
247
248 void adf_exit_aer(void)
249 {
250         if (device_reset_wq)
251                 destroy_workqueue(device_reset_wq);
252         device_reset_wq = NULL;
253 }