2 * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <asm-generic/kmap_types.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/mlx5/driver.h>
42 #include <linux/mlx5/cq.h>
43 #include <linux/mlx5/qp.h>
44 #include <linux/mlx5/srq.h>
45 #include <linux/debugfs.h>
46 #include <linux/kmod.h>
47 #include <linux/mlx5/mlx5_ifc.h>
48 #include "mlx5_core.h"
50 #define DRIVER_NAME "mlx5_core"
51 #define DRIVER_VERSION "2.2-1"
52 #define DRIVER_RELDATE "Feb 2014"
54 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
55 MODULE_DESCRIPTION("Mellanox ConnectX-IB HCA core library");
56 MODULE_LICENSE("Dual BSD/GPL");
57 MODULE_VERSION(DRIVER_VERSION);
59 int mlx5_core_debug_mask;
60 module_param_named(debug_mask, mlx5_core_debug_mask, int, 0644);
61 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
63 #define MLX5_DEFAULT_PROF 2
64 static int prof_sel = MLX5_DEFAULT_PROF;
65 module_param_named(prof_sel, prof_sel, int, 0444);
66 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
68 struct workqueue_struct *mlx5_core_wq;
69 static LIST_HEAD(intf_list);
70 static LIST_HEAD(dev_list);
71 static DEFINE_MUTEX(intf_mutex);
73 struct mlx5_device_context {
74 struct list_head list;
75 struct mlx5_interface *intf;
79 static struct mlx5_profile profile[] = {
84 .mask = MLX5_PROF_MASK_QP_SIZE,
88 .mask = MLX5_PROF_MASK_QP_SIZE |
89 MLX5_PROF_MASK_MR_CACHE,
158 static int set_dma_caps(struct pci_dev *pdev)
162 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
164 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
165 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
167 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
172 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
175 "Warning: couldn't set 64-bit consistent PCI DMA mask\n");
176 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
179 "Can't set consistent PCI DMA mask, aborting\n");
184 dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
188 static int request_bar(struct pci_dev *pdev)
192 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
193 dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
197 err = pci_request_regions(pdev, DRIVER_NAME);
199 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
204 static void release_bar(struct pci_dev *pdev)
206 pci_release_regions(pdev);
209 static int mlx5_enable_msix(struct mlx5_core_dev *dev)
211 struct mlx5_eq_table *table = &dev->priv.eq_table;
212 int num_eqs = 1 << dev->caps.gen.log_max_eq;
216 nvec = dev->caps.gen.num_ports * num_online_cpus() + MLX5_EQ_VEC_COMP_BASE;
217 nvec = min_t(int, nvec, num_eqs);
218 if (nvec <= MLX5_EQ_VEC_COMP_BASE)
221 table->msix_arr = kzalloc(nvec * sizeof(*table->msix_arr), GFP_KERNEL);
222 if (!table->msix_arr)
225 for (i = 0; i < nvec; i++)
226 table->msix_arr[i].entry = i;
228 nvec = pci_enable_msix_range(dev->pdev, table->msix_arr,
229 MLX5_EQ_VEC_COMP_BASE + 1, nvec);
233 table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
238 static void mlx5_disable_msix(struct mlx5_core_dev *dev)
240 struct mlx5_eq_table *table = &dev->priv.eq_table;
242 pci_disable_msix(dev->pdev);
243 kfree(table->msix_arr);
246 struct mlx5_reg_host_endianess {
252 #define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
255 MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
256 MLX5_DEV_CAP_FLAG_DCT,
259 static u16 to_fw_pkey_sz(u32 size)
275 pr_warn("invalid pkey table size %d\n", size);
280 /* selectively copy writable fields clearing any reserved area
282 static void copy_rw_fields(void *to, struct mlx5_caps *from)
284 __be64 *flags_off = (__be64 *)MLX5_ADDR_OF(cmd_hca_cap, to, reserved_22);
287 MLX5_SET(cmd_hca_cap, to, log_max_qp, from->gen.log_max_qp);
288 MLX5_SET(cmd_hca_cap, to, log_max_ra_req_qp, from->gen.log_max_ra_req_qp);
289 MLX5_SET(cmd_hca_cap, to, log_max_ra_res_qp, from->gen.log_max_ra_res_qp);
290 MLX5_SET(cmd_hca_cap, to, pkey_table_size, from->gen.pkey_table_size);
291 MLX5_SET(cmd_hca_cap, to, log_max_ra_req_dc, from->gen.log_max_ra_req_dc);
292 MLX5_SET(cmd_hca_cap, to, log_max_ra_res_dc, from->gen.log_max_ra_res_dc);
293 MLX5_SET(cmd_hca_cap, to, pkey_table_size, to_fw_pkey_sz(from->gen.pkey_table_size));
294 v64 = from->gen.flags & MLX5_CAP_BITS_RW_MASK;
295 *flags_off = cpu_to_be64(v64);
298 static u16 get_pkey_table_size(int pkey)
300 if (pkey > MLX5_MAX_LOG_PKEY_TABLE)
303 return MLX5_MIN_PKEY_TABLE_SIZE << pkey;
306 static void fw2drv_caps(struct mlx5_caps *caps, void *out)
308 struct mlx5_general_caps *gen = &caps->gen;
310 gen->max_srq_wqes = 1 << MLX5_GET_PR(cmd_hca_cap, out, log_max_srq_sz);
311 gen->max_wqes = 1 << MLX5_GET_PR(cmd_hca_cap, out, log_max_qp_sz);
312 gen->log_max_qp = MLX5_GET_PR(cmd_hca_cap, out, log_max_qp);
313 gen->log_max_strq = MLX5_GET_PR(cmd_hca_cap, out, log_max_strq_sz);
314 gen->log_max_srq = MLX5_GET_PR(cmd_hca_cap, out, log_max_srqs);
315 gen->max_cqes = 1 << MLX5_GET_PR(cmd_hca_cap, out, log_max_cq_sz);
316 gen->log_max_cq = MLX5_GET_PR(cmd_hca_cap, out, log_max_cq);
317 gen->max_eqes = 1 << MLX5_GET_PR(cmd_hca_cap, out, log_max_eq_sz);
318 gen->log_max_mkey = MLX5_GET_PR(cmd_hca_cap, out, log_max_mkey);
319 gen->log_max_eq = MLX5_GET_PR(cmd_hca_cap, out, log_max_eq);
320 gen->max_indirection = MLX5_GET_PR(cmd_hca_cap, out, max_indirection);
321 gen->log_max_mrw_sz = MLX5_GET_PR(cmd_hca_cap, out, log_max_mrw_sz);
322 gen->log_max_bsf_list_size = MLX5_GET_PR(cmd_hca_cap, out, log_max_bsf_list_size);
323 gen->log_max_klm_list_size = MLX5_GET_PR(cmd_hca_cap, out, log_max_klm_list_size);
324 gen->log_max_ra_req_dc = MLX5_GET_PR(cmd_hca_cap, out, log_max_ra_req_dc);
325 gen->log_max_ra_res_dc = MLX5_GET_PR(cmd_hca_cap, out, log_max_ra_res_dc);
326 gen->log_max_ra_req_qp = MLX5_GET_PR(cmd_hca_cap, out, log_max_ra_req_qp);
327 gen->log_max_ra_res_qp = MLX5_GET_PR(cmd_hca_cap, out, log_max_ra_res_qp);
328 gen->max_qp_counters = MLX5_GET_PR(cmd_hca_cap, out, max_qp_cnt);
329 gen->pkey_table_size = get_pkey_table_size(MLX5_GET_PR(cmd_hca_cap, out, pkey_table_size));
330 gen->local_ca_ack_delay = MLX5_GET_PR(cmd_hca_cap, out, local_ca_ack_delay);
331 gen->num_ports = MLX5_GET_PR(cmd_hca_cap, out, num_ports);
332 gen->log_max_msg = MLX5_GET_PR(cmd_hca_cap, out, log_max_msg);
333 gen->stat_rate_support = MLX5_GET_PR(cmd_hca_cap, out, stat_rate_support);
334 gen->flags = be64_to_cpu(*(__be64 *)MLX5_ADDR_OF(cmd_hca_cap, out, reserved_22));
335 pr_debug("flags = 0x%llx\n", gen->flags);
336 gen->uar_sz = MLX5_GET_PR(cmd_hca_cap, out, uar_sz);
337 gen->min_log_pg_sz = MLX5_GET_PR(cmd_hca_cap, out, log_pg_sz);
338 gen->bf_reg_size = MLX5_GET_PR(cmd_hca_cap, out, bf);
339 gen->bf_reg_size = 1 << MLX5_GET_PR(cmd_hca_cap, out, log_bf_reg_size);
340 gen->max_sq_desc_sz = MLX5_GET_PR(cmd_hca_cap, out, max_wqe_sz_sq);
341 gen->max_rq_desc_sz = MLX5_GET_PR(cmd_hca_cap, out, max_wqe_sz_rq);
342 gen->max_dc_sq_desc_sz = MLX5_GET_PR(cmd_hca_cap, out, max_wqe_sz_sq_dc);
343 gen->max_qp_mcg = MLX5_GET_PR(cmd_hca_cap, out, max_qp_mcg);
344 gen->log_max_pd = MLX5_GET_PR(cmd_hca_cap, out, log_max_pd);
345 gen->log_max_xrcd = MLX5_GET_PR(cmd_hca_cap, out, log_max_xrcd);
346 gen->log_uar_page_sz = MLX5_GET_PR(cmd_hca_cap, out, log_uar_page_sz);
349 static const char *caps_opmod_str(u16 opmod)
352 case HCA_CAP_OPMOD_GET_MAX:
354 case HCA_CAP_OPMOD_GET_CUR:
361 int mlx5_core_get_caps(struct mlx5_core_dev *dev, struct mlx5_caps *caps,
364 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
365 int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
369 memset(in, 0, sizeof(in));
370 out = kzalloc(out_sz, GFP_KERNEL);
373 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
374 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
375 err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
379 err = mlx5_cmd_status_to_err_v2(out);
381 mlx5_core_warn(dev, "query max hca cap failed, %d\n", err);
384 mlx5_core_dbg(dev, "%s\n", caps_opmod_str(opmod));
385 fw2drv_caps(caps, MLX5_ADDR_OF(query_hca_cap_out, out, capability_struct));
392 static int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz)
394 u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)];
397 memset(out, 0, sizeof(out));
399 MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
400 err = mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
404 err = mlx5_cmd_status_to_err_v2(out);
409 static int handle_hca_cap(struct mlx5_core_dev *dev)
411 void *set_ctx = NULL;
412 struct mlx5_profile *prof = dev->profile;
413 struct mlx5_caps *cur_caps = NULL;
414 struct mlx5_caps *max_caps = NULL;
416 int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
418 set_ctx = kzalloc(set_sz, GFP_KERNEL);
422 max_caps = kzalloc(sizeof(*max_caps), GFP_KERNEL);
426 cur_caps = kzalloc(sizeof(*cur_caps), GFP_KERNEL);
430 err = mlx5_core_get_caps(dev, max_caps, HCA_CAP_OPMOD_GET_MAX);
434 err = mlx5_core_get_caps(dev, cur_caps, HCA_CAP_OPMOD_GET_CUR);
438 /* we limit the size of the pkey table to 128 entries for now */
439 cur_caps->gen.pkey_table_size = 128;
441 if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
442 cur_caps->gen.log_max_qp = prof->log_max_qp;
444 /* disable checksum */
445 cur_caps->gen.flags &= ~MLX5_DEV_CAP_FLAG_CMDIF_CSUM;
447 copy_rw_fields(MLX5_ADDR_OF(set_hca_cap_in, set_ctx, hca_capability_struct),
449 err = set_caps(dev, set_ctx, set_sz);
459 static int set_hca_ctrl(struct mlx5_core_dev *dev)
461 struct mlx5_reg_host_endianess he_in;
462 struct mlx5_reg_host_endianess he_out;
465 memset(&he_in, 0, sizeof(he_in));
466 he_in.he = MLX5_SET_HOST_ENDIANNESS;
467 err = mlx5_core_access_reg(dev, &he_in, sizeof(he_in),
468 &he_out, sizeof(he_out),
469 MLX5_REG_HOST_ENDIANNESS, 0, 1);
473 static int mlx5_core_enable_hca(struct mlx5_core_dev *dev)
476 struct mlx5_enable_hca_mbox_in in;
477 struct mlx5_enable_hca_mbox_out out;
479 memset(&in, 0, sizeof(in));
480 memset(&out, 0, sizeof(out));
481 in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_ENABLE_HCA);
482 err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
487 return mlx5_cmd_status_to_err(&out.hdr);
492 static int mlx5_core_disable_hca(struct mlx5_core_dev *dev)
495 struct mlx5_disable_hca_mbox_in in;
496 struct mlx5_disable_hca_mbox_out out;
498 memset(&in, 0, sizeof(in));
499 memset(&out, 0, sizeof(out));
500 in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DISABLE_HCA);
501 err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
506 return mlx5_cmd_status_to_err(&out.hdr);
511 static int mlx5_dev_init(struct mlx5_core_dev *dev, struct pci_dev *pdev)
513 struct mlx5_priv *priv = &dev->priv;
517 pci_set_drvdata(dev->pdev, dev);
518 strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
519 priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
521 mutex_init(&priv->pgdir_mutex);
522 INIT_LIST_HEAD(&priv->pgdir_list);
523 spin_lock_init(&priv->mkey_lock);
525 priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
529 err = pci_enable_device(pdev);
531 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
535 err = request_bar(pdev);
537 dev_err(&pdev->dev, "error requesting BARs, aborting\n");
541 pci_set_master(pdev);
543 err = set_dma_caps(pdev);
545 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
549 dev->iseg_base = pci_resource_start(dev->pdev, 0);
550 dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
553 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
556 dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
557 fw_rev_min(dev), fw_rev_sub(dev));
559 err = mlx5_cmd_init(dev);
561 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
565 mlx5_pagealloc_init(dev);
567 err = mlx5_core_enable_hca(dev);
569 dev_err(&pdev->dev, "enable hca failed\n");
570 goto err_pagealloc_cleanup;
573 err = mlx5_satisfy_startup_pages(dev, 1);
575 dev_err(&pdev->dev, "failed to allocate boot pages\n");
576 goto err_disable_hca;
579 err = set_hca_ctrl(dev);
581 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
582 goto reclaim_boot_pages;
585 err = handle_hca_cap(dev);
587 dev_err(&pdev->dev, "handle_hca_cap failed\n");
588 goto reclaim_boot_pages;
591 err = mlx5_satisfy_startup_pages(dev, 0);
593 dev_err(&pdev->dev, "failed to allocate init pages\n");
594 goto reclaim_boot_pages;
597 err = mlx5_pagealloc_start(dev);
599 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
600 goto reclaim_boot_pages;
603 err = mlx5_cmd_init_hca(dev);
605 dev_err(&pdev->dev, "init hca failed\n");
606 goto err_pagealloc_stop;
609 mlx5_start_health_poll(dev);
611 err = mlx5_cmd_query_hca_cap(dev, &dev->caps);
613 dev_err(&pdev->dev, "query hca failed\n");
617 err = mlx5_cmd_query_adapter(dev);
619 dev_err(&pdev->dev, "query adapter failed\n");
623 err = mlx5_enable_msix(dev);
625 dev_err(&pdev->dev, "enable msix failed\n");
629 err = mlx5_eq_init(dev);
631 dev_err(&pdev->dev, "failed to initialize eq\n");
635 err = mlx5_alloc_uuars(dev, &priv->uuari);
637 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
641 err = mlx5_start_eqs(dev);
643 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
647 MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
649 mlx5_init_cq_table(dev);
650 mlx5_init_qp_table(dev);
651 mlx5_init_srq_table(dev);
652 mlx5_init_mr_table(dev);
657 mlx5_free_uuars(dev, &priv->uuari);
660 mlx5_eq_cleanup(dev);
663 mlx5_disable_msix(dev);
666 mlx5_stop_health_poll(dev);
667 if (mlx5_cmd_teardown_hca(dev)) {
668 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
673 mlx5_pagealloc_stop(dev);
676 mlx5_reclaim_startup_pages(dev);
679 mlx5_core_disable_hca(dev);
681 err_pagealloc_cleanup:
682 mlx5_pagealloc_cleanup(dev);
683 mlx5_cmd_cleanup(dev);
689 pci_clear_master(dev->pdev);
690 release_bar(dev->pdev);
693 pci_disable_device(dev->pdev);
696 debugfs_remove(priv->dbg_root);
699 EXPORT_SYMBOL(mlx5_dev_init);
701 static void mlx5_dev_cleanup(struct mlx5_core_dev *dev)
703 struct mlx5_priv *priv = &dev->priv;
705 mlx5_cleanup_srq_table(dev);
706 mlx5_cleanup_qp_table(dev);
707 mlx5_cleanup_cq_table(dev);
709 mlx5_free_uuars(dev, &priv->uuari);
710 mlx5_eq_cleanup(dev);
711 mlx5_disable_msix(dev);
712 mlx5_stop_health_poll(dev);
713 if (mlx5_cmd_teardown_hca(dev)) {
714 dev_err(&dev->pdev->dev, "tear_down_hca failed, skip cleanup\n");
717 mlx5_pagealloc_stop(dev);
718 mlx5_reclaim_startup_pages(dev);
719 mlx5_core_disable_hca(dev);
720 mlx5_pagealloc_cleanup(dev);
721 mlx5_cmd_cleanup(dev);
723 pci_clear_master(dev->pdev);
724 release_bar(dev->pdev);
725 pci_disable_device(dev->pdev);
726 debugfs_remove(priv->dbg_root);
729 static void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
731 struct mlx5_device_context *dev_ctx;
732 struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
734 dev_ctx = kmalloc(sizeof(*dev_ctx), GFP_KERNEL);
736 pr_warn("mlx5_add_device: alloc context failed\n");
740 dev_ctx->intf = intf;
741 dev_ctx->context = intf->add(dev);
743 if (dev_ctx->context) {
744 spin_lock_irq(&priv->ctx_lock);
745 list_add_tail(&dev_ctx->list, &priv->ctx_list);
746 spin_unlock_irq(&priv->ctx_lock);
752 static void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
754 struct mlx5_device_context *dev_ctx;
755 struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
757 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
758 if (dev_ctx->intf == intf) {
759 spin_lock_irq(&priv->ctx_lock);
760 list_del(&dev_ctx->list);
761 spin_unlock_irq(&priv->ctx_lock);
763 intf->remove(dev, dev_ctx->context);
768 static int mlx5_register_device(struct mlx5_core_dev *dev)
770 struct mlx5_priv *priv = &dev->priv;
771 struct mlx5_interface *intf;
773 mutex_lock(&intf_mutex);
774 list_add_tail(&priv->dev_list, &dev_list);
775 list_for_each_entry(intf, &intf_list, list)
776 mlx5_add_device(intf, priv);
777 mutex_unlock(&intf_mutex);
781 static void mlx5_unregister_device(struct mlx5_core_dev *dev)
783 struct mlx5_priv *priv = &dev->priv;
784 struct mlx5_interface *intf;
786 mutex_lock(&intf_mutex);
787 list_for_each_entry(intf, &intf_list, list)
788 mlx5_remove_device(intf, priv);
789 list_del(&priv->dev_list);
790 mutex_unlock(&intf_mutex);
793 int mlx5_register_interface(struct mlx5_interface *intf)
795 struct mlx5_priv *priv;
797 if (!intf->add || !intf->remove)
800 mutex_lock(&intf_mutex);
801 list_add_tail(&intf->list, &intf_list);
802 list_for_each_entry(priv, &dev_list, dev_list)
803 mlx5_add_device(intf, priv);
804 mutex_unlock(&intf_mutex);
808 EXPORT_SYMBOL(mlx5_register_interface);
810 void mlx5_unregister_interface(struct mlx5_interface *intf)
812 struct mlx5_priv *priv;
814 mutex_lock(&intf_mutex);
815 list_for_each_entry(priv, &dev_list, dev_list)
816 mlx5_remove_device(intf, priv);
817 list_del(&intf->list);
818 mutex_unlock(&intf_mutex);
820 EXPORT_SYMBOL(mlx5_unregister_interface);
822 static void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
825 struct mlx5_priv *priv = &dev->priv;
826 struct mlx5_device_context *dev_ctx;
829 spin_lock_irqsave(&priv->ctx_lock, flags);
831 list_for_each_entry(dev_ctx, &priv->ctx_list, list)
832 if (dev_ctx->intf->event)
833 dev_ctx->intf->event(dev, dev_ctx->context, event, param);
835 spin_unlock_irqrestore(&priv->ctx_lock, flags);
838 struct mlx5_core_event_handler {
839 void (*event)(struct mlx5_core_dev *dev,
840 enum mlx5_dev_event event,
844 #define MLX5_IB_MOD "mlx5_ib"
846 static int init_one(struct pci_dev *pdev,
847 const struct pci_device_id *id)
849 struct mlx5_core_dev *dev;
850 struct mlx5_priv *priv;
853 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
855 dev_err(&pdev->dev, "kzalloc failed\n");
860 pci_set_drvdata(pdev, dev);
862 if (prof_sel < 0 || prof_sel >= ARRAY_SIZE(profile)) {
863 pr_warn("selected profile out of range, selecting default (%d)\n",
865 prof_sel = MLX5_DEFAULT_PROF;
867 dev->profile = &profile[prof_sel];
868 dev->event = mlx5_core_event;
870 INIT_LIST_HEAD(&priv->ctx_list);
871 spin_lock_init(&priv->ctx_lock);
872 err = mlx5_dev_init(dev, pdev);
874 dev_err(&pdev->dev, "mlx5_dev_init failed %d\n", err);
878 err = mlx5_register_device(dev);
880 dev_err(&pdev->dev, "mlx5_register_device failed %d\n", err);
884 err = request_module_nowait(MLX5_IB_MOD);
886 pr_info("failed request module on %s\n", MLX5_IB_MOD);
891 mlx5_dev_cleanup(dev);
896 static void remove_one(struct pci_dev *pdev)
898 struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
900 mlx5_unregister_device(dev);
901 mlx5_dev_cleanup(dev);
905 static const struct pci_device_id mlx5_core_pci_table[] = {
906 { PCI_VDEVICE(MELLANOX, 0x1011) }, /* Connect-IB */
907 { PCI_VDEVICE(MELLANOX, 0x1012) }, /* Connect-IB VF */
908 { PCI_VDEVICE(MELLANOX, 0x1013) }, /* ConnectX-4 */
909 { PCI_VDEVICE(MELLANOX, 0x1014) }, /* ConnectX-4 VF */
910 { PCI_VDEVICE(MELLANOX, 0x1015) }, /* ConnectX-4LX */
911 { PCI_VDEVICE(MELLANOX, 0x1016) }, /* ConnectX-4LX VF */
915 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
917 static struct pci_driver mlx5_core_driver = {
919 .id_table = mlx5_core_pci_table,
924 static int __init init(void)
928 mlx5_register_debugfs();
929 mlx5_core_wq = create_singlethread_workqueue("mlx5_core_wq");
936 err = pci_register_driver(&mlx5_core_driver);
943 mlx5_health_cleanup();
944 destroy_workqueue(mlx5_core_wq);
946 mlx5_unregister_debugfs();
950 static void __exit cleanup(void)
952 pci_unregister_driver(&mlx5_core_driver);
953 mlx5_health_cleanup();
954 destroy_workqueue(mlx5_core_wq);
955 mlx5_unregister_debugfs();
959 module_exit(cleanup);