ntb_hw.c 39 KB

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