ntb_hw.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * BSD LICENSE
  14. *
  15. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copy
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * * Neither the name of Intel Corporation nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * Intel PCIe NTB Linux driver
  44. *
  45. * Contact Information:
  46. * Jon Mason <jon.mason@intel.com>
  47. */
  48. #include <linux/debugfs.h>
  49. #include <linux/delay.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/module.h>
  53. #include <linux/pci.h>
  54. #include <linux/random.h>
  55. #include <linux/slab.h>
  56. #include "ntb_hw.h"
  57. #include "ntb_regs.h"
  58. #define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
  59. #define NTB_VER "1.0"
  60. MODULE_DESCRIPTION(NTB_NAME);
  61. MODULE_VERSION(NTB_VER);
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. MODULE_AUTHOR("Intel Corporation");
  64. enum {
  65. NTB_CONN_TRANSPARENT = 0,
  66. NTB_CONN_B2B,
  67. NTB_CONN_RP,
  68. };
  69. enum {
  70. NTB_DEV_USD = 0,
  71. NTB_DEV_DSD,
  72. };
  73. enum {
  74. SNB_HW = 0,
  75. BWD_HW,
  76. };
  77. static struct dentry *debugfs_dir;
  78. #define BWD_LINK_RECOVERY_TIME 500
  79. /* Translate memory window 0,1,2 to BAR 2,4,5 */
  80. #define MW_TO_BAR(mw) (mw == 0 ? 2 : (mw == 1 ? 4 : 5))
  81. static const struct pci_device_id ntb_pci_tbl[] = {
  82. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
  83. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
  84. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
  85. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
  86. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
  87. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
  88. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
  89. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
  90. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
  91. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
  92. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
  93. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
  94. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
  95. {0}
  96. };
  97. MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
  98. static int is_ntb_xeon(struct ntb_device *ndev)
  99. {
  100. switch (ndev->pdev->device) {
  101. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  102. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  103. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  104. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  105. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  106. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  107. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  108. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  109. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  110. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  111. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  112. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  113. return 1;
  114. default:
  115. return 0;
  116. }
  117. return 0;
  118. }
  119. static int is_ntb_atom(struct ntb_device *ndev)
  120. {
  121. switch (ndev->pdev->device) {
  122. case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
  123. return 1;
  124. default:
  125. return 0;
  126. }
  127. return 0;
  128. }
  129. static void ntb_set_errata_flags(struct ntb_device *ndev)
  130. {
  131. switch (ndev->pdev->device) {
  132. /*
  133. * this workaround applies to all platform up to IvyBridge
  134. * Haswell has splitbar support and use a different workaround
  135. */
  136. case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
  137. case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
  138. case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
  139. case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
  140. case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
  141. case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
  142. case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
  143. case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
  144. case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
  145. case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
  146. case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
  147. case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
  148. ndev->wa_flags |= WA_SNB_ERR;
  149. break;
  150. }
  151. }
  152. /**
  153. * ntb_register_event_callback() - register event callback
  154. * @ndev: pointer to ntb_device instance
  155. * @func: callback function to register
  156. *
  157. * This function registers a callback for any HW driver events such as link
  158. * up/down, power management notices and etc.
  159. *
  160. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  161. */
  162. int ntb_register_event_callback(struct ntb_device *ndev,
  163. void (*func)(void *handle,
  164. enum ntb_hw_event event))
  165. {
  166. if (ndev->event_cb)
  167. return -EINVAL;
  168. ndev->event_cb = func;
  169. return 0;
  170. }
  171. /**
  172. * ntb_unregister_event_callback() - unregisters the event callback
  173. * @ndev: pointer to ntb_device instance
  174. *
  175. * This function unregisters the existing callback from transport
  176. */
  177. void ntb_unregister_event_callback(struct ntb_device *ndev)
  178. {
  179. ndev->event_cb = NULL;
  180. }
  181. static void ntb_irq_work(unsigned long data)
  182. {
  183. struct ntb_db_cb *db_cb = (struct ntb_db_cb *)data;
  184. int rc;
  185. rc = db_cb->callback(db_cb->data, db_cb->db_num);
  186. if (rc)
  187. tasklet_schedule(&db_cb->irq_work);
  188. else {
  189. struct ntb_device *ndev = db_cb->ndev;
  190. unsigned long mask;
  191. mask = readw(ndev->reg_ofs.ldb_mask);
  192. clear_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  193. writew(mask, ndev->reg_ofs.ldb_mask);
  194. }
  195. }
  196. /**
  197. * ntb_register_db_callback() - register a callback for doorbell interrupt
  198. * @ndev: pointer to ntb_device instance
  199. * @idx: doorbell index to register callback, zero based
  200. * @data: pointer to be returned to caller with every callback
  201. * @func: callback function to register
  202. *
  203. * This function registers a callback function for the doorbell interrupt
  204. * on the primary side. The function will unmask the doorbell as well to
  205. * allow interrupt.
  206. *
  207. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  208. */
  209. int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
  210. void *data, int (*func)(void *data, int db_num))
  211. {
  212. unsigned long mask;
  213. if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
  214. dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
  215. return -EINVAL;
  216. }
  217. ndev->db_cb[idx].callback = func;
  218. ndev->db_cb[idx].data = data;
  219. ndev->db_cb[idx].ndev = ndev;
  220. tasklet_init(&ndev->db_cb[idx].irq_work, ntb_irq_work,
  221. (unsigned long) &ndev->db_cb[idx]);
  222. /* unmask interrupt */
  223. mask = readw(ndev->reg_ofs.ldb_mask);
  224. clear_bit(idx * ndev->bits_per_vector, &mask);
  225. writew(mask, ndev->reg_ofs.ldb_mask);
  226. return 0;
  227. }
  228. /**
  229. * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
  230. * @ndev: pointer to ntb_device instance
  231. * @idx: doorbell index to register callback, zero based
  232. *
  233. * This function unregisters a callback function for the doorbell interrupt
  234. * on the primary side. The function will also mask the said doorbell.
  235. */
  236. void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
  237. {
  238. unsigned long mask;
  239. if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
  240. return;
  241. mask = readw(ndev->reg_ofs.ldb_mask);
  242. set_bit(idx * ndev->bits_per_vector, &mask);
  243. writew(mask, ndev->reg_ofs.ldb_mask);
  244. tasklet_disable(&ndev->db_cb[idx].irq_work);
  245. ndev->db_cb[idx].callback = NULL;
  246. }
  247. /**
  248. * ntb_find_transport() - find the transport pointer
  249. * @transport: pointer to pci device
  250. *
  251. * Given the pci device pointer, return the transport pointer passed in when
  252. * the transport attached when it was inited.
  253. *
  254. * RETURNS: pointer to transport.
  255. */
  256. void *ntb_find_transport(struct pci_dev *pdev)
  257. {
  258. struct ntb_device *ndev = pci_get_drvdata(pdev);
  259. return ndev->ntb_transport;
  260. }
  261. /**
  262. * ntb_register_transport() - Register NTB transport with NTB HW driver
  263. * @transport: transport identifier
  264. *
  265. * This function allows a transport to reserve the hardware driver for
  266. * NTB usage.
  267. *
  268. * RETURNS: pointer to ntb_device, NULL on error.
  269. */
  270. struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
  271. {
  272. struct ntb_device *ndev = pci_get_drvdata(pdev);
  273. if (ndev->ntb_transport)
  274. return NULL;
  275. ndev->ntb_transport = transport;
  276. return ndev;
  277. }
  278. /**
  279. * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
  280. * @ndev - ntb_device of the transport to be freed
  281. *
  282. * This function unregisters the transport from the HW driver and performs any
  283. * necessary cleanups.
  284. */
  285. void ntb_unregister_transport(struct ntb_device *ndev)
  286. {
  287. int i;
  288. if (!ndev->ntb_transport)
  289. return;
  290. for (i = 0; i < ndev->max_cbs; i++)
  291. ntb_unregister_db_callback(ndev, i);
  292. ntb_unregister_event_callback(ndev);
  293. ndev->ntb_transport = NULL;
  294. }
  295. /**
  296. * ntb_write_local_spad() - write to the secondary scratchpad register
  297. * @ndev: pointer to ntb_device instance
  298. * @idx: index to the scratchpad register, 0 based
  299. * @val: the data value to put into the register
  300. *
  301. * This function allows writing of a 32bit value to the indexed scratchpad
  302. * register. This writes over the data mirrored to the local scratchpad register
  303. * by the remote system.
  304. *
  305. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  306. */
  307. int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  308. {
  309. if (idx >= ndev->limits.max_spads)
  310. return -EINVAL;
  311. dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
  312. val, idx);
  313. writel(val, ndev->reg_ofs.spad_read + idx * 4);
  314. return 0;
  315. }
  316. /**
  317. * ntb_read_local_spad() - read from the primary scratchpad register
  318. * @ndev: pointer to ntb_device instance
  319. * @idx: index to scratchpad register, 0 based
  320. * @val: pointer to 32bit integer for storing the register value
  321. *
  322. * This function allows reading of the 32bit scratchpad register on
  323. * the primary (internal) side. This allows the local system to read data
  324. * written and mirrored to the scratchpad register by the remote system.
  325. *
  326. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  327. */
  328. int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  329. {
  330. if (idx >= ndev->limits.max_spads)
  331. return -EINVAL;
  332. *val = readl(ndev->reg_ofs.spad_write + idx * 4);
  333. dev_dbg(&ndev->pdev->dev,
  334. "Reading %x from local scratch pad index %d\n", *val, idx);
  335. return 0;
  336. }
  337. /**
  338. * ntb_write_remote_spad() - write to the secondary scratchpad register
  339. * @ndev: pointer to ntb_device instance
  340. * @idx: index to the scratchpad register, 0 based
  341. * @val: the data value to put into the register
  342. *
  343. * This function allows writing of a 32bit value to the indexed scratchpad
  344. * register. The register resides on the secondary (external) side. This allows
  345. * the local system to write data to be mirrored to the remote systems
  346. * scratchpad register.
  347. *
  348. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  349. */
  350. int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
  351. {
  352. if (idx >= ndev->limits.max_spads)
  353. return -EINVAL;
  354. dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
  355. val, idx);
  356. writel(val, ndev->reg_ofs.spad_write + idx * 4);
  357. return 0;
  358. }
  359. /**
  360. * ntb_read_remote_spad() - read from the primary scratchpad register
  361. * @ndev: pointer to ntb_device instance
  362. * @idx: index to scratchpad register, 0 based
  363. * @val: pointer to 32bit integer for storing the register value
  364. *
  365. * This function allows reading of the 32bit scratchpad register on
  366. * the primary (internal) side. This alloows the local system to read the data
  367. * it wrote to be mirrored on the remote system.
  368. *
  369. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  370. */
  371. int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
  372. {
  373. if (idx >= ndev->limits.max_spads)
  374. return -EINVAL;
  375. *val = readl(ndev->reg_ofs.spad_read + idx * 4);
  376. dev_dbg(&ndev->pdev->dev,
  377. "Reading %x from remote scratch pad index %d\n", *val, idx);
  378. return 0;
  379. }
  380. /**
  381. * ntb_get_mw_base() - get addr for the NTB memory window
  382. * @ndev: pointer to ntb_device instance
  383. * @mw: memory window number
  384. *
  385. * This function provides the base address of the memory window specified.
  386. *
  387. * RETURNS: address, or NULL on error.
  388. */
  389. resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw)
  390. {
  391. if (mw >= ntb_max_mw(ndev))
  392. return 0;
  393. return pci_resource_start(ndev->pdev, MW_TO_BAR(mw));
  394. }
  395. /**
  396. * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
  397. * @ndev: pointer to ntb_device instance
  398. * @mw: memory window number
  399. *
  400. * This function provides the base virtual address of the memory window
  401. * specified.
  402. *
  403. * RETURNS: pointer to virtual address, or NULL on error.
  404. */
  405. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
  406. {
  407. if (mw >= ntb_max_mw(ndev))
  408. return NULL;
  409. return ndev->mw[mw].vbase;
  410. }
  411. /**
  412. * ntb_get_mw_size() - return size of NTB memory window
  413. * @ndev: pointer to ntb_device instance
  414. * @mw: memory window number
  415. *
  416. * This function provides the physical size of the memory window specified
  417. *
  418. * RETURNS: the size of the memory window or zero on error
  419. */
  420. u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
  421. {
  422. if (mw >= ntb_max_mw(ndev))
  423. return 0;
  424. return ndev->mw[mw].bar_sz;
  425. }
  426. /**
  427. * ntb_set_mw_addr - set the memory window address
  428. * @ndev: pointer to ntb_device instance
  429. * @mw: memory window number
  430. * @addr: base address for data
  431. *
  432. * This function sets the base physical address of the memory window. This
  433. * memory address is where data from the remote system will be transfered into
  434. * or out of depending on how the transport is configured.
  435. */
  436. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
  437. {
  438. if (mw >= ntb_max_mw(ndev))
  439. return;
  440. dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
  441. MW_TO_BAR(mw));
  442. ndev->mw[mw].phys_addr = addr;
  443. switch (MW_TO_BAR(mw)) {
  444. case NTB_BAR_23:
  445. writeq(addr, ndev->reg_ofs.bar2_xlat);
  446. break;
  447. case NTB_BAR_4:
  448. if (ndev->split_bar)
  449. writel(addr, ndev->reg_ofs.bar4_xlat);
  450. else
  451. writeq(addr, ndev->reg_ofs.bar4_xlat);
  452. break;
  453. case NTB_BAR_5:
  454. writel(addr, ndev->reg_ofs.bar5_xlat);
  455. break;
  456. }
  457. }
  458. /**
  459. * ntb_ring_doorbell() - Set the doorbell on the secondary/external side
  460. * @ndev: pointer to ntb_device instance
  461. * @db: doorbell to ring
  462. *
  463. * This function allows triggering of a doorbell on the secondary/external
  464. * side that will initiate an interrupt on the remote host
  465. *
  466. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  467. */
  468. void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int db)
  469. {
  470. dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
  471. if (ndev->hw_type == BWD_HW)
  472. writeq((u64) 1 << db, ndev->reg_ofs.rdb);
  473. else
  474. writew(((1 << ndev->bits_per_vector) - 1) <<
  475. (db * ndev->bits_per_vector), ndev->reg_ofs.rdb);
  476. }
  477. static void bwd_recover_link(struct ntb_device *ndev)
  478. {
  479. u32 status;
  480. /* Driver resets the NTB ModPhy lanes - magic! */
  481. writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
  482. writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
  483. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
  484. writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
  485. /* Driver waits 100ms to allow the NTB ModPhy to settle */
  486. msleep(100);
  487. /* Clear AER Errors, write to clear */
  488. status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  489. dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
  490. status &= PCI_ERR_COR_REP_ROLL;
  491. writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
  492. /* Clear unexpected electrical idle event in LTSSM, write to clear */
  493. status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  494. dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
  495. status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
  496. writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
  497. /* Clear DeSkew Buffer error, write to clear */
  498. status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  499. dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
  500. status |= BWD_DESKEWSTS_DBERR;
  501. writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
  502. status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  503. dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
  504. status &= BWD_IBIST_ERR_OFLOW;
  505. writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  506. /* Releases the NTB state machine to allow the link to retrain */
  507. status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  508. dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
  509. status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
  510. writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  511. }
  512. static void ntb_link_event(struct ntb_device *ndev, int link_state)
  513. {
  514. unsigned int event;
  515. if (ndev->link_status == link_state)
  516. return;
  517. if (link_state == NTB_LINK_UP) {
  518. u16 status;
  519. dev_info(&ndev->pdev->dev, "Link Up\n");
  520. ndev->link_status = NTB_LINK_UP;
  521. event = NTB_EVENT_HW_LINK_UP;
  522. if (is_ntb_atom(ndev) ||
  523. ndev->conn_type == NTB_CONN_TRANSPARENT)
  524. status = readw(ndev->reg_ofs.lnk_stat);
  525. else {
  526. int rc = pci_read_config_word(ndev->pdev,
  527. SNB_LINK_STATUS_OFFSET,
  528. &status);
  529. if (rc)
  530. return;
  531. }
  532. ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
  533. ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
  534. dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
  535. ndev->link_width, ndev->link_speed);
  536. } else {
  537. dev_info(&ndev->pdev->dev, "Link Down\n");
  538. ndev->link_status = NTB_LINK_DOWN;
  539. event = NTB_EVENT_HW_LINK_DOWN;
  540. /* Don't modify link width/speed, we need it in link recovery */
  541. }
  542. /* notify the upper layer if we have an event change */
  543. if (ndev->event_cb)
  544. ndev->event_cb(ndev->ntb_transport, event);
  545. }
  546. static int ntb_link_status(struct ntb_device *ndev)
  547. {
  548. int link_state;
  549. if (is_ntb_atom(ndev)) {
  550. u32 ntb_cntl;
  551. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  552. if (ntb_cntl & BWD_CNTL_LINK_DOWN)
  553. link_state = NTB_LINK_DOWN;
  554. else
  555. link_state = NTB_LINK_UP;
  556. } else {
  557. u16 status;
  558. int rc;
  559. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  560. &status);
  561. if (rc)
  562. return rc;
  563. if (status & NTB_LINK_STATUS_ACTIVE)
  564. link_state = NTB_LINK_UP;
  565. else
  566. link_state = NTB_LINK_DOWN;
  567. }
  568. ntb_link_event(ndev, link_state);
  569. return 0;
  570. }
  571. static void bwd_link_recovery(struct work_struct *work)
  572. {
  573. struct ntb_device *ndev = container_of(work, struct ntb_device,
  574. lr_timer.work);
  575. u32 status32;
  576. bwd_recover_link(ndev);
  577. /* There is a potential race between the 2 NTB devices recovering at the
  578. * same time. If the times are the same, the link will not recover and
  579. * the driver will be stuck in this loop forever. Add a random interval
  580. * to the recovery time to prevent this race.
  581. */
  582. msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
  583. status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
  584. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
  585. goto retry;
  586. status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
  587. if (status32 & BWD_IBIST_ERR_OFLOW)
  588. goto retry;
  589. status32 = readl(ndev->reg_ofs.lnk_cntl);
  590. if (!(status32 & BWD_CNTL_LINK_DOWN)) {
  591. unsigned char speed, width;
  592. u16 status16;
  593. status16 = readw(ndev->reg_ofs.lnk_stat);
  594. width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
  595. speed = (status16 & NTB_LINK_SPEED_MASK);
  596. if (ndev->link_width != width || ndev->link_speed != speed)
  597. goto retry;
  598. }
  599. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  600. return;
  601. retry:
  602. schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
  603. }
  604. /* BWD doesn't have link status interrupt, poll on that platform */
  605. static void bwd_link_poll(struct work_struct *work)
  606. {
  607. struct ntb_device *ndev = container_of(work, struct ntb_device,
  608. hb_timer.work);
  609. unsigned long ts = jiffies;
  610. /* If we haven't gotten an interrupt in a while, check the BWD link
  611. * status bit
  612. */
  613. if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
  614. int rc = ntb_link_status(ndev);
  615. if (rc)
  616. dev_err(&ndev->pdev->dev,
  617. "Error determining link status\n");
  618. /* Check to see if a link error is the cause of the link down */
  619. if (ndev->link_status == NTB_LINK_DOWN) {
  620. u32 status32 = readl(ndev->reg_base +
  621. BWD_LTSSMSTATEJMP_OFFSET);
  622. if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
  623. schedule_delayed_work(&ndev->lr_timer, 0);
  624. return;
  625. }
  626. }
  627. }
  628. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  629. }
  630. static int ntb_xeon_setup(struct ntb_device *ndev)
  631. {
  632. switch (ndev->conn_type) {
  633. case NTB_CONN_B2B:
  634. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  635. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  636. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  637. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  638. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  639. if (ndev->split_bar)
  640. ndev->reg_ofs.bar5_xlat =
  641. ndev->reg_base + SNB_SBAR5XLAT_OFFSET;
  642. ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
  643. /* There is a Xeon hardware errata related to writes to
  644. * SDOORBELL or B2BDOORBELL in conjunction with inbound access
  645. * to NTB MMIO Space, which may hang the system. To workaround
  646. * this use the second memory window to access the interrupt and
  647. * scratch pad registers on the remote system.
  648. */
  649. if (ndev->wa_flags & WA_SNB_ERR) {
  650. if (!ndev->mw[ndev->limits.max_mw - 1].bar_sz)
  651. return -EINVAL;
  652. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  653. ndev->reg_ofs.spad_write =
  654. ndev->mw[ndev->limits.max_mw - 1].vbase +
  655. SNB_SPAD_OFFSET;
  656. ndev->reg_ofs.rdb =
  657. ndev->mw[ndev->limits.max_mw - 1].vbase +
  658. SNB_PDOORBELL_OFFSET;
  659. /* Set the Limit register to 4k, the minimum size, to
  660. * prevent an illegal access
  661. */
  662. writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
  663. SNB_PBAR4LMT_OFFSET);
  664. /* HW errata on the Limit registers. They can only be
  665. * written when the base register is 4GB aligned and
  666. * < 32bit. This should already be the case based on
  667. * the driver defaults, but write the Limit registers
  668. * first just in case.
  669. */
  670. ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
  671. } else {
  672. /* HW Errata on bit 14 of b2bdoorbell register. Writes
  673. * will not be mirrored to the remote system. Shrink
  674. * the number of bits by one, since bit 14 is the last
  675. * bit.
  676. */
  677. ndev->limits.max_db_bits = SNB_MAX_DB_BITS - 1;
  678. ndev->reg_ofs.spad_write = ndev->reg_base +
  679. SNB_B2B_SPAD_OFFSET;
  680. ndev->reg_ofs.rdb = ndev->reg_base +
  681. SNB_B2B_DOORBELL_OFFSET;
  682. /* Disable the Limit register, just incase it is set to
  683. * something silly. A 64bit write should handle it
  684. * regardless of whether it has a split BAR or not.
  685. */
  686. writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
  687. /* HW errata on the Limit registers. They can only be
  688. * written when the base register is 4GB aligned and
  689. * < 32bit. This should already be the case based on
  690. * the driver defaults, but write the Limit registers
  691. * first just in case.
  692. */
  693. if (ndev->split_bar)
  694. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  695. else
  696. ndev->limits.max_mw = SNB_MAX_MW;
  697. }
  698. /* The Xeon errata workaround requires setting SBAR Base
  699. * addresses to known values, so that the PBAR XLAT can be
  700. * pointed at SBAR0 of the remote system.
  701. */
  702. if (ndev->dev_type == NTB_DEV_USD) {
  703. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  704. SNB_PBAR2XLAT_OFFSET);
  705. if (ndev->wa_flags & WA_SNB_ERR)
  706. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  707. SNB_PBAR4XLAT_OFFSET);
  708. else {
  709. if (ndev->split_bar) {
  710. writel(SNB_MBAR4_DSD_ADDR,
  711. ndev->reg_base +
  712. SNB_PBAR4XLAT_OFFSET);
  713. writel(SNB_MBAR5_DSD_ADDR,
  714. ndev->reg_base +
  715. SNB_PBAR5XLAT_OFFSET);
  716. } else
  717. writeq(SNB_MBAR4_DSD_ADDR,
  718. ndev->reg_base +
  719. SNB_PBAR4XLAT_OFFSET);
  720. /* B2B_XLAT_OFFSET is a 64bit register, but can
  721. * only take 32bit writes
  722. */
  723. writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
  724. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  725. writel(SNB_MBAR01_DSD_ADDR >> 32,
  726. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  727. }
  728. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  729. SNB_SBAR0BASE_OFFSET);
  730. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  731. SNB_SBAR2BASE_OFFSET);
  732. if (ndev->split_bar) {
  733. writel(SNB_MBAR4_USD_ADDR, ndev->reg_base +
  734. SNB_SBAR4BASE_OFFSET);
  735. writel(SNB_MBAR5_USD_ADDR, ndev->reg_base +
  736. SNB_SBAR5BASE_OFFSET);
  737. } else
  738. writeq(SNB_MBAR4_USD_ADDR, ndev->reg_base +
  739. SNB_SBAR4BASE_OFFSET);
  740. } else {
  741. writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
  742. SNB_PBAR2XLAT_OFFSET);
  743. if (ndev->wa_flags & WA_SNB_ERR)
  744. writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
  745. SNB_PBAR4XLAT_OFFSET);
  746. else {
  747. if (ndev->split_bar) {
  748. writel(SNB_MBAR4_USD_ADDR,
  749. ndev->reg_base +
  750. SNB_PBAR4XLAT_OFFSET);
  751. writel(SNB_MBAR5_USD_ADDR,
  752. ndev->reg_base +
  753. SNB_PBAR5XLAT_OFFSET);
  754. } else
  755. writeq(SNB_MBAR4_USD_ADDR,
  756. ndev->reg_base +
  757. SNB_PBAR4XLAT_OFFSET);
  758. /*
  759. * B2B_XLAT_OFFSET is a 64bit register, but can
  760. * only take 32bit writes
  761. */
  762. writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
  763. ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
  764. writel(SNB_MBAR01_USD_ADDR >> 32,
  765. ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
  766. }
  767. writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
  768. SNB_SBAR0BASE_OFFSET);
  769. writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
  770. SNB_SBAR2BASE_OFFSET);
  771. if (ndev->split_bar) {
  772. writel(SNB_MBAR4_DSD_ADDR, ndev->reg_base +
  773. SNB_SBAR4BASE_OFFSET);
  774. writel(SNB_MBAR5_DSD_ADDR, ndev->reg_base +
  775. SNB_SBAR5BASE_OFFSET);
  776. } else
  777. writeq(SNB_MBAR4_DSD_ADDR, ndev->reg_base +
  778. SNB_SBAR4BASE_OFFSET);
  779. }
  780. break;
  781. case NTB_CONN_RP:
  782. if (ndev->wa_flags & WA_SNB_ERR) {
  783. dev_err(&ndev->pdev->dev,
  784. "NTB-RP disabled due to hardware errata.\n");
  785. return -EINVAL;
  786. }
  787. /* Scratch pads need to have exclusive access from the primary
  788. * or secondary side. Halve the num spads so that each side can
  789. * have an equal amount.
  790. */
  791. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  792. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  793. /* Note: The SDOORBELL is the cause of the errata. You REALLY
  794. * don't want to touch it.
  795. */
  796. ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  797. ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  798. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
  799. /* Offset the start of the spads to correspond to whether it is
  800. * primary or secondary
  801. */
  802. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
  803. ndev->limits.max_spads * 4;
  804. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
  805. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
  806. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
  807. if (ndev->split_bar) {
  808. ndev->reg_ofs.bar5_xlat =
  809. ndev->reg_base + SNB_SBAR5XLAT_OFFSET;
  810. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  811. } else
  812. ndev->limits.max_mw = SNB_MAX_MW;
  813. break;
  814. case NTB_CONN_TRANSPARENT:
  815. if (ndev->wa_flags & WA_SNB_ERR) {
  816. dev_err(&ndev->pdev->dev,
  817. "NTB-TRANSPARENT disabled due to hardware errata.\n");
  818. return -EINVAL;
  819. }
  820. /* Scratch pads need to have exclusive access from the primary
  821. * or secondary side. Halve the num spads so that each side can
  822. * have an equal amount.
  823. */
  824. ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
  825. ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
  826. ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
  827. ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
  828. ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
  829. ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
  830. /* Offset the start of the spads to correspond to whether it is
  831. * primary or secondary
  832. */
  833. ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
  834. ndev->limits.max_spads * 4;
  835. ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
  836. ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
  837. if (ndev->split_bar) {
  838. ndev->reg_ofs.bar5_xlat =
  839. ndev->reg_base + SNB_PBAR5XLAT_OFFSET;
  840. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  841. } else
  842. ndev->limits.max_mw = SNB_MAX_MW;
  843. break;
  844. default:
  845. /*
  846. * we should never hit this. the detect function should've
  847. * take cared of everything.
  848. */
  849. return -EINVAL;
  850. }
  851. ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
  852. ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
  853. ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
  854. ndev->limits.msix_cnt = SNB_MSIX_CNT;
  855. ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
  856. return 0;
  857. }
  858. static int ntb_bwd_setup(struct ntb_device *ndev)
  859. {
  860. int rc;
  861. u32 val;
  862. ndev->hw_type = BWD_HW;
  863. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
  864. if (rc)
  865. return rc;
  866. switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
  867. case NTB_CONN_B2B:
  868. ndev->conn_type = NTB_CONN_B2B;
  869. break;
  870. case NTB_CONN_RP:
  871. default:
  872. dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
  873. return -EINVAL;
  874. }
  875. if (val & BWD_PPD_DEV_TYPE)
  876. ndev->dev_type = NTB_DEV_DSD;
  877. else
  878. ndev->dev_type = NTB_DEV_USD;
  879. /* Initiate PCI-E link training */
  880. rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
  881. val | BWD_PPD_INIT_LINK);
  882. if (rc)
  883. return rc;
  884. ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
  885. ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
  886. ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
  887. ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
  888. ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
  889. ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
  890. ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
  891. ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
  892. ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
  893. ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
  894. ndev->limits.max_mw = BWD_MAX_MW;
  895. ndev->limits.max_spads = BWD_MAX_SPADS;
  896. ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
  897. ndev->limits.msix_cnt = BWD_MSIX_CNT;
  898. ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
  899. /* Since bwd doesn't have a link interrupt, setup a poll timer */
  900. INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
  901. INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
  902. schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
  903. return 0;
  904. }
  905. static int ntb_device_setup(struct ntb_device *ndev)
  906. {
  907. int rc;
  908. if (is_ntb_xeon(ndev))
  909. rc = ntb_xeon_setup(ndev);
  910. else if (is_ntb_atom(ndev))
  911. rc = ntb_bwd_setup(ndev);
  912. else
  913. rc = -ENODEV;
  914. if (rc)
  915. return rc;
  916. if (ndev->conn_type == NTB_CONN_B2B)
  917. /* Enable Bus Master and Memory Space on the secondary side */
  918. writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
  919. ndev->reg_ofs.spci_cmd);
  920. return 0;
  921. }
  922. static void ntb_device_free(struct ntb_device *ndev)
  923. {
  924. if (is_ntb_atom(ndev)) {
  925. cancel_delayed_work_sync(&ndev->hb_timer);
  926. cancel_delayed_work_sync(&ndev->lr_timer);
  927. }
  928. }
  929. static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
  930. {
  931. struct ntb_db_cb *db_cb = data;
  932. struct ntb_device *ndev = db_cb->ndev;
  933. unsigned long mask;
  934. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  935. db_cb->db_num);
  936. mask = readw(ndev->reg_ofs.ldb_mask);
  937. set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  938. writew(mask, ndev->reg_ofs.ldb_mask);
  939. tasklet_schedule(&db_cb->irq_work);
  940. /* No need to check for the specific HB irq, any interrupt means
  941. * we're connected.
  942. */
  943. ndev->last_ts = jiffies;
  944. writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
  945. return IRQ_HANDLED;
  946. }
  947. static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
  948. {
  949. struct ntb_db_cb *db_cb = data;
  950. struct ntb_device *ndev = db_cb->ndev;
  951. unsigned long mask;
  952. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
  953. db_cb->db_num);
  954. mask = readw(ndev->reg_ofs.ldb_mask);
  955. set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
  956. writew(mask, ndev->reg_ofs.ldb_mask);
  957. tasklet_schedule(&db_cb->irq_work);
  958. /* On Sandybridge, there are 16 bits in the interrupt register
  959. * but only 4 vectors. So, 5 bits are assigned to the first 3
  960. * vectors, with the 4th having a single bit for link
  961. * interrupts.
  962. */
  963. writew(((1 << ndev->bits_per_vector) - 1) <<
  964. (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
  965. return IRQ_HANDLED;
  966. }
  967. /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
  968. static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
  969. {
  970. struct ntb_device *ndev = dev;
  971. int rc;
  972. dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
  973. rc = ntb_link_status(ndev);
  974. if (rc)
  975. dev_err(&ndev->pdev->dev, "Error determining link status\n");
  976. /* bit 15 is always the link bit */
  977. writew(1 << SNB_LINK_DB, ndev->reg_ofs.ldb);
  978. return IRQ_HANDLED;
  979. }
  980. static irqreturn_t ntb_interrupt(int irq, void *dev)
  981. {
  982. struct ntb_device *ndev = dev;
  983. unsigned int i = 0;
  984. if (is_ntb_atom(ndev)) {
  985. u64 ldb = readq(ndev->reg_ofs.ldb);
  986. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
  987. while (ldb) {
  988. i = __ffs(ldb);
  989. ldb &= ldb - 1;
  990. bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
  991. }
  992. } else {
  993. u16 ldb = readw(ndev->reg_ofs.ldb);
  994. dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
  995. if (ldb & SNB_DB_HW_LINK) {
  996. xeon_event_msix_irq(irq, dev);
  997. ldb &= ~SNB_DB_HW_LINK;
  998. }
  999. while (ldb) {
  1000. i = __ffs(ldb);
  1001. ldb &= ldb - 1;
  1002. xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
  1003. }
  1004. }
  1005. return IRQ_HANDLED;
  1006. }
  1007. static int ntb_setup_snb_msix(struct ntb_device *ndev, int msix_entries)
  1008. {
  1009. struct pci_dev *pdev = ndev->pdev;
  1010. struct msix_entry *msix;
  1011. int rc, i;
  1012. if (msix_entries < ndev->limits.msix_cnt)
  1013. return -ENOSPC;
  1014. rc = pci_enable_msix_exact(pdev, ndev->msix_entries, msix_entries);
  1015. if (rc < 0)
  1016. return rc;
  1017. for (i = 0; i < msix_entries; i++) {
  1018. msix = &ndev->msix_entries[i];
  1019. WARN_ON(!msix->vector);
  1020. if (i == msix_entries - 1) {
  1021. rc = request_irq(msix->vector,
  1022. xeon_event_msix_irq, 0,
  1023. "ntb-event-msix", ndev);
  1024. if (rc)
  1025. goto err;
  1026. } else {
  1027. rc = request_irq(msix->vector,
  1028. xeon_callback_msix_irq, 0,
  1029. "ntb-callback-msix",
  1030. &ndev->db_cb[i]);
  1031. if (rc)
  1032. goto err;
  1033. }
  1034. }
  1035. ndev->num_msix = msix_entries;
  1036. ndev->max_cbs = msix_entries - 1;
  1037. return 0;
  1038. err:
  1039. while (--i >= 0) {
  1040. /* Code never reaches here for entry nr 'ndev->num_msix - 1' */
  1041. msix = &ndev->msix_entries[i];
  1042. free_irq(msix->vector, &ndev->db_cb[i]);
  1043. }
  1044. pci_disable_msix(pdev);
  1045. ndev->num_msix = 0;
  1046. return rc;
  1047. }
  1048. static int ntb_setup_bwd_msix(struct ntb_device *ndev, int msix_entries)
  1049. {
  1050. struct pci_dev *pdev = ndev->pdev;
  1051. struct msix_entry *msix;
  1052. int rc, i;
  1053. msix_entries = pci_enable_msix_range(pdev, ndev->msix_entries,
  1054. 1, msix_entries);
  1055. if (msix_entries < 0)
  1056. return msix_entries;
  1057. for (i = 0; i < msix_entries; i++) {
  1058. msix = &ndev->msix_entries[i];
  1059. WARN_ON(!msix->vector);
  1060. rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
  1061. "ntb-callback-msix", &ndev->db_cb[i]);
  1062. if (rc)
  1063. goto err;
  1064. }
  1065. ndev->num_msix = msix_entries;
  1066. ndev->max_cbs = msix_entries;
  1067. return 0;
  1068. err:
  1069. while (--i >= 0)
  1070. free_irq(msix->vector, &ndev->db_cb[i]);
  1071. pci_disable_msix(pdev);
  1072. ndev->num_msix = 0;
  1073. return rc;
  1074. }
  1075. static int ntb_setup_msix(struct ntb_device *ndev)
  1076. {
  1077. struct pci_dev *pdev = ndev->pdev;
  1078. int msix_entries;
  1079. int rc, i;
  1080. msix_entries = pci_msix_vec_count(pdev);
  1081. if (msix_entries < 0) {
  1082. rc = msix_entries;
  1083. goto err;
  1084. } else if (msix_entries > ndev->limits.msix_cnt) {
  1085. rc = -EINVAL;
  1086. goto err;
  1087. }
  1088. ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
  1089. GFP_KERNEL);
  1090. if (!ndev->msix_entries) {
  1091. rc = -ENOMEM;
  1092. goto err;
  1093. }
  1094. for (i = 0; i < msix_entries; i++)
  1095. ndev->msix_entries[i].entry = i;
  1096. if (is_ntb_atom(ndev))
  1097. rc = ntb_setup_bwd_msix(ndev, msix_entries);
  1098. else
  1099. rc = ntb_setup_snb_msix(ndev, msix_entries);
  1100. if (rc)
  1101. goto err1;
  1102. return 0;
  1103. err1:
  1104. kfree(ndev->msix_entries);
  1105. err:
  1106. dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
  1107. return rc;
  1108. }
  1109. static int ntb_setup_msi(struct ntb_device *ndev)
  1110. {
  1111. struct pci_dev *pdev = ndev->pdev;
  1112. int rc;
  1113. rc = pci_enable_msi(pdev);
  1114. if (rc)
  1115. return rc;
  1116. rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
  1117. if (rc) {
  1118. pci_disable_msi(pdev);
  1119. dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
  1120. return rc;
  1121. }
  1122. return 0;
  1123. }
  1124. static int ntb_setup_intx(struct ntb_device *ndev)
  1125. {
  1126. struct pci_dev *pdev = ndev->pdev;
  1127. int rc;
  1128. pci_msi_off(pdev);
  1129. /* Verify intx is enabled */
  1130. pci_intx(pdev, 1);
  1131. rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
  1132. ndev);
  1133. if (rc)
  1134. return rc;
  1135. return 0;
  1136. }
  1137. static int ntb_setup_interrupts(struct ntb_device *ndev)
  1138. {
  1139. int rc;
  1140. /* On BWD, disable all interrupts. On SNB, disable all but Link
  1141. * Interrupt. The rest will be unmasked as callbacks are registered.
  1142. */
  1143. if (is_ntb_atom(ndev))
  1144. writeq(~0, ndev->reg_ofs.ldb_mask);
  1145. else {
  1146. u16 var = 1 << SNB_LINK_DB;
  1147. writew(~var, ndev->reg_ofs.ldb_mask);
  1148. }
  1149. rc = ntb_setup_msix(ndev);
  1150. if (!rc)
  1151. goto done;
  1152. ndev->bits_per_vector = 1;
  1153. ndev->max_cbs = ndev->limits.max_db_bits;
  1154. rc = ntb_setup_msi(ndev);
  1155. if (!rc)
  1156. goto done;
  1157. rc = ntb_setup_intx(ndev);
  1158. if (rc) {
  1159. dev_err(&ndev->pdev->dev, "no usable interrupts\n");
  1160. return rc;
  1161. }
  1162. done:
  1163. return 0;
  1164. }
  1165. static void ntb_free_interrupts(struct ntb_device *ndev)
  1166. {
  1167. struct pci_dev *pdev = ndev->pdev;
  1168. /* mask interrupts */
  1169. if (is_ntb_atom(ndev))
  1170. writeq(~0, ndev->reg_ofs.ldb_mask);
  1171. else
  1172. writew(~0, ndev->reg_ofs.ldb_mask);
  1173. if (ndev->num_msix) {
  1174. struct msix_entry *msix;
  1175. u32 i;
  1176. for (i = 0; i < ndev->num_msix; i++) {
  1177. msix = &ndev->msix_entries[i];
  1178. if (is_ntb_xeon(ndev) && i == ndev->num_msix - 1)
  1179. free_irq(msix->vector, ndev);
  1180. else
  1181. free_irq(msix->vector, &ndev->db_cb[i]);
  1182. }
  1183. pci_disable_msix(pdev);
  1184. kfree(ndev->msix_entries);
  1185. } else {
  1186. free_irq(pdev->irq, ndev);
  1187. if (pci_dev_msi_enabled(pdev))
  1188. pci_disable_msi(pdev);
  1189. }
  1190. }
  1191. static int ntb_create_callbacks(struct ntb_device *ndev)
  1192. {
  1193. int i;
  1194. /* Chicken-egg issue. We won't know how many callbacks are necessary
  1195. * until we see how many MSI-X vectors we get, but these pointers need
  1196. * to be passed into the MSI-X register function. So, we allocate the
  1197. * max, knowing that they might not all be used, to work around this.
  1198. */
  1199. ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
  1200. sizeof(struct ntb_db_cb),
  1201. GFP_KERNEL);
  1202. if (!ndev->db_cb)
  1203. return -ENOMEM;
  1204. for (i = 0; i < ndev->limits.max_db_bits; i++) {
  1205. ndev->db_cb[i].db_num = i;
  1206. ndev->db_cb[i].ndev = ndev;
  1207. }
  1208. return 0;
  1209. }
  1210. static void ntb_free_callbacks(struct ntb_device *ndev)
  1211. {
  1212. int i;
  1213. for (i = 0; i < ndev->limits.max_db_bits; i++)
  1214. ntb_unregister_db_callback(ndev, i);
  1215. kfree(ndev->db_cb);
  1216. }
  1217. static ssize_t ntb_debugfs_read(struct file *filp, char __user *ubuf,
  1218. size_t count, loff_t *offp)
  1219. {
  1220. struct ntb_device *ndev;
  1221. char *buf;
  1222. ssize_t ret, offset, out_count;
  1223. out_count = 500;
  1224. buf = kmalloc(out_count, GFP_KERNEL);
  1225. if (!buf)
  1226. return -ENOMEM;
  1227. ndev = filp->private_data;
  1228. offset = 0;
  1229. offset += snprintf(buf + offset, out_count - offset,
  1230. "NTB Device Information:\n");
  1231. offset += snprintf(buf + offset, out_count - offset,
  1232. "Connection Type - \t\t%s\n",
  1233. ndev->conn_type == NTB_CONN_TRANSPARENT ?
  1234. "Transparent" : (ndev->conn_type == NTB_CONN_B2B) ?
  1235. "Back to back" : "Root Port");
  1236. offset += snprintf(buf + offset, out_count - offset,
  1237. "Device Type - \t\t\t%s\n",
  1238. ndev->dev_type == NTB_DEV_USD ?
  1239. "DSD/USP" : "USD/DSP");
  1240. offset += snprintf(buf + offset, out_count - offset,
  1241. "Max Number of Callbacks - \t%u\n",
  1242. ntb_max_cbs(ndev));
  1243. offset += snprintf(buf + offset, out_count - offset,
  1244. "Link Status - \t\t\t%s\n",
  1245. ntb_hw_link_status(ndev) ? "Up" : "Down");
  1246. if (ntb_hw_link_status(ndev)) {
  1247. offset += snprintf(buf + offset, out_count - offset,
  1248. "Link Speed - \t\t\tPCI-E Gen %u\n",
  1249. ndev->link_speed);
  1250. offset += snprintf(buf + offset, out_count - offset,
  1251. "Link Width - \t\t\tx%u\n",
  1252. ndev->link_width);
  1253. }
  1254. if (is_ntb_xeon(ndev)) {
  1255. u32 status32;
  1256. u16 status16;
  1257. int rc;
  1258. offset += snprintf(buf + offset, out_count - offset,
  1259. "\nNTB Device Statistics:\n");
  1260. offset += snprintf(buf + offset, out_count - offset,
  1261. "Upstream Memory Miss - \t%u\n",
  1262. readw(ndev->reg_base +
  1263. SNB_USMEMMISS_OFFSET));
  1264. offset += snprintf(buf + offset, out_count - offset,
  1265. "\nNTB Hardware Errors:\n");
  1266. rc = pci_read_config_word(ndev->pdev, SNB_DEVSTS_OFFSET,
  1267. &status16);
  1268. if (!rc)
  1269. offset += snprintf(buf + offset, out_count - offset,
  1270. "DEVSTS - \t%#06x\n", status16);
  1271. rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
  1272. &status16);
  1273. if (!rc)
  1274. offset += snprintf(buf + offset, out_count - offset,
  1275. "LNKSTS - \t%#06x\n", status16);
  1276. rc = pci_read_config_dword(ndev->pdev, SNB_UNCERRSTS_OFFSET,
  1277. &status32);
  1278. if (!rc)
  1279. offset += snprintf(buf + offset, out_count - offset,
  1280. "UNCERRSTS - \t%#010x\n", status32);
  1281. rc = pci_read_config_dword(ndev->pdev, SNB_CORERRSTS_OFFSET,
  1282. &status32);
  1283. if (!rc)
  1284. offset += snprintf(buf + offset, out_count - offset,
  1285. "CORERRSTS - \t%#010x\n", status32);
  1286. }
  1287. if (offset > out_count)
  1288. offset = out_count;
  1289. ret = simple_read_from_buffer(ubuf, count, offp, buf, offset);
  1290. kfree(buf);
  1291. return ret;
  1292. }
  1293. static const struct file_operations ntb_debugfs_info = {
  1294. .owner = THIS_MODULE,
  1295. .open = simple_open,
  1296. .read = ntb_debugfs_read,
  1297. };
  1298. static void ntb_setup_debugfs(struct ntb_device *ndev)
  1299. {
  1300. if (!debugfs_initialized())
  1301. return;
  1302. if (!debugfs_dir)
  1303. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1304. ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
  1305. debugfs_dir);
  1306. if (ndev->debugfs_dir)
  1307. ndev->debugfs_info = debugfs_create_file("info", S_IRUSR,
  1308. ndev->debugfs_dir,
  1309. ndev,
  1310. &ntb_debugfs_info);
  1311. }
  1312. static void ntb_free_debugfs(struct ntb_device *ndev)
  1313. {
  1314. debugfs_remove_recursive(ndev->debugfs_dir);
  1315. if (debugfs_dir && simple_empty(debugfs_dir)) {
  1316. debugfs_remove_recursive(debugfs_dir);
  1317. debugfs_dir = NULL;
  1318. }
  1319. }
  1320. static void ntb_hw_link_up(struct ntb_device *ndev)
  1321. {
  1322. if (ndev->conn_type == NTB_CONN_TRANSPARENT)
  1323. ntb_link_event(ndev, NTB_LINK_UP);
  1324. else {
  1325. u32 ntb_cntl;
  1326. /* Let's bring the NTB link up */
  1327. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1328. ntb_cntl &= ~(NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK);
  1329. ntb_cntl |= NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP;
  1330. ntb_cntl |= NTB_CNTL_P2S_BAR4_SNOOP | NTB_CNTL_S2P_BAR4_SNOOP;
  1331. if (ndev->split_bar)
  1332. ntb_cntl |= NTB_CNTL_P2S_BAR5_SNOOP |
  1333. NTB_CNTL_S2P_BAR5_SNOOP;
  1334. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1335. }
  1336. }
  1337. static void ntb_hw_link_down(struct ntb_device *ndev)
  1338. {
  1339. u32 ntb_cntl;
  1340. if (ndev->conn_type == NTB_CONN_TRANSPARENT) {
  1341. ntb_link_event(ndev, NTB_LINK_DOWN);
  1342. return;
  1343. }
  1344. /* Bring NTB link down */
  1345. ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
  1346. ntb_cntl &= ~(NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP);
  1347. ntb_cntl &= ~(NTB_CNTL_P2S_BAR4_SNOOP | NTB_CNTL_S2P_BAR4_SNOOP);
  1348. if (ndev->split_bar)
  1349. ntb_cntl &= ~(NTB_CNTL_P2S_BAR5_SNOOP |
  1350. NTB_CNTL_S2P_BAR5_SNOOP);
  1351. ntb_cntl |= NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK;
  1352. writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
  1353. }
  1354. static void ntb_max_mw_detect(struct ntb_device *ndev)
  1355. {
  1356. if (ndev->split_bar)
  1357. ndev->limits.max_mw = HSX_SPLITBAR_MAX_MW;
  1358. else
  1359. ndev->limits.max_mw = SNB_MAX_MW;
  1360. }
  1361. static int ntb_xeon_detect(struct ntb_device *ndev)
  1362. {
  1363. int rc, bars_mask;
  1364. u32 bars;
  1365. u8 ppd;
  1366. ndev->hw_type = SNB_HW;
  1367. rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &ppd);
  1368. if (rc)
  1369. return -EIO;
  1370. if (ppd & SNB_PPD_DEV_TYPE)
  1371. ndev->dev_type = NTB_DEV_USD;
  1372. else
  1373. ndev->dev_type = NTB_DEV_DSD;
  1374. ndev->split_bar = (ppd & SNB_PPD_SPLIT_BAR) ? 1 : 0;
  1375. switch (ppd & SNB_PPD_CONN_TYPE) {
  1376. case NTB_CONN_B2B:
  1377. dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
  1378. ndev->conn_type = NTB_CONN_B2B;
  1379. break;
  1380. case NTB_CONN_RP:
  1381. dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
  1382. ndev->conn_type = NTB_CONN_RP;
  1383. break;
  1384. case NTB_CONN_TRANSPARENT:
  1385. dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
  1386. ndev->conn_type = NTB_CONN_TRANSPARENT;
  1387. /*
  1388. * This mode is default to USD/DSP. HW does not report
  1389. * properly in transparent mode as it has no knowledge of
  1390. * NTB. We will just force correct here.
  1391. */
  1392. ndev->dev_type = NTB_DEV_USD;
  1393. /*
  1394. * This is a way for transparent BAR to figure out if we
  1395. * are doing split BAR or not. There is no way for the hw
  1396. * on the transparent side to know and set the PPD.
  1397. */
  1398. bars_mask = pci_select_bars(ndev->pdev, IORESOURCE_MEM);
  1399. bars = hweight32(bars_mask);
  1400. if (bars == (HSX_SPLITBAR_MAX_MW + 1))
  1401. ndev->split_bar = 1;
  1402. break;
  1403. default:
  1404. dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", ppd);
  1405. return -ENODEV;
  1406. }
  1407. ntb_max_mw_detect(ndev);
  1408. return 0;
  1409. }
  1410. static int ntb_atom_detect(struct ntb_device *ndev)
  1411. {
  1412. int rc;
  1413. u32 ppd;
  1414. ndev->hw_type = BWD_HW;
  1415. rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &ppd);
  1416. if (rc)
  1417. return rc;
  1418. switch ((ppd & BWD_PPD_CONN_TYPE) >> 8) {
  1419. case NTB_CONN_B2B:
  1420. dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
  1421. ndev->conn_type = NTB_CONN_B2B;
  1422. break;
  1423. case NTB_CONN_RP:
  1424. default:
  1425. dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
  1426. return -EINVAL;
  1427. }
  1428. if (ppd & BWD_PPD_DEV_TYPE)
  1429. ndev->dev_type = NTB_DEV_DSD;
  1430. else
  1431. ndev->dev_type = NTB_DEV_USD;
  1432. return 0;
  1433. }
  1434. static int ntb_device_detect(struct ntb_device *ndev)
  1435. {
  1436. int rc;
  1437. if (is_ntb_xeon(ndev))
  1438. rc = ntb_xeon_detect(ndev);
  1439. else if (is_ntb_atom(ndev))
  1440. rc = ntb_atom_detect(ndev);
  1441. else
  1442. rc = -ENODEV;
  1443. dev_info(&ndev->pdev->dev, "Device Type = %s\n",
  1444. ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
  1445. return 0;
  1446. }
  1447. static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1448. {
  1449. struct ntb_device *ndev;
  1450. int rc, i;
  1451. ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
  1452. if (!ndev)
  1453. return -ENOMEM;
  1454. ndev->pdev = pdev;
  1455. ntb_set_errata_flags(ndev);
  1456. ndev->link_status = NTB_LINK_DOWN;
  1457. pci_set_drvdata(pdev, ndev);
  1458. ntb_setup_debugfs(ndev);
  1459. rc = pci_enable_device(pdev);
  1460. if (rc)
  1461. goto err;
  1462. pci_set_master(ndev->pdev);
  1463. rc = ntb_device_detect(ndev);
  1464. if (rc)
  1465. goto err;
  1466. ndev->mw = kcalloc(ndev->limits.max_mw, sizeof(struct ntb_mw),
  1467. GFP_KERNEL);
  1468. if (!ndev->mw) {
  1469. rc = -ENOMEM;
  1470. goto err1;
  1471. }
  1472. if (ndev->split_bar)
  1473. rc = pci_request_selected_regions(pdev, NTB_SPLITBAR_MASK,
  1474. KBUILD_MODNAME);
  1475. else
  1476. rc = pci_request_selected_regions(pdev, NTB_BAR_MASK,
  1477. KBUILD_MODNAME);
  1478. if (rc)
  1479. goto err2;
  1480. ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
  1481. if (!ndev->reg_base) {
  1482. dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
  1483. rc = -EIO;
  1484. goto err3;
  1485. }
  1486. for (i = 0; i < ndev->limits.max_mw; i++) {
  1487. ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
  1488. /*
  1489. * with the errata we need to steal last of the memory
  1490. * windows for workarounds and they point to MMIO registers.
  1491. */
  1492. if ((ndev->wa_flags & WA_SNB_ERR) &&
  1493. (i == (ndev->limits.max_mw - 1))) {
  1494. ndev->mw[i].vbase =
  1495. ioremap_nocache(pci_resource_start(pdev,
  1496. MW_TO_BAR(i)),
  1497. ndev->mw[i].bar_sz);
  1498. } else {
  1499. ndev->mw[i].vbase =
  1500. ioremap_wc(pci_resource_start(pdev,
  1501. MW_TO_BAR(i)),
  1502. ndev->mw[i].bar_sz);
  1503. }
  1504. dev_info(&pdev->dev, "MW %d size %llu\n", i,
  1505. (unsigned long long) ndev->mw[i].bar_sz);
  1506. if (!ndev->mw[i].vbase) {
  1507. dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
  1508. MW_TO_BAR(i));
  1509. rc = -EIO;
  1510. goto err3;
  1511. }
  1512. }
  1513. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1514. if (rc) {
  1515. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1516. if (rc)
  1517. goto err4;
  1518. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  1519. }
  1520. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1521. if (rc) {
  1522. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1523. if (rc)
  1524. goto err4;
  1525. dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
  1526. }
  1527. rc = ntb_device_setup(ndev);
  1528. if (rc)
  1529. goto err4;
  1530. rc = ntb_create_callbacks(ndev);
  1531. if (rc)
  1532. goto err5;
  1533. rc = ntb_setup_interrupts(ndev);
  1534. if (rc)
  1535. goto err6;
  1536. /* The scratchpad registers keep the values between rmmod/insmod,
  1537. * blast them now
  1538. */
  1539. for (i = 0; i < ndev->limits.max_spads; i++) {
  1540. ntb_write_local_spad(ndev, i, 0);
  1541. ntb_write_remote_spad(ndev, i, 0);
  1542. }
  1543. rc = ntb_transport_init(pdev);
  1544. if (rc)
  1545. goto err7;
  1546. ntb_hw_link_up(ndev);
  1547. return 0;
  1548. err7:
  1549. ntb_free_interrupts(ndev);
  1550. err6:
  1551. ntb_free_callbacks(ndev);
  1552. err5:
  1553. ntb_device_free(ndev);
  1554. err4:
  1555. for (i--; i >= 0; i--)
  1556. iounmap(ndev->mw[i].vbase);
  1557. iounmap(ndev->reg_base);
  1558. err3:
  1559. if (ndev->split_bar)
  1560. pci_release_selected_regions(pdev, NTB_SPLITBAR_MASK);
  1561. else
  1562. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1563. err2:
  1564. kfree(ndev->mw);
  1565. err1:
  1566. pci_disable_device(pdev);
  1567. err:
  1568. ntb_free_debugfs(ndev);
  1569. kfree(ndev);
  1570. dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
  1571. return rc;
  1572. }
  1573. static void ntb_pci_remove(struct pci_dev *pdev)
  1574. {
  1575. struct ntb_device *ndev = pci_get_drvdata(pdev);
  1576. int i;
  1577. ntb_hw_link_down(ndev);
  1578. ntb_transport_free(ndev->ntb_transport);
  1579. ntb_free_interrupts(ndev);
  1580. ntb_free_callbacks(ndev);
  1581. ntb_device_free(ndev);
  1582. /* need to reset max_mw limits so we can unmap properly */
  1583. if (ndev->hw_type == SNB_HW)
  1584. ntb_max_mw_detect(ndev);
  1585. for (i = 0; i < ndev->limits.max_mw; i++)
  1586. iounmap(ndev->mw[i].vbase);
  1587. kfree(ndev->mw);
  1588. iounmap(ndev->reg_base);
  1589. if (ndev->split_bar)
  1590. pci_release_selected_regions(pdev, NTB_SPLITBAR_MASK);
  1591. else
  1592. pci_release_selected_regions(pdev, NTB_BAR_MASK);
  1593. pci_disable_device(pdev);
  1594. ntb_free_debugfs(ndev);
  1595. kfree(ndev);
  1596. }
  1597. static struct pci_driver ntb_pci_driver = {
  1598. .name = KBUILD_MODNAME,
  1599. .id_table = ntb_pci_tbl,
  1600. .probe = ntb_pci_probe,
  1601. .remove = ntb_pci_remove,
  1602. };
  1603. module_pci_driver(ntb_pci_driver);