]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/ntb.h
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.
7 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
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.
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.
21 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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
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.
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.
50 * PCIe NTB Linux driver
52 * Contact Information:
53 * Allen Hubbe <Allen.Hubbe@emc.com>
59 #include <linux/completion.h>
60 #include <linux/device.h>
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.
82 static inline int ntb_topo_is_b2b(enum ntb_topo topo
)
85 case NTB_TOPO_B2B_USD
:
86 case NTB_TOPO_B2B_DSD
:
92 static inline char *ntb_topo_string(enum ntb_topo 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";
101 return "NTB_TOPO_INVALID";
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.
121 * enum ntb_width - NTB link training width
122 * @NTB_WIDTH_AUTO: Request the max supported width.
123 * @NTB_WIDTH_NONE: Link is not trained to any width.
124 * @NTB_WIDTH_1: Link is trained to 1 lane width.
125 * @NTB_WIDTH_2: Link is trained to 2 lane width.
126 * @NTB_WIDTH_4: Link is trained to 4 lane width.
127 * @NTB_WIDTH_8: Link is trained to 8 lane width.
128 * @NTB_WIDTH_12: Link is trained to 12 lane width.
129 * @NTB_WIDTH_16: Link is trained to 16 lane width.
130 * @NTB_WIDTH_32: Link is trained to 32 lane width.
145 * enum ntb_default_port - NTB default port number
146 * @NTB_PORT_PRI_USD: Default port of the NTB_TOPO_PRI/NTB_TOPO_B2B_USD
148 * @NTB_PORT_SEC_DSD: Default port of the NTB_TOPO_SEC/NTB_TOPO_B2B_DSD
151 enum ntb_default_port
{
155 #define NTB_DEF_PEER_CNT (1)
156 #define NTB_DEF_PEER_IDX (0)
159 * struct ntb_client_ops - ntb client operations
160 * @probe: Notify client of a new device.
161 * @remove: Notify client to remove a device.
163 struct ntb_client_ops
{
164 int (*probe
)(struct ntb_client
*client
, struct ntb_dev
*ntb
);
165 void (*remove
)(struct ntb_client
*client
, struct ntb_dev
*ntb
);
168 static inline int ntb_client_ops_is_valid(const struct ntb_client_ops
*ops
)
170 /* commented callbacks are not required: */
178 * struct ntb_ctx_ops - ntb driver context operations
179 * @link_event: See ntb_link_event().
180 * @db_event: See ntb_db_event().
183 void (*link_event
)(void *ctx
);
184 void (*db_event
)(void *ctx
, int db_vector
);
187 static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops
*ops
)
189 /* commented callbacks are not required: */
191 /* ops->link_event && */
192 /* ops->db_event && */
197 * struct ntb_ctx_ops - ntb device operations
198 * @port_number: See ntb_port_number().
199 * @peer_port_count: See ntb_peer_port_count().
200 * @peer_port_number: See ntb_peer_port_number().
201 * @peer_port_idx: See ntb_peer_port_idx().
202 * @link_is_up: See ntb_link_is_up().
203 * @link_enable: See ntb_link_enable().
204 * @link_disable: See ntb_link_disable().
205 * @mw_count: See ntb_mw_count().
206 * @mw_get_align: See ntb_mw_get_align().
207 * @mw_set_trans: See ntb_mw_set_trans().
208 * @mw_clear_trans: See ntb_mw_clear_trans().
209 * @peer_mw_count: See ntb_peer_mw_count().
210 * @peer_mw_get_addr: See ntb_peer_mw_get_addr().
211 * @peer_mw_set_trans: See ntb_peer_mw_set_trans().
212 * @peer_mw_clear_trans:See ntb_peer_mw_clear_trans().
213 * @db_is_unsafe: See ntb_db_is_unsafe().
214 * @db_valid_mask: See ntb_db_valid_mask().
215 * @db_vector_count: See ntb_db_vector_count().
216 * @db_vector_mask: See ntb_db_vector_mask().
217 * @db_read: See ntb_db_read().
218 * @db_set: See ntb_db_set().
219 * @db_clear: See ntb_db_clear().
220 * @db_read_mask: See ntb_db_read_mask().
221 * @db_set_mask: See ntb_db_set_mask().
222 * @db_clear_mask: See ntb_db_clear_mask().
223 * @peer_db_addr: See ntb_peer_db_addr().
224 * @peer_db_read: See ntb_peer_db_read().
225 * @peer_db_set: See ntb_peer_db_set().
226 * @peer_db_clear: See ntb_peer_db_clear().
227 * @peer_db_read_mask: See ntb_peer_db_read_mask().
228 * @peer_db_set_mask: See ntb_peer_db_set_mask().
229 * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
230 * @spad_is_unsafe: See ntb_spad_is_unsafe().
231 * @spad_count: See ntb_spad_count().
232 * @spad_read: See ntb_spad_read().
233 * @spad_write: See ntb_spad_write().
234 * @peer_spad_addr: See ntb_peer_spad_addr().
235 * @peer_spad_read: See ntb_peer_spad_read().
236 * @peer_spad_write: See ntb_peer_spad_write().
239 int (*port_number
)(struct ntb_dev
*ntb
);
240 int (*peer_port_count
)(struct ntb_dev
*ntb
);
241 int (*peer_port_number
)(struct ntb_dev
*ntb
, int pidx
);
242 int (*peer_port_idx
)(struct ntb_dev
*ntb
, int port
);
244 u64 (*link_is_up
)(struct ntb_dev
*ntb
,
245 enum ntb_speed
*speed
, enum ntb_width
*width
);
246 int (*link_enable
)(struct ntb_dev
*ntb
,
247 enum ntb_speed max_speed
, enum ntb_width max_width
);
248 int (*link_disable
)(struct ntb_dev
*ntb
);
250 int (*mw_count
)(struct ntb_dev
*ntb
, int pidx
);
251 int (*mw_get_align
)(struct ntb_dev
*ntb
, int pidx
, int widx
,
252 resource_size_t
*addr_align
,
253 resource_size_t
*size_align
,
254 resource_size_t
*size_max
);
255 int (*mw_set_trans
)(struct ntb_dev
*ntb
, int pidx
, int widx
,
256 dma_addr_t addr
, resource_size_t size
);
257 int (*mw_clear_trans
)(struct ntb_dev
*ntb
, int pidx
, int widx
);
258 int (*peer_mw_count
)(struct ntb_dev
*ntb
);
259 int (*peer_mw_get_addr
)(struct ntb_dev
*ntb
, int widx
,
260 phys_addr_t
*base
, resource_size_t
*size
);
261 int (*peer_mw_set_trans
)(struct ntb_dev
*ntb
, int pidx
, int widx
,
262 u64 addr
, resource_size_t size
);
263 int (*peer_mw_clear_trans
)(struct ntb_dev
*ntb
, int pidx
, int widx
);
265 int (*db_is_unsafe
)(struct ntb_dev
*ntb
);
266 u64 (*db_valid_mask
)(struct ntb_dev
*ntb
);
267 int (*db_vector_count
)(struct ntb_dev
*ntb
);
268 u64 (*db_vector_mask
)(struct ntb_dev
*ntb
, int db_vector
);
270 u64 (*db_read
)(struct ntb_dev
*ntb
);
271 int (*db_set
)(struct ntb_dev
*ntb
, u64 db_bits
);
272 int (*db_clear
)(struct ntb_dev
*ntb
, u64 db_bits
);
274 u64 (*db_read_mask
)(struct ntb_dev
*ntb
);
275 int (*db_set_mask
)(struct ntb_dev
*ntb
, u64 db_bits
);
276 int (*db_clear_mask
)(struct ntb_dev
*ntb
, u64 db_bits
);
278 int (*peer_db_addr
)(struct ntb_dev
*ntb
,
279 phys_addr_t
*db_addr
, resource_size_t
*db_size
);
280 u64 (*peer_db_read
)(struct ntb_dev
*ntb
);
281 int (*peer_db_set
)(struct ntb_dev
*ntb
, u64 db_bits
);
282 int (*peer_db_clear
)(struct ntb_dev
*ntb
, u64 db_bits
);
284 u64 (*peer_db_read_mask
)(struct ntb_dev
*ntb
);
285 int (*peer_db_set_mask
)(struct ntb_dev
*ntb
, u64 db_bits
);
286 int (*peer_db_clear_mask
)(struct ntb_dev
*ntb
, u64 db_bits
);
288 int (*spad_is_unsafe
)(struct ntb_dev
*ntb
);
289 int (*spad_count
)(struct ntb_dev
*ntb
);
291 u32 (*spad_read
)(struct ntb_dev
*ntb
, int sidx
);
292 int (*spad_write
)(struct ntb_dev
*ntb
, int sidx
, u32 val
);
294 int (*peer_spad_addr
)(struct ntb_dev
*ntb
, int pidx
, int sidx
,
295 phys_addr_t
*spad_addr
);
296 u32 (*peer_spad_read
)(struct ntb_dev
*ntb
, int pidx
, int sidx
);
297 int (*peer_spad_write
)(struct ntb_dev
*ntb
, int pidx
, int sidx
,
301 static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops
*ops
)
303 /* commented callbacks are not required: */
305 !ops
->peer_port_count
== !ops
->port_number
&&
306 !ops
->peer_port_number
== !ops
->port_number
&&
307 !ops
->peer_port_idx
== !ops
->port_number
&&
313 (ops
->mw_set_trans
||
314 ops
->peer_mw_set_trans
) &&
315 /* ops->mw_clear_trans && */
316 ops
->peer_mw_count
&&
317 ops
->peer_mw_get_addr
&&
318 /* ops->peer_mw_clear_trans && */
320 /* ops->db_is_unsafe && */
321 ops
->db_valid_mask
&&
323 /* both set, or both unset */
324 (!ops
->db_vector_count
== !ops
->db_vector_mask
) &&
329 /* ops->db_read_mask && */
331 ops
->db_clear_mask
&&
332 /* ops->peer_db_addr && */
333 /* ops->peer_db_read && */
335 /* ops->peer_db_clear && */
336 /* ops->peer_db_read_mask && */
337 /* ops->peer_db_set_mask && */
338 /* ops->peer_db_clear_mask && */
339 /* !ops->spad_is_unsafe == !ops->spad_count && */
340 !ops
->spad_read
== !ops
->spad_count
&&
341 !ops
->spad_write
== !ops
->spad_count
&&
342 /* !ops->peer_spad_addr == !ops->spad_count && */
343 /* !ops->peer_spad_read == !ops->spad_count && */
344 !ops
->peer_spad_write
== !ops
->spad_count
&&
349 * struct ntb_client - client interested in ntb devices
350 * @drv: Linux driver object.
351 * @ops: See &ntb_client_ops.
354 struct device_driver drv
;
355 const struct ntb_client_ops ops
;
358 #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
361 * struct ntb_device - ntb device
362 * @dev: Linux device object.
363 * @pdev: Pci device entry of the ntb.
364 * @topo: Detected topology of the ntb.
365 * @ops: See &ntb_dev_ops.
366 * @ctx: See &ntb_ctx_ops.
367 * @ctx_ops: See &ntb_ctx_ops.
371 struct pci_dev
*pdev
;
373 const struct ntb_dev_ops
*ops
;
375 const struct ntb_ctx_ops
*ctx_ops
;
379 /* synchronize setting, clearing, and calling ctx_ops */
381 /* block unregister until device is fully released */
382 struct completion released
;
385 #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
388 * ntb_register_client() - register a client for interest in ntb devices
389 * @client: Client context.
391 * The client will be added to the list of clients interested in ntb devices.
392 * The client will be notified of any ntb devices that are not already
393 * associated with a client, or if ntb devices are registered later.
395 * Return: Zero if the client is registered, otherwise an error number.
397 #define ntb_register_client(client) \
398 __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
400 int __ntb_register_client(struct ntb_client
*client
, struct module
*mod
,
401 const char *mod_name
);
404 * ntb_unregister_client() - unregister a client for interest in ntb devices
405 * @client: Client context.
407 * The client will be removed from the list of clients interested in ntb
408 * devices. If any ntb devices are associated with the client, the client will
409 * be notified to remove those devices.
411 void ntb_unregister_client(struct ntb_client
*client
);
413 #define module_ntb_client(__ntb_client) \
414 module_driver(__ntb_client, ntb_register_client, \
415 ntb_unregister_client)
418 * ntb_register_device() - register a ntb device
419 * @ntb: NTB device context.
421 * The device will be added to the list of ntb devices. If any clients are
422 * interested in ntb devices, each client will be notified of the ntb device,
423 * until at most one client accepts the device.
425 * Return: Zero if the device is registered, otherwise an error number.
427 int ntb_register_device(struct ntb_dev
*ntb
);
430 * ntb_register_device() - unregister a ntb device
431 * @ntb: NTB device context.
433 * The device will be removed from the list of ntb devices. If the ntb device
434 * is associated with a client, the client will be notified to remove the
437 void ntb_unregister_device(struct ntb_dev
*ntb
);
440 * ntb_set_ctx() - associate a driver context with an ntb device
441 * @ntb: NTB device context.
442 * @ctx: Driver context.
443 * @ctx_ops: Driver context operations.
445 * Associate a driver context and operations with a ntb device. The context is
446 * provided by the client driver, and the driver may associate a different
447 * context with each ntb device.
449 * Return: Zero if the context is associated, otherwise an error number.
451 int ntb_set_ctx(struct ntb_dev
*ntb
, void *ctx
,
452 const struct ntb_ctx_ops
*ctx_ops
);
455 * ntb_clear_ctx() - disassociate any driver context from an ntb device
456 * @ntb: NTB device context.
458 * Clear any association that may exist between a driver context and the ntb
461 void ntb_clear_ctx(struct ntb_dev
*ntb
);
464 * ntb_link_event() - notify driver context of a change in link status
465 * @ntb: NTB device context.
467 * Notify the driver context that the link status may have changed. The driver
468 * should call ntb_link_is_up() to get the current status.
470 void ntb_link_event(struct ntb_dev
*ntb
);
473 * ntb_db_event() - notify driver context of a doorbell event
474 * @ntb: NTB device context.
475 * @vector: Interrupt vector number.
477 * Notify the driver context of a doorbell event. If hardware supports
478 * multiple interrupt vectors for doorbells, the vector number indicates which
479 * vector received the interrupt. The vector number is relative to the first
480 * vector used for doorbells, starting at zero, and must be less than
481 ** ntb_db_vector_count(). The driver may call ntb_db_read() to check which
482 * doorbell bits need service, and ntb_db_vector_mask() to determine which of
483 * those bits are associated with the vector number.
485 void ntb_db_event(struct ntb_dev
*ntb
, int vector
);
488 * ntb_default_port_number() - get the default local port number
489 * @ntb: NTB device context.
491 * If hardware driver doesn't specify port_number() callback method, the NTB
492 * is considered with just two ports. So this method returns default local
493 * port number in compliance with topology.
495 * NOTE Don't call this method directly. The ntb_port_number() function should
498 * Return: the default local port number
500 int ntb_default_port_number(struct ntb_dev
*ntb
);
503 * ntb_default_port_count() - get the default number of peer device ports
504 * @ntb: NTB device context.
506 * By default hardware driver supports just one peer device.
508 * NOTE Don't call this method directly. The ntb_peer_port_count() function
509 * should be used instead.
511 * Return: the default number of peer ports
513 int ntb_default_peer_port_count(struct ntb_dev
*ntb
);
516 * ntb_default_peer_port_number() - get the default peer port by given index
517 * @ntb: NTB device context.
518 * @idx: Peer port index (should not differ from zero).
520 * By default hardware driver supports just one peer device, so this method
521 * shall return the corresponding value from enum ntb_default_port.
523 * NOTE Don't call this method directly. The ntb_peer_port_number() function
524 * should be used instead.
526 * Return: the peer device port or negative value indicating an error
528 int ntb_default_peer_port_number(struct ntb_dev
*ntb
, int pidx
);
531 * ntb_default_peer_port_idx() - get the default peer device port index by
533 * @ntb: NTB device context.
534 * @port: Peer port number (should be one of enum ntb_default_port).
536 * By default hardware driver supports just one peer device, so while
537 * specified port-argument indicates peer port from enum ntb_default_port,
538 * the return value shall be zero.
540 * NOTE Don't call this method directly. The ntb_peer_port_idx() function
541 * should be used instead.
543 * Return: the peer port index or negative value indicating an error
545 int ntb_default_peer_port_idx(struct ntb_dev
*ntb
, int port
);
548 * ntb_port_number() - get the local port number
549 * @ntb: NTB device context.
551 * Hardware must support at least simple two-ports ntb connection
553 * Return: the local port number
555 static inline int ntb_port_number(struct ntb_dev
*ntb
)
557 if (!ntb
->ops
->port_number
)
558 return ntb_default_port_number(ntb
);
560 return ntb
->ops
->port_number(ntb
);
564 * ntb_peer_port_count() - get the number of peer device ports
565 * @ntb: NTB device context.
567 * Hardware may support an access to memory of several remote domains
568 * over multi-port NTB devices. This method returns the number of peers,
569 * local device can have shared memory with.
571 * Return: the number of peer ports
573 static inline int ntb_peer_port_count(struct ntb_dev
*ntb
)
575 if (!ntb
->ops
->peer_port_count
)
576 return ntb_default_peer_port_count(ntb
);
578 return ntb
->ops
->peer_port_count(ntb
);
582 * ntb_peer_port_number() - get the peer port by given index
583 * @ntb: NTB device context.
584 * @pidx: Peer port index.
586 * Peer ports are continuously enumerated by NTB API logic, so this method
587 * lets to retrieve port real number by its index.
589 * Return: the peer device port or negative value indicating an error
591 static inline int ntb_peer_port_number(struct ntb_dev
*ntb
, int pidx
)
593 if (!ntb
->ops
->peer_port_number
)
594 return ntb_default_peer_port_number(ntb
, pidx
);
596 return ntb
->ops
->peer_port_number(ntb
, pidx
);
600 * ntb_peer_port_idx() - get the peer device port index by given port number
601 * @ntb: NTB device context.
602 * @port: Peer port number.
604 * Inverse operation of ntb_peer_port_number(), so one can get port index
605 * by specified port number.
607 * Return: the peer port index or negative value indicating an error
609 static inline int ntb_peer_port_idx(struct ntb_dev
*ntb
, int port
)
611 if (!ntb
->ops
->peer_port_idx
)
612 return ntb_default_peer_port_idx(ntb
, port
);
614 return ntb
->ops
->peer_port_idx(ntb
, port
);
618 * ntb_link_is_up() - get the current ntb link state
619 * @ntb: NTB device context.
620 * @speed: OUT - The link speed expressed as PCIe generation number.
621 * @width: OUT - The link width expressed as the number of PCIe lanes.
623 * Get the current state of the ntb link. It is recommended to query the link
624 * state once after every link event. It is safe to query the link state in
625 * the context of the link event callback.
627 * Return: bitfield of indexed ports link state: bit is set/cleared if the
628 * link is up/down respectively.
630 static inline u64
ntb_link_is_up(struct ntb_dev
*ntb
,
631 enum ntb_speed
*speed
, enum ntb_width
*width
)
633 return ntb
->ops
->link_is_up(ntb
, speed
, width
);
637 * ntb_link_enable() - enable the local port ntb connection
638 * @ntb: NTB device context.
639 * @max_speed: The maximum link speed expressed as PCIe generation number.
640 * @max_width: The maximum link width expressed as the number of PCIe lanes.
642 * Enable the NTB/PCIe link on the local or remote (for bridge-to-bridge
643 * topology) side of the bridge. If it's supported the ntb device should train
644 * the link to its maximum speed and width, or the requested speed and width,
645 * whichever is smaller. Some hardware doesn't support PCIe link training, so
646 * the last two arguments will be ignored then.
648 * Return: Zero on success, otherwise an error number.
650 static inline int ntb_link_enable(struct ntb_dev
*ntb
,
651 enum ntb_speed max_speed
,
652 enum ntb_width max_width
)
654 return ntb
->ops
->link_enable(ntb
, max_speed
, max_width
);
658 * ntb_link_disable() - disable the local port ntb connection
659 * @ntb: NTB device context.
661 * Disable the link on the local or remote (for b2b topology) of the ntb.
662 * The ntb device should disable the link. Returning from this call must
663 * indicate that a barrier has passed, though with no more writes may pass in
664 * either direction across the link, except if this call returns an error
667 * Return: Zero on success, otherwise an error number.
669 static inline int ntb_link_disable(struct ntb_dev
*ntb
)
671 return ntb
->ops
->link_disable(ntb
);
675 * ntb_mw_count() - get the number of inbound memory windows, which could
676 * be created for a specified peer device
677 * @ntb: NTB device context.
678 * @pidx: Port index of peer device.
680 * Hardware and topology may support a different number of memory windows.
681 * Moreover different peer devices can support different number of memory
682 * windows. Simply speaking this method returns the number of possible inbound
683 * memory windows to share with specified peer device.
685 * Return: the number of memory windows.
687 static inline int ntb_mw_count(struct ntb_dev
*ntb
, int pidx
)
689 return ntb
->ops
->mw_count(ntb
, pidx
);
693 * ntb_mw_get_align() - get the restriction parameters of inbound memory window
694 * @ntb: NTB device context.
695 * @pidx: Port index of peer device.
696 * @widx: Memory window index.
697 * @addr_align: OUT - the base alignment for translating the memory window
698 * @size_align: OUT - the size alignment for translating the memory window
699 * @size_max: OUT - the maximum size of the memory window
701 * Get the alignments of an inbound memory window with specified index.
702 * NULL may be given for any output parameter if the value is not needed.
703 * The alignment and size parameters may be used for allocation of proper
706 * Return: Zero on success, otherwise a negative error number.
708 static inline int ntb_mw_get_align(struct ntb_dev
*ntb
, int pidx
, int widx
,
709 resource_size_t
*addr_align
,
710 resource_size_t
*size_align
,
711 resource_size_t
*size_max
)
713 return ntb
->ops
->mw_get_align(ntb
, pidx
, widx
, addr_align
, size_align
,
718 * ntb_mw_set_trans() - set the translation of an inbound memory window
719 * @ntb: NTB device context.
720 * @pidx: Port index of peer device.
721 * @widx: Memory window index.
722 * @addr: The dma address of local memory to expose to the peer.
723 * @size: The size of the local memory to expose to the peer.
725 * Set the translation of a memory window. The peer may access local memory
726 * through the window starting at the address, up to the size. The address
727 * and size must be aligned in compliance with restrictions of
728 * ntb_mw_get_align(). The region size should not exceed the size_max parameter
731 * This method may not be implemented due to the hardware specific memory
734 * Return: Zero on success, otherwise an error number.
736 static inline int ntb_mw_set_trans(struct ntb_dev
*ntb
, int pidx
, int widx
,
737 dma_addr_t addr
, resource_size_t size
)
739 if (!ntb
->ops
->mw_set_trans
)
742 return ntb
->ops
->mw_set_trans(ntb
, pidx
, widx
, addr
, size
);
746 * ntb_mw_clear_trans() - clear the translation address of an inbound memory
748 * @ntb: NTB device context.
749 * @pidx: Port index of peer device.
750 * @widx: Memory window index.
752 * Clear the translation of an inbound memory window. The peer may no longer
753 * access local memory through the window.
755 * Return: Zero on success, otherwise an error number.
757 static inline int ntb_mw_clear_trans(struct ntb_dev
*ntb
, int pidx
, int widx
)
759 if (!ntb
->ops
->mw_clear_trans
)
760 return ntb_mw_set_trans(ntb
, pidx
, widx
, 0, 0);
762 return ntb
->ops
->mw_clear_trans(ntb
, pidx
, widx
);
766 * ntb_peer_mw_count() - get the number of outbound memory windows, which could
767 * be mapped to access a shared memory
768 * @ntb: NTB device context.
770 * Hardware and topology may support a different number of memory windows.
771 * This method returns the number of outbound memory windows supported by
774 * Return: the number of memory windows.
776 static inline int ntb_peer_mw_count(struct ntb_dev
*ntb
)
778 return ntb
->ops
->peer_mw_count(ntb
);
782 * ntb_peer_mw_get_addr() - get map address of an outbound memory window
783 * @ntb: NTB device context.
784 * @widx: Memory window index (within ntb_peer_mw_count() return value).
785 * @base: OUT - the base address of mapping region.
786 * @size: OUT - the size of mapping region.
788 * Get base and size of memory region to map. NULL may be given for any output
789 * parameter if the value is not needed. The base and size may be used for
790 * mapping the memory window, to access the peer memory.
792 * Return: Zero on success, otherwise a negative error number.
794 static inline int ntb_peer_mw_get_addr(struct ntb_dev
*ntb
, int widx
,
795 phys_addr_t
*base
, resource_size_t
*size
)
797 return ntb
->ops
->peer_mw_get_addr(ntb
, widx
, base
, size
);
801 * ntb_peer_mw_set_trans() - set a translation address of a memory window
802 * retrieved from a peer device
803 * @ntb: NTB device context.
804 * @pidx: Port index of peer device the translation address received from.
805 * @widx: Memory window index.
806 * @addr: The dma address of the shared memory to access.
807 * @size: The size of the shared memory to access.
809 * Set the translation of an outbound memory window. The local device may
810 * access shared memory allocated by a peer device sent the address.
812 * This method may not be implemented due to the hardware specific memory
813 * windows interface, so a translation address can be only set on the side,
814 * where shared memory (inbound memory windows) is allocated.
816 * Return: Zero on success, otherwise an error number.
818 static inline int ntb_peer_mw_set_trans(struct ntb_dev
*ntb
, int pidx
, int widx
,
819 u64 addr
, resource_size_t size
)
821 if (!ntb
->ops
->peer_mw_set_trans
)
824 return ntb
->ops
->peer_mw_set_trans(ntb
, pidx
, widx
, addr
, size
);
828 * ntb_peer_mw_clear_trans() - clear the translation address of an outbound
830 * @ntb: NTB device context.
831 * @pidx: Port index of peer device.
832 * @widx: Memory window index.
834 * Clear the translation of a outbound memory window. The local device may no
835 * longer access a shared memory through the window.
837 * This method may not be implemented due to the hardware specific memory
840 * Return: Zero on success, otherwise an error number.
842 static inline int ntb_peer_mw_clear_trans(struct ntb_dev
*ntb
, int pidx
,
845 if (!ntb
->ops
->peer_mw_clear_trans
)
846 return ntb_peer_mw_set_trans(ntb
, pidx
, widx
, 0, 0);
848 return ntb
->ops
->peer_mw_clear_trans(ntb
, pidx
, widx
);
852 * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
853 * @ntb: NTB device context.
855 * It is possible for some ntb hardware to be affected by errata. Hardware
856 * drivers can advise clients to avoid using doorbells. Clients may ignore
857 * this advice, though caution is recommended.
859 * Return: Zero if it is safe to use doorbells, or One if it is not safe.
861 static inline int ntb_db_is_unsafe(struct ntb_dev
*ntb
)
863 if (!ntb
->ops
->db_is_unsafe
)
866 return ntb
->ops
->db_is_unsafe(ntb
);
870 * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
871 * @ntb: NTB device context.
873 * Hardware may support different number or arrangement of doorbell bits.
875 * Return: A mask of doorbell bits supported by the ntb.
877 static inline u64
ntb_db_valid_mask(struct ntb_dev
*ntb
)
879 return ntb
->ops
->db_valid_mask(ntb
);
883 * ntb_db_vector_count() - get the number of doorbell interrupt vectors
884 * @ntb: NTB device context.
886 * Hardware may support different number of interrupt vectors.
888 * Return: The number of doorbell interrupt vectors.
890 static inline int ntb_db_vector_count(struct ntb_dev
*ntb
)
892 if (!ntb
->ops
->db_vector_count
)
895 return ntb
->ops
->db_vector_count(ntb
);
899 * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
900 * @ntb: NTB device context.
901 * @vector: Doorbell vector number.
903 * Each interrupt vector may have a different number or arrangement of bits.
905 * Return: A mask of doorbell bits serviced by a vector.
907 static inline u64
ntb_db_vector_mask(struct ntb_dev
*ntb
, int vector
)
909 if (!ntb
->ops
->db_vector_mask
)
910 return ntb_db_valid_mask(ntb
);
912 return ntb
->ops
->db_vector_mask(ntb
, vector
);
916 * ntb_db_read() - read the local doorbell register
917 * @ntb: NTB device context.
919 * Read the local doorbell register, and return the bits that are set.
921 * Return: The bits currently set in the local doorbell register.
923 static inline u64
ntb_db_read(struct ntb_dev
*ntb
)
925 return ntb
->ops
->db_read(ntb
);
929 * ntb_db_set() - set bits in the local doorbell register
930 * @ntb: NTB device context.
931 * @db_bits: Doorbell bits to set.
933 * Set bits in the local doorbell register, which may generate a local doorbell
934 * interrupt. Bits that were already set must remain set.
936 * This is unusual, and hardware may not support it.
938 * Return: Zero on success, otherwise an error number.
940 static inline int ntb_db_set(struct ntb_dev
*ntb
, u64 db_bits
)
942 if (!ntb
->ops
->db_set
)
945 return ntb
->ops
->db_set(ntb
, db_bits
);
949 * ntb_db_clear() - clear bits in the local doorbell register
950 * @ntb: NTB device context.
951 * @db_bits: Doorbell bits to clear.
953 * Clear bits in the local doorbell register, arming the bits for the next
956 * Return: Zero on success, otherwise an error number.
958 static inline int ntb_db_clear(struct ntb_dev
*ntb
, u64 db_bits
)
960 return ntb
->ops
->db_clear(ntb
, db_bits
);
964 * ntb_db_read_mask() - read the local doorbell mask
965 * @ntb: NTB device context.
967 * Read the local doorbell mask register, and return the bits that are set.
969 * This is unusual, though hardware is likely to support it.
971 * Return: The bits currently set in the local doorbell mask register.
973 static inline u64
ntb_db_read_mask(struct ntb_dev
*ntb
)
975 if (!ntb
->ops
->db_read_mask
)
978 return ntb
->ops
->db_read_mask(ntb
);
982 * ntb_db_set_mask() - set bits in the local doorbell mask
983 * @ntb: NTB device context.
984 * @db_bits: Doorbell mask bits to set.
986 * Set bits in the local doorbell mask register, preventing doorbell interrupts
987 * from being generated for those doorbell bits. Bits that were already set
990 * Return: Zero on success, otherwise an error number.
992 static inline int ntb_db_set_mask(struct ntb_dev
*ntb
, u64 db_bits
)
994 return ntb
->ops
->db_set_mask(ntb
, db_bits
);
998 * ntb_db_clear_mask() - clear bits in the local doorbell mask
999 * @ntb: NTB device context.
1000 * @db_bits: Doorbell bits to clear.
1002 * Clear bits in the local doorbell mask register, allowing doorbell interrupts
1003 * from being generated for those doorbell bits. If a doorbell bit is already
1004 * set at the time the mask is cleared, and the corresponding mask bit is
1005 * changed from set to clear, then the ntb driver must ensure that
1006 * ntb_db_event() is called. If the hardware does not generate the interrupt
1007 * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
1009 * Return: Zero on success, otherwise an error number.
1011 static inline int ntb_db_clear_mask(struct ntb_dev
*ntb
, u64 db_bits
)
1013 return ntb
->ops
->db_clear_mask(ntb
, db_bits
);
1017 * ntb_peer_db_addr() - address and size of the peer doorbell register
1018 * @ntb: NTB device context.
1019 * @db_addr: OUT - The address of the peer doorbell register.
1020 * @db_size: OUT - The number of bytes to write the peer doorbell register.
1022 * Return the address of the peer doorbell register. This may be used, for
1023 * example, by drivers that offload memory copy operations to a dma engine.
1024 * The drivers may wish to ring the peer doorbell at the completion of memory
1025 * copy operations. For efficiency, and to simplify ordering of operations
1026 * between the dma memory copies and the ringing doorbell, the driver may
1027 * append one additional dma memory copy with the doorbell register as the
1028 * destination, after the memory copy operations.
1030 * Return: Zero on success, otherwise an error number.
1032 static inline int ntb_peer_db_addr(struct ntb_dev
*ntb
,
1033 phys_addr_t
*db_addr
,
1034 resource_size_t
*db_size
)
1036 if (!ntb
->ops
->peer_db_addr
)
1039 return ntb
->ops
->peer_db_addr(ntb
, db_addr
, db_size
);
1043 * ntb_peer_db_read() - read the peer doorbell register
1044 * @ntb: NTB device context.
1046 * Read the peer doorbell register, and return the bits that are set.
1048 * This is unusual, and hardware may not support it.
1050 * Return: The bits currently set in the peer doorbell register.
1052 static inline u64
ntb_peer_db_read(struct ntb_dev
*ntb
)
1054 if (!ntb
->ops
->peer_db_read
)
1057 return ntb
->ops
->peer_db_read(ntb
);
1061 * ntb_peer_db_set() - set bits in the peer doorbell register
1062 * @ntb: NTB device context.
1063 * @db_bits: Doorbell bits to set.
1065 * Set bits in the peer doorbell register, which may generate a peer doorbell
1066 * interrupt. Bits that were already set must remain set.
1068 * Return: Zero on success, otherwise an error number.
1070 static inline int ntb_peer_db_set(struct ntb_dev
*ntb
, u64 db_bits
)
1072 return ntb
->ops
->peer_db_set(ntb
, db_bits
);
1076 * ntb_peer_db_clear() - clear bits in the peer doorbell register
1077 * @ntb: NTB device context.
1078 * @db_bits: Doorbell bits to clear.
1080 * Clear bits in the peer doorbell register, arming the bits for the next
1083 * This is unusual, and hardware may not support it.
1085 * Return: Zero on success, otherwise an error number.
1087 static inline int ntb_peer_db_clear(struct ntb_dev
*ntb
, u64 db_bits
)
1089 if (!ntb
->ops
->db_clear
)
1092 return ntb
->ops
->peer_db_clear(ntb
, db_bits
);
1096 * ntb_peer_db_read_mask() - read the peer doorbell mask
1097 * @ntb: NTB device context.
1099 * Read the peer doorbell mask register, and return the bits that are set.
1101 * This is unusual, and hardware may not support it.
1103 * Return: The bits currently set in the peer doorbell mask register.
1105 static inline u64
ntb_peer_db_read_mask(struct ntb_dev
*ntb
)
1107 if (!ntb
->ops
->db_read_mask
)
1110 return ntb
->ops
->peer_db_read_mask(ntb
);
1114 * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
1115 * @ntb: NTB device context.
1116 * @db_bits: Doorbell mask bits to set.
1118 * Set bits in the peer doorbell mask register, preventing doorbell interrupts
1119 * from being generated for those doorbell bits. Bits that were already set
1122 * This is unusual, and hardware may not support it.
1124 * Return: Zero on success, otherwise an error number.
1126 static inline int ntb_peer_db_set_mask(struct ntb_dev
*ntb
, u64 db_bits
)
1128 if (!ntb
->ops
->db_set_mask
)
1131 return ntb
->ops
->peer_db_set_mask(ntb
, db_bits
);
1135 * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
1136 * @ntb: NTB device context.
1137 * @db_bits: Doorbell bits to clear.
1139 * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
1140 * from being generated for those doorbell bits. If the hardware does not
1141 * generate the interrupt on clearing the mask bit, then the driver should not
1142 * implement this function!
1144 * This is unusual, and hardware may not support it.
1146 * Return: Zero on success, otherwise an error number.
1148 static inline int ntb_peer_db_clear_mask(struct ntb_dev
*ntb
, u64 db_bits
)
1150 if (!ntb
->ops
->db_clear_mask
)
1153 return ntb
->ops
->peer_db_clear_mask(ntb
, db_bits
);
1157 * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
1158 * @ntb: NTB device context.
1160 * It is possible for some ntb hardware to be affected by errata. Hardware
1161 * drivers can advise clients to avoid using scratchpads. Clients may ignore
1162 * this advice, though caution is recommended.
1164 * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
1166 static inline int ntb_spad_is_unsafe(struct ntb_dev
*ntb
)
1168 if (!ntb
->ops
->spad_is_unsafe
)
1171 return ntb
->ops
->spad_is_unsafe(ntb
);
1175 * ntb_spad_count() - get the number of scratchpads
1176 * @ntb: NTB device context.
1178 * Hardware and topology may support a different number of scratchpads.
1179 * Although it must be the same for all ports per NTB device.
1181 * Return: the number of scratchpads.
1183 static inline int ntb_spad_count(struct ntb_dev
*ntb
)
1185 if (!ntb
->ops
->spad_count
)
1188 return ntb
->ops
->spad_count(ntb
);
1192 * ntb_spad_read() - read the local scratchpad register
1193 * @ntb: NTB device context.
1194 * @sidx: Scratchpad index.
1196 * Read the local scratchpad register, and return the value.
1198 * Return: The value of the local scratchpad register.
1200 static inline u32
ntb_spad_read(struct ntb_dev
*ntb
, int sidx
)
1202 if (!ntb
->ops
->spad_read
)
1205 return ntb
->ops
->spad_read(ntb
, sidx
);
1209 * ntb_spad_write() - write the local scratchpad register
1210 * @ntb: NTB device context.
1211 * @sidx: Scratchpad index.
1212 * @val: Scratchpad value.
1214 * Write the value to the local scratchpad register.
1216 * Return: Zero on success, otherwise an error number.
1218 static inline int ntb_spad_write(struct ntb_dev
*ntb
, int sidx
, u32 val
)
1220 if (!ntb
->ops
->spad_write
)
1223 return ntb
->ops
->spad_write(ntb
, sidx
, val
);
1227 * ntb_peer_spad_addr() - address of the peer scratchpad register
1228 * @ntb: NTB device context.
1229 * @pidx: Port index of peer device.
1230 * @sidx: Scratchpad index.
1231 * @spad_addr: OUT - The address of the peer scratchpad register.
1233 * Return the address of the peer doorbell register. This may be used, for
1234 * example, by drivers that offload memory copy operations to a dma engine.
1236 * Return: Zero on success, otherwise an error number.
1238 static inline int ntb_peer_spad_addr(struct ntb_dev
*ntb
, int pidx
, int sidx
,
1239 phys_addr_t
*spad_addr
)
1241 if (!ntb
->ops
->peer_spad_addr
)
1244 return ntb
->ops
->peer_spad_addr(ntb
, pidx
, sidx
, spad_addr
);
1248 * ntb_peer_spad_read() - read the peer scratchpad register
1249 * @ntb: NTB device context.
1250 * @pidx: Port index of peer device.
1251 * @sidx: Scratchpad index.
1253 * Read the peer scratchpad register, and return the value.
1255 * Return: The value of the local scratchpad register.
1257 static inline u32
ntb_peer_spad_read(struct ntb_dev
*ntb
, int pidx
, int sidx
)
1259 if (!ntb
->ops
->peer_spad_read
)
1262 return ntb
->ops
->peer_spad_read(ntb
, pidx
, sidx
);
1266 * ntb_peer_spad_write() - write the peer scratchpad register
1267 * @ntb: NTB device context.
1268 * @pidx: Port index of peer device.
1269 * @sidx: Scratchpad index.
1270 * @val: Scratchpad value.
1272 * Write the value to the peer scratchpad register.
1274 * Return: Zero on success, otherwise an error number.
1276 static inline int ntb_peer_spad_write(struct ntb_dev
*ntb
, int pidx
, int sidx
,
1279 if (!ntb
->ops
->peer_spad_write
)
1282 return ntb
->ops
->peer_spad_write(ntb
, pidx
, sidx
, val
);