xref: /OK3568_Linux_fs/kernel/drivers/ntb/hw/idt/ntb_hw_idt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   This file is provided under a GPLv2 license.  When using or
3*4882a593Smuzhiyun  *   redistributing this file, you may do so under that license.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *   GPL LICENSE SUMMARY
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   Copyright (C) 2016-2018 T-Platforms JSC All Rights Reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *   This program is free software; you can redistribute it and/or modify it
10*4882a593Smuzhiyun  *   under the terms and conditions of the GNU General Public License,
11*4882a593Smuzhiyun  *   version 2, as published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  *   WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16*4882a593Smuzhiyun  *   Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *   You should have received a copy of the GNU General Public License along
19*4882a593Smuzhiyun  *   with this program; if not, one can be found http://www.gnu.org/licenses/.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *   The full GNU General Public License is included in this distribution in
22*4882a593Smuzhiyun  *   the file called "COPYING".
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25*4882a593Smuzhiyun  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26*4882a593Smuzhiyun  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27*4882a593Smuzhiyun  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28*4882a593Smuzhiyun  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29*4882a593Smuzhiyun  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30*4882a593Smuzhiyun  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31*4882a593Smuzhiyun  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32*4882a593Smuzhiyun  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*4882a593Smuzhiyun  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34*4882a593Smuzhiyun  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * IDT PCIe-switch NTB Linux driver
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Contact Information:
39*4882a593Smuzhiyun  * Serge Semin <fancer.lancer@gmail.com>, <Sergey.Semin@t-platforms.ru>
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include <linux/stddef.h>
43*4882a593Smuzhiyun #include <linux/types.h>
44*4882a593Smuzhiyun #include <linux/kernel.h>
45*4882a593Smuzhiyun #include <linux/bitops.h>
46*4882a593Smuzhiyun #include <linux/sizes.h>
47*4882a593Smuzhiyun #include <linux/module.h>
48*4882a593Smuzhiyun #include <linux/moduleparam.h>
49*4882a593Smuzhiyun #include <linux/init.h>
50*4882a593Smuzhiyun #include <linux/interrupt.h>
51*4882a593Smuzhiyun #include <linux/spinlock.h>
52*4882a593Smuzhiyun #include <linux/mutex.h>
53*4882a593Smuzhiyun #include <linux/pci.h>
54*4882a593Smuzhiyun #include <linux/aer.h>
55*4882a593Smuzhiyun #include <linux/slab.h>
56*4882a593Smuzhiyun #include <linux/list.h>
57*4882a593Smuzhiyun #include <linux/debugfs.h>
58*4882a593Smuzhiyun #include <linux/hwmon.h>
59*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
60*4882a593Smuzhiyun #include <linux/ntb.h>
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #include "ntb_hw_idt.h"
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #define NTB_NAME	"ntb_hw_idt"
65*4882a593Smuzhiyun #define NTB_DESC	"IDT PCI-E Non-Transparent Bridge Driver"
66*4882a593Smuzhiyun #define NTB_VER		"2.0"
67*4882a593Smuzhiyun #define NTB_IRQNAME	"ntb_irq_idt"
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun MODULE_DESCRIPTION(NTB_DESC);
70*4882a593Smuzhiyun MODULE_VERSION(NTB_VER);
71*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
72*4882a593Smuzhiyun MODULE_AUTHOR("T-platforms");
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun  * NT Endpoint registers table simplifying a loop access to the functionally
76*4882a593Smuzhiyun  * related registers
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun static const struct idt_ntb_regs ntdata_tbl = {
79*4882a593Smuzhiyun 	{ {IDT_NT_BARSETUP0,	IDT_NT_BARLIMIT0,
80*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE0,	IDT_NT_BARUTBASE0},
81*4882a593Smuzhiyun 	  {IDT_NT_BARSETUP1,	IDT_NT_BARLIMIT1,
82*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE1,	IDT_NT_BARUTBASE1},
83*4882a593Smuzhiyun 	  {IDT_NT_BARSETUP2,	IDT_NT_BARLIMIT2,
84*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE2,	IDT_NT_BARUTBASE2},
85*4882a593Smuzhiyun 	  {IDT_NT_BARSETUP3,	IDT_NT_BARLIMIT3,
86*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE3,	IDT_NT_BARUTBASE3},
87*4882a593Smuzhiyun 	  {IDT_NT_BARSETUP4,	IDT_NT_BARLIMIT4,
88*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE4,	IDT_NT_BARUTBASE4},
89*4882a593Smuzhiyun 	  {IDT_NT_BARSETUP5,	IDT_NT_BARLIMIT5,
90*4882a593Smuzhiyun 	   IDT_NT_BARLTBASE5,	IDT_NT_BARUTBASE5} },
91*4882a593Smuzhiyun 	{ {IDT_NT_INMSG0,	IDT_NT_OUTMSG0,	IDT_NT_INMSGSRC0},
92*4882a593Smuzhiyun 	  {IDT_NT_INMSG1,	IDT_NT_OUTMSG1,	IDT_NT_INMSGSRC1},
93*4882a593Smuzhiyun 	  {IDT_NT_INMSG2,	IDT_NT_OUTMSG2,	IDT_NT_INMSGSRC2},
94*4882a593Smuzhiyun 	  {IDT_NT_INMSG3,	IDT_NT_OUTMSG3,	IDT_NT_INMSGSRC3} }
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * NT Endpoint ports data table with the corresponding pcie command, link
99*4882a593Smuzhiyun  * status, control and BAR-related registers
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun static const struct idt_ntb_port portdata_tbl[IDT_MAX_NR_PORTS] = {
102*4882a593Smuzhiyun /*0*/	{ IDT_SW_NTP0_PCIECMDSTS,	IDT_SW_NTP0_PCIELCTLSTS,
103*4882a593Smuzhiyun 	  IDT_SW_NTP0_NTCTL,
104*4882a593Smuzhiyun 	  IDT_SW_SWPORT0CTL,		IDT_SW_SWPORT0STS,
105*4882a593Smuzhiyun 	  { {IDT_SW_NTP0_BARSETUP0,	IDT_SW_NTP0_BARLIMIT0,
106*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE0,	IDT_SW_NTP0_BARUTBASE0},
107*4882a593Smuzhiyun 	    {IDT_SW_NTP0_BARSETUP1,	IDT_SW_NTP0_BARLIMIT1,
108*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE1,	IDT_SW_NTP0_BARUTBASE1},
109*4882a593Smuzhiyun 	    {IDT_SW_NTP0_BARSETUP2,	IDT_SW_NTP0_BARLIMIT2,
110*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE2,	IDT_SW_NTP0_BARUTBASE2},
111*4882a593Smuzhiyun 	    {IDT_SW_NTP0_BARSETUP3,	IDT_SW_NTP0_BARLIMIT3,
112*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE3,	IDT_SW_NTP0_BARUTBASE3},
113*4882a593Smuzhiyun 	    {IDT_SW_NTP0_BARSETUP4,	IDT_SW_NTP0_BARLIMIT4,
114*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE4,	IDT_SW_NTP0_BARUTBASE4},
115*4882a593Smuzhiyun 	    {IDT_SW_NTP0_BARSETUP5,	IDT_SW_NTP0_BARLIMIT5,
116*4882a593Smuzhiyun 	     IDT_SW_NTP0_BARLTBASE5,	IDT_SW_NTP0_BARUTBASE5} } },
117*4882a593Smuzhiyun /*1*/	{0},
118*4882a593Smuzhiyun /*2*/	{ IDT_SW_NTP2_PCIECMDSTS,	IDT_SW_NTP2_PCIELCTLSTS,
119*4882a593Smuzhiyun 	  IDT_SW_NTP2_NTCTL,
120*4882a593Smuzhiyun 	  IDT_SW_SWPORT2CTL,		IDT_SW_SWPORT2STS,
121*4882a593Smuzhiyun 	  { {IDT_SW_NTP2_BARSETUP0,	IDT_SW_NTP2_BARLIMIT0,
122*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE0,	IDT_SW_NTP2_BARUTBASE0},
123*4882a593Smuzhiyun 	    {IDT_SW_NTP2_BARSETUP1,	IDT_SW_NTP2_BARLIMIT1,
124*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE1,	IDT_SW_NTP2_BARUTBASE1},
125*4882a593Smuzhiyun 	    {IDT_SW_NTP2_BARSETUP2,	IDT_SW_NTP2_BARLIMIT2,
126*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE2,	IDT_SW_NTP2_BARUTBASE2},
127*4882a593Smuzhiyun 	    {IDT_SW_NTP2_BARSETUP3,	IDT_SW_NTP2_BARLIMIT3,
128*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE3,	IDT_SW_NTP2_BARUTBASE3},
129*4882a593Smuzhiyun 	    {IDT_SW_NTP2_BARSETUP4,	IDT_SW_NTP2_BARLIMIT4,
130*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE4,	IDT_SW_NTP2_BARUTBASE4},
131*4882a593Smuzhiyun 	    {IDT_SW_NTP2_BARSETUP5,	IDT_SW_NTP2_BARLIMIT5,
132*4882a593Smuzhiyun 	     IDT_SW_NTP2_BARLTBASE5,	IDT_SW_NTP2_BARUTBASE5} } },
133*4882a593Smuzhiyun /*3*/	{0},
134*4882a593Smuzhiyun /*4*/	{ IDT_SW_NTP4_PCIECMDSTS,	IDT_SW_NTP4_PCIELCTLSTS,
135*4882a593Smuzhiyun 	  IDT_SW_NTP4_NTCTL,
136*4882a593Smuzhiyun 	  IDT_SW_SWPORT4CTL,		IDT_SW_SWPORT4STS,
137*4882a593Smuzhiyun 	  { {IDT_SW_NTP4_BARSETUP0,	IDT_SW_NTP4_BARLIMIT0,
138*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE0,	IDT_SW_NTP4_BARUTBASE0},
139*4882a593Smuzhiyun 	    {IDT_SW_NTP4_BARSETUP1,	IDT_SW_NTP4_BARLIMIT1,
140*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE1,	IDT_SW_NTP4_BARUTBASE1},
141*4882a593Smuzhiyun 	    {IDT_SW_NTP4_BARSETUP2,	IDT_SW_NTP4_BARLIMIT2,
142*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE2,	IDT_SW_NTP4_BARUTBASE2},
143*4882a593Smuzhiyun 	    {IDT_SW_NTP4_BARSETUP3,	IDT_SW_NTP4_BARLIMIT3,
144*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE3,	IDT_SW_NTP4_BARUTBASE3},
145*4882a593Smuzhiyun 	    {IDT_SW_NTP4_BARSETUP4,	IDT_SW_NTP4_BARLIMIT4,
146*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE4,	IDT_SW_NTP4_BARUTBASE4},
147*4882a593Smuzhiyun 	    {IDT_SW_NTP4_BARSETUP5,	IDT_SW_NTP4_BARLIMIT5,
148*4882a593Smuzhiyun 	     IDT_SW_NTP4_BARLTBASE5,	IDT_SW_NTP4_BARUTBASE5} } },
149*4882a593Smuzhiyun /*5*/	{0},
150*4882a593Smuzhiyun /*6*/	{ IDT_SW_NTP6_PCIECMDSTS,	IDT_SW_NTP6_PCIELCTLSTS,
151*4882a593Smuzhiyun 	  IDT_SW_NTP6_NTCTL,
152*4882a593Smuzhiyun 	  IDT_SW_SWPORT6CTL,		IDT_SW_SWPORT6STS,
153*4882a593Smuzhiyun 	  { {IDT_SW_NTP6_BARSETUP0,	IDT_SW_NTP6_BARLIMIT0,
154*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE0,	IDT_SW_NTP6_BARUTBASE0},
155*4882a593Smuzhiyun 	    {IDT_SW_NTP6_BARSETUP1,	IDT_SW_NTP6_BARLIMIT1,
156*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE1,	IDT_SW_NTP6_BARUTBASE1},
157*4882a593Smuzhiyun 	    {IDT_SW_NTP6_BARSETUP2,	IDT_SW_NTP6_BARLIMIT2,
158*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE2,	IDT_SW_NTP6_BARUTBASE2},
159*4882a593Smuzhiyun 	    {IDT_SW_NTP6_BARSETUP3,	IDT_SW_NTP6_BARLIMIT3,
160*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE3,	IDT_SW_NTP6_BARUTBASE3},
161*4882a593Smuzhiyun 	    {IDT_SW_NTP6_BARSETUP4,	IDT_SW_NTP6_BARLIMIT4,
162*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE4,	IDT_SW_NTP6_BARUTBASE4},
163*4882a593Smuzhiyun 	    {IDT_SW_NTP6_BARSETUP5,	IDT_SW_NTP6_BARLIMIT5,
164*4882a593Smuzhiyun 	     IDT_SW_NTP6_BARLTBASE5,	IDT_SW_NTP6_BARUTBASE5} } },
165*4882a593Smuzhiyun /*7*/	{0},
166*4882a593Smuzhiyun /*8*/	{ IDT_SW_NTP8_PCIECMDSTS,	IDT_SW_NTP8_PCIELCTLSTS,
167*4882a593Smuzhiyun 	  IDT_SW_NTP8_NTCTL,
168*4882a593Smuzhiyun 	  IDT_SW_SWPORT8CTL,		IDT_SW_SWPORT8STS,
169*4882a593Smuzhiyun 	  { {IDT_SW_NTP8_BARSETUP0,	IDT_SW_NTP8_BARLIMIT0,
170*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE0,	IDT_SW_NTP8_BARUTBASE0},
171*4882a593Smuzhiyun 	    {IDT_SW_NTP8_BARSETUP1,	IDT_SW_NTP8_BARLIMIT1,
172*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE1,	IDT_SW_NTP8_BARUTBASE1},
173*4882a593Smuzhiyun 	    {IDT_SW_NTP8_BARSETUP2,	IDT_SW_NTP8_BARLIMIT2,
174*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE2,	IDT_SW_NTP8_BARUTBASE2},
175*4882a593Smuzhiyun 	    {IDT_SW_NTP8_BARSETUP3,	IDT_SW_NTP8_BARLIMIT3,
176*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE3,	IDT_SW_NTP8_BARUTBASE3},
177*4882a593Smuzhiyun 	    {IDT_SW_NTP8_BARSETUP4,	IDT_SW_NTP8_BARLIMIT4,
178*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE4,	IDT_SW_NTP8_BARUTBASE4},
179*4882a593Smuzhiyun 	    {IDT_SW_NTP8_BARSETUP5,	IDT_SW_NTP8_BARLIMIT5,
180*4882a593Smuzhiyun 	     IDT_SW_NTP8_BARLTBASE5,	IDT_SW_NTP8_BARUTBASE5} } },
181*4882a593Smuzhiyun /*9*/	{0},
182*4882a593Smuzhiyun /*10*/	{0},
183*4882a593Smuzhiyun /*11*/	{0},
184*4882a593Smuzhiyun /*12*/	{ IDT_SW_NTP12_PCIECMDSTS,	IDT_SW_NTP12_PCIELCTLSTS,
185*4882a593Smuzhiyun 	  IDT_SW_NTP12_NTCTL,
186*4882a593Smuzhiyun 	  IDT_SW_SWPORT12CTL,		IDT_SW_SWPORT12STS,
187*4882a593Smuzhiyun 	  { {IDT_SW_NTP12_BARSETUP0,	IDT_SW_NTP12_BARLIMIT0,
188*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE0,	IDT_SW_NTP12_BARUTBASE0},
189*4882a593Smuzhiyun 	    {IDT_SW_NTP12_BARSETUP1,	IDT_SW_NTP12_BARLIMIT1,
190*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE1,	IDT_SW_NTP12_BARUTBASE1},
191*4882a593Smuzhiyun 	    {IDT_SW_NTP12_BARSETUP2,	IDT_SW_NTP12_BARLIMIT2,
192*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE2,	IDT_SW_NTP12_BARUTBASE2},
193*4882a593Smuzhiyun 	    {IDT_SW_NTP12_BARSETUP3,	IDT_SW_NTP12_BARLIMIT3,
194*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE3,	IDT_SW_NTP12_BARUTBASE3},
195*4882a593Smuzhiyun 	    {IDT_SW_NTP12_BARSETUP4,	IDT_SW_NTP12_BARLIMIT4,
196*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE4,	IDT_SW_NTP12_BARUTBASE4},
197*4882a593Smuzhiyun 	    {IDT_SW_NTP12_BARSETUP5,	IDT_SW_NTP12_BARLIMIT5,
198*4882a593Smuzhiyun 	     IDT_SW_NTP12_BARLTBASE5,	IDT_SW_NTP12_BARUTBASE5} } },
199*4882a593Smuzhiyun /*13*/	{0},
200*4882a593Smuzhiyun /*14*/	{0},
201*4882a593Smuzhiyun /*15*/	{0},
202*4882a593Smuzhiyun /*16*/	{ IDT_SW_NTP16_PCIECMDSTS,	IDT_SW_NTP16_PCIELCTLSTS,
203*4882a593Smuzhiyun 	  IDT_SW_NTP16_NTCTL,
204*4882a593Smuzhiyun 	  IDT_SW_SWPORT16CTL,		IDT_SW_SWPORT16STS,
205*4882a593Smuzhiyun 	  { {IDT_SW_NTP16_BARSETUP0,	IDT_SW_NTP16_BARLIMIT0,
206*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE0,	IDT_SW_NTP16_BARUTBASE0},
207*4882a593Smuzhiyun 	    {IDT_SW_NTP16_BARSETUP1,	IDT_SW_NTP16_BARLIMIT1,
208*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE1,	IDT_SW_NTP16_BARUTBASE1},
209*4882a593Smuzhiyun 	    {IDT_SW_NTP16_BARSETUP2,	IDT_SW_NTP16_BARLIMIT2,
210*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE2,	IDT_SW_NTP16_BARUTBASE2},
211*4882a593Smuzhiyun 	    {IDT_SW_NTP16_BARSETUP3,	IDT_SW_NTP16_BARLIMIT3,
212*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE3,	IDT_SW_NTP16_BARUTBASE3},
213*4882a593Smuzhiyun 	    {IDT_SW_NTP16_BARSETUP4,	IDT_SW_NTP16_BARLIMIT4,
214*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE4,	IDT_SW_NTP16_BARUTBASE4},
215*4882a593Smuzhiyun 	    {IDT_SW_NTP16_BARSETUP5,	IDT_SW_NTP16_BARLIMIT5,
216*4882a593Smuzhiyun 	     IDT_SW_NTP16_BARLTBASE5,	IDT_SW_NTP16_BARUTBASE5} } },
217*4882a593Smuzhiyun /*17*/	{0},
218*4882a593Smuzhiyun /*18*/	{0},
219*4882a593Smuzhiyun /*19*/	{0},
220*4882a593Smuzhiyun /*20*/	{ IDT_SW_NTP20_PCIECMDSTS,	IDT_SW_NTP20_PCIELCTLSTS,
221*4882a593Smuzhiyun 	  IDT_SW_NTP20_NTCTL,
222*4882a593Smuzhiyun 	  IDT_SW_SWPORT20CTL,		IDT_SW_SWPORT20STS,
223*4882a593Smuzhiyun 	  { {IDT_SW_NTP20_BARSETUP0,	IDT_SW_NTP20_BARLIMIT0,
224*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE0,	IDT_SW_NTP20_BARUTBASE0},
225*4882a593Smuzhiyun 	    {IDT_SW_NTP20_BARSETUP1,	IDT_SW_NTP20_BARLIMIT1,
226*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE1,	IDT_SW_NTP20_BARUTBASE1},
227*4882a593Smuzhiyun 	    {IDT_SW_NTP20_BARSETUP2,	IDT_SW_NTP20_BARLIMIT2,
228*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE2,	IDT_SW_NTP20_BARUTBASE2},
229*4882a593Smuzhiyun 	    {IDT_SW_NTP20_BARSETUP3,	IDT_SW_NTP20_BARLIMIT3,
230*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE3,	IDT_SW_NTP20_BARUTBASE3},
231*4882a593Smuzhiyun 	    {IDT_SW_NTP20_BARSETUP4,	IDT_SW_NTP20_BARLIMIT4,
232*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE4,	IDT_SW_NTP20_BARUTBASE4},
233*4882a593Smuzhiyun 	    {IDT_SW_NTP20_BARSETUP5,	IDT_SW_NTP20_BARLIMIT5,
234*4882a593Smuzhiyun 	     IDT_SW_NTP20_BARLTBASE5,	IDT_SW_NTP20_BARUTBASE5} } },
235*4882a593Smuzhiyun /*21*/	{0},
236*4882a593Smuzhiyun /*22*/	{0},
237*4882a593Smuzhiyun /*23*/	{0}
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /*
241*4882a593Smuzhiyun  * IDT PCIe-switch partitions table with the corresponding control, status
242*4882a593Smuzhiyun  * and messages control registers
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun static const struct idt_ntb_part partdata_tbl[IDT_MAX_NR_PARTS] = {
245*4882a593Smuzhiyun /*0*/	{ IDT_SW_SWPART0CTL,	IDT_SW_SWPART0STS,
246*4882a593Smuzhiyun 	  {IDT_SW_SWP0MSGCTL0,	IDT_SW_SWP0MSGCTL1,
247*4882a593Smuzhiyun 	   IDT_SW_SWP0MSGCTL2,	IDT_SW_SWP0MSGCTL3} },
248*4882a593Smuzhiyun /*1*/	{ IDT_SW_SWPART1CTL,	IDT_SW_SWPART1STS,
249*4882a593Smuzhiyun 	  {IDT_SW_SWP1MSGCTL0,	IDT_SW_SWP1MSGCTL1,
250*4882a593Smuzhiyun 	   IDT_SW_SWP1MSGCTL2,	IDT_SW_SWP1MSGCTL3} },
251*4882a593Smuzhiyun /*2*/	{ IDT_SW_SWPART2CTL,	IDT_SW_SWPART2STS,
252*4882a593Smuzhiyun 	  {IDT_SW_SWP2MSGCTL0,	IDT_SW_SWP2MSGCTL1,
253*4882a593Smuzhiyun 	   IDT_SW_SWP2MSGCTL2,	IDT_SW_SWP2MSGCTL3} },
254*4882a593Smuzhiyun /*3*/	{ IDT_SW_SWPART3CTL,	IDT_SW_SWPART3STS,
255*4882a593Smuzhiyun 	  {IDT_SW_SWP3MSGCTL0,	IDT_SW_SWP3MSGCTL1,
256*4882a593Smuzhiyun 	   IDT_SW_SWP3MSGCTL2,	IDT_SW_SWP3MSGCTL3} },
257*4882a593Smuzhiyun /*4*/	{ IDT_SW_SWPART4CTL,	IDT_SW_SWPART4STS,
258*4882a593Smuzhiyun 	  {IDT_SW_SWP4MSGCTL0,	IDT_SW_SWP4MSGCTL1,
259*4882a593Smuzhiyun 	   IDT_SW_SWP4MSGCTL2,	IDT_SW_SWP4MSGCTL3} },
260*4882a593Smuzhiyun /*5*/	{ IDT_SW_SWPART5CTL,	IDT_SW_SWPART5STS,
261*4882a593Smuzhiyun 	  {IDT_SW_SWP5MSGCTL0,	IDT_SW_SWP5MSGCTL1,
262*4882a593Smuzhiyun 	   IDT_SW_SWP5MSGCTL2,	IDT_SW_SWP5MSGCTL3} },
263*4882a593Smuzhiyun /*6*/	{ IDT_SW_SWPART6CTL,	IDT_SW_SWPART6STS,
264*4882a593Smuzhiyun 	  {IDT_SW_SWP6MSGCTL0,	IDT_SW_SWP6MSGCTL1,
265*4882a593Smuzhiyun 	   IDT_SW_SWP6MSGCTL2,	IDT_SW_SWP6MSGCTL3} },
266*4882a593Smuzhiyun /*7*/	{ IDT_SW_SWPART7CTL,	IDT_SW_SWPART7STS,
267*4882a593Smuzhiyun 	  {IDT_SW_SWP7MSGCTL0,	IDT_SW_SWP7MSGCTL1,
268*4882a593Smuzhiyun 	   IDT_SW_SWP7MSGCTL2,	IDT_SW_SWP7MSGCTL3} }
269*4882a593Smuzhiyun };
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /*
272*4882a593Smuzhiyun  * DebugFS directory to place the driver debug file
273*4882a593Smuzhiyun  */
274*4882a593Smuzhiyun static struct dentry *dbgfs_topdir;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*=============================================================================
277*4882a593Smuzhiyun  *                1. IDT PCIe-switch registers IO-functions
278*4882a593Smuzhiyun  *
279*4882a593Smuzhiyun  *    Beside ordinary configuration space registers IDT PCIe-switch expose
280*4882a593Smuzhiyun  * global configuration registers, which are used to determine state of other
281*4882a593Smuzhiyun  * device ports as well as being notified of some switch-related events.
282*4882a593Smuzhiyun  * Additionally all the configuration space registers of all the IDT
283*4882a593Smuzhiyun  * PCIe-switch functions are mapped to the Global Address space, so each
284*4882a593Smuzhiyun  * function can determine a configuration of any other PCI-function.
285*4882a593Smuzhiyun  *    Functions declared in this chapter are created to encapsulate access
286*4882a593Smuzhiyun  * to configuration and global registers, so the driver code just need to
287*4882a593Smuzhiyun  * provide IDT NTB hardware descriptor and a register address.
288*4882a593Smuzhiyun  *=============================================================================
289*4882a593Smuzhiyun  */
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun  * idt_nt_write() - PCI configuration space registers write method
293*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
294*4882a593Smuzhiyun  * @reg:	Register to write data to
295*4882a593Smuzhiyun  * @data:	Value to write to the register
296*4882a593Smuzhiyun  *
297*4882a593Smuzhiyun  * IDT PCIe-switch registers are all Little endian.
298*4882a593Smuzhiyun  */
idt_nt_write(struct idt_ntb_dev * ndev,const unsigned int reg,const u32 data)299*4882a593Smuzhiyun static void idt_nt_write(struct idt_ntb_dev *ndev,
300*4882a593Smuzhiyun 			 const unsigned int reg, const u32 data)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	/*
303*4882a593Smuzhiyun 	 * It's obvious bug to request a register exceeding the maximum possible
304*4882a593Smuzhiyun 	 * value as well as to have it unaligned.
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 	if (WARN_ON(reg > IDT_REG_PCI_MAX || !IS_ALIGNED(reg, IDT_REG_ALIGN)))
307*4882a593Smuzhiyun 		return;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/* Just write the value to the specified register */
310*4882a593Smuzhiyun 	iowrite32(data, ndev->cfgspc + (ptrdiff_t)reg);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /*
314*4882a593Smuzhiyun  * idt_nt_read() - PCI configuration space registers read method
315*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
316*4882a593Smuzhiyun  * @reg:	Register to write data to
317*4882a593Smuzhiyun  *
318*4882a593Smuzhiyun  * IDT PCIe-switch Global configuration registers are all Little endian.
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * Return: register value
321*4882a593Smuzhiyun  */
idt_nt_read(struct idt_ntb_dev * ndev,const unsigned int reg)322*4882a593Smuzhiyun static u32 idt_nt_read(struct idt_ntb_dev *ndev, const unsigned int reg)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	/*
325*4882a593Smuzhiyun 	 * It's obvious bug to request a register exceeding the maximum possible
326*4882a593Smuzhiyun 	 * value as well as to have it unaligned.
327*4882a593Smuzhiyun 	 */
328*4882a593Smuzhiyun 	if (WARN_ON(reg > IDT_REG_PCI_MAX || !IS_ALIGNED(reg, IDT_REG_ALIGN)))
329*4882a593Smuzhiyun 		return ~0;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	/* Just read the value from the specified register */
332*4882a593Smuzhiyun 	return ioread32(ndev->cfgspc + (ptrdiff_t)reg);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun  * idt_sw_write() - Global registers write method
337*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
338*4882a593Smuzhiyun  * @reg:	Register to write data to
339*4882a593Smuzhiyun  * @data:	Value to write to the register
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * IDT PCIe-switch Global configuration registers are all Little endian.
342*4882a593Smuzhiyun  */
idt_sw_write(struct idt_ntb_dev * ndev,const unsigned int reg,const u32 data)343*4882a593Smuzhiyun static void idt_sw_write(struct idt_ntb_dev *ndev,
344*4882a593Smuzhiyun 			 const unsigned int reg, const u32 data)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	unsigned long irqflags;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/*
349*4882a593Smuzhiyun 	 * It's obvious bug to request a register exceeding the maximum possible
350*4882a593Smuzhiyun 	 * value as well as to have it unaligned.
351*4882a593Smuzhiyun 	 */
352*4882a593Smuzhiyun 	if (WARN_ON(reg > IDT_REG_SW_MAX || !IS_ALIGNED(reg, IDT_REG_ALIGN)))
353*4882a593Smuzhiyun 		return;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	/* Lock GASA registers operations */
356*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->gasa_lock, irqflags);
357*4882a593Smuzhiyun 	/* Set the global register address */
358*4882a593Smuzhiyun 	iowrite32((u32)reg, ndev->cfgspc + (ptrdiff_t)IDT_NT_GASAADDR);
359*4882a593Smuzhiyun 	/* Put the new value of the register */
360*4882a593Smuzhiyun 	iowrite32(data, ndev->cfgspc + (ptrdiff_t)IDT_NT_GASADATA);
361*4882a593Smuzhiyun 	/* Unlock GASA registers operations */
362*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->gasa_lock, irqflags);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /*
366*4882a593Smuzhiyun  * idt_sw_read() - Global registers read method
367*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
368*4882a593Smuzhiyun  * @reg:	Register to write data to
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * IDT PCIe-switch Global configuration registers are all Little endian.
371*4882a593Smuzhiyun  *
372*4882a593Smuzhiyun  * Return: register value
373*4882a593Smuzhiyun  */
idt_sw_read(struct idt_ntb_dev * ndev,const unsigned int reg)374*4882a593Smuzhiyun static u32 idt_sw_read(struct idt_ntb_dev *ndev, const unsigned int reg)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	unsigned long irqflags;
377*4882a593Smuzhiyun 	u32 data;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/*
380*4882a593Smuzhiyun 	 * It's obvious bug to request a register exceeding the maximum possible
381*4882a593Smuzhiyun 	 * value as well as to have it unaligned.
382*4882a593Smuzhiyun 	 */
383*4882a593Smuzhiyun 	if (WARN_ON(reg > IDT_REG_SW_MAX || !IS_ALIGNED(reg, IDT_REG_ALIGN)))
384*4882a593Smuzhiyun 		return ~0;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	/* Lock GASA registers operations */
387*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->gasa_lock, irqflags);
388*4882a593Smuzhiyun 	/* Set the global register address */
389*4882a593Smuzhiyun 	iowrite32((u32)reg, ndev->cfgspc + (ptrdiff_t)IDT_NT_GASAADDR);
390*4882a593Smuzhiyun 	/* Get the data of the register (read ops acts as MMIO barrier) */
391*4882a593Smuzhiyun 	data = ioread32(ndev->cfgspc + (ptrdiff_t)IDT_NT_GASADATA);
392*4882a593Smuzhiyun 	/* Unlock GASA registers operations */
393*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->gasa_lock, irqflags);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	return data;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun  * idt_reg_set_bits() - set bits of a passed register
400*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
401*4882a593Smuzhiyun  * @reg:	Register to change bits of
402*4882a593Smuzhiyun  * @reg_lock:	Register access spin lock
403*4882a593Smuzhiyun  * @valid_mask:	Mask of valid bits
404*4882a593Smuzhiyun  * @set_bits:	Bitmask to set
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * Helper method to check whether a passed bitfield is valid and set
407*4882a593Smuzhiyun  * corresponding bits of a register.
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * WARNING! Make sure the passed register isn't accessed over plane
410*4882a593Smuzhiyun  * idt_nt_write() method (read method is ok to be used concurrently).
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * Return: zero on success, negative error on invalid bitmask.
413*4882a593Smuzhiyun  */
idt_reg_set_bits(struct idt_ntb_dev * ndev,unsigned int reg,spinlock_t * reg_lock,u64 valid_mask,u64 set_bits)414*4882a593Smuzhiyun static inline int idt_reg_set_bits(struct idt_ntb_dev *ndev, unsigned int reg,
415*4882a593Smuzhiyun 				   spinlock_t *reg_lock,
416*4882a593Smuzhiyun 				   u64 valid_mask, u64 set_bits)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	unsigned long irqflags;
419*4882a593Smuzhiyun 	u32 data;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	if (set_bits & ~(u64)valid_mask)
422*4882a593Smuzhiyun 		return -EINVAL;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	/* Lock access to the register unless the change is written back */
425*4882a593Smuzhiyun 	spin_lock_irqsave(reg_lock, irqflags);
426*4882a593Smuzhiyun 	data = idt_nt_read(ndev, reg) | (u32)set_bits;
427*4882a593Smuzhiyun 	idt_nt_write(ndev, reg, data);
428*4882a593Smuzhiyun 	/* Unlock the register */
429*4882a593Smuzhiyun 	spin_unlock_irqrestore(reg_lock, irqflags);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /*
435*4882a593Smuzhiyun  * idt_reg_clear_bits() - clear bits of a passed register
436*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
437*4882a593Smuzhiyun  * @reg:	Register to change bits of
438*4882a593Smuzhiyun  * @reg_lock:	Register access spin lock
439*4882a593Smuzhiyun  * @set_bits:	Bitmask to clear
440*4882a593Smuzhiyun  *
441*4882a593Smuzhiyun  * Helper method to check whether a passed bitfield is valid and clear
442*4882a593Smuzhiyun  * corresponding bits of a register.
443*4882a593Smuzhiyun  *
444*4882a593Smuzhiyun  * NOTE! Invalid bits are always considered cleared so it's not an error
445*4882a593Smuzhiyun  * to clear them over.
446*4882a593Smuzhiyun  *
447*4882a593Smuzhiyun  * WARNING! Make sure the passed register isn't accessed over plane
448*4882a593Smuzhiyun  * idt_nt_write() method (read method is ok to use concurrently).
449*4882a593Smuzhiyun  */
idt_reg_clear_bits(struct idt_ntb_dev * ndev,unsigned int reg,spinlock_t * reg_lock,u64 clear_bits)450*4882a593Smuzhiyun static inline void idt_reg_clear_bits(struct idt_ntb_dev *ndev,
451*4882a593Smuzhiyun 				     unsigned int reg, spinlock_t *reg_lock,
452*4882a593Smuzhiyun 				     u64 clear_bits)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	unsigned long irqflags;
455*4882a593Smuzhiyun 	u32 data;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* Lock access to the register unless the change is written back */
458*4882a593Smuzhiyun 	spin_lock_irqsave(reg_lock, irqflags);
459*4882a593Smuzhiyun 	data = idt_nt_read(ndev, reg) & ~(u32)clear_bits;
460*4882a593Smuzhiyun 	idt_nt_write(ndev, reg, data);
461*4882a593Smuzhiyun 	/* Unlock the register */
462*4882a593Smuzhiyun 	spin_unlock_irqrestore(reg_lock, irqflags);
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /*===========================================================================
466*4882a593Smuzhiyun  *                           2. Ports operations
467*4882a593Smuzhiyun  *
468*4882a593Smuzhiyun  *    IDT PCIe-switches can have from 3 up to 8 ports with possible
469*4882a593Smuzhiyun  * NT-functions enabled. So all the possible ports need to be scanned looking
470*4882a593Smuzhiyun  * for NTB activated. NTB API will have enumerated only the ports with NTB.
471*4882a593Smuzhiyun  *===========================================================================
472*4882a593Smuzhiyun  */
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun  * idt_scan_ports() - scan IDT PCIe-switch ports collecting info in the tables
476*4882a593Smuzhiyun  * @ndev:	Pointer to the PCI device descriptor
477*4882a593Smuzhiyun  *
478*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number.
479*4882a593Smuzhiyun  */
idt_scan_ports(struct idt_ntb_dev * ndev)480*4882a593Smuzhiyun static int idt_scan_ports(struct idt_ntb_dev *ndev)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	unsigned char pidx, port, part;
483*4882a593Smuzhiyun 	u32 data, portsts, partsts;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* Retrieve the local port number */
486*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_PCIELCAP);
487*4882a593Smuzhiyun 	ndev->port = GET_FIELD(PCIELCAP_PORTNUM, data);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	/* Retrieve the local partition number */
490*4882a593Smuzhiyun 	portsts = idt_sw_read(ndev, portdata_tbl[ndev->port].sts);
491*4882a593Smuzhiyun 	ndev->part = GET_FIELD(SWPORTxSTS_SWPART, portsts);
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	/* Initialize port/partition -> index tables with invalid values */
494*4882a593Smuzhiyun 	memset(ndev->port_idx_map, -EINVAL, sizeof(ndev->port_idx_map));
495*4882a593Smuzhiyun 	memset(ndev->part_idx_map, -EINVAL, sizeof(ndev->part_idx_map));
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	/*
498*4882a593Smuzhiyun 	 * Walk over all the possible ports checking whether any of them has
499*4882a593Smuzhiyun 	 * NT-function activated
500*4882a593Smuzhiyun 	 */
501*4882a593Smuzhiyun 	ndev->peer_cnt = 0;
502*4882a593Smuzhiyun 	for (pidx = 0; pidx < ndev->swcfg->port_cnt; pidx++) {
503*4882a593Smuzhiyun 		port = ndev->swcfg->ports[pidx];
504*4882a593Smuzhiyun 		/* Skip local port */
505*4882a593Smuzhiyun 		if (port == ndev->port)
506*4882a593Smuzhiyun 			continue;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 		/* Read the port status register to get it partition */
509*4882a593Smuzhiyun 		portsts = idt_sw_read(ndev, portdata_tbl[port].sts);
510*4882a593Smuzhiyun 		part = GET_FIELD(SWPORTxSTS_SWPART, portsts);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 		/* Retrieve the partition status */
513*4882a593Smuzhiyun 		partsts = idt_sw_read(ndev, partdata_tbl[part].sts);
514*4882a593Smuzhiyun 		/* Check if partition state is active and port has NTB */
515*4882a593Smuzhiyun 		if (IS_FLD_SET(SWPARTxSTS_STATE, partsts, ACT) &&
516*4882a593Smuzhiyun 		    (IS_FLD_SET(SWPORTxSTS_MODE, portsts, NT) ||
517*4882a593Smuzhiyun 		     IS_FLD_SET(SWPORTxSTS_MODE, portsts, USNT) ||
518*4882a593Smuzhiyun 		     IS_FLD_SET(SWPORTxSTS_MODE, portsts, USNTDMA) ||
519*4882a593Smuzhiyun 		     IS_FLD_SET(SWPORTxSTS_MODE, portsts, NTDMA))) {
520*4882a593Smuzhiyun 			/* Save the port and partition numbers */
521*4882a593Smuzhiyun 			ndev->peers[ndev->peer_cnt].port = port;
522*4882a593Smuzhiyun 			ndev->peers[ndev->peer_cnt].part = part;
523*4882a593Smuzhiyun 			/* Fill in the port/partition -> index tables */
524*4882a593Smuzhiyun 			ndev->port_idx_map[port] = ndev->peer_cnt;
525*4882a593Smuzhiyun 			ndev->part_idx_map[part] = ndev->peer_cnt;
526*4882a593Smuzhiyun 			ndev->peer_cnt++;
527*4882a593Smuzhiyun 		}
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Local port: %hhu, num of peers: %hhu\n",
531*4882a593Smuzhiyun 		ndev->port, ndev->peer_cnt);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	/* It's useless to have this driver loaded if there is no any peer */
534*4882a593Smuzhiyun 	if (ndev->peer_cnt == 0) {
535*4882a593Smuzhiyun 		dev_warn(&ndev->ntb.pdev->dev, "No active peer found\n");
536*4882a593Smuzhiyun 		return -ENODEV;
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	return 0;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun /*
543*4882a593Smuzhiyun  * idt_ntb_port_number() - get the local port number
544*4882a593Smuzhiyun  * @ntb:	NTB device context.
545*4882a593Smuzhiyun  *
546*4882a593Smuzhiyun  * Return: the local port number
547*4882a593Smuzhiyun  */
idt_ntb_port_number(struct ntb_dev * ntb)548*4882a593Smuzhiyun static int idt_ntb_port_number(struct ntb_dev *ntb)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	return ndev->port;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /*
556*4882a593Smuzhiyun  * idt_ntb_peer_port_count() - get the number of peer ports
557*4882a593Smuzhiyun  * @ntb:	NTB device context.
558*4882a593Smuzhiyun  *
559*4882a593Smuzhiyun  * Return the count of detected peer NT-functions.
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * Return: number of peer ports
562*4882a593Smuzhiyun  */
idt_ntb_peer_port_count(struct ntb_dev * ntb)563*4882a593Smuzhiyun static int idt_ntb_peer_port_count(struct ntb_dev *ntb)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	return ndev->peer_cnt;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun /*
571*4882a593Smuzhiyun  * idt_ntb_peer_port_number() - get peer port by given index
572*4882a593Smuzhiyun  * @ntb:	NTB device context.
573*4882a593Smuzhiyun  * @pidx:	Peer port index.
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * Return: peer port or negative error
576*4882a593Smuzhiyun  */
idt_ntb_peer_port_number(struct ntb_dev * ntb,int pidx)577*4882a593Smuzhiyun static int idt_ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
582*4882a593Smuzhiyun 		return -EINVAL;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* Return the detected NT-function port number */
585*4882a593Smuzhiyun 	return ndev->peers[pidx].port;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun /*
589*4882a593Smuzhiyun  * idt_ntb_peer_port_idx() - get peer port index by given port number
590*4882a593Smuzhiyun  * @ntb:	NTB device context.
591*4882a593Smuzhiyun  * @port:	Peer port number.
592*4882a593Smuzhiyun  *
593*4882a593Smuzhiyun  * Internal port -> index table is pre-initialized with -EINVAL values,
594*4882a593Smuzhiyun  * so we just need to return it value
595*4882a593Smuzhiyun  *
596*4882a593Smuzhiyun  * Return: peer NT-function port index or negative error
597*4882a593Smuzhiyun  */
idt_ntb_peer_port_idx(struct ntb_dev * ntb,int port)598*4882a593Smuzhiyun static int idt_ntb_peer_port_idx(struct ntb_dev *ntb, int port)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	if (port < 0 || IDT_MAX_NR_PORTS <= port)
603*4882a593Smuzhiyun 		return -EINVAL;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	return ndev->port_idx_map[port];
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun /*===========================================================================
609*4882a593Smuzhiyun  *                         3. Link status operations
610*4882a593Smuzhiyun  *    There is no any ready-to-use method to have peer ports notified if NTB
611*4882a593Smuzhiyun  * link is set up or got down. Instead global signal can be used instead.
612*4882a593Smuzhiyun  * In case if any one of ports changes local NTB link state, it sends
613*4882a593Smuzhiyun  * global signal and clears corresponding global state bit. Then all the ports
614*4882a593Smuzhiyun  * receive a notification of that, so to make client driver being aware of
615*4882a593Smuzhiyun  * possible NTB link change.
616*4882a593Smuzhiyun  *    Additionally each of active NT-functions is subscribed to PCIe-link
617*4882a593Smuzhiyun  * state changes of peer ports.
618*4882a593Smuzhiyun  *===========================================================================
619*4882a593Smuzhiyun  */
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun static void idt_ntb_local_link_disable(struct idt_ntb_dev *ndev);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun /*
624*4882a593Smuzhiyun  * idt_init_link() - Initialize NTB link state notification subsystem
625*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
626*4882a593Smuzhiyun  *
627*4882a593Smuzhiyun  * Function performs the basic initialization of some global registers
628*4882a593Smuzhiyun  * needed to enable IRQ-based notifications of PCIe Link Up/Down and
629*4882a593Smuzhiyun  * Global Signal events.
630*4882a593Smuzhiyun  * NOTE Since it's not possible to determine when all the NTB peer drivers are
631*4882a593Smuzhiyun  * unloaded as well as have those registers accessed concurrently, we must
632*4882a593Smuzhiyun  * preinitialize them with the same value and leave it uncleared on local
633*4882a593Smuzhiyun  * driver unload.
634*4882a593Smuzhiyun  */
idt_init_link(struct idt_ntb_dev * ndev)635*4882a593Smuzhiyun static void idt_init_link(struct idt_ntb_dev *ndev)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	u32 part_mask, port_mask, se_mask;
638*4882a593Smuzhiyun 	unsigned char pidx;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	/* Initialize spin locker of Mapping Table access registers */
641*4882a593Smuzhiyun 	spin_lock_init(&ndev->mtbl_lock);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	/* Walk over all detected peers collecting port and partition masks */
644*4882a593Smuzhiyun 	port_mask = ~BIT(ndev->port);
645*4882a593Smuzhiyun 	part_mask = ~BIT(ndev->part);
646*4882a593Smuzhiyun 	for (pidx = 0; pidx < ndev->peer_cnt; pidx++) {
647*4882a593Smuzhiyun 		port_mask &= ~BIT(ndev->peers[pidx].port);
648*4882a593Smuzhiyun 		part_mask &= ~BIT(ndev->peers[pidx].part);
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/* Clean the Link Up/Down and GLobal Signal status registers */
652*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKUPSTS, (u32)-1);
653*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKDNSTS, (u32)-1);
654*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEGSIGSTS, (u32)-1);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	/* Unmask NT-activated partitions to receive Global Switch events */
657*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEPMSK, part_mask);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	/* Enable PCIe Link Up events of NT-activated ports */
660*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKUPMSK, port_mask);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	/* Enable PCIe Link Down events of NT-activated ports */
663*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKDNMSK, port_mask);
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	/* Unmask NT-activated partitions to receive Global Signal events */
666*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEGSIGMSK, part_mask);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	/* Unmask Link Up/Down and Global Switch Events */
669*4882a593Smuzhiyun 	se_mask = ~(IDT_SEMSK_LINKUP | IDT_SEMSK_LINKDN | IDT_SEMSK_GSIGNAL);
670*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEMSK, se_mask);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB link status events initialized");
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun /*
676*4882a593Smuzhiyun  * idt_deinit_link() - deinitialize link subsystem
677*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  * Just disable the link back.
680*4882a593Smuzhiyun  */
idt_deinit_link(struct idt_ntb_dev * ndev)681*4882a593Smuzhiyun static void idt_deinit_link(struct idt_ntb_dev *ndev)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun 	/* Disable the link */
684*4882a593Smuzhiyun 	idt_ntb_local_link_disable(ndev);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB link status events deinitialized");
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun /*
690*4882a593Smuzhiyun  * idt_se_isr() - switch events ISR
691*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
692*4882a593Smuzhiyun  * @ntint_sts:	NT-function interrupt status
693*4882a593Smuzhiyun  *
694*4882a593Smuzhiyun  * This driver doesn't support IDT PCIe-switch dynamic reconfigurations,
695*4882a593Smuzhiyun  * Failover capability, etc, so switch events are utilized to notify of
696*4882a593Smuzhiyun  * PCIe and NTB link events.
697*4882a593Smuzhiyun  * The method is called from PCIe ISR bottom-half routine.
698*4882a593Smuzhiyun  */
idt_se_isr(struct idt_ntb_dev * ndev,u32 ntint_sts)699*4882a593Smuzhiyun static void idt_se_isr(struct idt_ntb_dev *ndev, u32 ntint_sts)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	u32 sests;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	/* Read Switch Events status */
704*4882a593Smuzhiyun 	sests = idt_sw_read(ndev, IDT_SW_SESTS);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	/* Clean the Link Up/Down and Global Signal status registers */
707*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKUPSTS, (u32)-1);
708*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SELINKDNSTS, (u32)-1);
709*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEGSIGSTS, (u32)-1);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	/* Clean the corresponding interrupt bit */
712*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTINTSTS, IDT_NTINTSTS_SEVENT);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "SE IRQ detected %#08x (SESTS %#08x)",
715*4882a593Smuzhiyun 			  ntint_sts, sests);
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	/* Notify the client driver of possible link state change */
718*4882a593Smuzhiyun 	ntb_link_event(&ndev->ntb);
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun /*
722*4882a593Smuzhiyun  * idt_ntb_local_link_enable() - enable the local NTB link.
723*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
724*4882a593Smuzhiyun  *
725*4882a593Smuzhiyun  * In order to enable the NTB link we need:
726*4882a593Smuzhiyun  * - enable Completion TLPs translation
727*4882a593Smuzhiyun  * - initialize mapping table to enable the Request ID translation
728*4882a593Smuzhiyun  * - notify peers of NTB link state change
729*4882a593Smuzhiyun  */
idt_ntb_local_link_enable(struct idt_ntb_dev * ndev)730*4882a593Smuzhiyun static void idt_ntb_local_link_enable(struct idt_ntb_dev *ndev)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	u32 reqid, mtbldata = 0;
733*4882a593Smuzhiyun 	unsigned long irqflags;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	/* Enable the ID protection and Completion TLPs translation */
736*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTCTL, IDT_NTCTL_CPEN);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	/* Retrieve the current Requester ID (Bus:Device:Function) */
739*4882a593Smuzhiyun 	reqid = idt_nt_read(ndev, IDT_NT_REQIDCAP);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/*
742*4882a593Smuzhiyun 	 * Set the corresponding NT Mapping table entry of port partition index
743*4882a593Smuzhiyun 	 * with the data to perform the Request ID translation
744*4882a593Smuzhiyun 	 */
745*4882a593Smuzhiyun 	mtbldata = SET_FIELD(NTMTBLDATA_REQID, 0, reqid) |
746*4882a593Smuzhiyun 		   SET_FIELD(NTMTBLDATA_PART, 0, ndev->part) |
747*4882a593Smuzhiyun 		   IDT_NTMTBLDATA_VALID;
748*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->mtbl_lock, irqflags);
749*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLADDR, ndev->part);
750*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLDATA, mtbldata);
751*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->mtbl_lock, irqflags);
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	/* Notify the peers by setting and clearing the global signal bit */
754*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTGSIGNAL, IDT_NTGSIGNAL_SET);
755*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEGSIGSTS, (u32)1 << ndev->part);
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun /*
759*4882a593Smuzhiyun  * idt_ntb_local_link_disable() - disable the local NTB link.
760*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
761*4882a593Smuzhiyun  *
762*4882a593Smuzhiyun  * In order to enable the NTB link we need:
763*4882a593Smuzhiyun  * - disable Completion TLPs translation
764*4882a593Smuzhiyun  * - clear corresponding mapping table entry
765*4882a593Smuzhiyun  * - notify peers of NTB link state change
766*4882a593Smuzhiyun  */
idt_ntb_local_link_disable(struct idt_ntb_dev * ndev)767*4882a593Smuzhiyun static void idt_ntb_local_link_disable(struct idt_ntb_dev *ndev)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	unsigned long irqflags;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	/* Disable Completion TLPs translation */
772*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTCTL, 0);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* Clear the corresponding NT Mapping table entry */
775*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->mtbl_lock, irqflags);
776*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLADDR, ndev->part);
777*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLDATA, 0);
778*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->mtbl_lock, irqflags);
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	/* Notify the peers by setting and clearing the global signal bit */
781*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTGSIGNAL, IDT_NTGSIGNAL_SET);
782*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_SEGSIGSTS, (u32)1 << ndev->part);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun /*
786*4882a593Smuzhiyun  * idt_ntb_local_link_is_up() - test wethter local NTB link is up
787*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
788*4882a593Smuzhiyun  *
789*4882a593Smuzhiyun  * Local link is up under the following conditions:
790*4882a593Smuzhiyun  * - Bus mastering is enabled
791*4882a593Smuzhiyun  * - NTCTL has Completion TLPs translation enabled
792*4882a593Smuzhiyun  * - Mapping table permits Request TLPs translation
793*4882a593Smuzhiyun  * NOTE: We don't need to check PCIe link state since it's obviously
794*4882a593Smuzhiyun  * up while we are able to communicate with IDT PCIe-switch
795*4882a593Smuzhiyun  *
796*4882a593Smuzhiyun  * Return: true if link is up, otherwise false
797*4882a593Smuzhiyun  */
idt_ntb_local_link_is_up(struct idt_ntb_dev * ndev)798*4882a593Smuzhiyun static bool idt_ntb_local_link_is_up(struct idt_ntb_dev *ndev)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	unsigned long irqflags;
801*4882a593Smuzhiyun 	u32 data;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	/* Read the local Bus Master Enable status */
804*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_PCICMDSTS);
805*4882a593Smuzhiyun 	if (!(data & IDT_PCICMDSTS_BME))
806*4882a593Smuzhiyun 		return false;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/* Read the local Completion TLPs translation enable status */
809*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_NTCTL);
810*4882a593Smuzhiyun 	if (!(data & IDT_NTCTL_CPEN))
811*4882a593Smuzhiyun 		return false;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	/* Read Mapping table entry corresponding to the local partition */
814*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->mtbl_lock, irqflags);
815*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLADDR, ndev->part);
816*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_NTMTBLDATA);
817*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->mtbl_lock, irqflags);
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	return !!(data & IDT_NTMTBLDATA_VALID);
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun /*
823*4882a593Smuzhiyun  * idt_ntb_peer_link_is_up() - test whether peer NTB link is up
824*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
825*4882a593Smuzhiyun  * @pidx:	Peer port index
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * Peer link is up under the following conditions:
828*4882a593Smuzhiyun  * - PCIe link is up
829*4882a593Smuzhiyun  * - Bus mastering is enabled
830*4882a593Smuzhiyun  * - NTCTL has Completion TLPs translation enabled
831*4882a593Smuzhiyun  * - Mapping table permits Request TLPs translation
832*4882a593Smuzhiyun  *
833*4882a593Smuzhiyun  * Return: true if link is up, otherwise false
834*4882a593Smuzhiyun  */
idt_ntb_peer_link_is_up(struct idt_ntb_dev * ndev,int pidx)835*4882a593Smuzhiyun static bool idt_ntb_peer_link_is_up(struct idt_ntb_dev *ndev, int pidx)
836*4882a593Smuzhiyun {
837*4882a593Smuzhiyun 	unsigned long irqflags;
838*4882a593Smuzhiyun 	unsigned char port;
839*4882a593Smuzhiyun 	u32 data;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/* Retrieve the device port number */
842*4882a593Smuzhiyun 	port = ndev->peers[pidx].port;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	/* Check whether PCIe link is up */
845*4882a593Smuzhiyun 	data = idt_sw_read(ndev, portdata_tbl[port].sts);
846*4882a593Smuzhiyun 	if (!(data & IDT_SWPORTxSTS_LINKUP))
847*4882a593Smuzhiyun 		return false;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	/* Check whether bus mastering is enabled on the peer port */
850*4882a593Smuzhiyun 	data = idt_sw_read(ndev, portdata_tbl[port].pcicmdsts);
851*4882a593Smuzhiyun 	if (!(data & IDT_PCICMDSTS_BME))
852*4882a593Smuzhiyun 		return false;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	/* Check if Completion TLPs translation is enabled on the peer port */
855*4882a593Smuzhiyun 	data = idt_sw_read(ndev, portdata_tbl[port].ntctl);
856*4882a593Smuzhiyun 	if (!(data & IDT_NTCTL_CPEN))
857*4882a593Smuzhiyun 		return false;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	/* Read Mapping table entry corresponding to the peer partition */
860*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->mtbl_lock, irqflags);
861*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTMTBLADDR, ndev->peers[pidx].part);
862*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_NTMTBLDATA);
863*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->mtbl_lock, irqflags);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	return !!(data & IDT_NTMTBLDATA_VALID);
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun /*
869*4882a593Smuzhiyun  * idt_ntb_link_is_up() - get the current ntb link state (NTB API callback)
870*4882a593Smuzhiyun  * @ntb:	NTB device context.
871*4882a593Smuzhiyun  * @speed:	OUT - The link speed expressed as PCIe generation number.
872*4882a593Smuzhiyun  * @width:	OUT - The link width expressed as the number of PCIe lanes.
873*4882a593Smuzhiyun  *
874*4882a593Smuzhiyun  * Get the bitfield of NTB link states for all peer ports
875*4882a593Smuzhiyun  *
876*4882a593Smuzhiyun  * Return: bitfield of indexed ports link state: bit is set/cleared if the
877*4882a593Smuzhiyun  *         link is up/down respectively.
878*4882a593Smuzhiyun  */
idt_ntb_link_is_up(struct ntb_dev * ntb,enum ntb_speed * speed,enum ntb_width * width)879*4882a593Smuzhiyun static u64 idt_ntb_link_is_up(struct ntb_dev *ntb,
880*4882a593Smuzhiyun 			      enum ntb_speed *speed, enum ntb_width *width)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
883*4882a593Smuzhiyun 	unsigned char pidx;
884*4882a593Smuzhiyun 	u64 status;
885*4882a593Smuzhiyun 	u32 data;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	/* Retrieve the local link speed and width */
888*4882a593Smuzhiyun 	if (speed != NULL || width != NULL) {
889*4882a593Smuzhiyun 		data = idt_nt_read(ndev, IDT_NT_PCIELCTLSTS);
890*4882a593Smuzhiyun 		if (speed != NULL)
891*4882a593Smuzhiyun 			*speed = GET_FIELD(PCIELCTLSTS_CLS, data);
892*4882a593Smuzhiyun 		if (width != NULL)
893*4882a593Smuzhiyun 			*width = GET_FIELD(PCIELCTLSTS_NLW, data);
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	/* If local NTB link isn't up then all the links are considered down */
897*4882a593Smuzhiyun 	if (!idt_ntb_local_link_is_up(ndev))
898*4882a593Smuzhiyun 		return 0;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	/* Collect all the peer ports link states into the bitfield */
901*4882a593Smuzhiyun 	status = 0;
902*4882a593Smuzhiyun 	for (pidx = 0; pidx < ndev->peer_cnt; pidx++) {
903*4882a593Smuzhiyun 		if (idt_ntb_peer_link_is_up(ndev, pidx))
904*4882a593Smuzhiyun 			status |= ((u64)1 << pidx);
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	return status;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun /*
911*4882a593Smuzhiyun  * idt_ntb_link_enable() - enable local port ntb link (NTB API callback)
912*4882a593Smuzhiyun  * @ntb:	NTB device context.
913*4882a593Smuzhiyun  * @max_speed:	The maximum link speed expressed as PCIe generation number.
914*4882a593Smuzhiyun  * @max_width:	The maximum link width expressed as the number of PCIe lanes.
915*4882a593Smuzhiyun  *
916*4882a593Smuzhiyun  * Enable just local NTB link. PCIe link parameters are ignored.
917*4882a593Smuzhiyun  *
918*4882a593Smuzhiyun  * Return: always zero.
919*4882a593Smuzhiyun  */
idt_ntb_link_enable(struct ntb_dev * ntb,enum ntb_speed speed,enum ntb_width width)920*4882a593Smuzhiyun static int idt_ntb_link_enable(struct ntb_dev *ntb, enum ntb_speed speed,
921*4882a593Smuzhiyun 			       enum ntb_width width)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	/* Just enable the local NTB link */
926*4882a593Smuzhiyun 	idt_ntb_local_link_enable(ndev);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Local NTB link enabled");
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	return 0;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /*
934*4882a593Smuzhiyun  * idt_ntb_link_disable() - disable local port ntb link (NTB API callback)
935*4882a593Smuzhiyun  * @ntb:	NTB device context.
936*4882a593Smuzhiyun  *
937*4882a593Smuzhiyun  * Disable just local NTB link.
938*4882a593Smuzhiyun  *
939*4882a593Smuzhiyun  * Return: always zero.
940*4882a593Smuzhiyun  */
idt_ntb_link_disable(struct ntb_dev * ntb)941*4882a593Smuzhiyun static int idt_ntb_link_disable(struct ntb_dev *ntb)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* Just disable the local NTB link */
946*4882a593Smuzhiyun 	idt_ntb_local_link_disable(ndev);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Local NTB link disabled");
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	return 0;
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun /*=============================================================================
954*4882a593Smuzhiyun  *                         4. Memory Window operations
955*4882a593Smuzhiyun  *
956*4882a593Smuzhiyun  *    IDT PCIe-switches have two types of memory windows: MWs with direct
957*4882a593Smuzhiyun  * address translation and MWs with LUT based translation. The first type of
958*4882a593Smuzhiyun  * MWs is simple map of corresponding BAR address space to a memory space
959*4882a593Smuzhiyun  * of specified target port. So it implemets just ont-to-one mapping. Lookup
960*4882a593Smuzhiyun  * table in its turn can map one BAR address space to up to 24 different
961*4882a593Smuzhiyun  * memory spaces of different ports.
962*4882a593Smuzhiyun  *    NT-functions BARs can be turned on to implement either direct or lookup
963*4882a593Smuzhiyun  * table based address translations, so:
964*4882a593Smuzhiyun  * BAR0 - NT configuration registers space/direct address translation
965*4882a593Smuzhiyun  * BAR1 - direct address translation/upper address of BAR0x64
966*4882a593Smuzhiyun  * BAR2 - direct address translation/Lookup table with either 12 or 24 entries
967*4882a593Smuzhiyun  * BAR3 - direct address translation/upper address of BAR2x64
968*4882a593Smuzhiyun  * BAR4 - direct address translation/Lookup table with either 12 or 24 entries
969*4882a593Smuzhiyun  * BAR5 - direct address translation/upper address of BAR4x64
970*4882a593Smuzhiyun  *    Additionally BAR2 and BAR4 can't have 24-entries LUT enabled at the same
971*4882a593Smuzhiyun  * time. Since the BARs setup can be rather complicated this driver implements
972*4882a593Smuzhiyun  * a scanning algorithm to have all the possible memory windows configuration
973*4882a593Smuzhiyun  * covered.
974*4882a593Smuzhiyun  *
975*4882a593Smuzhiyun  * NOTE 1 BAR setup must be done before Linux kernel enumerated NT-function
976*4882a593Smuzhiyun  * of any port, so this driver would have memory windows configurations fixed.
977*4882a593Smuzhiyun  * In this way all initializations must be performed either by platform BIOS
978*4882a593Smuzhiyun  * or using EEPROM connected to IDT PCIe-switch master SMBus.
979*4882a593Smuzhiyun  *
980*4882a593Smuzhiyun  * NOTE 2 This driver expects BAR0 mapping NT-function configuration space.
981*4882a593Smuzhiyun  * Easy calculation can give us an upper boundary of 29 possible memory windows
982*4882a593Smuzhiyun  * per each NT-function if all the BARs are of 32bit type.
983*4882a593Smuzhiyun  *=============================================================================
984*4882a593Smuzhiyun  */
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun /*
987*4882a593Smuzhiyun  * idt_get_mw_count() - get memory window count
988*4882a593Smuzhiyun  * @mw_type:	Memory window type
989*4882a593Smuzhiyun  *
990*4882a593Smuzhiyun  * Return: number of memory windows with respect to the BAR type
991*4882a593Smuzhiyun  */
idt_get_mw_count(enum idt_mw_type mw_type)992*4882a593Smuzhiyun static inline unsigned char idt_get_mw_count(enum idt_mw_type mw_type)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun 	switch (mw_type) {
995*4882a593Smuzhiyun 	case IDT_MW_DIR:
996*4882a593Smuzhiyun 		return 1;
997*4882a593Smuzhiyun 	case IDT_MW_LUT12:
998*4882a593Smuzhiyun 		return 12;
999*4882a593Smuzhiyun 	case IDT_MW_LUT24:
1000*4882a593Smuzhiyun 		return 24;
1001*4882a593Smuzhiyun 	default:
1002*4882a593Smuzhiyun 		break;
1003*4882a593Smuzhiyun 	}
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	return 0;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun /*
1009*4882a593Smuzhiyun  * idt_get_mw_name() - get memory window name
1010*4882a593Smuzhiyun  * @mw_type:	Memory window type
1011*4882a593Smuzhiyun  *
1012*4882a593Smuzhiyun  * Return: pointer to a string with name
1013*4882a593Smuzhiyun  */
idt_get_mw_name(enum idt_mw_type mw_type)1014*4882a593Smuzhiyun static inline char *idt_get_mw_name(enum idt_mw_type mw_type)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	switch (mw_type) {
1017*4882a593Smuzhiyun 	case IDT_MW_DIR:
1018*4882a593Smuzhiyun 		return "DIR  ";
1019*4882a593Smuzhiyun 	case IDT_MW_LUT12:
1020*4882a593Smuzhiyun 		return "LUT12";
1021*4882a593Smuzhiyun 	case IDT_MW_LUT24:
1022*4882a593Smuzhiyun 		return "LUT24";
1023*4882a593Smuzhiyun 	default:
1024*4882a593Smuzhiyun 		break;
1025*4882a593Smuzhiyun 	}
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	return "unknown";
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun /*
1031*4882a593Smuzhiyun  * idt_scan_mws() - scan memory windows of the port
1032*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
1033*4882a593Smuzhiyun  * @port:	Port to get number of memory windows for
1034*4882a593Smuzhiyun  * @mw_cnt:	Out - number of memory windows
1035*4882a593Smuzhiyun  *
1036*4882a593Smuzhiyun  * It walks over BAR setup registers of the specified port and determines
1037*4882a593Smuzhiyun  * the memory windows parameters if any activated.
1038*4882a593Smuzhiyun  *
1039*4882a593Smuzhiyun  * Return: array of memory windows
1040*4882a593Smuzhiyun  */
idt_scan_mws(struct idt_ntb_dev * ndev,int port,unsigned char * mw_cnt)1041*4882a593Smuzhiyun static struct idt_mw_cfg *idt_scan_mws(struct idt_ntb_dev *ndev, int port,
1042*4882a593Smuzhiyun 				       unsigned char *mw_cnt)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	struct idt_mw_cfg mws[IDT_MAX_NR_MWS], *ret_mws;
1045*4882a593Smuzhiyun 	const struct idt_ntb_bar *bars;
1046*4882a593Smuzhiyun 	enum idt_mw_type mw_type;
1047*4882a593Smuzhiyun 	unsigned char widx, bidx, en_cnt;
1048*4882a593Smuzhiyun 	bool bar_64bit = false;
1049*4882a593Smuzhiyun 	int aprt_size;
1050*4882a593Smuzhiyun 	u32 data;
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	/* Retrieve the array of the BARs registers */
1053*4882a593Smuzhiyun 	bars = portdata_tbl[port].bars;
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	/* Scan all the BARs belonging to the port */
1056*4882a593Smuzhiyun 	*mw_cnt = 0;
1057*4882a593Smuzhiyun 	for (bidx = 0; bidx < IDT_BAR_CNT; bidx += 1 + bar_64bit) {
1058*4882a593Smuzhiyun 		/* Read BARSETUP register value */
1059*4882a593Smuzhiyun 		data = idt_sw_read(ndev, bars[bidx].setup);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 		/* Skip disabled BARs */
1062*4882a593Smuzhiyun 		if (!(data & IDT_BARSETUP_EN)) {
1063*4882a593Smuzhiyun 			bar_64bit = false;
1064*4882a593Smuzhiyun 			continue;
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 		/* Skip next BARSETUP if current one has 64bit addressing */
1068*4882a593Smuzhiyun 		bar_64bit = IS_FLD_SET(BARSETUP_TYPE, data, 64);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 		/* Skip configuration space mapping BARs */
1071*4882a593Smuzhiyun 		if (data & IDT_BARSETUP_MODE_CFG)
1072*4882a593Smuzhiyun 			continue;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 		/* Retrieve MW type/entries count and aperture size */
1075*4882a593Smuzhiyun 		mw_type = GET_FIELD(BARSETUP_ATRAN, data);
1076*4882a593Smuzhiyun 		en_cnt = idt_get_mw_count(mw_type);
1077*4882a593Smuzhiyun 		aprt_size = (u64)1 << GET_FIELD(BARSETUP_SIZE, data);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		/* Save configurations of all available memory windows */
1080*4882a593Smuzhiyun 		for (widx = 0; widx < en_cnt; widx++, (*mw_cnt)++) {
1081*4882a593Smuzhiyun 			/*
1082*4882a593Smuzhiyun 			 * IDT can expose a limited number of MWs, so it's bug
1083*4882a593Smuzhiyun 			 * to have more than the driver expects
1084*4882a593Smuzhiyun 			 */
1085*4882a593Smuzhiyun 			if (*mw_cnt >= IDT_MAX_NR_MWS)
1086*4882a593Smuzhiyun 				return ERR_PTR(-EINVAL);
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 			/* Save basic MW info */
1089*4882a593Smuzhiyun 			mws[*mw_cnt].type = mw_type;
1090*4882a593Smuzhiyun 			mws[*mw_cnt].bar = bidx;
1091*4882a593Smuzhiyun 			mws[*mw_cnt].idx = widx;
1092*4882a593Smuzhiyun 			/* It's always DWORD aligned */
1093*4882a593Smuzhiyun 			mws[*mw_cnt].addr_align = IDT_TRANS_ALIGN;
1094*4882a593Smuzhiyun 			/* DIR and LUT approachs differently configure MWs */
1095*4882a593Smuzhiyun 			if (mw_type == IDT_MW_DIR)
1096*4882a593Smuzhiyun 				mws[*mw_cnt].size_max = aprt_size;
1097*4882a593Smuzhiyun 			else if (mw_type == IDT_MW_LUT12)
1098*4882a593Smuzhiyun 				mws[*mw_cnt].size_max = aprt_size / 16;
1099*4882a593Smuzhiyun 			else
1100*4882a593Smuzhiyun 				mws[*mw_cnt].size_max = aprt_size / 32;
1101*4882a593Smuzhiyun 			mws[*mw_cnt].size_align = (mw_type == IDT_MW_DIR) ?
1102*4882a593Smuzhiyun 				IDT_DIR_SIZE_ALIGN : mws[*mw_cnt].size_max;
1103*4882a593Smuzhiyun 		}
1104*4882a593Smuzhiyun 	}
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	/* Allocate memory for memory window descriptors */
1107*4882a593Smuzhiyun 	ret_mws = devm_kcalloc(&ndev->ntb.pdev->dev, *mw_cnt, sizeof(*ret_mws),
1108*4882a593Smuzhiyun 			       GFP_KERNEL);
1109*4882a593Smuzhiyun 	if (!ret_mws)
1110*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	/* Copy the info of detected memory windows */
1113*4882a593Smuzhiyun 	memcpy(ret_mws, mws, (*mw_cnt)*sizeof(*ret_mws));
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	return ret_mws;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun /*
1119*4882a593Smuzhiyun  * idt_init_mws() - initialize memory windows subsystem
1120*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
1121*4882a593Smuzhiyun  *
1122*4882a593Smuzhiyun  * Scan BAR setup registers of local and peer ports to determine the
1123*4882a593Smuzhiyun  * outbound and inbound memory windows parameters
1124*4882a593Smuzhiyun  *
1125*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number
1126*4882a593Smuzhiyun  */
idt_init_mws(struct idt_ntb_dev * ndev)1127*4882a593Smuzhiyun static int idt_init_mws(struct idt_ntb_dev *ndev)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	struct idt_ntb_peer *peer;
1130*4882a593Smuzhiyun 	unsigned char pidx;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	/* Scan memory windows of the local port */
1133*4882a593Smuzhiyun 	ndev->mws = idt_scan_mws(ndev, ndev->port, &ndev->mw_cnt);
1134*4882a593Smuzhiyun 	if (IS_ERR(ndev->mws)) {
1135*4882a593Smuzhiyun 		dev_err(&ndev->ntb.pdev->dev,
1136*4882a593Smuzhiyun 			"Failed to scan mws of local port %hhu", ndev->port);
1137*4882a593Smuzhiyun 		return PTR_ERR(ndev->mws);
1138*4882a593Smuzhiyun 	}
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	/* Scan memory windows of the peer ports */
1141*4882a593Smuzhiyun 	for (pidx = 0; pidx < ndev->peer_cnt; pidx++) {
1142*4882a593Smuzhiyun 		peer = &ndev->peers[pidx];
1143*4882a593Smuzhiyun 		peer->mws = idt_scan_mws(ndev, peer->port, &peer->mw_cnt);
1144*4882a593Smuzhiyun 		if (IS_ERR(peer->mws)) {
1145*4882a593Smuzhiyun 			dev_err(&ndev->ntb.pdev->dev,
1146*4882a593Smuzhiyun 				"Failed to scan mws of port %hhu", peer->port);
1147*4882a593Smuzhiyun 			return PTR_ERR(peer->mws);
1148*4882a593Smuzhiyun 		}
1149*4882a593Smuzhiyun 	}
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	/* Initialize spin locker of the LUT registers */
1152*4882a593Smuzhiyun 	spin_lock_init(&ndev->lut_lock);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Outbound and inbound MWs initialized");
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	return 0;
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun /*
1160*4882a593Smuzhiyun  * idt_ntb_mw_count() - number of inbound memory windows (NTB API callback)
1161*4882a593Smuzhiyun  * @ntb:	NTB device context.
1162*4882a593Smuzhiyun  * @pidx:	Port index of peer device.
1163*4882a593Smuzhiyun  *
1164*4882a593Smuzhiyun  * The value is returned for the specified peer, so generally speaking it can
1165*4882a593Smuzhiyun  * be different for different port depending on the IDT PCIe-switch
1166*4882a593Smuzhiyun  * initialization.
1167*4882a593Smuzhiyun  *
1168*4882a593Smuzhiyun  * Return: the number of memory windows.
1169*4882a593Smuzhiyun  */
idt_ntb_mw_count(struct ntb_dev * ntb,int pidx)1170*4882a593Smuzhiyun static int idt_ntb_mw_count(struct ntb_dev *ntb, int pidx)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
1175*4882a593Smuzhiyun 		return -EINVAL;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	return ndev->peers[pidx].mw_cnt;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun /*
1181*4882a593Smuzhiyun  * idt_ntb_mw_get_align() - inbound memory window parameters (NTB API callback)
1182*4882a593Smuzhiyun  * @ntb:	NTB device context.
1183*4882a593Smuzhiyun  * @pidx:	Port index of peer device.
1184*4882a593Smuzhiyun  * @widx:	Memory window index.
1185*4882a593Smuzhiyun  * @addr_align:	OUT - the base alignment for translating the memory window
1186*4882a593Smuzhiyun  * @size_align:	OUT - the size alignment for translating the memory window
1187*4882a593Smuzhiyun  * @size_max:	OUT - the maximum size of the memory window
1188*4882a593Smuzhiyun  *
1189*4882a593Smuzhiyun  * The peer memory window parameters have already been determined, so just
1190*4882a593Smuzhiyun  * return the corresponding values, which mustn't change within session.
1191*4882a593Smuzhiyun  *
1192*4882a593Smuzhiyun  * Return: Zero on success, otherwise a negative error number.
1193*4882a593Smuzhiyun  */
idt_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)1194*4882a593Smuzhiyun static int idt_ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
1195*4882a593Smuzhiyun 				resource_size_t *addr_align,
1196*4882a593Smuzhiyun 				resource_size_t *size_align,
1197*4882a593Smuzhiyun 				resource_size_t *size_max)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1200*4882a593Smuzhiyun 	struct idt_ntb_peer *peer;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
1203*4882a593Smuzhiyun 		return -EINVAL;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	peer = &ndev->peers[pidx];
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	if (widx < 0 || peer->mw_cnt <= widx)
1208*4882a593Smuzhiyun 		return -EINVAL;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	if (addr_align != NULL)
1211*4882a593Smuzhiyun 		*addr_align = peer->mws[widx].addr_align;
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 	if (size_align != NULL)
1214*4882a593Smuzhiyun 		*size_align = peer->mws[widx].size_align;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	if (size_max != NULL)
1217*4882a593Smuzhiyun 		*size_max = peer->mws[widx].size_max;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	return 0;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun /*
1223*4882a593Smuzhiyun  * idt_ntb_peer_mw_count() - number of outbound memory windows
1224*4882a593Smuzhiyun  *			     (NTB API callback)
1225*4882a593Smuzhiyun  * @ntb:	NTB device context.
1226*4882a593Smuzhiyun  *
1227*4882a593Smuzhiyun  * Outbound memory windows parameters have been determined based on the
1228*4882a593Smuzhiyun  * BAR setup registers value, which are mostly constants within one session.
1229*4882a593Smuzhiyun  *
1230*4882a593Smuzhiyun  * Return: the number of memory windows.
1231*4882a593Smuzhiyun  */
idt_ntb_peer_mw_count(struct ntb_dev * ntb)1232*4882a593Smuzhiyun static int idt_ntb_peer_mw_count(struct ntb_dev *ntb)
1233*4882a593Smuzhiyun {
1234*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	return ndev->mw_cnt;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun /*
1240*4882a593Smuzhiyun  * idt_ntb_peer_mw_get_addr() - get map address of an outbound memory window
1241*4882a593Smuzhiyun  *				(NTB API callback)
1242*4882a593Smuzhiyun  * @ntb:	NTB device context.
1243*4882a593Smuzhiyun  * @widx:	Memory window index (within ntb_peer_mw_count() return value).
1244*4882a593Smuzhiyun  * @base:	OUT - the base address of mapping region.
1245*4882a593Smuzhiyun  * @size:	OUT - the size of mapping region.
1246*4882a593Smuzhiyun  *
1247*4882a593Smuzhiyun  * Return just parameters of BAR resources mapping. Size reflects just the size
1248*4882a593Smuzhiyun  * of the resource
1249*4882a593Smuzhiyun  *
1250*4882a593Smuzhiyun  * Return: Zero on success, otherwise a negative error number.
1251*4882a593Smuzhiyun  */
idt_ntb_peer_mw_get_addr(struct ntb_dev * ntb,int widx,phys_addr_t * base,resource_size_t * size)1252*4882a593Smuzhiyun static int idt_ntb_peer_mw_get_addr(struct ntb_dev *ntb, int widx,
1253*4882a593Smuzhiyun 				    phys_addr_t *base, resource_size_t *size)
1254*4882a593Smuzhiyun {
1255*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	if (widx < 0 || ndev->mw_cnt <= widx)
1258*4882a593Smuzhiyun 		return -EINVAL;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	/* Mapping address is just properly shifted BAR resource start */
1261*4882a593Smuzhiyun 	if (base != NULL)
1262*4882a593Smuzhiyun 		*base = pci_resource_start(ntb->pdev, ndev->mws[widx].bar) +
1263*4882a593Smuzhiyun 			ndev->mws[widx].idx * ndev->mws[widx].size_max;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	/* Mapping size has already been calculated at MWs scanning */
1266*4882a593Smuzhiyun 	if (size != NULL)
1267*4882a593Smuzhiyun 		*size = ndev->mws[widx].size_max;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	return 0;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun /*
1273*4882a593Smuzhiyun  * idt_ntb_peer_mw_set_trans() - set a translation address of a memory window
1274*4882a593Smuzhiyun  *				 (NTB API callback)
1275*4882a593Smuzhiyun  * @ntb:	NTB device context.
1276*4882a593Smuzhiyun  * @pidx:	Port index of peer device the translation address received from.
1277*4882a593Smuzhiyun  * @widx:	Memory window index.
1278*4882a593Smuzhiyun  * @addr:	The dma address of the shared memory to access.
1279*4882a593Smuzhiyun  * @size:	The size of the shared memory to access.
1280*4882a593Smuzhiyun  *
1281*4882a593Smuzhiyun  * The Direct address translation and LUT base translation is initialized a
1282*4882a593Smuzhiyun  * bit differenet. Although the parameters restriction are now determined by
1283*4882a593Smuzhiyun  * the same code.
1284*4882a593Smuzhiyun  *
1285*4882a593Smuzhiyun  * Return: Zero on success, otherwise an error number.
1286*4882a593Smuzhiyun  */
idt_ntb_peer_mw_set_trans(struct ntb_dev * ntb,int pidx,int widx,u64 addr,resource_size_t size)1287*4882a593Smuzhiyun static int idt_ntb_peer_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
1288*4882a593Smuzhiyun 				     u64 addr, resource_size_t size)
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1291*4882a593Smuzhiyun 	struct idt_mw_cfg *mw_cfg;
1292*4882a593Smuzhiyun 	u32 data = 0, lutoff = 0;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
1295*4882a593Smuzhiyun 		return -EINVAL;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	if (widx < 0 || ndev->mw_cnt <= widx)
1298*4882a593Smuzhiyun 		return -EINVAL;
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	/*
1301*4882a593Smuzhiyun 	 * Retrieve the memory window config to make sure the passed arguments
1302*4882a593Smuzhiyun 	 * fit it restrictions
1303*4882a593Smuzhiyun 	 */
1304*4882a593Smuzhiyun 	mw_cfg = &ndev->mws[widx];
1305*4882a593Smuzhiyun 	if (!IS_ALIGNED(addr, mw_cfg->addr_align))
1306*4882a593Smuzhiyun 		return -EINVAL;
1307*4882a593Smuzhiyun 	if (!IS_ALIGNED(size, mw_cfg->size_align) || size > mw_cfg->size_max)
1308*4882a593Smuzhiyun 		return -EINVAL;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	/* DIR and LUT based translations are initialized differently */
1311*4882a593Smuzhiyun 	if (mw_cfg->type == IDT_MW_DIR) {
1312*4882a593Smuzhiyun 		const struct idt_ntb_bar *bar = &ntdata_tbl.bars[mw_cfg->bar];
1313*4882a593Smuzhiyun 		u64 limit;
1314*4882a593Smuzhiyun 		/* Set destination partition of translation */
1315*4882a593Smuzhiyun 		data = idt_nt_read(ndev, bar->setup);
1316*4882a593Smuzhiyun 		data = SET_FIELD(BARSETUP_TPART, data, ndev->peers[pidx].part);
1317*4882a593Smuzhiyun 		idt_nt_write(ndev, bar->setup, data);
1318*4882a593Smuzhiyun 		/* Set translation base address */
1319*4882a593Smuzhiyun 		idt_nt_write(ndev, bar->ltbase, (u32)addr);
1320*4882a593Smuzhiyun 		idt_nt_write(ndev, bar->utbase, (u32)(addr >> 32));
1321*4882a593Smuzhiyun 		/* Set the custom BAR aperture limit */
1322*4882a593Smuzhiyun 		limit = pci_bus_address(ntb->pdev, mw_cfg->bar) + size;
1323*4882a593Smuzhiyun 		idt_nt_write(ndev, bar->limit, (u32)limit);
1324*4882a593Smuzhiyun 		if (IS_FLD_SET(BARSETUP_TYPE, data, 64))
1325*4882a593Smuzhiyun 			idt_nt_write(ndev, (bar + 1)->limit, (limit >> 32));
1326*4882a593Smuzhiyun 	} else {
1327*4882a593Smuzhiyun 		unsigned long irqflags;
1328*4882a593Smuzhiyun 		/* Initialize corresponding LUT entry */
1329*4882a593Smuzhiyun 		lutoff = SET_FIELD(LUTOFFSET_INDEX, 0, mw_cfg->idx) |
1330*4882a593Smuzhiyun 			 SET_FIELD(LUTOFFSET_BAR, 0, mw_cfg->bar);
1331*4882a593Smuzhiyun 		data = SET_FIELD(LUTUDATA_PART, 0, ndev->peers[pidx].part) |
1332*4882a593Smuzhiyun 			IDT_LUTUDATA_VALID;
1333*4882a593Smuzhiyun 		spin_lock_irqsave(&ndev->lut_lock, irqflags);
1334*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTOFFSET, lutoff);
1335*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTLDATA, (u32)addr);
1336*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTMDATA, (u32)(addr >> 32));
1337*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTUDATA, data);
1338*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ndev->lut_lock, irqflags);
1339*4882a593Smuzhiyun 		/* Limit address isn't specified since size is fixed for LUT */
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	return 0;
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun /*
1346*4882a593Smuzhiyun  * idt_ntb_peer_mw_clear_trans() - clear the outbound MW translation address
1347*4882a593Smuzhiyun  *				   (NTB API callback)
1348*4882a593Smuzhiyun  * @ntb:	NTB device context.
1349*4882a593Smuzhiyun  * @pidx:	Port index of peer device.
1350*4882a593Smuzhiyun  * @widx:	Memory window index.
1351*4882a593Smuzhiyun  *
1352*4882a593Smuzhiyun  * It effectively disables the translation over the specified outbound MW.
1353*4882a593Smuzhiyun  *
1354*4882a593Smuzhiyun  * Return: Zero on success, otherwise an error number.
1355*4882a593Smuzhiyun  */
idt_ntb_peer_mw_clear_trans(struct ntb_dev * ntb,int pidx,int widx)1356*4882a593Smuzhiyun static int idt_ntb_peer_mw_clear_trans(struct ntb_dev *ntb, int pidx,
1357*4882a593Smuzhiyun 					int widx)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1360*4882a593Smuzhiyun 	struct idt_mw_cfg *mw_cfg;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
1363*4882a593Smuzhiyun 		return -EINVAL;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	if (widx < 0 || ndev->mw_cnt <= widx)
1366*4882a593Smuzhiyun 		return -EINVAL;
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	mw_cfg = &ndev->mws[widx];
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	/* DIR and LUT based translations are initialized differently */
1371*4882a593Smuzhiyun 	if (mw_cfg->type == IDT_MW_DIR) {
1372*4882a593Smuzhiyun 		const struct idt_ntb_bar *bar = &ntdata_tbl.bars[mw_cfg->bar];
1373*4882a593Smuzhiyun 		u32 data;
1374*4882a593Smuzhiyun 		/* Read BARSETUP to check BAR type */
1375*4882a593Smuzhiyun 		data = idt_nt_read(ndev, bar->setup);
1376*4882a593Smuzhiyun 		/* Disable translation by specifying zero BAR limit */
1377*4882a593Smuzhiyun 		idt_nt_write(ndev, bar->limit, 0);
1378*4882a593Smuzhiyun 		if (IS_FLD_SET(BARSETUP_TYPE, data, 64))
1379*4882a593Smuzhiyun 			idt_nt_write(ndev, (bar + 1)->limit, 0);
1380*4882a593Smuzhiyun 	} else {
1381*4882a593Smuzhiyun 		unsigned long irqflags;
1382*4882a593Smuzhiyun 		u32 lutoff;
1383*4882a593Smuzhiyun 		/* Clear the corresponding LUT entry up */
1384*4882a593Smuzhiyun 		lutoff = SET_FIELD(LUTOFFSET_INDEX, 0, mw_cfg->idx) |
1385*4882a593Smuzhiyun 			 SET_FIELD(LUTOFFSET_BAR, 0, mw_cfg->bar);
1386*4882a593Smuzhiyun 		spin_lock_irqsave(&ndev->lut_lock, irqflags);
1387*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTOFFSET, lutoff);
1388*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTLDATA, 0);
1389*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTMDATA, 0);
1390*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_LUTUDATA, 0);
1391*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ndev->lut_lock, irqflags);
1392*4882a593Smuzhiyun 	}
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	return 0;
1395*4882a593Smuzhiyun }
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun /*=============================================================================
1398*4882a593Smuzhiyun  *                          5. Doorbell operations
1399*4882a593Smuzhiyun  *
1400*4882a593Smuzhiyun  *    Doorbell functionality of IDT PCIe-switches is pretty unusual. First of
1401*4882a593Smuzhiyun  * all there is global doorbell register which state can be changed by any
1402*4882a593Smuzhiyun  * NT-function of the IDT device in accordance with global permissions. These
1403*4882a593Smuzhiyun  * permissions configs are not supported by NTB API, so it must be done by
1404*4882a593Smuzhiyun  * either BIOS or EEPROM settings. In the same way the state of the global
1405*4882a593Smuzhiyun  * doorbell is reflected to the NT-functions local inbound doorbell registers.
1406*4882a593Smuzhiyun  * It can lead to situations when client driver sets some peer doorbell bits
1407*4882a593Smuzhiyun  * and get them bounced back to local inbound doorbell if permissions are
1408*4882a593Smuzhiyun  * granted.
1409*4882a593Smuzhiyun  *    Secondly there is just one IRQ vector for Doorbell, Message, Temperature
1410*4882a593Smuzhiyun  * and Switch events, so if client driver left any of Doorbell bits set and
1411*4882a593Smuzhiyun  * some other event occurred, the driver will be notified of Doorbell event
1412*4882a593Smuzhiyun  * again.
1413*4882a593Smuzhiyun  *=============================================================================
1414*4882a593Smuzhiyun  */
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun /*
1417*4882a593Smuzhiyun  * idt_db_isr() - doorbell event ISR
1418*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
1419*4882a593Smuzhiyun  * @ntint_sts:	NT-function interrupt status
1420*4882a593Smuzhiyun  *
1421*4882a593Smuzhiyun  * Doorbell event happans when DBELL bit of NTINTSTS switches from 0 to 1.
1422*4882a593Smuzhiyun  * It happens only when unmasked doorbell bits are set to ones on completely
1423*4882a593Smuzhiyun  * zeroed doorbell register.
1424*4882a593Smuzhiyun  * The method is called from PCIe ISR bottom-half routine.
1425*4882a593Smuzhiyun  */
idt_db_isr(struct idt_ntb_dev * ndev,u32 ntint_sts)1426*4882a593Smuzhiyun static void idt_db_isr(struct idt_ntb_dev *ndev, u32 ntint_sts)
1427*4882a593Smuzhiyun {
1428*4882a593Smuzhiyun 	/*
1429*4882a593Smuzhiyun 	 * Doorbell IRQ status will be cleaned only when client
1430*4882a593Smuzhiyun 	 * driver unsets all the doorbell bits.
1431*4882a593Smuzhiyun 	 */
1432*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "DB IRQ detected %#08x", ntint_sts);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* Notify the client driver of possible doorbell state change */
1435*4882a593Smuzhiyun 	ntb_db_event(&ndev->ntb, 0);
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun /*
1439*4882a593Smuzhiyun  * idt_ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
1440*4882a593Smuzhiyun  *			     (NTB API callback)
1441*4882a593Smuzhiyun  * @ntb:	NTB device context.
1442*4882a593Smuzhiyun  *
1443*4882a593Smuzhiyun  * IDT PCIe-switches expose just one Doorbell register of DWORD size.
1444*4882a593Smuzhiyun  *
1445*4882a593Smuzhiyun  * Return: A mask of doorbell bits supported by the ntb.
1446*4882a593Smuzhiyun  */
idt_ntb_db_valid_mask(struct ntb_dev * ntb)1447*4882a593Smuzhiyun static u64 idt_ntb_db_valid_mask(struct ntb_dev *ntb)
1448*4882a593Smuzhiyun {
1449*4882a593Smuzhiyun 	return IDT_DBELL_MASK;
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun /*
1453*4882a593Smuzhiyun  * idt_ntb_db_read() - read the local doorbell register (NTB API callback)
1454*4882a593Smuzhiyun  * @ntb:	NTB device context.
1455*4882a593Smuzhiyun  *
1456*4882a593Smuzhiyun  * There is just on inbound doorbell register of each NT-function, so
1457*4882a593Smuzhiyun  * this method return it value.
1458*4882a593Smuzhiyun  *
1459*4882a593Smuzhiyun  * Return: The bits currently set in the local doorbell register.
1460*4882a593Smuzhiyun  */
idt_ntb_db_read(struct ntb_dev * ntb)1461*4882a593Smuzhiyun static u64 idt_ntb_db_read(struct ntb_dev *ntb)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	return idt_nt_read(ndev, IDT_NT_INDBELLSTS);
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun /*
1469*4882a593Smuzhiyun  * idt_ntb_db_clear() - clear bits in the local doorbell register
1470*4882a593Smuzhiyun  *			(NTB API callback)
1471*4882a593Smuzhiyun  * @ntb:	NTB device context.
1472*4882a593Smuzhiyun  * @db_bits:	Doorbell bits to clear.
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * Clear bits of inbound doorbell register by writing ones to it.
1475*4882a593Smuzhiyun  *
1476*4882a593Smuzhiyun  * NOTE! Invalid bits are always considered cleared so it's not an error
1477*4882a593Smuzhiyun  * to clear them over.
1478*4882a593Smuzhiyun  *
1479*4882a593Smuzhiyun  * Return: always zero as success.
1480*4882a593Smuzhiyun  */
idt_ntb_db_clear(struct ntb_dev * ntb,u64 db_bits)1481*4882a593Smuzhiyun static int idt_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_INDBELLSTS, (u32)db_bits);
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	return 0;
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun /*
1491*4882a593Smuzhiyun  * idt_ntb_db_read_mask() - read the local doorbell mask (NTB API callback)
1492*4882a593Smuzhiyun  * @ntb:	NTB device context.
1493*4882a593Smuzhiyun  *
1494*4882a593Smuzhiyun  * Each inbound doorbell bit can be masked from generating IRQ by setting
1495*4882a593Smuzhiyun  * the corresponding bit in inbound doorbell mask. So this method returns
1496*4882a593Smuzhiyun  * the value of the register.
1497*4882a593Smuzhiyun  *
1498*4882a593Smuzhiyun  * Return: The bits currently set in the local doorbell mask register.
1499*4882a593Smuzhiyun  */
idt_ntb_db_read_mask(struct ntb_dev * ntb)1500*4882a593Smuzhiyun static u64 idt_ntb_db_read_mask(struct ntb_dev *ntb)
1501*4882a593Smuzhiyun {
1502*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	return idt_nt_read(ndev, IDT_NT_INDBELLMSK);
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun /*
1508*4882a593Smuzhiyun  * idt_ntb_db_set_mask() - set bits in the local doorbell mask
1509*4882a593Smuzhiyun  *			   (NTB API callback)
1510*4882a593Smuzhiyun  * @ntb:	NTB device context.
1511*4882a593Smuzhiyun  * @db_bits:	Doorbell mask bits to set.
1512*4882a593Smuzhiyun  *
1513*4882a593Smuzhiyun  * The inbound doorbell register mask value must be read, then OR'ed with
1514*4882a593Smuzhiyun  * passed field and only then set back.
1515*4882a593Smuzhiyun  *
1516*4882a593Smuzhiyun  * Return: zero on success, negative error if invalid argument passed.
1517*4882a593Smuzhiyun  */
idt_ntb_db_set_mask(struct ntb_dev * ntb,u64 db_bits)1518*4882a593Smuzhiyun static int idt_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	return idt_reg_set_bits(ndev, IDT_NT_INDBELLMSK, &ndev->db_mask_lock,
1523*4882a593Smuzhiyun 				IDT_DBELL_MASK, db_bits);
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun /*
1527*4882a593Smuzhiyun  * idt_ntb_db_clear_mask() - clear bits in the local doorbell mask
1528*4882a593Smuzhiyun  *			     (NTB API callback)
1529*4882a593Smuzhiyun  * @ntb:	NTB device context.
1530*4882a593Smuzhiyun  * @db_bits:	Doorbell bits to clear.
1531*4882a593Smuzhiyun  *
1532*4882a593Smuzhiyun  * The method just clears the set bits up in accordance with the passed
1533*4882a593Smuzhiyun  * bitfield. IDT PCIe-switch shall generate an interrupt if there hasn't
1534*4882a593Smuzhiyun  * been any unmasked bit set before current unmasking. Otherwise IRQ won't
1535*4882a593Smuzhiyun  * be generated since there is only one IRQ vector for all doorbells.
1536*4882a593Smuzhiyun  *
1537*4882a593Smuzhiyun  * Return: always zero as success
1538*4882a593Smuzhiyun  */
idt_ntb_db_clear_mask(struct ntb_dev * ntb,u64 db_bits)1539*4882a593Smuzhiyun static int idt_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	idt_reg_clear_bits(ndev, IDT_NT_INDBELLMSK, &ndev->db_mask_lock,
1544*4882a593Smuzhiyun 			   db_bits);
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun 	return 0;
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun /*
1550*4882a593Smuzhiyun  * idt_ntb_peer_db_set() - set bits in the peer doorbell register
1551*4882a593Smuzhiyun  *			   (NTB API callback)
1552*4882a593Smuzhiyun  * @ntb:	NTB device context.
1553*4882a593Smuzhiyun  * @db_bits:	Doorbell bits to set.
1554*4882a593Smuzhiyun  *
1555*4882a593Smuzhiyun  * IDT PCIe-switches exposes local outbound doorbell register to change peer
1556*4882a593Smuzhiyun  * inbound doorbell register state.
1557*4882a593Smuzhiyun  *
1558*4882a593Smuzhiyun  * Return: zero on success, negative error if invalid argument passed.
1559*4882a593Smuzhiyun  */
idt_ntb_peer_db_set(struct ntb_dev * ntb,u64 db_bits)1560*4882a593Smuzhiyun static int idt_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
1561*4882a593Smuzhiyun {
1562*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	if (db_bits & ~(u64)IDT_DBELL_MASK)
1565*4882a593Smuzhiyun 		return -EINVAL;
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_OUTDBELLSET, (u32)db_bits);
1568*4882a593Smuzhiyun 	return 0;
1569*4882a593Smuzhiyun }
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun /*=============================================================================
1572*4882a593Smuzhiyun  *                          6. Messaging operations
1573*4882a593Smuzhiyun  *
1574*4882a593Smuzhiyun  *    Each NT-function of IDT PCIe-switch has four inbound and four outbound
1575*4882a593Smuzhiyun  * message registers. Each outbound message register can be connected to one or
1576*4882a593Smuzhiyun  * even more than one peer inbound message registers by setting global
1577*4882a593Smuzhiyun  * configurations. Since NTB API permits one-on-one message registers mapping
1578*4882a593Smuzhiyun  * only, the driver acts in according with that restriction.
1579*4882a593Smuzhiyun  *=============================================================================
1580*4882a593Smuzhiyun  */
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun /*
1583*4882a593Smuzhiyun  * idt_init_msg() - initialize messaging interface
1584*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
1585*4882a593Smuzhiyun  *
1586*4882a593Smuzhiyun  * Just initialize the message registers routing tables locker.
1587*4882a593Smuzhiyun  */
idt_init_msg(struct idt_ntb_dev * ndev)1588*4882a593Smuzhiyun static void idt_init_msg(struct idt_ntb_dev *ndev)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun 	unsigned char midx;
1591*4882a593Smuzhiyun 
1592*4882a593Smuzhiyun 	/* Init the messages routing table lockers */
1593*4882a593Smuzhiyun 	for (midx = 0; midx < IDT_MSG_CNT; midx++)
1594*4882a593Smuzhiyun 		spin_lock_init(&ndev->msg_locks[midx]);
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB Messaging initialized");
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun /*
1600*4882a593Smuzhiyun  * idt_msg_isr() - message event ISR
1601*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
1602*4882a593Smuzhiyun  * @ntint_sts:	NT-function interrupt status
1603*4882a593Smuzhiyun  *
1604*4882a593Smuzhiyun  * Message event happens when MSG bit of NTINTSTS switches from 0 to 1.
1605*4882a593Smuzhiyun  * It happens only when unmasked message status bits are set to ones on
1606*4882a593Smuzhiyun  * completely zeroed message status register.
1607*4882a593Smuzhiyun  * The method is called from PCIe ISR bottom-half routine.
1608*4882a593Smuzhiyun  */
idt_msg_isr(struct idt_ntb_dev * ndev,u32 ntint_sts)1609*4882a593Smuzhiyun static void idt_msg_isr(struct idt_ntb_dev *ndev, u32 ntint_sts)
1610*4882a593Smuzhiyun {
1611*4882a593Smuzhiyun 	/*
1612*4882a593Smuzhiyun 	 * Message IRQ status will be cleaned only when client
1613*4882a593Smuzhiyun 	 * driver unsets all the message status bits.
1614*4882a593Smuzhiyun 	 */
1615*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Message IRQ detected %#08x", ntint_sts);
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	/* Notify the client driver of possible message status change */
1618*4882a593Smuzhiyun 	ntb_msg_event(&ndev->ntb);
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun /*
1622*4882a593Smuzhiyun  * idt_ntb_msg_count() - get the number of message registers (NTB API callback)
1623*4882a593Smuzhiyun  * @ntb:	NTB device context.
1624*4882a593Smuzhiyun  *
1625*4882a593Smuzhiyun  * IDT PCIe-switches support four message registers.
1626*4882a593Smuzhiyun  *
1627*4882a593Smuzhiyun  * Return: the number of message registers.
1628*4882a593Smuzhiyun  */
idt_ntb_msg_count(struct ntb_dev * ntb)1629*4882a593Smuzhiyun static int idt_ntb_msg_count(struct ntb_dev *ntb)
1630*4882a593Smuzhiyun {
1631*4882a593Smuzhiyun 	return IDT_MSG_CNT;
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun /*
1635*4882a593Smuzhiyun  * idt_ntb_msg_inbits() - get a bitfield of inbound message registers status
1636*4882a593Smuzhiyun  *			  (NTB API callback)
1637*4882a593Smuzhiyun  * @ntb:	NTB device context.
1638*4882a593Smuzhiyun  *
1639*4882a593Smuzhiyun  * NT message status register is shared between inbound and outbound message
1640*4882a593Smuzhiyun  * registers status
1641*4882a593Smuzhiyun  *
1642*4882a593Smuzhiyun  * Return: bitfield of inbound message registers.
1643*4882a593Smuzhiyun  */
idt_ntb_msg_inbits(struct ntb_dev * ntb)1644*4882a593Smuzhiyun static u64 idt_ntb_msg_inbits(struct ntb_dev *ntb)
1645*4882a593Smuzhiyun {
1646*4882a593Smuzhiyun 	return (u64)IDT_INMSG_MASK;
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun /*
1650*4882a593Smuzhiyun  * idt_ntb_msg_outbits() - get a bitfield of outbound message registers status
1651*4882a593Smuzhiyun  *			  (NTB API callback)
1652*4882a593Smuzhiyun  * @ntb:	NTB device context.
1653*4882a593Smuzhiyun  *
1654*4882a593Smuzhiyun  * NT message status register is shared between inbound and outbound message
1655*4882a593Smuzhiyun  * registers status
1656*4882a593Smuzhiyun  *
1657*4882a593Smuzhiyun  * Return: bitfield of outbound message registers.
1658*4882a593Smuzhiyun  */
idt_ntb_msg_outbits(struct ntb_dev * ntb)1659*4882a593Smuzhiyun static u64 idt_ntb_msg_outbits(struct ntb_dev *ntb)
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun 	return (u64)IDT_OUTMSG_MASK;
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun /*
1665*4882a593Smuzhiyun  * idt_ntb_msg_read_sts() - read the message registers status (NTB API callback)
1666*4882a593Smuzhiyun  * @ntb:	NTB device context.
1667*4882a593Smuzhiyun  *
1668*4882a593Smuzhiyun  * IDT PCIe-switches expose message status registers to notify drivers of
1669*4882a593Smuzhiyun  * incoming data and failures in case if peer message register isn't freed.
1670*4882a593Smuzhiyun  *
1671*4882a593Smuzhiyun  * Return: status bits of message registers
1672*4882a593Smuzhiyun  */
idt_ntb_msg_read_sts(struct ntb_dev * ntb)1673*4882a593Smuzhiyun static u64 idt_ntb_msg_read_sts(struct ntb_dev *ntb)
1674*4882a593Smuzhiyun {
1675*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	return idt_nt_read(ndev, IDT_NT_MSGSTS);
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun /*
1681*4882a593Smuzhiyun  * idt_ntb_msg_clear_sts() - clear status bits of message registers
1682*4882a593Smuzhiyun  *			     (NTB API callback)
1683*4882a593Smuzhiyun  * @ntb:	NTB device context.
1684*4882a593Smuzhiyun  * @sts_bits:	Status bits to clear.
1685*4882a593Smuzhiyun  *
1686*4882a593Smuzhiyun  * Clear bits in the status register by writing ones.
1687*4882a593Smuzhiyun  *
1688*4882a593Smuzhiyun  * NOTE! Invalid bits are always considered cleared so it's not an error
1689*4882a593Smuzhiyun  * to clear them over.
1690*4882a593Smuzhiyun  *
1691*4882a593Smuzhiyun  * Return: always zero as success.
1692*4882a593Smuzhiyun  */
idt_ntb_msg_clear_sts(struct ntb_dev * ntb,u64 sts_bits)1693*4882a593Smuzhiyun static int idt_ntb_msg_clear_sts(struct ntb_dev *ntb, u64 sts_bits)
1694*4882a593Smuzhiyun {
1695*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1696*4882a593Smuzhiyun 
1697*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_MSGSTS, sts_bits);
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	return 0;
1700*4882a593Smuzhiyun }
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun /*
1703*4882a593Smuzhiyun  * idt_ntb_msg_set_mask() - set mask of message register status bits
1704*4882a593Smuzhiyun  *			    (NTB API callback)
1705*4882a593Smuzhiyun  * @ntb:	NTB device context.
1706*4882a593Smuzhiyun  * @mask_bits:	Mask bits.
1707*4882a593Smuzhiyun  *
1708*4882a593Smuzhiyun  * Mask the message status bits from raising an IRQ.
1709*4882a593Smuzhiyun  *
1710*4882a593Smuzhiyun  * Return: zero on success, negative error if invalid argument passed.
1711*4882a593Smuzhiyun  */
idt_ntb_msg_set_mask(struct ntb_dev * ntb,u64 mask_bits)1712*4882a593Smuzhiyun static int idt_ntb_msg_set_mask(struct ntb_dev *ntb, u64 mask_bits)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	return idt_reg_set_bits(ndev, IDT_NT_MSGSTSMSK, &ndev->msg_mask_lock,
1717*4882a593Smuzhiyun 				IDT_MSG_MASK, mask_bits);
1718*4882a593Smuzhiyun }
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun /*
1721*4882a593Smuzhiyun  * idt_ntb_msg_clear_mask() - clear message registers mask
1722*4882a593Smuzhiyun  *			      (NTB API callback)
1723*4882a593Smuzhiyun  * @ntb:	NTB device context.
1724*4882a593Smuzhiyun  * @mask_bits:	Mask bits.
1725*4882a593Smuzhiyun  *
1726*4882a593Smuzhiyun  * Clear mask of message status bits IRQs.
1727*4882a593Smuzhiyun  *
1728*4882a593Smuzhiyun  * Return: always zero as success.
1729*4882a593Smuzhiyun  */
idt_ntb_msg_clear_mask(struct ntb_dev * ntb,u64 mask_bits)1730*4882a593Smuzhiyun static int idt_ntb_msg_clear_mask(struct ntb_dev *ntb, u64 mask_bits)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 	idt_reg_clear_bits(ndev, IDT_NT_MSGSTSMSK, &ndev->msg_mask_lock,
1735*4882a593Smuzhiyun 			   mask_bits);
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	return 0;
1738*4882a593Smuzhiyun }
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun /*
1741*4882a593Smuzhiyun  * idt_ntb_msg_read() - read message register with specified index
1742*4882a593Smuzhiyun  *			(NTB API callback)
1743*4882a593Smuzhiyun  * @ntb:	NTB device context.
1744*4882a593Smuzhiyun  * @pidx:	OUT - Port index of peer device a message retrieved from
1745*4882a593Smuzhiyun  * @midx:	Message register index
1746*4882a593Smuzhiyun  *
1747*4882a593Smuzhiyun  * Read data from the specified message register and source register.
1748*4882a593Smuzhiyun  *
1749*4882a593Smuzhiyun  * Return: inbound message register value.
1750*4882a593Smuzhiyun  */
idt_ntb_msg_read(struct ntb_dev * ntb,int * pidx,int midx)1751*4882a593Smuzhiyun static u32 idt_ntb_msg_read(struct ntb_dev *ntb, int *pidx, int midx)
1752*4882a593Smuzhiyun {
1753*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	if (midx < 0 || IDT_MSG_CNT <= midx)
1756*4882a593Smuzhiyun 		return ~(u32)0;
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 	/* Retrieve source port index of the message */
1759*4882a593Smuzhiyun 	if (pidx != NULL) {
1760*4882a593Smuzhiyun 		u32 srcpart;
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 		srcpart = idt_nt_read(ndev, ntdata_tbl.msgs[midx].src);
1763*4882a593Smuzhiyun 		*pidx = ndev->part_idx_map[srcpart];
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 		/* Sanity check partition index (for initial case) */
1766*4882a593Smuzhiyun 		if (*pidx == -EINVAL)
1767*4882a593Smuzhiyun 			*pidx = 0;
1768*4882a593Smuzhiyun 	}
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	/* Retrieve data of the corresponding message register */
1771*4882a593Smuzhiyun 	return idt_nt_read(ndev, ntdata_tbl.msgs[midx].in);
1772*4882a593Smuzhiyun }
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun /*
1775*4882a593Smuzhiyun  * idt_ntb_peer_msg_write() - write data to the specified message register
1776*4882a593Smuzhiyun  *			      (NTB API callback)
1777*4882a593Smuzhiyun  * @ntb:	NTB device context.
1778*4882a593Smuzhiyun  * @pidx:	Port index of peer device a message being sent to
1779*4882a593Smuzhiyun  * @midx:	Message register index
1780*4882a593Smuzhiyun  * @msg:	Data to send
1781*4882a593Smuzhiyun  *
1782*4882a593Smuzhiyun  * Just try to send data to a peer. Message status register should be
1783*4882a593Smuzhiyun  * checked by client driver.
1784*4882a593Smuzhiyun  *
1785*4882a593Smuzhiyun  * Return: zero on success, negative error if invalid argument passed.
1786*4882a593Smuzhiyun  */
idt_ntb_peer_msg_write(struct ntb_dev * ntb,int pidx,int midx,u32 msg)1787*4882a593Smuzhiyun static int idt_ntb_peer_msg_write(struct ntb_dev *ntb, int pidx, int midx,
1788*4882a593Smuzhiyun 				  u32 msg)
1789*4882a593Smuzhiyun {
1790*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = to_ndev_ntb(ntb);
1791*4882a593Smuzhiyun 	unsigned long irqflags;
1792*4882a593Smuzhiyun 	u32 swpmsgctl = 0;
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 	if (midx < 0 || IDT_MSG_CNT <= midx)
1795*4882a593Smuzhiyun 		return -EINVAL;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 	if (pidx < 0 || ndev->peer_cnt <= pidx)
1798*4882a593Smuzhiyun 		return -EINVAL;
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	/* Collect the routing information */
1801*4882a593Smuzhiyun 	swpmsgctl = SET_FIELD(SWPxMSGCTL_REG, 0, midx) |
1802*4882a593Smuzhiyun 		    SET_FIELD(SWPxMSGCTL_PART, 0, ndev->peers[pidx].part);
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	/* Lock the messages routing table of the specified register */
1805*4882a593Smuzhiyun 	spin_lock_irqsave(&ndev->msg_locks[midx], irqflags);
1806*4882a593Smuzhiyun 	/* Set the route and send the data */
1807*4882a593Smuzhiyun 	idt_sw_write(ndev, partdata_tbl[ndev->part].msgctl[midx], swpmsgctl);
1808*4882a593Smuzhiyun 	idt_nt_write(ndev, ntdata_tbl.msgs[midx].out, msg);
1809*4882a593Smuzhiyun 	/* Unlock the messages routing table */
1810*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ndev->msg_locks[midx], irqflags);
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	/* Client driver shall check the status register */
1813*4882a593Smuzhiyun 	return 0;
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun /*=============================================================================
1817*4882a593Smuzhiyun  *                      7. Temperature sensor operations
1818*4882a593Smuzhiyun  *
1819*4882a593Smuzhiyun  *    IDT PCIe-switch has an embedded temperature sensor, which can be used to
1820*4882a593Smuzhiyun  * check current chip core temperature. Since a workload environment can be
1821*4882a593Smuzhiyun  * different on different platforms, an offset and ADC/filter settings can be
1822*4882a593Smuzhiyun  * specified. Although the offset configuration is only exposed to the sysfs
1823*4882a593Smuzhiyun  * hwmon interface at the moment. The rest of the settings can be adjusted
1824*4882a593Smuzhiyun  * for instance by the BIOS/EEPROM firmware.
1825*4882a593Smuzhiyun  *=============================================================================
1826*4882a593Smuzhiyun  */
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun /*
1829*4882a593Smuzhiyun  * idt_get_deg() - convert millidegree Celsius value to just degree
1830*4882a593Smuzhiyun  * @mdegC:	IN - millidegree Celsius value
1831*4882a593Smuzhiyun  *
1832*4882a593Smuzhiyun  * Return: Degree corresponding to the passed millidegree value
1833*4882a593Smuzhiyun  */
idt_get_deg(long mdegC)1834*4882a593Smuzhiyun static inline s8 idt_get_deg(long mdegC)
1835*4882a593Smuzhiyun {
1836*4882a593Smuzhiyun 	return mdegC / 1000;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun /*
1840*4882a593Smuzhiyun  * idt_get_frac() - retrieve 0/0.5 fraction of the millidegree Celsius value
1841*4882a593Smuzhiyun  * @mdegC:	IN - millidegree Celsius value
1842*4882a593Smuzhiyun  *
1843*4882a593Smuzhiyun  * Return: 0/0.5 degree fraction of the passed millidegree value
1844*4882a593Smuzhiyun  */
idt_get_deg_frac(long mdegC)1845*4882a593Smuzhiyun static inline u8 idt_get_deg_frac(long mdegC)
1846*4882a593Smuzhiyun {
1847*4882a593Smuzhiyun 	return (mdegC % 1000) >= 500 ? 5 : 0;
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun 
1850*4882a593Smuzhiyun /*
1851*4882a593Smuzhiyun  * idt_get_temp_fmt() - convert millidegree Celsius value to 0:7:1 format
1852*4882a593Smuzhiyun  * @mdegC:	IN - millidegree Celsius value
1853*4882a593Smuzhiyun  *
1854*4882a593Smuzhiyun  * Return: 0:7:1 format acceptable by the IDT temperature sensor
1855*4882a593Smuzhiyun  */
idt_temp_get_fmt(long mdegC)1856*4882a593Smuzhiyun static inline u8 idt_temp_get_fmt(long mdegC)
1857*4882a593Smuzhiyun {
1858*4882a593Smuzhiyun 	return (idt_get_deg(mdegC) << 1) | (idt_get_deg_frac(mdegC) ? 1 : 0);
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun /*
1862*4882a593Smuzhiyun  * idt_get_temp_sval() - convert temp sample to signed millidegree Celsius
1863*4882a593Smuzhiyun  * @data:	IN - shifted to LSB 8-bits temperature sample
1864*4882a593Smuzhiyun  *
1865*4882a593Smuzhiyun  * Return: signed millidegree Celsius
1866*4882a593Smuzhiyun  */
idt_get_temp_sval(u32 data)1867*4882a593Smuzhiyun static inline long idt_get_temp_sval(u32 data)
1868*4882a593Smuzhiyun {
1869*4882a593Smuzhiyun 	return ((s8)data / 2) * 1000 + (data & 0x1 ? 500 : 0);
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun /*
1873*4882a593Smuzhiyun  * idt_get_temp_sval() - convert temp sample to unsigned millidegree Celsius
1874*4882a593Smuzhiyun  * @data:	IN - shifted to LSB 8-bits temperature sample
1875*4882a593Smuzhiyun  *
1876*4882a593Smuzhiyun  * Return: unsigned millidegree Celsius
1877*4882a593Smuzhiyun  */
idt_get_temp_uval(u32 data)1878*4882a593Smuzhiyun static inline long idt_get_temp_uval(u32 data)
1879*4882a593Smuzhiyun {
1880*4882a593Smuzhiyun 	return (data / 2) * 1000 + (data & 0x1 ? 500 : 0);
1881*4882a593Smuzhiyun }
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun /*
1884*4882a593Smuzhiyun  * idt_read_temp() - read temperature from chip sensor
1885*4882a593Smuzhiyun  * @ntb:	NTB device context.
1886*4882a593Smuzhiyun  * @type:	IN - type of the temperature value to read
1887*4882a593Smuzhiyun  * @val:	OUT - integer value of temperature in millidegree Celsius
1888*4882a593Smuzhiyun  */
idt_read_temp(struct idt_ntb_dev * ndev,const enum idt_temp_val type,long * val)1889*4882a593Smuzhiyun static void idt_read_temp(struct idt_ntb_dev *ndev,
1890*4882a593Smuzhiyun 			  const enum idt_temp_val type, long *val)
1891*4882a593Smuzhiyun {
1892*4882a593Smuzhiyun 	u32 data;
1893*4882a593Smuzhiyun 
1894*4882a593Smuzhiyun 	/* Alter the temperature field in accordance with the passed type */
1895*4882a593Smuzhiyun 	switch (type) {
1896*4882a593Smuzhiyun 	case IDT_TEMP_CUR:
1897*4882a593Smuzhiyun 		data = GET_FIELD(TMPSTS_TEMP,
1898*4882a593Smuzhiyun 				 idt_sw_read(ndev, IDT_SW_TMPSTS));
1899*4882a593Smuzhiyun 		break;
1900*4882a593Smuzhiyun 	case IDT_TEMP_LOW:
1901*4882a593Smuzhiyun 		data = GET_FIELD(TMPSTS_LTEMP,
1902*4882a593Smuzhiyun 				 idt_sw_read(ndev, IDT_SW_TMPSTS));
1903*4882a593Smuzhiyun 		break;
1904*4882a593Smuzhiyun 	case IDT_TEMP_HIGH:
1905*4882a593Smuzhiyun 		data = GET_FIELD(TMPSTS_HTEMP,
1906*4882a593Smuzhiyun 				 idt_sw_read(ndev, IDT_SW_TMPSTS));
1907*4882a593Smuzhiyun 		break;
1908*4882a593Smuzhiyun 	case IDT_TEMP_OFFSET:
1909*4882a593Smuzhiyun 		/* This is the only field with signed 0:7:1 format */
1910*4882a593Smuzhiyun 		data = GET_FIELD(TMPADJ_OFFSET,
1911*4882a593Smuzhiyun 				 idt_sw_read(ndev, IDT_SW_TMPADJ));
1912*4882a593Smuzhiyun 		*val = idt_get_temp_sval(data);
1913*4882a593Smuzhiyun 		return;
1914*4882a593Smuzhiyun 	default:
1915*4882a593Smuzhiyun 		data = GET_FIELD(TMPSTS_TEMP,
1916*4882a593Smuzhiyun 				 idt_sw_read(ndev, IDT_SW_TMPSTS));
1917*4882a593Smuzhiyun 		break;
1918*4882a593Smuzhiyun 	}
1919*4882a593Smuzhiyun 
1920*4882a593Smuzhiyun 	/* The rest of the fields accept unsigned 0:7:1 format */
1921*4882a593Smuzhiyun 	*val = idt_get_temp_uval(data);
1922*4882a593Smuzhiyun }
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun /*
1925*4882a593Smuzhiyun  * idt_write_temp() - write temperature to the chip sensor register
1926*4882a593Smuzhiyun  * @ntb:	NTB device context.
1927*4882a593Smuzhiyun  * @type:	IN - type of the temperature value to change
1928*4882a593Smuzhiyun  * @val:	IN - integer value of temperature in millidegree Celsius
1929*4882a593Smuzhiyun  */
idt_write_temp(struct idt_ntb_dev * ndev,const enum idt_temp_val type,const long val)1930*4882a593Smuzhiyun static void idt_write_temp(struct idt_ntb_dev *ndev,
1931*4882a593Smuzhiyun 			   const enum idt_temp_val type, const long val)
1932*4882a593Smuzhiyun {
1933*4882a593Smuzhiyun 	unsigned int reg;
1934*4882a593Smuzhiyun 	u32 data;
1935*4882a593Smuzhiyun 	u8 fmt;
1936*4882a593Smuzhiyun 
1937*4882a593Smuzhiyun 	/* Retrieve the properly formatted temperature value */
1938*4882a593Smuzhiyun 	fmt = idt_temp_get_fmt(val);
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun 	mutex_lock(&ndev->hwmon_mtx);
1941*4882a593Smuzhiyun 	switch (type) {
1942*4882a593Smuzhiyun 	case IDT_TEMP_LOW:
1943*4882a593Smuzhiyun 		reg = IDT_SW_TMPALARM;
1944*4882a593Smuzhiyun 		data = SET_FIELD(TMPALARM_LTEMP, idt_sw_read(ndev, reg), fmt) &
1945*4882a593Smuzhiyun 			~IDT_TMPALARM_IRQ_MASK;
1946*4882a593Smuzhiyun 		break;
1947*4882a593Smuzhiyun 	case IDT_TEMP_HIGH:
1948*4882a593Smuzhiyun 		reg = IDT_SW_TMPALARM;
1949*4882a593Smuzhiyun 		data = SET_FIELD(TMPALARM_HTEMP, idt_sw_read(ndev, reg), fmt) &
1950*4882a593Smuzhiyun 			~IDT_TMPALARM_IRQ_MASK;
1951*4882a593Smuzhiyun 		break;
1952*4882a593Smuzhiyun 	case IDT_TEMP_OFFSET:
1953*4882a593Smuzhiyun 		reg = IDT_SW_TMPADJ;
1954*4882a593Smuzhiyun 		data = SET_FIELD(TMPADJ_OFFSET, idt_sw_read(ndev, reg), fmt);
1955*4882a593Smuzhiyun 		break;
1956*4882a593Smuzhiyun 	default:
1957*4882a593Smuzhiyun 		goto inval_spin_unlock;
1958*4882a593Smuzhiyun 	}
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	idt_sw_write(ndev, reg, data);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun inval_spin_unlock:
1963*4882a593Smuzhiyun 	mutex_unlock(&ndev->hwmon_mtx);
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun /*
1967*4882a593Smuzhiyun  * idt_sysfs_show_temp() - printout corresponding temperature value
1968*4882a593Smuzhiyun  * @dev:	Pointer to the NTB device structure
1969*4882a593Smuzhiyun  * @da:		Sensor device attribute structure
1970*4882a593Smuzhiyun  * @buf:	Buffer to print temperature out
1971*4882a593Smuzhiyun  *
1972*4882a593Smuzhiyun  * Return: Number of written symbols or negative error
1973*4882a593Smuzhiyun  */
idt_sysfs_show_temp(struct device * dev,struct device_attribute * da,char * buf)1974*4882a593Smuzhiyun static ssize_t idt_sysfs_show_temp(struct device *dev,
1975*4882a593Smuzhiyun 				   struct device_attribute *da, char *buf)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
1978*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = dev_get_drvdata(dev);
1979*4882a593Smuzhiyun 	enum idt_temp_val type = attr->index;
1980*4882a593Smuzhiyun 	long mdeg;
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	idt_read_temp(ndev, type, &mdeg);
1983*4882a593Smuzhiyun 	return sprintf(buf, "%ld\n", mdeg);
1984*4882a593Smuzhiyun }
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun /*
1987*4882a593Smuzhiyun  * idt_sysfs_set_temp() - set corresponding temperature value
1988*4882a593Smuzhiyun  * @dev:	Pointer to the NTB device structure
1989*4882a593Smuzhiyun  * @da:		Sensor device attribute structure
1990*4882a593Smuzhiyun  * @buf:	Buffer to print temperature out
1991*4882a593Smuzhiyun  * @count:	Size of the passed buffer
1992*4882a593Smuzhiyun  *
1993*4882a593Smuzhiyun  * Return: Number of written symbols or negative error
1994*4882a593Smuzhiyun  */
idt_sysfs_set_temp(struct device * dev,struct device_attribute * da,const char * buf,size_t count)1995*4882a593Smuzhiyun static ssize_t idt_sysfs_set_temp(struct device *dev,
1996*4882a593Smuzhiyun 				  struct device_attribute *da, const char *buf,
1997*4882a593Smuzhiyun 				  size_t count)
1998*4882a593Smuzhiyun {
1999*4882a593Smuzhiyun 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
2000*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = dev_get_drvdata(dev);
2001*4882a593Smuzhiyun 	enum idt_temp_val type = attr->index;
2002*4882a593Smuzhiyun 	long mdeg;
2003*4882a593Smuzhiyun 	int ret;
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	ret = kstrtol(buf, 10, &mdeg);
2006*4882a593Smuzhiyun 	if (ret)
2007*4882a593Smuzhiyun 		return ret;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	/* Clamp the passed value in accordance with the type */
2010*4882a593Smuzhiyun 	if (type == IDT_TEMP_OFFSET)
2011*4882a593Smuzhiyun 		mdeg = clamp_val(mdeg, IDT_TEMP_MIN_OFFSET,
2012*4882a593Smuzhiyun 				 IDT_TEMP_MAX_OFFSET);
2013*4882a593Smuzhiyun 	else
2014*4882a593Smuzhiyun 		mdeg = clamp_val(mdeg, IDT_TEMP_MIN_MDEG, IDT_TEMP_MAX_MDEG);
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	idt_write_temp(ndev, type, mdeg);
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	return count;
2019*4882a593Smuzhiyun }
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun /*
2022*4882a593Smuzhiyun  * idt_sysfs_reset_hist() - reset temperature history
2023*4882a593Smuzhiyun  * @dev:	Pointer to the NTB device structure
2024*4882a593Smuzhiyun  * @da:		Sensor device attribute structure
2025*4882a593Smuzhiyun  * @buf:	Buffer to print temperature out
2026*4882a593Smuzhiyun  * @count:	Size of the passed buffer
2027*4882a593Smuzhiyun  *
2028*4882a593Smuzhiyun  * Return: Number of written symbols or negative error
2029*4882a593Smuzhiyun  */
idt_sysfs_reset_hist(struct device * dev,struct device_attribute * da,const char * buf,size_t count)2030*4882a593Smuzhiyun static ssize_t idt_sysfs_reset_hist(struct device *dev,
2031*4882a593Smuzhiyun 				    struct device_attribute *da,
2032*4882a593Smuzhiyun 				    const char *buf, size_t count)
2033*4882a593Smuzhiyun {
2034*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = dev_get_drvdata(dev);
2035*4882a593Smuzhiyun 
2036*4882a593Smuzhiyun 	/* Just set the maximal value to the lowest temperature field and
2037*4882a593Smuzhiyun 	 * minimal value to the highest temperature field
2038*4882a593Smuzhiyun 	 */
2039*4882a593Smuzhiyun 	idt_write_temp(ndev, IDT_TEMP_LOW, IDT_TEMP_MAX_MDEG);
2040*4882a593Smuzhiyun 	idt_write_temp(ndev, IDT_TEMP_HIGH, IDT_TEMP_MIN_MDEG);
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 	return count;
2043*4882a593Smuzhiyun }
2044*4882a593Smuzhiyun 
2045*4882a593Smuzhiyun /*
2046*4882a593Smuzhiyun  * Hwmon IDT sysfs attributes
2047*4882a593Smuzhiyun  */
2048*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR(temp1_input, 0444, idt_sysfs_show_temp, NULL,
2049*4882a593Smuzhiyun 			  IDT_TEMP_CUR);
2050*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR(temp1_lowest, 0444, idt_sysfs_show_temp, NULL,
2051*4882a593Smuzhiyun 			  IDT_TEMP_LOW);
2052*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR(temp1_highest, 0444, idt_sysfs_show_temp, NULL,
2053*4882a593Smuzhiyun 			  IDT_TEMP_HIGH);
2054*4882a593Smuzhiyun static SENSOR_DEVICE_ATTR(temp1_offset, 0644, idt_sysfs_show_temp,
2055*4882a593Smuzhiyun 			  idt_sysfs_set_temp, IDT_TEMP_OFFSET);
2056*4882a593Smuzhiyun static DEVICE_ATTR(temp1_reset_history, 0200, NULL, idt_sysfs_reset_hist);
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun /*
2059*4882a593Smuzhiyun  * Hwmon IDT sysfs attributes group
2060*4882a593Smuzhiyun  */
2061*4882a593Smuzhiyun static struct attribute *idt_temp_attrs[] = {
2062*4882a593Smuzhiyun 	&sensor_dev_attr_temp1_input.dev_attr.attr,
2063*4882a593Smuzhiyun 	&sensor_dev_attr_temp1_lowest.dev_attr.attr,
2064*4882a593Smuzhiyun 	&sensor_dev_attr_temp1_highest.dev_attr.attr,
2065*4882a593Smuzhiyun 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
2066*4882a593Smuzhiyun 	&dev_attr_temp1_reset_history.attr,
2067*4882a593Smuzhiyun 	NULL
2068*4882a593Smuzhiyun };
2069*4882a593Smuzhiyun ATTRIBUTE_GROUPS(idt_temp);
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /*
2072*4882a593Smuzhiyun  * idt_init_temp() - initialize temperature sensor interface
2073*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2074*4882a593Smuzhiyun  *
2075*4882a593Smuzhiyun  * Simple sensor initializarion method is responsible for device switching
2076*4882a593Smuzhiyun  * on and resource management based hwmon interface registration. Note, that
2077*4882a593Smuzhiyun  * since the device is shared we won't disable it on remove, but leave it
2078*4882a593Smuzhiyun  * working until the system is powered off.
2079*4882a593Smuzhiyun  */
idt_init_temp(struct idt_ntb_dev * ndev)2080*4882a593Smuzhiyun static void idt_init_temp(struct idt_ntb_dev *ndev)
2081*4882a593Smuzhiyun {
2082*4882a593Smuzhiyun 	struct device *hwmon;
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 	/* Enable sensor if it hasn't been already */
2085*4882a593Smuzhiyun 	idt_sw_write(ndev, IDT_SW_TMPCTL, 0x0);
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	/* Initialize hwmon interface fields */
2088*4882a593Smuzhiyun 	mutex_init(&ndev->hwmon_mtx);
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	hwmon = devm_hwmon_device_register_with_groups(&ndev->ntb.pdev->dev,
2091*4882a593Smuzhiyun 		ndev->swcfg->name, ndev, idt_temp_groups);
2092*4882a593Smuzhiyun 	if (IS_ERR(hwmon)) {
2093*4882a593Smuzhiyun 		dev_err(&ndev->ntb.pdev->dev, "Couldn't create hwmon device");
2094*4882a593Smuzhiyun 		return;
2095*4882a593Smuzhiyun 	}
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "Temperature HWmon interface registered");
2098*4882a593Smuzhiyun }
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun /*=============================================================================
2101*4882a593Smuzhiyun  *                           8. ISRs related operations
2102*4882a593Smuzhiyun  *
2103*4882a593Smuzhiyun  *    IDT PCIe-switch has strangely developed IRQ system. There is just one
2104*4882a593Smuzhiyun  * interrupt vector for doorbell and message registers. So the hardware driver
2105*4882a593Smuzhiyun  * can't determine actual source of IRQ if, for example, message event happened
2106*4882a593Smuzhiyun  * while any of unmasked doorbell is still set. The similar situation may be if
2107*4882a593Smuzhiyun  * switch or temperature sensor events pop up. The difference is that SEVENT
2108*4882a593Smuzhiyun  * and TMPSENSOR bits of NT interrupt status register can be cleaned by
2109*4882a593Smuzhiyun  * IRQ handler so a next interrupt request won't have false handling of
2110*4882a593Smuzhiyun  * corresponding events.
2111*4882a593Smuzhiyun  *    The hardware driver has only bottom-half handler of the IRQ, since if any
2112*4882a593Smuzhiyun  * of events happened the device won't raise it again before the last one is
2113*4882a593Smuzhiyun  * handled by clearing of corresponding NTINTSTS bit.
2114*4882a593Smuzhiyun  *=============================================================================
2115*4882a593Smuzhiyun  */
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun static irqreturn_t idt_thread_isr(int irq, void *devid);
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun /*
2120*4882a593Smuzhiyun  * idt_init_isr() - initialize PCIe interrupt handler
2121*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2122*4882a593Smuzhiyun  *
2123*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number.
2124*4882a593Smuzhiyun  */
idt_init_isr(struct idt_ntb_dev * ndev)2125*4882a593Smuzhiyun static int idt_init_isr(struct idt_ntb_dev *ndev)
2126*4882a593Smuzhiyun {
2127*4882a593Smuzhiyun 	struct pci_dev *pdev = ndev->ntb.pdev;
2128*4882a593Smuzhiyun 	u32 ntint_mask;
2129*4882a593Smuzhiyun 	int ret;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	/* Allocate just one interrupt vector for the ISR */
2132*4882a593Smuzhiyun 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_LEGACY);
2133*4882a593Smuzhiyun 	if (ret != 1) {
2134*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to allocate IRQ vector");
2135*4882a593Smuzhiyun 		return ret;
2136*4882a593Smuzhiyun 	}
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	/* Retrieve the IRQ vector */
2139*4882a593Smuzhiyun 	ret = pci_irq_vector(pdev, 0);
2140*4882a593Smuzhiyun 	if (ret < 0) {
2141*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to get IRQ vector");
2142*4882a593Smuzhiyun 		goto err_free_vectors;
2143*4882a593Smuzhiyun 	}
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	/* Set the IRQ handler */
2146*4882a593Smuzhiyun 	ret = devm_request_threaded_irq(&pdev->dev, ret, NULL, idt_thread_isr,
2147*4882a593Smuzhiyun 					IRQF_ONESHOT, NTB_IRQNAME, ndev);
2148*4882a593Smuzhiyun 	if (ret != 0) {
2149*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to set MSI IRQ handler, %d", ret);
2150*4882a593Smuzhiyun 		goto err_free_vectors;
2151*4882a593Smuzhiyun 	}
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	/* Unmask Message/Doorbell/SE interrupts */
2154*4882a593Smuzhiyun 	ntint_mask = idt_nt_read(ndev, IDT_NT_NTINTMSK) & ~IDT_NTINTMSK_ALL;
2155*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTINTMSK, ntint_mask);
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	/* From now on the interrupts are enabled */
2158*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NTB interrupts initialized");
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	return 0;
2161*4882a593Smuzhiyun 
2162*4882a593Smuzhiyun err_free_vectors:
2163*4882a593Smuzhiyun 	pci_free_irq_vectors(pdev);
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	return ret;
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun /*
2169*4882a593Smuzhiyun  * idt_deinit_ist() - deinitialize PCIe interrupt handler
2170*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2171*4882a593Smuzhiyun  *
2172*4882a593Smuzhiyun  * Disable corresponding interrupts and free allocated IRQ vectors.
2173*4882a593Smuzhiyun  */
idt_deinit_isr(struct idt_ntb_dev * ndev)2174*4882a593Smuzhiyun static void idt_deinit_isr(struct idt_ntb_dev *ndev)
2175*4882a593Smuzhiyun {
2176*4882a593Smuzhiyun 	struct pci_dev *pdev = ndev->ntb.pdev;
2177*4882a593Smuzhiyun 	u32 ntint_mask;
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun 	/* Mask interrupts back */
2180*4882a593Smuzhiyun 	ntint_mask = idt_nt_read(ndev, IDT_NT_NTINTMSK) | IDT_NTINTMSK_ALL;
2181*4882a593Smuzhiyun 	idt_nt_write(ndev, IDT_NT_NTINTMSK, ntint_mask);
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	/* Manually free IRQ otherwise PCI free irq vectors will fail */
2184*4882a593Smuzhiyun 	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 0), ndev);
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	/* Free allocated IRQ vectors */
2187*4882a593Smuzhiyun 	pci_free_irq_vectors(pdev);
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NTB interrupts deinitialized");
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun /*
2193*4882a593Smuzhiyun  * idt_thread_isr() - NT function interrupts handler
2194*4882a593Smuzhiyun  * @irq:	IRQ number
2195*4882a593Smuzhiyun  * @devid:	Custom buffer
2196*4882a593Smuzhiyun  *
2197*4882a593Smuzhiyun  * It reads current NT interrupts state register and handles all the event
2198*4882a593Smuzhiyun  * it declares.
2199*4882a593Smuzhiyun  * The method is bottom-half routine of actual default PCIe IRQ handler.
2200*4882a593Smuzhiyun  */
idt_thread_isr(int irq,void * devid)2201*4882a593Smuzhiyun static irqreturn_t idt_thread_isr(int irq, void *devid)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = devid;
2204*4882a593Smuzhiyun 	bool handled = false;
2205*4882a593Smuzhiyun 	u32 ntint_sts;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	/* Read the NT interrupts status register */
2208*4882a593Smuzhiyun 	ntint_sts = idt_nt_read(ndev, IDT_NT_NTINTSTS);
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 	/* Handle messaging interrupts */
2211*4882a593Smuzhiyun 	if (ntint_sts & IDT_NTINTSTS_MSG) {
2212*4882a593Smuzhiyun 		idt_msg_isr(ndev, ntint_sts);
2213*4882a593Smuzhiyun 		handled = true;
2214*4882a593Smuzhiyun 	}
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	/* Handle doorbell interrupts */
2217*4882a593Smuzhiyun 	if (ntint_sts & IDT_NTINTSTS_DBELL) {
2218*4882a593Smuzhiyun 		idt_db_isr(ndev, ntint_sts);
2219*4882a593Smuzhiyun 		handled = true;
2220*4882a593Smuzhiyun 	}
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun 	/* Handle switch event interrupts */
2223*4882a593Smuzhiyun 	if (ntint_sts & IDT_NTINTSTS_SEVENT) {
2224*4882a593Smuzhiyun 		idt_se_isr(ndev, ntint_sts);
2225*4882a593Smuzhiyun 		handled = true;
2226*4882a593Smuzhiyun 	}
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "IDT IRQs 0x%08x handled", ntint_sts);
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	return handled ? IRQ_HANDLED : IRQ_NONE;
2231*4882a593Smuzhiyun }
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun /*===========================================================================
2234*4882a593Smuzhiyun  *                     9. NTB hardware driver initialization
2235*4882a593Smuzhiyun  *===========================================================================
2236*4882a593Smuzhiyun  */
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun /*
2239*4882a593Smuzhiyun  * NTB API operations
2240*4882a593Smuzhiyun  */
2241*4882a593Smuzhiyun static const struct ntb_dev_ops idt_ntb_ops = {
2242*4882a593Smuzhiyun 	.port_number		= idt_ntb_port_number,
2243*4882a593Smuzhiyun 	.peer_port_count	= idt_ntb_peer_port_count,
2244*4882a593Smuzhiyun 	.peer_port_number	= idt_ntb_peer_port_number,
2245*4882a593Smuzhiyun 	.peer_port_idx		= idt_ntb_peer_port_idx,
2246*4882a593Smuzhiyun 	.link_is_up		= idt_ntb_link_is_up,
2247*4882a593Smuzhiyun 	.link_enable		= idt_ntb_link_enable,
2248*4882a593Smuzhiyun 	.link_disable		= idt_ntb_link_disable,
2249*4882a593Smuzhiyun 	.mw_count		= idt_ntb_mw_count,
2250*4882a593Smuzhiyun 	.mw_get_align		= idt_ntb_mw_get_align,
2251*4882a593Smuzhiyun 	.peer_mw_count		= idt_ntb_peer_mw_count,
2252*4882a593Smuzhiyun 	.peer_mw_get_addr	= idt_ntb_peer_mw_get_addr,
2253*4882a593Smuzhiyun 	.peer_mw_set_trans	= idt_ntb_peer_mw_set_trans,
2254*4882a593Smuzhiyun 	.peer_mw_clear_trans	= idt_ntb_peer_mw_clear_trans,
2255*4882a593Smuzhiyun 	.db_valid_mask		= idt_ntb_db_valid_mask,
2256*4882a593Smuzhiyun 	.db_read		= idt_ntb_db_read,
2257*4882a593Smuzhiyun 	.db_clear		= idt_ntb_db_clear,
2258*4882a593Smuzhiyun 	.db_read_mask		= idt_ntb_db_read_mask,
2259*4882a593Smuzhiyun 	.db_set_mask		= idt_ntb_db_set_mask,
2260*4882a593Smuzhiyun 	.db_clear_mask		= idt_ntb_db_clear_mask,
2261*4882a593Smuzhiyun 	.peer_db_set		= idt_ntb_peer_db_set,
2262*4882a593Smuzhiyun 	.msg_count		= idt_ntb_msg_count,
2263*4882a593Smuzhiyun 	.msg_inbits		= idt_ntb_msg_inbits,
2264*4882a593Smuzhiyun 	.msg_outbits		= idt_ntb_msg_outbits,
2265*4882a593Smuzhiyun 	.msg_read_sts		= idt_ntb_msg_read_sts,
2266*4882a593Smuzhiyun 	.msg_clear_sts		= idt_ntb_msg_clear_sts,
2267*4882a593Smuzhiyun 	.msg_set_mask		= idt_ntb_msg_set_mask,
2268*4882a593Smuzhiyun 	.msg_clear_mask		= idt_ntb_msg_clear_mask,
2269*4882a593Smuzhiyun 	.msg_read		= idt_ntb_msg_read,
2270*4882a593Smuzhiyun 	.peer_msg_write		= idt_ntb_peer_msg_write
2271*4882a593Smuzhiyun };
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun /*
2274*4882a593Smuzhiyun  * idt_register_device() - register IDT NTB device
2275*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2276*4882a593Smuzhiyun  *
2277*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number.
2278*4882a593Smuzhiyun  */
idt_register_device(struct idt_ntb_dev * ndev)2279*4882a593Smuzhiyun static int idt_register_device(struct idt_ntb_dev *ndev)
2280*4882a593Smuzhiyun {
2281*4882a593Smuzhiyun 	int ret;
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun 	/* Initialize the rest of NTB device structure and register it */
2284*4882a593Smuzhiyun 	ndev->ntb.ops = &idt_ntb_ops;
2285*4882a593Smuzhiyun 	ndev->ntb.topo = NTB_TOPO_SWITCH;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 	ret = ntb_register_device(&ndev->ntb);
2288*4882a593Smuzhiyun 	if (ret != 0) {
2289*4882a593Smuzhiyun 		dev_err(&ndev->ntb.pdev->dev, "Failed to register NTB device");
2290*4882a593Smuzhiyun 		return ret;
2291*4882a593Smuzhiyun 	}
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB device successfully registered");
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	return 0;
2296*4882a593Smuzhiyun }
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun /*
2299*4882a593Smuzhiyun  * idt_unregister_device() - unregister IDT NTB device
2300*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2301*4882a593Smuzhiyun  */
idt_unregister_device(struct idt_ntb_dev * ndev)2302*4882a593Smuzhiyun static void idt_unregister_device(struct idt_ntb_dev *ndev)
2303*4882a593Smuzhiyun {
2304*4882a593Smuzhiyun 	/* Just unregister the NTB device */
2305*4882a593Smuzhiyun 	ntb_unregister_device(&ndev->ntb);
2306*4882a593Smuzhiyun 
2307*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB device unregistered");
2308*4882a593Smuzhiyun }
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun /*=============================================================================
2311*4882a593Smuzhiyun  *                        10. DebugFS node initialization
2312*4882a593Smuzhiyun  *=============================================================================
2313*4882a593Smuzhiyun  */
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun static ssize_t idt_dbgfs_info_read(struct file *filp, char __user *ubuf,
2316*4882a593Smuzhiyun 				   size_t count, loff_t *offp);
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun /*
2319*4882a593Smuzhiyun  * Driver DebugFS info file operations
2320*4882a593Smuzhiyun  */
2321*4882a593Smuzhiyun static const struct file_operations idt_dbgfs_info_ops = {
2322*4882a593Smuzhiyun 	.owner = THIS_MODULE,
2323*4882a593Smuzhiyun 	.open = simple_open,
2324*4882a593Smuzhiyun 	.read = idt_dbgfs_info_read
2325*4882a593Smuzhiyun };
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun /*
2328*4882a593Smuzhiyun  * idt_dbgfs_info_read() - DebugFS read info node callback
2329*4882a593Smuzhiyun  * @file:	File node descriptor.
2330*4882a593Smuzhiyun  * @ubuf:	User-space buffer to put data to
2331*4882a593Smuzhiyun  * @count:	Size of the buffer
2332*4882a593Smuzhiyun  * @offp:	Offset within the buffer
2333*4882a593Smuzhiyun  */
idt_dbgfs_info_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)2334*4882a593Smuzhiyun static ssize_t idt_dbgfs_info_read(struct file *filp, char __user *ubuf,
2335*4882a593Smuzhiyun 				   size_t count, loff_t *offp)
2336*4882a593Smuzhiyun {
2337*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = filp->private_data;
2338*4882a593Smuzhiyun 	unsigned char idx, pidx, cnt;
2339*4882a593Smuzhiyun 	unsigned long irqflags, mdeg;
2340*4882a593Smuzhiyun 	ssize_t ret = 0, off = 0;
2341*4882a593Smuzhiyun 	enum ntb_speed speed;
2342*4882a593Smuzhiyun 	enum ntb_width width;
2343*4882a593Smuzhiyun 	char *strbuf;
2344*4882a593Smuzhiyun 	size_t size;
2345*4882a593Smuzhiyun 	u32 data;
2346*4882a593Smuzhiyun 
2347*4882a593Smuzhiyun 	/* Lets limit the buffer size the way the Intel/AMD drivers do */
2348*4882a593Smuzhiyun 	size = min_t(size_t, count, 0x1000U);
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 	/* Allocate the memory for the buffer */
2351*4882a593Smuzhiyun 	strbuf = kmalloc(size, GFP_KERNEL);
2352*4882a593Smuzhiyun 	if (strbuf == NULL)
2353*4882a593Smuzhiyun 		return -ENOMEM;
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	/* Put the data into the string buffer */
2356*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2357*4882a593Smuzhiyun 		"\n\t\tIDT NTB device Information:\n\n");
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	/* General local device configurations */
2360*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2361*4882a593Smuzhiyun 		"Local Port %hhu, Partition %hhu\n", ndev->port, ndev->part);
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun 	/* Peer ports information */
2364*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "Peers:\n");
2365*4882a593Smuzhiyun 	for (idx = 0; idx < ndev->peer_cnt; idx++) {
2366*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off,
2367*4882a593Smuzhiyun 			"\t%hhu. Port %hhu, Partition %hhu\n",
2368*4882a593Smuzhiyun 			idx, ndev->peers[idx].port, ndev->peers[idx].part);
2369*4882a593Smuzhiyun 	}
2370*4882a593Smuzhiyun 
2371*4882a593Smuzhiyun 	/* Links status */
2372*4882a593Smuzhiyun 	data = idt_ntb_link_is_up(&ndev->ntb, &speed, &width);
2373*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2374*4882a593Smuzhiyun 		"NTB link status\t- 0x%08x, ", data);
2375*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "PCIe Gen %d x%d lanes\n",
2376*4882a593Smuzhiyun 		speed, width);
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun 	/* Mapping table entries */
2379*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "NTB Mapping Table:\n");
2380*4882a593Smuzhiyun 	for (idx = 0; idx < IDT_MTBL_ENTRY_CNT; idx++) {
2381*4882a593Smuzhiyun 		spin_lock_irqsave(&ndev->mtbl_lock, irqflags);
2382*4882a593Smuzhiyun 		idt_nt_write(ndev, IDT_NT_NTMTBLADDR, idx);
2383*4882a593Smuzhiyun 		data = idt_nt_read(ndev, IDT_NT_NTMTBLDATA);
2384*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ndev->mtbl_lock, irqflags);
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun 		/* Print valid entries only */
2387*4882a593Smuzhiyun 		if (data & IDT_NTMTBLDATA_VALID) {
2388*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2389*4882a593Smuzhiyun 				"\t%hhu. Partition %d, Requester ID 0x%04x\n",
2390*4882a593Smuzhiyun 				idx, GET_FIELD(NTMTBLDATA_PART, data),
2391*4882a593Smuzhiyun 				GET_FIELD(NTMTBLDATA_REQID, data));
2392*4882a593Smuzhiyun 		}
2393*4882a593Smuzhiyun 	}
2394*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "\n");
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 	/* Outbound memory windows information */
2397*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2398*4882a593Smuzhiyun 		"Outbound Memory Windows:\n");
2399*4882a593Smuzhiyun 	for (idx = 0; idx < ndev->mw_cnt; idx += cnt) {
2400*4882a593Smuzhiyun 		data = ndev->mws[idx].type;
2401*4882a593Smuzhiyun 		cnt = idt_get_mw_count(data);
2402*4882a593Smuzhiyun 
2403*4882a593Smuzhiyun 		/* Print Memory Window information */
2404*4882a593Smuzhiyun 		if (data == IDT_MW_DIR)
2405*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2406*4882a593Smuzhiyun 				"\t%hhu.\t", idx);
2407*4882a593Smuzhiyun 		else
2408*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2409*4882a593Smuzhiyun 				"\t%hhu-%hhu.\t", idx, idx + cnt - 1);
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off, "%s BAR%hhu, ",
2412*4882a593Smuzhiyun 			idt_get_mw_name(data), ndev->mws[idx].bar);
2413*4882a593Smuzhiyun 
2414*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off,
2415*4882a593Smuzhiyun 			"Address align 0x%08llx, ", ndev->mws[idx].addr_align);
2416*4882a593Smuzhiyun 
2417*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off,
2418*4882a593Smuzhiyun 			"Size align 0x%08llx, Size max %llu\n",
2419*4882a593Smuzhiyun 			ndev->mws[idx].size_align, ndev->mws[idx].size_max);
2420*4882a593Smuzhiyun 	}
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	/* Inbound memory windows information */
2423*4882a593Smuzhiyun 	for (pidx = 0; pidx < ndev->peer_cnt; pidx++) {
2424*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off,
2425*4882a593Smuzhiyun 			"Inbound Memory Windows for peer %hhu (Port %hhu):\n",
2426*4882a593Smuzhiyun 			pidx, ndev->peers[pidx].port);
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 		/* Print Memory Windows information */
2429*4882a593Smuzhiyun 		for (idx = 0; idx < ndev->peers[pidx].mw_cnt; idx += cnt) {
2430*4882a593Smuzhiyun 			data = ndev->peers[pidx].mws[idx].type;
2431*4882a593Smuzhiyun 			cnt = idt_get_mw_count(data);
2432*4882a593Smuzhiyun 
2433*4882a593Smuzhiyun 			if (data == IDT_MW_DIR)
2434*4882a593Smuzhiyun 				off += scnprintf(strbuf + off, size - off,
2435*4882a593Smuzhiyun 					"\t%hhu.\t", idx);
2436*4882a593Smuzhiyun 			else
2437*4882a593Smuzhiyun 				off += scnprintf(strbuf + off, size - off,
2438*4882a593Smuzhiyun 					"\t%hhu-%hhu.\t", idx, idx + cnt - 1);
2439*4882a593Smuzhiyun 
2440*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2441*4882a593Smuzhiyun 				"%s BAR%hhu, ", idt_get_mw_name(data),
2442*4882a593Smuzhiyun 				ndev->peers[pidx].mws[idx].bar);
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2445*4882a593Smuzhiyun 				"Address align 0x%08llx, ",
2446*4882a593Smuzhiyun 				ndev->peers[pidx].mws[idx].addr_align);
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 			off += scnprintf(strbuf + off, size - off,
2449*4882a593Smuzhiyun 				"Size align 0x%08llx, Size max %llu\n",
2450*4882a593Smuzhiyun 				ndev->peers[pidx].mws[idx].size_align,
2451*4882a593Smuzhiyun 				ndev->peers[pidx].mws[idx].size_max);
2452*4882a593Smuzhiyun 		}
2453*4882a593Smuzhiyun 	}
2454*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "\n");
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun 	/* Doorbell information */
2457*4882a593Smuzhiyun 	data = idt_sw_read(ndev, IDT_SW_GDBELLSTS);
2458*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2459*4882a593Smuzhiyun 		 "Global Doorbell state\t- 0x%08x\n", data);
2460*4882a593Smuzhiyun 	data = idt_ntb_db_read(&ndev->ntb);
2461*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2462*4882a593Smuzhiyun 		 "Local  Doorbell state\t- 0x%08x\n", data);
2463*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_INDBELLMSK);
2464*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2465*4882a593Smuzhiyun 		 "Local  Doorbell mask\t- 0x%08x\n", data);
2466*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "\n");
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun 	/* Messaging information */
2469*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2470*4882a593Smuzhiyun 		 "Message event valid\t- 0x%08x\n", IDT_MSG_MASK);
2471*4882a593Smuzhiyun 	data = idt_ntb_msg_read_sts(&ndev->ntb);
2472*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2473*4882a593Smuzhiyun 		 "Message event status\t- 0x%08x\n", data);
2474*4882a593Smuzhiyun 	data = idt_nt_read(ndev, IDT_NT_MSGSTSMSK);
2475*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2476*4882a593Smuzhiyun 		 "Message event mask\t- 0x%08x\n", data);
2477*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2478*4882a593Smuzhiyun 		 "Message data:\n");
2479*4882a593Smuzhiyun 	for (idx = 0; idx < IDT_MSG_CNT; idx++) {
2480*4882a593Smuzhiyun 		int src;
2481*4882a593Smuzhiyun 		data = idt_ntb_msg_read(&ndev->ntb, &src, idx);
2482*4882a593Smuzhiyun 		off += scnprintf(strbuf + off, size - off,
2483*4882a593Smuzhiyun 			"\t%hhu. 0x%08x from peer %hhu (Port %hhu)\n",
2484*4882a593Smuzhiyun 			idx, data, src, ndev->peers[src].port);
2485*4882a593Smuzhiyun 	}
2486*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off, "\n");
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun 	/* Current temperature */
2489*4882a593Smuzhiyun 	idt_read_temp(ndev, IDT_TEMP_CUR, &mdeg);
2490*4882a593Smuzhiyun 	off += scnprintf(strbuf + off, size - off,
2491*4882a593Smuzhiyun 		"Switch temperature\t\t- %hhd.%hhuC\n",
2492*4882a593Smuzhiyun 		idt_get_deg(mdeg), idt_get_deg_frac(mdeg));
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	/* Copy the buffer to the User Space */
2495*4882a593Smuzhiyun 	ret = simple_read_from_buffer(ubuf, count, offp, strbuf, off);
2496*4882a593Smuzhiyun 	kfree(strbuf);
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	return ret;
2499*4882a593Smuzhiyun }
2500*4882a593Smuzhiyun 
2501*4882a593Smuzhiyun /*
2502*4882a593Smuzhiyun  * idt_init_dbgfs() - initialize DebugFS node
2503*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2504*4882a593Smuzhiyun  *
2505*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number.
2506*4882a593Smuzhiyun  */
idt_init_dbgfs(struct idt_ntb_dev * ndev)2507*4882a593Smuzhiyun static int idt_init_dbgfs(struct idt_ntb_dev *ndev)
2508*4882a593Smuzhiyun {
2509*4882a593Smuzhiyun 	char devname[64];
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 	/* If the top directory is not created then do nothing */
2512*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(dbgfs_topdir)) {
2513*4882a593Smuzhiyun 		dev_info(&ndev->ntb.pdev->dev, "Top DebugFS directory absent");
2514*4882a593Smuzhiyun 		return PTR_ERR(dbgfs_topdir);
2515*4882a593Smuzhiyun 	}
2516*4882a593Smuzhiyun 
2517*4882a593Smuzhiyun 	/* Create the info file node */
2518*4882a593Smuzhiyun 	snprintf(devname, 64, "info:%s", pci_name(ndev->ntb.pdev));
2519*4882a593Smuzhiyun 	ndev->dbgfs_info = debugfs_create_file(devname, 0400, dbgfs_topdir,
2520*4882a593Smuzhiyun 		ndev, &idt_dbgfs_info_ops);
2521*4882a593Smuzhiyun 	if (IS_ERR(ndev->dbgfs_info)) {
2522*4882a593Smuzhiyun 		dev_dbg(&ndev->ntb.pdev->dev, "Failed to create DebugFS node");
2523*4882a593Smuzhiyun 		return PTR_ERR(ndev->dbgfs_info);
2524*4882a593Smuzhiyun 	}
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB device DebugFS node created");
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 	return 0;
2529*4882a593Smuzhiyun }
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun /*
2532*4882a593Smuzhiyun  * idt_deinit_dbgfs() - deinitialize DebugFS node
2533*4882a593Smuzhiyun  * @ndev:	IDT NTB hardware driver descriptor
2534*4882a593Smuzhiyun  *
2535*4882a593Smuzhiyun  * Just discard the info node from DebugFS
2536*4882a593Smuzhiyun  */
idt_deinit_dbgfs(struct idt_ntb_dev * ndev)2537*4882a593Smuzhiyun static void idt_deinit_dbgfs(struct idt_ntb_dev *ndev)
2538*4882a593Smuzhiyun {
2539*4882a593Smuzhiyun 	debugfs_remove(ndev->dbgfs_info);
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 	dev_dbg(&ndev->ntb.pdev->dev, "NTB device DebugFS node discarded");
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun /*=============================================================================
2545*4882a593Smuzhiyun  *                     11. Basic PCIe device initialization
2546*4882a593Smuzhiyun  *=============================================================================
2547*4882a593Smuzhiyun  */
2548*4882a593Smuzhiyun 
2549*4882a593Smuzhiyun /*
2550*4882a593Smuzhiyun  * idt_check_setup() - Check whether the IDT PCIe-swtich is properly
2551*4882a593Smuzhiyun  *		       pre-initialized
2552*4882a593Smuzhiyun  * @pdev:	Pointer to the PCI device descriptor
2553*4882a593Smuzhiyun  *
2554*4882a593Smuzhiyun  * Return: zero on success, otherwise a negative error number.
2555*4882a593Smuzhiyun  */
idt_check_setup(struct pci_dev * pdev)2556*4882a593Smuzhiyun static int idt_check_setup(struct pci_dev *pdev)
2557*4882a593Smuzhiyun {
2558*4882a593Smuzhiyun 	u32 data;
2559*4882a593Smuzhiyun 	int ret;
2560*4882a593Smuzhiyun 
2561*4882a593Smuzhiyun 	/* Read the BARSETUP0 */
2562*4882a593Smuzhiyun 	ret = pci_read_config_dword(pdev, IDT_NT_BARSETUP0, &data);
2563*4882a593Smuzhiyun 	if (ret != 0) {
2564*4882a593Smuzhiyun 		dev_err(&pdev->dev,
2565*4882a593Smuzhiyun 			"Failed to read BARSETUP0 config register");
2566*4882a593Smuzhiyun 		return ret;
2567*4882a593Smuzhiyun 	}
2568*4882a593Smuzhiyun 
2569*4882a593Smuzhiyun 	/* Check whether the BAR0 register is enabled to be of config space */
2570*4882a593Smuzhiyun 	if (!(data & IDT_BARSETUP_EN) || !(data & IDT_BARSETUP_MODE_CFG)) {
2571*4882a593Smuzhiyun 		dev_err(&pdev->dev, "BAR0 doesn't map config space");
2572*4882a593Smuzhiyun 		return -EINVAL;
2573*4882a593Smuzhiyun 	}
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	/* Configuration space BAR0 must have certain size */
2576*4882a593Smuzhiyun 	if ((data & IDT_BARSETUP_SIZE_MASK) != IDT_BARSETUP_SIZE_CFG) {
2577*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Invalid size of config space");
2578*4882a593Smuzhiyun 		return -EINVAL;
2579*4882a593Smuzhiyun 	}
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NTB device pre-initialized correctly");
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 	return 0;
2584*4882a593Smuzhiyun }
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun /*
2587*4882a593Smuzhiyun  * Create the IDT PCIe-switch driver descriptor
2588*4882a593Smuzhiyun  * @pdev:	Pointer to the PCI device descriptor
2589*4882a593Smuzhiyun  * @id:		IDT PCIe-device configuration
2590*4882a593Smuzhiyun  *
2591*4882a593Smuzhiyun  * It just allocates a memory for IDT PCIe-switch device structure and
2592*4882a593Smuzhiyun  * initializes some commonly used fields.
2593*4882a593Smuzhiyun  *
2594*4882a593Smuzhiyun  * No need of release method, since managed device resource is used for
2595*4882a593Smuzhiyun  * memory allocation.
2596*4882a593Smuzhiyun  *
2597*4882a593Smuzhiyun  * Return: pointer to the descriptor, otherwise a negative error number.
2598*4882a593Smuzhiyun  */
idt_create_dev(struct pci_dev * pdev,const struct pci_device_id * id)2599*4882a593Smuzhiyun static struct idt_ntb_dev *idt_create_dev(struct pci_dev *pdev,
2600*4882a593Smuzhiyun 					  const struct pci_device_id *id)
2601*4882a593Smuzhiyun {
2602*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev;
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 	/* Allocate memory for the IDT PCIe-device descriptor */
2605*4882a593Smuzhiyun 	ndev = devm_kzalloc(&pdev->dev, sizeof(*ndev), GFP_KERNEL);
2606*4882a593Smuzhiyun 	if (!ndev) {
2607*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Memory allocation failed for descriptor");
2608*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
2609*4882a593Smuzhiyun 	}
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun 	/* Save the IDT PCIe-switch ports configuration */
2612*4882a593Smuzhiyun 	ndev->swcfg = (struct idt_89hpes_cfg *)id->driver_data;
2613*4882a593Smuzhiyun 	/* Save the PCI-device pointer inside the NTB device structure */
2614*4882a593Smuzhiyun 	ndev->ntb.pdev = pdev;
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	/* Initialize spin locker of Doorbell, Message and GASA registers */
2617*4882a593Smuzhiyun 	spin_lock_init(&ndev->db_mask_lock);
2618*4882a593Smuzhiyun 	spin_lock_init(&ndev->msg_mask_lock);
2619*4882a593Smuzhiyun 	spin_lock_init(&ndev->gasa_lock);
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun 	dev_info(&pdev->dev, "IDT %s discovered", ndev->swcfg->name);
2622*4882a593Smuzhiyun 
2623*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NTB device descriptor created");
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun 	return ndev;
2626*4882a593Smuzhiyun }
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun /*
2629*4882a593Smuzhiyun  * idt_init_pci() - initialize the basic PCI-related subsystem
2630*4882a593Smuzhiyun  * @ndev:	Pointer to the IDT PCIe-switch driver descriptor
2631*4882a593Smuzhiyun  *
2632*4882a593Smuzhiyun  * Managed device resources will be freed automatically in case of failure or
2633*4882a593Smuzhiyun  * driver detachment.
2634*4882a593Smuzhiyun  *
2635*4882a593Smuzhiyun  * Return: zero on success, otherwise negative error number.
2636*4882a593Smuzhiyun  */
idt_init_pci(struct idt_ntb_dev * ndev)2637*4882a593Smuzhiyun static int idt_init_pci(struct idt_ntb_dev *ndev)
2638*4882a593Smuzhiyun {
2639*4882a593Smuzhiyun 	struct pci_dev *pdev = ndev->ntb.pdev;
2640*4882a593Smuzhiyun 	int ret;
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	/* Initialize the bit mask of PCI/NTB DMA */
2643*4882a593Smuzhiyun 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2644*4882a593Smuzhiyun 	if (ret != 0) {
2645*4882a593Smuzhiyun 		ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2646*4882a593Smuzhiyun 		if (ret != 0) {
2647*4882a593Smuzhiyun 			dev_err(&pdev->dev, "Failed to set DMA bit mask\n");
2648*4882a593Smuzhiyun 			return ret;
2649*4882a593Smuzhiyun 		}
2650*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "Cannot set DMA highmem bit mask\n");
2651*4882a593Smuzhiyun 	}
2652*4882a593Smuzhiyun 	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2653*4882a593Smuzhiyun 	if (ret != 0) {
2654*4882a593Smuzhiyun 		ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2655*4882a593Smuzhiyun 		if (ret != 0) {
2656*4882a593Smuzhiyun 			dev_err(&pdev->dev,
2657*4882a593Smuzhiyun 				"Failed to set consistent DMA bit mask\n");
2658*4882a593Smuzhiyun 			return ret;
2659*4882a593Smuzhiyun 		}
2660*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
2661*4882a593Smuzhiyun 			"Cannot set consistent DMA highmem bit mask\n");
2662*4882a593Smuzhiyun 	}
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	/*
2665*4882a593Smuzhiyun 	 * Enable the device advanced error reporting. It's not critical to
2666*4882a593Smuzhiyun 	 * have AER disabled in the kernel.
2667*4882a593Smuzhiyun 	 */
2668*4882a593Smuzhiyun 	ret = pci_enable_pcie_error_reporting(pdev);
2669*4882a593Smuzhiyun 	if (ret != 0)
2670*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "PCIe AER capability disabled\n");
2671*4882a593Smuzhiyun 	else /* Cleanup nonfatal error status before getting to init */
2672*4882a593Smuzhiyun 		pci_aer_clear_nonfatal_status(pdev);
2673*4882a593Smuzhiyun 
2674*4882a593Smuzhiyun 	/* First enable the PCI device */
2675*4882a593Smuzhiyun 	ret = pcim_enable_device(pdev);
2676*4882a593Smuzhiyun 	if (ret != 0) {
2677*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to enable PCIe device\n");
2678*4882a593Smuzhiyun 		goto err_disable_aer;
2679*4882a593Smuzhiyun 	}
2680*4882a593Smuzhiyun 
2681*4882a593Smuzhiyun 	/*
2682*4882a593Smuzhiyun 	 * Enable the bus mastering, which effectively enables MSI IRQs and
2683*4882a593Smuzhiyun 	 * Request TLPs translation
2684*4882a593Smuzhiyun 	 */
2685*4882a593Smuzhiyun 	pci_set_master(pdev);
2686*4882a593Smuzhiyun 
2687*4882a593Smuzhiyun 	/* Request all BARs resources and map BAR0 only */
2688*4882a593Smuzhiyun 	ret = pcim_iomap_regions_request_all(pdev, 1, NTB_NAME);
2689*4882a593Smuzhiyun 	if (ret != 0) {
2690*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to request resources\n");
2691*4882a593Smuzhiyun 		goto err_clear_master;
2692*4882a593Smuzhiyun 	}
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 	/* Retrieve virtual address of BAR0 - PCI configuration space */
2695*4882a593Smuzhiyun 	ndev->cfgspc = pcim_iomap_table(pdev)[0];
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun 	/* Put the IDT driver data pointer to the PCI-device private pointer */
2698*4882a593Smuzhiyun 	pci_set_drvdata(pdev, ndev);
2699*4882a593Smuzhiyun 
2700*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NT-function PCIe interface initialized");
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun 	return 0;
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun err_clear_master:
2705*4882a593Smuzhiyun 	pci_clear_master(pdev);
2706*4882a593Smuzhiyun err_disable_aer:
2707*4882a593Smuzhiyun 	(void)pci_disable_pcie_error_reporting(pdev);
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun 	return ret;
2710*4882a593Smuzhiyun }
2711*4882a593Smuzhiyun 
2712*4882a593Smuzhiyun /*
2713*4882a593Smuzhiyun  * idt_deinit_pci() - deinitialize the basic PCI-related subsystem
2714*4882a593Smuzhiyun  * @ndev:	Pointer to the IDT PCIe-switch driver descriptor
2715*4882a593Smuzhiyun  *
2716*4882a593Smuzhiyun  * Managed resources will be freed on the driver detachment
2717*4882a593Smuzhiyun  */
idt_deinit_pci(struct idt_ntb_dev * ndev)2718*4882a593Smuzhiyun static void idt_deinit_pci(struct idt_ntb_dev *ndev)
2719*4882a593Smuzhiyun {
2720*4882a593Smuzhiyun 	struct pci_dev *pdev = ndev->ntb.pdev;
2721*4882a593Smuzhiyun 
2722*4882a593Smuzhiyun 	/* Clean up the PCI-device private data pointer */
2723*4882a593Smuzhiyun 	pci_set_drvdata(pdev, NULL);
2724*4882a593Smuzhiyun 
2725*4882a593Smuzhiyun 	/* Clear the bus master disabling the Request TLPs translation */
2726*4882a593Smuzhiyun 	pci_clear_master(pdev);
2727*4882a593Smuzhiyun 
2728*4882a593Smuzhiyun 	/* Disable the AER capability */
2729*4882a593Smuzhiyun 	(void)pci_disable_pcie_error_reporting(pdev);
2730*4882a593Smuzhiyun 
2731*4882a593Smuzhiyun 	dev_dbg(&pdev->dev, "NT-function PCIe interface cleared");
2732*4882a593Smuzhiyun }
2733*4882a593Smuzhiyun 
2734*4882a593Smuzhiyun /*===========================================================================
2735*4882a593Smuzhiyun  *                       12. PCI bus callback functions
2736*4882a593Smuzhiyun  *===========================================================================
2737*4882a593Smuzhiyun  */
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun /*
2740*4882a593Smuzhiyun  * idt_pci_probe() - PCI device probe callback
2741*4882a593Smuzhiyun  * @pdev:	Pointer to PCI device structure
2742*4882a593Smuzhiyun  * @id:		PCIe device custom descriptor
2743*4882a593Smuzhiyun  *
2744*4882a593Smuzhiyun  * Return: zero on success, otherwise negative error number
2745*4882a593Smuzhiyun  */
idt_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)2746*4882a593Smuzhiyun static int idt_pci_probe(struct pci_dev *pdev,
2747*4882a593Smuzhiyun 			 const struct pci_device_id *id)
2748*4882a593Smuzhiyun {
2749*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev;
2750*4882a593Smuzhiyun 	int ret;
2751*4882a593Smuzhiyun 
2752*4882a593Smuzhiyun 	/* Check whether IDT PCIe-switch is properly pre-initialized */
2753*4882a593Smuzhiyun 	ret = idt_check_setup(pdev);
2754*4882a593Smuzhiyun 	if (ret != 0)
2755*4882a593Smuzhiyun 		return ret;
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	/* Allocate the memory for IDT NTB device data */
2758*4882a593Smuzhiyun 	ndev = idt_create_dev(pdev, id);
2759*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(ndev))
2760*4882a593Smuzhiyun 		return PTR_ERR(ndev);
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 	/* Initialize the basic PCI subsystem of the device */
2763*4882a593Smuzhiyun 	ret = idt_init_pci(ndev);
2764*4882a593Smuzhiyun 	if (ret != 0)
2765*4882a593Smuzhiyun 		return ret;
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun 	/* Scan ports of the IDT PCIe-switch */
2768*4882a593Smuzhiyun 	(void)idt_scan_ports(ndev);
2769*4882a593Smuzhiyun 
2770*4882a593Smuzhiyun 	/* Initialize NTB link events subsystem */
2771*4882a593Smuzhiyun 	idt_init_link(ndev);
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 	/* Initialize MWs subsystem */
2774*4882a593Smuzhiyun 	ret = idt_init_mws(ndev);
2775*4882a593Smuzhiyun 	if (ret != 0)
2776*4882a593Smuzhiyun 		goto err_deinit_link;
2777*4882a593Smuzhiyun 
2778*4882a593Smuzhiyun 	/* Initialize Messaging subsystem */
2779*4882a593Smuzhiyun 	idt_init_msg(ndev);
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun 	/* Initialize hwmon interface */
2782*4882a593Smuzhiyun 	idt_init_temp(ndev);
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 	/* Initialize IDT interrupts handler */
2785*4882a593Smuzhiyun 	ret = idt_init_isr(ndev);
2786*4882a593Smuzhiyun 	if (ret != 0)
2787*4882a593Smuzhiyun 		goto err_deinit_link;
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	/* Register IDT NTB devices on the NTB bus */
2790*4882a593Smuzhiyun 	ret = idt_register_device(ndev);
2791*4882a593Smuzhiyun 	if (ret != 0)
2792*4882a593Smuzhiyun 		goto err_deinit_isr;
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun 	/* Initialize DebugFS info node */
2795*4882a593Smuzhiyun 	(void)idt_init_dbgfs(ndev);
2796*4882a593Smuzhiyun 
2797*4882a593Smuzhiyun 	/* IDT PCIe-switch NTB driver is finally initialized */
2798*4882a593Smuzhiyun 	dev_info(&pdev->dev, "IDT NTB device is ready");
2799*4882a593Smuzhiyun 
2800*4882a593Smuzhiyun 	/* May the force be with us... */
2801*4882a593Smuzhiyun 	return 0;
2802*4882a593Smuzhiyun 
2803*4882a593Smuzhiyun err_deinit_isr:
2804*4882a593Smuzhiyun 	idt_deinit_isr(ndev);
2805*4882a593Smuzhiyun err_deinit_link:
2806*4882a593Smuzhiyun 	idt_deinit_link(ndev);
2807*4882a593Smuzhiyun 	idt_deinit_pci(ndev);
2808*4882a593Smuzhiyun 
2809*4882a593Smuzhiyun 	return ret;
2810*4882a593Smuzhiyun }
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun /*
2813*4882a593Smuzhiyun  * idt_pci_probe() - PCI device remove callback
2814*4882a593Smuzhiyun  * @pdev:	Pointer to PCI device structure
2815*4882a593Smuzhiyun  */
idt_pci_remove(struct pci_dev * pdev)2816*4882a593Smuzhiyun static void idt_pci_remove(struct pci_dev *pdev)
2817*4882a593Smuzhiyun {
2818*4882a593Smuzhiyun 	struct idt_ntb_dev *ndev = pci_get_drvdata(pdev);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun 	/* Deinit the DebugFS node */
2821*4882a593Smuzhiyun 	idt_deinit_dbgfs(ndev);
2822*4882a593Smuzhiyun 
2823*4882a593Smuzhiyun 	/* Unregister NTB device */
2824*4882a593Smuzhiyun 	idt_unregister_device(ndev);
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 	/* Stop the interrupts handling */
2827*4882a593Smuzhiyun 	idt_deinit_isr(ndev);
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun 	/* Deinitialize link event subsystem */
2830*4882a593Smuzhiyun 	idt_deinit_link(ndev);
2831*4882a593Smuzhiyun 
2832*4882a593Smuzhiyun 	/* Deinit basic PCI subsystem */
2833*4882a593Smuzhiyun 	idt_deinit_pci(ndev);
2834*4882a593Smuzhiyun 
2835*4882a593Smuzhiyun 	/* IDT PCIe-switch NTB driver is finally initialized */
2836*4882a593Smuzhiyun 	dev_info(&pdev->dev, "IDT NTB device is removed");
2837*4882a593Smuzhiyun 
2838*4882a593Smuzhiyun 	/* Sayonara... */
2839*4882a593Smuzhiyun }
2840*4882a593Smuzhiyun 
2841*4882a593Smuzhiyun /*
2842*4882a593Smuzhiyun  * IDT PCIe-switch models ports configuration structures
2843*4882a593Smuzhiyun  */
2844*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes24nt6ag2_config = {
2845*4882a593Smuzhiyun 	.name = "89HPES24NT6AG2",
2846*4882a593Smuzhiyun 	.port_cnt = 6, .ports = {0, 2, 4, 6, 8, 12}
2847*4882a593Smuzhiyun };
2848*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes32nt8ag2_config = {
2849*4882a593Smuzhiyun 	.name = "89HPES32NT8AG2",
2850*4882a593Smuzhiyun 	.port_cnt = 8, .ports = {0, 2, 4, 6, 8, 12, 16, 20}
2851*4882a593Smuzhiyun };
2852*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes32nt8bg2_config = {
2853*4882a593Smuzhiyun 	.name = "89HPES32NT8BG2",
2854*4882a593Smuzhiyun 	.port_cnt = 8, .ports = {0, 2, 4, 6, 8, 12, 16, 20}
2855*4882a593Smuzhiyun };
2856*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes12nt12g2_config = {
2857*4882a593Smuzhiyun 	.name = "89HPES12NT12G2",
2858*4882a593Smuzhiyun 	.port_cnt = 3, .ports = {0, 8, 16}
2859*4882a593Smuzhiyun };
2860*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes16nt16g2_config = {
2861*4882a593Smuzhiyun 	.name = "89HPES16NT16G2",
2862*4882a593Smuzhiyun 	.port_cnt = 4, .ports = {0, 8, 12, 16}
2863*4882a593Smuzhiyun };
2864*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes24nt24g2_config = {
2865*4882a593Smuzhiyun 	.name = "89HPES24NT24G2",
2866*4882a593Smuzhiyun 	.port_cnt = 8, .ports = {0, 2, 4, 6, 8, 12, 16, 20}
2867*4882a593Smuzhiyun };
2868*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes32nt24ag2_config = {
2869*4882a593Smuzhiyun 	.name = "89HPES32NT24AG2",
2870*4882a593Smuzhiyun 	.port_cnt = 8, .ports = {0, 2, 4, 6, 8, 12, 16, 20}
2871*4882a593Smuzhiyun };
2872*4882a593Smuzhiyun static const struct idt_89hpes_cfg idt_89hpes32nt24bg2_config = {
2873*4882a593Smuzhiyun 	.name = "89HPES32NT24BG2",
2874*4882a593Smuzhiyun 	.port_cnt = 8, .ports = {0, 2, 4, 6, 8, 12, 16, 20}
2875*4882a593Smuzhiyun };
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun /*
2878*4882a593Smuzhiyun  * PCI-ids table of the supported IDT PCIe-switch devices
2879*4882a593Smuzhiyun  */
2880*4882a593Smuzhiyun static const struct pci_device_id idt_pci_tbl[] = {
2881*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES24NT6AG2,  idt_89hpes24nt6ag2_config)},
2882*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES32NT8AG2,  idt_89hpes32nt8ag2_config)},
2883*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES32NT8BG2,  idt_89hpes32nt8bg2_config)},
2884*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES12NT12G2,  idt_89hpes12nt12g2_config)},
2885*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES16NT16G2,  idt_89hpes16nt16g2_config)},
2886*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES24NT24G2,  idt_89hpes24nt24g2_config)},
2887*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES32NT24AG2, idt_89hpes32nt24ag2_config)},
2888*4882a593Smuzhiyun 	{IDT_PCI_DEVICE_IDS(89HPES32NT24BG2, idt_89hpes32nt24bg2_config)},
2889*4882a593Smuzhiyun 	{0}
2890*4882a593Smuzhiyun };
2891*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, idt_pci_tbl);
2892*4882a593Smuzhiyun 
2893*4882a593Smuzhiyun /*
2894*4882a593Smuzhiyun  * IDT PCIe-switch NT-function device driver structure definition
2895*4882a593Smuzhiyun  */
2896*4882a593Smuzhiyun static struct pci_driver idt_pci_driver = {
2897*4882a593Smuzhiyun 	.name		= KBUILD_MODNAME,
2898*4882a593Smuzhiyun 	.probe		= idt_pci_probe,
2899*4882a593Smuzhiyun 	.remove		= idt_pci_remove,
2900*4882a593Smuzhiyun 	.id_table	= idt_pci_tbl,
2901*4882a593Smuzhiyun };
2902*4882a593Smuzhiyun 
idt_pci_driver_init(void)2903*4882a593Smuzhiyun static int __init idt_pci_driver_init(void)
2904*4882a593Smuzhiyun {
2905*4882a593Smuzhiyun 	pr_info("%s %s\n", NTB_DESC, NTB_VER);
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun 	/* Create the top DebugFS directory if the FS is initialized */
2908*4882a593Smuzhiyun 	if (debugfs_initialized())
2909*4882a593Smuzhiyun 		dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2910*4882a593Smuzhiyun 
2911*4882a593Smuzhiyun 	/* Register the NTB hardware driver to handle the PCI device */
2912*4882a593Smuzhiyun 	return pci_register_driver(&idt_pci_driver);
2913*4882a593Smuzhiyun }
2914*4882a593Smuzhiyun module_init(idt_pci_driver_init);
2915*4882a593Smuzhiyun 
idt_pci_driver_exit(void)2916*4882a593Smuzhiyun static void __exit idt_pci_driver_exit(void)
2917*4882a593Smuzhiyun {
2918*4882a593Smuzhiyun 	/* Unregister the NTB hardware driver */
2919*4882a593Smuzhiyun 	pci_unregister_driver(&idt_pci_driver);
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	/* Discard the top DebugFS directory */
2922*4882a593Smuzhiyun 	debugfs_remove_recursive(dbgfs_topdir);
2923*4882a593Smuzhiyun }
2924*4882a593Smuzhiyun module_exit(idt_pci_driver_exit);
2925*4882a593Smuzhiyun 
2926