ntb_hw.c 29 KB

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