ntb_hw.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * BSD LICENSE
  14. *
  15. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copy
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * * Neither the name of Intel Corporation nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * Intel PCIe NTB Linux driver
  44. *
  45. * Contact Information:
  46. * Jon Mason <jon.mason@intel.com>
  47. */
  48. #include <linux/debugfs.h>
  49. #include <linux/delay.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/module.h>
  53. #include <linux/pci.h>
  54. #include <linux/random.h>
  55. #include <linux/slab.h>
  56. #include "ntb_hw.h"
  57. #include "ntb_regs.h"
  58. #define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
  59. #define NTB_VER "1.0"
  60. MODULE_DESCRIPTION(NTB_NAME);
  61. MODULE_VERSION(NTB_VER);
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. MODULE_AUTHOR("Intel Corporation");
  64. enum {
  65. NTB_CONN_TRANSPARENT = 0,
  66. NTB_CONN_B2B,
  67. NTB_CONN_RP,
  68. };
  69. enum {
  70. NTB_DEV_USD = 0,
  71. NTB_DEV_DSD,
  72. };
  73. enum {
  74. SNB_HW = 0,
  75. BWD_HW,
  76. };
  77. static struct dentry *debugfs_dir;
  78. #define BWD_LINK_RECOVERY_TIME 500
  79. /* Translate memory window 0,1,2 to BAR 2,4,5 */
  80. #define MW_TO_BAR(mw) (mw == 0 ? 2 : (mw == 1 ? 4 : 5))
  81. static const struct pci_device_id ntb_pci_tbl[] = {
  82. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  83. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  84. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  85. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  86. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  87. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  88. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  89. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  90. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  91. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  92. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  93. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  94. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  95. {0}
  96. };
  97. MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
  98. static int is_ntb_xeon(struct ntb_device *ndev)
  99. {
  100. switch (ndev->pdev->device) {
  101. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  102. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  103. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  104. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  105. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  106. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  107. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  108. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  109. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  110. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  111. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  112. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  113. return 1;
  114. default:
  115. return 0;
  116. }
  117. return 0;
  118. }
  119. static int is_ntb_atom(struct ntb_device *ndev)
  120. {
  121. switch (ndev->pdev->device) {
  122. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  123. return 1;
  124. default:
  125. return 0;
  126. }
  127. return 0;
  128. }
  129. static void ntb_set_errata_flags(struct ntb_device *ndev)
  130. {
  131. switch (ndev->pdev->device) {
  132. /*
  133. * this workaround applies to all platform up to IvyBridge
  134. * Haswell has splitbar support and use a different workaround
  135. */
  136. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  137. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  138. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  139. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  140. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  141. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  142. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  143. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  144. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  145. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  146. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  147. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  148. ndev->wa_flags |= WA_SNB_ERR;
  149. break;
  150. }
  151. }
  152. /**
  153. * ntb_register_event_callback() - register event callback
  154. * @ndev: pointer to ntb_device instance
  155. * @func: callback function to register
  156. *
  157. * This function registers a callback for any HW driver events such as link
  158. * up/down, power management notices and etc.
  159. *
  160. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  161. */
  162. int ntb_register_event_callback(struct ntb_device *ndev,
  163. void (*func)(void *handle,
  164. enum ntb_hw_event event))
  165. {
  166. if (ndev->event_cb)
  167. return -EINVAL;
  168. ndev->event_cb = func;
  169. return 0;
  170. }
  171. /**
  172. * ntb_unregister_event_callback() - unregisters the event callback
  173. * @ndev: pointer to ntb_device instance
  174. *
  175. * This function unregisters the existing callback from transport
  176. */
  177. void ntb_unregister_event_callback(struct ntb_device *ndev)
  178. {
  179. ndev->event_cb = NULL;
  180. }
  181. static void ntb_irq_work(unsigned long data)
  182. {
  183. struct ntb_db_cb *db_cb = (struct ntb_db_cb *)data;
  184. int rc;
  185. rc = db_cb->callback(db_cb->data, db_cb->db_num);
  186. if (rc)
  187. tasklet_schedule(&db_cb->irq_work);
  188. else {
  189. struct ntb_device *ndev = db_cb->ndev;
  190. unsigned long mask;
  191. mask = readw(ndev->reg_ofs.ldb_mask);
  192. clear_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  193. writew(mask, ndev->reg_ofs.ldb_mask);
  194. }
  195. }
  196. /**
  197. * ntb_register_db_callback() - register a callback for doorbell interrupt
  198. * @ndev: pointer to ntb_device instance
  199. * @idx: doorbell index to register callback, zero based
  200. * @data: pointer to be returned to caller with every callback
  201. * @func: callback function to register
  202. *
  203. * This function registers a callback function for the doorbell interrupt
  204. * on the primary side. The function will unmask the doorbell as well to
  205. * allow interrupt.
  206. *
  207. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  208. */
  209. int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
  210. void *data, int (*func)(void *data, int db_num))
  211. {
  212. unsigned long mask;
  213. if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
  214. dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
  215. return -EINVAL;
  216. }
  217. ndev->db_cb[idx].callback = func;
  218. ndev->db_cb[idx].data = data;
  219. ndev->db_cb[idx].ndev = ndev;
  220. tasklet_init(&ndev->db_cb[idx].irq_work, ntb_irq_work,
  221. (unsigned long) &ndev->db_cb[idx]);
  222. /* unmask interrupt */
  223. mask = readw(ndev->reg_ofs.ldb_mask);
  224. clear_bit(idx * ndev->bits_per_vector, &mask);
  225. writew(mask, ndev->reg_ofs.ldb_mask);
  226. return 0;
  227. }
  228. /**
  229. * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
  230. * @ndev: pointer to ntb_device instance
  231. * @idx: doorbell index to register callback, zero based
  232. *
  233. * This function unregisters a callback function for the doorbell interrupt
  234. * on the primary side. The function will also mask the said doorbell.
  235. */
  236. void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
  237. {
  238. unsigned long mask;
  239. if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
  240. return;
  241. mask = readw(ndev->reg_ofs.ldb_mask);
  242. set_bit(idx * ndev->bits_per_vector, &mask);
  243. writew(mask, ndev->reg_ofs.ldb_mask);
  244. tasklet_disable(&ndev->db_cb[idx].irq_work);
  245. ndev->db_cb[idx].callback = NULL;
  246. }
  247. /**
  248. * ntb_find_transport() - find the transport pointer
  249. * @transport: pointer to pci device
  250. *
  251. * Given the pci device pointer, return the transport pointer passed in when
  252. * the transport attached when it was inited.
  253. *
  254. * RETURNS: pointer to transport.
  255. */
  256. void *ntb_find_transport(struct pci_dev *pdev)
  257. {
  258. struct ntb_device *ndev = pci_get_drvdata(pdev);
  259. return ndev->ntb_transport;
  260. }
  261. /**
  262. * ntb_register_transport() - Register NTB transport with NTB HW driver
  263. * @transport: transport identifier
  264. *
  265. * This function allows a transport to reserve the hardware driver for
  266. * NTB usage.
  267. *
  268. * RETURNS: pointer to ntb_device, NULL on error.
  269. */
  270. struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
  271. {
  272. struct ntb_device *ndev = pci_get_drvdata(pdev);
  273. if (ndev->ntb_transport)
  274. return NULL;
  275. ndev->ntb_transport = transport;
  276. return ndev;
  277. }
  278. /**
  279. * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
  280. * @ndev - ntb_device of the transport to be freed
  281. *
  282. * This function unregisters the transport from the HW driver and performs any
  283. * necessary cleanups.
  284. */
  285. void ntb_unregister_transport(struct ntb_device *ndev)
  286. {
  287. int i;
  288. if (!ndev->ntb_transport)
  289. return;
  290. for (i = 0; i < ndev->max_cbs; i++)
  291. ntb_unregister_db_callback(ndev, i);
  292. ntb_unregister_event_callback(ndev);
  293. ndev->ntb_transport = NULL;
  294. }
  295. /**
  296. * ntb_write_local_spad() - write to the secondary scratchpad register
  297. * @ndev: pointer to ntb_device instance
  298. * @idx: index to the scratchpad register, 0 based
  299. * @val: the data value to put into the register
  300. *
  301. * This function allows writing of a 32bit value to the indexed scratchpad
  302. * register. This writes over the data mirrored to the local scratchpad register
  303. * by the remote system.
  304. *
  305. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  306. */
  307. int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  308. {
  309. if (idx >= ndev->limits.max_spads)
  310. return -EINVAL;
  311. dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
  312. val, idx);
  313. writel(val, ndev->reg_ofs.spad_read + idx * 4);
  314. return 0;
  315. }
  316. /**
  317. * ntb_read_local_spad() - read from the primary scratchpad register
  318. * @ndev: pointer to ntb_device instance
  319. * @idx: index to scratchpad register, 0 based
  320. * @val: pointer to 32bit integer for storing the register value
  321. *
  322. * This function allows reading of the 32bit scratchpad register on
  323. * the primary (internal) side. This allows the local system to read data
  324. * written and mirrored to the scratchpad register by the remote system.
  325. *
  326. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  327. */
  328. int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  329. {
  330. if (idx >= ndev->limits.max_spads)
  331. return -EINVAL;
  332. *val = readl(ndev->reg_ofs.spad_write + idx * 4);
  333. dev_dbg(&ndev->pdev->dev,
  334. "Reading %x from local scratch pad index %d\n", *val, idx);
  335. return 0;
  336. }
  337. /**
  338. * ntb_write_remote_spad() - write to the secondary scratchpad register
  339. * @ndev: pointer to ntb_device instance
  340. * @idx: index to the scratchpad register, 0 based
  341. * @val: the data value to put into the register
  342. *
  343. * This function allows writing of a 32bit value to the indexed scratchpad
  344. * register. The register resides on the secondary (external) side. This allows
  345. * the local system to write data to be mirrored to the remote systems
  346. * scratchpad register.
  347. *
  348. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  349. */
  350. int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  351. {
  352. if (idx >= ndev->limits.max_spads)
  353. return -EINVAL;
  354. dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
  355. val, idx);
  356. writel(val, ndev->reg_ofs.spad_write + idx * 4);
  357. return 0;
  358. }
  359. /**
  360. * ntb_read_remote_spad() - read from the primary scratchpad register
  361. * @ndev: pointer to ntb_device instance
  362. * @idx: index to scratchpad register, 0 based
  363. * @val: pointer to 32bit integer for storing the register value
  364. *
  365. * This function allows reading of the 32bit scratchpad register on
  366. * the primary (internal) side. This alloows the local system to read the data
  367. * it wrote to be mirrored on the remote system.
  368. *
  369. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  370. */
  371. int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  372. {
  373. if (idx >= ndev->limits.max_spads)
  374. return -EINVAL;
  375. *val = readl(ndev->reg_ofs.spad_read + idx * 4);
  376. dev_dbg(&ndev->pdev->dev,
  377. "Reading %x from remote scratch pad index %d\n", *val, idx);
  378. return 0;
  379. }
  380. /**
  381. * ntb_get_mw_base() - get addr for the NTB memory window
  382. * @ndev: pointer to ntb_device instance
  383. * @mw: memory window number
  384. *
  385. * This function provides the base address of the memory window specified.
  386. *
  387. * RETURNS: address, or NULL on error.
  388. */
  389. resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw)
  390. {
  391. if (mw >= ntb_max_mw(ndev))
  392. return 0;
  393. return pci_resource_start(ndev->pdev, MW_TO_BAR(mw));
  394. }
  395. /**
  396. * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
  397. * @ndev: pointer to ntb_device instance
  398. * @mw: memory window number
  399. *
  400. * This function provides the base virtual address of the memory window
  401. * specified.
  402. *
  403. * RETURNS: pointer to virtual address, or NULL on error.
  404. */
  405. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
  406. {
  407. if (mw >= ntb_max_mw(ndev))
  408. return NULL;
  409. return ndev->mw[mw].vbase;
  410. }
  411. /**
  412. * ntb_get_mw_size() - return size of NTB memory window
  413. * @ndev: pointer to ntb_device instance
  414. * @mw: memory window number
  415. *
  416. * This function provides the physical size of the memory window specified
  417. *
  418. * RETURNS: the size of the memory window or zero on error
  419. */
  420. u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
  421. {
  422. if (mw >= ntb_max_mw(ndev))
  423. return 0;
  424. return ndev->mw[mw].bar_sz;
  425. }
  426. /**
  427. * ntb_set_mw_addr - set the memory window address
  428. * @ndev: pointer to ntb_device instance
  429. * @mw: memory window number
  430. * @addr: base address for data
  431. *
  432. * This function sets the base physical address of the memory window. This
  433. * memory address is where data from the remote system will be transfered into
  434. * or out of depending on how the transport is configured.
  435. */
  436. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
  437. {
  438. if (mw >= ntb_max_mw(ndev))
  439. return;
  440. dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
  441. MW_TO_BAR(mw));
  442. ndev->mw[mw].phys_addr = addr;
  443. switch (MW_TO_BAR(mw)) {
  444. case NTB_BAR_23:
  445. writeq(addr, ndev->reg_ofs.bar2_xlat);
  446. break;
  447. case NTB_BAR_4:
  448. if (ndev->split_bar)
  449. writel(addr, ndev->reg_ofs.bar4_xlat);
  450. else
  451. writeq(addr, ndev->reg_ofs.bar4_xlat);
  452. break;
  453. case NTB_BAR_5:
  454. writel(addr, ndev->reg_ofs.bar5_xlat);
  455. break;
  456. }
  457. }
  458. /**
  459. * ntb_ring_doorbell() - Set the doorbell on the secondary/external side
  460. * @ndev: pointer to ntb_device instance
  461. * @db: doorbell to ring
  462. *
  463. * This function allows triggering of a doorbell on the secondary/external
  464. * side that will initiate an interrupt on the remote host
  465. *
  466. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  467. */
  468. void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int db)
  469. {
  470. dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
  471. if (ndev->hw_type == BWD_HW)
  472. writeq((u64) 1 << db, ndev->reg_ofs.rdb);
  473. else
  474. writew(((1 << ndev->bits_per_vector) - 1) <<
  475. (db * ndev->bits_per_vector), ndev->reg_ofs.rdb);
  476. }
  477. static void bwd_recover_link(struct ntb_device *ndev)
  478. {
  479. u32 status;
  480. /* Driver resets the NTB ModPhy lanes - magic! */
  481. writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
  482. writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
  483. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
  484. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
  485. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  486. msleep(100);
  487. /* Clear AER Errors, write to clear */
  488. status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  489. dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
  490. status &= PCI_ERR_COR_REP_ROLL;
  491. writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  492. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  493. status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  494. dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
  495. status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
  496. writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  497. /* Clear DeSkew Buffer error, write to clear */
  498. status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  499. dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
  500. status |= BWD_DESKEWSTS_DBERR;
  501. writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  502. status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  503. dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
  504. status &= BWD_IBIST_ERR_OFLOW;
  505. writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  506. /* Releases the NTB state machine to allow the link to retrain */
  507. status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  508. dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
  509. status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
  510. writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  511. }
  512. static void ntb_link_event(struct ntb_device *ndev, int link_state)
  513. {
  514. unsigned int event;
  515. if (ndev->link_status == link_state)
  516. return;
  517. if (link_state == NTB_LINK_UP) {
  518. u16 status;
  519. dev_info(&ndev->pdev->dev, "Link Up\n");
  520. ndev->link_status = NTB_LINK_UP;
  521. event = NTB_EVENT_HW_LINK_UP;
  522. if (is_ntb_atom(ndev) ||
  523. ndev->conn_type == NTB_CONN_TRANSPARENT)
  524. status = readw(ndev->reg_ofs.lnk_stat);
  525. else {
  526. int rc = pci_read_config_word(ndev->pdev,
  527. SNB_LINK_STATUS_OFFSET,
  528. &status);
  529. if (rc)
  530. return;
  531. }
  532. ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
  533. ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
  534. dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
  535. ndev->link_width, ndev->link_speed);
  536. } else {
  537. dev_info(&ndev->pdev->dev, "Link Down\n");
  538. ndev->link_status = NTB_LINK_DOWN;
  539. event = NTB_EVENT_HW_LINK_DOWN;
  540. /* Don't modify link width/speed, we need it in link recovery */
  541. }
  542. /* notify the upper layer if we have an event change */
  543. if (ndev->event_cb)
  544. ndev->event_cb(ndev->ntb_transport, event);
  545. }
  546. static int ntb_link_status(struct ntb_device *ndev)
  547. {
  548. int link_state;
  549. if (is_ntb_atom(ndev)) {
  550. u32 ntb_cntl;
  551. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  552. if (ntb_cntl & BWD_CNTL_LINK_DOWN)
  553. link_state = NTB_LINK_DOWN;
  554. else
  555. link_state = NTB_LINK_UP;
  556. } else {
  557. u16 status;
  558. int rc;
  559. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  560. &status);
  561. if (rc)
  562. return rc;
  563. if (status & NTB_LINK_STATUS_ACTIVE)
  564. link_state = NTB_LINK_UP;
  565. else
  566. link_state = NTB_LINK_DOWN;
  567. }
  568. ntb_link_event(ndev, link_state);
  569. return 0;
  570. }
  571. static void bwd_link_recovery(struct work_struct *work)
  572. {
  573. struct ntb_device *ndev = container_of(work, struct ntb_device,
  574. lr_timer.work);
  575. u32 status32;
  576. bwd_recover_link(ndev);
  577. /* There is a potential race between the 2 NTB devices recovering at the
  578. * same time. If the times are the same, the link will not recover and
  579. * the driver will be stuck in this loop forever. Add a random interval
  580. * to the recovery time to prevent this race.
  581. */
  582. msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
  583. status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  584. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
  585. goto retry;
  586. status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  587. if (status32 & BWD_IBIST_ERR_OFLOW)
  588. goto retry;
  589. status32 = readl(ndev->reg_ofs.lnk_cntl);
  590. if (!(status32 & BWD_CNTL_LINK_DOWN)) {
  591. unsigned char speed, width;
  592. u16 status16;
  593. status16 = readw(ndev->reg_ofs.lnk_stat);
  594. width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
  595. speed = (status16 & NTB_LINK_SPEED_MASK);
  596. if (ndev->link_width != width || ndev->link_speed != speed)
  597. goto retry;
  598. }
  599. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  600. return;
  601. retry:
  602. schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
  603. }
  604. /* BWD doesn't have link status interrupt, poll on that platform */
  605. static void bwd_link_poll(struct work_struct *work)
  606. {
  607. struct ntb_device *ndev = container_of(work, struct ntb_device,
  608. hb_timer.work);
  609. unsigned long ts = jiffies;
  610. /* If we haven't gotten an interrupt in a while, check the BWD link
  611. * status bit
  612. */
  613. if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
  614. int rc = ntb_link_status(ndev);
  615. if (rc)
  616. dev_err(&ndev->pdev->dev,
  617. "Error determining link status\n");
  618. /* Check to see if a link error is the cause of the link down */
  619. if (ndev->link_status == NTB_LINK_DOWN) {
  620. u32 status32 = readl(ndev->reg_base +
  621. BWD_LTSSMSTATEJMP_OFFSET);
  622. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
  623. schedule_delayed_work(&ndev->lr_timer, 0);
  624. return;
  625. }
  626. }
  627. }
  628. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  629. }
  630. static int ntb_xeon_setup(struct ntb_device *ndev)
  631. {
  632. switch (ndev->conn_type) {
  633. case NTB_CONN_B2B:
  634. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  635. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  636. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  637. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  638. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  639. if (ndev->split_bar)
  640. ndev->reg_ofs.bar5_xlat =
  641. ndev->reg_base + SNB_SBAR5XLAT_OFFSET;
  642. ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
  643. /* There is a Xeon hardware errata related to writes to
  644. * SDOORBELL or B2BDOORBELL in conjunction with inbound access
  645. * to NTB MMIO Space, which may hang the system. To workaround
  646. * this use the second memory window to access the interrupt and
  647. * scratch pad registers on the remote system.
  648. */
  649. if (ndev->wa_flags & WA_SNB_ERR) {
  650. if (!ndev->mw[ndev->limits.max_mw - 1].bar_sz)
  651. return -EINVAL;
  652. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  653. ndev->reg_ofs.spad_write =
  654. ndev->mw[ndev->limits.max_mw - 1].vbase +
  655. SNB_SPAD_OFFSET;
  656. ndev->reg_ofs.rdb =
  657. ndev->mw[ndev->limits.max_mw - 1].vbase +
  658. SNB_PDOORBELL_OFFSET;
  659. /* Set the Limit register to 4k, the minimum size, to
  660. * prevent an illegal access
  661. */
  662. writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
  663. SNB_PBAR4LMT_OFFSET);
  664. /* HW errata on the Limit registers. They can only be
  665. * written when the base register is 4GB aligned and
  666. * < 32bit. This should already be the case based on
  667. * the driver defaults, but write the Limit registers
  668. * first just in case.
  669. */
  670. ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
  671. } else {
  672. /* HW Errata on bit 14 of b2bdoorbell register. Writes
  673. * will not be mirrored to the remote system. Shrink
  674. * the number of bits by one, since bit 14 is the last
  675. * bit.
  676. */
  677. ndev->limits.max_db_bits = SNB_MAX_DB_BITS - 1;
  678. ndev->reg_ofs.spad_write = ndev->reg_base +
  679. SNB_B2B_SPAD_OFFSET;
  680. ndev->reg_ofs.rdb = ndev->reg_base +
  681. SNB_B2B_DOORBELL_OFFSET;
  682. /* Disable the Limit register, just incase it is set to
  683. * something silly. A 64bit write should handle it
  684. * regardless of whether it has a split BAR or not.
  685. */
  686. writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
  687. /* HW errata on the Limit registers. They can only be
  688. * written when the base register is 4GB aligned and
  689. * < 32bit. This should already be the case based on
  690. * the driver defaults, but write the Limit registers
  691. * first just in case.
  692. */
  693. if (ndev->split_bar)
  694. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  695. else
  696. ndev->limits.max_mw = SNB_MAX_MW;
  697. }
  698. /* The Xeon errata workaround requires setting SBAR Base
  699. * addresses to known values, so that the PBAR XLAT can be
  700. * pointed at SBAR0 of the remote system.
  701. */
  702. if (ndev->dev_type == NTB_DEV_USD) {
  703. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  704. SNB_PBAR2XLAT_OFFSET);
  705. if (ndev->wa_flags & WA_SNB_ERR)
  706. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  707. SNB_PBAR4XLAT_OFFSET);
  708. else {
  709. if (ndev->split_bar) {
  710. writel(SNB_MBAR4_DSD_ADDR,
  711. ndev->reg_base +
  712. SNB_PBAR4XLAT_OFFSET);
  713. writel(SNB_MBAR5_DSD_ADDR,
  714. ndev->reg_base +
  715. SNB_PBAR5XLAT_OFFSET);
  716. } else
  717. writeq(SNB_MBAR4_DSD_ADDR,
  718. ndev->reg_base +
  719. SNB_PBAR4XLAT_OFFSET);
  720. /* B2B_XLAT_OFFSET is a 64bit register, but can
  721. * only take 32bit writes
  722. */
  723. writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
  724. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  725. writel(SNB_MBAR01_DSD_ADDR >> 32,
  726. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  727. }
  728. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  729. SNB_SBAR0BASE_OFFSET);
  730. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  731. SNB_SBAR2BASE_OFFSET);
  732. if (ndev->split_bar) {
  733. writel(SNB_MBAR4_USD_ADDR, ndev->reg_base +
  734. SNB_SBAR4BASE_OFFSET);
  735. writel(SNB_MBAR5_USD_ADDR, ndev->reg_base +
  736. SNB_SBAR5BASE_OFFSET);
  737. } else
  738. writeq(SNB_MBAR4_USD_ADDR, ndev->reg_base +
  739. SNB_SBAR4BASE_OFFSET);
  740. } else {
  741. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  742. SNB_PBAR2XLAT_OFFSET);
  743. if (ndev->wa_flags & WA_SNB_ERR)
  744. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  745. SNB_PBAR4XLAT_OFFSET);
  746. else {
  747. if (ndev->split_bar) {
  748. writel(SNB_MBAR4_USD_ADDR,
  749. ndev->reg_base +
  750. SNB_PBAR4XLAT_OFFSET);
  751. writel(SNB_MBAR5_USD_ADDR,
  752. ndev->reg_base +
  753. SNB_PBAR5XLAT_OFFSET);
  754. } else
  755. writeq(SNB_MBAR4_USD_ADDR,
  756. ndev->reg_base +
  757. SNB_PBAR4XLAT_OFFSET);
  758. /*
  759. * B2B_XLAT_OFFSET is a 64bit register, but can
  760. * only take 32bit writes
  761. */
  762. writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
  763. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  764. writel(SNB_MBAR01_USD_ADDR >> 32,
  765. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  766. }
  767. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  768. SNB_SBAR0BASE_OFFSET);
  769. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  770. SNB_SBAR2BASE_OFFSET);
  771. if (ndev->split_bar) {
  772. writel(SNB_MBAR4_DSD_ADDR, ndev->reg_base +
  773. SNB_SBAR4BASE_OFFSET);
  774. writel(SNB_MBAR5_DSD_ADDR, ndev->reg_base +
  775. SNB_SBAR5BASE_OFFSET);
  776. } else
  777. writeq(SNB_MBAR4_DSD_ADDR, ndev->reg_base +
  778. SNB_SBAR4BASE_OFFSET);
  779. }
  780. break;
  781. case NTB_CONN_RP:
  782. if (ndev->wa_flags & WA_SNB_ERR) {
  783. dev_err(&ndev->pdev->dev,
  784. "NTB-RP disabled due to hardware errata.\n");
  785. return -EINVAL;
  786. }
  787. /* Scratch pads need to have exclusive access from the primary
  788. * or secondary side. Halve the num spads so that each side can
  789. * have an equal amount.
  790. */
  791. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  792. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  793. /* Note: The SDOORBELL is the cause of the errata. You REALLY
  794. * don't want to touch it.
  795. */
  796. ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  797. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  798. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  799. /* Offset the start of the spads to correspond to whether it is
  800. * primary or secondary
  801. */
  802. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
  803. ndev->limits.max_spads * 4;
  804. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  805. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  806. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  807. if (ndev->split_bar) {
  808. ndev->reg_ofs.bar5_xlat =
  809. ndev->reg_base + SNB_SBAR5XLAT_OFFSET;
  810. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  811. } else
  812. ndev->limits.max_mw = SNB_MAX_MW;
  813. break;
  814. case NTB_CONN_TRANSPARENT:
  815. if (ndev->wa_flags & WA_SNB_ERR) {
  816. dev_err(&ndev->pdev->dev,
  817. "NTB-TRANSPARENT disabled due to hardware errata.\n");
  818. return -EINVAL;
  819. }
  820. /* Scratch pads need to have exclusive access from the primary
  821. * or secondary side. Halve the num spads so that each side can
  822. * have an equal amount.
  823. */
  824. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  825. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  826. ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  827. ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  828. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
  829. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
  830. /* Offset the start of the spads to correspond to whether it is
  831. * primary or secondary
  832. */
  833. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
  834. ndev->limits.max_spads * 4;
  835. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
  836. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
  837. if (ndev->split_bar) {
  838. ndev->reg_ofs.bar5_xlat =
  839. ndev->reg_base + SNB_PBAR5XLAT_OFFSET;
  840. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  841. } else
  842. ndev->limits.max_mw = SNB_MAX_MW;
  843. break;
  844. default:
  845. /*
  846. * we should never hit this. the detect function should've
  847. * take cared of everything.
  848. */
  849. return -EINVAL;
  850. }
  851. ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
  852. ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
  853. ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
  854. ndev->limits.msix_cnt = SNB_MSIX_CNT;
  855. ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
  856. return 0;
  857. }
  858. static int ntb_bwd_setup(struct ntb_device *ndev)
  859. {
  860. int rc;
  861. u32 val;
  862. ndev->hw_type = BWD_HW;
  863. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
  864. if (rc)
  865. return rc;
  866. switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
  867. case NTB_CONN_B2B:
  868. ndev->conn_type = NTB_CONN_B2B;
  869. break;
  870. case NTB_CONN_RP:
  871. default:
  872. dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
  873. return -EINVAL;
  874. }
  875. if (val & BWD_PPD_DEV_TYPE)
  876. ndev->dev_type = NTB_DEV_DSD;
  877. else
  878. ndev->dev_type = NTB_DEV_USD;
  879. /* Initiate PCI-E link training */
  880. rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
  881. val | BWD_PPD_INIT_LINK);
  882. if (rc)
  883. return rc;
  884. ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  885. ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
  886. ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  887. ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  888. ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  889. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  890. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  891. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  892. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  893. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  894. ndev->limits.max_mw = BWD_MAX_MW;
  895. ndev->limits.max_spads = BWD_MAX_SPADS;
  896. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  897. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  898. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  899. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  900. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  901. INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
  902. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  903. return 0;
  904. }
  905. static int ntb_device_setup(struct ntb_device *ndev)
  906. {
  907. int rc;
  908. if (is_ntb_xeon(ndev))
  909. rc = ntb_xeon_setup(ndev);
  910. else if (is_ntb_atom(ndev))
  911. rc = ntb_bwd_setup(ndev);
  912. else
  913. rc = -ENODEV;
  914. if (rc)
  915. return rc;
  916. if (ndev->conn_type == NTB_CONN_B2B)
  917. /* Enable Bus Master and Memory Space on the secondary side */
  918. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  919. ndev->reg_ofs.spci_cmd);
  920. return 0;
  921. }
  922. static void ntb_device_free(struct ntb_device *ndev)
  923. {
  924. if (is_ntb_atom(ndev)) {
  925. cancel_delayed_work_sync(&ndev->hb_timer);
  926. cancel_delayed_work_sync(&ndev->lr_timer);
  927. }
  928. }
  929. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  930. {
  931. struct ntb_db_cb *db_cb = data;
  932. struct ntb_device *ndev = db_cb->ndev;
  933. unsigned long mask;
  934. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  935. db_cb->db_num);
  936. mask = readw(ndev->reg_ofs.ldb_mask);
  937. set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  938. writew(mask, ndev->reg_ofs.ldb_mask);
  939. tasklet_schedule(&db_cb->irq_work);
  940. /* No need to check for the specific HB irq, any interrupt means
  941. * we're connected.
  942. */
  943. ndev->last_ts = jiffies;
  944. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
  945. return IRQ_HANDLED;
  946. }
  947. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  948. {
  949. struct ntb_db_cb *db_cb = data;
  950. struct ntb_device *ndev = db_cb->ndev;
  951. unsigned long mask;
  952. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  953. db_cb->db_num);
  954. mask = readw(ndev->reg_ofs.ldb_mask);
  955. set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  956. writew(mask, ndev->reg_ofs.ldb_mask);
  957. tasklet_schedule(&db_cb->irq_work);
  958. /* On Sandybridge, there are 16 bits in the interrupt register
  959. * but only 4 vectors. So, 5 bits are assigned to the first 3
  960. * vectors, with the 4th having a single bit for link
  961. * interrupts.
  962. */
  963. writew(((1 << ndev->bits_per_vector) - 1) <<
  964. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
  965. return IRQ_HANDLED;
  966. }
  967. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  968. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  969. {
  970. struct ntb_device *ndev = dev;
  971. int rc;
  972. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  973. rc = ntb_link_status(ndev);
  974. if (rc)
  975. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  976. /* bit 15 is always the link bit */
  977. writew(1 << SNB_LINK_DB, ndev->reg_ofs.ldb);
  978. return IRQ_HANDLED;
  979. }
  980. static irqreturn_t ntb_interrupt(int irq, void *dev)
  981. {
  982. struct ntb_device *ndev = dev;
  983. unsigned int i = 0;
  984. if (is_ntb_atom(ndev)) {
  985. u64 ldb = readq(ndev->reg_ofs.ldb);
  986. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
  987. while (ldb) {
  988. i = __ffs(ldb);
  989. ldb &= ldb - 1;
  990. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  991. }
  992. } else {
  993. u16 ldb = readw(ndev->reg_ofs.ldb);
  994. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
  995. if (ldb & SNB_DB_HW_LINK) {
  996. xeon_event_msix_irq(irq, dev);
  997. ldb &= ~SNB_DB_HW_LINK;
  998. }
  999. while (ldb) {
  1000. i = __ffs(ldb);
  1001. ldb &= ldb - 1;
  1002. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  1003. }
  1004. }
  1005. return IRQ_HANDLED;
  1006. }
  1007. static int ntb_setup_snb_msix(struct ntb_device *ndev, int msix_entries)
  1008. {
  1009. struct pci_dev *pdev = ndev->pdev;
  1010. struct msix_entry *msix;
  1011. int rc, i;
  1012. if (msix_entries < ndev->limits.msix_cnt)
  1013. return -ENOSPC;
  1014. rc = pci_enable_msix_exact(pdev, ndev->msix_entries, msix_entries);
  1015. if (rc < 0)
  1016. return rc;
  1017. for (i = 0; i < msix_entries; i++) {
  1018. msix = &ndev->msix_entries[i];
  1019. WARN_ON(!msix->vector);
  1020. if (i == msix_entries - 1) {
  1021. rc = request_irq(msix->vector,
  1022. xeon_event_msix_irq, 0,
  1023. "ntb-event-msix", ndev);
  1024. if (rc)
  1025. goto err;
  1026. } else {
  1027. rc = request_irq(msix->vector,
  1028. xeon_callback_msix_irq, 0,
  1029. "ntb-callback-msix",
  1030. &ndev->db_cb[i]);
  1031. if (rc)
  1032. goto err;
  1033. }
  1034. }
  1035. ndev->num_msix = msix_entries;
  1036. ndev->max_cbs = msix_entries - 1;
  1037. return 0;
  1038. err:
  1039. while (--i >= 0) {
  1040. /* Code never reaches here for entry nr 'ndev->num_msix - 1' */
  1041. msix = &ndev->msix_entries[i];
  1042. free_irq(msix->vector, &ndev->db_cb[i]);
  1043. }
  1044. pci_disable_msix(pdev);
  1045. ndev->num_msix = 0;
  1046. return rc;
  1047. }
  1048. static int ntb_setup_bwd_msix(struct ntb_device *ndev, int msix_entries)
  1049. {
  1050. struct pci_dev *pdev = ndev->pdev;
  1051. struct msix_entry *msix;
  1052. int rc, i;
  1053. msix_entries = pci_enable_msix_range(pdev, ndev->msix_entries,
  1054. 1, msix_entries);
  1055. if (msix_entries < 0)
  1056. return msix_entries;
  1057. for (i = 0; i < msix_entries; i++) {
  1058. msix = &ndev->msix_entries[i];
  1059. WARN_ON(!msix->vector);
  1060. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  1061. "ntb-callback-msix", &ndev->db_cb[i]);
  1062. if (rc)
  1063. goto err;
  1064. }
  1065. ndev->num_msix = msix_entries;
  1066. ndev->max_cbs = msix_entries;
  1067. return 0;
  1068. err:
  1069. while (--i >= 0)
  1070. free_irq(msix->vector, &ndev->db_cb[i]);
  1071. pci_disable_msix(pdev);
  1072. ndev->num_msix = 0;
  1073. return rc;
  1074. }
  1075. static int ntb_setup_msix(struct ntb_device *ndev)
  1076. {
  1077. struct pci_dev *pdev = ndev->pdev;
  1078. int msix_entries;
  1079. int rc, i;
  1080. msix_entries = pci_msix_vec_count(pdev);
  1081. if (msix_entries < 0) {
  1082. rc = msix_entries;
  1083. goto err;
  1084. } else if (msix_entries > ndev->limits.msix_cnt) {
  1085. rc = -EINVAL;
  1086. goto err;
  1087. }
  1088. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  1089. GFP_KERNEL);
  1090. if (!ndev->msix_entries) {
  1091. rc = -ENOMEM;
  1092. goto err;
  1093. }
  1094. for (i = 0; i < msix_entries; i++)
  1095. ndev->msix_entries[i].entry = i;
  1096. if (is_ntb_atom(ndev))
  1097. rc = ntb_setup_bwd_msix(ndev, msix_entries);
  1098. else
  1099. rc = ntb_setup_snb_msix(ndev, msix_entries);
  1100. if (rc)
  1101. goto err1;
  1102. return 0;
  1103. err1:
  1104. kfree(ndev->msix_entries);
  1105. err:
  1106. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  1107. return rc;
  1108. }
  1109. static int ntb_setup_msi(struct ntb_device *ndev)
  1110. {
  1111. struct pci_dev *pdev = ndev->pdev;
  1112. int rc;
  1113. rc = pci_enable_msi(pdev);
  1114. if (rc)
  1115. return rc;
  1116. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  1117. if (rc) {
  1118. pci_disable_msi(pdev);
  1119. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  1120. return rc;
  1121. }
  1122. return 0;
  1123. }
  1124. static int ntb_setup_intx(struct ntb_device *ndev)
  1125. {
  1126. struct pci_dev *pdev = ndev->pdev;
  1127. int rc;
  1128. pci_msi_off(pdev);
  1129. /* Verify intx is enabled */
  1130. pci_intx(pdev, 1);
  1131. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  1132. ndev);
  1133. if (rc)
  1134. return rc;
  1135. return 0;
  1136. }
  1137. static int ntb_setup_interrupts(struct ntb_device *ndev)
  1138. {
  1139. int rc;
  1140. /* On BWD, disable all interrupts. On SNB, disable all but Link
  1141. * Interrupt. The rest will be unmasked as callbacks are registered.
  1142. */
  1143. if (is_ntb_atom(ndev))
  1144. writeq(~0, ndev->reg_ofs.ldb_mask);
  1145. else {
  1146. u16 var = 1 << SNB_LINK_DB;
  1147. writew(~var, ndev->reg_ofs.ldb_mask);
  1148. }
  1149. rc = ntb_setup_msix(ndev);
  1150. if (!rc)
  1151. goto done;
  1152. ndev->bits_per_vector = 1;
  1153. ndev->max_cbs = ndev->limits.max_db_bits;
  1154. rc = ntb_setup_msi(ndev);
  1155. if (!rc)
  1156. goto done;
  1157. rc = ntb_setup_intx(ndev);
  1158. if (rc) {
  1159. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  1160. return rc;
  1161. }
  1162. done:
  1163. return 0;
  1164. }
  1165. static void ntb_free_interrupts(struct ntb_device *ndev)
  1166. {
  1167. struct pci_dev *pdev = ndev->pdev;
  1168. /* mask interrupts */
  1169. if (is_ntb_atom(ndev))
  1170. writeq(~0, ndev->reg_ofs.ldb_mask);
  1171. else
  1172. writew(~0, ndev->reg_ofs.ldb_mask);
  1173. if (ndev->num_msix) {
  1174. struct msix_entry *msix;
  1175. u32 i;
  1176. for (i = 0; i < ndev->num_msix; i++) {
  1177. msix = &ndev->msix_entries[i];
  1178. if (is_ntb_xeon(ndev) && i == ndev->num_msix - 1)
  1179. free_irq(msix->vector, ndev);
  1180. else
  1181. free_irq(msix->vector, &ndev->db_cb[i]);
  1182. }
  1183. pci_disable_msix(pdev);
  1184. kfree(ndev->msix_entries);
  1185. } else {
  1186. free_irq(pdev->irq, ndev);
  1187. if (pci_dev_msi_enabled(pdev))
  1188. pci_disable_msi(pdev);
  1189. }
  1190. }
  1191. static int ntb_create_callbacks(struct ntb_device *ndev)
  1192. {
  1193. int i;
  1194. /* Chicken-egg issue. We won't know how many callbacks are necessary
  1195. * until we see how many MSI-X vectors we get, but these pointers need
  1196. * to be passed into the MSI-X register function. So, we allocate the
  1197. * max, knowing that they might not all be used, to work around this.
  1198. */
  1199. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  1200. sizeof(struct ntb_db_cb),
  1201. GFP_KERNEL);
  1202. if (!ndev->db_cb)
  1203. return -ENOMEM;
  1204. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  1205. ndev->db_cb[i].db_num = i;
  1206. ndev->db_cb[i].ndev = ndev;
  1207. }
  1208. return 0;
  1209. }
  1210. static void ntb_free_callbacks(struct ntb_device *ndev)
  1211. {
  1212. int i;
  1213. for (i = 0; i < ndev->limits.max_db_bits; i++)
  1214. ntb_unregister_db_callback(ndev, i);
  1215. kfree(ndev->db_cb);
  1216. }
  1217. static ssize_t ntb_debugfs_read(struct file *filp, char __user *ubuf,
  1218. size_t count, loff_t *offp)
  1219. {
  1220. struct ntb_device *ndev;
  1221. char *buf;
  1222. ssize_t ret, offset, out_count;
  1223. out_count = 500;
  1224. buf = kmalloc(out_count, GFP_KERNEL);
  1225. if (!buf)
  1226. return -ENOMEM;
  1227. ndev = filp->private_data;
  1228. offset = 0;
  1229. offset += snprintf(buf + offset, out_count - offset,
  1230. "NTB Device Information:\n");
  1231. offset += snprintf(buf + offset, out_count - offset,
  1232. "Connection Type - \t\t%s\n",
  1233. ndev->conn_type == NTB_CONN_TRANSPARENT ?
  1234. "Transparent" : (ndev->conn_type == NTB_CONN_B2B) ?
  1235. "Back to back" : "Root Port");
  1236. offset += snprintf(buf + offset, out_count - offset,
  1237. "Device Type - \t\t\t%s\n",
  1238. ndev->dev_type == NTB_DEV_USD ?
  1239. "DSD/USP" : "USD/DSP");
  1240. offset += snprintf(buf + offset, out_count - offset,
  1241. "Max Number of Callbacks - \t%u\n",
  1242. ntb_max_cbs(ndev));
  1243. offset += snprintf(buf + offset, out_count - offset,
  1244. "Link Status - \t\t\t%s\n",
  1245. ntb_hw_link_status(ndev) ? "Up" : "Down");
  1246. if (ntb_hw_link_status(ndev)) {
  1247. offset += snprintf(buf + offset, out_count - offset,
  1248. "Link Speed - \t\t\tPCI-E Gen %u\n",
  1249. ndev->link_speed);
  1250. offset += snprintf(buf + offset, out_count - offset,
  1251. "Link Width - \t\t\tx%u\n",
  1252. ndev->link_width);
  1253. }
  1254. if (is_ntb_xeon(ndev)) {
  1255. u32 status32;
  1256. u16 status16;
  1257. int rc;
  1258. offset += snprintf(buf + offset, out_count - offset,
  1259. "\nNTB Device Statistics:\n");
  1260. offset += snprintf(buf + offset, out_count - offset,
  1261. "Upstream Memory Miss - \t%u\n",
  1262. readw(ndev->reg_base +
  1263. SNB_USMEMMISS_OFFSET));
  1264. offset += snprintf(buf + offset, out_count - offset,
  1265. "\nNTB Hardware Errors:\n");
  1266. rc = pci_read_config_word(ndev->pdev, SNB_DEVSTS_OFFSET,
  1267. &status16);
  1268. if (!rc)
  1269. offset += snprintf(buf + offset, out_count - offset,
  1270. "DEVSTS - \t%#06x\n", status16);
  1271. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  1272. &status16);
  1273. if (!rc)
  1274. offset += snprintf(buf + offset, out_count - offset,
  1275. "LNKSTS - \t%#06x\n", status16);
  1276. rc = pci_read_config_dword(ndev->pdev, SNB_UNCERRSTS_OFFSET,
  1277. &status32);
  1278. if (!rc)
  1279. offset += snprintf(buf + offset, out_count - offset,
  1280. "UNCERRSTS - \t%#010x\n", status32);
  1281. rc = pci_read_config_dword(ndev->pdev, SNB_CORERRSTS_OFFSET,
  1282. &status32);
  1283. if (!rc)
  1284. offset += snprintf(buf + offset, out_count - offset,
  1285. "CORERRSTS - \t%#010x\n", status32);
  1286. }
  1287. if (offset > out_count)
  1288. offset = out_count;
  1289. ret = simple_read_from_buffer(ubuf, count, offp, buf, offset);
  1290. kfree(buf);
  1291. return ret;
  1292. }
  1293. static const struct file_operations ntb_debugfs_info = {
  1294. .owner = THIS_MODULE,
  1295. .open = simple_open,
  1296. .read = ntb_debugfs_read,
  1297. };
  1298. static void ntb_setup_debugfs(struct ntb_device *ndev)
  1299. {
  1300. if (!debugfs_initialized())
  1301. return;
  1302. if (!debugfs_dir)
  1303. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1304. ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
  1305. debugfs_dir);
  1306. if (ndev->debugfs_dir)
  1307. ndev->debugfs_info = debugfs_create_file("info", S_IRUSR,
  1308. ndev->debugfs_dir,
  1309. ndev,
  1310. &ntb_debugfs_info);
  1311. }
  1312. static void ntb_free_debugfs(struct ntb_device *ndev)
  1313. {
  1314. debugfs_remove_recursive(ndev->debugfs_dir);
  1315. if (debugfs_dir && simple_empty(debugfs_dir)) {
  1316. debugfs_remove_recursive(debugfs_dir);
  1317. debugfs_dir = NULL;
  1318. }
  1319. }
  1320. static void ntb_hw_link_up(struct ntb_device *ndev)
  1321. {
  1322. if (ndev->conn_type == NTB_CONN_TRANSPARENT)
  1323. ntb_link_event(ndev, NTB_LINK_UP);
  1324. else {
  1325. u32 ntb_cntl;
  1326. /* Let's bring the NTB link up */
  1327. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1328. ntb_cntl &= ~(NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK);
  1329. ntb_cntl |= NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP;
  1330. ntb_cntl |= NTB_CNTL_P2S_BAR4_SNOOP | NTB_CNTL_S2P_BAR4_SNOOP;
  1331. if (ndev->split_bar)
  1332. ntb_cntl |= NTB_CNTL_P2S_BAR5_SNOOP |
  1333. NTB_CNTL_S2P_BAR5_SNOOP;
  1334. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1335. }
  1336. }
  1337. static void ntb_hw_link_down(struct ntb_device *ndev)
  1338. {
  1339. u32 ntb_cntl;
  1340. if (ndev->conn_type == NTB_CONN_TRANSPARENT) {
  1341. ntb_link_event(ndev, NTB_LINK_DOWN);
  1342. return;
  1343. }
  1344. /* Bring NTB link down */
  1345. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1346. ntb_cntl &= ~(NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP);
  1347. ntb_cntl &= ~(NTB_CNTL_P2S_BAR4_SNOOP | NTB_CNTL_S2P_BAR4_SNOOP);
  1348. if (ndev->split_bar)
  1349. ntb_cntl &= ~(NTB_CNTL_P2S_BAR5_SNOOP |
  1350. NTB_CNTL_S2P_BAR5_SNOOP);
  1351. ntb_cntl |= NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK;
  1352. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1353. }
  1354. static void ntb_max_mw_detect(struct ntb_device *ndev)
  1355. {
  1356. if (ndev->split_bar)
  1357. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  1358. else
  1359. ndev->limits.max_mw = SNB_MAX_MW;
  1360. }
  1361. static int ntb_xeon_detect(struct ntb_device *ndev)
  1362. {
  1363. int rc, bars_mask;
  1364. u32 bars;
  1365. u8 ppd;
  1366. ndev->hw_type = SNB_HW;
  1367. rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &ppd);
  1368. if (rc)
  1369. return -EIO;
  1370. if (ppd & SNB_PPD_DEV_TYPE)
  1371. ndev->dev_type = NTB_DEV_USD;
  1372. else
  1373. ndev->dev_type = NTB_DEV_DSD;
  1374. ndev->split_bar = (ppd & SNB_PPD_SPLIT_BAR) ? 1 : 0;
  1375. switch (ppd & SNB_PPD_CONN_TYPE) {
  1376. case NTB_CONN_B2B:
  1377. dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
  1378. ndev->conn_type = NTB_CONN_B2B;
  1379. break;
  1380. case NTB_CONN_RP:
  1381. dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
  1382. ndev->conn_type = NTB_CONN_RP;
  1383. break;
  1384. case NTB_CONN_TRANSPARENT:
  1385. dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
  1386. ndev->conn_type = NTB_CONN_TRANSPARENT;
  1387. /*
  1388. * This mode is default to USD/DSP. HW does not report
  1389. * properly in transparent mode as it has no knowledge of
  1390. * NTB. We will just force correct here.
  1391. */
  1392. ndev->dev_type = NTB_DEV_USD;
  1393. /*
  1394. * This is a way for transparent BAR to figure out if we
  1395. * are doing split BAR or not. There is no way for the hw
  1396. * on the transparent side to know and set the PPD.
  1397. */
  1398. bars_mask = pci_select_bars(ndev->pdev, IORESOURCE_MEM);
  1399. bars = hweight32(bars_mask);
  1400. if (bars == (HSX_SPLITBAR_MAX_MW + 1))
  1401. ndev->split_bar = 1;
  1402. break;
  1403. default:
  1404. dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", ppd);
  1405. return -ENODEV;
  1406. }
  1407. ntb_max_mw_detect(ndev);
  1408. return 0;
  1409. }
  1410. static int ntb_atom_detect(struct ntb_device *ndev)
  1411. {
  1412. int rc;
  1413. u32 ppd;
  1414. ndev->hw_type = BWD_HW;
  1415. ndev->limits.max_mw = BWD_MAX_MW;
  1416. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &ppd);
  1417. if (rc)
  1418. return rc;
  1419. switch ((ppd & BWD_PPD_CONN_TYPE) >> 8) {
  1420. case NTB_CONN_B2B:
  1421. dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
  1422. ndev->conn_type = NTB_CONN_B2B;
  1423. break;
  1424. case NTB_CONN_RP:
  1425. default:
  1426. dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
  1427. return -EINVAL;
  1428. }
  1429. if (ppd & BWD_PPD_DEV_TYPE)
  1430. ndev->dev_type = NTB_DEV_DSD;
  1431. else
  1432. ndev->dev_type = NTB_DEV_USD;
  1433. return 0;
  1434. }
  1435. static int ntb_device_detect(struct ntb_device *ndev)
  1436. {
  1437. int rc;
  1438. if (is_ntb_xeon(ndev))
  1439. rc = ntb_xeon_detect(ndev);
  1440. else if (is_ntb_atom(ndev))
  1441. rc = ntb_atom_detect(ndev);
  1442. else
  1443. rc = -ENODEV;
  1444. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  1445. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  1446. return 0;
  1447. }
  1448. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1449. {
  1450. struct ntb_device *ndev;
  1451. int rc, i;
  1452. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  1453. if (!ndev)
  1454. return -ENOMEM;
  1455. ndev->pdev = pdev;
  1456. ntb_set_errata_flags(ndev);
  1457. ndev->link_status = NTB_LINK_DOWN;
  1458. pci_set_drvdata(pdev, ndev);
  1459. ntb_setup_debugfs(ndev);
  1460. rc = pci_enable_device(pdev);
  1461. if (rc)
  1462. goto err;
  1463. pci_set_master(ndev->pdev);
  1464. rc = ntb_device_detect(ndev);
  1465. if (rc)
  1466. goto err;
  1467. ndev->mw = kcalloc(ndev->limits.max_mw, sizeof(struct ntb_mw),
  1468. GFP_KERNEL);
  1469. if (!ndev->mw) {
  1470. rc = -ENOMEM;
  1471. goto err1;
  1472. }
  1473. if (ndev->split_bar)
  1474. rc = pci_request_selected_regions(pdev, NTB_SPLITBAR_MASK,
  1475. KBUILD_MODNAME);
  1476. else
  1477. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK,
  1478. KBUILD_MODNAME);
  1479. if (rc)
  1480. goto err2;
  1481. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  1482. if (!ndev->reg_base) {
  1483. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  1484. rc = -EIO;
  1485. goto err3;
  1486. }
  1487. for (i = 0; i < ndev->limits.max_mw; i++) {
  1488. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  1489. /*
  1490. * with the errata we need to steal last of the memory
  1491. * windows for workarounds and they point to MMIO registers.
  1492. */
  1493. if ((ndev->wa_flags & WA_SNB_ERR) &&
  1494. (i == (ndev->limits.max_mw - 1))) {
  1495. ndev->mw[i].vbase =
  1496. ioremap_nocache(pci_resource_start(pdev,
  1497. MW_TO_BAR(i)),
  1498. ndev->mw[i].bar_sz);
  1499. } else {
  1500. ndev->mw[i].vbase =
  1501. ioremap_wc(pci_resource_start(pdev,
  1502. MW_TO_BAR(i)),
  1503. ndev->mw[i].bar_sz);
  1504. }
  1505. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  1506. (unsigned long long) ndev->mw[i].bar_sz);
  1507. if (!ndev->mw[i].vbase) {
  1508. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  1509. MW_TO_BAR(i));
  1510. rc = -EIO;
  1511. goto err4;
  1512. }
  1513. }
  1514. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1515. if (rc) {
  1516. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1517. if (rc)
  1518. goto err4;
  1519. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  1520. }
  1521. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1522. if (rc) {
  1523. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1524. if (rc)
  1525. goto err4;
  1526. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  1527. }
  1528. rc = ntb_device_setup(ndev);
  1529. if (rc)
  1530. goto err4;
  1531. rc = ntb_create_callbacks(ndev);
  1532. if (rc)
  1533. goto err5;
  1534. rc = ntb_setup_interrupts(ndev);
  1535. if (rc)
  1536. goto err6;
  1537. /* The scratchpad registers keep the values between rmmod/insmod,
  1538. * blast them now
  1539. */
  1540. for (i = 0; i < ndev->limits.max_spads; i++) {
  1541. ntb_write_local_spad(ndev, i, 0);
  1542. ntb_write_remote_spad(ndev, i, 0);
  1543. }
  1544. rc = ntb_transport_init(pdev);
  1545. if (rc)
  1546. goto err7;
  1547. ntb_hw_link_up(ndev);
  1548. return 0;
  1549. err7:
  1550. ntb_free_interrupts(ndev);
  1551. err6:
  1552. ntb_free_callbacks(ndev);
  1553. err5:
  1554. ntb_device_free(ndev);
  1555. err4:
  1556. for (i--; i >= 0; i--)
  1557. iounmap(ndev->mw[i].vbase);
  1558. iounmap(ndev->reg_base);
  1559. err3:
  1560. if (ndev->split_bar)
  1561. pci_release_selected_regions(pdev, NTB_SPLITBAR_MASK);
  1562. else
  1563. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1564. err2:
  1565. kfree(ndev->mw);
  1566. err1:
  1567. pci_disable_device(pdev);
  1568. err:
  1569. ntb_free_debugfs(ndev);
  1570. kfree(ndev);
  1571. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  1572. return rc;
  1573. }
  1574. static void ntb_pci_remove(struct pci_dev *pdev)
  1575. {
  1576. struct ntb_device *ndev = pci_get_drvdata(pdev);
  1577. int i;
  1578. ntb_hw_link_down(ndev);
  1579. ntb_transport_free(ndev->ntb_transport);
  1580. ntb_free_interrupts(ndev);
  1581. ntb_free_callbacks(ndev);
  1582. ntb_device_free(ndev);
  1583. /* need to reset max_mw limits so we can unmap properly */
  1584. if (ndev->hw_type == SNB_HW)
  1585. ntb_max_mw_detect(ndev);
  1586. for (i = 0; i < ndev->limits.max_mw; i++)
  1587. iounmap(ndev->mw[i].vbase);
  1588. kfree(ndev->mw);
  1589. iounmap(ndev->reg_base);
  1590. if (ndev->split_bar)
  1591. pci_release_selected_regions(pdev, NTB_SPLITBAR_MASK);
  1592. else
  1593. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1594. pci_disable_device(pdev);
  1595. ntb_free_debugfs(ndev);
  1596. kfree(ndev);
  1597. }
  1598. static struct pci_driver ntb_pci_driver = {
  1599. .name = KBUILD_MODNAME,
  1600. .id_table = ntb_pci_tbl,
  1601. .probe = ntb_pci_probe,
  1602. .remove = ntb_pci_remove,
  1603. };
  1604. module_pci_driver(ntb_pci_driver);