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