f9b950e1bd85add4bc20551fda0b4869db8e06ee
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / main.c
1 /*
2  * Copyright (c) 2013-2015, 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/highmem.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #include <linux/io-mapping.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/mlx5/driver.h>
44 #include <linux/mlx5/cq.h>
45 #include <linux/mlx5/qp.h>
46 #include <linux/mlx5/srq.h>
47 #include <linux/debugfs.h>
48 #include <linux/kmod.h>
49 #include <linux/mlx5/mlx5_ifc.h>
50 #include <linux/mlx5/vport.h>
51 #ifdef CONFIG_RFS_ACCEL
52 #include <linux/cpu_rmap.h>
53 #endif
54 #include <net/devlink.h>
55 #include "mlx5_core.h"
56 #include "fs_core.h"
57 #include "lib/mpfs.h"
58 #include "eswitch.h"
59 #include "lib/mlx5.h"
60 #include "fpga/core.h"
61 #include "fpga/ipsec.h"
62 #include "accel/ipsec.h"
63 #include "accel/tls.h"
64 #include "lib/clock.h"
65
66 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
67 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) core driver");
68 MODULE_LICENSE("Dual BSD/GPL");
69 MODULE_VERSION(DRIVER_VERSION);
70
71 unsigned int mlx5_core_debug_mask;
72 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
73 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
74
75 #define MLX5_DEFAULT_PROF       2
76 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
77 module_param_named(prof_sel, prof_sel, uint, 0444);
78 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
79
80 static u32 sw_owner_id[4];
81
82 enum {
83         MLX5_ATOMIC_REQ_MODE_BE = 0x0,
84         MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
85 };
86
87 static struct mlx5_profile profile[] = {
88         [0] = {
89                 .mask           = 0,
90         },
91         [1] = {
92                 .mask           = MLX5_PROF_MASK_QP_SIZE,
93                 .log_max_qp     = 12,
94         },
95         [2] = {
96                 .mask           = MLX5_PROF_MASK_QP_SIZE |
97                                   MLX5_PROF_MASK_MR_CACHE,
98                 .log_max_qp     = 18,
99                 .mr_cache[0]    = {
100                         .size   = 500,
101                         .limit  = 250
102                 },
103                 .mr_cache[1]    = {
104                         .size   = 500,
105                         .limit  = 250
106                 },
107                 .mr_cache[2]    = {
108                         .size   = 500,
109                         .limit  = 250
110                 },
111                 .mr_cache[3]    = {
112                         .size   = 500,
113                         .limit  = 250
114                 },
115                 .mr_cache[4]    = {
116                         .size   = 500,
117                         .limit  = 250
118                 },
119                 .mr_cache[5]    = {
120                         .size   = 500,
121                         .limit  = 250
122                 },
123                 .mr_cache[6]    = {
124                         .size   = 500,
125                         .limit  = 250
126                 },
127                 .mr_cache[7]    = {
128                         .size   = 500,
129                         .limit  = 250
130                 },
131                 .mr_cache[8]    = {
132                         .size   = 500,
133                         .limit  = 250
134                 },
135                 .mr_cache[9]    = {
136                         .size   = 500,
137                         .limit  = 250
138                 },
139                 .mr_cache[10]   = {
140                         .size   = 500,
141                         .limit  = 250
142                 },
143                 .mr_cache[11]   = {
144                         .size   = 500,
145                         .limit  = 250
146                 },
147                 .mr_cache[12]   = {
148                         .size   = 64,
149                         .limit  = 32
150                 },
151                 .mr_cache[13]   = {
152                         .size   = 32,
153                         .limit  = 16
154                 },
155                 .mr_cache[14]   = {
156                         .size   = 16,
157                         .limit  = 8
158                 },
159                 .mr_cache[15]   = {
160                         .size   = 8,
161                         .limit  = 4
162                 },
163                 .mr_cache[16]   = {
164                         .size   = 8,
165                         .limit  = 4
166                 },
167                 .mr_cache[17]   = {
168                         .size   = 8,
169                         .limit  = 4
170                 },
171                 .mr_cache[18]   = {
172                         .size   = 8,
173                         .limit  = 4
174                 },
175                 .mr_cache[19]   = {
176                         .size   = 4,
177                         .limit  = 2
178                 },
179                 .mr_cache[20]   = {
180                         .size   = 4,
181                         .limit  = 2
182                 },
183         },
184 };
185
186 #define FW_INIT_TIMEOUT_MILI            2000
187 #define FW_INIT_WAIT_MS                 2
188 #define FW_PRE_INIT_TIMEOUT_MILI        10000
189
190 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
191 {
192         unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
193         int err = 0;
194
195         while (fw_initializing(dev)) {
196                 if (time_after(jiffies, end)) {
197                         err = -EBUSY;
198                         break;
199                 }
200                 msleep(FW_INIT_WAIT_MS);
201         }
202
203         return err;
204 }
205
206 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
207 {
208         int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
209                                               driver_version);
210         u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {0};
211         u8 out[MLX5_ST_SZ_BYTES(set_driver_version_out)] = {0};
212         int remaining_size = driver_ver_sz;
213         char *string;
214
215         if (!MLX5_CAP_GEN(dev, driver_version))
216                 return;
217
218         string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
219
220         strncpy(string, "Linux", remaining_size);
221
222         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
223         strncat(string, ",", remaining_size);
224
225         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
226         strncat(string, DRIVER_NAME, remaining_size);
227
228         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
229         strncat(string, ",", remaining_size);
230
231         remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
232         strncat(string, DRIVER_VERSION, remaining_size);
233
234         /*Send the command*/
235         MLX5_SET(set_driver_version_in, in, opcode,
236                  MLX5_CMD_OP_SET_DRIVER_VERSION);
237
238         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
239 }
240
241 static int set_dma_caps(struct pci_dev *pdev)
242 {
243         int err;
244
245         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
246         if (err) {
247                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
248                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
249                 if (err) {
250                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
251                         return err;
252                 }
253         }
254
255         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
256         if (err) {
257                 dev_warn(&pdev->dev,
258                          "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
259                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
260                 if (err) {
261                         dev_err(&pdev->dev,
262                                 "Can't set consistent PCI DMA mask, aborting\n");
263                         return err;
264                 }
265         }
266
267         dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
268         return err;
269 }
270
271 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
272 {
273         struct pci_dev *pdev = dev->pdev;
274         int err = 0;
275
276         mutex_lock(&dev->pci_status_mutex);
277         if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
278                 err = pci_enable_device(pdev);
279                 if (!err)
280                         dev->pci_status = MLX5_PCI_STATUS_ENABLED;
281         }
282         mutex_unlock(&dev->pci_status_mutex);
283
284         return err;
285 }
286
287 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
288 {
289         struct pci_dev *pdev = dev->pdev;
290
291         mutex_lock(&dev->pci_status_mutex);
292         if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
293                 pci_disable_device(pdev);
294                 dev->pci_status = MLX5_PCI_STATUS_DISABLED;
295         }
296         mutex_unlock(&dev->pci_status_mutex);
297 }
298
299 static int request_bar(struct pci_dev *pdev)
300 {
301         int err = 0;
302
303         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
304                 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
305                 return -ENODEV;
306         }
307
308         err = pci_request_regions(pdev, DRIVER_NAME);
309         if (err)
310                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
311
312         return err;
313 }
314
315 static void release_bar(struct pci_dev *pdev)
316 {
317         pci_release_regions(pdev);
318 }
319
320 static int mlx5_alloc_irq_vectors(struct mlx5_core_dev *dev)
321 {
322         struct mlx5_priv *priv = &dev->priv;
323         struct mlx5_eq_table *table = &priv->eq_table;
324         int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
325         int nvec;
326         int err;
327
328         nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
329                MLX5_EQ_VEC_COMP_BASE;
330         nvec = min_t(int, nvec, num_eqs);
331         if (nvec <= MLX5_EQ_VEC_COMP_BASE)
332                 return -ENOMEM;
333
334         priv->irq_info = kcalloc(nvec, sizeof(*priv->irq_info), GFP_KERNEL);
335         if (!priv->irq_info)
336                 return -ENOMEM;
337
338         nvec = pci_alloc_irq_vectors(dev->pdev,
339                         MLX5_EQ_VEC_COMP_BASE + 1, nvec,
340                         PCI_IRQ_MSIX);
341         if (nvec < 0) {
342                 err = nvec;
343                 goto err_free_irq_info;
344         }
345
346         table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
347
348         return 0;
349
350 err_free_irq_info:
351         kfree(priv->irq_info);
352         return err;
353 }
354
355 static void mlx5_free_irq_vectors(struct mlx5_core_dev *dev)
356 {
357         struct mlx5_priv *priv = &dev->priv;
358
359         pci_free_irq_vectors(dev->pdev);
360         kfree(priv->irq_info);
361 }
362
363 struct mlx5_reg_host_endianness {
364         u8      he;
365         u8      rsvd[15];
366 };
367
368 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
369
370 enum {
371         MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
372                                 MLX5_DEV_CAP_FLAG_DCT,
373 };
374
375 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
376 {
377         switch (size) {
378         case 128:
379                 return 0;
380         case 256:
381                 return 1;
382         case 512:
383                 return 2;
384         case 1024:
385                 return 3;
386         case 2048:
387                 return 4;
388         case 4096:
389                 return 5;
390         default:
391                 mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
392                 return 0;
393         }
394 }
395
396 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
397                                    enum mlx5_cap_type cap_type,
398                                    enum mlx5_cap_mode cap_mode)
399 {
400         u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
401         int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
402         void *out, *hca_caps;
403         u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
404         int err;
405
406         memset(in, 0, sizeof(in));
407         out = kzalloc(out_sz, GFP_KERNEL);
408         if (!out)
409                 return -ENOMEM;
410
411         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
412         MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
413         err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
414         if (err) {
415                 mlx5_core_warn(dev,
416                                "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
417                                cap_type, cap_mode, err);
418                 goto query_ex;
419         }
420
421         hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
422
423         switch (cap_mode) {
424         case HCA_CAP_OPMOD_GET_MAX:
425                 memcpy(dev->caps.hca_max[cap_type], hca_caps,
426                        MLX5_UN_SZ_BYTES(hca_cap_union));
427                 break;
428         case HCA_CAP_OPMOD_GET_CUR:
429                 memcpy(dev->caps.hca_cur[cap_type], hca_caps,
430                        MLX5_UN_SZ_BYTES(hca_cap_union));
431                 break;
432         default:
433                 mlx5_core_warn(dev,
434                                "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
435                                cap_type, cap_mode);
436                 err = -EINVAL;
437                 break;
438         }
439 query_ex:
440         kfree(out);
441         return err;
442 }
443
444 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
445 {
446         int ret;
447
448         ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
449         if (ret)
450                 return ret;
451         return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
452 }
453
454 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz, int opmod)
455 {
456         u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)] = {0};
457
458         MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
459         MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
460         return mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
461 }
462
463 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev)
464 {
465         void *set_ctx;
466         void *set_hca_cap;
467         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
468         int req_endianness;
469         int err;
470
471         if (MLX5_CAP_GEN(dev, atomic)) {
472                 err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
473                 if (err)
474                         return err;
475         } else {
476                 return 0;
477         }
478
479         req_endianness =
480                 MLX5_CAP_ATOMIC(dev,
481                                 supported_atomic_req_8B_endianness_mode_1);
482
483         if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
484                 return 0;
485
486         set_ctx = kzalloc(set_sz, GFP_KERNEL);
487         if (!set_ctx)
488                 return -ENOMEM;
489
490         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
491
492         /* Set requestor to host endianness */
493         MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
494                  MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
495
496         err = set_caps(dev, set_ctx, set_sz, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
497
498         kfree(set_ctx);
499         return err;
500 }
501
502 static int handle_hca_cap(struct mlx5_core_dev *dev)
503 {
504         void *set_ctx = NULL;
505         struct mlx5_profile *prof = dev->profile;
506         int err = -ENOMEM;
507         int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
508         void *set_hca_cap;
509
510         set_ctx = kzalloc(set_sz, GFP_KERNEL);
511         if (!set_ctx)
512                 goto query_ex;
513
514         err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
515         if (err)
516                 goto query_ex;
517
518         set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
519                                    capability);
520         memcpy(set_hca_cap, dev->caps.hca_cur[MLX5_CAP_GENERAL],
521                MLX5_ST_SZ_BYTES(cmd_hca_cap));
522
523         mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
524                       mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
525                       128);
526         /* we limit the size of the pkey table to 128 entries for now */
527         MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
528                  to_fw_pkey_sz(dev, 128));
529
530         /* Check log_max_qp from HCA caps to set in current profile */
531         if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < profile[prof_sel].log_max_qp) {
532                 mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
533                                profile[prof_sel].log_max_qp,
534                                MLX5_CAP_GEN_MAX(dev, log_max_qp));
535                 profile[prof_sel].log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
536         }
537         if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
538                 MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
539                          prof->log_max_qp);
540
541         /* disable cmdif checksum */
542         MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
543
544         /* Enable 4K UAR only when HCA supports it and page size is bigger
545          * than 4K.
546          */
547         if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
548                 MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
549
550         MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
551
552         if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
553                 MLX5_SET(cmd_hca_cap,
554                          set_hca_cap,
555                          cache_line_128byte,
556                          cache_line_size() >= 128 ? 1 : 0);
557
558         if (MLX5_CAP_GEN_MAX(dev, dct))
559                 MLX5_SET(cmd_hca_cap, set_hca_cap, dct, 1);
560
561         if (MLX5_CAP_GEN_MAX(dev, num_vhca_ports))
562                 MLX5_SET(cmd_hca_cap,
563                          set_hca_cap,
564                          num_vhca_ports,
565                          MLX5_CAP_GEN_MAX(dev, num_vhca_ports));
566
567         err = set_caps(dev, set_ctx, set_sz,
568                        MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
569
570 query_ex:
571         kfree(set_ctx);
572         return err;
573 }
574
575 static int set_hca_ctrl(struct mlx5_core_dev *dev)
576 {
577         struct mlx5_reg_host_endianness he_in;
578         struct mlx5_reg_host_endianness he_out;
579         int err;
580
581         if (!mlx5_core_is_pf(dev))
582                 return 0;
583
584         memset(&he_in, 0, sizeof(he_in));
585         he_in.he = MLX5_SET_HOST_ENDIANNESS;
586         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
587                                         &he_out, sizeof(he_out),
588                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
589         return err;
590 }
591
592 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
593 {
594         int ret = 0;
595
596         /* Disable local_lb by default */
597         if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
598                 ret = mlx5_nic_vport_update_local_lb(dev, false);
599
600         return ret;
601 }
602
603 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
604 {
605         u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {0};
606         u32 in[MLX5_ST_SZ_DW(enable_hca_in)]   = {0};
607
608         MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
609         MLX5_SET(enable_hca_in, in, function_id, func_id);
610         return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
611 }
612
613 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
614 {
615         u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {0};
616         u32 in[MLX5_ST_SZ_DW(disable_hca_in)]   = {0};
617
618         MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
619         MLX5_SET(disable_hca_in, in, function_id, func_id);
620         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
621 }
622
623 u64 mlx5_read_internal_timer(struct mlx5_core_dev *dev)
624 {
625         u32 timer_h, timer_h1, timer_l;
626
627         timer_h = ioread32be(&dev->iseg->internal_timer_h);
628         timer_l = ioread32be(&dev->iseg->internal_timer_l);
629         timer_h1 = ioread32be(&dev->iseg->internal_timer_h);
630         if (timer_h != timer_h1) /* wrap around */
631                 timer_l = ioread32be(&dev->iseg->internal_timer_l);
632
633         return (u64)timer_l | (u64)timer_h1 << 32;
634 }
635
636 static int mlx5_irq_set_affinity_hint(struct mlx5_core_dev *mdev, int i)
637 {
638         struct mlx5_priv *priv  = &mdev->priv;
639         int irq = pci_irq_vector(mdev->pdev, MLX5_EQ_VEC_COMP_BASE + i);
640
641         if (!zalloc_cpumask_var(&priv->irq_info[i].mask, GFP_KERNEL)) {
642                 mlx5_core_warn(mdev, "zalloc_cpumask_var failed");
643                 return -ENOMEM;
644         }
645
646         cpumask_set_cpu(cpumask_local_spread(i, priv->numa_node),
647                         priv->irq_info[i].mask);
648
649         if (IS_ENABLED(CONFIG_SMP) &&
650             irq_set_affinity_hint(irq, priv->irq_info[i].mask))
651                 mlx5_core_warn(mdev, "irq_set_affinity_hint failed, irq 0x%.4x", irq);
652
653         return 0;
654 }
655
656 static void mlx5_irq_clear_affinity_hint(struct mlx5_core_dev *mdev, int i)
657 {
658         struct mlx5_priv *priv  = &mdev->priv;
659         int irq = pci_irq_vector(mdev->pdev, MLX5_EQ_VEC_COMP_BASE + i);
660
661         irq_set_affinity_hint(irq, NULL);
662         free_cpumask_var(priv->irq_info[i].mask);
663 }
664
665 static int mlx5_irq_set_affinity_hints(struct mlx5_core_dev *mdev)
666 {
667         int err;
668         int i;
669
670         for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++) {
671                 err = mlx5_irq_set_affinity_hint(mdev, i);
672                 if (err)
673                         goto err_out;
674         }
675
676         return 0;
677
678 err_out:
679         for (i--; i >= 0; i--)
680                 mlx5_irq_clear_affinity_hint(mdev, i);
681
682         return err;
683 }
684
685 static void mlx5_irq_clear_affinity_hints(struct mlx5_core_dev *mdev)
686 {
687         int i;
688
689         for (i = 0; i < mdev->priv.eq_table.num_comp_vectors; i++)
690                 mlx5_irq_clear_affinity_hint(mdev, i);
691 }
692
693 int mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn,
694                     unsigned int *irqn)
695 {
696         struct mlx5_eq_table *table = &dev->priv.eq_table;
697         struct mlx5_eq *eq, *n;
698         int err = -ENOENT;
699
700         spin_lock(&table->lock);
701         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
702                 if (eq->index == vector) {
703                         *eqn = eq->eqn;
704                         *irqn = eq->irqn;
705                         err = 0;
706                         break;
707                 }
708         }
709         spin_unlock(&table->lock);
710
711         return err;
712 }
713 EXPORT_SYMBOL(mlx5_vector2eqn);
714
715 struct mlx5_eq *mlx5_eqn2eq(struct mlx5_core_dev *dev, int eqn)
716 {
717         struct mlx5_eq_table *table = &dev->priv.eq_table;
718         struct mlx5_eq *eq;
719
720         spin_lock(&table->lock);
721         list_for_each_entry(eq, &table->comp_eqs_list, list)
722                 if (eq->eqn == eqn) {
723                         spin_unlock(&table->lock);
724                         return eq;
725                 }
726
727         spin_unlock(&table->lock);
728
729         return ERR_PTR(-ENOENT);
730 }
731
732 static void free_comp_eqs(struct mlx5_core_dev *dev)
733 {
734         struct mlx5_eq_table *table = &dev->priv.eq_table;
735         struct mlx5_eq *eq, *n;
736
737 #ifdef CONFIG_RFS_ACCEL
738         if (dev->rmap) {
739                 free_irq_cpu_rmap(dev->rmap);
740                 dev->rmap = NULL;
741         }
742 #endif
743         spin_lock(&table->lock);
744         list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
745                 list_del(&eq->list);
746                 spin_unlock(&table->lock);
747                 if (mlx5_destroy_unmap_eq(dev, eq))
748                         mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
749                                        eq->eqn);
750                 kfree(eq);
751                 spin_lock(&table->lock);
752         }
753         spin_unlock(&table->lock);
754 }
755
756 static int alloc_comp_eqs(struct mlx5_core_dev *dev)
757 {
758         struct mlx5_eq_table *table = &dev->priv.eq_table;
759         char name[MLX5_MAX_IRQ_NAME];
760         struct mlx5_eq *eq;
761         int ncomp_vec;
762         int nent;
763         int err;
764         int i;
765
766         INIT_LIST_HEAD(&table->comp_eqs_list);
767         ncomp_vec = table->num_comp_vectors;
768         nent = MLX5_COMP_EQ_SIZE;
769 #ifdef CONFIG_RFS_ACCEL
770         dev->rmap = alloc_irq_cpu_rmap(ncomp_vec);
771         if (!dev->rmap)
772                 return -ENOMEM;
773 #endif
774         for (i = 0; i < ncomp_vec; i++) {
775                 eq = kzalloc(sizeof(*eq), GFP_KERNEL);
776                 if (!eq) {
777                         err = -ENOMEM;
778                         goto clean;
779                 }
780
781 #ifdef CONFIG_RFS_ACCEL
782                 irq_cpu_rmap_add(dev->rmap, pci_irq_vector(dev->pdev,
783                                  MLX5_EQ_VEC_COMP_BASE + i));
784 #endif
785                 snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
786                 err = mlx5_create_map_eq(dev, eq,
787                                          i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
788                                          name, MLX5_EQ_TYPE_COMP);
789                 if (err) {
790                         kfree(eq);
791                         goto clean;
792                 }
793                 mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
794                 eq->index = i;
795                 spin_lock(&table->lock);
796                 list_add_tail(&eq->list, &table->comp_eqs_list);
797                 spin_unlock(&table->lock);
798         }
799
800         return 0;
801
802 clean:
803         free_comp_eqs(dev);
804         return err;
805 }
806
807 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
808 {
809         u32 query_in[MLX5_ST_SZ_DW(query_issi_in)]   = {0};
810         u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {0};
811         u32 sup_issi;
812         int err;
813
814         MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
815         err = mlx5_cmd_exec(dev, query_in, sizeof(query_in),
816                             query_out, sizeof(query_out));
817         if (err) {
818                 u32 syndrome;
819                 u8 status;
820
821                 mlx5_cmd_mbox_status(query_out, &status, &syndrome);
822                 if (!status || syndrome == MLX5_DRIVER_SYND) {
823                         mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
824                                       err, status, syndrome);
825                         return err;
826                 }
827
828                 mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
829                 dev->issi = 0;
830                 return 0;
831         }
832
833         sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
834
835         if (sup_issi & (1 << 1)) {
836                 u32 set_in[MLX5_ST_SZ_DW(set_issi_in)]   = {0};
837                 u32 set_out[MLX5_ST_SZ_DW(set_issi_out)] = {0};
838
839                 MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
840                 MLX5_SET(set_issi_in, set_in, current_issi, 1);
841                 err = mlx5_cmd_exec(dev, set_in, sizeof(set_in),
842                                     set_out, sizeof(set_out));
843                 if (err) {
844                         mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
845                                       err);
846                         return err;
847                 }
848
849                 dev->issi = 1;
850
851                 return 0;
852         } else if (sup_issi & (1 << 0) || !sup_issi) {
853                 return 0;
854         }
855
856         return -EOPNOTSUPP;
857 }
858
859 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
860 {
861         struct pci_dev *pdev = dev->pdev;
862         int err = 0;
863
864         pci_set_drvdata(dev->pdev, dev);
865         strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
866         priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
867
868         mutex_init(&priv->pgdir_mutex);
869         INIT_LIST_HEAD(&priv->pgdir_list);
870         spin_lock_init(&priv->mkey_lock);
871
872         mutex_init(&priv->alloc_mutex);
873
874         priv->numa_node = dev_to_node(&dev->pdev->dev);
875
876         priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
877         if (!priv->dbg_root)
878                 return -ENOMEM;
879
880         err = mlx5_pci_enable_device(dev);
881         if (err) {
882                 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
883                 goto err_dbg;
884         }
885
886         err = request_bar(pdev);
887         if (err) {
888                 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
889                 goto err_disable;
890         }
891
892         pci_set_master(pdev);
893
894         err = set_dma_caps(pdev);
895         if (err) {
896                 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
897                 goto err_clr_master;
898         }
899
900         dev->iseg_base = pci_resource_start(dev->pdev, 0);
901         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
902         if (!dev->iseg) {
903                 err = -ENOMEM;
904                 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
905                 goto err_clr_master;
906         }
907
908         return 0;
909
910 err_clr_master:
911         pci_clear_master(dev->pdev);
912         release_bar(dev->pdev);
913 err_disable:
914         mlx5_pci_disable_device(dev);
915
916 err_dbg:
917         debugfs_remove(priv->dbg_root);
918         return err;
919 }
920
921 static void mlx5_pci_close(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
922 {
923         iounmap(dev->iseg);
924         pci_clear_master(dev->pdev);
925         release_bar(dev->pdev);
926         mlx5_pci_disable_device(dev);
927         debugfs_remove(priv->dbg_root);
928 }
929
930 static int mlx5_init_once(struct mlx5_core_dev *dev, struct mlx5_priv *priv)
931 {
932         struct pci_dev *pdev = dev->pdev;
933         int err;
934
935         err = mlx5_query_board_id(dev);
936         if (err) {
937                 dev_err(&pdev->dev, "query board id failed\n");
938                 goto out;
939         }
940
941         err = mlx5_eq_init(dev);
942         if (err) {
943                 dev_err(&pdev->dev, "failed to initialize eq\n");
944                 goto out;
945         }
946
947         err = mlx5_cq_debugfs_init(dev);
948         if (err) {
949                 dev_err(&pdev->dev, "failed to initialize cq debugfs\n");
950                 goto err_eq_cleanup;
951         }
952
953         mlx5_init_qp_table(dev);
954
955         mlx5_init_srq_table(dev);
956
957         mlx5_init_mkey_table(dev);
958
959         mlx5_init_reserved_gids(dev);
960
961         mlx5_init_clock(dev);
962
963         err = mlx5_init_rl_table(dev);
964         if (err) {
965                 dev_err(&pdev->dev, "Failed to init rate limiting\n");
966                 goto err_tables_cleanup;
967         }
968
969         err = mlx5_mpfs_init(dev);
970         if (err) {
971                 dev_err(&pdev->dev, "Failed to init l2 table %d\n", err);
972                 goto err_rl_cleanup;
973         }
974
975         err = mlx5_eswitch_init(dev);
976         if (err) {
977                 dev_err(&pdev->dev, "Failed to init eswitch %d\n", err);
978                 goto err_mpfs_cleanup;
979         }
980
981         err = mlx5_sriov_init(dev);
982         if (err) {
983                 dev_err(&pdev->dev, "Failed to init sriov %d\n", err);
984                 goto err_eswitch_cleanup;
985         }
986
987         err = mlx5_fpga_init(dev);
988         if (err) {
989                 dev_err(&pdev->dev, "Failed to init fpga device %d\n", err);
990                 goto err_sriov_cleanup;
991         }
992
993         return 0;
994
995 err_sriov_cleanup:
996         mlx5_sriov_cleanup(dev);
997 err_eswitch_cleanup:
998         mlx5_eswitch_cleanup(dev->priv.eswitch);
999 err_mpfs_cleanup:
1000         mlx5_mpfs_cleanup(dev);
1001 err_rl_cleanup:
1002         mlx5_cleanup_rl_table(dev);
1003 err_tables_cleanup:
1004         mlx5_cleanup_mkey_table(dev);
1005         mlx5_cleanup_srq_table(dev);
1006         mlx5_cleanup_qp_table(dev);
1007         mlx5_cq_debugfs_cleanup(dev);
1008
1009 err_eq_cleanup:
1010         mlx5_eq_cleanup(dev);
1011
1012 out:
1013         return err;
1014 }
1015
1016 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
1017 {
1018         mlx5_fpga_cleanup(dev);
1019         mlx5_sriov_cleanup(dev);
1020         mlx5_eswitch_cleanup(dev->priv.eswitch);
1021         mlx5_mpfs_cleanup(dev);
1022         mlx5_cleanup_rl_table(dev);
1023         mlx5_cleanup_clock(dev);
1024         mlx5_cleanup_reserved_gids(dev);
1025         mlx5_cleanup_mkey_table(dev);
1026         mlx5_cleanup_srq_table(dev);
1027         mlx5_cleanup_qp_table(dev);
1028         mlx5_cq_debugfs_cleanup(dev);
1029         mlx5_eq_cleanup(dev);
1030 }
1031
1032 static int mlx5_load_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1033                          bool boot)
1034 {
1035         struct pci_dev *pdev = dev->pdev;
1036         int err;
1037
1038         mutex_lock(&dev->intf_state_mutex);
1039         if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1040                 dev_warn(&dev->pdev->dev, "%s: interface is up, NOP\n",
1041                          __func__);
1042                 goto out;
1043         }
1044
1045         dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
1046                  fw_rev_min(dev), fw_rev_sub(dev));
1047
1048         /* Only PFs hold the relevant PCIe information for this query */
1049         if (mlx5_core_is_pf(dev))
1050                 pcie_print_link_status(dev->pdev);
1051
1052         /* on load removing any previous indication of internal error, device is
1053          * up
1054          */
1055         dev->state = MLX5_DEVICE_STATE_UP;
1056
1057         /* wait for firmware to accept initialization segments configurations
1058          */
1059         err = wait_fw_init(dev, FW_PRE_INIT_TIMEOUT_MILI);
1060         if (err) {
1061                 dev_err(&dev->pdev->dev, "Firmware over %d MS in pre-initializing state, aborting\n",
1062                         FW_PRE_INIT_TIMEOUT_MILI);
1063                 goto out_err;
1064         }
1065
1066         err = mlx5_cmd_init(dev);
1067         if (err) {
1068                 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
1069                 goto out_err;
1070         }
1071
1072         err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
1073         if (err) {
1074                 dev_err(&dev->pdev->dev, "Firmware over %d MS in initializing state, aborting\n",
1075                         FW_INIT_TIMEOUT_MILI);
1076                 goto err_cmd_cleanup;
1077         }
1078
1079         err = mlx5_core_enable_hca(dev, 0);
1080         if (err) {
1081                 dev_err(&pdev->dev, "enable hca failed\n");
1082                 goto err_cmd_cleanup;
1083         }
1084
1085         err = mlx5_core_set_issi(dev);
1086         if (err) {
1087                 dev_err(&pdev->dev, "failed to set issi\n");
1088                 goto err_disable_hca;
1089         }
1090
1091         err = mlx5_satisfy_startup_pages(dev, 1);
1092         if (err) {
1093                 dev_err(&pdev->dev, "failed to allocate boot pages\n");
1094                 goto err_disable_hca;
1095         }
1096
1097         err = set_hca_ctrl(dev);
1098         if (err) {
1099                 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
1100                 goto reclaim_boot_pages;
1101         }
1102
1103         err = handle_hca_cap(dev);
1104         if (err) {
1105                 dev_err(&pdev->dev, "handle_hca_cap failed\n");
1106                 goto reclaim_boot_pages;
1107         }
1108
1109         err = handle_hca_cap_atomic(dev);
1110         if (err) {
1111                 dev_err(&pdev->dev, "handle_hca_cap_atomic failed\n");
1112                 goto reclaim_boot_pages;
1113         }
1114
1115         err = mlx5_satisfy_startup_pages(dev, 0);
1116         if (err) {
1117                 dev_err(&pdev->dev, "failed to allocate init pages\n");
1118                 goto reclaim_boot_pages;
1119         }
1120
1121         err = mlx5_pagealloc_start(dev);
1122         if (err) {
1123                 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
1124                 goto reclaim_boot_pages;
1125         }
1126
1127         err = mlx5_cmd_init_hca(dev, sw_owner_id);
1128         if (err) {
1129                 dev_err(&pdev->dev, "init hca failed\n");
1130                 goto err_pagealloc_stop;
1131         }
1132
1133         mlx5_set_driver_version(dev);
1134
1135         mlx5_start_health_poll(dev);
1136
1137         err = mlx5_query_hca_caps(dev);
1138         if (err) {
1139                 dev_err(&pdev->dev, "query hca failed\n");
1140                 goto err_stop_poll;
1141         }
1142
1143         if (boot) {
1144                 err = mlx5_init_once(dev, priv);
1145                 if (err) {
1146                         dev_err(&pdev->dev, "sw objs init failed\n");
1147                         goto err_stop_poll;
1148                 }
1149         }
1150
1151         err = mlx5_alloc_irq_vectors(dev);
1152         if (err) {
1153                 dev_err(&pdev->dev, "alloc irq vectors failed\n");
1154                 goto err_cleanup_once;
1155         }
1156
1157         dev->priv.uar = mlx5_get_uars_page(dev);
1158         if (IS_ERR(dev->priv.uar)) {
1159                 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
1160                 err = PTR_ERR(dev->priv.uar);
1161                 goto err_disable_msix;
1162         }
1163
1164         err = mlx5_start_eqs(dev);
1165         if (err) {
1166                 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
1167                 goto err_put_uars;
1168         }
1169
1170         err = alloc_comp_eqs(dev);
1171         if (err) {
1172                 dev_err(&pdev->dev, "Failed to alloc completion EQs\n");
1173                 goto err_stop_eqs;
1174         }
1175
1176         err = mlx5_irq_set_affinity_hints(dev);
1177         if (err) {
1178                 dev_err(&pdev->dev, "Failed to alloc affinity hint cpumask\n");
1179                 goto err_affinity_hints;
1180         }
1181
1182         err = mlx5_fpga_device_start(dev);
1183         if (err) {
1184                 dev_err(&pdev->dev, "fpga device start failed %d\n", err);
1185                 goto err_fpga_start;
1186         }
1187
1188         err = mlx5_accel_ipsec_init(dev);
1189         if (err) {
1190                 dev_err(&pdev->dev, "IPSec device start failed %d\n", err);
1191                 goto err_ipsec_start;
1192         }
1193
1194         err = mlx5_accel_tls_init(dev);
1195         if (err) {
1196                 dev_err(&pdev->dev, "TLS device start failed %d\n", err);
1197                 goto err_tls_start;
1198         }
1199
1200         err = mlx5_init_fs(dev);
1201         if (err) {
1202                 dev_err(&pdev->dev, "Failed to init flow steering\n");
1203                 goto err_fs;
1204         }
1205
1206         err = mlx5_core_set_hca_defaults(dev);
1207         if (err) {
1208                 dev_err(&pdev->dev, "Failed to set hca defaults\n");
1209                 goto err_fs;
1210         }
1211
1212         err = mlx5_sriov_attach(dev);
1213         if (err) {
1214                 dev_err(&pdev->dev, "sriov init failed %d\n", err);
1215                 goto err_sriov;
1216         }
1217
1218         if (mlx5_device_registered(dev)) {
1219                 mlx5_attach_device(dev);
1220         } else {
1221                 err = mlx5_register_device(dev);
1222                 if (err) {
1223                         dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
1224                         goto err_reg_dev;
1225                 }
1226         }
1227
1228         set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1229 out:
1230         mutex_unlock(&dev->intf_state_mutex);
1231
1232         return 0;
1233
1234 err_reg_dev:
1235         mlx5_sriov_detach(dev);
1236
1237 err_sriov:
1238         mlx5_cleanup_fs(dev);
1239
1240 err_fs:
1241         mlx5_accel_tls_cleanup(dev);
1242
1243 err_tls_start:
1244         mlx5_accel_ipsec_cleanup(dev);
1245
1246 err_ipsec_start:
1247         mlx5_fpga_device_stop(dev);
1248
1249 err_fpga_start:
1250         mlx5_irq_clear_affinity_hints(dev);
1251
1252 err_affinity_hints:
1253         free_comp_eqs(dev);
1254
1255 err_stop_eqs:
1256         mlx5_stop_eqs(dev);
1257
1258 err_put_uars:
1259         mlx5_put_uars_page(dev, priv->uar);
1260
1261 err_disable_msix:
1262         mlx5_free_irq_vectors(dev);
1263
1264 err_cleanup_once:
1265         if (boot)
1266                 mlx5_cleanup_once(dev);
1267
1268 err_stop_poll:
1269         mlx5_stop_health_poll(dev);
1270         if (mlx5_cmd_teardown_hca(dev)) {
1271                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1272                 goto out_err;
1273         }
1274
1275 err_pagealloc_stop:
1276         mlx5_pagealloc_stop(dev);
1277
1278 reclaim_boot_pages:
1279         mlx5_reclaim_startup_pages(dev);
1280
1281 err_disable_hca:
1282         mlx5_core_disable_hca(dev, 0);
1283
1284 err_cmd_cleanup:
1285         mlx5_cmd_cleanup(dev);
1286
1287 out_err:
1288         dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1289         mutex_unlock(&dev->intf_state_mutex);
1290
1291         return err;
1292 }
1293
1294 static int mlx5_unload_one(struct mlx5_core_dev *dev, struct mlx5_priv *priv,
1295                            bool cleanup)
1296 {
1297         int err = 0;
1298
1299         if (cleanup)
1300                 mlx5_drain_health_recovery(dev);
1301
1302         mutex_lock(&dev->intf_state_mutex);
1303         if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1304                 dev_warn(&dev->pdev->dev, "%s: interface is down, NOP\n",
1305                          __func__);
1306                 if (cleanup)
1307                         mlx5_cleanup_once(dev);
1308                 goto out;
1309         }
1310
1311         clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1312
1313         if (mlx5_device_registered(dev))
1314                 mlx5_detach_device(dev);
1315
1316         mlx5_sriov_detach(dev);
1317         mlx5_cleanup_fs(dev);
1318         mlx5_accel_ipsec_cleanup(dev);
1319         mlx5_accel_tls_cleanup(dev);
1320         mlx5_fpga_device_stop(dev);
1321         mlx5_irq_clear_affinity_hints(dev);
1322         free_comp_eqs(dev);
1323         mlx5_stop_eqs(dev);
1324         mlx5_put_uars_page(dev, priv->uar);
1325         mlx5_free_irq_vectors(dev);
1326         if (cleanup)
1327                 mlx5_cleanup_once(dev);
1328         mlx5_stop_health_poll(dev);
1329         err = mlx5_cmd_teardown_hca(dev);
1330         if (err) {
1331                 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
1332                 goto out;
1333         }
1334         mlx5_pagealloc_stop(dev);
1335         mlx5_reclaim_startup_pages(dev);
1336         mlx5_core_disable_hca(dev, 0);
1337         mlx5_cmd_cleanup(dev);
1338
1339 out:
1340         mutex_unlock(&dev->intf_state_mutex);
1341         return err;
1342 }
1343
1344 struct mlx5_core_event_handler {
1345         void (*event)(struct mlx5_core_dev *dev,
1346                       enum mlx5_dev_event event,
1347                       void *data);
1348 };
1349
1350 static const struct devlink_ops mlx5_devlink_ops = {
1351 #ifdef CONFIG_MLX5_ESWITCH
1352         .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
1353         .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
1354         .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
1355         .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
1356         .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
1357         .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
1358 #endif
1359 };
1360
1361 #define MLX5_IB_MOD "mlx5_ib"
1362 static int init_one(struct pci_dev *pdev,
1363                     const struct pci_device_id *id)
1364 {
1365         struct mlx5_core_dev *dev;
1366         struct devlink *devlink;
1367         struct mlx5_priv *priv;
1368         int err;
1369
1370         devlink = devlink_alloc(&mlx5_devlink_ops, sizeof(*dev));
1371         if (!devlink) {
1372                 dev_err(&pdev->dev, "kzalloc failed\n");
1373                 return -ENOMEM;
1374         }
1375
1376         dev = devlink_priv(devlink);
1377         priv = &dev->priv;
1378         priv->pci_dev_data = id->driver_data;
1379
1380         pci_set_drvdata(pdev, dev);
1381
1382         dev->pdev = pdev;
1383         dev->event = mlx5_core_event;
1384         dev->profile = &profile[prof_sel];
1385
1386         INIT_LIST_HEAD(&priv->ctx_list);
1387         spin_lock_init(&priv->ctx_lock);
1388         mutex_init(&dev->pci_status_mutex);
1389         mutex_init(&dev->intf_state_mutex);
1390
1391         INIT_LIST_HEAD(&priv->waiting_events_list);
1392         priv->is_accum_events = false;
1393
1394 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1395         err = init_srcu_struct(&priv->pfault_srcu);
1396         if (err) {
1397                 dev_err(&pdev->dev, "init_srcu_struct failed with error code %d\n",
1398                         err);
1399                 goto clean_dev;
1400         }
1401 #endif
1402         mutex_init(&priv->bfregs.reg_head.lock);
1403         mutex_init(&priv->bfregs.wc_head.lock);
1404         INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1405         INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1406
1407         err = mlx5_pci_init(dev, priv);
1408         if (err) {
1409                 dev_err(&pdev->dev, "mlx5_pci_init failed with error code %d\n", err);
1410                 goto clean_srcu;
1411         }
1412
1413         err = mlx5_health_init(dev);
1414         if (err) {
1415                 dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
1416                 goto close_pci;
1417         }
1418
1419         mlx5_pagealloc_init(dev);
1420
1421         err = mlx5_load_one(dev, priv, true);
1422         if (err) {
1423                 dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
1424                 goto clean_health;
1425         }
1426
1427         request_module_nowait(MLX5_IB_MOD);
1428
1429         err = devlink_register(devlink, &pdev->dev);
1430         if (err)
1431                 goto clean_load;
1432
1433         pci_save_state(pdev);
1434         return 0;
1435
1436 clean_load:
1437         mlx5_unload_one(dev, priv, true);
1438 clean_health:
1439         mlx5_pagealloc_cleanup(dev);
1440         mlx5_health_cleanup(dev);
1441 close_pci:
1442         mlx5_pci_close(dev, priv);
1443 clean_srcu:
1444 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1445         cleanup_srcu_struct(&priv->pfault_srcu);
1446 clean_dev:
1447 #endif
1448         devlink_free(devlink);
1449
1450         return err;
1451 }
1452
1453 static void remove_one(struct pci_dev *pdev)
1454 {
1455         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1456         struct devlink *devlink = priv_to_devlink(dev);
1457         struct mlx5_priv *priv = &dev->priv;
1458
1459         devlink_unregister(devlink);
1460         mlx5_unregister_device(dev);
1461
1462         if (mlx5_unload_one(dev, priv, true)) {
1463                 dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
1464                 mlx5_health_cleanup(dev);
1465                 return;
1466         }
1467
1468         mlx5_pagealloc_cleanup(dev);
1469         mlx5_health_cleanup(dev);
1470         mlx5_pci_close(dev, priv);
1471 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1472         cleanup_srcu_struct(&priv->pfault_srcu);
1473 #endif
1474         devlink_free(devlink);
1475 }
1476
1477 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1478                                               pci_channel_state_t state)
1479 {
1480         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1481         struct mlx5_priv *priv = &dev->priv;
1482
1483         dev_info(&pdev->dev, "%s was called\n", __func__);
1484
1485         mlx5_enter_error_state(dev, false);
1486         mlx5_unload_one(dev, priv, false);
1487         /* In case of kernel call drain the health wq */
1488         if (state) {
1489                 mlx5_drain_health_wq(dev);
1490                 mlx5_pci_disable_device(dev);
1491         }
1492
1493         return state == pci_channel_io_perm_failure ?
1494                 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1495 }
1496
1497 /* wait for the device to show vital signs by waiting
1498  * for the health counter to start counting.
1499  */
1500 static int wait_vital(struct pci_dev *pdev)
1501 {
1502         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1503         struct mlx5_core_health *health = &dev->priv.health;
1504         const int niter = 100;
1505         u32 last_count = 0;
1506         u32 count;
1507         int i;
1508
1509         for (i = 0; i < niter; i++) {
1510                 count = ioread32be(health->health_counter);
1511                 if (count && count != 0xffffffff) {
1512                         if (last_count && last_count != count) {
1513                                 dev_info(&pdev->dev, "Counter value 0x%x after %d iterations\n", count, i);
1514                                 return 0;
1515                         }
1516                         last_count = count;
1517                 }
1518                 msleep(50);
1519         }
1520
1521         return -ETIMEDOUT;
1522 }
1523
1524 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1525 {
1526         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1527         int err;
1528
1529         dev_info(&pdev->dev, "%s was called\n", __func__);
1530
1531         err = mlx5_pci_enable_device(dev);
1532         if (err) {
1533                 dev_err(&pdev->dev, "%s: mlx5_pci_enable_device failed with error code: %d\n"
1534                         , __func__, err);
1535                 return PCI_ERS_RESULT_DISCONNECT;
1536         }
1537
1538         pci_set_master(pdev);
1539         pci_restore_state(pdev);
1540         pci_save_state(pdev);
1541
1542         if (wait_vital(pdev)) {
1543                 dev_err(&pdev->dev, "%s: wait_vital timed out\n", __func__);
1544                 return PCI_ERS_RESULT_DISCONNECT;
1545         }
1546
1547         return PCI_ERS_RESULT_RECOVERED;
1548 }
1549
1550 static void mlx5_pci_resume(struct pci_dev *pdev)
1551 {
1552         struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1553         struct mlx5_priv *priv = &dev->priv;
1554         int err;
1555
1556         dev_info(&pdev->dev, "%s was called\n", __func__);
1557
1558         err = mlx5_load_one(dev, priv, false);
1559         if (err)
1560                 dev_err(&pdev->dev, "%s: mlx5_load_one failed with error code: %d\n"
1561                         , __func__, err);
1562         else
1563                 dev_info(&pdev->dev, "%s: device recovered\n", __func__);
1564 }
1565
1566 static const struct pci_error_handlers mlx5_err_handler = {
1567         .error_detected = mlx5_pci_err_detected,
1568         .slot_reset     = mlx5_pci_slot_reset,
1569         .resume         = mlx5_pci_resume
1570 };
1571
1572 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1573 {
1574         int ret;
1575
1576         if (!MLX5_CAP_GEN(dev, force_teardown)) {
1577                 mlx5_core_dbg(dev, "force teardown is not supported in the firmware\n");
1578                 return -EOPNOTSUPP;
1579         }
1580
1581         if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1582                 mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1583                 return -EAGAIN;
1584         }
1585
1586         /* Panic tear down fw command will stop the PCI bus communication
1587          * with the HCA, so the health polll is no longer needed.
1588          */
1589         mlx5_drain_health_wq(dev);
1590         mlx5_stop_health_poll(dev);
1591
1592         ret = mlx5_cmd_force_teardown_hca(dev);
1593         if (ret) {
1594                 mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1595                 mlx5_start_health_poll(dev);
1596                 return ret;
1597         }
1598
1599         mlx5_enter_error_state(dev, true);
1600
1601         /* Some platforms requiring freeing the IRQ's in the shutdown
1602          * flow. If they aren't freed they can't be allocated after
1603          * kexec. There is no need to cleanup the mlx5_core software
1604          * contexts.
1605          */
1606         mlx5_irq_clear_affinity_hints(dev);
1607         mlx5_core_eq_free_irqs(dev);
1608
1609         return 0;
1610 }
1611
1612 static void shutdown(struct pci_dev *pdev)
1613 {
1614         struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1615         struct mlx5_priv *priv = &dev->priv;
1616         int err;
1617
1618         dev_info(&pdev->dev, "Shutdown was called\n");
1619         err = mlx5_try_fast_unload(dev);
1620         if (err)
1621                 mlx5_unload_one(dev, priv, false);
1622         mlx5_pci_disable_device(dev);
1623 }
1624
1625 static const struct pci_device_id mlx5_core_pci_table[] = {
1626         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1627         { PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},   /* Connect-IB VF */
1628         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1629         { PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4 VF */
1630         { PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1631         { PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},   /* ConnectX-4LX VF */
1632         { PCI_VDEVICE(MELLANOX, 0x1017) },                      /* ConnectX-5, PCIe 3.0 */
1633         { PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 VF */
1634         { PCI_VDEVICE(MELLANOX, 0x1019) },                      /* ConnectX-5 Ex */
1635         { PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},   /* ConnectX-5 Ex VF */
1636         { PCI_VDEVICE(MELLANOX, 0x101b) },                      /* ConnectX-6 */
1637         { PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},   /* ConnectX-6 VF */
1638         { PCI_VDEVICE(MELLANOX, 0xa2d2) },                      /* BlueField integrated ConnectX-5 network controller */
1639         { PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},   /* BlueField integrated ConnectX-5 network controller VF */
1640         { 0, }
1641 };
1642
1643 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1644
1645 void mlx5_disable_device(struct mlx5_core_dev *dev)
1646 {
1647         mlx5_pci_err_detected(dev->pdev, 0);
1648 }
1649
1650 void mlx5_recover_device(struct mlx5_core_dev *dev)
1651 {
1652         mlx5_pci_disable_device(dev);
1653         if (mlx5_pci_slot_reset(dev->pdev) == PCI_ERS_RESULT_RECOVERED)
1654                 mlx5_pci_resume(dev->pdev);
1655 }
1656
1657 static struct pci_driver mlx5_core_driver = {
1658         .name           = DRIVER_NAME,
1659         .id_table       = mlx5_core_pci_table,
1660         .probe          = init_one,
1661         .remove         = remove_one,
1662         .shutdown       = shutdown,
1663         .err_handler    = &mlx5_err_handler,
1664         .sriov_configure   = mlx5_core_sriov_configure,
1665 };
1666
1667 static void mlx5_core_verify_params(void)
1668 {
1669         if (prof_sel >= ARRAY_SIZE(profile)) {
1670                 pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
1671                         prof_sel,
1672                         ARRAY_SIZE(profile) - 1,
1673                         MLX5_DEFAULT_PROF);
1674                 prof_sel = MLX5_DEFAULT_PROF;
1675         }
1676 }
1677
1678 static int __init init(void)
1679 {
1680         int err;
1681
1682         get_random_bytes(&sw_owner_id, sizeof(sw_owner_id));
1683
1684         mlx5_core_verify_params();
1685         mlx5_fpga_ipsec_build_fs_cmds();
1686         mlx5_register_debugfs();
1687
1688         err = pci_register_driver(&mlx5_core_driver);
1689         if (err)
1690                 goto err_debug;
1691
1692 #ifdef CONFIG_MLX5_CORE_EN
1693         mlx5e_init();
1694 #endif
1695
1696         return 0;
1697
1698 err_debug:
1699         mlx5_unregister_debugfs();
1700         return err;
1701 }
1702
1703 static void __exit cleanup(void)
1704 {
1705 #ifdef CONFIG_MLX5_CORE_EN
1706         mlx5e_cleanup();
1707 #endif
1708         pci_unregister_driver(&mlx5_core_driver);
1709         mlx5_unregister_debugfs();
1710 }
1711
1712 module_init(init);
1713 module_exit(cleanup);