1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
2 /*
3 * Copyright 2018-2022 Amazon.com, Inc. or its affiliates. All rights reserved.
4 */
5
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <linux/utsname.h>
9 #include <linux/version.h>
10
11 #include <rdma/ib_user_verbs.h>
12
13 #include "efa.h"
14
15 #define PCI_DEV_ID_EFA0_VF 0xefa0
16 #define PCI_DEV_ID_EFA1_VF 0xefa1
17 #define PCI_DEV_ID_EFA2_VF 0xefa2
18
19 static const struct pci_device_id efa_pci_tbl[] = {
20 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA0_VF) },
21 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA1_VF) },
22 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA2_VF) },
23 { }
24 };
25
26 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
27 MODULE_LICENSE("Dual BSD/GPL");
28 MODULE_DESCRIPTION(DEVICE_NAME);
29 MODULE_DEVICE_TABLE(pci, efa_pci_tbl);
30
31 #define EFA_REG_BAR 0
32 #define EFA_MEM_BAR 2
33 #define EFA_BASE_BAR_MASK (BIT(EFA_REG_BAR) | BIT(EFA_MEM_BAR))
34
35 #define EFA_AENQ_ENABLED_GROUPS \
36 (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \
37 BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE))
38
39 /* This handler will called for unknown event group or unimplemented handlers */
unimplemented_aenq_handler(void * data,struct efa_admin_aenq_entry * aenq_e)40 static void unimplemented_aenq_handler(void *data,
41 struct efa_admin_aenq_entry *aenq_e)
42 {
43 struct efa_dev *dev = (struct efa_dev *)data;
44
45 ibdev_err(&dev->ibdev,
46 "Unknown event was received or event with unimplemented handler\n");
47 }
48
efa_keep_alive(void * data,struct efa_admin_aenq_entry * aenq_e)49 static void efa_keep_alive(void *data, struct efa_admin_aenq_entry *aenq_e)
50 {
51 struct efa_dev *dev = (struct efa_dev *)data;
52
53 atomic64_inc(&dev->stats.keep_alive_rcvd);
54 }
55
56 static struct efa_aenq_handlers aenq_handlers = {
57 .handlers = {
58 [EFA_ADMIN_KEEP_ALIVE] = efa_keep_alive,
59 },
60 .unimplemented_handler = unimplemented_aenq_handler
61 };
62
efa_release_bars(struct efa_dev * dev,int bars_mask)63 static void efa_release_bars(struct efa_dev *dev, int bars_mask)
64 {
65 struct pci_dev *pdev = dev->pdev;
66 int release_bars;
67
68 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & bars_mask;
69 pci_release_selected_regions(pdev, release_bars);
70 }
71
efa_process_comp_eqe(struct efa_dev * dev,struct efa_admin_eqe * eqe)72 static void efa_process_comp_eqe(struct efa_dev *dev, struct efa_admin_eqe *eqe)
73 {
74 u16 cqn = eqe->u.comp_event.cqn;
75 struct efa_cq *cq;
76
77 /* Safe to load as we're in irq and removal calls synchronize_irq() */
78 cq = xa_load(&dev->cqs_xa, cqn);
79 if (unlikely(!cq)) {
80 ibdev_err_ratelimited(&dev->ibdev,
81 "Completion event on non-existent CQ[%u]",
82 cqn);
83 return;
84 }
85
86 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
87 }
88
efa_process_eqe(struct efa_com_eq * eeq,struct efa_admin_eqe * eqe)89 static void efa_process_eqe(struct efa_com_eq *eeq, struct efa_admin_eqe *eqe)
90 {
91 struct efa_dev *dev = container_of(eeq->edev, struct efa_dev, edev);
92
93 if (likely(EFA_GET(&eqe->common, EFA_ADMIN_EQE_EVENT_TYPE) ==
94 EFA_ADMIN_EQE_EVENT_TYPE_COMPLETION))
95 efa_process_comp_eqe(dev, eqe);
96 else
97 ibdev_err_ratelimited(&dev->ibdev,
98 "Unknown event type received %lu",
99 EFA_GET(&eqe->common,
100 EFA_ADMIN_EQE_EVENT_TYPE));
101 }
102
efa_intr_msix_comp(int irq,void * data)103 static irqreturn_t efa_intr_msix_comp(int irq, void *data)
104 {
105 struct efa_eq *eq = data;
106 struct efa_com_dev *edev = eq->eeq.edev;
107
108 efa_com_eq_comp_intr_handler(edev, &eq->eeq);
109
110 return IRQ_HANDLED;
111 }
112
efa_intr_msix_mgmnt(int irq,void * data)113 static irqreturn_t efa_intr_msix_mgmnt(int irq, void *data)
114 {
115 struct efa_dev *dev = data;
116
117 efa_com_admin_q_comp_intr_handler(&dev->edev);
118 efa_com_aenq_intr_handler(&dev->edev, data);
119
120 return IRQ_HANDLED;
121 }
122
efa_request_irq(struct efa_dev * dev,struct efa_irq * irq)123 static int efa_request_irq(struct efa_dev *dev, struct efa_irq *irq)
124 {
125 int err;
126
127 err = request_irq(irq->irqn, irq->handler, 0, irq->name, irq->data);
128 if (err) {
129 dev_err(&dev->pdev->dev, "Failed to request irq %s (%d)\n",
130 irq->name, err);
131 return err;
132 }
133
134 irq_set_affinity_hint(irq->irqn, &irq->affinity_hint_mask);
135
136 return 0;
137 }
138
efa_setup_comp_irq(struct efa_dev * dev,struct efa_eq * eq,int vector)139 static void efa_setup_comp_irq(struct efa_dev *dev, struct efa_eq *eq,
140 int vector)
141 {
142 u32 cpu;
143
144 cpu = vector - EFA_COMP_EQS_VEC_BASE;
145 snprintf(eq->irq.name, EFA_IRQNAME_SIZE, "efa-comp%d@pci:%s", cpu,
146 pci_name(dev->pdev));
147 eq->irq.handler = efa_intr_msix_comp;
148 eq->irq.data = eq;
149 eq->irq.vector = vector;
150 eq->irq.irqn = pci_irq_vector(dev->pdev, vector);
151 cpumask_set_cpu(cpu, &eq->irq.affinity_hint_mask);
152 }
153
efa_free_irq(struct efa_dev * dev,struct efa_irq * irq)154 static void efa_free_irq(struct efa_dev *dev, struct efa_irq *irq)
155 {
156 irq_set_affinity_hint(irq->irqn, NULL);
157 free_irq(irq->irqn, irq->data);
158 }
159
efa_setup_mgmnt_irq(struct efa_dev * dev)160 static void efa_setup_mgmnt_irq(struct efa_dev *dev)
161 {
162 u32 cpu;
163
164 snprintf(dev->admin_irq.name, EFA_IRQNAME_SIZE,
165 "efa-mgmnt@pci:%s", pci_name(dev->pdev));
166 dev->admin_irq.handler = efa_intr_msix_mgmnt;
167 dev->admin_irq.data = dev;
168 dev->admin_irq.vector = dev->admin_msix_vector_idx;
169 dev->admin_irq.irqn = pci_irq_vector(dev->pdev,
170 dev->admin_msix_vector_idx);
171 cpu = cpumask_first(cpu_online_mask);
172 cpumask_set_cpu(cpu,
173 &dev->admin_irq.affinity_hint_mask);
174 dev_info(&dev->pdev->dev, "Setup irq:%d name:%s\n",
175 dev->admin_irq.irqn,
176 dev->admin_irq.name);
177 }
178
efa_set_mgmnt_irq(struct efa_dev * dev)179 static int efa_set_mgmnt_irq(struct efa_dev *dev)
180 {
181 efa_setup_mgmnt_irq(dev);
182
183 return efa_request_irq(dev, &dev->admin_irq);
184 }
185
efa_request_doorbell_bar(struct efa_dev * dev)186 static int efa_request_doorbell_bar(struct efa_dev *dev)
187 {
188 u8 db_bar_idx = dev->dev_attr.db_bar;
189 struct pci_dev *pdev = dev->pdev;
190 int bars;
191 int err;
192
193 if (!(BIT(db_bar_idx) & EFA_BASE_BAR_MASK)) {
194 bars = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(db_bar_idx);
195
196 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
197 if (err) {
198 dev_err(&dev->pdev->dev,
199 "pci_request_selected_regions for bar %d failed %d\n",
200 db_bar_idx, err);
201 return err;
202 }
203 }
204
205 dev->db_bar_addr = pci_resource_start(dev->pdev, db_bar_idx);
206 dev->db_bar_len = pci_resource_len(dev->pdev, db_bar_idx);
207
208 return 0;
209 }
210
efa_release_doorbell_bar(struct efa_dev * dev)211 static void efa_release_doorbell_bar(struct efa_dev *dev)
212 {
213 if (!(BIT(dev->dev_attr.db_bar) & EFA_BASE_BAR_MASK))
214 efa_release_bars(dev, BIT(dev->dev_attr.db_bar));
215 }
216
efa_update_hw_hints(struct efa_dev * dev,struct efa_com_get_hw_hints_result * hw_hints)217 static void efa_update_hw_hints(struct efa_dev *dev,
218 struct efa_com_get_hw_hints_result *hw_hints)
219 {
220 struct efa_com_dev *edev = &dev->edev;
221
222 if (hw_hints->mmio_read_timeout)
223 edev->mmio_read.mmio_read_timeout =
224 hw_hints->mmio_read_timeout * 1000;
225
226 if (hw_hints->poll_interval)
227 edev->aq.poll_interval = hw_hints->poll_interval;
228
229 if (hw_hints->admin_completion_timeout)
230 edev->aq.completion_timeout =
231 hw_hints->admin_completion_timeout;
232 }
233
efa_stats_init(struct efa_dev * dev)234 static void efa_stats_init(struct efa_dev *dev)
235 {
236 atomic64_t *s = (atomic64_t *)&dev->stats;
237 int i;
238
239 for (i = 0; i < sizeof(dev->stats) / sizeof(*s); i++, s++)
240 atomic64_set(s, 0);
241 }
242
efa_set_host_info(struct efa_dev * dev)243 static void efa_set_host_info(struct efa_dev *dev)
244 {
245 struct efa_admin_set_feature_resp resp = {};
246 struct efa_admin_set_feature_cmd cmd = {};
247 struct efa_admin_host_info *hinf;
248 u32 bufsz = sizeof(*hinf);
249 dma_addr_t hinf_dma;
250
251 if (!efa_com_check_supported_feature_id(&dev->edev,
252 EFA_ADMIN_HOST_INFO))
253 return;
254
255 /* Failures in host info set shall not disturb probe */
256 hinf = dma_alloc_coherent(&dev->pdev->dev, bufsz, &hinf_dma,
257 GFP_KERNEL);
258 if (!hinf)
259 return;
260
261 strscpy(hinf->os_dist_str, utsname()->release,
262 sizeof(hinf->os_dist_str));
263 hinf->os_type = EFA_ADMIN_OS_LINUX;
264 strscpy(hinf->kernel_ver_str, utsname()->version,
265 sizeof(hinf->kernel_ver_str));
266 hinf->kernel_ver = LINUX_VERSION_CODE;
267 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MAJOR, 0);
268 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MINOR, 0);
269 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_SUB_MINOR, 0);
270 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MODULE_TYPE, 0);
271 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_BUS, dev->pdev->bus->number);
272 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_DEVICE,
273 PCI_SLOT(dev->pdev->devfn));
274 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_FUNCTION,
275 PCI_FUNC(dev->pdev->devfn));
276 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MAJOR,
277 EFA_COMMON_SPEC_VERSION_MAJOR);
278 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MINOR,
279 EFA_COMMON_SPEC_VERSION_MINOR);
280 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_INTREE, 1);
281 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_GDR, 0);
282
283 efa_com_set_feature_ex(&dev->edev, &resp, &cmd, EFA_ADMIN_HOST_INFO,
284 hinf_dma, bufsz);
285
286 dma_free_coherent(&dev->pdev->dev, bufsz, hinf, hinf_dma);
287 }
288
efa_destroy_eq(struct efa_dev * dev,struct efa_eq * eq)289 static void efa_destroy_eq(struct efa_dev *dev, struct efa_eq *eq)
290 {
291 efa_com_eq_destroy(&dev->edev, &eq->eeq);
292 efa_free_irq(dev, &eq->irq);
293 }
294
efa_create_eq(struct efa_dev * dev,struct efa_eq * eq,u8 msix_vec)295 static int efa_create_eq(struct efa_dev *dev, struct efa_eq *eq, u8 msix_vec)
296 {
297 int err;
298
299 efa_setup_comp_irq(dev, eq, msix_vec);
300 err = efa_request_irq(dev, &eq->irq);
301 if (err)
302 return err;
303
304 err = efa_com_eq_init(&dev->edev, &eq->eeq, efa_process_eqe,
305 dev->dev_attr.max_eq_depth, msix_vec);
306 if (err)
307 goto err_free_comp_irq;
308
309 return 0;
310
311 err_free_comp_irq:
312 efa_free_irq(dev, &eq->irq);
313 return err;
314 }
315
efa_create_eqs(struct efa_dev * dev)316 static int efa_create_eqs(struct efa_dev *dev)
317 {
318 unsigned int neqs = dev->dev_attr.max_eq;
319 int err;
320 int i;
321
322 neqs = min_t(unsigned int, neqs, num_online_cpus());
323 dev->neqs = neqs;
324 dev->eqs = kcalloc(neqs, sizeof(*dev->eqs), GFP_KERNEL);
325 if (!dev->eqs)
326 return -ENOMEM;
327
328 for (i = 0; i < neqs; i++) {
329 err = efa_create_eq(dev, &dev->eqs[i],
330 i + EFA_COMP_EQS_VEC_BASE);
331 if (err)
332 goto err_destroy_eqs;
333 }
334
335 return 0;
336
337 err_destroy_eqs:
338 for (i--; i >= 0; i--)
339 efa_destroy_eq(dev, &dev->eqs[i]);
340 kfree(dev->eqs);
341
342 return err;
343 }
344
efa_destroy_eqs(struct efa_dev * dev)345 static void efa_destroy_eqs(struct efa_dev *dev)
346 {
347 int i;
348
349 for (i = 0; i < dev->neqs; i++)
350 efa_destroy_eq(dev, &dev->eqs[i]);
351
352 kfree(dev->eqs);
353 }
354
355 static const struct ib_device_ops efa_dev_ops = {
356 .owner = THIS_MODULE,
357 .driver_id = RDMA_DRIVER_EFA,
358 .uverbs_abi_ver = EFA_UVERBS_ABI_VERSION,
359
360 .alloc_hw_port_stats = efa_alloc_hw_port_stats,
361 .alloc_hw_device_stats = efa_alloc_hw_device_stats,
362 .alloc_pd = efa_alloc_pd,
363 .alloc_ucontext = efa_alloc_ucontext,
364 .create_cq = efa_create_cq,
365 .create_qp = efa_create_qp,
366 .create_user_ah = efa_create_ah,
367 .dealloc_pd = efa_dealloc_pd,
368 .dealloc_ucontext = efa_dealloc_ucontext,
369 .dereg_mr = efa_dereg_mr,
370 .destroy_ah = efa_destroy_ah,
371 .destroy_cq = efa_destroy_cq,
372 .destroy_qp = efa_destroy_qp,
373 .get_hw_stats = efa_get_hw_stats,
374 .get_link_layer = efa_port_link_layer,
375 .get_port_immutable = efa_get_port_immutable,
376 .mmap = efa_mmap,
377 .mmap_free = efa_mmap_free,
378 .modify_qp = efa_modify_qp,
379 .query_device = efa_query_device,
380 .query_gid = efa_query_gid,
381 .query_pkey = efa_query_pkey,
382 .query_port = efa_query_port,
383 .query_qp = efa_query_qp,
384 .reg_user_mr = efa_reg_mr,
385 .reg_user_mr_dmabuf = efa_reg_user_mr_dmabuf,
386
387 INIT_RDMA_OBJ_SIZE(ib_ah, efa_ah, ibah),
388 INIT_RDMA_OBJ_SIZE(ib_cq, efa_cq, ibcq),
389 INIT_RDMA_OBJ_SIZE(ib_pd, efa_pd, ibpd),
390 INIT_RDMA_OBJ_SIZE(ib_qp, efa_qp, ibqp),
391 INIT_RDMA_OBJ_SIZE(ib_ucontext, efa_ucontext, ibucontext),
392 };
393
efa_ib_device_add(struct efa_dev * dev)394 static int efa_ib_device_add(struct efa_dev *dev)
395 {
396 struct efa_com_get_hw_hints_result hw_hints;
397 struct pci_dev *pdev = dev->pdev;
398 int err;
399
400 efa_stats_init(dev);
401
402 err = efa_com_get_device_attr(&dev->edev, &dev->dev_attr);
403 if (err)
404 return err;
405
406 dev_dbg(&dev->pdev->dev, "Doorbells bar (%d)\n", dev->dev_attr.db_bar);
407 err = efa_request_doorbell_bar(dev);
408 if (err)
409 return err;
410
411 err = efa_com_get_hw_hints(&dev->edev, &hw_hints);
412 if (err)
413 goto err_release_doorbell_bar;
414
415 efa_update_hw_hints(dev, &hw_hints);
416
417 /* Try to enable all the available aenq groups */
418 err = efa_com_set_aenq_config(&dev->edev, EFA_AENQ_ENABLED_GROUPS);
419 if (err)
420 goto err_release_doorbell_bar;
421
422 err = efa_create_eqs(dev);
423 if (err)
424 goto err_release_doorbell_bar;
425
426 efa_set_host_info(dev);
427
428 dev->ibdev.node_type = RDMA_NODE_UNSPECIFIED;
429 dev->ibdev.phys_port_cnt = 1;
430 dev->ibdev.num_comp_vectors = dev->neqs ?: 1;
431 dev->ibdev.dev.parent = &pdev->dev;
432
433 ib_set_device_ops(&dev->ibdev, &efa_dev_ops);
434
435 err = ib_register_device(&dev->ibdev, "efa_%d", &pdev->dev);
436 if (err)
437 goto err_destroy_eqs;
438
439 ibdev_info(&dev->ibdev, "IB device registered\n");
440
441 return 0;
442
443 err_destroy_eqs:
444 efa_destroy_eqs(dev);
445 err_release_doorbell_bar:
446 efa_release_doorbell_bar(dev);
447 return err;
448 }
449
efa_ib_device_remove(struct efa_dev * dev)450 static void efa_ib_device_remove(struct efa_dev *dev)
451 {
452 ibdev_info(&dev->ibdev, "Unregister ib device\n");
453 ib_unregister_device(&dev->ibdev);
454 efa_destroy_eqs(dev);
455 efa_com_dev_reset(&dev->edev, EFA_REGS_RESET_NORMAL);
456 efa_release_doorbell_bar(dev);
457 }
458
efa_disable_msix(struct efa_dev * dev)459 static void efa_disable_msix(struct efa_dev *dev)
460 {
461 pci_free_irq_vectors(dev->pdev);
462 }
463
efa_enable_msix(struct efa_dev * dev)464 static int efa_enable_msix(struct efa_dev *dev)
465 {
466 int msix_vecs, irq_num;
467
468 /*
469 * Reserve the max msix vectors we might need, one vector is reserved
470 * for admin.
471 */
472 msix_vecs = min_t(int, pci_msix_vec_count(dev->pdev),
473 num_online_cpus() + 1);
474 dev_dbg(&dev->pdev->dev, "Trying to enable MSI-X, vectors %d\n",
475 msix_vecs);
476
477 dev->admin_msix_vector_idx = EFA_MGMNT_MSIX_VEC_IDX;
478 irq_num = pci_alloc_irq_vectors(dev->pdev, msix_vecs,
479 msix_vecs, PCI_IRQ_MSIX);
480
481 if (irq_num < 0) {
482 dev_err(&dev->pdev->dev, "Failed to enable MSI-X. irq_num %d\n",
483 irq_num);
484 return -ENOSPC;
485 }
486
487 if (irq_num != msix_vecs) {
488 efa_disable_msix(dev);
489 dev_err(&dev->pdev->dev,
490 "Allocated %d MSI-X (out of %d requested)\n",
491 irq_num, msix_vecs);
492 return -ENOSPC;
493 }
494
495 return 0;
496 }
497
efa_device_init(struct efa_com_dev * edev,struct pci_dev * pdev)498 static int efa_device_init(struct efa_com_dev *edev, struct pci_dev *pdev)
499 {
500 int dma_width;
501 int err;
502
503 err = efa_com_dev_reset(edev, EFA_REGS_RESET_NORMAL);
504 if (err)
505 return err;
506
507 err = efa_com_validate_version(edev);
508 if (err)
509 return err;
510
511 dma_width = efa_com_get_dma_width(edev);
512 if (dma_width < 0) {
513 err = dma_width;
514 return err;
515 }
516
517 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(dma_width));
518 if (err) {
519 dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", err);
520 return err;
521 }
522
523 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
524 return 0;
525 }
526
efa_probe_device(struct pci_dev * pdev)527 static struct efa_dev *efa_probe_device(struct pci_dev *pdev)
528 {
529 struct efa_com_dev *edev;
530 struct efa_dev *dev;
531 int bars;
532 int err;
533
534 err = pci_enable_device_mem(pdev);
535 if (err) {
536 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
537 return ERR_PTR(err);
538 }
539
540 pci_set_master(pdev);
541
542 dev = ib_alloc_device(efa_dev, ibdev);
543 if (!dev) {
544 dev_err(&pdev->dev, "Device alloc failed\n");
545 err = -ENOMEM;
546 goto err_disable_device;
547 }
548
549 pci_set_drvdata(pdev, dev);
550 edev = &dev->edev;
551 edev->efa_dev = dev;
552 edev->dmadev = &pdev->dev;
553 dev->pdev = pdev;
554 xa_init(&dev->cqs_xa);
555
556 bars = pci_select_bars(pdev, IORESOURCE_MEM) & EFA_BASE_BAR_MASK;
557 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
558 if (err) {
559 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
560 err);
561 goto err_ibdev_destroy;
562 }
563
564 dev->reg_bar_addr = pci_resource_start(pdev, EFA_REG_BAR);
565 dev->reg_bar_len = pci_resource_len(pdev, EFA_REG_BAR);
566 dev->mem_bar_addr = pci_resource_start(pdev, EFA_MEM_BAR);
567 dev->mem_bar_len = pci_resource_len(pdev, EFA_MEM_BAR);
568
569 edev->reg_bar = devm_ioremap(&pdev->dev,
570 dev->reg_bar_addr,
571 dev->reg_bar_len);
572 if (!edev->reg_bar) {
573 dev_err(&pdev->dev, "Failed to remap register bar\n");
574 err = -EFAULT;
575 goto err_release_bars;
576 }
577
578 err = efa_com_mmio_reg_read_init(edev);
579 if (err) {
580 dev_err(&pdev->dev, "Failed to init readless MMIO\n");
581 goto err_iounmap;
582 }
583
584 err = efa_device_init(edev, pdev);
585 if (err) {
586 dev_err(&pdev->dev, "EFA device init failed\n");
587 if (err == -ETIME)
588 err = -EPROBE_DEFER;
589 goto err_reg_read_destroy;
590 }
591
592 err = efa_enable_msix(dev);
593 if (err)
594 goto err_reg_read_destroy;
595
596 edev->aq.msix_vector_idx = dev->admin_msix_vector_idx;
597 edev->aenq.msix_vector_idx = dev->admin_msix_vector_idx;
598
599 err = efa_set_mgmnt_irq(dev);
600 if (err)
601 goto err_disable_msix;
602
603 err = efa_com_admin_init(edev, &aenq_handlers);
604 if (err)
605 goto err_free_mgmnt_irq;
606
607 return dev;
608
609 err_free_mgmnt_irq:
610 efa_free_irq(dev, &dev->admin_irq);
611 err_disable_msix:
612 efa_disable_msix(dev);
613 err_reg_read_destroy:
614 efa_com_mmio_reg_read_destroy(edev);
615 err_iounmap:
616 devm_iounmap(&pdev->dev, edev->reg_bar);
617 err_release_bars:
618 efa_release_bars(dev, EFA_BASE_BAR_MASK);
619 err_ibdev_destroy:
620 ib_dealloc_device(&dev->ibdev);
621 err_disable_device:
622 pci_disable_device(pdev);
623 return ERR_PTR(err);
624 }
625
efa_remove_device(struct pci_dev * pdev)626 static void efa_remove_device(struct pci_dev *pdev)
627 {
628 struct efa_dev *dev = pci_get_drvdata(pdev);
629 struct efa_com_dev *edev;
630
631 edev = &dev->edev;
632 efa_com_admin_destroy(edev);
633 efa_free_irq(dev, &dev->admin_irq);
634 efa_disable_msix(dev);
635 efa_com_mmio_reg_read_destroy(edev);
636 devm_iounmap(&pdev->dev, edev->reg_bar);
637 efa_release_bars(dev, EFA_BASE_BAR_MASK);
638 xa_destroy(&dev->cqs_xa);
639 ib_dealloc_device(&dev->ibdev);
640 pci_disable_device(pdev);
641 }
642
efa_probe(struct pci_dev * pdev,const struct pci_device_id * ent)643 static int efa_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
644 {
645 struct efa_dev *dev;
646 int err;
647
648 dev = efa_probe_device(pdev);
649 if (IS_ERR(dev))
650 return PTR_ERR(dev);
651
652 err = efa_ib_device_add(dev);
653 if (err)
654 goto err_remove_device;
655
656 return 0;
657
658 err_remove_device:
659 efa_remove_device(pdev);
660 return err;
661 }
662
efa_remove(struct pci_dev * pdev)663 static void efa_remove(struct pci_dev *pdev)
664 {
665 struct efa_dev *dev = pci_get_drvdata(pdev);
666
667 efa_ib_device_remove(dev);
668 efa_remove_device(pdev);
669 }
670
671 static struct pci_driver efa_pci_driver = {
672 .name = DRV_MODULE_NAME,
673 .id_table = efa_pci_tbl,
674 .probe = efa_probe,
675 .remove = efa_remove,
676 };
677
678 module_pci_driver(efa_pci_driver);
679