ntb.h 47 KB

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