ntb.h 47 KB

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