ntb_hw.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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/init.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/module.h>
  52. #include <linux/pci.h>
  53. #include <linux/slab.h>
  54. #include "ntb_hw.h"
  55. #include "ntb_regs.h"
  56. #define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
  57. #define NTB_VER "0.25"
  58. MODULE_DESCRIPTION(NTB_NAME);
  59. MODULE_VERSION(NTB_VER);
  60. MODULE_LICENSE("Dual BSD/GPL");
  61. MODULE_AUTHOR("Intel Corporation");
  62. enum {
  63. NTB_CONN_CLASSIC = 0,
  64. NTB_CONN_B2B,
  65. NTB_CONN_RP,
  66. };
  67. enum {
  68. NTB_DEV_USD = 0,
  69. NTB_DEV_DSD,
  70. };
  71. enum {
  72. SNB_HW = 0,
  73. BWD_HW,
  74. };
  75. /* Translate memory window 0,1 to BAR 2,4 */
  76. #define MW_TO_BAR(mw) (mw * 2 + 2)
  77. static DEFINE_PCI_DEVICE_TABLE(ntb_pci_tbl) = {
  78. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  79. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  80. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF)},
  81. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_JSF)},
  82. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_SNB)},
  83. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  84. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB)},
  85. {0}
  86. };
  87. MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
  88. /**
  89. * ntb_register_event_callback() - register event callback
  90. * @ndev: pointer to ntb_device instance
  91. * @func: callback function to register
  92. *
  93. * This function registers a callback for any HW driver events such as link
  94. * up/down, power management notices and etc.
  95. *
  96. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  97. */
  98. int ntb_register_event_callback(struct ntb_device *ndev,
  99. void (*func)(void *handle, enum ntb_hw_event event))
  100. {
  101. if (ndev->event_cb)
  102. return -EINVAL;
  103. ndev->event_cb = func;
  104. return 0;
  105. }
  106. /**
  107. * ntb_unregister_event_callback() - unregisters the event callback
  108. * @ndev: pointer to ntb_device instance
  109. *
  110. * This function unregisters the existing callback from transport
  111. */
  112. void ntb_unregister_event_callback(struct ntb_device *ndev)
  113. {
  114. ndev->event_cb = NULL;
  115. }
  116. /**
  117. * ntb_register_db_callback() - register a callback for doorbell interrupt
  118. * @ndev: pointer to ntb_device instance
  119. * @idx: doorbell index to register callback, zero based
  120. * @func: callback function to register
  121. *
  122. * This function registers a callback function for the doorbell interrupt
  123. * on the primary side. The function will unmask the doorbell as well to
  124. * allow interrupt.
  125. *
  126. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  127. */
  128. int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
  129. void *data, void (*func)(void *data, int db_num))
  130. {
  131. unsigned long mask;
  132. if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
  133. dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
  134. return -EINVAL;
  135. }
  136. ndev->db_cb[idx].callback = func;
  137. ndev->db_cb[idx].data = data;
  138. /* unmask interrupt */
  139. mask = readw(ndev->reg_ofs.pdb_mask);
  140. clear_bit(idx * ndev->bits_per_vector, &mask);
  141. writew(mask, ndev->reg_ofs.pdb_mask);
  142. return 0;
  143. }
  144. /**
  145. * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
  146. * @ndev: pointer to ntb_device instance
  147. * @idx: doorbell index to register callback, zero based
  148. *
  149. * This function unregisters a callback function for the doorbell interrupt
  150. * on the primary side. The function will also mask the said doorbell.
  151. */
  152. void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
  153. {
  154. unsigned long mask;
  155. if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
  156. return;
  157. mask = readw(ndev->reg_ofs.pdb_mask);
  158. set_bit(idx * ndev->bits_per_vector, &mask);
  159. writew(mask, ndev->reg_ofs.pdb_mask);
  160. ndev->db_cb[idx].callback = NULL;
  161. }
  162. /**
  163. * ntb_find_transport() - find the transport pointer
  164. * @transport: pointer to pci device
  165. *
  166. * Given the pci device pointer, return the transport pointer passed in when
  167. * the transport attached when it was inited.
  168. *
  169. * RETURNS: pointer to transport.
  170. */
  171. void *ntb_find_transport(struct pci_dev *pdev)
  172. {
  173. struct ntb_device *ndev = pci_get_drvdata(pdev);
  174. return ndev->ntb_transport;
  175. }
  176. /**
  177. * ntb_register_transport() - Register NTB transport with NTB HW driver
  178. * @transport: transport identifier
  179. *
  180. * This function allows a transport to reserve the hardware driver for
  181. * NTB usage.
  182. *
  183. * RETURNS: pointer to ntb_device, NULL on error.
  184. */
  185. struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
  186. {
  187. struct ntb_device *ndev = pci_get_drvdata(pdev);
  188. if (ndev->ntb_transport)
  189. return NULL;
  190. ndev->ntb_transport = transport;
  191. return ndev;
  192. }
  193. /**
  194. * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
  195. * @ndev - ntb_device of the transport to be freed
  196. *
  197. * This function unregisters the transport from the HW driver and performs any
  198. * necessary cleanups.
  199. */
  200. void ntb_unregister_transport(struct ntb_device *ndev)
  201. {
  202. int i;
  203. if (!ndev->ntb_transport)
  204. return;
  205. for (i = 0; i < ndev->max_cbs; i++)
  206. ntb_unregister_db_callback(ndev, i);
  207. ntb_unregister_event_callback(ndev);
  208. ndev->ntb_transport = NULL;
  209. }
  210. /**
  211. * ntb_write_local_spad() - write to the secondary scratchpad register
  212. * @ndev: pointer to ntb_device instance
  213. * @idx: index to the scratchpad register, 0 based
  214. * @val: the data value to put into the register
  215. *
  216. * This function allows writing of a 32bit value to the indexed scratchpad
  217. * register. This writes over the data mirrored to the local scratchpad register
  218. * by the remote system.
  219. *
  220. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  221. */
  222. int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  223. {
  224. if (idx >= ndev->limits.max_spads)
  225. return -EINVAL;
  226. dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
  227. val, idx);
  228. writel(val, ndev->reg_ofs.spad_read + idx * 4);
  229. return 0;
  230. }
  231. /**
  232. * ntb_read_local_spad() - read from the primary scratchpad register
  233. * @ndev: pointer to ntb_device instance
  234. * @idx: index to scratchpad register, 0 based
  235. * @val: pointer to 32bit integer for storing the register value
  236. *
  237. * This function allows reading of the 32bit scratchpad register on
  238. * the primary (internal) side. This allows the local system to read data
  239. * written and mirrored to the scratchpad register by the remote system.
  240. *
  241. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  242. */
  243. int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  244. {
  245. if (idx >= ndev->limits.max_spads)
  246. return -EINVAL;
  247. *val = readl(ndev->reg_ofs.spad_write + idx * 4);
  248. dev_dbg(&ndev->pdev->dev,
  249. "Reading %x from local scratch pad index %d\n", *val, idx);
  250. return 0;
  251. }
  252. /**
  253. * ntb_write_remote_spad() - write to the secondary scratchpad register
  254. * @ndev: pointer to ntb_device instance
  255. * @idx: index to the scratchpad register, 0 based
  256. * @val: the data value to put into the register
  257. *
  258. * This function allows writing of a 32bit value to the indexed scratchpad
  259. * register. The register resides on the secondary (external) side. This allows
  260. * the local system to write data to be mirrored to the remote systems
  261. * scratchpad register.
  262. *
  263. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  264. */
  265. int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  266. {
  267. if (idx >= ndev->limits.max_spads)
  268. return -EINVAL;
  269. dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
  270. val, idx);
  271. writel(val, ndev->reg_ofs.spad_write + idx * 4);
  272. return 0;
  273. }
  274. /**
  275. * ntb_read_remote_spad() - read from the primary scratchpad register
  276. * @ndev: pointer to ntb_device instance
  277. * @idx: index to scratchpad register, 0 based
  278. * @val: pointer to 32bit integer for storing the register value
  279. *
  280. * This function allows reading of the 32bit scratchpad register on
  281. * the primary (internal) side. This alloows the local system to read the data
  282. * it wrote to be mirrored on the remote system.
  283. *
  284. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  285. */
  286. int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  287. {
  288. if (idx >= ndev->limits.max_spads)
  289. return -EINVAL;
  290. *val = readl(ndev->reg_ofs.spad_read + idx * 4);
  291. dev_dbg(&ndev->pdev->dev,
  292. "Reading %x from remote scratch pad index %d\n", *val, idx);
  293. return 0;
  294. }
  295. /**
  296. * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
  297. * @ndev: pointer to ntb_device instance
  298. * @mw: memory window number
  299. *
  300. * This function provides the base virtual address of the memory window
  301. * specified.
  302. *
  303. * RETURNS: pointer to virtual address, or NULL on error.
  304. */
  305. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
  306. {
  307. if (mw > NTB_NUM_MW)
  308. return NULL;
  309. return ndev->mw[mw].vbase;
  310. }
  311. /**
  312. * ntb_get_mw_size() - return size of NTB memory window
  313. * @ndev: pointer to ntb_device instance
  314. * @mw: memory window number
  315. *
  316. * This function provides the physical size of the memory window specified
  317. *
  318. * RETURNS: the size of the memory window or zero on error
  319. */
  320. resource_size_t ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
  321. {
  322. if (mw > NTB_NUM_MW)
  323. return 0;
  324. return ndev->mw[mw].bar_sz;
  325. }
  326. /**
  327. * ntb_set_mw_addr - set the memory window address
  328. * @ndev: pointer to ntb_device instance
  329. * @mw: memory window number
  330. * @addr: base address for data
  331. *
  332. * This function sets the base physical address of the memory window. This
  333. * memory address is where data from the remote system will be transfered into
  334. * or out of depending on how the transport is configured.
  335. */
  336. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
  337. {
  338. if (mw > NTB_NUM_MW)
  339. return;
  340. dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
  341. MW_TO_BAR(mw));
  342. ndev->mw[mw].phys_addr = addr;
  343. switch (MW_TO_BAR(mw)) {
  344. case NTB_BAR_23:
  345. writeq(addr, ndev->reg_ofs.sbar2_xlat);
  346. break;
  347. case NTB_BAR_45:
  348. writeq(addr, ndev->reg_ofs.sbar4_xlat);
  349. break;
  350. }
  351. }
  352. /**
  353. * ntb_ring_sdb() - Set the doorbell on the secondary/external side
  354. * @ndev: pointer to ntb_device instance
  355. * @db: doorbell to ring
  356. *
  357. * This function allows triggering of a doorbell on the secondary/external
  358. * side that will initiate an interrupt on the remote host
  359. *
  360. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  361. */
  362. void ntb_ring_sdb(struct ntb_device *ndev, unsigned int db)
  363. {
  364. dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
  365. if (ndev->hw_type == BWD_HW)
  366. writeq((u64) 1 << db, ndev->reg_ofs.sdb);
  367. else
  368. writew(((1 << ndev->bits_per_vector) - 1) <<
  369. (db * ndev->bits_per_vector), ndev->reg_ofs.sdb);
  370. }
  371. static void ntb_link_event(struct ntb_device *ndev, int link_state)
  372. {
  373. unsigned int event;
  374. if (ndev->link_status == link_state)
  375. return;
  376. if (link_state == NTB_LINK_UP) {
  377. u16 status;
  378. dev_info(&ndev->pdev->dev, "Link Up\n");
  379. ndev->link_status = NTB_LINK_UP;
  380. event = NTB_EVENT_HW_LINK_UP;
  381. if (ndev->hw_type == BWD_HW)
  382. status = readw(ndev->reg_ofs.lnk_stat);
  383. else {
  384. int rc = pci_read_config_word(ndev->pdev,
  385. SNB_LINK_STATUS_OFFSET,
  386. &status);
  387. if (rc)
  388. return;
  389. }
  390. dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
  391. (status & NTB_LINK_WIDTH_MASK) >> 4,
  392. (status & NTB_LINK_SPEED_MASK));
  393. } else {
  394. dev_info(&ndev->pdev->dev, "Link Down\n");
  395. ndev->link_status = NTB_LINK_DOWN;
  396. event = NTB_EVENT_HW_LINK_DOWN;
  397. }
  398. /* notify the upper layer if we have an event change */
  399. if (ndev->event_cb)
  400. ndev->event_cb(ndev->ntb_transport, event);
  401. }
  402. static int ntb_link_status(struct ntb_device *ndev)
  403. {
  404. int link_state;
  405. if (ndev->hw_type == BWD_HW) {
  406. u32 ntb_cntl;
  407. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  408. if (ntb_cntl & BWD_CNTL_LINK_DOWN)
  409. link_state = NTB_LINK_DOWN;
  410. else
  411. link_state = NTB_LINK_UP;
  412. } else {
  413. u16 status;
  414. int rc;
  415. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  416. &status);
  417. if (rc)
  418. return rc;
  419. if (status & NTB_LINK_STATUS_ACTIVE)
  420. link_state = NTB_LINK_UP;
  421. else
  422. link_state = NTB_LINK_DOWN;
  423. }
  424. ntb_link_event(ndev, link_state);
  425. return 0;
  426. }
  427. /* BWD doesn't have link status interrupt, poll on that platform */
  428. static void bwd_link_poll(struct work_struct *work)
  429. {
  430. struct ntb_device *ndev = container_of(work, struct ntb_device,
  431. hb_timer.work);
  432. unsigned long ts = jiffies;
  433. /* If we haven't gotten an interrupt in a while, check the BWD link
  434. * status bit
  435. */
  436. if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
  437. int rc = ntb_link_status(ndev);
  438. if (rc)
  439. dev_err(&ndev->pdev->dev,
  440. "Error determining link status\n");
  441. }
  442. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  443. }
  444. static int ntb_xeon_setup(struct ntb_device *ndev)
  445. {
  446. int rc;
  447. u8 val;
  448. ndev->hw_type = SNB_HW;
  449. rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
  450. if (rc)
  451. return rc;
  452. switch (val & SNB_PPD_CONN_TYPE) {
  453. case NTB_CONN_B2B:
  454. ndev->conn_type = NTB_CONN_B2B;
  455. break;
  456. case NTB_CONN_CLASSIC:
  457. case NTB_CONN_RP:
  458. default:
  459. dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
  460. return -EINVAL;
  461. }
  462. if (val & SNB_PPD_DEV_TYPE)
  463. ndev->dev_type = NTB_DEV_DSD;
  464. else
  465. ndev->dev_type = NTB_DEV_USD;
  466. ndev->reg_ofs.pdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  467. ndev->reg_ofs.pdb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  468. ndev->reg_ofs.sbar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  469. ndev->reg_ofs.sbar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  470. ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
  471. ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_LINK_STATUS_OFFSET;
  472. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  473. ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
  474. if (ndev->conn_type == NTB_CONN_B2B) {
  475. ndev->reg_ofs.sdb = ndev->reg_base + SNB_B2B_DOORBELL_OFFSET;
  476. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_B2B_SPAD_OFFSET;
  477. ndev->limits.max_spads = SNB_MAX_SPADS;
  478. } else {
  479. ndev->reg_ofs.sdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  480. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
  481. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS;
  482. }
  483. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  484. ndev->limits.msix_cnt = SNB_MSIX_CNT;
  485. ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
  486. return 0;
  487. }
  488. static int ntb_bwd_setup(struct ntb_device *ndev)
  489. {
  490. int rc;
  491. u32 val;
  492. ndev->hw_type = BWD_HW;
  493. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
  494. if (rc)
  495. return rc;
  496. switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
  497. case NTB_CONN_B2B:
  498. ndev->conn_type = NTB_CONN_B2B;
  499. break;
  500. case NTB_CONN_RP:
  501. default:
  502. dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
  503. return -EINVAL;
  504. }
  505. if (val & BWD_PPD_DEV_TYPE)
  506. ndev->dev_type = NTB_DEV_DSD;
  507. else
  508. ndev->dev_type = NTB_DEV_USD;
  509. /* Initiate PCI-E link training */
  510. rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
  511. val | BWD_PPD_INIT_LINK);
  512. if (rc)
  513. return rc;
  514. ndev->reg_ofs.pdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  515. ndev->reg_ofs.pdb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
  516. ndev->reg_ofs.sbar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  517. ndev->reg_ofs.sbar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  518. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  519. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  520. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  521. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  522. if (ndev->conn_type == NTB_CONN_B2B) {
  523. ndev->reg_ofs.sdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  524. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  525. ndev->limits.max_spads = BWD_MAX_SPADS;
  526. } else {
  527. ndev->reg_ofs.sdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  528. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
  529. ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
  530. }
  531. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  532. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  533. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  534. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  535. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  536. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  537. return 0;
  538. }
  539. static int ntb_device_setup(struct ntb_device *ndev)
  540. {
  541. int rc;
  542. switch (ndev->pdev->device) {
  543. case PCI_DEVICE_ID_INTEL_NTB_2ND_SNB:
  544. case PCI_DEVICE_ID_INTEL_NTB_RP_JSF:
  545. case PCI_DEVICE_ID_INTEL_NTB_RP_SNB:
  546. case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF:
  547. case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB:
  548. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  549. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  550. rc = ntb_xeon_setup(ndev);
  551. break;
  552. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  553. rc = ntb_bwd_setup(ndev);
  554. break;
  555. default:
  556. rc = -ENODEV;
  557. }
  558. /* Enable Bus Master and Memory Space on the secondary side */
  559. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
  560. return rc;
  561. }
  562. static void ntb_device_free(struct ntb_device *ndev)
  563. {
  564. if (ndev->hw_type == BWD_HW)
  565. cancel_delayed_work_sync(&ndev->hb_timer);
  566. }
  567. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  568. {
  569. struct ntb_db_cb *db_cb = data;
  570. struct ntb_device *ndev = db_cb->ndev;
  571. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  572. db_cb->db_num);
  573. if (db_cb->callback)
  574. db_cb->callback(db_cb->data, db_cb->db_num);
  575. /* No need to check for the specific HB irq, any interrupt means
  576. * we're connected.
  577. */
  578. ndev->last_ts = jiffies;
  579. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
  580. return IRQ_HANDLED;
  581. }
  582. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  583. {
  584. struct ntb_db_cb *db_cb = data;
  585. struct ntb_device *ndev = db_cb->ndev;
  586. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  587. db_cb->db_num);
  588. if (db_cb->callback)
  589. db_cb->callback(db_cb->data, db_cb->db_num);
  590. /* On Sandybridge, there are 16 bits in the interrupt register
  591. * but only 4 vectors. So, 5 bits are assigned to the first 3
  592. * vectors, with the 4th having a single bit for link
  593. * interrupts.
  594. */
  595. writew(((1 << ndev->bits_per_vector) - 1) <<
  596. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
  597. return IRQ_HANDLED;
  598. }
  599. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  600. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  601. {
  602. struct ntb_device *ndev = dev;
  603. int rc;
  604. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  605. rc = ntb_link_status(ndev);
  606. if (rc)
  607. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  608. /* bit 15 is always the link bit */
  609. writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
  610. return IRQ_HANDLED;
  611. }
  612. static irqreturn_t ntb_interrupt(int irq, void *dev)
  613. {
  614. struct ntb_device *ndev = dev;
  615. unsigned int i = 0;
  616. if (ndev->hw_type == BWD_HW) {
  617. u64 pdb = readq(ndev->reg_ofs.pdb);
  618. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
  619. while (pdb) {
  620. i = __ffs(pdb);
  621. pdb &= pdb - 1;
  622. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  623. }
  624. } else {
  625. u16 pdb = readw(ndev->reg_ofs.pdb);
  626. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
  627. pdb, readw(ndev->reg_ofs.sdb));
  628. if (pdb & SNB_DB_HW_LINK) {
  629. xeon_event_msix_irq(irq, dev);
  630. pdb &= ~SNB_DB_HW_LINK;
  631. }
  632. while (pdb) {
  633. i = __ffs(pdb);
  634. pdb &= pdb - 1;
  635. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  636. }
  637. }
  638. return IRQ_HANDLED;
  639. }
  640. static int ntb_setup_msix(struct ntb_device *ndev)
  641. {
  642. struct pci_dev *pdev = ndev->pdev;
  643. struct msix_entry *msix;
  644. int msix_entries;
  645. int rc, i, pos;
  646. u16 val;
  647. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  648. if (!pos) {
  649. rc = -EIO;
  650. goto err;
  651. }
  652. rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
  653. if (rc)
  654. goto err;
  655. msix_entries = msix_table_size(val);
  656. if (msix_entries > ndev->limits.msix_cnt) {
  657. rc = -EINVAL;
  658. goto err;
  659. }
  660. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  661. GFP_KERNEL);
  662. if (!ndev->msix_entries) {
  663. rc = -ENOMEM;
  664. goto err;
  665. }
  666. for (i = 0; i < msix_entries; i++)
  667. ndev->msix_entries[i].entry = i;
  668. rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
  669. if (rc < 0)
  670. goto err1;
  671. if (rc > 0) {
  672. /* On SNB, the link interrupt is always tied to 4th vector. If
  673. * we can't get all 4, then we can't use MSI-X.
  674. */
  675. if (ndev->hw_type != BWD_HW) {
  676. rc = -EIO;
  677. goto err1;
  678. }
  679. dev_warn(&pdev->dev,
  680. "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
  681. rc);
  682. msix_entries = rc;
  683. }
  684. for (i = 0; i < msix_entries; i++) {
  685. msix = &ndev->msix_entries[i];
  686. WARN_ON(!msix->vector);
  687. /* Use the last MSI-X vector for Link status */
  688. if (ndev->hw_type == BWD_HW) {
  689. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  690. "ntb-callback-msix", &ndev->db_cb[i]);
  691. if (rc)
  692. goto err2;
  693. } else {
  694. if (i == msix_entries - 1) {
  695. rc = request_irq(msix->vector,
  696. xeon_event_msix_irq, 0,
  697. "ntb-event-msix", ndev);
  698. if (rc)
  699. goto err2;
  700. } else {
  701. rc = request_irq(msix->vector,
  702. xeon_callback_msix_irq, 0,
  703. "ntb-callback-msix",
  704. &ndev->db_cb[i]);
  705. if (rc)
  706. goto err2;
  707. }
  708. }
  709. }
  710. ndev->num_msix = msix_entries;
  711. if (ndev->hw_type == BWD_HW)
  712. ndev->max_cbs = msix_entries;
  713. else
  714. ndev->max_cbs = msix_entries - 1;
  715. return 0;
  716. err2:
  717. while (--i >= 0) {
  718. msix = &ndev->msix_entries[i];
  719. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  720. free_irq(msix->vector, ndev);
  721. else
  722. free_irq(msix->vector, &ndev->db_cb[i]);
  723. }
  724. pci_disable_msix(pdev);
  725. err1:
  726. kfree(ndev->msix_entries);
  727. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  728. err:
  729. ndev->num_msix = 0;
  730. return rc;
  731. }
  732. static int ntb_setup_msi(struct ntb_device *ndev)
  733. {
  734. struct pci_dev *pdev = ndev->pdev;
  735. int rc;
  736. rc = pci_enable_msi(pdev);
  737. if (rc)
  738. return rc;
  739. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  740. if (rc) {
  741. pci_disable_msi(pdev);
  742. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  743. return rc;
  744. }
  745. return 0;
  746. }
  747. static int ntb_setup_intx(struct ntb_device *ndev)
  748. {
  749. struct pci_dev *pdev = ndev->pdev;
  750. int rc;
  751. pci_msi_off(pdev);
  752. /* Verify intx is enabled */
  753. pci_intx(pdev, 1);
  754. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  755. ndev);
  756. if (rc)
  757. return rc;
  758. return 0;
  759. }
  760. static int ntb_setup_interrupts(struct ntb_device *ndev)
  761. {
  762. int rc;
  763. /* On BWD, disable all interrupts. On SNB, disable all but Link
  764. * Interrupt. The rest will be unmasked as callbacks are registered.
  765. */
  766. if (ndev->hw_type == BWD_HW)
  767. writeq(~0, ndev->reg_ofs.pdb_mask);
  768. else
  769. writew(~(1 << ndev->limits.max_db_bits),
  770. ndev->reg_ofs.pdb_mask);
  771. rc = ntb_setup_msix(ndev);
  772. if (!rc)
  773. goto done;
  774. ndev->bits_per_vector = 1;
  775. ndev->max_cbs = ndev->limits.max_db_bits;
  776. rc = ntb_setup_msi(ndev);
  777. if (!rc)
  778. goto done;
  779. rc = ntb_setup_intx(ndev);
  780. if (rc) {
  781. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  782. return rc;
  783. }
  784. done:
  785. return 0;
  786. }
  787. static void ntb_free_interrupts(struct ntb_device *ndev)
  788. {
  789. struct pci_dev *pdev = ndev->pdev;
  790. /* mask interrupts */
  791. if (ndev->hw_type == BWD_HW)
  792. writeq(~0, ndev->reg_ofs.pdb_mask);
  793. else
  794. writew(~0, ndev->reg_ofs.pdb_mask);
  795. if (ndev->num_msix) {
  796. struct msix_entry *msix;
  797. u32 i;
  798. for (i = 0; i < ndev->num_msix; i++) {
  799. msix = &ndev->msix_entries[i];
  800. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  801. free_irq(msix->vector, ndev);
  802. else
  803. free_irq(msix->vector, &ndev->db_cb[i]);
  804. }
  805. pci_disable_msix(pdev);
  806. } else {
  807. free_irq(pdev->irq, ndev);
  808. if (pci_dev_msi_enabled(pdev))
  809. pci_disable_msi(pdev);
  810. }
  811. }
  812. static int ntb_create_callbacks(struct ntb_device *ndev)
  813. {
  814. int i;
  815. /* Checken-egg issue. We won't know how many callbacks are necessary
  816. * until we see how many MSI-X vectors we get, but these pointers need
  817. * to be passed into the MSI-X register fucntion. So, we allocate the
  818. * max, knowing that they might not all be used, to work around this.
  819. */
  820. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  821. sizeof(struct ntb_db_cb),
  822. GFP_KERNEL);
  823. if (!ndev->db_cb)
  824. return -ENOMEM;
  825. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  826. ndev->db_cb[i].db_num = i;
  827. ndev->db_cb[i].ndev = ndev;
  828. }
  829. return 0;
  830. }
  831. static void ntb_free_callbacks(struct ntb_device *ndev)
  832. {
  833. int i;
  834. for (i = 0; i < ndev->limits.max_db_bits; i++)
  835. ntb_unregister_db_callback(ndev, i);
  836. kfree(ndev->db_cb);
  837. }
  838. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  839. {
  840. struct ntb_device *ndev;
  841. int rc, i;
  842. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  843. if (!ndev)
  844. return -ENOMEM;
  845. ndev->pdev = pdev;
  846. ndev->link_status = NTB_LINK_DOWN;
  847. pci_set_drvdata(pdev, ndev);
  848. rc = pci_enable_device(pdev);
  849. if (rc)
  850. goto err;
  851. pci_set_master(ndev->pdev);
  852. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
  853. if (rc)
  854. goto err1;
  855. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  856. if (!ndev->reg_base) {
  857. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  858. rc = -EIO;
  859. goto err2;
  860. }
  861. for (i = 0; i < NTB_NUM_MW; i++) {
  862. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  863. ndev->mw[i].vbase =
  864. ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
  865. ndev->mw[i].bar_sz);
  866. dev_info(&pdev->dev, "MW %d size %d\n", i,
  867. (u32) pci_resource_len(pdev, MW_TO_BAR(i)));
  868. if (!ndev->mw[i].vbase) {
  869. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  870. MW_TO_BAR(i));
  871. rc = -EIO;
  872. goto err3;
  873. }
  874. }
  875. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  876. if (rc) {
  877. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  878. if (rc)
  879. goto err3;
  880. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  881. }
  882. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  883. if (rc) {
  884. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  885. if (rc)
  886. goto err3;
  887. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  888. }
  889. rc = ntb_device_setup(ndev);
  890. if (rc)
  891. goto err3;
  892. rc = ntb_create_callbacks(ndev);
  893. if (rc)
  894. goto err4;
  895. rc = ntb_setup_interrupts(ndev);
  896. if (rc)
  897. goto err5;
  898. /* The scratchpad registers keep the values between rmmod/insmod,
  899. * blast them now
  900. */
  901. for (i = 0; i < ndev->limits.max_spads; i++) {
  902. ntb_write_local_spad(ndev, i, 0);
  903. ntb_write_remote_spad(ndev, i, 0);
  904. }
  905. rc = ntb_transport_init(pdev);
  906. if (rc)
  907. goto err6;
  908. /* Let's bring the NTB link up */
  909. writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
  910. ndev->reg_ofs.lnk_cntl);
  911. return 0;
  912. err6:
  913. ntb_free_interrupts(ndev);
  914. err5:
  915. ntb_free_callbacks(ndev);
  916. err4:
  917. ntb_device_free(ndev);
  918. err3:
  919. for (i--; i >= 0; i--)
  920. iounmap(ndev->mw[i].vbase);
  921. iounmap(ndev->reg_base);
  922. err2:
  923. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  924. err1:
  925. pci_disable_device(pdev);
  926. err:
  927. kfree(ndev);
  928. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  929. return rc;
  930. }
  931. static void ntb_pci_remove(struct pci_dev *pdev)
  932. {
  933. struct ntb_device *ndev = pci_get_drvdata(pdev);
  934. int i;
  935. u32 ntb_cntl;
  936. /* Bring NTB link down */
  937. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  938. ntb_cntl |= NTB_LINK_DISABLE;
  939. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  940. ntb_transport_free(ndev->ntb_transport);
  941. ntb_free_interrupts(ndev);
  942. ntb_free_callbacks(ndev);
  943. ntb_device_free(ndev);
  944. for (i = 0; i < NTB_NUM_MW; i++)
  945. iounmap(ndev->mw[i].vbase);
  946. iounmap(ndev->reg_base);
  947. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  948. pci_disable_device(pdev);
  949. kfree(ndev);
  950. }
  951. static struct pci_driver ntb_pci_driver = {
  952. .name = KBUILD_MODNAME,
  953. .id_table = ntb_pci_tbl,
  954. .probe = ntb_pci_probe,
  955. .remove = ntb_pci_remove,
  956. };
  957. module_pci_driver(ntb_pci_driver);