ntb_hw_idt.c 85 KB

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