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