This source file includes following definitions.
- efa_update_network_attr
- unimplemented_aenq_handler
- efa_keep_alive
- efa_release_bars
- efa_intr_msix_mgmnt
- efa_request_mgmnt_irq
- efa_setup_mgmnt_irq
- efa_free_mgmnt_irq
- efa_set_mgmnt_irq
- efa_request_doorbell_bar
- efa_release_doorbell_bar
- efa_update_hw_hints
- efa_stats_init
- efa_ib_device_add
- efa_ib_device_remove
- efa_disable_msix
- efa_enable_msix
- efa_device_init
- efa_probe_device
- efa_remove_device
- efa_probe
- efa_remove
1
2
3
4
5
6 #include <linux/module.h>
7 #include <linux/pci.h>
8
9 #include <rdma/ib_user_verbs.h>
10
11 #include "efa.h"
12
13 #define PCI_DEV_ID_EFA_VF 0xefa0
14
15 static const struct pci_device_id efa_pci_tbl[] = {
16 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA_VF) },
17 { }
18 };
19
20 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
21 MODULE_LICENSE("Dual BSD/GPL");
22 MODULE_DESCRIPTION(DEVICE_NAME);
23 MODULE_DEVICE_TABLE(pci, efa_pci_tbl);
24
25 #define EFA_REG_BAR 0
26 #define EFA_MEM_BAR 2
27 #define EFA_BASE_BAR_MASK (BIT(EFA_REG_BAR) | BIT(EFA_MEM_BAR))
28
29 #define EFA_AENQ_ENABLED_GROUPS \
30 (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \
31 BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE))
32
33 static void efa_update_network_attr(struct efa_dev *dev,
34 struct efa_com_get_network_attr_result *network_attr)
35 {
36 memcpy(dev->addr, network_attr->addr, sizeof(network_attr->addr));
37 dev->mtu = network_attr->mtu;
38
39 dev_dbg(&dev->pdev->dev, "Full address %pI6\n", dev->addr);
40 }
41
42
43 static void unimplemented_aenq_handler(void *data,
44 struct efa_admin_aenq_entry *aenq_e)
45 {
46 struct efa_dev *dev = (struct efa_dev *)data;
47
48 ibdev_err(&dev->ibdev,
49 "Unknown event was received or event with unimplemented handler\n");
50 }
51
52 static void efa_keep_alive(void *data, struct efa_admin_aenq_entry *aenq_e)
53 {
54 struct efa_dev *dev = (struct efa_dev *)data;
55
56 atomic64_inc(&dev->stats.keep_alive_rcvd);
57 }
58
59 static struct efa_aenq_handlers aenq_handlers = {
60 .handlers = {
61 [EFA_ADMIN_KEEP_ALIVE] = efa_keep_alive,
62 },
63 .unimplemented_handler = unimplemented_aenq_handler
64 };
65
66 static void efa_release_bars(struct efa_dev *dev, int bars_mask)
67 {
68 struct pci_dev *pdev = dev->pdev;
69 int release_bars;
70
71 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & bars_mask;
72 pci_release_selected_regions(pdev, release_bars);
73 }
74
75 static irqreturn_t efa_intr_msix_mgmnt(int irq, void *data)
76 {
77 struct efa_dev *dev = data;
78
79 efa_com_admin_q_comp_intr_handler(&dev->edev);
80 efa_com_aenq_intr_handler(&dev->edev, data);
81
82 return IRQ_HANDLED;
83 }
84
85 static int efa_request_mgmnt_irq(struct efa_dev *dev)
86 {
87 struct efa_irq *irq;
88 int err;
89
90 irq = &dev->admin_irq;
91 err = request_irq(irq->vector, irq->handler, 0, irq->name,
92 irq->data);
93 if (err) {
94 dev_err(&dev->pdev->dev, "Failed to request admin irq (%d)\n",
95 err);
96 return err;
97 }
98
99 dev_dbg(&dev->pdev->dev, "Set affinity hint of mgmnt irq to %*pbl (irq vector: %d)\n",
100 nr_cpumask_bits, &irq->affinity_hint_mask, irq->vector);
101 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
102
103 return 0;
104 }
105
106 static void efa_setup_mgmnt_irq(struct efa_dev *dev)
107 {
108 u32 cpu;
109
110 snprintf(dev->admin_irq.name, EFA_IRQNAME_SIZE,
111 "efa-mgmnt@pci:%s", pci_name(dev->pdev));
112 dev->admin_irq.handler = efa_intr_msix_mgmnt;
113 dev->admin_irq.data = dev;
114 dev->admin_irq.vector =
115 pci_irq_vector(dev->pdev, dev->admin_msix_vector_idx);
116 cpu = cpumask_first(cpu_online_mask);
117 dev->admin_irq.cpu = cpu;
118 cpumask_set_cpu(cpu,
119 &dev->admin_irq.affinity_hint_mask);
120 dev_info(&dev->pdev->dev, "Setup irq:0x%p vector:%d name:%s\n",
121 &dev->admin_irq,
122 dev->admin_irq.vector,
123 dev->admin_irq.name);
124 }
125
126 static void efa_free_mgmnt_irq(struct efa_dev *dev)
127 {
128 struct efa_irq *irq;
129
130 irq = &dev->admin_irq;
131 irq_set_affinity_hint(irq->vector, NULL);
132 free_irq(irq->vector, irq->data);
133 }
134
135 static int efa_set_mgmnt_irq(struct efa_dev *dev)
136 {
137 efa_setup_mgmnt_irq(dev);
138
139 return efa_request_mgmnt_irq(dev);
140 }
141
142 static int efa_request_doorbell_bar(struct efa_dev *dev)
143 {
144 u8 db_bar_idx = dev->dev_attr.db_bar;
145 struct pci_dev *pdev = dev->pdev;
146 int bars;
147 int err;
148
149 if (!(BIT(db_bar_idx) & EFA_BASE_BAR_MASK)) {
150 bars = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(db_bar_idx);
151
152 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
153 if (err) {
154 dev_err(&dev->pdev->dev,
155 "pci_request_selected_regions for bar %d failed %d\n",
156 db_bar_idx, err);
157 return err;
158 }
159 }
160
161 dev->db_bar_addr = pci_resource_start(dev->pdev, db_bar_idx);
162 dev->db_bar_len = pci_resource_len(dev->pdev, db_bar_idx);
163
164 return 0;
165 }
166
167 static void efa_release_doorbell_bar(struct efa_dev *dev)
168 {
169 if (!(BIT(dev->dev_attr.db_bar) & EFA_BASE_BAR_MASK))
170 efa_release_bars(dev, BIT(dev->dev_attr.db_bar));
171 }
172
173 static void efa_update_hw_hints(struct efa_dev *dev,
174 struct efa_com_get_hw_hints_result *hw_hints)
175 {
176 struct efa_com_dev *edev = &dev->edev;
177
178 if (hw_hints->mmio_read_timeout)
179 edev->mmio_read.mmio_read_timeout =
180 hw_hints->mmio_read_timeout * 1000;
181
182 if (hw_hints->poll_interval)
183 edev->aq.poll_interval = hw_hints->poll_interval;
184
185 if (hw_hints->admin_completion_timeout)
186 edev->aq.completion_timeout =
187 hw_hints->admin_completion_timeout;
188 }
189
190 static void efa_stats_init(struct efa_dev *dev)
191 {
192 atomic64_t *s = (atomic64_t *)&dev->stats;
193 int i;
194
195 for (i = 0; i < sizeof(dev->stats) / sizeof(*s); i++, s++)
196 atomic64_set(s, 0);
197 }
198
199 static const struct ib_device_ops efa_dev_ops = {
200 .owner = THIS_MODULE,
201 .driver_id = RDMA_DRIVER_EFA,
202 .uverbs_abi_ver = EFA_UVERBS_ABI_VERSION,
203
204 .alloc_hw_stats = efa_alloc_hw_stats,
205 .alloc_pd = efa_alloc_pd,
206 .alloc_ucontext = efa_alloc_ucontext,
207 .create_ah = efa_create_ah,
208 .create_cq = efa_create_cq,
209 .create_qp = efa_create_qp,
210 .dealloc_pd = efa_dealloc_pd,
211 .dealloc_ucontext = efa_dealloc_ucontext,
212 .dereg_mr = efa_dereg_mr,
213 .destroy_ah = efa_destroy_ah,
214 .destroy_cq = efa_destroy_cq,
215 .destroy_qp = efa_destroy_qp,
216 .get_hw_stats = efa_get_hw_stats,
217 .get_link_layer = efa_port_link_layer,
218 .get_port_immutable = efa_get_port_immutable,
219 .mmap = efa_mmap,
220 .modify_qp = efa_modify_qp,
221 .query_device = efa_query_device,
222 .query_gid = efa_query_gid,
223 .query_pkey = efa_query_pkey,
224 .query_port = efa_query_port,
225 .query_qp = efa_query_qp,
226 .reg_user_mr = efa_reg_mr,
227
228 INIT_RDMA_OBJ_SIZE(ib_ah, efa_ah, ibah),
229 INIT_RDMA_OBJ_SIZE(ib_cq, efa_cq, ibcq),
230 INIT_RDMA_OBJ_SIZE(ib_pd, efa_pd, ibpd),
231 INIT_RDMA_OBJ_SIZE(ib_ucontext, efa_ucontext, ibucontext),
232 };
233
234 static int efa_ib_device_add(struct efa_dev *dev)
235 {
236 struct efa_com_get_network_attr_result network_attr;
237 struct efa_com_get_hw_hints_result hw_hints;
238 struct pci_dev *pdev = dev->pdev;
239 int err;
240
241 efa_stats_init(dev);
242
243 err = efa_com_get_device_attr(&dev->edev, &dev->dev_attr);
244 if (err)
245 return err;
246
247 dev_dbg(&dev->pdev->dev, "Doorbells bar (%d)\n", dev->dev_attr.db_bar);
248 err = efa_request_doorbell_bar(dev);
249 if (err)
250 return err;
251
252 err = efa_com_get_network_attr(&dev->edev, &network_attr);
253 if (err)
254 goto err_release_doorbell_bar;
255
256 efa_update_network_attr(dev, &network_attr);
257
258 err = efa_com_get_hw_hints(&dev->edev, &hw_hints);
259 if (err)
260 goto err_release_doorbell_bar;
261
262 efa_update_hw_hints(dev, &hw_hints);
263
264
265 err = efa_com_set_aenq_config(&dev->edev, EFA_AENQ_ENABLED_GROUPS);
266 if (err)
267 goto err_release_doorbell_bar;
268
269 dev->ibdev.node_type = RDMA_NODE_UNSPECIFIED;
270 dev->ibdev.phys_port_cnt = 1;
271 dev->ibdev.num_comp_vectors = 1;
272 dev->ibdev.dev.parent = &pdev->dev;
273
274 dev->ibdev.uverbs_cmd_mask =
275 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
276 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
277 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
278 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
279 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
280 (1ull << IB_USER_VERBS_CMD_REG_MR) |
281 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
282 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
283 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
284 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
285 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
286 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
287 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
288 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
289 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
290 (1ull << IB_USER_VERBS_CMD_DESTROY_AH);
291
292 dev->ibdev.uverbs_ex_cmd_mask =
293 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
294
295 ib_set_device_ops(&dev->ibdev, &efa_dev_ops);
296
297 err = ib_register_device(&dev->ibdev, "efa_%d");
298 if (err)
299 goto err_release_doorbell_bar;
300
301 ibdev_info(&dev->ibdev, "IB device registered\n");
302
303 return 0;
304
305 err_release_doorbell_bar:
306 efa_release_doorbell_bar(dev);
307 return err;
308 }
309
310 static void efa_ib_device_remove(struct efa_dev *dev)
311 {
312 efa_com_dev_reset(&dev->edev, EFA_REGS_RESET_NORMAL);
313 ibdev_info(&dev->ibdev, "Unregister ib device\n");
314 ib_unregister_device(&dev->ibdev);
315 efa_release_doorbell_bar(dev);
316 }
317
318 static void efa_disable_msix(struct efa_dev *dev)
319 {
320 pci_free_irq_vectors(dev->pdev);
321 }
322
323 static int efa_enable_msix(struct efa_dev *dev)
324 {
325 int msix_vecs, irq_num;
326
327
328 msix_vecs = EFA_NUM_MSIX_VEC;
329 dev_dbg(&dev->pdev->dev, "Trying to enable MSI-X, vectors %d\n",
330 msix_vecs);
331
332 dev->admin_msix_vector_idx = EFA_MGMNT_MSIX_VEC_IDX;
333 irq_num = pci_alloc_irq_vectors(dev->pdev, msix_vecs,
334 msix_vecs, PCI_IRQ_MSIX);
335
336 if (irq_num < 0) {
337 dev_err(&dev->pdev->dev, "Failed to enable MSI-X. irq_num %d\n",
338 irq_num);
339 return -ENOSPC;
340 }
341
342 if (irq_num != msix_vecs) {
343 dev_err(&dev->pdev->dev,
344 "Allocated %d MSI-X (out of %d requested)\n",
345 irq_num, msix_vecs);
346 return -ENOSPC;
347 }
348
349 return 0;
350 }
351
352 static int efa_device_init(struct efa_com_dev *edev, struct pci_dev *pdev)
353 {
354 int dma_width;
355 int err;
356
357 err = efa_com_dev_reset(edev, EFA_REGS_RESET_NORMAL);
358 if (err)
359 return err;
360
361 err = efa_com_validate_version(edev);
362 if (err)
363 return err;
364
365 dma_width = efa_com_get_dma_width(edev);
366 if (dma_width < 0) {
367 err = dma_width;
368 return err;
369 }
370
371 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_width));
372 if (err) {
373 dev_err(&pdev->dev, "pci_set_dma_mask failed %d\n", err);
374 return err;
375 }
376
377 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(dma_width));
378 if (err) {
379 dev_err(&pdev->dev,
380 "err_pci_set_consistent_dma_mask failed %d\n",
381 err);
382 return err;
383 }
384
385 return 0;
386 }
387
388 static struct efa_dev *efa_probe_device(struct pci_dev *pdev)
389 {
390 struct efa_com_dev *edev;
391 struct efa_dev *dev;
392 int bars;
393 int err;
394
395 err = pci_enable_device_mem(pdev);
396 if (err) {
397 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
398 return ERR_PTR(err);
399 }
400
401 pci_set_master(pdev);
402
403 dev = ib_alloc_device(efa_dev, ibdev);
404 if (!dev) {
405 dev_err(&pdev->dev, "Device alloc failed\n");
406 err = -ENOMEM;
407 goto err_disable_device;
408 }
409
410 pci_set_drvdata(pdev, dev);
411 edev = &dev->edev;
412 edev->efa_dev = dev;
413 edev->dmadev = &pdev->dev;
414 dev->pdev = pdev;
415
416 bars = pci_select_bars(pdev, IORESOURCE_MEM) & EFA_BASE_BAR_MASK;
417 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
418 if (err) {
419 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
420 err);
421 goto err_ibdev_destroy;
422 }
423
424 dev->reg_bar_addr = pci_resource_start(pdev, EFA_REG_BAR);
425 dev->reg_bar_len = pci_resource_len(pdev, EFA_REG_BAR);
426 dev->mem_bar_addr = pci_resource_start(pdev, EFA_MEM_BAR);
427 dev->mem_bar_len = pci_resource_len(pdev, EFA_MEM_BAR);
428
429 edev->reg_bar = devm_ioremap(&pdev->dev,
430 dev->reg_bar_addr,
431 dev->reg_bar_len);
432 if (!edev->reg_bar) {
433 dev_err(&pdev->dev, "Failed to remap register bar\n");
434 err = -EFAULT;
435 goto err_release_bars;
436 }
437
438 err = efa_com_mmio_reg_read_init(edev);
439 if (err) {
440 dev_err(&pdev->dev, "Failed to init readless MMIO\n");
441 goto err_iounmap;
442 }
443
444 err = efa_device_init(edev, pdev);
445 if (err) {
446 dev_err(&pdev->dev, "EFA device init failed\n");
447 if (err == -ETIME)
448 err = -EPROBE_DEFER;
449 goto err_reg_read_destroy;
450 }
451
452 err = efa_enable_msix(dev);
453 if (err)
454 goto err_reg_read_destroy;
455
456 edev->aq.msix_vector_idx = dev->admin_msix_vector_idx;
457 edev->aenq.msix_vector_idx = dev->admin_msix_vector_idx;
458
459 err = efa_set_mgmnt_irq(dev);
460 if (err)
461 goto err_disable_msix;
462
463 err = efa_com_admin_init(edev, &aenq_handlers);
464 if (err)
465 goto err_free_mgmnt_irq;
466
467 return dev;
468
469 err_free_mgmnt_irq:
470 efa_free_mgmnt_irq(dev);
471 err_disable_msix:
472 efa_disable_msix(dev);
473 err_reg_read_destroy:
474 efa_com_mmio_reg_read_destroy(edev);
475 err_iounmap:
476 devm_iounmap(&pdev->dev, edev->reg_bar);
477 err_release_bars:
478 efa_release_bars(dev, EFA_BASE_BAR_MASK);
479 err_ibdev_destroy:
480 ib_dealloc_device(&dev->ibdev);
481 err_disable_device:
482 pci_disable_device(pdev);
483 return ERR_PTR(err);
484 }
485
486 static void efa_remove_device(struct pci_dev *pdev)
487 {
488 struct efa_dev *dev = pci_get_drvdata(pdev);
489 struct efa_com_dev *edev;
490
491 edev = &dev->edev;
492 efa_com_admin_destroy(edev);
493 efa_free_mgmnt_irq(dev);
494 efa_disable_msix(dev);
495 efa_com_mmio_reg_read_destroy(edev);
496 devm_iounmap(&pdev->dev, edev->reg_bar);
497 efa_release_bars(dev, EFA_BASE_BAR_MASK);
498 ib_dealloc_device(&dev->ibdev);
499 pci_disable_device(pdev);
500 }
501
502 static int efa_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
503 {
504 struct efa_dev *dev;
505 int err;
506
507 dev = efa_probe_device(pdev);
508 if (IS_ERR(dev))
509 return PTR_ERR(dev);
510
511 err = efa_ib_device_add(dev);
512 if (err)
513 goto err_remove_device;
514
515 return 0;
516
517 err_remove_device:
518 efa_remove_device(pdev);
519 return err;
520 }
521
522 static void efa_remove(struct pci_dev *pdev)
523 {
524 struct efa_dev *dev = pci_get_drvdata(pdev);
525
526 efa_ib_device_remove(dev);
527 efa_remove_device(pdev);
528 }
529
530 static struct pci_driver efa_pci_driver = {
531 .name = DRV_MODULE_NAME,
532 .id_table = efa_pci_tbl,
533 .probe = efa_probe,
534 .remove = efa_remove,
535 };
536
537 module_pci_driver(efa_pci_driver);