pci-epf-ntb.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Endpoint Function Driver to implement Non-Transparent Bridge functionality
  4. *
  5. * Copyright (C) 2020 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <[email protected]>
  7. */
  8. /*
  9. * The PCI NTB function driver configures the SoC with multiple PCIe Endpoint
  10. * (EP) controller instances (see diagram below) in such a way that
  11. * transactions from one EP controller are routed to the other EP controller.
  12. * Once PCI NTB function driver configures the SoC with multiple EP instances,
  13. * HOST1 and HOST2 can communicate with each other using SoC as a bridge.
  14. *
  15. * +-------------+ +-------------+
  16. * | | | |
  17. * | HOST1 | | HOST2 |
  18. * | | | |
  19. * +------^------+ +------^------+
  20. * | |
  21. * | |
  22. * +---------|-------------------------------------------------|---------+
  23. * | +------v------+ +------v------+ |
  24. * | | | | | |
  25. * | | EP | | EP | |
  26. * | | CONTROLLER1 | | CONTROLLER2 | |
  27. * | | <-----------------------------------> | |
  28. * | | | | | |
  29. * | | | | | |
  30. * | | | SoC With Multiple EP Instances | | |
  31. * | | | (Configured using NTB Function) | | |
  32. * | +-------------+ +-------------+ |
  33. * +---------------------------------------------------------------------+
  34. */
  35. #include <linux/delay.h>
  36. #include <linux/io.h>
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/pci-epc.h>
  40. #include <linux/pci-epf.h>
  41. static struct workqueue_struct *kpcintb_workqueue;
  42. #define COMMAND_CONFIGURE_DOORBELL 1
  43. #define COMMAND_TEARDOWN_DOORBELL 2
  44. #define COMMAND_CONFIGURE_MW 3
  45. #define COMMAND_TEARDOWN_MW 4
  46. #define COMMAND_LINK_UP 5
  47. #define COMMAND_LINK_DOWN 6
  48. #define COMMAND_STATUS_OK 1
  49. #define COMMAND_STATUS_ERROR 2
  50. #define LINK_STATUS_UP BIT(0)
  51. #define SPAD_COUNT 64
  52. #define DB_COUNT 4
  53. #define NTB_MW_OFFSET 2
  54. #define DB_COUNT_MASK GENMASK(15, 0)
  55. #define MSIX_ENABLE BIT(16)
  56. #define MAX_DB_COUNT 32
  57. #define MAX_MW 4
  58. enum epf_ntb_bar {
  59. BAR_CONFIG,
  60. BAR_PEER_SPAD,
  61. BAR_DB_MW1,
  62. BAR_MW2,
  63. BAR_MW3,
  64. BAR_MW4,
  65. };
  66. struct epf_ntb {
  67. u32 num_mws;
  68. u32 db_count;
  69. u32 spad_count;
  70. struct pci_epf *epf;
  71. u64 mws_size[MAX_MW];
  72. struct config_group group;
  73. struct epf_ntb_epc *epc[2];
  74. };
  75. #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
  76. struct epf_ntb_epc {
  77. u8 func_no;
  78. u8 vfunc_no;
  79. bool linkup;
  80. bool is_msix;
  81. int msix_bar;
  82. u32 spad_size;
  83. struct pci_epc *epc;
  84. struct epf_ntb *epf_ntb;
  85. void __iomem *mw_addr[6];
  86. size_t msix_table_offset;
  87. struct epf_ntb_ctrl *reg;
  88. struct pci_epf_bar *epf_bar;
  89. enum pci_barno epf_ntb_bar[6];
  90. struct delayed_work cmd_handler;
  91. enum pci_epc_interface_type type;
  92. const struct pci_epc_features *epc_features;
  93. };
  94. struct epf_ntb_ctrl {
  95. u32 command;
  96. u32 argument;
  97. u16 command_status;
  98. u16 link_status;
  99. u32 topology;
  100. u64 addr;
  101. u64 size;
  102. u32 num_mws;
  103. u32 mw1_offset;
  104. u32 spad_offset;
  105. u32 spad_count;
  106. u32 db_entry_size;
  107. u32 db_data[MAX_DB_COUNT];
  108. u32 db_offset[MAX_DB_COUNT];
  109. } __packed;
  110. static struct pci_epf_header epf_ntb_header = {
  111. .vendorid = PCI_ANY_ID,
  112. .deviceid = PCI_ANY_ID,
  113. .baseclass_code = PCI_BASE_CLASS_MEMORY,
  114. .interrupt_pin = PCI_INTERRUPT_INTA,
  115. };
  116. /**
  117. * epf_ntb_link_up() - Raise link_up interrupt to both the hosts
  118. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  119. * @link_up: true or false indicating Link is UP or Down
  120. *
  121. * Once NTB function in HOST1 and the NTB function in HOST2 invoke
  122. * ntb_link_enable(), this NTB function driver will trigger a link event to
  123. * the NTB client in both the hosts.
  124. */
  125. static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
  126. {
  127. enum pci_epc_interface_type type;
  128. enum pci_epc_irq_type irq_type;
  129. struct epf_ntb_epc *ntb_epc;
  130. struct epf_ntb_ctrl *ctrl;
  131. struct pci_epc *epc;
  132. u8 func_no, vfunc_no;
  133. bool is_msix;
  134. int ret;
  135. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
  136. ntb_epc = ntb->epc[type];
  137. epc = ntb_epc->epc;
  138. func_no = ntb_epc->func_no;
  139. vfunc_no = ntb_epc->vfunc_no;
  140. is_msix = ntb_epc->is_msix;
  141. ctrl = ntb_epc->reg;
  142. if (link_up)
  143. ctrl->link_status |= LINK_STATUS_UP;
  144. else
  145. ctrl->link_status &= ~LINK_STATUS_UP;
  146. irq_type = is_msix ? PCI_EPC_IRQ_MSIX : PCI_EPC_IRQ_MSI;
  147. ret = pci_epc_raise_irq(epc, func_no, vfunc_no, irq_type, 1);
  148. if (ret) {
  149. dev_err(&epc->dev,
  150. "%s intf: Failed to raise Link Up IRQ\n",
  151. pci_epc_interface_string(type));
  152. return ret;
  153. }
  154. }
  155. return 0;
  156. }
  157. /**
  158. * epf_ntb_configure_mw() - Configure the Outbound Address Space for one host
  159. * to access the memory window of other host
  160. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  161. * @type: PRIMARY interface or SECONDARY interface
  162. * @mw: Index of the memory window (either 0, 1, 2 or 3)
  163. *
  164. * +-----------------+ +---->+----------------+-----------+-----------------+
  165. * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
  166. * +-----------------+ | +----------------+ +-----------------+
  167. * | BAR1 | | | Doorbell 2 +---------+ | |
  168. * +-----------------+----+ +----------------+ | | |
  169. * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
  170. * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
  171. * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
  172. * +-----------------+ | |----------------+ | | | |
  173. * | BAR4 | | | | | | +-----------------+
  174. * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
  175. * | BAR5 | | | | | | +-----------------+
  176. * +-----------------+ +---->-----------------+ | | | |
  177. * EP CONTROLLER 1 | | | | +-----------------+
  178. * | | | +---->+ MSI|X ADDRESS 4 |
  179. * +----------------+ | +-----------------+
  180. * (A) EP CONTROLLER 2 | | |
  181. * (OB SPACE) | | |
  182. * +-------> MW1 |
  183. * | |
  184. * | |
  185. * (B) +-----------------+
  186. * | |
  187. * | |
  188. * | |
  189. * | |
  190. * | |
  191. * +-----------------+
  192. * PCI Address Space
  193. * (Managed by HOST2)
  194. *
  195. * This function performs stage (B) in the above diagram (see MW1) i.e., map OB
  196. * address space of memory window to PCI address space.
  197. *
  198. * This operation requires 3 parameters
  199. * 1) Address in the outbound address space
  200. * 2) Address in the PCI Address space
  201. * 3) Size of the address region to be mapped
  202. *
  203. * The address in the outbound address space (for MW1, MW2, MW3 and MW4) is
  204. * stored in epf_bar corresponding to BAR_DB_MW1 for MW1 and BAR_MW2, BAR_MW3
  205. * BAR_MW4 for rest of the BARs of epf_ntb_epc that is connected to HOST1. This
  206. * is populated in epf_ntb_alloc_peer_mem() in this driver.
  207. *
  208. * The address and size of the PCI address region that has to be mapped would
  209. * be provided by HOST2 in ctrl->addr and ctrl->size of epf_ntb_epc that is
  210. * connected to HOST2.
  211. *
  212. * Please note Memory window1 (MW1) and Doorbell registers together will be
  213. * mapped to a single BAR (BAR2) above for 32-bit BARs. The exact BAR that's
  214. * used for Memory window (MW) can be obtained from epf_ntb_bar[BAR_DB_MW1],
  215. * epf_ntb_bar[BAR_MW2], epf_ntb_bar[BAR_MW2], epf_ntb_bar[BAR_MW2].
  216. */
  217. static int epf_ntb_configure_mw(struct epf_ntb *ntb,
  218. enum pci_epc_interface_type type, u32 mw)
  219. {
  220. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  221. struct pci_epf_bar *peer_epf_bar;
  222. enum pci_barno peer_barno;
  223. struct epf_ntb_ctrl *ctrl;
  224. phys_addr_t phys_addr;
  225. u8 func_no, vfunc_no;
  226. struct pci_epc *epc;
  227. u64 addr, size;
  228. int ret = 0;
  229. ntb_epc = ntb->epc[type];
  230. epc = ntb_epc->epc;
  231. peer_ntb_epc = ntb->epc[!type];
  232. peer_barno = peer_ntb_epc->epf_ntb_bar[mw + NTB_MW_OFFSET];
  233. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  234. phys_addr = peer_epf_bar->phys_addr;
  235. ctrl = ntb_epc->reg;
  236. addr = ctrl->addr;
  237. size = ctrl->size;
  238. if (mw + NTB_MW_OFFSET == BAR_DB_MW1)
  239. phys_addr += ctrl->mw1_offset;
  240. if (size > ntb->mws_size[mw]) {
  241. dev_err(&epc->dev,
  242. "%s intf: MW: %d Req Sz:%llxx > Supported Sz:%llx\n",
  243. pci_epc_interface_string(type), mw, size,
  244. ntb->mws_size[mw]);
  245. ret = -EINVAL;
  246. goto err_invalid_size;
  247. }
  248. func_no = ntb_epc->func_no;
  249. vfunc_no = ntb_epc->vfunc_no;
  250. ret = pci_epc_map_addr(epc, func_no, vfunc_no, phys_addr, addr, size);
  251. if (ret)
  252. dev_err(&epc->dev,
  253. "%s intf: Failed to map memory window %d address\n",
  254. pci_epc_interface_string(type), mw);
  255. err_invalid_size:
  256. return ret;
  257. }
  258. /**
  259. * epf_ntb_teardown_mw() - Teardown the configured OB ATU
  260. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  261. * @type: PRIMARY interface or SECONDARY interface
  262. * @mw: Index of the memory window (either 0, 1, 2 or 3)
  263. *
  264. * Teardown the configured OB ATU configured in epf_ntb_configure_mw() using
  265. * pci_epc_unmap_addr()
  266. */
  267. static void epf_ntb_teardown_mw(struct epf_ntb *ntb,
  268. enum pci_epc_interface_type type, u32 mw)
  269. {
  270. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  271. struct pci_epf_bar *peer_epf_bar;
  272. enum pci_barno peer_barno;
  273. struct epf_ntb_ctrl *ctrl;
  274. phys_addr_t phys_addr;
  275. u8 func_no, vfunc_no;
  276. struct pci_epc *epc;
  277. ntb_epc = ntb->epc[type];
  278. epc = ntb_epc->epc;
  279. peer_ntb_epc = ntb->epc[!type];
  280. peer_barno = peer_ntb_epc->epf_ntb_bar[mw + NTB_MW_OFFSET];
  281. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  282. phys_addr = peer_epf_bar->phys_addr;
  283. ctrl = ntb_epc->reg;
  284. if (mw + NTB_MW_OFFSET == BAR_DB_MW1)
  285. phys_addr += ctrl->mw1_offset;
  286. func_no = ntb_epc->func_no;
  287. vfunc_no = ntb_epc->vfunc_no;
  288. pci_epc_unmap_addr(epc, func_no, vfunc_no, phys_addr);
  289. }
  290. /**
  291. * epf_ntb_configure_msi() - Map OB address space to MSI address
  292. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  293. * @type: PRIMARY interface or SECONDARY interface
  294. * @db_count: Number of doorbell interrupts to map
  295. *
  296. *+-----------------+ +----->+----------------+-----------+-----------------+
  297. *| BAR0 | | | Doorbell 1 +---+-------> MSI ADDRESS |
  298. *+-----------------+ | +----------------+ | +-----------------+
  299. *| BAR1 | | | Doorbell 2 +---+ | |
  300. *+-----------------+----+ +----------------+ | | |
  301. *| BAR2 | | Doorbell 3 +---+ | |
  302. *+-----------------+----+ +----------------+ | | |
  303. *| BAR3 | | | Doorbell 4 +---+ | |
  304. *+-----------------+ | |----------------+ | |
  305. *| BAR4 | | | | | |
  306. *+-----------------+ | | MW1 | | |
  307. *| BAR5 | | | | | |
  308. *+-----------------+ +----->-----------------+ | |
  309. * EP CONTROLLER 1 | | | |
  310. * | | | |
  311. * +----------------+ +-----------------+
  312. * (A) EP CONTROLLER 2 | |
  313. * (OB SPACE) | |
  314. * | MW1 |
  315. * | |
  316. * | |
  317. * (B) +-----------------+
  318. * | |
  319. * | |
  320. * | |
  321. * | |
  322. * | |
  323. * +-----------------+
  324. * PCI Address Space
  325. * (Managed by HOST2)
  326. *
  327. *
  328. * This function performs stage (B) in the above diagram (see Doorbell 1,
  329. * Doorbell 2, Doorbell 3, Doorbell 4) i.e map OB address space corresponding to
  330. * doorbell to MSI address in PCI address space.
  331. *
  332. * This operation requires 3 parameters
  333. * 1) Address reserved for doorbell in the outbound address space
  334. * 2) MSI-X address in the PCIe Address space
  335. * 3) Number of MSI-X interrupts that has to be configured
  336. *
  337. * The address in the outbound address space (for the Doorbell) is stored in
  338. * epf_bar corresponding to BAR_DB_MW1 of epf_ntb_epc that is connected to
  339. * HOST1. This is populated in epf_ntb_alloc_peer_mem() in this driver along
  340. * with address for MW1.
  341. *
  342. * pci_epc_map_msi_irq() takes the MSI address from MSI capability register
  343. * and maps the OB address (obtained in epf_ntb_alloc_peer_mem()) to the MSI
  344. * address.
  345. *
  346. * epf_ntb_configure_msi() also stores the MSI data to raise each interrupt
  347. * in db_data of the peer's control region. This helps the peer to raise
  348. * doorbell of the other host by writing db_data to the BAR corresponding to
  349. * BAR_DB_MW1.
  350. */
  351. static int epf_ntb_configure_msi(struct epf_ntb *ntb,
  352. enum pci_epc_interface_type type, u16 db_count)
  353. {
  354. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  355. u32 db_entry_size, db_data, db_offset;
  356. struct pci_epf_bar *peer_epf_bar;
  357. struct epf_ntb_ctrl *peer_ctrl;
  358. enum pci_barno peer_barno;
  359. phys_addr_t phys_addr;
  360. u8 func_no, vfunc_no;
  361. struct pci_epc *epc;
  362. int ret, i;
  363. ntb_epc = ntb->epc[type];
  364. epc = ntb_epc->epc;
  365. peer_ntb_epc = ntb->epc[!type];
  366. peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
  367. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  368. peer_ctrl = peer_ntb_epc->reg;
  369. db_entry_size = peer_ctrl->db_entry_size;
  370. phys_addr = peer_epf_bar->phys_addr;
  371. func_no = ntb_epc->func_no;
  372. vfunc_no = ntb_epc->vfunc_no;
  373. ret = pci_epc_map_msi_irq(epc, func_no, vfunc_no, phys_addr, db_count,
  374. db_entry_size, &db_data, &db_offset);
  375. if (ret) {
  376. dev_err(&epc->dev, "%s intf: Failed to map MSI IRQ\n",
  377. pci_epc_interface_string(type));
  378. return ret;
  379. }
  380. for (i = 0; i < db_count; i++) {
  381. peer_ctrl->db_data[i] = db_data | i;
  382. peer_ctrl->db_offset[i] = db_offset;
  383. }
  384. return 0;
  385. }
  386. /**
  387. * epf_ntb_configure_msix() - Map OB address space to MSI-X address
  388. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  389. * @type: PRIMARY interface or SECONDARY interface
  390. * @db_count: Number of doorbell interrupts to map
  391. *
  392. *+-----------------+ +----->+----------------+-----------+-----------------+
  393. *| BAR0 | | | Doorbell 1 +-----------> MSI-X ADDRESS 1 |
  394. *+-----------------+ | +----------------+ +-----------------+
  395. *| BAR1 | | | Doorbell 2 +---------+ | |
  396. *+-----------------+----+ +----------------+ | | |
  397. *| BAR2 | | Doorbell 3 +-------+ | +-----------------+
  398. *+-----------------+----+ +----------------+ | +-> MSI-X ADDRESS 2 |
  399. *| BAR3 | | | Doorbell 4 +-----+ | +-----------------+
  400. *+-----------------+ | |----------------+ | | | |
  401. *| BAR4 | | | | | | +-----------------+
  402. *+-----------------+ | | MW1 + | +-->+ MSI-X ADDRESS 3||
  403. *| BAR5 | | | | | +-----------------+
  404. *+-----------------+ +----->-----------------+ | | |
  405. * EP CONTROLLER 1 | | | +-----------------+
  406. * | | +---->+ MSI-X ADDRESS 4 |
  407. * +----------------+ +-----------------+
  408. * (A) EP CONTROLLER 2 | |
  409. * (OB SPACE) | |
  410. * | MW1 |
  411. * | |
  412. * | |
  413. * (B) +-----------------+
  414. * | |
  415. * | |
  416. * | |
  417. * | |
  418. * | |
  419. * +-----------------+
  420. * PCI Address Space
  421. * (Managed by HOST2)
  422. *
  423. * This function performs stage (B) in the above diagram (see Doorbell 1,
  424. * Doorbell 2, Doorbell 3, Doorbell 4) i.e map OB address space corresponding to
  425. * doorbell to MSI-X address in PCI address space.
  426. *
  427. * This operation requires 3 parameters
  428. * 1) Address reserved for doorbell in the outbound address space
  429. * 2) MSI-X address in the PCIe Address space
  430. * 3) Number of MSI-X interrupts that has to be configured
  431. *
  432. * The address in the outbound address space (for the Doorbell) is stored in
  433. * epf_bar corresponding to BAR_DB_MW1 of epf_ntb_epc that is connected to
  434. * HOST1. This is populated in epf_ntb_alloc_peer_mem() in this driver along
  435. * with address for MW1.
  436. *
  437. * The MSI-X address is in the MSI-X table of EP CONTROLLER 2 and
  438. * the count of doorbell is in ctrl->argument of epf_ntb_epc that is connected
  439. * to HOST2. MSI-X table is stored memory mapped to ntb_epc->msix_bar and the
  440. * offset is in ntb_epc->msix_table_offset. From this epf_ntb_configure_msix()
  441. * gets the MSI-X address and data.
  442. *
  443. * epf_ntb_configure_msix() also stores the MSI-X data to raise each interrupt
  444. * in db_data of the peer's control region. This helps the peer to raise
  445. * doorbell of the other host by writing db_data to the BAR corresponding to
  446. * BAR_DB_MW1.
  447. */
  448. static int epf_ntb_configure_msix(struct epf_ntb *ntb,
  449. enum pci_epc_interface_type type,
  450. u16 db_count)
  451. {
  452. const struct pci_epc_features *epc_features;
  453. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  454. struct pci_epf_bar *peer_epf_bar, *epf_bar;
  455. struct pci_epf_msix_tbl *msix_tbl;
  456. struct epf_ntb_ctrl *peer_ctrl;
  457. u32 db_entry_size, msg_data;
  458. enum pci_barno peer_barno;
  459. phys_addr_t phys_addr;
  460. u8 func_no, vfunc_no;
  461. struct pci_epc *epc;
  462. size_t align;
  463. u64 msg_addr;
  464. int ret, i;
  465. ntb_epc = ntb->epc[type];
  466. epc = ntb_epc->epc;
  467. epf_bar = &ntb_epc->epf_bar[ntb_epc->msix_bar];
  468. msix_tbl = epf_bar->addr + ntb_epc->msix_table_offset;
  469. peer_ntb_epc = ntb->epc[!type];
  470. peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
  471. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  472. phys_addr = peer_epf_bar->phys_addr;
  473. peer_ctrl = peer_ntb_epc->reg;
  474. epc_features = ntb_epc->epc_features;
  475. align = epc_features->align;
  476. func_no = ntb_epc->func_no;
  477. vfunc_no = ntb_epc->vfunc_no;
  478. db_entry_size = peer_ctrl->db_entry_size;
  479. for (i = 0; i < db_count; i++) {
  480. msg_addr = ALIGN_DOWN(msix_tbl[i].msg_addr, align);
  481. msg_data = msix_tbl[i].msg_data;
  482. ret = pci_epc_map_addr(epc, func_no, vfunc_no, phys_addr, msg_addr,
  483. db_entry_size);
  484. if (ret) {
  485. dev_err(&epc->dev,
  486. "%s intf: Failed to configure MSI-X IRQ\n",
  487. pci_epc_interface_string(type));
  488. return ret;
  489. }
  490. phys_addr = phys_addr + db_entry_size;
  491. peer_ctrl->db_data[i] = msg_data;
  492. peer_ctrl->db_offset[i] = msix_tbl[i].msg_addr & (align - 1);
  493. }
  494. ntb_epc->is_msix = true;
  495. return 0;
  496. }
  497. /**
  498. * epf_ntb_configure_db() - Configure the Outbound Address Space for one host
  499. * to ring the doorbell of other host
  500. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  501. * @type: PRIMARY interface or SECONDARY interface
  502. * @db_count: Count of the number of doorbells that has to be configured
  503. * @msix: Indicates whether MSI-X or MSI should be used
  504. *
  505. * Invokes epf_ntb_configure_msix() or epf_ntb_configure_msi() required for
  506. * one HOST to ring the doorbell of other HOST.
  507. */
  508. static int epf_ntb_configure_db(struct epf_ntb *ntb,
  509. enum pci_epc_interface_type type,
  510. u16 db_count, bool msix)
  511. {
  512. struct epf_ntb_epc *ntb_epc;
  513. struct pci_epc *epc;
  514. int ret;
  515. if (db_count > MAX_DB_COUNT)
  516. return -EINVAL;
  517. ntb_epc = ntb->epc[type];
  518. epc = ntb_epc->epc;
  519. if (msix)
  520. ret = epf_ntb_configure_msix(ntb, type, db_count);
  521. else
  522. ret = epf_ntb_configure_msi(ntb, type, db_count);
  523. if (ret)
  524. dev_err(&epc->dev, "%s intf: Failed to configure DB\n",
  525. pci_epc_interface_string(type));
  526. return ret;
  527. }
  528. /**
  529. * epf_ntb_teardown_db() - Unmap address in OB address space to MSI/MSI-X
  530. * address
  531. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  532. * @type: PRIMARY interface or SECONDARY interface
  533. *
  534. * Invoke pci_epc_unmap_addr() to unmap OB address to MSI/MSI-X address.
  535. */
  536. static void
  537. epf_ntb_teardown_db(struct epf_ntb *ntb, enum pci_epc_interface_type type)
  538. {
  539. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  540. struct pci_epf_bar *peer_epf_bar;
  541. enum pci_barno peer_barno;
  542. phys_addr_t phys_addr;
  543. u8 func_no, vfunc_no;
  544. struct pci_epc *epc;
  545. ntb_epc = ntb->epc[type];
  546. epc = ntb_epc->epc;
  547. peer_ntb_epc = ntb->epc[!type];
  548. peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_DB_MW1];
  549. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  550. phys_addr = peer_epf_bar->phys_addr;
  551. func_no = ntb_epc->func_no;
  552. vfunc_no = ntb_epc->vfunc_no;
  553. pci_epc_unmap_addr(epc, func_no, vfunc_no, phys_addr);
  554. }
  555. /**
  556. * epf_ntb_cmd_handler() - Handle commands provided by the NTB Host
  557. * @work: work_struct for the two epf_ntb_epc (PRIMARY and SECONDARY)
  558. *
  559. * Workqueue function that gets invoked for the two epf_ntb_epc
  560. * periodically (once every 5ms) to see if it has received any commands
  561. * from NTB host. The host can send commands to configure doorbell or
  562. * configure memory window or to update link status.
  563. */
  564. static void epf_ntb_cmd_handler(struct work_struct *work)
  565. {
  566. enum pci_epc_interface_type type;
  567. struct epf_ntb_epc *ntb_epc;
  568. struct epf_ntb_ctrl *ctrl;
  569. u32 command, argument;
  570. struct epf_ntb *ntb;
  571. struct device *dev;
  572. u16 db_count;
  573. bool is_msix;
  574. int ret;
  575. ntb_epc = container_of(work, struct epf_ntb_epc, cmd_handler.work);
  576. ctrl = ntb_epc->reg;
  577. command = ctrl->command;
  578. if (!command)
  579. goto reset_handler;
  580. argument = ctrl->argument;
  581. ctrl->command = 0;
  582. ctrl->argument = 0;
  583. ctrl = ntb_epc->reg;
  584. type = ntb_epc->type;
  585. ntb = ntb_epc->epf_ntb;
  586. dev = &ntb->epf->dev;
  587. switch (command) {
  588. case COMMAND_CONFIGURE_DOORBELL:
  589. db_count = argument & DB_COUNT_MASK;
  590. is_msix = argument & MSIX_ENABLE;
  591. ret = epf_ntb_configure_db(ntb, type, db_count, is_msix);
  592. if (ret < 0)
  593. ctrl->command_status = COMMAND_STATUS_ERROR;
  594. else
  595. ctrl->command_status = COMMAND_STATUS_OK;
  596. break;
  597. case COMMAND_TEARDOWN_DOORBELL:
  598. epf_ntb_teardown_db(ntb, type);
  599. ctrl->command_status = COMMAND_STATUS_OK;
  600. break;
  601. case COMMAND_CONFIGURE_MW:
  602. ret = epf_ntb_configure_mw(ntb, type, argument);
  603. if (ret < 0)
  604. ctrl->command_status = COMMAND_STATUS_ERROR;
  605. else
  606. ctrl->command_status = COMMAND_STATUS_OK;
  607. break;
  608. case COMMAND_TEARDOWN_MW:
  609. epf_ntb_teardown_mw(ntb, type, argument);
  610. ctrl->command_status = COMMAND_STATUS_OK;
  611. break;
  612. case COMMAND_LINK_UP:
  613. ntb_epc->linkup = true;
  614. if (ntb->epc[PRIMARY_INTERFACE]->linkup &&
  615. ntb->epc[SECONDARY_INTERFACE]->linkup) {
  616. ret = epf_ntb_link_up(ntb, true);
  617. if (ret < 0)
  618. ctrl->command_status = COMMAND_STATUS_ERROR;
  619. else
  620. ctrl->command_status = COMMAND_STATUS_OK;
  621. goto reset_handler;
  622. }
  623. ctrl->command_status = COMMAND_STATUS_OK;
  624. break;
  625. case COMMAND_LINK_DOWN:
  626. ntb_epc->linkup = false;
  627. ret = epf_ntb_link_up(ntb, false);
  628. if (ret < 0)
  629. ctrl->command_status = COMMAND_STATUS_ERROR;
  630. else
  631. ctrl->command_status = COMMAND_STATUS_OK;
  632. break;
  633. default:
  634. dev_err(dev, "%s intf UNKNOWN command: %d\n",
  635. pci_epc_interface_string(type), command);
  636. break;
  637. }
  638. reset_handler:
  639. queue_delayed_work(kpcintb_workqueue, &ntb_epc->cmd_handler,
  640. msecs_to_jiffies(5));
  641. }
  642. /**
  643. * epf_ntb_peer_spad_bar_clear() - Clear Peer Scratchpad BAR
  644. * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
  645. * address.
  646. *
  647. *+-----------------+------->+------------------+ +-----------------+
  648. *| BAR0 | | CONFIG REGION | | BAR0 |
  649. *+-----------------+----+ +------------------+<-------+-----------------+
  650. *| BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  651. *+-----------------+ +-->+------------------+<-------+-----------------+
  652. *| BAR2 | Local Memory | BAR2 |
  653. *+-----------------+ +-----------------+
  654. *| BAR3 | | BAR3 |
  655. *+-----------------+ +-----------------+
  656. *| BAR4 | | BAR4 |
  657. *+-----------------+ +-----------------+
  658. *| BAR5 | | BAR5 |
  659. *+-----------------+ +-----------------+
  660. * EP CONTROLLER 1 EP CONTROLLER 2
  661. *
  662. * Clear BAR1 of EP CONTROLLER 2 which contains the HOST2's peer scratchpad
  663. * region. While BAR1 is the default peer scratchpad BAR, an NTB could have
  664. * other BARs for peer scratchpad (because of 64-bit BARs or reserved BARs).
  665. * This function can get the exact BAR used for peer scratchpad from
  666. * epf_ntb_bar[BAR_PEER_SPAD].
  667. *
  668. * Since HOST2's peer scratchpad is also HOST1's self scratchpad, this function
  669. * gets the address of peer scratchpad from
  670. * peer_ntb_epc->epf_ntb_bar[BAR_CONFIG].
  671. */
  672. static void epf_ntb_peer_spad_bar_clear(struct epf_ntb_epc *ntb_epc)
  673. {
  674. struct pci_epf_bar *epf_bar;
  675. enum pci_barno barno;
  676. u8 func_no, vfunc_no;
  677. struct pci_epc *epc;
  678. epc = ntb_epc->epc;
  679. func_no = ntb_epc->func_no;
  680. vfunc_no = ntb_epc->vfunc_no;
  681. barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
  682. epf_bar = &ntb_epc->epf_bar[barno];
  683. pci_epc_clear_bar(epc, func_no, vfunc_no, epf_bar);
  684. }
  685. /**
  686. * epf_ntb_peer_spad_bar_set() - Set peer scratchpad BAR
  687. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  688. * @type: PRIMARY interface or SECONDARY interface
  689. *
  690. *+-----------------+------->+------------------+ +-----------------+
  691. *| BAR0 | | CONFIG REGION | | BAR0 |
  692. *+-----------------+----+ +------------------+<-------+-----------------+
  693. *| BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  694. *+-----------------+ +-->+------------------+<-------+-----------------+
  695. *| BAR2 | Local Memory | BAR2 |
  696. *+-----------------+ +-----------------+
  697. *| BAR3 | | BAR3 |
  698. *+-----------------+ +-----------------+
  699. *| BAR4 | | BAR4 |
  700. *+-----------------+ +-----------------+
  701. *| BAR5 | | BAR5 |
  702. *+-----------------+ +-----------------+
  703. * EP CONTROLLER 1 EP CONTROLLER 2
  704. *
  705. * Set BAR1 of EP CONTROLLER 2 which contains the HOST2's peer scratchpad
  706. * region. While BAR1 is the default peer scratchpad BAR, an NTB could have
  707. * other BARs for peer scratchpad (because of 64-bit BARs or reserved BARs).
  708. * This function can get the exact BAR used for peer scratchpad from
  709. * epf_ntb_bar[BAR_PEER_SPAD].
  710. *
  711. * Since HOST2's peer scratchpad is also HOST1's self scratchpad, this function
  712. * gets the address of peer scratchpad from
  713. * peer_ntb_epc->epf_ntb_bar[BAR_CONFIG].
  714. */
  715. static int epf_ntb_peer_spad_bar_set(struct epf_ntb *ntb,
  716. enum pci_epc_interface_type type)
  717. {
  718. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  719. struct pci_epf_bar *peer_epf_bar, *epf_bar;
  720. enum pci_barno peer_barno, barno;
  721. u32 peer_spad_offset;
  722. u8 func_no, vfunc_no;
  723. struct pci_epc *epc;
  724. struct device *dev;
  725. int ret;
  726. dev = &ntb->epf->dev;
  727. peer_ntb_epc = ntb->epc[!type];
  728. peer_barno = peer_ntb_epc->epf_ntb_bar[BAR_CONFIG];
  729. peer_epf_bar = &peer_ntb_epc->epf_bar[peer_barno];
  730. ntb_epc = ntb->epc[type];
  731. barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
  732. epf_bar = &ntb_epc->epf_bar[barno];
  733. func_no = ntb_epc->func_no;
  734. vfunc_no = ntb_epc->vfunc_no;
  735. epc = ntb_epc->epc;
  736. peer_spad_offset = peer_ntb_epc->reg->spad_offset;
  737. epf_bar->phys_addr = peer_epf_bar->phys_addr + peer_spad_offset;
  738. epf_bar->size = peer_ntb_epc->spad_size;
  739. epf_bar->barno = barno;
  740. epf_bar->flags = PCI_BASE_ADDRESS_MEM_TYPE_32;
  741. ret = pci_epc_set_bar(epc, func_no, vfunc_no, epf_bar);
  742. if (ret) {
  743. dev_err(dev, "%s intf: peer SPAD BAR set failed\n",
  744. pci_epc_interface_string(type));
  745. return ret;
  746. }
  747. return 0;
  748. }
  749. /**
  750. * epf_ntb_config_sspad_bar_clear() - Clear Config + Self scratchpad BAR
  751. * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
  752. * address.
  753. *
  754. * +-----------------+------->+------------------+ +-----------------+
  755. * | BAR0 | | CONFIG REGION | | BAR0 |
  756. * +-----------------+----+ +------------------+<-------+-----------------+
  757. * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  758. * +-----------------+ +-->+------------------+<-------+-----------------+
  759. * | BAR2 | Local Memory | BAR2 |
  760. * +-----------------+ +-----------------+
  761. * | BAR3 | | BAR3 |
  762. * +-----------------+ +-----------------+
  763. * | BAR4 | | BAR4 |
  764. * +-----------------+ +-----------------+
  765. * | BAR5 | | BAR5 |
  766. * +-----------------+ +-----------------+
  767. * EP CONTROLLER 1 EP CONTROLLER 2
  768. *
  769. * Clear BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
  770. * self scratchpad region (removes inbound ATU configuration). While BAR0 is
  771. * the default self scratchpad BAR, an NTB could have other BARs for self
  772. * scratchpad (because of reserved BARs). This function can get the exact BAR
  773. * used for self scratchpad from epf_ntb_bar[BAR_CONFIG].
  774. *
  775. * Please note the self scratchpad region and config region is combined to
  776. * a single region and mapped using the same BAR. Also note HOST2's peer
  777. * scratchpad is HOST1's self scratchpad.
  778. */
  779. static void epf_ntb_config_sspad_bar_clear(struct epf_ntb_epc *ntb_epc)
  780. {
  781. struct pci_epf_bar *epf_bar;
  782. enum pci_barno barno;
  783. u8 func_no, vfunc_no;
  784. struct pci_epc *epc;
  785. epc = ntb_epc->epc;
  786. func_no = ntb_epc->func_no;
  787. vfunc_no = ntb_epc->vfunc_no;
  788. barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
  789. epf_bar = &ntb_epc->epf_bar[barno];
  790. pci_epc_clear_bar(epc, func_no, vfunc_no, epf_bar);
  791. }
  792. /**
  793. * epf_ntb_config_sspad_bar_set() - Set Config + Self scratchpad BAR
  794. * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
  795. * address.
  796. *
  797. * +-----------------+------->+------------------+ +-----------------+
  798. * | BAR0 | | CONFIG REGION | | BAR0 |
  799. * +-----------------+----+ +------------------+<-------+-----------------+
  800. * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  801. * +-----------------+ +-->+------------------+<-------+-----------------+
  802. * | BAR2 | Local Memory | BAR2 |
  803. * +-----------------+ +-----------------+
  804. * | BAR3 | | BAR3 |
  805. * +-----------------+ +-----------------+
  806. * | BAR4 | | BAR4 |
  807. * +-----------------+ +-----------------+
  808. * | BAR5 | | BAR5 |
  809. * +-----------------+ +-----------------+
  810. * EP CONTROLLER 1 EP CONTROLLER 2
  811. *
  812. * Map BAR0 of EP CONTROLLER 1 which contains the HOST1's config and
  813. * self scratchpad region. While BAR0 is the default self scratchpad BAR, an
  814. * NTB could have other BARs for self scratchpad (because of reserved BARs).
  815. * This function can get the exact BAR used for self scratchpad from
  816. * epf_ntb_bar[BAR_CONFIG].
  817. *
  818. * Please note the self scratchpad region and config region is combined to
  819. * a single region and mapped using the same BAR. Also note HOST2's peer
  820. * scratchpad is HOST1's self scratchpad.
  821. */
  822. static int epf_ntb_config_sspad_bar_set(struct epf_ntb_epc *ntb_epc)
  823. {
  824. struct pci_epf_bar *epf_bar;
  825. enum pci_barno barno;
  826. u8 func_no, vfunc_no;
  827. struct epf_ntb *ntb;
  828. struct pci_epc *epc;
  829. struct device *dev;
  830. int ret;
  831. ntb = ntb_epc->epf_ntb;
  832. dev = &ntb->epf->dev;
  833. epc = ntb_epc->epc;
  834. func_no = ntb_epc->func_no;
  835. vfunc_no = ntb_epc->vfunc_no;
  836. barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
  837. epf_bar = &ntb_epc->epf_bar[barno];
  838. ret = pci_epc_set_bar(epc, func_no, vfunc_no, epf_bar);
  839. if (ret) {
  840. dev_err(dev, "%s inft: Config/Status/SPAD BAR set failed\n",
  841. pci_epc_interface_string(ntb_epc->type));
  842. return ret;
  843. }
  844. return 0;
  845. }
  846. /**
  847. * epf_ntb_config_spad_bar_free() - Free the physical memory associated with
  848. * config + scratchpad region
  849. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  850. *
  851. * +-----------------+------->+------------------+ +-----------------+
  852. * | BAR0 | | CONFIG REGION | | BAR0 |
  853. * +-----------------+----+ +------------------+<-------+-----------------+
  854. * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  855. * +-----------------+ +-->+------------------+<-------+-----------------+
  856. * | BAR2 | Local Memory | BAR2 |
  857. * +-----------------+ +-----------------+
  858. * | BAR3 | | BAR3 |
  859. * +-----------------+ +-----------------+
  860. * | BAR4 | | BAR4 |
  861. * +-----------------+ +-----------------+
  862. * | BAR5 | | BAR5 |
  863. * +-----------------+ +-----------------+
  864. * EP CONTROLLER 1 EP CONTROLLER 2
  865. *
  866. * Free the Local Memory mentioned in the above diagram. After invoking this
  867. * function, any of config + self scratchpad region of HOST1 or peer scratchpad
  868. * region of HOST2 should not be accessed.
  869. */
  870. static void epf_ntb_config_spad_bar_free(struct epf_ntb *ntb)
  871. {
  872. enum pci_epc_interface_type type;
  873. struct epf_ntb_epc *ntb_epc;
  874. enum pci_barno barno;
  875. struct pci_epf *epf;
  876. epf = ntb->epf;
  877. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
  878. ntb_epc = ntb->epc[type];
  879. barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
  880. if (ntb_epc->reg)
  881. pci_epf_free_space(epf, ntb_epc->reg, barno, type);
  882. }
  883. }
  884. /**
  885. * epf_ntb_config_spad_bar_alloc() - Allocate memory for config + scratchpad
  886. * region
  887. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  888. * @type: PRIMARY interface or SECONDARY interface
  889. *
  890. * +-----------------+------->+------------------+ +-----------------+
  891. * | BAR0 | | CONFIG REGION | | BAR0 |
  892. * +-----------------+----+ +------------------+<-------+-----------------+
  893. * | BAR1 | | |SCRATCHPAD REGION | | BAR1 |
  894. * +-----------------+ +-->+------------------+<-------+-----------------+
  895. * | BAR2 | Local Memory | BAR2 |
  896. * +-----------------+ +-----------------+
  897. * | BAR3 | | BAR3 |
  898. * +-----------------+ +-----------------+
  899. * | BAR4 | | BAR4 |
  900. * +-----------------+ +-----------------+
  901. * | BAR5 | | BAR5 |
  902. * +-----------------+ +-----------------+
  903. * EP CONTROLLER 1 EP CONTROLLER 2
  904. *
  905. * Allocate the Local Memory mentioned in the above diagram. The size of
  906. * CONFIG REGION is sizeof(struct epf_ntb_ctrl) and size of SCRATCHPAD REGION
  907. * is obtained from "spad-count" configfs entry.
  908. *
  909. * The size of both config region and scratchpad region has to be aligned,
  910. * since the scratchpad region will also be mapped as PEER SCRATCHPAD of
  911. * other host using a separate BAR.
  912. */
  913. static int epf_ntb_config_spad_bar_alloc(struct epf_ntb *ntb,
  914. enum pci_epc_interface_type type)
  915. {
  916. const struct pci_epc_features *peer_epc_features, *epc_features;
  917. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  918. size_t msix_table_size, pba_size, align;
  919. enum pci_barno peer_barno, barno;
  920. struct epf_ntb_ctrl *ctrl;
  921. u32 spad_size, ctrl_size;
  922. u64 size, peer_size;
  923. struct pci_epf *epf;
  924. struct device *dev;
  925. bool msix_capable;
  926. u32 spad_count;
  927. void *base;
  928. epf = ntb->epf;
  929. dev = &epf->dev;
  930. ntb_epc = ntb->epc[type];
  931. epc_features = ntb_epc->epc_features;
  932. barno = ntb_epc->epf_ntb_bar[BAR_CONFIG];
  933. size = epc_features->bar_fixed_size[barno];
  934. align = epc_features->align;
  935. peer_ntb_epc = ntb->epc[!type];
  936. peer_epc_features = peer_ntb_epc->epc_features;
  937. peer_barno = ntb_epc->epf_ntb_bar[BAR_PEER_SPAD];
  938. peer_size = peer_epc_features->bar_fixed_size[peer_barno];
  939. /* Check if epc_features is populated incorrectly */
  940. if ((!IS_ALIGNED(size, align)))
  941. return -EINVAL;
  942. spad_count = ntb->spad_count;
  943. ctrl_size = sizeof(struct epf_ntb_ctrl);
  944. spad_size = spad_count * 4;
  945. msix_capable = epc_features->msix_capable;
  946. if (msix_capable) {
  947. msix_table_size = PCI_MSIX_ENTRY_SIZE * ntb->db_count;
  948. ctrl_size = ALIGN(ctrl_size, 8);
  949. ntb_epc->msix_table_offset = ctrl_size;
  950. ntb_epc->msix_bar = barno;
  951. /* Align to QWORD or 8 Bytes */
  952. pba_size = ALIGN(DIV_ROUND_UP(ntb->db_count, 8), 8);
  953. ctrl_size = ctrl_size + msix_table_size + pba_size;
  954. }
  955. if (!align) {
  956. ctrl_size = roundup_pow_of_two(ctrl_size);
  957. spad_size = roundup_pow_of_two(spad_size);
  958. } else {
  959. ctrl_size = ALIGN(ctrl_size, align);
  960. spad_size = ALIGN(spad_size, align);
  961. }
  962. if (peer_size) {
  963. if (peer_size < spad_size)
  964. spad_count = peer_size / 4;
  965. spad_size = peer_size;
  966. }
  967. /*
  968. * In order to make sure SPAD offset is aligned to its size,
  969. * expand control region size to the size of SPAD if SPAD size
  970. * is greater than control region size.
  971. */
  972. if (spad_size > ctrl_size)
  973. ctrl_size = spad_size;
  974. if (!size)
  975. size = ctrl_size + spad_size;
  976. else if (size < ctrl_size + spad_size)
  977. return -EINVAL;
  978. base = pci_epf_alloc_space(epf, size, barno, align, type);
  979. if (!base) {
  980. dev_err(dev, "%s intf: Config/Status/SPAD alloc region fail\n",
  981. pci_epc_interface_string(type));
  982. return -ENOMEM;
  983. }
  984. ntb_epc->reg = base;
  985. ctrl = ntb_epc->reg;
  986. ctrl->spad_offset = ctrl_size;
  987. ctrl->spad_count = spad_count;
  988. ctrl->num_mws = ntb->num_mws;
  989. ctrl->db_entry_size = align ? align : 4;
  990. ntb_epc->spad_size = spad_size;
  991. return 0;
  992. }
  993. /**
  994. * epf_ntb_config_spad_bar_alloc_interface() - Allocate memory for config +
  995. * scratchpad region for each of PRIMARY and SECONDARY interface
  996. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  997. *
  998. * Wrapper for epf_ntb_config_spad_bar_alloc() which allocates memory for
  999. * config + scratchpad region for a specific interface
  1000. */
  1001. static int epf_ntb_config_spad_bar_alloc_interface(struct epf_ntb *ntb)
  1002. {
  1003. enum pci_epc_interface_type type;
  1004. struct device *dev;
  1005. int ret;
  1006. dev = &ntb->epf->dev;
  1007. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
  1008. ret = epf_ntb_config_spad_bar_alloc(ntb, type);
  1009. if (ret) {
  1010. dev_err(dev, "%s intf: Config/SPAD BAR alloc failed\n",
  1011. pci_epc_interface_string(type));
  1012. return ret;
  1013. }
  1014. }
  1015. return 0;
  1016. }
  1017. /**
  1018. * epf_ntb_free_peer_mem() - Free memory allocated in peers outbound address
  1019. * space
  1020. * @ntb_epc: EPC associated with one of the HOST which holds peers outbound
  1021. * address regions
  1022. *
  1023. * +-----------------+ +---->+----------------+-----------+-----------------+
  1024. * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
  1025. * +-----------------+ | +----------------+ +-----------------+
  1026. * | BAR1 | | | Doorbell 2 +---------+ | |
  1027. * +-----------------+----+ +----------------+ | | |
  1028. * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
  1029. * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
  1030. * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
  1031. * +-----------------+ | |----------------+ | | | |
  1032. * | BAR4 | | | | | | +-----------------+
  1033. * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
  1034. * | BAR5 | | | | | | +-----------------+
  1035. * +-----------------+ +---->-----------------+ | | | |
  1036. * EP CONTROLLER 1 | | | | +-----------------+
  1037. * | | | +---->+ MSI|X ADDRESS 4 |
  1038. * +----------------+ | +-----------------+
  1039. * (A) EP CONTROLLER 2 | | |
  1040. * (OB SPACE) | | |
  1041. * +-------> MW1 |
  1042. * | |
  1043. * | |
  1044. * (B) +-----------------+
  1045. * | |
  1046. * | |
  1047. * | |
  1048. * | |
  1049. * | |
  1050. * +-----------------+
  1051. * PCI Address Space
  1052. * (Managed by HOST2)
  1053. *
  1054. * Free memory allocated in EP CONTROLLER 2 (OB SPACE) in the above diagram.
  1055. * It'll free Doorbell 1, Doorbell 2, Doorbell 3, Doorbell 4, MW1 (and MW2, MW3,
  1056. * MW4).
  1057. */
  1058. static void epf_ntb_free_peer_mem(struct epf_ntb_epc *ntb_epc)
  1059. {
  1060. struct pci_epf_bar *epf_bar;
  1061. void __iomem *mw_addr;
  1062. phys_addr_t phys_addr;
  1063. enum epf_ntb_bar bar;
  1064. enum pci_barno barno;
  1065. struct pci_epc *epc;
  1066. size_t size;
  1067. epc = ntb_epc->epc;
  1068. for (bar = BAR_DB_MW1; bar < BAR_MW4; bar++) {
  1069. barno = ntb_epc->epf_ntb_bar[bar];
  1070. mw_addr = ntb_epc->mw_addr[barno];
  1071. epf_bar = &ntb_epc->epf_bar[barno];
  1072. phys_addr = epf_bar->phys_addr;
  1073. size = epf_bar->size;
  1074. if (mw_addr) {
  1075. pci_epc_mem_free_addr(epc, phys_addr, mw_addr, size);
  1076. ntb_epc->mw_addr[barno] = NULL;
  1077. }
  1078. }
  1079. }
  1080. /**
  1081. * epf_ntb_db_mw_bar_clear() - Clear doorbell and memory BAR
  1082. * @ntb_epc: EPC associated with one of the HOST which holds peer's outbound
  1083. * address
  1084. *
  1085. * +-----------------+ +---->+----------------+-----------+-----------------+
  1086. * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
  1087. * +-----------------+ | +----------------+ +-----------------+
  1088. * | BAR1 | | | Doorbell 2 +---------+ | |
  1089. * +-----------------+----+ +----------------+ | | |
  1090. * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
  1091. * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
  1092. * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
  1093. * +-----------------+ | |----------------+ | | | |
  1094. * | BAR4 | | | | | | +-----------------+
  1095. * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
  1096. * | BAR5 | | | | | | +-----------------+
  1097. * +-----------------+ +---->-----------------+ | | | |
  1098. * EP CONTROLLER 1 | | | | +-----------------+
  1099. * | | | +---->+ MSI|X ADDRESS 4 |
  1100. * +----------------+ | +-----------------+
  1101. * (A) EP CONTROLLER 2 | | |
  1102. * (OB SPACE) | | |
  1103. * +-------> MW1 |
  1104. * | |
  1105. * | |
  1106. * (B) +-----------------+
  1107. * | |
  1108. * | |
  1109. * | |
  1110. * | |
  1111. * | |
  1112. * +-----------------+
  1113. * PCI Address Space
  1114. * (Managed by HOST2)
  1115. *
  1116. * Clear doorbell and memory BARs (remove inbound ATU configuration). In the above
  1117. * diagram it clears BAR2 TO BAR5 of EP CONTROLLER 1 (Doorbell BAR, MW1 BAR, MW2
  1118. * BAR, MW3 BAR and MW4 BAR).
  1119. */
  1120. static void epf_ntb_db_mw_bar_clear(struct epf_ntb_epc *ntb_epc)
  1121. {
  1122. struct pci_epf_bar *epf_bar;
  1123. enum epf_ntb_bar bar;
  1124. enum pci_barno barno;
  1125. u8 func_no, vfunc_no;
  1126. struct pci_epc *epc;
  1127. epc = ntb_epc->epc;
  1128. func_no = ntb_epc->func_no;
  1129. vfunc_no = ntb_epc->vfunc_no;
  1130. for (bar = BAR_DB_MW1; bar < BAR_MW4; bar++) {
  1131. barno = ntb_epc->epf_ntb_bar[bar];
  1132. epf_bar = &ntb_epc->epf_bar[barno];
  1133. pci_epc_clear_bar(epc, func_no, vfunc_no, epf_bar);
  1134. }
  1135. }
  1136. /**
  1137. * epf_ntb_db_mw_bar_cleanup() - Clear doorbell/memory BAR and free memory
  1138. * allocated in peers outbound address space
  1139. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1140. * @type: PRIMARY interface or SECONDARY interface
  1141. *
  1142. * Wrapper for epf_ntb_db_mw_bar_clear() to clear HOST1's BAR and
  1143. * epf_ntb_free_peer_mem() which frees up HOST2 outbound memory.
  1144. */
  1145. static void epf_ntb_db_mw_bar_cleanup(struct epf_ntb *ntb,
  1146. enum pci_epc_interface_type type)
  1147. {
  1148. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  1149. ntb_epc = ntb->epc[type];
  1150. peer_ntb_epc = ntb->epc[!type];
  1151. epf_ntb_db_mw_bar_clear(ntb_epc);
  1152. epf_ntb_free_peer_mem(peer_ntb_epc);
  1153. }
  1154. /**
  1155. * epf_ntb_configure_interrupt() - Configure MSI/MSI-X capability
  1156. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1157. * @type: PRIMARY interface or SECONDARY interface
  1158. *
  1159. * Configure MSI/MSI-X capability for each interface with number of
  1160. * interrupts equal to "db_count" configfs entry.
  1161. */
  1162. static int epf_ntb_configure_interrupt(struct epf_ntb *ntb,
  1163. enum pci_epc_interface_type type)
  1164. {
  1165. const struct pci_epc_features *epc_features;
  1166. bool msix_capable, msi_capable;
  1167. struct epf_ntb_epc *ntb_epc;
  1168. u8 func_no, vfunc_no;
  1169. struct pci_epc *epc;
  1170. struct device *dev;
  1171. u32 db_count;
  1172. int ret;
  1173. ntb_epc = ntb->epc[type];
  1174. dev = &ntb->epf->dev;
  1175. epc_features = ntb_epc->epc_features;
  1176. msix_capable = epc_features->msix_capable;
  1177. msi_capable = epc_features->msi_capable;
  1178. if (!(msix_capable || msi_capable)) {
  1179. dev_err(dev, "MSI or MSI-X is required for doorbell\n");
  1180. return -EINVAL;
  1181. }
  1182. func_no = ntb_epc->func_no;
  1183. vfunc_no = ntb_epc->vfunc_no;
  1184. db_count = ntb->db_count;
  1185. if (db_count > MAX_DB_COUNT) {
  1186. dev_err(dev, "DB count cannot be more than %d\n", MAX_DB_COUNT);
  1187. return -EINVAL;
  1188. }
  1189. ntb->db_count = db_count;
  1190. epc = ntb_epc->epc;
  1191. if (msi_capable) {
  1192. ret = pci_epc_set_msi(epc, func_no, vfunc_no, db_count);
  1193. if (ret) {
  1194. dev_err(dev, "%s intf: MSI configuration failed\n",
  1195. pci_epc_interface_string(type));
  1196. return ret;
  1197. }
  1198. }
  1199. if (msix_capable) {
  1200. ret = pci_epc_set_msix(epc, func_no, vfunc_no, db_count,
  1201. ntb_epc->msix_bar,
  1202. ntb_epc->msix_table_offset);
  1203. if (ret) {
  1204. dev_err(dev, "MSI configuration failed\n");
  1205. return ret;
  1206. }
  1207. }
  1208. return 0;
  1209. }
  1210. /**
  1211. * epf_ntb_alloc_peer_mem() - Allocate memory in peer's outbound address space
  1212. * @dev: The PCI device.
  1213. * @ntb_epc: EPC associated with one of the HOST whose BAR holds peer's outbound
  1214. * address
  1215. * @bar: BAR of @ntb_epc in for which memory has to be allocated (could be
  1216. * BAR_DB_MW1, BAR_MW2, BAR_MW3, BAR_MW4)
  1217. * @peer_ntb_epc: EPC associated with HOST whose outbound address space is
  1218. * used by @ntb_epc
  1219. * @size: Size of the address region that has to be allocated in peers OB SPACE
  1220. *
  1221. *
  1222. * +-----------------+ +---->+----------------+-----------+-----------------+
  1223. * | BAR0 | | | Doorbell 1 +-----------> MSI|X ADDRESS 1 |
  1224. * +-----------------+ | +----------------+ +-----------------+
  1225. * | BAR1 | | | Doorbell 2 +---------+ | |
  1226. * +-----------------+----+ +----------------+ | | |
  1227. * | BAR2 | | Doorbell 3 +-------+ | +-----------------+
  1228. * +-----------------+----+ +----------------+ | +-> MSI|X ADDRESS 2 |
  1229. * | BAR3 | | | Doorbell 4 +-----+ | +-----------------+
  1230. * +-----------------+ | |----------------+ | | | |
  1231. * | BAR4 | | | | | | +-----------------+
  1232. * +-----------------+ | | MW1 +---+ | +-->+ MSI|X ADDRESS 3||
  1233. * | BAR5 | | | | | | +-----------------+
  1234. * +-----------------+ +---->-----------------+ | | | |
  1235. * EP CONTROLLER 1 | | | | +-----------------+
  1236. * | | | +---->+ MSI|X ADDRESS 4 |
  1237. * +----------------+ | +-----------------+
  1238. * (A) EP CONTROLLER 2 | | |
  1239. * (OB SPACE) | | |
  1240. * +-------> MW1 |
  1241. * | |
  1242. * | |
  1243. * (B) +-----------------+
  1244. * | |
  1245. * | |
  1246. * | |
  1247. * | |
  1248. * | |
  1249. * +-----------------+
  1250. * PCI Address Space
  1251. * (Managed by HOST2)
  1252. *
  1253. * Allocate memory in OB space of EP CONTROLLER 2 in the above diagram. Allocate
  1254. * for Doorbell 1, Doorbell 2, Doorbell 3, Doorbell 4, MW1 (and MW2, MW3, MW4).
  1255. */
  1256. static int epf_ntb_alloc_peer_mem(struct device *dev,
  1257. struct epf_ntb_epc *ntb_epc,
  1258. enum epf_ntb_bar bar,
  1259. struct epf_ntb_epc *peer_ntb_epc,
  1260. size_t size)
  1261. {
  1262. const struct pci_epc_features *epc_features;
  1263. struct pci_epf_bar *epf_bar;
  1264. struct pci_epc *peer_epc;
  1265. phys_addr_t phys_addr;
  1266. void __iomem *mw_addr;
  1267. enum pci_barno barno;
  1268. size_t align;
  1269. epc_features = ntb_epc->epc_features;
  1270. align = epc_features->align;
  1271. if (size < 128)
  1272. size = 128;
  1273. if (align)
  1274. size = ALIGN(size, align);
  1275. else
  1276. size = roundup_pow_of_two(size);
  1277. peer_epc = peer_ntb_epc->epc;
  1278. mw_addr = pci_epc_mem_alloc_addr(peer_epc, &phys_addr, size);
  1279. if (!mw_addr) {
  1280. dev_err(dev, "%s intf: Failed to allocate OB address\n",
  1281. pci_epc_interface_string(peer_ntb_epc->type));
  1282. return -ENOMEM;
  1283. }
  1284. barno = ntb_epc->epf_ntb_bar[bar];
  1285. epf_bar = &ntb_epc->epf_bar[barno];
  1286. ntb_epc->mw_addr[barno] = mw_addr;
  1287. epf_bar->phys_addr = phys_addr;
  1288. epf_bar->size = size;
  1289. epf_bar->barno = barno;
  1290. epf_bar->flags = PCI_BASE_ADDRESS_MEM_TYPE_32;
  1291. return 0;
  1292. }
  1293. /**
  1294. * epf_ntb_db_mw_bar_init() - Configure Doorbell and Memory window BARs
  1295. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1296. * @type: PRIMARY interface or SECONDARY interface
  1297. *
  1298. * Wrapper for epf_ntb_alloc_peer_mem() and pci_epc_set_bar() that allocates
  1299. * memory in OB address space of HOST2 and configures BAR of HOST1
  1300. */
  1301. static int epf_ntb_db_mw_bar_init(struct epf_ntb *ntb,
  1302. enum pci_epc_interface_type type)
  1303. {
  1304. const struct pci_epc_features *epc_features;
  1305. struct epf_ntb_epc *peer_ntb_epc, *ntb_epc;
  1306. struct pci_epf_bar *epf_bar;
  1307. struct epf_ntb_ctrl *ctrl;
  1308. u32 num_mws, db_count;
  1309. enum epf_ntb_bar bar;
  1310. enum pci_barno barno;
  1311. u8 func_no, vfunc_no;
  1312. struct pci_epc *epc;
  1313. struct device *dev;
  1314. size_t align;
  1315. int ret, i;
  1316. u64 size;
  1317. ntb_epc = ntb->epc[type];
  1318. peer_ntb_epc = ntb->epc[!type];
  1319. dev = &ntb->epf->dev;
  1320. epc_features = ntb_epc->epc_features;
  1321. align = epc_features->align;
  1322. func_no = ntb_epc->func_no;
  1323. vfunc_no = ntb_epc->vfunc_no;
  1324. epc = ntb_epc->epc;
  1325. num_mws = ntb->num_mws;
  1326. db_count = ntb->db_count;
  1327. for (bar = BAR_DB_MW1, i = 0; i < num_mws; bar++, i++) {
  1328. if (bar == BAR_DB_MW1) {
  1329. align = align ? align : 4;
  1330. size = db_count * align;
  1331. size = ALIGN(size, ntb->mws_size[i]);
  1332. ctrl = ntb_epc->reg;
  1333. ctrl->mw1_offset = size;
  1334. size += ntb->mws_size[i];
  1335. } else {
  1336. size = ntb->mws_size[i];
  1337. }
  1338. ret = epf_ntb_alloc_peer_mem(dev, ntb_epc, bar,
  1339. peer_ntb_epc, size);
  1340. if (ret) {
  1341. dev_err(dev, "%s intf: DoorBell mem alloc failed\n",
  1342. pci_epc_interface_string(type));
  1343. goto err_alloc_peer_mem;
  1344. }
  1345. barno = ntb_epc->epf_ntb_bar[bar];
  1346. epf_bar = &ntb_epc->epf_bar[barno];
  1347. ret = pci_epc_set_bar(epc, func_no, vfunc_no, epf_bar);
  1348. if (ret) {
  1349. dev_err(dev, "%s intf: DoorBell BAR set failed\n",
  1350. pci_epc_interface_string(type));
  1351. goto err_alloc_peer_mem;
  1352. }
  1353. }
  1354. return 0;
  1355. err_alloc_peer_mem:
  1356. epf_ntb_db_mw_bar_cleanup(ntb, type);
  1357. return ret;
  1358. }
  1359. /**
  1360. * epf_ntb_epc_destroy_interface() - Cleanup NTB EPC interface
  1361. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1362. * @type: PRIMARY interface or SECONDARY interface
  1363. *
  1364. * Unbind NTB function device from EPC and relinquish reference to pci_epc
  1365. * for each of the interface.
  1366. */
  1367. static void epf_ntb_epc_destroy_interface(struct epf_ntb *ntb,
  1368. enum pci_epc_interface_type type)
  1369. {
  1370. struct epf_ntb_epc *ntb_epc;
  1371. struct pci_epc *epc;
  1372. struct pci_epf *epf;
  1373. if (type < 0)
  1374. return;
  1375. epf = ntb->epf;
  1376. ntb_epc = ntb->epc[type];
  1377. if (!ntb_epc)
  1378. return;
  1379. epc = ntb_epc->epc;
  1380. pci_epc_remove_epf(epc, epf, type);
  1381. pci_epc_put(epc);
  1382. }
  1383. /**
  1384. * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
  1385. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1386. *
  1387. * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
  1388. */
  1389. static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
  1390. {
  1391. enum pci_epc_interface_type type;
  1392. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++)
  1393. epf_ntb_epc_destroy_interface(ntb, type);
  1394. }
  1395. /**
  1396. * epf_ntb_epc_create_interface() - Create and initialize NTB EPC interface
  1397. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1398. * @epc: struct pci_epc to which a particular NTB interface should be associated
  1399. * @type: PRIMARY interface or SECONDARY interface
  1400. *
  1401. * Allocate memory for NTB EPC interface and initialize it.
  1402. */
  1403. static int epf_ntb_epc_create_interface(struct epf_ntb *ntb,
  1404. struct pci_epc *epc,
  1405. enum pci_epc_interface_type type)
  1406. {
  1407. const struct pci_epc_features *epc_features;
  1408. struct pci_epf_bar *epf_bar;
  1409. struct epf_ntb_epc *ntb_epc;
  1410. u8 func_no, vfunc_no;
  1411. struct pci_epf *epf;
  1412. struct device *dev;
  1413. dev = &ntb->epf->dev;
  1414. ntb_epc = devm_kzalloc(dev, sizeof(*ntb_epc), GFP_KERNEL);
  1415. if (!ntb_epc)
  1416. return -ENOMEM;
  1417. epf = ntb->epf;
  1418. vfunc_no = epf->vfunc_no;
  1419. if (type == PRIMARY_INTERFACE) {
  1420. func_no = epf->func_no;
  1421. epf_bar = epf->bar;
  1422. } else {
  1423. func_no = epf->sec_epc_func_no;
  1424. epf_bar = epf->sec_epc_bar;
  1425. }
  1426. ntb_epc->linkup = false;
  1427. ntb_epc->epc = epc;
  1428. ntb_epc->func_no = func_no;
  1429. ntb_epc->vfunc_no = vfunc_no;
  1430. ntb_epc->type = type;
  1431. ntb_epc->epf_bar = epf_bar;
  1432. ntb_epc->epf_ntb = ntb;
  1433. epc_features = pci_epc_get_features(epc, func_no, vfunc_no);
  1434. if (!epc_features)
  1435. return -EINVAL;
  1436. ntb_epc->epc_features = epc_features;
  1437. ntb->epc[type] = ntb_epc;
  1438. return 0;
  1439. }
  1440. /**
  1441. * epf_ntb_epc_create() - Create and initialize NTB EPC interface
  1442. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1443. *
  1444. * Get a reference to EPC device and bind NTB function device to that EPC
  1445. * for each of the interface. It is also a wrapper to
  1446. * epf_ntb_epc_create_interface() to allocate memory for NTB EPC interface
  1447. * and initialize it
  1448. */
  1449. static int epf_ntb_epc_create(struct epf_ntb *ntb)
  1450. {
  1451. struct pci_epf *epf;
  1452. struct device *dev;
  1453. int ret;
  1454. epf = ntb->epf;
  1455. dev = &epf->dev;
  1456. ret = epf_ntb_epc_create_interface(ntb, epf->epc, PRIMARY_INTERFACE);
  1457. if (ret) {
  1458. dev_err(dev, "PRIMARY intf: Fail to create NTB EPC\n");
  1459. return ret;
  1460. }
  1461. ret = epf_ntb_epc_create_interface(ntb, epf->sec_epc,
  1462. SECONDARY_INTERFACE);
  1463. if (ret) {
  1464. dev_err(dev, "SECONDARY intf: Fail to create NTB EPC\n");
  1465. goto err_epc_create;
  1466. }
  1467. return 0;
  1468. err_epc_create:
  1469. epf_ntb_epc_destroy_interface(ntb, PRIMARY_INTERFACE);
  1470. return ret;
  1471. }
  1472. /**
  1473. * epf_ntb_init_epc_bar_interface() - Identify BARs to be used for each of
  1474. * the NTB constructs (scratchpad region, doorbell, memorywindow)
  1475. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1476. * @type: PRIMARY interface or SECONDARY interface
  1477. *
  1478. * Identify the free BARs to be used for each of BAR_CONFIG, BAR_PEER_SPAD,
  1479. * BAR_DB_MW1, BAR_MW2, BAR_MW3 and BAR_MW4.
  1480. */
  1481. static int epf_ntb_init_epc_bar_interface(struct epf_ntb *ntb,
  1482. enum pci_epc_interface_type type)
  1483. {
  1484. const struct pci_epc_features *epc_features;
  1485. struct epf_ntb_epc *ntb_epc;
  1486. enum pci_barno barno;
  1487. enum epf_ntb_bar bar;
  1488. struct device *dev;
  1489. u32 num_mws;
  1490. int i;
  1491. barno = BAR_0;
  1492. ntb_epc = ntb->epc[type];
  1493. num_mws = ntb->num_mws;
  1494. dev = &ntb->epf->dev;
  1495. epc_features = ntb_epc->epc_features;
  1496. /* These are required BARs which are mandatory for NTB functionality */
  1497. for (bar = BAR_CONFIG; bar <= BAR_DB_MW1; bar++, barno++) {
  1498. barno = pci_epc_get_next_free_bar(epc_features, barno);
  1499. if (barno < 0) {
  1500. dev_err(dev, "%s intf: Fail to get NTB function BAR\n",
  1501. pci_epc_interface_string(type));
  1502. return barno;
  1503. }
  1504. ntb_epc->epf_ntb_bar[bar] = barno;
  1505. }
  1506. /* These are optional BARs which don't impact NTB functionality */
  1507. for (bar = BAR_MW2, i = 1; i < num_mws; bar++, barno++, i++) {
  1508. barno = pci_epc_get_next_free_bar(epc_features, barno);
  1509. if (barno < 0) {
  1510. ntb->num_mws = i;
  1511. dev_dbg(dev, "BAR not available for > MW%d\n", i + 1);
  1512. }
  1513. ntb_epc->epf_ntb_bar[bar] = barno;
  1514. }
  1515. return 0;
  1516. }
  1517. /**
  1518. * epf_ntb_init_epc_bar() - Identify BARs to be used for each of the NTB
  1519. * constructs (scratchpad region, doorbell, memorywindow)
  1520. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1521. *
  1522. * Wrapper to epf_ntb_init_epc_bar_interface() to identify the free BARs
  1523. * to be used for each of BAR_CONFIG, BAR_PEER_SPAD, BAR_DB_MW1, BAR_MW2,
  1524. * BAR_MW3 and BAR_MW4 for all the interfaces.
  1525. */
  1526. static int epf_ntb_init_epc_bar(struct epf_ntb *ntb)
  1527. {
  1528. enum pci_epc_interface_type type;
  1529. struct device *dev;
  1530. int ret;
  1531. dev = &ntb->epf->dev;
  1532. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
  1533. ret = epf_ntb_init_epc_bar_interface(ntb, type);
  1534. if (ret) {
  1535. dev_err(dev, "Fail to init EPC bar for %s interface\n",
  1536. pci_epc_interface_string(type));
  1537. return ret;
  1538. }
  1539. }
  1540. return 0;
  1541. }
  1542. /**
  1543. * epf_ntb_epc_init_interface() - Initialize NTB interface
  1544. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1545. * @type: PRIMARY interface or SECONDARY interface
  1546. *
  1547. * Wrapper to initialize a particular EPC interface and start the workqueue
  1548. * to check for commands from host. This function will write to the
  1549. * EP controller HW for configuring it.
  1550. */
  1551. static int epf_ntb_epc_init_interface(struct epf_ntb *ntb,
  1552. enum pci_epc_interface_type type)
  1553. {
  1554. struct epf_ntb_epc *ntb_epc;
  1555. u8 func_no, vfunc_no;
  1556. struct pci_epc *epc;
  1557. struct pci_epf *epf;
  1558. struct device *dev;
  1559. int ret;
  1560. ntb_epc = ntb->epc[type];
  1561. epf = ntb->epf;
  1562. dev = &epf->dev;
  1563. epc = ntb_epc->epc;
  1564. func_no = ntb_epc->func_no;
  1565. vfunc_no = ntb_epc->vfunc_no;
  1566. ret = epf_ntb_config_sspad_bar_set(ntb->epc[type]);
  1567. if (ret) {
  1568. dev_err(dev, "%s intf: Config/self SPAD BAR init failed\n",
  1569. pci_epc_interface_string(type));
  1570. return ret;
  1571. }
  1572. ret = epf_ntb_peer_spad_bar_set(ntb, type);
  1573. if (ret) {
  1574. dev_err(dev, "%s intf: Peer SPAD BAR init failed\n",
  1575. pci_epc_interface_string(type));
  1576. goto err_peer_spad_bar_init;
  1577. }
  1578. ret = epf_ntb_configure_interrupt(ntb, type);
  1579. if (ret) {
  1580. dev_err(dev, "%s intf: Interrupt configuration failed\n",
  1581. pci_epc_interface_string(type));
  1582. goto err_peer_spad_bar_init;
  1583. }
  1584. ret = epf_ntb_db_mw_bar_init(ntb, type);
  1585. if (ret) {
  1586. dev_err(dev, "%s intf: DB/MW BAR init failed\n",
  1587. pci_epc_interface_string(type));
  1588. goto err_db_mw_bar_init;
  1589. }
  1590. if (vfunc_no <= 1) {
  1591. ret = pci_epc_write_header(epc, func_no, vfunc_no, epf->header);
  1592. if (ret) {
  1593. dev_err(dev, "%s intf: Configuration header write failed\n",
  1594. pci_epc_interface_string(type));
  1595. goto err_write_header;
  1596. }
  1597. }
  1598. INIT_DELAYED_WORK(&ntb->epc[type]->cmd_handler, epf_ntb_cmd_handler);
  1599. queue_work(kpcintb_workqueue, &ntb->epc[type]->cmd_handler.work);
  1600. return 0;
  1601. err_write_header:
  1602. epf_ntb_db_mw_bar_cleanup(ntb, type);
  1603. err_db_mw_bar_init:
  1604. epf_ntb_peer_spad_bar_clear(ntb->epc[type]);
  1605. err_peer_spad_bar_init:
  1606. epf_ntb_config_sspad_bar_clear(ntb->epc[type]);
  1607. return ret;
  1608. }
  1609. /**
  1610. * epf_ntb_epc_cleanup_interface() - Cleanup NTB interface
  1611. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1612. * @type: PRIMARY interface or SECONDARY interface
  1613. *
  1614. * Wrapper to cleanup a particular NTB interface.
  1615. */
  1616. static void epf_ntb_epc_cleanup_interface(struct epf_ntb *ntb,
  1617. enum pci_epc_interface_type type)
  1618. {
  1619. struct epf_ntb_epc *ntb_epc;
  1620. if (type < 0)
  1621. return;
  1622. ntb_epc = ntb->epc[type];
  1623. cancel_delayed_work(&ntb_epc->cmd_handler);
  1624. epf_ntb_db_mw_bar_cleanup(ntb, type);
  1625. epf_ntb_peer_spad_bar_clear(ntb_epc);
  1626. epf_ntb_config_sspad_bar_clear(ntb_epc);
  1627. }
  1628. /**
  1629. * epf_ntb_epc_cleanup() - Cleanup all NTB interfaces
  1630. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1631. *
  1632. * Wrapper to cleanup all NTB interfaces.
  1633. */
  1634. static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
  1635. {
  1636. enum pci_epc_interface_type type;
  1637. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++)
  1638. epf_ntb_epc_cleanup_interface(ntb, type);
  1639. }
  1640. /**
  1641. * epf_ntb_epc_init() - Initialize all NTB interfaces
  1642. * @ntb: NTB device that facilitates communication between HOST1 and HOST2
  1643. *
  1644. * Wrapper to initialize all NTB interface and start the workqueue
  1645. * to check for commands from host.
  1646. */
  1647. static int epf_ntb_epc_init(struct epf_ntb *ntb)
  1648. {
  1649. enum pci_epc_interface_type type;
  1650. struct device *dev;
  1651. int ret;
  1652. dev = &ntb->epf->dev;
  1653. for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++) {
  1654. ret = epf_ntb_epc_init_interface(ntb, type);
  1655. if (ret) {
  1656. dev_err(dev, "%s intf: Failed to initialize\n",
  1657. pci_epc_interface_string(type));
  1658. goto err_init_type;
  1659. }
  1660. }
  1661. return 0;
  1662. err_init_type:
  1663. epf_ntb_epc_cleanup_interface(ntb, type - 1);
  1664. return ret;
  1665. }
  1666. /**
  1667. * epf_ntb_bind() - Initialize endpoint controller to provide NTB functionality
  1668. * @epf: NTB endpoint function device
  1669. *
  1670. * Initialize both the endpoint controllers associated with NTB function device.
  1671. * Invoked when a primary interface or secondary interface is bound to EPC
  1672. * device. This function will succeed only when EPC is bound to both the
  1673. * interfaces.
  1674. */
  1675. static int epf_ntb_bind(struct pci_epf *epf)
  1676. {
  1677. struct epf_ntb *ntb = epf_get_drvdata(epf);
  1678. struct device *dev = &epf->dev;
  1679. int ret;
  1680. if (!epf->epc) {
  1681. dev_dbg(dev, "PRIMARY EPC interface not yet bound\n");
  1682. return 0;
  1683. }
  1684. if (!epf->sec_epc) {
  1685. dev_dbg(dev, "SECONDARY EPC interface not yet bound\n");
  1686. return 0;
  1687. }
  1688. ret = epf_ntb_epc_create(ntb);
  1689. if (ret) {
  1690. dev_err(dev, "Failed to create NTB EPC\n");
  1691. return ret;
  1692. }
  1693. ret = epf_ntb_init_epc_bar(ntb);
  1694. if (ret) {
  1695. dev_err(dev, "Failed to create NTB EPC\n");
  1696. goto err_bar_init;
  1697. }
  1698. ret = epf_ntb_config_spad_bar_alloc_interface(ntb);
  1699. if (ret) {
  1700. dev_err(dev, "Failed to allocate BAR memory\n");
  1701. goto err_bar_alloc;
  1702. }
  1703. ret = epf_ntb_epc_init(ntb);
  1704. if (ret) {
  1705. dev_err(dev, "Failed to initialize EPC\n");
  1706. goto err_bar_alloc;
  1707. }
  1708. epf_set_drvdata(epf, ntb);
  1709. return 0;
  1710. err_bar_alloc:
  1711. epf_ntb_config_spad_bar_free(ntb);
  1712. err_bar_init:
  1713. epf_ntb_epc_destroy(ntb);
  1714. return ret;
  1715. }
  1716. /**
  1717. * epf_ntb_unbind() - Cleanup the initialization from epf_ntb_bind()
  1718. * @epf: NTB endpoint function device
  1719. *
  1720. * Cleanup the initialization from epf_ntb_bind()
  1721. */
  1722. static void epf_ntb_unbind(struct pci_epf *epf)
  1723. {
  1724. struct epf_ntb *ntb = epf_get_drvdata(epf);
  1725. epf_ntb_epc_cleanup(ntb);
  1726. epf_ntb_config_spad_bar_free(ntb);
  1727. epf_ntb_epc_destroy(ntb);
  1728. }
  1729. #define EPF_NTB_R(_name) \
  1730. static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
  1731. char *page) \
  1732. { \
  1733. struct config_group *group = to_config_group(item); \
  1734. struct epf_ntb *ntb = to_epf_ntb(group); \
  1735. \
  1736. return sysfs_emit(page, "%d\n", ntb->_name); \
  1737. }
  1738. #define EPF_NTB_W(_name) \
  1739. static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
  1740. const char *page, size_t len) \
  1741. { \
  1742. struct config_group *group = to_config_group(item); \
  1743. struct epf_ntb *ntb = to_epf_ntb(group); \
  1744. u32 val; \
  1745. \
  1746. if (kstrtou32(page, 0, &val) < 0) \
  1747. return -EINVAL; \
  1748. \
  1749. ntb->_name = val; \
  1750. \
  1751. return len; \
  1752. }
  1753. #define EPF_NTB_MW_R(_name) \
  1754. static ssize_t epf_ntb_##_name##_show(struct config_item *item, \
  1755. char *page) \
  1756. { \
  1757. struct config_group *group = to_config_group(item); \
  1758. struct epf_ntb *ntb = to_epf_ntb(group); \
  1759. int win_no; \
  1760. \
  1761. sscanf(#_name, "mw%d", &win_no); \
  1762. \
  1763. return sysfs_emit(page, "%lld\n", ntb->mws_size[win_no - 1]); \
  1764. }
  1765. #define EPF_NTB_MW_W(_name) \
  1766. static ssize_t epf_ntb_##_name##_store(struct config_item *item, \
  1767. const char *page, size_t len) \
  1768. { \
  1769. struct config_group *group = to_config_group(item); \
  1770. struct epf_ntb *ntb = to_epf_ntb(group); \
  1771. struct device *dev = &ntb->epf->dev; \
  1772. int win_no; \
  1773. u64 val; \
  1774. \
  1775. if (kstrtou64(page, 0, &val) < 0) \
  1776. return -EINVAL; \
  1777. \
  1778. if (sscanf(#_name, "mw%d", &win_no) != 1) \
  1779. return -EINVAL; \
  1780. \
  1781. if (ntb->num_mws < win_no) { \
  1782. dev_err(dev, "Invalid num_nws: %d value\n", ntb->num_mws); \
  1783. return -EINVAL; \
  1784. } \
  1785. \
  1786. ntb->mws_size[win_no - 1] = val; \
  1787. \
  1788. return len; \
  1789. }
  1790. static ssize_t epf_ntb_num_mws_store(struct config_item *item,
  1791. const char *page, size_t len)
  1792. {
  1793. struct config_group *group = to_config_group(item);
  1794. struct epf_ntb *ntb = to_epf_ntb(group);
  1795. u32 val;
  1796. if (kstrtou32(page, 0, &val) < 0)
  1797. return -EINVAL;
  1798. if (val > MAX_MW)
  1799. return -EINVAL;
  1800. ntb->num_mws = val;
  1801. return len;
  1802. }
  1803. EPF_NTB_R(spad_count)
  1804. EPF_NTB_W(spad_count)
  1805. EPF_NTB_R(db_count)
  1806. EPF_NTB_W(db_count)
  1807. EPF_NTB_R(num_mws)
  1808. EPF_NTB_MW_R(mw1)
  1809. EPF_NTB_MW_W(mw1)
  1810. EPF_NTB_MW_R(mw2)
  1811. EPF_NTB_MW_W(mw2)
  1812. EPF_NTB_MW_R(mw3)
  1813. EPF_NTB_MW_W(mw3)
  1814. EPF_NTB_MW_R(mw4)
  1815. EPF_NTB_MW_W(mw4)
  1816. CONFIGFS_ATTR(epf_ntb_, spad_count);
  1817. CONFIGFS_ATTR(epf_ntb_, db_count);
  1818. CONFIGFS_ATTR(epf_ntb_, num_mws);
  1819. CONFIGFS_ATTR(epf_ntb_, mw1);
  1820. CONFIGFS_ATTR(epf_ntb_, mw2);
  1821. CONFIGFS_ATTR(epf_ntb_, mw3);
  1822. CONFIGFS_ATTR(epf_ntb_, mw4);
  1823. static struct configfs_attribute *epf_ntb_attrs[] = {
  1824. &epf_ntb_attr_spad_count,
  1825. &epf_ntb_attr_db_count,
  1826. &epf_ntb_attr_num_mws,
  1827. &epf_ntb_attr_mw1,
  1828. &epf_ntb_attr_mw2,
  1829. &epf_ntb_attr_mw3,
  1830. &epf_ntb_attr_mw4,
  1831. NULL,
  1832. };
  1833. static const struct config_item_type ntb_group_type = {
  1834. .ct_attrs = epf_ntb_attrs,
  1835. .ct_owner = THIS_MODULE,
  1836. };
  1837. /**
  1838. * epf_ntb_add_cfs() - Add configfs directory specific to NTB
  1839. * @epf: NTB endpoint function device
  1840. * @group: A pointer to the config_group structure referencing a group of
  1841. * config_items of a specific type that belong to a specific sub-system.
  1842. *
  1843. * Add configfs directory specific to NTB. This directory will hold
  1844. * NTB specific properties like db_count, spad_count, num_mws etc.,
  1845. */
  1846. static struct config_group *epf_ntb_add_cfs(struct pci_epf *epf,
  1847. struct config_group *group)
  1848. {
  1849. struct epf_ntb *ntb = epf_get_drvdata(epf);
  1850. struct config_group *ntb_group = &ntb->group;
  1851. struct device *dev = &epf->dev;
  1852. config_group_init_type_name(ntb_group, dev_name(dev), &ntb_group_type);
  1853. return ntb_group;
  1854. }
  1855. /**
  1856. * epf_ntb_probe() - Probe NTB function driver
  1857. * @epf: NTB endpoint function device
  1858. *
  1859. * Probe NTB function driver when endpoint function bus detects a NTB
  1860. * endpoint function.
  1861. */
  1862. static int epf_ntb_probe(struct pci_epf *epf)
  1863. {
  1864. struct epf_ntb *ntb;
  1865. struct device *dev;
  1866. dev = &epf->dev;
  1867. ntb = devm_kzalloc(dev, sizeof(*ntb), GFP_KERNEL);
  1868. if (!ntb)
  1869. return -ENOMEM;
  1870. epf->header = &epf_ntb_header;
  1871. ntb->epf = epf;
  1872. epf_set_drvdata(epf, ntb);
  1873. return 0;
  1874. }
  1875. static struct pci_epf_ops epf_ntb_ops = {
  1876. .bind = epf_ntb_bind,
  1877. .unbind = epf_ntb_unbind,
  1878. .add_cfs = epf_ntb_add_cfs,
  1879. };
  1880. static const struct pci_epf_device_id epf_ntb_ids[] = {
  1881. {
  1882. .name = "pci_epf_ntb",
  1883. },
  1884. {},
  1885. };
  1886. static struct pci_epf_driver epf_ntb_driver = {
  1887. .driver.name = "pci_epf_ntb",
  1888. .probe = epf_ntb_probe,
  1889. .id_table = epf_ntb_ids,
  1890. .ops = &epf_ntb_ops,
  1891. .owner = THIS_MODULE,
  1892. };
  1893. static int __init epf_ntb_init(void)
  1894. {
  1895. int ret;
  1896. kpcintb_workqueue = alloc_workqueue("kpcintb", WQ_MEM_RECLAIM |
  1897. WQ_HIGHPRI, 0);
  1898. ret = pci_epf_register_driver(&epf_ntb_driver);
  1899. if (ret) {
  1900. destroy_workqueue(kpcintb_workqueue);
  1901. pr_err("Failed to register pci epf ntb driver --> %d\n", ret);
  1902. return ret;
  1903. }
  1904. return 0;
  1905. }
  1906. module_init(epf_ntb_init);
  1907. static void __exit epf_ntb_exit(void)
  1908. {
  1909. pci_epf_unregister_driver(&epf_ntb_driver);
  1910. destroy_workqueue(kpcintb_workqueue);
  1911. }
  1912. module_exit(epf_ntb_exit);
  1913. MODULE_DESCRIPTION("PCI EPF NTB DRIVER");
  1914. MODULE_AUTHOR("Kishon Vijay Abraham I <[email protected]>");
  1915. MODULE_LICENSE("GPL v2");