ntb_hw_intel.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  8. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * BSD LICENSE
  15. *
  16. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  17. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * * Redistributions in binary form must reproduce the above copy
  26. * notice, this list of conditions and the following disclaimer in
  27. * the documentation and/or other materials provided with the
  28. * distribution.
  29. * * Neither the name of Intel Corporation nor the names of its
  30. * contributors may be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * Intel PCIe NTB Linux driver
  46. *
  47. * Contact Information:
  48. * Jon Mason <jon.mason@intel.com>
  49. */
  50. #include <linux/debugfs.h>
  51. #include <linux/delay.h>
  52. #include <linux/init.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/module.h>
  55. #include <linux/pci.h>
  56. #include <linux/random.h>
  57. #include <linux/slab.h>
  58. #include <linux/ntb.h>
  59. #include "ntb_hw_intel.h"
  60. #define NTB_NAME "ntb_hw_intel"
  61. #define NTB_DESC "Intel(R) PCI-E Non-Transparent Bridge Driver"
  62. #define NTB_VER "2.0"
  63. MODULE_DESCRIPTION(NTB_DESC);
  64. MODULE_VERSION(NTB_VER);
  65. MODULE_LICENSE("Dual BSD/GPL");
  66. MODULE_AUTHOR("Intel Corporation");
  67. #define bar0_off(base, bar) ((base) + ((bar) << 2))
  68. #define bar2_off(base, bar) bar0_off(base, (bar) - 2)
  69. static const struct intel_ntb_reg atom_reg;
  70. static const struct intel_ntb_alt_reg atom_pri_reg;
  71. static const struct intel_ntb_alt_reg atom_sec_reg;
  72. static const struct intel_ntb_alt_reg atom_b2b_reg;
  73. static const struct intel_ntb_xlat_reg atom_pri_xlat;
  74. static const struct intel_ntb_xlat_reg atom_sec_xlat;
  75. static const struct intel_ntb_reg xeon_reg;
  76. static const struct intel_ntb_alt_reg xeon_pri_reg;
  77. static const struct intel_ntb_alt_reg xeon_sec_reg;
  78. static const struct intel_ntb_alt_reg xeon_b2b_reg;
  79. static const struct intel_ntb_xlat_reg xeon_pri_xlat;
  80. static const struct intel_ntb_xlat_reg xeon_sec_xlat;
  81. static struct intel_b2b_addr xeon_b2b_usd_addr;
  82. static struct intel_b2b_addr xeon_b2b_dsd_addr;
  83. static const struct intel_ntb_reg skx_reg;
  84. static const struct intel_ntb_alt_reg skx_pri_reg;
  85. static const struct intel_ntb_alt_reg skx_b2b_reg;
  86. static const struct intel_ntb_xlat_reg skx_sec_xlat;
  87. static const struct ntb_dev_ops intel_ntb_ops;
  88. static const struct ntb_dev_ops intel_ntb3_ops;
  89. static const struct file_operations intel_ntb_debugfs_info;
  90. static struct dentry *debugfs_dir;
  91. static int b2b_mw_idx = -1;
  92. module_param(b2b_mw_idx, int, 0644);
  93. MODULE_PARM_DESC(b2b_mw_idx, "Use this mw idx to access the peer ntb. A "
  94. "value of zero or positive starts from first mw idx, and a "
  95. "negative value starts from last mw idx. Both sides MUST "
  96. "set the same value here!");
  97. static unsigned int b2b_mw_share;
  98. module_param(b2b_mw_share, uint, 0644);
  99. MODULE_PARM_DESC(b2b_mw_share, "If the b2b mw is large enough, configure the "
  100. "ntb so that the peer ntb only occupies the first half of "
  101. "the mw, so the second half can still be used as a mw. Both "
  102. "sides MUST set the same value here!");
  103. module_param_named(xeon_b2b_usd_bar2_addr64,
  104. xeon_b2b_usd_addr.bar2_addr64, ullong, 0644);
  105. MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
  106. "XEON B2B USD BAR 2 64-bit address");
  107. module_param_named(xeon_b2b_usd_bar4_addr64,
  108. xeon_b2b_usd_addr.bar4_addr64, ullong, 0644);
  109. MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr64,
  110. "XEON B2B USD BAR 4 64-bit address");
  111. module_param_named(xeon_b2b_usd_bar4_addr32,
  112. xeon_b2b_usd_addr.bar4_addr32, ullong, 0644);
  113. MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr32,
  114. "XEON B2B USD split-BAR 4 32-bit address");
  115. module_param_named(xeon_b2b_usd_bar5_addr32,
  116. xeon_b2b_usd_addr.bar5_addr32, ullong, 0644);
  117. MODULE_PARM_DESC(xeon_b2b_usd_bar5_addr32,
  118. "XEON B2B USD split-BAR 5 32-bit address");
  119. module_param_named(xeon_b2b_dsd_bar2_addr64,
  120. xeon_b2b_dsd_addr.bar2_addr64, ullong, 0644);
  121. MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
  122. "XEON B2B DSD BAR 2 64-bit address");
  123. module_param_named(xeon_b2b_dsd_bar4_addr64,
  124. xeon_b2b_dsd_addr.bar4_addr64, ullong, 0644);
  125. MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr64,
  126. "XEON B2B DSD BAR 4 64-bit address");
  127. module_param_named(xeon_b2b_dsd_bar4_addr32,
  128. xeon_b2b_dsd_addr.bar4_addr32, ullong, 0644);
  129. MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr32,
  130. "XEON B2B DSD split-BAR 4 32-bit address");
  131. module_param_named(xeon_b2b_dsd_bar5_addr32,
  132. xeon_b2b_dsd_addr.bar5_addr32, ullong, 0644);
  133. MODULE_PARM_DESC(xeon_b2b_dsd_bar5_addr32,
  134. "XEON B2B DSD split-BAR 5 32-bit address");
  135. static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd);
  136. static int xeon_init_isr(struct intel_ntb_dev *ndev);
  137. #ifndef ioread64
  138. #ifdef readq
  139. #define ioread64 readq
  140. #else
  141. #define ioread64 _ioread64
  142. static inline u64 _ioread64(void __iomem *mmio)
  143. {
  144. u64 low, high;
  145. low = ioread32(mmio);
  146. high = ioread32(mmio + sizeof(u32));
  147. return low | (high << 32);
  148. }
  149. #endif
  150. #endif
  151. #ifndef iowrite64
  152. #ifdef writeq
  153. #define iowrite64 writeq
  154. #else
  155. #define iowrite64 _iowrite64
  156. static inline void _iowrite64(u64 val, void __iomem *mmio)
  157. {
  158. iowrite32(val, mmio);
  159. iowrite32(val >> 32, mmio + sizeof(u32));
  160. }
  161. #endif
  162. #endif
  163. static inline int pdev_is_atom(struct pci_dev *pdev)
  164. {
  165. switch (pdev->device) {
  166. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. static inline int pdev_is_xeon(struct pci_dev *pdev)
  172. {
  173. switch (pdev->device) {
  174. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  175. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  176. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  177. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  178. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  179. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  180. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  181. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  182. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  183. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  184. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  185. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  186. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  187. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  188. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. static inline int pdev_is_skx_xeon(struct pci_dev *pdev)
  194. {
  195. if (pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)
  196. return 1;
  197. return 0;
  198. }
  199. static inline void ndev_reset_unsafe_flags(struct intel_ntb_dev *ndev)
  200. {
  201. ndev->unsafe_flags = 0;
  202. ndev->unsafe_flags_ignore = 0;
  203. /* Only B2B has a workaround to avoid SDOORBELL */
  204. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP)
  205. if (!ntb_topo_is_b2b(ndev->ntb.topo))
  206. ndev->unsafe_flags |= NTB_UNSAFE_DB;
  207. /* No low level workaround to avoid SB01BASE */
  208. if (ndev->hwerr_flags & NTB_HWERR_SB01BASE_LOCKUP) {
  209. ndev->unsafe_flags |= NTB_UNSAFE_DB;
  210. ndev->unsafe_flags |= NTB_UNSAFE_SPAD;
  211. }
  212. }
  213. static inline int ndev_is_unsafe(struct intel_ntb_dev *ndev,
  214. unsigned long flag)
  215. {
  216. return !!(flag & ndev->unsafe_flags & ~ndev->unsafe_flags_ignore);
  217. }
  218. static inline int ndev_ignore_unsafe(struct intel_ntb_dev *ndev,
  219. unsigned long flag)
  220. {
  221. flag &= ndev->unsafe_flags;
  222. ndev->unsafe_flags_ignore |= flag;
  223. return !!flag;
  224. }
  225. static int ndev_mw_to_bar(struct intel_ntb_dev *ndev, int idx)
  226. {
  227. if (idx < 0 || idx >= ndev->mw_count)
  228. return -EINVAL;
  229. return ndev->reg->mw_bar[idx];
  230. }
  231. static inline int ndev_db_addr(struct intel_ntb_dev *ndev,
  232. phys_addr_t *db_addr, resource_size_t *db_size,
  233. phys_addr_t reg_addr, unsigned long reg)
  234. {
  235. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  236. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  237. if (db_addr) {
  238. *db_addr = reg_addr + reg;
  239. dev_dbg(ndev_dev(ndev), "Peer db addr %llx\n", *db_addr);
  240. }
  241. if (db_size) {
  242. *db_size = ndev->reg->db_size;
  243. dev_dbg(ndev_dev(ndev), "Peer db size %llx\n", *db_size);
  244. }
  245. return 0;
  246. }
  247. static inline u64 ndev_db_read(struct intel_ntb_dev *ndev,
  248. void __iomem *mmio)
  249. {
  250. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  251. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  252. return ndev->reg->db_ioread(mmio);
  253. }
  254. static inline int ndev_db_write(struct intel_ntb_dev *ndev, u64 db_bits,
  255. void __iomem *mmio)
  256. {
  257. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  258. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  259. if (db_bits & ~ndev->db_valid_mask)
  260. return -EINVAL;
  261. ndev->reg->db_iowrite(db_bits, mmio);
  262. return 0;
  263. }
  264. static inline int ndev_db_set_mask(struct intel_ntb_dev *ndev, u64 db_bits,
  265. void __iomem *mmio)
  266. {
  267. unsigned long irqflags;
  268. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  269. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  270. if (db_bits & ~ndev->db_valid_mask)
  271. return -EINVAL;
  272. spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
  273. {
  274. ndev->db_mask |= db_bits;
  275. ndev->reg->db_iowrite(ndev->db_mask, mmio);
  276. }
  277. spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
  278. return 0;
  279. }
  280. static inline int ndev_db_clear_mask(struct intel_ntb_dev *ndev, u64 db_bits,
  281. void __iomem *mmio)
  282. {
  283. unsigned long irqflags;
  284. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  285. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  286. if (db_bits & ~ndev->db_valid_mask)
  287. return -EINVAL;
  288. spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
  289. {
  290. ndev->db_mask &= ~db_bits;
  291. ndev->reg->db_iowrite(ndev->db_mask, mmio);
  292. }
  293. spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
  294. return 0;
  295. }
  296. static inline int ndev_vec_mask(struct intel_ntb_dev *ndev, int db_vector)
  297. {
  298. u64 shift, mask;
  299. shift = ndev->db_vec_shift;
  300. mask = BIT_ULL(shift) - 1;
  301. return mask << (shift * db_vector);
  302. }
  303. static inline int ndev_spad_addr(struct intel_ntb_dev *ndev, int idx,
  304. phys_addr_t *spad_addr, phys_addr_t reg_addr,
  305. unsigned long reg)
  306. {
  307. if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
  308. pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
  309. if (idx < 0 || idx >= ndev->spad_count)
  310. return -EINVAL;
  311. if (spad_addr) {
  312. *spad_addr = reg_addr + reg + (idx << 2);
  313. dev_dbg(ndev_dev(ndev), "Peer spad addr %llx\n", *spad_addr);
  314. }
  315. return 0;
  316. }
  317. static inline u32 ndev_spad_read(struct intel_ntb_dev *ndev, int idx,
  318. void __iomem *mmio)
  319. {
  320. if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
  321. pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
  322. if (idx < 0 || idx >= ndev->spad_count)
  323. return 0;
  324. return ioread32(mmio + (idx << 2));
  325. }
  326. static inline int ndev_spad_write(struct intel_ntb_dev *ndev, int idx, u32 val,
  327. void __iomem *mmio)
  328. {
  329. if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
  330. pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
  331. if (idx < 0 || idx >= ndev->spad_count)
  332. return -EINVAL;
  333. iowrite32(val, mmio + (idx << 2));
  334. return 0;
  335. }
  336. static irqreturn_t ndev_interrupt(struct intel_ntb_dev *ndev, int vec)
  337. {
  338. u64 vec_mask;
  339. vec_mask = ndev_vec_mask(ndev, vec);
  340. if ((ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) && (vec == 31))
  341. vec_mask |= ndev->db_link_mask;
  342. dev_dbg(ndev_dev(ndev), "vec %d vec_mask %llx\n", vec, vec_mask);
  343. ndev->last_ts = jiffies;
  344. if (vec_mask & ndev->db_link_mask) {
  345. if (ndev->reg->poll_link(ndev))
  346. ntb_link_event(&ndev->ntb);
  347. }
  348. if (vec_mask & ndev->db_valid_mask)
  349. ntb_db_event(&ndev->ntb, vec);
  350. return IRQ_HANDLED;
  351. }
  352. static irqreturn_t ndev_vec_isr(int irq, void *dev)
  353. {
  354. struct intel_ntb_vec *nvec = dev;
  355. dev_dbg(ndev_dev(nvec->ndev), "irq: %d nvec->num: %d\n",
  356. irq, nvec->num);
  357. return ndev_interrupt(nvec->ndev, nvec->num);
  358. }
  359. static irqreturn_t ndev_irq_isr(int irq, void *dev)
  360. {
  361. struct intel_ntb_dev *ndev = dev;
  362. return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
  363. }
  364. static int ndev_init_isr(struct intel_ntb_dev *ndev,
  365. int msix_min, int msix_max,
  366. int msix_shift, int total_shift)
  367. {
  368. struct pci_dev *pdev;
  369. int rc, i, msix_count, node;
  370. pdev = ndev_pdev(ndev);
  371. node = dev_to_node(&pdev->dev);
  372. /* Mask all doorbell interrupts */
  373. ndev->db_mask = ndev->db_valid_mask;
  374. ndev->reg->db_iowrite(ndev->db_mask,
  375. ndev->self_mmio +
  376. ndev->self_reg->db_mask);
  377. /* Try to set up msix irq */
  378. ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
  379. GFP_KERNEL, node);
  380. if (!ndev->vec)
  381. goto err_msix_vec_alloc;
  382. ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
  383. GFP_KERNEL, node);
  384. if (!ndev->msix)
  385. goto err_msix_alloc;
  386. for (i = 0; i < msix_max; ++i)
  387. ndev->msix[i].entry = i;
  388. msix_count = pci_enable_msix_range(pdev, ndev->msix,
  389. msix_min, msix_max);
  390. if (msix_count < 0)
  391. goto err_msix_enable;
  392. for (i = 0; i < msix_count; ++i) {
  393. ndev->vec[i].ndev = ndev;
  394. ndev->vec[i].num = i;
  395. rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
  396. "ndev_vec_isr", &ndev->vec[i]);
  397. if (rc)
  398. goto err_msix_request;
  399. }
  400. dev_dbg(ndev_dev(ndev), "Using %d msix interrupts\n", msix_count);
  401. ndev->db_vec_count = msix_count;
  402. ndev->db_vec_shift = msix_shift;
  403. return 0;
  404. err_msix_request:
  405. while (i-- > 0)
  406. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  407. pci_disable_msix(pdev);
  408. err_msix_enable:
  409. kfree(ndev->msix);
  410. err_msix_alloc:
  411. kfree(ndev->vec);
  412. err_msix_vec_alloc:
  413. ndev->msix = NULL;
  414. ndev->vec = NULL;
  415. /* Try to set up msi irq */
  416. rc = pci_enable_msi(pdev);
  417. if (rc)
  418. goto err_msi_enable;
  419. rc = request_irq(pdev->irq, ndev_irq_isr, 0,
  420. "ndev_irq_isr", ndev);
  421. if (rc)
  422. goto err_msi_request;
  423. dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
  424. ndev->db_vec_count = 1;
  425. ndev->db_vec_shift = total_shift;
  426. return 0;
  427. err_msi_request:
  428. pci_disable_msi(pdev);
  429. err_msi_enable:
  430. /* Try to set up intx irq */
  431. pci_intx(pdev, 1);
  432. rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
  433. "ndev_irq_isr", ndev);
  434. if (rc)
  435. goto err_intx_request;
  436. dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
  437. ndev->db_vec_count = 1;
  438. ndev->db_vec_shift = total_shift;
  439. return 0;
  440. err_intx_request:
  441. return rc;
  442. }
  443. static void ndev_deinit_isr(struct intel_ntb_dev *ndev)
  444. {
  445. struct pci_dev *pdev;
  446. int i;
  447. pdev = ndev_pdev(ndev);
  448. /* Mask all doorbell interrupts */
  449. ndev->db_mask = ndev->db_valid_mask;
  450. ndev->reg->db_iowrite(ndev->db_mask,
  451. ndev->self_mmio +
  452. ndev->self_reg->db_mask);
  453. if (ndev->msix) {
  454. i = ndev->db_vec_count;
  455. while (i--)
  456. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  457. pci_disable_msix(pdev);
  458. kfree(ndev->msix);
  459. kfree(ndev->vec);
  460. } else {
  461. free_irq(pdev->irq, ndev);
  462. if (pci_dev_msi_enabled(pdev))
  463. pci_disable_msi(pdev);
  464. }
  465. }
  466. static ssize_t ndev_ntb3_debugfs_read(struct file *filp, char __user *ubuf,
  467. size_t count, loff_t *offp)
  468. {
  469. struct intel_ntb_dev *ndev;
  470. void __iomem *mmio;
  471. char *buf;
  472. size_t buf_size;
  473. ssize_t ret, off;
  474. union { u64 v64; u32 v32; u16 v16; } u;
  475. ndev = filp->private_data;
  476. mmio = ndev->self_mmio;
  477. buf_size = min(count, 0x800ul);
  478. buf = kmalloc(buf_size, GFP_KERNEL);
  479. if (!buf)
  480. return -ENOMEM;
  481. off = 0;
  482. off += scnprintf(buf + off, buf_size - off,
  483. "NTB Device Information:\n");
  484. off += scnprintf(buf + off, buf_size - off,
  485. "Connection Topology -\t%s\n",
  486. ntb_topo_string(ndev->ntb.topo));
  487. off += scnprintf(buf + off, buf_size - off,
  488. "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
  489. off += scnprintf(buf + off, buf_size - off,
  490. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  491. if (!ndev->reg->link_is_up(ndev))
  492. off += scnprintf(buf + off, buf_size - off,
  493. "Link Status -\t\tDown\n");
  494. else {
  495. off += scnprintf(buf + off, buf_size - off,
  496. "Link Status -\t\tUp\n");
  497. off += scnprintf(buf + off, buf_size - off,
  498. "Link Speed -\t\tPCI-E Gen %u\n",
  499. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  500. off += scnprintf(buf + off, buf_size - off,
  501. "Link Width -\t\tx%u\n",
  502. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  503. }
  504. off += scnprintf(buf + off, buf_size - off,
  505. "Memory Window Count -\t%u\n", ndev->mw_count);
  506. off += scnprintf(buf + off, buf_size - off,
  507. "Scratchpad Count -\t%u\n", ndev->spad_count);
  508. off += scnprintf(buf + off, buf_size - off,
  509. "Doorbell Count -\t%u\n", ndev->db_count);
  510. off += scnprintf(buf + off, buf_size - off,
  511. "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
  512. off += scnprintf(buf + off, buf_size - off,
  513. "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
  514. off += scnprintf(buf + off, buf_size - off,
  515. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  516. off += scnprintf(buf + off, buf_size - off,
  517. "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
  518. off += scnprintf(buf + off, buf_size - off,
  519. "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
  520. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
  521. off += scnprintf(buf + off, buf_size - off,
  522. "Doorbell Mask -\t\t%#llx\n", u.v64);
  523. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
  524. off += scnprintf(buf + off, buf_size - off,
  525. "Doorbell Bell -\t\t%#llx\n", u.v64);
  526. off += scnprintf(buf + off, buf_size - off,
  527. "\nNTB Incoming XLAT:\n");
  528. u.v64 = ioread64(mmio + SKX_IMBAR1XBASE_OFFSET);
  529. off += scnprintf(buf + off, buf_size - off,
  530. "IMBAR1XBASE -\t\t%#018llx\n", u.v64);
  531. u.v64 = ioread64(mmio + SKX_IMBAR2XBASE_OFFSET);
  532. off += scnprintf(buf + off, buf_size - off,
  533. "IMBAR2XBASE -\t\t%#018llx\n", u.v64);
  534. u.v64 = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
  535. off += scnprintf(buf + off, buf_size - off,
  536. "IMBAR1XLMT -\t\t\t%#018llx\n", u.v64);
  537. u.v64 = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
  538. off += scnprintf(buf + off, buf_size - off,
  539. "IMBAR2XLMT -\t\t\t%#018llx\n", u.v64);
  540. if (ntb_topo_is_b2b(ndev->ntb.topo)) {
  541. off += scnprintf(buf + off, buf_size - off,
  542. "\nNTB Outgoing B2B XLAT:\n");
  543. u.v64 = ioread64(mmio + SKX_EMBAR1XBASE_OFFSET);
  544. off += scnprintf(buf + off, buf_size - off,
  545. "EMBAR1XBASE -\t\t%#018llx\n", u.v64);
  546. u.v64 = ioread64(mmio + SKX_EMBAR2XBASE_OFFSET);
  547. off += scnprintf(buf + off, buf_size - off,
  548. "EMBAR2XBASE -\t\t%#018llx\n", u.v64);
  549. u.v64 = ioread64(mmio + SKX_EMBAR1XLMT_OFFSET);
  550. off += scnprintf(buf + off, buf_size - off,
  551. "EMBAR1XLMT -\t\t%#018llx\n", u.v64);
  552. u.v64 = ioread64(mmio + SKX_EMBAR2XLMT_OFFSET);
  553. off += scnprintf(buf + off, buf_size - off,
  554. "EMBAR2XLMT -\t\t%#018llx\n", u.v64);
  555. off += scnprintf(buf + off, buf_size - off,
  556. "\nNTB Secondary BAR:\n");
  557. u.v64 = ioread64(mmio + SKX_EMBAR0_OFFSET);
  558. off += scnprintf(buf + off, buf_size - off,
  559. "EMBAR0 -\t\t%#018llx\n", u.v64);
  560. u.v64 = ioread64(mmio + SKX_EMBAR1_OFFSET);
  561. off += scnprintf(buf + off, buf_size - off,
  562. "EMBAR1 -\t\t%#018llx\n", u.v64);
  563. u.v64 = ioread64(mmio + SKX_EMBAR2_OFFSET);
  564. off += scnprintf(buf + off, buf_size - off,
  565. "EMBAR2 -\t\t%#018llx\n", u.v64);
  566. }
  567. off += scnprintf(buf + off, buf_size - off,
  568. "\nNTB Statistics:\n");
  569. u.v16 = ioread16(mmio + SKX_USMEMMISS_OFFSET);
  570. off += scnprintf(buf + off, buf_size - off,
  571. "Upstream Memory Miss -\t%u\n", u.v16);
  572. off += scnprintf(buf + off, buf_size - off,
  573. "\nNTB Hardware Errors:\n");
  574. if (!pci_read_config_word(ndev->ntb.pdev,
  575. SKX_DEVSTS_OFFSET, &u.v16))
  576. off += scnprintf(buf + off, buf_size - off,
  577. "DEVSTS -\t\t%#06x\n", u.v16);
  578. if (!pci_read_config_word(ndev->ntb.pdev,
  579. SKX_LINK_STATUS_OFFSET, &u.v16))
  580. off += scnprintf(buf + off, buf_size - off,
  581. "LNKSTS -\t\t%#06x\n", u.v16);
  582. if (!pci_read_config_dword(ndev->ntb.pdev,
  583. SKX_UNCERRSTS_OFFSET, &u.v32))
  584. off += scnprintf(buf + off, buf_size - off,
  585. "UNCERRSTS -\t\t%#06x\n", u.v32);
  586. if (!pci_read_config_dword(ndev->ntb.pdev,
  587. SKX_CORERRSTS_OFFSET, &u.v32))
  588. off += scnprintf(buf + off, buf_size - off,
  589. "CORERRSTS -\t\t%#06x\n", u.v32);
  590. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  591. kfree(buf);
  592. return ret;
  593. }
  594. static ssize_t ndev_ntb_debugfs_read(struct file *filp, char __user *ubuf,
  595. size_t count, loff_t *offp)
  596. {
  597. struct intel_ntb_dev *ndev;
  598. struct pci_dev *pdev;
  599. void __iomem *mmio;
  600. char *buf;
  601. size_t buf_size;
  602. ssize_t ret, off;
  603. union { u64 v64; u32 v32; u16 v16; u8 v8; } u;
  604. ndev = filp->private_data;
  605. pdev = ndev_pdev(ndev);
  606. mmio = ndev->self_mmio;
  607. buf_size = min(count, 0x800ul);
  608. buf = kmalloc(buf_size, GFP_KERNEL);
  609. if (!buf)
  610. return -ENOMEM;
  611. off = 0;
  612. off += scnprintf(buf + off, buf_size - off,
  613. "NTB Device Information:\n");
  614. off += scnprintf(buf + off, buf_size - off,
  615. "Connection Topology -\t%s\n",
  616. ntb_topo_string(ndev->ntb.topo));
  617. if (ndev->b2b_idx != UINT_MAX) {
  618. off += scnprintf(buf + off, buf_size - off,
  619. "B2B MW Idx -\t\t%u\n", ndev->b2b_idx);
  620. off += scnprintf(buf + off, buf_size - off,
  621. "B2B Offset -\t\t%#lx\n", ndev->b2b_off);
  622. }
  623. off += scnprintf(buf + off, buf_size - off,
  624. "BAR4 Split -\t\t%s\n",
  625. ndev->bar4_split ? "yes" : "no");
  626. off += scnprintf(buf + off, buf_size - off,
  627. "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
  628. off += scnprintf(buf + off, buf_size - off,
  629. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  630. if (!ndev->reg->link_is_up(ndev)) {
  631. off += scnprintf(buf + off, buf_size - off,
  632. "Link Status -\t\tDown\n");
  633. } else {
  634. off += scnprintf(buf + off, buf_size - off,
  635. "Link Status -\t\tUp\n");
  636. off += scnprintf(buf + off, buf_size - off,
  637. "Link Speed -\t\tPCI-E Gen %u\n",
  638. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  639. off += scnprintf(buf + off, buf_size - off,
  640. "Link Width -\t\tx%u\n",
  641. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  642. }
  643. off += scnprintf(buf + off, buf_size - off,
  644. "Memory Window Count -\t%u\n", ndev->mw_count);
  645. off += scnprintf(buf + off, buf_size - off,
  646. "Scratchpad Count -\t%u\n", ndev->spad_count);
  647. off += scnprintf(buf + off, buf_size - off,
  648. "Doorbell Count -\t%u\n", ndev->db_count);
  649. off += scnprintf(buf + off, buf_size - off,
  650. "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
  651. off += scnprintf(buf + off, buf_size - off,
  652. "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
  653. off += scnprintf(buf + off, buf_size - off,
  654. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  655. off += scnprintf(buf + off, buf_size - off,
  656. "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
  657. off += scnprintf(buf + off, buf_size - off,
  658. "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
  659. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
  660. off += scnprintf(buf + off, buf_size - off,
  661. "Doorbell Mask -\t\t%#llx\n", u.v64);
  662. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
  663. off += scnprintf(buf + off, buf_size - off,
  664. "Doorbell Bell -\t\t%#llx\n", u.v64);
  665. off += scnprintf(buf + off, buf_size - off,
  666. "\nNTB Window Size:\n");
  667. pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &u.v8);
  668. off += scnprintf(buf + off, buf_size - off,
  669. "PBAR23SZ %hhu\n", u.v8);
  670. if (!ndev->bar4_split) {
  671. pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &u.v8);
  672. off += scnprintf(buf + off, buf_size - off,
  673. "PBAR45SZ %hhu\n", u.v8);
  674. } else {
  675. pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &u.v8);
  676. off += scnprintf(buf + off, buf_size - off,
  677. "PBAR4SZ %hhu\n", u.v8);
  678. pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &u.v8);
  679. off += scnprintf(buf + off, buf_size - off,
  680. "PBAR5SZ %hhu\n", u.v8);
  681. }
  682. pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &u.v8);
  683. off += scnprintf(buf + off, buf_size - off,
  684. "SBAR23SZ %hhu\n", u.v8);
  685. if (!ndev->bar4_split) {
  686. pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &u.v8);
  687. off += scnprintf(buf + off, buf_size - off,
  688. "SBAR45SZ %hhu\n", u.v8);
  689. } else {
  690. pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &u.v8);
  691. off += scnprintf(buf + off, buf_size - off,
  692. "SBAR4SZ %hhu\n", u.v8);
  693. pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &u.v8);
  694. off += scnprintf(buf + off, buf_size - off,
  695. "SBAR5SZ %hhu\n", u.v8);
  696. }
  697. off += scnprintf(buf + off, buf_size - off,
  698. "\nNTB Incoming XLAT:\n");
  699. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 2));
  700. off += scnprintf(buf + off, buf_size - off,
  701. "XLAT23 -\t\t%#018llx\n", u.v64);
  702. if (ndev->bar4_split) {
  703. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
  704. off += scnprintf(buf + off, buf_size - off,
  705. "XLAT4 -\t\t\t%#06x\n", u.v32);
  706. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 5));
  707. off += scnprintf(buf + off, buf_size - off,
  708. "XLAT5 -\t\t\t%#06x\n", u.v32);
  709. } else {
  710. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
  711. off += scnprintf(buf + off, buf_size - off,
  712. "XLAT45 -\t\t%#018llx\n", u.v64);
  713. }
  714. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 2));
  715. off += scnprintf(buf + off, buf_size - off,
  716. "LMT23 -\t\t\t%#018llx\n", u.v64);
  717. if (ndev->bar4_split) {
  718. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
  719. off += scnprintf(buf + off, buf_size - off,
  720. "LMT4 -\t\t\t%#06x\n", u.v32);
  721. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 5));
  722. off += scnprintf(buf + off, buf_size - off,
  723. "LMT5 -\t\t\t%#06x\n", u.v32);
  724. } else {
  725. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
  726. off += scnprintf(buf + off, buf_size - off,
  727. "LMT45 -\t\t\t%#018llx\n", u.v64);
  728. }
  729. if (pdev_is_xeon(pdev)) {
  730. if (ntb_topo_is_b2b(ndev->ntb.topo)) {
  731. off += scnprintf(buf + off, buf_size - off,
  732. "\nNTB Outgoing B2B XLAT:\n");
  733. u.v64 = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
  734. off += scnprintf(buf + off, buf_size - off,
  735. "B2B XLAT23 -\t\t%#018llx\n", u.v64);
  736. if (ndev->bar4_split) {
  737. u.v32 = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
  738. off += scnprintf(buf + off, buf_size - off,
  739. "B2B XLAT4 -\t\t%#06x\n",
  740. u.v32);
  741. u.v32 = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
  742. off += scnprintf(buf + off, buf_size - off,
  743. "B2B XLAT5 -\t\t%#06x\n",
  744. u.v32);
  745. } else {
  746. u.v64 = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
  747. off += scnprintf(buf + off, buf_size - off,
  748. "B2B XLAT45 -\t\t%#018llx\n",
  749. u.v64);
  750. }
  751. u.v64 = ioread64(mmio + XEON_PBAR23LMT_OFFSET);
  752. off += scnprintf(buf + off, buf_size - off,
  753. "B2B LMT23 -\t\t%#018llx\n", u.v64);
  754. if (ndev->bar4_split) {
  755. u.v32 = ioread32(mmio + XEON_PBAR4LMT_OFFSET);
  756. off += scnprintf(buf + off, buf_size - off,
  757. "B2B LMT4 -\t\t%#06x\n",
  758. u.v32);
  759. u.v32 = ioread32(mmio + XEON_PBAR5LMT_OFFSET);
  760. off += scnprintf(buf + off, buf_size - off,
  761. "B2B LMT5 -\t\t%#06x\n",
  762. u.v32);
  763. } else {
  764. u.v64 = ioread64(mmio + XEON_PBAR45LMT_OFFSET);
  765. off += scnprintf(buf + off, buf_size - off,
  766. "B2B LMT45 -\t\t%#018llx\n",
  767. u.v64);
  768. }
  769. off += scnprintf(buf + off, buf_size - off,
  770. "\nNTB Secondary BAR:\n");
  771. u.v64 = ioread64(mmio + XEON_SBAR0BASE_OFFSET);
  772. off += scnprintf(buf + off, buf_size - off,
  773. "SBAR01 -\t\t%#018llx\n", u.v64);
  774. u.v64 = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
  775. off += scnprintf(buf + off, buf_size - off,
  776. "SBAR23 -\t\t%#018llx\n", u.v64);
  777. if (ndev->bar4_split) {
  778. u.v32 = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
  779. off += scnprintf(buf + off, buf_size - off,
  780. "SBAR4 -\t\t\t%#06x\n", u.v32);
  781. u.v32 = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
  782. off += scnprintf(buf + off, buf_size - off,
  783. "SBAR5 -\t\t\t%#06x\n", u.v32);
  784. } else {
  785. u.v64 = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
  786. off += scnprintf(buf + off, buf_size - off,
  787. "SBAR45 -\t\t%#018llx\n",
  788. u.v64);
  789. }
  790. }
  791. off += scnprintf(buf + off, buf_size - off,
  792. "\nXEON NTB Statistics:\n");
  793. u.v16 = ioread16(mmio + XEON_USMEMMISS_OFFSET);
  794. off += scnprintf(buf + off, buf_size - off,
  795. "Upstream Memory Miss -\t%u\n", u.v16);
  796. off += scnprintf(buf + off, buf_size - off,
  797. "\nXEON NTB Hardware Errors:\n");
  798. if (!pci_read_config_word(pdev,
  799. XEON_DEVSTS_OFFSET, &u.v16))
  800. off += scnprintf(buf + off, buf_size - off,
  801. "DEVSTS -\t\t%#06x\n", u.v16);
  802. if (!pci_read_config_word(pdev,
  803. XEON_LINK_STATUS_OFFSET, &u.v16))
  804. off += scnprintf(buf + off, buf_size - off,
  805. "LNKSTS -\t\t%#06x\n", u.v16);
  806. if (!pci_read_config_dword(pdev,
  807. XEON_UNCERRSTS_OFFSET, &u.v32))
  808. off += scnprintf(buf + off, buf_size - off,
  809. "UNCERRSTS -\t\t%#06x\n", u.v32);
  810. if (!pci_read_config_dword(pdev,
  811. XEON_CORERRSTS_OFFSET, &u.v32))
  812. off += scnprintf(buf + off, buf_size - off,
  813. "CORERRSTS -\t\t%#06x\n", u.v32);
  814. }
  815. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  816. kfree(buf);
  817. return ret;
  818. }
  819. static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
  820. size_t count, loff_t *offp)
  821. {
  822. struct intel_ntb_dev *ndev = filp->private_data;
  823. if (pdev_is_xeon(ndev->ntb.pdev) ||
  824. pdev_is_atom(ndev->ntb.pdev))
  825. return ndev_ntb_debugfs_read(filp, ubuf, count, offp);
  826. else if (pdev_is_skx_xeon(ndev->ntb.pdev))
  827. return ndev_ntb3_debugfs_read(filp, ubuf, count, offp);
  828. return -ENXIO;
  829. }
  830. static void ndev_init_debugfs(struct intel_ntb_dev *ndev)
  831. {
  832. if (!debugfs_dir) {
  833. ndev->debugfs_dir = NULL;
  834. ndev->debugfs_info = NULL;
  835. } else {
  836. ndev->debugfs_dir =
  837. debugfs_create_dir(ndev_name(ndev), debugfs_dir);
  838. if (!ndev->debugfs_dir)
  839. ndev->debugfs_info = NULL;
  840. else
  841. ndev->debugfs_info =
  842. debugfs_create_file("info", S_IRUSR,
  843. ndev->debugfs_dir, ndev,
  844. &intel_ntb_debugfs_info);
  845. }
  846. }
  847. static void ndev_deinit_debugfs(struct intel_ntb_dev *ndev)
  848. {
  849. debugfs_remove_recursive(ndev->debugfs_dir);
  850. }
  851. static int intel_ntb_mw_count(struct ntb_dev *ntb)
  852. {
  853. return ntb_ndev(ntb)->mw_count;
  854. }
  855. static int intel_ntb_mw_get_range(struct ntb_dev *ntb, int idx,
  856. phys_addr_t *base,
  857. resource_size_t *size,
  858. resource_size_t *align,
  859. resource_size_t *align_size)
  860. {
  861. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  862. int bar;
  863. if (idx >= ndev->b2b_idx && !ndev->b2b_off)
  864. idx += 1;
  865. bar = ndev_mw_to_bar(ndev, idx);
  866. if (bar < 0)
  867. return bar;
  868. if (base)
  869. *base = pci_resource_start(ndev->ntb.pdev, bar) +
  870. (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
  871. if (size)
  872. *size = pci_resource_len(ndev->ntb.pdev, bar) -
  873. (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
  874. if (align)
  875. *align = pci_resource_len(ndev->ntb.pdev, bar);
  876. if (align_size)
  877. *align_size = 1;
  878. return 0;
  879. }
  880. static int intel_ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
  881. dma_addr_t addr, resource_size_t size)
  882. {
  883. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  884. unsigned long base_reg, xlat_reg, limit_reg;
  885. resource_size_t bar_size, mw_size;
  886. void __iomem *mmio;
  887. u64 base, limit, reg_val;
  888. int bar;
  889. if (idx >= ndev->b2b_idx && !ndev->b2b_off)
  890. idx += 1;
  891. bar = ndev_mw_to_bar(ndev, idx);
  892. if (bar < 0)
  893. return bar;
  894. bar_size = pci_resource_len(ndev->ntb.pdev, bar);
  895. if (idx == ndev->b2b_idx)
  896. mw_size = bar_size - ndev->b2b_off;
  897. else
  898. mw_size = bar_size;
  899. /* hardware requires that addr is aligned to bar size */
  900. if (addr & (bar_size - 1))
  901. return -EINVAL;
  902. /* make sure the range fits in the usable mw size */
  903. if (size > mw_size)
  904. return -EINVAL;
  905. mmio = ndev->self_mmio;
  906. base_reg = bar0_off(ndev->xlat_reg->bar0_base, bar);
  907. xlat_reg = bar2_off(ndev->xlat_reg->bar2_xlat, bar);
  908. limit_reg = bar2_off(ndev->xlat_reg->bar2_limit, bar);
  909. if (bar < 4 || !ndev->bar4_split) {
  910. base = ioread64(mmio + base_reg) & NTB_BAR_MASK_64;
  911. /* Set the limit if supported, if size is not mw_size */
  912. if (limit_reg && size != mw_size)
  913. limit = base + size;
  914. else
  915. limit = 0;
  916. /* set and verify setting the translation address */
  917. iowrite64(addr, mmio + xlat_reg);
  918. reg_val = ioread64(mmio + xlat_reg);
  919. if (reg_val != addr) {
  920. iowrite64(0, mmio + xlat_reg);
  921. return -EIO;
  922. }
  923. /* set and verify setting the limit */
  924. iowrite64(limit, mmio + limit_reg);
  925. reg_val = ioread64(mmio + limit_reg);
  926. if (reg_val != limit) {
  927. iowrite64(base, mmio + limit_reg);
  928. iowrite64(0, mmio + xlat_reg);
  929. return -EIO;
  930. }
  931. } else {
  932. /* split bar addr range must all be 32 bit */
  933. if (addr & (~0ull << 32))
  934. return -EINVAL;
  935. if ((addr + size) & (~0ull << 32))
  936. return -EINVAL;
  937. base = ioread32(mmio + base_reg) & NTB_BAR_MASK_32;
  938. /* Set the limit if supported, if size is not mw_size */
  939. if (limit_reg && size != mw_size)
  940. limit = base + size;
  941. else
  942. limit = 0;
  943. /* set and verify setting the translation address */
  944. iowrite32(addr, mmio + xlat_reg);
  945. reg_val = ioread32(mmio + xlat_reg);
  946. if (reg_val != addr) {
  947. iowrite32(0, mmio + xlat_reg);
  948. return -EIO;
  949. }
  950. /* set and verify setting the limit */
  951. iowrite32(limit, mmio + limit_reg);
  952. reg_val = ioread32(mmio + limit_reg);
  953. if (reg_val != limit) {
  954. iowrite32(base, mmio + limit_reg);
  955. iowrite32(0, mmio + xlat_reg);
  956. return -EIO;
  957. }
  958. }
  959. return 0;
  960. }
  961. static int intel_ntb_link_is_up(struct ntb_dev *ntb,
  962. enum ntb_speed *speed,
  963. enum ntb_width *width)
  964. {
  965. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  966. if (ndev->reg->link_is_up(ndev)) {
  967. if (speed)
  968. *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
  969. if (width)
  970. *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
  971. return 1;
  972. } else {
  973. /* TODO MAYBE: is it possible to observe the link speed and
  974. * width while link is training? */
  975. if (speed)
  976. *speed = NTB_SPEED_NONE;
  977. if (width)
  978. *width = NTB_WIDTH_NONE;
  979. return 0;
  980. }
  981. }
  982. static int intel_ntb_link_enable(struct ntb_dev *ntb,
  983. enum ntb_speed max_speed,
  984. enum ntb_width max_width)
  985. {
  986. struct intel_ntb_dev *ndev;
  987. u32 ntb_ctl;
  988. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  989. if (ndev->ntb.topo == NTB_TOPO_SEC)
  990. return -EINVAL;
  991. dev_dbg(ndev_dev(ndev),
  992. "Enabling link with max_speed %d max_width %d\n",
  993. max_speed, max_width);
  994. if (max_speed != NTB_SPEED_AUTO)
  995. dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
  996. if (max_width != NTB_WIDTH_AUTO)
  997. dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
  998. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  999. ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
  1000. ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
  1001. ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
  1002. if (ndev->bar4_split)
  1003. ntb_ctl |= NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP;
  1004. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1005. return 0;
  1006. }
  1007. static int intel_ntb_link_disable(struct ntb_dev *ntb)
  1008. {
  1009. struct intel_ntb_dev *ndev;
  1010. u32 ntb_cntl;
  1011. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  1012. if (ndev->ntb.topo == NTB_TOPO_SEC)
  1013. return -EINVAL;
  1014. dev_dbg(ndev_dev(ndev), "Disabling link\n");
  1015. /* Bring NTB link down */
  1016. ntb_cntl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  1017. ntb_cntl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
  1018. ntb_cntl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
  1019. if (ndev->bar4_split)
  1020. ntb_cntl &= ~(NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP);
  1021. ntb_cntl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
  1022. iowrite32(ntb_cntl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1023. return 0;
  1024. }
  1025. static int intel_ntb_db_is_unsafe(struct ntb_dev *ntb)
  1026. {
  1027. return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_DB);
  1028. }
  1029. static u64 intel_ntb_db_valid_mask(struct ntb_dev *ntb)
  1030. {
  1031. return ntb_ndev(ntb)->db_valid_mask;
  1032. }
  1033. static int intel_ntb_db_vector_count(struct ntb_dev *ntb)
  1034. {
  1035. struct intel_ntb_dev *ndev;
  1036. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  1037. return ndev->db_vec_count;
  1038. }
  1039. static u64 intel_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
  1040. {
  1041. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1042. if (db_vector < 0 || db_vector > ndev->db_vec_count)
  1043. return 0;
  1044. return ndev->db_valid_mask & ndev_vec_mask(ndev, db_vector);
  1045. }
  1046. static u64 intel_ntb_db_read(struct ntb_dev *ntb)
  1047. {
  1048. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1049. return ndev_db_read(ndev,
  1050. ndev->self_mmio +
  1051. ndev->self_reg->db_bell);
  1052. }
  1053. static int intel_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  1054. {
  1055. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1056. return ndev_db_write(ndev, db_bits,
  1057. ndev->self_mmio +
  1058. ndev->self_reg->db_bell);
  1059. }
  1060. static int intel_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  1061. {
  1062. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1063. return ndev_db_set_mask(ndev, db_bits,
  1064. ndev->self_mmio +
  1065. ndev->self_reg->db_mask);
  1066. }
  1067. static int intel_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  1068. {
  1069. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1070. return ndev_db_clear_mask(ndev, db_bits,
  1071. ndev->self_mmio +
  1072. ndev->self_reg->db_mask);
  1073. }
  1074. static int intel_ntb_peer_db_addr(struct ntb_dev *ntb,
  1075. phys_addr_t *db_addr,
  1076. resource_size_t *db_size)
  1077. {
  1078. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1079. return ndev_db_addr(ndev, db_addr, db_size, ndev->peer_addr,
  1080. ndev->peer_reg->db_bell);
  1081. }
  1082. static int intel_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  1083. {
  1084. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1085. return ndev_db_write(ndev, db_bits,
  1086. ndev->peer_mmio +
  1087. ndev->peer_reg->db_bell);
  1088. }
  1089. static int intel_ntb_spad_is_unsafe(struct ntb_dev *ntb)
  1090. {
  1091. return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_SPAD);
  1092. }
  1093. static int intel_ntb_spad_count(struct ntb_dev *ntb)
  1094. {
  1095. struct intel_ntb_dev *ndev;
  1096. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  1097. return ndev->spad_count;
  1098. }
  1099. static u32 intel_ntb_spad_read(struct ntb_dev *ntb, int idx)
  1100. {
  1101. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1102. return ndev_spad_read(ndev, idx,
  1103. ndev->self_mmio +
  1104. ndev->self_reg->spad);
  1105. }
  1106. static int intel_ntb_spad_write(struct ntb_dev *ntb,
  1107. int idx, u32 val)
  1108. {
  1109. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1110. return ndev_spad_write(ndev, idx, val,
  1111. ndev->self_mmio +
  1112. ndev->self_reg->spad);
  1113. }
  1114. static int intel_ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
  1115. phys_addr_t *spad_addr)
  1116. {
  1117. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1118. return ndev_spad_addr(ndev, idx, spad_addr, ndev->peer_addr,
  1119. ndev->peer_reg->spad);
  1120. }
  1121. static u32 intel_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
  1122. {
  1123. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1124. return ndev_spad_read(ndev, idx,
  1125. ndev->peer_mmio +
  1126. ndev->peer_reg->spad);
  1127. }
  1128. static int intel_ntb_peer_spad_write(struct ntb_dev *ntb,
  1129. int idx, u32 val)
  1130. {
  1131. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1132. return ndev_spad_write(ndev, idx, val,
  1133. ndev->peer_mmio +
  1134. ndev->peer_reg->spad);
  1135. }
  1136. /* ATOM */
  1137. static u64 atom_db_ioread(void __iomem *mmio)
  1138. {
  1139. return ioread64(mmio);
  1140. }
  1141. static void atom_db_iowrite(u64 bits, void __iomem *mmio)
  1142. {
  1143. iowrite64(bits, mmio);
  1144. }
  1145. static int atom_poll_link(struct intel_ntb_dev *ndev)
  1146. {
  1147. u32 ntb_ctl;
  1148. ntb_ctl = ioread32(ndev->self_mmio + ATOM_NTBCNTL_OFFSET);
  1149. if (ntb_ctl == ndev->ntb_ctl)
  1150. return 0;
  1151. ndev->ntb_ctl = ntb_ctl;
  1152. ndev->lnk_sta = ioread32(ndev->self_mmio + ATOM_LINK_STATUS_OFFSET);
  1153. return 1;
  1154. }
  1155. static int atom_link_is_up(struct intel_ntb_dev *ndev)
  1156. {
  1157. return ATOM_NTB_CTL_ACTIVE(ndev->ntb_ctl);
  1158. }
  1159. static int atom_link_is_err(struct intel_ntb_dev *ndev)
  1160. {
  1161. if (ioread32(ndev->self_mmio + ATOM_LTSSMSTATEJMP_OFFSET)
  1162. & ATOM_LTSSMSTATEJMP_FORCEDETECT)
  1163. return 1;
  1164. if (ioread32(ndev->self_mmio + ATOM_IBSTERRRCRVSTS0_OFFSET)
  1165. & ATOM_IBIST_ERR_OFLOW)
  1166. return 1;
  1167. return 0;
  1168. }
  1169. static inline enum ntb_topo atom_ppd_topo(struct intel_ntb_dev *ndev, u32 ppd)
  1170. {
  1171. switch (ppd & ATOM_PPD_TOPO_MASK) {
  1172. case ATOM_PPD_TOPO_B2B_USD:
  1173. dev_dbg(ndev_dev(ndev), "PPD %d B2B USD\n", ppd);
  1174. return NTB_TOPO_B2B_USD;
  1175. case ATOM_PPD_TOPO_B2B_DSD:
  1176. dev_dbg(ndev_dev(ndev), "PPD %d B2B DSD\n", ppd);
  1177. return NTB_TOPO_B2B_DSD;
  1178. case ATOM_PPD_TOPO_PRI_USD:
  1179. case ATOM_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
  1180. case ATOM_PPD_TOPO_SEC_USD:
  1181. case ATOM_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
  1182. dev_dbg(ndev_dev(ndev), "PPD %d non B2B disabled\n", ppd);
  1183. return NTB_TOPO_NONE;
  1184. }
  1185. dev_dbg(ndev_dev(ndev), "PPD %d invalid\n", ppd);
  1186. return NTB_TOPO_NONE;
  1187. }
  1188. static void atom_link_hb(struct work_struct *work)
  1189. {
  1190. struct intel_ntb_dev *ndev = hb_ndev(work);
  1191. unsigned long poll_ts;
  1192. void __iomem *mmio;
  1193. u32 status32;
  1194. poll_ts = ndev->last_ts + ATOM_LINK_HB_TIMEOUT;
  1195. /* Delay polling the link status if an interrupt was received,
  1196. * unless the cached link status says the link is down.
  1197. */
  1198. if (time_after(poll_ts, jiffies) && atom_link_is_up(ndev)) {
  1199. schedule_delayed_work(&ndev->hb_timer, poll_ts - jiffies);
  1200. return;
  1201. }
  1202. if (atom_poll_link(ndev))
  1203. ntb_link_event(&ndev->ntb);
  1204. if (atom_link_is_up(ndev) || !atom_link_is_err(ndev)) {
  1205. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
  1206. return;
  1207. }
  1208. /* Link is down with error: recover the link! */
  1209. mmio = ndev->self_mmio;
  1210. /* Driver resets the NTB ModPhy lanes - magic! */
  1211. iowrite8(0xe0, mmio + ATOM_MODPHY_PCSREG6);
  1212. iowrite8(0x40, mmio + ATOM_MODPHY_PCSREG4);
  1213. iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG4);
  1214. iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG6);
  1215. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  1216. msleep(100);
  1217. /* Clear AER Errors, write to clear */
  1218. status32 = ioread32(mmio + ATOM_ERRCORSTS_OFFSET);
  1219. dev_dbg(ndev_dev(ndev), "ERRCORSTS = %x\n", status32);
  1220. status32 &= PCI_ERR_COR_REP_ROLL;
  1221. iowrite32(status32, mmio + ATOM_ERRCORSTS_OFFSET);
  1222. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  1223. status32 = ioread32(mmio + ATOM_LTSSMERRSTS0_OFFSET);
  1224. dev_dbg(ndev_dev(ndev), "LTSSMERRSTS0 = %x\n", status32);
  1225. status32 |= ATOM_LTSSMERRSTS0_UNEXPECTEDEI;
  1226. iowrite32(status32, mmio + ATOM_LTSSMERRSTS0_OFFSET);
  1227. /* Clear DeSkew Buffer error, write to clear */
  1228. status32 = ioread32(mmio + ATOM_DESKEWSTS_OFFSET);
  1229. dev_dbg(ndev_dev(ndev), "DESKEWSTS = %x\n", status32);
  1230. status32 |= ATOM_DESKEWSTS_DBERR;
  1231. iowrite32(status32, mmio + ATOM_DESKEWSTS_OFFSET);
  1232. status32 = ioread32(mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
  1233. dev_dbg(ndev_dev(ndev), "IBSTERRRCRVSTS0 = %x\n", status32);
  1234. status32 &= ATOM_IBIST_ERR_OFLOW;
  1235. iowrite32(status32, mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
  1236. /* Releases the NTB state machine to allow the link to retrain */
  1237. status32 = ioread32(mmio + ATOM_LTSSMSTATEJMP_OFFSET);
  1238. dev_dbg(ndev_dev(ndev), "LTSSMSTATEJMP = %x\n", status32);
  1239. status32 &= ~ATOM_LTSSMSTATEJMP_FORCEDETECT;
  1240. iowrite32(status32, mmio + ATOM_LTSSMSTATEJMP_OFFSET);
  1241. /* There is a potential race between the 2 NTB devices recovering at the
  1242. * same time. If the times are the same, the link will not recover and
  1243. * the driver will be stuck in this loop forever. Add a random interval
  1244. * to the recovery time to prevent this race.
  1245. */
  1246. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_RECOVERY_TIME
  1247. + prandom_u32() % ATOM_LINK_RECOVERY_TIME);
  1248. }
  1249. static int atom_init_isr(struct intel_ntb_dev *ndev)
  1250. {
  1251. int rc;
  1252. rc = ndev_init_isr(ndev, 1, ATOM_DB_MSIX_VECTOR_COUNT,
  1253. ATOM_DB_MSIX_VECTOR_SHIFT, ATOM_DB_TOTAL_SHIFT);
  1254. if (rc)
  1255. return rc;
  1256. /* ATOM doesn't have link status interrupt, poll on that platform */
  1257. ndev->last_ts = jiffies;
  1258. INIT_DELAYED_WORK(&ndev->hb_timer, atom_link_hb);
  1259. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
  1260. return 0;
  1261. }
  1262. static void atom_deinit_isr(struct intel_ntb_dev *ndev)
  1263. {
  1264. cancel_delayed_work_sync(&ndev->hb_timer);
  1265. ndev_deinit_isr(ndev);
  1266. }
  1267. static int atom_init_ntb(struct intel_ntb_dev *ndev)
  1268. {
  1269. ndev->mw_count = ATOM_MW_COUNT;
  1270. ndev->spad_count = ATOM_SPAD_COUNT;
  1271. ndev->db_count = ATOM_DB_COUNT;
  1272. switch (ndev->ntb.topo) {
  1273. case NTB_TOPO_B2B_USD:
  1274. case NTB_TOPO_B2B_DSD:
  1275. ndev->self_reg = &atom_pri_reg;
  1276. ndev->peer_reg = &atom_b2b_reg;
  1277. ndev->xlat_reg = &atom_sec_xlat;
  1278. /* Enable Bus Master and Memory Space on the secondary side */
  1279. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1280. ndev->self_mmio + ATOM_SPCICMD_OFFSET);
  1281. break;
  1282. default:
  1283. return -EINVAL;
  1284. }
  1285. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1286. return 0;
  1287. }
  1288. static int atom_init_dev(struct intel_ntb_dev *ndev)
  1289. {
  1290. u32 ppd;
  1291. int rc;
  1292. rc = pci_read_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET, &ppd);
  1293. if (rc)
  1294. return -EIO;
  1295. ndev->ntb.topo = atom_ppd_topo(ndev, ppd);
  1296. if (ndev->ntb.topo == NTB_TOPO_NONE)
  1297. return -EINVAL;
  1298. rc = atom_init_ntb(ndev);
  1299. if (rc)
  1300. return rc;
  1301. rc = atom_init_isr(ndev);
  1302. if (rc)
  1303. return rc;
  1304. if (ndev->ntb.topo != NTB_TOPO_SEC) {
  1305. /* Initiate PCI-E link training */
  1306. rc = pci_write_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET,
  1307. ppd | ATOM_PPD_INIT_LINK);
  1308. if (rc)
  1309. return rc;
  1310. }
  1311. return 0;
  1312. }
  1313. static void atom_deinit_dev(struct intel_ntb_dev *ndev)
  1314. {
  1315. atom_deinit_isr(ndev);
  1316. }
  1317. /* Skylake Xeon NTB */
  1318. static int skx_poll_link(struct intel_ntb_dev *ndev)
  1319. {
  1320. u16 reg_val;
  1321. int rc;
  1322. ndev->reg->db_iowrite(ndev->db_link_mask,
  1323. ndev->self_mmio +
  1324. ndev->self_reg->db_clear);
  1325. rc = pci_read_config_word(ndev->ntb.pdev,
  1326. SKX_LINK_STATUS_OFFSET, &reg_val);
  1327. if (rc)
  1328. return 0;
  1329. if (reg_val == ndev->lnk_sta)
  1330. return 0;
  1331. ndev->lnk_sta = reg_val;
  1332. return 1;
  1333. }
  1334. static u64 skx_db_ioread(void __iomem *mmio)
  1335. {
  1336. return ioread64(mmio);
  1337. }
  1338. static void skx_db_iowrite(u64 bits, void __iomem *mmio)
  1339. {
  1340. iowrite64(bits, mmio);
  1341. }
  1342. static int skx_init_isr(struct intel_ntb_dev *ndev)
  1343. {
  1344. int i;
  1345. /*
  1346. * The MSIX vectors and the interrupt status bits are not lined up
  1347. * on Skylake. By default the link status bit is bit 32, however it
  1348. * is by default MSIX vector0. We need to fixup to line them up.
  1349. * The vectors at reset is 1-32,0. We need to reprogram to 0-32.
  1350. */
  1351. for (i = 0; i < SKX_DB_MSIX_VECTOR_COUNT; i++)
  1352. iowrite8(i, ndev->self_mmio + SKX_INTVEC_OFFSET + i);
  1353. /* move link status down one as workaround */
  1354. if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) {
  1355. iowrite8(SKX_DB_MSIX_VECTOR_COUNT - 2,
  1356. ndev->self_mmio + SKX_INTVEC_OFFSET +
  1357. (SKX_DB_MSIX_VECTOR_COUNT - 1));
  1358. }
  1359. return ndev_init_isr(ndev, SKX_DB_MSIX_VECTOR_COUNT,
  1360. SKX_DB_MSIX_VECTOR_COUNT,
  1361. SKX_DB_MSIX_VECTOR_SHIFT,
  1362. SKX_DB_TOTAL_SHIFT);
  1363. }
  1364. static int skx_setup_b2b_mw(struct intel_ntb_dev *ndev,
  1365. const struct intel_b2b_addr *addr,
  1366. const struct intel_b2b_addr *peer_addr)
  1367. {
  1368. struct pci_dev *pdev;
  1369. void __iomem *mmio;
  1370. resource_size_t bar_size;
  1371. phys_addr_t bar_addr;
  1372. int b2b_bar;
  1373. u8 bar_sz;
  1374. pdev = ndev_pdev(ndev);
  1375. mmio = ndev->self_mmio;
  1376. if (ndev->b2b_idx == UINT_MAX) {
  1377. dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
  1378. b2b_bar = 0;
  1379. ndev->b2b_off = 0;
  1380. } else {
  1381. b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
  1382. if (b2b_bar < 0)
  1383. return -EIO;
  1384. dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
  1385. bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
  1386. dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
  1387. if (b2b_mw_share && ((bar_size >> 1) >= XEON_B2B_MIN_SIZE)) {
  1388. dev_dbg(ndev_dev(ndev),
  1389. "b2b using first half of bar\n");
  1390. ndev->b2b_off = bar_size >> 1;
  1391. } else if (bar_size >= XEON_B2B_MIN_SIZE) {
  1392. dev_dbg(ndev_dev(ndev),
  1393. "b2b using whole bar\n");
  1394. ndev->b2b_off = 0;
  1395. --ndev->mw_count;
  1396. } else {
  1397. dev_dbg(ndev_dev(ndev),
  1398. "b2b bar size is too small\n");
  1399. return -EIO;
  1400. }
  1401. }
  1402. /*
  1403. * Reset the secondary bar sizes to match the primary bar sizes,
  1404. * except disable or halve the size of the b2b secondary bar.
  1405. */
  1406. pci_read_config_byte(pdev, SKX_IMBAR1SZ_OFFSET, &bar_sz);
  1407. dev_dbg(ndev_dev(ndev), "IMBAR1SZ %#x\n", bar_sz);
  1408. if (b2b_bar == 1) {
  1409. if (ndev->b2b_off)
  1410. bar_sz -= 1;
  1411. else
  1412. bar_sz = 0;
  1413. }
  1414. pci_write_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, bar_sz);
  1415. pci_read_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, &bar_sz);
  1416. dev_dbg(ndev_dev(ndev), "EMBAR1SZ %#x\n", bar_sz);
  1417. pci_read_config_byte(pdev, SKX_IMBAR2SZ_OFFSET, &bar_sz);
  1418. dev_dbg(ndev_dev(ndev), "IMBAR2SZ %#x\n", bar_sz);
  1419. if (b2b_bar == 2) {
  1420. if (ndev->b2b_off)
  1421. bar_sz -= 1;
  1422. else
  1423. bar_sz = 0;
  1424. }
  1425. pci_write_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, bar_sz);
  1426. pci_read_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, &bar_sz);
  1427. dev_dbg(ndev_dev(ndev), "EMBAR2SZ %#x\n", bar_sz);
  1428. /* SBAR01 hit by first part of the b2b bar */
  1429. if (b2b_bar == 0)
  1430. bar_addr = addr->bar0_addr;
  1431. else if (b2b_bar == 1)
  1432. bar_addr = addr->bar2_addr64;
  1433. else if (b2b_bar == 2)
  1434. bar_addr = addr->bar4_addr64;
  1435. else
  1436. return -EIO;
  1437. /* setup incoming bar limits == base addrs (zero length windows) */
  1438. bar_addr = addr->bar2_addr64 + (b2b_bar == 1 ? ndev->b2b_off : 0);
  1439. iowrite64(bar_addr, mmio + SKX_IMBAR1XLMT_OFFSET);
  1440. bar_addr = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
  1441. dev_dbg(ndev_dev(ndev), "IMBAR1XLMT %#018llx\n", bar_addr);
  1442. bar_addr = addr->bar4_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
  1443. iowrite64(bar_addr, mmio + SKX_IMBAR2XLMT_OFFSET);
  1444. bar_addr = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
  1445. dev_dbg(ndev_dev(ndev), "IMBAR2XLMT %#018llx\n", bar_addr);
  1446. /* zero incoming translation addrs */
  1447. iowrite64(0, mmio + SKX_IMBAR1XBASE_OFFSET);
  1448. iowrite64(0, mmio + SKX_IMBAR2XBASE_OFFSET);
  1449. ndev->peer_mmio = ndev->self_mmio;
  1450. return 0;
  1451. }
  1452. static int skx_init_ntb(struct intel_ntb_dev *ndev)
  1453. {
  1454. int rc;
  1455. ndev->mw_count = XEON_MW_COUNT;
  1456. ndev->spad_count = SKX_SPAD_COUNT;
  1457. ndev->db_count = SKX_DB_COUNT;
  1458. ndev->db_link_mask = SKX_DB_LINK_BIT;
  1459. /* DB fixup for using 31 right now */
  1460. if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD)
  1461. ndev->db_link_mask |= BIT_ULL(31);
  1462. switch (ndev->ntb.topo) {
  1463. case NTB_TOPO_B2B_USD:
  1464. case NTB_TOPO_B2B_DSD:
  1465. ndev->self_reg = &skx_pri_reg;
  1466. ndev->peer_reg = &skx_b2b_reg;
  1467. ndev->xlat_reg = &skx_sec_xlat;
  1468. if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
  1469. rc = skx_setup_b2b_mw(ndev,
  1470. &xeon_b2b_dsd_addr,
  1471. &xeon_b2b_usd_addr);
  1472. } else {
  1473. rc = skx_setup_b2b_mw(ndev,
  1474. &xeon_b2b_usd_addr,
  1475. &xeon_b2b_dsd_addr);
  1476. }
  1477. if (rc)
  1478. return rc;
  1479. /* Enable Bus Master and Memory Space on the secondary side */
  1480. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1481. ndev->self_mmio + SKX_SPCICMD_OFFSET);
  1482. break;
  1483. default:
  1484. return -EINVAL;
  1485. }
  1486. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1487. ndev->reg->db_iowrite(ndev->db_valid_mask,
  1488. ndev->self_mmio +
  1489. ndev->self_reg->db_mask);
  1490. return 0;
  1491. }
  1492. static int skx_init_dev(struct intel_ntb_dev *ndev)
  1493. {
  1494. struct pci_dev *pdev;
  1495. u8 ppd;
  1496. int rc;
  1497. pdev = ndev_pdev(ndev);
  1498. ndev->reg = &skx_reg;
  1499. rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
  1500. if (rc)
  1501. return -EIO;
  1502. ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
  1503. dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
  1504. ntb_topo_string(ndev->ntb.topo));
  1505. if (ndev->ntb.topo == NTB_TOPO_NONE)
  1506. return -EINVAL;
  1507. if (pdev_is_skx_xeon(pdev))
  1508. ndev->hwerr_flags |= NTB_HWERR_MSIX_VECTOR32_BAD;
  1509. rc = skx_init_ntb(ndev);
  1510. if (rc)
  1511. return rc;
  1512. return skx_init_isr(ndev);
  1513. }
  1514. static int intel_ntb3_link_enable(struct ntb_dev *ntb,
  1515. enum ntb_speed max_speed,
  1516. enum ntb_width max_width)
  1517. {
  1518. struct intel_ntb_dev *ndev;
  1519. u32 ntb_ctl;
  1520. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  1521. dev_dbg(ndev_dev(ndev),
  1522. "Enabling link with max_speed %d max_width %d\n",
  1523. max_speed, max_width);
  1524. if (max_speed != NTB_SPEED_AUTO)
  1525. dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
  1526. if (max_width != NTB_WIDTH_AUTO)
  1527. dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
  1528. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  1529. ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
  1530. ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
  1531. ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
  1532. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1533. return 0;
  1534. }
  1535. static int intel_ntb3_mw_set_trans(struct ntb_dev *ntb, int idx,
  1536. dma_addr_t addr, resource_size_t size)
  1537. {
  1538. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1539. unsigned long xlat_reg, limit_reg;
  1540. resource_size_t bar_size, mw_size;
  1541. void __iomem *mmio;
  1542. u64 base, limit, reg_val;
  1543. int bar;
  1544. if (idx >= ndev->b2b_idx && !ndev->b2b_off)
  1545. idx += 1;
  1546. bar = ndev_mw_to_bar(ndev, idx);
  1547. if (bar < 0)
  1548. return bar;
  1549. bar_size = pci_resource_len(ndev->ntb.pdev, bar);
  1550. if (idx == ndev->b2b_idx)
  1551. mw_size = bar_size - ndev->b2b_off;
  1552. else
  1553. mw_size = bar_size;
  1554. /* hardware requires that addr is aligned to bar size */
  1555. if (addr & (bar_size - 1))
  1556. return -EINVAL;
  1557. /* make sure the range fits in the usable mw size */
  1558. if (size > mw_size)
  1559. return -EINVAL;
  1560. mmio = ndev->self_mmio;
  1561. xlat_reg = ndev->xlat_reg->bar2_xlat + (idx * 0x10);
  1562. limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10);
  1563. base = pci_resource_start(ndev->ntb.pdev, bar);
  1564. /* Set the limit if supported, if size is not mw_size */
  1565. if (limit_reg && size != mw_size)
  1566. limit = base + size;
  1567. else
  1568. limit = base + mw_size;
  1569. /* set and verify setting the translation address */
  1570. iowrite64(addr, mmio + xlat_reg);
  1571. reg_val = ioread64(mmio + xlat_reg);
  1572. if (reg_val != addr) {
  1573. iowrite64(0, mmio + xlat_reg);
  1574. return -EIO;
  1575. }
  1576. dev_dbg(ndev_dev(ndev), "BAR %d IMBARXBASE: %#Lx\n", bar, reg_val);
  1577. /* set and verify setting the limit */
  1578. iowrite64(limit, mmio + limit_reg);
  1579. reg_val = ioread64(mmio + limit_reg);
  1580. if (reg_val != limit) {
  1581. iowrite64(base, mmio + limit_reg);
  1582. iowrite64(0, mmio + xlat_reg);
  1583. return -EIO;
  1584. }
  1585. dev_dbg(ndev_dev(ndev), "BAR %d IMBARXLMT: %#Lx\n", bar, reg_val);
  1586. /* setup the EP */
  1587. limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10) + 0x4000;
  1588. base = ioread64(mmio + SKX_EMBAR1_OFFSET + (8 * idx));
  1589. base &= ~0xf;
  1590. if (limit_reg && size != mw_size)
  1591. limit = base + size;
  1592. else
  1593. limit = base + mw_size;
  1594. /* set and verify setting the limit */
  1595. iowrite64(limit, mmio + limit_reg);
  1596. reg_val = ioread64(mmio + limit_reg);
  1597. if (reg_val != limit) {
  1598. iowrite64(base, mmio + limit_reg);
  1599. iowrite64(0, mmio + xlat_reg);
  1600. return -EIO;
  1601. }
  1602. dev_dbg(ndev_dev(ndev), "BAR %d EMBARXLMT: %#Lx\n", bar, reg_val);
  1603. return 0;
  1604. }
  1605. static int intel_ntb3_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  1606. {
  1607. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1608. int bit;
  1609. if (db_bits & ~ndev->db_valid_mask)
  1610. return -EINVAL;
  1611. while (db_bits) {
  1612. bit = __ffs(db_bits);
  1613. iowrite32(1, ndev->peer_mmio +
  1614. ndev->peer_reg->db_bell + (bit * 4));
  1615. db_bits &= db_bits - 1;
  1616. }
  1617. return 0;
  1618. }
  1619. static u64 intel_ntb3_db_read(struct ntb_dev *ntb)
  1620. {
  1621. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1622. return ndev_db_read(ndev,
  1623. ndev->self_mmio +
  1624. ndev->self_reg->db_clear);
  1625. }
  1626. static int intel_ntb3_db_clear(struct ntb_dev *ntb, u64 db_bits)
  1627. {
  1628. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  1629. return ndev_db_write(ndev, db_bits,
  1630. ndev->self_mmio +
  1631. ndev->self_reg->db_clear);
  1632. }
  1633. /* XEON */
  1634. static u64 xeon_db_ioread(void __iomem *mmio)
  1635. {
  1636. return (u64)ioread16(mmio);
  1637. }
  1638. static void xeon_db_iowrite(u64 bits, void __iomem *mmio)
  1639. {
  1640. iowrite16((u16)bits, mmio);
  1641. }
  1642. static int xeon_poll_link(struct intel_ntb_dev *ndev)
  1643. {
  1644. u16 reg_val;
  1645. int rc;
  1646. ndev->reg->db_iowrite(ndev->db_link_mask,
  1647. ndev->self_mmio +
  1648. ndev->self_reg->db_bell);
  1649. rc = pci_read_config_word(ndev->ntb.pdev,
  1650. XEON_LINK_STATUS_OFFSET, &reg_val);
  1651. if (rc)
  1652. return 0;
  1653. if (reg_val == ndev->lnk_sta)
  1654. return 0;
  1655. ndev->lnk_sta = reg_val;
  1656. return 1;
  1657. }
  1658. static int xeon_link_is_up(struct intel_ntb_dev *ndev)
  1659. {
  1660. if (ndev->ntb.topo == NTB_TOPO_SEC)
  1661. return 1;
  1662. return NTB_LNK_STA_ACTIVE(ndev->lnk_sta);
  1663. }
  1664. static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd)
  1665. {
  1666. switch (ppd & XEON_PPD_TOPO_MASK) {
  1667. case XEON_PPD_TOPO_B2B_USD:
  1668. return NTB_TOPO_B2B_USD;
  1669. case XEON_PPD_TOPO_B2B_DSD:
  1670. return NTB_TOPO_B2B_DSD;
  1671. case XEON_PPD_TOPO_PRI_USD:
  1672. case XEON_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
  1673. return NTB_TOPO_PRI;
  1674. case XEON_PPD_TOPO_SEC_USD:
  1675. case XEON_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
  1676. return NTB_TOPO_SEC;
  1677. }
  1678. return NTB_TOPO_NONE;
  1679. }
  1680. static inline int xeon_ppd_bar4_split(struct intel_ntb_dev *ndev, u8 ppd)
  1681. {
  1682. if (ppd & XEON_PPD_SPLIT_BAR_MASK) {
  1683. dev_dbg(ndev_dev(ndev), "PPD %d split bar\n", ppd);
  1684. return 1;
  1685. }
  1686. return 0;
  1687. }
  1688. static int xeon_init_isr(struct intel_ntb_dev *ndev)
  1689. {
  1690. return ndev_init_isr(ndev, XEON_DB_MSIX_VECTOR_COUNT,
  1691. XEON_DB_MSIX_VECTOR_COUNT,
  1692. XEON_DB_MSIX_VECTOR_SHIFT,
  1693. XEON_DB_TOTAL_SHIFT);
  1694. }
  1695. static void xeon_deinit_isr(struct intel_ntb_dev *ndev)
  1696. {
  1697. ndev_deinit_isr(ndev);
  1698. }
  1699. static int xeon_setup_b2b_mw(struct intel_ntb_dev *ndev,
  1700. const struct intel_b2b_addr *addr,
  1701. const struct intel_b2b_addr *peer_addr)
  1702. {
  1703. struct pci_dev *pdev;
  1704. void __iomem *mmio;
  1705. resource_size_t bar_size;
  1706. phys_addr_t bar_addr;
  1707. int b2b_bar;
  1708. u8 bar_sz;
  1709. pdev = ndev_pdev(ndev);
  1710. mmio = ndev->self_mmio;
  1711. if (ndev->b2b_idx == UINT_MAX) {
  1712. dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
  1713. b2b_bar = 0;
  1714. ndev->b2b_off = 0;
  1715. } else {
  1716. b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
  1717. if (b2b_bar < 0)
  1718. return -EIO;
  1719. dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
  1720. bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
  1721. dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
  1722. if (b2b_mw_share && XEON_B2B_MIN_SIZE <= bar_size >> 1) {
  1723. dev_dbg(ndev_dev(ndev),
  1724. "b2b using first half of bar\n");
  1725. ndev->b2b_off = bar_size >> 1;
  1726. } else if (XEON_B2B_MIN_SIZE <= bar_size) {
  1727. dev_dbg(ndev_dev(ndev),
  1728. "b2b using whole bar\n");
  1729. ndev->b2b_off = 0;
  1730. --ndev->mw_count;
  1731. } else {
  1732. dev_dbg(ndev_dev(ndev),
  1733. "b2b bar size is too small\n");
  1734. return -EIO;
  1735. }
  1736. }
  1737. /* Reset the secondary bar sizes to match the primary bar sizes,
  1738. * except disable or halve the size of the b2b secondary bar.
  1739. *
  1740. * Note: code for each specific bar size register, because the register
  1741. * offsets are not in a consistent order (bar5sz comes after ppd, odd).
  1742. */
  1743. pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &bar_sz);
  1744. dev_dbg(ndev_dev(ndev), "PBAR23SZ %#x\n", bar_sz);
  1745. if (b2b_bar == 2) {
  1746. if (ndev->b2b_off)
  1747. bar_sz -= 1;
  1748. else
  1749. bar_sz = 0;
  1750. }
  1751. pci_write_config_byte(pdev, XEON_SBAR23SZ_OFFSET, bar_sz);
  1752. pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &bar_sz);
  1753. dev_dbg(ndev_dev(ndev), "SBAR23SZ %#x\n", bar_sz);
  1754. if (!ndev->bar4_split) {
  1755. pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &bar_sz);
  1756. dev_dbg(ndev_dev(ndev), "PBAR45SZ %#x\n", bar_sz);
  1757. if (b2b_bar == 4) {
  1758. if (ndev->b2b_off)
  1759. bar_sz -= 1;
  1760. else
  1761. bar_sz = 0;
  1762. }
  1763. pci_write_config_byte(pdev, XEON_SBAR45SZ_OFFSET, bar_sz);
  1764. pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &bar_sz);
  1765. dev_dbg(ndev_dev(ndev), "SBAR45SZ %#x\n", bar_sz);
  1766. } else {
  1767. pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &bar_sz);
  1768. dev_dbg(ndev_dev(ndev), "PBAR4SZ %#x\n", bar_sz);
  1769. if (b2b_bar == 4) {
  1770. if (ndev->b2b_off)
  1771. bar_sz -= 1;
  1772. else
  1773. bar_sz = 0;
  1774. }
  1775. pci_write_config_byte(pdev, XEON_SBAR4SZ_OFFSET, bar_sz);
  1776. pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &bar_sz);
  1777. dev_dbg(ndev_dev(ndev), "SBAR4SZ %#x\n", bar_sz);
  1778. pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &bar_sz);
  1779. dev_dbg(ndev_dev(ndev), "PBAR5SZ %#x\n", bar_sz);
  1780. if (b2b_bar == 5) {
  1781. if (ndev->b2b_off)
  1782. bar_sz -= 1;
  1783. else
  1784. bar_sz = 0;
  1785. }
  1786. pci_write_config_byte(pdev, XEON_SBAR5SZ_OFFSET, bar_sz);
  1787. pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &bar_sz);
  1788. dev_dbg(ndev_dev(ndev), "SBAR5SZ %#x\n", bar_sz);
  1789. }
  1790. /* SBAR01 hit by first part of the b2b bar */
  1791. if (b2b_bar == 0)
  1792. bar_addr = addr->bar0_addr;
  1793. else if (b2b_bar == 2)
  1794. bar_addr = addr->bar2_addr64;
  1795. else if (b2b_bar == 4 && !ndev->bar4_split)
  1796. bar_addr = addr->bar4_addr64;
  1797. else if (b2b_bar == 4)
  1798. bar_addr = addr->bar4_addr32;
  1799. else if (b2b_bar == 5)
  1800. bar_addr = addr->bar5_addr32;
  1801. else
  1802. return -EIO;
  1803. dev_dbg(ndev_dev(ndev), "SBAR01 %#018llx\n", bar_addr);
  1804. iowrite64(bar_addr, mmio + XEON_SBAR0BASE_OFFSET);
  1805. /* Other SBAR are normally hit by the PBAR xlat, except for b2b bar.
  1806. * The b2b bar is either disabled above, or configured half-size, and
  1807. * it starts at the PBAR xlat + offset.
  1808. */
  1809. bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
  1810. iowrite64(bar_addr, mmio + XEON_SBAR23BASE_OFFSET);
  1811. bar_addr = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
  1812. dev_dbg(ndev_dev(ndev), "SBAR23 %#018llx\n", bar_addr);
  1813. if (!ndev->bar4_split) {
  1814. bar_addr = addr->bar4_addr64 +
  1815. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1816. iowrite64(bar_addr, mmio + XEON_SBAR45BASE_OFFSET);
  1817. bar_addr = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
  1818. dev_dbg(ndev_dev(ndev), "SBAR45 %#018llx\n", bar_addr);
  1819. } else {
  1820. bar_addr = addr->bar4_addr32 +
  1821. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1822. iowrite32(bar_addr, mmio + XEON_SBAR4BASE_OFFSET);
  1823. bar_addr = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
  1824. dev_dbg(ndev_dev(ndev), "SBAR4 %#010llx\n", bar_addr);
  1825. bar_addr = addr->bar5_addr32 +
  1826. (b2b_bar == 5 ? ndev->b2b_off : 0);
  1827. iowrite32(bar_addr, mmio + XEON_SBAR5BASE_OFFSET);
  1828. bar_addr = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
  1829. dev_dbg(ndev_dev(ndev), "SBAR5 %#010llx\n", bar_addr);
  1830. }
  1831. /* setup incoming bar limits == base addrs (zero length windows) */
  1832. bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
  1833. iowrite64(bar_addr, mmio + XEON_SBAR23LMT_OFFSET);
  1834. bar_addr = ioread64(mmio + XEON_SBAR23LMT_OFFSET);
  1835. dev_dbg(ndev_dev(ndev), "SBAR23LMT %#018llx\n", bar_addr);
  1836. if (!ndev->bar4_split) {
  1837. bar_addr = addr->bar4_addr64 +
  1838. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1839. iowrite64(bar_addr, mmio + XEON_SBAR45LMT_OFFSET);
  1840. bar_addr = ioread64(mmio + XEON_SBAR45LMT_OFFSET);
  1841. dev_dbg(ndev_dev(ndev), "SBAR45LMT %#018llx\n", bar_addr);
  1842. } else {
  1843. bar_addr = addr->bar4_addr32 +
  1844. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1845. iowrite32(bar_addr, mmio + XEON_SBAR4LMT_OFFSET);
  1846. bar_addr = ioread32(mmio + XEON_SBAR4LMT_OFFSET);
  1847. dev_dbg(ndev_dev(ndev), "SBAR4LMT %#010llx\n", bar_addr);
  1848. bar_addr = addr->bar5_addr32 +
  1849. (b2b_bar == 5 ? ndev->b2b_off : 0);
  1850. iowrite32(bar_addr, mmio + XEON_SBAR5LMT_OFFSET);
  1851. bar_addr = ioread32(mmio + XEON_SBAR5LMT_OFFSET);
  1852. dev_dbg(ndev_dev(ndev), "SBAR5LMT %#05llx\n", bar_addr);
  1853. }
  1854. /* zero incoming translation addrs */
  1855. iowrite64(0, mmio + XEON_SBAR23XLAT_OFFSET);
  1856. if (!ndev->bar4_split) {
  1857. iowrite64(0, mmio + XEON_SBAR45XLAT_OFFSET);
  1858. } else {
  1859. iowrite32(0, mmio + XEON_SBAR4XLAT_OFFSET);
  1860. iowrite32(0, mmio + XEON_SBAR5XLAT_OFFSET);
  1861. }
  1862. /* zero outgoing translation limits (whole bar size windows) */
  1863. iowrite64(0, mmio + XEON_PBAR23LMT_OFFSET);
  1864. if (!ndev->bar4_split) {
  1865. iowrite64(0, mmio + XEON_PBAR45LMT_OFFSET);
  1866. } else {
  1867. iowrite32(0, mmio + XEON_PBAR4LMT_OFFSET);
  1868. iowrite32(0, mmio + XEON_PBAR5LMT_OFFSET);
  1869. }
  1870. /* set outgoing translation offsets */
  1871. bar_addr = peer_addr->bar2_addr64;
  1872. iowrite64(bar_addr, mmio + XEON_PBAR23XLAT_OFFSET);
  1873. bar_addr = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
  1874. dev_dbg(ndev_dev(ndev), "PBAR23XLAT %#018llx\n", bar_addr);
  1875. if (!ndev->bar4_split) {
  1876. bar_addr = peer_addr->bar4_addr64;
  1877. iowrite64(bar_addr, mmio + XEON_PBAR45XLAT_OFFSET);
  1878. bar_addr = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
  1879. dev_dbg(ndev_dev(ndev), "PBAR45XLAT %#018llx\n", bar_addr);
  1880. } else {
  1881. bar_addr = peer_addr->bar4_addr32;
  1882. iowrite32(bar_addr, mmio + XEON_PBAR4XLAT_OFFSET);
  1883. bar_addr = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
  1884. dev_dbg(ndev_dev(ndev), "PBAR4XLAT %#010llx\n", bar_addr);
  1885. bar_addr = peer_addr->bar5_addr32;
  1886. iowrite32(bar_addr, mmio + XEON_PBAR5XLAT_OFFSET);
  1887. bar_addr = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
  1888. dev_dbg(ndev_dev(ndev), "PBAR5XLAT %#010llx\n", bar_addr);
  1889. }
  1890. /* set the translation offset for b2b registers */
  1891. if (b2b_bar == 0)
  1892. bar_addr = peer_addr->bar0_addr;
  1893. else if (b2b_bar == 2)
  1894. bar_addr = peer_addr->bar2_addr64;
  1895. else if (b2b_bar == 4 && !ndev->bar4_split)
  1896. bar_addr = peer_addr->bar4_addr64;
  1897. else if (b2b_bar == 4)
  1898. bar_addr = peer_addr->bar4_addr32;
  1899. else if (b2b_bar == 5)
  1900. bar_addr = peer_addr->bar5_addr32;
  1901. else
  1902. return -EIO;
  1903. /* B2B_XLAT_OFFSET is 64bit, but can only take 32bit writes */
  1904. dev_dbg(ndev_dev(ndev), "B2BXLAT %#018llx\n", bar_addr);
  1905. iowrite32(bar_addr, mmio + XEON_B2B_XLAT_OFFSETL);
  1906. iowrite32(bar_addr >> 32, mmio + XEON_B2B_XLAT_OFFSETU);
  1907. if (b2b_bar) {
  1908. /* map peer ntb mmio config space registers */
  1909. ndev->peer_mmio = pci_iomap(pdev, b2b_bar,
  1910. XEON_B2B_MIN_SIZE);
  1911. if (!ndev->peer_mmio)
  1912. return -EIO;
  1913. ndev->peer_addr = pci_resource_start(pdev, b2b_bar);
  1914. }
  1915. return 0;
  1916. }
  1917. static int xeon_init_ntb(struct intel_ntb_dev *ndev)
  1918. {
  1919. int rc;
  1920. u32 ntb_ctl;
  1921. if (ndev->bar4_split)
  1922. ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
  1923. else
  1924. ndev->mw_count = XEON_MW_COUNT;
  1925. ndev->spad_count = XEON_SPAD_COUNT;
  1926. ndev->db_count = XEON_DB_COUNT;
  1927. ndev->db_link_mask = XEON_DB_LINK_BIT;
  1928. switch (ndev->ntb.topo) {
  1929. case NTB_TOPO_PRI:
  1930. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1931. dev_err(ndev_dev(ndev), "NTB Primary config disabled\n");
  1932. return -EINVAL;
  1933. }
  1934. /* enable link to allow secondary side device to appear */
  1935. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  1936. ntb_ctl &= ~NTB_CTL_DISABLE;
  1937. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1938. /* use half the spads for the peer */
  1939. ndev->spad_count >>= 1;
  1940. ndev->self_reg = &xeon_pri_reg;
  1941. ndev->peer_reg = &xeon_sec_reg;
  1942. ndev->xlat_reg = &xeon_sec_xlat;
  1943. break;
  1944. case NTB_TOPO_SEC:
  1945. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1946. dev_err(ndev_dev(ndev), "NTB Secondary config disabled\n");
  1947. return -EINVAL;
  1948. }
  1949. /* use half the spads for the peer */
  1950. ndev->spad_count >>= 1;
  1951. ndev->self_reg = &xeon_sec_reg;
  1952. ndev->peer_reg = &xeon_pri_reg;
  1953. ndev->xlat_reg = &xeon_pri_xlat;
  1954. break;
  1955. case NTB_TOPO_B2B_USD:
  1956. case NTB_TOPO_B2B_DSD:
  1957. ndev->self_reg = &xeon_pri_reg;
  1958. ndev->peer_reg = &xeon_b2b_reg;
  1959. ndev->xlat_reg = &xeon_sec_xlat;
  1960. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1961. ndev->peer_reg = &xeon_pri_reg;
  1962. if (b2b_mw_idx < 0)
  1963. ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
  1964. else
  1965. ndev->b2b_idx = b2b_mw_idx;
  1966. if (ndev->b2b_idx >= ndev->mw_count) {
  1967. dev_dbg(ndev_dev(ndev),
  1968. "b2b_mw_idx %d invalid for mw_count %u\n",
  1969. b2b_mw_idx, ndev->mw_count);
  1970. return -EINVAL;
  1971. }
  1972. dev_dbg(ndev_dev(ndev),
  1973. "setting up b2b mw idx %d means %d\n",
  1974. b2b_mw_idx, ndev->b2b_idx);
  1975. } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
  1976. dev_warn(ndev_dev(ndev), "Reduce doorbell count by 1\n");
  1977. ndev->db_count -= 1;
  1978. }
  1979. if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
  1980. rc = xeon_setup_b2b_mw(ndev,
  1981. &xeon_b2b_dsd_addr,
  1982. &xeon_b2b_usd_addr);
  1983. } else {
  1984. rc = xeon_setup_b2b_mw(ndev,
  1985. &xeon_b2b_usd_addr,
  1986. &xeon_b2b_dsd_addr);
  1987. }
  1988. if (rc)
  1989. return rc;
  1990. /* Enable Bus Master and Memory Space on the secondary side */
  1991. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1992. ndev->self_mmio + XEON_SPCICMD_OFFSET);
  1993. break;
  1994. default:
  1995. return -EINVAL;
  1996. }
  1997. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1998. ndev->reg->db_iowrite(ndev->db_valid_mask,
  1999. ndev->self_mmio +
  2000. ndev->self_reg->db_mask);
  2001. return 0;
  2002. }
  2003. static int xeon_init_dev(struct intel_ntb_dev *ndev)
  2004. {
  2005. struct pci_dev *pdev;
  2006. u8 ppd;
  2007. int rc, mem;
  2008. pdev = ndev_pdev(ndev);
  2009. switch (pdev->device) {
  2010. /* There is a Xeon hardware errata related to writes to SDOORBELL or
  2011. * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
  2012. * which may hang the system. To workaround this use the second memory
  2013. * window to access the interrupt and scratch pad registers on the
  2014. * remote system.
  2015. */
  2016. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  2017. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  2018. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  2019. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  2020. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  2021. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  2022. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  2023. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  2024. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  2025. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  2026. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  2027. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  2028. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  2029. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  2030. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  2031. ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
  2032. break;
  2033. }
  2034. switch (pdev->device) {
  2035. /* There is a hardware errata related to accessing any register in
  2036. * SB01BASE in the presence of bidirectional traffic crossing the NTB.
  2037. */
  2038. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  2039. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  2040. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  2041. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  2042. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  2043. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  2044. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  2045. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  2046. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  2047. ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
  2048. break;
  2049. }
  2050. switch (pdev->device) {
  2051. /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
  2052. * mirrored to the remote system. Shrink the number of bits by one,
  2053. * since bit 14 is the last bit.
  2054. */
  2055. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  2056. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  2057. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  2058. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  2059. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  2060. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  2061. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  2062. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  2063. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  2064. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  2065. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  2066. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  2067. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  2068. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  2069. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  2070. ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
  2071. break;
  2072. }
  2073. ndev->reg = &xeon_reg;
  2074. rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
  2075. if (rc)
  2076. return -EIO;
  2077. ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
  2078. dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
  2079. ntb_topo_string(ndev->ntb.topo));
  2080. if (ndev->ntb.topo == NTB_TOPO_NONE)
  2081. return -EINVAL;
  2082. if (ndev->ntb.topo != NTB_TOPO_SEC) {
  2083. ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
  2084. dev_dbg(ndev_dev(ndev), "ppd %#x bar4_split %d\n",
  2085. ppd, ndev->bar4_split);
  2086. } else {
  2087. /* This is a way for transparent BAR to figure out if we are
  2088. * doing split BAR or not. There is no way for the hw on the
  2089. * transparent side to know and set the PPD.
  2090. */
  2091. mem = pci_select_bars(pdev, IORESOURCE_MEM);
  2092. ndev->bar4_split = hweight32(mem) ==
  2093. HSX_SPLIT_BAR_MW_COUNT + 1;
  2094. dev_dbg(ndev_dev(ndev), "mem %#x bar4_split %d\n",
  2095. mem, ndev->bar4_split);
  2096. }
  2097. rc = xeon_init_ntb(ndev);
  2098. if (rc)
  2099. return rc;
  2100. return xeon_init_isr(ndev);
  2101. }
  2102. static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
  2103. {
  2104. xeon_deinit_isr(ndev);
  2105. }
  2106. static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
  2107. {
  2108. int rc;
  2109. pci_set_drvdata(pdev, ndev);
  2110. rc = pci_enable_device(pdev);
  2111. if (rc)
  2112. goto err_pci_enable;
  2113. rc = pci_request_regions(pdev, NTB_NAME);
  2114. if (rc)
  2115. goto err_pci_regions;
  2116. pci_set_master(pdev);
  2117. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2118. if (rc) {
  2119. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2120. if (rc)
  2121. goto err_dma_mask;
  2122. dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
  2123. }
  2124. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2125. if (rc) {
  2126. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2127. if (rc)
  2128. goto err_dma_mask;
  2129. dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
  2130. }
  2131. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  2132. if (!ndev->self_mmio) {
  2133. rc = -EIO;
  2134. goto err_mmio;
  2135. }
  2136. ndev->peer_mmio = ndev->self_mmio;
  2137. ndev->peer_addr = pci_resource_start(pdev, 0);
  2138. return 0;
  2139. err_mmio:
  2140. err_dma_mask:
  2141. pci_clear_master(pdev);
  2142. pci_release_regions(pdev);
  2143. err_pci_regions:
  2144. pci_disable_device(pdev);
  2145. err_pci_enable:
  2146. pci_set_drvdata(pdev, NULL);
  2147. return rc;
  2148. }
  2149. static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
  2150. {
  2151. struct pci_dev *pdev = ndev_pdev(ndev);
  2152. if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
  2153. pci_iounmap(pdev, ndev->peer_mmio);
  2154. pci_iounmap(pdev, ndev->self_mmio);
  2155. pci_clear_master(pdev);
  2156. pci_release_regions(pdev);
  2157. pci_disable_device(pdev);
  2158. pci_set_drvdata(pdev, NULL);
  2159. }
  2160. static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
  2161. struct pci_dev *pdev)
  2162. {
  2163. ndev->ntb.pdev = pdev;
  2164. ndev->ntb.topo = NTB_TOPO_NONE;
  2165. ndev->ntb.ops = &intel_ntb_ops;
  2166. ndev->b2b_off = 0;
  2167. ndev->b2b_idx = UINT_MAX;
  2168. ndev->bar4_split = 0;
  2169. ndev->mw_count = 0;
  2170. ndev->spad_count = 0;
  2171. ndev->db_count = 0;
  2172. ndev->db_vec_count = 0;
  2173. ndev->db_vec_shift = 0;
  2174. ndev->ntb_ctl = 0;
  2175. ndev->lnk_sta = 0;
  2176. ndev->db_valid_mask = 0;
  2177. ndev->db_link_mask = 0;
  2178. ndev->db_mask = 0;
  2179. spin_lock_init(&ndev->db_mask_lock);
  2180. }
  2181. static int intel_ntb_pci_probe(struct pci_dev *pdev,
  2182. const struct pci_device_id *id)
  2183. {
  2184. struct intel_ntb_dev *ndev;
  2185. int rc, node;
  2186. node = dev_to_node(&pdev->dev);
  2187. if (pdev_is_atom(pdev)) {
  2188. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  2189. if (!ndev) {
  2190. rc = -ENOMEM;
  2191. goto err_ndev;
  2192. }
  2193. ndev_init_struct(ndev, pdev);
  2194. rc = intel_ntb_init_pci(ndev, pdev);
  2195. if (rc)
  2196. goto err_init_pci;
  2197. rc = atom_init_dev(ndev);
  2198. if (rc)
  2199. goto err_init_dev;
  2200. } else if (pdev_is_xeon(pdev)) {
  2201. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  2202. if (!ndev) {
  2203. rc = -ENOMEM;
  2204. goto err_ndev;
  2205. }
  2206. ndev_init_struct(ndev, pdev);
  2207. rc = intel_ntb_init_pci(ndev, pdev);
  2208. if (rc)
  2209. goto err_init_pci;
  2210. rc = xeon_init_dev(ndev);
  2211. if (rc)
  2212. goto err_init_dev;
  2213. } else if (pdev_is_skx_xeon(pdev)) {
  2214. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  2215. if (!ndev) {
  2216. rc = -ENOMEM;
  2217. goto err_ndev;
  2218. }
  2219. ndev_init_struct(ndev, pdev);
  2220. ndev->ntb.ops = &intel_ntb3_ops;
  2221. rc = intel_ntb_init_pci(ndev, pdev);
  2222. if (rc)
  2223. goto err_init_pci;
  2224. rc = skx_init_dev(ndev);
  2225. if (rc)
  2226. goto err_init_dev;
  2227. } else {
  2228. rc = -EINVAL;
  2229. goto err_ndev;
  2230. }
  2231. ndev_reset_unsafe_flags(ndev);
  2232. ndev->reg->poll_link(ndev);
  2233. ndev_init_debugfs(ndev);
  2234. rc = ntb_register_device(&ndev->ntb);
  2235. if (rc)
  2236. goto err_register;
  2237. dev_info(&pdev->dev, "NTB device registered.\n");
  2238. return 0;
  2239. err_register:
  2240. ndev_deinit_debugfs(ndev);
  2241. if (pdev_is_atom(pdev))
  2242. atom_deinit_dev(ndev);
  2243. else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
  2244. xeon_deinit_dev(ndev);
  2245. err_init_dev:
  2246. intel_ntb_deinit_pci(ndev);
  2247. err_init_pci:
  2248. kfree(ndev);
  2249. err_ndev:
  2250. return rc;
  2251. }
  2252. static void intel_ntb_pci_remove(struct pci_dev *pdev)
  2253. {
  2254. struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
  2255. ntb_unregister_device(&ndev->ntb);
  2256. ndev_deinit_debugfs(ndev);
  2257. if (pdev_is_atom(pdev))
  2258. atom_deinit_dev(ndev);
  2259. else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
  2260. xeon_deinit_dev(ndev);
  2261. intel_ntb_deinit_pci(ndev);
  2262. kfree(ndev);
  2263. }
  2264. static const struct intel_ntb_reg atom_reg = {
  2265. .poll_link = atom_poll_link,
  2266. .link_is_up = atom_link_is_up,
  2267. .db_ioread = atom_db_ioread,
  2268. .db_iowrite = atom_db_iowrite,
  2269. .db_size = sizeof(u64),
  2270. .ntb_ctl = ATOM_NTBCNTL_OFFSET,
  2271. .mw_bar = {2, 4},
  2272. };
  2273. static const struct intel_ntb_alt_reg atom_pri_reg = {
  2274. .db_bell = ATOM_PDOORBELL_OFFSET,
  2275. .db_mask = ATOM_PDBMSK_OFFSET,
  2276. .spad = ATOM_SPAD_OFFSET,
  2277. };
  2278. static const struct intel_ntb_alt_reg atom_b2b_reg = {
  2279. .db_bell = ATOM_B2B_DOORBELL_OFFSET,
  2280. .spad = ATOM_B2B_SPAD_OFFSET,
  2281. };
  2282. static const struct intel_ntb_xlat_reg atom_sec_xlat = {
  2283. /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
  2284. /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
  2285. .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
  2286. };
  2287. static const struct intel_ntb_reg xeon_reg = {
  2288. .poll_link = xeon_poll_link,
  2289. .link_is_up = xeon_link_is_up,
  2290. .db_ioread = xeon_db_ioread,
  2291. .db_iowrite = xeon_db_iowrite,
  2292. .db_size = sizeof(u32),
  2293. .ntb_ctl = XEON_NTBCNTL_OFFSET,
  2294. .mw_bar = {2, 4, 5},
  2295. };
  2296. static const struct intel_ntb_alt_reg xeon_pri_reg = {
  2297. .db_bell = XEON_PDOORBELL_OFFSET,
  2298. .db_mask = XEON_PDBMSK_OFFSET,
  2299. .spad = XEON_SPAD_OFFSET,
  2300. };
  2301. static const struct intel_ntb_alt_reg xeon_sec_reg = {
  2302. .db_bell = XEON_SDOORBELL_OFFSET,
  2303. .db_mask = XEON_SDBMSK_OFFSET,
  2304. /* second half of the scratchpads */
  2305. .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
  2306. };
  2307. static const struct intel_ntb_alt_reg xeon_b2b_reg = {
  2308. .db_bell = XEON_B2B_DOORBELL_OFFSET,
  2309. .spad = XEON_B2B_SPAD_OFFSET,
  2310. };
  2311. static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
  2312. /* Note: no primary .bar0_base visible to the secondary side.
  2313. *
  2314. * The secondary side cannot get the base address stored in primary
  2315. * bars. The base address is necessary to set the limit register to
  2316. * any value other than zero, or unlimited.
  2317. *
  2318. * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
  2319. * window by setting the limit equal to base, nor can it limit the size
  2320. * of the memory window by setting the limit to base + size.
  2321. */
  2322. .bar2_limit = XEON_PBAR23LMT_OFFSET,
  2323. .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
  2324. };
  2325. static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
  2326. .bar0_base = XEON_SBAR0BASE_OFFSET,
  2327. .bar2_limit = XEON_SBAR23LMT_OFFSET,
  2328. .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
  2329. };
  2330. static struct intel_b2b_addr xeon_b2b_usd_addr = {
  2331. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  2332. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  2333. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  2334. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  2335. };
  2336. static struct intel_b2b_addr xeon_b2b_dsd_addr = {
  2337. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  2338. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  2339. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  2340. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  2341. };
  2342. static const struct intel_ntb_reg skx_reg = {
  2343. .poll_link = skx_poll_link,
  2344. .link_is_up = xeon_link_is_up,
  2345. .db_ioread = skx_db_ioread,
  2346. .db_iowrite = skx_db_iowrite,
  2347. .db_size = sizeof(u64),
  2348. .ntb_ctl = SKX_NTBCNTL_OFFSET,
  2349. .mw_bar = {2, 4},
  2350. };
  2351. static const struct intel_ntb_alt_reg skx_pri_reg = {
  2352. .db_bell = SKX_EM_DOORBELL_OFFSET,
  2353. .db_clear = SKX_IM_INT_STATUS_OFFSET,
  2354. .db_mask = SKX_IM_INT_DISABLE_OFFSET,
  2355. .spad = SKX_IM_SPAD_OFFSET,
  2356. };
  2357. static const struct intel_ntb_alt_reg skx_b2b_reg = {
  2358. .db_bell = SKX_IM_DOORBELL_OFFSET,
  2359. .db_clear = SKX_EM_INT_STATUS_OFFSET,
  2360. .db_mask = SKX_EM_INT_DISABLE_OFFSET,
  2361. .spad = SKX_B2B_SPAD_OFFSET,
  2362. };
  2363. static const struct intel_ntb_xlat_reg skx_sec_xlat = {
  2364. /* .bar0_base = SKX_EMBAR0_OFFSET, */
  2365. .bar2_limit = SKX_IMBAR1XLMT_OFFSET,
  2366. .bar2_xlat = SKX_IMBAR1XBASE_OFFSET,
  2367. };
  2368. /* operations for primary side of local ntb */
  2369. static const struct ntb_dev_ops intel_ntb_ops = {
  2370. .mw_count = intel_ntb_mw_count,
  2371. .mw_get_range = intel_ntb_mw_get_range,
  2372. .mw_set_trans = intel_ntb_mw_set_trans,
  2373. .link_is_up = intel_ntb_link_is_up,
  2374. .link_enable = intel_ntb_link_enable,
  2375. .link_disable = intel_ntb_link_disable,
  2376. .db_is_unsafe = intel_ntb_db_is_unsafe,
  2377. .db_valid_mask = intel_ntb_db_valid_mask,
  2378. .db_vector_count = intel_ntb_db_vector_count,
  2379. .db_vector_mask = intel_ntb_db_vector_mask,
  2380. .db_read = intel_ntb_db_read,
  2381. .db_clear = intel_ntb_db_clear,
  2382. .db_set_mask = intel_ntb_db_set_mask,
  2383. .db_clear_mask = intel_ntb_db_clear_mask,
  2384. .peer_db_addr = intel_ntb_peer_db_addr,
  2385. .peer_db_set = intel_ntb_peer_db_set,
  2386. .spad_is_unsafe = intel_ntb_spad_is_unsafe,
  2387. .spad_count = intel_ntb_spad_count,
  2388. .spad_read = intel_ntb_spad_read,
  2389. .spad_write = intel_ntb_spad_write,
  2390. .peer_spad_addr = intel_ntb_peer_spad_addr,
  2391. .peer_spad_read = intel_ntb_peer_spad_read,
  2392. .peer_spad_write = intel_ntb_peer_spad_write,
  2393. };
  2394. static const struct ntb_dev_ops intel_ntb3_ops = {
  2395. .mw_count = intel_ntb_mw_count,
  2396. .mw_get_range = intel_ntb_mw_get_range,
  2397. .mw_set_trans = intel_ntb3_mw_set_trans,
  2398. .link_is_up = intel_ntb_link_is_up,
  2399. .link_enable = intel_ntb3_link_enable,
  2400. .link_disable = intel_ntb_link_disable,
  2401. .db_valid_mask = intel_ntb_db_valid_mask,
  2402. .db_vector_count = intel_ntb_db_vector_count,
  2403. .db_vector_mask = intel_ntb_db_vector_mask,
  2404. .db_read = intel_ntb3_db_read,
  2405. .db_clear = intel_ntb3_db_clear,
  2406. .db_set_mask = intel_ntb_db_set_mask,
  2407. .db_clear_mask = intel_ntb_db_clear_mask,
  2408. .peer_db_addr = intel_ntb_peer_db_addr,
  2409. .peer_db_set = intel_ntb3_peer_db_set,
  2410. .spad_is_unsafe = intel_ntb_spad_is_unsafe,
  2411. .spad_count = intel_ntb_spad_count,
  2412. .spad_read = intel_ntb_spad_read,
  2413. .spad_write = intel_ntb_spad_write,
  2414. .peer_spad_addr = intel_ntb_peer_spad_addr,
  2415. .peer_spad_read = intel_ntb_peer_spad_read,
  2416. .peer_spad_write = intel_ntb_peer_spad_write,
  2417. };
  2418. static const struct file_operations intel_ntb_debugfs_info = {
  2419. .owner = THIS_MODULE,
  2420. .open = simple_open,
  2421. .read = ndev_debugfs_read,
  2422. };
  2423. static const struct pci_device_id intel_ntb_pci_tbl[] = {
  2424. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  2425. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  2426. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  2427. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  2428. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  2429. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BDX)},
  2430. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  2431. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  2432. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  2433. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  2434. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_BDX)},
  2435. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  2436. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  2437. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  2438. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  2439. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_BDX)},
  2440. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)},
  2441. {0}
  2442. };
  2443. MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
  2444. static struct pci_driver intel_ntb_pci_driver = {
  2445. .name = KBUILD_MODNAME,
  2446. .id_table = intel_ntb_pci_tbl,
  2447. .probe = intel_ntb_pci_probe,
  2448. .remove = intel_ntb_pci_remove,
  2449. };
  2450. static int __init intel_ntb_pci_driver_init(void)
  2451. {
  2452. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  2453. if (debugfs_initialized())
  2454. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  2455. return pci_register_driver(&intel_ntb_pci_driver);
  2456. }
  2457. module_init(intel_ntb_pci_driver_init);
  2458. static void __exit intel_ntb_pci_driver_exit(void)
  2459. {
  2460. pci_unregister_driver(&intel_ntb_pci_driver);
  2461. debugfs_remove_recursive(debugfs_dir);
  2462. }
  2463. module_exit(intel_ntb_pci_driver_exit);