ntb_hw.c 42 KB

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