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