ntb_hw.c 41 KB

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