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