ntb_hw.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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_CLASSIC = 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.pdb_mask);
  153. clear_bit(idx * ndev->bits_per_vector, &mask);
  154. writew(mask, ndev->reg_ofs.pdb_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.pdb_mask);
  171. set_bit(idx * ndev->bits_per_vector, &mask);
  172. writew(mask, ndev->reg_ofs.pdb_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_vbase() - get virtual 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 virtual address of the memory window
  314. * specified.
  315. *
  316. * RETURNS: pointer to virtual address, or NULL on error.
  317. */
  318. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
  319. {
  320. if (mw >= ntb_max_mw(ndev))
  321. return NULL;
  322. return ndev->mw[mw].vbase;
  323. }
  324. /**
  325. * ntb_get_mw_size() - return size of NTB memory window
  326. * @ndev: pointer to ntb_device instance
  327. * @mw: memory window number
  328. *
  329. * This function provides the physical size of the memory window specified
  330. *
  331. * RETURNS: the size of the memory window or zero on error
  332. */
  333. u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
  334. {
  335. if (mw >= ntb_max_mw(ndev))
  336. return 0;
  337. return ndev->mw[mw].bar_sz;
  338. }
  339. /**
  340. * ntb_set_mw_addr - set the memory window address
  341. * @ndev: pointer to ntb_device instance
  342. * @mw: memory window number
  343. * @addr: base address for data
  344. *
  345. * This function sets the base physical address of the memory window. This
  346. * memory address is where data from the remote system will be transfered into
  347. * or out of depending on how the transport is configured.
  348. */
  349. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
  350. {
  351. if (mw >= ntb_max_mw(ndev))
  352. return;
  353. dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
  354. MW_TO_BAR(mw));
  355. ndev->mw[mw].phys_addr = addr;
  356. switch (MW_TO_BAR(mw)) {
  357. case NTB_BAR_23:
  358. writeq(addr, ndev->reg_ofs.sbar2_xlat);
  359. break;
  360. case NTB_BAR_45:
  361. writeq(addr, ndev->reg_ofs.sbar4_xlat);
  362. break;
  363. }
  364. }
  365. /**
  366. * ntb_ring_sdb() - Set the doorbell on the secondary/external side
  367. * @ndev: pointer to ntb_device instance
  368. * @db: doorbell to ring
  369. *
  370. * This function allows triggering of a doorbell on the secondary/external
  371. * side that will initiate an interrupt on the remote host
  372. *
  373. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  374. */
  375. void ntb_ring_sdb(struct ntb_device *ndev, unsigned int db)
  376. {
  377. dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
  378. if (ndev->hw_type == BWD_HW)
  379. writeq((u64) 1 << db, ndev->reg_ofs.sdb);
  380. else
  381. writew(((1 << ndev->bits_per_vector) - 1) <<
  382. (db * ndev->bits_per_vector), ndev->reg_ofs.sdb);
  383. }
  384. static void bwd_recover_link(struct ntb_device *ndev)
  385. {
  386. u32 status;
  387. /* Driver resets the NTB ModPhy lanes - magic! */
  388. writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
  389. writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
  390. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
  391. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
  392. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  393. msleep(100);
  394. /* Clear AER Errors, write to clear */
  395. status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  396. dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
  397. status &= PCI_ERR_COR_REP_ROLL;
  398. writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  399. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  400. status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  401. dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
  402. status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
  403. writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  404. /* Clear DeSkew Buffer error, write to clear */
  405. status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  406. dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
  407. status |= BWD_DESKEWSTS_DBERR;
  408. writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  409. status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  410. dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
  411. status &= BWD_IBIST_ERR_OFLOW;
  412. writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  413. /* Releases the NTB state machine to allow the link to retrain */
  414. status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  415. dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
  416. status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
  417. writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  418. }
  419. static void ntb_link_event(struct ntb_device *ndev, int link_state)
  420. {
  421. unsigned int event;
  422. if (ndev->link_status == link_state)
  423. return;
  424. if (link_state == NTB_LINK_UP) {
  425. u16 status;
  426. dev_info(&ndev->pdev->dev, "Link Up\n");
  427. ndev->link_status = NTB_LINK_UP;
  428. event = NTB_EVENT_HW_LINK_UP;
  429. if (ndev->hw_type == BWD_HW)
  430. status = readw(ndev->reg_ofs.lnk_stat);
  431. else {
  432. int rc = pci_read_config_word(ndev->pdev,
  433. SNB_LINK_STATUS_OFFSET,
  434. &status);
  435. if (rc)
  436. return;
  437. }
  438. ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
  439. ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
  440. dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
  441. ndev->link_width, ndev->link_speed);
  442. } else {
  443. dev_info(&ndev->pdev->dev, "Link Down\n");
  444. ndev->link_status = NTB_LINK_DOWN;
  445. event = NTB_EVENT_HW_LINK_DOWN;
  446. /* Don't modify link width/speed, we need it in link recovery */
  447. }
  448. /* notify the upper layer if we have an event change */
  449. if (ndev->event_cb)
  450. ndev->event_cb(ndev->ntb_transport, event);
  451. }
  452. static int ntb_link_status(struct ntb_device *ndev)
  453. {
  454. int link_state;
  455. if (ndev->hw_type == BWD_HW) {
  456. u32 ntb_cntl;
  457. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  458. if (ntb_cntl & BWD_CNTL_LINK_DOWN)
  459. link_state = NTB_LINK_DOWN;
  460. else
  461. link_state = NTB_LINK_UP;
  462. } else {
  463. u16 status;
  464. int rc;
  465. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  466. &status);
  467. if (rc)
  468. return rc;
  469. if (status & NTB_LINK_STATUS_ACTIVE)
  470. link_state = NTB_LINK_UP;
  471. else
  472. link_state = NTB_LINK_DOWN;
  473. }
  474. ntb_link_event(ndev, link_state);
  475. return 0;
  476. }
  477. static void bwd_link_recovery(struct work_struct *work)
  478. {
  479. struct ntb_device *ndev = container_of(work, struct ntb_device,
  480. lr_timer.work);
  481. u32 status32;
  482. bwd_recover_link(ndev);
  483. /* There is a potential race between the 2 NTB devices recovering at the
  484. * same time. If the times are the same, the link will not recover and
  485. * the driver will be stuck in this loop forever. Add a random interval
  486. * to the recovery time to prevent this race.
  487. */
  488. msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
  489. status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  490. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
  491. goto retry;
  492. status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  493. if (status32 & BWD_IBIST_ERR_OFLOW)
  494. goto retry;
  495. status32 = readl(ndev->reg_ofs.lnk_cntl);
  496. if (!(status32 & BWD_CNTL_LINK_DOWN)) {
  497. unsigned char speed, width;
  498. u16 status16;
  499. status16 = readw(ndev->reg_ofs.lnk_stat);
  500. width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
  501. speed = (status16 & NTB_LINK_SPEED_MASK);
  502. if (ndev->link_width != width || ndev->link_speed != speed)
  503. goto retry;
  504. }
  505. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  506. return;
  507. retry:
  508. schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
  509. }
  510. /* BWD doesn't have link status interrupt, poll on that platform */
  511. static void bwd_link_poll(struct work_struct *work)
  512. {
  513. struct ntb_device *ndev = container_of(work, struct ntb_device,
  514. hb_timer.work);
  515. unsigned long ts = jiffies;
  516. /* If we haven't gotten an interrupt in a while, check the BWD link
  517. * status bit
  518. */
  519. if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
  520. int rc = ntb_link_status(ndev);
  521. if (rc)
  522. dev_err(&ndev->pdev->dev,
  523. "Error determining link status\n");
  524. /* Check to see if a link error is the cause of the link down */
  525. if (ndev->link_status == NTB_LINK_DOWN) {
  526. u32 status32 = readl(ndev->reg_base +
  527. BWD_LTSSMSTATEJMP_OFFSET);
  528. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
  529. schedule_delayed_work(&ndev->lr_timer, 0);
  530. return;
  531. }
  532. }
  533. }
  534. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  535. }
  536. static int ntb_xeon_setup(struct ntb_device *ndev)
  537. {
  538. int rc;
  539. u8 val;
  540. ndev->hw_type = SNB_HW;
  541. rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
  542. if (rc)
  543. return rc;
  544. switch (val & SNB_PPD_CONN_TYPE) {
  545. case NTB_CONN_B2B:
  546. ndev->conn_type = NTB_CONN_B2B;
  547. break;
  548. case NTB_CONN_CLASSIC:
  549. case NTB_CONN_RP:
  550. default:
  551. dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
  552. return -EINVAL;
  553. }
  554. if (val & SNB_PPD_DEV_TYPE)
  555. ndev->dev_type = NTB_DEV_USD;
  556. else
  557. ndev->dev_type = NTB_DEV_DSD;
  558. ndev->reg_ofs.pdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  559. ndev->reg_ofs.pdb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  560. ndev->reg_ofs.sbar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  561. ndev->reg_ofs.sbar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  562. ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
  563. ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_LINK_STATUS_OFFSET;
  564. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  565. ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
  566. /* There is a Xeon hardware errata related to writes to
  567. * SDOORBELL or B2BDOORBELL in conjunction with inbound access
  568. * to NTB MMIO Space, which may hang the system. To workaround
  569. * this use the second memory window to access the interrupt and
  570. * scratch pad registers on the remote system.
  571. */
  572. if (xeon_errata_workaround) {
  573. if (!ndev->mw[1].bar_sz)
  574. return -EINVAL;
  575. ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
  576. ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
  577. SNB_SPAD_OFFSET;
  578. ndev->reg_ofs.sdb = ndev->mw[1].vbase +
  579. SNB_PDOORBELL_OFFSET;
  580. /* Set the Limit register to 4k, the minimum size, to
  581. * prevent an illegal access
  582. */
  583. writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
  584. SNB_PBAR4LMT_OFFSET);
  585. } else {
  586. ndev->limits.max_mw = SNB_MAX_MW;
  587. ndev->reg_ofs.spad_write = ndev->reg_base +
  588. SNB_B2B_SPAD_OFFSET;
  589. ndev->reg_ofs.sdb = ndev->reg_base +
  590. SNB_B2B_DOORBELL_OFFSET;
  591. /* Disable the Limit register, just incase it is set to
  592. * something silly
  593. */
  594. writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
  595. }
  596. /* The Xeon errata workaround requires setting SBAR Base
  597. * addresses to known values, so that the PBAR XLAT can be
  598. * pointed at SBAR0 of the remote system.
  599. */
  600. if (ndev->dev_type == NTB_DEV_USD) {
  601. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  602. SNB_PBAR2XLAT_OFFSET);
  603. if (xeon_errata_workaround)
  604. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  605. SNB_PBAR4XLAT_OFFSET);
  606. else {
  607. writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
  608. SNB_PBAR4XLAT_OFFSET);
  609. /* B2B_XLAT_OFFSET is a 64bit register, but can
  610. * only take 32bit writes
  611. */
  612. writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
  613. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  614. writel(SNB_MBAR01_DSD_ADDR >> 32,
  615. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  616. }
  617. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  618. SNB_SBAR0BASE_OFFSET);
  619. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  620. SNB_SBAR2BASE_OFFSET);
  621. writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
  622. SNB_SBAR4BASE_OFFSET);
  623. } else {
  624. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  625. SNB_PBAR2XLAT_OFFSET);
  626. if (xeon_errata_workaround)
  627. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  628. SNB_PBAR4XLAT_OFFSET);
  629. else {
  630. writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
  631. SNB_PBAR4XLAT_OFFSET);
  632. /* B2B_XLAT_OFFSET is a 64bit register, but can
  633. * only take 32bit writes
  634. */
  635. writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
  636. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  637. writel(SNB_MBAR01_USD_ADDR >> 32,
  638. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  639. }
  640. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  641. SNB_SBAR0BASE_OFFSET);
  642. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  643. SNB_SBAR2BASE_OFFSET);
  644. writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
  645. SNB_SBAR4BASE_OFFSET);
  646. }
  647. ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
  648. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  649. ndev->limits.msix_cnt = SNB_MSIX_CNT;
  650. ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
  651. return 0;
  652. }
  653. static int ntb_bwd_setup(struct ntb_device *ndev)
  654. {
  655. int rc;
  656. u32 val;
  657. ndev->hw_type = BWD_HW;
  658. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
  659. if (rc)
  660. return rc;
  661. switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
  662. case NTB_CONN_B2B:
  663. ndev->conn_type = NTB_CONN_B2B;
  664. break;
  665. case NTB_CONN_RP:
  666. default:
  667. dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
  668. return -EINVAL;
  669. }
  670. if (val & BWD_PPD_DEV_TYPE)
  671. ndev->dev_type = NTB_DEV_DSD;
  672. else
  673. ndev->dev_type = NTB_DEV_USD;
  674. /* Initiate PCI-E link training */
  675. rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
  676. val | BWD_PPD_INIT_LINK);
  677. if (rc)
  678. return rc;
  679. ndev->reg_ofs.pdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  680. ndev->reg_ofs.pdb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
  681. ndev->reg_ofs.sbar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  682. ndev->reg_ofs.sbar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  683. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  684. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  685. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  686. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  687. if (ndev->conn_type == NTB_CONN_B2B) {
  688. ndev->reg_ofs.sdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  689. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  690. ndev->limits.max_spads = BWD_MAX_SPADS;
  691. } else {
  692. ndev->reg_ofs.sdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  693. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
  694. ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
  695. }
  696. ndev->limits.max_mw = BWD_MAX_MW;
  697. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  698. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  699. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  700. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  701. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  702. INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
  703. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  704. return 0;
  705. }
  706. static int ntb_device_setup(struct ntb_device *ndev)
  707. {
  708. int rc;
  709. switch (ndev->pdev->device) {
  710. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  711. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  712. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  713. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  714. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  715. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  716. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  717. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  718. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  719. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  720. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  721. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  722. rc = ntb_xeon_setup(ndev);
  723. break;
  724. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  725. rc = ntb_bwd_setup(ndev);
  726. break;
  727. default:
  728. rc = -ENODEV;
  729. }
  730. if (rc)
  731. return rc;
  732. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  733. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  734. /* Enable Bus Master and Memory Space on the secondary side */
  735. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
  736. return 0;
  737. }
  738. static void ntb_device_free(struct ntb_device *ndev)
  739. {
  740. if (ndev->hw_type == BWD_HW) {
  741. cancel_delayed_work_sync(&ndev->hb_timer);
  742. cancel_delayed_work_sync(&ndev->lr_timer);
  743. }
  744. }
  745. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  746. {
  747. struct ntb_db_cb *db_cb = data;
  748. struct ntb_device *ndev = db_cb->ndev;
  749. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  750. db_cb->db_num);
  751. if (db_cb->callback)
  752. db_cb->callback(db_cb->data, db_cb->db_num);
  753. /* No need to check for the specific HB irq, any interrupt means
  754. * we're connected.
  755. */
  756. ndev->last_ts = jiffies;
  757. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
  758. return IRQ_HANDLED;
  759. }
  760. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  761. {
  762. struct ntb_db_cb *db_cb = data;
  763. struct ntb_device *ndev = db_cb->ndev;
  764. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  765. db_cb->db_num);
  766. if (db_cb->callback)
  767. db_cb->callback(db_cb->data, db_cb->db_num);
  768. /* On Sandybridge, there are 16 bits in the interrupt register
  769. * but only 4 vectors. So, 5 bits are assigned to the first 3
  770. * vectors, with the 4th having a single bit for link
  771. * interrupts.
  772. */
  773. writew(((1 << ndev->bits_per_vector) - 1) <<
  774. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
  775. return IRQ_HANDLED;
  776. }
  777. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  778. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  779. {
  780. struct ntb_device *ndev = dev;
  781. int rc;
  782. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  783. rc = ntb_link_status(ndev);
  784. if (rc)
  785. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  786. /* bit 15 is always the link bit */
  787. writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
  788. return IRQ_HANDLED;
  789. }
  790. static irqreturn_t ntb_interrupt(int irq, void *dev)
  791. {
  792. struct ntb_device *ndev = dev;
  793. unsigned int i = 0;
  794. if (ndev->hw_type == BWD_HW) {
  795. u64 pdb = readq(ndev->reg_ofs.pdb);
  796. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
  797. while (pdb) {
  798. i = __ffs(pdb);
  799. pdb &= pdb - 1;
  800. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  801. }
  802. } else {
  803. u16 pdb = readw(ndev->reg_ofs.pdb);
  804. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
  805. pdb, readw(ndev->reg_ofs.sdb));
  806. if (pdb & SNB_DB_HW_LINK) {
  807. xeon_event_msix_irq(irq, dev);
  808. pdb &= ~SNB_DB_HW_LINK;
  809. }
  810. while (pdb) {
  811. i = __ffs(pdb);
  812. pdb &= pdb - 1;
  813. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  814. }
  815. }
  816. return IRQ_HANDLED;
  817. }
  818. static int ntb_setup_msix(struct ntb_device *ndev)
  819. {
  820. struct pci_dev *pdev = ndev->pdev;
  821. struct msix_entry *msix;
  822. int msix_entries;
  823. int rc, i, pos;
  824. u16 val;
  825. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  826. if (!pos) {
  827. rc = -EIO;
  828. goto err;
  829. }
  830. rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
  831. if (rc)
  832. goto err;
  833. msix_entries = msix_table_size(val);
  834. if (msix_entries > ndev->limits.msix_cnt) {
  835. rc = -EINVAL;
  836. goto err;
  837. }
  838. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  839. GFP_KERNEL);
  840. if (!ndev->msix_entries) {
  841. rc = -ENOMEM;
  842. goto err;
  843. }
  844. for (i = 0; i < msix_entries; i++)
  845. ndev->msix_entries[i].entry = i;
  846. rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
  847. if (rc < 0)
  848. goto err1;
  849. if (rc > 0) {
  850. /* On SNB, the link interrupt is always tied to 4th vector. If
  851. * we can't get all 4, then we can't use MSI-X.
  852. */
  853. if (ndev->hw_type != BWD_HW) {
  854. rc = -EIO;
  855. goto err1;
  856. }
  857. dev_warn(&pdev->dev,
  858. "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
  859. rc);
  860. msix_entries = rc;
  861. }
  862. for (i = 0; i < msix_entries; i++) {
  863. msix = &ndev->msix_entries[i];
  864. WARN_ON(!msix->vector);
  865. /* Use the last MSI-X vector for Link status */
  866. if (ndev->hw_type == BWD_HW) {
  867. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  868. "ntb-callback-msix", &ndev->db_cb[i]);
  869. if (rc)
  870. goto err2;
  871. } else {
  872. if (i == msix_entries - 1) {
  873. rc = request_irq(msix->vector,
  874. xeon_event_msix_irq, 0,
  875. "ntb-event-msix", ndev);
  876. if (rc)
  877. goto err2;
  878. } else {
  879. rc = request_irq(msix->vector,
  880. xeon_callback_msix_irq, 0,
  881. "ntb-callback-msix",
  882. &ndev->db_cb[i]);
  883. if (rc)
  884. goto err2;
  885. }
  886. }
  887. }
  888. ndev->num_msix = msix_entries;
  889. if (ndev->hw_type == BWD_HW)
  890. ndev->max_cbs = msix_entries;
  891. else
  892. ndev->max_cbs = msix_entries - 1;
  893. return 0;
  894. err2:
  895. while (--i >= 0) {
  896. msix = &ndev->msix_entries[i];
  897. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  898. free_irq(msix->vector, ndev);
  899. else
  900. free_irq(msix->vector, &ndev->db_cb[i]);
  901. }
  902. pci_disable_msix(pdev);
  903. err1:
  904. kfree(ndev->msix_entries);
  905. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  906. err:
  907. ndev->num_msix = 0;
  908. return rc;
  909. }
  910. static int ntb_setup_msi(struct ntb_device *ndev)
  911. {
  912. struct pci_dev *pdev = ndev->pdev;
  913. int rc;
  914. rc = pci_enable_msi(pdev);
  915. if (rc)
  916. return rc;
  917. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  918. if (rc) {
  919. pci_disable_msi(pdev);
  920. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  921. return rc;
  922. }
  923. return 0;
  924. }
  925. static int ntb_setup_intx(struct ntb_device *ndev)
  926. {
  927. struct pci_dev *pdev = ndev->pdev;
  928. int rc;
  929. pci_msi_off(pdev);
  930. /* Verify intx is enabled */
  931. pci_intx(pdev, 1);
  932. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  933. ndev);
  934. if (rc)
  935. return rc;
  936. return 0;
  937. }
  938. static int ntb_setup_interrupts(struct ntb_device *ndev)
  939. {
  940. int rc;
  941. /* On BWD, disable all interrupts. On SNB, disable all but Link
  942. * Interrupt. The rest will be unmasked as callbacks are registered.
  943. */
  944. if (ndev->hw_type == BWD_HW)
  945. writeq(~0, ndev->reg_ofs.pdb_mask);
  946. else
  947. writew(~(1 << ndev->limits.max_db_bits),
  948. ndev->reg_ofs.pdb_mask);
  949. rc = ntb_setup_msix(ndev);
  950. if (!rc)
  951. goto done;
  952. ndev->bits_per_vector = 1;
  953. ndev->max_cbs = ndev->limits.max_db_bits;
  954. rc = ntb_setup_msi(ndev);
  955. if (!rc)
  956. goto done;
  957. rc = ntb_setup_intx(ndev);
  958. if (rc) {
  959. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  960. return rc;
  961. }
  962. done:
  963. return 0;
  964. }
  965. static void ntb_free_interrupts(struct ntb_device *ndev)
  966. {
  967. struct pci_dev *pdev = ndev->pdev;
  968. /* mask interrupts */
  969. if (ndev->hw_type == BWD_HW)
  970. writeq(~0, ndev->reg_ofs.pdb_mask);
  971. else
  972. writew(~0, ndev->reg_ofs.pdb_mask);
  973. if (ndev->num_msix) {
  974. struct msix_entry *msix;
  975. u32 i;
  976. for (i = 0; i < ndev->num_msix; i++) {
  977. msix = &ndev->msix_entries[i];
  978. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  979. free_irq(msix->vector, ndev);
  980. else
  981. free_irq(msix->vector, &ndev->db_cb[i]);
  982. }
  983. pci_disable_msix(pdev);
  984. } else {
  985. free_irq(pdev->irq, ndev);
  986. if (pci_dev_msi_enabled(pdev))
  987. pci_disable_msi(pdev);
  988. }
  989. }
  990. static int ntb_create_callbacks(struct ntb_device *ndev)
  991. {
  992. int i;
  993. /* Checken-egg issue. We won't know how many callbacks are necessary
  994. * until we see how many MSI-X vectors we get, but these pointers need
  995. * to be passed into the MSI-X register fucntion. So, we allocate the
  996. * max, knowing that they might not all be used, to work around this.
  997. */
  998. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  999. sizeof(struct ntb_db_cb),
  1000. GFP_KERNEL);
  1001. if (!ndev->db_cb)
  1002. return -ENOMEM;
  1003. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  1004. ndev->db_cb[i].db_num = i;
  1005. ndev->db_cb[i].ndev = ndev;
  1006. }
  1007. return 0;
  1008. }
  1009. static void ntb_free_callbacks(struct ntb_device *ndev)
  1010. {
  1011. int i;
  1012. for (i = 0; i < ndev->limits.max_db_bits; i++)
  1013. ntb_unregister_db_callback(ndev, i);
  1014. kfree(ndev->db_cb);
  1015. }
  1016. static void ntb_setup_debugfs(struct ntb_device *ndev)
  1017. {
  1018. if (!debugfs_initialized())
  1019. return;
  1020. if (!debugfs_dir)
  1021. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1022. ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
  1023. debugfs_dir);
  1024. }
  1025. static void ntb_free_debugfs(struct ntb_device *ndev)
  1026. {
  1027. debugfs_remove_recursive(ndev->debugfs_dir);
  1028. if (debugfs_dir && simple_empty(debugfs_dir)) {
  1029. debugfs_remove_recursive(debugfs_dir);
  1030. debugfs_dir = NULL;
  1031. }
  1032. }
  1033. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1034. {
  1035. struct ntb_device *ndev;
  1036. int rc, i;
  1037. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  1038. if (!ndev)
  1039. return -ENOMEM;
  1040. ndev->pdev = pdev;
  1041. ndev->link_status = NTB_LINK_DOWN;
  1042. pci_set_drvdata(pdev, ndev);
  1043. ntb_setup_debugfs(ndev);
  1044. rc = pci_enable_device(pdev);
  1045. if (rc)
  1046. goto err;
  1047. pci_set_master(ndev->pdev);
  1048. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
  1049. if (rc)
  1050. goto err1;
  1051. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  1052. if (!ndev->reg_base) {
  1053. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  1054. rc = -EIO;
  1055. goto err2;
  1056. }
  1057. for (i = 0; i < NTB_MAX_NUM_MW; i++) {
  1058. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  1059. ndev->mw[i].vbase =
  1060. ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
  1061. ndev->mw[i].bar_sz);
  1062. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  1063. (unsigned long long) ndev->mw[i].bar_sz);
  1064. if (!ndev->mw[i].vbase) {
  1065. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  1066. MW_TO_BAR(i));
  1067. rc = -EIO;
  1068. goto err3;
  1069. }
  1070. }
  1071. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1072. if (rc) {
  1073. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1074. if (rc)
  1075. goto err3;
  1076. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  1077. }
  1078. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1079. if (rc) {
  1080. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1081. if (rc)
  1082. goto err3;
  1083. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  1084. }
  1085. rc = ntb_device_setup(ndev);
  1086. if (rc)
  1087. goto err3;
  1088. rc = ntb_create_callbacks(ndev);
  1089. if (rc)
  1090. goto err4;
  1091. rc = ntb_setup_interrupts(ndev);
  1092. if (rc)
  1093. goto err5;
  1094. /* The scratchpad registers keep the values between rmmod/insmod,
  1095. * blast them now
  1096. */
  1097. for (i = 0; i < ndev->limits.max_spads; i++) {
  1098. ntb_write_local_spad(ndev, i, 0);
  1099. ntb_write_remote_spad(ndev, i, 0);
  1100. }
  1101. rc = ntb_transport_init(pdev);
  1102. if (rc)
  1103. goto err6;
  1104. /* Let's bring the NTB link up */
  1105. writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
  1106. ndev->reg_ofs.lnk_cntl);
  1107. return 0;
  1108. err6:
  1109. ntb_free_interrupts(ndev);
  1110. err5:
  1111. ntb_free_callbacks(ndev);
  1112. err4:
  1113. ntb_device_free(ndev);
  1114. err3:
  1115. for (i--; i >= 0; i--)
  1116. iounmap(ndev->mw[i].vbase);
  1117. iounmap(ndev->reg_base);
  1118. err2:
  1119. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1120. err1:
  1121. pci_disable_device(pdev);
  1122. err:
  1123. ntb_free_debugfs(ndev);
  1124. kfree(ndev);
  1125. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  1126. return rc;
  1127. }
  1128. static void ntb_pci_remove(struct pci_dev *pdev)
  1129. {
  1130. struct ntb_device *ndev = pci_get_drvdata(pdev);
  1131. int i;
  1132. u32 ntb_cntl;
  1133. /* Bring NTB link down */
  1134. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1135. ntb_cntl |= NTB_LINK_DISABLE;
  1136. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1137. ntb_transport_free(ndev->ntb_transport);
  1138. ntb_free_interrupts(ndev);
  1139. ntb_free_callbacks(ndev);
  1140. ntb_device_free(ndev);
  1141. for (i = 0; i < NTB_MAX_NUM_MW; i++)
  1142. iounmap(ndev->mw[i].vbase);
  1143. iounmap(ndev->reg_base);
  1144. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1145. pci_disable_device(pdev);
  1146. ntb_free_debugfs(ndev);
  1147. kfree(ndev);
  1148. }
  1149. static struct pci_driver ntb_pci_driver = {
  1150. .name = KBUILD_MODNAME,
  1151. .id_table = ntb_pci_tbl,
  1152. .probe = ntb_pci_probe,
  1153. .remove = ntb_pci_remove,
  1154. };
  1155. module_pci_driver(ntb_pci_driver);