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