pci-epf-vntb.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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 <[email protected]>
  11. * Author: Kishon Vijay Abraham I <[email protected]>
  12. */
  13. /*
  14. * +------------+ +---------------------------------------+
  15. * | | | |
  16. * +------------+ | +--------------+
  17. * | NTB | | | NTB |
  18. * | NetDev | | | NetDev |
  19. * +------------+ | +--------------+
  20. * | NTB | | | NTB |
  21. * | Transfer | | | Transfer |
  22. * +------------+ | +--------------+
  23. * | | | | |
  24. * | PCI NTB | | | |
  25. * | EPF | | | |
  26. * | Driver | | | PCI Virtual |
  27. * | | +---------------+ | NTB Driver |
  28. * | | | PCI EP NTB |<------>| |
  29. * | | | FN Driver | | |
  30. * +------------+ +---------------+ +--------------+
  31. * | | | | | |
  32. * | PCI Bus | <-----> | PCI EP Bus | | Virtual PCI |
  33. * | | PCI | | | Bus |
  34. * +------------+ +---------------+--------+--------------+
  35. * PCIe Root Port PCI EP
  36. */
  37. #include <linux/delay.h>
  38. #include <linux/io.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/pci-epc.h>
  42. #include <linux/pci-epf.h>
  43. #include <linux/ntb.h>
  44. static struct workqueue_struct *kpcintb_workqueue;
  45. #define COMMAND_CONFIGURE_DOORBELL 1
  46. #define COMMAND_TEARDOWN_DOORBELL 2
  47. #define COMMAND_CONFIGURE_MW 3
  48. #define COMMAND_TEARDOWN_MW 4
  49. #define COMMAND_LINK_UP 5
  50. #define COMMAND_LINK_DOWN 6
  51. #define COMMAND_STATUS_OK 1
  52. #define COMMAND_STATUS_ERROR 2
  53. #define LINK_STATUS_UP BIT(0)
  54. #define SPAD_COUNT 64
  55. #define DB_COUNT 4
  56. #define NTB_MW_OFFSET 2
  57. #define DB_COUNT_MASK GENMASK(15, 0)
  58. #define MSIX_ENABLE BIT(16)
  59. #define MAX_DB_COUNT 32
  60. #define MAX_MW 4
  61. enum epf_ntb_bar {
  62. BAR_CONFIG,
  63. BAR_DB,
  64. BAR_MW0,
  65. BAR_MW1,
  66. BAR_MW2,
  67. };
  68. /*
  69. * +--------------------------------------------------+ Base
  70. * | |
  71. * | |
  72. * | |
  73. * | Common Control Register |
  74. * | |
  75. * | |
  76. * | |
  77. * +-----------------------+--------------------------+ Base+span_offset
  78. * | | |
  79. * | Peer Span Space | Span Space |
  80. * | | |
  81. * | | |
  82. * +-----------------------+--------------------------+ Base+span_offset
  83. * | | | +span_count * 4
  84. * | | |
  85. * | Span Space | Peer Span Space |
  86. * | | |
  87. * +-----------------------+--------------------------+
  88. * Virtual PCI PCIe Endpoint
  89. * NTB Driver NTB Driver
  90. */
  91. struct epf_ntb_ctrl {
  92. u32 command;
  93. u32 argument;
  94. u16 command_status;
  95. u16 link_status;
  96. u32 topology;
  97. u64 addr;
  98. u64 size;
  99. u32 num_mws;
  100. u32 reserved;
  101. u32 spad_offset;
  102. u32 spad_count;
  103. u32 db_entry_size;
  104. u32 db_data[MAX_DB_COUNT];
  105. u32 db_offset[MAX_DB_COUNT];
  106. } __packed;
  107. struct epf_ntb {
  108. struct ntb_dev ntb;
  109. struct pci_epf *epf;
  110. struct config_group group;
  111. u32 num_mws;
  112. u32 db_count;
  113. u32 spad_count;
  114. u64 mws_size[MAX_MW];
  115. u64 db;
  116. u32 vbus_number;
  117. u16 vntb_pid;
  118. u16 vntb_vid;
  119. bool linkup;
  120. u32 spad_size;
  121. enum pci_barno epf_ntb_bar[6];
  122. struct epf_ntb_ctrl *reg;
  123. phys_addr_t epf_db_phy;
  124. void __iomem *epf_db;
  125. phys_addr_t vpci_mw_phy[MAX_MW];
  126. void __iomem *vpci_mw_addr[MAX_MW];
  127. struct delayed_work cmd_handler;
  128. };
  129. #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
  130. #define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
  131. static struct pci_epf_header epf_ntb_header = {
  132. .vendorid = PCI_ANY_ID,
  133. .deviceid = PCI_ANY_ID,
  134. .baseclass_code = PCI_BASE_CLASS_MEMORY,
  135. .interrupt_pin = PCI_INTERRUPT_INTA,
  136. };
  137. /**
  138. * epf_ntb_link_up() - Raise link_up interrupt to Virtual Host (VHOST)
  139. * @ntb: NTB device that facilitates communication between HOST and VHOST
  140. * @link_up: true or false indicating Link is UP or Down
  141. *
  142. * Once NTB function in HOST invoke ntb_link_enable(),
  143. * this NTB function driver will trigger a link event to VHOST.
  144. *
  145. * Returns: Zero for success, or an error code in case of failure
  146. */
  147. static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
  148. {
  149. if (link_up)
  150. ntb->reg->link_status |= LINK_STATUS_UP;
  151. else
  152. ntb->reg->link_status &= ~LINK_STATUS_UP;
  153. ntb_link_event(&ntb->ntb);
  154. return 0;
  155. }
  156. /**
  157. * epf_ntb_configure_mw() - Configure the Outbound Address Space for VHOST
  158. * to access the memory window of HOST
  159. * @ntb: NTB device that facilitates communication between HOST and VHOST
  160. * @mw: Index of the memory window (either 0, 1, 2 or 3)
  161. *
  162. * EP Outbound Window
  163. * +--------+ +-----------+
  164. * | | | |
  165. * | | | |
  166. * | | | |
  167. * | | | |
  168. * | | +-----------+
  169. * | Virtual| | Memory Win|
  170. * | NTB | -----------> | |
  171. * | Driver | | |
  172. * | | +-----------+
  173. * | | | |
  174. * | | | |
  175. * +--------+ +-----------+
  176. * VHOST PCI EP
  177. *
  178. * Returns: Zero for success, or an error code in case of failure
  179. */
  180. static int epf_ntb_configure_mw(struct epf_ntb *ntb, u32 mw)
  181. {
  182. phys_addr_t phys_addr;
  183. u8 func_no, vfunc_no;
  184. u64 addr, size;
  185. int ret = 0;
  186. phys_addr = ntb->vpci_mw_phy[mw];
  187. addr = ntb->reg->addr;
  188. size = ntb->reg->size;
  189. func_no = ntb->epf->func_no;
  190. vfunc_no = ntb->epf->vfunc_no;
  191. ret = pci_epc_map_addr(ntb->epf->epc, func_no, vfunc_no, phys_addr, addr, size);
  192. if (ret)
  193. dev_err(&ntb->epf->epc->dev,
  194. "Failed to map memory window %d address\n", mw);
  195. return ret;
  196. }
  197. /**
  198. * epf_ntb_teardown_mw() - Teardown the configured OB ATU
  199. * @ntb: NTB device that facilitates communication between HOST and VHOST
  200. * @mw: Index of the memory window (either 0, 1, 2 or 3)
  201. *
  202. * Teardown the configured OB ATU configured in epf_ntb_configure_mw() using
  203. * pci_epc_unmap_addr()
  204. */
  205. static void epf_ntb_teardown_mw(struct epf_ntb *ntb, u32 mw)
  206. {
  207. pci_epc_unmap_addr(ntb->epf->epc,
  208. ntb->epf->func_no,
  209. ntb->epf->vfunc_no,
  210. ntb->vpci_mw_phy[mw]);
  211. }
  212. /**
  213. * epf_ntb_cmd_handler() - Handle commands provided by the NTB HOST
  214. * @work: work_struct for the epf_ntb_epc
  215. *
  216. * Workqueue function that gets invoked for the two epf_ntb_epc
  217. * periodically (once every 5ms) to see if it has received any commands
  218. * from NTB HOST. The HOST can send commands to configure doorbell or
  219. * configure memory window or to update link status.
  220. */
  221. static void epf_ntb_cmd_handler(struct work_struct *work)
  222. {
  223. struct epf_ntb_ctrl *ctrl;
  224. u32 command, argument;
  225. struct epf_ntb *ntb;
  226. struct device *dev;
  227. int ret;
  228. int i;
  229. ntb = container_of(work, struct epf_ntb, cmd_handler.work);
  230. for (i = 1; i < ntb->db_count; i++) {
  231. if (readl(ntb->epf_db + i * 4)) {
  232. if (readl(ntb->epf_db + i * 4))
  233. ntb->db |= 1 << (i - 1);
  234. ntb_db_event(&ntb->ntb, i);
  235. writel(0, ntb->epf_db + i * 4);
  236. }
  237. }
  238. ctrl = ntb->reg;
  239. command = ctrl->command;
  240. if (!command)
  241. goto reset_handler;
  242. argument = ctrl->argument;
  243. ctrl->command = 0;
  244. ctrl->argument = 0;
  245. ctrl = ntb->reg;
  246. dev = &ntb->epf->dev;
  247. switch (command) {
  248. case COMMAND_CONFIGURE_DOORBELL:
  249. ctrl->command_status = COMMAND_STATUS_OK;
  250. break;
  251. case COMMAND_TEARDOWN_DOORBELL:
  252. ctrl->command_status = COMMAND_STATUS_OK;
  253. break;
  254. case COMMAND_CONFIGURE_MW:
  255. ret = epf_ntb_configure_mw(ntb, argument);
  256. if (ret < 0)
  257. ctrl->command_status = COMMAND_STATUS_ERROR;
  258. else
  259. ctrl->command_status = COMMAND_STATUS_OK;
  260. break;
  261. case COMMAND_TEARDOWN_MW:
  262. epf_ntb_teardown_mw(ntb, argument);
  263. ctrl->command_status = COMMAND_STATUS_OK;
  264. break;
  265. case COMMAND_LINK_UP:
  266. ntb->linkup = true;
  267. ret = epf_ntb_link_up(ntb, true);
  268. if (ret < 0)
  269. ctrl->command_status = COMMAND_STATUS_ERROR;
  270. else
  271. ctrl->command_status = COMMAND_STATUS_OK;
  272. goto reset_handler;
  273. case COMMAND_LINK_DOWN:
  274. ntb->linkup = false;
  275. ret = epf_ntb_link_up(ntb, false);
  276. if (ret < 0)
  277. ctrl->command_status = COMMAND_STATUS_ERROR;
  278. else
  279. ctrl->command_status = COMMAND_STATUS_OK;
  280. break;
  281. default:
  282. dev_err(dev, "UNKNOWN command: %d\n", command);
  283. break;
  284. }
  285. reset_handler:
  286. queue_delayed_work(kpcintb_workqueue, &ntb->cmd_handler,
  287. msecs_to_jiffies(5));
  288. }
  289. /**
  290. * epf_ntb_config_sspad_bar_clear() - Clear Config + Self scratchpad BAR
  291. * @ntb: EPC associated with one of the HOST which holds peer's outbound
  292. * address.
  293. *
  294. * Clear BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
  295. * self scratchpad region (removes inbound ATU configuration). While BAR0 is
  296. * the default self scratchpad BAR, an NTB could have other BARs for self
  297. * scratchpad (because of reserved BARs). This function can get the exact BAR
  298. * used for self scratchpad from epf_ntb_bar[BAR_CONFIG].
  299. *
  300. * Please note the self scratchpad region and config region is combined to
  301. * a single region and mapped using the same BAR. Also note VHOST's peer
  302. * scratchpad is HOST's self scratchpad.
  303. *
  304. * Returns: void
  305. */
  306. static void epf_ntb_config_sspad_bar_clear(struct epf_ntb *ntb)
  307. {
  308. struct pci_epf_bar *epf_bar;
  309. enum pci_barno barno;
  310. barno = ntb->epf_ntb_bar[BAR_CONFIG];
  311. epf_bar = &ntb->epf->bar[barno];
  312. pci_epc_clear_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
  313. }
  314. /**
  315. * epf_ntb_config_sspad_bar_set() - Set Config + Self scratchpad BAR
  316. * @ntb: NTB device that facilitates communication between HOST and VHOST
  317. *
  318. * Map BAR0 of EP CONTROLLER which contains the VHOST's config and
  319. * self scratchpad region.
  320. *
  321. * Please note the self scratchpad region and config region is combined to
  322. * a single region and mapped using the same BAR.
  323. *
  324. * Returns: Zero for success, or an error code in case of failure
  325. */
  326. static int epf_ntb_config_sspad_bar_set(struct epf_ntb *ntb)
  327. {
  328. struct pci_epf_bar *epf_bar;
  329. enum pci_barno barno;
  330. u8 func_no, vfunc_no;
  331. struct device *dev;
  332. int ret;
  333. dev = &ntb->epf->dev;
  334. func_no = ntb->epf->func_no;
  335. vfunc_no = ntb->epf->vfunc_no;
  336. barno = ntb->epf_ntb_bar[BAR_CONFIG];
  337. epf_bar = &ntb->epf->bar[barno];
  338. ret = pci_epc_set_bar(ntb->epf->epc, func_no, vfunc_no, epf_bar);
  339. if (ret) {
  340. dev_err(dev, "inft: Config/Status/SPAD BAR set failed\n");
  341. return ret;
  342. }
  343. return 0;
  344. }
  345. /**
  346. * epf_ntb_config_spad_bar_free() - Free the physical memory associated with
  347. * config + scratchpad region
  348. * @ntb: NTB device that facilitates communication between HOST and VHOST
  349. */
  350. static void epf_ntb_config_spad_bar_free(struct epf_ntb *ntb)
  351. {
  352. enum pci_barno barno;
  353. barno = ntb->epf_ntb_bar[BAR_CONFIG];
  354. pci_epf_free_space(ntb->epf, ntb->reg, barno, 0);
  355. }
  356. /**
  357. * epf_ntb_config_spad_bar_alloc() - Allocate memory for config + scratchpad
  358. * region
  359. * @ntb: NTB device that facilitates communication between HOST and VHOST
  360. *
  361. * Allocate the Local Memory mentioned in the above diagram. The size of
  362. * CONFIG REGION is sizeof(struct epf_ntb_ctrl) and size of SCRATCHPAD REGION
  363. * is obtained from "spad-count" configfs entry.
  364. *
  365. * Returns: Zero for success, or an error code in case of failure
  366. */
  367. static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb)
  368. {
  369. size_t align;
  370. enum pci_barno barno;
  371. struct epf_ntb_ctrl *ctrl;
  372. u32 spad_size, ctrl_size;
  373. u64 size;
  374. struct pci_epf *epf = ntb->epf;
  375. struct device *dev = &epf->dev;
  376. u32 spad_count;
  377. void *base;
  378. int i;
  379. const struct pci_epc_features *epc_features = pci_epc_get_features(epf->epc,
  380. epf->func_no,
  381. epf->vfunc_no);
  382. barno = ntb->epf_ntb_bar[BAR_CONFIG];
  383. size = epc_features->bar_fixed_size[barno];
  384. align = epc_features->align;
  385. if ((!IS_ALIGNED(size, align)))
  386. return -EINVAL;
  387. spad_count = ntb->spad_count;
  388. ctrl_size = sizeof(struct epf_ntb_ctrl);
  389. spad_size = 2 * spad_count * 4;
  390. if (!align) {
  391. ctrl_size = roundup_pow_of_two(ctrl_size);
  392. spad_size = roundup_pow_of_two(spad_size);
  393. } else {
  394. ctrl_size = ALIGN(ctrl_size, align);
  395. spad_size = ALIGN(spad_size, align);
  396. }
  397. if (!size)
  398. size = ctrl_size + spad_size;
  399. else if (size < ctrl_size + spad_size)
  400. return -EINVAL;
  401. base = pci_epf_alloc_space(epf, size, barno, align, 0);
  402. if (!base) {
  403. dev_err(dev, "Config/Status/SPAD alloc region fail\n");
  404. return -ENOMEM;
  405. }
  406. ntb->reg = base;
  407. ctrl = ntb->reg;
  408. ctrl->spad_offset = ctrl_size;
  409. ctrl->spad_count = spad_count;
  410. ctrl->num_mws = ntb->num_mws;
  411. ntb->spad_size = spad_size;
  412. ctrl->db_entry_size = 4;
  413. for (i = 0; i < ntb->db_count; i++) {
  414. ntb->reg->db_data[i] = 1 + i;
  415. ntb->reg->db_offset[i] = 0;
  416. }
  417. return 0;
  418. }
  419. /**
  420. * epf_ntb_configure_interrupt() - Configure MSI/MSI-X capability
  421. * @ntb: NTB device that facilitates communication between HOST and VHOST
  422. *
  423. * Configure MSI/MSI-X capability for each interface with number of
  424. * interrupts equal to "db_count" configfs entry.
  425. *
  426. * Returns: Zero for success, or an error code in case of failure
  427. */
  428. static int epf_ntb_configure_interrupt(struct epf_ntb *ntb)
  429. {
  430. const struct pci_epc_features *epc_features;
  431. struct device *dev;
  432. u32 db_count;
  433. int ret;
  434. dev = &ntb->epf->dev;
  435. epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
  436. if (!(epc_features->msix_capable || epc_features->msi_capable)) {
  437. dev_err(dev, "MSI or MSI-X is required for doorbell\n");
  438. return -EINVAL;
  439. }
  440. db_count = ntb->db_count;
  441. if (db_count > MAX_DB_COUNT) {
  442. dev_err(dev, "DB count cannot be more than %d\n", MAX_DB_COUNT);
  443. return -EINVAL;
  444. }
  445. ntb->db_count = db_count;
  446. if (epc_features->msi_capable) {
  447. ret = pci_epc_set_msi(ntb->epf->epc,
  448. ntb->epf->func_no,
  449. ntb->epf->vfunc_no,
  450. 16);
  451. if (ret) {
  452. dev_err(dev, "MSI configuration failed\n");
  453. return ret;
  454. }
  455. }
  456. return 0;
  457. }
  458. /**
  459. * epf_ntb_db_bar_init() - Configure Doorbell window BARs
  460. * @ntb: NTB device that facilitates communication between HOST and VHOST
  461. *
  462. * Returns: Zero for success, or an error code in case of failure
  463. */
  464. static int epf_ntb_db_bar_init(struct epf_ntb *ntb)
  465. {
  466. const struct pci_epc_features *epc_features;
  467. u32 align;
  468. struct device *dev = &ntb->epf->dev;
  469. int ret;
  470. struct pci_epf_bar *epf_bar;
  471. void __iomem *mw_addr;
  472. enum pci_barno barno;
  473. size_t size = 4 * ntb->db_count;
  474. epc_features = pci_epc_get_features(ntb->epf->epc,
  475. ntb->epf->func_no,
  476. ntb->epf->vfunc_no);
  477. align = epc_features->align;
  478. if (size < 128)
  479. size = 128;
  480. if (align)
  481. size = ALIGN(size, align);
  482. else
  483. size = roundup_pow_of_two(size);
  484. barno = ntb->epf_ntb_bar[BAR_DB];
  485. mw_addr = pci_epf_alloc_space(ntb->epf, size, barno, align, 0);
  486. if (!mw_addr) {
  487. dev_err(dev, "Failed to allocate OB address\n");
  488. return -ENOMEM;
  489. }
  490. ntb->epf_db = mw_addr;
  491. epf_bar = &ntb->epf->bar[barno];
  492. ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no, epf_bar);
  493. if (ret) {
  494. dev_err(dev, "Doorbell BAR set failed\n");
  495. goto err_alloc_peer_mem;
  496. }
  497. return ret;
  498. err_alloc_peer_mem:
  499. pci_epf_free_space(ntb->epf, mw_addr, barno, 0);
  500. return -1;
  501. }
  502. static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws);
  503. /**
  504. * epf_ntb_db_bar_clear() - Clear doorbell BAR and free memory
  505. * allocated in peer's outbound address space
  506. * @ntb: NTB device that facilitates communication between HOST and VHOST
  507. */
  508. static void epf_ntb_db_bar_clear(struct epf_ntb *ntb)
  509. {
  510. enum pci_barno barno;
  511. barno = ntb->epf_ntb_bar[BAR_DB];
  512. pci_epf_free_space(ntb->epf, ntb->epf_db, barno, 0);
  513. pci_epc_clear_bar(ntb->epf->epc,
  514. ntb->epf->func_no,
  515. ntb->epf->vfunc_no,
  516. &ntb->epf->bar[barno]);
  517. }
  518. /**
  519. * epf_ntb_mw_bar_init() - Configure Memory window BARs
  520. * @ntb: NTB device that facilitates communication between HOST and VHOST
  521. *
  522. * Returns: Zero for success, or an error code in case of failure
  523. */
  524. static int epf_ntb_mw_bar_init(struct epf_ntb *ntb)
  525. {
  526. int ret = 0;
  527. int i;
  528. u64 size;
  529. enum pci_barno barno;
  530. struct device *dev = &ntb->epf->dev;
  531. for (i = 0; i < ntb->num_mws; i++) {
  532. size = ntb->mws_size[i];
  533. barno = ntb->epf_ntb_bar[BAR_MW0 + i];
  534. ntb->epf->bar[barno].barno = barno;
  535. ntb->epf->bar[barno].size = size;
  536. ntb->epf->bar[barno].addr = NULL;
  537. ntb->epf->bar[barno].phys_addr = 0;
  538. ntb->epf->bar[barno].flags |= upper_32_bits(size) ?
  539. PCI_BASE_ADDRESS_MEM_TYPE_64 :
  540. PCI_BASE_ADDRESS_MEM_TYPE_32;
  541. ret = pci_epc_set_bar(ntb->epf->epc,
  542. ntb->epf->func_no,
  543. ntb->epf->vfunc_no,
  544. &ntb->epf->bar[barno]);
  545. if (ret) {
  546. dev_err(dev, "MW set failed\n");
  547. goto err_alloc_mem;
  548. }
  549. /* Allocate EPC outbound memory windows to vpci vntb device */
  550. ntb->vpci_mw_addr[i] = pci_epc_mem_alloc_addr(ntb->epf->epc,
  551. &ntb->vpci_mw_phy[i],
  552. size);
  553. if (!ntb->vpci_mw_addr[i]) {
  554. ret = -ENOMEM;
  555. dev_err(dev, "Failed to allocate source address\n");
  556. goto err_set_bar;
  557. }
  558. }
  559. return ret;
  560. err_set_bar:
  561. pci_epc_clear_bar(ntb->epf->epc,
  562. ntb->epf->func_no,
  563. ntb->epf->vfunc_no,
  564. &ntb->epf->bar[barno]);
  565. err_alloc_mem:
  566. epf_ntb_mw_bar_clear(ntb, i);
  567. return ret;
  568. }
  569. /**
  570. * epf_ntb_mw_bar_clear() - Clear Memory window BARs
  571. * @ntb: NTB device that facilitates communication between HOST and VHOST
  572. * @num_mws: the number of Memory window BARs that to be cleared
  573. */
  574. static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws)
  575. {
  576. enum pci_barno barno;
  577. int i;
  578. for (i = 0; i < num_mws; i++) {
  579. barno = ntb->epf_ntb_bar[BAR_MW0 + i];
  580. pci_epc_clear_bar(ntb->epf->epc,
  581. ntb->epf->func_no,
  582. ntb->epf->vfunc_no,
  583. &ntb->epf->bar[barno]);
  584. pci_epc_mem_free_addr(ntb->epf->epc,
  585. ntb->vpci_mw_phy[i],
  586. ntb->vpci_mw_addr[i],
  587. ntb->mws_size[i]);
  588. }
  589. }
  590. /**
  591. * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
  592. * @ntb: NTB device that facilitates communication between HOST and VHOST
  593. *
  594. * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
  595. */
  596. static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
  597. {
  598. pci_epc_remove_epf(ntb->epf->epc, ntb->epf, 0);
  599. pci_epc_put(ntb->epf->epc);
  600. }
  601. /**
  602. * epf_ntb_init_epc_bar() - Identify BARs to be used for each of the NTB
  603. * constructs (scratchpad region, doorbell, memorywindow)
  604. * @ntb: NTB device that facilitates communication between HOST and VHOST
  605. *
  606. * Returns: Zero for success, or an error code in case of failure
  607. */
  608. static int epf_ntb_init_epc_bar(struct epf_ntb *ntb)
  609. {
  610. const struct pci_epc_features *epc_features;
  611. enum pci_barno barno;
  612. enum epf_ntb_bar bar;
  613. struct device *dev;
  614. u32 num_mws;
  615. int i;
  616. barno = BAR_0;
  617. num_mws = ntb->num_mws;
  618. dev = &ntb->epf->dev;
  619. epc_features = pci_epc_get_features(ntb->epf->epc, ntb->epf->func_no, ntb->epf->vfunc_no);
  620. /* These are required BARs which are mandatory for NTB functionality */
  621. for (bar = BAR_CONFIG; bar <= BAR_MW0; bar++, barno++) {
  622. barno = pci_epc_get_next_free_bar(epc_features, barno);
  623. if (barno < 0) {
  624. dev_err(dev, "Fail to get NTB function BAR\n");
  625. return barno;
  626. }
  627. ntb->epf_ntb_bar[bar] = barno;
  628. }
  629. /* These are optional BARs which don't impact NTB functionality */
  630. for (bar = BAR_MW1, i = 1; i < num_mws; bar++, barno++, i++) {
  631. barno = pci_epc_get_next_free_bar(epc_features, barno);
  632. if (barno < 0) {
  633. ntb->num_mws = i;
  634. dev_dbg(dev, "BAR not available for > MW%d\n", i + 1);
  635. }
  636. ntb->epf_ntb_bar[bar] = barno;
  637. }
  638. return 0;
  639. }
  640. /**
  641. * epf_ntb_epc_init() - Initialize NTB interface
  642. * @ntb: NTB device that facilitates communication between HOST and VHOST
  643. *
  644. * Wrapper to initialize a particular EPC interface and start the workqueue
  645. * to check for commands from HOST. This function will write to the
  646. * EP controller HW for configuring it.
  647. *
  648. * Returns: Zero for success, or an error code in case of failure
  649. */
  650. static int epf_ntb_epc_init(struct epf_ntb *ntb)
  651. {
  652. u8 func_no, vfunc_no;
  653. struct pci_epc *epc;
  654. struct pci_epf *epf;
  655. struct device *dev;
  656. int ret;
  657. epf = ntb->epf;
  658. dev = &epf->dev;
  659. epc = epf->epc;
  660. func_no = ntb->epf->func_no;
  661. vfunc_no = ntb->epf->vfunc_no;
  662. ret = epf_ntb_config_sspad_bar_set(ntb);
  663. if (ret) {
  664. dev_err(dev, "Config/self SPAD BAR init failed");
  665. return ret;
  666. }
  667. ret = epf_ntb_configure_interrupt(ntb);
  668. if (ret) {
  669. dev_err(dev, "Interrupt configuration failed\n");
  670. goto err_config_interrupt;
  671. }
  672. ret = epf_ntb_db_bar_init(ntb);
  673. if (ret) {
  674. dev_err(dev, "DB BAR init failed\n");
  675. goto err_db_bar_init;
  676. }
  677. ret = epf_ntb_mw_bar_init(ntb);
  678. if (ret) {
  679. dev_err(dev, "MW BAR init failed\n");
  680. goto err_mw_bar_init;
  681. }
  682. if (vfunc_no <= 1) {
  683. ret = pci_epc_write_header(epc, func_no, vfunc_no, epf->header);
  684. if (ret) {
  685. dev_err(dev, "Configuration header write failed\n");
  686. goto err_write_header;
  687. }
  688. }
  689. INIT_DELAYED_WORK(&ntb->cmd_handler, epf_ntb_cmd_handler);
  690. queue_work(kpcintb_workqueue, &ntb->cmd_handler.work);
  691. return 0;
  692. err_write_header:
  693. epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
  694. err_mw_bar_init:
  695. epf_ntb_db_bar_clear(ntb);
  696. err_db_bar_init:
  697. err_config_interrupt:
  698. epf_ntb_config_sspad_bar_clear(ntb);
  699. return ret;
  700. }
  701. /**
  702. * epf_ntb_epc_cleanup() - Cleanup all NTB interfaces
  703. * @ntb: NTB device that facilitates communication between HOST and VHOST
  704. *
  705. * Wrapper to cleanup all NTB interfaces.
  706. */
  707. static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
  708. {
  709. epf_ntb_db_bar_clear(ntb);
  710. epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
  711. }
  712. #define EPF_NTB_R(_name) \
  713. static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
  714. char *page) \
  715. { \
  716. struct config_group *group = to_config_group(item); \
  717. struct epf_ntb *ntb = to_epf_ntb(group); \
  718. \
  719. return sprintf(page, "%d\n", ntb->_name); \
  720. }
  721. #define EPF_NTB_W(_name) \
  722. static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
  723. const char *page, size_t len) \
  724. { \
  725. struct config_group *group = to_config_group(item); \
  726. struct epf_ntb *ntb = to_epf_ntb(group); \
  727. u32 val; \
  728. int ret; \
  729. \
  730. ret = kstrtou32(page, 0, &val); \
  731. if (ret) \
  732. return ret; \
  733. \
  734. ntb->_name = val; \
  735. \
  736. return len; \
  737. }
  738. #define EPF_NTB_MW_R(_name) \
  739. static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
  740. char *page) \
  741. { \
  742. struct config_group *group = to_config_group(item); \
  743. struct epf_ntb *ntb = to_epf_ntb(group); \
  744. struct device *dev = &ntb->epf->dev; \
  745. int win_no; \
  746. \
  747. if (sscanf(#_name, "mw%d", &win_no) != 1) \
  748. return -EINVAL; \
  749. \
  750. if (win_no <= 0 || win_no > ntb->num_mws) { \
  751. dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
  752. return -EINVAL; \
  753. } \
  754. \
  755. return sprintf(page, "%lld\n", ntb->mws_size[win_no - 1]); \
  756. }
  757. #define EPF_NTB_MW_W(_name) \
  758. static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
  759. const char *page, size_t len) \
  760. { \
  761. struct config_group *group = to_config_group(item); \
  762. struct epf_ntb *ntb = to_epf_ntb(group); \
  763. struct device *dev = &ntb->epf->dev; \
  764. int win_no; \
  765. u64 val; \
  766. int ret; \
  767. \
  768. ret = kstrtou64(page, 0, &val); \
  769. if (ret) \
  770. return ret; \
  771. \
  772. if (sscanf(#_name, "mw%d", &win_no) != 1) \
  773. return -EINVAL; \
  774. \
  775. if (win_no <= 0 || win_no > ntb->num_mws) { \
  776. dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
  777. return -EINVAL; \
  778. } \
  779. \
  780. ntb->mws_size[win_no - 1] = val; \
  781. \
  782. return len; \
  783. }
  784. static ssize_t epf_ntb_num_mws_store(struct config_item *item,
  785. const char *page, size_t len)
  786. {
  787. struct config_group *group = to_config_group(item);
  788. struct epf_ntb *ntb = to_epf_ntb(group);
  789. u32 val;
  790. int ret;
  791. ret = kstrtou32(page, 0, &val);
  792. if (ret)
  793. return ret;
  794. if (val > MAX_MW)
  795. return -EINVAL;
  796. ntb->num_mws = val;
  797. return len;
  798. }
  799. EPF_NTB_R(spad_count)
  800. EPF_NTB_W(spad_count)
  801. EPF_NTB_R(db_count)
  802. EPF_NTB_W(db_count)
  803. EPF_NTB_R(num_mws)
  804. EPF_NTB_R(vbus_number)
  805. EPF_NTB_W(vbus_number)
  806. EPF_NTB_R(vntb_pid)
  807. EPF_NTB_W(vntb_pid)
  808. EPF_NTB_R(vntb_vid)
  809. EPF_NTB_W(vntb_vid)
  810. EPF_NTB_MW_R(mw1)
  811. EPF_NTB_MW_W(mw1)
  812. EPF_NTB_MW_R(mw2)
  813. EPF_NTB_MW_W(mw2)
  814. EPF_NTB_MW_R(mw3)
  815. EPF_NTB_MW_W(mw3)
  816. EPF_NTB_MW_R(mw4)
  817. EPF_NTB_MW_W(mw4)
  818. CONFIGFS_ATTR(epf_ntb_, spad_count);
  819. CONFIGFS_ATTR(epf_ntb_, db_count);
  820. CONFIGFS_ATTR(epf_ntb_, num_mws);
  821. CONFIGFS_ATTR(epf_ntb_, mw1);
  822. CONFIGFS_ATTR(epf_ntb_, mw2);
  823. CONFIGFS_ATTR(epf_ntb_, mw3);
  824. CONFIGFS_ATTR(epf_ntb_, mw4);
  825. CONFIGFS_ATTR(epf_ntb_, vbus_number);
  826. CONFIGFS_ATTR(epf_ntb_, vntb_pid);
  827. CONFIGFS_ATTR(epf_ntb_, vntb_vid);
  828. static struct configfs_attribute *epf_ntb_attrs[] = {
  829. &epf_ntb_attr_spad_count,
  830. &epf_ntb_attr_db_count,
  831. &epf_ntb_attr_num_mws,
  832. &epf_ntb_attr_mw1,
  833. &epf_ntb_attr_mw2,
  834. &epf_ntb_attr_mw3,
  835. &epf_ntb_attr_mw4,
  836. &epf_ntb_attr_vbus_number,
  837. &epf_ntb_attr_vntb_pid,
  838. &epf_ntb_attr_vntb_vid,
  839. NULL,
  840. };
  841. static const struct config_item_type ntb_group_type = {
  842. .ct_attrs = epf_ntb_attrs,
  843. .ct_owner = THIS_MODULE,
  844. };
  845. /**
  846. * epf_ntb_add_cfs() - Add configfs directory specific to NTB
  847. * @epf: NTB endpoint function device
  848. * @group: A pointer to the config_group structure referencing a group of
  849. * config_items of a specific type that belong to a specific sub-system.
  850. *
  851. * Add configfs directory specific to NTB. This directory will hold
  852. * NTB specific properties like db_count, spad_count, num_mws etc.,
  853. *
  854. * Returns: Pointer to config_group
  855. */
  856. static struct config_group *epf_ntb_add_cfs(struct pci_epf *epf,
  857. struct config_group *group)
  858. {
  859. struct epf_ntb *ntb = epf_get_drvdata(epf);
  860. struct config_group *ntb_group = &ntb->group;
  861. struct device *dev = &epf->dev;
  862. config_group_init_type_name(ntb_group, dev_name(dev), &ntb_group_type);
  863. return ntb_group;
  864. }
  865. /*==== virtual PCI bus driver, which only load virtual NTB PCI driver ====*/
  866. static u32 pci_space[] = {
  867. 0xffffffff, /*DeviceID, Vendor ID*/
  868. 0, /*Status, Command*/
  869. 0xffffffff, /*Class code, subclass, prog if, revision id*/
  870. 0x40, /*bist, header type, latency Timer, cache line size*/
  871. 0, /*BAR 0*/
  872. 0, /*BAR 1*/
  873. 0, /*BAR 2*/
  874. 0, /*BAR 3*/
  875. 0, /*BAR 4*/
  876. 0, /*BAR 5*/
  877. 0, /*Cardbus cis point*/
  878. 0, /*Subsystem ID Subystem vendor id*/
  879. 0, /*ROM Base Address*/
  880. 0, /*Reserved, Cap. Point*/
  881. 0, /*Reserved,*/
  882. 0, /*Max Lat, Min Gnt, interrupt pin, interrupt line*/
  883. };
  884. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val)
  885. {
  886. if (devfn == 0) {
  887. memcpy(val, ((u8 *)pci_space) + where, size);
  888. return PCIBIOS_SUCCESSFUL;
  889. }
  890. return PCIBIOS_DEVICE_NOT_FOUND;
  891. }
  892. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
  893. {
  894. return 0;
  895. }
  896. static struct pci_ops vpci_ops = {
  897. .read = pci_read,
  898. .write = pci_write,
  899. };
  900. static int vpci_scan_bus(void *sysdata)
  901. {
  902. struct pci_bus *vpci_bus;
  903. struct epf_ntb *ndev = sysdata;
  904. vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
  905. if (vpci_bus)
  906. pr_err("create pci bus\n");
  907. pci_bus_add_devices(vpci_bus);
  908. return 0;
  909. }
  910. /*==================== Virtual PCIe NTB driver ==========================*/
  911. static int vntb_epf_mw_count(struct ntb_dev *ntb, int pidx)
  912. {
  913. struct epf_ntb *ndev = ntb_ndev(ntb);
  914. return ndev->num_mws;
  915. }
  916. static int vntb_epf_spad_count(struct ntb_dev *ntb)
  917. {
  918. return ntb_ndev(ntb)->spad_count;
  919. }
  920. static int vntb_epf_peer_mw_count(struct ntb_dev *ntb)
  921. {
  922. return ntb_ndev(ntb)->num_mws;
  923. }
  924. static u64 vntb_epf_db_valid_mask(struct ntb_dev *ntb)
  925. {
  926. return BIT_ULL(ntb_ndev(ntb)->db_count) - 1;
  927. }
  928. static int vntb_epf_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  929. {
  930. return 0;
  931. }
  932. static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
  933. dma_addr_t addr, resource_size_t size)
  934. {
  935. struct epf_ntb *ntb = ntb_ndev(ndev);
  936. struct pci_epf_bar *epf_bar;
  937. enum pci_barno barno;
  938. int ret;
  939. struct device *dev;
  940. dev = &ntb->ntb.dev;
  941. barno = ntb->epf_ntb_bar[BAR_MW0 + idx];
  942. epf_bar = &ntb->epf->bar[barno];
  943. epf_bar->phys_addr = addr;
  944. epf_bar->barno = barno;
  945. epf_bar->size = size;
  946. ret = pci_epc_set_bar(ntb->epf->epc, 0, 0, epf_bar);
  947. if (ret) {
  948. dev_err(dev, "failure set mw trans\n");
  949. return ret;
  950. }
  951. return 0;
  952. }
  953. static int vntb_epf_mw_clear_trans(struct ntb_dev *ntb, int pidx, int idx)
  954. {
  955. return 0;
  956. }
  957. static int vntb_epf_peer_mw_get_addr(struct ntb_dev *ndev, int idx,
  958. phys_addr_t *base, resource_size_t *size)
  959. {
  960. struct epf_ntb *ntb = ntb_ndev(ndev);
  961. if (base)
  962. *base = ntb->vpci_mw_phy[idx];
  963. if (size)
  964. *size = ntb->mws_size[idx];
  965. return 0;
  966. }
  967. static int vntb_epf_link_enable(struct ntb_dev *ntb,
  968. enum ntb_speed max_speed,
  969. enum ntb_width max_width)
  970. {
  971. return 0;
  972. }
  973. static u32 vntb_epf_spad_read(struct ntb_dev *ndev, int idx)
  974. {
  975. struct epf_ntb *ntb = ntb_ndev(ndev);
  976. int off = ntb->reg->spad_offset, ct = ntb->reg->spad_count * 4;
  977. u32 val;
  978. void __iomem *base = ntb->reg;
  979. val = readl(base + off + ct + idx * 4);
  980. return val;
  981. }
  982. static int vntb_epf_spad_write(struct ntb_dev *ndev, int idx, u32 val)
  983. {
  984. struct epf_ntb *ntb = ntb_ndev(ndev);
  985. struct epf_ntb_ctrl *ctrl = ntb->reg;
  986. int off = ctrl->spad_offset, ct = ctrl->spad_count * 4;
  987. void __iomem *base = ntb->reg;
  988. writel(val, base + off + ct + idx * 4);
  989. return 0;
  990. }
  991. static u32 vntb_epf_peer_spad_read(struct ntb_dev *ndev, int pidx, int idx)
  992. {
  993. struct epf_ntb *ntb = ntb_ndev(ndev);
  994. struct epf_ntb_ctrl *ctrl = ntb->reg;
  995. int off = ctrl->spad_offset;
  996. void __iomem *base = ntb->reg;
  997. u32 val;
  998. val = readl(base + off + idx * 4);
  999. return val;
  1000. }
  1001. static int vntb_epf_peer_spad_write(struct ntb_dev *ndev, int pidx, int idx, u32 val)
  1002. {
  1003. struct epf_ntb *ntb = ntb_ndev(ndev);
  1004. struct epf_ntb_ctrl *ctrl = ntb->reg;
  1005. int off = ctrl->spad_offset;
  1006. void __iomem *base = ntb->reg;
  1007. writel(val, base + off + idx * 4);
  1008. return 0;
  1009. }
  1010. static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
  1011. {
  1012. u32 interrupt_num = ffs(db_bits) + 1;
  1013. struct epf_ntb *ntb = ntb_ndev(ndev);
  1014. u8 func_no, vfunc_no;
  1015. int ret;
  1016. func_no = ntb->epf->func_no;
  1017. vfunc_no = ntb->epf->vfunc_no;
  1018. ret = pci_epc_raise_irq(ntb->epf->epc,
  1019. func_no,
  1020. vfunc_no,
  1021. PCI_EPC_IRQ_MSI,
  1022. interrupt_num + 1);
  1023. if (ret)
  1024. dev_err(&ntb->ntb.dev, "Failed to raise IRQ\n");
  1025. return ret;
  1026. }
  1027. static u64 vntb_epf_db_read(struct ntb_dev *ndev)
  1028. {
  1029. struct epf_ntb *ntb = ntb_ndev(ndev);
  1030. return ntb->db;
  1031. }
  1032. static int vntb_epf_mw_get_align(struct ntb_dev *ndev, int pidx, int idx,
  1033. resource_size_t *addr_align,
  1034. resource_size_t *size_align,
  1035. resource_size_t *size_max)
  1036. {
  1037. struct epf_ntb *ntb = ntb_ndev(ndev);
  1038. if (addr_align)
  1039. *addr_align = SZ_4K;
  1040. if (size_align)
  1041. *size_align = 1;
  1042. if (size_max)
  1043. *size_max = ntb->mws_size[idx];
  1044. return 0;
  1045. }
  1046. static u64 vntb_epf_link_is_up(struct ntb_dev *ndev,
  1047. enum ntb_speed *speed,
  1048. enum ntb_width *width)
  1049. {
  1050. struct epf_ntb *ntb = ntb_ndev(ndev);
  1051. return ntb->reg->link_status;
  1052. }
  1053. static int vntb_epf_db_clear_mask(struct ntb_dev *ndev, u64 db_bits)
  1054. {
  1055. return 0;
  1056. }
  1057. static int vntb_epf_db_clear(struct ntb_dev *ndev, u64 db_bits)
  1058. {
  1059. struct epf_ntb *ntb = ntb_ndev(ndev);
  1060. ntb->db &= ~db_bits;
  1061. return 0;
  1062. }
  1063. static int vntb_epf_link_disable(struct ntb_dev *ntb)
  1064. {
  1065. return 0;
  1066. }
  1067. static const struct ntb_dev_ops vntb_epf_ops = {
  1068. .mw_count = vntb_epf_mw_count,
  1069. .spad_count = vntb_epf_spad_count,
  1070. .peer_mw_count = vntb_epf_peer_mw_count,
  1071. .db_valid_mask = vntb_epf_db_valid_mask,
  1072. .db_set_mask = vntb_epf_db_set_mask,
  1073. .mw_set_trans = vntb_epf_mw_set_trans,
  1074. .mw_clear_trans = vntb_epf_mw_clear_trans,
  1075. .peer_mw_get_addr = vntb_epf_peer_mw_get_addr,
  1076. .link_enable = vntb_epf_link_enable,
  1077. .spad_read = vntb_epf_spad_read,
  1078. .spad_write = vntb_epf_spad_write,
  1079. .peer_spad_read = vntb_epf_peer_spad_read,
  1080. .peer_spad_write = vntb_epf_peer_spad_write,
  1081. .peer_db_set = vntb_epf_peer_db_set,
  1082. .db_read = vntb_epf_db_read,
  1083. .mw_get_align = vntb_epf_mw_get_align,
  1084. .link_is_up = vntb_epf_link_is_up,
  1085. .db_clear_mask = vntb_epf_db_clear_mask,
  1086. .db_clear = vntb_epf_db_clear,
  1087. .link_disable = vntb_epf_link_disable,
  1088. };
  1089. static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1090. {
  1091. int ret;
  1092. struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
  1093. struct device *dev = &pdev->dev;
  1094. ndev->ntb.pdev = pdev;
  1095. ndev->ntb.topo = NTB_TOPO_NONE;
  1096. ndev->ntb.ops = &vntb_epf_ops;
  1097. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1098. if (ret) {
  1099. dev_err(dev, "Cannot set DMA mask\n");
  1100. return -EINVAL;
  1101. }
  1102. ret = ntb_register_device(&ndev->ntb);
  1103. if (ret) {
  1104. dev_err(dev, "Failed to register NTB device\n");
  1105. goto err_register_dev;
  1106. }
  1107. dev_dbg(dev, "PCI Virtual NTB driver loaded\n");
  1108. return 0;
  1109. err_register_dev:
  1110. return -EINVAL;
  1111. }
  1112. static struct pci_device_id pci_vntb_table[] = {
  1113. {
  1114. PCI_DEVICE(0xffff, 0xffff),
  1115. },
  1116. {},
  1117. };
  1118. static struct pci_driver vntb_pci_driver = {
  1119. .name = "pci-vntb",
  1120. .id_table = pci_vntb_table,
  1121. .probe = pci_vntb_probe,
  1122. };
  1123. /* ============ PCIe EPF Driver Bind ====================*/
  1124. /**
  1125. * epf_ntb_bind() - Initialize endpoint controller to provide NTB functionality
  1126. * @epf: NTB endpoint function device
  1127. *
  1128. * Initialize both the endpoint controllers associated with NTB function device.
  1129. * Invoked when a primary interface or secondary interface is bound to EPC
  1130. * device. This function will succeed only when EPC is bound to both the
  1131. * interfaces.
  1132. *
  1133. * Returns: Zero for success, or an error code in case of failure
  1134. */
  1135. static int epf_ntb_bind(struct pci_epf *epf)
  1136. {
  1137. struct epf_ntb *ntb = epf_get_drvdata(epf);
  1138. struct device *dev = &epf->dev;
  1139. int ret;
  1140. if (!epf->epc) {
  1141. dev_dbg(dev, "PRIMARY EPC interface not yet bound\n");
  1142. return 0;
  1143. }
  1144. ret = epf_ntb_init_epc_bar(ntb);
  1145. if (ret) {
  1146. dev_err(dev, "Failed to create NTB EPC\n");
  1147. goto err_bar_init;
  1148. }
  1149. ret = epf_ntb_config_spad_bar_alloc(ntb);
  1150. if (ret) {
  1151. dev_err(dev, "Failed to allocate BAR memory\n");
  1152. goto err_bar_alloc;
  1153. }
  1154. ret = epf_ntb_epc_init(ntb);
  1155. if (ret) {
  1156. dev_err(dev, "Failed to initialize EPC\n");
  1157. goto err_bar_alloc;
  1158. }
  1159. epf_set_drvdata(epf, ntb);
  1160. pci_space[0] = (ntb->vntb_pid << 16) | ntb->vntb_vid;
  1161. pci_vntb_table[0].vendor = ntb->vntb_vid;
  1162. pci_vntb_table[0].device = ntb->vntb_pid;
  1163. ret = pci_register_driver(&vntb_pci_driver);
  1164. if (ret) {
  1165. dev_err(dev, "failure register vntb pci driver\n");
  1166. goto err_bar_alloc;
  1167. }
  1168. vpci_scan_bus(ntb);
  1169. return 0;
  1170. err_bar_alloc:
  1171. epf_ntb_config_spad_bar_free(ntb);
  1172. err_bar_init:
  1173. epf_ntb_epc_destroy(ntb);
  1174. return ret;
  1175. }
  1176. /**
  1177. * epf_ntb_unbind() - Cleanup the initialization from epf_ntb_bind()
  1178. * @epf: NTB endpoint function device
  1179. *
  1180. * Cleanup the initialization from epf_ntb_bind()
  1181. */
  1182. static void epf_ntb_unbind(struct pci_epf *epf)
  1183. {
  1184. struct epf_ntb *ntb = epf_get_drvdata(epf);
  1185. epf_ntb_epc_cleanup(ntb);
  1186. epf_ntb_config_spad_bar_free(ntb);
  1187. epf_ntb_epc_destroy(ntb);
  1188. pci_unregister_driver(&vntb_pci_driver);
  1189. }
  1190. // EPF driver probe
  1191. static struct pci_epf_ops epf_ntb_ops = {
  1192. .bind = epf_ntb_bind,
  1193. .unbind = epf_ntb_unbind,
  1194. .add_cfs = epf_ntb_add_cfs,
  1195. };
  1196. /**
  1197. * epf_ntb_probe() - Probe NTB function driver
  1198. * @epf: NTB endpoint function device
  1199. *
  1200. * Probe NTB function driver when endpoint function bus detects a NTB
  1201. * endpoint function.
  1202. *
  1203. * Returns: Zero for success, or an error code in case of failure
  1204. */
  1205. static int epf_ntb_probe(struct pci_epf *epf)
  1206. {
  1207. struct epf_ntb *ntb;
  1208. struct device *dev;
  1209. dev = &epf->dev;
  1210. ntb = devm_kzalloc(dev, sizeof(*ntb), GFP_KERNEL);
  1211. if (!ntb)
  1212. return -ENOMEM;
  1213. epf->header = &epf_ntb_header;
  1214. ntb->epf = epf;
  1215. ntb->vbus_number = 0xff;
  1216. epf_set_drvdata(epf, ntb);
  1217. dev_info(dev, "pci-ep epf driver loaded\n");
  1218. return 0;
  1219. }
  1220. static const struct pci_epf_device_id epf_ntb_ids[] = {
  1221. {
  1222. .name = "pci_epf_vntb",
  1223. },
  1224. {},
  1225. };
  1226. static struct pci_epf_driver epf_ntb_driver = {
  1227. .driver.name = "pci_epf_vntb",
  1228. .probe = epf_ntb_probe,
  1229. .id_table = epf_ntb_ids,
  1230. .ops = &epf_ntb_ops,
  1231. .owner = THIS_MODULE,
  1232. };
  1233. static int __init epf_ntb_init(void)
  1234. {
  1235. int ret;
  1236. kpcintb_workqueue = alloc_workqueue("kpcintb", WQ_MEM_RECLAIM |
  1237. WQ_HIGHPRI, 0);
  1238. ret = pci_epf_register_driver(&epf_ntb_driver);
  1239. if (ret) {
  1240. destroy_workqueue(kpcintb_workqueue);
  1241. pr_err("Failed to register pci epf ntb driver --> %d\n", ret);
  1242. return ret;
  1243. }
  1244. return 0;
  1245. }
  1246. module_init(epf_ntb_init);
  1247. static void __exit epf_ntb_exit(void)
  1248. {
  1249. pci_epf_unregister_driver(&epf_ntb_driver);
  1250. destroy_workqueue(kpcintb_workqueue);
  1251. }
  1252. module_exit(epf_ntb_exit);
  1253. MODULE_DESCRIPTION("PCI EPF NTB DRIVER");
  1254. MODULE_AUTHOR("Frank Li <[email protected]>");
  1255. MODULE_LICENSE("GPL v2");