ntb_hw.c 29 KB

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