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