ntb_hw.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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, "Only B2B supported at this time\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.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  750. ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  751. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  752. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  753. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  754. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  755. if (ndev->conn_type == NTB_CONN_B2B) {
  756. ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  757. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  758. ndev->limits.max_spads = BWD_MAX_SPADS;
  759. } else {
  760. ndev->reg_ofs.rdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  761. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
  762. ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
  763. }
  764. ndev->limits.max_mw = BWD_MAX_MW;
  765. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  766. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  767. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  768. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  769. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  770. INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
  771. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  772. return 0;
  773. }
  774. static int ntb_device_setup(struct ntb_device *ndev)
  775. {
  776. int rc;
  777. switch (ndev->pdev->device) {
  778. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  779. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  780. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  781. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  782. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  783. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  784. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  785. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  786. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  787. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  788. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  789. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  790. rc = ntb_xeon_setup(ndev);
  791. break;
  792. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  793. rc = ntb_bwd_setup(ndev);
  794. break;
  795. default:
  796. rc = -ENODEV;
  797. }
  798. if (rc)
  799. return rc;
  800. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  801. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  802. if (ndev->conn_type == NTB_CONN_B2B)
  803. /* Enable Bus Master and Memory Space on the secondary side */
  804. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  805. ndev->reg_ofs.spci_cmd);
  806. return 0;
  807. }
  808. static void ntb_device_free(struct ntb_device *ndev)
  809. {
  810. if (ndev->hw_type == BWD_HW) {
  811. cancel_delayed_work_sync(&ndev->hb_timer);
  812. cancel_delayed_work_sync(&ndev->lr_timer);
  813. }
  814. }
  815. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  816. {
  817. struct ntb_db_cb *db_cb = data;
  818. struct ntb_device *ndev = db_cb->ndev;
  819. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  820. db_cb->db_num);
  821. if (db_cb->callback)
  822. db_cb->callback(db_cb->data, db_cb->db_num);
  823. /* No need to check for the specific HB irq, any interrupt means
  824. * we're connected.
  825. */
  826. ndev->last_ts = jiffies;
  827. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
  828. return IRQ_HANDLED;
  829. }
  830. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  831. {
  832. struct ntb_db_cb *db_cb = data;
  833. struct ntb_device *ndev = db_cb->ndev;
  834. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  835. db_cb->db_num);
  836. if (db_cb->callback)
  837. db_cb->callback(db_cb->data, db_cb->db_num);
  838. /* On Sandybridge, there are 16 bits in the interrupt register
  839. * but only 4 vectors. So, 5 bits are assigned to the first 3
  840. * vectors, with the 4th having a single bit for link
  841. * interrupts.
  842. */
  843. writew(((1 << ndev->bits_per_vector) - 1) <<
  844. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
  845. return IRQ_HANDLED;
  846. }
  847. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  848. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  849. {
  850. struct ntb_device *ndev = dev;
  851. int rc;
  852. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  853. rc = ntb_link_status(ndev);
  854. if (rc)
  855. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  856. /* bit 15 is always the link bit */
  857. writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.ldb);
  858. return IRQ_HANDLED;
  859. }
  860. static irqreturn_t ntb_interrupt(int irq, void *dev)
  861. {
  862. struct ntb_device *ndev = dev;
  863. unsigned int i = 0;
  864. if (ndev->hw_type == BWD_HW) {
  865. u64 ldb = readq(ndev->reg_ofs.ldb);
  866. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
  867. while (ldb) {
  868. i = __ffs(ldb);
  869. ldb &= ldb - 1;
  870. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  871. }
  872. } else {
  873. u16 ldb = readw(ndev->reg_ofs.ldb);
  874. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
  875. if (ldb & SNB_DB_HW_LINK) {
  876. xeon_event_msix_irq(irq, dev);
  877. ldb &= ~SNB_DB_HW_LINK;
  878. }
  879. while (ldb) {
  880. i = __ffs(ldb);
  881. ldb &= ldb - 1;
  882. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  883. }
  884. }
  885. return IRQ_HANDLED;
  886. }
  887. static int ntb_setup_msix(struct ntb_device *ndev)
  888. {
  889. struct pci_dev *pdev = ndev->pdev;
  890. struct msix_entry *msix;
  891. int msix_entries;
  892. int rc, i, pos;
  893. u16 val;
  894. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  895. if (!pos) {
  896. rc = -EIO;
  897. goto err;
  898. }
  899. rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
  900. if (rc)
  901. goto err;
  902. msix_entries = msix_table_size(val);
  903. if (msix_entries > ndev->limits.msix_cnt) {
  904. rc = -EINVAL;
  905. goto err;
  906. }
  907. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  908. GFP_KERNEL);
  909. if (!ndev->msix_entries) {
  910. rc = -ENOMEM;
  911. goto err;
  912. }
  913. for (i = 0; i < msix_entries; i++)
  914. ndev->msix_entries[i].entry = i;
  915. rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
  916. if (rc < 0)
  917. goto err1;
  918. if (rc > 0) {
  919. /* On SNB, the link interrupt is always tied to 4th vector. If
  920. * we can't get all 4, then we can't use MSI-X.
  921. */
  922. if (ndev->hw_type != BWD_HW) {
  923. rc = -EIO;
  924. goto err1;
  925. }
  926. dev_warn(&pdev->dev,
  927. "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
  928. rc);
  929. msix_entries = rc;
  930. }
  931. for (i = 0; i < msix_entries; i++) {
  932. msix = &ndev->msix_entries[i];
  933. WARN_ON(!msix->vector);
  934. /* Use the last MSI-X vector for Link status */
  935. if (ndev->hw_type == BWD_HW) {
  936. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  937. "ntb-callback-msix", &ndev->db_cb[i]);
  938. if (rc)
  939. goto err2;
  940. } else {
  941. if (i == msix_entries - 1) {
  942. rc = request_irq(msix->vector,
  943. xeon_event_msix_irq, 0,
  944. "ntb-event-msix", ndev);
  945. if (rc)
  946. goto err2;
  947. } else {
  948. rc = request_irq(msix->vector,
  949. xeon_callback_msix_irq, 0,
  950. "ntb-callback-msix",
  951. &ndev->db_cb[i]);
  952. if (rc)
  953. goto err2;
  954. }
  955. }
  956. }
  957. ndev->num_msix = msix_entries;
  958. if (ndev->hw_type == BWD_HW)
  959. ndev->max_cbs = msix_entries;
  960. else
  961. ndev->max_cbs = msix_entries - 1;
  962. return 0;
  963. err2:
  964. while (--i >= 0) {
  965. msix = &ndev->msix_entries[i];
  966. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  967. free_irq(msix->vector, ndev);
  968. else
  969. free_irq(msix->vector, &ndev->db_cb[i]);
  970. }
  971. pci_disable_msix(pdev);
  972. err1:
  973. kfree(ndev->msix_entries);
  974. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  975. err:
  976. ndev->num_msix = 0;
  977. return rc;
  978. }
  979. static int ntb_setup_msi(struct ntb_device *ndev)
  980. {
  981. struct pci_dev *pdev = ndev->pdev;
  982. int rc;
  983. rc = pci_enable_msi(pdev);
  984. if (rc)
  985. return rc;
  986. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  987. if (rc) {
  988. pci_disable_msi(pdev);
  989. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  990. return rc;
  991. }
  992. return 0;
  993. }
  994. static int ntb_setup_intx(struct ntb_device *ndev)
  995. {
  996. struct pci_dev *pdev = ndev->pdev;
  997. int rc;
  998. pci_msi_off(pdev);
  999. /* Verify intx is enabled */
  1000. pci_intx(pdev, 1);
  1001. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  1002. ndev);
  1003. if (rc)
  1004. return rc;
  1005. return 0;
  1006. }
  1007. static int ntb_setup_interrupts(struct ntb_device *ndev)
  1008. {
  1009. int rc;
  1010. /* On BWD, disable all interrupts. On SNB, disable all but Link
  1011. * Interrupt. The rest will be unmasked as callbacks are registered.
  1012. */
  1013. if (ndev->hw_type == BWD_HW)
  1014. writeq(~0, ndev->reg_ofs.ldb_mask);
  1015. else
  1016. writew(~(1 << ndev->limits.max_db_bits),
  1017. ndev->reg_ofs.ldb_mask);
  1018. rc = ntb_setup_msix(ndev);
  1019. if (!rc)
  1020. goto done;
  1021. ndev->bits_per_vector = 1;
  1022. ndev->max_cbs = ndev->limits.max_db_bits;
  1023. rc = ntb_setup_msi(ndev);
  1024. if (!rc)
  1025. goto done;
  1026. rc = ntb_setup_intx(ndev);
  1027. if (rc) {
  1028. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  1029. return rc;
  1030. }
  1031. done:
  1032. return 0;
  1033. }
  1034. static void ntb_free_interrupts(struct ntb_device *ndev)
  1035. {
  1036. struct pci_dev *pdev = ndev->pdev;
  1037. /* mask interrupts */
  1038. if (ndev->hw_type == BWD_HW)
  1039. writeq(~0, ndev->reg_ofs.ldb_mask);
  1040. else
  1041. writew(~0, ndev->reg_ofs.ldb_mask);
  1042. if (ndev->num_msix) {
  1043. struct msix_entry *msix;
  1044. u32 i;
  1045. for (i = 0; i < ndev->num_msix; i++) {
  1046. msix = &ndev->msix_entries[i];
  1047. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  1048. free_irq(msix->vector, ndev);
  1049. else
  1050. free_irq(msix->vector, &ndev->db_cb[i]);
  1051. }
  1052. pci_disable_msix(pdev);
  1053. } else {
  1054. free_irq(pdev->irq, ndev);
  1055. if (pci_dev_msi_enabled(pdev))
  1056. pci_disable_msi(pdev);
  1057. }
  1058. }
  1059. static int ntb_create_callbacks(struct ntb_device *ndev)
  1060. {
  1061. int i;
  1062. /* Checken-egg issue. We won't know how many callbacks are necessary
  1063. * until we see how many MSI-X vectors we get, but these pointers need
  1064. * to be passed into the MSI-X register fucntion. So, we allocate the
  1065. * max, knowing that they might not all be used, to work around this.
  1066. */
  1067. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  1068. sizeof(struct ntb_db_cb),
  1069. GFP_KERNEL);
  1070. if (!ndev->db_cb)
  1071. return -ENOMEM;
  1072. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  1073. ndev->db_cb[i].db_num = i;
  1074. ndev->db_cb[i].ndev = ndev;
  1075. }
  1076. return 0;
  1077. }
  1078. static void ntb_free_callbacks(struct ntb_device *ndev)
  1079. {
  1080. int i;
  1081. for (i = 0; i < ndev->limits.max_db_bits; i++)
  1082. ntb_unregister_db_callback(ndev, i);
  1083. kfree(ndev->db_cb);
  1084. }
  1085. static void ntb_setup_debugfs(struct ntb_device *ndev)
  1086. {
  1087. if (!debugfs_initialized())
  1088. return;
  1089. if (!debugfs_dir)
  1090. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1091. ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
  1092. debugfs_dir);
  1093. }
  1094. static void ntb_free_debugfs(struct ntb_device *ndev)
  1095. {
  1096. debugfs_remove_recursive(ndev->debugfs_dir);
  1097. if (debugfs_dir && simple_empty(debugfs_dir)) {
  1098. debugfs_remove_recursive(debugfs_dir);
  1099. debugfs_dir = NULL;
  1100. }
  1101. }
  1102. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1103. {
  1104. struct ntb_device *ndev;
  1105. int rc, i;
  1106. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  1107. if (!ndev)
  1108. return -ENOMEM;
  1109. ndev->pdev = pdev;
  1110. ndev->link_status = NTB_LINK_DOWN;
  1111. pci_set_drvdata(pdev, ndev);
  1112. ntb_setup_debugfs(ndev);
  1113. rc = pci_enable_device(pdev);
  1114. if (rc)
  1115. goto err;
  1116. pci_set_master(ndev->pdev);
  1117. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
  1118. if (rc)
  1119. goto err1;
  1120. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  1121. if (!ndev->reg_base) {
  1122. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  1123. rc = -EIO;
  1124. goto err2;
  1125. }
  1126. for (i = 0; i < NTB_MAX_NUM_MW; i++) {
  1127. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  1128. ndev->mw[i].vbase =
  1129. ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
  1130. ndev->mw[i].bar_sz);
  1131. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  1132. (unsigned long long) ndev->mw[i].bar_sz);
  1133. if (!ndev->mw[i].vbase) {
  1134. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  1135. MW_TO_BAR(i));
  1136. rc = -EIO;
  1137. goto err3;
  1138. }
  1139. }
  1140. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1141. if (rc) {
  1142. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1143. if (rc)
  1144. goto err3;
  1145. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  1146. }
  1147. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1148. if (rc) {
  1149. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1150. if (rc)
  1151. goto err3;
  1152. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  1153. }
  1154. rc = ntb_device_setup(ndev);
  1155. if (rc)
  1156. goto err3;
  1157. rc = ntb_create_callbacks(ndev);
  1158. if (rc)
  1159. goto err4;
  1160. rc = ntb_setup_interrupts(ndev);
  1161. if (rc)
  1162. goto err5;
  1163. /* The scratchpad registers keep the values between rmmod/insmod,
  1164. * blast them now
  1165. */
  1166. for (i = 0; i < ndev->limits.max_spads; i++) {
  1167. ntb_write_local_spad(ndev, i, 0);
  1168. ntb_write_remote_spad(ndev, i, 0);
  1169. }
  1170. rc = ntb_transport_init(pdev);
  1171. if (rc)
  1172. goto err6;
  1173. /* Let's bring the NTB link up */
  1174. writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
  1175. ndev->reg_ofs.lnk_cntl);
  1176. return 0;
  1177. err6:
  1178. ntb_free_interrupts(ndev);
  1179. err5:
  1180. ntb_free_callbacks(ndev);
  1181. err4:
  1182. ntb_device_free(ndev);
  1183. err3:
  1184. for (i--; i >= 0; i--)
  1185. iounmap(ndev->mw[i].vbase);
  1186. iounmap(ndev->reg_base);
  1187. err2:
  1188. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1189. err1:
  1190. pci_disable_device(pdev);
  1191. err:
  1192. ntb_free_debugfs(ndev);
  1193. kfree(ndev);
  1194. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  1195. return rc;
  1196. }
  1197. static void ntb_pci_remove(struct pci_dev *pdev)
  1198. {
  1199. struct ntb_device *ndev = pci_get_drvdata(pdev);
  1200. int i;
  1201. u32 ntb_cntl;
  1202. /* Bring NTB link down */
  1203. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1204. ntb_cntl |= NTB_CNTL_LINK_DISABLE;
  1205. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1206. ntb_transport_free(ndev->ntb_transport);
  1207. ntb_free_interrupts(ndev);
  1208. ntb_free_callbacks(ndev);
  1209. ntb_device_free(ndev);
  1210. for (i = 0; i < NTB_MAX_NUM_MW; i++)
  1211. iounmap(ndev->mw[i].vbase);
  1212. iounmap(ndev->reg_base);
  1213. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1214. pci_disable_device(pdev);
  1215. ntb_free_debugfs(ndev);
  1216. kfree(ndev);
  1217. }
  1218. static struct pci_driver ntb_pci_driver = {
  1219. .name = KBUILD_MODNAME,
  1220. .id_table = ntb_pci_tbl,
  1221. .probe = ntb_pci_probe,
  1222. .remove = ntb_pci_remove,
  1223. };
  1224. module_pci_driver(ntb_pci_driver);