xref: /linux-6.15/include/linux/ntb.h (revision 46f21af8)
1a1bd3baeSAllen Hubbe /*
2a1bd3baeSAllen Hubbe  * This file is provided under a dual BSD/GPLv2 license.  When using or
3a1bd3baeSAllen Hubbe  *   redistributing this file, you may do so under either license.
4a1bd3baeSAllen Hubbe  *
5a1bd3baeSAllen Hubbe  *   GPL LICENSE SUMMARY
6a1bd3baeSAllen Hubbe  *
7a1bd3baeSAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8443b9a14SSerge Semin  *   Copyright (C) 2016 T-Platforms. All Rights Reserved.
9a1bd3baeSAllen Hubbe  *
10a1bd3baeSAllen Hubbe  *   This program is free software; you can redistribute it and/or modify
11a1bd3baeSAllen Hubbe  *   it under the terms of version 2 of the GNU General Public License as
12a1bd3baeSAllen Hubbe  *   published by the Free Software Foundation.
13a1bd3baeSAllen Hubbe  *
14a1bd3baeSAllen Hubbe  *   This program is distributed in the hope that it will be useful, but
15a1bd3baeSAllen Hubbe  *   WITHOUT ANY WARRANTY; without even the implied warranty of
16a1bd3baeSAllen Hubbe  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17a1bd3baeSAllen Hubbe  *   General Public License for more details.
18a1bd3baeSAllen Hubbe  *
19a1bd3baeSAllen Hubbe  *   BSD LICENSE
20a1bd3baeSAllen Hubbe  *
21a1bd3baeSAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22443b9a14SSerge Semin  *   Copyright (C) 2016 T-Platforms. All Rights Reserved.
23a1bd3baeSAllen Hubbe  *
24a1bd3baeSAllen Hubbe  *   Redistribution and use in source and binary forms, with or without
25a1bd3baeSAllen Hubbe  *   modification, are permitted provided that the following conditions
26a1bd3baeSAllen Hubbe  *   are met:
27a1bd3baeSAllen Hubbe  *
28a1bd3baeSAllen Hubbe  *     * Redistributions of source code must retain the above copyright
29a1bd3baeSAllen Hubbe  *       notice, this list of conditions and the following disclaimer.
30a1bd3baeSAllen Hubbe  *     * Redistributions in binary form must reproduce the above copy
31a1bd3baeSAllen Hubbe  *       notice, this list of conditions and the following disclaimer in
32a1bd3baeSAllen Hubbe  *       the documentation and/or other materials provided with the
33a1bd3baeSAllen Hubbe  *       distribution.
34a1bd3baeSAllen Hubbe  *     * Neither the name of Intel Corporation nor the names of its
35a1bd3baeSAllen Hubbe  *       contributors may be used to endorse or promote products derived
36a1bd3baeSAllen Hubbe  *       from this software without specific prior written permission.
37a1bd3baeSAllen Hubbe  *
38a1bd3baeSAllen Hubbe  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39a1bd3baeSAllen Hubbe  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40a1bd3baeSAllen Hubbe  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41a1bd3baeSAllen Hubbe  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42a1bd3baeSAllen Hubbe  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43a1bd3baeSAllen Hubbe  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44a1bd3baeSAllen Hubbe  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45a1bd3baeSAllen Hubbe  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46a1bd3baeSAllen Hubbe  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47a1bd3baeSAllen Hubbe  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48a1bd3baeSAllen Hubbe  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49a1bd3baeSAllen Hubbe  *
50a1bd3baeSAllen Hubbe  * PCIe NTB Linux driver
51a1bd3baeSAllen Hubbe  *
52a1bd3baeSAllen Hubbe  * Contact Information:
53a1bd3baeSAllen Hubbe  * Allen Hubbe <[email protected]>
54a1bd3baeSAllen Hubbe  */
55a1bd3baeSAllen Hubbe 
56a1bd3baeSAllen Hubbe #ifndef _NTB_H_
57a1bd3baeSAllen Hubbe #define _NTB_H_
58a1bd3baeSAllen Hubbe 
59a1bd3baeSAllen Hubbe #include <linux/completion.h>
60a1bd3baeSAllen Hubbe #include <linux/device.h>
6126b3a37bSLogan Gunthorpe #include <linux/interrupt.h>
62a1bd3baeSAllen Hubbe 
63a1bd3baeSAllen Hubbe struct ntb_client;
64a1bd3baeSAllen Hubbe struct ntb_dev;
6526b3a37bSLogan Gunthorpe struct ntb_msi;
66a1bd3baeSAllen Hubbe struct pci_dev;
67a1bd3baeSAllen Hubbe 
68a1bd3baeSAllen Hubbe /**
69a1bd3baeSAllen Hubbe  * enum ntb_topo - NTB connection topology
70a1bd3baeSAllen Hubbe  * @NTB_TOPO_NONE:	Topology is unknown or invalid.
71a1bd3baeSAllen Hubbe  * @NTB_TOPO_PRI:	On primary side of local ntb.
72a1bd3baeSAllen Hubbe  * @NTB_TOPO_SEC:	On secondary side of remote ntb.
73a1bd3baeSAllen Hubbe  * @NTB_TOPO_B2B_USD:	On primary side of local ntb upstream of remote ntb.
74a1bd3baeSAllen Hubbe  * @NTB_TOPO_B2B_DSD:	On primary side of local ntb downstream of remote ntb.
75e099b45bSLogan Gunthorpe  * @NTB_TOPO_SWITCH:	Connected via a switch which supports ntb.
7601752501SLogan Gunthorpe  * @NTB_TOPO_CROSSLINK: Connected via two symmetric switchecs
77a1bd3baeSAllen Hubbe  */
78a1bd3baeSAllen Hubbe enum ntb_topo {
79a1bd3baeSAllen Hubbe 	NTB_TOPO_NONE = -1,
80a1bd3baeSAllen Hubbe 	NTB_TOPO_PRI,
81a1bd3baeSAllen Hubbe 	NTB_TOPO_SEC,
82a1bd3baeSAllen Hubbe 	NTB_TOPO_B2B_USD,
83a1bd3baeSAllen Hubbe 	NTB_TOPO_B2B_DSD,
84e099b45bSLogan Gunthorpe 	NTB_TOPO_SWITCH,
8501752501SLogan Gunthorpe 	NTB_TOPO_CROSSLINK,
86a1bd3baeSAllen Hubbe };
87a1bd3baeSAllen Hubbe 
ntb_topo_is_b2b(enum ntb_topo topo)88a1bd3baeSAllen Hubbe static inline int ntb_topo_is_b2b(enum ntb_topo topo)
89a1bd3baeSAllen Hubbe {
90a1bd3baeSAllen Hubbe 	switch ((int)topo) {
91a1bd3baeSAllen Hubbe 	case NTB_TOPO_B2B_USD:
92a1bd3baeSAllen Hubbe 	case NTB_TOPO_B2B_DSD:
93a1bd3baeSAllen Hubbe 		return 1;
94a1bd3baeSAllen Hubbe 	}
95a1bd3baeSAllen Hubbe 	return 0;
96a1bd3baeSAllen Hubbe }
97a1bd3baeSAllen Hubbe 
ntb_topo_string(enum ntb_topo topo)98a1bd3baeSAllen Hubbe static inline char *ntb_topo_string(enum ntb_topo topo)
99a1bd3baeSAllen Hubbe {
100a1bd3baeSAllen Hubbe 	switch (topo) {
101a1bd3baeSAllen Hubbe 	case NTB_TOPO_NONE:		return "NTB_TOPO_NONE";
102a1bd3baeSAllen Hubbe 	case NTB_TOPO_PRI:		return "NTB_TOPO_PRI";
103a1bd3baeSAllen Hubbe 	case NTB_TOPO_SEC:		return "NTB_TOPO_SEC";
104a1bd3baeSAllen Hubbe 	case NTB_TOPO_B2B_USD:		return "NTB_TOPO_B2B_USD";
105a1bd3baeSAllen Hubbe 	case NTB_TOPO_B2B_DSD:		return "NTB_TOPO_B2B_DSD";
106e099b45bSLogan Gunthorpe 	case NTB_TOPO_SWITCH:		return "NTB_TOPO_SWITCH";
10701752501SLogan Gunthorpe 	case NTB_TOPO_CROSSLINK:	return "NTB_TOPO_CROSSLINK";
108a1bd3baeSAllen Hubbe 	}
109a1bd3baeSAllen Hubbe 	return "NTB_TOPO_INVALID";
110a1bd3baeSAllen Hubbe }
111a1bd3baeSAllen Hubbe 
112a1bd3baeSAllen Hubbe /**
113a1bd3baeSAllen Hubbe  * enum ntb_speed - NTB link training speed
114a1bd3baeSAllen Hubbe  * @NTB_SPEED_AUTO:	Request the max supported speed.
115a1bd3baeSAllen Hubbe  * @NTB_SPEED_NONE:	Link is not trained to any speed.
116a1bd3baeSAllen Hubbe  * @NTB_SPEED_GEN1:	Link is trained to gen1 speed.
117a1bd3baeSAllen Hubbe  * @NTB_SPEED_GEN2:	Link is trained to gen2 speed.
118a1bd3baeSAllen Hubbe  * @NTB_SPEED_GEN3:	Link is trained to gen3 speed.
11985dce3aaSSerge Semin  * @NTB_SPEED_GEN4:	Link is trained to gen4 speed.
120a1bd3baeSAllen Hubbe  */
121a1bd3baeSAllen Hubbe enum ntb_speed {
122a1bd3baeSAllen Hubbe 	NTB_SPEED_AUTO = -1,
123a1bd3baeSAllen Hubbe 	NTB_SPEED_NONE = 0,
124a1bd3baeSAllen Hubbe 	NTB_SPEED_GEN1 = 1,
125a1bd3baeSAllen Hubbe 	NTB_SPEED_GEN2 = 2,
126a1bd3baeSAllen Hubbe 	NTB_SPEED_GEN3 = 3,
12785dce3aaSSerge Semin 	NTB_SPEED_GEN4 = 4
128a1bd3baeSAllen Hubbe };
129a1bd3baeSAllen Hubbe 
130a1bd3baeSAllen Hubbe /**
131a1bd3baeSAllen Hubbe  * enum ntb_width - NTB link training width
132a1bd3baeSAllen Hubbe  * @NTB_WIDTH_AUTO:	Request the max supported width.
133a1bd3baeSAllen Hubbe  * @NTB_WIDTH_NONE:	Link is not trained to any width.
134a1bd3baeSAllen Hubbe  * @NTB_WIDTH_1:	Link is trained to 1 lane width.
135a1bd3baeSAllen Hubbe  * @NTB_WIDTH_2:	Link is trained to 2 lane width.
136a1bd3baeSAllen Hubbe  * @NTB_WIDTH_4:	Link is trained to 4 lane width.
137a1bd3baeSAllen Hubbe  * @NTB_WIDTH_8:	Link is trained to 8 lane width.
138a1bd3baeSAllen Hubbe  * @NTB_WIDTH_12:	Link is trained to 12 lane width.
139a1bd3baeSAllen Hubbe  * @NTB_WIDTH_16:	Link is trained to 16 lane width.
140a1bd3baeSAllen Hubbe  * @NTB_WIDTH_32:	Link is trained to 32 lane width.
141a1bd3baeSAllen Hubbe  */
142a1bd3baeSAllen Hubbe enum ntb_width {
143a1bd3baeSAllen Hubbe 	NTB_WIDTH_AUTO = -1,
144a1bd3baeSAllen Hubbe 	NTB_WIDTH_NONE = 0,
145a1bd3baeSAllen Hubbe 	NTB_WIDTH_1 = 1,
146a1bd3baeSAllen Hubbe 	NTB_WIDTH_2 = 2,
147a1bd3baeSAllen Hubbe 	NTB_WIDTH_4 = 4,
148a1bd3baeSAllen Hubbe 	NTB_WIDTH_8 = 8,
149a1bd3baeSAllen Hubbe 	NTB_WIDTH_12 = 12,
150a1bd3baeSAllen Hubbe 	NTB_WIDTH_16 = 16,
151a1bd3baeSAllen Hubbe 	NTB_WIDTH_32 = 32,
152a1bd3baeSAllen Hubbe };
153a1bd3baeSAllen Hubbe 
154a1bd3baeSAllen Hubbe /**
1551e530119SSerge Semin  * enum ntb_default_port - NTB default port number
1561e530119SSerge Semin  * @NTB_PORT_PRI_USD:	Default port of the NTB_TOPO_PRI/NTB_TOPO_B2B_USD
1571e530119SSerge Semin  *			topologies
1581e530119SSerge Semin  * @NTB_PORT_SEC_DSD:	Default port of the NTB_TOPO_SEC/NTB_TOPO_B2B_DSD
1591e530119SSerge Semin  *			topologies
1601e530119SSerge Semin  */
1611e530119SSerge Semin enum ntb_default_port {
1621e530119SSerge Semin 	NTB_PORT_PRI_USD,
1631e530119SSerge Semin 	NTB_PORT_SEC_DSD
1641e530119SSerge Semin };
1651e530119SSerge Semin #define NTB_DEF_PEER_CNT	(1)
1661e530119SSerge Semin #define NTB_DEF_PEER_IDX	(0)
1671e530119SSerge Semin 
1681e530119SSerge Semin /**
169a1bd3baeSAllen Hubbe  * struct ntb_client_ops - ntb client operations
170a1bd3baeSAllen Hubbe  * @probe:		Notify client of a new device.
171a1bd3baeSAllen Hubbe  * @remove:		Notify client to remove a device.
172a1bd3baeSAllen Hubbe  */
173a1bd3baeSAllen Hubbe struct ntb_client_ops {
174a1bd3baeSAllen Hubbe 	int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
175a1bd3baeSAllen Hubbe 	void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
176a1bd3baeSAllen Hubbe };
177a1bd3baeSAllen Hubbe 
ntb_client_ops_is_valid(const struct ntb_client_ops * ops)178a1bd3baeSAllen Hubbe static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
179a1bd3baeSAllen Hubbe {
180a1bd3baeSAllen Hubbe 	/* commented callbacks are not required: */
181a1bd3baeSAllen Hubbe 	return
182a1bd3baeSAllen Hubbe 		ops->probe			&&
183a1bd3baeSAllen Hubbe 		ops->remove			&&
184a1bd3baeSAllen Hubbe 		1;
185a1bd3baeSAllen Hubbe }
186a1bd3baeSAllen Hubbe 
187a1bd3baeSAllen Hubbe /**
188a1bd3baeSAllen Hubbe  * struct ntb_ctx_ops - ntb driver context operations
189a1bd3baeSAllen Hubbe  * @link_event:		See ntb_link_event().
190a1bd3baeSAllen Hubbe  * @db_event:		See ntb_db_event().
191bc3e49adSSerge Semin  * @msg_event:		See ntb_msg_event().
192a1bd3baeSAllen Hubbe  */
193a1bd3baeSAllen Hubbe struct ntb_ctx_ops {
194a1bd3baeSAllen Hubbe 	void (*link_event)(void *ctx);
195a1bd3baeSAllen Hubbe 	void (*db_event)(void *ctx, int db_vector);
196bc3e49adSSerge Semin 	void (*msg_event)(void *ctx);
197a1bd3baeSAllen Hubbe };
198a1bd3baeSAllen Hubbe 
ntb_ctx_ops_is_valid(const struct ntb_ctx_ops * ops)199a1bd3baeSAllen Hubbe static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
200a1bd3baeSAllen Hubbe {
201a1bd3baeSAllen Hubbe 	/* commented callbacks are not required: */
202a1bd3baeSAllen Hubbe 	return
203a1bd3baeSAllen Hubbe 		/* ops->link_event		&& */
204a1bd3baeSAllen Hubbe 		/* ops->db_event		&& */
205bc3e49adSSerge Semin 		/* ops->msg_event		&& */
206a1bd3baeSAllen Hubbe 		1;
207a1bd3baeSAllen Hubbe }
208a1bd3baeSAllen Hubbe 
209a1bd3baeSAllen Hubbe /**
21018c8c095SWesley Sheng  * struct ntb_dev_ops - ntb device operations
2111e530119SSerge Semin  * @port_number:	See ntb_port_number().
2121e530119SSerge Semin  * @peer_port_count:	See ntb_peer_port_count().
2131e530119SSerge Semin  * @peer_port_number:	See ntb_peer_port_number().
2141e530119SSerge Semin  * @peer_port_idx:	See ntb_peer_port_idx().
21560934b20SSerge Semin  * @link_is_up:		See ntb_link_is_up().
21660934b20SSerge Semin  * @link_enable:	See ntb_link_enable().
21760934b20SSerge Semin  * @link_disable:	See ntb_link_disable().
218a1bd3baeSAllen Hubbe  * @mw_count:		See ntb_mw_count().
219443b9a14SSerge Semin  * @mw_get_align:	See ntb_mw_get_align().
220a1bd3baeSAllen Hubbe  * @mw_set_trans:	See ntb_mw_set_trans().
221a1bd3baeSAllen Hubbe  * @mw_clear_trans:	See ntb_mw_clear_trans().
222443b9a14SSerge Semin  * @peer_mw_count:	See ntb_peer_mw_count().
223443b9a14SSerge Semin  * @peer_mw_get_addr:	See ntb_peer_mw_get_addr().
224443b9a14SSerge Semin  * @peer_mw_set_trans:	See ntb_peer_mw_set_trans().
225443b9a14SSerge Semin  * @peer_mw_clear_trans:See ntb_peer_mw_clear_trans().
226a1bd3baeSAllen Hubbe  * @db_is_unsafe:	See ntb_db_is_unsafe().
227a1bd3baeSAllen Hubbe  * @db_valid_mask:	See ntb_db_valid_mask().
228a1bd3baeSAllen Hubbe  * @db_vector_count:	See ntb_db_vector_count().
229a1bd3baeSAllen Hubbe  * @db_vector_mask:	See ntb_db_vector_mask().
230a1bd3baeSAllen Hubbe  * @db_read:		See ntb_db_read().
231a1bd3baeSAllen Hubbe  * @db_set:		See ntb_db_set().
232a1bd3baeSAllen Hubbe  * @db_clear:		See ntb_db_clear().
233a1bd3baeSAllen Hubbe  * @db_read_mask:	See ntb_db_read_mask().
234a1bd3baeSAllen Hubbe  * @db_set_mask:	See ntb_db_set_mask().
235a1bd3baeSAllen Hubbe  * @db_clear_mask:	See ntb_db_clear_mask().
236a1bd3baeSAllen Hubbe  * @peer_db_addr:	See ntb_peer_db_addr().
237a1bd3baeSAllen Hubbe  * @peer_db_read:	See ntb_peer_db_read().
238a1bd3baeSAllen Hubbe  * @peer_db_set:	See ntb_peer_db_set().
239a1bd3baeSAllen Hubbe  * @peer_db_clear:	See ntb_peer_db_clear().
240a1bd3baeSAllen Hubbe  * @peer_db_read_mask:	See ntb_peer_db_read_mask().
241a1bd3baeSAllen Hubbe  * @peer_db_set_mask:	See ntb_peer_db_set_mask().
242a1bd3baeSAllen Hubbe  * @peer_db_clear_mask:	See ntb_peer_db_clear_mask().
243a1bd3baeSAllen Hubbe  * @spad_is_unsafe:	See ntb_spad_is_unsafe().
244a1bd3baeSAllen Hubbe  * @spad_count:		See ntb_spad_count().
245a1bd3baeSAllen Hubbe  * @spad_read:		See ntb_spad_read().
246a1bd3baeSAllen Hubbe  * @spad_write:		See ntb_spad_write().
247a1bd3baeSAllen Hubbe  * @peer_spad_addr:	See ntb_peer_spad_addr().
248a1bd3baeSAllen Hubbe  * @peer_spad_read:	See ntb_peer_spad_read().
249a1bd3baeSAllen Hubbe  * @peer_spad_write:	See ntb_peer_spad_write().
250bc3e49adSSerge Semin  * @msg_count:		See ntb_msg_count().
251bc3e49adSSerge Semin  * @msg_inbits:		See ntb_msg_inbits().
252bc3e49adSSerge Semin  * @msg_outbits:	See ntb_msg_outbits().
253bc3e49adSSerge Semin  * @msg_read_sts:	See ntb_msg_read_sts().
254bc3e49adSSerge Semin  * @msg_clear_sts:	See ntb_msg_clear_sts().
255bc3e49adSSerge Semin  * @msg_set_mask:	See ntb_msg_set_mask().
256bc3e49adSSerge Semin  * @msg_clear_mask:	See ntb_msg_clear_mask().
257bc3e49adSSerge Semin  * @msg_read:		See ntb_msg_read().
258b87ab219SSerge Semin  * @peer_msg_write:	See ntb_peer_msg_write().
259a1bd3baeSAllen Hubbe  */
260a1bd3baeSAllen Hubbe struct ntb_dev_ops {
2611e530119SSerge Semin 	int (*port_number)(struct ntb_dev *ntb);
2621e530119SSerge Semin 	int (*peer_port_count)(struct ntb_dev *ntb);
2631e530119SSerge Semin 	int (*peer_port_number)(struct ntb_dev *ntb, int pidx);
2641e530119SSerge Semin 	int (*peer_port_idx)(struct ntb_dev *ntb, int port);
2651e530119SSerge Semin 
2664e8c11b7SSerge Semin 	u64 (*link_is_up)(struct ntb_dev *ntb,
26760934b20SSerge Semin 			  enum ntb_speed *speed, enum ntb_width *width);
26860934b20SSerge Semin 	int (*link_enable)(struct ntb_dev *ntb,
26960934b20SSerge Semin 			   enum ntb_speed max_speed, enum ntb_width max_width);
27060934b20SSerge Semin 	int (*link_disable)(struct ntb_dev *ntb);
27160934b20SSerge Semin 
272443b9a14SSerge Semin 	int (*mw_count)(struct ntb_dev *ntb, int pidx);
273443b9a14SSerge Semin 	int (*mw_get_align)(struct ntb_dev *ntb, int pidx, int widx,
274443b9a14SSerge Semin 			    resource_size_t *addr_align,
275443b9a14SSerge Semin 			    resource_size_t *size_align,
276443b9a14SSerge Semin 			    resource_size_t *size_max);
277443b9a14SSerge Semin 	int (*mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
278a1bd3baeSAllen Hubbe 			    dma_addr_t addr, resource_size_t size);
279443b9a14SSerge Semin 	int (*mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
280443b9a14SSerge Semin 	int (*peer_mw_count)(struct ntb_dev *ntb);
281443b9a14SSerge Semin 	int (*peer_mw_get_addr)(struct ntb_dev *ntb, int widx,
282443b9a14SSerge Semin 				phys_addr_t *base, resource_size_t *size);
283443b9a14SSerge Semin 	int (*peer_mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
284443b9a14SSerge Semin 				 u64 addr, resource_size_t size);
285443b9a14SSerge Semin 	int (*peer_mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
286a1bd3baeSAllen Hubbe 
287a1bd3baeSAllen Hubbe 	int (*db_is_unsafe)(struct ntb_dev *ntb);
288a1bd3baeSAllen Hubbe 	u64 (*db_valid_mask)(struct ntb_dev *ntb);
289a1bd3baeSAllen Hubbe 	int (*db_vector_count)(struct ntb_dev *ntb);
290a1bd3baeSAllen Hubbe 	u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
291a1bd3baeSAllen Hubbe 
292a1bd3baeSAllen Hubbe 	u64 (*db_read)(struct ntb_dev *ntb);
293a1bd3baeSAllen Hubbe 	int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
294a1bd3baeSAllen Hubbe 	int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
295a1bd3baeSAllen Hubbe 
296a1bd3baeSAllen Hubbe 	u64 (*db_read_mask)(struct ntb_dev *ntb);
297a1bd3baeSAllen Hubbe 	int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
298a1bd3baeSAllen Hubbe 	int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
299a1bd3baeSAllen Hubbe 
300a1bd3baeSAllen Hubbe 	int (*peer_db_addr)(struct ntb_dev *ntb,
301ebb09b33SLeonid Ravich 			    phys_addr_t *db_addr, resource_size_t *db_size,
302ebb09b33SLeonid Ravich 				u64 *db_data, int db_bit);
303a1bd3baeSAllen Hubbe 	u64 (*peer_db_read)(struct ntb_dev *ntb);
304a1bd3baeSAllen Hubbe 	int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
305a1bd3baeSAllen Hubbe 	int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
306a1bd3baeSAllen Hubbe 
307a1bd3baeSAllen Hubbe 	u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
308a1bd3baeSAllen Hubbe 	int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
309a1bd3baeSAllen Hubbe 	int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
310a1bd3baeSAllen Hubbe 
311a1bd3baeSAllen Hubbe 	int (*spad_is_unsafe)(struct ntb_dev *ntb);
312a1bd3baeSAllen Hubbe 	int (*spad_count)(struct ntb_dev *ntb);
313a1bd3baeSAllen Hubbe 
314d67288a3SSerge Semin 	u32 (*spad_read)(struct ntb_dev *ntb, int sidx);
315d67288a3SSerge Semin 	int (*spad_write)(struct ntb_dev *ntb, int sidx, u32 val);
316a1bd3baeSAllen Hubbe 
317d67288a3SSerge Semin 	int (*peer_spad_addr)(struct ntb_dev *ntb, int pidx, int sidx,
318a1bd3baeSAllen Hubbe 			      phys_addr_t *spad_addr);
319d67288a3SSerge Semin 	u32 (*peer_spad_read)(struct ntb_dev *ntb, int pidx, int sidx);
320d67288a3SSerge Semin 	int (*peer_spad_write)(struct ntb_dev *ntb, int pidx, int sidx,
321d67288a3SSerge Semin 			       u32 val);
322bc3e49adSSerge Semin 
323bc3e49adSSerge Semin 	int (*msg_count)(struct ntb_dev *ntb);
324bc3e49adSSerge Semin 	u64 (*msg_inbits)(struct ntb_dev *ntb);
325bc3e49adSSerge Semin 	u64 (*msg_outbits)(struct ntb_dev *ntb);
326bc3e49adSSerge Semin 	u64 (*msg_read_sts)(struct ntb_dev *ntb);
327bc3e49adSSerge Semin 	int (*msg_clear_sts)(struct ntb_dev *ntb, u64 sts_bits);
328bc3e49adSSerge Semin 	int (*msg_set_mask)(struct ntb_dev *ntb, u64 mask_bits);
329bc3e49adSSerge Semin 	int (*msg_clear_mask)(struct ntb_dev *ntb, u64 mask_bits);
330b87ab219SSerge Semin 	u32 (*msg_read)(struct ntb_dev *ntb, int *pidx, int midx);
331b87ab219SSerge Semin 	int (*peer_msg_write)(struct ntb_dev *ntb, int pidx, int midx, u32 msg);
332a1bd3baeSAllen Hubbe };
333a1bd3baeSAllen Hubbe 
ntb_dev_ops_is_valid(const struct ntb_dev_ops * ops)334a1bd3baeSAllen Hubbe static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
335a1bd3baeSAllen Hubbe {
336a1bd3baeSAllen Hubbe 	/* commented callbacks are not required: */
337a1bd3baeSAllen Hubbe 	return
3383c69f5d6SSerge Semin 		/* Port operations are required for multiport devices */
3391e530119SSerge Semin 		!ops->peer_port_count == !ops->port_number	&&
3401e530119SSerge Semin 		!ops->peer_port_number == !ops->port_number	&&
3411e530119SSerge Semin 		!ops->peer_port_idx == !ops->port_number	&&
3423c69f5d6SSerge Semin 
3433c69f5d6SSerge Semin 		/* Link operations are required */
34460934b20SSerge Semin 		ops->link_is_up				&&
34560934b20SSerge Semin 		ops->link_enable			&&
34660934b20SSerge Semin 		ops->link_disable			&&
3473c69f5d6SSerge Semin 
3483c69f5d6SSerge Semin 		/* One or both MW interfaces should be developed */
349a1bd3baeSAllen Hubbe 		ops->mw_count				&&
350443b9a14SSerge Semin 		ops->mw_get_align			&&
351443b9a14SSerge Semin 		(ops->mw_set_trans			||
352443b9a14SSerge Semin 		 ops->peer_mw_set_trans)		&&
353a1bd3baeSAllen Hubbe 		/* ops->mw_clear_trans			&& */
354443b9a14SSerge Semin 		ops->peer_mw_count			&&
355443b9a14SSerge Semin 		ops->peer_mw_get_addr			&&
356443b9a14SSerge Semin 		/* ops->peer_mw_clear_trans		&& */
35760934b20SSerge Semin 
3583c69f5d6SSerge Semin 		/* Doorbell operations are mostly required */
359a1bd3baeSAllen Hubbe 		/* ops->db_is_unsafe			&& */
360a1bd3baeSAllen Hubbe 		ops->db_valid_mask			&&
361a1bd3baeSAllen Hubbe 		/* both set, or both unset */
362a1bd3baeSAllen Hubbe 		(!ops->db_vector_count == !ops->db_vector_mask)	&&
363a1bd3baeSAllen Hubbe 		ops->db_read				&&
364a1bd3baeSAllen Hubbe 		/* ops->db_set				&& */
365a1bd3baeSAllen Hubbe 		ops->db_clear				&&
366a1bd3baeSAllen Hubbe 		/* ops->db_read_mask			&& */
367a1bd3baeSAllen Hubbe 		ops->db_set_mask			&&
368a1bd3baeSAllen Hubbe 		ops->db_clear_mask			&&
369afc54992SAllen Hubbe 		/* ops->peer_db_addr			&& */
370a1bd3baeSAllen Hubbe 		/* ops->peer_db_read			&& */
371a1bd3baeSAllen Hubbe 		ops->peer_db_set			&&
372a1bd3baeSAllen Hubbe 		/* ops->peer_db_clear			&& */
373a1bd3baeSAllen Hubbe 		/* ops->peer_db_read_mask		&& */
374a1bd3baeSAllen Hubbe 		/* ops->peer_db_set_mask		&& */
375a1bd3baeSAllen Hubbe 		/* ops->peer_db_clear_mask		&& */
3763c69f5d6SSerge Semin 
3773c69f5d6SSerge Semin 		/* Scrachpads interface is optional */
378d67288a3SSerge Semin 		/* !ops->spad_is_unsafe == !ops->spad_count	&& */
379d67288a3SSerge Semin 		!ops->spad_read == !ops->spad_count		&&
380d67288a3SSerge Semin 		!ops->spad_write == !ops->spad_count		&&
381d67288a3SSerge Semin 		/* !ops->peer_spad_addr == !ops->spad_count	&& */
382d67288a3SSerge Semin 		/* !ops->peer_spad_read == !ops->spad_count	&& */
383d67288a3SSerge Semin 		!ops->peer_spad_write == !ops->spad_count	&&
384bc3e49adSSerge Semin 
3853c69f5d6SSerge Semin 		/* Messaging interface is optional */
386bc3e49adSSerge Semin 		!ops->msg_inbits == !ops->msg_count		&&
387bc3e49adSSerge Semin 		!ops->msg_outbits == !ops->msg_count		&&
388bc3e49adSSerge Semin 		!ops->msg_read_sts == !ops->msg_count		&&
389bc3e49adSSerge Semin 		!ops->msg_clear_sts == !ops->msg_count		&&
390bc3e49adSSerge Semin 		/* !ops->msg_set_mask == !ops->msg_count	&& */
391bc3e49adSSerge Semin 		/* !ops->msg_clear_mask == !ops->msg_count	&& */
392bc3e49adSSerge Semin 		!ops->msg_read == !ops->msg_count		&&
393b87ab219SSerge Semin 		!ops->peer_msg_write == !ops->msg_count		&&
394a1bd3baeSAllen Hubbe 		1;
395a1bd3baeSAllen Hubbe }
396a1bd3baeSAllen Hubbe 
397a1bd3baeSAllen Hubbe /**
398a1bd3baeSAllen Hubbe  * struct ntb_client - client interested in ntb devices
399a1bd3baeSAllen Hubbe  * @drv:		Linux driver object.
400a1bd3baeSAllen Hubbe  * @ops:		See &ntb_client_ops.
401a1bd3baeSAllen Hubbe  */
402a1bd3baeSAllen Hubbe struct ntb_client {
403a1bd3baeSAllen Hubbe 	struct device_driver		drv;
404a1bd3baeSAllen Hubbe 	const struct ntb_client_ops	ops;
405a1bd3baeSAllen Hubbe };
406a1bd3baeSAllen Hubbe #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
407a1bd3baeSAllen Hubbe 
408a1bd3baeSAllen Hubbe /**
40918c8c095SWesley Sheng  * struct ntb_dev - ntb device
410a1bd3baeSAllen Hubbe  * @dev:		Linux device object.
4113c69f5d6SSerge Semin  * @pdev:		PCI device entry of the ntb.
412a1bd3baeSAllen Hubbe  * @topo:		Detected topology of the ntb.
413a1bd3baeSAllen Hubbe  * @ops:		See &ntb_dev_ops.
414a1bd3baeSAllen Hubbe  * @ctx:		See &ntb_ctx_ops.
415a1bd3baeSAllen Hubbe  * @ctx_ops:		See &ntb_ctx_ops.
416a1bd3baeSAllen Hubbe  */
417a1bd3baeSAllen Hubbe struct ntb_dev {
418a1bd3baeSAllen Hubbe 	struct device			dev;
419a1bd3baeSAllen Hubbe 	struct pci_dev			*pdev;
420a1bd3baeSAllen Hubbe 	enum ntb_topo			topo;
421a1bd3baeSAllen Hubbe 	const struct ntb_dev_ops	*ops;
422a1bd3baeSAllen Hubbe 	void				*ctx;
423a1bd3baeSAllen Hubbe 	const struct ntb_ctx_ops	*ctx_ops;
424a1bd3baeSAllen Hubbe 
425a1bd3baeSAllen Hubbe 	/* private: */
426a1bd3baeSAllen Hubbe 
427a1bd3baeSAllen Hubbe 	/* synchronize setting, clearing, and calling ctx_ops */
428a1bd3baeSAllen Hubbe 	spinlock_t			ctx_lock;
429a1bd3baeSAllen Hubbe 	/* block unregister until device is fully released */
430a1bd3baeSAllen Hubbe 	struct completion		released;
43126b3a37bSLogan Gunthorpe 
43226b3a37bSLogan Gunthorpe #ifdef CONFIG_NTB_MSI
43326b3a37bSLogan Gunthorpe 	struct ntb_msi *msi;
43426b3a37bSLogan Gunthorpe #endif
435a1bd3baeSAllen Hubbe };
436a1bd3baeSAllen Hubbe #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
437a1bd3baeSAllen Hubbe 
438a1bd3baeSAllen Hubbe /**
439a1bd3baeSAllen Hubbe  * ntb_register_client() - register a client for interest in ntb devices
440a1bd3baeSAllen Hubbe  * @client:	Client context.
441a1bd3baeSAllen Hubbe  *
442a1bd3baeSAllen Hubbe  * The client will be added to the list of clients interested in ntb devices.
443a1bd3baeSAllen Hubbe  * The client will be notified of any ntb devices that are not already
444a1bd3baeSAllen Hubbe  * associated with a client, or if ntb devices are registered later.
445a1bd3baeSAllen Hubbe  *
446a1bd3baeSAllen Hubbe  * Return: Zero if the client is registered, otherwise an error number.
447a1bd3baeSAllen Hubbe  */
448a1bd3baeSAllen Hubbe #define ntb_register_client(client) \
449a1bd3baeSAllen Hubbe 	__ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
450a1bd3baeSAllen Hubbe 
451a1bd3baeSAllen Hubbe int __ntb_register_client(struct ntb_client *client, struct module *mod,
452a1bd3baeSAllen Hubbe 			  const char *mod_name);
453a1bd3baeSAllen Hubbe 
454a1bd3baeSAllen Hubbe /**
455a1bd3baeSAllen Hubbe  * ntb_unregister_client() - unregister a client for interest in ntb devices
456a1bd3baeSAllen Hubbe  * @client:	Client context.
457a1bd3baeSAllen Hubbe  *
458a1bd3baeSAllen Hubbe  * The client will be removed from the list of clients interested in ntb
459a1bd3baeSAllen Hubbe  * devices.  If any ntb devices are associated with the client, the client will
460a1bd3baeSAllen Hubbe  * be notified to remove those devices.
461a1bd3baeSAllen Hubbe  */
462a1bd3baeSAllen Hubbe void ntb_unregister_client(struct ntb_client *client);
463a1bd3baeSAllen Hubbe 
464a1bd3baeSAllen Hubbe #define module_ntb_client(__ntb_client) \
465a1bd3baeSAllen Hubbe 	module_driver(__ntb_client, ntb_register_client, \
466a1bd3baeSAllen Hubbe 			ntb_unregister_client)
467a1bd3baeSAllen Hubbe 
468a1bd3baeSAllen Hubbe /**
469a1bd3baeSAllen Hubbe  * ntb_register_device() - register a ntb device
470a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
471a1bd3baeSAllen Hubbe  *
472a1bd3baeSAllen Hubbe  * The device will be added to the list of ntb devices.  If any clients are
473a1bd3baeSAllen Hubbe  * interested in ntb devices, each client will be notified of the ntb device,
474a1bd3baeSAllen Hubbe  * until at most one client accepts the device.
475a1bd3baeSAllen Hubbe  *
476a1bd3baeSAllen Hubbe  * Return: Zero if the device is registered, otherwise an error number.
477a1bd3baeSAllen Hubbe  */
478a1bd3baeSAllen Hubbe int ntb_register_device(struct ntb_dev *ntb);
479a1bd3baeSAllen Hubbe 
480a1bd3baeSAllen Hubbe /**
481f454f4d1SMaciej Grochowski  * ntb_unregister_device() - unregister a ntb device
482a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
483a1bd3baeSAllen Hubbe  *
484a1bd3baeSAllen Hubbe  * The device will be removed from the list of ntb devices.  If the ntb device
485a1bd3baeSAllen Hubbe  * is associated with a client, the client will be notified to remove the
486a1bd3baeSAllen Hubbe  * device.
487a1bd3baeSAllen Hubbe  */
488a1bd3baeSAllen Hubbe void ntb_unregister_device(struct ntb_dev *ntb);
489a1bd3baeSAllen Hubbe 
490a1bd3baeSAllen Hubbe /**
491a1bd3baeSAllen Hubbe  * ntb_set_ctx() - associate a driver context with an ntb device
492a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
493a1bd3baeSAllen Hubbe  * @ctx:	Driver context.
494a1bd3baeSAllen Hubbe  * @ctx_ops:	Driver context operations.
495a1bd3baeSAllen Hubbe  *
496a1bd3baeSAllen Hubbe  * Associate a driver context and operations with a ntb device.  The context is
497a1bd3baeSAllen Hubbe  * provided by the client driver, and the driver may associate a different
498a1bd3baeSAllen Hubbe  * context with each ntb device.
499a1bd3baeSAllen Hubbe  *
500a1bd3baeSAllen Hubbe  * Return: Zero if the context is associated, otherwise an error number.
501a1bd3baeSAllen Hubbe  */
502a1bd3baeSAllen Hubbe int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
503a1bd3baeSAllen Hubbe 		const struct ntb_ctx_ops *ctx_ops);
504a1bd3baeSAllen Hubbe 
505a1bd3baeSAllen Hubbe /**
506a1bd3baeSAllen Hubbe  * ntb_clear_ctx() - disassociate any driver context from an ntb device
507a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
508a1bd3baeSAllen Hubbe  *
509a1bd3baeSAllen Hubbe  * Clear any association that may exist between a driver context and the ntb
510a1bd3baeSAllen Hubbe  * device.
511a1bd3baeSAllen Hubbe  */
512a1bd3baeSAllen Hubbe void ntb_clear_ctx(struct ntb_dev *ntb);
513a1bd3baeSAllen Hubbe 
514a1bd3baeSAllen Hubbe /**
515a1bd3baeSAllen Hubbe  * ntb_link_event() - notify driver context of a change in link status
516a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
517a1bd3baeSAllen Hubbe  *
518a1bd3baeSAllen Hubbe  * Notify the driver context that the link status may have changed.  The driver
519a1bd3baeSAllen Hubbe  * should call ntb_link_is_up() to get the current status.
520a1bd3baeSAllen Hubbe  */
521a1bd3baeSAllen Hubbe void ntb_link_event(struct ntb_dev *ntb);
522a1bd3baeSAllen Hubbe 
523a1bd3baeSAllen Hubbe /**
524a1bd3baeSAllen Hubbe  * ntb_db_event() - notify driver context of a doorbell event
525a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
526a1bd3baeSAllen Hubbe  * @vector:	Interrupt vector number.
527a1bd3baeSAllen Hubbe  *
528a1bd3baeSAllen Hubbe  * Notify the driver context of a doorbell event.  If hardware supports
529a1bd3baeSAllen Hubbe  * multiple interrupt vectors for doorbells, the vector number indicates which
530a1bd3baeSAllen Hubbe  * vector received the interrupt.  The vector number is relative to the first
531a1bd3baeSAllen Hubbe  * vector used for doorbells, starting at zero, and must be less than
5323c69f5d6SSerge Semin  * ntb_db_vector_count().  The driver may call ntb_db_read() to check which
533a1bd3baeSAllen Hubbe  * doorbell bits need service, and ntb_db_vector_mask() to determine which of
534a1bd3baeSAllen Hubbe  * those bits are associated with the vector number.
535a1bd3baeSAllen Hubbe  */
536a1bd3baeSAllen Hubbe void ntb_db_event(struct ntb_dev *ntb, int vector);
537a1bd3baeSAllen Hubbe 
538a1bd3baeSAllen Hubbe /**
539bc3e49adSSerge Semin  * ntb_msg_event() - notify driver context of a message event
540bc3e49adSSerge Semin  * @ntb:	NTB device context.
541bc3e49adSSerge Semin  *
542bc3e49adSSerge Semin  * Notify the driver context of a message event.  If hardware supports
543bc3e49adSSerge Semin  * message registers, this event indicates, that a new message arrived in
544bc3e49adSSerge Semin  * some incoming message register or last sent message couldn't be delivered.
545bc3e49adSSerge Semin  * The events can be masked/unmasked by the methods ntb_msg_set_mask() and
546bc3e49adSSerge Semin  * ntb_msg_clear_mask().
547bc3e49adSSerge Semin  */
548bc3e49adSSerge Semin void ntb_msg_event(struct ntb_dev *ntb);
549bc3e49adSSerge Semin 
550bc3e49adSSerge Semin /**
5511e530119SSerge Semin  * ntb_default_port_number() - get the default local port number
5521e530119SSerge Semin  * @ntb:	NTB device context.
5531e530119SSerge Semin  *
5541e530119SSerge Semin  * If hardware driver doesn't specify port_number() callback method, the NTB
5551e530119SSerge Semin  * is considered with just two ports. So this method returns default local
5561e530119SSerge Semin  * port number in compliance with topology.
5571e530119SSerge Semin  *
5581e530119SSerge Semin  * NOTE Don't call this method directly. The ntb_port_number() function should
5591e530119SSerge Semin  * be used instead.
5601e530119SSerge Semin  *
5611e530119SSerge Semin  * Return: the default local port number
5621e530119SSerge Semin  */
5631e530119SSerge Semin int ntb_default_port_number(struct ntb_dev *ntb);
5641e530119SSerge Semin 
5651e530119SSerge Semin /**
5661e530119SSerge Semin  * ntb_default_port_count() - get the default number of peer device ports
5671e530119SSerge Semin  * @ntb:	NTB device context.
5681e530119SSerge Semin  *
5691e530119SSerge Semin  * By default hardware driver supports just one peer device.
5701e530119SSerge Semin  *
5711e530119SSerge Semin  * NOTE Don't call this method directly. The ntb_peer_port_count() function
5721e530119SSerge Semin  * should be used instead.
5731e530119SSerge Semin  *
5741e530119SSerge Semin  * Return: the default number of peer ports
5751e530119SSerge Semin  */
5761e530119SSerge Semin int ntb_default_peer_port_count(struct ntb_dev *ntb);
5771e530119SSerge Semin 
5781e530119SSerge Semin /**
5791e530119SSerge Semin  * ntb_default_peer_port_number() - get the default peer port by given index
5801e530119SSerge Semin  * @ntb:	NTB device context.
5811e530119SSerge Semin  * @idx:	Peer port index (should not differ from zero).
5821e530119SSerge Semin  *
5831e530119SSerge Semin  * By default hardware driver supports just one peer device, so this method
5841e530119SSerge Semin  * shall return the corresponding value from enum ntb_default_port.
5851e530119SSerge Semin  *
5861e530119SSerge Semin  * NOTE Don't call this method directly. The ntb_peer_port_number() function
5871e530119SSerge Semin  * should be used instead.
5881e530119SSerge Semin  *
5891e530119SSerge Semin  * Return: the peer device port or negative value indicating an error
5901e530119SSerge Semin  */
5911e530119SSerge Semin int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx);
5921e530119SSerge Semin 
5931e530119SSerge Semin /**
5941e530119SSerge Semin  * ntb_default_peer_port_idx() - get the default peer device port index by
5951e530119SSerge Semin  *				 given port number
5961e530119SSerge Semin  * @ntb:	NTB device context.
5971e530119SSerge Semin  * @port:	Peer port number (should be one of enum ntb_default_port).
5981e530119SSerge Semin  *
5991e530119SSerge Semin  * By default hardware driver supports just one peer device, so while
6001e530119SSerge Semin  * specified port-argument indicates peer port from enum ntb_default_port,
6011e530119SSerge Semin  * the return value shall be zero.
6021e530119SSerge Semin  *
6031e530119SSerge Semin  * NOTE Don't call this method directly. The ntb_peer_port_idx() function
6041e530119SSerge Semin  * should be used instead.
6051e530119SSerge Semin  *
6061e530119SSerge Semin  * Return: the peer port index or negative value indicating an error
6071e530119SSerge Semin  */
6081e530119SSerge Semin int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port);
6091e530119SSerge Semin 
6101e530119SSerge Semin /**
6111e530119SSerge Semin  * ntb_port_number() - get the local port number
6121e530119SSerge Semin  * @ntb:	NTB device context.
6131e530119SSerge Semin  *
6141e530119SSerge Semin  * Hardware must support at least simple two-ports ntb connection
6151e530119SSerge Semin  *
6161e530119SSerge Semin  * Return: the local port number
6171e530119SSerge Semin  */
ntb_port_number(struct ntb_dev * ntb)6181e530119SSerge Semin static inline int ntb_port_number(struct ntb_dev *ntb)
6191e530119SSerge Semin {
6201e530119SSerge Semin 	if (!ntb->ops->port_number)
6211e530119SSerge Semin 		return ntb_default_port_number(ntb);
6221e530119SSerge Semin 
6231e530119SSerge Semin 	return ntb->ops->port_number(ntb);
6241e530119SSerge Semin }
6251e530119SSerge Semin /**
6261e530119SSerge Semin  * ntb_peer_port_count() - get the number of peer device ports
6271e530119SSerge Semin  * @ntb:	NTB device context.
6281e530119SSerge Semin  *
6291e530119SSerge Semin  * Hardware may support an access to memory of several remote domains
6301e530119SSerge Semin  * over multi-port NTB devices. This method returns the number of peers,
6311e530119SSerge Semin  * local device can have shared memory with.
6321e530119SSerge Semin  *
6331e530119SSerge Semin  * Return: the number of peer ports
6341e530119SSerge Semin  */
ntb_peer_port_count(struct ntb_dev * ntb)6351e530119SSerge Semin static inline int ntb_peer_port_count(struct ntb_dev *ntb)
6361e530119SSerge Semin {
6371e530119SSerge Semin 	if (!ntb->ops->peer_port_count)
6381e530119SSerge Semin 		return ntb_default_peer_port_count(ntb);
6391e530119SSerge Semin 
6401e530119SSerge Semin 	return ntb->ops->peer_port_count(ntb);
6411e530119SSerge Semin }
6421e530119SSerge Semin 
6431e530119SSerge Semin /**
6441e530119SSerge Semin  * ntb_peer_port_number() - get the peer port by given index
6451e530119SSerge Semin  * @ntb:	NTB device context.
6461e530119SSerge Semin  * @pidx:	Peer port index.
6471e530119SSerge Semin  *
6481e530119SSerge Semin  * Peer ports are continuously enumerated by NTB API logic, so this method
6491e530119SSerge Semin  * lets to retrieve port real number by its index.
6501e530119SSerge Semin  *
6511e530119SSerge Semin  * Return: the peer device port or negative value indicating an error
6521e530119SSerge Semin  */
ntb_peer_port_number(struct ntb_dev * ntb,int pidx)6531e530119SSerge Semin static inline int ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
6541e530119SSerge Semin {
6551e530119SSerge Semin 	if (!ntb->ops->peer_port_number)
6561e530119SSerge Semin 		return ntb_default_peer_port_number(ntb, pidx);
6571e530119SSerge Semin 
6581e530119SSerge Semin 	return ntb->ops->peer_port_number(ntb, pidx);
6591e530119SSerge Semin }
6601e530119SSerge Semin 
6611e530119SSerge Semin /**
662246a42c5SLogan Gunthorpe  * ntb_logical_port_number() - get the logical port number of the local port
663246a42c5SLogan Gunthorpe  * @ntb:	NTB device context.
664246a42c5SLogan Gunthorpe  *
665246a42c5SLogan Gunthorpe  * The Logical Port Number is defined to be a unique number for each
666246a42c5SLogan Gunthorpe  * port starting from zero through to the number of ports minus one.
667246a42c5SLogan Gunthorpe  * This is in contrast to the Port Number where each port can be assigned
668246a42c5SLogan Gunthorpe  * any unique physical number by the hardware.
669246a42c5SLogan Gunthorpe  *
670246a42c5SLogan Gunthorpe  * The logical port number is useful for calculating the resource indexes
671246a42c5SLogan Gunthorpe  * used by peers.
672246a42c5SLogan Gunthorpe  *
673246a42c5SLogan Gunthorpe  * Return: the logical port number or negative value indicating an error
674246a42c5SLogan Gunthorpe  */
ntb_logical_port_number(struct ntb_dev * ntb)675246a42c5SLogan Gunthorpe static inline int ntb_logical_port_number(struct ntb_dev *ntb)
676246a42c5SLogan Gunthorpe {
677246a42c5SLogan Gunthorpe 	int lport = ntb_port_number(ntb);
678246a42c5SLogan Gunthorpe 	int pidx;
679246a42c5SLogan Gunthorpe 
680246a42c5SLogan Gunthorpe 	if (lport < 0)
681246a42c5SLogan Gunthorpe 		return lport;
682246a42c5SLogan Gunthorpe 
683246a42c5SLogan Gunthorpe 	for (pidx = 0; pidx < ntb_peer_port_count(ntb); pidx++)
684246a42c5SLogan Gunthorpe 		if (lport <= ntb_peer_port_number(ntb, pidx))
685246a42c5SLogan Gunthorpe 			return pidx;
686246a42c5SLogan Gunthorpe 
687246a42c5SLogan Gunthorpe 	return pidx;
688246a42c5SLogan Gunthorpe }
689246a42c5SLogan Gunthorpe 
690246a42c5SLogan Gunthorpe /**
691246a42c5SLogan Gunthorpe  * ntb_peer_logical_port_number() - get the logical peer port by given index
692246a42c5SLogan Gunthorpe  * @ntb:	NTB device context.
693246a42c5SLogan Gunthorpe  * @pidx:	Peer port index.
694246a42c5SLogan Gunthorpe  *
695246a42c5SLogan Gunthorpe  * The Logical Port Number is defined to be a unique number for each
696246a42c5SLogan Gunthorpe  * port starting from zero through to the number of ports minus one.
697246a42c5SLogan Gunthorpe  * This is in contrast to the Port Number where each port can be assigned
698246a42c5SLogan Gunthorpe  * any unique physical number by the hardware.
699246a42c5SLogan Gunthorpe  *
700246a42c5SLogan Gunthorpe  * The logical port number is useful for calculating the resource indexes
701246a42c5SLogan Gunthorpe  * used by peers.
702246a42c5SLogan Gunthorpe  *
703246a42c5SLogan Gunthorpe  * Return: the peer's logical port number or negative value indicating an error
704246a42c5SLogan Gunthorpe  */
ntb_peer_logical_port_number(struct ntb_dev * ntb,int pidx)705246a42c5SLogan Gunthorpe static inline int ntb_peer_logical_port_number(struct ntb_dev *ntb, int pidx)
706246a42c5SLogan Gunthorpe {
707246a42c5SLogan Gunthorpe 	if (ntb_peer_port_number(ntb, pidx) < ntb_port_number(ntb))
708246a42c5SLogan Gunthorpe 		return pidx;
709246a42c5SLogan Gunthorpe 	else
710246a42c5SLogan Gunthorpe 		return pidx + 1;
711246a42c5SLogan Gunthorpe }
712246a42c5SLogan Gunthorpe 
713246a42c5SLogan Gunthorpe /**
7141e530119SSerge Semin  * ntb_peer_port_idx() - get the peer device port index by given port number
7151e530119SSerge Semin  * @ntb:	NTB device context.
7161e530119SSerge Semin  * @port:	Peer port number.
7171e530119SSerge Semin  *
7181e530119SSerge Semin  * Inverse operation of ntb_peer_port_number(), so one can get port index
7191e530119SSerge Semin  * by specified port number.
7201e530119SSerge Semin  *
7211e530119SSerge Semin  * Return: the peer port index or negative value indicating an error
7221e530119SSerge Semin  */
ntb_peer_port_idx(struct ntb_dev * ntb,int port)7231e530119SSerge Semin static inline int ntb_peer_port_idx(struct ntb_dev *ntb, int port)
7241e530119SSerge Semin {
7251e530119SSerge Semin 	if (!ntb->ops->peer_port_idx)
7261e530119SSerge Semin 		return ntb_default_peer_port_idx(ntb, port);
7271e530119SSerge Semin 
7281e530119SSerge Semin 	return ntb->ops->peer_port_idx(ntb, port);
7291e530119SSerge Semin }
7301e530119SSerge Semin 
7311e530119SSerge Semin /**
73260934b20SSerge Semin  * ntb_link_is_up() - get the current ntb link state
73360934b20SSerge Semin  * @ntb:	NTB device context.
73460934b20SSerge Semin  * @speed:	OUT - The link speed expressed as PCIe generation number.
73560934b20SSerge Semin  * @width:	OUT - The link width expressed as the number of PCIe lanes.
73660934b20SSerge Semin  *
73760934b20SSerge Semin  * Get the current state of the ntb link.  It is recommended to query the link
73860934b20SSerge Semin  * state once after every link event.  It is safe to query the link state in
73960934b20SSerge Semin  * the context of the link event callback.
74060934b20SSerge Semin  *
7414e8c11b7SSerge Semin  * Return: bitfield of indexed ports link state: bit is set/cleared if the
7424e8c11b7SSerge Semin  *         link is up/down respectively.
74360934b20SSerge Semin  */
ntb_link_is_up(struct ntb_dev * ntb,enum ntb_speed * speed,enum ntb_width * width)7444e8c11b7SSerge Semin static inline u64 ntb_link_is_up(struct ntb_dev *ntb,
74560934b20SSerge Semin 				 enum ntb_speed *speed, enum ntb_width *width)
74660934b20SSerge Semin {
74760934b20SSerge Semin 	return ntb->ops->link_is_up(ntb, speed, width);
74860934b20SSerge Semin }
74960934b20SSerge Semin 
75060934b20SSerge Semin /**
7514e8c11b7SSerge Semin  * ntb_link_enable() - enable the local port ntb connection
75260934b20SSerge Semin  * @ntb:	NTB device context.
75360934b20SSerge Semin  * @max_speed:	The maximum link speed expressed as PCIe generation number.
75460934b20SSerge Semin  * @max_width:	The maximum link width expressed as the number of PCIe lanes.
75560934b20SSerge Semin  *
7564e8c11b7SSerge Semin  * Enable the NTB/PCIe link on the local or remote (for bridge-to-bridge
7574e8c11b7SSerge Semin  * topology) side of the bridge. If it's supported the ntb device should train
7584e8c11b7SSerge Semin  * the link to its maximum speed and width, or the requested speed and width,
7594e8c11b7SSerge Semin  * whichever is smaller. Some hardware doesn't support PCIe link training, so
7604e8c11b7SSerge Semin  * the last two arguments will be ignored then.
76160934b20SSerge Semin  *
76260934b20SSerge Semin  * Return: Zero on success, otherwise an error number.
76360934b20SSerge Semin  */
ntb_link_enable(struct ntb_dev * ntb,enum ntb_speed max_speed,enum ntb_width max_width)76460934b20SSerge Semin static inline int ntb_link_enable(struct ntb_dev *ntb,
76560934b20SSerge Semin 				  enum ntb_speed max_speed,
76660934b20SSerge Semin 				  enum ntb_width max_width)
76760934b20SSerge Semin {
76860934b20SSerge Semin 	return ntb->ops->link_enable(ntb, max_speed, max_width);
76960934b20SSerge Semin }
77060934b20SSerge Semin 
77160934b20SSerge Semin /**
7724e8c11b7SSerge Semin  * ntb_link_disable() - disable the local port ntb connection
77360934b20SSerge Semin  * @ntb:	NTB device context.
77460934b20SSerge Semin  *
7754e8c11b7SSerge Semin  * Disable the link on the local or remote (for b2b topology) of the ntb.
7764e8c11b7SSerge Semin  * The ntb device should disable the link.  Returning from this call must
7774e8c11b7SSerge Semin  * indicate that a barrier has passed, though with no more writes may pass in
7784e8c11b7SSerge Semin  * either direction across the link, except if this call returns an error
7794e8c11b7SSerge Semin  * number.
78060934b20SSerge Semin  *
78160934b20SSerge Semin  * Return: Zero on success, otherwise an error number.
78260934b20SSerge Semin  */
ntb_link_disable(struct ntb_dev * ntb)78360934b20SSerge Semin static inline int ntb_link_disable(struct ntb_dev *ntb)
78460934b20SSerge Semin {
78560934b20SSerge Semin 	return ntb->ops->link_disable(ntb);
78660934b20SSerge Semin }
78760934b20SSerge Semin 
78860934b20SSerge Semin /**
789443b9a14SSerge Semin  * ntb_mw_count() - get the number of inbound memory windows, which could
790443b9a14SSerge Semin  *                  be created for a specified peer device
791a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
792443b9a14SSerge Semin  * @pidx:	Port index of peer device.
793a1bd3baeSAllen Hubbe  *
794a1bd3baeSAllen Hubbe  * Hardware and topology may support a different number of memory windows.
795443b9a14SSerge Semin  * Moreover different peer devices can support different number of memory
796443b9a14SSerge Semin  * windows. Simply speaking this method returns the number of possible inbound
797fa5ab66eSLogan Gunthorpe  * memory windows to share with specified peer device. Note: this may return
798fa5ab66eSLogan Gunthorpe  * zero if the link is not up yet.
799a1bd3baeSAllen Hubbe  *
800a1bd3baeSAllen Hubbe  * Return: the number of memory windows.
801a1bd3baeSAllen Hubbe  */
ntb_mw_count(struct ntb_dev * ntb,int pidx)802443b9a14SSerge Semin static inline int ntb_mw_count(struct ntb_dev *ntb, int pidx)
803a1bd3baeSAllen Hubbe {
804443b9a14SSerge Semin 	return ntb->ops->mw_count(ntb, pidx);
805a1bd3baeSAllen Hubbe }
806a1bd3baeSAllen Hubbe 
807a1bd3baeSAllen Hubbe /**
808443b9a14SSerge Semin  * ntb_mw_get_align() - get the restriction parameters of inbound memory window
809a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
810443b9a14SSerge Semin  * @pidx:	Port index of peer device.
811443b9a14SSerge Semin  * @widx:	Memory window index.
812443b9a14SSerge Semin  * @addr_align:	OUT - the base alignment for translating the memory window
813443b9a14SSerge Semin  * @size_align:	OUT - the size alignment for translating the memory window
814443b9a14SSerge Semin  * @size_max:	OUT - the maximum size of the memory window
815a1bd3baeSAllen Hubbe  *
816443b9a14SSerge Semin  * Get the alignments of an inbound memory window with specified index.
817443b9a14SSerge Semin  * NULL may be given for any output parameter if the value is not needed.
818443b9a14SSerge Semin  * The alignment and size parameters may be used for allocation of proper
819fa5ab66eSLogan Gunthorpe  * shared memory. Note: this must only be called when the link is up.
820a1bd3baeSAllen Hubbe  *
821443b9a14SSerge Semin  * Return: Zero on success, otherwise a negative error number.
822a1bd3baeSAllen Hubbe  */
ntb_mw_get_align(struct ntb_dev * ntb,int pidx,int widx,resource_size_t * addr_align,resource_size_t * size_align,resource_size_t * size_max)823443b9a14SSerge Semin static inline int ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
824443b9a14SSerge Semin 				   resource_size_t *addr_align,
825443b9a14SSerge Semin 				   resource_size_t *size_align,
826443b9a14SSerge Semin 				   resource_size_t *size_max)
827a1bd3baeSAllen Hubbe {
828f1678a4cSSerge Semin 	if (!(ntb_link_is_up(ntb, NULL, NULL) & BIT_ULL(pidx)))
829fa5ab66eSLogan Gunthorpe 		return -ENOTCONN;
830fa5ab66eSLogan Gunthorpe 
831443b9a14SSerge Semin 	return ntb->ops->mw_get_align(ntb, pidx, widx, addr_align, size_align,
832443b9a14SSerge Semin 				      size_max);
833a1bd3baeSAllen Hubbe }
834a1bd3baeSAllen Hubbe 
835a1bd3baeSAllen Hubbe /**
836443b9a14SSerge Semin  * ntb_mw_set_trans() - set the translation of an inbound memory window
837a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
838443b9a14SSerge Semin  * @pidx:	Port index of peer device.
839443b9a14SSerge Semin  * @widx:	Memory window index.
840443b9a14SSerge Semin  * @addr:	The dma address of local memory to expose to the peer.
841a1bd3baeSAllen Hubbe  * @size:	The size of the local memory to expose to the peer.
842a1bd3baeSAllen Hubbe  *
843a1bd3baeSAllen Hubbe  * Set the translation of a memory window.  The peer may access local memory
844a1bd3baeSAllen Hubbe  * through the window starting at the address, up to the size.  The address
845443b9a14SSerge Semin  * and size must be aligned in compliance with restrictions of
846443b9a14SSerge Semin  * ntb_mw_get_align(). The region size should not exceed the size_max parameter
847443b9a14SSerge Semin  * of that method.
848443b9a14SSerge Semin  *
849443b9a14SSerge Semin  * This method may not be implemented due to the hardware specific memory
850443b9a14SSerge Semin  * windows interface.
851a1bd3baeSAllen Hubbe  *
852a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
853a1bd3baeSAllen Hubbe  */
ntb_mw_set_trans(struct ntb_dev * ntb,int pidx,int widx,dma_addr_t addr,resource_size_t size)854443b9a14SSerge Semin static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
855a1bd3baeSAllen Hubbe 				   dma_addr_t addr, resource_size_t size)
856a1bd3baeSAllen Hubbe {
857443b9a14SSerge Semin 	if (!ntb->ops->mw_set_trans)
858443b9a14SSerge Semin 		return 0;
859443b9a14SSerge Semin 
860443b9a14SSerge Semin 	return ntb->ops->mw_set_trans(ntb, pidx, widx, addr, size);
861a1bd3baeSAllen Hubbe }
862a1bd3baeSAllen Hubbe 
863a1bd3baeSAllen Hubbe /**
864443b9a14SSerge Semin  * ntb_mw_clear_trans() - clear the translation address of an inbound memory
865443b9a14SSerge Semin  *                        window
866a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
867443b9a14SSerge Semin  * @pidx:	Port index of peer device.
868443b9a14SSerge Semin  * @widx:	Memory window index.
869a1bd3baeSAllen Hubbe  *
870443b9a14SSerge Semin  * Clear the translation of an inbound memory window.  The peer may no longer
871443b9a14SSerge Semin  * access local memory through the window.
872a1bd3baeSAllen Hubbe  *
873a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
874a1bd3baeSAllen Hubbe  */
ntb_mw_clear_trans(struct ntb_dev * ntb,int pidx,int widx)875443b9a14SSerge Semin static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int pidx, int widx)
876a1bd3baeSAllen Hubbe {
877a1bd3baeSAllen Hubbe 	if (!ntb->ops->mw_clear_trans)
878443b9a14SSerge Semin 		return ntb_mw_set_trans(ntb, pidx, widx, 0, 0);
879a1bd3baeSAllen Hubbe 
880443b9a14SSerge Semin 	return ntb->ops->mw_clear_trans(ntb, pidx, widx);
881443b9a14SSerge Semin }
882443b9a14SSerge Semin 
883443b9a14SSerge Semin /**
884443b9a14SSerge Semin  * ntb_peer_mw_count() - get the number of outbound memory windows, which could
885443b9a14SSerge Semin  *                       be mapped to access a shared memory
886443b9a14SSerge Semin  * @ntb:	NTB device context.
887443b9a14SSerge Semin  *
888443b9a14SSerge Semin  * Hardware and topology may support a different number of memory windows.
889443b9a14SSerge Semin  * This method returns the number of outbound memory windows supported by
890443b9a14SSerge Semin  * local device.
891443b9a14SSerge Semin  *
892443b9a14SSerge Semin  * Return: the number of memory windows.
893443b9a14SSerge Semin  */
ntb_peer_mw_count(struct ntb_dev * ntb)894443b9a14SSerge Semin static inline int ntb_peer_mw_count(struct ntb_dev *ntb)
895443b9a14SSerge Semin {
896443b9a14SSerge Semin 	return ntb->ops->peer_mw_count(ntb);
897443b9a14SSerge Semin }
898443b9a14SSerge Semin 
899443b9a14SSerge Semin /**
900443b9a14SSerge Semin  * ntb_peer_mw_get_addr() - get map address of an outbound memory window
901443b9a14SSerge Semin  * @ntb:	NTB device context.
902443b9a14SSerge Semin  * @widx:	Memory window index (within ntb_peer_mw_count() return value).
903443b9a14SSerge Semin  * @base:	OUT - the base address of mapping region.
904443b9a14SSerge Semin  * @size:	OUT - the size of mapping region.
905443b9a14SSerge Semin  *
906443b9a14SSerge Semin  * Get base and size of memory region to map.  NULL may be given for any output
907443b9a14SSerge Semin  * parameter if the value is not needed.  The base and size may be used for
908443b9a14SSerge Semin  * mapping the memory window, to access the peer memory.
909443b9a14SSerge Semin  *
910443b9a14SSerge Semin  * Return: Zero on success, otherwise a negative error number.
911443b9a14SSerge Semin  */
ntb_peer_mw_get_addr(struct ntb_dev * ntb,int widx,phys_addr_t * base,resource_size_t * size)912443b9a14SSerge Semin static inline int ntb_peer_mw_get_addr(struct ntb_dev *ntb, int widx,
913443b9a14SSerge Semin 				      phys_addr_t *base, resource_size_t *size)
914443b9a14SSerge Semin {
915443b9a14SSerge Semin 	return ntb->ops->peer_mw_get_addr(ntb, widx, base, size);
916443b9a14SSerge Semin }
917443b9a14SSerge Semin 
918443b9a14SSerge Semin /**
919443b9a14SSerge Semin  * ntb_peer_mw_set_trans() - set a translation address of a memory window
920443b9a14SSerge Semin  *                           retrieved from a peer device
921443b9a14SSerge Semin  * @ntb:	NTB device context.
922443b9a14SSerge Semin  * @pidx:	Port index of peer device the translation address received from.
923443b9a14SSerge Semin  * @widx:	Memory window index.
924443b9a14SSerge Semin  * @addr:	The dma address of the shared memory to access.
925443b9a14SSerge Semin  * @size:	The size of the shared memory to access.
926443b9a14SSerge Semin  *
927443b9a14SSerge Semin  * Set the translation of an outbound memory window.  The local device may
928443b9a14SSerge Semin  * access shared memory allocated by a peer device sent the address.
929443b9a14SSerge Semin  *
930443b9a14SSerge Semin  * This method may not be implemented due to the hardware specific memory
931443b9a14SSerge Semin  * windows interface, so a translation address can be only set on the side,
932443b9a14SSerge Semin  * where shared memory (inbound memory windows) is allocated.
933443b9a14SSerge Semin  *
934443b9a14SSerge Semin  * Return: Zero on success, otherwise an error number.
935443b9a14SSerge Semin  */
ntb_peer_mw_set_trans(struct ntb_dev * ntb,int pidx,int widx,u64 addr,resource_size_t size)936443b9a14SSerge Semin static inline int ntb_peer_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
937443b9a14SSerge Semin 					u64 addr, resource_size_t size)
938443b9a14SSerge Semin {
939443b9a14SSerge Semin 	if (!ntb->ops->peer_mw_set_trans)
940443b9a14SSerge Semin 		return 0;
941443b9a14SSerge Semin 
942443b9a14SSerge Semin 	return ntb->ops->peer_mw_set_trans(ntb, pidx, widx, addr, size);
943443b9a14SSerge Semin }
944443b9a14SSerge Semin 
945443b9a14SSerge Semin /**
946443b9a14SSerge Semin  * ntb_peer_mw_clear_trans() - clear the translation address of an outbound
947443b9a14SSerge Semin  *                             memory window
948443b9a14SSerge Semin  * @ntb:	NTB device context.
949443b9a14SSerge Semin  * @pidx:	Port index of peer device.
950443b9a14SSerge Semin  * @widx:	Memory window index.
951443b9a14SSerge Semin  *
952443b9a14SSerge Semin  * Clear the translation of a outbound memory window.  The local device may no
953443b9a14SSerge Semin  * longer access a shared memory through the window.
954443b9a14SSerge Semin  *
955443b9a14SSerge Semin  * This method may not be implemented due to the hardware specific memory
956443b9a14SSerge Semin  * windows interface.
957443b9a14SSerge Semin  *
958443b9a14SSerge Semin  * Return: Zero on success, otherwise an error number.
959443b9a14SSerge Semin  */
ntb_peer_mw_clear_trans(struct ntb_dev * ntb,int pidx,int widx)960443b9a14SSerge Semin static inline int ntb_peer_mw_clear_trans(struct ntb_dev *ntb, int pidx,
961443b9a14SSerge Semin 					  int widx)
962443b9a14SSerge Semin {
963443b9a14SSerge Semin 	if (!ntb->ops->peer_mw_clear_trans)
964443b9a14SSerge Semin 		return ntb_peer_mw_set_trans(ntb, pidx, widx, 0, 0);
965443b9a14SSerge Semin 
966443b9a14SSerge Semin 	return ntb->ops->peer_mw_clear_trans(ntb, pidx, widx);
967a1bd3baeSAllen Hubbe }
968a1bd3baeSAllen Hubbe 
969a1bd3baeSAllen Hubbe /**
970a1bd3baeSAllen Hubbe  * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
971a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
972a1bd3baeSAllen Hubbe  *
973a1bd3baeSAllen Hubbe  * It is possible for some ntb hardware to be affected by errata.  Hardware
974a1bd3baeSAllen Hubbe  * drivers can advise clients to avoid using doorbells.  Clients may ignore
975a1bd3baeSAllen Hubbe  * this advice, though caution is recommended.
976a1bd3baeSAllen Hubbe  *
977a1bd3baeSAllen Hubbe  * Return: Zero if it is safe to use doorbells, or One if it is not safe.
978a1bd3baeSAllen Hubbe  */
ntb_db_is_unsafe(struct ntb_dev * ntb)979a1bd3baeSAllen Hubbe static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
980a1bd3baeSAllen Hubbe {
981a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_is_unsafe)
982a1bd3baeSAllen Hubbe 		return 0;
983a1bd3baeSAllen Hubbe 
984a1bd3baeSAllen Hubbe 	return ntb->ops->db_is_unsafe(ntb);
985a1bd3baeSAllen Hubbe }
986a1bd3baeSAllen Hubbe 
987a1bd3baeSAllen Hubbe /**
988a1bd3baeSAllen Hubbe  * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
989a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
990a1bd3baeSAllen Hubbe  *
991a1bd3baeSAllen Hubbe  * Hardware may support different number or arrangement of doorbell bits.
992a1bd3baeSAllen Hubbe  *
993a1bd3baeSAllen Hubbe  * Return: A mask of doorbell bits supported by the ntb.
994a1bd3baeSAllen Hubbe  */
ntb_db_valid_mask(struct ntb_dev * ntb)995a1bd3baeSAllen Hubbe static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
996a1bd3baeSAllen Hubbe {
997a1bd3baeSAllen Hubbe 	return ntb->ops->db_valid_mask(ntb);
998a1bd3baeSAllen Hubbe }
999a1bd3baeSAllen Hubbe 
1000a1bd3baeSAllen Hubbe /**
1001a1bd3baeSAllen Hubbe  * ntb_db_vector_count() - get the number of doorbell interrupt vectors
1002a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1003a1bd3baeSAllen Hubbe  *
1004a1bd3baeSAllen Hubbe  * Hardware may support different number of interrupt vectors.
1005a1bd3baeSAllen Hubbe  *
1006a1bd3baeSAllen Hubbe  * Return: The number of doorbell interrupt vectors.
1007a1bd3baeSAllen Hubbe  */
ntb_db_vector_count(struct ntb_dev * ntb)1008a1bd3baeSAllen Hubbe static inline int ntb_db_vector_count(struct ntb_dev *ntb)
1009a1bd3baeSAllen Hubbe {
1010a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_vector_count)
1011a1bd3baeSAllen Hubbe 		return 1;
1012a1bd3baeSAllen Hubbe 
1013a1bd3baeSAllen Hubbe 	return ntb->ops->db_vector_count(ntb);
1014a1bd3baeSAllen Hubbe }
1015a1bd3baeSAllen Hubbe 
1016a1bd3baeSAllen Hubbe /**
1017a1bd3baeSAllen Hubbe  * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
1018a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1019a1bd3baeSAllen Hubbe  * @vector:	Doorbell vector number.
1020a1bd3baeSAllen Hubbe  *
1021a1bd3baeSAllen Hubbe  * Each interrupt vector may have a different number or arrangement of bits.
1022a1bd3baeSAllen Hubbe  *
1023a1bd3baeSAllen Hubbe  * Return: A mask of doorbell bits serviced by a vector.
1024a1bd3baeSAllen Hubbe  */
ntb_db_vector_mask(struct ntb_dev * ntb,int vector)1025a1bd3baeSAllen Hubbe static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
1026a1bd3baeSAllen Hubbe {
1027a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_vector_mask)
1028a1bd3baeSAllen Hubbe 		return ntb_db_valid_mask(ntb);
1029a1bd3baeSAllen Hubbe 
1030a1bd3baeSAllen Hubbe 	return ntb->ops->db_vector_mask(ntb, vector);
1031a1bd3baeSAllen Hubbe }
1032a1bd3baeSAllen Hubbe 
1033a1bd3baeSAllen Hubbe /**
1034a1bd3baeSAllen Hubbe  * ntb_db_read() - read the local doorbell register
1035a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1036a1bd3baeSAllen Hubbe  *
1037a1bd3baeSAllen Hubbe  * Read the local doorbell register, and return the bits that are set.
1038a1bd3baeSAllen Hubbe  *
1039a1bd3baeSAllen Hubbe  * Return: The bits currently set in the local doorbell register.
1040a1bd3baeSAllen Hubbe  */
ntb_db_read(struct ntb_dev * ntb)1041a1bd3baeSAllen Hubbe static inline u64 ntb_db_read(struct ntb_dev *ntb)
1042a1bd3baeSAllen Hubbe {
1043a1bd3baeSAllen Hubbe 	return ntb->ops->db_read(ntb);
1044a1bd3baeSAllen Hubbe }
1045a1bd3baeSAllen Hubbe 
1046a1bd3baeSAllen Hubbe /**
1047a1bd3baeSAllen Hubbe  * ntb_db_set() - set bits in the local doorbell register
1048a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1049a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to set.
1050a1bd3baeSAllen Hubbe  *
1051a1bd3baeSAllen Hubbe  * Set bits in the local doorbell register, which may generate a local doorbell
1052a1bd3baeSAllen Hubbe  * interrupt.  Bits that were already set must remain set.
1053a1bd3baeSAllen Hubbe  *
1054a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1055a1bd3baeSAllen Hubbe  *
1056a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1057a1bd3baeSAllen Hubbe  */
ntb_db_set(struct ntb_dev * ntb,u64 db_bits)1058a1bd3baeSAllen Hubbe static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
1059a1bd3baeSAllen Hubbe {
1060a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_set)
1061a1bd3baeSAllen Hubbe 		return -EINVAL;
1062a1bd3baeSAllen Hubbe 
1063a1bd3baeSAllen Hubbe 	return ntb->ops->db_set(ntb, db_bits);
1064a1bd3baeSAllen Hubbe }
1065a1bd3baeSAllen Hubbe 
1066a1bd3baeSAllen Hubbe /**
1067a1bd3baeSAllen Hubbe  * ntb_db_clear() - clear bits in the local doorbell register
1068a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1069a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to clear.
1070a1bd3baeSAllen Hubbe  *
1071a1bd3baeSAllen Hubbe  * Clear bits in the local doorbell register, arming the bits for the next
1072a1bd3baeSAllen Hubbe  * doorbell.
1073a1bd3baeSAllen Hubbe  *
1074a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1075a1bd3baeSAllen Hubbe  */
ntb_db_clear(struct ntb_dev * ntb,u64 db_bits)1076a1bd3baeSAllen Hubbe static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1077a1bd3baeSAllen Hubbe {
1078a1bd3baeSAllen Hubbe 	return ntb->ops->db_clear(ntb, db_bits);
1079a1bd3baeSAllen Hubbe }
1080a1bd3baeSAllen Hubbe 
1081a1bd3baeSAllen Hubbe /**
1082a1bd3baeSAllen Hubbe  * ntb_db_read_mask() - read the local doorbell mask
1083a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1084a1bd3baeSAllen Hubbe  *
1085a1bd3baeSAllen Hubbe  * Read the local doorbell mask register, and return the bits that are set.
1086a1bd3baeSAllen Hubbe  *
1087a1bd3baeSAllen Hubbe  * This is unusual, though hardware is likely to support it.
1088a1bd3baeSAllen Hubbe  *
1089a1bd3baeSAllen Hubbe  * Return: The bits currently set in the local doorbell mask register.
1090a1bd3baeSAllen Hubbe  */
ntb_db_read_mask(struct ntb_dev * ntb)1091a1bd3baeSAllen Hubbe static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
1092a1bd3baeSAllen Hubbe {
1093a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_read_mask)
1094a1bd3baeSAllen Hubbe 		return 0;
1095a1bd3baeSAllen Hubbe 
1096a1bd3baeSAllen Hubbe 	return ntb->ops->db_read_mask(ntb);
1097a1bd3baeSAllen Hubbe }
1098a1bd3baeSAllen Hubbe 
1099a1bd3baeSAllen Hubbe /**
1100a1bd3baeSAllen Hubbe  * ntb_db_set_mask() - set bits in the local doorbell mask
1101a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1102a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell mask bits to set.
1103a1bd3baeSAllen Hubbe  *
1104a1bd3baeSAllen Hubbe  * Set bits in the local doorbell mask register, preventing doorbell interrupts
1105a1bd3baeSAllen Hubbe  * from being generated for those doorbell bits.  Bits that were already set
1106a1bd3baeSAllen Hubbe  * must remain set.
1107a1bd3baeSAllen Hubbe  *
1108a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1109a1bd3baeSAllen Hubbe  */
ntb_db_set_mask(struct ntb_dev * ntb,u64 db_bits)1110a1bd3baeSAllen Hubbe static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1111a1bd3baeSAllen Hubbe {
1112a1bd3baeSAllen Hubbe 	return ntb->ops->db_set_mask(ntb, db_bits);
1113a1bd3baeSAllen Hubbe }
1114a1bd3baeSAllen Hubbe 
1115a1bd3baeSAllen Hubbe /**
1116a1bd3baeSAllen Hubbe  * ntb_db_clear_mask() - clear bits in the local doorbell mask
1117a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1118a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to clear.
1119a1bd3baeSAllen Hubbe  *
1120a1bd3baeSAllen Hubbe  * Clear bits in the local doorbell mask register, allowing doorbell interrupts
1121a1bd3baeSAllen Hubbe  * from being generated for those doorbell bits.  If a doorbell bit is already
1122a1bd3baeSAllen Hubbe  * set at the time the mask is cleared, and the corresponding mask bit is
1123a1bd3baeSAllen Hubbe  * changed from set to clear, then the ntb driver must ensure that
1124a1bd3baeSAllen Hubbe  * ntb_db_event() is called.  If the hardware does not generate the interrupt
1125a1bd3baeSAllen Hubbe  * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
1126a1bd3baeSAllen Hubbe  *
1127a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1128a1bd3baeSAllen Hubbe  */
ntb_db_clear_mask(struct ntb_dev * ntb,u64 db_bits)1129a1bd3baeSAllen Hubbe static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1130a1bd3baeSAllen Hubbe {
1131a1bd3baeSAllen Hubbe 	return ntb->ops->db_clear_mask(ntb, db_bits);
1132a1bd3baeSAllen Hubbe }
1133a1bd3baeSAllen Hubbe 
1134a1bd3baeSAllen Hubbe /**
1135a1bd3baeSAllen Hubbe  * ntb_peer_db_addr() - address and size of the peer doorbell register
1136a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1137a1bd3baeSAllen Hubbe  * @db_addr:	OUT - The address of the peer doorbell register.
1138a1bd3baeSAllen Hubbe  * @db_size:	OUT - The number of bytes to write the peer doorbell register.
1139ebb09b33SLeonid Ravich  * @db_data:	OUT - The data of peer doorbell register
1140ebb09b33SLeonid Ravich  * @db_bit:		door bell bit number
1141a1bd3baeSAllen Hubbe  *
1142a1bd3baeSAllen Hubbe  * Return the address of the peer doorbell register.  This may be used, for
1143a1bd3baeSAllen Hubbe  * example, by drivers that offload memory copy operations to a dma engine.
1144a1bd3baeSAllen Hubbe  * The drivers may wish to ring the peer doorbell at the completion of memory
1145a1bd3baeSAllen Hubbe  * copy operations.  For efficiency, and to simplify ordering of operations
1146a1bd3baeSAllen Hubbe  * between the dma memory copies and the ringing doorbell, the driver may
1147a1bd3baeSAllen Hubbe  * append one additional dma memory copy with the doorbell register as the
1148a1bd3baeSAllen Hubbe  * destination, after the memory copy operations.
1149a1bd3baeSAllen Hubbe  *
1150a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1151a1bd3baeSAllen Hubbe  */
ntb_peer_db_addr(struct ntb_dev * ntb,phys_addr_t * db_addr,resource_size_t * db_size,u64 * db_data,int db_bit)1152a1bd3baeSAllen Hubbe static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
1153a1bd3baeSAllen Hubbe 				   phys_addr_t *db_addr,
1154ebb09b33SLeonid Ravich 				   resource_size_t *db_size,
1155ebb09b33SLeonid Ravich 				   u64 *db_data, int db_bit)
1156a1bd3baeSAllen Hubbe {
1157afc54992SAllen Hubbe 	if (!ntb->ops->peer_db_addr)
1158afc54992SAllen Hubbe 		return -EINVAL;
1159afc54992SAllen Hubbe 
1160ebb09b33SLeonid Ravich 	return ntb->ops->peer_db_addr(ntb, db_addr, db_size, db_data, db_bit);
1161a1bd3baeSAllen Hubbe }
1162a1bd3baeSAllen Hubbe 
1163a1bd3baeSAllen Hubbe /**
1164a1bd3baeSAllen Hubbe  * ntb_peer_db_read() - read the peer doorbell register
1165a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1166a1bd3baeSAllen Hubbe  *
1167a1bd3baeSAllen Hubbe  * Read the peer doorbell register, and return the bits that are set.
1168a1bd3baeSAllen Hubbe  *
1169a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1170a1bd3baeSAllen Hubbe  *
1171a1bd3baeSAllen Hubbe  * Return: The bits currently set in the peer doorbell register.
1172a1bd3baeSAllen Hubbe  */
ntb_peer_db_read(struct ntb_dev * ntb)1173a1bd3baeSAllen Hubbe static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
1174a1bd3baeSAllen Hubbe {
1175a1bd3baeSAllen Hubbe 	if (!ntb->ops->peer_db_read)
1176a1bd3baeSAllen Hubbe 		return 0;
1177a1bd3baeSAllen Hubbe 
1178a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_read(ntb);
1179a1bd3baeSAllen Hubbe }
1180a1bd3baeSAllen Hubbe 
1181a1bd3baeSAllen Hubbe /**
1182a1bd3baeSAllen Hubbe  * ntb_peer_db_set() - set bits in the peer doorbell register
1183a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1184a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to set.
1185a1bd3baeSAllen Hubbe  *
1186a1bd3baeSAllen Hubbe  * Set bits in the peer doorbell register, which may generate a peer doorbell
1187a1bd3baeSAllen Hubbe  * interrupt.  Bits that were already set must remain set.
1188a1bd3baeSAllen Hubbe  *
1189a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1190a1bd3baeSAllen Hubbe  */
ntb_peer_db_set(struct ntb_dev * ntb,u64 db_bits)1191a1bd3baeSAllen Hubbe static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
1192a1bd3baeSAllen Hubbe {
1193a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_set(ntb, db_bits);
1194a1bd3baeSAllen Hubbe }
1195a1bd3baeSAllen Hubbe 
1196a1bd3baeSAllen Hubbe /**
119786663c91SAllen Hubbe  * ntb_peer_db_clear() - clear bits in the peer doorbell register
1198a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1199a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to clear.
1200a1bd3baeSAllen Hubbe  *
1201a1bd3baeSAllen Hubbe  * Clear bits in the peer doorbell register, arming the bits for the next
1202a1bd3baeSAllen Hubbe  * doorbell.
1203a1bd3baeSAllen Hubbe  *
1204a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1205a1bd3baeSAllen Hubbe  *
1206a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1207a1bd3baeSAllen Hubbe  */
ntb_peer_db_clear(struct ntb_dev * ntb,u64 db_bits)1208a1bd3baeSAllen Hubbe static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
1209a1bd3baeSAllen Hubbe {
1210a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_clear)
1211a1bd3baeSAllen Hubbe 		return -EINVAL;
1212a1bd3baeSAllen Hubbe 
1213a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_clear(ntb, db_bits);
1214a1bd3baeSAllen Hubbe }
1215a1bd3baeSAllen Hubbe 
1216a1bd3baeSAllen Hubbe /**
1217a1bd3baeSAllen Hubbe  * ntb_peer_db_read_mask() - read the peer doorbell mask
1218a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1219a1bd3baeSAllen Hubbe  *
1220a1bd3baeSAllen Hubbe  * Read the peer doorbell mask register, and return the bits that are set.
1221a1bd3baeSAllen Hubbe  *
1222a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1223a1bd3baeSAllen Hubbe  *
1224a1bd3baeSAllen Hubbe  * Return: The bits currently set in the peer doorbell mask register.
1225a1bd3baeSAllen Hubbe  */
ntb_peer_db_read_mask(struct ntb_dev * ntb)1226a1bd3baeSAllen Hubbe static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
1227a1bd3baeSAllen Hubbe {
1228a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_read_mask)
1229a1bd3baeSAllen Hubbe 		return 0;
1230a1bd3baeSAllen Hubbe 
1231a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_read_mask(ntb);
1232a1bd3baeSAllen Hubbe }
1233a1bd3baeSAllen Hubbe 
1234a1bd3baeSAllen Hubbe /**
1235a1bd3baeSAllen Hubbe  * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
1236a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1237a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell mask bits to set.
1238a1bd3baeSAllen Hubbe  *
1239a1bd3baeSAllen Hubbe  * Set bits in the peer doorbell mask register, preventing doorbell interrupts
1240a1bd3baeSAllen Hubbe  * from being generated for those doorbell bits.  Bits that were already set
1241a1bd3baeSAllen Hubbe  * must remain set.
1242a1bd3baeSAllen Hubbe  *
1243a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1244a1bd3baeSAllen Hubbe  *
1245a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1246a1bd3baeSAllen Hubbe  */
ntb_peer_db_set_mask(struct ntb_dev * ntb,u64 db_bits)1247a1bd3baeSAllen Hubbe static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1248a1bd3baeSAllen Hubbe {
1249a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_set_mask)
1250a1bd3baeSAllen Hubbe 		return -EINVAL;
1251a1bd3baeSAllen Hubbe 
1252a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_set_mask(ntb, db_bits);
1253a1bd3baeSAllen Hubbe }
1254a1bd3baeSAllen Hubbe 
1255a1bd3baeSAllen Hubbe /**
1256a1bd3baeSAllen Hubbe  * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
1257a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1258a1bd3baeSAllen Hubbe  * @db_bits:	Doorbell bits to clear.
1259a1bd3baeSAllen Hubbe  *
1260a1bd3baeSAllen Hubbe  * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
1261a1bd3baeSAllen Hubbe  * from being generated for those doorbell bits.  If the hardware does not
1262a1bd3baeSAllen Hubbe  * generate the interrupt on clearing the mask bit, then the driver should not
1263a1bd3baeSAllen Hubbe  * implement this function!
1264a1bd3baeSAllen Hubbe  *
1265a1bd3baeSAllen Hubbe  * This is unusual, and hardware may not support it.
1266a1bd3baeSAllen Hubbe  *
1267a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1268a1bd3baeSAllen Hubbe  */
ntb_peer_db_clear_mask(struct ntb_dev * ntb,u64 db_bits)1269a1bd3baeSAllen Hubbe static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1270a1bd3baeSAllen Hubbe {
1271a1bd3baeSAllen Hubbe 	if (!ntb->ops->db_clear_mask)
1272a1bd3baeSAllen Hubbe 		return -EINVAL;
1273a1bd3baeSAllen Hubbe 
1274a1bd3baeSAllen Hubbe 	return ntb->ops->peer_db_clear_mask(ntb, db_bits);
1275a1bd3baeSAllen Hubbe }
1276a1bd3baeSAllen Hubbe 
1277a1bd3baeSAllen Hubbe /**
1278a1bd3baeSAllen Hubbe  * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
1279a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1280a1bd3baeSAllen Hubbe  *
1281a1bd3baeSAllen Hubbe  * It is possible for some ntb hardware to be affected by errata.  Hardware
1282a1bd3baeSAllen Hubbe  * drivers can advise clients to avoid using scratchpads.  Clients may ignore
1283a1bd3baeSAllen Hubbe  * this advice, though caution is recommended.
1284a1bd3baeSAllen Hubbe  *
1285a1bd3baeSAllen Hubbe  * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
1286a1bd3baeSAllen Hubbe  */
ntb_spad_is_unsafe(struct ntb_dev * ntb)1287a1bd3baeSAllen Hubbe static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
1288a1bd3baeSAllen Hubbe {
1289a1bd3baeSAllen Hubbe 	if (!ntb->ops->spad_is_unsafe)
1290a1bd3baeSAllen Hubbe 		return 0;
1291a1bd3baeSAllen Hubbe 
1292a1bd3baeSAllen Hubbe 	return ntb->ops->spad_is_unsafe(ntb);
1293a1bd3baeSAllen Hubbe }
1294a1bd3baeSAllen Hubbe 
1295a1bd3baeSAllen Hubbe /**
129674dcba35SAaron Sierra  * ntb_spad_count() - get the number of scratchpads
1297a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1298a1bd3baeSAllen Hubbe  *
1299a1bd3baeSAllen Hubbe  * Hardware and topology may support a different number of scratchpads.
1300d67288a3SSerge Semin  * Although it must be the same for all ports per NTB device.
1301a1bd3baeSAllen Hubbe  *
1302a1bd3baeSAllen Hubbe  * Return: the number of scratchpads.
1303a1bd3baeSAllen Hubbe  */
ntb_spad_count(struct ntb_dev * ntb)1304a1bd3baeSAllen Hubbe static inline int ntb_spad_count(struct ntb_dev *ntb)
1305a1bd3baeSAllen Hubbe {
1306d67288a3SSerge Semin 	if (!ntb->ops->spad_count)
1307d67288a3SSerge Semin 		return 0;
1308d67288a3SSerge Semin 
1309a1bd3baeSAllen Hubbe 	return ntb->ops->spad_count(ntb);
1310a1bd3baeSAllen Hubbe }
1311a1bd3baeSAllen Hubbe 
1312a1bd3baeSAllen Hubbe /**
1313a1bd3baeSAllen Hubbe  * ntb_spad_read() - read the local scratchpad register
1314a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1315d67288a3SSerge Semin  * @sidx:	Scratchpad index.
1316a1bd3baeSAllen Hubbe  *
1317a1bd3baeSAllen Hubbe  * Read the local scratchpad register, and return the value.
1318a1bd3baeSAllen Hubbe  *
1319a1bd3baeSAllen Hubbe  * Return: The value of the local scratchpad register.
1320a1bd3baeSAllen Hubbe  */
ntb_spad_read(struct ntb_dev * ntb,int sidx)1321d67288a3SSerge Semin static inline u32 ntb_spad_read(struct ntb_dev *ntb, int sidx)
1322a1bd3baeSAllen Hubbe {
1323d67288a3SSerge Semin 	if (!ntb->ops->spad_read)
1324d67288a3SSerge Semin 		return ~(u32)0;
1325d67288a3SSerge Semin 
1326d67288a3SSerge Semin 	return ntb->ops->spad_read(ntb, sidx);
1327a1bd3baeSAllen Hubbe }
1328a1bd3baeSAllen Hubbe 
1329a1bd3baeSAllen Hubbe /**
1330a1bd3baeSAllen Hubbe  * ntb_spad_write() - write the local scratchpad register
1331a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1332d67288a3SSerge Semin  * @sidx:	Scratchpad index.
1333a1bd3baeSAllen Hubbe  * @val:	Scratchpad value.
1334a1bd3baeSAllen Hubbe  *
1335a1bd3baeSAllen Hubbe  * Write the value to the local scratchpad register.
1336a1bd3baeSAllen Hubbe  *
1337a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1338a1bd3baeSAllen Hubbe  */
ntb_spad_write(struct ntb_dev * ntb,int sidx,u32 val)1339d67288a3SSerge Semin static inline int ntb_spad_write(struct ntb_dev *ntb, int sidx, u32 val)
1340a1bd3baeSAllen Hubbe {
1341d67288a3SSerge Semin 	if (!ntb->ops->spad_write)
1342d67288a3SSerge Semin 		return -EINVAL;
1343d67288a3SSerge Semin 
1344d67288a3SSerge Semin 	return ntb->ops->spad_write(ntb, sidx, val);
1345a1bd3baeSAllen Hubbe }
1346a1bd3baeSAllen Hubbe 
1347a1bd3baeSAllen Hubbe /**
1348a1bd3baeSAllen Hubbe  * ntb_peer_spad_addr() - address of the peer scratchpad register
1349a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1350d67288a3SSerge Semin  * @pidx:	Port index of peer device.
1351d67288a3SSerge Semin  * @sidx:	Scratchpad index.
1352a1bd3baeSAllen Hubbe  * @spad_addr:	OUT - The address of the peer scratchpad register.
1353a1bd3baeSAllen Hubbe  *
1354*46f21af8SWesley Sheng  * Return the address of the peer scratchpad register.  This may be used, for
1355a1bd3baeSAllen Hubbe  * example, by drivers that offload memory copy operations to a dma engine.
1356a1bd3baeSAllen Hubbe  *
1357a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1358a1bd3baeSAllen Hubbe  */
ntb_peer_spad_addr(struct ntb_dev * ntb,int pidx,int sidx,phys_addr_t * spad_addr)1359d67288a3SSerge Semin static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int pidx, int sidx,
1360a1bd3baeSAllen Hubbe 				     phys_addr_t *spad_addr)
1361a1bd3baeSAllen Hubbe {
1362afc54992SAllen Hubbe 	if (!ntb->ops->peer_spad_addr)
1363afc54992SAllen Hubbe 		return -EINVAL;
1364afc54992SAllen Hubbe 
1365d67288a3SSerge Semin 	return ntb->ops->peer_spad_addr(ntb, pidx, sidx, spad_addr);
1366a1bd3baeSAllen Hubbe }
1367a1bd3baeSAllen Hubbe 
1368a1bd3baeSAllen Hubbe /**
1369a1bd3baeSAllen Hubbe  * ntb_peer_spad_read() - read the peer scratchpad register
1370a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1371d67288a3SSerge Semin  * @pidx:	Port index of peer device.
1372d67288a3SSerge Semin  * @sidx:	Scratchpad index.
1373a1bd3baeSAllen Hubbe  *
1374a1bd3baeSAllen Hubbe  * Read the peer scratchpad register, and return the value.
1375a1bd3baeSAllen Hubbe  *
1376*46f21af8SWesley Sheng  * Return: The value of the peer scratchpad register.
1377a1bd3baeSAllen Hubbe  */
ntb_peer_spad_read(struct ntb_dev * ntb,int pidx,int sidx)1378d67288a3SSerge Semin static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
1379a1bd3baeSAllen Hubbe {
13805c43c52dSSteven Wahl 	if (!ntb->ops->peer_spad_read)
1381d67288a3SSerge Semin 		return ~(u32)0;
13825c43c52dSSteven Wahl 
1383d67288a3SSerge Semin 	return ntb->ops->peer_spad_read(ntb, pidx, sidx);
1384a1bd3baeSAllen Hubbe }
1385a1bd3baeSAllen Hubbe 
1386a1bd3baeSAllen Hubbe /**
1387a1bd3baeSAllen Hubbe  * ntb_peer_spad_write() - write the peer scratchpad register
1388a1bd3baeSAllen Hubbe  * @ntb:	NTB device context.
1389d67288a3SSerge Semin  * @pidx:	Port index of peer device.
1390d67288a3SSerge Semin  * @sidx:	Scratchpad index.
1391a1bd3baeSAllen Hubbe  * @val:	Scratchpad value.
1392a1bd3baeSAllen Hubbe  *
1393a1bd3baeSAllen Hubbe  * Write the value to the peer scratchpad register.
1394a1bd3baeSAllen Hubbe  *
1395a1bd3baeSAllen Hubbe  * Return: Zero on success, otherwise an error number.
1396a1bd3baeSAllen Hubbe  */
ntb_peer_spad_write(struct ntb_dev * ntb,int pidx,int sidx,u32 val)1397d67288a3SSerge Semin static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int pidx, int sidx,
1398d67288a3SSerge Semin 				      u32 val)
1399a1bd3baeSAllen Hubbe {
1400d67288a3SSerge Semin 	if (!ntb->ops->peer_spad_write)
1401d67288a3SSerge Semin 		return -EINVAL;
1402d67288a3SSerge Semin 
1403d67288a3SSerge Semin 	return ntb->ops->peer_spad_write(ntb, pidx, sidx, val);
1404a1bd3baeSAllen Hubbe }
1405a1bd3baeSAllen Hubbe 
1406bc3e49adSSerge Semin /**
1407bc3e49adSSerge Semin  * ntb_msg_count() - get the number of message registers
1408bc3e49adSSerge Semin  * @ntb:	NTB device context.
1409bc3e49adSSerge Semin  *
1410bc3e49adSSerge Semin  * Hardware may support a different number of message registers.
1411bc3e49adSSerge Semin  *
1412bc3e49adSSerge Semin  * Return: the number of message registers.
1413bc3e49adSSerge Semin  */
ntb_msg_count(struct ntb_dev * ntb)1414bc3e49adSSerge Semin static inline int ntb_msg_count(struct ntb_dev *ntb)
1415bc3e49adSSerge Semin {
1416bc3e49adSSerge Semin 	if (!ntb->ops->msg_count)
1417bc3e49adSSerge Semin 		return 0;
1418bc3e49adSSerge Semin 
1419bc3e49adSSerge Semin 	return ntb->ops->msg_count(ntb);
1420bc3e49adSSerge Semin }
1421bc3e49adSSerge Semin 
1422bc3e49adSSerge Semin /**
1423bc3e49adSSerge Semin  * ntb_msg_inbits() - get a bitfield of inbound message registers status
1424bc3e49adSSerge Semin  * @ntb:	NTB device context.
1425bc3e49adSSerge Semin  *
1426bc3e49adSSerge Semin  * The method returns the bitfield of status and mask registers, which related
1427bc3e49adSSerge Semin  * to inbound message registers.
1428bc3e49adSSerge Semin  *
1429bc3e49adSSerge Semin  * Return: bitfield of inbound message registers.
1430bc3e49adSSerge Semin  */
ntb_msg_inbits(struct ntb_dev * ntb)1431bc3e49adSSerge Semin static inline u64 ntb_msg_inbits(struct ntb_dev *ntb)
1432bc3e49adSSerge Semin {
1433bc3e49adSSerge Semin 	if (!ntb->ops->msg_inbits)
1434bc3e49adSSerge Semin 		return 0;
1435bc3e49adSSerge Semin 
1436bc3e49adSSerge Semin 	return ntb->ops->msg_inbits(ntb);
1437bc3e49adSSerge Semin }
1438bc3e49adSSerge Semin 
1439bc3e49adSSerge Semin /**
1440bc3e49adSSerge Semin  * ntb_msg_outbits() - get a bitfield of outbound message registers status
1441bc3e49adSSerge Semin  * @ntb:	NTB device context.
1442bc3e49adSSerge Semin  *
1443bc3e49adSSerge Semin  * The method returns the bitfield of status and mask registers, which related
1444bc3e49adSSerge Semin  * to outbound message registers.
1445bc3e49adSSerge Semin  *
1446bc3e49adSSerge Semin  * Return: bitfield of outbound message registers.
1447bc3e49adSSerge Semin  */
ntb_msg_outbits(struct ntb_dev * ntb)1448bc3e49adSSerge Semin static inline u64 ntb_msg_outbits(struct ntb_dev *ntb)
1449bc3e49adSSerge Semin {
1450bc3e49adSSerge Semin 	if (!ntb->ops->msg_outbits)
1451bc3e49adSSerge Semin 		return 0;
1452bc3e49adSSerge Semin 
1453bc3e49adSSerge Semin 	return ntb->ops->msg_outbits(ntb);
1454bc3e49adSSerge Semin }
1455bc3e49adSSerge Semin 
1456bc3e49adSSerge Semin /**
1457bc3e49adSSerge Semin  * ntb_msg_read_sts() - read the message registers status
1458bc3e49adSSerge Semin  * @ntb:	NTB device context.
1459bc3e49adSSerge Semin  *
1460bc3e49adSSerge Semin  * Read the status of message register. Inbound and outbound message registers
1461bc3e49adSSerge Semin  * related bits can be filtered by masks retrieved from ntb_msg_inbits() and
1462bc3e49adSSerge Semin  * ntb_msg_outbits().
1463bc3e49adSSerge Semin  *
1464bc3e49adSSerge Semin  * Return: status bits of message registers
1465bc3e49adSSerge Semin  */
ntb_msg_read_sts(struct ntb_dev * ntb)1466bc3e49adSSerge Semin static inline u64 ntb_msg_read_sts(struct ntb_dev *ntb)
1467bc3e49adSSerge Semin {
1468bc3e49adSSerge Semin 	if (!ntb->ops->msg_read_sts)
1469bc3e49adSSerge Semin 		return 0;
1470bc3e49adSSerge Semin 
1471bc3e49adSSerge Semin 	return ntb->ops->msg_read_sts(ntb);
1472bc3e49adSSerge Semin }
1473bc3e49adSSerge Semin 
1474bc3e49adSSerge Semin /**
1475bc3e49adSSerge Semin  * ntb_msg_clear_sts() - clear status bits of message registers
1476bc3e49adSSerge Semin  * @ntb:	NTB device context.
1477bc3e49adSSerge Semin  * @sts_bits:	Status bits to clear.
1478bc3e49adSSerge Semin  *
1479bc3e49adSSerge Semin  * Clear bits in the status register.
1480bc3e49adSSerge Semin  *
1481bc3e49adSSerge Semin  * Return: Zero on success, otherwise a negative error number.
1482bc3e49adSSerge Semin  */
ntb_msg_clear_sts(struct ntb_dev * ntb,u64 sts_bits)1483bc3e49adSSerge Semin static inline int ntb_msg_clear_sts(struct ntb_dev *ntb, u64 sts_bits)
1484bc3e49adSSerge Semin {
1485bc3e49adSSerge Semin 	if (!ntb->ops->msg_clear_sts)
1486bc3e49adSSerge Semin 		return -EINVAL;
1487bc3e49adSSerge Semin 
1488bc3e49adSSerge Semin 	return ntb->ops->msg_clear_sts(ntb, sts_bits);
1489bc3e49adSSerge Semin }
1490bc3e49adSSerge Semin 
1491bc3e49adSSerge Semin /**
1492bc3e49adSSerge Semin  * ntb_msg_set_mask() - set mask of message register status bits
1493bc3e49adSSerge Semin  * @ntb:	NTB device context.
1494bc3e49adSSerge Semin  * @mask_bits:	Mask bits.
1495bc3e49adSSerge Semin  *
1496bc3e49adSSerge Semin  * Mask the message registers status bits from raising the message event.
1497bc3e49adSSerge Semin  *
1498bc3e49adSSerge Semin  * Return: Zero on success, otherwise a negative error number.
1499bc3e49adSSerge Semin  */
ntb_msg_set_mask(struct ntb_dev * ntb,u64 mask_bits)1500bc3e49adSSerge Semin static inline int ntb_msg_set_mask(struct ntb_dev *ntb, u64 mask_bits)
1501bc3e49adSSerge Semin {
1502bc3e49adSSerge Semin 	if (!ntb->ops->msg_set_mask)
1503bc3e49adSSerge Semin 		return -EINVAL;
1504bc3e49adSSerge Semin 
1505bc3e49adSSerge Semin 	return ntb->ops->msg_set_mask(ntb, mask_bits);
1506bc3e49adSSerge Semin }
1507bc3e49adSSerge Semin 
1508bc3e49adSSerge Semin /**
1509bc3e49adSSerge Semin  * ntb_msg_clear_mask() - clear message registers mask
1510bc3e49adSSerge Semin  * @ntb:	NTB device context.
1511bc3e49adSSerge Semin  * @mask_bits:	Mask bits to clear.
1512bc3e49adSSerge Semin  *
1513bc3e49adSSerge Semin  * Clear bits in the message events mask register.
1514bc3e49adSSerge Semin  *
1515bc3e49adSSerge Semin  * Return: Zero on success, otherwise a negative error number.
1516bc3e49adSSerge Semin  */
ntb_msg_clear_mask(struct ntb_dev * ntb,u64 mask_bits)1517bc3e49adSSerge Semin static inline int ntb_msg_clear_mask(struct ntb_dev *ntb, u64 mask_bits)
1518bc3e49adSSerge Semin {
1519bc3e49adSSerge Semin 	if (!ntb->ops->msg_clear_mask)
1520bc3e49adSSerge Semin 		return -EINVAL;
1521bc3e49adSSerge Semin 
1522bc3e49adSSerge Semin 	return ntb->ops->msg_clear_mask(ntb, mask_bits);
1523bc3e49adSSerge Semin }
1524bc3e49adSSerge Semin 
1525bc3e49adSSerge Semin /**
1526b87ab219SSerge Semin  * ntb_msg_read() - read inbound message register with specified index
1527bc3e49adSSerge Semin  * @ntb:	NTB device context.
1528bc3e49adSSerge Semin  * @pidx:	OUT - Port index of peer device a message retrieved from
1529b87ab219SSerge Semin  * @midx:	Message register index
1530bc3e49adSSerge Semin  *
1531bc3e49adSSerge Semin  * Read data from the specified message register. Source port index of a
1532bc3e49adSSerge Semin  * message is retrieved as well.
1533bc3e49adSSerge Semin  *
1534b87ab219SSerge Semin  * Return: The value of the inbound message register.
1535bc3e49adSSerge Semin  */
ntb_msg_read(struct ntb_dev * ntb,int * pidx,int midx)1536b87ab219SSerge Semin static inline u32 ntb_msg_read(struct ntb_dev *ntb, int *pidx, int midx)
1537bc3e49adSSerge Semin {
1538bc3e49adSSerge Semin 	if (!ntb->ops->msg_read)
1539b87ab219SSerge Semin 		return ~(u32)0;
1540bc3e49adSSerge Semin 
1541b87ab219SSerge Semin 	return ntb->ops->msg_read(ntb, pidx, midx);
1542bc3e49adSSerge Semin }
1543bc3e49adSSerge Semin 
1544bc3e49adSSerge Semin /**
1545b87ab219SSerge Semin  * ntb_peer_msg_write() - write data to the specified peer message register
1546bc3e49adSSerge Semin  * @ntb:	NTB device context.
1547bc3e49adSSerge Semin  * @pidx:	Port index of peer device a message being sent to
1548b87ab219SSerge Semin  * @midx:	Message register index
1549bc3e49adSSerge Semin  * @msg:	Data to send
1550bc3e49adSSerge Semin  *
1551bc3e49adSSerge Semin  * Send data to a specified peer device using the defined message register.
1552bc3e49adSSerge Semin  * Message event can be raised if the midx registers isn't empty while
1553bc3e49adSSerge Semin  * calling this method and the corresponding interrupt isn't masked.
1554bc3e49adSSerge Semin  *
1555bc3e49adSSerge Semin  * Return: Zero on success, otherwise a negative error number.
1556bc3e49adSSerge Semin  */
ntb_peer_msg_write(struct ntb_dev * ntb,int pidx,int midx,u32 msg)1557b87ab219SSerge Semin static inline int ntb_peer_msg_write(struct ntb_dev *ntb, int pidx, int midx,
1558bc3e49adSSerge Semin 				     u32 msg)
1559bc3e49adSSerge Semin {
1560b87ab219SSerge Semin 	if (!ntb->ops->peer_msg_write)
1561bc3e49adSSerge Semin 		return -EINVAL;
1562bc3e49adSSerge Semin 
1563b87ab219SSerge Semin 	return ntb->ops->peer_msg_write(ntb, pidx, midx, msg);
1564bc3e49adSSerge Semin }
1565bc3e49adSSerge Semin 
15665f1b1f06SLogan Gunthorpe /**
15675f1b1f06SLogan Gunthorpe  * ntb_peer_resource_idx() - get a resource index for a given peer idx
15685f1b1f06SLogan Gunthorpe  * @ntb:	NTB device context.
15695f1b1f06SLogan Gunthorpe  * @pidx:	Peer port index.
15705f1b1f06SLogan Gunthorpe  *
15715f1b1f06SLogan Gunthorpe  * When constructing a graph of peers, each remote peer must use a different
15725f1b1f06SLogan Gunthorpe  * resource index (mw, doorbell, etc) to communicate with each other
15735f1b1f06SLogan Gunthorpe  * peer.
15745f1b1f06SLogan Gunthorpe  *
15755f1b1f06SLogan Gunthorpe  * In a two peer system, this function should always return 0 such that
15765f1b1f06SLogan Gunthorpe  * resource 0 points to the remote peer on both ports.
15775f1b1f06SLogan Gunthorpe  *
15785f1b1f06SLogan Gunthorpe  * In a 5 peer system, this function will return the following matrix
15795f1b1f06SLogan Gunthorpe  *
15805f1b1f06SLogan Gunthorpe  * pidx \ port    0    1    2    3    4
15815f1b1f06SLogan Gunthorpe  * 0              0    0    1    2    3
15825f1b1f06SLogan Gunthorpe  * 1              0    1    1    2    3
15835f1b1f06SLogan Gunthorpe  * 2              0    1    2    2    3
15845f1b1f06SLogan Gunthorpe  * 3              0    1    2    3    3
15855f1b1f06SLogan Gunthorpe  *
15865f1b1f06SLogan Gunthorpe  * For example, if this function is used to program peer's memory
15875f1b1f06SLogan Gunthorpe  * windows, port 0 will program MW 0 on all it's peers to point to itself.
15885f1b1f06SLogan Gunthorpe  * port 1 will program MW 0 in port 0 to point to itself and MW 1 on all
15895f1b1f06SLogan Gunthorpe  * other ports. etc.
15905f1b1f06SLogan Gunthorpe  *
15915f1b1f06SLogan Gunthorpe  * For the legacy two host case, ntb_port_number() and ntb_peer_port_number()
15925f1b1f06SLogan Gunthorpe  * both return zero and therefore this function will always return zero.
15935f1b1f06SLogan Gunthorpe  * So MW 0 on each host would be programmed to point to the other host.
15945f1b1f06SLogan Gunthorpe  *
15955f1b1f06SLogan Gunthorpe  * Return: the resource index to use for that peer.
15965f1b1f06SLogan Gunthorpe  */
ntb_peer_resource_idx(struct ntb_dev * ntb,int pidx)15975f1b1f06SLogan Gunthorpe static inline int ntb_peer_resource_idx(struct ntb_dev *ntb, int pidx)
15985f1b1f06SLogan Gunthorpe {
15995f1b1f06SLogan Gunthorpe 	int local_port, peer_port;
16005f1b1f06SLogan Gunthorpe 
16015f1b1f06SLogan Gunthorpe 	if (pidx >= ntb_peer_port_count(ntb))
16025f1b1f06SLogan Gunthorpe 		return -EINVAL;
16035f1b1f06SLogan Gunthorpe 
16045f1b1f06SLogan Gunthorpe 	local_port = ntb_logical_port_number(ntb);
16055f1b1f06SLogan Gunthorpe 	peer_port = ntb_peer_logical_port_number(ntb, pidx);
16065f1b1f06SLogan Gunthorpe 
16075f1b1f06SLogan Gunthorpe 	if (peer_port < local_port)
16085f1b1f06SLogan Gunthorpe 		return local_port - 1;
16095f1b1f06SLogan Gunthorpe 	else
16105f1b1f06SLogan Gunthorpe 		return local_port;
16115f1b1f06SLogan Gunthorpe }
16125f1b1f06SLogan Gunthorpe 
16135f1b1f06SLogan Gunthorpe /**
16145f1b1f06SLogan Gunthorpe  * ntb_peer_highest_mw_idx() - get a memory window index for a given peer idx
16155f1b1f06SLogan Gunthorpe  *	using the highest index memory windows first
16165f1b1f06SLogan Gunthorpe  *
16175f1b1f06SLogan Gunthorpe  * @ntb:	NTB device context.
16185f1b1f06SLogan Gunthorpe  * @pidx:	Peer port index.
16195f1b1f06SLogan Gunthorpe  *
16205f1b1f06SLogan Gunthorpe  * Like ntb_peer_resource_idx(), except it returns indexes starting with
16215f1b1f06SLogan Gunthorpe  * last memory window index.
16225f1b1f06SLogan Gunthorpe  *
16235f1b1f06SLogan Gunthorpe  * Return: the resource index to use for that peer.
16245f1b1f06SLogan Gunthorpe  */
ntb_peer_highest_mw_idx(struct ntb_dev * ntb,int pidx)16255f1b1f06SLogan Gunthorpe static inline int ntb_peer_highest_mw_idx(struct ntb_dev *ntb, int pidx)
16265f1b1f06SLogan Gunthorpe {
16275f1b1f06SLogan Gunthorpe 	int ret;
16285f1b1f06SLogan Gunthorpe 
16295f1b1f06SLogan Gunthorpe 	ret = ntb_peer_resource_idx(ntb, pidx);
16305f1b1f06SLogan Gunthorpe 	if (ret < 0)
16315f1b1f06SLogan Gunthorpe 		return ret;
16325f1b1f06SLogan Gunthorpe 
16335f1b1f06SLogan Gunthorpe 	return ntb_mw_count(ntb, pidx) - ret - 1;
16345f1b1f06SLogan Gunthorpe }
16355f1b1f06SLogan Gunthorpe 
163626b3a37bSLogan Gunthorpe struct ntb_msi_desc {
163726b3a37bSLogan Gunthorpe 	u32 addr_offset;
163826b3a37bSLogan Gunthorpe 	u32 data;
163926b3a37bSLogan Gunthorpe };
164026b3a37bSLogan Gunthorpe 
164126b3a37bSLogan Gunthorpe #ifdef CONFIG_NTB_MSI
164226b3a37bSLogan Gunthorpe 
164326b3a37bSLogan Gunthorpe int ntb_msi_init(struct ntb_dev *ntb, void (*desc_changed)(void *ctx));
164426b3a37bSLogan Gunthorpe int ntb_msi_setup_mws(struct ntb_dev *ntb);
164526b3a37bSLogan Gunthorpe void ntb_msi_clear_mws(struct ntb_dev *ntb);
164626b3a37bSLogan Gunthorpe int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
164726b3a37bSLogan Gunthorpe 				  irq_handler_t thread_fn,
164826b3a37bSLogan Gunthorpe 				  const char *name, void *dev_id,
164926b3a37bSLogan Gunthorpe 				  struct ntb_msi_desc *msi_desc);
165026b3a37bSLogan Gunthorpe void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq, void *dev_id);
165126b3a37bSLogan Gunthorpe int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
165226b3a37bSLogan Gunthorpe 			 struct ntb_msi_desc *desc);
165326b3a37bSLogan Gunthorpe int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
165426b3a37bSLogan Gunthorpe 		      struct ntb_msi_desc *desc,
165526b3a37bSLogan Gunthorpe 		      phys_addr_t *msi_addr);
165626b3a37bSLogan Gunthorpe 
165726b3a37bSLogan Gunthorpe #else /* not CONFIG_NTB_MSI */
165826b3a37bSLogan Gunthorpe 
ntb_msi_init(struct ntb_dev * ntb,void (* desc_changed)(void * ctx))165926b3a37bSLogan Gunthorpe static inline int ntb_msi_init(struct ntb_dev *ntb,
166026b3a37bSLogan Gunthorpe 			       void (*desc_changed)(void *ctx))
166126b3a37bSLogan Gunthorpe {
166226b3a37bSLogan Gunthorpe 	return -EOPNOTSUPP;
166326b3a37bSLogan Gunthorpe }
ntb_msi_setup_mws(struct ntb_dev * ntb)166426b3a37bSLogan Gunthorpe static inline int ntb_msi_setup_mws(struct ntb_dev *ntb)
166526b3a37bSLogan Gunthorpe {
166626b3a37bSLogan Gunthorpe 	return -EOPNOTSUPP;
166726b3a37bSLogan Gunthorpe }
ntb_msi_clear_mws(struct ntb_dev * ntb)166826b3a37bSLogan Gunthorpe static inline void ntb_msi_clear_mws(struct ntb_dev *ntb) {}
ntbm_msi_request_threaded_irq(struct ntb_dev * ntb,irq_handler_t handler,irq_handler_t thread_fn,const char * name,void * dev_id,struct ntb_msi_desc * msi_desc)166926b3a37bSLogan Gunthorpe static inline int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb,
167026b3a37bSLogan Gunthorpe 						irq_handler_t handler,
167126b3a37bSLogan Gunthorpe 						irq_handler_t thread_fn,
167226b3a37bSLogan Gunthorpe 						const char *name, void *dev_id,
167326b3a37bSLogan Gunthorpe 						struct ntb_msi_desc *msi_desc)
167426b3a37bSLogan Gunthorpe {
167526b3a37bSLogan Gunthorpe 	return -EOPNOTSUPP;
167626b3a37bSLogan Gunthorpe }
ntbm_msi_free_irq(struct ntb_dev * ntb,unsigned int irq,void * dev_id)167726b3a37bSLogan Gunthorpe static inline void ntbm_msi_free_irq(struct ntb_dev *ntb, unsigned int irq,
167826b3a37bSLogan Gunthorpe 				     void *dev_id) {}
ntb_msi_peer_trigger(struct ntb_dev * ntb,int peer,struct ntb_msi_desc * desc)167926b3a37bSLogan Gunthorpe static inline int ntb_msi_peer_trigger(struct ntb_dev *ntb, int peer,
168026b3a37bSLogan Gunthorpe 				       struct ntb_msi_desc *desc)
168126b3a37bSLogan Gunthorpe {
168226b3a37bSLogan Gunthorpe 	return -EOPNOTSUPP;
168326b3a37bSLogan Gunthorpe }
ntb_msi_peer_addr(struct ntb_dev * ntb,int peer,struct ntb_msi_desc * desc,phys_addr_t * msi_addr)168426b3a37bSLogan Gunthorpe static inline int ntb_msi_peer_addr(struct ntb_dev *ntb, int peer,
168526b3a37bSLogan Gunthorpe 				    struct ntb_msi_desc *desc,
168626b3a37bSLogan Gunthorpe 				    phys_addr_t *msi_addr)
168726b3a37bSLogan Gunthorpe {
168826b3a37bSLogan Gunthorpe 	return -EOPNOTSUPP;
168926b3a37bSLogan Gunthorpe 
169026b3a37bSLogan Gunthorpe }
169126b3a37bSLogan Gunthorpe 
169226b3a37bSLogan Gunthorpe #endif /* CONFIG_NTB_MSI */
169326b3a37bSLogan Gunthorpe 
ntbm_msi_request_irq(struct ntb_dev * ntb,irq_handler_t handler,const char * name,void * dev_id,struct ntb_msi_desc * msi_desc)169426b3a37bSLogan Gunthorpe static inline int ntbm_msi_request_irq(struct ntb_dev *ntb,
169526b3a37bSLogan Gunthorpe 				       irq_handler_t handler,
169626b3a37bSLogan Gunthorpe 				       const char *name, void *dev_id,
169726b3a37bSLogan Gunthorpe 				       struct ntb_msi_desc *msi_desc)
169826b3a37bSLogan Gunthorpe {
169926b3a37bSLogan Gunthorpe 	return ntbm_msi_request_threaded_irq(ntb, handler, NULL, name,
170026b3a37bSLogan Gunthorpe 					     dev_id, msi_desc);
170126b3a37bSLogan Gunthorpe }
170226b3a37bSLogan Gunthorpe 
1703a1bd3baeSAllen Hubbe #endif
1704