ntb_hw_idt.c 91 KB

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