ntb_hw_intel.c 80 KB

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