ntb.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  8. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * BSD LICENSE
  20. *
  21. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  22. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above copy
  31. * notice, this list of conditions and the following disclaimer in
  32. * the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name of Intel Corporation nor the names of its
  35. * contributors may be used to endorse or promote products derived
  36. * from this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. * PCIe NTB Linux driver
  51. *
  52. * Contact Information:
  53. * Allen Hubbe <[email protected]>
  54. */
  55. #ifndef _NTB_H_
  56. #define _NTB_H_
  57. #include <linux/completion.h>
  58. #include <linux/device.h>
  59. #include <linux/interrupt.h>
  60. struct ntb_client;
  61. struct ntb_dev;
  62. struct ntb_msi;
  63. struct pci_dev;
  64. /**
  65. * enum ntb_topo - NTB connection topology
  66. * @NTB_TOPO_NONE: Topology is unknown or invalid.
  67. * @NTB_TOPO_PRI: On primary side of local ntb.
  68. * @NTB_TOPO_SEC: On secondary side of remote ntb.
  69. * @NTB_TOPO_B2B_USD: On primary side of local ntb upstream of remote ntb.
  70. * @NTB_TOPO_B2B_DSD: On primary side of local ntb downstream of remote ntb.
  71. * @NTB_TOPO_SWITCH: Connected via a switch which supports ntb.
  72. * @NTB_TOPO_CROSSLINK: Connected via two symmetric switchecs
  73. */
  74. enum ntb_topo {
  75. NTB_TOPO_NONE = -1,
  76. NTB_TOPO_PRI,
  77. NTB_TOPO_SEC,
  78. NTB_TOPO_B2B_USD,
  79. NTB_TOPO_B2B_DSD,
  80. NTB_TOPO_SWITCH,
  81. NTB_TOPO_CROSSLINK,
  82. };
  83. static inline int ntb_topo_is_b2b(enum ntb_topo topo)
  84. {
  85. switch ((int)topo) {
  86. case NTB_TOPO_B2B_USD:
  87. case NTB_TOPO_B2B_DSD:
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. static inline char *ntb_topo_string(enum ntb_topo topo)
  93. {
  94. switch (topo) {
  95. case NTB_TOPO_NONE: return "NTB_TOPO_NONE";
  96. case NTB_TOPO_PRI: return "NTB_TOPO_PRI";
  97. case NTB_TOPO_SEC: return "NTB_TOPO_SEC";
  98. case NTB_TOPO_B2B_USD: return "NTB_TOPO_B2B_USD";
  99. case NTB_TOPO_B2B_DSD: return "NTB_TOPO_B2B_DSD";
  100. case NTB_TOPO_SWITCH: return "NTB_TOPO_SWITCH";
  101. case NTB_TOPO_CROSSLINK: return "NTB_TOPO_CROSSLINK";
  102. }
  103. return "NTB_TOPO_INVALID";
  104. }
  105. /**
  106. * enum ntb_speed - NTB link training speed
  107. * @NTB_SPEED_AUTO: Request the max supported speed.
  108. * @NTB_SPEED_NONE: Link is not trained to any speed.
  109. * @NTB_SPEED_GEN1: Link is trained to gen1 speed.
  110. * @NTB_SPEED_GEN2: Link is trained to gen2 speed.
  111. * @NTB_SPEED_GEN3: Link is trained to gen3 speed.
  112. * @NTB_SPEED_GEN4: Link is trained to gen4 speed.
  113. */
  114. enum ntb_speed {
  115. NTB_SPEED_AUTO = -1,
  116. NTB_SPEED_NONE = 0,
  117. NTB_SPEED_GEN1 = 1,
  118. NTB_SPEED_GEN2 = 2,
  119. NTB_SPEED_GEN3 = 3,
  120. NTB_SPEED_GEN4 = 4
  121. };
  122. /**
  123. * enum ntb_width - NTB link training width
  124. * @NTB_WIDTH_AUTO: Request the max supported width.
  125. * @NTB_WIDTH_NONE: Link is not trained to any width.
  126. * @NTB_WIDTH_1: Link is trained to 1 lane width.
  127. * @NTB_WIDTH_2: Link is trained to 2 lane width.
  128. * @NTB_WIDTH_4: Link is trained to 4 lane width.
  129. * @NTB_WIDTH_8: Link is trained to 8 lane width.
  130. * @NTB_WIDTH_12: Link is trained to 12 lane width.
  131. * @NTB_WIDTH_16: Link is trained to 16 lane width.
  132. * @NTB_WIDTH_32: Link is trained to 32 lane width.
  133. */
  134. enum ntb_width {
  135. NTB_WIDTH_AUTO = -1,
  136. NTB_WIDTH_NONE = 0,
  137. NTB_WIDTH_1 = 1,
  138. NTB_WIDTH_2 = 2,
  139. NTB_WIDTH_4 = 4,
  140. NTB_WIDTH_8 = 8,
  141. NTB_WIDTH_12 = 12,
  142. NTB_WIDTH_16 = 16,
  143. NTB_WIDTH_32 = 32,
  144. };
  145. /**
  146. * enum ntb_default_port - NTB default port number
  147. * @NTB_PORT_PRI_USD: Default port of the NTB_TOPO_PRI/NTB_TOPO_B2B_USD
  148. * topologies
  149. * @NTB_PORT_SEC_DSD: Default port of the NTB_TOPO_SEC/NTB_TOPO_B2B_DSD
  150. * topologies
  151. */
  152. enum ntb_default_port {
  153. NTB_PORT_PRI_USD,
  154. NTB_PORT_SEC_DSD
  155. };
  156. #define NTB_DEF_PEER_CNT (1)
  157. #define NTB_DEF_PEER_IDX (0)
  158. /**
  159. * struct ntb_client_ops - ntb client operations
  160. * @probe: Notify client of a new device.
  161. * @remove: Notify client to remove a device.
  162. */
  163. struct ntb_client_ops {
  164. int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
  165. void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
  166. };
  167. static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
  168. {
  169. /* commented callbacks are not required: */
  170. return
  171. ops->probe &&
  172. ops->remove &&
  173. 1;
  174. }
  175. /**
  176. * struct ntb_ctx_ops - ntb driver context operations
  177. * @link_event: See ntb_link_event().
  178. * @db_event: See ntb_db_event().
  179. * @msg_event: See ntb_msg_event().
  180. */
  181. struct ntb_ctx_ops {
  182. void (*link_event)(void *ctx);
  183. void (*db_event)(void *ctx, int db_vector);
  184. void (*msg_event)(void *ctx);
  185. };
  186. static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
  187. {
  188. /* commented callbacks are not required: */
  189. return
  190. /* ops->link_event && */
  191. /* ops->db_event && */
  192. /* ops->msg_event && */
  193. 1;
  194. }
  195. /**
  196. * struct ntb_dev_ops - ntb device operations
  197. * @port_number: See ntb_port_number().
  198. * @peer_port_count: See ntb_peer_port_count().
  199. * @peer_port_number: See ntb_peer_port_number().
  200. * @peer_port_idx: See ntb_peer_port_idx().
  201. * @link_is_up: See ntb_link_is_up().
  202. * @link_enable: See ntb_link_enable().
  203. * @link_disable: See ntb_link_disable().
  204. * @mw_count: See ntb_mw_count().
  205. * @mw_get_align: See ntb_mw_get_align().
  206. * @mw_set_trans: See ntb_mw_set_trans().
  207. * @mw_clear_trans: See ntb_mw_clear_trans().
  208. * @peer_mw_count: See ntb_peer_mw_count().
  209. * @peer_mw_get_addr: See ntb_peer_mw_get_addr().
  210. * @peer_mw_set_trans: See ntb_peer_mw_set_trans().
  211. * @peer_mw_clear_trans:See ntb_peer_mw_clear_trans().
  212. * @db_is_unsafe: See ntb_db_is_unsafe().
  213. * @db_valid_mask: See ntb_db_valid_mask().
  214. * @db_vector_count: See ntb_db_vector_count().
  215. * @db_vector_mask: See ntb_db_vector_mask().
  216. * @db_read: See ntb_db_read().
  217. * @db_set: See ntb_db_set().
  218. * @db_clear: See ntb_db_clear().
  219. * @db_read_mask: See ntb_db_read_mask().
  220. * @db_set_mask: See ntb_db_set_mask().
  221. * @db_clear_mask: See ntb_db_clear_mask().
  222. * @peer_db_addr: See ntb_peer_db_addr().
  223. * @peer_db_read: See ntb_peer_db_read().
  224. * @peer_db_set: See ntb_peer_db_set().
  225. * @peer_db_clear: See ntb_peer_db_clear().
  226. * @peer_db_read_mask: See ntb_peer_db_read_mask().
  227. * @peer_db_set_mask: See ntb_peer_db_set_mask().
  228. * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
  229. * @spad_is_unsafe: See ntb_spad_is_unsafe().
  230. * @spad_count: See ntb_spad_count().
  231. * @spad_read: See ntb_spad_read().
  232. * @spad_write: See ntb_spad_write().
  233. * @peer_spad_addr: See ntb_peer_spad_addr().
  234. * @peer_spad_read: See ntb_peer_spad_read().
  235. * @peer_spad_write: See ntb_peer_spad_write().
  236. * @msg_count: See ntb_msg_count().
  237. * @msg_inbits: See ntb_msg_inbits().
  238. * @msg_outbits: See ntb_msg_outbits().
  239. * @msg_read_sts: See ntb_msg_read_sts().
  240. * @msg_clear_sts: See ntb_msg_clear_sts().
  241. * @msg_set_mask: See ntb_msg_set_mask().
  242. * @msg_clear_mask: See ntb_msg_clear_mask().
  243. * @msg_read: See ntb_msg_read().
  244. * @peer_msg_write: See ntb_peer_msg_write().
  245. */
  246. struct ntb_dev_ops {
  247. int (*port_number)(struct ntb_dev *ntb);
  248. int (*peer_port_count)(struct ntb_dev *ntb);
  249. int (*peer_port_number)(struct ntb_dev *ntb, int pidx);
  250. int (*peer_port_idx)(struct ntb_dev *ntb, int port);
  251. u64 (*link_is_up)(struct ntb_dev *ntb,
  252. enum ntb_speed *speed, enum ntb_width *width);
  253. int (*link_enable)(struct ntb_dev *ntb,
  254. enum ntb_speed max_speed, enum ntb_width max_width);
  255. int (*link_disable)(struct ntb_dev *ntb);
  256. int (*mw_count)(struct ntb_dev *ntb, int pidx);
  257. int (*mw_get_align)(struct ntb_dev *ntb, int pidx, int widx,
  258. resource_size_t *addr_align,
  259. resource_size_t *size_align,
  260. resource_size_t *size_max);
  261. int (*mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
  262. dma_addr_t addr, resource_size_t size);
  263. int (*mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
  264. int (*peer_mw_count)(struct ntb_dev *ntb);
  265. int (*peer_mw_get_addr)(struct ntb_dev *ntb, int widx,
  266. phys_addr_t *base, resource_size_t *size);
  267. int (*peer_mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
  268. u64 addr, resource_size_t size);
  269. int (*peer_mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
  270. int (*db_is_unsafe)(struct ntb_dev *ntb);
  271. u64 (*db_valid_mask)(struct ntb_dev *ntb);
  272. int (*db_vector_count)(struct ntb_dev *ntb);
  273. u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
  274. u64 (*db_read)(struct ntb_dev *ntb);
  275. int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
  276. int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
  277. u64 (*db_read_mask)(struct ntb_dev *ntb);
  278. int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
  279. int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
  280. int (*peer_db_addr)(struct ntb_dev *ntb,
  281. phys_addr_t *db_addr, resource_size_t *db_size,
  282. u64 *db_data, int db_bit);
  283. u64 (*peer_db_read)(struct ntb_dev *ntb);
  284. int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
  285. int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
  286. u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
  287. int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
  288. int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
  289. int (*spad_is_unsafe)(struct ntb_dev *ntb);
  290. int (*spad_count)(struct ntb_dev *ntb);
  291. u32 (*spad_read)(struct ntb_dev *ntb, int sidx);
  292. int (*spad_write)(struct ntb_dev *ntb, int sidx, u32 val);
  293. int (*peer_spad_addr)(struct ntb_dev *ntb, int pidx, int sidx,
  294. phys_addr_t *spad_addr);
  295. u32 (*peer_spad_read)(struct ntb_dev *ntb, int pidx, int sidx);
  296. int (*peer_spad_write)(struct ntb_dev *ntb, int pidx, int sidx,
  297. u32 val);
  298. int (*msg_count)(struct ntb_dev *ntb);
  299. u64 (*msg_inbits)(struct ntb_dev *ntb);
  300. u64 (*msg_outbits)(struct ntb_dev *ntb);
  301. u64 (*msg_read_sts)(struct ntb_dev *ntb);
  302. int (*msg_clear_sts)(struct ntb_dev *ntb, u64 sts_bits);
  303. int (*msg_set_mask)(struct ntb_dev *ntb, u64 mask_bits);
  304. int (*msg_clear_mask)(struct ntb_dev *ntb, u64 mask_bits);
  305. u32 (*msg_read)(struct ntb_dev *ntb, int *pidx, int midx);
  306. int (*peer_msg_write)(struct ntb_dev *ntb, int pidx, int midx, u32 msg);
  307. };
  308. static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
  309. {
  310. /* commented callbacks are not required: */
  311. return
  312. /* Port operations are required for multiport devices */
  313. !ops->peer_port_count == !ops->port_number &&
  314. !ops->peer_port_number == !ops->port_number &&
  315. !ops->peer_port_idx == !ops->port_number &&
  316. /* Link operations are required */
  317. ops->link_is_up &&
  318. ops->link_enable &&
  319. ops->link_disable &&
  320. /* One or both MW interfaces should be developed */
  321. ops->mw_count &&
  322. ops->mw_get_align &&
  323. (ops->mw_set_trans ||
  324. ops->peer_mw_set_trans) &&
  325. /* ops->mw_clear_trans && */
  326. ops->peer_mw_count &&
  327. ops->peer_mw_get_addr &&
  328. /* ops->peer_mw_clear_trans && */
  329. /* Doorbell operations are mostly required */
  330. /* ops->db_is_unsafe && */
  331. ops->db_valid_mask &&
  332. /* both set, or both unset */
  333. (!ops->db_vector_count == !ops->db_vector_mask) &&
  334. ops->db_read &&
  335. /* ops->db_set && */
  336. ops->db_clear &&
  337. /* ops->db_read_mask && */
  338. ops->db_set_mask &&
  339. ops->db_clear_mask &&
  340. /* ops->peer_db_addr && */
  341. /* ops->peer_db_read && */
  342. ops->peer_db_set &&
  343. /* ops->peer_db_clear && */
  344. /* ops->peer_db_read_mask && */
  345. /* ops->peer_db_set_mask && */
  346. /* ops->peer_db_clear_mask && */
  347. /* Scrachpads interface is optional */
  348. /* !ops->spad_is_unsafe == !ops->spad_count && */
  349. !ops->spad_read == !ops->spad_count &&
  350. !ops->spad_write == !ops->spad_count &&
  351. /* !ops->peer_spad_addr == !ops->spad_count && */
  352. /* !ops->peer_spad_read == !ops->spad_count && */
  353. !ops->peer_spad_write == !ops->spad_count &&
  354. /* Messaging interface is optional */
  355. !ops->msg_inbits == !ops->msg_count &&
  356. !ops->msg_outbits == !ops->msg_count &&
  357. !ops->msg_read_sts == !ops->msg_count &&
  358. !ops->msg_clear_sts == !ops->msg_count &&
  359. /* !ops->msg_set_mask == !ops->msg_count && */
  360. /* !ops->msg_clear_mask == !ops->msg_count && */
  361. !ops->msg_read == !ops->msg_count &&
  362. !ops->peer_msg_write == !ops->msg_count &&
  363. 1;
  364. }
  365. /**
  366. * struct ntb_client - client interested in ntb devices
  367. * @drv: Linux driver object.
  368. * @ops: See &ntb_client_ops.
  369. */
  370. struct ntb_client {
  371. struct device_driver drv;
  372. const struct ntb_client_ops ops;
  373. };
  374. #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
  375. /**
  376. * struct ntb_dev - ntb device
  377. * @dev: Linux device object.
  378. * @pdev: PCI device entry of the ntb.
  379. * @topo: Detected topology of the ntb.
  380. * @ops: See &ntb_dev_ops.
  381. * @ctx: See &ntb_ctx_ops.
  382. * @ctx_ops: See &ntb_ctx_ops.
  383. */
  384. struct ntb_dev {
  385. struct device dev;
  386. struct pci_dev *pdev;
  387. enum ntb_topo topo;
  388. const struct ntb_dev_ops *ops;
  389. void *ctx;
  390. const struct ntb_ctx_ops *ctx_ops;
  391. /* private: */
  392. /* synchronize setting, clearing, and calling ctx_ops */
  393. spinlock_t ctx_lock;
  394. /* block unregister until device is fully released */
  395. struct completion released;
  396. #ifdef CONFIG_NTB_MSI
  397. struct ntb_msi *msi;
  398. #endif
  399. };
  400. #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
  401. /**
  402. * ntb_register_client() - register a client for interest in ntb devices
  403. * @client: Client context.
  404. *
  405. * The client will be added to the list of clients interested in ntb devices.
  406. * The client will be notified of any ntb devices that are not already
  407. * associated with a client, or if ntb devices are registered later.
  408. *
  409. * Return: Zero if the client is registered, otherwise an error number.
  410. */
  411. #define ntb_register_client(client) \
  412. __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
  413. int __ntb_register_client(struct ntb_client *client, struct module *mod,
  414. const char *mod_name);
  415. /**
  416. * ntb_unregister_client() - unregister a client for interest in ntb devices
  417. * @client: Client context.
  418. *
  419. * The client will be removed from the list of clients interested in ntb
  420. * devices. If any ntb devices are associated with the client, the client will
  421. * be notified to remove those devices.
  422. */
  423. void ntb_unregister_client(struct ntb_client *client);
  424. #define module_ntb_client(__ntb_client) \
  425. module_driver(__ntb_client, ntb_register_client, \
  426. ntb_unregister_client)
  427. /**
  428. * ntb_register_device() - register a ntb device
  429. * @ntb: NTB device context.
  430. *
  431. * The device will be added to the list of ntb devices. If any clients are
  432. * interested in ntb devices, each client will be notified of the ntb device,
  433. * until at most one client accepts the device.
  434. *
  435. * Return: Zero if the device is registered, otherwise an error number.
  436. */
  437. int ntb_register_device(struct ntb_dev *ntb);
  438. /**
  439. * ntb_unregister_device() - unregister a ntb device
  440. * @ntb: NTB device context.
  441. *
  442. * The device will be removed from the list of ntb devices. If the ntb device
  443. * is associated with a client, the client will be notified to remove the
  444. * device.
  445. */
  446. void ntb_unregister_device(struct ntb_dev *ntb);
  447. /**
  448. * ntb_set_ctx() - associate a driver context with an ntb device
  449. * @ntb: NTB device context.
  450. * @ctx: Driver context.
  451. * @ctx_ops: Driver context operations.
  452. *
  453. * Associate a driver context and operations with a ntb device. The context is
  454. * provided by the client driver, and the driver may associate a different
  455. * context with each ntb device.
  456. *
  457. * Return: Zero if the context is associated, otherwise an error number.
  458. */
  459. int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
  460. const struct ntb_ctx_ops *ctx_ops);
  461. /**
  462. * ntb_clear_ctx() - disassociate any driver context from an ntb device
  463. * @ntb: NTB device context.
  464. *
  465. * Clear any association that may exist between a driver context and the ntb
  466. * device.
  467. */
  468. void ntb_clear_ctx(struct ntb_dev *ntb);
  469. /**
  470. * ntb_link_event() - notify driver context of a change in link status
  471. * @ntb: NTB device context.
  472. *
  473. * Notify the driver context that the link status may have changed. The driver
  474. * should call ntb_link_is_up() to get the current status.
  475. */
  476. void ntb_link_event(struct ntb_dev *ntb);
  477. /**
  478. * ntb_db_event() - notify driver context of a doorbell event
  479. * @ntb: NTB device context.
  480. * @vector: Interrupt vector number.
  481. *
  482. * Notify the driver context of a doorbell event. If hardware supports
  483. * multiple interrupt vectors for doorbells, the vector number indicates which
  484. * vector received the interrupt. The vector number is relative to the first
  485. * vector used for doorbells, starting at zero, and must be less than
  486. * ntb_db_vector_count(). The driver may call ntb_db_read() to check which
  487. * doorbell bits need service, and ntb_db_vector_mask() to determine which of
  488. * those bits are associated with the vector number.
  489. */
  490. void ntb_db_event(struct ntb_dev *ntb, int vector);
  491. /**
  492. * ntb_msg_event() - notify driver context of a message event
  493. * @ntb: NTB device context.
  494. *
  495. * Notify the driver context of a message event. If hardware supports
  496. * message registers, this event indicates, that a new message arrived in
  497. * some incoming message register or last sent message couldn't be delivered.
  498. * The events can be masked/unmasked by the methods ntb_msg_set_mask() and
  499. * ntb_msg_clear_mask().
  500. */
  501. void ntb_msg_event(struct ntb_dev *ntb);
  502. /**
  503. * ntb_default_port_number() - get the default local port number
  504. * @ntb: NTB device context.
  505. *
  506. * If hardware driver doesn't specify port_number() callback method, the NTB
  507. * is considered with just two ports. So this method returns default local
  508. * port number in compliance with topology.
  509. *
  510. * NOTE Don't call this method directly. The ntb_port_number() function should
  511. * be used instead.
  512. *
  513. * Return: the default local port number
  514. */
  515. int ntb_default_port_number(struct ntb_dev *ntb);
  516. /**
  517. * ntb_default_port_count() - get the default number of peer device ports
  518. * @ntb: NTB device context.
  519. *
  520. * By default hardware driver supports just one peer device.
  521. *
  522. * NOTE Don't call this method directly. The ntb_peer_port_count() function
  523. * should be used instead.
  524. *
  525. * Return: the default number of peer ports
  526. */
  527. int ntb_default_peer_port_count(struct ntb_dev *ntb);
  528. /**
  529. * ntb_default_peer_port_number() - get the default peer port by given index
  530. * @ntb: NTB device context.
  531. * @idx: Peer port index (should not differ from zero).
  532. *
  533. * By default hardware driver supports just one peer device, so this method
  534. * shall return the corresponding value from enum ntb_default_port.
  535. *
  536. * NOTE Don't call this method directly. The ntb_peer_port_number() function
  537. * should be used instead.
  538. *
  539. * Return: the peer device port or negative value indicating an error
  540. */
  541. int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx);
  542. /**
  543. * ntb_default_peer_port_idx() - get the default peer device port index by
  544. * given port number
  545. * @ntb: NTB device context.
  546. * @port: Peer port number (should be one of enum ntb_default_port).
  547. *
  548. * By default hardware driver supports just one peer device, so while
  549. * specified port-argument indicates peer port from enum ntb_default_port,
  550. * the return value shall be zero.
  551. *
  552. * NOTE Don't call this method directly. The ntb_peer_port_idx() function
  553. * should be used instead.
  554. *
  555. * Return: the peer port index or negative value indicating an error
  556. */
  557. int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port);
  558. /**
  559. * ntb_port_number() - get the local port number
  560. * @ntb: NTB device context.
  561. *
  562. * Hardware must support at least simple two-ports ntb connection
  563. *
  564. * Return: the local port number
  565. */
  566. static inline int ntb_port_number(struct ntb_dev *ntb)
  567. {
  568. if (!ntb->ops->port_number)
  569. return ntb_default_port_number(ntb);
  570. return ntb->ops->port_number(ntb);
  571. }
  572. /**
  573. * ntb_peer_port_count() - get the number of peer device ports
  574. * @ntb: NTB device context.
  575. *
  576. * Hardware may support an access to memory of several remote domains
  577. * over multi-port NTB devices. This method returns the number of peers,
  578. * local device can have shared memory with.
  579. *
  580. * Return: the number of peer ports
  581. */
  582. static inline int ntb_peer_port_count(struct ntb_dev *ntb)
  583. {
  584. if (!ntb->ops->peer_port_count)
  585. return ntb_default_peer_port_count(ntb);
  586. return ntb->ops->peer_port_count(ntb);
  587. }
  588. /**
  589. * ntb_peer_port_number() - get the peer port by given index
  590. * @ntb: NTB device context.
  591. * @pidx: Peer port index.
  592. *
  593. * Peer ports are continuously enumerated by NTB API logic, so this method
  594. * lets to retrieve port real number by its index.
  595. *
  596. * Return: the peer device port or negative value indicating an error
  597. */
  598. static inline int ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
  599. {
  600. if (!ntb->ops->peer_port_number)
  601. return ntb_default_peer_port_number(ntb, pidx);
  602. return ntb->ops->peer_port_number(ntb, pidx);
  603. }
  604. /**
  605. * ntb_logical_port_number() - get the logical port number of the local port
  606. * @ntb: NTB device context.
  607. *
  608. * The Logical Port Number is defined to be a unique number for each
  609. * port starting from zero through to the number of ports minus one.
  610. * This is in contrast to the Port Number where each port can be assigned
  611. * any unique physical number by the hardware.
  612. *
  613. * The logical port number is useful for calculating the resource indexes
  614. * used by peers.
  615. *
  616. * Return: the logical port number or negative value indicating an error
  617. */
  618. static inline int ntb_logical_port_number(struct ntb_dev *ntb)
  619. {
  620. int lport = ntb_port_number(ntb);
  621. int pidx;
  622. if (lport < 0)
  623. return lport;
  624. for (pidx = 0; pidx < ntb_peer_port_count(ntb); pidx++)
  625. if (lport <= ntb_peer_port_number(ntb, pidx))
  626. return pidx;
  627. return pidx;
  628. }
  629. /**
  630. * ntb_peer_logical_port_number() - get the logical peer port by given index
  631. * @ntb: NTB device context.
  632. * @pidx: Peer port index.
  633. *
  634. * The Logical Port Number is defined to be a unique number for each
  635. * port starting from zero through to the number of ports minus one.
  636. * This is in contrast to the Port Number where each port can be assigned
  637. * any unique physical number by the hardware.
  638. *
  639. * The logical port number is useful for calculating the resource indexes
  640. * used by peers.
  641. *
  642. * Return: the peer's logical port number or negative value indicating an error
  643. */
  644. static inline int ntb_peer_logical_port_number(struct ntb_dev *ntb, int pidx)
  645. {
  646. if (ntb_peer_port_number(ntb, pidx) < ntb_port_number(ntb))
  647. return pidx;
  648. else
  649. return pidx + 1;
  650. }
  651. /**
  652. * ntb_peer_port_idx() - get the peer device port index by given port number
  653. * @ntb: NTB device context.
  654. * @port: Peer port number.
  655. *
  656. * Inverse operation of ntb_peer_port_number(), so one can get port index
  657. * by specified port number.
  658. *
  659. * Return: the peer port index or negative value indicating an error
  660. */
  661. static inline int ntb_peer_port_idx(struct ntb_dev *ntb, int port)
  662. {
  663. if (!ntb->ops->peer_port_idx)
  664. return ntb_default_peer_port_idx(ntb, port);
  665. return ntb->ops->peer_port_idx(ntb, port);
  666. }
  667. /**
  668. * ntb_link_is_up() - get the current ntb link state
  669. * @ntb: NTB device context.
  670. * @speed: OUT - The link speed expressed as PCIe generation number.
  671. * @width: OUT - The link width expressed as the number of PCIe lanes.
  672. *
  673. * Get the current state of the ntb link. It is recommended to query the link
  674. * state once after every link event. It is safe to query the link state in
  675. * the context of the link event callback.
  676. *
  677. * Return: bitfield of indexed ports link state: bit is set/cleared if the
  678. * link is up/down respectively.
  679. */
  680. static inline u64 ntb_link_is_up(struct ntb_dev *ntb,
  681. enum ntb_speed *speed, enum ntb_width *width)
  682. {
  683. return ntb->ops->link_is_up(ntb, speed, width);
  684. }
  685. /**
  686. * ntb_link_enable() - enable the local port ntb connection
  687. * @ntb: NTB device context.
  688. * @max_speed: The maximum link speed expressed as PCIe generation number.
  689. * @max_width: The maximum link width expressed as the number of PCIe lanes.
  690. *
  691. * Enable the NTB/PCIe link on the local or remote (for bridge-to-bridge
  692. * topology) side of the bridge. If it's supported the ntb device should train
  693. * the link to its maximum speed and width, or the requested speed and width,
  694. * whichever is smaller. Some hardware doesn't support PCIe link training, so
  695. * the last two arguments will be ignored then.
  696. *
  697. * Return: Zero on success, otherwise an error number.
  698. */
  699. static inline int ntb_link_enable(struct ntb_dev *ntb,
  700. enum ntb_speed max_speed,
  701. enum ntb_width max_width)
  702. {
  703. return ntb->ops->link_enable(ntb, max_speed, max_width);
  704. }
  705. /**
  706. * ntb_link_disable() - disable the local port ntb connection
  707. * @ntb: NTB device context.
  708. *
  709. * Disable the link on the local or remote (for b2b topology) of the ntb.
  710. * The ntb device should disable the link. Returning from this call must
  711. * indicate that a barrier has passed, though with no more writes may pass in
  712. * either direction across the link, except if this call returns an error
  713. * number.
  714. *
  715. * Return: Zero on success, otherwise an error number.
  716. */
  717. static inline int ntb_link_disable(struct ntb_dev *ntb)
  718. {
  719. return ntb->ops->link_disable(ntb);
  720. }
  721. /**
  722. * ntb_mw_count() - get the number of inbound memory windows, which could
  723. * be created for a specified peer device
  724. * @ntb: NTB device context.
  725. * @pidx: Port index of peer device.
  726. *
  727. * Hardware and topology may support a different number of memory windows.
  728. * Moreover different peer devices can support different number of memory
  729. * windows. Simply speaking this method returns the number of possible inbound
  730. * memory windows to share with specified peer device. Note: this may return
  731. * zero if the link is not up yet.
  732. *
  733. * Return: the number of memory windows.
  734. */
  735. static inline int ntb_mw_count(struct ntb_dev *ntb, int pidx)
  736. {
  737. return ntb->ops->mw_count(ntb, pidx);
  738. }
  739. /**
  740. * ntb_mw_get_align() - get the restriction parameters of inbound memory window
  741. * @ntb: NTB device context.
  742. * @pidx: Port index of peer device.
  743. * @widx: Memory window index.
  744. * @addr_align: OUT - the base alignment for translating the memory window
  745. * @size_align: OUT - the size alignment for translating the memory window
  746. * @size_max: OUT - the maximum size of the memory window
  747. *
  748. * Get the alignments of an inbound memory window with specified index.
  749. * NULL may be given for any output parameter if the value is not needed.
  750. * The alignment and size parameters may be used for allocation of proper
  751. * shared memory. Note: this must only be called when the link is up.
  752. *
  753. * Return: Zero on success, otherwise a negative error number.
  754. */
  755. static inline int ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
  756. resource_size_t *addr_align,
  757. resource_size_t *size_align,
  758. resource_size_t *size_max)
  759. {
  760. if (!(ntb_link_is_up(ntb, NULL, NULL) & BIT_ULL(pidx)))
  761. return -ENOTCONN;
  762. return ntb->ops->mw_get_align(ntb, pidx, widx, addr_align, size_align,
  763. size_max);
  764. }
  765. /**
  766. * ntb_mw_set_trans() - set the translation of an inbound memory window
  767. * @ntb: NTB device context.
  768. * @pidx: Port index of peer device.
  769. * @widx: Memory window index.
  770. * @addr: The dma address of local memory to expose to the peer.
  771. * @size: The size of the local memory to expose to the peer.
  772. *
  773. * Set the translation of a memory window. The peer may access local memory
  774. * through the window starting at the address, up to the size. The address
  775. * and size must be aligned in compliance with restrictions of
  776. * ntb_mw_get_align(). The region size should not exceed the size_max parameter
  777. * of that method.
  778. *
  779. * This method may not be implemented due to the hardware specific memory
  780. * windows interface.
  781. *
  782. * Return: Zero on success, otherwise an error number.
  783. */
  784. static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
  785. dma_addr_t addr, resource_size_t size)
  786. {
  787. if (!ntb->ops->mw_set_trans)
  788. return 0;
  789. return ntb->ops->mw_set_trans(ntb, pidx, widx, addr, size);
  790. }
  791. /**
  792. * ntb_mw_clear_trans() - clear the translation address of an inbound memory
  793. * window
  794. * @ntb: NTB device context.
  795. * @pidx: Port index of peer device.
  796. * @widx: Memory window index.
  797. *
  798. * Clear the translation of an inbound memory window. The peer may no longer
  799. * access local memory through the window.
  800. *
  801. * Return: Zero on success, otherwise an error number.
  802. */
  803. static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int pidx, int widx)
  804. {
  805. if (!ntb->ops->mw_clear_trans)
  806. return ntb_mw_set_trans(ntb, pidx, widx, 0, 0);
  807. return ntb->ops->mw_clear_trans(ntb, pidx, widx);
  808. }
  809. /**
  810. * ntb_peer_mw_count() - get the number of outbound memory windows, which could
  811. * be mapped to access a shared memory
  812. * @ntb: NTB device context.
  813. *
  814. * Hardware and topology may support a different number of memory windows.
  815. * This method returns the number of outbound memory windows supported by
  816. * local device.
  817. *
  818. * Return: the number of memory windows.
  819. */
  820. static inline int ntb_peer_mw_count(struct ntb_dev *ntb)
  821. {
  822. return ntb->ops->peer_mw_count(ntb);
  823. }
  824. /**
  825. * ntb_peer_mw_get_addr() - get map address of an outbound memory window
  826. * @ntb: NTB device context.
  827. * @widx: Memory window index (within ntb_peer_mw_count() return value).
  828. * @base: OUT - the base address of mapping region.
  829. * @size: OUT - the size of mapping region.
  830. *
  831. * Get base and size of memory region to map. NULL may be given for any output
  832. * parameter if the value is not needed. The base and size may be used for
  833. * mapping the memory window, to access the peer memory.
  834. *
  835. * Return: Zero on success, otherwise a negative error number.
  836. */
  837. static inline int ntb_peer_mw_get_addr(struct ntb_dev *ntb, int widx,
  838. phys_addr_t *base, resource_size_t *size)
  839. {
  840. return ntb->ops->peer_mw_get_addr(ntb, widx, base, size);
  841. }
  842. /**
  843. * ntb_peer_mw_set_trans() - set a translation address of a memory window
  844. * retrieved from a peer device
  845. * @ntb: NTB device context.
  846. * @pidx: Port index of peer device the translation address received from.
  847. * @widx: Memory window index.
  848. * @addr: The dma address of the shared memory to access.
  849. * @size: The size of the shared memory to access.
  850. *
  851. * Set the translation of an outbound memory window. The local device may
  852. * access shared memory allocated by a peer device sent the address.
  853. *
  854. * This method may not be implemented due to the hardware specific memory
  855. * windows interface, so a translation address can be only set on the side,
  856. * where shared memory (inbound memory windows) is allocated.
  857. *
  858. * Return: Zero on success, otherwise an error number.
  859. */
  860. static inline int ntb_peer_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
  861. u64 addr, resource_size_t size)
  862. {
  863. if (!ntb->ops->peer_mw_set_trans)
  864. return 0;
  865. return ntb->ops->peer_mw_set_trans(ntb, pidx, widx, addr, size);
  866. }
  867. /**
  868. * ntb_peer_mw_clear_trans() - clear the translation address of an outbound
  869. * memory window
  870. * @ntb: NTB device context.
  871. * @pidx: Port index of peer device.
  872. * @widx: Memory window index.
  873. *
  874. * Clear the translation of a outbound memory window. The local device may no
  875. * longer access a shared memory through the window.
  876. *
  877. * This method may not be implemented due to the hardware specific memory
  878. * windows interface.
  879. *
  880. * Return: Zero on success, otherwise an error number.
  881. */
  882. static inline int ntb_peer_mw_clear_trans(struct ntb_dev *ntb, int pidx,
  883. int widx)
  884. {
  885. if (!ntb->ops->peer_mw_clear_trans)
  886. return ntb_peer_mw_set_trans(ntb, pidx, widx, 0, 0);
  887. return ntb->ops->peer_mw_clear_trans(ntb, pidx, widx);
  888. }
  889. /**
  890. * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
  891. * @ntb: NTB device context.
  892. *
  893. * It is possible for some ntb hardware to be affected by errata. Hardware
  894. * drivers can advise clients to avoid using doorbells. Clients may ignore
  895. * this advice, though caution is recommended.
  896. *
  897. * Return: Zero if it is safe to use doorbells, or One if it is not safe.
  898. */
  899. static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
  900. {
  901. if (!ntb->ops->db_is_unsafe)
  902. return 0;
  903. return ntb->ops->db_is_unsafe(ntb);
  904. }
  905. /**
  906. * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
  907. * @ntb: NTB device context.
  908. *
  909. * Hardware may support different number or arrangement of doorbell bits.
  910. *
  911. * Return: A mask of doorbell bits supported by the ntb.
  912. */
  913. static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
  914. {
  915. return ntb->ops->db_valid_mask(ntb);
  916. }
  917. /**
  918. * ntb_db_vector_count() - get the number of doorbell interrupt vectors
  919. * @ntb: NTB device context.
  920. *
  921. * Hardware may support different number of interrupt vectors.
  922. *
  923. * Return: The number of doorbell interrupt vectors.
  924. */
  925. static inline int ntb_db_vector_count(struct ntb_dev *ntb)
  926. {
  927. if (!ntb->ops->db_vector_count)
  928. return 1;
  929. return ntb->ops->db_vector_count(ntb);
  930. }
  931. /**
  932. * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
  933. * @ntb: NTB device context.
  934. * @vector: Doorbell vector number.
  935. *
  936. * Each interrupt vector may have a different number or arrangement of bits.
  937. *
  938. * Return: A mask of doorbell bits serviced by a vector.
  939. */
  940. static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
  941. {
  942. if (!ntb->ops->db_vector_mask)
  943. return ntb_db_valid_mask(ntb);
  944. return ntb->ops->db_vector_mask(ntb, vector);
  945. }
  946. /**
  947. * ntb_db_read() - read the local doorbell register
  948. * @ntb: NTB device context.
  949. *
  950. * Read the local doorbell register, and return the bits that are set.
  951. *
  952. * Return: The bits currently set in the local doorbell register.
  953. */
  954. static inline u64 ntb_db_read(struct ntb_dev *ntb)
  955. {
  956. return ntb->ops->db_read(ntb);
  957. }
  958. /**
  959. * ntb_db_set() - set bits in the local doorbell register
  960. * @ntb: NTB device context.
  961. * @db_bits: Doorbell bits to set.
  962. *
  963. * Set bits in the local doorbell register, which may generate a local doorbell
  964. * interrupt. Bits that were already set must remain set.
  965. *
  966. * This is unusual, and hardware may not support it.
  967. *
  968. * Return: Zero on success, otherwise an error number.
  969. */
  970. static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
  971. {
  972. if (!ntb->ops->db_set)
  973. return -EINVAL;
  974. return ntb->ops->db_set(ntb, db_bits);
  975. }
  976. /**
  977. * ntb_db_clear() - clear bits in the local doorbell register
  978. * @ntb: NTB device context.
  979. * @db_bits: Doorbell bits to clear.
  980. *
  981. * Clear bits in the local doorbell register, arming the bits for the next
  982. * doorbell.
  983. *
  984. * Return: Zero on success, otherwise an error number.
  985. */
  986. static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  987. {
  988. return ntb->ops->db_clear(ntb, db_bits);
  989. }
  990. /**
  991. * ntb_db_read_mask() - read the local doorbell mask
  992. * @ntb: NTB device context.
  993. *
  994. * Read the local doorbell mask register, and return the bits that are set.
  995. *
  996. * This is unusual, though hardware is likely to support it.
  997. *
  998. * Return: The bits currently set in the local doorbell mask register.
  999. */
  1000. static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
  1001. {
  1002. if (!ntb->ops->db_read_mask)
  1003. return 0;
  1004. return ntb->ops->db_read_mask(ntb);
  1005. }
  1006. /**
  1007. * ntb_db_set_mask() - set bits in the local doorbell mask
  1008. * @ntb: NTB device context.
  1009. * @db_bits: Doorbell mask bits to set.
  1010. *
  1011. * Set bits in the local doorbell mask register, preventing doorbell interrupts
  1012. * from being generated for those doorbell bits. Bits that were already set
  1013. * must remain set.
  1014. *
  1015. * Return: Zero on success, otherwise an error number.
  1016. */
  1017. static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  1018. {
  1019. return ntb->ops->db_set_mask(ntb, db_bits);
  1020. }
  1021. /**
  1022. * ntb_db_clear_mask() - clear bits in the local doorbell mask
  1023. * @ntb: NTB device context.
  1024. * @db_bits: Doorbell bits to clear.
  1025. *
  1026. * Clear bits in the local doorbell mask register, allowing doorbell interrupts
  1027. * from being generated for those doorbell bits. If a doorbell bit is already
  1028. * set at the time the mask is cleared, and the corresponding mask bit is
  1029. * changed from set to clear, then the ntb driver must ensure that
  1030. * ntb_db_event() is called. If the hardware does not generate the interrupt
  1031. * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
  1032. *
  1033. * Return: Zero on success, otherwise an error number.
  1034. */
  1035. static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  1036. {
  1037. return ntb->ops->db_clear_mask(ntb, db_bits);
  1038. }
  1039. /**
  1040. * ntb_peer_db_addr() - address and size of the peer doorbell register
  1041. * @ntb: NTB device context.
  1042. * @db_addr: OUT - The address of the peer doorbell register.
  1043. * @db_size: OUT - The number of bytes to write the peer doorbell register.
  1044. * @db_data: OUT - The data of peer doorbell register
  1045. * @db_bit: door bell bit number
  1046. *
  1047. * Return the address of the peer doorbell register. This may be used, for
  1048. * example, by drivers that offload memory copy operations to a dma engine.
  1049. * The drivers may wish to ring the peer doorbell at the completion of memory
  1050. * copy operations. For efficiency, and to simplify ordering of operations
  1051. * between the dma memory copies and the ringing doorbell, the driver may
  1052. * append one additional dma memory copy with the doorbell register as the
  1053. * destination, after the memory copy operations.
  1054. *
  1055. * Return: Zero on success, otherwise an error number.
  1056. */
  1057. static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
  1058. phys_addr_t *db_addr,
  1059. resource_size_t *db_size,
  1060. u64 *db_data, int db_bit)
  1061. {
  1062. if (!ntb->ops->peer_db_addr)
  1063. return -EINVAL;
  1064. return ntb->ops->peer_db_addr(ntb, db_addr, db_size, db_data, db_bit);
  1065. }
  1066. /**
  1067. * ntb_peer_db_read() - read the peer doorbell register
  1068. * @ntb: NTB device context.
  1069. *
  1070. * Read the peer doorbell register, and return the bits that are set.
  1071. *
  1072. * This is unusual, and hardware may not support it.
  1073. *
  1074. * Return: The bits currently set in the peer doorbell register.
  1075. */
  1076. static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
  1077. {
  1078. if (!ntb->ops->peer_db_read)
  1079. return 0;
  1080. return ntb->ops->peer_db_read(ntb);
  1081. }
  1082. /**
  1083. * ntb_peer_db_set() - set bits in the peer doorbell register
  1084. * @ntb: NTB device context.
  1085. * @db_bits: Doorbell bits to set.
  1086. *
  1087. * Set bits in the peer doorbell register, which may generate a peer doorbell
  1088. * interrupt. Bits that were already set must remain set.
  1089. *
  1090. * Return: Zero on success, otherwise an error number.
  1091. */
  1092. static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  1093. {
  1094. return ntb->ops->peer_db_set(ntb, db_bits);
  1095. }
  1096. /**
  1097. * ntb_peer_db_clear() - clear bits in the peer doorbell register
  1098. * @ntb: NTB device context.
  1099. * @db_bits: Doorbell bits to clear.
  1100. *
  1101. * Clear bits in the peer doorbell register, arming the bits for the next
  1102. * doorbell.
  1103. *
  1104. * This is unusual, and hardware may not support it.
  1105. *
  1106. * Return: Zero on success, otherwise an error number.
  1107. */
  1108. static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
  1109. {
  1110. if (!ntb->ops->db_clear)
  1111. return -EINVAL;
  1112. return ntb->ops->peer_db_clear(ntb, db_bits);
  1113. }
  1114. /**
  1115. * ntb_peer_db_read_mask() - read the peer doorbell mask
  1116. * @ntb: NTB device context.
  1117. *
  1118. * Read the peer doorbell mask register, and return the bits that are set.
  1119. *
  1120. * This is unusual, and hardware may not support it.
  1121. *
  1122. * Return: The bits currently set in the peer doorbell mask register.
  1123. */
  1124. static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
  1125. {
  1126. if (!ntb->ops->db_read_mask)
  1127. return 0;
  1128. return ntb->ops->peer_db_read_mask(ntb);
  1129. }
  1130. /**
  1131. * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
  1132. * @ntb: NTB device context.
  1133. * @db_bits: Doorbell mask bits to set.
  1134. *
  1135. * Set bits in the peer doorbell mask register, preventing doorbell interrupts
  1136. * from being generated for those doorbell bits. Bits that were already set
  1137. * must remain set.
  1138. *
  1139. * This is unusual, and hardware may not support it.
  1140. *
  1141. * Return: Zero on success, otherwise an error number.
  1142. */
  1143. static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  1144. {
  1145. if (!ntb->ops->db_set_mask)
  1146. return -EINVAL;
  1147. return ntb->ops->peer_db_set_mask(ntb, db_bits);
  1148. }
  1149. /**
  1150. * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
  1151. * @ntb: NTB device context.
  1152. * @db_bits: Doorbell bits to clear.
  1153. *
  1154. * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
  1155. * from being generated for those doorbell bits. If the hardware does not
  1156. * generate the interrupt on clearing the mask bit, then the driver should not
  1157. * implement this function!
  1158. *
  1159. * This is unusual, and hardware may not support it.
  1160. *
  1161. * Return: Zero on success, otherwise an error number.
  1162. */
  1163. static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  1164. {
  1165. if (!ntb->ops->db_clear_mask)
  1166. return -EINVAL;
  1167. return ntb->ops->peer_db_clear_mask(ntb, db_bits);
  1168. }
  1169. /**
  1170. * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
  1171. * @ntb: NTB device context.
  1172. *
  1173. * It is possible for some ntb hardware to be affected by errata. Hardware
  1174. * drivers can advise clients to avoid using scratchpads. Clients may ignore
  1175. * this advice, though caution is recommended.
  1176. *
  1177. * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
  1178. */
  1179. static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
  1180. {
  1181. if (!ntb->ops->spad_is_unsafe)
  1182. return 0;
  1183. return ntb->ops->spad_is_unsafe(ntb);
  1184. }
  1185. /**
  1186. * ntb_spad_count() - get the number of scratchpads
  1187. * @ntb: NTB device context.
  1188. *
  1189. * Hardware and topology may support a different number of scratchpads.
  1190. * Although it must be the same for all ports per NTB device.
  1191. *
  1192. * Return: the number of scratchpads.
  1193. */
  1194. static inline int ntb_spad_count(struct ntb_dev *ntb)
  1195. {
  1196. if (!ntb->ops->spad_count)
  1197. return 0;
  1198. return ntb->ops->spad_count(ntb);
  1199. }
  1200. /**
  1201. * ntb_spad_read() - read the local scratchpad register
  1202. * @ntb: NTB device context.
  1203. * @sidx: Scratchpad index.
  1204. *
  1205. * Read the local scratchpad register, and return the value.
  1206. *
  1207. * Return: The value of the local scratchpad register.
  1208. */
  1209. static inline u32 ntb_spad_read(struct ntb_dev *ntb, int sidx)
  1210. {
  1211. if (!ntb->ops->spad_read)
  1212. return ~(u32)0;
  1213. return ntb->ops->spad_read(ntb, sidx);
  1214. }
  1215. /**
  1216. * ntb_spad_write() - write the local scratchpad register
  1217. * @ntb: NTB device context.
  1218. * @sidx: Scratchpad index.
  1219. * @val: Scratchpad value.
  1220. *
  1221. * Write the value to the local scratchpad register.
  1222. *
  1223. * Return: Zero on success, otherwise an error number.
  1224. */
  1225. static inline int ntb_spad_write(struct ntb_dev *ntb, int sidx, u32 val)
  1226. {
  1227. if (!ntb->ops->spad_write)
  1228. return -EINVAL;
  1229. return ntb->ops->spad_write(ntb, sidx, val);
  1230. }
  1231. /**
  1232. * ntb_peer_spad_addr() - address of the peer scratchpad register
  1233. * @ntb: NTB device context.
  1234. * @pidx: Port index of peer device.
  1235. * @sidx: Scratchpad index.
  1236. * @spad_addr: OUT - The address of the peer scratchpad register.
  1237. *
  1238. * Return the address of the peer scratchpad register. This may be used, for
  1239. * example, by drivers that offload memory copy operations to a dma engine.
  1240. *
  1241. * Return: Zero on success, otherwise an error number.
  1242. */
  1243. static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int pidx, int sidx,
  1244. phys_addr_t *spad_addr)
  1245. {
  1246. if (!ntb->ops->peer_spad_addr)
  1247. return -EINVAL;
  1248. return ntb->ops->peer_spad_addr(ntb, pidx, sidx, spad_addr);
  1249. }
  1250. /**
  1251. * ntb_peer_spad_read() - read the peer scratchpad register
  1252. * @ntb: NTB device context.
  1253. * @pidx: Port index of peer device.
  1254. * @sidx: Scratchpad index.
  1255. *
  1256. * Read the peer scratchpad register, and return the value.
  1257. *
  1258. * Return: The value of the peer scratchpad register.
  1259. */
  1260. static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
  1261. {
  1262. if (!ntb->ops->peer_spad_read)
  1263. return ~(u32)0;
  1264. return ntb->ops->peer_spad_read(ntb, pidx, sidx);
  1265. }
  1266. /**
  1267. * ntb_peer_spad_write() - write the peer scratchpad register
  1268. * @ntb: NTB device context.
  1269. * @pidx: Port index of peer device.
  1270. * @sidx: Scratchpad index.
  1271. * @val: Scratchpad value.
  1272. *
  1273. * Write the value to the peer scratchpad register.
  1274. *
  1275. * Return: Zero on success, otherwise an error number.
  1276. */
  1277. static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int pidx, int sidx,
  1278. u32 val)
  1279. {
  1280. if (!ntb->ops->peer_spad_write)
  1281. return -EINVAL;
  1282. return ntb->ops->peer_spad_write(ntb, pidx, sidx, val);
  1283. }
  1284. /**
  1285. * ntb_msg_count() - get the number of message registers
  1286. * @ntb: NTB device context.
  1287. *
  1288. * Hardware may support a different number of message registers.
  1289. *
  1290. * Return: the number of message registers.
  1291. */
  1292. static inline int ntb_msg_count(struct ntb_dev *ntb)
  1293. {
  1294. if (!ntb->ops->msg_count)
  1295. return 0;
  1296. return ntb->ops->msg_count(ntb);
  1297. }
  1298. /**
  1299. * ntb_msg_inbits() - get a bitfield of inbound message registers status
  1300. * @ntb: NTB device context.
  1301. *
  1302. * The method returns the bitfield of status and mask registers, which related
  1303. * to inbound message registers.
  1304. *
  1305. * Return: bitfield of inbound message registers.
  1306. */
  1307. static inline u64 ntb_msg_inbits(struct ntb_dev *ntb)
  1308. {
  1309. if (!ntb->ops->msg_inbits)
  1310. return 0;
  1311. return ntb->ops->msg_inbits(ntb);
  1312. }
  1313. /**
  1314. * ntb_msg_outbits() - get a bitfield of outbound message registers status
  1315. * @ntb: NTB device context.
  1316. *
  1317. * The method returns the bitfield of status and mask registers, which related
  1318. * to outbound message registers.
  1319. *
  1320. * Return: bitfield of outbound message registers.
  1321. */
  1322. static inline u64 ntb_msg_outbits(struct ntb_dev *ntb)
  1323. {
  1324. if (!ntb->ops->msg_outbits)
  1325. return 0;
  1326. return ntb->ops->msg_outbits(ntb);
  1327. }
  1328. /**
  1329. * ntb_msg_read_sts() - read the message registers status
  1330. * @ntb: NTB device context.
  1331. *
  1332. * Read the status of message register. Inbound and outbound message registers
  1333. * related bits can be filtered by masks retrieved from ntb_msg_inbits() and
  1334. * ntb_msg_outbits().
  1335. *
  1336. * Return: status bits of message registers
  1337. */
  1338. static inline u64 ntb_msg_read_sts(struct ntb_dev *ntb)
  1339. {
  1340. if (!ntb->ops->msg_read_sts)
  1341. return 0;
  1342. return ntb->ops->msg_read_sts(ntb);
  1343. }
  1344. /**
  1345. * ntb_msg_clear_sts() - clear status bits of message registers
  1346. * @ntb: NTB device context.
  1347. * @sts_bits: Status bits to clear.
  1348. *
  1349. * Clear bits in the status register.
  1350. *
  1351. * Return: Zero on success, otherwise a negative error number.
  1352. */
  1353. static inline int ntb_msg_clear_sts(struct ntb_dev *ntb, u64 sts_bits)
  1354. {
  1355. if (!ntb->ops->msg_clear_sts)
  1356. return -EINVAL;
  1357. return ntb->ops->msg_clear_sts(ntb, sts_bits);
  1358. }
  1359. /**
  1360. * ntb_msg_set_mask() - set mask of message register status bits
  1361. * @ntb: NTB device context.
  1362. * @mask_bits: Mask bits.
  1363. *
  1364. * Mask the message registers status bits from raising the message event.
  1365. *
  1366. * Return: Zero on success, otherwise a negative error number.
  1367. */
  1368. static inline int ntb_msg_set_mask(struct ntb_dev *ntb, u64 mask_bits)
  1369. {
  1370. if (!ntb->ops->msg_set_mask)
  1371. return -EINVAL;
  1372. return ntb->ops->msg_set_mask(ntb, mask_bits);
  1373. }
  1374. /**
  1375. * ntb_msg_clear_mask() - clear message registers mask
  1376. * @ntb: NTB device context.
  1377. * @mask_bits: Mask bits to clear.
  1378. *
  1379. * Clear bits in the message events mask register.
  1380. *
  1381. * Return: Zero on success, otherwise a negative error number.
  1382. */
  1383. static inline int ntb_msg_clear_mask(struct ntb_dev *ntb, u64 mask_bits)
  1384. {
  1385. if (!ntb->ops->msg_clear_mask)
  1386. return -EINVAL;
  1387. return ntb->ops->msg_clear_mask(ntb, mask_bits);
  1388. }
  1389. /**
  1390. * ntb_msg_read() - read inbound message register with specified index
  1391. * @ntb: NTB device context.
  1392. * @pidx: OUT - Port index of peer device a message retrieved from
  1393. * @midx: Message register index
  1394. *
  1395. * Read data from the specified message register. Source port index of a
  1396. * message is retrieved as well.
  1397. *
  1398. * Return: The value of the inbound message register.
  1399. */
  1400. static inline u32 ntb_msg_read(struct ntb_dev *ntb, int *pidx, int midx)
  1401. {
  1402. if (!ntb->ops->msg_read)
  1403. return ~(u32)0;
  1404. return ntb->ops->msg_read(ntb, pidx, midx);
  1405. }
  1406. /**
  1407. * ntb_peer_msg_write() - write data to the specified peer message register
  1408. * @ntb: NTB device context.
  1409. * @pidx: Port index of peer device a message being sent to
  1410. * @midx: Message register index
  1411. * @msg: Data to send
  1412. *
  1413. * Send data to a specified peer device using the defined message register.
  1414. * Message event can be raised if the midx registers isn't empty while
  1415. * calling this method and the corresponding interrupt isn't masked.
  1416. *
  1417. * Return: Zero on success, otherwise a negative error number.
  1418. */
  1419. static inline int ntb_peer_msg_write(struct ntb_dev *ntb, int pidx, int midx,
  1420. u32 msg)
  1421. {
  1422. if (!ntb->ops->peer_msg_write)
  1423. return -EINVAL;
  1424. return ntb->ops->peer_msg_write(ntb, pidx, midx, msg);
  1425. }
  1426. /**
  1427. * ntb_peer_resource_idx() - get a resource index for a given peer idx
  1428. * @ntb: NTB device context.
  1429. * @pidx: Peer port index.
  1430. *
  1431. * When constructing a graph of peers, each remote peer must use a different
  1432. * resource index (mw, doorbell, etc) to communicate with each other
  1433. * peer.
  1434. *
  1435. * In a two peer system, this function should always return 0 such that
  1436. * resource 0 points to the remote peer on both ports.
  1437. *
  1438. * In a 5 peer system, this function will return the following matrix
  1439. *
  1440. * pidx \ port 0 1 2 3 4
  1441. * 0 0 0 1 2 3
  1442. * 1 0 1 1 2 3
  1443. * 2 0 1 2 2 3
  1444. * 3 0 1 2 3 3
  1445. *
  1446. * For example, if this function is used to program peer's memory
  1447. * windows, port 0 will program MW 0 on all it's peers to point to itself.
  1448. * port 1 will program MW 0 in port 0 to point to itself and MW 1 on all
  1449. * other ports. etc.
  1450. *
  1451. * For the legacy two host case, ntb_port_number() and ntb_peer_port_number()
  1452. * both return zero and therefore this function will always return zero.
  1453. * So MW 0 on each host would be programmed to point to the other host.
  1454. *
  1455. * Return: the resource index to use for that peer.
  1456. */
  1457. static inline int ntb_peer_resource_idx(struct ntb_dev *ntb, int pidx)
  1458. {
  1459. int local_port, peer_port;
  1460. if (pidx >= ntb_peer_port_count(ntb))
  1461. return -EINVAL;
  1462. local_port = ntb_logical_port_number(ntb);
  1463. peer_port = ntb_peer_logical_port_number(ntb, pidx);
  1464. if (peer_port < local_port)
  1465. return local_port - 1;
  1466. else
  1467. return local_port;
  1468. }
  1469. /**
  1470. * ntb_peer_highest_mw_idx() - get a memory window index for a given peer idx
  1471. * using the highest index memory windows first
  1472. *
  1473. * @ntb: NTB device context.
  1474. * @pidx: Peer port index.
  1475. *
  1476. * Like ntb_peer_resource_idx(), except it returns indexes starting with
  1477. * last memory window index.
  1478. *
  1479. * Return: the resource index to use for that peer.
  1480. */
  1481. static inline int ntb_peer_highest_mw_idx(struct ntb_dev *ntb, int pidx)
  1482. {
  1483. int ret;
  1484. ret = ntb_peer_resource_idx(ntb, pidx);
  1485. if (ret < 0)
  1486. return ret;
  1487. return ntb_mw_count(ntb, pidx) - ret - 1;
  1488. }
  1489. struct ntb_msi_desc {
  1490. u32 addr_offset;
  1491. u32 data;
  1492. };
  1493. #ifdef CONFIG_NTB_MSI
  1494. int ntb_msi_init(struct ntb_dev *ntb, void (*desc_changed)(void *ctx));
  1495. int ntb_msi_setup_mws(struct ntb_dev *ntb);
  1496. void ntb_msi_clear_mws(struct ntb_dev *ntb);
  1497. int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
  1498. irq_handler_t thread_fn,
  1499. const char *name, void *dev_id,
  1500. struct ntb_msi_desc *msi_desc);
  1501. void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq, void *dev_id);
  1502. int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
  1503. struct ntb_msi_desc *desc);
  1504. int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
  1505. struct ntb_msi_desc *desc,
  1506. phys_addr_t *msi_addr);
  1507. #else /* not CONFIG_NTB_MSI */
  1508. static inline int ntb_msi_init(struct ntb_dev *ntb,
  1509. void (*desc_changed)(void *ctx))
  1510. {
  1511. return -EOPNOTSUPP;
  1512. }
  1513. static inline int ntb_msi_setup_mws(struct ntb_dev *ntb)
  1514. {
  1515. return -EOPNOTSUPP;
  1516. }
  1517. static inline void ntb_msi_clear_mws(struct ntb_dev *ntb) {}
  1518. static inline int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb,
  1519. irq_handler_t handler,
  1520. irq_handler_t thread_fn,
  1521. const char *name, void *dev_id,
  1522. struct ntb_msi_desc *msi_desc)
  1523. {
  1524. return -EOPNOTSUPP;
  1525. }
  1526. static inline void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq,
  1527. void *dev_id) {}
  1528. static inline int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
  1529. struct ntb_msi_desc *desc)
  1530. {
  1531. return -EOPNOTSUPP;
  1532. }
  1533. static inline int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
  1534. struct ntb_msi_desc *desc,
  1535. phys_addr_t *msi_addr)
  1536. {
  1537. return -EOPNOTSUPP;
  1538. }
  1539. #endif /* CONFIG_NTB_MSI */
  1540. static inline int ntbm_msi_request_irq(struct ntb_dev *ntb,
  1541. irq_handler_t handler,
  1542. const char *name, void *dev_id,
  1543. struct ntb_msi_desc *msi_desc)
  1544. {
  1545. return ntbm_msi_request_threaded_irq(ntb, handler, NULL, name,
  1546. dev_id, msi_desc);
  1547. }
  1548. #endif