ntb_hw.c 37 KB

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