ntb_hw.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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_USD;
  464. else
  465. ndev->dev_type = NTB_DEV_DSD;
  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_B2B_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. if (rc)
  559. return rc;
  560. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  561. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  562. /* Enable Bus Master and Memory Space on the secondary side */
  563. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
  564. return 0;
  565. }
  566. static void ntb_device_free(struct ntb_device *ndev)
  567. {
  568. if (ndev->hw_type == BWD_HW)
  569. cancel_delayed_work_sync(&ndev->hb_timer);
  570. }
  571. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  572. {
  573. struct ntb_db_cb *db_cb = data;
  574. struct ntb_device *ndev = db_cb->ndev;
  575. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  576. db_cb->db_num);
  577. if (db_cb->callback)
  578. db_cb->callback(db_cb->data, db_cb->db_num);
  579. /* No need to check for the specific HB irq, any interrupt means
  580. * we're connected.
  581. */
  582. ndev->last_ts = jiffies;
  583. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
  584. return IRQ_HANDLED;
  585. }
  586. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  587. {
  588. struct ntb_db_cb *db_cb = data;
  589. struct ntb_device *ndev = db_cb->ndev;
  590. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  591. db_cb->db_num);
  592. if (db_cb->callback)
  593. db_cb->callback(db_cb->data, db_cb->db_num);
  594. /* On Sandybridge, there are 16 bits in the interrupt register
  595. * but only 4 vectors. So, 5 bits are assigned to the first 3
  596. * vectors, with the 4th having a single bit for link
  597. * interrupts.
  598. */
  599. writew(((1 << ndev->bits_per_vector) - 1) <<
  600. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
  601. return IRQ_HANDLED;
  602. }
  603. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  604. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  605. {
  606. struct ntb_device *ndev = dev;
  607. int rc;
  608. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  609. rc = ntb_link_status(ndev);
  610. if (rc)
  611. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  612. /* bit 15 is always the link bit */
  613. writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
  614. return IRQ_HANDLED;
  615. }
  616. static irqreturn_t ntb_interrupt(int irq, void *dev)
  617. {
  618. struct ntb_device *ndev = dev;
  619. unsigned int i = 0;
  620. if (ndev->hw_type == BWD_HW) {
  621. u64 pdb = readq(ndev->reg_ofs.pdb);
  622. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
  623. while (pdb) {
  624. i = __ffs(pdb);
  625. pdb &= pdb - 1;
  626. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  627. }
  628. } else {
  629. u16 pdb = readw(ndev->reg_ofs.pdb);
  630. dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
  631. pdb, readw(ndev->reg_ofs.sdb));
  632. if (pdb & SNB_DB_HW_LINK) {
  633. xeon_event_msix_irq(irq, dev);
  634. pdb &= ~SNB_DB_HW_LINK;
  635. }
  636. while (pdb) {
  637. i = __ffs(pdb);
  638. pdb &= pdb - 1;
  639. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  640. }
  641. }
  642. return IRQ_HANDLED;
  643. }
  644. static int ntb_setup_msix(struct ntb_device *ndev)
  645. {
  646. struct pci_dev *pdev = ndev->pdev;
  647. struct msix_entry *msix;
  648. int msix_entries;
  649. int rc, i, pos;
  650. u16 val;
  651. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  652. if (!pos) {
  653. rc = -EIO;
  654. goto err;
  655. }
  656. rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
  657. if (rc)
  658. goto err;
  659. msix_entries = msix_table_size(val);
  660. if (msix_entries > ndev->limits.msix_cnt) {
  661. rc = -EINVAL;
  662. goto err;
  663. }
  664. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  665. GFP_KERNEL);
  666. if (!ndev->msix_entries) {
  667. rc = -ENOMEM;
  668. goto err;
  669. }
  670. for (i = 0; i < msix_entries; i++)
  671. ndev->msix_entries[i].entry = i;
  672. rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
  673. if (rc < 0)
  674. goto err1;
  675. if (rc > 0) {
  676. /* On SNB, the link interrupt is always tied to 4th vector. If
  677. * we can't get all 4, then we can't use MSI-X.
  678. */
  679. if (ndev->hw_type != BWD_HW) {
  680. rc = -EIO;
  681. goto err1;
  682. }
  683. dev_warn(&pdev->dev,
  684. "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
  685. rc);
  686. msix_entries = rc;
  687. }
  688. for (i = 0; i < msix_entries; i++) {
  689. msix = &ndev->msix_entries[i];
  690. WARN_ON(!msix->vector);
  691. /* Use the last MSI-X vector for Link status */
  692. if (ndev->hw_type == BWD_HW) {
  693. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  694. "ntb-callback-msix", &ndev->db_cb[i]);
  695. if (rc)
  696. goto err2;
  697. } else {
  698. if (i == msix_entries - 1) {
  699. rc = request_irq(msix->vector,
  700. xeon_event_msix_irq, 0,
  701. "ntb-event-msix", ndev);
  702. if (rc)
  703. goto err2;
  704. } else {
  705. rc = request_irq(msix->vector,
  706. xeon_callback_msix_irq, 0,
  707. "ntb-callback-msix",
  708. &ndev->db_cb[i]);
  709. if (rc)
  710. goto err2;
  711. }
  712. }
  713. }
  714. ndev->num_msix = msix_entries;
  715. if (ndev->hw_type == BWD_HW)
  716. ndev->max_cbs = msix_entries;
  717. else
  718. ndev->max_cbs = msix_entries - 1;
  719. return 0;
  720. err2:
  721. while (--i >= 0) {
  722. msix = &ndev->msix_entries[i];
  723. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  724. free_irq(msix->vector, ndev);
  725. else
  726. free_irq(msix->vector, &ndev->db_cb[i]);
  727. }
  728. pci_disable_msix(pdev);
  729. err1:
  730. kfree(ndev->msix_entries);
  731. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  732. err:
  733. ndev->num_msix = 0;
  734. return rc;
  735. }
  736. static int ntb_setup_msi(struct ntb_device *ndev)
  737. {
  738. struct pci_dev *pdev = ndev->pdev;
  739. int rc;
  740. rc = pci_enable_msi(pdev);
  741. if (rc)
  742. return rc;
  743. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  744. if (rc) {
  745. pci_disable_msi(pdev);
  746. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  747. return rc;
  748. }
  749. return 0;
  750. }
  751. static int ntb_setup_intx(struct ntb_device *ndev)
  752. {
  753. struct pci_dev *pdev = ndev->pdev;
  754. int rc;
  755. pci_msi_off(pdev);
  756. /* Verify intx is enabled */
  757. pci_intx(pdev, 1);
  758. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  759. ndev);
  760. if (rc)
  761. return rc;
  762. return 0;
  763. }
  764. static int ntb_setup_interrupts(struct ntb_device *ndev)
  765. {
  766. int rc;
  767. /* On BWD, disable all interrupts. On SNB, disable all but Link
  768. * Interrupt. The rest will be unmasked as callbacks are registered.
  769. */
  770. if (ndev->hw_type == BWD_HW)
  771. writeq(~0, ndev->reg_ofs.pdb_mask);
  772. else
  773. writew(~(1 << ndev->limits.max_db_bits),
  774. ndev->reg_ofs.pdb_mask);
  775. rc = ntb_setup_msix(ndev);
  776. if (!rc)
  777. goto done;
  778. ndev->bits_per_vector = 1;
  779. ndev->max_cbs = ndev->limits.max_db_bits;
  780. rc = ntb_setup_msi(ndev);
  781. if (!rc)
  782. goto done;
  783. rc = ntb_setup_intx(ndev);
  784. if (rc) {
  785. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  786. return rc;
  787. }
  788. done:
  789. return 0;
  790. }
  791. static void ntb_free_interrupts(struct ntb_device *ndev)
  792. {
  793. struct pci_dev *pdev = ndev->pdev;
  794. /* mask interrupts */
  795. if (ndev->hw_type == BWD_HW)
  796. writeq(~0, ndev->reg_ofs.pdb_mask);
  797. else
  798. writew(~0, ndev->reg_ofs.pdb_mask);
  799. if (ndev->num_msix) {
  800. struct msix_entry *msix;
  801. u32 i;
  802. for (i = 0; i < ndev->num_msix; i++) {
  803. msix = &ndev->msix_entries[i];
  804. if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
  805. free_irq(msix->vector, ndev);
  806. else
  807. free_irq(msix->vector, &ndev->db_cb[i]);
  808. }
  809. pci_disable_msix(pdev);
  810. } else {
  811. free_irq(pdev->irq, ndev);
  812. if (pci_dev_msi_enabled(pdev))
  813. pci_disable_msi(pdev);
  814. }
  815. }
  816. static int ntb_create_callbacks(struct ntb_device *ndev)
  817. {
  818. int i;
  819. /* Checken-egg issue. We won't know how many callbacks are necessary
  820. * until we see how many MSI-X vectors we get, but these pointers need
  821. * to be passed into the MSI-X register fucntion. So, we allocate the
  822. * max, knowing that they might not all be used, to work around this.
  823. */
  824. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  825. sizeof(struct ntb_db_cb),
  826. GFP_KERNEL);
  827. if (!ndev->db_cb)
  828. return -ENOMEM;
  829. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  830. ndev->db_cb[i].db_num = i;
  831. ndev->db_cb[i].ndev = ndev;
  832. }
  833. return 0;
  834. }
  835. static void ntb_free_callbacks(struct ntb_device *ndev)
  836. {
  837. int i;
  838. for (i = 0; i < ndev->limits.max_db_bits; i++)
  839. ntb_unregister_db_callback(ndev, i);
  840. kfree(ndev->db_cb);
  841. }
  842. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  843. {
  844. struct ntb_device *ndev;
  845. int rc, i;
  846. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  847. if (!ndev)
  848. return -ENOMEM;
  849. ndev->pdev = pdev;
  850. ndev->link_status = NTB_LINK_DOWN;
  851. pci_set_drvdata(pdev, ndev);
  852. rc = pci_enable_device(pdev);
  853. if (rc)
  854. goto err;
  855. pci_set_master(ndev->pdev);
  856. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
  857. if (rc)
  858. goto err1;
  859. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  860. if (!ndev->reg_base) {
  861. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  862. rc = -EIO;
  863. goto err2;
  864. }
  865. for (i = 0; i < NTB_NUM_MW; i++) {
  866. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  867. ndev->mw[i].vbase =
  868. ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
  869. ndev->mw[i].bar_sz);
  870. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  871. pci_resource_len(pdev, MW_TO_BAR(i)));
  872. if (!ndev->mw[i].vbase) {
  873. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  874. MW_TO_BAR(i));
  875. rc = -EIO;
  876. goto err3;
  877. }
  878. }
  879. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  880. if (rc) {
  881. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  882. if (rc)
  883. goto err3;
  884. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  885. }
  886. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  887. if (rc) {
  888. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  889. if (rc)
  890. goto err3;
  891. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  892. }
  893. rc = ntb_device_setup(ndev);
  894. if (rc)
  895. goto err3;
  896. rc = ntb_create_callbacks(ndev);
  897. if (rc)
  898. goto err4;
  899. rc = ntb_setup_interrupts(ndev);
  900. if (rc)
  901. goto err5;
  902. /* The scratchpad registers keep the values between rmmod/insmod,
  903. * blast them now
  904. */
  905. for (i = 0; i < ndev->limits.max_spads; i++) {
  906. ntb_write_local_spad(ndev, i, 0);
  907. ntb_write_remote_spad(ndev, i, 0);
  908. }
  909. rc = ntb_transport_init(pdev);
  910. if (rc)
  911. goto err6;
  912. /* Let's bring the NTB link up */
  913. writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
  914. ndev->reg_ofs.lnk_cntl);
  915. return 0;
  916. err6:
  917. ntb_free_interrupts(ndev);
  918. err5:
  919. ntb_free_callbacks(ndev);
  920. err4:
  921. ntb_device_free(ndev);
  922. err3:
  923. for (i--; i >= 0; i--)
  924. iounmap(ndev->mw[i].vbase);
  925. iounmap(ndev->reg_base);
  926. err2:
  927. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  928. err1:
  929. pci_disable_device(pdev);
  930. err:
  931. kfree(ndev);
  932. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  933. return rc;
  934. }
  935. static void ntb_pci_remove(struct pci_dev *pdev)
  936. {
  937. struct ntb_device *ndev = pci_get_drvdata(pdev);
  938. int i;
  939. u32 ntb_cntl;
  940. /* Bring NTB link down */
  941. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  942. ntb_cntl |= NTB_LINK_DISABLE;
  943. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  944. ntb_transport_free(ndev->ntb_transport);
  945. ntb_free_interrupts(ndev);
  946. ntb_free_callbacks(ndev);
  947. ntb_device_free(ndev);
  948. for (i = 0; i < NTB_NUM_MW; i++)
  949. iounmap(ndev->mw[i].vbase);
  950. iounmap(ndev->reg_base);
  951. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  952. pci_disable_device(pdev);
  953. kfree(ndev);
  954. }
  955. static struct pci_driver ntb_pci_driver = {
  956. .name = KBUILD_MODNAME,
  957. .id_table = ntb_pci_tbl,
  958. .probe = ntb_pci_probe,
  959. .remove = ntb_pci_remove,
  960. };
  961. module_pci_driver(ntb_pci_driver);