ntb_hw_intel.c 61 KB

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