ntb_hw_intel.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  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 ntb_dev_ops intel_ntb_ops;
  84. static const struct file_operations intel_ntb_debugfs_info;
  85. static struct dentry *debugfs_dir;
  86. static int b2b_mw_idx = -1;
  87. module_param(b2b_mw_idx, int, 0644);
  88. MODULE_PARM_DESC(b2b_mw_idx, "Use this mw idx to access the peer ntb. A "
  89. "value of zero or positive starts from first mw idx, and a "
  90. "negative value starts from last mw idx. Both sides MUST "
  91. "set the same value here!");
  92. static unsigned int b2b_mw_share;
  93. module_param(b2b_mw_share, uint, 0644);
  94. MODULE_PARM_DESC(b2b_mw_share, "If the b2b mw is large enough, configure the "
  95. "ntb so that the peer ntb only occupies the first half of "
  96. "the mw, so the second half can still be used as a mw. Both "
  97. "sides MUST set the same value here!");
  98. module_param_named(xeon_b2b_usd_bar2_addr64,
  99. xeon_b2b_usd_addr.bar2_addr64, ullong, 0644);
  100. MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
  101. "XEON B2B USD BAR 2 64-bit address");
  102. module_param_named(xeon_b2b_usd_bar4_addr64,
  103. xeon_b2b_usd_addr.bar4_addr64, ullong, 0644);
  104. MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
  105. "XEON B2B USD BAR 4 64-bit address");
  106. module_param_named(xeon_b2b_usd_bar4_addr32,
  107. xeon_b2b_usd_addr.bar4_addr32, ullong, 0644);
  108. MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
  109. "XEON B2B USD split-BAR 4 32-bit address");
  110. module_param_named(xeon_b2b_usd_bar5_addr32,
  111. xeon_b2b_usd_addr.bar5_addr32, ullong, 0644);
  112. MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
  113. "XEON B2B USD split-BAR 5 32-bit address");
  114. module_param_named(xeon_b2b_dsd_bar2_addr64,
  115. xeon_b2b_dsd_addr.bar2_addr64, ullong, 0644);
  116. MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
  117. "XEON B2B DSD BAR 2 64-bit address");
  118. module_param_named(xeon_b2b_dsd_bar4_addr64,
  119. xeon_b2b_dsd_addr.bar4_addr64, ullong, 0644);
  120. MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
  121. "XEON B2B DSD BAR 4 64-bit address");
  122. module_param_named(xeon_b2b_dsd_bar4_addr32,
  123. xeon_b2b_dsd_addr.bar4_addr32, ullong, 0644);
  124. MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
  125. "XEON B2B DSD split-BAR 4 32-bit address");
  126. module_param_named(xeon_b2b_dsd_bar5_addr32,
  127. xeon_b2b_dsd_addr.bar5_addr32, ullong, 0644);
  128. MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
  129. "XEON B2B DSD split-BAR 5 32-bit address");
  130. #ifndef ioread64
  131. #ifdef readq
  132. #define ioread64 readq
  133. #else
  134. #define ioread64 _ioread64
  135. static inline u64 _ioread64(void __iomem *mmio)
  136. {
  137. u64 low, high;
  138. low = ioread32(mmio);
  139. high = ioread32(mmio + sizeof(u32));
  140. return low | (high << 32);
  141. }
  142. #endif
  143. #endif
  144. #ifndef iowrite64
  145. #ifdef writeq
  146. #define iowrite64 writeq
  147. #else
  148. #define iowrite64 _iowrite64
  149. static inline void _iowrite64(u64 val, void __iomem *mmio)
  150. {
  151. iowrite32(val, mmio);
  152. iowrite32(val >> 32, mmio + sizeof(u32));
  153. }
  154. #endif
  155. #endif
  156. static inline int pdev_is_atom(struct pci_dev *pdev)
  157. {
  158. switch (pdev->device) {
  159. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static inline int pdev_is_xeon(struct pci_dev *pdev)
  165. {
  166. switch (pdev->device) {
  167. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  168. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  169. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  170. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  171. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  172. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  173. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  174. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  175. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  176. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  177. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  178. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  179. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  180. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  181. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. static inline void ndev_reset_unsafe_flags(struct intel_ntb_dev *ndev)
  187. {
  188. ndev->unsafe_flags = 0;
  189. ndev->unsafe_flags_ignore = 0;
  190. /* Only B2B has a workaround to avoid SDOORBELL */
  191. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP)
  192. if (!ntb_topo_is_b2b(ndev->ntb.topo))
  193. ndev->unsafe_flags |= NTB_UNSAFE_DB;
  194. /* No low level workaround to avoid SB01BASE */
  195. if (ndev->hwerr_flags & NTB_HWERR_SB01BASE_LOCKUP) {
  196. ndev->unsafe_flags |= NTB_UNSAFE_DB;
  197. ndev->unsafe_flags |= NTB_UNSAFE_SPAD;
  198. }
  199. }
  200. static inline int ndev_is_unsafe(struct intel_ntb_dev *ndev,
  201. unsigned long flag)
  202. {
  203. return !!(flag & ndev->unsafe_flags & ~ndev->unsafe_flags_ignore);
  204. }
  205. static inline int ndev_ignore_unsafe(struct intel_ntb_dev *ndev,
  206. unsigned long flag)
  207. {
  208. flag &= ndev->unsafe_flags;
  209. ndev->unsafe_flags_ignore |= flag;
  210. return !!flag;
  211. }
  212. static int ndev_mw_to_bar(struct intel_ntb_dev *ndev, int idx)
  213. {
  214. if (idx < 0 || idx >= ndev->mw_count)
  215. return -EINVAL;
  216. return ndev->reg->mw_bar[idx];
  217. }
  218. static inline int ndev_db_addr(struct intel_ntb_dev *ndev,
  219. phys_addr_t *db_addr, resource_size_t *db_size,
  220. phys_addr_t reg_addr, unsigned long reg)
  221. {
  222. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  223. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  224. if (db_addr) {
  225. *db_addr = reg_addr + reg;
  226. dev_dbg(ndev_dev(ndev), "Peer db addr %llx\n", *db_addr);
  227. }
  228. if (db_size) {
  229. *db_size = ndev->reg->db_size;
  230. dev_dbg(ndev_dev(ndev), "Peer db size %llx\n", *db_size);
  231. }
  232. return 0;
  233. }
  234. static inline u64 ndev_db_read(struct intel_ntb_dev *ndev,
  235. void __iomem *mmio)
  236. {
  237. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  238. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  239. return ndev->reg->db_ioread(mmio);
  240. }
  241. static inline int ndev_db_write(struct intel_ntb_dev *ndev, u64 db_bits,
  242. void __iomem *mmio)
  243. {
  244. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  245. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  246. if (db_bits & ~ndev->db_valid_mask)
  247. return -EINVAL;
  248. ndev->reg->db_iowrite(db_bits, mmio);
  249. return 0;
  250. }
  251. static inline int ndev_db_set_mask(struct intel_ntb_dev *ndev, u64 db_bits,
  252. void __iomem *mmio)
  253. {
  254. unsigned long irqflags;
  255. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  256. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  257. if (db_bits & ~ndev->db_valid_mask)
  258. return -EINVAL;
  259. spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
  260. {
  261. ndev->db_mask |= db_bits;
  262. ndev->reg->db_iowrite(ndev->db_mask, mmio);
  263. }
  264. spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
  265. return 0;
  266. }
  267. static inline int ndev_db_clear_mask(struct intel_ntb_dev *ndev, u64 db_bits,
  268. void __iomem *mmio)
  269. {
  270. unsigned long irqflags;
  271. if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
  272. pr_warn_once("%s: NTB unsafe doorbell access", __func__);
  273. if (db_bits & ~ndev->db_valid_mask)
  274. return -EINVAL;
  275. spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
  276. {
  277. ndev->db_mask &= ~db_bits;
  278. ndev->reg->db_iowrite(ndev->db_mask, mmio);
  279. }
  280. spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
  281. return 0;
  282. }
  283. static inline int ndev_vec_mask(struct intel_ntb_dev *ndev, int db_vector)
  284. {
  285. u64 shift, mask;
  286. shift = ndev->db_vec_shift;
  287. mask = BIT_ULL(shift) - 1;
  288. return mask << (shift * db_vector);
  289. }
  290. static inline int ndev_spad_addr(struct intel_ntb_dev *ndev, int idx,
  291. phys_addr_t *spad_addr, phys_addr_t reg_addr,
  292. unsigned long reg)
  293. {
  294. if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
  295. pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
  296. if (idx < 0 || idx >= ndev->spad_count)
  297. return -EINVAL;
  298. if (spad_addr) {
  299. *spad_addr = reg_addr + reg + (idx << 2);
  300. dev_dbg(ndev_dev(ndev), "Peer spad addr %llx\n", *spad_addr);
  301. }
  302. return 0;
  303. }
  304. static inline u32 ndev_spad_read(struct intel_ntb_dev *ndev, int idx,
  305. void __iomem *mmio)
  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 0;
  311. return ioread32(mmio + (idx << 2));
  312. }
  313. static inline int ndev_spad_write(struct intel_ntb_dev *ndev, int idx, u32 val,
  314. void __iomem *mmio)
  315. {
  316. if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
  317. pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
  318. if (idx < 0 || idx >= ndev->spad_count)
  319. return -EINVAL;
  320. iowrite32(val, mmio + (idx << 2));
  321. return 0;
  322. }
  323. static irqreturn_t ndev_interrupt(struct intel_ntb_dev *ndev, int vec)
  324. {
  325. u64 vec_mask;
  326. vec_mask = ndev_vec_mask(ndev, vec);
  327. dev_dbg(ndev_dev(ndev), "vec %d vec_mask %llx\n", vec, vec_mask);
  328. ndev->last_ts = jiffies;
  329. if (vec_mask & ndev->db_link_mask) {
  330. if (ndev->reg->poll_link(ndev))
  331. ntb_link_event(&ndev->ntb);
  332. }
  333. if (vec_mask & ndev->db_valid_mask)
  334. ntb_db_event(&ndev->ntb, vec);
  335. return IRQ_HANDLED;
  336. }
  337. static irqreturn_t ndev_vec_isr(int irq, void *dev)
  338. {
  339. struct intel_ntb_vec *nvec = dev;
  340. return ndev_interrupt(nvec->ndev, nvec->num);
  341. }
  342. static irqreturn_t ndev_irq_isr(int irq, void *dev)
  343. {
  344. struct intel_ntb_dev *ndev = dev;
  345. return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
  346. }
  347. static int ndev_init_isr(struct intel_ntb_dev *ndev,
  348. int msix_min, int msix_max,
  349. int msix_shift, int total_shift)
  350. {
  351. struct pci_dev *pdev;
  352. int rc, i, msix_count, node;
  353. pdev = ndev_pdev(ndev);
  354. node = dev_to_node(&pdev->dev);
  355. /* Mask all doorbell interrupts */
  356. ndev->db_mask = ndev->db_valid_mask;
  357. ndev->reg->db_iowrite(ndev->db_mask,
  358. ndev->self_mmio +
  359. ndev->self_reg->db_mask);
  360. /* Try to set up msix irq */
  361. ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
  362. GFP_KERNEL, node);
  363. if (!ndev->vec)
  364. goto err_msix_vec_alloc;
  365. ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
  366. GFP_KERNEL, node);
  367. if (!ndev->msix)
  368. goto err_msix_alloc;
  369. for (i = 0; i < msix_max; ++i)
  370. ndev->msix[i].entry = i;
  371. msix_count = pci_enable_msix_range(pdev, ndev->msix,
  372. msix_min, msix_max);
  373. if (msix_count < 0)
  374. goto err_msix_enable;
  375. for (i = 0; i < msix_count; ++i) {
  376. ndev->vec[i].ndev = ndev;
  377. ndev->vec[i].num = i;
  378. rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
  379. "ndev_vec_isr", &ndev->vec[i]);
  380. if (rc)
  381. goto err_msix_request;
  382. }
  383. dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
  384. ndev->db_vec_count = msix_count;
  385. ndev->db_vec_shift = msix_shift;
  386. return 0;
  387. err_msix_request:
  388. while (i-- > 0)
  389. free_irq(ndev->msix[i].vector, ndev);
  390. pci_disable_msix(pdev);
  391. err_msix_enable:
  392. kfree(ndev->msix);
  393. err_msix_alloc:
  394. kfree(ndev->vec);
  395. err_msix_vec_alloc:
  396. ndev->msix = NULL;
  397. ndev->vec = NULL;
  398. /* Try to set up msi irq */
  399. rc = pci_enable_msi(pdev);
  400. if (rc)
  401. goto err_msi_enable;
  402. rc = request_irq(pdev->irq, ndev_irq_isr, 0,
  403. "ndev_irq_isr", ndev);
  404. if (rc)
  405. goto err_msi_request;
  406. dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
  407. ndev->db_vec_count = 1;
  408. ndev->db_vec_shift = total_shift;
  409. return 0;
  410. err_msi_request:
  411. pci_disable_msi(pdev);
  412. err_msi_enable:
  413. /* Try to set up intx irq */
  414. pci_intx(pdev, 1);
  415. rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
  416. "ndev_irq_isr", ndev);
  417. if (rc)
  418. goto err_intx_request;
  419. dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
  420. ndev->db_vec_count = 1;
  421. ndev->db_vec_shift = total_shift;
  422. return 0;
  423. err_intx_request:
  424. return rc;
  425. }
  426. static void ndev_deinit_isr(struct intel_ntb_dev *ndev)
  427. {
  428. struct pci_dev *pdev;
  429. int i;
  430. pdev = ndev_pdev(ndev);
  431. /* Mask all doorbell interrupts */
  432. ndev->db_mask = ndev->db_valid_mask;
  433. ndev->reg->db_iowrite(ndev->db_mask,
  434. ndev->self_mmio +
  435. ndev->self_reg->db_mask);
  436. if (ndev->msix) {
  437. i = ndev->db_vec_count;
  438. while (i--)
  439. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  440. pci_disable_msix(pdev);
  441. kfree(ndev->msix);
  442. kfree(ndev->vec);
  443. } else {
  444. free_irq(pdev->irq, ndev);
  445. if (pci_dev_msi_enabled(pdev))
  446. pci_disable_msi(pdev);
  447. }
  448. }
  449. static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
  450. size_t count, loff_t *offp)
  451. {
  452. struct intel_ntb_dev *ndev;
  453. struct pci_dev *pdev;
  454. void __iomem *mmio;
  455. char *buf;
  456. size_t buf_size;
  457. ssize_t ret, off;
  458. union { u64 v64; u32 v32; u16 v16; u8 v8; } u;
  459. ndev = filp->private_data;
  460. pdev = ndev_pdev(ndev);
  461. mmio = ndev->self_mmio;
  462. buf_size = min(count, 0x800ul);
  463. buf = kmalloc(buf_size, GFP_KERNEL);
  464. if (!buf)
  465. return -ENOMEM;
  466. off = 0;
  467. off += scnprintf(buf + off, buf_size - off,
  468. "NTB Device Information:\n");
  469. off += scnprintf(buf + off, buf_size - off,
  470. "Connection Topology -\t%s\n",
  471. ntb_topo_string(ndev->ntb.topo));
  472. if (ndev->b2b_idx != UINT_MAX) {
  473. off += scnprintf(buf + off, buf_size - off,
  474. "B2B MW Idx -\t\t%u\n", ndev->b2b_idx);
  475. off += scnprintf(buf + off, buf_size - off,
  476. "B2B Offset -\t\t%#lx\n", ndev->b2b_off);
  477. }
  478. off += scnprintf(buf + off, buf_size - off,
  479. "BAR4 Split -\t\t%s\n",
  480. ndev->bar4_split ? "yes" : "no");
  481. off += scnprintf(buf + off, buf_size - off,
  482. "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
  483. off += scnprintf(buf + off, buf_size - off,
  484. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  485. if (!ndev->reg->link_is_up(ndev)) {
  486. off += scnprintf(buf + off, buf_size - off,
  487. "Link Status -\t\tDown\n");
  488. } else {
  489. off += scnprintf(buf + off, buf_size - off,
  490. "Link Status -\t\tUp\n");
  491. off += scnprintf(buf + off, buf_size - off,
  492. "Link Speed -\t\tPCI-E Gen %u\n",
  493. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  494. off += scnprintf(buf + off, buf_size - off,
  495. "Link Width -\t\tx%u\n",
  496. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  497. }
  498. off += scnprintf(buf + off, buf_size - off,
  499. "Memory Window Count -\t%u\n", ndev->mw_count);
  500. off += scnprintf(buf + off, buf_size - off,
  501. "Scratchpad Count -\t%u\n", ndev->spad_count);
  502. off += scnprintf(buf + off, buf_size - off,
  503. "Doorbell Count -\t%u\n", ndev->db_count);
  504. off += scnprintf(buf + off, buf_size - off,
  505. "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
  506. off += scnprintf(buf + off, buf_size - off,
  507. "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
  508. off += scnprintf(buf + off, buf_size - off,
  509. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  510. off += scnprintf(buf + off, buf_size - off,
  511. "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
  512. off += scnprintf(buf + off, buf_size - off,
  513. "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
  514. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
  515. off += scnprintf(buf + off, buf_size - off,
  516. "Doorbell Mask -\t\t%#llx\n", u.v64);
  517. u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
  518. off += scnprintf(buf + off, buf_size - off,
  519. "Doorbell Bell -\t\t%#llx\n", u.v64);
  520. off += scnprintf(buf + off, buf_size - off,
  521. "\nNTB Window Size:\n");
  522. pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &u.v8);
  523. off += scnprintf(buf + off, buf_size - off,
  524. "PBAR23SZ %hhu\n", u.v8);
  525. if (!ndev->bar4_split) {
  526. pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &u.v8);
  527. off += scnprintf(buf + off, buf_size - off,
  528. "PBAR45SZ %hhu\n", u.v8);
  529. } else {
  530. pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &u.v8);
  531. off += scnprintf(buf + off, buf_size - off,
  532. "PBAR4SZ %hhu\n", u.v8);
  533. pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &u.v8);
  534. off += scnprintf(buf + off, buf_size - off,
  535. "PBAR5SZ %hhu\n", u.v8);
  536. }
  537. pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &u.v8);
  538. off += scnprintf(buf + off, buf_size - off,
  539. "SBAR23SZ %hhu\n", u.v8);
  540. if (!ndev->bar4_split) {
  541. pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &u.v8);
  542. off += scnprintf(buf + off, buf_size - off,
  543. "SBAR45SZ %hhu\n", u.v8);
  544. } else {
  545. pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &u.v8);
  546. off += scnprintf(buf + off, buf_size - off,
  547. "SBAR4SZ %hhu\n", u.v8);
  548. pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &u.v8);
  549. off += scnprintf(buf + off, buf_size - off,
  550. "SBAR5SZ %hhu\n", u.v8);
  551. }
  552. off += scnprintf(buf + off, buf_size - off,
  553. "\nNTB Incoming XLAT:\n");
  554. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 2));
  555. off += scnprintf(buf + off, buf_size - off,
  556. "XLAT23 -\t\t%#018llx\n", u.v64);
  557. if (ndev->bar4_split) {
  558. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
  559. off += scnprintf(buf + off, buf_size - off,
  560. "XLAT4 -\t\t\t%#06x\n", u.v32);
  561. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 5));
  562. off += scnprintf(buf + off, buf_size - off,
  563. "XLAT5 -\t\t\t%#06x\n", u.v32);
  564. } else {
  565. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
  566. off += scnprintf(buf + off, buf_size - off,
  567. "XLAT45 -\t\t%#018llx\n", u.v64);
  568. }
  569. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 2));
  570. off += scnprintf(buf + off, buf_size - off,
  571. "LMT23 -\t\t\t%#018llx\n", u.v64);
  572. if (ndev->bar4_split) {
  573. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
  574. off += scnprintf(buf + off, buf_size - off,
  575. "LMT4 -\t\t\t%#06x\n", u.v32);
  576. u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 5));
  577. off += scnprintf(buf + off, buf_size - off,
  578. "LMT5 -\t\t\t%#06x\n", u.v32);
  579. } else {
  580. u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
  581. off += scnprintf(buf + off, buf_size - off,
  582. "LMT45 -\t\t\t%#018llx\n", u.v64);
  583. }
  584. if (pdev_is_xeon(pdev)) {
  585. if (ntb_topo_is_b2b(ndev->ntb.topo)) {
  586. off += scnprintf(buf + off, buf_size - off,
  587. "\nNTB Outgoing B2B XLAT:\n");
  588. u.v64 = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
  589. off += scnprintf(buf + off, buf_size - off,
  590. "B2B XLAT23 -\t\t%#018llx\n", u.v64);
  591. if (ndev->bar4_split) {
  592. u.v32 = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
  593. off += scnprintf(buf + off, buf_size - off,
  594. "B2B XLAT4 -\t\t%#06x\n",
  595. u.v32);
  596. u.v32 = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
  597. off += scnprintf(buf + off, buf_size - off,
  598. "B2B XLAT5 -\t\t%#06x\n",
  599. u.v32);
  600. } else {
  601. u.v64 = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
  602. off += scnprintf(buf + off, buf_size - off,
  603. "B2B XLAT45 -\t\t%#018llx\n",
  604. u.v64);
  605. }
  606. u.v64 = ioread64(mmio + XEON_PBAR23LMT_OFFSET);
  607. off += scnprintf(buf + off, buf_size - off,
  608. "B2B LMT23 -\t\t%#018llx\n", u.v64);
  609. if (ndev->bar4_split) {
  610. u.v32 = ioread32(mmio + XEON_PBAR4LMT_OFFSET);
  611. off += scnprintf(buf + off, buf_size - off,
  612. "B2B LMT4 -\t\t%#06x\n",
  613. u.v32);
  614. u.v32 = ioread32(mmio + XEON_PBAR5LMT_OFFSET);
  615. off += scnprintf(buf + off, buf_size - off,
  616. "B2B LMT5 -\t\t%#06x\n",
  617. u.v32);
  618. } else {
  619. u.v64 = ioread64(mmio + XEON_PBAR45LMT_OFFSET);
  620. off += scnprintf(buf + off, buf_size - off,
  621. "B2B LMT45 -\t\t%#018llx\n",
  622. u.v64);
  623. }
  624. off += scnprintf(buf + off, buf_size - off,
  625. "\nNTB Secondary BAR:\n");
  626. u.v64 = ioread64(mmio + XEON_SBAR0BASE_OFFSET);
  627. off += scnprintf(buf + off, buf_size - off,
  628. "SBAR01 -\t\t%#018llx\n", u.v64);
  629. u.v64 = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
  630. off += scnprintf(buf + off, buf_size - off,
  631. "SBAR23 -\t\t%#018llx\n", u.v64);
  632. if (ndev->bar4_split) {
  633. u.v32 = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
  634. off += scnprintf(buf + off, buf_size - off,
  635. "SBAR4 -\t\t\t%#06x\n", u.v32);
  636. u.v32 = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
  637. off += scnprintf(buf + off, buf_size - off,
  638. "SBAR5 -\t\t\t%#06x\n", u.v32);
  639. } else {
  640. u.v64 = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
  641. off += scnprintf(buf + off, buf_size - off,
  642. "SBAR45 -\t\t%#018llx\n",
  643. u.v64);
  644. }
  645. }
  646. off += scnprintf(buf + off, buf_size - off,
  647. "\nXEON NTB Statistics:\n");
  648. u.v16 = ioread16(mmio + XEON_USMEMMISS_OFFSET);
  649. off += scnprintf(buf + off, buf_size - off,
  650. "Upstream Memory Miss -\t%u\n", u.v16);
  651. off += scnprintf(buf + off, buf_size - off,
  652. "\nXEON NTB Hardware Errors:\n");
  653. if (!pci_read_config_word(pdev,
  654. XEON_DEVSTS_OFFSET, &u.v16))
  655. off += scnprintf(buf + off, buf_size - off,
  656. "DEVSTS -\t\t%#06x\n", u.v16);
  657. if (!pci_read_config_word(pdev,
  658. XEON_LINK_STATUS_OFFSET, &u.v16))
  659. off += scnprintf(buf + off, buf_size - off,
  660. "LNKSTS -\t\t%#06x\n", u.v16);
  661. if (!pci_read_config_dword(pdev,
  662. XEON_UNCERRSTS_OFFSET, &u.v32))
  663. off += scnprintf(buf + off, buf_size - off,
  664. "UNCERRSTS -\t\t%#06x\n", u.v32);
  665. if (!pci_read_config_dword(pdev,
  666. XEON_CORERRSTS_OFFSET, &u.v32))
  667. off += scnprintf(buf + off, buf_size - off,
  668. "CORERRSTS -\t\t%#06x\n", u.v32);
  669. }
  670. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  671. kfree(buf);
  672. return ret;
  673. }
  674. static void ndev_init_debugfs(struct intel_ntb_dev *ndev)
  675. {
  676. if (!debugfs_dir) {
  677. ndev->debugfs_dir = NULL;
  678. ndev->debugfs_info = NULL;
  679. } else {
  680. ndev->debugfs_dir =
  681. debugfs_create_dir(ndev_name(ndev), debugfs_dir);
  682. if (!ndev->debugfs_dir)
  683. ndev->debugfs_info = NULL;
  684. else
  685. ndev->debugfs_info =
  686. debugfs_create_file("info", S_IRUSR,
  687. ndev->debugfs_dir, ndev,
  688. &intel_ntb_debugfs_info);
  689. }
  690. }
  691. static void ndev_deinit_debugfs(struct intel_ntb_dev *ndev)
  692. {
  693. debugfs_remove_recursive(ndev->debugfs_dir);
  694. }
  695. static int intel_ntb_mw_count(struct ntb_dev *ntb)
  696. {
  697. return ntb_ndev(ntb)->mw_count;
  698. }
  699. static int intel_ntb_mw_get_range(struct ntb_dev *ntb, int idx,
  700. phys_addr_t *base,
  701. resource_size_t *size,
  702. resource_size_t *align,
  703. resource_size_t *align_size)
  704. {
  705. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  706. int bar;
  707. if (idx >= ndev->b2b_idx && !ndev->b2b_off)
  708. idx += 1;
  709. bar = ndev_mw_to_bar(ndev, idx);
  710. if (bar < 0)
  711. return bar;
  712. if (base)
  713. *base = pci_resource_start(ndev->ntb.pdev, bar) +
  714. (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
  715. if (size)
  716. *size = pci_resource_len(ndev->ntb.pdev, bar) -
  717. (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
  718. if (align)
  719. *align = pci_resource_len(ndev->ntb.pdev, bar);
  720. if (align_size)
  721. *align_size = 1;
  722. return 0;
  723. }
  724. static int intel_ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
  725. dma_addr_t addr, resource_size_t size)
  726. {
  727. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  728. unsigned long base_reg, xlat_reg, limit_reg;
  729. resource_size_t bar_size, mw_size;
  730. void __iomem *mmio;
  731. u64 base, limit, reg_val;
  732. int bar;
  733. if (idx >= ndev->b2b_idx && !ndev->b2b_off)
  734. idx += 1;
  735. bar = ndev_mw_to_bar(ndev, idx);
  736. if (bar < 0)
  737. return bar;
  738. bar_size = pci_resource_len(ndev->ntb.pdev, bar);
  739. if (idx == ndev->b2b_idx)
  740. mw_size = bar_size - ndev->b2b_off;
  741. else
  742. mw_size = bar_size;
  743. /* hardware requires that addr is aligned to bar size */
  744. if (addr & (bar_size - 1))
  745. return -EINVAL;
  746. /* make sure the range fits in the usable mw size */
  747. if (size > mw_size)
  748. return -EINVAL;
  749. mmio = ndev->self_mmio;
  750. base_reg = bar0_off(ndev->xlat_reg->bar0_base, bar);
  751. xlat_reg = bar2_off(ndev->xlat_reg->bar2_xlat, bar);
  752. limit_reg = bar2_off(ndev->xlat_reg->bar2_limit, bar);
  753. if (bar < 4 || !ndev->bar4_split) {
  754. base = ioread64(mmio + base_reg) & NTB_BAR_MASK_64;
  755. /* Set the limit if supported, if size is not mw_size */
  756. if (limit_reg && size != mw_size)
  757. limit = base + size;
  758. else
  759. limit = 0;
  760. /* set and verify setting the translation address */
  761. iowrite64(addr, mmio + xlat_reg);
  762. reg_val = ioread64(mmio + xlat_reg);
  763. if (reg_val != addr) {
  764. iowrite64(0, mmio + xlat_reg);
  765. return -EIO;
  766. }
  767. /* set and verify setting the limit */
  768. iowrite64(limit, mmio + limit_reg);
  769. reg_val = ioread64(mmio + limit_reg);
  770. if (reg_val != limit) {
  771. iowrite64(base, mmio + limit_reg);
  772. iowrite64(0, mmio + xlat_reg);
  773. return -EIO;
  774. }
  775. } else {
  776. /* split bar addr range must all be 32 bit */
  777. if (addr & (~0ull << 32))
  778. return -EINVAL;
  779. if ((addr + size) & (~0ull << 32))
  780. return -EINVAL;
  781. base = ioread32(mmio + base_reg) & NTB_BAR_MASK_32;
  782. /* Set the limit if supported, if size is not mw_size */
  783. if (limit_reg && size != mw_size)
  784. limit = base + size;
  785. else
  786. limit = 0;
  787. /* set and verify setting the translation address */
  788. iowrite32(addr, mmio + xlat_reg);
  789. reg_val = ioread32(mmio + xlat_reg);
  790. if (reg_val != addr) {
  791. iowrite32(0, mmio + xlat_reg);
  792. return -EIO;
  793. }
  794. /* set and verify setting the limit */
  795. iowrite32(limit, mmio + limit_reg);
  796. reg_val = ioread32(mmio + limit_reg);
  797. if (reg_val != limit) {
  798. iowrite32(base, mmio + limit_reg);
  799. iowrite32(0, mmio + xlat_reg);
  800. return -EIO;
  801. }
  802. }
  803. return 0;
  804. }
  805. static int intel_ntb_link_is_up(struct ntb_dev *ntb,
  806. enum ntb_speed *speed,
  807. enum ntb_width *width)
  808. {
  809. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  810. if (ndev->reg->link_is_up(ndev)) {
  811. if (speed)
  812. *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
  813. if (width)
  814. *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
  815. return 1;
  816. } else {
  817. /* TODO MAYBE: is it possible to observe the link speed and
  818. * width while link is training? */
  819. if (speed)
  820. *speed = NTB_SPEED_NONE;
  821. if (width)
  822. *width = NTB_WIDTH_NONE;
  823. return 0;
  824. }
  825. }
  826. static int intel_ntb_link_enable(struct ntb_dev *ntb,
  827. enum ntb_speed max_speed,
  828. enum ntb_width max_width)
  829. {
  830. struct intel_ntb_dev *ndev;
  831. u32 ntb_ctl;
  832. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  833. if (ndev->ntb.topo == NTB_TOPO_SEC)
  834. return -EINVAL;
  835. dev_dbg(ndev_dev(ndev),
  836. "Enabling link with max_speed %d max_width %d\n",
  837. max_speed, max_width);
  838. if (max_speed != NTB_SPEED_AUTO)
  839. dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
  840. if (max_width != NTB_WIDTH_AUTO)
  841. dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
  842. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  843. ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
  844. ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
  845. ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
  846. if (ndev->bar4_split)
  847. ntb_ctl |= NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP;
  848. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  849. return 0;
  850. }
  851. static int intel_ntb_link_disable(struct ntb_dev *ntb)
  852. {
  853. struct intel_ntb_dev *ndev;
  854. u32 ntb_cntl;
  855. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  856. if (ndev->ntb.topo == NTB_TOPO_SEC)
  857. return -EINVAL;
  858. dev_dbg(ndev_dev(ndev), "Disabling link\n");
  859. /* Bring NTB link down */
  860. ntb_cntl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  861. ntb_cntl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
  862. ntb_cntl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
  863. if (ndev->bar4_split)
  864. ntb_cntl &= ~(NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP);
  865. ntb_cntl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
  866. iowrite32(ntb_cntl, ndev->self_mmio + ndev->reg->ntb_ctl);
  867. return 0;
  868. }
  869. static int intel_ntb_db_is_unsafe(struct ntb_dev *ntb)
  870. {
  871. return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_DB);
  872. }
  873. static u64 intel_ntb_db_valid_mask(struct ntb_dev *ntb)
  874. {
  875. return ntb_ndev(ntb)->db_valid_mask;
  876. }
  877. static int intel_ntb_db_vector_count(struct ntb_dev *ntb)
  878. {
  879. struct intel_ntb_dev *ndev;
  880. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  881. return ndev->db_vec_count;
  882. }
  883. static u64 intel_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
  884. {
  885. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  886. if (db_vector < 0 || db_vector > ndev->db_vec_count)
  887. return 0;
  888. return ndev->db_valid_mask & ndev_vec_mask(ndev, db_vector);
  889. }
  890. static u64 intel_ntb_db_read(struct ntb_dev *ntb)
  891. {
  892. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  893. return ndev_db_read(ndev,
  894. ndev->self_mmio +
  895. ndev->self_reg->db_bell);
  896. }
  897. static int intel_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  898. {
  899. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  900. return ndev_db_write(ndev, db_bits,
  901. ndev->self_mmio +
  902. ndev->self_reg->db_bell);
  903. }
  904. static int intel_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  905. {
  906. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  907. return ndev_db_set_mask(ndev, db_bits,
  908. ndev->self_mmio +
  909. ndev->self_reg->db_mask);
  910. }
  911. static int intel_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  912. {
  913. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  914. return ndev_db_clear_mask(ndev, db_bits,
  915. ndev->self_mmio +
  916. ndev->self_reg->db_mask);
  917. }
  918. static int intel_ntb_peer_db_addr(struct ntb_dev *ntb,
  919. phys_addr_t *db_addr,
  920. resource_size_t *db_size)
  921. {
  922. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  923. return ndev_db_addr(ndev, db_addr, db_size, ndev->peer_addr,
  924. ndev->peer_reg->db_bell);
  925. }
  926. static int intel_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  927. {
  928. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  929. return ndev_db_write(ndev, db_bits,
  930. ndev->peer_mmio +
  931. ndev->peer_reg->db_bell);
  932. }
  933. static int intel_ntb_spad_is_unsafe(struct ntb_dev *ntb)
  934. {
  935. return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_SPAD);
  936. }
  937. static int intel_ntb_spad_count(struct ntb_dev *ntb)
  938. {
  939. struct intel_ntb_dev *ndev;
  940. ndev = container_of(ntb, struct intel_ntb_dev, ntb);
  941. return ndev->spad_count;
  942. }
  943. static u32 intel_ntb_spad_read(struct ntb_dev *ntb, int idx)
  944. {
  945. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  946. return ndev_spad_read(ndev, idx,
  947. ndev->self_mmio +
  948. ndev->self_reg->spad);
  949. }
  950. static int intel_ntb_spad_write(struct ntb_dev *ntb,
  951. int idx, u32 val)
  952. {
  953. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  954. return ndev_spad_write(ndev, idx, val,
  955. ndev->self_mmio +
  956. ndev->self_reg->spad);
  957. }
  958. static int intel_ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
  959. phys_addr_t *spad_addr)
  960. {
  961. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  962. return ndev_spad_addr(ndev, idx, spad_addr, ndev->peer_addr,
  963. ndev->peer_reg->spad);
  964. }
  965. static u32 intel_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
  966. {
  967. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  968. return ndev_spad_read(ndev, idx,
  969. ndev->peer_mmio +
  970. ndev->peer_reg->spad);
  971. }
  972. static int intel_ntb_peer_spad_write(struct ntb_dev *ntb,
  973. int idx, u32 val)
  974. {
  975. struct intel_ntb_dev *ndev = ntb_ndev(ntb);
  976. return ndev_spad_write(ndev, idx, val,
  977. ndev->peer_mmio +
  978. ndev->peer_reg->spad);
  979. }
  980. /* ATOM */
  981. static u64 atom_db_ioread(void __iomem *mmio)
  982. {
  983. return ioread64(mmio);
  984. }
  985. static void atom_db_iowrite(u64 bits, void __iomem *mmio)
  986. {
  987. iowrite64(bits, mmio);
  988. }
  989. static int atom_poll_link(struct intel_ntb_dev *ndev)
  990. {
  991. u32 ntb_ctl;
  992. ntb_ctl = ioread32(ndev->self_mmio + ATOM_NTBCNTL_OFFSET);
  993. if (ntb_ctl == ndev->ntb_ctl)
  994. return 0;
  995. ndev->ntb_ctl = ntb_ctl;
  996. ndev->lnk_sta = ioread32(ndev->self_mmio + ATOM_LINK_STATUS_OFFSET);
  997. return 1;
  998. }
  999. static int atom_link_is_up(struct intel_ntb_dev *ndev)
  1000. {
  1001. return ATOM_NTB_CTL_ACTIVE(ndev->ntb_ctl);
  1002. }
  1003. static int atom_link_is_err(struct intel_ntb_dev *ndev)
  1004. {
  1005. if (ioread32(ndev->self_mmio + ATOM_LTSSMSTATEJMP_OFFSET)
  1006. & ATOM_LTSSMSTATEJMP_FORCEDETECT)
  1007. return 1;
  1008. if (ioread32(ndev->self_mmio + ATOM_IBSTERRRCRVSTS0_OFFSET)
  1009. & ATOM_IBIST_ERR_OFLOW)
  1010. return 1;
  1011. return 0;
  1012. }
  1013. static inline enum ntb_topo atom_ppd_topo(struct intel_ntb_dev *ndev, u32 ppd)
  1014. {
  1015. switch (ppd & ATOM_PPD_TOPO_MASK) {
  1016. case ATOM_PPD_TOPO_B2B_USD:
  1017. dev_dbg(ndev_dev(ndev), "PPD %d B2B USD\n", ppd);
  1018. return NTB_TOPO_B2B_USD;
  1019. case ATOM_PPD_TOPO_B2B_DSD:
  1020. dev_dbg(ndev_dev(ndev), "PPD %d B2B DSD\n", ppd);
  1021. return NTB_TOPO_B2B_DSD;
  1022. case ATOM_PPD_TOPO_PRI_USD:
  1023. case ATOM_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
  1024. case ATOM_PPD_TOPO_SEC_USD:
  1025. case ATOM_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
  1026. dev_dbg(ndev_dev(ndev), "PPD %d non B2B disabled\n", ppd);
  1027. return NTB_TOPO_NONE;
  1028. }
  1029. dev_dbg(ndev_dev(ndev), "PPD %d invalid\n", ppd);
  1030. return NTB_TOPO_NONE;
  1031. }
  1032. static void atom_link_hb(struct work_struct *work)
  1033. {
  1034. struct intel_ntb_dev *ndev = hb_ndev(work);
  1035. unsigned long poll_ts;
  1036. void __iomem *mmio;
  1037. u32 status32;
  1038. poll_ts = ndev->last_ts + ATOM_LINK_HB_TIMEOUT;
  1039. /* Delay polling the link status if an interrupt was received,
  1040. * unless the cached link status says the link is down.
  1041. */
  1042. if (time_after(poll_ts, jiffies) && atom_link_is_up(ndev)) {
  1043. schedule_delayed_work(&ndev->hb_timer, poll_ts - jiffies);
  1044. return;
  1045. }
  1046. if (atom_poll_link(ndev))
  1047. ntb_link_event(&ndev->ntb);
  1048. if (atom_link_is_up(ndev) || !atom_link_is_err(ndev)) {
  1049. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
  1050. return;
  1051. }
  1052. /* Link is down with error: recover the link! */
  1053. mmio = ndev->self_mmio;
  1054. /* Driver resets the NTB ModPhy lanes - magic! */
  1055. iowrite8(0xe0, mmio + ATOM_MODPHY_PCSREG6);
  1056. iowrite8(0x40, mmio + ATOM_MODPHY_PCSREG4);
  1057. iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG4);
  1058. iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG6);
  1059. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  1060. msleep(100);
  1061. /* Clear AER Errors, write to clear */
  1062. status32 = ioread32(mmio + ATOM_ERRCORSTS_OFFSET);
  1063. dev_dbg(ndev_dev(ndev), "ERRCORSTS = %x\n", status32);
  1064. status32 &= PCI_ERR_COR_REP_ROLL;
  1065. iowrite32(status32, mmio + ATOM_ERRCORSTS_OFFSET);
  1066. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  1067. status32 = ioread32(mmio + ATOM_LTSSMERRSTS0_OFFSET);
  1068. dev_dbg(ndev_dev(ndev), "LTSSMERRSTS0 = %x\n", status32);
  1069. status32 |= ATOM_LTSSMERRSTS0_UNEXPECTEDEI;
  1070. iowrite32(status32, mmio + ATOM_LTSSMERRSTS0_OFFSET);
  1071. /* Clear DeSkew Buffer error, write to clear */
  1072. status32 = ioread32(mmio + ATOM_DESKEWSTS_OFFSET);
  1073. dev_dbg(ndev_dev(ndev), "DESKEWSTS = %x\n", status32);
  1074. status32 |= ATOM_DESKEWSTS_DBERR;
  1075. iowrite32(status32, mmio + ATOM_DESKEWSTS_OFFSET);
  1076. status32 = ioread32(mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
  1077. dev_dbg(ndev_dev(ndev), "IBSTERRRCRVSTS0 = %x\n", status32);
  1078. status32 &= ATOM_IBIST_ERR_OFLOW;
  1079. iowrite32(status32, mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
  1080. /* Releases the NTB state machine to allow the link to retrain */
  1081. status32 = ioread32(mmio + ATOM_LTSSMSTATEJMP_OFFSET);
  1082. dev_dbg(ndev_dev(ndev), "LTSSMSTATEJMP = %x\n", status32);
  1083. status32 &= ~ATOM_LTSSMSTATEJMP_FORCEDETECT;
  1084. iowrite32(status32, mmio + ATOM_LTSSMSTATEJMP_OFFSET);
  1085. /* There is a potential race between the 2 NTB devices recovering at the
  1086. * same time. If the times are the same, the link will not recover and
  1087. * the driver will be stuck in this loop forever. Add a random interval
  1088. * to the recovery time to prevent this race.
  1089. */
  1090. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_RECOVERY_TIME
  1091. + prandom_u32() % ATOM_LINK_RECOVERY_TIME);
  1092. }
  1093. static int atom_init_isr(struct intel_ntb_dev *ndev)
  1094. {
  1095. int rc;
  1096. rc = ndev_init_isr(ndev, 1, ATOM_DB_MSIX_VECTOR_COUNT,
  1097. ATOM_DB_MSIX_VECTOR_SHIFT, ATOM_DB_TOTAL_SHIFT);
  1098. if (rc)
  1099. return rc;
  1100. /* ATOM doesn't have link status interrupt, poll on that platform */
  1101. ndev->last_ts = jiffies;
  1102. INIT_DELAYED_WORK(&ndev->hb_timer, atom_link_hb);
  1103. schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
  1104. return 0;
  1105. }
  1106. static void atom_deinit_isr(struct intel_ntb_dev *ndev)
  1107. {
  1108. cancel_delayed_work_sync(&ndev->hb_timer);
  1109. ndev_deinit_isr(ndev);
  1110. }
  1111. static int atom_init_ntb(struct intel_ntb_dev *ndev)
  1112. {
  1113. ndev->mw_count = ATOM_MW_COUNT;
  1114. ndev->spad_count = ATOM_SPAD_COUNT;
  1115. ndev->db_count = ATOM_DB_COUNT;
  1116. switch (ndev->ntb.topo) {
  1117. case NTB_TOPO_B2B_USD:
  1118. case NTB_TOPO_B2B_DSD:
  1119. ndev->self_reg = &atom_pri_reg;
  1120. ndev->peer_reg = &atom_b2b_reg;
  1121. ndev->xlat_reg = &atom_sec_xlat;
  1122. /* Enable Bus Master and Memory Space on the secondary side */
  1123. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1124. ndev->self_mmio + ATOM_SPCICMD_OFFSET);
  1125. break;
  1126. default:
  1127. return -EINVAL;
  1128. }
  1129. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1130. return 0;
  1131. }
  1132. static int atom_init_dev(struct intel_ntb_dev *ndev)
  1133. {
  1134. u32 ppd;
  1135. int rc;
  1136. rc = pci_read_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET, &ppd);
  1137. if (rc)
  1138. return -EIO;
  1139. ndev->ntb.topo = atom_ppd_topo(ndev, ppd);
  1140. if (ndev->ntb.topo == NTB_TOPO_NONE)
  1141. return -EINVAL;
  1142. rc = atom_init_ntb(ndev);
  1143. if (rc)
  1144. return rc;
  1145. rc = atom_init_isr(ndev);
  1146. if (rc)
  1147. return rc;
  1148. if (ndev->ntb.topo != NTB_TOPO_SEC) {
  1149. /* Initiate PCI-E link training */
  1150. rc = pci_write_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET,
  1151. ppd | ATOM_PPD_INIT_LINK);
  1152. if (rc)
  1153. return rc;
  1154. }
  1155. return 0;
  1156. }
  1157. static void atom_deinit_dev(struct intel_ntb_dev *ndev)
  1158. {
  1159. atom_deinit_isr(ndev);
  1160. }
  1161. /* XEON */
  1162. static u64 xeon_db_ioread(void __iomem *mmio)
  1163. {
  1164. return (u64)ioread16(mmio);
  1165. }
  1166. static void xeon_db_iowrite(u64 bits, void __iomem *mmio)
  1167. {
  1168. iowrite16((u16)bits, mmio);
  1169. }
  1170. static int xeon_poll_link(struct intel_ntb_dev *ndev)
  1171. {
  1172. u16 reg_val;
  1173. int rc;
  1174. ndev->reg->db_iowrite(ndev->db_link_mask,
  1175. ndev->self_mmio +
  1176. ndev->self_reg->db_bell);
  1177. rc = pci_read_config_word(ndev->ntb.pdev,
  1178. XEON_LINK_STATUS_OFFSET, &reg_val);
  1179. if (rc)
  1180. return 0;
  1181. if (reg_val == ndev->lnk_sta)
  1182. return 0;
  1183. ndev->lnk_sta = reg_val;
  1184. return 1;
  1185. }
  1186. static int xeon_link_is_up(struct intel_ntb_dev *ndev)
  1187. {
  1188. if (ndev->ntb.topo == NTB_TOPO_SEC)
  1189. return 1;
  1190. return NTB_LNK_STA_ACTIVE(ndev->lnk_sta);
  1191. }
  1192. static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd)
  1193. {
  1194. switch (ppd & XEON_PPD_TOPO_MASK) {
  1195. case XEON_PPD_TOPO_B2B_USD:
  1196. return NTB_TOPO_B2B_USD;
  1197. case XEON_PPD_TOPO_B2B_DSD:
  1198. return NTB_TOPO_B2B_DSD;
  1199. case XEON_PPD_TOPO_PRI_USD:
  1200. case XEON_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
  1201. return NTB_TOPO_PRI;
  1202. case XEON_PPD_TOPO_SEC_USD:
  1203. case XEON_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
  1204. return NTB_TOPO_SEC;
  1205. }
  1206. return NTB_TOPO_NONE;
  1207. }
  1208. static inline int xeon_ppd_bar4_split(struct intel_ntb_dev *ndev, u8 ppd)
  1209. {
  1210. if (ppd & XEON_PPD_SPLIT_BAR_MASK) {
  1211. dev_dbg(ndev_dev(ndev), "PPD %d split bar\n", ppd);
  1212. return 1;
  1213. }
  1214. return 0;
  1215. }
  1216. static int xeon_init_isr(struct intel_ntb_dev *ndev)
  1217. {
  1218. return ndev_init_isr(ndev, XEON_DB_MSIX_VECTOR_COUNT,
  1219. XEON_DB_MSIX_VECTOR_COUNT,
  1220. XEON_DB_MSIX_VECTOR_SHIFT,
  1221. XEON_DB_TOTAL_SHIFT);
  1222. }
  1223. static void xeon_deinit_isr(struct intel_ntb_dev *ndev)
  1224. {
  1225. ndev_deinit_isr(ndev);
  1226. }
  1227. static int xeon_setup_b2b_mw(struct intel_ntb_dev *ndev,
  1228. const struct intel_b2b_addr *addr,
  1229. const struct intel_b2b_addr *peer_addr)
  1230. {
  1231. struct pci_dev *pdev;
  1232. void __iomem *mmio;
  1233. resource_size_t bar_size;
  1234. phys_addr_t bar_addr;
  1235. int b2b_bar;
  1236. u8 bar_sz;
  1237. pdev = ndev_pdev(ndev);
  1238. mmio = ndev->self_mmio;
  1239. if (ndev->b2b_idx == UINT_MAX) {
  1240. dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
  1241. b2b_bar = 0;
  1242. ndev->b2b_off = 0;
  1243. } else {
  1244. b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
  1245. if (b2b_bar < 0)
  1246. return -EIO;
  1247. dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
  1248. bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
  1249. dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
  1250. if (b2b_mw_share && XEON_B2B_MIN_SIZE <= bar_size >> 1) {
  1251. dev_dbg(ndev_dev(ndev),
  1252. "b2b using first half of bar\n");
  1253. ndev->b2b_off = bar_size >> 1;
  1254. } else if (XEON_B2B_MIN_SIZE <= bar_size) {
  1255. dev_dbg(ndev_dev(ndev),
  1256. "b2b using whole bar\n");
  1257. ndev->b2b_off = 0;
  1258. --ndev->mw_count;
  1259. } else {
  1260. dev_dbg(ndev_dev(ndev),
  1261. "b2b bar size is too small\n");
  1262. return -EIO;
  1263. }
  1264. }
  1265. /* Reset the secondary bar sizes to match the primary bar sizes,
  1266. * except disable or halve the size of the b2b secondary bar.
  1267. *
  1268. * Note: code for each specific bar size register, because the register
  1269. * offsets are not in a consistent order (bar5sz comes after ppd, odd).
  1270. */
  1271. pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &bar_sz);
  1272. dev_dbg(ndev_dev(ndev), "PBAR23SZ %#x\n", bar_sz);
  1273. if (b2b_bar == 2) {
  1274. if (ndev->b2b_off)
  1275. bar_sz -= 1;
  1276. else
  1277. bar_sz = 0;
  1278. }
  1279. pci_write_config_byte(pdev, XEON_SBAR23SZ_OFFSET, bar_sz);
  1280. pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &bar_sz);
  1281. dev_dbg(ndev_dev(ndev), "SBAR23SZ %#x\n", bar_sz);
  1282. if (!ndev->bar4_split) {
  1283. pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &bar_sz);
  1284. dev_dbg(ndev_dev(ndev), "PBAR45SZ %#x\n", bar_sz);
  1285. if (b2b_bar == 4) {
  1286. if (ndev->b2b_off)
  1287. bar_sz -= 1;
  1288. else
  1289. bar_sz = 0;
  1290. }
  1291. pci_write_config_byte(pdev, XEON_SBAR45SZ_OFFSET, bar_sz);
  1292. pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &bar_sz);
  1293. dev_dbg(ndev_dev(ndev), "SBAR45SZ %#x\n", bar_sz);
  1294. } else {
  1295. pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &bar_sz);
  1296. dev_dbg(ndev_dev(ndev), "PBAR4SZ %#x\n", bar_sz);
  1297. if (b2b_bar == 4) {
  1298. if (ndev->b2b_off)
  1299. bar_sz -= 1;
  1300. else
  1301. bar_sz = 0;
  1302. }
  1303. pci_write_config_byte(pdev, XEON_SBAR4SZ_OFFSET, bar_sz);
  1304. pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &bar_sz);
  1305. dev_dbg(ndev_dev(ndev), "SBAR4SZ %#x\n", bar_sz);
  1306. pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &bar_sz);
  1307. dev_dbg(ndev_dev(ndev), "PBAR5SZ %#x\n", bar_sz);
  1308. if (b2b_bar == 5) {
  1309. if (ndev->b2b_off)
  1310. bar_sz -= 1;
  1311. else
  1312. bar_sz = 0;
  1313. }
  1314. pci_write_config_byte(pdev, XEON_SBAR5SZ_OFFSET, bar_sz);
  1315. pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &bar_sz);
  1316. dev_dbg(ndev_dev(ndev), "SBAR5SZ %#x\n", bar_sz);
  1317. }
  1318. /* SBAR01 hit by first part of the b2b bar */
  1319. if (b2b_bar == 0)
  1320. bar_addr = addr->bar0_addr;
  1321. else if (b2b_bar == 2)
  1322. bar_addr = addr->bar2_addr64;
  1323. else if (b2b_bar == 4 && !ndev->bar4_split)
  1324. bar_addr = addr->bar4_addr64;
  1325. else if (b2b_bar == 4)
  1326. bar_addr = addr->bar4_addr32;
  1327. else if (b2b_bar == 5)
  1328. bar_addr = addr->bar5_addr32;
  1329. else
  1330. return -EIO;
  1331. dev_dbg(ndev_dev(ndev), "SBAR01 %#018llx\n", bar_addr);
  1332. iowrite64(bar_addr, mmio + XEON_SBAR0BASE_OFFSET);
  1333. /* Other SBAR are normally hit by the PBAR xlat, except for b2b bar.
  1334. * The b2b bar is either disabled above, or configured half-size, and
  1335. * it starts at the PBAR xlat + offset.
  1336. */
  1337. bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
  1338. iowrite64(bar_addr, mmio + XEON_SBAR23BASE_OFFSET);
  1339. bar_addr = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
  1340. dev_dbg(ndev_dev(ndev), "SBAR23 %#018llx\n", bar_addr);
  1341. if (!ndev->bar4_split) {
  1342. bar_addr = addr->bar4_addr64 +
  1343. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1344. iowrite64(bar_addr, mmio + XEON_SBAR45BASE_OFFSET);
  1345. bar_addr = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
  1346. dev_dbg(ndev_dev(ndev), "SBAR45 %#018llx\n", bar_addr);
  1347. } else {
  1348. bar_addr = addr->bar4_addr32 +
  1349. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1350. iowrite32(bar_addr, mmio + XEON_SBAR4BASE_OFFSET);
  1351. bar_addr = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
  1352. dev_dbg(ndev_dev(ndev), "SBAR4 %#010llx\n", bar_addr);
  1353. bar_addr = addr->bar5_addr32 +
  1354. (b2b_bar == 5 ? ndev->b2b_off : 0);
  1355. iowrite32(bar_addr, mmio + XEON_SBAR5BASE_OFFSET);
  1356. bar_addr = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
  1357. dev_dbg(ndev_dev(ndev), "SBAR5 %#010llx\n", bar_addr);
  1358. }
  1359. /* setup incoming bar limits == base addrs (zero length windows) */
  1360. bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
  1361. iowrite64(bar_addr, mmio + XEON_SBAR23LMT_OFFSET);
  1362. bar_addr = ioread64(mmio + XEON_SBAR23LMT_OFFSET);
  1363. dev_dbg(ndev_dev(ndev), "SBAR23LMT %#018llx\n", bar_addr);
  1364. if (!ndev->bar4_split) {
  1365. bar_addr = addr->bar4_addr64 +
  1366. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1367. iowrite64(bar_addr, mmio + XEON_SBAR45LMT_OFFSET);
  1368. bar_addr = ioread64(mmio + XEON_SBAR45LMT_OFFSET);
  1369. dev_dbg(ndev_dev(ndev), "SBAR45LMT %#018llx\n", bar_addr);
  1370. } else {
  1371. bar_addr = addr->bar4_addr32 +
  1372. (b2b_bar == 4 ? ndev->b2b_off : 0);
  1373. iowrite32(bar_addr, mmio + XEON_SBAR4LMT_OFFSET);
  1374. bar_addr = ioread32(mmio + XEON_SBAR4LMT_OFFSET);
  1375. dev_dbg(ndev_dev(ndev), "SBAR4LMT %#010llx\n", bar_addr);
  1376. bar_addr = addr->bar5_addr32 +
  1377. (b2b_bar == 5 ? ndev->b2b_off : 0);
  1378. iowrite32(bar_addr, mmio + XEON_SBAR5LMT_OFFSET);
  1379. bar_addr = ioread32(mmio + XEON_SBAR5LMT_OFFSET);
  1380. dev_dbg(ndev_dev(ndev), "SBAR5LMT %#05llx\n", bar_addr);
  1381. }
  1382. /* zero incoming translation addrs */
  1383. iowrite64(0, mmio + XEON_SBAR23XLAT_OFFSET);
  1384. if (!ndev->bar4_split) {
  1385. iowrite64(0, mmio + XEON_SBAR45XLAT_OFFSET);
  1386. } else {
  1387. iowrite32(0, mmio + XEON_SBAR4XLAT_OFFSET);
  1388. iowrite32(0, mmio + XEON_SBAR5XLAT_OFFSET);
  1389. }
  1390. /* zero outgoing translation limits (whole bar size windows) */
  1391. iowrite64(0, mmio + XEON_PBAR23LMT_OFFSET);
  1392. if (!ndev->bar4_split) {
  1393. iowrite64(0, mmio + XEON_PBAR45LMT_OFFSET);
  1394. } else {
  1395. iowrite32(0, mmio + XEON_PBAR4LMT_OFFSET);
  1396. iowrite32(0, mmio + XEON_PBAR5LMT_OFFSET);
  1397. }
  1398. /* set outgoing translation offsets */
  1399. bar_addr = peer_addr->bar2_addr64;
  1400. iowrite64(bar_addr, mmio + XEON_PBAR23XLAT_OFFSET);
  1401. bar_addr = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
  1402. dev_dbg(ndev_dev(ndev), "PBAR23XLAT %#018llx\n", bar_addr);
  1403. if (!ndev->bar4_split) {
  1404. bar_addr = peer_addr->bar4_addr64;
  1405. iowrite64(bar_addr, mmio + XEON_PBAR45XLAT_OFFSET);
  1406. bar_addr = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
  1407. dev_dbg(ndev_dev(ndev), "PBAR45XLAT %#018llx\n", bar_addr);
  1408. } else {
  1409. bar_addr = peer_addr->bar4_addr32;
  1410. iowrite32(bar_addr, mmio + XEON_PBAR4XLAT_OFFSET);
  1411. bar_addr = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
  1412. dev_dbg(ndev_dev(ndev), "PBAR4XLAT %#010llx\n", bar_addr);
  1413. bar_addr = peer_addr->bar5_addr32;
  1414. iowrite32(bar_addr, mmio + XEON_PBAR5XLAT_OFFSET);
  1415. bar_addr = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
  1416. dev_dbg(ndev_dev(ndev), "PBAR5XLAT %#010llx\n", bar_addr);
  1417. }
  1418. /* set the translation offset for b2b registers */
  1419. if (b2b_bar == 0)
  1420. bar_addr = peer_addr->bar0_addr;
  1421. else if (b2b_bar == 2)
  1422. bar_addr = peer_addr->bar2_addr64;
  1423. else if (b2b_bar == 4 && !ndev->bar4_split)
  1424. bar_addr = peer_addr->bar4_addr64;
  1425. else if (b2b_bar == 4)
  1426. bar_addr = peer_addr->bar4_addr32;
  1427. else if (b2b_bar == 5)
  1428. bar_addr = peer_addr->bar5_addr32;
  1429. else
  1430. return -EIO;
  1431. /* B2B_XLAT_OFFSET is 64bit, but can only take 32bit writes */
  1432. dev_dbg(ndev_dev(ndev), "B2BXLAT %#018llx\n", bar_addr);
  1433. iowrite32(bar_addr, mmio + XEON_B2B_XLAT_OFFSETL);
  1434. iowrite32(bar_addr >> 32, mmio + XEON_B2B_XLAT_OFFSETU);
  1435. if (b2b_bar) {
  1436. /* map peer ntb mmio config space registers */
  1437. ndev->peer_mmio = pci_iomap(pdev, b2b_bar,
  1438. XEON_B2B_MIN_SIZE);
  1439. if (!ndev->peer_mmio)
  1440. return -EIO;
  1441. }
  1442. return 0;
  1443. }
  1444. static int xeon_init_ntb(struct intel_ntb_dev *ndev)
  1445. {
  1446. int rc;
  1447. u32 ntb_ctl;
  1448. if (ndev->bar4_split)
  1449. ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
  1450. else
  1451. ndev->mw_count = XEON_MW_COUNT;
  1452. ndev->spad_count = XEON_SPAD_COUNT;
  1453. ndev->db_count = XEON_DB_COUNT;
  1454. ndev->db_link_mask = XEON_DB_LINK_BIT;
  1455. switch (ndev->ntb.topo) {
  1456. case NTB_TOPO_PRI:
  1457. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1458. dev_err(ndev_dev(ndev), "NTB Primary config disabled\n");
  1459. return -EINVAL;
  1460. }
  1461. /* enable link to allow secondary side device to appear */
  1462. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  1463. ntb_ctl &= ~NTB_CTL_DISABLE;
  1464. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1465. /* use half the spads for the peer */
  1466. ndev->spad_count >>= 1;
  1467. ndev->self_reg = &xeon_pri_reg;
  1468. ndev->peer_reg = &xeon_sec_reg;
  1469. ndev->xlat_reg = &xeon_sec_xlat;
  1470. break;
  1471. case NTB_TOPO_SEC:
  1472. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1473. dev_err(ndev_dev(ndev), "NTB Secondary config disabled\n");
  1474. return -EINVAL;
  1475. }
  1476. /* use half the spads for the peer */
  1477. ndev->spad_count >>= 1;
  1478. ndev->self_reg = &xeon_sec_reg;
  1479. ndev->peer_reg = &xeon_pri_reg;
  1480. ndev->xlat_reg = &xeon_pri_xlat;
  1481. break;
  1482. case NTB_TOPO_B2B_USD:
  1483. case NTB_TOPO_B2B_DSD:
  1484. ndev->self_reg = &xeon_pri_reg;
  1485. ndev->peer_reg = &xeon_b2b_reg;
  1486. ndev->xlat_reg = &xeon_sec_xlat;
  1487. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1488. ndev->peer_reg = &xeon_pri_reg;
  1489. if (b2b_mw_idx < 0)
  1490. ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
  1491. else
  1492. ndev->b2b_idx = b2b_mw_idx;
  1493. if (ndev->b2b_idx >= ndev->mw_count) {
  1494. dev_dbg(ndev_dev(ndev),
  1495. "b2b_mw_idx %d invalid for mw_count %u\n",
  1496. b2b_mw_idx, ndev->mw_count);
  1497. return -EINVAL;
  1498. }
  1499. dev_dbg(ndev_dev(ndev),
  1500. "setting up b2b mw idx %d means %d\n",
  1501. b2b_mw_idx, ndev->b2b_idx);
  1502. } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
  1503. dev_warn(ndev_dev(ndev), "Reduce doorbell count by 1\n");
  1504. ndev->db_count -= 1;
  1505. }
  1506. if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
  1507. rc = xeon_setup_b2b_mw(ndev,
  1508. &xeon_b2b_dsd_addr,
  1509. &xeon_b2b_usd_addr);
  1510. } else {
  1511. rc = xeon_setup_b2b_mw(ndev,
  1512. &xeon_b2b_usd_addr,
  1513. &xeon_b2b_dsd_addr);
  1514. }
  1515. if (rc)
  1516. return rc;
  1517. /* Enable Bus Master and Memory Space on the secondary side */
  1518. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1519. ndev->self_mmio + XEON_SPCICMD_OFFSET);
  1520. break;
  1521. default:
  1522. return -EINVAL;
  1523. }
  1524. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1525. ndev->reg->db_iowrite(ndev->db_valid_mask,
  1526. ndev->self_mmio +
  1527. ndev->self_reg->db_mask);
  1528. return 0;
  1529. }
  1530. static int xeon_init_dev(struct intel_ntb_dev *ndev)
  1531. {
  1532. struct pci_dev *pdev;
  1533. u8 ppd;
  1534. int rc, mem;
  1535. pdev = ndev_pdev(ndev);
  1536. switch (pdev->device) {
  1537. /* There is a Xeon hardware errata related to writes to SDOORBELL or
  1538. * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
  1539. * which may hang the system. To workaround this use the second memory
  1540. * window to access the interrupt and scratch pad registers on the
  1541. * remote system.
  1542. */
  1543. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  1544. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  1545. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  1546. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  1547. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  1548. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  1549. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1550. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1551. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1552. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1553. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1554. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1555. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1556. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1557. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1558. ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
  1559. break;
  1560. }
  1561. switch (pdev->device) {
  1562. /* There is a hardware errata related to accessing any register in
  1563. * SB01BASE in the presence of bidirectional traffic crossing the NTB.
  1564. */
  1565. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1566. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1567. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1568. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1569. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1570. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1571. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1572. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1573. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1574. ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
  1575. break;
  1576. }
  1577. switch (pdev->device) {
  1578. /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
  1579. * mirrored to the remote system. Shrink the number of bits by one,
  1580. * since bit 14 is the last bit.
  1581. */
  1582. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  1583. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  1584. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  1585. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  1586. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  1587. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  1588. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1589. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1590. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1591. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1592. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1593. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1594. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1595. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1596. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1597. ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
  1598. break;
  1599. }
  1600. ndev->reg = &xeon_reg;
  1601. rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
  1602. if (rc)
  1603. return -EIO;
  1604. ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
  1605. dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
  1606. ntb_topo_string(ndev->ntb.topo));
  1607. if (ndev->ntb.topo == NTB_TOPO_NONE)
  1608. return -EINVAL;
  1609. if (ndev->ntb.topo != NTB_TOPO_SEC) {
  1610. ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
  1611. dev_dbg(ndev_dev(ndev), "ppd %#x bar4_split %d\n",
  1612. ppd, ndev->bar4_split);
  1613. } else {
  1614. /* This is a way for transparent BAR to figure out if we are
  1615. * doing split BAR or not. There is no way for the hw on the
  1616. * transparent side to know and set the PPD.
  1617. */
  1618. mem = pci_select_bars(pdev, IORESOURCE_MEM);
  1619. ndev->bar4_split = hweight32(mem) ==
  1620. HSX_SPLIT_BAR_MW_COUNT + 1;
  1621. dev_dbg(ndev_dev(ndev), "mem %#x bar4_split %d\n",
  1622. mem, ndev->bar4_split);
  1623. }
  1624. rc = xeon_init_ntb(ndev);
  1625. if (rc)
  1626. return rc;
  1627. return xeon_init_isr(ndev);
  1628. }
  1629. static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
  1630. {
  1631. xeon_deinit_isr(ndev);
  1632. }
  1633. static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
  1634. {
  1635. int rc;
  1636. pci_set_drvdata(pdev, ndev);
  1637. rc = pci_enable_device(pdev);
  1638. if (rc)
  1639. goto err_pci_enable;
  1640. rc = pci_request_regions(pdev, NTB_NAME);
  1641. if (rc)
  1642. goto err_pci_regions;
  1643. pci_set_master(pdev);
  1644. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1645. if (rc) {
  1646. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1647. if (rc)
  1648. goto err_dma_mask;
  1649. dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
  1650. }
  1651. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1652. if (rc) {
  1653. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1654. if (rc)
  1655. goto err_dma_mask;
  1656. dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
  1657. }
  1658. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  1659. if (!ndev->self_mmio) {
  1660. rc = -EIO;
  1661. goto err_mmio;
  1662. }
  1663. ndev->peer_mmio = ndev->self_mmio;
  1664. return 0;
  1665. err_mmio:
  1666. err_dma_mask:
  1667. pci_clear_master(pdev);
  1668. pci_release_regions(pdev);
  1669. err_pci_regions:
  1670. pci_disable_device(pdev);
  1671. err_pci_enable:
  1672. pci_set_drvdata(pdev, NULL);
  1673. return rc;
  1674. }
  1675. static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
  1676. {
  1677. struct pci_dev *pdev = ndev_pdev(ndev);
  1678. if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
  1679. pci_iounmap(pdev, ndev->peer_mmio);
  1680. pci_iounmap(pdev, ndev->self_mmio);
  1681. pci_clear_master(pdev);
  1682. pci_release_regions(pdev);
  1683. pci_disable_device(pdev);
  1684. pci_set_drvdata(pdev, NULL);
  1685. }
  1686. static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
  1687. struct pci_dev *pdev)
  1688. {
  1689. ndev->ntb.pdev = pdev;
  1690. ndev->ntb.topo = NTB_TOPO_NONE;
  1691. ndev->ntb.ops = &intel_ntb_ops;
  1692. ndev->b2b_off = 0;
  1693. ndev->b2b_idx = UINT_MAX;
  1694. ndev->bar4_split = 0;
  1695. ndev->mw_count = 0;
  1696. ndev->spad_count = 0;
  1697. ndev->db_count = 0;
  1698. ndev->db_vec_count = 0;
  1699. ndev->db_vec_shift = 0;
  1700. ndev->ntb_ctl = 0;
  1701. ndev->lnk_sta = 0;
  1702. ndev->db_valid_mask = 0;
  1703. ndev->db_link_mask = 0;
  1704. ndev->db_mask = 0;
  1705. spin_lock_init(&ndev->db_mask_lock);
  1706. }
  1707. static int intel_ntb_pci_probe(struct pci_dev *pdev,
  1708. const struct pci_device_id *id)
  1709. {
  1710. struct intel_ntb_dev *ndev;
  1711. int rc, node;
  1712. node = dev_to_node(&pdev->dev);
  1713. if (pdev_is_atom(pdev)) {
  1714. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  1715. if (!ndev) {
  1716. rc = -ENOMEM;
  1717. goto err_ndev;
  1718. }
  1719. ndev_init_struct(ndev, pdev);
  1720. rc = intel_ntb_init_pci(ndev, pdev);
  1721. if (rc)
  1722. goto err_init_pci;
  1723. rc = atom_init_dev(ndev);
  1724. if (rc)
  1725. goto err_init_dev;
  1726. } else if (pdev_is_xeon(pdev)) {
  1727. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  1728. if (!ndev) {
  1729. rc = -ENOMEM;
  1730. goto err_ndev;
  1731. }
  1732. ndev_init_struct(ndev, pdev);
  1733. rc = intel_ntb_init_pci(ndev, pdev);
  1734. if (rc)
  1735. goto err_init_pci;
  1736. rc = xeon_init_dev(ndev);
  1737. if (rc)
  1738. goto err_init_dev;
  1739. } else {
  1740. rc = -EINVAL;
  1741. goto err_ndev;
  1742. }
  1743. ndev_reset_unsafe_flags(ndev);
  1744. ndev->reg->poll_link(ndev);
  1745. ndev_init_debugfs(ndev);
  1746. rc = ntb_register_device(&ndev->ntb);
  1747. if (rc)
  1748. goto err_register;
  1749. dev_info(&pdev->dev, "NTB device registered.\n");
  1750. return 0;
  1751. err_register:
  1752. ndev_deinit_debugfs(ndev);
  1753. if (pdev_is_atom(pdev))
  1754. atom_deinit_dev(ndev);
  1755. else if (pdev_is_xeon(pdev))
  1756. xeon_deinit_dev(ndev);
  1757. err_init_dev:
  1758. intel_ntb_deinit_pci(ndev);
  1759. err_init_pci:
  1760. kfree(ndev);
  1761. err_ndev:
  1762. return rc;
  1763. }
  1764. static void intel_ntb_pci_remove(struct pci_dev *pdev)
  1765. {
  1766. struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
  1767. ntb_unregister_device(&ndev->ntb);
  1768. ndev_deinit_debugfs(ndev);
  1769. if (pdev_is_atom(pdev))
  1770. atom_deinit_dev(ndev);
  1771. else if (pdev_is_xeon(pdev))
  1772. xeon_deinit_dev(ndev);
  1773. intel_ntb_deinit_pci(ndev);
  1774. kfree(ndev);
  1775. }
  1776. static const struct intel_ntb_reg atom_reg = {
  1777. .poll_link = atom_poll_link,
  1778. .link_is_up = atom_link_is_up,
  1779. .db_ioread = atom_db_ioread,
  1780. .db_iowrite = atom_db_iowrite,
  1781. .db_size = sizeof(u64),
  1782. .ntb_ctl = ATOM_NTBCNTL_OFFSET,
  1783. .mw_bar = {2, 4},
  1784. };
  1785. static const struct intel_ntb_alt_reg atom_pri_reg = {
  1786. .db_bell = ATOM_PDOORBELL_OFFSET,
  1787. .db_mask = ATOM_PDBMSK_OFFSET,
  1788. .spad = ATOM_SPAD_OFFSET,
  1789. };
  1790. static const struct intel_ntb_alt_reg atom_b2b_reg = {
  1791. .db_bell = ATOM_B2B_DOORBELL_OFFSET,
  1792. .spad = ATOM_B2B_SPAD_OFFSET,
  1793. };
  1794. static const struct intel_ntb_xlat_reg atom_sec_xlat = {
  1795. /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
  1796. /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
  1797. .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
  1798. };
  1799. static const struct intel_ntb_reg xeon_reg = {
  1800. .poll_link = xeon_poll_link,
  1801. .link_is_up = xeon_link_is_up,
  1802. .db_ioread = xeon_db_ioread,
  1803. .db_iowrite = xeon_db_iowrite,
  1804. .db_size = sizeof(u32),
  1805. .ntb_ctl = XEON_NTBCNTL_OFFSET,
  1806. .mw_bar = {2, 4, 5},
  1807. };
  1808. static const struct intel_ntb_alt_reg xeon_pri_reg = {
  1809. .db_bell = XEON_PDOORBELL_OFFSET,
  1810. .db_mask = XEON_PDBMSK_OFFSET,
  1811. .spad = XEON_SPAD_OFFSET,
  1812. };
  1813. static const struct intel_ntb_alt_reg xeon_sec_reg = {
  1814. .db_bell = XEON_SDOORBELL_OFFSET,
  1815. .db_mask = XEON_SDBMSK_OFFSET,
  1816. /* second half of the scratchpads */
  1817. .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
  1818. };
  1819. static const struct intel_ntb_alt_reg xeon_b2b_reg = {
  1820. .db_bell = XEON_B2B_DOORBELL_OFFSET,
  1821. .spad = XEON_B2B_SPAD_OFFSET,
  1822. };
  1823. static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
  1824. /* Note: no primary .bar0_base visible to the secondary side.
  1825. *
  1826. * The secondary side cannot get the base address stored in primary
  1827. * bars. The base address is necessary to set the limit register to
  1828. * any value other than zero, or unlimited.
  1829. *
  1830. * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
  1831. * window by setting the limit equal to base, nor can it limit the size
  1832. * of the memory window by setting the limit to base + size.
  1833. */
  1834. .bar2_limit = XEON_PBAR23LMT_OFFSET,
  1835. .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
  1836. };
  1837. static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
  1838. .bar0_base = XEON_SBAR0BASE_OFFSET,
  1839. .bar2_limit = XEON_SBAR23LMT_OFFSET,
  1840. .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
  1841. };
  1842. static struct intel_b2b_addr xeon_b2b_usd_addr = {
  1843. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  1844. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  1845. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  1846. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  1847. };
  1848. static struct intel_b2b_addr xeon_b2b_dsd_addr = {
  1849. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  1850. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  1851. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  1852. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  1853. };
  1854. /* operations for primary side of local ntb */
  1855. static const struct ntb_dev_ops intel_ntb_ops = {
  1856. .mw_count = intel_ntb_mw_count,
  1857. .mw_get_range = intel_ntb_mw_get_range,
  1858. .mw_set_trans = intel_ntb_mw_set_trans,
  1859. .link_is_up = intel_ntb_link_is_up,
  1860. .link_enable = intel_ntb_link_enable,
  1861. .link_disable = intel_ntb_link_disable,
  1862. .db_is_unsafe = intel_ntb_db_is_unsafe,
  1863. .db_valid_mask = intel_ntb_db_valid_mask,
  1864. .db_vector_count = intel_ntb_db_vector_count,
  1865. .db_vector_mask = intel_ntb_db_vector_mask,
  1866. .db_read = intel_ntb_db_read,
  1867. .db_clear = intel_ntb_db_clear,
  1868. .db_set_mask = intel_ntb_db_set_mask,
  1869. .db_clear_mask = intel_ntb_db_clear_mask,
  1870. .peer_db_addr = intel_ntb_peer_db_addr,
  1871. .peer_db_set = intel_ntb_peer_db_set,
  1872. .spad_is_unsafe = intel_ntb_spad_is_unsafe,
  1873. .spad_count = intel_ntb_spad_count,
  1874. .spad_read = intel_ntb_spad_read,
  1875. .spad_write = intel_ntb_spad_write,
  1876. .peer_spad_addr = intel_ntb_peer_spad_addr,
  1877. .peer_spad_read = intel_ntb_peer_spad_read,
  1878. .peer_spad_write = intel_ntb_peer_spad_write,
  1879. };
  1880. static const struct file_operations intel_ntb_debugfs_info = {
  1881. .owner = THIS_MODULE,
  1882. .open = simple_open,
  1883. .read = ndev_debugfs_read,
  1884. };
  1885. static const struct pci_device_id intel_ntb_pci_tbl[] = {
  1886. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  1887. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  1888. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  1889. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  1890. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  1891. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BDX)},
  1892. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  1893. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  1894. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  1895. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  1896. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_BDX)},
  1897. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  1898. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  1899. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  1900. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  1901. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_BDX)},
  1902. {0}
  1903. };
  1904. MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
  1905. static struct pci_driver intel_ntb_pci_driver = {
  1906. .name = KBUILD_MODNAME,
  1907. .id_table = intel_ntb_pci_tbl,
  1908. .probe = intel_ntb_pci_probe,
  1909. .remove = intel_ntb_pci_remove,
  1910. };
  1911. static int __init intel_ntb_pci_driver_init(void)
  1912. {
  1913. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  1914. if (debugfs_initialized())
  1915. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1916. return pci_register_driver(&intel_ntb_pci_driver);
  1917. }
  1918. module_init(intel_ntb_pci_driver_init);
  1919. static void __exit intel_ntb_pci_driver_exit(void)
  1920. {
  1921. pci_unregister_driver(&intel_ntb_pci_driver);
  1922. debugfs_remove_recursive(debugfs_dir);
  1923. }
  1924. module_exit(intel_ntb_pci_driver_exit);