ntb_hw.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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 "0.25"
  60. MODULE_DESCRIPTION(NTB_NAME);
  61. MODULE_VERSION(NTB_VER);
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. MODULE_AUTHOR("Intel Corporation");
  64. static bool xeon_errata_workaround = true;
  65. module_param(xeon_errata_workaround, bool, 0644);
  66. MODULE_PARM_DESC(xeon_errata_workaround, "Workaround for the Xeon Errata");
  67. enum {
  68. NTB_CONN_TRANSPARENT = 0,
  69. NTB_CONN_B2B,
  70. NTB_CONN_RP,
  71. };
  72. enum {
  73. NTB_DEV_USD = 0,
  74. NTB_DEV_DSD,
  75. };
  76. enum {
  77. SNB_HW = 0,
  78. BWD_HW,
  79. };
  80. static struct dentry *debugfs_dir;
  81. #define BWD_LINK_RECOVERY_TIME 500
  82. /* Translate memory window 0,1 to BAR 2,4 */
  83. #define MW_TO_BAR(mw) (mw * NTB_MAX_NUM_MW + 2)
  84. static DEFINE_PCI_DEVICE_TABLE(ntb_pci_tbl) = {
  85. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  86. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  87. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  88. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  89. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  90. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  91. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  92. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  93. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  94. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  95. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  96. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  97. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  98. {0}
  99. };
  100. MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
  101. /**
  102. * ntb_register_event_callback() - register event callback
  103. * @ndev: pointer to ntb_device instance
  104. * @func: callback function to register
  105. *
  106. * This function registers a callback for any HW driver events such as link
  107. * up/down, power management notices and etc.
  108. *
  109. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  110. */
  111. int ntb_register_event_callback(struct ntb_device *ndev,
  112. void (*func)(void *handle, enum ntb_hw_event event))
  113. {
  114. if (ndev->event_cb)
  115. return -EINVAL;
  116. ndev->event_cb = func;
  117. return 0;
  118. }
  119. /**
  120. * ntb_unregister_event_callback() - unregisters the event callback
  121. * @ndev: pointer to ntb_device instance
  122. *
  123. * This function unregisters the existing callback from transport
  124. */
  125. void ntb_unregister_event_callback(struct ntb_device *ndev)
  126. {
  127. ndev->event_cb = NULL;
  128. }
  129. /**
  130. * ntb_register_db_callback() - register a callback for doorbell interrupt
  131. * @ndev: pointer to ntb_device instance
  132. * @idx: doorbell index to register callback, zero based
  133. * @func: callback function to register
  134. *
  135. * This function registers a callback function for the doorbell interrupt
  136. * on the primary side. The function will unmask the doorbell as well to
  137. * allow interrupt.
  138. *
  139. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  140. */
  141. int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
  142. void *data, void (*func)(void *data, int db_num))
  143. {
  144. unsigned long mask;
  145. if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
  146. dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
  147. return -EINVAL;
  148. }
  149. ndev->db_cb[idx].callback = func;
  150. ndev->db_cb[idx].data = data;
  151. /* unmask interrupt */
  152. mask = readw(ndev->reg_ofs.ldb_mask);
  153. clear_bit(idx * ndev->bits_per_vector, &mask);
  154. writew(mask, ndev->reg_ofs.ldb_mask);
  155. return 0;
  156. }
  157. /**
  158. * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
  159. * @ndev: pointer to ntb_device instance
  160. * @idx: doorbell index to register callback, zero based
  161. *
  162. * This function unregisters a callback function for the doorbell interrupt
  163. * on the primary side. The function will also mask the said doorbell.
  164. */
  165. void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
  166. {
  167. unsigned long mask;
  168. if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
  169. return;
  170. mask = readw(ndev->reg_ofs.ldb_mask);
  171. set_bit(idx * ndev->bits_per_vector, &mask);
  172. writew(mask, ndev->reg_ofs.ldb_mask);
  173. ndev->db_cb[idx].callback = NULL;
  174. }
  175. /**
  176. * ntb_find_transport() - find the transport pointer
  177. * @transport: pointer to pci device
  178. *
  179. * Given the pci device pointer, return the transport pointer passed in when
  180. * the transport attached when it was inited.
  181. *
  182. * RETURNS: pointer to transport.
  183. */
  184. void *ntb_find_transport(struct pci_dev *pdev)
  185. {
  186. struct ntb_device *ndev = pci_get_drvdata(pdev);
  187. return ndev->ntb_transport;
  188. }
  189. /**
  190. * ntb_register_transport() - Register NTB transport with NTB HW driver
  191. * @transport: transport identifier
  192. *
  193. * This function allows a transport to reserve the hardware driver for
  194. * NTB usage.
  195. *
  196. * RETURNS: pointer to ntb_device, NULL on error.
  197. */
  198. struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
  199. {
  200. struct ntb_device *ndev = pci_get_drvdata(pdev);
  201. if (ndev->ntb_transport)
  202. return NULL;
  203. ndev->ntb_transport = transport;
  204. return ndev;
  205. }
  206. /**
  207. * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
  208. * @ndev - ntb_device of the transport to be freed
  209. *
  210. * This function unregisters the transport from the HW driver and performs any
  211. * necessary cleanups.
  212. */
  213. void ntb_unregister_transport(struct ntb_device *ndev)
  214. {
  215. int i;
  216. if (!ndev->ntb_transport)
  217. return;
  218. for (i = 0; i < ndev->max_cbs; i++)
  219. ntb_unregister_db_callback(ndev, i);
  220. ntb_unregister_event_callback(ndev);
  221. ndev->ntb_transport = NULL;
  222. }
  223. /**
  224. * ntb_write_local_spad() - write to the secondary scratchpad register
  225. * @ndev: pointer to ntb_device instance
  226. * @idx: index to the scratchpad register, 0 based
  227. * @val: the data value to put into the register
  228. *
  229. * This function allows writing of a 32bit value to the indexed scratchpad
  230. * register. This writes over the data mirrored to the local scratchpad register
  231. * by the remote system.
  232. *
  233. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  234. */
  235. int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  236. {
  237. if (idx >= ndev->limits.max_spads)
  238. return -EINVAL;
  239. dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
  240. val, idx);
  241. writel(val, ndev->reg_ofs.spad_read + idx * 4);
  242. return 0;
  243. }
  244. /**
  245. * ntb_read_local_spad() - read from the primary scratchpad register
  246. * @ndev: pointer to ntb_device instance
  247. * @idx: index to scratchpad register, 0 based
  248. * @val: pointer to 32bit integer for storing the register value
  249. *
  250. * This function allows reading of the 32bit scratchpad register on
  251. * the primary (internal) side. This allows the local system to read data
  252. * written and mirrored to the scratchpad register by the remote system.
  253. *
  254. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  255. */
  256. int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  257. {
  258. if (idx >= ndev->limits.max_spads)
  259. return -EINVAL;
  260. *val = readl(ndev->reg_ofs.spad_write + idx * 4);
  261. dev_dbg(&ndev->pdev->dev,
  262. "Reading %x from local scratch pad index %d\n", *val, idx);
  263. return 0;
  264. }
  265. /**
  266. * ntb_write_remote_spad() - write to the secondary scratchpad register
  267. * @ndev: pointer to ntb_device instance
  268. * @idx: index to the scratchpad register, 0 based
  269. * @val: the data value to put into the register
  270. *
  271. * This function allows writing of a 32bit value to the indexed scratchpad
  272. * register. The register resides on the secondary (external) side. This allows
  273. * the local system to write data to be mirrored to the remote systems
  274. * scratchpad register.
  275. *
  276. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  277. */
  278. int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  279. {
  280. if (idx >= ndev->limits.max_spads)
  281. return -EINVAL;
  282. dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
  283. val, idx);
  284. writel(val, ndev->reg_ofs.spad_write + idx * 4);
  285. return 0;
  286. }
  287. /**
  288. * ntb_read_remote_spad() - read from the primary scratchpad register
  289. * @ndev: pointer to ntb_device instance
  290. * @idx: index to scratchpad register, 0 based
  291. * @val: pointer to 32bit integer for storing the register value
  292. *
  293. * This function allows reading of the 32bit scratchpad register on
  294. * the primary (internal) side. This alloows the local system to read the data
  295. * it wrote to be mirrored on the remote system.
  296. *
  297. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  298. */
  299. int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  300. {
  301. if (idx >= ndev->limits.max_spads)
  302. return -EINVAL;
  303. *val = readl(ndev->reg_ofs.spad_read + idx * 4);
  304. dev_dbg(&ndev->pdev->dev,
  305. "Reading %x from remote scratch pad index %d\n", *val, idx);
  306. return 0;
  307. }
  308. /**
  309. * ntb_get_mw_base() - get addr for the NTB memory window
  310. * @ndev: pointer to ntb_device instance
  311. * @mw: memory window number
  312. *
  313. * This function provides the base address of the memory window specified.
  314. *
  315. * RETURNS: address, or NULL on error.
  316. */
  317. resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw)
  318. {
  319. if (mw >= ntb_max_mw(ndev))
  320. return 0;
  321. return pci_resource_start(ndev->pdev, MW_TO_BAR(mw));
  322. }
  323. /**
  324. * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
  325. * @ndev: pointer to ntb_device instance
  326. * @mw: memory window number
  327. *
  328. * This function provides the base virtual address of the memory window
  329. * specified.
  330. *
  331. * RETURNS: pointer to virtual address, or NULL on error.
  332. */
  333. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
  334. {
  335. if (mw >= ntb_max_mw(ndev))
  336. return NULL;
  337. return ndev->mw[mw].vbase;
  338. }
  339. /**
  340. * ntb_get_mw_size() - return size of NTB memory window
  341. * @ndev: pointer to ntb_device instance
  342. * @mw: memory window number
  343. *
  344. * This function provides the physical size of the memory window specified
  345. *
  346. * RETURNS: the size of the memory window or zero on error
  347. */
  348. u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
  349. {
  350. if (mw >= ntb_max_mw(ndev))
  351. return 0;
  352. return ndev->mw[mw].bar_sz;
  353. }
  354. /**
  355. * ntb_set_mw_addr - set the memory window address
  356. * @ndev: pointer to ntb_device instance
  357. * @mw: memory window number
  358. * @addr: base address for data
  359. *
  360. * This function sets the base physical address of the memory window. This
  361. * memory address is where data from the remote system will be transfered into
  362. * or out of depending on how the transport is configured.
  363. */
  364. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
  365. {
  366. if (mw >= ntb_max_mw(ndev))
  367. return;
  368. dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
  369. MW_TO_BAR(mw));
  370. ndev->mw[mw].phys_addr = addr;
  371. switch (MW_TO_BAR(mw)) {
  372. case NTB_BAR_23:
  373. writeq(addr, ndev->reg_ofs.bar2_xlat);
  374. break;
  375. case NTB_BAR_45:
  376. writeq(addr, ndev->reg_ofs.bar4_xlat);
  377. break;
  378. }
  379. }
  380. /**
  381. * ntb_ring_doorbell() - Set the doorbell on the secondary/external side
  382. * @ndev: pointer to ntb_device instance
  383. * @db: doorbell to ring
  384. *
  385. * This function allows triggering of a doorbell on the secondary/external
  386. * side that will initiate an interrupt on the remote host
  387. *
  388. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  389. */
  390. void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int db)
  391. {
  392. dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
  393. if (ndev->hw_type == BWD_HW)
  394. writeq((u64) 1 << db, ndev->reg_ofs.rdb);
  395. else
  396. writew(((1 << ndev->bits_per_vector) - 1) <<
  397. (db * ndev->bits_per_vector), ndev->reg_ofs.rdb);
  398. }
  399. static void bwd_recover_link(struct ntb_device *ndev)
  400. {
  401. u32 status;
  402. /* Driver resets the NTB ModPhy lanes - magic! */
  403. writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
  404. writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
  405. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
  406. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
  407. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  408. msleep(100);
  409. /* Clear AER Errors, write to clear */
  410. status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  411. dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
  412. status &= PCI_ERR_COR_REP_ROLL;
  413. writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  414. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  415. status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  416. dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
  417. status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
  418. writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  419. /* Clear DeSkew Buffer error, write to clear */
  420. status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  421. dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
  422. status |= BWD_DESKEWSTS_DBERR;
  423. writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  424. status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  425. dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
  426. status &= BWD_IBIST_ERR_OFLOW;
  427. writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  428. /* Releases the NTB state machine to allow the link to retrain */
  429. status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  430. dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
  431. status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
  432. writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  433. }
  434. static void ntb_link_event(struct ntb_device *ndev, int link_state)
  435. {
  436. unsigned int event;
  437. if (ndev->link_status == link_state)
  438. return;
  439. if (link_state == NTB_LINK_UP) {
  440. u16 status;
  441. dev_info(&ndev->pdev->dev, "Link Up\n");
  442. ndev->link_status = NTB_LINK_UP;
  443. event = NTB_EVENT_HW_LINK_UP;
  444. if (ndev->hw_type == BWD_HW ||
  445. ndev->conn_type == NTB_CONN_TRANSPARENT)
  446. status = readw(ndev->reg_ofs.lnk_stat);
  447. else {
  448. int rc = pci_read_config_word(ndev->pdev,
  449. SNB_LINK_STATUS_OFFSET,
  450. &status);
  451. if (rc)
  452. return;
  453. }
  454. ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
  455. ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
  456. dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
  457. ndev->link_width, ndev->link_speed);
  458. } else {
  459. dev_info(&ndev->pdev->dev, "Link Down\n");
  460. ndev->link_status = NTB_LINK_DOWN;
  461. event = NTB_EVENT_HW_LINK_DOWN;
  462. /* Don't modify link width/speed, we need it in link recovery */
  463. }
  464. /* notify the upper layer if we have an event change */
  465. if (ndev->event_cb)
  466. ndev->event_cb(ndev->ntb_transport, event);
  467. }
  468. static int ntb_link_status(struct ntb_device *ndev)
  469. {
  470. int link_state;
  471. if (ndev->hw_type == BWD_HW) {
  472. u32 ntb_cntl;
  473. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  474. if (ntb_cntl & BWD_CNTL_LINK_DOWN)
  475. link_state = NTB_LINK_DOWN;
  476. else
  477. link_state = NTB_LINK_UP;
  478. } else {
  479. u16 status;
  480. int rc;
  481. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  482. &status);
  483. if (rc)
  484. return rc;
  485. if (status & NTB_LINK_STATUS_ACTIVE)
  486. link_state = NTB_LINK_UP;
  487. else
  488. link_state = NTB_LINK_DOWN;
  489. }
  490. ntb_link_event(ndev, link_state);
  491. return 0;
  492. }
  493. static void bwd_link_recovery(struct work_struct *work)
  494. {
  495. struct ntb_device *ndev = container_of(work, struct ntb_device,
  496. lr_timer.work);
  497. u32 status32;
  498. bwd_recover_link(ndev);
  499. /* There is a potential race between the 2 NTB devices recovering at the
  500. * same time. If the times are the same, the link will not recover and
  501. * the driver will be stuck in this loop forever. Add a random interval
  502. * to the recovery time to prevent this race.
  503. */
  504. msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
  505. status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  506. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
  507. goto retry;
  508. status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  509. if (status32 & BWD_IBIST_ERR_OFLOW)
  510. goto retry;
  511. status32 = readl(ndev->reg_ofs.lnk_cntl);
  512. if (!(status32 & BWD_CNTL_LINK_DOWN)) {
  513. unsigned char speed, width;
  514. u16 status16;
  515. status16 = readw(ndev->reg_ofs.lnk_stat);
  516. width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
  517. speed = (status16 & NTB_LINK_SPEED_MASK);
  518. if (ndev->link_width != width || ndev->link_speed != speed)
  519. goto retry;
  520. }
  521. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  522. return;
  523. retry:
  524. schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
  525. }
  526. /* BWD doesn't have link status interrupt, poll on that platform */
  527. static void bwd_link_poll(struct work_struct *work)
  528. {
  529. struct ntb_device *ndev = container_of(work, struct ntb_device,
  530. hb_timer.work);
  531. unsigned long ts = jiffies;
  532. /* If we haven't gotten an interrupt in a while, check the BWD link
  533. * status bit
  534. */
  535. if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
  536. int rc = ntb_link_status(ndev);
  537. if (rc)
  538. dev_err(&ndev->pdev->dev,
  539. "Error determining link status\n");
  540. /* Check to see if a link error is the cause of the link down */
  541. if (ndev->link_status == NTB_LINK_DOWN) {
  542. u32 status32 = readl(ndev->reg_base +
  543. BWD_LTSSMSTATEJMP_OFFSET);
  544. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
  545. schedule_delayed_work(&ndev->lr_timer, 0);
  546. return;
  547. }
  548. }
  549. }
  550. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  551. }
  552. static int ntb_xeon_setup(struct ntb_device *ndev)
  553. {
  554. int rc;
  555. u8 val;
  556. ndev->hw_type = SNB_HW;
  557. rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
  558. if (rc)
  559. return rc;
  560. if (val & SNB_PPD_DEV_TYPE)
  561. ndev->dev_type = NTB_DEV_USD;
  562. else
  563. ndev->dev_type = NTB_DEV_DSD;
  564. switch (val & SNB_PPD_CONN_TYPE) {
  565. case NTB_CONN_B2B:
  566. dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
  567. ndev->conn_type = NTB_CONN_B2B;
  568. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  569. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  570. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  571. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  572. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  573. ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
  574. /* There is a Xeon hardware errata related to writes to
  575. * SDOORBELL or B2BDOORBELL in conjunction with inbound access
  576. * to NTB MMIO Space, which may hang the system. To workaround
  577. * this use the second memory window to access the interrupt and
  578. * scratch pad registers on the remote system.
  579. */
  580. if (xeon_errata_workaround) {
  581. if (!ndev->mw[1].bar_sz)
  582. return -EINVAL;
  583. ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
  584. ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
  585. SNB_SPAD_OFFSET;
  586. ndev->reg_ofs.rdb = ndev->mw[1].vbase +
  587. SNB_PDOORBELL_OFFSET;
  588. /* Set the Limit register to 4k, the minimum size, to
  589. * prevent an illegal access
  590. */
  591. writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
  592. SNB_PBAR4LMT_OFFSET);
  593. } else {
  594. ndev->limits.max_mw = SNB_MAX_MW;
  595. ndev->reg_ofs.spad_write = ndev->reg_base +
  596. SNB_B2B_SPAD_OFFSET;
  597. ndev->reg_ofs.rdb = ndev->reg_base +
  598. SNB_B2B_DOORBELL_OFFSET;
  599. /* Disable the Limit register, just incase it is set to
  600. * something silly
  601. */
  602. writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
  603. }
  604. /* The Xeon errata workaround requires setting SBAR Base
  605. * addresses to known values, so that the PBAR XLAT can be
  606. * pointed at SBAR0 of the remote system.
  607. */
  608. if (ndev->dev_type == NTB_DEV_USD) {
  609. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  610. SNB_PBAR2XLAT_OFFSET);
  611. if (xeon_errata_workaround)
  612. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  613. SNB_PBAR4XLAT_OFFSET);
  614. else {
  615. writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
  616. SNB_PBAR4XLAT_OFFSET);
  617. /* B2B_XLAT_OFFSET is a 64bit register, but can
  618. * only take 32bit writes
  619. */
  620. writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
  621. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  622. writel(SNB_MBAR01_DSD_ADDR >> 32,
  623. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  624. }
  625. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  626. SNB_SBAR0BASE_OFFSET);
  627. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  628. SNB_SBAR2BASE_OFFSET);
  629. writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
  630. SNB_SBAR4BASE_OFFSET);
  631. } else {
  632. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  633. SNB_PBAR2XLAT_OFFSET);
  634. if (xeon_errata_workaround)
  635. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  636. SNB_PBAR4XLAT_OFFSET);
  637. else {
  638. writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
  639. SNB_PBAR4XLAT_OFFSET);
  640. /* B2B_XLAT_OFFSET is a 64bit register, but can
  641. * only take 32bit writes
  642. */
  643. writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
  644. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  645. writel(SNB_MBAR01_USD_ADDR >> 32,
  646. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  647. }
  648. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  649. SNB_SBAR0BASE_OFFSET);
  650. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  651. SNB_SBAR2BASE_OFFSET);
  652. writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
  653. SNB_SBAR4BASE_OFFSET);
  654. }
  655. break;
  656. case NTB_CONN_RP:
  657. dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
  658. ndev->conn_type = NTB_CONN_RP;
  659. if (xeon_errata_workaround) {
  660. dev_err(&ndev->pdev->dev,
  661. "NTB-RP disabled due to hardware errata. To disregard this warning and potentially lock-up the system, add the parameter 'xeon_errata_workaround=0'.\n");
  662. return -EINVAL;
  663. }
  664. /* Scratch pads need to have exclusive access from the primary
  665. * or secondary side. Halve the num spads so that each side can
  666. * have an equal amount.
  667. */
  668. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  669. /* Note: The SDOORBELL is the cause of the errata. You REALLY
  670. * don't want to touch it.
  671. */
  672. ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  673. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  674. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  675. /* Offset the start of the spads to correspond to whether it is
  676. * primary or secondary
  677. */
  678. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
  679. ndev->limits.max_spads * 4;
  680. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  681. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  682. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  683. ndev->limits.max_mw = SNB_MAX_MW;
  684. break;
  685. case NTB_CONN_TRANSPARENT:
  686. dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
  687. ndev->conn_type = NTB_CONN_TRANSPARENT;
  688. /* Scratch pads need to have exclusive access from the primary
  689. * or secondary side. Halve the num spads so that each side can
  690. * have an equal amount.
  691. */
  692. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  693. ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  694. ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  695. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
  696. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
  697. /* Offset the start of the spads to correspond to whether it is
  698. * primary or secondary
  699. */
  700. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
  701. ndev->limits.max_spads * 4;
  702. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
  703. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
  704. ndev->limits.max_mw = SNB_MAX_MW;
  705. break;
  706. default:
  707. /* Most likely caused by the remote NTB-RP device not being
  708. * configured
  709. */
  710. dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", val);
  711. return -EINVAL;
  712. }
  713. ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
  714. ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
  715. ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
  716. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  717. ndev->limits.msix_cnt = SNB_MSIX_CNT;
  718. ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
  719. return 0;
  720. }
  721. static int ntb_bwd_setup(struct ntb_device *ndev)
  722. {
  723. int rc;
  724. u32 val;
  725. ndev->hw_type = BWD_HW;
  726. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
  727. if (rc)
  728. return rc;
  729. switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
  730. case NTB_CONN_B2B:
  731. ndev->conn_type = NTB_CONN_B2B;
  732. break;
  733. case NTB_CONN_RP:
  734. default:
  735. dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
  736. return -EINVAL;
  737. }
  738. if (val & BWD_PPD_DEV_TYPE)
  739. ndev->dev_type = NTB_DEV_DSD;
  740. else
  741. ndev->dev_type = NTB_DEV_USD;
  742. /* Initiate PCI-E link training */
  743. rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
  744. val | BWD_PPD_INIT_LINK);
  745. if (rc)
  746. return rc;
  747. ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  748. ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
  749. ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  750. ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  751. ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  752. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  753. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  754. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  755. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  756. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  757. ndev->limits.max_mw = BWD_MAX_MW;
  758. ndev->limits.max_spads = BWD_MAX_SPADS;
  759. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  760. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  761. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  762. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  763. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  764. INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
  765. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  766. return 0;
  767. }
  768. static int ntb_device_setup(struct ntb_device *ndev)
  769. {
  770. int rc;
  771. switch (ndev->pdev->device) {
  772. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  773. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  774. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  775. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  776. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  777. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  778. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  779. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  780. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  781. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  782. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  783. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  784. rc = ntb_xeon_setup(ndev);
  785. break;
  786. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  787. rc = ntb_bwd_setup(ndev);
  788. break;
  789. default:
  790. rc = -ENODEV;
  791. }
  792. if (rc)
  793. return rc;
  794. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  795. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  796. if (ndev->conn_type == NTB_CONN_B2B)
  797. /* Enable Bus Master and Memory Space on the secondary side */
  798. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  799. ndev->reg_ofs.spci_cmd);
  800. return 0;
  801. }
  802. static void ntb_device_free(struct ntb_device *ndev)
  803. {
  804. if (ndev->hw_type == BWD_HW) {
  805. cancel_delayed_work_sync(&ndev->hb_timer);
  806. cancel_delayed_work_sync(&ndev->lr_timer);
  807. }
  808. }
  809. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  810. {
  811. struct ntb_db_cb *db_cb = data;
  812. struct ntb_device *ndev = db_cb->ndev;
  813. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  814. db_cb->db_num);
  815. if (db_cb->callback)
  816. db_cb->callback(db_cb->data, db_cb->db_num);
  817. /* No need to check for the specific HB irq, any interrupt means
  818. * we're connected.
  819. */
  820. ndev->last_ts = jiffies;
  821. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
  822. return IRQ_HANDLED;
  823. }
  824. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  825. {
  826. struct ntb_db_cb *db_cb = data;
  827. struct ntb_device *ndev = db_cb->ndev;
  828. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  829. db_cb->db_num);
  830. if (db_cb->callback)
  831. db_cb->callback(db_cb->data, db_cb->db_num);
  832. /* On Sandybridge, there are 16 bits in the interrupt register
  833. * but only 4 vectors. So, 5 bits are assigned to the first 3
  834. * vectors, with the 4th having a single bit for link
  835. * interrupts.
  836. */
  837. writew(((1 << ndev->bits_per_vector) - 1) <<
  838. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
  839. return IRQ_HANDLED;
  840. }
  841. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  842. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  843. {
  844. struct ntb_device *ndev = dev;
  845. int rc;
  846. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  847. rc = ntb_link_status(ndev);
  848. if (rc)
  849. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  850. /* bit 15 is always the link bit */
  851. writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.ldb);
  852. return IRQ_HANDLED;
  853. }
  854. static irqreturn_t ntb_interrupt(int irq, void *dev)
  855. {
  856. struct ntb_device *ndev = dev;
  857. unsigned int i = 0;
  858. if (ndev->hw_type == BWD_HW) {
  859. u64 ldb = readq(ndev->reg_ofs.ldb);
  860. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
  861. while (ldb) {
  862. i = __ffs(ldb);
  863. ldb &= ldb - 1;
  864. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  865. }
  866. } else {
  867. u16 ldb = readw(ndev->reg_ofs.ldb);
  868. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
  869. if (ldb & SNB_DB_HW_LINK) {
  870. xeon_event_msix_irq(irq, dev);
  871. ldb &= ~SNB_DB_HW_LINK;
  872. }
  873. while (ldb) {
  874. i = __ffs(ldb);
  875. ldb &= ldb - 1;
  876. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  877. }
  878. }
  879. return IRQ_HANDLED;
  880. }
  881. static int ntb_setup_msix(struct ntb_device *ndev)
  882. {
  883. struct pci_dev *pdev = ndev->pdev;
  884. struct msix_entry *msix;
  885. int msix_entries;
  886. int rc, i, pos;
  887. u16 val;
  888. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  889. if (!pos) {
  890. rc = -EIO;
  891. goto err;
  892. }
  893. rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
  894. if (rc)
  895. goto err;
  896. msix_entries = msix_table_size(val);
  897. if (msix_entries > ndev->limits.msix_cnt) {
  898. rc = -EINVAL;
  899. goto err;
  900. }
  901. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  902. GFP_KERNEL);
  903. if (!ndev->msix_entries) {
  904. rc = -ENOMEM;
  905. goto err;
  906. }
  907. for (i = 0; i < msix_entries; i++)
  908. ndev->msix_entries[i].entry = i;
  909. rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
  910. if (rc < 0)
  911. goto err1;
  912. if (rc > 0) {
  913. /* On SNB, the link interrupt is always tied to 4th vector. If
  914. * we can't get all 4, then we can't use MSI-X.
  915. */
  916. if (ndev->hw_type != BWD_HW) {
  917. rc = -EIO;
  918. goto err1;
  919. }
  920. dev_warn(&pdev->dev,
  921. "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
  922. rc);
  923. msix_entries = rc;
  924. }
  925. for (i = 0; i < msix_entries; i++) {
  926. msix = &ndev->msix_entries[i];
  927. WARN_ON(!msix->vector);
  928. /* Use the last MSI-X vector for Link status */
  929. if (ndev->hw_type == BWD_HW) {
  930. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  931. "ntb-callback-msix", &ndev->db_cb[i]);
  932. if (rc)
  933. goto err2;
  934. } else {
  935. if (i == msix_entries - 1) {
  936. rc = request_irq(msix->vector,
  937. xeon_event_msix_irq, 0,
  938. "ntb-event-msix", ndev);
  939. if (rc)
  940. goto err2;
  941. } else {
  942. rc = request_irq(msix->vector,
  943. xeon_callback_msix_irq, 0,
  944. "ntb-callback-msix",
  945. &ndev->db_cb[i]);
  946. if (rc)
  947. goto err2;
  948. }
  949. }
  950. }
  951. ndev->num_msix = msix_entries;
  952. if (ndev->hw_type == BWD_HW)
  953. ndev->max_cbs = msix_entries;
  954. else
  955. ndev->max_cbs = msix_entries - 1;
  956. return 0;
  957. err2:
  958. while (--i >= 0) {
  959. msix = &ndev->msix_entries[i];
  960. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  961. free_irq(msix->vector, ndev);
  962. else
  963. free_irq(msix->vector, &ndev->db_cb[i]);
  964. }
  965. pci_disable_msix(pdev);
  966. err1:
  967. kfree(ndev->msix_entries);
  968. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  969. err:
  970. ndev->num_msix = 0;
  971. return rc;
  972. }
  973. static int ntb_setup_msi(struct ntb_device *ndev)
  974. {
  975. struct pci_dev *pdev = ndev->pdev;
  976. int rc;
  977. rc = pci_enable_msi(pdev);
  978. if (rc)
  979. return rc;
  980. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  981. if (rc) {
  982. pci_disable_msi(pdev);
  983. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  984. return rc;
  985. }
  986. return 0;
  987. }
  988. static int ntb_setup_intx(struct ntb_device *ndev)
  989. {
  990. struct pci_dev *pdev = ndev->pdev;
  991. int rc;
  992. pci_msi_off(pdev);
  993. /* Verify intx is enabled */
  994. pci_intx(pdev, 1);
  995. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  996. ndev);
  997. if (rc)
  998. return rc;
  999. return 0;
  1000. }
  1001. static int ntb_setup_interrupts(struct ntb_device *ndev)
  1002. {
  1003. int rc;
  1004. /* On BWD, disable all interrupts. On SNB, disable all but Link
  1005. * Interrupt. The rest will be unmasked as callbacks are registered.
  1006. */
  1007. if (ndev->hw_type == BWD_HW)
  1008. writeq(~0, ndev->reg_ofs.ldb_mask);
  1009. else
  1010. writew(~(1 << ndev->limits.max_db_bits),
  1011. ndev->reg_ofs.ldb_mask);
  1012. rc = ntb_setup_msix(ndev);
  1013. if (!rc)
  1014. goto done;
  1015. ndev->bits_per_vector = 1;
  1016. ndev->max_cbs = ndev->limits.max_db_bits;
  1017. rc = ntb_setup_msi(ndev);
  1018. if (!rc)
  1019. goto done;
  1020. rc = ntb_setup_intx(ndev);
  1021. if (rc) {
  1022. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  1023. return rc;
  1024. }
  1025. done:
  1026. return 0;
  1027. }
  1028. static void ntb_free_interrupts(struct ntb_device *ndev)
  1029. {
  1030. struct pci_dev *pdev = ndev->pdev;
  1031. /* mask interrupts */
  1032. if (ndev->hw_type == BWD_HW)
  1033. writeq(~0, ndev->reg_ofs.ldb_mask);
  1034. else
  1035. writew(~0, ndev->reg_ofs.ldb_mask);
  1036. if (ndev->num_msix) {
  1037. struct msix_entry *msix;
  1038. u32 i;
  1039. for (i = 0; i < ndev->num_msix; i++) {
  1040. msix = &ndev->msix_entries[i];
  1041. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  1042. free_irq(msix->vector, ndev);
  1043. else
  1044. free_irq(msix->vector, &ndev->db_cb[i]);
  1045. }
  1046. pci_disable_msix(pdev);
  1047. } else {
  1048. free_irq(pdev->irq, ndev);
  1049. if (pci_dev_msi_enabled(pdev))
  1050. pci_disable_msi(pdev);
  1051. }
  1052. }
  1053. static int ntb_create_callbacks(struct ntb_device *ndev)
  1054. {
  1055. int i;
  1056. /* Checken-egg issue. We won't know how many callbacks are necessary
  1057. * until we see how many MSI-X vectors we get, but these pointers need
  1058. * to be passed into the MSI-X register fucntion. So, we allocate the
  1059. * max, knowing that they might not all be used, to work around this.
  1060. */
  1061. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  1062. sizeof(struct ntb_db_cb),
  1063. GFP_KERNEL);
  1064. if (!ndev->db_cb)
  1065. return -ENOMEM;
  1066. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  1067. ndev->db_cb[i].db_num = i;
  1068. ndev->db_cb[i].ndev = ndev;
  1069. }
  1070. return 0;
  1071. }
  1072. static void ntb_free_callbacks(struct ntb_device *ndev)
  1073. {
  1074. int i;
  1075. for (i = 0; i < ndev->limits.max_db_bits; i++)
  1076. ntb_unregister_db_callback(ndev, i);
  1077. kfree(ndev->db_cb);
  1078. }
  1079. static void ntb_setup_debugfs(struct ntb_device *ndev)
  1080. {
  1081. if (!debugfs_initialized())
  1082. return;
  1083. if (!debugfs_dir)
  1084. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1085. ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
  1086. debugfs_dir);
  1087. }
  1088. static void ntb_free_debugfs(struct ntb_device *ndev)
  1089. {
  1090. debugfs_remove_recursive(ndev->debugfs_dir);
  1091. if (debugfs_dir && simple_empty(debugfs_dir)) {
  1092. debugfs_remove_recursive(debugfs_dir);
  1093. debugfs_dir = NULL;
  1094. }
  1095. }
  1096. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1097. {
  1098. struct ntb_device *ndev;
  1099. int rc, i;
  1100. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  1101. if (!ndev)
  1102. return -ENOMEM;
  1103. ndev->pdev = pdev;
  1104. ndev->link_status = NTB_LINK_DOWN;
  1105. pci_set_drvdata(pdev, ndev);
  1106. ntb_setup_debugfs(ndev);
  1107. rc = pci_enable_device(pdev);
  1108. if (rc)
  1109. goto err;
  1110. pci_set_master(ndev->pdev);
  1111. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
  1112. if (rc)
  1113. goto err1;
  1114. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  1115. if (!ndev->reg_base) {
  1116. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  1117. rc = -EIO;
  1118. goto err2;
  1119. }
  1120. for (i = 0; i < NTB_MAX_NUM_MW; i++) {
  1121. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  1122. ndev->mw[i].vbase =
  1123. ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
  1124. ndev->mw[i].bar_sz);
  1125. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  1126. (unsigned long long) ndev->mw[i].bar_sz);
  1127. if (!ndev->mw[i].vbase) {
  1128. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  1129. MW_TO_BAR(i));
  1130. rc = -EIO;
  1131. goto err3;
  1132. }
  1133. }
  1134. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1135. if (rc) {
  1136. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1137. if (rc)
  1138. goto err3;
  1139. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  1140. }
  1141. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1142. if (rc) {
  1143. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1144. if (rc)
  1145. goto err3;
  1146. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  1147. }
  1148. rc = ntb_device_setup(ndev);
  1149. if (rc)
  1150. goto err3;
  1151. rc = ntb_create_callbacks(ndev);
  1152. if (rc)
  1153. goto err4;
  1154. rc = ntb_setup_interrupts(ndev);
  1155. if (rc)
  1156. goto err5;
  1157. /* The scratchpad registers keep the values between rmmod/insmod,
  1158. * blast them now
  1159. */
  1160. for (i = 0; i < ndev->limits.max_spads; i++) {
  1161. ntb_write_local_spad(ndev, i, 0);
  1162. ntb_write_remote_spad(ndev, i, 0);
  1163. }
  1164. rc = ntb_transport_init(pdev);
  1165. if (rc)
  1166. goto err6;
  1167. /* Let's bring the NTB link up */
  1168. writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
  1169. ndev->reg_ofs.lnk_cntl);
  1170. return 0;
  1171. err6:
  1172. ntb_free_interrupts(ndev);
  1173. err5:
  1174. ntb_free_callbacks(ndev);
  1175. err4:
  1176. ntb_device_free(ndev);
  1177. err3:
  1178. for (i--; i >= 0; i--)
  1179. iounmap(ndev->mw[i].vbase);
  1180. iounmap(ndev->reg_base);
  1181. err2:
  1182. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1183. err1:
  1184. pci_disable_device(pdev);
  1185. err:
  1186. ntb_free_debugfs(ndev);
  1187. kfree(ndev);
  1188. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  1189. return rc;
  1190. }
  1191. static void ntb_pci_remove(struct pci_dev *pdev)
  1192. {
  1193. struct ntb_device *ndev = pci_get_drvdata(pdev);
  1194. int i;
  1195. u32 ntb_cntl;
  1196. /* Bring NTB link down */
  1197. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1198. ntb_cntl |= NTB_CNTL_LINK_DISABLE;
  1199. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1200. ntb_transport_free(ndev->ntb_transport);
  1201. ntb_free_interrupts(ndev);
  1202. ntb_free_callbacks(ndev);
  1203. ntb_device_free(ndev);
  1204. for (i = 0; i < NTB_MAX_NUM_MW; i++)
  1205. iounmap(ndev->mw[i].vbase);
  1206. iounmap(ndev->reg_base);
  1207. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1208. pci_disable_device(pdev);
  1209. ntb_free_debugfs(ndev);
  1210. kfree(ndev);
  1211. }
  1212. static struct pci_driver ntb_pci_driver = {
  1213. .name = KBUILD_MODNAME,
  1214. .id_table = ntb_pci_tbl,
  1215. .probe = ntb_pci_probe,
  1216. .remove = ntb_pci_remove,
  1217. };
  1218. module_pci_driver(ntb_pci_driver);