1 // SPDX-License-Identifier: GPL-2.0
3 * Endpoint Function Driver to implement Non-Transparent Bridge functionality
5 * Copyright (C) 2020 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
10 * The PCI NTB function driver configures the SoC with multiple PCIe Endpoint
11 * (EP) controller instances (see diagram below) in such a way that
12 * transactions from one EP controller are routed to the other EP controller.
13 * Once PCI NTB function driver configures the SoC with multiple EP instances,
14 * HOST1 and HOST2 can communicate with each other using SoC as a bridge.
16 * +-------------+ +-------------+
20 * +------^------+ +------^------+
23 * +---------|-------------------------------------------------|---------+
24 * | +------v------+ +------v------+ |
27 * | | CONTROLLER1 | | CONTROLLER2 | |
28 * | | <-----------------------------------> | |
31 * | | | SoC With Multiple EP Instances | | |
32 * | | | (Configured using NTB Function) | | |
33 * | +-------------+ +-------------+ |
34 * +---------------------------------------------------------------------+
37 #include <linux/delay.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
42 #include <linux/pci-epc.h>
43 #include <linux/pci-epf.h>
45 static struct workqueue_struct *kpcintb_workqueue;
47 #define COMMAND_CONFIGURE_DOORBELL 1
48 #define COMMAND_TEARDOWN_DOORBELL 2
49 #define COMMAND_CONFIGURE_MW 3
50 #define COMMAND_TEARDOWN_MW 4
51 #define COMMAND_LINK_UP 5
52 #define COMMAND_LINK_DOWN 6
54 #define COMMAND_STATUS_OK 1
55 #define COMMAND_STATUS_ERROR 2
57 #define LINK_STATUS_UP BIT(0)
61 #define NTB_MW_OFFSET 2
62 #define DB_COUNT_MASK GENMASK(15, 0)
63 #define MSIX_ENABLE BIT(16)
64 #define MAX_DB_COUNT 32
82 struct config_group group;
83 struct epf_ntb_epc *epc[2];
86 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
95 struct epf_ntb *epf_ntb;
96 void __iomem *mw_addr[6];
97 size_t msix_table_offset;
98 struct epf_ntb_ctrl *reg;
99 struct pci_epf_bar *epf_bar;
100 enum pci_barno epf_ntb_bar[6];
101 struct delayed_work cmd_handler;
102 enum pci_epc_interface_type type;
103 const struct pci_epc_features *epc_features;
106 struct epf_ntb_ctrl {
119 u32 db_data[MAX_DB_COUNT];
120 u32 db_offset[MAX_DB_COUNT];
123 static struct pci_epf_header epf_ntb_header = {
124 .vendorid = PCI_ANY_ID,
125 .deviceid = PCI_ANY_ID,
126 .baseclass_code = PCI_BASE_CLASS_MEMORY,
127 .interrupt_pin = PCI_INTERRUPT_INTA,
131 * epf_ntb_link_up() - Raise link_up interrupt to both the hosts
132 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
133 * @link_up: true or false indicating Link is UP or Down
135 * Once NTB function in HOST1 and the NTB function in HOST2 invoke
136 * ntb_link_enable(), this NTB function driver will trigger a link event to
137 * the NTB client in both the hosts.
139 static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
141 enum pci_epc_interface_type type;
142 enum pci_epc_irq_type irq_type;
143 struct epf_ntb_epc *ntb_epc;
144 struct epf_ntb_ctrl *ctrl;
150 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
151 ntb_epc = ntb->epc[type];
153 func_no = ntb_epc->func_no;
154 is_msix = ntb_epc->is_msix;
157 ctrl->link_status |= LINK_STATUS_UP;
159 ctrl->link_status &= ~LINK_STATUS_UP;
160 irq_type = is_msix ? PCI_EPC_IRQ_MSIX : PCI_EPC_IRQ_MSI;
161 ret = pci_epc_raise_irq(epc, func_no, irq_type, 1);
164 "%s intf: Failed to raise Link Up IRQ\n",
165 pci_epc_interface_string(type));
174 * epf_ntb_configure_mw() - Configure the Outbound Address Space for one host
175 * to access the memory window of other host
176 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
177 * @type: PRIMARY interface or SECONDARY interface
178 * @mw: Index of the memory window (either 0, 1, 2 or 3)
180 * +-----------------+ +---->+----------------+-----------+-----------------+
181 * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
182 * +-----------------+ | +----------------+ +-----------------+
183 * | BAR1 | | | Doorbell 2 +---------+ | |
184 * +-----------------+----+ +----------------+ | | |
185 * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
186 * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
187 * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
188 * +-----------------+ | |----------------+ | | | |
189 * | BAR4 | | | | | | +-----------------+
190 * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
191 * | BAR5 | | | | | | +-----------------+
192 * +-----------------+ +---->-----------------+ | | | |
193 * EP CONTROLLER 1 | | | | +-----------------+
194 * | | | +---->+ MSI|X ADDRESS 4 |
195 * +----------------+ | +-----------------+
196 * (A) EP CONTROLLER 2 | | |
201 * (B) +-----------------+
207 * +-----------------+
211 * This function performs stage (B) in the above diagram (see MW1) i.e., map OB
212 * address space of memory window to PCI address space.
214 * This operation requires 3 parameters
215 * 1) Address in the outbound address space
216 * 2) Address in the PCI Address space
217 * 3) Size of the address region to be mapped
219 * The address in the outbound address space (for MW1, MW2, MW3 and MW4) is
220 * stored in epf_bar corresponding to BAR_DB_MW1 for MW1 and BAR_MW2, BAR_MW3
221 * BAR_MW4 for rest of the BARs of epf_ntb_epc that is connected to HOST1. This
222 * is populated in epf_ntb_alloc_peer_mem() in this driver.
224 * The address and size of the PCI address region that has to be mapped would
225 * be provided by HOST2 in ctrl->addr and ctrl->size of epf_ntb_epc that is
226 * connected to HOST2.
228 * Please note Memory window1 (MW1) and Doorbell registers together will be
229 * mapped to a single BAR (BAR2) above for 32-bit BARs. The exact BAR that's
230 * used for Memory window (MW) can be obtained from epf_ntb_bar[BAR_DB_MW1],
231 * epf_ntb_bar[BAR_MW2], epf_ntb_bar[BAR_MW2], epf_ntb_bar[BAR_MW2].
233 static int epf_ntb_configure_mw(struct epf_ntb *ntb,
234 enum pci_epc_interface_type type, u32 mw)
236 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
237 struct pci_epf_bar *peer_epf_bar;
238 enum pci_barno peer_barno;
239 struct epf_ntb_ctrl *ctrl;
240 phys_addr_t phys_addr;
246 ntb_epc = ntb->epc[type];
249 peer_ntb_epc = ntb->epc[!type];
250 peer_barno = peer_ntb_epc->epf_ntb_bar[mw + NTB_MW_OFFSET];
251 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
253 phys_addr = peer_epf_bar->phys_addr;
257 if (mw + NTB_MW_OFFSET == BAR_DB_MW1)
258 phys_addr += ctrl->mw1_offset;
260 if (size > ntb->mws_size[mw]) {
262 "%s intf: MW: %d Req Sz:%llxx > Supported Sz:%llx\n",
263 pci_epc_interface_string(type), mw, size,
266 goto err_invalid_size;
269 func_no = ntb_epc->func_no;
271 ret = pci_epc_map_addr(epc, func_no, phys_addr, addr, size);
274 "%s intf: Failed to map memory window %d address\n",
275 pci_epc_interface_string(type), mw);
283 * epf_ntb_teardown_mw() - Teardown the configured OB ATU
284 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
285 * @type: PRIMARY interface or SECONDARY interface
286 * @mw: Index of the memory window (either 0, 1, 2 or 3)
288 * Teardown the configured OB ATU configured in epf_ntb_configure_mw() using
289 * pci_epc_unmap_addr()
291 static void epf_ntb_teardown_mw(struct epf_ntb *ntb,
292 enum pci_epc_interface_type type, u32 mw)
294 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
295 struct pci_epf_bar *peer_epf_bar;
296 enum pci_barno peer_barno;
297 struct epf_ntb_ctrl *ctrl;
298 phys_addr_t phys_addr;
302 ntb_epc = ntb->epc[type];
305 peer_ntb_epc = ntb->epc[!type];
306 peer_barno = peer_ntb_epc->epf_ntb_bar[mw + NTB_MW_OFFSET];
307 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
309 phys_addr = peer_epf_bar->phys_addr;
311 if (mw + NTB_MW_OFFSET == BAR_DB_MW1)
312 phys_addr += ctrl->mw1_offset;
313 func_no = ntb_epc->func_no;
315 pci_epc_unmap_addr(epc, func_no, phys_addr);
319 * epf_ntb_configure_msi() - Map OB address space to MSI address
320 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
321 * @type: PRIMARY interface or SECONDARY interface
322 * @db_count: Number of doorbell interrupts to map
324 *+-----------------+ +----->+----------------+-----------+-----------------+
325 *| BAR0 | | | Doorbell 1 +---+-------> MSI ADDRESS |
326 *+-----------------+ | +----------------+ | +-----------------+
327 *| BAR1 | | | Doorbell 2 +---+ | |
328 *+-----------------+----+ +----------------+ | | |
329 *| BAR2 | | Doorbell 3 +---+ | |
330 *+-----------------+----+ +----------------+ | | |
331 *| BAR3 | | | Doorbell 4 +---+ | |
332 *+-----------------+ | |----------------+ | |
334 *+-----------------+ | | MW1 | | |
336 *+-----------------+ +----->-----------------+ | |
337 * EP CONTROLLER 1 | | | |
339 * +----------------+ +-----------------+
340 * (A) EP CONTROLLER 2 | |
345 * (B) +-----------------+
351 * +-----------------+
356 * This function performs stage (B) in the above diagram (see Doorbell 1,
357 * Doorbell 2, Doorbell 3, Doorbell 4) i.e map OB address space corresponding to
358 * doorbell to MSI address in PCI address space.
360 * This operation requires 3 parameters
361 * 1) Address reserved for doorbell in the outbound address space
362 * 2) MSI-X address in the PCIe Address space
363 * 3) Number of MSI-X interrupts that has to be configured
365 * The address in the outbound address space (for the Doorbell) is stored in
366 * epf_bar corresponding to BAR_DB_MW1 of epf_ntb_epc that is connected to
367 * HOST1. This is populated in epf_ntb_alloc_peer_mem() in this driver along
368 * with address for MW1.
370 * pci_epc_map_msi_irq() takes the MSI address from MSI capability register
371 * and maps the OB address (obtained in epf_ntb_alloc_peer_mem()) to the MSI
374 * epf_ntb_configure_msi() also stores the MSI data to raise each interrupt
375 * in db_data of the peer's control region. This helps the peer to raise
376 * doorbell of the other host by writing db_data to the BAR corresponding to
379 static int epf_ntb_configure_msi(struct epf_ntb *ntb,
380 enum pci_epc_interface_type type, u16 db_count)
382 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
383 u32 db_entry_size, db_data, db_offset;
384 struct pci_epf_bar *peer_epf_bar;
385 struct epf_ntb_ctrl *peer_ctrl;
386 enum pci_barno peer_barno;
387 phys_addr_t phys_addr;
392 ntb_epc = ntb->epc[type];
395 peer_ntb_epc = ntb->epc[!type];
396 peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
397 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
398 peer_ctrl = peer_ntb_epc->reg;
399 db_entry_size = peer_ctrl->db_entry_size;
401 phys_addr = peer_epf_bar->phys_addr;
402 func_no = ntb_epc->func_no;
404 ret = pci_epc_map_msi_irq(epc, func_no, phys_addr, db_count,
405 db_entry_size, &db_data, &db_offset);
407 dev_err(&epc->dev, "%s intf: Failed to map MSI IRQ\n",
408 pci_epc_interface_string(type));
412 for (i = 0; i < db_count; i++) {
413 peer_ctrl->db_data[i] = db_data | i;
414 peer_ctrl->db_offset[i] = db_offset;
421 * epf_ntb_configure_msix() - Map OB address space to MSI-X address
422 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
423 * @type: PRIMARY interface or SECONDARY interface
424 * @db_count: Number of doorbell interrupts to map
426 *+-----------------+ +----->+----------------+-----------+-----------------+
427 *| BAR0 | | | Doorbell 1 +-----------> MSI-X ADDRESS 1 |
428 *+-----------------+ | +----------------+ +-----------------+
429 *| BAR1 | | | Doorbell 2 +---------+ | |
430 *+-----------------+----+ +----------------+ | | |
431 *| BAR2 | | Doorbell 3 +-------+ | +-----------------+
432 *+-----------------+----+ +----------------+ | +-> MSI-X ADDRESS 2 |
433 *| BAR3 | | | Doorbell 4 +-----+ | +-----------------+
434 *+-----------------+ | |----------------+ | | | |
435 *| BAR4 | | | | | | +-----------------+
436 *+-----------------+ | | MW1 + | +-->+ MSI-X ADDRESS 3||
437 *| BAR5 | | | | | +-----------------+
438 *+-----------------+ +----->-----------------+ | | |
439 * EP CONTROLLER 1 | | | +-----------------+
440 * | | +---->+ MSI-X ADDRESS 4 |
441 * +----------------+ +-----------------+
442 * (A) EP CONTROLLER 2 | |
447 * (B) +-----------------+
453 * +-----------------+
457 * This function performs stage (B) in the above diagram (see Doorbell 1,
458 * Doorbell 2, Doorbell 3, Doorbell 4) i.e map OB address space corresponding to
459 * doorbell to MSI-X address in PCI address space.
461 * This operation requires 3 parameters
462 * 1) Address reserved for doorbell in the outbound address space
463 * 2) MSI-X address in the PCIe Address space
464 * 3) Number of MSI-X interrupts that has to be configured
466 * The address in the outbound address space (for the Doorbell) is stored in
467 * epf_bar corresponding to BAR_DB_MW1 of epf_ntb_epc that is connected to
468 * HOST1. This is populated in epf_ntb_alloc_peer_mem() in this driver along
469 * with address for MW1.
471 * The MSI-X address is in the MSI-X table of EP CONTROLLER 2 and
472 * the count of doorbell is in ctrl->argument of epf_ntb_epc that is connected
473 * to HOST2. MSI-X table is stored memory mapped to ntb_epc->msix_bar and the
474 * offset is in ntb_epc->msix_table_offset. From this epf_ntb_configure_msix()
475 * gets the MSI-X address and data.
477 * epf_ntb_configure_msix() also stores the MSI-X data to raise each interrupt
478 * in db_data of the peer's control region. This helps the peer to raise
479 * doorbell of the other host by writing db_data to the BAR corresponding to
482 static int epf_ntb_configure_msix(struct epf_ntb *ntb,
483 enum pci_epc_interface_type type,
486 const struct pci_epc_features *epc_features;
487 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
488 struct pci_epf_bar *peer_epf_bar, *epf_bar;
489 struct pci_epf_msix_tbl *msix_tbl;
490 struct epf_ntb_ctrl *peer_ctrl;
491 u32 db_entry_size, msg_data;
492 enum pci_barno peer_barno;
493 phys_addr_t phys_addr;
500 ntb_epc = ntb->epc[type];
503 epf_bar = &ntb_epc->epf_bar[ntb_epc->msix_bar];
504 msix_tbl = epf_bar->addr + ntb_epc->msix_table_offset;
506 peer_ntb_epc = ntb->epc[!type];
507 peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
508 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
509 phys_addr = peer_epf_bar->phys_addr;
510 peer_ctrl = peer_ntb_epc->reg;
511 epc_features = ntb_epc->epc_features;
512 align = epc_features->align;
514 func_no = ntb_epc->func_no;
515 db_entry_size = peer_ctrl->db_entry_size;
517 for (i = 0; i < db_count; i++) {
518 msg_addr = ALIGN_DOWN(msix_tbl[i].msg_addr, align);
519 msg_data = msix_tbl[i].msg_data;
520 ret = pci_epc_map_addr(epc, func_no, phys_addr, msg_addr,
524 "%s intf: Failed to configure MSI-X IRQ\n",
525 pci_epc_interface_string(type));
528 phys_addr = phys_addr + db_entry_size;
529 peer_ctrl->db_data[i] = msg_data;
530 peer_ctrl->db_offset[i] = msix_tbl[i].msg_addr & (align - 1);
532 ntb_epc->is_msix = true;
538 * epf_ntb_configure_db() - Configure the Outbound Address Space for one host
539 * to ring the doorbell of other host
540 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
541 * @type: PRIMARY interface or SECONDARY interface
542 * @db_count: Count of the number of doorbells that has to be configured
543 * @msix: Indicates whether MSI-X or MSI should be used
545 * Invokes epf_ntb_configure_msix() or epf_ntb_configure_msi() required for
546 * one HOST to ring the doorbell of other HOST.
548 static int epf_ntb_configure_db(struct epf_ntb *ntb,
549 enum pci_epc_interface_type type,
550 u16 db_count, bool msix)
552 struct epf_ntb_epc *ntb_epc;
556 if (db_count > MAX_DB_COUNT)
559 ntb_epc = ntb->epc[type];
563 ret = epf_ntb_configure_msix(ntb, type, db_count);
565 ret = epf_ntb_configure_msi(ntb, type, db_count);
568 dev_err(&epc->dev, "%s intf: Failed to configure DB\n",
569 pci_epc_interface_string(type));
575 * epf_ntb_teardown_db() - Unmap address in OB address space to MSI/MSI-X
577 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
578 * @type: PRIMARY interface or SECONDARY interface
580 * Invoke pci_epc_unmap_addr() to unmap OB address to MSI/MSI-X address.
583 epf_ntb_teardown_db(struct epf_ntb *ntb, enum pci_epc_interface_type type)
585 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
586 struct pci_epf_bar *peer_epf_bar;
587 enum pci_barno peer_barno;
588 phys_addr_t phys_addr;
592 ntb_epc = ntb->epc[type];
595 peer_ntb_epc = ntb->epc[!type];
596 peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
597 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
598 phys_addr = peer_epf_bar->phys_addr;
599 func_no = ntb_epc->func_no;
601 pci_epc_unmap_addr(epc, func_no, phys_addr);
605 * epf_ntb_cmd_handler() - Handle commands provided by the NTB Host
606 * @work: work_struct for the two epf_ntb_epc (PRIMARY and SECONDARY)
608 * Workqueue function that gets invoked for the two epf_ntb_epc
609 * periodically (once every 5ms) to see if it has received any commands
610 * from NTB host. The host can send commands to configure doorbell or
611 * configure memory window or to update link status.
613 static void epf_ntb_cmd_handler(struct work_struct *work)
615 enum pci_epc_interface_type type;
616 struct epf_ntb_epc *ntb_epc;
617 struct epf_ntb_ctrl *ctrl;
618 u32 command, argument;
625 ntb_epc = container_of(work, struct epf_ntb_epc, cmd_handler.work);
627 command = ctrl->command;
630 argument = ctrl->argument;
636 type = ntb_epc->type;
637 ntb = ntb_epc->epf_ntb;
638 dev = &ntb->epf->dev;
641 case COMMAND_CONFIGURE_DOORBELL:
642 db_count = argument & DB_COUNT_MASK;
643 is_msix = argument & MSIX_ENABLE;
644 ret = epf_ntb_configure_db(ntb, type, db_count, is_msix);
646 ctrl->command_status = COMMAND_STATUS_ERROR;
648 ctrl->command_status = COMMAND_STATUS_OK;
650 case COMMAND_TEARDOWN_DOORBELL:
651 epf_ntb_teardown_db(ntb, type);
652 ctrl->command_status = COMMAND_STATUS_OK;
654 case COMMAND_CONFIGURE_MW:
655 ret = epf_ntb_configure_mw(ntb, type, argument);
657 ctrl->command_status = COMMAND_STATUS_ERROR;
659 ctrl->command_status = COMMAND_STATUS_OK;
661 case COMMAND_TEARDOWN_MW:
662 epf_ntb_teardown_mw(ntb, type, argument);
663 ctrl->command_status = COMMAND_STATUS_OK;
665 case COMMAND_LINK_UP:
666 ntb_epc->linkup = true;
667 if (ntb->epc[PRIMARY_INTERFACE]->linkup &&
668 ntb->epc[SECONDARY_INTERFACE]->linkup) {
669 ret = epf_ntb_link_up(ntb, true);
671 ctrl->command_status = COMMAND_STATUS_ERROR;
673 ctrl->command_status = COMMAND_STATUS_OK;
676 ctrl->command_status = COMMAND_STATUS_OK;
678 case COMMAND_LINK_DOWN:
679 ntb_epc->linkup = false;
680 ret = epf_ntb_link_up(ntb, false);
682 ctrl->command_status = COMMAND_STATUS_ERROR;
684 ctrl->command_status = COMMAND_STATUS_OK;
687 dev_err(dev, "%s intf UNKNOWN command: %d\n",
688 pci_epc_interface_string(type), command);
693 queue_delayed_work(kpcintb_workqueue, &ntb_epc->cmd_handler,
694 msecs_to_jiffies(5));
698 * epf_ntb_peer_spad_bar_clear() - Clear Peer Scratchpad BAR
699 * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
702 *+-----------------+------->+------------------+ +-----------------+
703 *| BAR0 | | CONFIG REGION | | BAR0 |
704 *+-----------------+----+ +------------------+<-------+-----------------+
705 *| BAR1 | | |SCRATCHPAD REGION | | BAR1 |
706 *+-----------------+ +-->+------------------+<-------+-----------------+
707 *| BAR2 | Local Memory | BAR2 |
708 *+-----------------+ +-----------------+
710 *+-----------------+ +-----------------+
712 *+-----------------+ +-----------------+
714 *+-----------------+ +-----------------+
715 * EP CONTROLLER 1 EP CONTROLLER 2
717 * Clear BAR1 of EP CONTROLLER 2 which contains the HOST2's peer scratchpad
718 * region. While BAR1 is the default peer scratchpad BAR, an NTB could have
719 * other BARs for peer scratchpad (because of 64-bit BARs or reserved BARs).
720 * This function can get the exact BAR used for peer scratchpad from
721 * epf_ntb_bar[BAR_PEER_SPAD].
723 * Since HOST2's peer scratchpad is also HOST1's self scratchpad, this function
724 * gets the address of peer scratchpad from
725 * peer_ntb_epc->epf_ntb_bar[BAR_CONFIG].
727 static void epf_ntb_peer_spad_bar_clear(struct epf_ntb_epc *ntb_epc)
729 struct pci_epf_bar *epf_bar;
730 enum pci_barno barno;
735 func_no = ntb_epc->func_no;
736 barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
737 epf_bar = &ntb_epc->epf_bar[barno];
738 pci_epc_clear_bar(epc, func_no, epf_bar);
742 * epf_ntb_peer_spad_bar_set() - Set peer scratchpad BAR
743 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
744 * @type: PRIMARY interface or SECONDARY interface
746 *+-----------------+------->+------------------+ +-----------------+
747 *| BAR0 | | CONFIG REGION | | BAR0 |
748 *+-----------------+----+ +------------------+<-------+-----------------+
749 *| BAR1 | | |SCRATCHPAD REGION | | BAR1 |
750 *+-----------------+ +-->+------------------+<-------+-----------------+
751 *| BAR2 | Local Memory | BAR2 |
752 *+-----------------+ +-----------------+
754 *+-----------------+ +-----------------+
756 *+-----------------+ +-----------------+
758 *+-----------------+ +-----------------+
759 * EP CONTROLLER 1 EP CONTROLLER 2
761 * Set BAR1 of EP CONTROLLER 2 which contains the HOST2's peer scratchpad
762 * region. While BAR1 is the default peer scratchpad BAR, an NTB could have
763 * other BARs for peer scratchpad (because of 64-bit BARs or reserved BARs).
764 * This function can get the exact BAR used for peer scratchpad from
765 * epf_ntb_bar[BAR_PEER_SPAD].
767 * Since HOST2's peer scratchpad is also HOST1's self scratchpad, this function
768 * gets the address of peer scratchpad from
769 * peer_ntb_epc->epf_ntb_bar[BAR_CONFIG].
771 static int epf_ntb_peer_spad_bar_set(struct epf_ntb *ntb,
772 enum pci_epc_interface_type type)
774 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
775 struct pci_epf_bar *peer_epf_bar, *epf_bar;
776 enum pci_barno peer_barno, barno;
777 u32 peer_spad_offset;
783 dev = &ntb->epf->dev;
785 peer_ntb_epc = ntb->epc[!type];
786 peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_CONFIG];
787 peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
789 ntb_epc = ntb->epc[type];
790 barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
791 epf_bar = &ntb_epc->epf_bar[barno];
792 func_no = ntb_epc->func_no;
795 peer_spad_offset = peer_ntb_epc->reg->spad_offset;
796 epf_bar->phys_addr = peer_epf_bar->phys_addr + peer_spad_offset;
797 epf_bar->size = peer_ntb_epc->spad_size;
798 epf_bar->barno = barno;
799 epf_bar->flags = PCI_BASE_ADDRESS_MEM_TYPE_32;
801 ret = pci_epc_set_bar(epc, func_no, epf_bar);
803 dev_err(dev, "%s intf: peer SPAD BAR set failed\n",
804 pci_epc_interface_string(type));
812 * epf_ntb_config_sspad_bar_clear() - Clear Config + Self scratchpad BAR
813 * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
816 * +-----------------+------->+------------------+ +-----------------+
817 * | BAR0 | | CONFIG REGION | | BAR0 |
818 * +-----------------+----+ +------------------+<-------+-----------------+
819 * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
820 * +-----------------+ +-->+------------------+<-------+-----------------+
821 * | BAR2 | Local Memory | BAR2 |
822 * +-----------------+ +-----------------+
824 * +-----------------+ +-----------------+
826 * +-----------------+ +-----------------+
828 * +-----------------+ +-----------------+
829 * EP CONTROLLER 1 EP CONTROLLER 2
831 * Clear BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
832 * self scratchpad region (removes inbound ATU configuration). While BAR0 is
833 * the default self scratchpad BAR, an NTB could have other BARs for self
834 * scratchpad (because of reserved BARs). This function can get the exact BAR
835 * used for self scratchpad from epf_ntb_bar[BAR_CONFIG].
837 * Please note the self scratchpad region and config region is combined to
838 * a single region and mapped using the same BAR. Also note HOST2's peer
839 * scratchpad is HOST1's self scratchpad.
841 static void epf_ntb_config_sspad_bar_clear(struct epf_ntb_epc *ntb_epc)
843 struct pci_epf_bar *epf_bar;
844 enum pci_barno barno;
849 func_no = ntb_epc->func_no;
850 barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
851 epf_bar = &ntb_epc->epf_bar[barno];
852 pci_epc_clear_bar(epc, func_no, epf_bar);
856 * epf_ntb_config_sspad_bar_set() - Set Config + Self scratchpad BAR
857 * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
860 * +-----------------+------->+------------------+ +-----------------+
861 * | BAR0 | | CONFIG REGION | | BAR0 |
862 * +-----------------+----+ +------------------+<-------+-----------------+
863 * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
864 * +-----------------+ +-->+------------------+<-------+-----------------+
865 * | BAR2 | Local Memory | BAR2 |
866 * +-----------------+ +-----------------+
868 * +-----------------+ +-----------------+
870 * +-----------------+ +-----------------+
872 * +-----------------+ +-----------------+
873 * EP CONTROLLER 1 EP CONTROLLER 2
875 * Map BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
876 * self scratchpad region. While BAR0 is the default self scratchpad BAR, an
877 * NTB could have other BARs for self scratchpad (because of reserved BARs).
878 * This function can get the exact BAR used for self scratchpad from
879 * epf_ntb_bar[BAR_CONFIG].
881 * Please note the self scratchpad region and config region is combined to
882 * a single region and mapped using the same BAR. Also note HOST2's peer
883 * scratchpad is HOST1's self scratchpad.
885 static int epf_ntb_config_sspad_bar_set(struct epf_ntb_epc *ntb_epc)
887 struct pci_epf_bar *epf_bar;
888 enum pci_barno barno;
895 ntb = ntb_epc->epf_ntb;
896 dev = &ntb->epf->dev;
899 func_no = ntb_epc->func_no;
900 barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
901 epf_bar = &ntb_epc->epf_bar[barno];
903 ret = pci_epc_set_bar(epc, func_no, epf_bar);
905 dev_err(dev, "%s inft: Config/Status/SPAD BAR set failed\n",
906 pci_epc_interface_string(ntb_epc->type));
914 * epf_ntb_config_spad_bar_free() - Free the physical memory associated with
915 * config + scratchpad region
916 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
918 * +-----------------+------->+------------------+ +-----------------+
919 * | BAR0 | | CONFIG REGION | | BAR0 |
920 * +-----------------+----+ +------------------+<-------+-----------------+
921 * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
922 * +-----------------+ +-->+------------------+<-------+-----------------+
923 * | BAR2 | Local Memory | BAR2 |
924 * +-----------------+ +-----------------+
926 * +-----------------+ +-----------------+
928 * +-----------------+ +-----------------+
930 * +-----------------+ +-----------------+
931 * EP CONTROLLER 1 EP CONTROLLER 2
933 * Free the Local Memory mentioned in the above diagram. After invoking this
934 * function, any of config + self scratchpad region of HOST1 or peer scratchpad
935 * region of HOST2 should not be accessed.
937 static void epf_ntb_config_spad_bar_free(struct epf_ntb *ntb)
939 enum pci_epc_interface_type type;
940 struct epf_ntb_epc *ntb_epc;
941 enum pci_barno barno;
945 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
946 ntb_epc = ntb->epc[type];
947 barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
949 pci_epf_free_space(epf, ntb_epc->reg, barno, type);
954 * epf_ntb_config_spad_bar_alloc() - Allocate memory for config + scratchpad
956 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
957 * @type: PRIMARY interface or SECONDARY interface
959 * +-----------------+------->+------------------+ +-----------------+
960 * | BAR0 | | CONFIG REGION | | BAR0 |
961 * +-----------------+----+ +------------------+<-------+-----------------+
962 * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
963 * +-----------------+ +-->+------------------+<-------+-----------------+
964 * | BAR2 | Local Memory | BAR2 |
965 * +-----------------+ +-----------------+
967 * +-----------------+ +-----------------+
969 * +-----------------+ +-----------------+
971 * +-----------------+ +-----------------+
972 * EP CONTROLLER 1 EP CONTROLLER 2
974 * Allocate the Local Memory mentioned in the above diagram. The size of
975 * CONFIG REGION is sizeof(struct epf_ntb_ctrl) and size of SCRATCHPAD REGION
976 * is obtained from "spad-count" configfs entry.
978 * The size of both config region and scratchpad region has to be aligned,
979 * since the scratchpad region will also be mapped as PEER SCRATCHPAD of
980 * other host using a separate BAR.
982 static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb,
983 enum pci_epc_interface_type type)
985 const struct pci_epc_features *peer_epc_features, *epc_features;
986 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
987 size_t msix_table_size, pba_size, align;
988 enum pci_barno peer_barno, barno;
989 struct epf_ntb_ctrl *ctrl;
990 u32 spad_size, ctrl_size;
1000 ntb_epc = ntb->epc[type];
1002 epc_features = ntb_epc->epc_features;
1003 barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
1004 size = epc_features->bar_fixed_size[barno];
1005 align = epc_features->align;
1007 peer_ntb_epc = ntb->epc[!type];
1008 peer_epc_features = peer_ntb_epc->epc_features;
1009 peer_barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
1010 peer_size = peer_epc_features->bar_fixed_size[peer_barno];
1012 /* Check if epc_features is populated incorrectly */
1013 if ((!IS_ALIGNED(size, align)))
1016 spad_count = ntb->spad_count;
1018 ctrl_size = sizeof(struct epf_ntb_ctrl);
1019 spad_size = spad_count * 4;
1021 msix_capable = epc_features->msix_capable;
1023 msix_table_size = PCI_MSIX_ENTRY_SIZE * ntb->db_count;
1024 ctrl_size = ALIGN(ctrl_size, 8);
1025 ntb_epc->msix_table_offset = ctrl_size;
1026 ntb_epc->msix_bar = barno;
1027 /* Align to QWORD or 8 Bytes */
1028 pba_size = ALIGN(DIV_ROUND_UP(ntb->db_count, 8), 8);
1029 ctrl_size = ctrl_size + msix_table_size + pba_size;
1033 ctrl_size = roundup_pow_of_two(ctrl_size);
1034 spad_size = roundup_pow_of_two(spad_size);
1036 ctrl_size = ALIGN(ctrl_size, align);
1037 spad_size = ALIGN(spad_size, align);
1041 if (peer_size < spad_size)
1042 spad_count = peer_size / 4;
1043 spad_size = peer_size;
1047 * In order to make sure SPAD offset is aligned to its size,
1048 * expand control region size to the size of SPAD if SPAD size
1049 * is greater than control region size.
1051 if (spad_size > ctrl_size)
1052 ctrl_size = spad_size;
1055 size = ctrl_size + spad_size;
1056 else if (size < ctrl_size + spad_size)
1059 base = pci_epf_alloc_space(epf, size, barno, align, type);
1061 dev_err(dev, "%s intf: Config/Status/SPAD alloc region fail\n",
1062 pci_epc_interface_string(type));
1066 ntb_epc->reg = base;
1068 ctrl = ntb_epc->reg;
1069 ctrl->spad_offset = ctrl_size;
1070 ctrl->spad_count = spad_count;
1071 ctrl->num_mws = ntb->num_mws;
1072 ctrl->db_entry_size = align ? align : 4;
1073 ntb_epc->spad_size = spad_size;
1079 * epf_ntb_config_spad_bar_alloc_interface() - Allocate memory for config +
1080 * scratchpad region for each of PRIMARY and SECONDARY interface
1081 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1083 * Wrapper for epf_ntb_config_spad_bar_alloc() which allocates memory for
1084 * config + scratchpad region for a specific interface
1086 static int epf_ntb_config_spad_bar_alloc_interface(struct epf_ntb *ntb)
1088 enum pci_epc_interface_type type;
1092 dev = &ntb->epf->dev;
1094 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
1095 ret = epf_ntb_config_spad_bar_alloc(ntb, type);
1097 dev_err(dev, "%s intf: Config/SPAD BAR alloc failed\n",
1098 pci_epc_interface_string(type));
1107 * epf_ntb_free_peer_mem() - Free memory allocated in peers outbound address
1109 * @ntb_epc: EPC associated with one of the HOST which holds peers outbound
1112 * +-----------------+ +---->+----------------+-----------+-----------------+
1113 * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
1114 * +-----------------+ | +----------------+ +-----------------+
1115 * | BAR1 | | | Doorbell 2 +---------+ | |
1116 * +-----------------+----+ +----------------+ | | |
1117 * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
1118 * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
1119 * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
1120 * +-----------------+ | |----------------+ | | | |
1121 * | BAR4 | | | | | | +-----------------+
1122 * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
1123 * | BAR5 | | | | | | +-----------------+
1124 * +-----------------+ +---->-----------------+ | | | |
1125 * EP CONTROLLER 1 | | | | +-----------------+
1126 * | | | +---->+ MSI|X ADDRESS 4 |
1127 * +----------------+ | +-----------------+
1128 * (A) EP CONTROLLER 2 | | |
1133 * (B) +-----------------+
1139 * +-----------------+
1141 * (Managed by HOST2)
1143 * Free memory allocated in EP CONTROLLER 2 (OB SPACE) in the above diagram.
1144 * It'll free Doorbell 1, Doorbell 2, Doorbell 3, Doorbell 4, MW1 (and MW2, MW3,
1147 static void epf_ntb_free_peer_mem(struct epf_ntb_epc *ntb_epc)
1149 struct pci_epf_bar *epf_bar;
1150 void __iomem *mw_addr;
1151 phys_addr_t phys_addr;
1152 enum epf_ntb_bar bar;
1153 enum pci_barno barno;
1154 struct pci_epc *epc;
1159 for (bar = BAR_DB_MW1; bar < BAR_MW4; bar++) {
1160 barno = ntb_epc->epf_ntb_bar[bar];
1161 mw_addr = ntb_epc->mw_addr[barno];
1162 epf_bar = &ntb_epc->epf_bar[barno];
1163 phys_addr = epf_bar->phys_addr;
1164 size = epf_bar->size;
1166 pci_epc_mem_free_addr(epc, phys_addr, mw_addr, size);
1167 ntb_epc->mw_addr[barno] = NULL;
1173 * epf_ntb_db_mw_bar_clear() - Clear doorbell and memory BAR
1174 * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
1177 * +-----------------+ +---->+----------------+-----------+-----------------+
1178 * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
1179 * +-----------------+ | +----------------+ +-----------------+
1180 * | BAR1 | | | Doorbell 2 +---------+ | |
1181 * +-----------------+----+ +----------------+ | | |
1182 * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
1183 * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
1184 * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
1185 * +-----------------+ | |----------------+ | | | |
1186 * | BAR4 | | | | | | +-----------------+
1187 * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
1188 * | BAR5 | | | | | | +-----------------+
1189 * +-----------------+ +---->-----------------+ | | | |
1190 * EP CONTROLLER 1 | | | | +-----------------+
1191 * | | | +---->+ MSI|X ADDRESS 4 |
1192 * +----------------+ | +-----------------+
1193 * (A) EP CONTROLLER 2 | | |
1198 * (B) +-----------------+
1204 * +-----------------+
1206 * (Managed by HOST2)
1208 * Clear doorbell and memory BARs (remove inbound ATU configuration). In the above
1209 * diagram it clears BAR2 TO BAR5 of EP CONTROLLER 1 (Doorbell BAR, MW1 BAR, MW2
1210 * BAR, MW3 BAR and MW4 BAR).
1212 static void epf_ntb_db_mw_bar_clear(struct epf_ntb_epc *ntb_epc)
1214 struct pci_epf_bar *epf_bar;
1215 enum epf_ntb_bar bar;
1216 enum pci_barno barno;
1217 struct pci_epc *epc;
1222 func_no = ntb_epc->func_no;
1224 for (bar = BAR_DB_MW1; bar < BAR_MW4; bar++) {
1225 barno = ntb_epc->epf_ntb_bar[bar];
1226 epf_bar = &ntb_epc->epf_bar[barno];
1227 pci_epc_clear_bar(epc, func_no, epf_bar);
1232 * epf_ntb_db_mw_bar_cleanup() - Clear doorbell/memory BAR and free memory
1233 * allocated in peers outbound address space
1234 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1235 * @type: PRIMARY interface or SECONDARY interface
1237 * Wrapper for epf_ntb_db_mw_bar_clear() to clear HOST1's BAR and
1238 * epf_ntb_free_peer_mem() which frees up HOST2 outbound memory.
1240 static void epf_ntb_db_mw_bar_cleanup(struct epf_ntb *ntb,
1241 enum pci_epc_interface_type type)
1243 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
1245 ntb_epc = ntb->epc[type];
1246 peer_ntb_epc = ntb->epc[!type];
1248 epf_ntb_db_mw_bar_clear(ntb_epc);
1249 epf_ntb_free_peer_mem(peer_ntb_epc);
1253 * epf_ntb_configure_interrupt() - Configure MSI/MSI-X capaiblity
1254 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1255 * @type: PRIMARY interface or SECONDARY interface
1257 * Configure MSI/MSI-X capability for each interface with number of
1258 * interrupts equal to "db_count" configfs entry.
1260 static int epf_ntb_configure_interrupt(struct epf_ntb *ntb,
1261 enum pci_epc_interface_type type)
1263 const struct pci_epc_features *epc_features;
1264 bool msix_capable, msi_capable;
1265 struct epf_ntb_epc *ntb_epc;
1266 struct pci_epc *epc;
1272 ntb_epc = ntb->epc[type];
1273 dev = &ntb->epf->dev;
1275 epc_features = ntb_epc->epc_features;
1276 msix_capable = epc_features->msix_capable;
1277 msi_capable = epc_features->msi_capable;
1279 if (!(msix_capable || msi_capable)) {
1280 dev_err(dev, "MSI or MSI-X is required for doorbell\n");
1284 func_no = ntb_epc->func_no;
1286 db_count = ntb->db_count;
1287 if (db_count > MAX_DB_COUNT) {
1288 dev_err(dev, "DB count cannot be more than %d\n", MAX_DB_COUNT);
1292 ntb->db_count = db_count;
1296 ret = pci_epc_set_msi(epc, func_no, db_count);
1298 dev_err(dev, "%s intf: MSI configuration failed\n",
1299 pci_epc_interface_string(type));
1305 ret = pci_epc_set_msix(epc, func_no, db_count,
1307 ntb_epc->msix_table_offset);
1309 dev_err(dev, "MSI configuration failed\n");
1318 * epf_ntb_alloc_peer_mem() - Allocate memory in peer's outbound address space
1319 * @dev: The PCI device.
1320 * @ntb_epc: EPC associated with one of the HOST whose BAR holds peer's outbound
1322 * @bar: BAR of @ntb_epc in for which memory has to be allocated (could be
1323 * BAR_DB_MW1, BAR_MW2, BAR_MW3, BAR_MW4)
1324 * @peer_ntb_epc: EPC associated with HOST whose outbound address space is
1326 * @size: Size of the address region that has to be allocated in peers OB SPACE
1329 * +-----------------+ +---->+----------------+-----------+-----------------+
1330 * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
1331 * +-----------------+ | +----------------+ +-----------------+
1332 * | BAR1 | | | Doorbell 2 +---------+ | |
1333 * +-----------------+----+ +----------------+ | | |
1334 * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
1335 * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
1336 * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
1337 * +-----------------+ | |----------------+ | | | |
1338 * | BAR4 | | | | | | +-----------------+
1339 * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
1340 * | BAR5 | | | | | | +-----------------+
1341 * +-----------------+ +---->-----------------+ | | | |
1342 * EP CONTROLLER 1 | | | | +-----------------+
1343 * | | | +---->+ MSI|X ADDRESS 4 |
1344 * +----------------+ | +-----------------+
1345 * (A) EP CONTROLLER 2 | | |
1350 * (B) +-----------------+
1356 * +-----------------+
1358 * (Managed by HOST2)
1360 * Allocate memory in OB space of EP CONTROLLER 2 in the above diagram. Allocate
1361 * for Doorbell 1, Doorbell 2, Doorbell 3, Doorbell 4, MW1 (and MW2, MW3, MW4).
1363 static int epf_ntb_alloc_peer_mem(struct device *dev,
1364 struct epf_ntb_epc *ntb_epc,
1365 enum epf_ntb_bar bar,
1366 struct epf_ntb_epc *peer_ntb_epc,
1369 const struct pci_epc_features *epc_features;
1370 struct pci_epf_bar *epf_bar;
1371 struct pci_epc *peer_epc;
1372 phys_addr_t phys_addr;
1373 void __iomem *mw_addr;
1374 enum pci_barno barno;
1377 epc_features = ntb_epc->epc_features;
1378 align = epc_features->align;
1384 size = ALIGN(size, align);
1386 size = roundup_pow_of_two(size);
1388 peer_epc = peer_ntb_epc->epc;
1389 mw_addr = pci_epc_mem_alloc_addr(peer_epc, &phys_addr, size);
1391 dev_err(dev, "%s intf: Failed to allocate OB address\n",
1392 pci_epc_interface_string(peer_ntb_epc->type));
1396 barno = ntb_epc->epf_ntb_bar[bar];
1397 epf_bar = &ntb_epc->epf_bar[barno];
1398 ntb_epc->mw_addr[barno] = mw_addr;
1400 epf_bar->phys_addr = phys_addr;
1401 epf_bar->size = size;
1402 epf_bar->barno = barno;
1403 epf_bar->flags = PCI_BASE_ADDRESS_MEM_TYPE_32;
1409 * epf_ntb_db_mw_bar_init() - Configure Doorbell and Memory window BARs
1410 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1411 * @type: PRIMARY interface or SECONDARY interface
1413 * Wrapper for epf_ntb_alloc_peer_mem() and pci_epc_set_bar() that allocates
1414 * memory in OB address space of HOST2 and configures BAR of HOST1
1416 static int epf_ntb_db_mw_bar_init(struct epf_ntb *ntb,
1417 enum pci_epc_interface_type type)
1419 const struct pci_epc_features *epc_features;
1420 struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
1421 struct pci_epf_bar *epf_bar;
1422 struct epf_ntb_ctrl *ctrl;
1423 u32 num_mws, db_count;
1424 enum epf_ntb_bar bar;
1425 enum pci_barno barno;
1426 struct pci_epc *epc;
1433 ntb_epc = ntb->epc[type];
1434 peer_ntb_epc = ntb->epc[!type];
1436 dev = &ntb->epf->dev;
1437 epc_features = ntb_epc->epc_features;
1438 align = epc_features->align;
1439 func_no = ntb_epc->func_no;
1441 num_mws = ntb->num_mws;
1442 db_count = ntb->db_count;
1444 for (bar = BAR_DB_MW1, i = 0; i < num_mws; bar++, i++) {
1445 if (bar == BAR_DB_MW1) {
1446 align = align ? align : 4;
1447 size = db_count * align;
1448 size = ALIGN(size, ntb->mws_size[i]);
1449 ctrl = ntb_epc->reg;
1450 ctrl->mw1_offset = size;
1451 size += ntb->mws_size[i];
1453 size = ntb->mws_size[i];
1456 ret = epf_ntb_alloc_peer_mem(dev, ntb_epc, bar,
1457 peer_ntb_epc, size);
1459 dev_err(dev, "%s intf: DoorBell mem alloc failed\n",
1460 pci_epc_interface_string(type));
1461 goto err_alloc_peer_mem;
1464 barno = ntb_epc->epf_ntb_bar[bar];
1465 epf_bar = &ntb_epc->epf_bar[barno];
1467 ret = pci_epc_set_bar(epc, func_no, epf_bar);
1469 dev_err(dev, "%s intf: DoorBell BAR set failed\n",
1470 pci_epc_interface_string(type));
1471 goto err_alloc_peer_mem;
1478 epf_ntb_db_mw_bar_cleanup(ntb, type);
1484 * epf_ntb_epc_destroy_interface() - Cleanup NTB EPC interface
1485 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1486 * @type: PRIMARY interface or SECONDARY interface
1488 * Unbind NTB function device from EPC and relinquish reference to pci_epc
1489 * for each of the interface.
1491 static void epf_ntb_epc_destroy_interface(struct epf_ntb *ntb,
1492 enum pci_epc_interface_type type)
1494 struct epf_ntb_epc *ntb_epc;
1495 struct pci_epc *epc;
1496 struct pci_epf *epf;
1502 ntb_epc = ntb->epc[type];
1506 pci_epc_remove_epf(epc, epf, type);
1511 * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
1512 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1514 * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
1516 static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
1518 enum pci_epc_interface_type type;
1520 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++)
1521 epf_ntb_epc_destroy_interface(ntb, type);
1525 * epf_ntb_epc_create_interface() - Create and initialize NTB EPC interface
1526 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1527 * @epc: struct pci_epc to which a particular NTB interface should be associated
1528 * @type: PRIMARY interface or SECONDARY interface
1530 * Allocate memory for NTB EPC interface and initialize it.
1532 static int epf_ntb_epc_create_interface(struct epf_ntb *ntb,
1533 struct pci_epc *epc,
1534 enum pci_epc_interface_type type)
1536 const struct pci_epc_features *epc_features;
1537 struct pci_epf_bar *epf_bar;
1538 struct epf_ntb_epc *ntb_epc;
1539 struct pci_epf *epf;
1543 dev = &ntb->epf->dev;
1545 ntb_epc = devm_kzalloc(dev, sizeof(*ntb_epc), GFP_KERNEL);
1550 if (type == PRIMARY_INTERFACE) {
1551 func_no = epf->func_no;
1554 func_no = epf->sec_epc_func_no;
1555 epf_bar = epf->sec_epc_bar;
1558 ntb_epc->linkup = false;
1560 ntb_epc->func_no = func_no;
1561 ntb_epc->type = type;
1562 ntb_epc->epf_bar = epf_bar;
1563 ntb_epc->epf_ntb = ntb;
1565 epc_features = pci_epc_get_features(epc, func_no);
1568 ntb_epc->epc_features = epc_features;
1570 ntb->epc[type] = ntb_epc;
1576 * epf_ntb_epc_create() - Create and initialize NTB EPC interface
1577 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1579 * Get a reference to EPC device and bind NTB function device to that EPC
1580 * for each of the interface. It is also a wrapper to
1581 * epf_ntb_epc_create_interface() to allocate memory for NTB EPC interface
1584 static int epf_ntb_epc_create(struct epf_ntb *ntb)
1586 struct pci_epf *epf;
1593 ret = epf_ntb_epc_create_interface(ntb, epf->epc, PRIMARY_INTERFACE);
1595 dev_err(dev, "PRIMARY intf: Fail to create NTB EPC\n");
1599 ret = epf_ntb_epc_create_interface(ntb, epf->sec_epc,
1600 SECONDARY_INTERFACE);
1602 dev_err(dev, "SECONDARY intf: Fail to create NTB EPC\n");
1603 goto err_epc_create;
1609 epf_ntb_epc_destroy_interface(ntb, PRIMARY_INTERFACE);
1615 * epf_ntb_init_epc_bar_interface() - Identify BARs to be used for each of
1616 * the NTB constructs (scratchpad region, doorbell, memorywindow)
1617 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1618 * @type: PRIMARY interface or SECONDARY interface
1620 * Identify the free BARs to be used for each of BAR_CONFIG, BAR_PEER_SPAD,
1621 * BAR_DB_MW1, BAR_MW2, BAR_MW3 and BAR_MW4.
1623 static int epf_ntb_init_epc_bar_interface(struct epf_ntb *ntb,
1624 enum pci_epc_interface_type type)
1626 const struct pci_epc_features *epc_features;
1627 struct epf_ntb_epc *ntb_epc;
1628 enum pci_barno barno;
1629 enum epf_ntb_bar bar;
1635 ntb_epc = ntb->epc[type];
1636 num_mws = ntb->num_mws;
1637 dev = &ntb->epf->dev;
1638 epc_features = ntb_epc->epc_features;
1640 /* These are required BARs which are mandatory for NTB functionality */
1641 for (bar = BAR_CONFIG; bar <= BAR_DB_MW1; bar++, barno++) {
1642 barno = pci_epc_get_next_free_bar(epc_features, barno);
1644 dev_err(dev, "%s intf: Fail to get NTB function BAR\n",
1645 pci_epc_interface_string(type));
1648 ntb_epc->epf_ntb_bar[bar] = barno;
1651 /* These are optional BARs which don't impact NTB functionality */
1652 for (bar = BAR_MW2, i = 1; i < num_mws; bar++, barno++, i++) {
1653 barno = pci_epc_get_next_free_bar(epc_features, barno);
1656 dev_dbg(dev, "BAR not available for > MW%d\n", i + 1);
1658 ntb_epc->epf_ntb_bar[bar] = barno;
1665 * epf_ntb_init_epc_bar() - Identify BARs to be used for each of the NTB
1666 * constructs (scratchpad region, doorbell, memorywindow)
1667 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1669 * Wrapper to epf_ntb_init_epc_bar_interface() to identify the free BARs
1670 * to be used for each of BAR_CONFIG, BAR_PEER_SPAD, BAR_DB_MW1, BAR_MW2,
1671 * BAR_MW3 and BAR_MW4 for all the interfaces.
1673 static int epf_ntb_init_epc_bar(struct epf_ntb *ntb)
1675 enum pci_epc_interface_type type;
1679 dev = &ntb->epf->dev;
1680 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
1681 ret = epf_ntb_init_epc_bar_interface(ntb, type);
1683 dev_err(dev, "Fail to init EPC bar for %s interface\n",
1684 pci_epc_interface_string(type));
1693 * epf_ntb_epc_init_interface() - Initialize NTB interface
1694 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1695 * @type: PRIMARY interface or SECONDARY interface
1697 * Wrapper to initialize a particular EPC interface and start the workqueue
1698 * to check for commands from host. This function will write to the
1699 * EP controller HW for configuring it.
1701 static int epf_ntb_epc_init_interface(struct epf_ntb *ntb,
1702 enum pci_epc_interface_type type)
1704 struct epf_ntb_epc *ntb_epc;
1705 struct pci_epc *epc;
1706 struct pci_epf *epf;
1711 ntb_epc = ntb->epc[type];
1715 func_no = ntb_epc->func_no;
1717 ret = epf_ntb_config_sspad_bar_set(ntb->epc[type]);
1719 dev_err(dev, "%s intf: Config/self SPAD BAR init failed\n",
1720 pci_epc_interface_string(type));
1724 ret = epf_ntb_peer_spad_bar_set(ntb, type);
1726 dev_err(dev, "%s intf: Peer SPAD BAR init failed\n",
1727 pci_epc_interface_string(type));
1728 goto err_peer_spad_bar_init;
1731 ret = epf_ntb_configure_interrupt(ntb, type);
1733 dev_err(dev, "%s intf: Interrupt configuration failed\n",
1734 pci_epc_interface_string(type));
1735 goto err_peer_spad_bar_init;
1738 ret = epf_ntb_db_mw_bar_init(ntb, type);
1740 dev_err(dev, "%s intf: DB/MW BAR init failed\n",
1741 pci_epc_interface_string(type));
1742 goto err_db_mw_bar_init;
1745 ret = pci_epc_write_header(epc, func_no, epf->header);
1747 dev_err(dev, "%s intf: Configuration header write failed\n",
1748 pci_epc_interface_string(type));
1749 goto err_write_header;
1752 INIT_DELAYED_WORK(&ntb->epc[type]->cmd_handler, epf_ntb_cmd_handler);
1753 queue_work(kpcintb_workqueue, &ntb->epc[type]->cmd_handler.work);
1758 epf_ntb_db_mw_bar_cleanup(ntb, type);
1761 epf_ntb_peer_spad_bar_clear(ntb->epc[type]);
1763 err_peer_spad_bar_init:
1764 epf_ntb_config_sspad_bar_clear(ntb->epc[type]);
1770 * epf_ntb_epc_cleanup_interface() - Cleanup NTB interface
1771 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1772 * @type: PRIMARY interface or SECONDARY interface
1774 * Wrapper to cleanup a particular NTB interface.
1776 static void epf_ntb_epc_cleanup_interface(struct epf_ntb *ntb,
1777 enum pci_epc_interface_type type)
1779 struct epf_ntb_epc *ntb_epc;
1784 ntb_epc = ntb->epc[type];
1785 cancel_delayed_work(&ntb_epc->cmd_handler);
1786 epf_ntb_db_mw_bar_cleanup(ntb, type);
1787 epf_ntb_peer_spad_bar_clear(ntb_epc);
1788 epf_ntb_config_sspad_bar_clear(ntb_epc);
1792 * epf_ntb_epc_cleanup() - Cleanup all NTB interfaces
1793 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1795 * Wrapper to cleanup all NTB interfaces.
1797 static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
1799 enum pci_epc_interface_type type;
1801 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++)
1802 epf_ntb_epc_cleanup_interface(ntb, type);
1806 * epf_ntb_epc_init() - Initialize all NTB interfaces
1807 * @ntb: NTB device that facilitates communication between HOST1 and HOST2
1809 * Wrapper to initialize all NTB interface and start the workqueue
1810 * to check for commands from host.
1812 static int epf_ntb_epc_init(struct epf_ntb *ntb)
1814 enum pci_epc_interface_type type;
1818 dev = &ntb->epf->dev;
1820 for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
1821 ret = epf_ntb_epc_init_interface(ntb, type);
1823 dev_err(dev, "%s intf: Failed to initialize\n",
1824 pci_epc_interface_string(type));
1832 epf_ntb_epc_cleanup_interface(ntb, type - 1);
1838 * epf_ntb_bind() - Initialize endpoint controller to provide NTB functionality
1839 * @epf: NTB endpoint function device
1841 * Initialize both the endpoint controllers associated with NTB function device.
1842 * Invoked when a primary interface or secondary interface is bound to EPC
1843 * device. This function will succeed only when EPC is bound to both the
1846 static int epf_ntb_bind(struct pci_epf *epf)
1848 struct epf_ntb *ntb = epf_get_drvdata(epf);
1849 struct device *dev = &epf->dev;
1853 dev_dbg(dev, "PRIMARY EPC interface not yet bound\n");
1857 if (!epf->sec_epc) {
1858 dev_dbg(dev, "SECONDARY EPC interface not yet bound\n");
1862 ret = epf_ntb_epc_create(ntb);
1864 dev_err(dev, "Failed to create NTB EPC\n");
1868 ret = epf_ntb_init_epc_bar(ntb);
1870 dev_err(dev, "Failed to create NTB EPC\n");
1874 ret = epf_ntb_config_spad_bar_alloc_interface(ntb);
1876 dev_err(dev, "Failed to allocate BAR memory\n");
1880 ret = epf_ntb_epc_init(ntb);
1882 dev_err(dev, "Failed to initialize EPC\n");
1886 epf_set_drvdata(epf, ntb);
1891 epf_ntb_config_spad_bar_free(ntb);
1894 epf_ntb_epc_destroy(ntb);
1900 * epf_ntb_unbind() - Cleanup the initialization from epf_ntb_bind()
1901 * @epf: NTB endpoint function device
1903 * Cleanup the initialization from epf_ntb_bind()
1905 static void epf_ntb_unbind(struct pci_epf *epf)
1907 struct epf_ntb *ntb = epf_get_drvdata(epf);
1909 epf_ntb_epc_cleanup(ntb);
1910 epf_ntb_config_spad_bar_free(ntb);
1911 epf_ntb_epc_destroy(ntb);
1914 #define EPF_NTB_R(_name) \
1915 static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
1918 struct config_group *group = to_config_group(item); \
1919 struct epf_ntb *ntb = to_epf_ntb(group); \
1921 return sprintf(page, "%d\n", ntb->_name); \
1924 #define EPF_NTB_W(_name) \
1925 static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
1926 const char *page, size_t len) \
1928 struct config_group *group = to_config_group(item); \
1929 struct epf_ntb *ntb = to_epf_ntb(group); \
1933 ret = kstrtou32(page, 0, &val); \
1942 #define EPF_NTB_MW_R(_name) \
1943 static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
1946 struct config_group *group = to_config_group(item); \
1947 struct epf_ntb *ntb = to_epf_ntb(group); \
1950 sscanf(#_name, "mw%d", &win_no); \
1952 return sprintf(page, "%lld\n", ntb->mws_size[win_no - 1]); \
1955 #define EPF_NTB_MW_W(_name) \
1956 static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
1957 const char *page, size_t len) \
1959 struct config_group *group = to_config_group(item); \
1960 struct epf_ntb *ntb = to_epf_ntb(group); \
1961 struct device *dev = &ntb->epf->dev; \
1966 ret = kstrtou64(page, 0, &val); \
1970 if (sscanf(#_name, "mw%d", &win_no) != 1) \
1973 if (ntb->num_mws < win_no) { \
1974 dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
1978 ntb->mws_size[win_no - 1] = val; \
1983 static ssize_t epf_ntb_num_mws_store(struct config_item *item,
1984 const char *page, size_t len)
1986 struct config_group *group = to_config_group(item);
1987 struct epf_ntb *ntb = to_epf_ntb(group);
1991 ret = kstrtou32(page, 0, &val);
2003 EPF_NTB_R(spad_count)
2004 EPF_NTB_W(spad_count)
2017 CONFIGFS_ATTR(epf_ntb_, spad_count);
2018 CONFIGFS_ATTR(epf_ntb_, db_count);
2019 CONFIGFS_ATTR(epf_ntb_, num_mws);
2020 CONFIGFS_ATTR(epf_ntb_, mw1);
2021 CONFIGFS_ATTR(epf_ntb_, mw2);
2022 CONFIGFS_ATTR(epf_ntb_, mw3);
2023 CONFIGFS_ATTR(epf_ntb_, mw4);
2025 static struct configfs_attribute *epf_ntb_attrs[] = {
2026 &epf_ntb_attr_spad_count,
2027 &epf_ntb_attr_db_count,
2028 &epf_ntb_attr_num_mws,
2036 static const struct config_item_type ntb_group_type = {
2037 .ct_attrs = epf_ntb_attrs,
2038 .ct_owner = THIS_MODULE,
2042 * epf_ntb_add_cfs() - Add configfs directory specific to NTB
2043 * @epf: NTB endpoint function device
2044 * @group: A pointer to the config_group structure referencing a group of
2045 * config_items of a specific type that belong to a specific sub-system.
2047 * Add configfs directory specific to NTB. This directory will hold
2048 * NTB specific properties like db_count, spad_count, num_mws etc.,
2050 static struct config_group *epf_ntb_add_cfs(struct pci_epf *epf,
2051 struct config_group *group)
2053 struct epf_ntb *ntb = epf_get_drvdata(epf);
2054 struct config_group *ntb_group = &ntb->group;
2055 struct device *dev = &epf->dev;
2057 config_group_init_type_name(ntb_group, dev_name(dev), &ntb_group_type);
2063 * epf_ntb_probe() - Probe NTB function driver
2064 * @epf: NTB endpoint function device
2066 * Probe NTB function driver when endpoint function bus detects a NTB
2067 * endpoint function.
2069 static int epf_ntb_probe(struct pci_epf *epf)
2071 struct epf_ntb *ntb;
2076 ntb = devm_kzalloc(dev, sizeof(*ntb), GFP_KERNEL);
2080 epf->header = &epf_ntb_header;
2082 epf_set_drvdata(epf, ntb);
2087 static struct pci_epf_ops epf_ntb_ops = {
2088 .bind = epf_ntb_bind,
2089 .unbind = epf_ntb_unbind,
2090 .add_cfs = epf_ntb_add_cfs,
2093 static const struct pci_epf_device_id epf_ntb_ids[] = {
2095 .name = "pci_epf_ntb",
2100 static struct pci_epf_driver epf_ntb_driver = {
2101 .driver.name = "pci_epf_ntb",
2102 .probe = epf_ntb_probe,
2103 .id_table = epf_ntb_ids,
2104 .ops = &epf_ntb_ops,
2105 .owner = THIS_MODULE,
2108 static int __init epf_ntb_init(void)
2112 kpcintb_workqueue = alloc_workqueue("kpcintb", WQ_MEM_RECLAIM |
2114 ret = pci_epf_register_driver(&epf_ntb_driver);
2116 destroy_workqueue(kpcintb_workqueue);
2117 pr_err("Failed to register pci epf ntb driver --> %d\n", ret);
2123 module_init(epf_ntb_init);
2125 static void __exit epf_ntb_exit(void)
2127 pci_epf_unregister_driver(&epf_ntb_driver);
2128 destroy_workqueue(kpcintb_workqueue);
2130 module_exit(epf_ntb_exit);
2132 MODULE_DESCRIPTION("PCI EPF NTB DRIVER");
2133 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
2134 MODULE_LICENSE("GPL v2");