02319f779a49f7061d39fcc914ccfa35f34d14ca
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / fpga / core.c
1 /*
2  * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/module.h>
34 #include <linux/etherdevice.h>
35 #include <linux/mlx5/driver.h>
36
37 #include "mlx5_core.h"
38 #include "lib/mlx5.h"
39 #include "fpga/core.h"
40 #include "fpga/conn.h"
41
42 static const char *const mlx5_fpga_error_strings[] = {
43         "Null Syndrome",
44         "Corrupted DDR",
45         "Flash Timeout",
46         "Internal Link Error",
47         "Watchdog HW Failure",
48         "I2C Failure",
49         "Image Changed",
50         "Temperature Critical",
51 };
52
53 static struct mlx5_fpga_device *mlx5_fpga_device_alloc(void)
54 {
55         struct mlx5_fpga_device *fdev = NULL;
56
57         fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
58         if (!fdev)
59                 return NULL;
60
61         spin_lock_init(&fdev->state_lock);
62         fdev->state = MLX5_FPGA_STATUS_NONE;
63         return fdev;
64 }
65
66 static const char *mlx5_fpga_image_name(enum mlx5_fpga_image image)
67 {
68         switch (image) {
69         case MLX5_FPGA_IMAGE_USER:
70                 return "user";
71         case MLX5_FPGA_IMAGE_FACTORY:
72                 return "factory";
73         default:
74                 return "unknown";
75         }
76 }
77
78 static const char *mlx5_fpga_device_name(u32 device)
79 {
80         switch (device) {
81         case MLX5_FPGA_DEVICE_KU040:
82                 return "ku040";
83         case MLX5_FPGA_DEVICE_KU060:
84                 return "ku060";
85         case MLX5_FPGA_DEVICE_KU060_2:
86                 return "ku060_2";
87         case MLX5_FPGA_DEVICE_UNKNOWN:
88         default:
89                 return "unknown";
90         }
91 }
92
93 static int mlx5_fpga_device_load_check(struct mlx5_fpga_device *fdev)
94 {
95         struct mlx5_fpga_query query;
96         int err;
97
98         err = mlx5_fpga_query(fdev->mdev, &query);
99         if (err) {
100                 mlx5_fpga_err(fdev, "Failed to query status: %d\n", err);
101                 return err;
102         }
103
104         fdev->last_admin_image = query.admin_image;
105         fdev->last_oper_image = query.oper_image;
106
107         mlx5_fpga_dbg(fdev, "Status %u; Admin image %u; Oper image %u\n",
108                       query.status, query.admin_image, query.oper_image);
109
110         if (query.status != MLX5_FPGA_STATUS_SUCCESS) {
111                 mlx5_fpga_err(fdev, "%s image failed to load; status %u\n",
112                               mlx5_fpga_image_name(fdev->last_oper_image),
113                               query.status);
114                 return -EIO;
115         }
116
117         return 0;
118 }
119
120 static int mlx5_fpga_device_brb(struct mlx5_fpga_device *fdev)
121 {
122         int err;
123         struct mlx5_core_dev *mdev = fdev->mdev;
124
125         err = mlx5_fpga_ctrl_op(mdev, MLX5_FPGA_CTRL_OPERATION_SANDBOX_BYPASS_ON);
126         if (err) {
127                 mlx5_fpga_err(fdev, "Failed to set bypass on: %d\n", err);
128                 return err;
129         }
130         err = mlx5_fpga_ctrl_op(mdev, MLX5_FPGA_CTRL_OPERATION_RESET_SANDBOX);
131         if (err) {
132                 mlx5_fpga_err(fdev, "Failed to reset SBU: %d\n", err);
133                 return err;
134         }
135         err = mlx5_fpga_ctrl_op(mdev, MLX5_FPGA_CTRL_OPERATION_SANDBOX_BYPASS_OFF);
136         if (err) {
137                 mlx5_fpga_err(fdev, "Failed to set bypass off: %d\n", err);
138                 return err;
139         }
140         return 0;
141 }
142
143 int mlx5_fpga_device_start(struct mlx5_core_dev *mdev)
144 {
145         struct mlx5_fpga_device *fdev = mdev->fpga;
146         unsigned int max_num_qps;
147         unsigned long flags;
148         u32 fpga_device_id;
149         int err;
150
151         if (!fdev)
152                 return 0;
153
154         err = mlx5_fpga_device_load_check(fdev);
155         if (err)
156                 goto out;
157
158         err = mlx5_fpga_caps(fdev->mdev);
159         if (err)
160                 goto out;
161
162         fpga_device_id = MLX5_CAP_FPGA(fdev->mdev, fpga_device);
163         mlx5_fpga_info(fdev, "%s:%u; %s image, version %u; SBU %06x:%04x version %d\n",
164                        mlx5_fpga_device_name(fpga_device_id),
165                        fpga_device_id,
166                        mlx5_fpga_image_name(fdev->last_oper_image),
167                        MLX5_CAP_FPGA(fdev->mdev, image_version),
168                        MLX5_CAP_FPGA(fdev->mdev, ieee_vendor_id),
169                        MLX5_CAP_FPGA(fdev->mdev, sandbox_product_id),
170                        MLX5_CAP_FPGA(fdev->mdev, sandbox_product_version));
171
172         max_num_qps = MLX5_CAP_FPGA(mdev, shell_caps.max_num_qps);
173         err = mlx5_core_reserve_gids(mdev, max_num_qps);
174         if (err)
175                 goto out;
176
177         err = mlx5_fpga_conn_device_init(fdev);
178         if (err)
179                 goto err_rsvd_gid;
180
181         if (fdev->last_oper_image == MLX5_FPGA_IMAGE_USER) {
182                 err = mlx5_fpga_device_brb(fdev);
183                 if (err)
184                         goto err_conn_init;
185         }
186
187         goto out;
188
189 err_conn_init:
190         mlx5_fpga_conn_device_cleanup(fdev);
191
192 err_rsvd_gid:
193         mlx5_core_unreserve_gids(mdev, max_num_qps);
194 out:
195         spin_lock_irqsave(&fdev->state_lock, flags);
196         fdev->state = err ? MLX5_FPGA_STATUS_FAILURE : MLX5_FPGA_STATUS_SUCCESS;
197         spin_unlock_irqrestore(&fdev->state_lock, flags);
198         return err;
199 }
200
201 int mlx5_fpga_init(struct mlx5_core_dev *mdev)
202 {
203         struct mlx5_fpga_device *fdev = NULL;
204
205         if (!MLX5_CAP_GEN(mdev, fpga)) {
206                 mlx5_core_dbg(mdev, "FPGA capability not present\n");
207                 return 0;
208         }
209
210         mlx5_core_dbg(mdev, "Initializing FPGA\n");
211
212         fdev = mlx5_fpga_device_alloc();
213         if (!fdev)
214                 return -ENOMEM;
215
216         fdev->mdev = mdev;
217         mdev->fpga = fdev;
218
219         return 0;
220 }
221
222 void mlx5_fpga_device_stop(struct mlx5_core_dev *mdev)
223 {
224         struct mlx5_fpga_device *fdev = mdev->fpga;
225         unsigned int max_num_qps;
226         unsigned long flags;
227         int err;
228
229         if (!fdev)
230                 return;
231
232         spin_lock_irqsave(&fdev->state_lock, flags);
233         if (fdev->state != MLX5_FPGA_STATUS_SUCCESS) {
234                 spin_unlock_irqrestore(&fdev->state_lock, flags);
235                 return;
236         }
237         fdev->state = MLX5_FPGA_STATUS_NONE;
238         spin_unlock_irqrestore(&fdev->state_lock, flags);
239
240         if (fdev->last_oper_image == MLX5_FPGA_IMAGE_USER) {
241                 err = mlx5_fpga_ctrl_op(mdev, MLX5_FPGA_CTRL_OPERATION_SANDBOX_BYPASS_ON);
242                 if (err)
243                         mlx5_fpga_err(fdev, "Failed to re-set SBU bypass on: %d\n",
244                                       err);
245         }
246
247         mlx5_fpga_conn_device_cleanup(fdev);
248         max_num_qps = MLX5_CAP_FPGA(mdev, shell_caps.max_num_qps);
249         mlx5_core_unreserve_gids(mdev, max_num_qps);
250 }
251
252 void mlx5_fpga_cleanup(struct mlx5_core_dev *mdev)
253 {
254         struct mlx5_fpga_device *fdev = mdev->fpga;
255
256         mlx5_fpga_device_stop(mdev);
257         kfree(fdev);
258         mdev->fpga = NULL;
259 }
260
261 static const char *mlx5_fpga_syndrome_to_string(u8 syndrome)
262 {
263         if (syndrome < ARRAY_SIZE(mlx5_fpga_error_strings))
264                 return mlx5_fpga_error_strings[syndrome];
265         return "Unknown";
266 }
267
268 void mlx5_fpga_event(struct mlx5_core_dev *mdev, u8 event, void *data)
269 {
270         struct mlx5_fpga_device *fdev = mdev->fpga;
271         const char *event_name;
272         bool teardown = false;
273         unsigned long flags;
274         u8 syndrome;
275
276         if (event != MLX5_EVENT_TYPE_FPGA_ERROR) {
277                 mlx5_fpga_warn_ratelimited(fdev, "Unexpected event %u\n",
278                                            event);
279                 return;
280         }
281
282         syndrome = MLX5_GET(fpga_error_event, data, syndrome);
283         event_name = mlx5_fpga_syndrome_to_string(syndrome);
284
285         spin_lock_irqsave(&fdev->state_lock, flags);
286         switch (fdev->state) {
287         case MLX5_FPGA_STATUS_SUCCESS:
288                 mlx5_fpga_warn(fdev, "Error %u: %s\n", syndrome, event_name);
289                 teardown = true;
290                 break;
291         default:
292                 mlx5_fpga_warn_ratelimited(fdev, "Unexpected error event %u: %s\n",
293                                            syndrome, event_name);
294         }
295         spin_unlock_irqrestore(&fdev->state_lock, flags);
296         /* We tear-down the card's interfaces and functionality because
297          * the FPGA bump-on-the-wire is misbehaving and we lose ability
298          * to communicate with the network. User may still be able to
299          * recover by re-programming or debugging the FPGA
300          */
301         if (teardown)
302                 mlx5_trigger_health_work(fdev->mdev);
303 }