WIP FPC-III support
[linux/fpc-iii.git] / drivers / infiniband / hw / efa / efa_main.c
blob0f578734bddb60e34041c63081927ab9a388ba31
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
2 /*
3 * Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
4 */
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <linux/utsname.h>
9 #include <linux/version.h>
11 #include <rdma/ib_user_verbs.h>
13 #include "efa.h"
15 #define PCI_DEV_ID_EFA0_VF 0xefa0
16 #define PCI_DEV_ID_EFA1_VF 0xefa1
18 static const struct pci_device_id efa_pci_tbl[] = {
19 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA0_VF) },
20 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA1_VF) },
21 { }
24 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
25 MODULE_LICENSE("Dual BSD/GPL");
26 MODULE_DESCRIPTION(DEVICE_NAME);
27 MODULE_DEVICE_TABLE(pci, efa_pci_tbl);
29 #define EFA_REG_BAR 0
30 #define EFA_MEM_BAR 2
31 #define EFA_BASE_BAR_MASK (BIT(EFA_REG_BAR) | BIT(EFA_MEM_BAR))
33 #define EFA_AENQ_ENABLED_GROUPS \
34 (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \
35 BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE))
37 /* This handler will called for unknown event group or unimplemented handlers */
38 static void unimplemented_aenq_handler(void *data,
39 struct efa_admin_aenq_entry *aenq_e)
41 struct efa_dev *dev = (struct efa_dev *)data;
43 ibdev_err(&dev->ibdev,
44 "Unknown event was received or event with unimplemented handler\n");
47 static void efa_keep_alive(void *data, struct efa_admin_aenq_entry *aenq_e)
49 struct efa_dev *dev = (struct efa_dev *)data;
51 atomic64_inc(&dev->stats.keep_alive_rcvd);
54 static struct efa_aenq_handlers aenq_handlers = {
55 .handlers = {
56 [EFA_ADMIN_KEEP_ALIVE] = efa_keep_alive,
58 .unimplemented_handler = unimplemented_aenq_handler
61 static void efa_release_bars(struct efa_dev *dev, int bars_mask)
63 struct pci_dev *pdev = dev->pdev;
64 int release_bars;
66 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & bars_mask;
67 pci_release_selected_regions(pdev, release_bars);
70 static irqreturn_t efa_intr_msix_mgmnt(int irq, void *data)
72 struct efa_dev *dev = data;
74 efa_com_admin_q_comp_intr_handler(&dev->edev);
75 efa_com_aenq_intr_handler(&dev->edev, data);
77 return IRQ_HANDLED;
80 static int efa_request_mgmnt_irq(struct efa_dev *dev)
82 struct efa_irq *irq;
83 int err;
85 irq = &dev->admin_irq;
86 err = request_irq(irq->vector, irq->handler, 0, irq->name,
87 irq->data);
88 if (err) {
89 dev_err(&dev->pdev->dev, "Failed to request admin irq (%d)\n",
90 err);
91 return err;
94 dev_dbg(&dev->pdev->dev, "Set affinity hint of mgmnt irq to %*pbl (irq vector: %d)\n",
95 nr_cpumask_bits, &irq->affinity_hint_mask, irq->vector);
96 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
98 return 0;
101 static void efa_setup_mgmnt_irq(struct efa_dev *dev)
103 u32 cpu;
105 snprintf(dev->admin_irq.name, EFA_IRQNAME_SIZE,
106 "efa-mgmnt@pci:%s", pci_name(dev->pdev));
107 dev->admin_irq.handler = efa_intr_msix_mgmnt;
108 dev->admin_irq.data = dev;
109 dev->admin_irq.vector =
110 pci_irq_vector(dev->pdev, dev->admin_msix_vector_idx);
111 cpu = cpumask_first(cpu_online_mask);
112 dev->admin_irq.cpu = cpu;
113 cpumask_set_cpu(cpu,
114 &dev->admin_irq.affinity_hint_mask);
115 dev_info(&dev->pdev->dev, "Setup irq:0x%p vector:%d name:%s\n",
116 &dev->admin_irq,
117 dev->admin_irq.vector,
118 dev->admin_irq.name);
121 static void efa_free_mgmnt_irq(struct efa_dev *dev)
123 struct efa_irq *irq;
125 irq = &dev->admin_irq;
126 irq_set_affinity_hint(irq->vector, NULL);
127 free_irq(irq->vector, irq->data);
130 static int efa_set_mgmnt_irq(struct efa_dev *dev)
132 efa_setup_mgmnt_irq(dev);
134 return efa_request_mgmnt_irq(dev);
137 static int efa_request_doorbell_bar(struct efa_dev *dev)
139 u8 db_bar_idx = dev->dev_attr.db_bar;
140 struct pci_dev *pdev = dev->pdev;
141 int bars;
142 int err;
144 if (!(BIT(db_bar_idx) & EFA_BASE_BAR_MASK)) {
145 bars = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(db_bar_idx);
147 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
148 if (err) {
149 dev_err(&dev->pdev->dev,
150 "pci_request_selected_regions for bar %d failed %d\n",
151 db_bar_idx, err);
152 return err;
156 dev->db_bar_addr = pci_resource_start(dev->pdev, db_bar_idx);
157 dev->db_bar_len = pci_resource_len(dev->pdev, db_bar_idx);
159 return 0;
162 static void efa_release_doorbell_bar(struct efa_dev *dev)
164 if (!(BIT(dev->dev_attr.db_bar) & EFA_BASE_BAR_MASK))
165 efa_release_bars(dev, BIT(dev->dev_attr.db_bar));
168 static void efa_update_hw_hints(struct efa_dev *dev,
169 struct efa_com_get_hw_hints_result *hw_hints)
171 struct efa_com_dev *edev = &dev->edev;
173 if (hw_hints->mmio_read_timeout)
174 edev->mmio_read.mmio_read_timeout =
175 hw_hints->mmio_read_timeout * 1000;
177 if (hw_hints->poll_interval)
178 edev->aq.poll_interval = hw_hints->poll_interval;
180 if (hw_hints->admin_completion_timeout)
181 edev->aq.completion_timeout =
182 hw_hints->admin_completion_timeout;
185 static void efa_stats_init(struct efa_dev *dev)
187 atomic64_t *s = (atomic64_t *)&dev->stats;
188 int i;
190 for (i = 0; i < sizeof(dev->stats) / sizeof(*s); i++, s++)
191 atomic64_set(s, 0);
194 static void efa_set_host_info(struct efa_dev *dev)
196 struct efa_admin_set_feature_resp resp = {};
197 struct efa_admin_set_feature_cmd cmd = {};
198 struct efa_admin_host_info *hinf;
199 u32 bufsz = sizeof(*hinf);
200 dma_addr_t hinf_dma;
202 if (!efa_com_check_supported_feature_id(&dev->edev,
203 EFA_ADMIN_HOST_INFO))
204 return;
206 /* Failures in host info set shall not disturb probe */
207 hinf = dma_alloc_coherent(&dev->pdev->dev, bufsz, &hinf_dma,
208 GFP_KERNEL);
209 if (!hinf)
210 return;
212 strlcpy(hinf->os_dist_str, utsname()->release,
213 min(sizeof(hinf->os_dist_str), sizeof(utsname()->release)));
214 hinf->os_type = EFA_ADMIN_OS_LINUX;
215 strlcpy(hinf->kernel_ver_str, utsname()->version,
216 min(sizeof(hinf->kernel_ver_str), sizeof(utsname()->version)));
217 hinf->kernel_ver = LINUX_VERSION_CODE;
218 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MAJOR, 0);
219 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MINOR, 0);
220 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_SUB_MINOR, 0);
221 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MODULE_TYPE, 0);
222 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_BUS, dev->pdev->bus->number);
223 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_DEVICE,
224 PCI_SLOT(dev->pdev->devfn));
225 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_FUNCTION,
226 PCI_FUNC(dev->pdev->devfn));
227 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MAJOR,
228 EFA_COMMON_SPEC_VERSION_MAJOR);
229 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MINOR,
230 EFA_COMMON_SPEC_VERSION_MINOR);
231 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_INTREE, 1);
232 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_GDR, 0);
234 efa_com_set_feature_ex(&dev->edev, &resp, &cmd, EFA_ADMIN_HOST_INFO,
235 hinf_dma, bufsz);
237 dma_free_coherent(&dev->pdev->dev, bufsz, hinf, hinf_dma);
240 static const struct ib_device_ops efa_dev_ops = {
241 .owner = THIS_MODULE,
242 .driver_id = RDMA_DRIVER_EFA,
243 .uverbs_abi_ver = EFA_UVERBS_ABI_VERSION,
245 .alloc_hw_stats = efa_alloc_hw_stats,
246 .alloc_pd = efa_alloc_pd,
247 .alloc_ucontext = efa_alloc_ucontext,
248 .create_cq = efa_create_cq,
249 .create_qp = efa_create_qp,
250 .create_user_ah = efa_create_ah,
251 .dealloc_pd = efa_dealloc_pd,
252 .dealloc_ucontext = efa_dealloc_ucontext,
253 .dereg_mr = efa_dereg_mr,
254 .destroy_ah = efa_destroy_ah,
255 .destroy_cq = efa_destroy_cq,
256 .destroy_qp = efa_destroy_qp,
257 .get_hw_stats = efa_get_hw_stats,
258 .get_link_layer = efa_port_link_layer,
259 .get_port_immutable = efa_get_port_immutable,
260 .mmap = efa_mmap,
261 .mmap_free = efa_mmap_free,
262 .modify_qp = efa_modify_qp,
263 .query_device = efa_query_device,
264 .query_gid = efa_query_gid,
265 .query_pkey = efa_query_pkey,
266 .query_port = efa_query_port,
267 .query_qp = efa_query_qp,
268 .reg_user_mr = efa_reg_mr,
270 INIT_RDMA_OBJ_SIZE(ib_ah, efa_ah, ibah),
271 INIT_RDMA_OBJ_SIZE(ib_cq, efa_cq, ibcq),
272 INIT_RDMA_OBJ_SIZE(ib_pd, efa_pd, ibpd),
273 INIT_RDMA_OBJ_SIZE(ib_ucontext, efa_ucontext, ibucontext),
276 static int efa_ib_device_add(struct efa_dev *dev)
278 struct efa_com_get_hw_hints_result hw_hints;
279 struct pci_dev *pdev = dev->pdev;
280 int err;
282 efa_stats_init(dev);
284 err = efa_com_get_device_attr(&dev->edev, &dev->dev_attr);
285 if (err)
286 return err;
288 dev_dbg(&dev->pdev->dev, "Doorbells bar (%d)\n", dev->dev_attr.db_bar);
289 err = efa_request_doorbell_bar(dev);
290 if (err)
291 return err;
293 err = efa_com_get_hw_hints(&dev->edev, &hw_hints);
294 if (err)
295 goto err_release_doorbell_bar;
297 efa_update_hw_hints(dev, &hw_hints);
299 /* Try to enable all the available aenq groups */
300 err = efa_com_set_aenq_config(&dev->edev, EFA_AENQ_ENABLED_GROUPS);
301 if (err)
302 goto err_release_doorbell_bar;
304 efa_set_host_info(dev);
306 dev->ibdev.node_type = RDMA_NODE_UNSPECIFIED;
307 dev->ibdev.phys_port_cnt = 1;
308 dev->ibdev.num_comp_vectors = 1;
309 dev->ibdev.dev.parent = &pdev->dev;
311 ib_set_device_ops(&dev->ibdev, &efa_dev_ops);
313 err = ib_register_device(&dev->ibdev, "efa_%d", &pdev->dev);
314 if (err)
315 goto err_release_doorbell_bar;
317 ibdev_info(&dev->ibdev, "IB device registered\n");
319 return 0;
321 err_release_doorbell_bar:
322 efa_release_doorbell_bar(dev);
323 return err;
326 static void efa_ib_device_remove(struct efa_dev *dev)
328 efa_com_dev_reset(&dev->edev, EFA_REGS_RESET_NORMAL);
329 ibdev_info(&dev->ibdev, "Unregister ib device\n");
330 ib_unregister_device(&dev->ibdev);
331 efa_release_doorbell_bar(dev);
334 static void efa_disable_msix(struct efa_dev *dev)
336 pci_free_irq_vectors(dev->pdev);
339 static int efa_enable_msix(struct efa_dev *dev)
341 int msix_vecs, irq_num;
343 /* Reserve the max msix vectors we might need */
344 msix_vecs = EFA_NUM_MSIX_VEC;
345 dev_dbg(&dev->pdev->dev, "Trying to enable MSI-X, vectors %d\n",
346 msix_vecs);
348 dev->admin_msix_vector_idx = EFA_MGMNT_MSIX_VEC_IDX;
349 irq_num = pci_alloc_irq_vectors(dev->pdev, msix_vecs,
350 msix_vecs, PCI_IRQ_MSIX);
352 if (irq_num < 0) {
353 dev_err(&dev->pdev->dev, "Failed to enable MSI-X. irq_num %d\n",
354 irq_num);
355 return -ENOSPC;
358 if (irq_num != msix_vecs) {
359 dev_err(&dev->pdev->dev,
360 "Allocated %d MSI-X (out of %d requested)\n",
361 irq_num, msix_vecs);
362 return -ENOSPC;
365 return 0;
368 static int efa_device_init(struct efa_com_dev *edev, struct pci_dev *pdev)
370 int dma_width;
371 int err;
373 err = efa_com_dev_reset(edev, EFA_REGS_RESET_NORMAL);
374 if (err)
375 return err;
377 err = efa_com_validate_version(edev);
378 if (err)
379 return err;
381 dma_width = efa_com_get_dma_width(edev);
382 if (dma_width < 0) {
383 err = dma_width;
384 return err;
387 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(dma_width));
388 if (err) {
389 dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", err);
390 return err;
393 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
394 return 0;
397 static struct efa_dev *efa_probe_device(struct pci_dev *pdev)
399 struct efa_com_dev *edev;
400 struct efa_dev *dev;
401 int bars;
402 int err;
404 err = pci_enable_device_mem(pdev);
405 if (err) {
406 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
407 return ERR_PTR(err);
410 pci_set_master(pdev);
412 dev = ib_alloc_device(efa_dev, ibdev);
413 if (!dev) {
414 dev_err(&pdev->dev, "Device alloc failed\n");
415 err = -ENOMEM;
416 goto err_disable_device;
419 pci_set_drvdata(pdev, dev);
420 edev = &dev->edev;
421 edev->efa_dev = dev;
422 edev->dmadev = &pdev->dev;
423 dev->pdev = pdev;
425 bars = pci_select_bars(pdev, IORESOURCE_MEM) & EFA_BASE_BAR_MASK;
426 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
427 if (err) {
428 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
429 err);
430 goto err_ibdev_destroy;
433 dev->reg_bar_addr = pci_resource_start(pdev, EFA_REG_BAR);
434 dev->reg_bar_len = pci_resource_len(pdev, EFA_REG_BAR);
435 dev->mem_bar_addr = pci_resource_start(pdev, EFA_MEM_BAR);
436 dev->mem_bar_len = pci_resource_len(pdev, EFA_MEM_BAR);
438 edev->reg_bar = devm_ioremap(&pdev->dev,
439 dev->reg_bar_addr,
440 dev->reg_bar_len);
441 if (!edev->reg_bar) {
442 dev_err(&pdev->dev, "Failed to remap register bar\n");
443 err = -EFAULT;
444 goto err_release_bars;
447 err = efa_com_mmio_reg_read_init(edev);
448 if (err) {
449 dev_err(&pdev->dev, "Failed to init readless MMIO\n");
450 goto err_iounmap;
453 err = efa_device_init(edev, pdev);
454 if (err) {
455 dev_err(&pdev->dev, "EFA device init failed\n");
456 if (err == -ETIME)
457 err = -EPROBE_DEFER;
458 goto err_reg_read_destroy;
461 err = efa_enable_msix(dev);
462 if (err)
463 goto err_reg_read_destroy;
465 edev->aq.msix_vector_idx = dev->admin_msix_vector_idx;
466 edev->aenq.msix_vector_idx = dev->admin_msix_vector_idx;
468 err = efa_set_mgmnt_irq(dev);
469 if (err)
470 goto err_disable_msix;
472 err = efa_com_admin_init(edev, &aenq_handlers);
473 if (err)
474 goto err_free_mgmnt_irq;
476 return dev;
478 err_free_mgmnt_irq:
479 efa_free_mgmnt_irq(dev);
480 err_disable_msix:
481 efa_disable_msix(dev);
482 err_reg_read_destroy:
483 efa_com_mmio_reg_read_destroy(edev);
484 err_iounmap:
485 devm_iounmap(&pdev->dev, edev->reg_bar);
486 err_release_bars:
487 efa_release_bars(dev, EFA_BASE_BAR_MASK);
488 err_ibdev_destroy:
489 ib_dealloc_device(&dev->ibdev);
490 err_disable_device:
491 pci_disable_device(pdev);
492 return ERR_PTR(err);
495 static void efa_remove_device(struct pci_dev *pdev)
497 struct efa_dev *dev = pci_get_drvdata(pdev);
498 struct efa_com_dev *edev;
500 edev = &dev->edev;
501 efa_com_admin_destroy(edev);
502 efa_free_mgmnt_irq(dev);
503 efa_disable_msix(dev);
504 efa_com_mmio_reg_read_destroy(edev);
505 devm_iounmap(&pdev->dev, edev->reg_bar);
506 efa_release_bars(dev, EFA_BASE_BAR_MASK);
507 ib_dealloc_device(&dev->ibdev);
508 pci_disable_device(pdev);
511 static int efa_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
513 struct efa_dev *dev;
514 int err;
516 dev = efa_probe_device(pdev);
517 if (IS_ERR(dev))
518 return PTR_ERR(dev);
520 err = efa_ib_device_add(dev);
521 if (err)
522 goto err_remove_device;
524 return 0;
526 err_remove_device:
527 efa_remove_device(pdev);
528 return err;
531 static void efa_remove(struct pci_dev *pdev)
533 struct efa_dev *dev = pci_get_drvdata(pdev);
535 efa_ib_device_remove(dev);
536 efa_remove_device(pdev);
539 static struct pci_driver efa_pci_driver = {
540 .name = DRV_MODULE_NAME,
541 .id_table = efa_pci_tbl,
542 .probe = efa_probe,
543 .remove = efa_remove,
546 module_pci_driver(efa_pci_driver);