1 // SPDX-License-Identifier: GPL-2.0
3 * Endpoint Function Driver to implement Non-Transparent Bridge functionality
4 * Between PCI RC and EP
6 * Copyright (C) 2020 Texas Instruments
7 * Copyright (C) 2022 NXP
9 * Based on pci-epf-ntb.c
15 * +------------+ +---------------------------------------+
17 * +------------+ | +--------------+
19 * | NetDev | | | NetDev |
20 * +------------+ | +--------------+
22 * | Transfer | | | Transfer |
23 * +------------+ | +--------------+
27 * | Driver | | | PCI Virtual |
28 * | | +---------------+ | NTB Driver |
29 * | | | PCI EP NTB |<------>| |
30 * | | | FN Driver | | |
31 * +------------+ +---------------+ +--------------+
33 * | PCI Bus | <-----> | PCI EP Bus | | Virtual PCI |
35 * +------------+ +---------------+--------+--------------+
36 * PCIe Root Port PCI EP
39 #include <linux/delay.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
44 #include <linux/pci-epc.h>
45 #include <linux/pci-epf.h>
46 #include <linux/ntb.h>
48 static struct workqueue_struct *kpcintb_workqueue;
50 #define COMMAND_CONFIGURE_DOORBELL 1
51 #define COMMAND_TEARDOWN_DOORBELL 2
52 #define COMMAND_CONFIGURE_MW 3
53 #define COMMAND_TEARDOWN_MW 4
54 #define COMMAND_LINK_UP 5
55 #define COMMAND_LINK_DOWN 6
57 #define COMMAND_STATUS_OK 1
58 #define COMMAND_STATUS_ERROR 2
60 #define LINK_STATUS_UP BIT(0)
64 #define NTB_MW_OFFSET 2
65 #define DB_COUNT_MASK GENMASK(15, 0)
66 #define MSIX_ENABLE BIT(16)
67 #define MAX_DB_COUNT 32
79 * +--------------------------------------------------+ Base
83 * | Common Control Register |
87 * +-----------------------+--------------------------+ Base+spad_offset
89 * | Peer Spad Space | Spad Space |
92 * +-----------------------+--------------------------+ Base+spad_offset
93 * | | | +spad_count * 4
95 * | Spad Space | Peer Spad Space |
97 * +-----------------------+--------------------------+
98 * Virtual PCI PCIe Endpoint
99 * NTB Driver NTB Driver
101 struct epf_ntb_ctrl {
114 u32 db_data[MAX_DB_COUNT];
115 u32 db_offset[MAX_DB_COUNT];
121 struct config_group group;
126 u64 mws_size[MAX_MW];
135 enum pci_barno epf_ntb_bar[6];
137 struct epf_ntb_ctrl *reg;
141 phys_addr_t vpci_mw_phy[MAX_MW];
142 void __iomem *vpci_mw_addr[MAX_MW];
144 struct delayed_work cmd_handler;
147 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
148 #define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
150 static struct pci_epf_header epf_ntb_header = {
151 .vendorid = PCI_ANY_ID,
152 .deviceid = PCI_ANY_ID,
153 .baseclass_code = PCI_BASE_CLASS_MEMORY,
154 .interrupt_pin = PCI_INTERRUPT_INTA,
158 * epf_ntb_link_up() - Raise link_up interrupt to Virtual Host (VHOST)
159 * @ntb: NTB device that facilitates communication between HOST and VHOST
160 * @link_up: true or false indicating Link is UP or Down
162 * Once NTB function in HOST invoke ntb_link_enable(),
163 * this NTB function driver will trigger a link event to VHOST.
165 * Returns: Zero for success, or an error code in case of failure
167 static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
170 ntb->reg->link_status |= LINK_STATUS_UP;
172 ntb->reg->link_status &= ~LINK_STATUS_UP;
174 ntb_link_event(&ntb->ntb);
179 * epf_ntb_configure_mw() - Configure the Outbound Address Space for VHOST
180 * to access the memory window of HOST
181 * @ntb: NTB device that facilitates communication between HOST and VHOST
182 * @mw: Index of the memory window (either 0, 1, 2 or 3)
185 * +--------+ +-----------+
191 * | Virtual| | Memory Win|
192 * | NTB | -----------> | |
197 * +--------+ +-----------+
200 * Returns: Zero for success, or an error code in case of failure
202 static int epf_ntb_configure_mw(struct epf_ntb *ntb, u32 mw)
204 phys_addr_t phys_addr;
205 u8 func_no, vfunc_no;
209 phys_addr = ntb->vpci_mw_phy[mw];
210 addr = ntb->reg->addr;
211 size = ntb->reg->size;
213 func_no = ntb->epf->func_no;
214 vfunc_no = ntb->epf->vfunc_no;
216 ret = pci_epc_map_addr(ntb->epf->epc, func_no, vfunc_no, phys_addr, addr, size);
218 dev_err(&ntb->epf->epc->dev,
219 "Failed to map memory window %d address\n", mw);
224 * epf_ntb_teardown_mw() - Teardown the configured OB ATU
225 * @ntb: NTB device that facilitates communication between HOST and VHOST
226 * @mw: Index of the memory window (either 0, 1, 2 or 3)
228 * Teardown the configured OB ATU configured in epf_ntb_configure_mw() using
229 * pci_epc_unmap_addr()
231 static void epf_ntb_teardown_mw(struct epf_ntb *ntb, u32 mw)
233 pci_epc_unmap_addr(ntb->epf->epc,
236 ntb->vpci_mw_phy[mw]);
240 * epf_ntb_cmd_handler() - Handle commands provided by the NTB HOST
241 * @work: work_struct for the epf_ntb_epc
243 * Workqueue function that gets invoked for the two epf_ntb_epc
244 * periodically (once every 5ms) to see if it has received any commands
245 * from NTB HOST. The HOST can send commands to configure doorbell or
246 * configure memory window or to update link status.
248 static void epf_ntb_cmd_handler(struct work_struct *work)
250 struct epf_ntb_ctrl *ctrl;
251 u32 command, argument;
257 ntb = container_of(work, struct epf_ntb, cmd_handler.work);
259 for (i = 1; i < ntb->db_count; i++) {
260 if (ntb->epf_db[i]) {
261 ntb->db |= 1 << (i - 1);
262 ntb_db_event(&ntb->ntb, i);
268 command = ctrl->command;
271 argument = ctrl->argument;
277 dev = &ntb->epf->dev;
280 case COMMAND_CONFIGURE_DOORBELL:
281 ctrl->command_status = COMMAND_STATUS_OK;
283 case COMMAND_TEARDOWN_DOORBELL:
284 ctrl->command_status = COMMAND_STATUS_OK;
286 case COMMAND_CONFIGURE_MW:
287 ret = epf_ntb_configure_mw(ntb, argument);
289 ctrl->command_status = COMMAND_STATUS_ERROR;
291 ctrl->command_status = COMMAND_STATUS_OK;
293 case COMMAND_TEARDOWN_MW:
294 epf_ntb_teardown_mw(ntb, argument);
295 ctrl->command_status = COMMAND_STATUS_OK;
297 case COMMAND_LINK_UP:
299 ret = epf_ntb_link_up(ntb, true);
301 ctrl->command_status = COMMAND_STATUS_ERROR;
303 ctrl->command_status = COMMAND_STATUS_OK;
305 case COMMAND_LINK_DOWN:
307 ret = epf_ntb_link_up(ntb, false);
309 ctrl->command_status = COMMAND_STATUS_ERROR;
311 ctrl->command_status = COMMAND_STATUS_OK;
314 dev_err(dev, "UNKNOWN command: %d\n", command);
319 queue_delayed_work(kpcintb_workqueue, &ntb->cmd_handler,
320 msecs_to_jiffies(5));
324 * epf_ntb_config_sspad_bar_clear() - Clear Config + Self scratchpad BAR
325 * @ntb: EPC associated with one of the HOST which holds peer's outbound
328 * Clear BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
329 * self scratchpad region (removes inbound ATU configuration). While BAR0 is
330 * the default self scratchpad BAR, an NTB could have other BARs for self
331 * scratchpad (because of reserved BARs). This function can get the exact BAR
332 * used for self scratchpad from epf_ntb_bar[BAR_CONFIG].
334 * Please note the self scratchpad region and config region is combined to
335 * a single region and mapped using the same BAR. Also note VHOST's peer
336 * scratchpad is HOST's self scratchpad.
340 static void epf_ntb_config_sspad_bar_clear(struct epf_ntb *ntb)
342 struct pci_epf_bar *epf_bar;
343 enum pci_barno barno;
345 barno = ntb->epf_ntb_bar[BAR_CONFIG];
346 epf_bar = &ntb->epf->bar[barno];
348 pci_epc_clear_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
352 * epf_ntb_config_sspad_bar_set() - Set Config + Self scratchpad BAR
353 * @ntb: NTB device that facilitates communication between HOST and VHOST
355 * Map BAR0 of EP CONTROLLER which contains the VHOST's config and
356 * self scratchpad region.
358 * Please note the self scratchpad region and config region is combined to
359 * a single region and mapped using the same BAR.
361 * Returns: Zero for success, or an error code in case of failure
363 static int epf_ntb_config_sspad_bar_set(struct epf_ntb *ntb)
365 struct pci_epf_bar *epf_bar;
366 enum pci_barno barno;
367 u8 func_no, vfunc_no;
371 dev = &ntb->epf->dev;
372 func_no = ntb->epf->func_no;
373 vfunc_no = ntb->epf->vfunc_no;
374 barno = ntb->epf_ntb_bar[BAR_CONFIG];
375 epf_bar = &ntb->epf->bar[barno];
377 ret = pci_epc_set_bar(ntb->epf->epc, func_no, vfunc_no, epf_bar);
379 dev_err(dev, "inft: Config/Status/SPAD BAR set failed\n");
386 * epf_ntb_config_spad_bar_free() - Free the physical memory associated with
387 * config + scratchpad region
388 * @ntb: NTB device that facilitates communication between HOST and VHOST
390 static void epf_ntb_config_spad_bar_free(struct epf_ntb *ntb)
392 enum pci_barno barno;
394 barno = ntb->epf_ntb_bar[BAR_CONFIG];
395 pci_epf_free_space(ntb->epf, ntb->reg, barno, 0);
399 * epf_ntb_config_spad_bar_alloc() - Allocate memory for config + scratchpad
401 * @ntb: NTB device that facilitates communication between HOST and VHOST
403 * Allocate the Local Memory mentioned in the above diagram. The size of
404 * CONFIG REGION is sizeof(struct epf_ntb_ctrl) and size of SCRATCHPAD REGION
405 * is obtained from "spad-count" configfs entry.
407 * Returns: Zero for success, or an error code in case of failure
409 static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb)
412 enum pci_barno barno;
413 struct epf_ntb_ctrl *ctrl;
414 u32 spad_size, ctrl_size;
416 struct pci_epf *epf = ntb->epf;
417 struct device *dev = &epf->dev;
421 const struct pci_epc_features *epc_features = pci_epc_get_features(epf->epc,
424 barno = ntb->epf_ntb_bar[BAR_CONFIG];
425 size = epc_features->bar_fixed_size[barno];
426 align = epc_features->align;
428 if ((!IS_ALIGNED(size, align)))
431 spad_count = ntb->spad_count;
433 ctrl_size = sizeof(struct epf_ntb_ctrl);
434 spad_size = 2 * spad_count * sizeof(u32);
437 ctrl_size = roundup_pow_of_two(ctrl_size);
438 spad_size = roundup_pow_of_two(spad_size);
440 ctrl_size = ALIGN(ctrl_size, align);
441 spad_size = ALIGN(spad_size, align);
445 size = ctrl_size + spad_size;
446 else if (size < ctrl_size + spad_size)
449 base = pci_epf_alloc_space(epf, size, barno, align, 0);
451 dev_err(dev, "Config/Status/SPAD alloc region fail\n");
458 ctrl->spad_offset = ctrl_size;
460 ctrl->spad_count = spad_count;
461 ctrl->num_mws = ntb->num_mws;
462 ntb->spad_size = spad_size;
464 ctrl->db_entry_size = sizeof(u32);
466 for (i = 0; i < ntb->db_count; i++) {
467 ntb->reg->db_data[i] = 1 + i;
468 ntb->reg->db_offset[i] = 0;
475 * epf_ntb_configure_interrupt() - Configure MSI/MSI-X capability
476 * @ntb: NTB device that facilitates communication between HOST and VHOST
478 * Configure MSI/MSI-X capability for each interface with number of
479 * interrupts equal to "db_count" configfs entry.
481 * Returns: Zero for success, or an error code in case of failure
483 static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
485 const struct pci_epc_features *epc_features;
490 dev = &ntb->epf->dev;
492 epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
494 if (!(epc_features->msix_capable || epc_features->msi_capable)) {
495 dev_err(dev, "MSI or MSI-X is required for doorbell\n");
499 db_count = ntb->db_count;
500 if (db_count > MAX_DB_COUNT) {
501 dev_err(dev, "DB count cannot be more than %d\n", MAX_DB_COUNT);
505 ntb->db_count = db_count;
507 if (epc_features->msi_capable) {
508 ret = pci_epc_set_msi(ntb->epf->epc,
513 dev_err(dev, "MSI configuration failed\n");
522 * epf_ntb_db_bar_init() - Configure Doorbell window BARs
523 * @ntb: NTB device that facilitates communication between HOST and VHOST
525 * Returns: Zero for success, or an error code in case of failure
527 static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
529 const struct pci_epc_features *epc_features;
531 struct device *dev = &ntb->epf->dev;
533 struct pci_epf_bar *epf_bar;
534 void __iomem *mw_addr;
535 enum pci_barno barno;
536 size_t size = sizeof(u32) * ntb->db_count;
538 epc_features = pci_epc_get_features(ntb->epf->epc,
541 align = epc_features->align;
547 size = ALIGN(size, align);
549 size = roundup_pow_of_two(size);
551 barno = ntb->epf_ntb_bar[BAR_DB];
553 mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, align, 0);
555 dev_err(dev, "Failed to allocate OB address\n");
559 ntb->epf_db = mw_addr;
561 epf_bar = &ntb->epf->bar[barno];
563 ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
565 dev_err(dev, "Doorbell BAR set failed\n");
566 goto err_alloc_peer_mem;
571 pci_epf_free_space(ntb->epf, mw_addr, barno, 0);
575 static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws);
578 * epf_ntb_db_bar_clear() - Clear doorbell BAR and free memory
579 * allocated in peer's outbound address space
580 * @ntb: NTB device that facilitates communication between HOST and VHOST
582 static void epf_ntb_db_bar_clear(struct epf_ntb *ntb)
584 enum pci_barno barno;
586 barno = ntb->epf_ntb_bar[BAR_DB];
587 pci_epf_free_space(ntb->epf, ntb->epf_db, barno, 0);
588 pci_epc_clear_bar(ntb->epf->epc,
591 &ntb->epf->bar[barno]);
595 * epf_ntb_mw_bar_init() - Configure Memory window BARs
596 * @ntb: NTB device that facilitates communication between HOST and VHOST
598 * Returns: Zero for success, or an error code in case of failure
600 static int epf_ntb_mw_bar_init(struct epf_ntb *ntb)
605 enum pci_barno barno;
606 struct device *dev = &ntb->epf->dev;
608 for (i = 0; i < ntb->num_mws; i++) {
609 size = ntb->mws_size[i];
610 barno = ntb->epf_ntb_bar[BAR_MW0 + i];
612 ntb->epf->bar[barno].barno = barno;
613 ntb->epf->bar[barno].size = size;
614 ntb->epf->bar[barno].addr = NULL;
615 ntb->epf->bar[barno].phys_addr = 0;
616 ntb->epf->bar[barno].flags |= upper_32_bits(size) ?
617 PCI_BASE_ADDRESS_MEM_TYPE_64 :
618 PCI_BASE_ADDRESS_MEM_TYPE_32;
620 ret = pci_epc_set_bar(ntb->epf->epc,
623 &ntb->epf->bar[barno]);
625 dev_err(dev, "MW set failed\n");
629 /* Allocate EPC outbound memory windows to vpci vntb device */
630 ntb->vpci_mw_addr[i] = pci_epc_mem_alloc_addr(ntb->epf->epc,
631 &ntb->vpci_mw_phy[i],
633 if (!ntb->vpci_mw_addr[i]) {
635 dev_err(dev, "Failed to allocate source address\n");
643 pci_epc_clear_bar(ntb->epf->epc,
646 &ntb->epf->bar[barno]);
648 epf_ntb_mw_bar_clear(ntb, i);
653 * epf_ntb_mw_bar_clear() - Clear Memory window BARs
654 * @ntb: NTB device that facilitates communication between HOST and VHOST
655 * @num_mws: the number of Memory window BARs that to be cleared
657 static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws)
659 enum pci_barno barno;
662 for (i = 0; i < num_mws; i++) {
663 barno = ntb->epf_ntb_bar[BAR_MW0 + i];
664 pci_epc_clear_bar(ntb->epf->epc,
667 &ntb->epf->bar[barno]);
669 pci_epc_mem_free_addr(ntb->epf->epc,
671 ntb->vpci_mw_addr[i],
677 * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
678 * @ntb: NTB device that facilitates communication between HOST and VHOST
680 * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
682 static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
684 pci_epc_remove_epf(ntb->epf->epc, ntb->epf, 0);
685 pci_epc_put(ntb->epf->epc);
689 * epf_ntb_init_epc_bar() - Identify BARs to be used for each of the NTB
690 * constructs (scratchpad region, doorbell, memorywindow)
691 * @ntb: NTB device that facilitates communication between HOST and VHOST
693 * Returns: Zero for success, or an error code in case of failure
695 static int epf_ntb_init_epc_bar(struct epf_ntb *ntb)
697 const struct pci_epc_features *epc_features;
698 enum pci_barno barno;
699 enum epf_ntb_bar bar;
705 num_mws = ntb->num_mws;
706 dev = &ntb->epf->dev;
707 epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
709 /* These are required BARs which are mandatory for NTB functionality */
710 for (bar = BAR_CONFIG; bar <= BAR_MW0; bar++, barno++) {
711 barno = pci_epc_get_next_free_bar(epc_features, barno);
713 dev_err(dev, "Fail to get NTB function BAR\n");
716 ntb->epf_ntb_bar[bar] = barno;
719 /* These are optional BARs which don't impact NTB functionality */
720 for (bar = BAR_MW1, i = 1; i < num_mws; bar++, barno++, i++) {
721 barno = pci_epc_get_next_free_bar(epc_features, barno);
724 dev_dbg(dev, "BAR not available for > MW%d\n", i + 1);
726 ntb->epf_ntb_bar[bar] = barno;
733 * epf_ntb_epc_init() - Initialize NTB interface
734 * @ntb: NTB device that facilitates communication between HOST and VHOST
736 * Wrapper to initialize a particular EPC interface and start the workqueue
737 * to check for commands from HOST. This function will write to the
738 * EP controller HW for configuring it.
740 * Returns: Zero for success, or an error code in case of failure
742 static int epf_ntb_epc_init(struct epf_ntb *ntb)
744 u8 func_no, vfunc_no;
753 func_no = ntb->epf->func_no;
754 vfunc_no = ntb->epf->vfunc_no;
756 ret = epf_ntb_config_sspad_bar_set(ntb);
758 dev_err(dev, "Config/self SPAD BAR init failed");
762 ret = epf_ntb_configure_interrupt(ntb);
764 dev_err(dev, "Interrupt configuration failed\n");
765 goto err_config_interrupt;
768 ret = epf_ntb_db_bar_init(ntb);
770 dev_err(dev, "DB BAR init failed\n");
771 goto err_db_bar_init;
774 ret = epf_ntb_mw_bar_init(ntb);
776 dev_err(dev, "MW BAR init failed\n");
777 goto err_mw_bar_init;
781 ret = pci_epc_write_header(epc, func_no, vfunc_no, epf->header);
783 dev_err(dev, "Configuration header write failed\n");
784 goto err_write_header;
788 INIT_DELAYED_WORK(&ntb->cmd_handler, epf_ntb_cmd_handler);
789 queue_work(kpcintb_workqueue, &ntb->cmd_handler.work);
794 epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
796 epf_ntb_db_bar_clear(ntb);
798 err_config_interrupt:
799 epf_ntb_config_sspad_bar_clear(ntb);
806 * epf_ntb_epc_cleanup() - Cleanup all NTB interfaces
807 * @ntb: NTB device that facilitates communication between HOST and VHOST
809 * Wrapper to cleanup all NTB interfaces.
811 static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
813 epf_ntb_db_bar_clear(ntb);
814 epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
817 #define EPF_NTB_R(_name) \
818 static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
821 struct config_group *group = to_config_group(item); \
822 struct epf_ntb *ntb = to_epf_ntb(group); \
824 return sprintf(page, "%d\n", ntb->_name); \
827 #define EPF_NTB_W(_name) \
828 static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
829 const char *page, size_t len) \
831 struct config_group *group = to_config_group(item); \
832 struct epf_ntb *ntb = to_epf_ntb(group); \
836 ret = kstrtou32(page, 0, &val); \
845 #define EPF_NTB_MW_R(_name) \
846 static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
849 struct config_group *group = to_config_group(item); \
850 struct epf_ntb *ntb = to_epf_ntb(group); \
851 struct device *dev = &ntb->epf->dev; \
854 if (sscanf(#_name, "mw%d", &win_no) != 1) \
857 if (win_no <= 0 || win_no > ntb->num_mws) { \
858 dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
862 return sprintf(page, "%lld\n", ntb->mws_size[win_no - 1]); \
865 #define EPF_NTB_MW_W(_name) \
866 static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
867 const char *page, size_t len) \
869 struct config_group *group = to_config_group(item); \
870 struct epf_ntb *ntb = to_epf_ntb(group); \
871 struct device *dev = &ntb->epf->dev; \
876 ret = kstrtou64(page, 0, &val); \
880 if (sscanf(#_name, "mw%d", &win_no) != 1) \
883 if (win_no <= 0 || win_no > ntb->num_mws) { \
884 dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
888 ntb->mws_size[win_no - 1] = val; \
893 static ssize_t epf_ntb_num_mws_store(struct config_item *item,
894 const char *page, size_t len)
896 struct config_group *group = to_config_group(item);
897 struct epf_ntb *ntb = to_epf_ntb(group);
901 ret = kstrtou32(page, 0, &val);
913 EPF_NTB_R(spad_count)
914 EPF_NTB_W(spad_count)
918 EPF_NTB_R(vbus_number)
919 EPF_NTB_W(vbus_number)
933 CONFIGFS_ATTR(epf_ntb_, spad_count);
934 CONFIGFS_ATTR(epf_ntb_, db_count);
935 CONFIGFS_ATTR(epf_ntb_, num_mws);
936 CONFIGFS_ATTR(epf_ntb_, mw1);
937 CONFIGFS_ATTR(epf_ntb_, mw2);
938 CONFIGFS_ATTR(epf_ntb_, mw3);
939 CONFIGFS_ATTR(epf_ntb_, mw4);
940 CONFIGFS_ATTR(epf_ntb_, vbus_number);
941 CONFIGFS_ATTR(epf_ntb_, vntb_pid);
942 CONFIGFS_ATTR(epf_ntb_, vntb_vid);
944 static struct configfs_attribute *epf_ntb_attrs[] = {
945 &epf_ntb_attr_spad_count,
946 &epf_ntb_attr_db_count,
947 &epf_ntb_attr_num_mws,
952 &epf_ntb_attr_vbus_number,
953 &epf_ntb_attr_vntb_pid,
954 &epf_ntb_attr_vntb_vid,
958 static const struct config_item_type ntb_group_type = {
959 .ct_attrs = epf_ntb_attrs,
960 .ct_owner = THIS_MODULE,
964 * epf_ntb_add_cfs() - Add configfs directory specific to NTB
965 * @epf: NTB endpoint function device
966 * @group: A pointer to the config_group structure referencing a group of
967 * config_items of a specific type that belong to a specific sub-system.
969 * Add configfs directory specific to NTB. This directory will hold
970 * NTB specific properties like db_count, spad_count, num_mws etc.,
972 * Returns: Pointer to config_group
974 static struct config_group *epf_ntb_add_cfs(struct pci_epf *epf,
975 struct config_group *group)
977 struct epf_ntb *ntb = epf_get_drvdata(epf);
978 struct config_group *ntb_group = &ntb->group;
979 struct device *dev = &epf->dev;
981 config_group_init_type_name(ntb_group, dev_name(dev), &ntb_group_type);
986 /*==== virtual PCI bus driver, which only load virtual NTB PCI driver ====*/
988 static u32 pci_space[] = {
989 0xffffffff, /*DeviceID, Vendor ID*/
990 0, /*Status, Command*/
991 0xffffffff, /*Class code, subclass, prog if, revision id*/
992 0x40, /*bist, header type, latency Timer, cache line size*/
999 0, /*Cardbus cis point*/
1000 0, /*Subsystem ID Subystem vendor id*/
1001 0, /*ROM Base Address*/
1002 0, /*Reserved, Cap. Point*/
1004 0, /*Max Lat, Min Gnt, interrupt pin, interrupt line*/
1007 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val)
1010 memcpy(val, ((u8 *)pci_space) + where, size);
1011 return PCIBIOS_SUCCESSFUL;
1013 return PCIBIOS_DEVICE_NOT_FOUND;
1016 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
1021 static struct pci_ops vpci_ops = {
1026 static int vpci_scan_bus(void *sysdata)
1028 struct pci_bus *vpci_bus;
1029 struct epf_ntb *ndev = sysdata;
1031 vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
1033 pr_err("create pci bus\n");
1035 pci_bus_add_devices(vpci_bus);
1040 /*==================== Virtual PCIe NTB driver ==========================*/
1042 static int vntb_epf_mw_count(struct ntb_dev *ntb, int pidx)
1044 struct epf_ntb *ndev = ntb_ndev(ntb);
1046 return ndev->num_mws;
1049 static int vntb_epf_spad_count(struct ntb_dev *ntb)
1051 return ntb_ndev(ntb)->spad_count;
1054 static int vntb_epf_peer_mw_count(struct ntb_dev *ntb)
1056 return ntb_ndev(ntb)->num_mws;
1059 static u64 vntb_epf_db_valid_mask(struct ntb_dev *ntb)
1061 return BIT_ULL(ntb_ndev(ntb)->db_count) - 1;
1064 static int vntb_epf_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1069 static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
1070 dma_addr_t addr, resource_size_t size)
1072 struct epf_ntb *ntb = ntb_ndev(ndev);
1073 struct pci_epf_bar *epf_bar;
1074 enum pci_barno barno;
1078 dev = &ntb->ntb.dev;
1079 barno = ntb->epf_ntb_bar[BAR_MW0 + idx];
1080 epf_bar = &ntb->epf->bar[barno];
1081 epf_bar->phys_addr = addr;
1082 epf_bar->barno = barno;
1083 epf_bar->size = size;
1085 ret = pci_epc_set_bar(ntb->epf->epc, 0, 0, epf_bar);
1087 dev_err(dev, "failure set mw trans\n");
1093 static int vntb_epf_mw_clear_trans(struct ntb_dev *ntb, int pidx, int idx)
1098 static int vntb_epf_peer_mw_get_addr(struct ntb_dev *ndev, int idx,
1099 phys_addr_t *base, resource_size_t *size)
1102 struct epf_ntb *ntb = ntb_ndev(ndev);
1105 *base = ntb->vpci_mw_phy[idx];
1108 *size = ntb->mws_size[idx];
1113 static int vntb_epf_link_enable(struct ntb_dev *ntb,
1114 enum ntb_speed max_speed,
1115 enum ntb_width max_width)
1120 static u32 vntb_epf_spad_read(struct ntb_dev *ndev, int idx)
1122 struct epf_ntb *ntb = ntb_ndev(ndev);
1123 int off = ntb->reg->spad_offset, ct = ntb->reg->spad_count * sizeof(u32);
1125 void __iomem *base = (void __iomem *)ntb->reg;
1127 val = readl(base + off + ct + idx * sizeof(u32));
1131 static int vntb_epf_spad_write(struct ntb_dev *ndev, int idx, u32 val)
1133 struct epf_ntb *ntb = ntb_ndev(ndev);
1134 struct epf_ntb_ctrl *ctrl = ntb->reg;
1135 int off = ctrl->spad_offset, ct = ctrl->spad_count * sizeof(u32);
1136 void __iomem *base = (void __iomem *)ntb->reg;
1138 writel(val, base + off + ct + idx * sizeof(u32));
1142 static u32 vntb_epf_peer_spad_read(struct ntb_dev *ndev, int pidx, int idx)
1144 struct epf_ntb *ntb = ntb_ndev(ndev);
1145 struct epf_ntb_ctrl *ctrl = ntb->reg;
1146 int off = ctrl->spad_offset;
1147 void __iomem *base = (void __iomem *)ntb->reg;
1150 val = readl(base + off + idx * sizeof(u32));
1154 static int vntb_epf_peer_spad_write(struct ntb_dev *ndev, int pidx, int idx, u32 val)
1156 struct epf_ntb *ntb = ntb_ndev(ndev);
1157 struct epf_ntb_ctrl *ctrl = ntb->reg;
1158 int off = ctrl->spad_offset;
1159 void __iomem *base = (void __iomem *)ntb->reg;
1161 writel(val, base + off + idx * sizeof(u32));
1165 static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
1167 u32 interrupt_num = ffs(db_bits) + 1;
1168 struct epf_ntb *ntb = ntb_ndev(ndev);
1169 u8 func_no, vfunc_no;
1172 func_no = ntb->epf->func_no;
1173 vfunc_no = ntb->epf->vfunc_no;
1175 ret = pci_epc_raise_irq(ntb->epf->epc,
1181 dev_err(&ntb->ntb.dev, "Failed to raise IRQ\n");
1186 static u64 vntb_epf_db_read(struct ntb_dev *ndev)
1188 struct epf_ntb *ntb = ntb_ndev(ndev);
1193 static int vntb_epf_mw_get_align(struct ntb_dev *ndev, int pidx, int idx,
1194 resource_size_t *addr_align,
1195 resource_size_t *size_align,
1196 resource_size_t *size_max)
1198 struct epf_ntb *ntb = ntb_ndev(ndev);
1201 *addr_align = SZ_4K;
1207 *size_max = ntb->mws_size[idx];
1212 static u64 vntb_epf_link_is_up(struct ntb_dev *ndev,
1213 enum ntb_speed *speed,
1214 enum ntb_width *width)
1216 struct epf_ntb *ntb = ntb_ndev(ndev);
1218 return ntb->reg->link_status;
1221 static int vntb_epf_db_clear_mask(struct ntb_dev *ndev, u64 db_bits)
1226 static int vntb_epf_db_clear(struct ntb_dev *ndev, u64 db_bits)
1228 struct epf_ntb *ntb = ntb_ndev(ndev);
1230 ntb->db &= ~db_bits;
1234 static int vntb_epf_link_disable(struct ntb_dev *ntb)
1239 static const struct ntb_dev_ops vntb_epf_ops = {
1240 .mw_count = vntb_epf_mw_count,
1241 .spad_count = vntb_epf_spad_count,
1242 .peer_mw_count = vntb_epf_peer_mw_count,
1243 .db_valid_mask = vntb_epf_db_valid_mask,
1244 .db_set_mask = vntb_epf_db_set_mask,
1245 .mw_set_trans = vntb_epf_mw_set_trans,
1246 .mw_clear_trans = vntb_epf_mw_clear_trans,
1247 .peer_mw_get_addr = vntb_epf_peer_mw_get_addr,
1248 .link_enable = vntb_epf_link_enable,
1249 .spad_read = vntb_epf_spad_read,
1250 .spad_write = vntb_epf_spad_write,
1251 .peer_spad_read = vntb_epf_peer_spad_read,
1252 .peer_spad_write = vntb_epf_peer_spad_write,
1253 .peer_db_set = vntb_epf_peer_db_set,
1254 .db_read = vntb_epf_db_read,
1255 .mw_get_align = vntb_epf_mw_get_align,
1256 .link_is_up = vntb_epf_link_is_up,
1257 .db_clear_mask = vntb_epf_db_clear_mask,
1258 .db_clear = vntb_epf_db_clear,
1259 .link_disable = vntb_epf_link_disable,
1262 static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1265 struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
1266 struct device *dev = &pdev->dev;
1268 ndev->ntb.pdev = pdev;
1269 ndev->ntb.topo = NTB_TOPO_NONE;
1270 ndev->ntb.ops = &vntb_epf_ops;
1272 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1274 dev_err(dev, "Cannot set DMA mask\n");
1278 ret = ntb_register_device(&ndev->ntb);
1280 dev_err(dev, "Failed to register NTB device\n");
1281 goto err_register_dev;
1284 dev_dbg(dev, "PCI Virtual NTB driver loaded\n");
1288 put_device(&ndev->ntb.dev);
1292 static struct pci_device_id pci_vntb_table[] = {
1294 PCI_DEVICE(0xffff, 0xffff),
1299 static struct pci_driver vntb_pci_driver = {
1301 .id_table = pci_vntb_table,
1302 .probe = pci_vntb_probe,
1305 /* ============ PCIe EPF Driver Bind ====================*/
1308 * epf_ntb_bind() - Initialize endpoint controller to provide NTB functionality
1309 * @epf: NTB endpoint function device
1311 * Initialize both the endpoint controllers associated with NTB function device.
1312 * Invoked when a primary interface or secondary interface is bound to EPC
1313 * device. This function will succeed only when EPC is bound to both the
1316 * Returns: Zero for success, or an error code in case of failure
1318 static int epf_ntb_bind(struct pci_epf *epf)
1320 struct epf_ntb *ntb = epf_get_drvdata(epf);
1321 struct device *dev = &epf->dev;
1325 dev_dbg(dev, "PRIMARY EPC interface not yet bound\n");
1329 ret = epf_ntb_init_epc_bar(ntb);
1331 dev_err(dev, "Failed to create NTB EPC\n");
1335 ret = epf_ntb_config_spad_bar_alloc(ntb);
1337 dev_err(dev, "Failed to allocate BAR memory\n");
1341 ret = epf_ntb_epc_init(ntb);
1343 dev_err(dev, "Failed to initialize EPC\n");
1347 epf_set_drvdata(epf, ntb);
1349 pci_space[0] = (ntb->vntb_pid << 16) | ntb->vntb_vid;
1350 pci_vntb_table[0].vendor = ntb->vntb_vid;
1351 pci_vntb_table[0].device = ntb->vntb_pid;
1353 ret = pci_register_driver(&vntb_pci_driver);
1355 dev_err(dev, "failure register vntb pci driver\n");
1364 epf_ntb_config_spad_bar_free(ntb);
1367 epf_ntb_epc_destroy(ntb);
1373 * epf_ntb_unbind() - Cleanup the initialization from epf_ntb_bind()
1374 * @epf: NTB endpoint function device
1376 * Cleanup the initialization from epf_ntb_bind()
1378 static void epf_ntb_unbind(struct pci_epf *epf)
1380 struct epf_ntb *ntb = epf_get_drvdata(epf);
1382 epf_ntb_epc_cleanup(ntb);
1383 epf_ntb_config_spad_bar_free(ntb);
1384 epf_ntb_epc_destroy(ntb);
1386 pci_unregister_driver(&vntb_pci_driver);
1390 static struct pci_epf_ops epf_ntb_ops = {
1391 .bind = epf_ntb_bind,
1392 .unbind = epf_ntb_unbind,
1393 .add_cfs = epf_ntb_add_cfs,
1397 * epf_ntb_probe() - Probe NTB function driver
1398 * @epf: NTB endpoint function device
1399 * @id: NTB endpoint function device ID
1401 * Probe NTB function driver when endpoint function bus detects a NTB
1402 * endpoint function.
1404 * Returns: Zero for success, or an error code in case of failure
1406 static int epf_ntb_probe(struct pci_epf *epf,
1407 const struct pci_epf_device_id *id)
1409 struct epf_ntb *ntb;
1414 ntb = devm_kzalloc(dev, sizeof(*ntb), GFP_KERNEL);
1418 epf->header = &epf_ntb_header;
1420 ntb->vbus_number = 0xff;
1421 epf_set_drvdata(epf, ntb);
1423 dev_info(dev, "pci-ep epf driver loaded\n");
1427 static const struct pci_epf_device_id epf_ntb_ids[] = {
1429 .name = "pci_epf_vntb",
1434 static struct pci_epf_driver epf_ntb_driver = {
1435 .driver.name = "pci_epf_vntb",
1436 .probe = epf_ntb_probe,
1437 .id_table = epf_ntb_ids,
1438 .ops = &epf_ntb_ops,
1439 .owner = THIS_MODULE,
1442 static int __init epf_ntb_init(void)
1446 kpcintb_workqueue = alloc_workqueue("kpcintb", WQ_MEM_RECLAIM |
1448 ret = pci_epf_register_driver(&epf_ntb_driver);
1450 destroy_workqueue(kpcintb_workqueue);
1451 pr_err("Failed to register pci epf ntb driver --> %d\n", ret);
1457 module_init(epf_ntb_init);
1459 static void __exit epf_ntb_exit(void)
1461 pci_epf_unregister_driver(&epf_ntb_driver);
1462 destroy_workqueue(kpcintb_workqueue);
1464 module_exit(epf_ntb_exit);
1466 MODULE_DESCRIPTION("PCI EPF NTB DRIVER");
1468 MODULE_LICENSE("GPL v2");