ntb_hw_intel.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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_bar4_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_bar4_addr32,
  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_bar5_addr32,
  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_bar4_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_bar4_addr32,
  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_bar5_addr32,
  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. ndev->peer_addr = pci_resource_start(pdev, b2b_bar);
  1442. }
  1443. return 0;
  1444. }
  1445. static int xeon_init_ntb(struct intel_ntb_dev *ndev)
  1446. {
  1447. int rc;
  1448. u32 ntb_ctl;
  1449. if (ndev->bar4_split)
  1450. ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
  1451. else
  1452. ndev->mw_count = XEON_MW_COUNT;
  1453. ndev->spad_count = XEON_SPAD_COUNT;
  1454. ndev->db_count = XEON_DB_COUNT;
  1455. ndev->db_link_mask = XEON_DB_LINK_BIT;
  1456. switch (ndev->ntb.topo) {
  1457. case NTB_TOPO_PRI:
  1458. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1459. dev_err(ndev_dev(ndev), "NTB Primary config disabled\n");
  1460. return -EINVAL;
  1461. }
  1462. /* enable link to allow secondary side device to appear */
  1463. ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
  1464. ntb_ctl &= ~NTB_CTL_DISABLE;
  1465. iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
  1466. /* use half the spads for the peer */
  1467. ndev->spad_count >>= 1;
  1468. ndev->self_reg = &xeon_pri_reg;
  1469. ndev->peer_reg = &xeon_sec_reg;
  1470. ndev->xlat_reg = &xeon_sec_xlat;
  1471. break;
  1472. case NTB_TOPO_SEC:
  1473. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1474. dev_err(ndev_dev(ndev), "NTB Secondary config disabled\n");
  1475. return -EINVAL;
  1476. }
  1477. /* use half the spads for the peer */
  1478. ndev->spad_count >>= 1;
  1479. ndev->self_reg = &xeon_sec_reg;
  1480. ndev->peer_reg = &xeon_pri_reg;
  1481. ndev->xlat_reg = &xeon_pri_xlat;
  1482. break;
  1483. case NTB_TOPO_B2B_USD:
  1484. case NTB_TOPO_B2B_DSD:
  1485. ndev->self_reg = &xeon_pri_reg;
  1486. ndev->peer_reg = &xeon_b2b_reg;
  1487. ndev->xlat_reg = &xeon_sec_xlat;
  1488. if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
  1489. ndev->peer_reg = &xeon_pri_reg;
  1490. if (b2b_mw_idx < 0)
  1491. ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
  1492. else
  1493. ndev->b2b_idx = b2b_mw_idx;
  1494. if (ndev->b2b_idx >= ndev->mw_count) {
  1495. dev_dbg(ndev_dev(ndev),
  1496. "b2b_mw_idx %d invalid for mw_count %u\n",
  1497. b2b_mw_idx, ndev->mw_count);
  1498. return -EINVAL;
  1499. }
  1500. dev_dbg(ndev_dev(ndev),
  1501. "setting up b2b mw idx %d means %d\n",
  1502. b2b_mw_idx, ndev->b2b_idx);
  1503. } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
  1504. dev_warn(ndev_dev(ndev), "Reduce doorbell count by 1\n");
  1505. ndev->db_count -= 1;
  1506. }
  1507. if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
  1508. rc = xeon_setup_b2b_mw(ndev,
  1509. &xeon_b2b_dsd_addr,
  1510. &xeon_b2b_usd_addr);
  1511. } else {
  1512. rc = xeon_setup_b2b_mw(ndev,
  1513. &xeon_b2b_usd_addr,
  1514. &xeon_b2b_dsd_addr);
  1515. }
  1516. if (rc)
  1517. return rc;
  1518. /* Enable Bus Master and Memory Space on the secondary side */
  1519. iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  1520. ndev->self_mmio + XEON_SPCICMD_OFFSET);
  1521. break;
  1522. default:
  1523. return -EINVAL;
  1524. }
  1525. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  1526. ndev->reg->db_iowrite(ndev->db_valid_mask,
  1527. ndev->self_mmio +
  1528. ndev->self_reg->db_mask);
  1529. return 0;
  1530. }
  1531. static int xeon_init_dev(struct intel_ntb_dev *ndev)
  1532. {
  1533. struct pci_dev *pdev;
  1534. u8 ppd;
  1535. int rc, mem;
  1536. pdev = ndev_pdev(ndev);
  1537. switch (pdev->device) {
  1538. /* There is a Xeon hardware errata related to writes to SDOORBELL or
  1539. * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
  1540. * which may hang the system. To workaround this use the second memory
  1541. * window to access the interrupt and scratch pad registers on the
  1542. * remote system.
  1543. */
  1544. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  1545. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  1546. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  1547. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  1548. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  1549. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  1550. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1551. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1552. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1553. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1554. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1555. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1556. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1557. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1558. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1559. ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
  1560. break;
  1561. }
  1562. switch (pdev->device) {
  1563. /* There is a hardware errata related to accessing any register in
  1564. * SB01BASE in the presence of bidirectional traffic crossing the NTB.
  1565. */
  1566. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1567. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1568. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1569. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1570. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1571. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1572. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1573. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1574. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1575. ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
  1576. break;
  1577. }
  1578. switch (pdev->device) {
  1579. /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
  1580. * mirrored to the remote system. Shrink the number of bits by one,
  1581. * since bit 14 is the last bit.
  1582. */
  1583. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  1584. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  1585. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  1586. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  1587. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  1588. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  1589. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  1590. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  1591. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  1592. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  1593. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  1594. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  1595. case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
  1596. case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
  1597. case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
  1598. ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
  1599. break;
  1600. }
  1601. ndev->reg = &xeon_reg;
  1602. rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
  1603. if (rc)
  1604. return -EIO;
  1605. ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
  1606. dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
  1607. ntb_topo_string(ndev->ntb.topo));
  1608. if (ndev->ntb.topo == NTB_TOPO_NONE)
  1609. return -EINVAL;
  1610. if (ndev->ntb.topo != NTB_TOPO_SEC) {
  1611. ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
  1612. dev_dbg(ndev_dev(ndev), "ppd %#x bar4_split %d\n",
  1613. ppd, ndev->bar4_split);
  1614. } else {
  1615. /* This is a way for transparent BAR to figure out if we are
  1616. * doing split BAR or not. There is no way for the hw on the
  1617. * transparent side to know and set the PPD.
  1618. */
  1619. mem = pci_select_bars(pdev, IORESOURCE_MEM);
  1620. ndev->bar4_split = hweight32(mem) ==
  1621. HSX_SPLIT_BAR_MW_COUNT + 1;
  1622. dev_dbg(ndev_dev(ndev), "mem %#x bar4_split %d\n",
  1623. mem, ndev->bar4_split);
  1624. }
  1625. rc = xeon_init_ntb(ndev);
  1626. if (rc)
  1627. return rc;
  1628. return xeon_init_isr(ndev);
  1629. }
  1630. static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
  1631. {
  1632. xeon_deinit_isr(ndev);
  1633. }
  1634. static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
  1635. {
  1636. int rc;
  1637. pci_set_drvdata(pdev, ndev);
  1638. rc = pci_enable_device(pdev);
  1639. if (rc)
  1640. goto err_pci_enable;
  1641. rc = pci_request_regions(pdev, NTB_NAME);
  1642. if (rc)
  1643. goto err_pci_regions;
  1644. pci_set_master(pdev);
  1645. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1646. if (rc) {
  1647. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1648. if (rc)
  1649. goto err_dma_mask;
  1650. dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
  1651. }
  1652. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1653. if (rc) {
  1654. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1655. if (rc)
  1656. goto err_dma_mask;
  1657. dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
  1658. }
  1659. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  1660. if (!ndev->self_mmio) {
  1661. rc = -EIO;
  1662. goto err_mmio;
  1663. }
  1664. ndev->peer_mmio = ndev->self_mmio;
  1665. ndev->peer_addr = pci_resource_start(pdev, 0);
  1666. return 0;
  1667. err_mmio:
  1668. err_dma_mask:
  1669. pci_clear_master(pdev);
  1670. pci_release_regions(pdev);
  1671. err_pci_regions:
  1672. pci_disable_device(pdev);
  1673. err_pci_enable:
  1674. pci_set_drvdata(pdev, NULL);
  1675. return rc;
  1676. }
  1677. static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
  1678. {
  1679. struct pci_dev *pdev = ndev_pdev(ndev);
  1680. if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
  1681. pci_iounmap(pdev, ndev->peer_mmio);
  1682. pci_iounmap(pdev, ndev->self_mmio);
  1683. pci_clear_master(pdev);
  1684. pci_release_regions(pdev);
  1685. pci_disable_device(pdev);
  1686. pci_set_drvdata(pdev, NULL);
  1687. }
  1688. static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
  1689. struct pci_dev *pdev)
  1690. {
  1691. ndev->ntb.pdev = pdev;
  1692. ndev->ntb.topo = NTB_TOPO_NONE;
  1693. ndev->ntb.ops = &intel_ntb_ops;
  1694. ndev->b2b_off = 0;
  1695. ndev->b2b_idx = UINT_MAX;
  1696. ndev->bar4_split = 0;
  1697. ndev->mw_count = 0;
  1698. ndev->spad_count = 0;
  1699. ndev->db_count = 0;
  1700. ndev->db_vec_count = 0;
  1701. ndev->db_vec_shift = 0;
  1702. ndev->ntb_ctl = 0;
  1703. ndev->lnk_sta = 0;
  1704. ndev->db_valid_mask = 0;
  1705. ndev->db_link_mask = 0;
  1706. ndev->db_mask = 0;
  1707. spin_lock_init(&ndev->db_mask_lock);
  1708. }
  1709. static int intel_ntb_pci_probe(struct pci_dev *pdev,
  1710. const struct pci_device_id *id)
  1711. {
  1712. struct intel_ntb_dev *ndev;
  1713. int rc, node;
  1714. node = dev_to_node(&pdev->dev);
  1715. if (pdev_is_atom(pdev)) {
  1716. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  1717. if (!ndev) {
  1718. rc = -ENOMEM;
  1719. goto err_ndev;
  1720. }
  1721. ndev_init_struct(ndev, pdev);
  1722. rc = intel_ntb_init_pci(ndev, pdev);
  1723. if (rc)
  1724. goto err_init_pci;
  1725. rc = atom_init_dev(ndev);
  1726. if (rc)
  1727. goto err_init_dev;
  1728. } else if (pdev_is_xeon(pdev)) {
  1729. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  1730. if (!ndev) {
  1731. rc = -ENOMEM;
  1732. goto err_ndev;
  1733. }
  1734. ndev_init_struct(ndev, pdev);
  1735. rc = intel_ntb_init_pci(ndev, pdev);
  1736. if (rc)
  1737. goto err_init_pci;
  1738. rc = xeon_init_dev(ndev);
  1739. if (rc)
  1740. goto err_init_dev;
  1741. } else {
  1742. rc = -EINVAL;
  1743. goto err_ndev;
  1744. }
  1745. ndev_reset_unsafe_flags(ndev);
  1746. ndev->reg->poll_link(ndev);
  1747. ndev_init_debugfs(ndev);
  1748. rc = ntb_register_device(&ndev->ntb);
  1749. if (rc)
  1750. goto err_register;
  1751. dev_info(&pdev->dev, "NTB device registered.\n");
  1752. return 0;
  1753. err_register:
  1754. ndev_deinit_debugfs(ndev);
  1755. if (pdev_is_atom(pdev))
  1756. atom_deinit_dev(ndev);
  1757. else if (pdev_is_xeon(pdev))
  1758. xeon_deinit_dev(ndev);
  1759. err_init_dev:
  1760. intel_ntb_deinit_pci(ndev);
  1761. err_init_pci:
  1762. kfree(ndev);
  1763. err_ndev:
  1764. return rc;
  1765. }
  1766. static void intel_ntb_pci_remove(struct pci_dev *pdev)
  1767. {
  1768. struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
  1769. ntb_unregister_device(&ndev->ntb);
  1770. ndev_deinit_debugfs(ndev);
  1771. if (pdev_is_atom(pdev))
  1772. atom_deinit_dev(ndev);
  1773. else if (pdev_is_xeon(pdev))
  1774. xeon_deinit_dev(ndev);
  1775. intel_ntb_deinit_pci(ndev);
  1776. kfree(ndev);
  1777. }
  1778. static const struct intel_ntb_reg atom_reg = {
  1779. .poll_link = atom_poll_link,
  1780. .link_is_up = atom_link_is_up,
  1781. .db_ioread = atom_db_ioread,
  1782. .db_iowrite = atom_db_iowrite,
  1783. .db_size = sizeof(u64),
  1784. .ntb_ctl = ATOM_NTBCNTL_OFFSET,
  1785. .mw_bar = {2, 4},
  1786. };
  1787. static const struct intel_ntb_alt_reg atom_pri_reg = {
  1788. .db_bell = ATOM_PDOORBELL_OFFSET,
  1789. .db_mask = ATOM_PDBMSK_OFFSET,
  1790. .spad = ATOM_SPAD_OFFSET,
  1791. };
  1792. static const struct intel_ntb_alt_reg atom_b2b_reg = {
  1793. .db_bell = ATOM_B2B_DOORBELL_OFFSET,
  1794. .spad = ATOM_B2B_SPAD_OFFSET,
  1795. };
  1796. static const struct intel_ntb_xlat_reg atom_sec_xlat = {
  1797. /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
  1798. /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
  1799. .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
  1800. };
  1801. static const struct intel_ntb_reg xeon_reg = {
  1802. .poll_link = xeon_poll_link,
  1803. .link_is_up = xeon_link_is_up,
  1804. .db_ioread = xeon_db_ioread,
  1805. .db_iowrite = xeon_db_iowrite,
  1806. .db_size = sizeof(u32),
  1807. .ntb_ctl = XEON_NTBCNTL_OFFSET,
  1808. .mw_bar = {2, 4, 5},
  1809. };
  1810. static const struct intel_ntb_alt_reg xeon_pri_reg = {
  1811. .db_bell = XEON_PDOORBELL_OFFSET,
  1812. .db_mask = XEON_PDBMSK_OFFSET,
  1813. .spad = XEON_SPAD_OFFSET,
  1814. };
  1815. static const struct intel_ntb_alt_reg xeon_sec_reg = {
  1816. .db_bell = XEON_SDOORBELL_OFFSET,
  1817. .db_mask = XEON_SDBMSK_OFFSET,
  1818. /* second half of the scratchpads */
  1819. .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
  1820. };
  1821. static const struct intel_ntb_alt_reg xeon_b2b_reg = {
  1822. .db_bell = XEON_B2B_DOORBELL_OFFSET,
  1823. .spad = XEON_B2B_SPAD_OFFSET,
  1824. };
  1825. static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
  1826. /* Note: no primary .bar0_base visible to the secondary side.
  1827. *
  1828. * The secondary side cannot get the base address stored in primary
  1829. * bars. The base address is necessary to set the limit register to
  1830. * any value other than zero, or unlimited.
  1831. *
  1832. * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
  1833. * window by setting the limit equal to base, nor can it limit the size
  1834. * of the memory window by setting the limit to base + size.
  1835. */
  1836. .bar2_limit = XEON_PBAR23LMT_OFFSET,
  1837. .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
  1838. };
  1839. static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
  1840. .bar0_base = XEON_SBAR0BASE_OFFSET,
  1841. .bar2_limit = XEON_SBAR23LMT_OFFSET,
  1842. .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
  1843. };
  1844. static struct intel_b2b_addr xeon_b2b_usd_addr = {
  1845. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  1846. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  1847. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  1848. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  1849. };
  1850. static struct intel_b2b_addr xeon_b2b_dsd_addr = {
  1851. .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
  1852. .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
  1853. .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
  1854. .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
  1855. };
  1856. /* operations for primary side of local ntb */
  1857. static const struct ntb_dev_ops intel_ntb_ops = {
  1858. .mw_count = intel_ntb_mw_count,
  1859. .mw_get_range = intel_ntb_mw_get_range,
  1860. .mw_set_trans = intel_ntb_mw_set_trans,
  1861. .link_is_up = intel_ntb_link_is_up,
  1862. .link_enable = intel_ntb_link_enable,
  1863. .link_disable = intel_ntb_link_disable,
  1864. .db_is_unsafe = intel_ntb_db_is_unsafe,
  1865. .db_valid_mask = intel_ntb_db_valid_mask,
  1866. .db_vector_count = intel_ntb_db_vector_count,
  1867. .db_vector_mask = intel_ntb_db_vector_mask,
  1868. .db_read = intel_ntb_db_read,
  1869. .db_clear = intel_ntb_db_clear,
  1870. .db_set_mask = intel_ntb_db_set_mask,
  1871. .db_clear_mask = intel_ntb_db_clear_mask,
  1872. .peer_db_addr = intel_ntb_peer_db_addr,
  1873. .peer_db_set = intel_ntb_peer_db_set,
  1874. .spad_is_unsafe = intel_ntb_spad_is_unsafe,
  1875. .spad_count = intel_ntb_spad_count,
  1876. .spad_read = intel_ntb_spad_read,
  1877. .spad_write = intel_ntb_spad_write,
  1878. .peer_spad_addr = intel_ntb_peer_spad_addr,
  1879. .peer_spad_read = intel_ntb_peer_spad_read,
  1880. .peer_spad_write = intel_ntb_peer_spad_write,
  1881. };
  1882. static const struct file_operations intel_ntb_debugfs_info = {
  1883. .owner = THIS_MODULE,
  1884. .open = simple_open,
  1885. .read = ndev_debugfs_read,
  1886. };
  1887. static const struct pci_device_id intel_ntb_pci_tbl[] = {
  1888. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  1889. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  1890. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  1891. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  1892. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  1893. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BDX)},
  1894. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  1895. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  1896. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  1897. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  1898. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_BDX)},
  1899. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  1900. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  1901. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  1902. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  1903. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_BDX)},
  1904. {0}
  1905. };
  1906. MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
  1907. static struct pci_driver intel_ntb_pci_driver = {
  1908. .name = KBUILD_MODNAME,
  1909. .id_table = intel_ntb_pci_tbl,
  1910. .probe = intel_ntb_pci_probe,
  1911. .remove = intel_ntb_pci_remove,
  1912. };
  1913. static int __init intel_ntb_pci_driver_init(void)
  1914. {
  1915. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  1916. if (debugfs_initialized())
  1917. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1918. return pci_register_driver(&intel_ntb_pci_driver);
  1919. }
  1920. module_init(intel_ntb_pci_driver_init);
  1921. static void __exit intel_ntb_pci_driver_exit(void)
  1922. {
  1923. pci_unregister_driver(&intel_ntb_pci_driver);
  1924. debugfs_remove_recursive(debugfs_dir);
  1925. }
  1926. module_exit(intel_ntb_pci_driver_exit);