xref: /linux/drivers/pci/endpoint/functions/pci-epf-vntb.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Endpoint Function Driver to implement Non-Transparent Bridge functionality
4  * Between PCI RC and EP
5  *
6  * Copyright (C) 2020 Texas Instruments
7  * Copyright (C) 2022 NXP
8  *
9  * Based on pci-epf-ntb.c
10  * Author: Frank Li <Frank.Li@nxp.com>
11  * Author: Kishon Vijay Abraham I <kishon@ti.com>
12  */
13 
14 /*
15  * +------------+         +---------------------------------------+
16  * |            |         |                                       |
17  * +------------+         |                        +--------------+
18  * | NTB        |         |                        | NTB          |
19  * | NetDev     |         |                        | NetDev       |
20  * +------------+         |                        +--------------+
21  * | NTB        |         |                        | NTB          |
22  * | Transfer   |         |                        | Transfer     |
23  * +------------+         |                        +--------------+
24  * |            |         |                        |              |
25  * |  PCI NTB   |         |                        |              |
26  * |    EPF     |         |                        |              |
27  * |   Driver   |         |                        | PCI Virtual  |
28  * |            |         +---------------+        | NTB Driver   |
29  * |            |         | PCI EP NTB    |<------>|              |
30  * |            |         |  FN Driver    |        |              |
31  * +------------+         +---------------+        +--------------+
32  * |            |         |               |        |              |
33  * |  PCI Bus   | <-----> |  PCI EP Bus   |        |  Virtual PCI |
34  * |            |  PCI    |               |        |     Bus      |
35  * +------------+         +---------------+--------+--------------+
36  * PCIe Root Port                        PCI EP
37  */
38 
39 #include <linux/atomic.h>
40 #include <linux/bitops.h>
41 #include <linux/delay.h>
42 #include <linux/io.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 
46 #include <linux/pci-ep-msi.h>
47 #include <linux/pci-epc.h>
48 #include <linux/pci-epf.h>
49 #include <linux/ntb.h>
50 
51 static struct workqueue_struct *kpcintb_workqueue;
52 
53 #define COMMAND_CONFIGURE_DOORBELL	1
54 #define COMMAND_TEARDOWN_DOORBELL	2
55 #define COMMAND_CONFIGURE_MW		3
56 #define COMMAND_TEARDOWN_MW		4
57 #define COMMAND_LINK_UP			5
58 #define COMMAND_LINK_DOWN		6
59 
60 #define COMMAND_STATUS_OK		1
61 #define COMMAND_STATUS_ERROR		2
62 
63 #define LINK_STATUS_UP			BIT(0)
64 
65 #define SPAD_COUNT			64
66 #define DB_COUNT			4
67 #define NTB_MW_OFFSET			2
68 #define DB_COUNT_MASK			GENMASK(15, 0)
69 #define MSIX_ENABLE			BIT(16)
70 #define MAX_MW				4
71 
72 /* Limit per-work execution to avoid monopolizing kworker on doorbell storms. */
73 #define VNTB_PEER_DB_WORK_BUDGET	5
74 
75 enum epf_ntb_bar {
76 	BAR_CONFIG,
77 	BAR_DB,
78 	BAR_MW1,
79 	BAR_MW2,
80 	BAR_MW3,
81 	BAR_MW4,
82 	VNTB_BAR_NUM,
83 };
84 
85 enum epf_irq_slot {
86 	EPF_IRQ_LINK = 0,
87 	EPF_IRQ_RESERVED_DB, /* Historically skipped slot */
88 	EPF_IRQ_DB_START,
89 };
90 
91 #define MIN_DB_COUNT			(EPF_IRQ_DB_START + 1)
92 #define MAX_DB_COUNT			32
93 
94 /*
95  * +--------------------------------------------------+ Base
96  * |                                                  |
97  * |                                                  |
98  * |                                                  |
99  * |          Common Control Register                 |
100  * |                                                  |
101  * |                                                  |
102  * |                                                  |
103  * +-----------------------+--------------------------+ Base+spad_offset
104  * |                       |                          |
105  * |    Peer Spad Space    |    Spad Space            |
106  * |                       |                          |
107  * |                       |                          |
108  * +-----------------------+--------------------------+ Base+spad_offset
109  * |                       |                          |     +spad_count * 4
110  * |                       |                          |
111  * |     Spad Space        |   Peer Spad Space        |
112  * |                       |                          |
113  * +-----------------------+--------------------------+
114  *       Virtual PCI             PCIe Endpoint
115  *       NTB Driver               NTB Driver
116  */
117 struct epf_ntb_ctrl {
118 	u32 command;
119 	u32 argument;
120 	u16 command_status;
121 	u16 link_status;
122 	u32 topology;
123 	u64 addr;
124 	u64 size;
125 	u32 num_mws;
126 	u32 reserved;
127 	u32 spad_offset;
128 	u32 spad_count;
129 	u32 db_entry_size;
130 	u32 db_data[MAX_DB_COUNT];
131 	u32 db_offset[MAX_DB_COUNT];
132 } __packed;
133 
134 struct epf_ntb {
135 	struct ntb_dev ntb;
136 	struct pci_epf *epf;
137 	struct config_group group;
138 
139 	u32 num_mws;
140 	u32 db_count;
141 	u32 spad_count;
142 	u64 mws_size[MAX_MW];
143 	atomic64_t db;
144 	atomic64_t peer_db_pending;
145 	struct work_struct peer_db_work;
146 	u32 vbus_number;
147 	u16 vntb_pid;
148 	u16 vntb_vid;
149 
150 	bool linkup;
151 
152 	/*
153 	 * True when doorbells are interrupt-driven (MSI or embedded), false
154 	 * when polled.
155 	 */
156 	bool msi_doorbell;
157 	u32 spad_size;
158 
159 	enum pci_barno epf_ntb_bar[VNTB_BAR_NUM];
160 
161 	struct epf_ntb_ctrl *reg;
162 
163 	u32 *epf_db;
164 
165 	phys_addr_t vpci_mw_phy[MAX_MW];
166 	void __iomem *vpci_mw_addr[MAX_MW];
167 
168 	struct delayed_work cmd_handler;
169 };
170 
171 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
172 #define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
173 
174 static struct pci_epf_header epf_ntb_header = {
175 	.vendorid	= PCI_ANY_ID,
176 	.deviceid	= PCI_ANY_ID,
177 	.baseclass_code	= PCI_BASE_CLASS_MEMORY,
178 	.interrupt_pin	= PCI_INTERRUPT_INTA,
179 };
180 
181 /**
182  * epf_ntb_link_up() - Raise link_up interrupt to Virtual Host (VHOST)
183  * @ntb: NTB device that facilitates communication between HOST and VHOST
184  * @link_up: true or false indicating Link is UP or Down
185  *
186  * Once NTB function in HOST invoke ntb_link_enable(),
187  * this NTB function driver will trigger a link event to VHOST.
188  *
189  * Returns: Zero for success, or an error code in case of failure
190  */
epf_ntb_link_up(struct epf_ntb * ntb,bool link_up)191 static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
192 {
193 	if (link_up)
194 		ntb->reg->link_status |= LINK_STATUS_UP;
195 	else
196 		ntb->reg->link_status &= ~LINK_STATUS_UP;
197 
198 	ntb_link_event(&ntb->ntb);
199 	return 0;
200 }
201 
202 /**
203  * epf_ntb_configure_mw() - Configure the Outbound Address Space for VHOST
204  *   to access the memory window of HOST
205  * @ntb: NTB device that facilitates communication between HOST and VHOST
206  * @mw: Index of the memory window (either 0, 1, 2 or 3)
207  *
208  *                          EP Outbound Window
209  * +--------+              +-----------+
210  * |        |              |           |
211  * |        |              |           |
212  * |        |              |           |
213  * |        |              |           |
214  * |        |              +-----------+
215  * | Virtual|              | Memory Win|
216  * | NTB    | -----------> |           |
217  * | Driver |              |           |
218  * |        |              +-----------+
219  * |        |              |           |
220  * |        |              |           |
221  * +--------+              +-----------+
222  *  VHOST                   PCI EP
223  *
224  * Returns: Zero for success, or an error code in case of failure
225  */
epf_ntb_configure_mw(struct epf_ntb * ntb,u32 mw)226 static int epf_ntb_configure_mw(struct epf_ntb *ntb, u32 mw)
227 {
228 	phys_addr_t phys_addr;
229 	u8 func_no, vfunc_no;
230 	u64 addr, size;
231 	int ret = 0;
232 
233 	phys_addr = ntb->vpci_mw_phy[mw];
234 	addr = ntb->reg->addr;
235 	size = ntb->reg->size;
236 
237 	func_no = ntb->epf->func_no;
238 	vfunc_no = ntb->epf->vfunc_no;
239 
240 	ret = pci_epc_map_addr(ntb->epf->epc, func_no, vfunc_no, phys_addr, addr, size);
241 	if (ret)
242 		dev_err(&ntb->epf->epc->dev,
243 			"Failed to map memory window %d address\n", mw);
244 	return ret;
245 }
246 
247 /**
248  * epf_ntb_teardown_mw() - Teardown the configured OB ATU
249  * @ntb: NTB device that facilitates communication between HOST and VHOST
250  * @mw: Index of the memory window (either 0, 1, 2 or 3)
251  *
252  * Teardown the configured OB ATU configured in epf_ntb_configure_mw() using
253  * pci_epc_unmap_addr()
254  */
epf_ntb_teardown_mw(struct epf_ntb * ntb,u32 mw)255 static void epf_ntb_teardown_mw(struct epf_ntb *ntb, u32 mw)
256 {
257 	pci_epc_unmap_addr(ntb->epf->epc,
258 			   ntb->epf->func_no,
259 			   ntb->epf->vfunc_no,
260 			   ntb->vpci_mw_phy[mw]);
261 }
262 
263 /**
264  * epf_ntb_cmd_handler() - Handle commands provided by the NTB HOST
265  * @work: work_struct for the epf_ntb_epc
266  *
267  * Workqueue function that gets invoked for the two epf_ntb_epc
268  * periodically (once every 5ms) to see if it has received any commands
269  * from NTB HOST. The HOST can send commands to configure doorbell or
270  * configure memory window or to update link status.
271  */
epf_ntb_cmd_handler(struct work_struct * work)272 static void epf_ntb_cmd_handler(struct work_struct *work)
273 {
274 	struct epf_ntb_ctrl *ctrl;
275 	u32 command, argument;
276 	struct epf_ntb *ntb;
277 	struct device *dev;
278 	int ret;
279 	int i;
280 
281 	ntb = container_of(work, struct epf_ntb, cmd_handler.work);
282 
283 	for (i = EPF_IRQ_DB_START; i < ntb->db_count && !ntb->msi_doorbell;
284 	     i++) {
285 		if (ntb->epf_db[i]) {
286 			atomic64_or(1 << (i - EPF_IRQ_DB_START), &ntb->db);
287 			ntb_db_event(&ntb->ntb, i - EPF_IRQ_DB_START);
288 			ntb->epf_db[i] = 0;
289 		}
290 	}
291 
292 	ctrl = ntb->reg;
293 	command = ctrl->command;
294 	if (!command)
295 		goto reset_handler;
296 	argument = ctrl->argument;
297 
298 	ctrl->command = 0;
299 	ctrl->argument = 0;
300 
301 	ctrl = ntb->reg;
302 	dev = &ntb->epf->dev;
303 
304 	switch (command) {
305 	case COMMAND_CONFIGURE_DOORBELL:
306 		ctrl->command_status = COMMAND_STATUS_OK;
307 		break;
308 	case COMMAND_TEARDOWN_DOORBELL:
309 		ctrl->command_status = COMMAND_STATUS_OK;
310 		break;
311 	case COMMAND_CONFIGURE_MW:
312 		ret = epf_ntb_configure_mw(ntb, argument);
313 		if (ret < 0)
314 			ctrl->command_status = COMMAND_STATUS_ERROR;
315 		else
316 			ctrl->command_status = COMMAND_STATUS_OK;
317 		break;
318 	case COMMAND_TEARDOWN_MW:
319 		epf_ntb_teardown_mw(ntb, argument);
320 		ctrl->command_status = COMMAND_STATUS_OK;
321 		break;
322 	case COMMAND_LINK_UP:
323 		ntb->linkup = true;
324 		ret = epf_ntb_link_up(ntb, true);
325 		if (ret < 0)
326 			ctrl->command_status = COMMAND_STATUS_ERROR;
327 		else
328 			ctrl->command_status = COMMAND_STATUS_OK;
329 		goto reset_handler;
330 	case COMMAND_LINK_DOWN:
331 		ntb->linkup = false;
332 		ret = epf_ntb_link_up(ntb, false);
333 		if (ret < 0)
334 			ctrl->command_status = COMMAND_STATUS_ERROR;
335 		else
336 			ctrl->command_status = COMMAND_STATUS_OK;
337 		break;
338 	default:
339 		dev_err(dev, "UNKNOWN command: %d\n", command);
340 		break;
341 	}
342 
343 reset_handler:
344 	queue_delayed_work(kpcintb_workqueue, &ntb->cmd_handler,
345 			   ntb->msi_doorbell ? msecs_to_jiffies(500) : msecs_to_jiffies(5));
346 }
347 
epf_ntb_doorbell_handler(int irq,void * data)348 static irqreturn_t epf_ntb_doorbell_handler(int irq, void *data)
349 {
350 	struct epf_ntb *ntb = data;
351 	int i;
352 
353 	for (i = EPF_IRQ_DB_START; i < ntb->db_count; i++)
354 		if (irq == ntb->epf->db_msg[i].virq) {
355 			atomic64_or(1 << (i - EPF_IRQ_DB_START), &ntb->db);
356 			ntb_db_event(&ntb->ntb, i - EPF_IRQ_DB_START);
357 		}
358 
359 	return IRQ_HANDLED;
360 }
361 
362 /**
363  * epf_ntb_config_sspad_bar_clear() - Clear Config + Self scratchpad BAR
364  * @ntb: EPC associated with one of the HOST which holds peer's outbound
365  *	 address.
366  *
367  * Clear BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
368  * self scratchpad region (removes inbound ATU configuration). While BAR0 is
369  * the default self scratchpad BAR, an NTB could have other BARs for self
370  * scratchpad (because of reserved BARs). This function can get the exact BAR
371  * used for self scratchpad from epf_ntb_bar[BAR_CONFIG].
372  *
373  * Please note the self scratchpad region and config region is combined to
374  * a single region and mapped using the same BAR. Also note VHOST's peer
375  * scratchpad is HOST's self scratchpad.
376  *
377  * Returns: void
378  */
epf_ntb_config_sspad_bar_clear(struct epf_ntb * ntb)379 static void epf_ntb_config_sspad_bar_clear(struct epf_ntb *ntb)
380 {
381 	struct pci_epf_bar *epf_bar;
382 	enum pci_barno barno;
383 
384 	barno = ntb->epf_ntb_bar[BAR_CONFIG];
385 	epf_bar = &ntb->epf->bar[barno];
386 
387 	pci_epc_clear_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
388 }
389 
390 /**
391  * epf_ntb_config_sspad_bar_set() - Set Config + Self scratchpad BAR
392  * @ntb: NTB device that facilitates communication between HOST and VHOST
393  *
394  * Map BAR0 of EP CONTROLLER which contains the VHOST's config and
395  * self scratchpad region.
396  *
397  * Please note the self scratchpad region and config region is combined to
398  * a single region and mapped using the same BAR.
399  *
400  * Returns: Zero for success, or an error code in case of failure
401  */
epf_ntb_config_sspad_bar_set(struct epf_ntb * ntb)402 static int epf_ntb_config_sspad_bar_set(struct epf_ntb *ntb)
403 {
404 	struct pci_epf_bar *epf_bar;
405 	enum pci_barno barno;
406 	u8 func_no, vfunc_no;
407 	struct device *dev;
408 	int ret;
409 
410 	dev = &ntb->epf->dev;
411 	func_no = ntb->epf->func_no;
412 	vfunc_no = ntb->epf->vfunc_no;
413 	barno = ntb->epf_ntb_bar[BAR_CONFIG];
414 	epf_bar = &ntb->epf->bar[barno];
415 
416 	ret = pci_epc_set_bar(ntb->epf->epc, func_no, vfunc_no, epf_bar);
417 	if (ret) {
418 		dev_err(dev, "inft: Config/Status/SPAD BAR set failed\n");
419 		return ret;
420 	}
421 	return 0;
422 }
423 
424 /**
425  * epf_ntb_config_spad_bar_free() - Free the physical memory associated with
426  *   config + scratchpad region
427  * @ntb: NTB device that facilitates communication between HOST and VHOST
428  */
epf_ntb_config_spad_bar_free(struct epf_ntb * ntb)429 static void epf_ntb_config_spad_bar_free(struct epf_ntb *ntb)
430 {
431 	enum pci_barno barno;
432 
433 	barno = ntb->epf_ntb_bar[BAR_CONFIG];
434 	pci_epf_free_space(ntb->epf, ntb->reg, barno, 0);
435 }
436 
437 /**
438  * epf_ntb_config_spad_bar_alloc() - Allocate memory for config + scratchpad
439  *   region
440  * @ntb: NTB device that facilitates communication between HOST and VHOST
441  *
442  * Allocate the Local Memory mentioned in the above diagram. The size of
443  * CONFIG REGION is sizeof(struct epf_ntb_ctrl) and size of SCRATCHPAD REGION
444  * is obtained from "spad-count" configfs entry.
445  *
446  * Returns: Zero for success, or an error code in case of failure
447  */
epf_ntb_config_spad_bar_alloc(struct epf_ntb * ntb)448 static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb)
449 {
450 	enum pci_barno barno;
451 	struct epf_ntb_ctrl *ctrl;
452 	u32 spad_size, ctrl_size;
453 	struct pci_epf *epf = ntb->epf;
454 	struct device *dev = &epf->dev;
455 	u32 spad_count;
456 	void *base;
457 	int i;
458 	const struct pci_epc_features *epc_features = pci_epc_get_features(epf->epc,
459 								epf->func_no,
460 								epf->vfunc_no);
461 	barno = ntb->epf_ntb_bar[BAR_CONFIG];
462 	spad_count = ntb->spad_count;
463 
464 	ctrl_size = ALIGN(sizeof(struct epf_ntb_ctrl), sizeof(u32));
465 	spad_size = 2 * spad_count * sizeof(u32);
466 
467 	base = pci_epf_alloc_space(epf, ctrl_size + spad_size,
468 				   barno, epc_features, 0);
469 	if (!base) {
470 		dev_err(dev, "Config/Status/SPAD alloc region fail\n");
471 		return -ENOMEM;
472 	}
473 
474 	ntb->reg = base;
475 
476 	ctrl = ntb->reg;
477 	ctrl->spad_offset = ctrl_size;
478 
479 	ctrl->spad_count = spad_count;
480 	ctrl->num_mws = ntb->num_mws;
481 	ntb->spad_size = spad_size;
482 
483 	ctrl->db_entry_size = sizeof(u32);
484 
485 	for (i = 0; i < ntb->db_count; i++) {
486 		ntb->reg->db_data[i] = 1 + i;
487 		ntb->reg->db_offset[i] = 0;
488 	}
489 
490 	return 0;
491 }
492 
493 /**
494  * epf_ntb_configure_interrupt() - Configure MSI/MSI-X capability
495  * @ntb: NTB device that facilitates communication between HOST and VHOST
496  *
497  * Configure MSI/MSI-X capability for each interface with number of
498  * interrupts equal to "db_count" configfs entry.
499  *
500  * Returns: Zero for success, or an error code in case of failure
501  */
epf_ntb_configure_interrupt(struct epf_ntb * ntb)502 static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
503 {
504 	const struct pci_epc_features *epc_features;
505 	struct device *dev;
506 	int ret;
507 
508 	dev = &ntb->epf->dev;
509 
510 	epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
511 
512 	if (!(epc_features->msix_capable || epc_features->msi_capable)) {
513 		dev_err(dev, "MSI or MSI-X is required for doorbell\n");
514 		return -EINVAL;
515 	}
516 
517 	if (ntb->db_count < MIN_DB_COUNT || ntb->db_count > MAX_DB_COUNT) {
518 		dev_err(dev, "DB count %d out of range (%d - %d)\n",
519 			ntb->db_count, MIN_DB_COUNT, MAX_DB_COUNT);
520 		return -EINVAL;
521 	}
522 
523 	if (epc_features->msi_capable) {
524 		ret = pci_epc_set_msi(ntb->epf->epc,
525 				      ntb->epf->func_no,
526 				      ntb->epf->vfunc_no,
527 				      16);
528 		if (ret) {
529 			dev_err(dev, "MSI configuration failed\n");
530 			return ret;
531 		}
532 	}
533 
534 	return 0;
535 }
536 
epf_ntb_db_irq_is_duplicated(const struct pci_epf * epf,unsigned int idx)537 static bool epf_ntb_db_irq_is_duplicated(const struct pci_epf *epf, unsigned int idx)
538 {
539 	unsigned int i;
540 
541 	for (i = 0; i < idx; i++)
542 		if (epf->db_msg[i].virq == epf->db_msg[idx].virq)
543 			return true;
544 
545 	return false;
546 }
547 
epf_ntb_db_bar_init_msi_doorbell(struct epf_ntb * ntb,struct pci_epf_bar * db_bar,const struct pci_epc_features * epc_features,enum pci_barno barno)548 static int epf_ntb_db_bar_init_msi_doorbell(struct epf_ntb *ntb,
549 					    struct pci_epf_bar *db_bar,
550 					    const struct pci_epc_features *epc_features,
551 					    enum pci_barno barno)
552 {
553 	struct pci_epf *epf = ntb->epf;
554 	dma_addr_t low, high;
555 	struct msi_msg *msg;
556 	size_t sz;
557 	int ret;
558 	int i, req;
559 
560 	ret = pci_epf_alloc_doorbell(epf,  ntb->db_count);
561 	if (ret)
562 		return ret;
563 
564 	/*
565 	 * The doorbell target may already be exposed by a platform-owned fixed
566 	 * BAR. In that case, we must reuse it and the requested db_bar must
567 	 * match.
568 	 */
569 	if (epf->db_msg[0].bar != NO_BAR && epf->db_msg[0].bar != barno) {
570 		ret = -EINVAL;
571 		goto err_free_doorbell;
572 	}
573 
574 	for (req = 0; req < ntb->db_count; req++) {
575 		/* Avoid requesting duplicate handlers */
576 		if (epf_ntb_db_irq_is_duplicated(epf, req))
577 			continue;
578 
579 		ret = request_irq(epf->db_msg[req].virq, epf_ntb_doorbell_handler,
580 				  epf->db_msg[req].irq_flags, "pci_epf_vntb_db",
581 				  ntb);
582 
583 		if (ret) {
584 			dev_err(&epf->dev,
585 				"Failed to request doorbell IRQ: %d\n",
586 				epf->db_msg[req].virq);
587 			goto err_free_irq;
588 		}
589 	}
590 
591 	if (epf->db_msg[0].bar != NO_BAR) {
592 		for (i = 0; i < ntb->db_count; i++) {
593 			msg = &epf->db_msg[i].msg;
594 
595 			if (epf->db_msg[i].bar != barno) {
596 				ret = -EINVAL;
597 				goto err_free_irq;
598 			}
599 
600 			ntb->reg->db_data[i] = msg->data;
601 			ntb->reg->db_offset[i] = epf->db_msg[i].offset;
602 		}
603 		goto out;
604 	}
605 
606 	/* Program inbound mapping for the doorbell */
607 	msg = &epf->db_msg[0].msg;
608 
609 	high = 0;
610 	low = (u64)msg->address_hi << 32 | msg->address_lo;
611 
612 	for (i = 0; i < ntb->db_count; i++) {
613 		struct msi_msg *msg = &epf->db_msg[i].msg;
614 		dma_addr_t addr = (u64)msg->address_hi << 32 | msg->address_lo;
615 
616 		low = min(low, addr);
617 		high = max(high, addr);
618 	}
619 
620 	sz = high - low + sizeof(u32);
621 
622 	ret = pci_epf_assign_bar_space(epf, sz, barno, epc_features, 0, low);
623 	if (ret) {
624 		dev_err(&epf->dev, "Failed to assign Doorbell BAR space\n");
625 		goto err_free_irq;
626 	}
627 
628 	ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
629 			      ntb->epf->vfunc_no, db_bar);
630 	if (ret) {
631 		dev_err(&epf->dev, "Failed to set Doorbell BAR\n");
632 		goto err_free_irq;
633 	}
634 
635 	for (i = 0; i < ntb->db_count; i++) {
636 		struct msi_msg *msg = &epf->db_msg[i].msg;
637 		dma_addr_t addr;
638 		size_t offset;
639 
640 		ret = pci_epf_align_inbound_addr(epf, db_bar->barno,
641 				((u64)msg->address_hi << 32) | msg->address_lo,
642 				&addr, &offset);
643 
644 		if (ret) {
645 			ntb->msi_doorbell = false;
646 			goto err_free_irq;
647 		}
648 
649 		ntb->reg->db_data[i] = msg->data;
650 		ntb->reg->db_offset[i] = offset;
651 	}
652 
653 out:
654 	ntb->reg->db_entry_size = 0;
655 
656 	ntb->msi_doorbell = true;
657 
658 	return 0;
659 
660 err_free_irq:
661 	for (req--; req >= 0; req--) {
662 		if (epf_ntb_db_irq_is_duplicated(epf, req))
663 			continue;
664 		free_irq(epf->db_msg[req].virq, ntb);
665 	}
666 
667 err_free_doorbell:
668 	pci_epf_free_doorbell(ntb->epf);
669 	return ret;
670 }
671 
672 /**
673  * epf_ntb_db_bar_init() - Configure Doorbell window BARs
674  * @ntb: NTB device that facilitates communication between HOST and VHOST
675  *
676  * Returns: Zero for success, or an error code in case of failure
677  */
epf_ntb_db_bar_init(struct epf_ntb * ntb)678 static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
679 {
680 	const struct pci_epc_features *epc_features;
681 	struct device *dev = &ntb->epf->dev;
682 	int ret;
683 	struct pci_epf_bar *epf_bar;
684 	void *mw_addr;
685 	enum pci_barno barno;
686 	size_t size = sizeof(u32) * ntb->db_count;
687 
688 	epc_features = pci_epc_get_features(ntb->epf->epc,
689 					    ntb->epf->func_no,
690 					    ntb->epf->vfunc_no);
691 	barno = ntb->epf_ntb_bar[BAR_DB];
692 	epf_bar = &ntb->epf->bar[barno];
693 
694 	ret = epf_ntb_db_bar_init_msi_doorbell(ntb, epf_bar, epc_features, barno);
695 	if (ret) {
696 		/* fall back to polling mode */
697 		mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, epc_features, 0);
698 		if (!mw_addr) {
699 			dev_err(dev, "Failed to allocate OB address\n");
700 			return -ENOMEM;
701 		}
702 
703 		ntb->epf_db = mw_addr;
704 
705 		ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
706 				      ntb->epf->vfunc_no, epf_bar);
707 		if (ret) {
708 			dev_err(dev, "Doorbell BAR set failed\n");
709 			goto err_alloc_peer_mem;
710 		}
711 	}
712 	return ret;
713 
714 err_alloc_peer_mem:
715 	pci_epf_free_space(ntb->epf, mw_addr, barno, 0);
716 	return -1;
717 }
718 
719 static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws);
720 
721 /**
722  * epf_ntb_db_bar_clear() - Clear doorbell BAR and free memory
723  *   allocated in peer's outbound address space
724  * @ntb: NTB device that facilitates communication between HOST and VHOST
725  */
epf_ntb_db_bar_clear(struct epf_ntb * ntb)726 static void epf_ntb_db_bar_clear(struct epf_ntb *ntb)
727 {
728 	enum pci_barno barno;
729 
730 	if (ntb->msi_doorbell) {
731 		int i;
732 
733 		for (i = 0; i < ntb->db_count; i++) {
734 			if (epf_ntb_db_irq_is_duplicated(ntb->epf, i))
735 				continue;
736 			free_irq(ntb->epf->db_msg[i].virq, ntb);
737 		}
738 	}
739 
740 	if (ntb->epf->db_msg)
741 		pci_epf_free_doorbell(ntb->epf);
742 
743 	barno = ntb->epf_ntb_bar[BAR_DB];
744 	pci_epf_free_space(ntb->epf, ntb->epf_db, barno, 0);
745 	pci_epc_clear_bar(ntb->epf->epc,
746 			  ntb->epf->func_no,
747 			  ntb->epf->vfunc_no,
748 			  &ntb->epf->bar[barno]);
749 }
750 
751 /**
752  * epf_ntb_mw_bar_init() - Configure Memory window BARs
753  * @ntb: NTB device that facilitates communication between HOST and VHOST
754  *
755  * Returns: Zero for success, or an error code in case of failure
756  */
epf_ntb_mw_bar_init(struct epf_ntb * ntb)757 static int epf_ntb_mw_bar_init(struct epf_ntb *ntb)
758 {
759 	int ret = 0;
760 	int i;
761 	u64 size;
762 	enum pci_barno barno;
763 	struct device *dev = &ntb->epf->dev;
764 
765 	for (i = 0; i < ntb->num_mws; i++) {
766 		size = ntb->mws_size[i];
767 		barno = ntb->epf_ntb_bar[BAR_MW1 + i];
768 
769 		ntb->epf->bar[barno].barno = barno;
770 		ntb->epf->bar[barno].size = size;
771 		ntb->epf->bar[barno].addr = NULL;
772 		ntb->epf->bar[barno].phys_addr = 0;
773 		ntb->epf->bar[barno].flags |= upper_32_bits(size) ?
774 				PCI_BASE_ADDRESS_MEM_TYPE_64 :
775 				PCI_BASE_ADDRESS_MEM_TYPE_32;
776 
777 		ret = pci_epc_set_bar(ntb->epf->epc,
778 				      ntb->epf->func_no,
779 				      ntb->epf->vfunc_no,
780 				      &ntb->epf->bar[barno]);
781 		if (ret) {
782 			dev_err(dev, "MW set failed\n");
783 			goto err_alloc_mem;
784 		}
785 
786 		/* Allocate EPC outbound memory windows to vpci vntb device */
787 		ntb->vpci_mw_addr[i] = pci_epc_mem_alloc_addr(ntb->epf->epc,
788 							      &ntb->vpci_mw_phy[i],
789 							      size);
790 		if (!ntb->vpci_mw_addr[i]) {
791 			ret = -ENOMEM;
792 			dev_err(dev, "Failed to allocate source address\n");
793 			goto err_set_bar;
794 		}
795 	}
796 
797 	return ret;
798 
799 err_set_bar:
800 	pci_epc_clear_bar(ntb->epf->epc,
801 			  ntb->epf->func_no,
802 			  ntb->epf->vfunc_no,
803 			  &ntb->epf->bar[barno]);
804 err_alloc_mem:
805 	epf_ntb_mw_bar_clear(ntb, i);
806 	return ret;
807 }
808 
809 /**
810  * epf_ntb_mw_bar_clear() - Clear Memory window BARs
811  * @ntb: NTB device that facilitates communication between HOST and VHOST
812  * @num_mws: the number of Memory window BARs that to be cleared
813  */
epf_ntb_mw_bar_clear(struct epf_ntb * ntb,int num_mws)814 static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws)
815 {
816 	enum pci_barno barno;
817 	int i;
818 
819 	for (i = 0; i < num_mws; i++) {
820 		barno = ntb->epf_ntb_bar[BAR_MW1 + i];
821 		pci_epc_clear_bar(ntb->epf->epc,
822 				  ntb->epf->func_no,
823 				  ntb->epf->vfunc_no,
824 				  &ntb->epf->bar[barno]);
825 
826 		pci_epc_mem_free_addr(ntb->epf->epc,
827 				      ntb->vpci_mw_phy[i],
828 				      ntb->vpci_mw_addr[i],
829 				      ntb->mws_size[i]);
830 	}
831 }
832 
833 /**
834  * epf_ntb_is_bar_used() - Check if a bar is used in the ntb configuration
835  * @ntb: NTB device that facilitates communication between HOST and VHOST
836  * @barno: Checked bar number
837  *
838  * Returns: true if used, false if free.
839  */
epf_ntb_is_bar_used(struct epf_ntb * ntb,enum pci_barno barno)840 static bool epf_ntb_is_bar_used(struct epf_ntb *ntb,
841 				enum pci_barno barno)
842 {
843 	int i;
844 
845 	for (i = 0; i < VNTB_BAR_NUM; i++) {
846 		if (ntb->epf_ntb_bar[i] == barno)
847 			return true;
848 	}
849 
850 	return false;
851 }
852 
853 /**
854  * epf_ntb_find_bar() - Assign BAR number when no configuration is provided
855  * @ntb: NTB device that facilitates communication between HOST and VHOST
856  * @epc_features: The features provided by the EPC specific to this EPF
857  * @bar: NTB BAR index
858  * @barno: Bar start index
859  *
860  * When the BAR configuration was not provided through the userspace
861  * configuration, automatically assign BAR as it has been historically
862  * done by this endpoint function.
863  *
864  * Returns: the BAR number found, if any. -1 otherwise
865  */
epf_ntb_find_bar(struct epf_ntb * ntb,const struct pci_epc_features * epc_features,enum epf_ntb_bar bar,enum pci_barno barno)866 static int epf_ntb_find_bar(struct epf_ntb *ntb,
867 			    const struct pci_epc_features *epc_features,
868 			    enum epf_ntb_bar bar,
869 			    enum pci_barno barno)
870 {
871 	while (ntb->epf_ntb_bar[bar] < 0) {
872 		barno = pci_epc_get_next_free_bar(epc_features, barno);
873 		if (barno < 0)
874 			break; /* No more BAR available */
875 
876 		/*
877 		 * Verify if the BAR found is not already assigned
878 		 * through the provided configuration
879 		 */
880 		if (!epf_ntb_is_bar_used(ntb, barno))
881 			ntb->epf_ntb_bar[bar] = barno;
882 
883 		barno += 1;
884 	}
885 
886 	return barno;
887 }
888 
889 /**
890  * epf_ntb_init_epc_bar() - Identify BARs to be used for each of the NTB
891  * constructs (scratchpad region, doorbell, memorywindow)
892  * @ntb: NTB device that facilitates communication between HOST and VHOST
893  *
894  * Returns: Zero for success, or an error code in case of failure
895  */
epf_ntb_init_epc_bar(struct epf_ntb * ntb)896 static int epf_ntb_init_epc_bar(struct epf_ntb *ntb)
897 {
898 	const struct pci_epc_features *epc_features;
899 	enum pci_barno barno;
900 	enum epf_ntb_bar bar;
901 	struct device *dev;
902 	u32 num_mws;
903 	int i;
904 
905 	barno = BAR_0;
906 	num_mws = ntb->num_mws;
907 	dev = &ntb->epf->dev;
908 	epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
909 
910 	/* These are required BARs which are mandatory for NTB functionality */
911 	for (bar = BAR_CONFIG; bar <= BAR_MW1; bar++) {
912 		barno = epf_ntb_find_bar(ntb, epc_features, bar, barno);
913 		if (barno < 0) {
914 			dev_err(dev, "Fail to get NTB function BAR\n");
915 			return -ENOENT;
916 		}
917 	}
918 
919 	/* These are optional BARs which don't impact NTB functionality */
920 	for (bar = BAR_MW1, i = 1; i < num_mws; bar++, i++) {
921 		barno = epf_ntb_find_bar(ntb, epc_features, bar, barno);
922 		if (barno < 0) {
923 			ntb->num_mws = i;
924 			dev_dbg(dev, "BAR not available for > MW%d\n", i + 1);
925 		}
926 	}
927 
928 	return 0;
929 }
930 
931 /**
932  * epf_ntb_epc_init() - Initialize NTB interface
933  * @ntb: NTB device that facilitates communication between HOST and VHOST
934  *
935  * Wrapper to initialize a particular EPC interface and start the workqueue
936  * to check for commands from HOST. This function will write to the
937  * EP controller HW for configuring it.
938  *
939  * Returns: Zero for success, or an error code in case of failure
940  */
epf_ntb_epc_init(struct epf_ntb * ntb)941 static int epf_ntb_epc_init(struct epf_ntb *ntb)
942 {
943 	u8 func_no, vfunc_no;
944 	struct pci_epc *epc;
945 	struct pci_epf *epf;
946 	struct device *dev;
947 	int ret;
948 
949 	epf = ntb->epf;
950 	dev = &epf->dev;
951 	epc = epf->epc;
952 	func_no = ntb->epf->func_no;
953 	vfunc_no = ntb->epf->vfunc_no;
954 
955 	ret = epf_ntb_config_sspad_bar_set(ntb);
956 	if (ret) {
957 		dev_err(dev, "Config/self SPAD BAR init failed");
958 		return ret;
959 	}
960 
961 	ret = epf_ntb_configure_interrupt(ntb);
962 	if (ret) {
963 		dev_err(dev, "Interrupt configuration failed\n");
964 		goto err_config_interrupt;
965 	}
966 
967 	ret = epf_ntb_db_bar_init(ntb);
968 	if (ret) {
969 		dev_err(dev, "DB BAR init failed\n");
970 		goto err_db_bar_init;
971 	}
972 
973 	ret = epf_ntb_mw_bar_init(ntb);
974 	if (ret) {
975 		dev_err(dev, "MW BAR init failed\n");
976 		goto err_mw_bar_init;
977 	}
978 
979 	if (vfunc_no <= 1) {
980 		ret = pci_epc_write_header(epc, func_no, vfunc_no, epf->header);
981 		if (ret) {
982 			dev_err(dev, "Configuration header write failed\n");
983 			goto err_write_header;
984 		}
985 	}
986 
987 	INIT_DELAYED_WORK(&ntb->cmd_handler, epf_ntb_cmd_handler);
988 	queue_work(kpcintb_workqueue, &ntb->cmd_handler.work);
989 
990 	atomic64_set(&ntb->peer_db_pending, 0);
991 	enable_work(&ntb->peer_db_work);
992 
993 	return 0;
994 
995 err_write_header:
996 	epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
997 err_mw_bar_init:
998 	epf_ntb_db_bar_clear(ntb);
999 err_db_bar_init:
1000 err_config_interrupt:
1001 	epf_ntb_config_sspad_bar_clear(ntb);
1002 
1003 	return ret;
1004 }
1005 
1006 
1007 /**
1008  * epf_ntb_epc_cleanup() - Cleanup all NTB interfaces
1009  * @ntb: NTB device that facilitates communication between HOST and VHOST
1010  *
1011  * Wrapper to cleanup all NTB interfaces.
1012  */
epf_ntb_epc_cleanup(struct epf_ntb * ntb)1013 static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
1014 {
1015 	disable_delayed_work_sync(&ntb->cmd_handler);
1016 	disable_work_sync(&ntb->peer_db_work);
1017 	atomic64_set(&ntb->peer_db_pending, 0);
1018 	epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
1019 	epf_ntb_db_bar_clear(ntb);
1020 	epf_ntb_config_sspad_bar_clear(ntb);
1021 }
1022 
epf_ntb_epc_attached(struct epf_ntb * ntb)1023 static bool epf_ntb_epc_attached(struct epf_ntb *ntb)
1024 {
1025 	return ntb->epf->epc || ntb->epf->sec_epc;
1026 }
1027 
1028 #define EPF_NTB_R(_name)						\
1029 static ssize_t epf_ntb_##_name##_show(struct config_item *item,		\
1030 				      char *page)			\
1031 {									\
1032 	struct config_group *group = to_config_group(item);		\
1033 	struct epf_ntb *ntb = to_epf_ntb(group);			\
1034 									\
1035 	return sprintf(page, "%d\n", ntb->_name);			\
1036 }
1037 
1038 #define EPF_NTB_W(_name)						\
1039 static ssize_t epf_ntb_##_name##_store(struct config_item *item,	\
1040 				       const char *page, size_t len)	\
1041 {									\
1042 	struct config_group *group = to_config_group(item);		\
1043 	struct epf_ntb *ntb = to_epf_ntb(group);			\
1044 	u32 val;							\
1045 	int ret;							\
1046 									\
1047 	if (epf_ntb_epc_attached(ntb))					\
1048 		return -EOPNOTSUPP;					\
1049 									\
1050 	ret = kstrtou32(page, 0, &val);					\
1051 	if (ret)							\
1052 		return ret;						\
1053 									\
1054 	ntb->_name = val;						\
1055 									\
1056 	return len;							\
1057 }
1058 
1059 #define EPF_NTB_MW_R(_name)						\
1060 static ssize_t epf_ntb_##_name##_show(struct config_item *item,		\
1061 				      char *page)			\
1062 {									\
1063 	struct config_group *group = to_config_group(item);		\
1064 	struct epf_ntb *ntb = to_epf_ntb(group);			\
1065 	struct device *dev = &ntb->epf->dev;				\
1066 	int win_no, idx;						\
1067 									\
1068 	if (sscanf(#_name, "mw%d", &win_no) != 1)			\
1069 		return -EINVAL;						\
1070 									\
1071 	idx = win_no - 1;						\
1072 	if (idx < 0 || idx >= ntb->num_mws) {				\
1073 		dev_err(dev, "MW%d out of range (num_mws=%d)\n",	\
1074 			win_no, ntb->num_mws);				\
1075 		return -ERANGE;						\
1076 	}								\
1077 	idx = array_index_nospec(idx, ntb->num_mws);			\
1078 	return sprintf(page, "%llu\n", ntb->mws_size[idx]);		\
1079 }
1080 
1081 #define EPF_NTB_MW_W(_name)						\
1082 static ssize_t epf_ntb_##_name##_store(struct config_item *item,	\
1083 				       const char *page, size_t len)	\
1084 {									\
1085 	struct config_group *group = to_config_group(item);		\
1086 	struct epf_ntb *ntb = to_epf_ntb(group);			\
1087 	struct device *dev = &ntb->epf->dev;				\
1088 	int win_no, idx;						\
1089 	u64 val;							\
1090 	int ret;							\
1091 									\
1092 	if (epf_ntb_epc_attached(ntb))					\
1093 		return -EOPNOTSUPP;					\
1094 									\
1095 	ret = kstrtou64(page, 0, &val);					\
1096 	if (ret)							\
1097 		return ret;						\
1098 									\
1099 	if (sscanf(#_name, "mw%d", &win_no) != 1)			\
1100 		return -EINVAL;						\
1101 									\
1102 	idx = win_no - 1;						\
1103 	if (idx < 0 || idx >= ntb->num_mws) {				\
1104 		dev_err(dev, "MW%d out of range (num_mws=%d)\n",	\
1105 			win_no, ntb->num_mws);				\
1106 		return -ERANGE;						\
1107 	}								\
1108 	idx = array_index_nospec(idx, ntb->num_mws);			\
1109 	ntb->mws_size[idx] = val;					\
1110 									\
1111 	return len;							\
1112 }
1113 
1114 #define EPF_NTB_BAR_R(_name, _id)					\
1115 	static ssize_t epf_ntb_##_name##_show(struct config_item *item,	\
1116 					      char *page)		\
1117 	{								\
1118 		struct config_group *group = to_config_group(item);	\
1119 		struct epf_ntb *ntb = to_epf_ntb(group);		\
1120 									\
1121 		return sprintf(page, "%d\n", ntb->epf_ntb_bar[_id]);	\
1122 	}
1123 
1124 #define EPF_NTB_BAR_W(_name, _id)					\
1125 	static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
1126 					       const char *page, size_t len) \
1127 	{								\
1128 		struct config_group *group = to_config_group(item);	\
1129 		struct epf_ntb *ntb = to_epf_ntb(group);		\
1130 		int val;						\
1131 		int ret;						\
1132 									\
1133 		if (epf_ntb_epc_attached(ntb))				\
1134 			return -EOPNOTSUPP;				\
1135 									\
1136 		ret = kstrtoint(page, 0, &val);				\
1137 		if (ret)						\
1138 			return ret;					\
1139 									\
1140 		if (val < NO_BAR || val > BAR_5)			\
1141 			return -EINVAL;					\
1142 									\
1143 		ntb->epf_ntb_bar[_id] = val;				\
1144 									\
1145 		return len;						\
1146 	}
1147 
epf_ntb_num_mws_store(struct config_item * item,const char * page,size_t len)1148 static ssize_t epf_ntb_num_mws_store(struct config_item *item,
1149 				     const char *page, size_t len)
1150 {
1151 	struct config_group *group = to_config_group(item);
1152 	struct epf_ntb *ntb = to_epf_ntb(group);
1153 	u32 val;
1154 	int ret;
1155 
1156 	if (epf_ntb_epc_attached(ntb))
1157 		return -EOPNOTSUPP;
1158 
1159 	ret = kstrtou32(page, 0, &val);
1160 	if (ret)
1161 		return ret;
1162 
1163 	if (val > MAX_MW)
1164 		return -EINVAL;
1165 
1166 	ntb->num_mws = val;
1167 
1168 	return len;
1169 }
1170 
epf_ntb_db_count_store(struct config_item * item,const char * page,size_t len)1171 static ssize_t epf_ntb_db_count_store(struct config_item *item,
1172 				      const char *page, size_t len)
1173 {
1174 	struct config_group *group = to_config_group(item);
1175 	struct epf_ntb *ntb = to_epf_ntb(group);
1176 	u32 val;
1177 	int ret;
1178 
1179 	if (epf_ntb_epc_attached(ntb))
1180 		return -EOPNOTSUPP;
1181 
1182 	ret = kstrtou32(page, 0, &val);
1183 	if (ret)
1184 		return ret;
1185 
1186 	if (val < MIN_DB_COUNT || val > MAX_DB_COUNT)
1187 		return -EINVAL;
1188 
1189 	WRITE_ONCE(ntb->db_count, val);
1190 
1191 	return len;
1192 }
1193 
1194 EPF_NTB_R(spad_count)
1195 EPF_NTB_W(spad_count)
1196 EPF_NTB_R(db_count)
1197 EPF_NTB_R(num_mws)
1198 EPF_NTB_R(vbus_number)
1199 EPF_NTB_W(vbus_number)
1200 EPF_NTB_R(vntb_pid)
1201 EPF_NTB_W(vntb_pid)
1202 EPF_NTB_R(vntb_vid)
1203 EPF_NTB_W(vntb_vid)
1204 EPF_NTB_MW_R(mw1)
1205 EPF_NTB_MW_W(mw1)
1206 EPF_NTB_MW_R(mw2)
1207 EPF_NTB_MW_W(mw2)
1208 EPF_NTB_MW_R(mw3)
1209 EPF_NTB_MW_W(mw3)
1210 EPF_NTB_MW_R(mw4)
1211 EPF_NTB_MW_W(mw4)
1212 EPF_NTB_BAR_R(ctrl_bar, BAR_CONFIG)
1213 EPF_NTB_BAR_W(ctrl_bar, BAR_CONFIG)
1214 EPF_NTB_BAR_R(db_bar, BAR_DB)
1215 EPF_NTB_BAR_W(db_bar, BAR_DB)
1216 EPF_NTB_BAR_R(mw1_bar, BAR_MW1)
1217 EPF_NTB_BAR_W(mw1_bar, BAR_MW1)
1218 EPF_NTB_BAR_R(mw2_bar, BAR_MW2)
1219 EPF_NTB_BAR_W(mw2_bar, BAR_MW2)
1220 EPF_NTB_BAR_R(mw3_bar, BAR_MW3)
1221 EPF_NTB_BAR_W(mw3_bar, BAR_MW3)
1222 EPF_NTB_BAR_R(mw4_bar, BAR_MW4)
1223 EPF_NTB_BAR_W(mw4_bar, BAR_MW4)
1224 
1225 CONFIGFS_ATTR(epf_ntb_, spad_count);
1226 CONFIGFS_ATTR(epf_ntb_, db_count);
1227 CONFIGFS_ATTR(epf_ntb_, num_mws);
1228 CONFIGFS_ATTR(epf_ntb_, mw1);
1229 CONFIGFS_ATTR(epf_ntb_, mw2);
1230 CONFIGFS_ATTR(epf_ntb_, mw3);
1231 CONFIGFS_ATTR(epf_ntb_, mw4);
1232 CONFIGFS_ATTR(epf_ntb_, vbus_number);
1233 CONFIGFS_ATTR(epf_ntb_, vntb_pid);
1234 CONFIGFS_ATTR(epf_ntb_, vntb_vid);
1235 CONFIGFS_ATTR(epf_ntb_, ctrl_bar);
1236 CONFIGFS_ATTR(epf_ntb_, db_bar);
1237 CONFIGFS_ATTR(epf_ntb_, mw1_bar);
1238 CONFIGFS_ATTR(epf_ntb_, mw2_bar);
1239 CONFIGFS_ATTR(epf_ntb_, mw3_bar);
1240 CONFIGFS_ATTR(epf_ntb_, mw4_bar);
1241 
1242 static struct configfs_attribute *epf_ntb_attrs[] = {
1243 	&epf_ntb_attr_spad_count,
1244 	&epf_ntb_attr_db_count,
1245 	&epf_ntb_attr_num_mws,
1246 	&epf_ntb_attr_mw1,
1247 	&epf_ntb_attr_mw2,
1248 	&epf_ntb_attr_mw3,
1249 	&epf_ntb_attr_mw4,
1250 	&epf_ntb_attr_vbus_number,
1251 	&epf_ntb_attr_vntb_pid,
1252 	&epf_ntb_attr_vntb_vid,
1253 	&epf_ntb_attr_ctrl_bar,
1254 	&epf_ntb_attr_db_bar,
1255 	&epf_ntb_attr_mw1_bar,
1256 	&epf_ntb_attr_mw2_bar,
1257 	&epf_ntb_attr_mw3_bar,
1258 	&epf_ntb_attr_mw4_bar,
1259 	NULL,
1260 };
1261 
1262 static const struct config_item_type ntb_group_type = {
1263 	.ct_attrs	= epf_ntb_attrs,
1264 	.ct_owner	= THIS_MODULE,
1265 };
1266 
1267 /**
1268  * epf_ntb_add_cfs() - Add configfs directory specific to NTB
1269  * @epf: NTB endpoint function device
1270  * @group: A pointer to the config_group structure referencing a group of
1271  *	   config_items of a specific type that belong to a specific sub-system.
1272  *
1273  * Add configfs directory specific to NTB. This directory will hold
1274  * NTB specific properties like db_count, spad_count, num_mws etc.,
1275  *
1276  * Returns: Pointer to config_group
1277  */
epf_ntb_add_cfs(struct pci_epf * epf,struct config_group * group)1278 static struct config_group *epf_ntb_add_cfs(struct pci_epf *epf,
1279 					    struct config_group *group)
1280 {
1281 	struct epf_ntb *ntb = epf_get_drvdata(epf);
1282 	struct config_group *ntb_group = &ntb->group;
1283 	struct device *dev = &epf->dev;
1284 
1285 	config_group_init_type_name(ntb_group, dev_name(dev), &ntb_group_type);
1286 
1287 	return ntb_group;
1288 }
1289 
1290 /*==== virtual PCI bus driver, which only load virtual NTB PCI driver ====*/
1291 
1292 static u32 pci_space[] = {
1293 	0xffffffff,	/* Device ID, Vendor ID */
1294 	0,		/* Status, Command */
1295 	0xffffffff,	/* Base Class, Subclass, Prog Intf, Revision ID */
1296 	0x40,		/* BIST, Header Type, Latency Timer, Cache Line Size */
1297 	0,		/* BAR 0 */
1298 	0,		/* BAR 1 */
1299 	0,		/* BAR 2 */
1300 	0,		/* BAR 3 */
1301 	0,		/* BAR 4 */
1302 	0,		/* BAR 5 */
1303 	0,		/* Cardbus CIS Pointer */
1304 	0,		/* Subsystem ID, Subsystem Vendor ID */
1305 	0,		/* ROM Base Address */
1306 	0,		/* Reserved, Capabilities Pointer */
1307 	0,		/* Reserved */
1308 	0,		/* Max_Lat, Min_Gnt, Interrupt Pin, Interrupt Line */
1309 };
1310 
pci_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)1311 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val)
1312 {
1313 	if (devfn == 0) {
1314 		memcpy(val, ((u8 *)pci_space) + where, size);
1315 		return PCIBIOS_SUCCESSFUL;
1316 	}
1317 	return PCIBIOS_DEVICE_NOT_FOUND;
1318 }
1319 
pci_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)1320 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
1321 {
1322 	return 0;
1323 }
1324 
1325 static struct pci_ops vpci_ops = {
1326 	.read = pci_read,
1327 	.write = pci_write,
1328 };
1329 
vpci_scan_bus(void * sysdata)1330 static int vpci_scan_bus(void *sysdata)
1331 {
1332 	struct pci_bus *vpci_bus;
1333 	struct epf_ntb *ndev = sysdata;
1334 
1335 	vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
1336 	if (!vpci_bus) {
1337 		pr_err("create pci bus failed\n");
1338 		return -EINVAL;
1339 	}
1340 
1341 	pci_bus_add_devices(vpci_bus);
1342 
1343 	return 0;
1344 }
1345 
1346 /*==================== Virtual PCIe NTB driver ==========================*/
1347 
vntb_epf_mw_count(struct ntb_dev * ntb,int pidx)1348 static int vntb_epf_mw_count(struct ntb_dev *ntb, int pidx)
1349 {
1350 	struct epf_ntb *ndev = ntb_ndev(ntb);
1351 
1352 	return ndev->num_mws;
1353 }
1354 
vntb_epf_spad_count(struct ntb_dev * ntb)1355 static int vntb_epf_spad_count(struct ntb_dev *ntb)
1356 {
1357 	return ntb_ndev(ntb)->spad_count;
1358 }
1359 
vntb_epf_peer_mw_count(struct ntb_dev * ntb)1360 static int vntb_epf_peer_mw_count(struct ntb_dev *ntb)
1361 {
1362 	return ntb_ndev(ntb)->num_mws;
1363 }
1364 
vntb_epf_db_vector_count(struct ntb_dev * ntb)1365 static int vntb_epf_db_vector_count(struct ntb_dev *ntb)
1366 {
1367 	struct epf_ntb *ndev = ntb_ndev(ntb);
1368 	u32 db_count = READ_ONCE(ndev->db_count);
1369 
1370 	/*
1371 	 * db_count is the total number of doorbell slots exposed to
1372 	 * the peer, including:
1373 	 *   - slot #0 reserved for link events
1374 	 *   - slot #1 historically unused (kept for protocol compatibility)
1375 	 *
1376 	 * Report only usable per-vector doorbell interrupts.
1377 	 */
1378 	if (db_count < MIN_DB_COUNT || db_count > MAX_DB_COUNT)
1379 		return 0;
1380 
1381 	return db_count - EPF_IRQ_DB_START;
1382 }
1383 
vntb_epf_db_valid_mask(struct ntb_dev * ntb)1384 static u64 vntb_epf_db_valid_mask(struct ntb_dev *ntb)
1385 {
1386 	int nr_vec = vntb_epf_db_vector_count(ntb);
1387 
1388 	if (!nr_vec)
1389 		return 0;
1390 
1391 	return GENMASK_ULL(nr_vec - 1, 0);
1392 }
1393 
vntb_epf_db_vector_mask(struct ntb_dev * ntb,int db_vector)1394 static u64 vntb_epf_db_vector_mask(struct ntb_dev *ntb, int db_vector)
1395 {
1396 	int nr_vec;
1397 
1398 	/*
1399 	 * Doorbell vectors are numbered [0 .. nr_vec - 1], where nr_vec
1400 	 * excludes the two reserved slots described above.
1401 	 */
1402 	nr_vec = vntb_epf_db_vector_count(ntb);
1403 	if (db_vector < 0 || db_vector >= nr_vec)
1404 		return 0;
1405 
1406 	return BIT_ULL(db_vector);
1407 }
1408 
vntb_epf_db_set_mask(struct ntb_dev * ntb,u64 db_bits)1409 static int vntb_epf_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1410 {
1411 	return 0;
1412 }
1413 
vntb_epf_mw_set_trans(struct ntb_dev * ndev,int pidx,int idx,dma_addr_t addr,resource_size_t size)1414 static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
1415 		dma_addr_t addr, resource_size_t size)
1416 {
1417 	struct epf_ntb *ntb = ntb_ndev(ndev);
1418 	struct pci_epf_bar *epf_bar;
1419 	enum pci_barno barno;
1420 	int ret;
1421 	struct device *dev;
1422 
1423 	dev = &ntb->ntb.dev;
1424 	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
1425 	epf_bar = &ntb->epf->bar[barno];
1426 	epf_bar->phys_addr = addr;
1427 	epf_bar->barno = barno;
1428 	epf_bar->size = size;
1429 
1430 	ret = pci_epc_set_bar(ntb->epf->epc, 0, 0, epf_bar);
1431 	if (ret) {
1432 		dev_err(dev, "failure set mw trans\n");
1433 		return ret;
1434 	}
1435 	return 0;
1436 }
1437 
vntb_epf_mw_clear_trans(struct ntb_dev * ntb,int pidx,int idx)1438 static int vntb_epf_mw_clear_trans(struct ntb_dev *ntb, int pidx, int idx)
1439 {
1440 	return 0;
1441 }
1442 
vntb_epf_peer_mw_get_addr(struct ntb_dev * ndev,int idx,phys_addr_t * base,resource_size_t * size)1443 static int vntb_epf_peer_mw_get_addr(struct ntb_dev *ndev, int idx,
1444 				phys_addr_t *base, resource_size_t *size)
1445 {
1446 
1447 	struct epf_ntb *ntb = ntb_ndev(ndev);
1448 
1449 	if (base)
1450 		*base = ntb->vpci_mw_phy[idx];
1451 
1452 	if (size)
1453 		*size = ntb->mws_size[idx];
1454 
1455 	return 0;
1456 }
1457 
vntb_epf_link_enable(struct ntb_dev * ntb,enum ntb_speed max_speed,enum ntb_width max_width)1458 static int vntb_epf_link_enable(struct ntb_dev *ntb,
1459 			enum ntb_speed max_speed,
1460 			enum ntb_width max_width)
1461 {
1462 	return 0;
1463 }
1464 
vntb_epf_spad_read(struct ntb_dev * ndev,int idx)1465 static u32 vntb_epf_spad_read(struct ntb_dev *ndev, int idx)
1466 {
1467 	struct epf_ntb *ntb = ntb_ndev(ndev);
1468 	int off = ntb->reg->spad_offset, ct = ntb->reg->spad_count * sizeof(u32);
1469 	u32 val;
1470 	void __iomem *base = (void __iomem *)ntb->reg;
1471 
1472 	val = readl(base + off + ct + idx * sizeof(u32));
1473 	return val;
1474 }
1475 
vntb_epf_spad_write(struct ntb_dev * ndev,int idx,u32 val)1476 static int vntb_epf_spad_write(struct ntb_dev *ndev, int idx, u32 val)
1477 {
1478 	struct epf_ntb *ntb = ntb_ndev(ndev);
1479 	struct epf_ntb_ctrl *ctrl = ntb->reg;
1480 	int off = ctrl->spad_offset, ct = ctrl->spad_count * sizeof(u32);
1481 	void __iomem *base = (void __iomem *)ntb->reg;
1482 
1483 	writel(val, base + off + ct + idx * sizeof(u32));
1484 	return 0;
1485 }
1486 
vntb_epf_peer_spad_read(struct ntb_dev * ndev,int pidx,int idx)1487 static u32 vntb_epf_peer_spad_read(struct ntb_dev *ndev, int pidx, int idx)
1488 {
1489 	struct epf_ntb *ntb = ntb_ndev(ndev);
1490 	struct epf_ntb_ctrl *ctrl = ntb->reg;
1491 	int off = ctrl->spad_offset;
1492 	void __iomem *base = (void __iomem *)ntb->reg;
1493 	u32 val;
1494 
1495 	val = readl(base + off + idx * sizeof(u32));
1496 	return val;
1497 }
1498 
vntb_epf_peer_spad_write(struct ntb_dev * ndev,int pidx,int idx,u32 val)1499 static int vntb_epf_peer_spad_write(struct ntb_dev *ndev, int pidx, int idx, u32 val)
1500 {
1501 	struct epf_ntb *ntb = ntb_ndev(ndev);
1502 	struct epf_ntb_ctrl *ctrl = ntb->reg;
1503 	int off = ctrl->spad_offset;
1504 	void __iomem *base = (void __iomem *)ntb->reg;
1505 
1506 	writel(val, base + off + idx * sizeof(u32));
1507 	return 0;
1508 }
1509 
vntb_epf_peer_db_work(struct work_struct * work)1510 static void vntb_epf_peer_db_work(struct work_struct *work)
1511 {
1512 	struct epf_ntb *ntb = container_of(work, struct epf_ntb, peer_db_work);
1513 	struct pci_epf *epf = ntb->epf;
1514 	unsigned int budget = VNTB_PEER_DB_WORK_BUDGET;
1515 	u8 func_no, vfunc_no;
1516 	unsigned int db_bit;
1517 	u32 interrupt_num;
1518 	u64 db_bits;
1519 	int ret;
1520 
1521 	if (!epf || !epf->epc)
1522 		return;
1523 
1524 	func_no = epf->func_no;
1525 	vfunc_no = epf->vfunc_no;
1526 
1527 	/*
1528 	 * Drain doorbells from peer_db_pending in snapshots (atomic64_xchg()).
1529 	 * Limit the number of snapshots handled per run so we don't monopolize
1530 	 * the workqueue under a doorbell storm.
1531 	 */
1532 	while (budget--) {
1533 		db_bits = atomic64_xchg(&ntb->peer_db_pending, 0);
1534 		if (!db_bits)
1535 			return;
1536 
1537 		while (db_bits) {
1538 			/*
1539 			 * pci_epc_raise_irq() for MSI expects a 1-based
1540 			 * interrupt number. The first usable doorbell starts
1541 			 * at EPF_IRQ_DB_START in the legacy slot layout.
1542 			 *
1543 			 * Legacy mapping (kept for compatibility):
1544 			 *
1545 			 *   MSI #1 : link event (reserved)
1546 			 *   MSI #2 : unused (historical offset)
1547 			 *   MSI #3 : doorbell bit 0 (DB#0)
1548 			 *   MSI #4 : doorbell bit 1 (DB#1)
1549 			 *   ...
1550 			 *
1551 			 * Do not change this mapping to avoid breaking
1552 			 * interoperability with older peers.
1553 			 */
1554 			db_bit = __ffs64(db_bits);
1555 			interrupt_num = db_bit + EPF_IRQ_DB_START + 1;
1556 			db_bits &= ~BIT_ULL(db_bit);
1557 
1558 			ret = pci_epc_raise_irq(epf->epc, func_no, vfunc_no,
1559 						PCI_IRQ_MSI, interrupt_num);
1560 			if (ret)
1561 				dev_err(&ntb->ntb.dev,
1562 					"Failed to raise IRQ for interrupt_num %u: %d\n",
1563 					interrupt_num, ret);
1564 		}
1565 	}
1566 
1567 	if (atomic64_read(&ntb->peer_db_pending))
1568 		queue_work(kpcintb_workqueue, &ntb->peer_db_work);
1569 }
1570 
vntb_epf_peer_db_set(struct ntb_dev * ndev,u64 db_bits)1571 static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
1572 {
1573 	struct epf_ntb *ntb = ntb_ndev(ndev);
1574 
1575 	db_bits &= vntb_epf_db_valid_mask(ndev);
1576 	if (!db_bits)
1577 		return 0;
1578 
1579 	/*
1580 	 * .peer_db_set() may be called from atomic context. pci_epc_raise_irq()
1581 	 * can sleep (it takes epc->lock), so defer MSI raising to process
1582 	 * context. Doorbell requests are coalesced in peer_db_pending.
1583 	 */
1584 	atomic64_or(db_bits, &ntb->peer_db_pending);
1585 	queue_work(kpcintb_workqueue, &ntb->peer_db_work);
1586 
1587 	return 0;
1588 }
1589 
vntb_epf_db_read(struct ntb_dev * ndev)1590 static u64 vntb_epf_db_read(struct ntb_dev *ndev)
1591 {
1592 	struct epf_ntb *ntb = ntb_ndev(ndev);
1593 
1594 	return atomic64_read(&ntb->db);
1595 }
1596 
vntb_epf_mw_get_align(struct ntb_dev * ndev,int pidx,int idx,resource_size_t * addr_align,resource_size_t * size_align,resource_size_t * size_max)1597 static int vntb_epf_mw_get_align(struct ntb_dev *ndev, int pidx, int idx,
1598 			resource_size_t *addr_align,
1599 			resource_size_t *size_align,
1600 			resource_size_t *size_max)
1601 {
1602 	struct epf_ntb *ntb = ntb_ndev(ndev);
1603 
1604 	if (addr_align)
1605 		*addr_align = SZ_4K;
1606 
1607 	if (size_align)
1608 		*size_align = 1;
1609 
1610 	if (size_max)
1611 		*size_max = ntb->mws_size[idx];
1612 
1613 	return 0;
1614 }
1615 
vntb_epf_link_is_up(struct ntb_dev * ndev,enum ntb_speed * speed,enum ntb_width * width)1616 static u64 vntb_epf_link_is_up(struct ntb_dev *ndev,
1617 			enum ntb_speed *speed,
1618 			enum ntb_width *width)
1619 {
1620 	struct epf_ntb *ntb = ntb_ndev(ndev);
1621 
1622 	return ntb->reg->link_status;
1623 }
1624 
vntb_epf_db_clear_mask(struct ntb_dev * ndev,u64 db_bits)1625 static int vntb_epf_db_clear_mask(struct ntb_dev *ndev, u64 db_bits)
1626 {
1627 	return 0;
1628 }
1629 
vntb_epf_db_clear(struct ntb_dev * ndev,u64 db_bits)1630 static int vntb_epf_db_clear(struct ntb_dev *ndev, u64 db_bits)
1631 {
1632 	struct epf_ntb *ntb = ntb_ndev(ndev);
1633 
1634 	atomic64_and(~db_bits, &ntb->db);
1635 	return 0;
1636 }
1637 
vntb_epf_link_disable(struct ntb_dev * ntb)1638 static int vntb_epf_link_disable(struct ntb_dev *ntb)
1639 {
1640 	return 0;
1641 }
1642 
vntb_epf_get_dma_dev(struct ntb_dev * ndev)1643 static struct device *vntb_epf_get_dma_dev(struct ntb_dev *ndev)
1644 {
1645 	struct epf_ntb *ntb = ntb_ndev(ndev);
1646 	struct pci_epc *epc = ntb->epf->epc;
1647 
1648 	return epc->dev.parent;
1649 }
1650 
1651 static const struct ntb_dev_ops vntb_epf_ops = {
1652 	.mw_count		= vntb_epf_mw_count,
1653 	.spad_count		= vntb_epf_spad_count,
1654 	.peer_mw_count		= vntb_epf_peer_mw_count,
1655 	.db_valid_mask		= vntb_epf_db_valid_mask,
1656 	.db_vector_count	= vntb_epf_db_vector_count,
1657 	.db_vector_mask		= vntb_epf_db_vector_mask,
1658 	.db_set_mask		= vntb_epf_db_set_mask,
1659 	.mw_set_trans		= vntb_epf_mw_set_trans,
1660 	.mw_clear_trans		= vntb_epf_mw_clear_trans,
1661 	.peer_mw_get_addr	= vntb_epf_peer_mw_get_addr,
1662 	.link_enable		= vntb_epf_link_enable,
1663 	.spad_read		= vntb_epf_spad_read,
1664 	.spad_write		= vntb_epf_spad_write,
1665 	.peer_spad_read		= vntb_epf_peer_spad_read,
1666 	.peer_spad_write	= vntb_epf_peer_spad_write,
1667 	.peer_db_set		= vntb_epf_peer_db_set,
1668 	.db_read		= vntb_epf_db_read,
1669 	.mw_get_align		= vntb_epf_mw_get_align,
1670 	.link_is_up		= vntb_epf_link_is_up,
1671 	.db_clear_mask		= vntb_epf_db_clear_mask,
1672 	.db_clear		= vntb_epf_db_clear,
1673 	.link_disable		= vntb_epf_link_disable,
1674 	.get_dma_dev		= vntb_epf_get_dma_dev,
1675 };
1676 
pci_vntb_probe(struct pci_dev * pdev,const struct pci_device_id * id)1677 static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1678 {
1679 	int ret;
1680 	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
1681 	struct device *dev = &pdev->dev;
1682 
1683 	ndev->ntb.pdev = pdev;
1684 	ndev->ntb.topo = NTB_TOPO_NONE;
1685 	ndev->ntb.ops =  &vntb_epf_ops;
1686 
1687 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1688 	if (ret) {
1689 		dev_err(dev, "Cannot set DMA mask\n");
1690 		return ret;
1691 	}
1692 
1693 	ret = ntb_register_device(&ndev->ntb);
1694 	if (ret) {
1695 		dev_err(dev, "Failed to register NTB device\n");
1696 		return ret;
1697 	}
1698 
1699 	dev_dbg(dev, "PCI Virtual NTB driver loaded\n");
1700 	return 0;
1701 }
1702 
1703 static struct pci_device_id pci_vntb_table[] = {
1704 	{
1705 		PCI_DEVICE(0xffff, 0xffff),
1706 	},
1707 	{},
1708 };
1709 
1710 static struct pci_driver vntb_pci_driver = {
1711 	.name           = "pci-vntb",
1712 	.id_table       = pci_vntb_table,
1713 	.probe          = pci_vntb_probe,
1714 };
1715 
1716 /* ============ PCIe EPF Driver Bind ====================*/
1717 
1718 /**
1719  * epf_ntb_bind() - Initialize endpoint controller to provide NTB functionality
1720  * @epf: NTB endpoint function device
1721  *
1722  * Initialize both the endpoint controllers associated with NTB function device.
1723  * Invoked when a primary interface or secondary interface is bound to EPC
1724  * device. This function will succeed only when EPC is bound to both the
1725  * interfaces.
1726  *
1727  * Returns: Zero for success, or an error code in case of failure
1728  */
epf_ntb_bind(struct pci_epf * epf)1729 static int epf_ntb_bind(struct pci_epf *epf)
1730 {
1731 	struct epf_ntb *ntb = epf_get_drvdata(epf);
1732 	struct device *dev = &epf->dev;
1733 	int ret;
1734 
1735 	if (!epf->epc) {
1736 		dev_dbg(dev, "PRIMARY EPC interface not yet bound\n");
1737 		return 0;
1738 	}
1739 
1740 	ret = epf_ntb_init_epc_bar(ntb);
1741 	if (ret) {
1742 		dev_err(dev, "Failed to create NTB EPC\n");
1743 		return ret;
1744 	}
1745 
1746 	ret = epf_ntb_config_spad_bar_alloc(ntb);
1747 	if (ret) {
1748 		dev_err(dev, "Failed to allocate BAR memory\n");
1749 		goto err_bar_alloc;
1750 	}
1751 
1752 	ret = epf_ntb_epc_init(ntb);
1753 	if (ret) {
1754 		dev_err(dev, "Failed to initialize EPC\n");
1755 		goto err_bar_alloc;
1756 	}
1757 
1758 	epf_set_drvdata(epf, ntb);
1759 
1760 	pci_space[0] = (ntb->vntb_pid << 16) | ntb->vntb_vid;
1761 	pci_vntb_table[0].vendor = ntb->vntb_vid;
1762 	pci_vntb_table[0].device = ntb->vntb_pid;
1763 
1764 	ret = pci_register_driver(&vntb_pci_driver);
1765 	if (ret) {
1766 		dev_err(dev, "failure register vntb pci driver\n");
1767 		goto err_epc_cleanup;
1768 	}
1769 
1770 	ret = vpci_scan_bus(ntb);
1771 	if (ret)
1772 		goto err_unregister;
1773 
1774 	return 0;
1775 
1776 err_unregister:
1777 	pci_unregister_driver(&vntb_pci_driver);
1778 err_epc_cleanup:
1779 	epf_ntb_epc_cleanup(ntb);
1780 err_bar_alloc:
1781 	epf_ntb_config_spad_bar_free(ntb);
1782 
1783 	return ret;
1784 }
1785 
1786 /**
1787  * epf_ntb_unbind() - Cleanup the initialization from epf_ntb_bind()
1788  * @epf: NTB endpoint function device
1789  *
1790  * Cleanup the initialization from epf_ntb_bind()
1791  */
epf_ntb_unbind(struct pci_epf * epf)1792 static void epf_ntb_unbind(struct pci_epf *epf)
1793 {
1794 	struct epf_ntb *ntb = epf_get_drvdata(epf);
1795 
1796 	epf_ntb_epc_cleanup(ntb);
1797 	epf_ntb_config_spad_bar_free(ntb);
1798 
1799 	pci_unregister_driver(&vntb_pci_driver);
1800 }
1801 
1802 // EPF driver probe
1803 static const struct pci_epf_ops epf_ntb_ops = {
1804 	.bind   = epf_ntb_bind,
1805 	.unbind = epf_ntb_unbind,
1806 	.add_cfs = epf_ntb_add_cfs,
1807 };
1808 
1809 /**
1810  * epf_ntb_probe() - Probe NTB function driver
1811  * @epf: NTB endpoint function device
1812  * @id: NTB endpoint function device ID
1813  *
1814  * Probe NTB function driver when endpoint function bus detects a NTB
1815  * endpoint function.
1816  *
1817  * Returns: Zero for success, or an error code in case of failure
1818  */
epf_ntb_probe(struct pci_epf * epf,const struct pci_epf_device_id * id)1819 static int epf_ntb_probe(struct pci_epf *epf,
1820 			 const struct pci_epf_device_id *id)
1821 {
1822 	struct epf_ntb *ntb;
1823 	struct device *dev;
1824 	int i;
1825 
1826 	dev = &epf->dev;
1827 
1828 	ntb = devm_kzalloc(dev, sizeof(*ntb), GFP_KERNEL);
1829 	if (!ntb)
1830 		return -ENOMEM;
1831 
1832 	epf->header = &epf_ntb_header;
1833 	ntb->epf = epf;
1834 	ntb->vbus_number = 0xff;
1835 
1836 	INIT_WORK(&ntb->peer_db_work, vntb_epf_peer_db_work);
1837 	disable_work(&ntb->peer_db_work);
1838 	atomic64_set(&ntb->peer_db_pending, 0);
1839 
1840 	/* Initially, no bar is assigned */
1841 	for (i = 0; i < VNTB_BAR_NUM; i++)
1842 		ntb->epf_ntb_bar[i] = NO_BAR;
1843 
1844 	epf_set_drvdata(epf, ntb);
1845 
1846 	dev_info(dev, "pci-ep epf driver loaded\n");
1847 	return 0;
1848 }
1849 
1850 static const struct pci_epf_device_id epf_ntb_ids[] = {
1851 	{
1852 		.name = "pci_epf_vntb",
1853 	},
1854 	{},
1855 };
1856 
1857 static struct pci_epf_driver epf_ntb_driver = {
1858 	.driver.name    = "pci_epf_vntb",
1859 	.probe          = epf_ntb_probe,
1860 	.id_table       = epf_ntb_ids,
1861 	.ops            = &epf_ntb_ops,
1862 	.owner          = THIS_MODULE,
1863 };
1864 
epf_ntb_init(void)1865 static int __init epf_ntb_init(void)
1866 {
1867 	int ret;
1868 
1869 	kpcintb_workqueue = alloc_workqueue("kpcintb",
1870 				    WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_PERCPU, 0);
1871 	if (!kpcintb_workqueue) {
1872 		pr_err("Failed to allocate kpcintb workqueue\n");
1873 		return -ENOMEM;
1874 	}
1875 
1876 	ret = pci_epf_register_driver(&epf_ntb_driver);
1877 	if (ret) {
1878 		destroy_workqueue(kpcintb_workqueue);
1879 		pr_err("Failed to register pci epf ntb driver --> %d\n", ret);
1880 		return ret;
1881 	}
1882 
1883 	return 0;
1884 }
1885 module_init(epf_ntb_init);
1886 
epf_ntb_exit(void)1887 static void __exit epf_ntb_exit(void)
1888 {
1889 	pci_epf_unregister_driver(&epf_ntb_driver);
1890 	destroy_workqueue(kpcintb_workqueue);
1891 }
1892 module_exit(epf_ntb_exit);
1893 
1894 MODULE_DESCRIPTION("PCI EPF NTB DRIVER");
1895 MODULE_AUTHOR("Frank Li <Frank.li@nxp.com>");
1896 MODULE_LICENSE("GPL v2");
1897