ntb.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copy
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * * Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. * PCIe NTB Linux driver
  49. *
  50. * Contact Information:
  51. * Allen Hubbe <Allen.Hubbe@emc.com>
  52. */
  53. #ifndef _NTB_H_
  54. #define _NTB_H_
  55. #include <linux/completion.h>
  56. #include <linux/device.h>
  57. struct ntb_client;
  58. struct ntb_dev;
  59. struct pci_dev;
  60. /**
  61. * enum ntb_topo - NTB connection topology
  62. * @NTB_TOPO_NONE: Topology is unknown or invalid.
  63. * @NTB_TOPO_PRI: On primary side of local ntb.
  64. * @NTB_TOPO_SEC: On secondary side of remote ntb.
  65. * @NTB_TOPO_B2B_USD: On primary side of local ntb upstream of remote ntb.
  66. * @NTB_TOPO_B2B_DSD: On primary side of local ntb downstream of remote ntb.
  67. */
  68. enum ntb_topo {
  69. NTB_TOPO_NONE = -1,
  70. NTB_TOPO_PRI,
  71. NTB_TOPO_SEC,
  72. NTB_TOPO_B2B_USD,
  73. NTB_TOPO_B2B_DSD,
  74. };
  75. static inline int ntb_topo_is_b2b(enum ntb_topo topo)
  76. {
  77. switch ((int)topo) {
  78. case NTB_TOPO_B2B_USD:
  79. case NTB_TOPO_B2B_DSD:
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static inline char *ntb_topo_string(enum ntb_topo topo)
  85. {
  86. switch (topo) {
  87. case NTB_TOPO_NONE: return "NTB_TOPO_NONE";
  88. case NTB_TOPO_PRI: return "NTB_TOPO_PRI";
  89. case NTB_TOPO_SEC: return "NTB_TOPO_SEC";
  90. case NTB_TOPO_B2B_USD: return "NTB_TOPO_B2B_USD";
  91. case NTB_TOPO_B2B_DSD: return "NTB_TOPO_B2B_DSD";
  92. }
  93. return "NTB_TOPO_INVALID";
  94. }
  95. /**
  96. * enum ntb_speed - NTB link training speed
  97. * @NTB_SPEED_AUTO: Request the max supported speed.
  98. * @NTB_SPEED_NONE: Link is not trained to any speed.
  99. * @NTB_SPEED_GEN1: Link is trained to gen1 speed.
  100. * @NTB_SPEED_GEN2: Link is trained to gen2 speed.
  101. * @NTB_SPEED_GEN3: Link is trained to gen3 speed.
  102. */
  103. enum ntb_speed {
  104. NTB_SPEED_AUTO = -1,
  105. NTB_SPEED_NONE = 0,
  106. NTB_SPEED_GEN1 = 1,
  107. NTB_SPEED_GEN2 = 2,
  108. NTB_SPEED_GEN3 = 3,
  109. };
  110. /**
  111. * enum ntb_width - NTB link training width
  112. * @NTB_WIDTH_AUTO: Request the max supported width.
  113. * @NTB_WIDTH_NONE: Link is not trained to any width.
  114. * @NTB_WIDTH_1: Link is trained to 1 lane width.
  115. * @NTB_WIDTH_2: Link is trained to 2 lane width.
  116. * @NTB_WIDTH_4: Link is trained to 4 lane width.
  117. * @NTB_WIDTH_8: Link is trained to 8 lane width.
  118. * @NTB_WIDTH_12: Link is trained to 12 lane width.
  119. * @NTB_WIDTH_16: Link is trained to 16 lane width.
  120. * @NTB_WIDTH_32: Link is trained to 32 lane width.
  121. */
  122. enum ntb_width {
  123. NTB_WIDTH_AUTO = -1,
  124. NTB_WIDTH_NONE = 0,
  125. NTB_WIDTH_1 = 1,
  126. NTB_WIDTH_2 = 2,
  127. NTB_WIDTH_4 = 4,
  128. NTB_WIDTH_8 = 8,
  129. NTB_WIDTH_12 = 12,
  130. NTB_WIDTH_16 = 16,
  131. NTB_WIDTH_32 = 32,
  132. };
  133. /**
  134. * struct ntb_client_ops - ntb client operations
  135. * @probe: Notify client of a new device.
  136. * @remove: Notify client to remove a device.
  137. */
  138. struct ntb_client_ops {
  139. int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
  140. void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
  141. };
  142. static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
  143. {
  144. /* commented callbacks are not required: */
  145. return
  146. ops->probe &&
  147. ops->remove &&
  148. 1;
  149. }
  150. /**
  151. * struct ntb_ctx_ops - ntb driver context operations
  152. * @link_event: See ntb_link_event().
  153. * @db_event: See ntb_db_event().
  154. */
  155. struct ntb_ctx_ops {
  156. void (*link_event)(void *ctx);
  157. void (*db_event)(void *ctx, int db_vector);
  158. };
  159. static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
  160. {
  161. /* commented callbacks are not required: */
  162. return
  163. /* ops->link_event && */
  164. /* ops->db_event && */
  165. 1;
  166. }
  167. /**
  168. * struct ntb_ctx_ops - ntb device operations
  169. * @mw_count: See ntb_mw_count().
  170. * @mw_get_range: See ntb_mw_get_range().
  171. * @mw_set_trans: See ntb_mw_set_trans().
  172. * @mw_clear_trans: See ntb_mw_clear_trans().
  173. * @link_is_up: See ntb_link_is_up().
  174. * @link_enable: See ntb_link_enable().
  175. * @link_disable: See ntb_link_disable().
  176. * @db_is_unsafe: See ntb_db_is_unsafe().
  177. * @db_valid_mask: See ntb_db_valid_mask().
  178. * @db_vector_count: See ntb_db_vector_count().
  179. * @db_vector_mask: See ntb_db_vector_mask().
  180. * @db_read: See ntb_db_read().
  181. * @db_set: See ntb_db_set().
  182. * @db_clear: See ntb_db_clear().
  183. * @db_read_mask: See ntb_db_read_mask().
  184. * @db_set_mask: See ntb_db_set_mask().
  185. * @db_clear_mask: See ntb_db_clear_mask().
  186. * @peer_db_addr: See ntb_peer_db_addr().
  187. * @peer_db_read: See ntb_peer_db_read().
  188. * @peer_db_set: See ntb_peer_db_set().
  189. * @peer_db_clear: See ntb_peer_db_clear().
  190. * @peer_db_read_mask: See ntb_peer_db_read_mask().
  191. * @peer_db_set_mask: See ntb_peer_db_set_mask().
  192. * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
  193. * @spad_is_unsafe: See ntb_spad_is_unsafe().
  194. * @spad_count: See ntb_spad_count().
  195. * @spad_read: See ntb_spad_read().
  196. * @spad_write: See ntb_spad_write().
  197. * @peer_spad_addr: See ntb_peer_spad_addr().
  198. * @peer_spad_read: See ntb_peer_spad_read().
  199. * @peer_spad_write: See ntb_peer_spad_write().
  200. */
  201. struct ntb_dev_ops {
  202. int (*mw_count)(struct ntb_dev *ntb);
  203. int (*mw_get_range)(struct ntb_dev *ntb, int idx,
  204. phys_addr_t *base, resource_size_t *size,
  205. resource_size_t *align, resource_size_t *align_size);
  206. int (*mw_set_trans)(struct ntb_dev *ntb, int idx,
  207. dma_addr_t addr, resource_size_t size);
  208. int (*mw_clear_trans)(struct ntb_dev *ntb, int idx);
  209. int (*link_is_up)(struct ntb_dev *ntb,
  210. enum ntb_speed *speed, enum ntb_width *width);
  211. int (*link_enable)(struct ntb_dev *ntb,
  212. enum ntb_speed max_speed, enum ntb_width max_width);
  213. int (*link_disable)(struct ntb_dev *ntb);
  214. int (*db_is_unsafe)(struct ntb_dev *ntb);
  215. u64 (*db_valid_mask)(struct ntb_dev *ntb);
  216. int (*db_vector_count)(struct ntb_dev *ntb);
  217. u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
  218. u64 (*db_read)(struct ntb_dev *ntb);
  219. int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
  220. int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
  221. u64 (*db_read_mask)(struct ntb_dev *ntb);
  222. int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
  223. int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
  224. int (*peer_db_addr)(struct ntb_dev *ntb,
  225. phys_addr_t *db_addr, resource_size_t *db_size);
  226. u64 (*peer_db_read)(struct ntb_dev *ntb);
  227. int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
  228. int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
  229. u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
  230. int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
  231. int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
  232. int (*spad_is_unsafe)(struct ntb_dev *ntb);
  233. int (*spad_count)(struct ntb_dev *ntb);
  234. u32 (*spad_read)(struct ntb_dev *ntb, int idx);
  235. int (*spad_write)(struct ntb_dev *ntb, int idx, u32 val);
  236. int (*peer_spad_addr)(struct ntb_dev *ntb, int idx,
  237. phys_addr_t *spad_addr);
  238. u32 (*peer_spad_read)(struct ntb_dev *ntb, int idx);
  239. int (*peer_spad_write)(struct ntb_dev *ntb, int idx, u32 val);
  240. };
  241. static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
  242. {
  243. /* commented callbacks are not required: */
  244. return
  245. ops->mw_count &&
  246. ops->mw_get_range &&
  247. ops->mw_set_trans &&
  248. /* ops->mw_clear_trans && */
  249. ops->link_is_up &&
  250. ops->link_enable &&
  251. ops->link_disable &&
  252. /* ops->db_is_unsafe && */
  253. ops->db_valid_mask &&
  254. /* both set, or both unset */
  255. (!ops->db_vector_count == !ops->db_vector_mask) &&
  256. ops->db_read &&
  257. /* ops->db_set && */
  258. ops->db_clear &&
  259. /* ops->db_read_mask && */
  260. ops->db_set_mask &&
  261. ops->db_clear_mask &&
  262. /* ops->peer_db_addr && */
  263. /* ops->peer_db_read && */
  264. ops->peer_db_set &&
  265. /* ops->peer_db_clear && */
  266. /* ops->peer_db_read_mask && */
  267. /* ops->peer_db_set_mask && */
  268. /* ops->peer_db_clear_mask && */
  269. /* ops->spad_is_unsafe && */
  270. ops->spad_count &&
  271. ops->spad_read &&
  272. ops->spad_write &&
  273. /* ops->peer_spad_addr && */
  274. /* ops->peer_spad_read && */
  275. ops->peer_spad_write &&
  276. 1;
  277. }
  278. /**
  279. * struct ntb_client - client interested in ntb devices
  280. * @drv: Linux driver object.
  281. * @ops: See &ntb_client_ops.
  282. */
  283. struct ntb_client {
  284. struct device_driver drv;
  285. const struct ntb_client_ops ops;
  286. };
  287. #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
  288. /**
  289. * struct ntb_device - ntb device
  290. * @dev: Linux device object.
  291. * @pdev: Pci device entry of the ntb.
  292. * @topo: Detected topology of the ntb.
  293. * @ops: See &ntb_dev_ops.
  294. * @ctx: See &ntb_ctx_ops.
  295. * @ctx_ops: See &ntb_ctx_ops.
  296. */
  297. struct ntb_dev {
  298. struct device dev;
  299. struct pci_dev *pdev;
  300. enum ntb_topo topo;
  301. const struct ntb_dev_ops *ops;
  302. void *ctx;
  303. const struct ntb_ctx_ops *ctx_ops;
  304. /* private: */
  305. /* synchronize setting, clearing, and calling ctx_ops */
  306. spinlock_t ctx_lock;
  307. /* block unregister until device is fully released */
  308. struct completion released;
  309. };
  310. #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
  311. /**
  312. * ntb_register_client() - register a client for interest in ntb devices
  313. * @client: Client context.
  314. *
  315. * The client will be added to the list of clients interested in ntb devices.
  316. * The client will be notified of any ntb devices that are not already
  317. * associated with a client, or if ntb devices are registered later.
  318. *
  319. * Return: Zero if the client is registered, otherwise an error number.
  320. */
  321. #define ntb_register_client(client) \
  322. __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
  323. int __ntb_register_client(struct ntb_client *client, struct module *mod,
  324. const char *mod_name);
  325. /**
  326. * ntb_unregister_client() - unregister a client for interest in ntb devices
  327. * @client: Client context.
  328. *
  329. * The client will be removed from the list of clients interested in ntb
  330. * devices. If any ntb devices are associated with the client, the client will
  331. * be notified to remove those devices.
  332. */
  333. void ntb_unregister_client(struct ntb_client *client);
  334. #define module_ntb_client(__ntb_client) \
  335. module_driver(__ntb_client, ntb_register_client, \
  336. ntb_unregister_client)
  337. /**
  338. * ntb_register_device() - register a ntb device
  339. * @ntb: NTB device context.
  340. *
  341. * The device will be added to the list of ntb devices. If any clients are
  342. * interested in ntb devices, each client will be notified of the ntb device,
  343. * until at most one client accepts the device.
  344. *
  345. * Return: Zero if the device is registered, otherwise an error number.
  346. */
  347. int ntb_register_device(struct ntb_dev *ntb);
  348. /**
  349. * ntb_register_device() - unregister a ntb device
  350. * @ntb: NTB device context.
  351. *
  352. * The device will be removed from the list of ntb devices. If the ntb device
  353. * is associated with a client, the client will be notified to remove the
  354. * device.
  355. */
  356. void ntb_unregister_device(struct ntb_dev *ntb);
  357. /**
  358. * ntb_set_ctx() - associate a driver context with an ntb device
  359. * @ntb: NTB device context.
  360. * @ctx: Driver context.
  361. * @ctx_ops: Driver context operations.
  362. *
  363. * Associate a driver context and operations with a ntb device. The context is
  364. * provided by the client driver, and the driver may associate a different
  365. * context with each ntb device.
  366. *
  367. * Return: Zero if the context is associated, otherwise an error number.
  368. */
  369. int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
  370. const struct ntb_ctx_ops *ctx_ops);
  371. /**
  372. * ntb_clear_ctx() - disassociate any driver context from an ntb device
  373. * @ntb: NTB device context.
  374. *
  375. * Clear any association that may exist between a driver context and the ntb
  376. * device.
  377. */
  378. void ntb_clear_ctx(struct ntb_dev *ntb);
  379. /**
  380. * ntb_link_event() - notify driver context of a change in link status
  381. * @ntb: NTB device context.
  382. *
  383. * Notify the driver context that the link status may have changed. The driver
  384. * should call ntb_link_is_up() to get the current status.
  385. */
  386. void ntb_link_event(struct ntb_dev *ntb);
  387. /**
  388. * ntb_db_event() - notify driver context of a doorbell event
  389. * @ntb: NTB device context.
  390. * @vector: Interrupt vector number.
  391. *
  392. * Notify the driver context of a doorbell event. If hardware supports
  393. * multiple interrupt vectors for doorbells, the vector number indicates which
  394. * vector received the interrupt. The vector number is relative to the first
  395. * vector used for doorbells, starting at zero, and must be less than
  396. ** ntb_db_vector_count(). The driver may call ntb_db_read() to check which
  397. * doorbell bits need service, and ntb_db_vector_mask() to determine which of
  398. * those bits are associated with the vector number.
  399. */
  400. void ntb_db_event(struct ntb_dev *ntb, int vector);
  401. /**
  402. * ntb_mw_count() - get the number of memory windows
  403. * @ntb: NTB device context.
  404. *
  405. * Hardware and topology may support a different number of memory windows.
  406. *
  407. * Return: the number of memory windows.
  408. */
  409. static inline int ntb_mw_count(struct ntb_dev *ntb)
  410. {
  411. return ntb->ops->mw_count(ntb);
  412. }
  413. /**
  414. * ntb_mw_get_range() - get the range of a memory window
  415. * @ntb: NTB device context.
  416. * @idx: Memory window number.
  417. * @base: OUT - the base address for mapping the memory window
  418. * @size: OUT - the size for mapping the memory window
  419. * @align: OUT - the base alignment for translating the memory window
  420. * @align_size: OUT - the size alignment for translating the memory window
  421. *
  422. * Get the range of a memory window. NULL may be given for any output
  423. * parameter if the value is not needed. The base and size may be used for
  424. * mapping the memory window, to access the peer memory. The alignment and
  425. * size may be used for translating the memory window, for the peer to access
  426. * memory on the local system.
  427. *
  428. * Return: Zero on success, otherwise an error number.
  429. */
  430. static inline int ntb_mw_get_range(struct ntb_dev *ntb, int idx,
  431. phys_addr_t *base, resource_size_t *size,
  432. resource_size_t *align, resource_size_t *align_size)
  433. {
  434. return ntb->ops->mw_get_range(ntb, idx, base, size,
  435. align, align_size);
  436. }
  437. /**
  438. * ntb_mw_set_trans() - set the translation of a memory window
  439. * @ntb: NTB device context.
  440. * @idx: Memory window number.
  441. * @addr: The dma address local memory to expose to the peer.
  442. * @size: The size of the local memory to expose to the peer.
  443. *
  444. * Set the translation of a memory window. The peer may access local memory
  445. * through the window starting at the address, up to the size. The address
  446. * must be aligned to the alignment specified by ntb_mw_get_range(). The size
  447. * must be aligned to the size alignment specified by ntb_mw_get_range().
  448. *
  449. * Return: Zero on success, otherwise an error number.
  450. */
  451. static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
  452. dma_addr_t addr, resource_size_t size)
  453. {
  454. return ntb->ops->mw_set_trans(ntb, idx, addr, size);
  455. }
  456. /**
  457. * ntb_mw_clear_trans() - clear the translation of a memory window
  458. * @ntb: NTB device context.
  459. * @idx: Memory window number.
  460. *
  461. * Clear the translation of a memory window. The peer may no longer access
  462. * local memory through the window.
  463. *
  464. * Return: Zero on success, otherwise an error number.
  465. */
  466. static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int idx)
  467. {
  468. if (!ntb->ops->mw_clear_trans)
  469. return ntb->ops->mw_set_trans(ntb, idx, 0, 0);
  470. return ntb->ops->mw_clear_trans(ntb, idx);
  471. }
  472. /**
  473. * ntb_link_is_up() - get the current ntb link state
  474. * @ntb: NTB device context.
  475. * @speed: OUT - The link speed expressed as PCIe generation number.
  476. * @width: OUT - The link width expressed as the number of PCIe lanes.
  477. *
  478. * Get the current state of the ntb link. It is recommended to query the link
  479. * state once after every link event. It is safe to query the link state in
  480. * the context of the link event callback.
  481. *
  482. * Return: One if the link is up, zero if the link is down, otherwise a
  483. * negative value indicating the error number.
  484. */
  485. static inline int ntb_link_is_up(struct ntb_dev *ntb,
  486. enum ntb_speed *speed, enum ntb_width *width)
  487. {
  488. return ntb->ops->link_is_up(ntb, speed, width);
  489. }
  490. /**
  491. * ntb_link_enable() - enable the link on the secondary side of the ntb
  492. * @ntb: NTB device context.
  493. * @max_speed: The maximum link speed expressed as PCIe generation number.
  494. * @max_width: The maximum link width expressed as the number of PCIe lanes.
  495. *
  496. * Enable the link on the secondary side of the ntb. This can only be done
  497. * from the primary side of the ntb in primary or b2b topology. The ntb device
  498. * should train the link to its maximum speed and width, or the requested speed
  499. * and width, whichever is smaller, if supported.
  500. *
  501. * Return: Zero on success, otherwise an error number.
  502. */
  503. static inline int ntb_link_enable(struct ntb_dev *ntb,
  504. enum ntb_speed max_speed,
  505. enum ntb_width max_width)
  506. {
  507. return ntb->ops->link_enable(ntb, max_speed, max_width);
  508. }
  509. /**
  510. * ntb_link_disable() - disable the link on the secondary side of the ntb
  511. * @ntb: NTB device context.
  512. *
  513. * Disable the link on the secondary side of the ntb. This can only be
  514. * done from the primary side of the ntb in primary or b2b topology. The ntb
  515. * device should disable the link. Returning from this call must indicate that
  516. * a barrier has passed, though with no more writes may pass in either
  517. * direction across the link, except if this call returns an error number.
  518. *
  519. * Return: Zero on success, otherwise an error number.
  520. */
  521. static inline int ntb_link_disable(struct ntb_dev *ntb)
  522. {
  523. return ntb->ops->link_disable(ntb);
  524. }
  525. /**
  526. * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
  527. * @ntb: NTB device context.
  528. *
  529. * It is possible for some ntb hardware to be affected by errata. Hardware
  530. * drivers can advise clients to avoid using doorbells. Clients may ignore
  531. * this advice, though caution is recommended.
  532. *
  533. * Return: Zero if it is safe to use doorbells, or One if it is not safe.
  534. */
  535. static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
  536. {
  537. if (!ntb->ops->db_is_unsafe)
  538. return 0;
  539. return ntb->ops->db_is_unsafe(ntb);
  540. }
  541. /**
  542. * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
  543. * @ntb: NTB device context.
  544. *
  545. * Hardware may support different number or arrangement of doorbell bits.
  546. *
  547. * Return: A mask of doorbell bits supported by the ntb.
  548. */
  549. static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
  550. {
  551. return ntb->ops->db_valid_mask(ntb);
  552. }
  553. /**
  554. * ntb_db_vector_count() - get the number of doorbell interrupt vectors
  555. * @ntb: NTB device context.
  556. *
  557. * Hardware may support different number of interrupt vectors.
  558. *
  559. * Return: The number of doorbell interrupt vectors.
  560. */
  561. static inline int ntb_db_vector_count(struct ntb_dev *ntb)
  562. {
  563. if (!ntb->ops->db_vector_count)
  564. return 1;
  565. return ntb->ops->db_vector_count(ntb);
  566. }
  567. /**
  568. * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
  569. * @ntb: NTB device context.
  570. * @vector: Doorbell vector number.
  571. *
  572. * Each interrupt vector may have a different number or arrangement of bits.
  573. *
  574. * Return: A mask of doorbell bits serviced by a vector.
  575. */
  576. static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
  577. {
  578. if (!ntb->ops->db_vector_mask)
  579. return ntb_db_valid_mask(ntb);
  580. return ntb->ops->db_vector_mask(ntb, vector);
  581. }
  582. /**
  583. * ntb_db_read() - read the local doorbell register
  584. * @ntb: NTB device context.
  585. *
  586. * Read the local doorbell register, and return the bits that are set.
  587. *
  588. * Return: The bits currently set in the local doorbell register.
  589. */
  590. static inline u64 ntb_db_read(struct ntb_dev *ntb)
  591. {
  592. return ntb->ops->db_read(ntb);
  593. }
  594. /**
  595. * ntb_db_set() - set bits in the local doorbell register
  596. * @ntb: NTB device context.
  597. * @db_bits: Doorbell bits to set.
  598. *
  599. * Set bits in the local doorbell register, which may generate a local doorbell
  600. * interrupt. Bits that were already set must remain set.
  601. *
  602. * This is unusual, and hardware may not support it.
  603. *
  604. * Return: Zero on success, otherwise an error number.
  605. */
  606. static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
  607. {
  608. if (!ntb->ops->db_set)
  609. return -EINVAL;
  610. return ntb->ops->db_set(ntb, db_bits);
  611. }
  612. /**
  613. * ntb_db_clear() - clear bits in the local doorbell register
  614. * @ntb: NTB device context.
  615. * @db_bits: Doorbell bits to clear.
  616. *
  617. * Clear bits in the local doorbell register, arming the bits for the next
  618. * doorbell.
  619. *
  620. * Return: Zero on success, otherwise an error number.
  621. */
  622. static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  623. {
  624. return ntb->ops->db_clear(ntb, db_bits);
  625. }
  626. /**
  627. * ntb_db_read_mask() - read the local doorbell mask
  628. * @ntb: NTB device context.
  629. *
  630. * Read the local doorbell mask register, and return the bits that are set.
  631. *
  632. * This is unusual, though hardware is likely to support it.
  633. *
  634. * Return: The bits currently set in the local doorbell mask register.
  635. */
  636. static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
  637. {
  638. if (!ntb->ops->db_read_mask)
  639. return 0;
  640. return ntb->ops->db_read_mask(ntb);
  641. }
  642. /**
  643. * ntb_db_set_mask() - set bits in the local doorbell mask
  644. * @ntb: NTB device context.
  645. * @db_bits: Doorbell mask bits to set.
  646. *
  647. * Set bits in the local doorbell mask register, preventing doorbell interrupts
  648. * from being generated for those doorbell bits. Bits that were already set
  649. * must remain set.
  650. *
  651. * Return: Zero on success, otherwise an error number.
  652. */
  653. static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  654. {
  655. return ntb->ops->db_set_mask(ntb, db_bits);
  656. }
  657. /**
  658. * ntb_db_clear_mask() - clear bits in the local doorbell mask
  659. * @ntb: NTB device context.
  660. * @db_bits: Doorbell bits to clear.
  661. *
  662. * Clear bits in the local doorbell mask register, allowing doorbell interrupts
  663. * from being generated for those doorbell bits. If a doorbell bit is already
  664. * set at the time the mask is cleared, and the corresponding mask bit is
  665. * changed from set to clear, then the ntb driver must ensure that
  666. * ntb_db_event() is called. If the hardware does not generate the interrupt
  667. * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
  668. *
  669. * Return: Zero on success, otherwise an error number.
  670. */
  671. static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  672. {
  673. return ntb->ops->db_clear_mask(ntb, db_bits);
  674. }
  675. /**
  676. * ntb_peer_db_addr() - address and size of the peer doorbell register
  677. * @ntb: NTB device context.
  678. * @db_addr: OUT - The address of the peer doorbell register.
  679. * @db_size: OUT - The number of bytes to write the peer doorbell register.
  680. *
  681. * Return the address of the peer doorbell register. This may be used, for
  682. * example, by drivers that offload memory copy operations to a dma engine.
  683. * The drivers may wish to ring the peer doorbell at the completion of memory
  684. * copy operations. For efficiency, and to simplify ordering of operations
  685. * between the dma memory copies and the ringing doorbell, the driver may
  686. * append one additional dma memory copy with the doorbell register as the
  687. * destination, after the memory copy operations.
  688. *
  689. * Return: Zero on success, otherwise an error number.
  690. */
  691. static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
  692. phys_addr_t *db_addr,
  693. resource_size_t *db_size)
  694. {
  695. if (!ntb->ops->peer_db_addr)
  696. return -EINVAL;
  697. return ntb->ops->peer_db_addr(ntb, db_addr, db_size);
  698. }
  699. /**
  700. * ntb_peer_db_read() - read the peer doorbell register
  701. * @ntb: NTB device context.
  702. *
  703. * Read the peer doorbell register, and return the bits that are set.
  704. *
  705. * This is unusual, and hardware may not support it.
  706. *
  707. * Return: The bits currently set in the peer doorbell register.
  708. */
  709. static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
  710. {
  711. if (!ntb->ops->peer_db_read)
  712. return 0;
  713. return ntb->ops->peer_db_read(ntb);
  714. }
  715. /**
  716. * ntb_peer_db_set() - set bits in the peer doorbell register
  717. * @ntb: NTB device context.
  718. * @db_bits: Doorbell bits to set.
  719. *
  720. * Set bits in the peer doorbell register, which may generate a peer doorbell
  721. * interrupt. Bits that were already set must remain set.
  722. *
  723. * Return: Zero on success, otherwise an error number.
  724. */
  725. static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  726. {
  727. return ntb->ops->peer_db_set(ntb, db_bits);
  728. }
  729. /**
  730. * ntb_peer_db_clear() - clear bits in the peer doorbell register
  731. * @ntb: NTB device context.
  732. * @db_bits: Doorbell bits to clear.
  733. *
  734. * Clear bits in the peer doorbell register, arming the bits for the next
  735. * doorbell.
  736. *
  737. * This is unusual, and hardware may not support it.
  738. *
  739. * Return: Zero on success, otherwise an error number.
  740. */
  741. static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
  742. {
  743. if (!ntb->ops->db_clear)
  744. return -EINVAL;
  745. return ntb->ops->peer_db_clear(ntb, db_bits);
  746. }
  747. /**
  748. * ntb_peer_db_read_mask() - read the peer doorbell mask
  749. * @ntb: NTB device context.
  750. *
  751. * Read the peer doorbell mask register, and return the bits that are set.
  752. *
  753. * This is unusual, and hardware may not support it.
  754. *
  755. * Return: The bits currently set in the peer doorbell mask register.
  756. */
  757. static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
  758. {
  759. if (!ntb->ops->db_read_mask)
  760. return 0;
  761. return ntb->ops->peer_db_read_mask(ntb);
  762. }
  763. /**
  764. * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
  765. * @ntb: NTB device context.
  766. * @db_bits: Doorbell mask bits to set.
  767. *
  768. * Set bits in the peer doorbell mask register, preventing doorbell interrupts
  769. * from being generated for those doorbell bits. Bits that were already set
  770. * must remain set.
  771. *
  772. * This is unusual, and hardware may not support it.
  773. *
  774. * Return: Zero on success, otherwise an error number.
  775. */
  776. static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  777. {
  778. if (!ntb->ops->db_set_mask)
  779. return -EINVAL;
  780. return ntb->ops->peer_db_set_mask(ntb, db_bits);
  781. }
  782. /**
  783. * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
  784. * @ntb: NTB device context.
  785. * @db_bits: Doorbell bits to clear.
  786. *
  787. * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
  788. * from being generated for those doorbell bits. If the hardware does not
  789. * generate the interrupt on clearing the mask bit, then the driver should not
  790. * implement this function!
  791. *
  792. * This is unusual, and hardware may not support it.
  793. *
  794. * Return: Zero on success, otherwise an error number.
  795. */
  796. static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  797. {
  798. if (!ntb->ops->db_clear_mask)
  799. return -EINVAL;
  800. return ntb->ops->peer_db_clear_mask(ntb, db_bits);
  801. }
  802. /**
  803. * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
  804. * @ntb: NTB device context.
  805. *
  806. * It is possible for some ntb hardware to be affected by errata. Hardware
  807. * drivers can advise clients to avoid using scratchpads. Clients may ignore
  808. * this advice, though caution is recommended.
  809. *
  810. * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
  811. */
  812. static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
  813. {
  814. if (!ntb->ops->spad_is_unsafe)
  815. return 0;
  816. return ntb->ops->spad_is_unsafe(ntb);
  817. }
  818. /**
  819. * ntb_mw_count() - get the number of scratchpads
  820. * @ntb: NTB device context.
  821. *
  822. * Hardware and topology may support a different number of scratchpads.
  823. *
  824. * Return: the number of scratchpads.
  825. */
  826. static inline int ntb_spad_count(struct ntb_dev *ntb)
  827. {
  828. return ntb->ops->spad_count(ntb);
  829. }
  830. /**
  831. * ntb_spad_read() - read the local scratchpad register
  832. * @ntb: NTB device context.
  833. * @idx: Scratchpad index.
  834. *
  835. * Read the local scratchpad register, and return the value.
  836. *
  837. * Return: The value of the local scratchpad register.
  838. */
  839. static inline u32 ntb_spad_read(struct ntb_dev *ntb, int idx)
  840. {
  841. return ntb->ops->spad_read(ntb, idx);
  842. }
  843. /**
  844. * ntb_spad_write() - write the local scratchpad register
  845. * @ntb: NTB device context.
  846. * @idx: Scratchpad index.
  847. * @val: Scratchpad value.
  848. *
  849. * Write the value to the local scratchpad register.
  850. *
  851. * Return: Zero on success, otherwise an error number.
  852. */
  853. static inline int ntb_spad_write(struct ntb_dev *ntb, int idx, u32 val)
  854. {
  855. return ntb->ops->spad_write(ntb, idx, val);
  856. }
  857. /**
  858. * ntb_peer_spad_addr() - address of the peer scratchpad register
  859. * @ntb: NTB device context.
  860. * @idx: Scratchpad index.
  861. * @spad_addr: OUT - The address of the peer scratchpad register.
  862. *
  863. * Return the address of the peer doorbell register. This may be used, for
  864. * example, by drivers that offload memory copy operations to a dma engine.
  865. *
  866. * Return: Zero on success, otherwise an error number.
  867. */
  868. static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
  869. phys_addr_t *spad_addr)
  870. {
  871. if (!ntb->ops->peer_spad_addr)
  872. return -EINVAL;
  873. return ntb->ops->peer_spad_addr(ntb, idx, spad_addr);
  874. }
  875. /**
  876. * ntb_peer_spad_read() - read the peer scratchpad register
  877. * @ntb: NTB device context.
  878. * @idx: Scratchpad index.
  879. *
  880. * Read the peer scratchpad register, and return the value.
  881. *
  882. * Return: The value of the local scratchpad register.
  883. */
  884. static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
  885. {
  886. return ntb->ops->peer_spad_read(ntb, idx);
  887. }
  888. /**
  889. * ntb_peer_spad_write() - write the peer scratchpad register
  890. * @ntb: NTB device context.
  891. * @idx: Scratchpad index.
  892. * @val: Scratchpad value.
  893. *
  894. * Write the value to the peer scratchpad register.
  895. *
  896. * Return: Zero on success, otherwise an error number.
  897. */
  898. static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int idx, u32 val)
  899. {
  900. return ntb->ops->peer_spad_write(ntb, idx, val);
  901. }
  902. #endif