ntb_hw.c 41 KB

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